4db9bebede
When we encounter a directory in the argument list, we don't want to walk every argument, just that directory.
38 lines
730 B
Bash
Executable file
38 lines
730 B
Bash
Executable file
#!/bin/rc
|
|
rfork e
|
|
nl='
|
|
'
|
|
flags=()
|
|
recurse=()
|
|
files=()
|
|
while(! ~ $#* 1 && ~ $1 -* && ! ~ $1 --){
|
|
if(~ $1 '-n')
|
|
recurse=-n1
|
|
if not
|
|
flags=($flags $1);
|
|
shift
|
|
}
|
|
if(~ $1 --)
|
|
shift
|
|
|
|
suffixes='\.([bcChlmsy]|asm|awk|cc|cgi|cpp|cs|go|goc|hs|java|lua|lx|mk|ml|mli|ms|myr|pl|py|rc|sh|tex|xy)$'
|
|
fullnames='(^|/)mkfile$'
|
|
switch($#*){
|
|
case 0
|
|
echo 'usage: g [flags] pattern [files]' >[1=2]
|
|
exit usage
|
|
case 1
|
|
pattern=$1
|
|
files=`$nl{walk -f $recurse | grep -e $fullnames -e $suffixes >[2]/dev/null}
|
|
case *
|
|
pattern=$1
|
|
shift
|
|
for(f in $*){
|
|
if(test -d $f)
|
|
files=($files `$nl{walk -f $recurse -- $f \
|
|
| grep -e $fullnames -e $suffixes >[2]/dev/null})
|
|
if not
|
|
files=($files $f)
|
|
}
|
|
}
|
|
grep -n $flags -- $pattern $files /dev/null
|