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