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