hotplug2 update - include hotplug2-init.rules from hotplug2.rules, fix permissions...
[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 file
89 case "$1" in
90 /*) file="$1";;
91 *) file="$UCI_ROOT/etc/config/$1";;
92 esac
93 _C=0
94 export ${NO_EXPORT:+-n} CONFIG_SECTIONS=
95 export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=0
96 export ${NO_EXPORT:+-n} CONFIG_SECTION=
97
98 [ -e "$file" ] && {
99 . $file
100 } || return 1
101
102 ${CONFIG_SECTION:+config_cb}
103 }
104
105 config_get() {
106 case "$3" in
107 "") eval "echo \"\${CONFIG_${1}_${2}}\"";;
108 *) eval "export ${NO_EXPORT:+-n} -- \"$1=\${CONFIG_${2}_${3}}\"";;
109 esac
110 }
111
112 # config_get_bool <variable> <section> <option> [<default>]
113 config_get_bool() {
114 local _tmp
115 config_get "_tmp" "$2" "$3"
116 case "$_tmp" in
117 1|on|enabled) export ${NO_EXPORT:+-n} "$1=1";;
118 0|off|disabled) export ${NO_EXPORT:+-n} "$1=0";;
119 *) eval "$1=${4:-0}";;
120 esac
121 }
122
123 config_set() {
124 local section="$1"
125 local option="$2"
126 local value="$3"
127 local old_section="$CONFIG_SECTION"
128
129 CONFIG_SECTION="$section"
130 option "$option" "$value"
131 CONFIG_SECTION="$old_section"
132 }
133
134 config_foreach() {
135 local function="$1"
136 local type="$2"
137 local section cfgtype
138
139 [ -z "$CONFIG_SECTIONS" ] && return 0
140 for section in ${CONFIG_SECTIONS}; do
141 config_get cfgtype "$section" TYPE
142 [ -n "$type" -a "x$cfgtype" != "x$type" ] && continue
143 eval "$function \"\$section\""
144 done
145 }
146
147 load_modules() {
148 [ -d /etc/modules.d ] && {
149 cd /etc/modules.d
150 sed 's/^[^#]/insmod &/' $* | ash 2>&- || :
151 }
152 }
153
154 include() {
155 local file
156
157 for file in $(ls $1/*.sh 2>/dev/null); do
158 . $file
159 done
160 }
161
162 find_mtd_part() {
163 local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
164 local PREFIX=/dev/mtdblock
165
166 PART="${PART##mtd}"
167 [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
168 echo "${PART:+$PREFIX$PART}"
169 }
170
171 strtok() { # <string> { <variable> [<separator>] ... }
172 local tmp
173 local val="$1"
174 local count=0
175
176 shift
177
178 while [ $# -gt 1 ]; do
179 tmp="${val%%$2*}"
180
181 [ "$tmp" = "$val" ] && break
182
183 val="${val#$tmp$2}"
184
185 export ${NO_EXPORT:+-n} "$1=$tmp"; count=$((count+1))
186 shift 2
187 done
188
189 if [ $# -gt 0 -a -n "$val" ]; then
190 export ${NO_EXPORT:+-n} "$1=$val"; count=$((count+1))
191 fi
192
193 return $count
194 }
195
196
197 jffs2_mark_erase() {
198 local part="$(find_mtd_part "$1")"
199 [ -z "$part" ] && {
200 echo Partition not found.
201 return 1
202 }
203 echo -e "\xde\xad\xc0\xde" | mtd -qq write - "$1"
204 }
This page took 0.057248 seconds and 5 git commands to generate.