uci: add host build
[openwrt/staging/yousong.git] / package / system / uci / files / lib / config / uci.sh
1 #!/bin/sh
2 # Shell script compatibility wrappers for /sbin/uci
3 #
4 # Copyright (C) 2008-2010 OpenWrt.org
5 # Copyright (C) 2008 Felix Fietkau <nbd@nbd.name>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
21 CONFIG_APPEND=
22 UCI="${UCI:-/sbin/uci}"
23 UCI_STATE_DIR="${IPKG_INSTROOT}/var/state}"
24
25 uci_load() {
26 local PACKAGE="$1"
27 local DATA
28 local RET
29 local VAR
30
31 _C=0
32 if [ -z "$CONFIG_APPEND" ]; then
33 for VAR in $CONFIG_LIST_STATE; do
34 export ${NO_EXPORT:+-n} CONFIG_${VAR}=
35 export ${NO_EXPORT:+-n} CONFIG_${VAR}_LENGTH=
36 done
37 export ${NO_EXPORT:+-n} CONFIG_LIST_STATE=
38 export ${NO_EXPORT:+-n} CONFIG_SECTIONS=
39 export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=0
40 export ${NO_EXPORT:+-n} CONFIG_SECTION=
41 fi
42
43 DATA="$($UCI ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} ${LOAD_STATE:+-P "$UCI_STATE_DIR"} -S -n export "$PACKAGE" 2>/dev/null)"
44 RET="$?"
45 [ "$RET" != 0 -o -z "$DATA" ] || eval "$DATA"
46 unset DATA
47
48 ${CONFIG_SECTION:+config_cb}
49 return "$RET"
50 }
51
52 uci_set_default() {
53 local PACKAGE="$1"
54 $UCI ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -q show "$PACKAGE" > /dev/null && return 0
55 $UCI ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} import "$PACKAGE"
56 $UCI ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} commit "$PACKAGE"
57 }
58
59 uci_revert_state() {
60 local PACKAGE="$1"
61 local CONFIG="$2"
62 local OPTION="$3"
63
64 $UCI ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -P "$UCI_STATE_DIR" revert "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
65 }
66
67 uci_set_state() {
68 local PACKAGE="$1"
69 local CONFIG="$2"
70 local OPTION="$3"
71 local VALUE="$4"
72
73 [ "$#" = 4 ] || return 0
74 $UCI ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -P "$UCI_STATE_DIR" set "$PACKAGE.$CONFIG${OPTION:+.$OPTION}=$VALUE"
75 }
76
77 uci_toggle_state() {
78 uci_revert_state "$1" "$2" "$3"
79 uci_set_state "$1" "$2" "$3" "$4"
80 }
81
82 uci_set() {
83 local PACKAGE="$1"
84 local CONFIG="$2"
85 local OPTION="$3"
86 local VALUE="$4"
87
88 $UCI ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} set "$PACKAGE.$CONFIG.$OPTION=$VALUE"
89 }
90
91 uci_get_state() {
92 uci_get "$1" "$2" "$3" "$4" ""$UCI_STATE_DIR""
93 }
94
95 uci_get() {
96 local PACKAGE="$1"
97 local CONFIG="$2"
98 local OPTION="$3"
99 local DEFAULT="$4"
100 local STATE="$5"
101
102 $UCI ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} ${STATE:+-P $STATE} -q get "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
103 RET="$?"
104 [ "$RET" -ne 0 ] && [ -n "$DEFAULT" ] && echo "$DEFAULT"
105 return "$RET"
106 }
107
108 uci_add() {
109 local PACKAGE="$1"
110 local TYPE="$2"
111 local CONFIG="$3"
112
113 if [ -z "$CONFIG" ]; then
114 export ${NO_EXPORT:+-n} CONFIG_SECTION="$($UCI add "$PACKAGE" "$TYPE")"
115 else
116 $UCI ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} set "$PACKAGE.$CONFIG=$TYPE"
117 export ${NO_EXPORT:+-n} CONFIG_SECTION="$CONFIG"
118 fi
119 }
120
121 uci_rename() {
122 local PACKAGE="$1"
123 local CONFIG="$2"
124 local VALUE="$3"
125
126 $UCI ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} rename "$PACKAGE.$CONFIG=$VALUE"
127 }
128
129 uci_remove() {
130 local PACKAGE="$1"
131 local CONFIG="$2"
132 local OPTION="$3"
133
134 $UCI ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} del "$PACKAGE.$CONFIG${OPTION:+.$OPTION}"
135 }
136
137 uci_commit() {
138 local PACKAGE="$1"
139 $UCI ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} commit $PACKAGE
140 }