4df8f33ff4be76beb5af4355b307394c5a340715
[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 -a $? -ne 2 ] && {
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 [ "${package##.*}" != "$package" ] && continue # ignore .lock files
90 uci_commit "$package"
91 done
92 }
93
94 do_show() {
95 local PACKAGE
96 local CONFIG
97 local DUMMY
98
99 strtok "$*" PACKAGE . CONFIG $SEP DUMMY
100 [ $? -gt 2 ] && {
101 uci_usage show
102 exit 1
103 }
104
105 for package in ${PACKAGE:-$(cd $UCI_ROOT/etc/config; ls)}; do
106 SECTION=""
107
108 config_cb() {
109 if [ -z "$CONFIG" -o "$CONFIG" = "$2" ]; then
110 append SECTION "$2"
111 option_cb() {
112 append "${CONFIG_SECTION}_VARS" "$1"
113 }
114 else
115 option_cb() {
116 return 0
117 }
118 fi
119 }
120
121 uci_load "$package"
122
123 for section in $SECTION; do
124 config_get type "$section" TYPE
125 [ -z "$type" ] && continue
126 echo "$package.$section=$type"
127 eval "VARS=\"\${${section}_VARS}\""
128 for var in $VARS; do
129 config_get val "$section" "$var"
130 [ -n "$val" ] && {
131 echo "$package.$section.$var=$val"
132 config_set "$section" "$var" ""
133 }
134 done
135 config_set "$section" TYPE ""
136 done
137 done
138 }
139
140 do_validate() {
141 [ "$#" -ne 1 ] && {
142 uci_usage validate
143 exit 1
144 }
145 uci_validate "$1" || exit "$?"
146 }
147
148 uci_usage() {
149 case "$1" in
150 show) echo "$0 show [<package>[.<config>]]";;
151 get) echo "$0 get <package>.<config>.<option>";;
152 set) echo "$0 set <package>.<config>[.<option>]=<value>";;
153 del) echo "$0 del <package>.<config>[.<option>]";;
154 rename) echo "$0 rename <package> <config> <name>";;
155 commit) echo "$0 commit [<package> ... ]";;
156 validate) echo "$0 validate <package>";;
157 *)
158 echo "Syntax: $0 <command> <arguments...>"
159 echo
160 uci_usage show
161 uci_usage get
162 uci_usage set
163 uci_usage del
164 uci_usage rename
165 uci_usage commit
166 uci_usage validate
167 echo
168 exit 1
169 ;;
170 esac
171 }
172
173 if [ $# -eq 0 ] ; then
174 uci_usage
175 exit 0
176 fi
177
178 CMD="$1"
179 shift
180 case "$CMD" in
181 set) do_set "$@";;
182 del) do_remove "$@";;
183 rename) do_rename "$@";;
184 get) do_get "$@";;
185 show) do_show "$@";;
186 commit) do_commit "$@";;
187 validate) do_validate "$@";;
188 *) uci_usage;;
189 esac
190 exit 0