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