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