💾 Archived View for runjimmyrunrunyoufuckerrun.com › rc › s captured on 2021-12-17 at 13:26:06.

View Raw

More Information

-=-=-=-=-=-=-

#!/bin/rc
# to be run inside sam with !
# sets up a bunch of convenience functions and loads last dumped state

# !u [fn...] # print sam fn usage
fn u{
	if(~ $#* 0) sed -n 's/^# ([_!^|><])/\1/p' /bin/s
	if not for(i) sed -n 's/^# ([_!^|><]'^$i^' )/\1/p' /bin/s
}

# ^b regex # jump to file
fn b{echo 'X/'^$*^'/!B $%'}

# ^+ [n] # increase indent
fn +{
	if(~ $%dot(2) $%dot(3)) echo ,
	if(~ $1 [1-9]) echo 'x/^/i/'^`''{seq $1 | tr -d \xa | tr 1-9 '	'}
	if not echo 'x/^/i/	/'
}

# ^- [n] # decrease indent
fn -{
	if(~ $%dot(2) $%dot(3)) echo ,
	if(~ $1 [1-9]) echo 'x/^'^`''{seq 9 | tail -$1 | tr -d \xa | tr 1-9 '	'}^/d
	if not echo 'x/^	/d'
}

# ^c [args] # test build and run for C programs
fn c{
	rfork e
	if(! ~ $% *.[ch]){
		echo !echo not a c file!
		exit
	}
	echo w
	eval `{grep '^O=.


 /$objtype/mkfile}
	targ = $O.out
	d = `{basename -d $%}
	cd $d
	if(! mk -n $targ >/dev/null >[1=2])
		targ = `{echo $% | sed 's•(.*/)?(.*)\.c$•'^$O^'.\2•'}
	builtin w c $d ';' mk $targ '&&' $targ $*
}

# |calc # inline arithmetic
fn calc{pc -n | tr -d $nl}

# ^cc # c comment
fn cc{
	echo '#'^$%dot(3)^'a/*\//'
	echo '#'^$%dot(2)^'i/\/*/'
}

# ^d [file] # save unwritten changes and dump sam state to file
fn d{
	rfork e
	switch($#*){
	case 0
		dump = /tmp/sam.dump
	case 1
		dump = $1
	case *
		echo !u d
		exit
	}
	d = `{pwd}
	echo 'X/^''/w'
	echo !rm -f $dump
	echo 'Y/^..\./!echo `{cleanname -d' $d '$%}^'':#''^$%dot(2)^'',#''^$%dot(3) >>' $dump
	echo 'X/^..\./!echo `{cleanname -d' $d '$%}^'':#''^$%dot(2)^'',#''^$%dot(3) >>' $dump
}

# _i # find information about selected C function/struct or command
fn i{
	rfork e
	name = `{read}
	if(! ~ $#name 1){
		echo !echo select a function/struct/command name first!
		exit
	}
	cd `{basename -d $%}
	match = `{g '^([*a-zA-Z]+[ 	]+)*'^$name^'[ 	]*(\([^;]*)?


 | sed '1{s/: .*//;q;}'}
	if(~ $#match 1)	# local function definition
		plumb $match
	if not{
		match = `{g 'struct[ 	]+'^$name^'[ 	]*({|$)' | sed '1{s/: .*//;q;}'}
		if(~ $#match 1)	# local struct definition
			plumb $match
		if not{
			match = `{struct $name | sed 1q}
			if(~ $#match 1)	# system struct definition
				plumb $match
			if not{
				match = `{man -w $name >[2]/dev/null | sed 's†.*/([0-9])/(.*)†\2(\1)†'}
				if(! ~ $#match 0){	# system function or command
					echo !echo ''''^$"match^''''
					echo !sig $name
				}
				if not{	# getting really desperate now...
					match = `{lookman $name | sed 's/.*#//'}
					if(~ $#match 0)
						echo !echo no information found for $name
					if not
						echo !echo ''''^$"match^''''
				}
			}
		}
	}
}

# ^j # jump between two files
fn j{
	if(! ~ $samfilejump '')
		echo 'builtin < echo' $samfilejump
	echo '!samfilejump =' $%
}

# !l [file] # load dump file
fn l{
	switch($#*){
	case 0
		xargs -n1 plumb </tmp/sam.dump
	case 1
		xargs -n1 plumb <$1
	case *
		u l
	}
}

# _m # plumb man page for selected name
fn m{sed '1s/.*/!plumb &''(0)''/;q'}

# ^q [string] # email-style block quote
fn q{
	if(~ $%dot(2) $%dot(3)) echo ,
	if(~ $#* 0) echo 'x/^/i/> '
	if not echo 'x/^/i/'^$"*
}

# ^rc # rc comment
fn rc{
	if(~ $%dot(2) $%dot(3)) echo ,
	echo 'x/^/i/# '
}

# ^rn from to # rename C identifier
fn rn{
	rfork e
	if(! ~ $#* 2){
		echo 'usage: ^rn from to'
		exit
	}
	if(~ $%dot(2) $%dot(3)) echo ,
	charconst='''[^'']*'''
	strconst='"([^"]|\\")*"'
	name='[a-zA-Z_¡-￿][a-zA-Z_0-9¡-￿]*'
	partial=`{
		echo -n $1 |
		awk '{for(i = 0; i < length($1) + 1; i++) printf "."}'
	}
	echo 'y/'$charconst'/ y/'$strconst'/ x/'$name'/ g/'$1'/ v/'$partial'/ c/'$2
}

# _s # print function signature
fn s{sed '1s/.*/!sig &/;q'}

# ^uc # uncomment c/rc code
fn uc{echo 'x/^(#|\/\/) ?/d'}

# ^w [cmd] # write buffer, open a rio window in base dir & run a cmd (eg. mk)
fn w{
	echo w
	builtin w c `{basename -d $%} ';' $*
}

if(test -r /tmp/sam.dump)
	l