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