2005-11-19 11:17:20 +08:00
|
|
|
#!/bin/sh
|
2006-06-27 08:36:13 +08:00
|
|
|
# Copyright (C) 2006 OpenWrt.org
|
2006-07-30 03:59:45 +08:00
|
|
|
# Copyright (C) 2006 Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
|
2006-06-27 08:36:13 +08:00
|
|
|
|
2005-02-06 08:52:16 +08:00
|
|
|
alias debug=${DEBUG:-:}
|
|
|
|
|
2006-06-20 05:45:07 +08:00
|
|
|
# newline
|
2006-11-24 02:22:06 +08:00
|
|
|
N="
|
2006-06-20 05:45:07 +08:00
|
|
|
"
|
|
|
|
|
2006-06-25 23:32:30 +08:00
|
|
|
_C=0
|
2007-02-27 04:04:04 +08:00
|
|
|
NO_EXPORT=1
|
2006-06-25 23:32:30 +08:00
|
|
|
|
2005-11-19 11:17:20 +08:00
|
|
|
hotplug_dev() {
|
2007-03-05 04:31:53 +08:00
|
|
|
env -i ACTION=$1 INTERFACE=$2 /sbin/hotplug-call net
|
2005-10-23 04:47:19 +08:00
|
|
|
}
|
2006-05-11 02:17:12 +08:00
|
|
|
|
2006-06-20 05:45:07 +08:00
|
|
|
append() {
|
|
|
|
local var="$1"
|
|
|
|
local value="$2"
|
|
|
|
local sep="${3:- }"
|
2006-11-21 10:13:08 +08:00
|
|
|
|
2007-02-27 04:04:04 +08:00
|
|
|
eval "export ${NO_EXPORT:+-n} -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
|
2006-06-20 05:45:07 +08:00
|
|
|
}
|
|
|
|
|
2006-06-25 23:32:30 +08:00
|
|
|
reset_cb() {
|
2006-11-21 10:13:08 +08:00
|
|
|
config_cb() { return 0; }
|
|
|
|
option_cb() { return 0; }
|
2006-05-11 02:17:12 +08:00
|
|
|
}
|
2006-06-25 23:32:30 +08:00
|
|
|
reset_cb
|
2006-05-11 02:17:12 +08:00
|
|
|
|
|
|
|
config () {
|
2006-11-21 10:13:08 +08:00
|
|
|
local cfgtype="$1"
|
|
|
|
local name="$2"
|
2006-12-20 12:15:13 +08:00
|
|
|
|
2007-02-27 04:04:04 +08:00
|
|
|
export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1))
|
2006-12-20 12:15:13 +08:00
|
|
|
name="${name:-cfg$CONFIG_NUM_SECTIONS}"
|
|
|
|
append CONFIG_SECTIONS "$name"
|
2007-04-12 03:34:20 +08:00
|
|
|
[ -n "$NO_CALLBACK" ] || config_cb "$cfgtype" "$name"
|
2007-02-27 04:04:04 +08:00
|
|
|
export ${NO_EXPORT:+-n} CONFIG_SECTION="$name"
|
|
|
|
export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_TYPE=$cfgtype"
|
2006-05-11 02:17:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
option () {
|
2006-07-30 03:59:45 +08:00
|
|
|
local varname="$1"; shift
|
2006-11-22 21:32:24 +08:00
|
|
|
local value="$*"
|
2006-11-21 10:13:08 +08:00
|
|
|
|
2007-02-27 04:04:04 +08:00
|
|
|
export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_${varname}=$value"
|
2007-04-12 03:34:20 +08:00
|
|
|
[ -n "$NO_CALLBACK" ] || option_cb "$varname" "$*"
|
2006-05-11 02:17:12 +08:00
|
|
|
}
|
|
|
|
|
2006-10-09 13:59:26 +08:00
|
|
|
config_rename() {
|
|
|
|
local OLD="$1"
|
|
|
|
local NEW="$2"
|
2006-11-21 10:13:08 +08:00
|
|
|
local oldvar
|
2006-10-09 13:59:26 +08:00
|
|
|
local newvar
|
|
|
|
|
2006-11-21 10:13:08 +08:00
|
|
|
[ "$OLD" -a "$NEW" ] || return
|
|
|
|
for oldvar in `set | grep ^CONFIG_${OLD}_ | \
|
2006-10-09 13:59:26 +08:00
|
|
|
sed -e 's/\(.*\)=.*$/\1/'` ; do
|
2006-11-21 10:13:08 +08:00
|
|
|
newvar="CONFIG_${NEW}_${oldvar##CONFIG_${OLD}_}"
|
2007-02-27 04:04:04 +08:00
|
|
|
eval "export ${NO_EXPORT:+-n} \"$newvar=\${$oldvar}\""
|
2006-11-21 10:13:08 +08:00
|
|
|
unset "$oldvar"
|
2006-10-09 13:59:26 +08:00
|
|
|
done
|
2007-02-27 04:04:04 +08:00
|
|
|
export ${NO_EXPORT:+-n} CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , $NEW ,")"
|
2006-11-21 10:13:08 +08:00
|
|
|
|
2007-02-27 04:04:04 +08:00
|
|
|
[ "$CONFIG_SECTION" = "$OLD" ] && export ${NO_EXPORT:+-n} CONFIG_SECTION="$NEW"
|
2006-10-09 13:59:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
config_unset() {
|
|
|
|
config_set "$1" "$2" ""
|
|
|
|
}
|
|
|
|
|
2006-05-11 02:17:12 +08:00
|
|
|
config_clear() {
|
2006-11-21 10:13:08 +08:00
|
|
|
local SECTION="$1"
|
|
|
|
local oldvar
|
|
|
|
|
2007-02-27 04:04:04 +08:00
|
|
|
export ${NO_EXPORT:+-n} CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , ,")"
|
|
|
|
export ${NO_EXPORT:+-n} CONFIG_SECTIONS="${SECTION:+$CONFIG_SECTIONS}"
|
2006-12-20 12:15:13 +08:00
|
|
|
|
2007-01-30 06:07:17 +08:00
|
|
|
for oldvar in `set | grep ^CONFIG_${SECTION:+${SECTION}_} | \
|
2006-05-11 02:17:12 +08:00
|
|
|
sed -e 's/\(.*\)=.*$/\1/'` ; do
|
2006-11-21 10:13:08 +08:00
|
|
|
unset $oldvar
|
2006-05-11 02:17:12 +08:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
config_load() {
|
2007-02-27 04:04:04 +08:00
|
|
|
local file="$UCI_ROOT/etc/config/$1"
|
2006-06-25 23:32:30 +08:00
|
|
|
_C=0
|
2007-02-27 04:04:04 +08:00
|
|
|
export ${NO_EXPORT:+-n} CONFIG_SECTIONS=
|
|
|
|
export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=0
|
|
|
|
export ${NO_EXPORT:+-n} CONFIG_SECTION=
|
2006-11-21 10:13:08 +08:00
|
|
|
|
|
|
|
[ -e "$file" ] && {
|
|
|
|
. $file
|
2006-06-25 23:32:30 +08:00
|
|
|
} || return 1
|
2006-11-21 10:13:08 +08:00
|
|
|
|
2006-05-11 02:17:12 +08:00
|
|
|
${CONFIG_SECTION:+config_cb}
|
|
|
|
}
|
|
|
|
|
|
|
|
config_get() {
|
|
|
|
case "$3" in
|
2006-10-09 13:59:26 +08:00
|
|
|
"") eval "echo \"\${CONFIG_${1}_${2}}\"";;
|
2007-02-27 04:04:04 +08:00
|
|
|
*) eval "export ${NO_EXPORT:+-n} -- \"$1=\${CONFIG_${2}_${3}}\"";;
|
2006-05-11 02:17:12 +08:00
|
|
|
esac
|
2007-02-28 07:09:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# config_get_bool <variable> <section> <option> [<default>]
|
|
|
|
config_get_bool() {
|
|
|
|
local _tmp
|
|
|
|
config_get "_tmp" "$2" "$3"
|
|
|
|
case "$_tmp" in
|
|
|
|
1|on|enabled) export ${NO_EXPORT:+-n} "$1=1";;
|
|
|
|
0|off|disabled) export ${NO_EXPORT:+-n} "$1=0";;
|
|
|
|
*) eval "$1=${4:-0}";;
|
|
|
|
esac
|
2006-05-11 02:17:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
config_set() {
|
2006-11-22 21:32:24 +08:00
|
|
|
local section="$1"
|
|
|
|
local option="$2"
|
|
|
|
local value="$3"
|
2007-03-24 21:43:04 +08:00
|
|
|
local old_section="$CONFIG_SECTION"
|
|
|
|
|
|
|
|
CONFIG_SECTION="$section"
|
|
|
|
option "$option" "$value"
|
|
|
|
CONFIG_SECTION="$old_section"
|
2006-05-11 02:17:12 +08:00
|
|
|
}
|
2006-05-31 03:38:38 +08:00
|
|
|
|
2006-12-20 12:15:13 +08:00
|
|
|
config_foreach() {
|
|
|
|
local function="$1"
|
2007-03-02 23:48:29 +08:00
|
|
|
local type="$2"
|
|
|
|
local section cfgtype
|
2006-12-20 12:15:13 +08:00
|
|
|
|
|
|
|
[ -z "$CONFIG_SECTIONS" ] && return 0
|
|
|
|
for section in ${CONFIG_SECTIONS}; do
|
2007-03-02 23:48:29 +08:00
|
|
|
config_get cfgtype "$section" TYPE
|
|
|
|
[ -n "$type" -a "$cfgtype" != "$type" ] && continue
|
2006-12-20 12:15:13 +08:00
|
|
|
eval "$function \"\$section\""
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2006-05-31 03:38:38 +08:00
|
|
|
load_modules() {
|
2006-11-24 02:22:06 +08:00
|
|
|
cd /etc/modules.d
|
2006-05-31 03:38:38 +08:00
|
|
|
sed 's/^[^#]/insmod &/' $* | ash 2>&- || :
|
|
|
|
}
|
2006-07-30 03:59:45 +08:00
|
|
|
|
|
|
|
include() {
|
2006-11-21 10:13:08 +08:00
|
|
|
local file
|
|
|
|
|
2006-09-24 21:32:18 +08:00
|
|
|
for file in $(ls $1/*.sh 2>/dev/null); do
|
2006-07-30 03:59:45 +08:00
|
|
|
. $file
|
|
|
|
done
|
|
|
|
}
|
2006-10-26 09:35:50 +08:00
|
|
|
|
|
|
|
find_mtd_part() {
|
|
|
|
local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
|
2007-03-03 03:01:49 +08:00
|
|
|
local PREFIX=/dev/mtdblock
|
2006-11-21 10:13:08 +08:00
|
|
|
|
2006-10-26 09:35:50 +08:00
|
|
|
PART="${PART##mtd}"
|
2007-03-03 03:01:49 +08:00
|
|
|
[ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
|
|
|
|
echo "${PART:+$PREFIX$PART}"
|
2006-10-26 09:35:50 +08:00
|
|
|
}
|
|
|
|
|
2006-11-21 10:13:08 +08:00
|
|
|
strtok() { # <string> { <variable> [<separator>] ... }
|
|
|
|
local tmp
|
|
|
|
local val="$1"
|
2006-11-21 03:30:50 +08:00
|
|
|
local count=0
|
|
|
|
|
|
|
|
shift
|
|
|
|
|
|
|
|
while [ $# -gt 1 ]; do
|
2006-11-21 10:13:08 +08:00
|
|
|
tmp="${val%%$2*}"
|
2006-11-21 03:30:50 +08:00
|
|
|
|
2006-11-21 10:13:08 +08:00
|
|
|
[ "$tmp" = "$val" ] && break
|
2006-11-21 03:30:50 +08:00
|
|
|
|
2006-11-21 10:13:08 +08:00
|
|
|
val="${val#$tmp$2}"
|
2006-11-21 03:30:50 +08:00
|
|
|
|
2007-02-27 04:04:04 +08:00
|
|
|
export ${NO_EXPORT:+-n} "$1=$tmp"; count=$((count+1))
|
2006-11-21 03:30:50 +08:00
|
|
|
shift 2
|
|
|
|
done
|
|
|
|
|
2006-11-21 10:13:08 +08:00
|
|
|
if [ $# -gt 0 -a "$val" ]; then
|
2007-02-27 04:04:04 +08:00
|
|
|
export ${NO_EXPORT:+-n} "$1=$val"; count=$((count+1))
|
2006-11-21 03:30:50 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
return $count
|
|
|
|
}
|