shithub: bin.rc

ref: 858601ce88f1dc0d9758d101fc7ac26cfcf1c2fb
dir: /bin/c/

View raw version
#!/bin/rc
rfork e
fn usage {>[2=1] echo 'usage:' $usage && exit 'usage'}
usage='c [-fs] [regex] [search]
	-f        find functions
	-s        find structs'

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

fn struct {
	ignore = (/sys/include/ape)
	search = $*
	if(~ $#search 0)
		search = (/sys/include .)
	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 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
}

identifier = '[a-zA-Z_0-9]*'
op = (struct)
while(~ $1 -* && ! ~ $1 --){
	switch($1){
	case -f -func*
		op = 'function'
	case -s -struct
		op = 'struct'
	case *
		usage
	}
	shift
}
if(~ $1 --)
	shift

if(! isatty){
	nl = '
'
	'*' = `$nl{cat}
	if(! ~ $#% 0)
		cd `{basename -d $%}
}
regex = `{echo $1 | sed 's:%:'$identifier':g'}
if(~ $#regex 0)
	regex = $identifier
shift

$op $*