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