uci: add missing 'option' support to uci_rename()
[openwrt/openwrt.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_load() {
23 local PACKAGE="$1"
24 local DATA
25 local RET
26 local VAR
27
28 _C=0
29 if [ -z "$CONFIG_APPEND" ]; then
30 for VAR in $CONFIG_LIST_STATE; do
31 export ${NO_EXPORT:+-n} CONFIG_${VAR}=
32 export ${NO_EXPORT:+-n} CONFIG_${VAR}_LENGTH=
33 done
34 export ${NO_EXPORT:+-n} CONFIG_LIST_STATE=
35 export ${NO_EXPORT:+-n} CONFIG_SECTIONS=
36 export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=0
37 export ${NO_EXPORT:+-n} CONFIG_SECTION=
38 fi
39
40 DATA="$(/sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} ${LOAD_STATE:+-P /var/state} -S -n export "$PACKAGE" 2>/dev/null)"
41 RET="$?"
42 [ "$RET" != 0 -o -z "$DATA" ] || eval "$DATA"
43 unset DATA
44
45 ${CONFIG_SECTION:+config_cb}
46 return "$RET"
47 }
48
49 uci_set_default() {
50 local PACKAGE="$1"
51 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -q show "$PACKAGE" > /dev/null && return 0
52 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} import "$PACKAGE"
53 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} commit "$PACKAGE"
54 }
55
56 uci_revert_state() {
57 local PACKAGE="$1"
58 local CONFIG="$2"
59 local OPTION="$3"
60
61 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -P /var/state revert "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
62 }
63
64 uci_set_state() {
65 local PACKAGE="$1"
66 local CONFIG="$2"
67 local OPTION="$3"
68 local VALUE="$4"
69
70 [ "$#" = 4 ] || return 0
71 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -P /var/state set "$PACKAGE.$CONFIG${OPTION:+.$OPTION}=$VALUE"
72 }
73
74 uci_toggle_state() {
75 uci_revert_state "$1" "$2" "$3"
76 uci_set_state "$1" "$2" "$3" "$4"
77 }
78
79 uci_set() {
80 local PACKAGE="$1"
81 local CONFIG="$2"
82 local OPTION="$3"
83 local VALUE="$4"
84
85 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} set "$PACKAGE.$CONFIG.$OPTION=$VALUE"
86 }
87
88 uci_get_state() {
89 uci_get "$1" "$2" "$3" "$4" "/var/state"
90 }
91
92 uci_get() {
93 local PACKAGE="$1"
94 local CONFIG="$2"
95 local OPTION="$3"
96 local DEFAULT="$4"
97 local STATE="$5"
98
99 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} ${STATE:+-P $STATE} -q get "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
100 RET="$?"
101 [ "$RET" -ne 0 ] && [ -n "$DEFAULT" ] && echo "$DEFAULT"
102 return "$RET"
103 }
104
105 uci_add() {
106 local PACKAGE="$1"
107 local TYPE="$2"
108 local CONFIG="$3"
109
110 if [ -z "$CONFIG" ]; then
111 export ${NO_EXPORT:+-n} CONFIG_SECTION="$(/sbin/uci add "$PACKAGE" "$TYPE")"
112 else
113 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} set "$PACKAGE.$CONFIG=$TYPE"
114 export ${NO_EXPORT:+-n} CONFIG_SECTION="$CONFIG"
115 fi
116 }
117
118 uci_rename() {
119 local PACKAGE="$1"
120 local CONFIG="$2"
121 local OPTION="$3"
122 local VALUE="$4"
123
124 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} rename "$PACKAGE.$CONFIG${VALUE:+.$OPTION}=${VALUE:-$OPTION}"
125 }
126
127 uci_remove() {
128 local PACKAGE="$1"
129 local CONFIG="$2"
130 local OPTION="$3"
131
132 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} del "$PACKAGE.$CONFIG${OPTION:+.$OPTION}"
133 }
134
135 uci_commit() {
136 local PACKAGE="$1"
137 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} commit $PACKAGE
138 }