]> git.ipfire.org Git - thirdparty/man-pages.git/commitdiff
Don't find files recursively
authorAlejandro Colomar <alx@kernel.org>
Fri, 3 Nov 2023 14:08:49 +0000 (15:08 +0100)
committerAlejandro Colomar <alx@kernel.org>
Wed, 29 Oct 2025 20:29:16 +0000 (21:29 +0100)
grep only one file (or stdin).

The current implementation doesn't allow filterin standard input, which
I miss some times.  I'm removing this feature now, to be able to rewrite
most of the program in a way that allows filtering stdin, and then will
consider adding back a recursive mode if necessary.

Also, don't remove the two blanks at the start of the output, for
consistency when calling this program via xargs(1), which will become
more common now that it only handles single files.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
bin/grepc
share/man/man1/grepc.1

index e0dd5cf05e68de7308e72f344054840fa9452598..05eeee46fb1dd7f87823cb36c1882f937217878a 100755 (executable)
--- a/bin/grepc
+++ b/bin/grepc
@@ -2,7 +2,7 @@
 
 
 # Defaults:
-FILES='.';
+file='/dev/stdin';
 iflag='';
 lflag='';
 kflag='no';
@@ -158,9 +158,12 @@ grepc_parse_cmd()
        shift;
 
        if [ $# -gt 0 ]; then
-               FILES=$*;
+               file="$1";
+               shift;
        fi;
 
+       test $# -gt 1 && grepc_err "Trailing unknown arguments.";
+
        if [ "$tflag" = 'no' ]; then
                t_e='yes';
                t_fp='yes';
@@ -180,23 +183,12 @@ grepc_parse_cmd()
 }
 
 
-grepc_find_files()
-{
-       find $FILES -type f \
-       | xargs grep -${iflag}lPI -- "$1" \
-       | tee "$files_use" \
-       | xargs grep -${iflag}lP -- "$1\b" \
-       > "$files";
-}
-
-
 grepc_helper()
 {
-       <"$files" \
-       xargs grep -${iflag}lP -- "$1" \
-       | xargs grep -${iflag}lP -- "$2" \
-       | sort \
-       | xargs pcre2grep -${iflag}${lflag}Mn -- "$3" /dev/null \
+       grep -${iflag}zP -- "$1" \
+       | grep -${iflag}zP -- "$2" \
+       | head -c-1 \
+       | pcre2grep -${iflag}${lflag}HMn --label="$file" -- "$3" \
        | if [ "$kflag" = 'no' ]; then
                sed -E 's/^[^: ]+:[0-9]+:/\n\n&\n/';
        else
@@ -207,11 +199,10 @@ grepc_helper()
 
 grepc_helper_use()
 {
-       <"$files_use" \
-       xargs grep -${iflag}lP -- "$1" \
-       | xargs grep -${iflag}lP -- "$2" \
-       | sort \
-       | xargs pcre2grep -${iflag}${lflag}Mn -- "$3" /dev/null \
+       grep -${iflag}zP -- "$1" \
+       | grep -${iflag}zP -- "$2" \
+       | head -c-1 \
+       | pcre2grep -${iflag}${lflag}HMn --label="$file" -- "$3" \
        | if [ "$kflag" = 'no' ]; then
                sed -E 's/^[^: ]+:[0-9]+:/\n\n&\n/';
        else
@@ -398,43 +389,62 @@ grepc_ut_td_su()
 
 grepc_search()
 {
-       test "$t_e" = 'yes' && grepc_e "$1";
-       test "$t_fp" = 'yes' && grepc_fp "$1";
-       test "$t_fd" = 'yes' && grepc_fd "$1";
-       test "$t_fsp" = 'yes' && grepc_fsp "$1";
-       test "$t_fsd" = 'yes' && grepc_fsd "$1";
-       test "$t_fgp" = 'yes' && grepc_fgp "$1";
-       test "$t_fgd_libm" = 'yes' && grepc_fgd_libm "$1";
-       test "$t_fgd_libio" = 'yes' && grepc_fgd_libio "$1";
-       test "$t_mf" = 'yes' && grepc_mf "$1";
-       test "$t_mo" = 'yes' && grepc_mo "$1";
-       test "$t_t_braced" = 'yes' && grepc_t_braced "$1";
-       test "$t_t_td_simple" = 'yes' && grepc_t_td_simple "$1";
-       test "$t_t_td_braced" = 'yes' && grepc_t_td_braced "$1";
-       test "$t_t_td_func" = 'yes' && grepc_t_td_func "$1";
-       test "$t_ue" = 'yes' && grepc_ue "$1";
-       test "$t_uf_def" = 'yes' && grepc_uf_def "$1";
-       test "$t_uf_linux_def" = 'yes' && grepc_uf_linux_def "$1";
-       test "$t_um" = 'yes' && grepc_um "$1";
-       test "$t_ut_su" = 'yes' && grepc_ut_su "$1";
-       test "$t_ut_td_simple" = 'yes' && grepc_ut_td_simple "$1";
-       test "$t_ut_td_su" = 'yes' && grepc_ut_td_su "$1";
+       local t="";
+       local f="$(mktemp -u -t grepc.XXXXXX)";
+       local fi="";
+       local fo="";
+
+       t="$t e";
+       t="$t fp fd fsp fsd fgp fgd_libm fgd_libio";
+       t="$t mf mo";
+       t="$t t_braced t_td_simple t_td_braced t_td_func";
+       t="$t ue uf_def uf_linux_def um ut_su ut_td_simple ut_td_su";
+
+       for ti in $t; do
+               fi="$fi $f.$ti.i";
+               fo="$fo $f.$ti.o";
+       done;
+
+       mkfifo -m600 $fi $fo;
+       cat $fo &
+
+       if test $t_e = yes; then grepc_e "$1"; else cat >/dev/null & printf ''; fi <$f.e.i >$f.e.o &
+       if test $t_fp = yes; then grepc_fp "$1"; else cat >/dev/null & printf ''; fi <$f.fp.i >$f.fp.o &
+       if test $t_fd = yes; then grepc_fd "$1"; else cat >/dev/null & printf ''; fi <$f.fd.i >$f.fd.o &
+       if test $t_fsp = yes; then grepc_fsp "$1"; else cat >/dev/null & printf ''; fi <$f.fsp.i >$f.fsp.o &
+       if test $t_fsd = yes; then grepc_fsd "$1"; else cat >/dev/null & printf ''; fi <$f.fsd.i >$f.fsd.o &
+       if test $t_fgp = yes; then grepc_fgp "$1"; else cat >/dev/null & printf ''; fi <$f.fgp.i >$f.fgp.o &
+       if test $t_fgd_libm = yes; then grepc_fgd_libm "$1"; else cat >/dev/null & printf ''; fi <$f.fgd_libm.i >$f.fgd_libm.o &
+       if test $t_fgd_libio = yes; then grepc_fgd_libio "$1"; else cat >/dev/null & printf ''; fi <$f.fgd_libio.i >$f.fgd_libio.o &
+       if test $t_mf = yes; then grepc_mf "$1"; else cat >/dev/null & printf ''; fi <$f.mf.i >$f.mf.o &
+       if test $t_mo = yes; then grepc_mo "$1"; else cat >/dev/null & printf ''; fi <$f.mo.i >$f.mo.o &
+       if test $t_t_braced = yes; then grepc_t_braced "$1"; else cat >/dev/null & printf ''; fi <$f.t_braced.i >$f.t_braced.o &
+       if test $t_t_td_simple = yes; then grepc_t_td_simple "$1"; else cat >/dev/null & printf ''; fi <$f.t_td_simple.i >$f.t_td_simple.o &
+       if test $t_t_td_braced = yes; then grepc_t_td_braced "$1"; else cat >/dev/null & printf ''; fi <$f.t_td_braced.i >$f.t_td_braced.o &
+       if test $t_t_td_func = yes; then grepc_t_td_func "$1"; else cat >/dev/null & printf ''; fi <$f.t_td_func.i >$f.t_td_func.o &
+       if test $t_ue = yes; then grepc_ue "$1"; else cat >/dev/null & printf ''; fi <$f.ue.i >$f.ue.o &
+       if test $t_uf_def = yes; then grepc_uf_def "$1"; else cat >/dev/null & printf ''; fi <$f.uf_def.i >$f.uf_def.o &
+       if test $t_uf_linux_def = yes; then grepc_uf_linux_def "$1"; else cat >/dev/null & printf ''; fi <$f.uf_linux_def.i >$f.uf_linux_def.o &
+       if test $t_um = yes; then grepc_um "$1"; else cat >/dev/null & printf ''; fi <$f.um.i >$f.um.o &
+       if test $t_ut_su = yes; then grepc_ut_su "$1"; else cat >/dev/null & printf ''; fi <$f.ut_su.i >$f.ut_su.o &
+       if test $t_ut_td_simple = yes; then grepc_ut_td_simple "$1"; else cat >/dev/null & printf ''; fi <$f.ut_td_simple.i >$f.ut_td_simple.o &
+       if test $t_ut_td_su = yes; then grepc_ut_td_su "$1"; else cat >/dev/null & printf ''; fi <$f.ut_td_su.i >$f.ut_td_su.o &
+
+       tee $fi >/dev/null;
+       wait;
+       rm $fi $fo;
 }
 
 
 main()
 {
-       files_use="$(mktemp -t 'grepc.XXXXXX')";
-       files="$(mktemp -t 'grepc.XXXXXX')";
-
-       grepc_parse_cmd "$@";
-       grepc_find_files "$identifier";
-       grepc_search "$identifier" \
+       grepc_parse_cmd "$@" </dev/null;
+       grepc_search "$identifier" <"$file" \
        | if [ -n "$lflag" ]; then
                sort \
                | uniq;
        else
-               sed -n '/./,$p';
+               cat;
        fi;
 }
 
index 2d9e8ddfe0324baead86e0b5512268c644bab378..2d9cde5c82de4045beffb6004ab76aa4d701f295 100644 (file)
@@ -5,7 +5,7 @@ grepc \- find C declarations, definitions, and uses in source code
 .B grepc
 .RI [ option\~ .\|.\|.\&]
 .I pattern
-.RI [ file\~ .\|.\|.]
+.RI [ file ]
 .SH DESCRIPTION
 .MR grepc 1
 searches for C declarations, definitions, and/or uses of
@@ -18,14 +18,10 @@ is a PCRE pattern.
 It normally represents a C identifier,
 and word boundaries are implicitly added to it in most cases.
 .PP
-If
-.I file
-is a directory,
-the search is recursive within that directory.
 If no
 .I file
 is given,
-the working directory is searched.
+the program reads the standard input.
 .SS Types of code
 This program can search for several types of code.
 The following arguments can be passed to the
@@ -158,6 +154,8 @@ Default:
 .SH EXAMPLES
 .EX
 .RB \(ti/src/nginx/unit$ " grepc nxt_sprintf;"
+\&
+\&
 \&./src/nxt_sprintf.h:15:
 NXT_EXPORT u_char *nxt_cdecl nxt_sprintf(u_char *buf, u_char *end,
     const char *fmt, ...);