fixes for common config functions
[openwrt.git] / package / base-files / default / etc / functions.sh
1 #!/bin/sh
2 alias debug=${DEBUG:-:}
3
4 # newline
5 N="
6 "
7
8 _C=0
9
10 # valid interface?
11 if_valid () (
12 ifconfig "$1" >&- 2>&- ||
13 [ "${1%%[0-9]}" = "br" ] ||
14 { debug "# missing interface '$1' ignored"; false; }
15 )
16
17 hotplug_dev() {
18 env -i ACTION=$1 INTERFACE=$2 /sbin/hotplug net
19 }
20
21 append() {
22 local var="$1"
23 local value="$2"
24 local sep="${3:- }"
25 eval "export ${var}=\"\${${var}:+\${${var}}${value:+$sep}}$value\""
26 }
27
28 reset_cb() {
29 config_cb() {
30 return 0
31 }
32 option_cb() {
33 return 0
34 }
35 }
36 reset_cb
37
38 config () {
39 local type="$1"
40 local name="$2"
41 _C=$(($_C + 1))
42 name="${name:-cfg${_C}}"
43 config_cb "$type" "$name"
44 export CONFIG_SECTION="$name"
45 export CONFIG_${CONFIG_SECTION}_TYPE="$type"
46 }
47
48 option () {
49 local varname="$1" ; shift
50 export CONFIG_${CONFIG_SECTION}_${varname}="$*"
51 option_cb "$varname" "$*"
52 }
53
54 config_clear() {
55 [ -z "$CONFIG_SECTION" ] && return
56 for oldsetting in `set | grep ^CONFIG_${CONFIG_SECTION}_ | \
57 sed -e 's/\(.*\)=.*$/\1/'` ; do
58 unset $oldsetting
59 done
60 unset CONFIG_SECTION
61 }
62
63 config_load() {
64 local DIR="./"
65 _C=0
66 [ \! -e "$1" -a -e "/etc/config/$1" ] && {
67 DIR="/etc/config/"
68 }
69 [ -e "$DIR$1" ] && {
70 CONFIG_FILENAME="$DIR$1"
71 . ${CONFIG_FILENAME}
72 } || return 1
73 ${CD:+cd -} >/dev/null
74 ${CONFIG_SECTION:+config_cb}
75 }
76
77 config_get() {
78 case "$3" in
79 "") eval "echo \${CONFIG_${1}_${2}}";;
80 *) eval "$1=\"\${CONFIG_${2}_${3}}\"";;
81 esac
82 }
83
84 config_set() {
85 export CONFIG_${1}_${2}="${3}"
86 }
87
88 load_modules() {
89 sed 's/^[^#]/insmod &/' $* | ash 2>&- || :
90 }
This page took 0.050432 seconds and 5 git commands to generate.