add new command del_list
[project/uci.git] / sh / uci.sh
1 #!/bin/sh
2 # Copyright (C) 2006 Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
3 # Copyright (C) 2006-2008 OpenWrt.org
4
5 # newline
6 N="
7 "
8
9 _C=0
10 NO_EXPORT=1
11 LOAD_STATE=1
12 LIST_SEP=" "
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 list_contains() {
27         local var="$1"
28         local str="$2"
29         local val
30
31         eval "val=\" \${$var} \""
32         [ "${val%% $str *}" != "$val" ]
33 }
34
35 list_remove() {
36         local var="$1"
37         local remove="$2"
38         local val
39
40         eval "val=\" \${$var} \""
41         val1="${val%% $remove *}"
42         [ "$val1" = "$val" ] && return
43         val2="${val##* $remove }"
44         [ "$val2" = "$val" ] && return
45         val="${val1## } ${val2%% }"
46         val="${val%% }"
47         eval "export ${NO_EXPORT:+-n} -- \"$var=\$val\""
48 }
49
50 config_load() {
51         [ -n "$IPKG_INSTROOT" ] && return 0
52         uci_load "$@"
53 }
54
55 reset_cb() {
56         config_cb() { return 0; }
57         option_cb() { return 0; }
58         list_cb() { return 0; }
59 }
60 reset_cb
61
62 package() {
63         return 0
64 }
65
66 config () {
67         local cfgtype="$1"
68         local name="$2"
69         
70         export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1))
71         name="${name:-cfg$CONFIG_NUM_SECTIONS}"
72         append CONFIG_SECTIONS "$name"
73         [ -n "$NO_CALLBACK" ] || config_cb "$cfgtype" "$name"
74         export ${NO_EXPORT:+-n} CONFIG_SECTION="$name"
75         export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_TYPE=$cfgtype"
76 }
77
78 option () {
79         local varname="$1"; shift
80         local value="$*"
81
82         export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_${varname}=$value"
83         [ -n "$NO_CALLBACK" ] || option_cb "$varname" "$*"
84 }
85
86 list() {
87         local varname="$1"; shift
88         local value="$*"
89         local len
90
91         config_get len "$CONFIG_SECTION" "${varname}_LENGTH" 
92         len="$((${len:-0} + 1))"
93         config_set "$CONFIG_SECTION" "${varname}_ITEM$len" "$value"
94         config_set "$CONFIG_SECTION" "${varname}_LENGTH" "$len"
95         append "CONFIG_${CONFIG_SECTION}_${varname}" "$value" "$LIST_SEP"
96         list_cb "$varname" "$*"
97 }
98
99 config_unset() {
100         config_set "$1" "$2" ""
101 }
102
103 config_clear() {
104         local SECTION="$1"
105         local oldvar
106
107         list_remove CONFIG_SECTIONS "$SECTION"
108         export ${NO_EXPORT:+-n} CONFIG_SECTIONS="${SECTION:+$CONFIG_SECTIONS}"
109
110         for oldvar in `set | grep ^CONFIG_${SECTION:+${SECTION}_} | \
111                 sed -e 's/\(.*\)=.*$/\1/'` ; do 
112                 unset $oldvar 
113         done
114 }
115
116 config_get() {
117         case "$3" in
118                 "") eval "echo \"\${CONFIG_${1}_${2}}\"";;
119                 *)  eval "export ${NO_EXPORT:+-n} -- \"$1=\${CONFIG_${2}_${3}}\"";;
120         esac
121 }
122
123 # config_get_bool <variable> <section> <option> [<default>]
124 config_get_bool() {
125         local _tmp
126         config_get "_tmp" "$2" "$3"
127         case "$_tmp" in
128                 1|on|true|enabled) export ${NO_EXPORT:+-n} "$1=1";;
129                 0|off|false|disabled) export ${NO_EXPORT:+-n} "$1=0";;
130                 *) eval "$1=$4";;
131         esac
132 }
133
134 config_set() {
135         local section="$1"
136         local option="$2"
137         local value="$3"
138         local old_section="$CONFIG_SECTION"
139
140         CONFIG_SECTION="$section"
141         option "$option" "$value"
142         CONFIG_SECTION="$old_section"
143 }
144
145 config_foreach() {
146         local function="$1"
147         [ "$#" -ge 1 ] && shift
148         local type="$1"
149         [ "$#" -ge 1 ] && shift
150         local section cfgtype
151         
152         [ -z "$CONFIG_SECTIONS" ] && return 0
153         for section in ${CONFIG_SECTIONS}; do
154                 config_get cfgtype "$section" TYPE
155                 [ -n "$type" -a "x$cfgtype" != "x$type" ] && continue
156                 eval "$function \"\$section\" \"\$@\""
157         done
158 }
159
160 config_list_foreach() {
161         [ "$#" -ge 3 ] || return 0
162         local section="$1"; shift
163         local option="$1"; shift
164         local function="$1"; shift
165         local val
166         local len
167         local c=1
168
169         config_get len "${section}" "${option}_LENGTH"
170         [ -z "$len" ] && return 0
171         while [ $c -le "$len" ]; do
172                 config_get val "${section}" "${option}_ITEM$c"
173                 eval "$function \"\$val\" \"$@\""
174                 c="$(($c + 1))"
175         done
176 }
177
178