shithub: bin.rc

ref: 32dae5e4a11c5d7876ce45d221930f84d37e83e2
dir: /bin/c/

View raw version
#!/bin/rc
rfork e

fn isatty {grep -s '/dev/cons' /fd/0ctl}

fn struct {
	ignore = (/sys/include/ape)
	search = $*
	if(~ $#search 0)
		search = (.)
	if(~ $search *^$ignore^*)
		ignore = (cat)
	if not
		ignore = (grep -v -e^'^'$ignore)

	files = `{walk -f $search | $ignore | grep '\.[chsyl]$'}

	echo 'X ,x@^(typedef[ 	\n]+)?struct([^}][^;]*\n*)+}.*;\n@ g@^(typedef[ 	\n]+)?struct[ 	]+'$regex'@ {
		!echo '•' $%:$%dot(1)
		p
	}' | sam -d $files >[2]/dev/null
}

fn enum {
	search = $*
	if(~ $#search 0)
		search = (.)
	files = `{walk -f $search | grep '\.[ch]$'}
	awk '
	/^enum[ 	]*{/ {
		matched = 1
		if(nfound++ > 0) printf "\n"
		if(ARGC != 1)
			print FILENAME ":" FNR
	}
	matched         {print}
	matched && /^}/ {matched = 0}
	' $files
}

fn function {
	ignore = ('\.root.s$' 'cmd/python/')
	search = $*
	if(~ $#search 0)
		search = (.)
	if(~ $search *^$ignore^*)
		ignore = (cat)
	if not
		ignore = (grep -v -e^'^'$ignore)

	files = `{walk -f $search | $ignore | grep '\.[chsyl]$'}

	sep = '	• '
	echo 'X ,x g@^([^ 	]+[ 	]+)*'$regex'\(@ {
		!echo -n $%:$%dot(1) $sep
		p
	}' | sam -d $files >[2]/dev/null
}


op = 'struct'
identifier = '[a-zA-Z_0-9]*'

flagfmt = 'e,f,s'; args = '[regexp] [searchpath]'
eval `{aux/getflags $*} || exec aux/usage
if(~ $flage 1) op = 'enum'
if(~ $flagf 1) op = 'function'
if(~ $flags 1) op = 'struct'
if(! isatty){
	nl = '
'
	'*' = `$nl{cat}
	if(! ~ $#% 0)
		cd `{basename -d $%}
}
regex = `{echo $1 | sed 's:%:'$identifier':g'}
if(~ $#regex 0)
	regex = $identifier
shift

$op $*