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