firstboot: add support for union mounts
[openwrt.git] / package / base-files / files / etc / functions.sh
1 #!/bin/sh
2 # Copyright (C) 2006 OpenWrt.org
3 # Copyright (C) 2006 Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
4
5 alias debug=${DEBUG:-:}
6 alias mount='busybox mount'
7
8 # newline
9 N="
10 "
11
12 _C=0
13 NO_EXPORT=1
14 LOAD_STATE=1
15 LIST_SEP=" "
16
17 hotplug_dev() {
18 env -i ACTION=$1 INTERFACE=$2 /sbin/hotplug-call net
19 }
20
21 append() {
22 local var="$1"
23 local value="$2"
24 local sep="${3:- }"
25
26 eval "export ${NO_EXPORT:+-n} -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
27 }
28
29 list_contains() {
30 local var="$1"
31 local str="$2"
32 local val
33
34 eval "val=\" \${$var} \""
35 [ "${val%% $str *}" != "$val" ]
36 }
37
38 list_remove() {
39 local var="$1"
40 local remove="$2"
41 local val
42
43 eval "val=\" \${$var} \""
44 val1="${val%% $remove *}"
45 [ "$val1" = "$val" ] && return
46 val2="${val##* $remove }"
47 [ "$val2" = "$val" ] && return
48 val="${val1## } ${val2%% }"
49 val="${val%% }"
50 eval "export ${NO_EXPORT:+-n} -- \"$var=\$val\""
51 }
52
53 config_load() {
54 [ -n "$IPKG_INSTROOT" ] && return 0
55 uci_load "$@"
56 }
57
58 reset_cb() {
59 config_cb() { return 0; }
60 option_cb() { return 0; }
61 list_cb() { return 0; }
62 }
63 reset_cb
64
65 package() {
66 return 0
67 }
68
69 config () {
70 local cfgtype="$1"
71 local name="$2"
72
73 export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1))
74 name="${name:-cfg$CONFIG_NUM_SECTIONS}"
75 append CONFIG_SECTIONS "$name"
76 [ -n "$NO_CALLBACK" ] || config_cb "$cfgtype" "$name"
77 export ${NO_EXPORT:+-n} CONFIG_SECTION="$name"
78 export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_TYPE=$cfgtype"
79 }
80
81 option () {
82 local varname="$1"; shift
83 local value="$*"
84
85 export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_${varname}=$value"
86 [ -n "$NO_CALLBACK" ] || option_cb "$varname" "$*"
87 }
88
89 list() {
90 local varname="$1"; shift
91 local value="$*"
92 local len
93
94 config_get len "$CONFIG_SECTION" "${varname}_LENGTH" 0
95 len=$(($len + 1))
96 config_set "$CONFIG_SECTION" "${varname}_ITEM$len" "$value"
97 config_set "$CONFIG_SECTION" "${varname}_LENGTH" "$len"
98 append "CONFIG_${CONFIG_SECTION}_${varname}" "$value" "$LIST_SEP"
99 list_cb "$varname" "$*"
100 }
101
102 config_rename() {
103 local OLD="$1"
104 local NEW="$2"
105 local oldvar
106 local newvar
107
108 [ -n "$OLD" -a -n "$NEW" ] || return
109 for oldvar in `set | grep ^CONFIG_${OLD}_ | \
110 sed -e 's/\(.*\)=.*$/\1/'` ; do
111 newvar="CONFIG_${NEW}_${oldvar##CONFIG_${OLD}_}"
112 eval "export ${NO_EXPORT:+-n} \"$newvar=\${$oldvar}\""
113 unset "$oldvar"
114 done
115 export ${NO_EXPORT:+-n} CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , $NEW ,")"
116
117 [ "$CONFIG_SECTION" = "$OLD" ] && export ${NO_EXPORT:+-n} CONFIG_SECTION="$NEW"
118 }
119
120 config_unset() {
121 config_set "$1" "$2" ""
122 }
123
124 config_clear() {
125 local SECTION="$1"
126 local oldvar
127
128 list_remove CONFIG_SECTIONS "$SECTION"
129 export ${NO_EXPORT:+-n} CONFIG_SECTIONS="${SECTION:+$CONFIG_SECTIONS}"
130
131 for oldvar in `set | grep ^CONFIG_${SECTION:+${SECTION}_} | \
132 sed -e 's/\(.*\)=.*$/\1/'` ; do
133 unset $oldvar
134 done
135 }
136
137 # config_get <variable> <section> <option> [<default>]
138 # config_get <section> <option>
139 config_get() {
140 case "$3" in
141 "") eval echo "\${CONFIG_${1}_${2}:-\${4}}";;
142 *) eval export ${NO_EXPORT:+-n} -- "${1}=\${CONFIG_${2}_${3}:-\${4}}";;
143 esac
144 }
145
146 # config_get_bool <variable> <section> <option> [<default>]
147 config_get_bool() {
148 local _tmp
149 config_get _tmp "$2" "$3" "$4"
150 case "$_tmp" in
151 1|on|true|enabled) _tmp=1;;
152 0|off|false|disabled) _tmp=0;;
153 *) _tmp="$4";;
154 esac
155 export ${NO_EXPORT:+-n} "$1=$_tmp"
156 }
157
158 config_set() {
159 local section="$1"
160 local option="$2"
161 local value="$3"
162 local old_section="$CONFIG_SECTION"
163
164 CONFIG_SECTION="$section"
165 option "$option" "$value"
166 CONFIG_SECTION="$old_section"
167 }
168
169 config_foreach() {
170 local function="$1"
171 [ "$#" -ge 1 ] && shift
172 local type="$1"
173 [ "$#" -ge 1 ] && shift
174 local section cfgtype
175
176 [ -z "$CONFIG_SECTIONS" ] && return 0
177 for section in ${CONFIG_SECTIONS}; do
178 config_get cfgtype "$section" TYPE
179 [ -n "$type" -a "x$cfgtype" != "x$type" ] && continue
180 eval "$function \"\$section\" \"\$@\""
181 done
182 }
183
184 config_list_foreach() {
185 [ "$#" -ge 3 ] || return 0
186 local section="$1"; shift
187 local option="$1"; shift
188 local function="$1"; shift
189 local val
190 local len
191 local c=1
192
193 config_get len "${section}" "${option}_LENGTH"
194 [ -z "$len" ] && return 0
195 while [ $c -le "$len" ]; do
196 config_get val "${section}" "${option}_ITEM$c"
197 eval "$function \"\$val\" \"$@\""
198 c="$(($c + 1))"
199 done
200 }
201
202 load_modules() {
203 [ -d /etc/modules.d ] && {
204 cd /etc/modules.d
205 sed 's/^[^#]/insmod &/' $* | ash 2>&- || :
206 }
207 }
208
209 include() {
210 local file
211
212 for file in $(ls $1/*.sh 2>/dev/null); do
213 . $file
214 done
215 }
216
217 find_mtd_part() {
218 local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
219 local PREFIX=/dev/mtdblock
220
221 PART="${PART##mtd}"
222 [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
223 echo "${PART:+$PREFIX$PART}"
224 }
225
226 strtok() { # <string> { <variable> [<separator>] ... }
227 local tmp
228 local val="$1"
229 local count=0
230
231 shift
232
233 while [ $# -gt 1 ]; do
234 tmp="${val%%$2*}"
235
236 [ "$tmp" = "$val" ] && break
237
238 val="${val#$tmp$2}"
239
240 export ${NO_EXPORT:+-n} "$1=$tmp"; count=$((count+1))
241 shift 2
242 done
243
244 if [ $# -gt 0 -a -n "$val" ]; then
245 export ${NO_EXPORT:+-n} "$1=$val"; count=$((count+1))
246 fi
247
248 return $count
249 }
250
251
252 jffs2_mark_erase() {
253 local part="$(find_mtd_part "$1")"
254 [ -z "$part" ] && {
255 echo Partition not found.
256 return 1
257 }
258 echo -e "\xde\xad\xc0\xde" | mtd -qq write - "$1"
259 }
260
261 uci_apply_defaults() {(
262 cd /etc/uci-defaults || return 0
263 files="$(ls)"
264 [ -z "$files" ] && return 0
265 mkdir -p /tmp/.uci
266 for file in $files; do
267 ( . "./$(basename $file)" ) && rm -f "$file"
268 done
269 uci commit
270 )}
271
272 [ -z "$IPKG_INSTROOT" -a -f /lib/config/uci.sh ] && . /lib/config/uci.sh
This page took 0.054131 seconds and 5 git commands to generate.