summaryrefslogtreecommitdiff
path: root/sys/src/9/boot/menu.rc
blob: c1f5242e13096bd72788fc598bbe302168a8c39d (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
#!/bin/rc

conffile=/mnt/conf/plan9.ini
items=()

fn menuitems{
oifs=$ifs
ifs='
'
	# get menu items and save them in the form 'item\tstring'
	x=(`{awk -F'\n' '
			$0 ~ /\[menu\]/ {
				FS="[= ]"
				for(nitem=1;;nitem++){
					getline
					if(match($0, /\[/))	# if we entered a block, we are done
						break
					sub(/\,/, "")		# remove comma
					if(match($0, /^#/)) # comments
						continue
					if(match($0, /^$/)) # empty line
						continue
					printf("%s\t\"    %d. ", $2, nitem)
					for(i=3; i <= NF; i++)
						printf("%s ", $i)
					printf("\"\t\n")
				}
			}
	' $conffile})

	ifs='	'
	for(itemline in $x){
		# separate item from string
		item=`{echo $itemline(1)}

		# $menuitemtext holds the string for the item
		$item(1)^text=$item(2)
		items=($items $item(1))
	}
	if(! ~ $#items 0){
		echo 'Plan 9 Startup Menu:'
		echo '--------------------'
	}
	ifs=$oifs
}

# load block definitions
fn menublock{
	for(i in `{
		awk -F'\n' -v item'='`{echo '['$1']' | sed 's/ //g'} '
		{
				# find menuitem block
				if(index($0, item)){
					for(;;){
						if(getline <= 0)
							break
						if(match($0, /\[/))	# entered a block, we are done
							break
						if(match($0, /^$/))
							continue
			
						# skip comments, quote kernel devices
						if(index($0, "#") == 1)
							continue
						else
							gsub("#", "''#''")
						printf("%s\n", $1)
					}
				}
		}' $conffile}){
			name=`{echo $i | awk -F'=' '{print $1}'}
			val=`{echo $i | awk -F'=' '{print $2}'}

			# a name beginning with * denotes
			# a kernel variable, save to #ec
			v0=`{echo $i | sed 's/(\*).*/\1/'}
			if(~ $v0 '*'){
				bind -bc '#ec' /env
				eval $name'='$val
				unmount '#ec' /env
			}
			if not 
				eval $name'='$val
		}
}

fn freevars{
	for(i in `{
		awk -F'\n' '{
			if(match($0, /\[/))	# entered a block, we are done
				exit
			if(match($0, /^$/))
				exit
			# skip comments, quote kernel devices
			if(index($0, "#") == 1)
				exit
			else
				gsub("#", "''#''")
			printf("%s\n", $1)
		}' $conffile}){
		# a name beginning with * denotes
		# a kernel variable, save to #ec
		val=`{echo $i | sed 's/(\*).*/\1/'}
		if(~ $val '*'){
			bind -bc '#ec' /env
			eval $i
			unmount '#ec' /env
		}
		if not eval $i
	}
}

fn parseconf{
	opt=0
	if(test -f $conffile){
		freevars	
		menuitems
		menublock 'common'

		if(! ~ $#items 0){
			while(test $opt -lt 1 || test $opt -gt $#items){
				for(item in $items)
					echo -n $`{echo $item^text} | sed 's/"//g'

				echo -n '    Selection: '
				opt=`{read}

				if(test $opt -ge 1 && test $opt -le $#items)
					menublock $items($opt)
			}
		}
	}
}