Use block events for the mounting/unmounting of external storage devices.
[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"
95 len="$((${len:-0} + 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() {
138 case "$3" in
139 "") eval "echo \"\${CONFIG_${1}_${2}}\"";;
140 *) eval "export ${NO_EXPORT:+-n} -- \"$1=\${CONFIG_${2}_${3}}\"";;
141 esac
142 }
143
144 # config_get_bool <variable> <section> <option> [<default>]
145 config_get_bool() {
146 local _tmp
147 config_get "_tmp" "$2" "$3"
148 case "$_tmp" in
149 1|on|true|enabled) export ${NO_EXPORT:+-n} "$1=1";;
150 0|off|false|disabled) export ${NO_EXPORT:+-n} "$1=0";;
151 *) eval "$1=$4";;
152 esac
153 }
154
155 config_set() {
156 local section="$1"
157 local option="$2"
158 local value="$3"
159 local old_section="$CONFIG_SECTION"
160
161 CONFIG_SECTION="$section"
162 option "$option" "$value"
163 CONFIG_SECTION="$old_section"
164 }
165
166 config_foreach() {
167 local function="$1"
168 [ "$#" -ge 1 ] && shift
169 local type="$1"
170 [ "$#" -ge 1 ] && shift
171 local section cfgtype
172
173 [ -z "$CONFIG_SECTIONS" ] && return 0
174 for section in ${CONFIG_SECTIONS}; do
175 config_get cfgtype "$section" TYPE
176 [ -n "$type" -a "x$cfgtype" != "x$type" ] && continue
177 eval "$function \"\$section\" \"\$@\""
178 done
179 }
180
181 config_list_foreach() {
182 [ "$#" -ge 3 ] || return 0
183 local section="$1"; shift
184 local option="$1"; shift
185 local function="$1"; shift
186 local val
187 local len
188 local c=1
189
190 config_get len "${section}" "${option}_LENGTH"
191 [ -z "$len" ] && return 0
192 while [ $c -le "$len" ]; do
193 config_get val "${section}" "${option}_ITEM$c"
194 eval "$function \"\$val\" \"$@\""
195 c="$(($c + 1))"
196 done
197 }
198
199 load_modules() {
200 [ -d /etc/modules.d ] && {
201 cd /etc/modules.d
202 sed 's/^[^#]/insmod &/' $* | ash 2>&- || :
203 }
204 }
205
206 include() {
207 local file
208
209 for file in $(ls $1/*.sh 2>/dev/null); do
210 . $file
211 done
212 }
213
214 find_mtd_part() {
215 local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
216 local PREFIX=/dev/mtdblock
217
218 PART="${PART##mtd}"
219 [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
220 echo "${PART:+$PREFIX$PART}"
221 }
222
223 strtok() { # <string> { <variable> [<separator>] ... }
224 local tmp
225 local val="$1"
226 local count=0
227
228 shift
229
230 while [ $# -gt 1 ]; do
231 tmp="${val%%$2*}"
232
233 [ "$tmp" = "$val" ] && break
234
235 val="${val#$tmp$2}"
236
237 export ${NO_EXPORT:+-n} "$1=$tmp"; count=$((count+1))
238 shift 2
239 done
240
241 if [ $# -gt 0 -a -n "$val" ]; then
242 export ${NO_EXPORT:+-n} "$1=$val"; count=$((count+1))
243 fi
244
245 return $count
246 }
247
248
249 jffs2_mark_erase() {
250 local part="$(find_mtd_part "$1")"
251 [ -z "$part" ] && {
252 echo Partition not found.
253 return 1
254 }
255 echo -e "\xde\xad\xc0\xde" | mtd -qq write - "$1"
256 }
257
258 uci_apply_defaults() {(
259 cd /etc/uci-defaults || return 0
260 files="$(ls)"
261 [ -z "$files" ] && return 0
262 mkdir -p /tmp/.uci
263 for file in $files; do
264 ( . "./$(basename $file)" ) && rm -f "$file"
265 done
266 uci commit
267 )}
268
269 [ -z "$IPKG_INSTROOT" -a -f /lib/config/uci.sh ] && . /lib/config/uci.sh
This page took 0.068747 seconds and 5 git commands to generate.