upgrade the ixp4xx ethernet driver to v0.3.1, split the Gateway 7001 support to two...
[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 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 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 option_cb "$varname" "$*"
50 }
51
52 config_rename() {
53 local OLD="$1"
54 local NEW="$2"
55 local oldvar
56 local newvar
57
58 [ "$OLD" -a "$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="$UCI_ROOT/etc/config/$1"
89 _C=0
90 export ${NO_EXPORT:+-n} CONFIG_SECTIONS=
91 export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=0
92 export ${NO_EXPORT:+-n} CONFIG_SECTION=
93
94 [ -e "$file" ] && {
95 . $file
96 } || return 1
97
98 ${CONFIG_SECTION:+config_cb}
99 }
100
101 config_get() {
102 case "$3" in
103 "") eval "echo \"\${CONFIG_${1}_${2}}\"";;
104 *) eval "export ${NO_EXPORT:+-n} -- \"$1=\${CONFIG_${2}_${3}}\"";;
105 esac
106 }
107
108 # config_get_bool <variable> <section> <option> [<default>]
109 config_get_bool() {
110 local _tmp
111 config_get "_tmp" "$2" "$3"
112 case "$_tmp" in
113 1|on|enabled) export ${NO_EXPORT:+-n} "$1=1";;
114 0|off|disabled) export ${NO_EXPORT:+-n} "$1=0";;
115 *) eval "$1=${4:-0}";;
116 esac
117 }
118
119 config_set() {
120 local section="$1"
121 local option="$2"
122 local value="$3"
123 export ${NO_EXPORT:+-n} "CONFIG_${section}_${option}=$value"
124 }
125
126 config_foreach() {
127 local function="$1"
128 local type="$2"
129 local section cfgtype
130
131 [ -z "$CONFIG_SECTIONS" ] && return 0
132 for section in ${CONFIG_SECTIONS}; do
133 config_get cfgtype "$section" TYPE
134 [ -n "$type" -a "$cfgtype" != "$type" ] && continue
135 eval "$function \"\$section\""
136 done
137 }
138
139 load_modules() {
140 cd /etc/modules.d
141 sed 's/^[^#]/insmod &/' $* | ash 2>&- || :
142 }
143
144 include() {
145 local file
146
147 for file in $(ls $1/*.sh 2>/dev/null); do
148 . $file
149 done
150 }
151
152 find_mtd_part() {
153 local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
154 local PREFIX=/dev/mtdblock
155
156 PART="${PART##mtd}"
157 [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
158 echo "${PART:+$PREFIX$PART}"
159 }
160
161 strtok() { # <string> { <variable> [<separator>] ... }
162 local tmp
163 local val="$1"
164 local count=0
165
166 shift
167
168 while [ $# -gt 1 ]; do
169 tmp="${val%%$2*}"
170
171 [ "$tmp" = "$val" ] && break
172
173 val="${val#$tmp$2}"
174
175 export ${NO_EXPORT:+-n} "$1=$tmp"; count=$((count+1))
176 shift 2
177 done
178
179 if [ $# -gt 0 -a "$val" ]; then
180 export ${NO_EXPORT:+-n} "$1=$val"; count=$((count+1))
181 fi
182
183 return $count
184 }
This page took 0.058155 seconds and 5 git commands to generate.