9b50380df2f831e00ecba44cef00f1c373cef7ae
[openwrt/openwrt.git] / package / base-files / files / bin / uci
1 #!/bin/sh
2 # Shell script for interacting with config files
3 #
4 # Copyright (C) 2006 by Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
5 # Copyright (C) 2006 by 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 . /etc/functions.sh
22 include /lib/config
23
24 SEP="[^0-9A-Za-z_]"
25
26 do_get() {
27 local PACKAGE
28 local CONFIG
29 local OPTION
30 local DUMMY
31
32 strtok "$*" PACKAGE . CONFIG . OPTION $SEP DUMMY
33
34 [ $? -ne 3 ] && {
35 uci_usage get
36 exit 1
37 }
38
39 uci_load "$PACKAGE"
40 config_get "$CONFIG" "$OPTION"
41 }
42
43 do_set() {
44 local PACKAGE
45 local CONFIG
46 local OPTION
47 local VALUE
48
49 strtok "$1" PACKAGE . CONFIG = VALUE
50 [ $? -ne 3 ] && {
51 uci_usage set
52 exit 1
53 }
54
55 strtok "$CONFIG" CONFIG . OPTION
56
57 if [ $? -eq 1 ]; then
58 uci_add "$PACKAGE" "$VALUE" "$CONFIG"
59 else
60 uci_set "$PACKAGE" "$CONFIG" "$OPTION" "$VALUE"
61 fi
62 }
63
64 do_rename() {
65 [ $# -ne 3 ] && {
66 uci_usage rename
67 exit 1
68 }
69 uci_rename "$@"
70 }
71
72 do_remove() {
73 local PACKAGE
74 local CONFIG
75 local OPTION
76 local DUMMY
77
78 strtok "$*" PACKAGE . CONFIG . OPTION $SEP DUMMY
79 [ $? -ne 3 -a $? -ne 2 ] && {
80 uci_usage rename
81 exit 1
82 }
83 uci_remove "$PACKAGE" "$CONFIG" ${OPTION:+"$OPTION"}
84 }
85
86 do_commit() {
87 local PACKAGE="$1"
88 for package in ${PACKAGE:-$(cd /tmp/.uci; ls)}; do
89 uci_commit "$package"
90 done
91 }
92
93 do_show() {
94 local PACKAGE
95 local CONFIG
96 local DUMMY
97
98 strtok "$*" PACKAGE . CONFIG $SEP DUMMY
99 [ $? -gt 2 ] && {
100 uci_usage show
101 exit 1
102 }
103
104 for package in ${PACKAGE:-$(cd /etc/config; ls)}; do
105 SECTION=""
106
107 config_cb() {
108 if [ -z "$CONFIG" -o "$CONFIG" = "$2" ]; then
109 append SECTION "$2"
110 option_cb() {
111 append "${CONFIG_SECTION}_VARS" "$1"
112 }
113 else
114 option_cb() {
115 return 0
116 }
117 fi
118 }
119
120 uci_load "$package"
121
122 for section in $SECTION; do
123 config_get type "$section" TYPE
124 [ -z "$type" ] && continue
125 echo "$package.$section=$type"
126 eval "VARS=\"\${${section}_VARS}\""
127 for var in $VARS; do
128 config_get val "$section" "$var"
129 [ -n "$val" ] && {
130 echo "$package.$section.$var=$val"
131 config_set "$section" "$var" ""
132 }
133 done
134 config_set "$section" TYPE ""
135 done
136 done
137 }
138
139 uci_usage() {
140 case "$1" in
141 show) echo "$0 show [<package>[.<config>]]";;
142 get) echo "$0 get <package>.<config>.<option>";;
143 set) echo "$0 set <package>.<config>[.<option>]=<value>";;
144 del) echo "$0 del <package>.<config>[.<option>]";;
145 rename) echo "$0 rename <package> <config> <name>";;
146 commit) echo "$0 commit [<package> ... ]";;
147 *)
148 echo "Syntax: $0 <command> <arguments...>"
149 echo
150 uci_usage show
151 uci_usage get
152 uci_usage set
153 uci_usage del
154 uci_usage rename
155 uci_usage commit
156 echo
157 exit 1
158 ;;
159 esac
160 }
161
162 if [ $# -eq 0 ] ; then
163 uci_usage
164 exit 0
165 fi
166
167 local CMD="$1"
168 shift
169 case "$CMD" in
170 set) do_set "$@";;
171 del) do_remove "$@";;
172 rename) do_rename "$@";;
173 get) do_get "$@";;
174 show) do_show "$@";;
175 commit) do_commit "$@";;
176 *) uci_usage;;
177 esac
178 exit 0