plan9fox/rc/bin/g

39 lines
738 B
Text
Raw Normal View History

2019-06-05 05:47:07 +00:00
#!/bin/rc
rfork e
2019-06-05 05:36:32 +00:00
nl='
'
flags=()
recurse=()
2020-12-19 12:33:11 +00:00
files=()
2019-06-05 05:36:32 +00:00
while(! ~ $#* 1 && ~ $1 -* && ! ~ $1 --){
if(~ $1 '-n')
recurse=-n1
if not
flags=($flags $1);
2019-06-05 05:47:07 +00:00
shift
}
if(~ $1 --)
shift
if(~ $#* 0) {
echo 'usage: g [flags] pattern [files]' >[1=2]
exit usage
}
pattern=$1
shift
2011-11-21 16:39:34 +00:00
2022-05-15 08:21:34 +00:00
suffixes='\.([bcChlmsy]|asm|awk|cc|cgi|cpp|cs|go|goc|ha|hs|java|lua|lx|mk|ml|mli|ms|myr|pl|py|rc|sh|tex|xy)$'
fullnames='(^|/)mkfile$'
repodirs='(^|/)(.git|.hg)($|/)'
2019-06-05 05:47:07 +00:00
switch($#*){
case 0
walk -f $recurse | grep -e $fullnames -e $suffixes >[2]/dev/null
2019-06-05 05:47:07 +00:00
case *
for(f in $*){
if(test -d $f)
walk -f $recurse -- $f \
| grep -e $fullnames -e $suffixes >[2]/dev/null
if not
echo $f
}
} | grep -v $repodirs | xargs grep -n $flags -- $pattern /dev/null