f8e08f87470487e15eba836b4e7bf7562cfdae8e
[openwrt/svn-archive/archive.git] / package / base-files / files / bin / uci
1 #!/bin/sh
2 # Shell script for interacting with config files
3 #
4 # Copyright (C) 2006 Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
5 # Copyright (C) 2006,2007 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 . $UCI_ROOT/etc/functions.sh
22 include $UCI_ROOT/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 $UCI_ROOT/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 do_validate() {
140 [ "$#" -ne 1 ] && {
141 uci_usage validate
142 exit 1
143 }
144 uci_validate "$1" || exit "$?"
145 }
146
147 uci_usage() {
148 case "$1" in
149 show) echo "$0 show [<package>[.<config>]]";;
150 get) echo "$0 get <package>.<config>.<option>";;
151 set) echo "$0 set <package>.<config>[.<option>]=<value>";;
152 del) echo "$0 del <package>.<config>[.<option>]";;
153 rename) echo "$0 rename <package> <config> <name>";;
154 commit) echo "$0 commit [<package> ... ]";;
155 validate) echo "$0 validate <package>";;
156 *)
157 echo "Syntax: $0 <command> <arguments...>"
158 echo
159 uci_usage show
160 uci_usage get
161 uci_usage set
162 uci_usage del
163 uci_usage rename
164 uci_usage commit
165 uci_usage validate
166 echo
167 exit 1
168 ;;
169 esac
170 }
171
172 if [ $# -eq 0 ] ; then
173 uci_usage
174 exit 0
175 fi
176
177 CMD="$1"
178 shift
179 case "$CMD" in
180 set) do_set "$@";;
181 del) do_remove "$@";;
182 rename) do_rename "$@";;
183 get) do_get "$@";;
184 show) do_show "$@";;
185 commit) do_commit "$@";;
186 validate) do_validate "$@";;
187 *) uci_usage;;
188 esac
189 exit 0