allow packages to optionally append config file contents in the environment by settin...
[openwrt.git] / package / uci / files / uci-sh / lib / config / uci.sh
1 #!/bin/sh
2 # Shell script defining macros for manipulating 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_set_default() {
22         local PACKAGE="$1"
23         [ -e "/etc/config/$1" ] && return 0
24         cat > "/etc/config/$1"
25 }
26
27 uci_load() {
28         local cfg
29         local uci
30
31         _C=0
32         export ${NO_EXPORT:+-n} CONFIG_SECTIONS=
33         export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=0
34         export ${NO_EXPORT:+-n} CONFIG_SECTION=
35
36         case "$PACKAGE" in
37                 /*) cfg="$PACKAGE";;
38                  *) 
39                         cfg="$UCI_ROOT/etc/config/$PACKAGE"
40                         uci="$UCI_ROOT/tmp/.uci/$PACKAGE"
41                         state="$UCI_ROOT/var/state/$PACKAGE"
42                 ;;
43         esac
44
45         # no config?
46         [ -z "$cfg" -o \! -f "$cfg" ] && return 1
47         . "$cfg"
48
49         ${CONFIG_SECTION:+config_cb}
50
51         [ -z "$uci" -o \! -f "$uci" ] || . "$uci"
52         [ -z "$LOAD_STATE" -z "$state" -o \! -f "$state" ] || . "$state"
53
54         return 0
55 }
56
57 uci_call_awk() {
58         local CMD="$*"
59         awk -f $UCI_ROOT/lib/config/uci.awk -f - <<EOF
60 BEGIN {
61         $CMD
62 }
63 EOF
64 }
65
66 uci_do_update() {
67         local FILENAME="$1"
68         local UPDATE="$2"
69         uci_call_awk "
70         config = read_file(\"$FILENAME\")
71         $UPDATE
72         print config
73 "
74 }
75
76 uci_add_update() {
77         local PACKAGE="$1"
78         local UPDATE="$2"
79         local PACKAGE_BASE="$(basename "$PACKAGE")"
80         local UCIFILE
81
82         case "$PACKAGE" in
83                 /*) UCIFILE="$PACKAGE";;
84                 *)
85                         UCIFILE="/tmp/.uci/$PACKAGE_BASE"
86                         mkdir -p "/tmp/.uci"
87                 ;;
88         esac
89
90         # FIXME: add locking?
91         echo "$UPDATE" >> "$UCIFILE"
92 }
93
94 uci_revert_state() {
95         local PACKAGE="$1"
96         local CONFIG="$2"
97         FILE="/var/state/$PACKAGE.$$"
98         grep -v "^config_set '$CONFIG' " "/var/state/$PAKAGE" > "$FILE"
99         mv "$FILE" "/var/state/$PACKAGE"
100 }
101
102 uci_set_state() {
103         local PACKAGE="$1"
104         local CONFIG="$2"
105         local OPTION="$3"
106         local VALUE="$4"
107
108         [ -z "$VALUE" ] && return 1
109         uci_set "/var/state/$PACKAGE" "$CONFIG" "$OPTION" "$VALUE"
110 }
111
112
113 uci_set() {
114         local PACKAGE="$1"
115         local CONFIG="$2"
116         local OPTION="$3"
117         local VALUE="$4"
118
119         case "$PACKAGE" in
120                 /*)
121                         uci_add_update "$PACKAGE" "config_set '$CONFIG' '$OPTION' '$VALUE'"
122                 ;;
123                 *)
124                         ( # spawn a subshell so you don't mess up the current environment
125                                 uci_load "$PACKAGE"
126                                 config_get OLDVAL "$CONFIG" "$OPTION"
127                                 if [ "x$OLDVAL" != "x$VALUE" ]; then
128                                         config_get type "$CONFIG" TYPE
129                                         [ -z "$type" ]
130                                 fi
131                         ) || uci_add_update "$PACKAGE" "config_set '$CONFIG' '$OPTION' '$VALUE'"
132                 ;;
133         esac
134 }
135
136 uci_add() {
137         local PACKAGE="$1"
138         local TYPE="$2"
139         local CONFIG="$3"
140
141         uci_add_update "$PACKAGE" "config '$TYPE' '$CONFIG'"
142 }
143
144 uci_rename() {
145         local PACKAGE="$1"
146         local CONFIG="$2"
147         local VALUE="$3"
148
149         uci_add_update "$PACKAGE" "config_rename '$CONFIG' '$VALUE'"
150 }
151
152 uci_remove() {
153         local PACKAGE="$1"
154         local CONFIG="$2"
155         local OPTION="$3"
156
157         if [ -z "$OPTION" ]; then
158                 uci_add_update "$PACKAGE" "config_clear '$CONFIG'"
159         else
160                 uci_add_update "$PACKAGE" "config_unset '$CONFIG' '$OPTION'"
161         fi
162 }
163
164 uci_commit() {
165         local PACKAGE="$1"
166         local PACKAGE_BASE="$(basename "$PACKAGE")"
167
168         case "$PACKAGE" in
169                 /*) return 0;;
170         esac
171         mkdir -p /tmp/.uci
172         LOCK=`which lock` || LOCK=:
173         $LOCK "/tmp/.uci/$PACKAGE_BASE.lock"
174         [ -f "/tmp/.uci/$PACKAGE_BASE" ] && (
175                 updatestr=""
176                 
177                 # replace handlers
178                 config() {
179                         append updatestr "config = uci_update_config(config, \"@$2=$1\")" "$N"
180                 }
181                 option() {
182                         append updatestr "config = uci_update_config(config, \"$CONFIG_SECTION.$1=$2\")" "$N"
183                 }
184                 config_rename() {
185                         append updatestr "config = uci_update_config(config, \"&$1=$2\")" "$N"
186                 }
187                 config_unset() {
188                         append updatestr "config = uci_update_config(config, \"-$1.$2\")" "$N"
189                 }
190                 config_clear() {
191                         append updatestr "config = uci_update_config(config, \"-$1\")" "$N"
192                 }
193                 
194                 . "/tmp/.uci/$PACKAGE_BASE"
195
196                 # completely disable handlers so that they don't get in the way
197                 config() {
198                         return 0
199                 }
200                 option() {
201                         return 0
202                 }
203                 
204                 config_load "$PACKAGE"
205                 CONFIG_FILENAME="${CONFIG_FILENAME:-$UCI_ROOT/etc/config/$PACKAGE_BASE}"
206                 uci_do_update "$CONFIG_FILENAME" "$updatestr" > "/tmp/.uci/$PACKAGE_BASE.new" && {
207                         mv -f "/tmp/.uci/$PACKAGE_BASE.new" "$CONFIG_FILENAME" && \
208                         rm -f "/tmp/.uci/$PACKAGE_BASE"
209                 } 
210         )
211         $LOCK -u "/tmp/.uci/$PACKAGE_BASE.lock"
212 }
213
214