-
Notifications
You must be signed in to change notification settings - Fork 9
/
shell-cmdline
125 lines (106 loc) · 2.11 KB
/
shell-cmdline
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
#!/bin/sh -efu
### This file is covered by the GNU General Public License,
### which should be included with libshell as the file LICENSE.
### All copyright information are listed in the COPYING.
if [ -z "${__included_shell_cmdline-}" ]; then
__included_shell_cmdline=1
. shell-string
### Run handler for each variable in /proc/cmdline.
cmdline_foreach()
{
local l h c='' m=''
l="$1"; shift
h="$1"; shift
__is_set() {
[ $(( $1 & $2 )) -eq $2 ]
}
fill_mask m "$l"
local state=$((0))
local VALUE=$((2))
local EQUAL=$((4))
local QUOTE=$((16))
local n='' v='' f=1
while [ -n "$l" ]; do
c="${l%$m}"
case "$c" in
'"')
__is_set $state $QUOTE &&
state=$(($state ^ $QUOTE)) ||
state=$(($state | $QUOTE))
;;
' ')
if __is_set $state $QUOTE; then
v="$v$c"
else
f=
__is_set $state $EQUAL ||
{ v=1; f=1; }
$h "$n" "$v" "$f" ||
break
n='' v='' f=''
state=$((0))
fi
;;
'=')
! __is_set $state $VALUE ||
v="$v$c"
state=$(($state | $VALUE | $EQUAL))
;;
*)
if ! __is_set $state $VALUE; then
[ "$c" != '-' ] || c='_'
n="$n$c"
else
v="$v$c"
fi
;;
esac
l="${l#?}"
m="${m#?}"
done
if [ -n "$n" ]; then
f=
__is_set $state $EQUAL ||
{ v=1; f=1; }
$h "$n" "$v" "$f" ||:
fi
unset -f __is_set
}
### Find spicified variable in /proc/cmdline and store result into variable.
### Usage example:
### read cmdline < /proc/cmdline
### cmdline_get 'initrd_value' 'initrd' "$cmdline"
### echo "$initrd_value"
### or:
### cmdline_get 'initrd_value' 'initrd'
### echo "$initrd_value"
###
cmdline_get()
{
local __cmdline __retv __getn
__retv="$1"; shift
__getn="$1"; shift
if [ "$#" -eq 0 ]; then
read -r __cmdline < /proc/cmdline
else
__cmdline="$1"
fi
local c m l="$__getn"
__getn=
fill_mask m "$l"
while [ -n "$l" ]; do
c="${l%$m}"
[ "$c" != '-' ] || c='_'
__getn="$__getn$c"
l="${l#?}"
m="${m#?}"
done
unset c m l
__getval()
{
[ "$1" != "$__getn" ] || eval "$__retv=\"\$2\""
}
cmdline_foreach "$__cmdline" __getval
unset -f __getval
}
fi #__included_shell_cmdline