shithub: riscv

ref: 2968225196d6078203e88d26572a51216f545c1d
dir: /rc/bin/g/

View raw version
#!/bin/rc
rfork e
nl='
'
flags=()
recurse=()
while(! ~ $#* 1 && ~ $1 -* && ! ~ $1 --){
	if(~ $1 '-n')
		recurse=-n1
	if not
		flags=($flags $1);
	shift
}
if(~ $1 --)
	shift

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 -- $*}
}
grep -n $flags -- $pattern $files /dev/null