From 97008caa4174cee3dd849d9962c529897717e333 Mon Sep 17 00:00:00 2001 From: Ori Bernstein Date: Sat, 21 Nov 2020 21:23:46 -0800 Subject: [PATCH] g: filter directory arguments When searching directories recursively, it's still desirable to filter the contents by the file pattern, so that 'g foo /sys/src' doesn't end up searching for foo within .$O files. Files passed explicitly are still searched, so for the old behavior, just use walk: g foo `{walk -f $dir} --- rc/bin/g | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/rc/bin/g b/rc/bin/g index d400b1c93..8bfb1135d 100755 --- a/rc/bin/g +++ b/rc/bin/g @@ -14,18 +14,24 @@ while(! ~ $#* 1 && ~ $1 -* && ! ~ $1 --){ if(~ $1 --) shift +suffixes='\.([bcChlmsy]|asm|awk|cc|cgi|cpp|cs|go|goc|hs|java|lx|ms|pl|py|rc|tex|xy)$' +fullnames='(^|/)mkfile$' switch($#*){ case 0 echo 'usage: g [flags] pattern [files]' >[1=2] exit usage case 1 pattern=$1 - fullnames='mkfile$' - suffixes='\.([bcChlmsy]|asm|awk|cc|cgi|cpp|cs|go|goc|hs|java|lx|ms|pl|py|rc|tex|xy)$' files=`$nl{walk -f $recurse | grep -e $fullnames -e $suffixes >[2]/dev/null} case * pattern=$1 shift - files=`$nl{walk -f $recurse -- $*} + for(f in $*){ + if(test -d $f) + files=($files `$nl{walk -f $recurse -- $* \ + | grep -e $fullnames -e $suffixes >[2]/dev/null}) + if not + files=$(files $f) + } } grep -n $flags -- $pattern $files /dev/null