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