shithub: riscv

ref: 0d29a5d81f8c2e45f94aa214d47a568a741547f4
dir: /rc/bin/inst/defs/

View raw version
nl='
'
tab='	'
if(~ $#distname 0)
	distname=plan9

wctl=/dev/null
if(test -w /dev/wctl)
	wctl=/dev/wctl

fn log {
	echo $* >>/srv/log
}

fn logprog {
	echo '% '^$"* >>/srv/log
	$* >[2=1] >>/srv/log
}

fn sigint {
	# nothing happens
}

fn prompt {
	def=()
	what=()
	if(~ $1 -d && ! ~ $#* 1){
		def=$2
		shift
		shift
	}

	optstr=()
	if(~ $1 -w && ! ~ $#* 1){
		optstr=$2
		shift
		shift
	}

	pr=$1
	shift

	opts=($*)
	if(~ $#opts 0) {
		suf=' '
	}
	if not if(! ~ $#optstr 0) {
		if(~ $optstr '') 
			suf=' '
		if not {
			pr=$pr^' ('^$"optstr^')'
			suf=''
		}
	}
	if not {
		pr=$pr^' ('^$1
		shift
		for(i)
			pr=$pr^', '^$i
		pr=$pr^')'
		suf=''
	}

	if(~ $#def 1)
		pr=$pr^$suf^'['^$def^']'
	if not
		pr=$pr^$suf^'[no default]'

	pr=$pr^': '


	okay=no
	while(~ $okay no) {
		echo -n current >$wctl
		echo -n top >$wctl
		echo -n $pr >[1=2]
		ifs='' {rd=`{read}}
		if(~ $#rd 0)
			exit notdone
		if(~ $rd !*){
			ifs='' {rd=`{echo $rd | sed 's/!//'}}
			echo $rd
			rc -c $rd
			echo !$status
		}
		if not{
			rd=`{echo $rd}
			if(~ $#rd 0 || ~ $rd '')
				rd=$def
	
			switch($#opts){
			case 0
				if(! ~ $rd '')
					okay=yes
			case *
				if(~ $rd $opts)
					okay=yes
			}
		}
	}
	echo -n $rd >/env/rd	# just in case
}

fn desc {
	echo -n '	'^$1^'	- '
	grep '^# desc: ' $1 | sed 's/# desc: //'
}

fn prereq {
	grep '^# prereq:' $1 | sed 's/# prereq://'
}

fn mustdo {
	echo You must `{grep '^# mustdo:' $1 | sed 's/# mustdo://'}
}

# there's no easy way to pass shell variables
# up from children to parents; the parents have
# to be coerced into noticing that the environment
# changed, even when in the same environment group.
#
# instead, we explicitly export the variables we want
# to percolate, and the parent calls coherence to reread
# the variables.
#
# we just append to the vars file, so that later exports
# override earlier ones; when we call coherence, 
# the duplicates are thrown out.

fn export {
	null=()
	nonnull=()
	for(i in $*){
		if(~ $#$i 0)
			null=($null $i)
		if not
			nonnull=($nonnull $i)
	}
	if(! ~ $#nonnull 0)
		whatis $nonnull |grep -v '^\./' >>/tmp/vars >[2]/dev/null
	for(i in $null)
		echo $i^'=()' >>/tmp/vars
}

fn coherence {
	if(test -f /tmp/vars) {
		grep '^[a-z]*=' /tmp/vars >/tmp/vars2
		v=`{sed 's/^([a-z]*)=.*/\1/' /tmp/vars2 | sort -u}
		. /tmp/vars2
		rm /tmp/vars2
		rm /tmp/vars
		export $v
	}
}

# ip device stats

fn isipdevup {
	grep -s $1 /net/ipifc/*/status >[2]/dev/null 
}