Add an 'Image Configuration' menu to menuconfig
[openwrt.org/openwrt.git] / package / base-files / files / bin / uci
nbd 1 #!/bin/sh
2 # Shell script for interacting with config files
3 #
nbd 4 # Copyright (C) 2006 Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
5 # Copyright (C) 2006,2007 Felix Fietkau <nbd@openwrt.org>
nbd 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
nbd 21 . $UCI_ROOT/etc/functions.sh
22 include $UCI_ROOT/lib/config
nbd 23
mbm 24 SEP="[^0-9A-Za-z_]"
25
nbd 26 do_get() {
mbm 27 local PACKAGE
28 local CONFIG
29 local OPTION
30 local DUMMY
31
32 strtok "$*" PACKAGE . CONFIG . OPTION $SEP DUMMY
33
34 [ $? -ne 3 ] && {
nbd 35 uci_usage get
36 exit 1
37 }
38
mbm 39 uci_load "$PACKAGE"
nbd 40 config_get "$CONFIG" "$OPTION"
41 }
42
43 do_set() {
mbm 44 local PACKAGE
45 local CONFIG
46 local OPTION
47 local VALUE
nbd 48
mbm 49 strtok "$1" PACKAGE . CONFIG = VALUE
nbd 50 [ $? -ne 3 -a $? -ne 2 ] && {
mbm 51 uci_usage set
nbd 52 exit 1
53 }
mbm 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
nbd 62 }
63
64 do_rename() {
65 [ $# -ne 3 ] && {
66 uci_usage rename
67 exit 1
68 }
69 uci_rename "$@"
70 }
71
72 do_remove() {
mbm 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 ] && {
nbd 80 uci_usage rename
81 exit 1
82 }
mbm 83 uci_remove "$PACKAGE" "$CONFIG" ${OPTION:+"$OPTION"}
nbd 84 }
85
86 do_commit() {
mbm 87 local PACKAGE="$1"
88 for package in ${PACKAGE:-$(cd /tmp/.uci; ls)}; do
nbd 89 [ "${package##.*}" != "$package" ] && continue # ignore .lock files
mbm 90 uci_commit "$package"
91 done
nbd 92 }
93
94 do_show() {
mbm 95 local PACKAGE
96 local CONFIG
97 local DUMMY
98
99 strtok "$*" PACKAGE . CONFIG $SEP DUMMY
100 [ $? -gt 2 ] && {
nbd 101 uci_usage show
102 exit 1
103 }
104
nbd 105 for package in ${PACKAGE:-$(cd $UCI_ROOT/etc/config; ls)}; do
mbm 106 SECTION=""
nbd 107
mbm 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 }
nbd 120
mbm 121 uci_load "$package"
nbd 122
mbm 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 ""
nbd 136 done
137 done
138 }
139
nbd 140 do_validate() {
141 [ "$#" -ne 1 ] && {
142 uci_usage validate
143 exit 1
144 }
145 uci_validate "$1" || exit "$?"
146 }
147
nbd 148 uci_usage() {
149 case "$1" in
mbm 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>]";;
nbd 154 rename) echo "$0 rename <package> <config> <name>";;
mbm 155 commit) echo "$0 commit [<package> ... ]";;
nbd 156 validate) echo "$0 validate <package>";;
nbd 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
nbd 166 uci_usage validate
nbd 167 echo
168 exit 1
169 ;;
170 esac
171 }
172
173 if [ $# -eq 0 ] ; then
174 uci_usage
175 exit 0
176 fi
mbm 177
nbd 178 CMD="$1"
nbd 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 "$@";;
nbd 187 validate) do_validate "$@";;
nbd 188 *) uci_usage;;
189 esac
190 exit 0
191
comments