summaryrefslogtreecommitdiff
path: root/rc/bin/inst/defs
blob: f9c9454c06893c28f613c94ad78777c9d1b39162 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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 
}