[adm5120] refresh .23 kernel paches
[openwrt.git] / package / base-files / files / 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_load() {
22         config_load "$1"
23 }
24
25 uci_apply_defaults() {(
26         cd /etc/uci-defaults || return 0
27         files="$(ls)"
28         [ -z "$files" ] && return 0
29         mkdir -p /tmp/.uci
30         for file in $files; do
31                 ( . "./$(basename $file)" ) && rm -f "$file"
32         done
33         uci commit
34 )}
35
36 uci_call_awk() {
37         local CMD="$*"
38         awk -f $UCI_ROOT/lib/config/uci.awk -f - <<EOF
39 BEGIN {
40         $CMD
41 }
42 EOF
43 }
44
45 uci_do_update() {
46         local FILENAME="$1"
47         local UPDATE="$2"
48         uci_call_awk "
49         config = read_file(\"$FILENAME\")
50         $UPDATE
51         print config
52 "
53 }
54
55 uci_add_update() {
56         local PACKAGE="$1"
57         local UPDATE="$2"
58         local PACKAGE_BASE="$(basename "$PACKAGE")"
59         local UCIFILE
60
61         case "$PACKAGE" in
62                 /*) UCIFILE="$PACKAGE";;
63                 *)
64                         UCIFILE="/tmp/.uci/$PACKAGE_BASE"
65                         mkdir -p "/tmp/.uci"
66                 ;;
67         esac
68
69         # FIXME: add locking?
70         echo "$UPDATE" >> "$UCIFILE"
71 }
72
73 uci_set() {
74         local PACKAGE="$1"
75         local CONFIG="$2"
76         local OPTION="$3"
77         local VALUE="$4"
78
79         case "$PACKAGE" in
80                 /*)
81                         uci_add_update "$PACKAGE" "config_set '$CONFIG' '$OPTION' '$VALUE'"
82                 ;;
83                 *)
84                         ( # spawn a subshell so you don't mess up the current environment
85                                 uci_load "$PACKAGE"
86                                 config_get OLDVAL "$CONFIG" "$OPTION"
87                                 if [ "x$OLDVAL" != "x$VALUE" ]; then
88                                         config_get type "$CONFIG" TYPE
89                                         [ -z "$type" ]
90                                 fi
91                         ) || uci_add_update "$PACKAGE" "config_set '$CONFIG' '$OPTION' '$VALUE'"
92                 ;;
93         esac
94 }
95
96 uci_add() {
97         local PACKAGE="$1"
98         local TYPE="$2"
99         local CONFIG="$3"
100
101         uci_add_update "$PACKAGE" "config '$TYPE' '$CONFIG'"
102 }
103
104 uci_rename() {
105         local PACKAGE="$1"
106         local CONFIG="$2"
107         local VALUE="$3"
108
109         uci_add_update "$PACKAGE" "config_rename '$CONFIG' '$VALUE'"
110 }
111
112 uci_remove() {
113         local PACKAGE="$1"
114         local CONFIG="$2"
115         local OPTION="$3"
116
117         if [ -z "$OPTION" ]; then
118                 uci_add_update "$PACKAGE" "config_clear '$CONFIG'"
119         else
120                 uci_add_update "$PACKAGE" "config_unset '$CONFIG' '$OPTION'"
121         fi
122 }
123
124 uci_commit() {
125         local PACKAGE="$1"
126         local PACKAGE_BASE="$(basename "$PACKAGE")"
127
128         case "$PACKAGE" in
129                 /*) return 0;;
130         esac
131         mkdir -p /tmp/.uci
132         LOCK=`which lock` || LOCK=:
133         $LOCK "/tmp/.uci/$PACKAGE_BASE.lock"
134         [ -f "/tmp/.uci/$PACKAGE_BASE" ] && (
135                 updatestr=""
136                 
137                 # replace handlers
138                 config() {
139                         append updatestr "config = uci_update_config(config, \"@$2=$1\")" "$N"
140                 }
141                 option() {
142                         append updatestr "config = uci_update_config(config, \"$CONFIG_SECTION.$1=$2\")" "$N"
143                 }
144                 config_rename() {
145                         append updatestr "config = uci_update_config(config, \"&$1=$2\")" "$N"
146                 }
147                 config_unset() {
148                         append updatestr "config = uci_update_config(config, \"-$1.$2\")" "$N"
149                 }
150                 config_clear() {
151                         append updatestr "config = uci_update_config(config, \"-$1\")" "$N"
152                 }
153                 
154                 . "/tmp/.uci/$PACKAGE_BASE"
155
156                 # completely disable handlers so that they don't get in the way
157                 config() {
158                         return 0
159                 }
160                 option() {
161                         return 0
162                 }
163                 
164                 config_load "$PACKAGE"
165                 CONFIG_FILENAME="${CONFIG_FILENAME:-$UCI_ROOT/etc/config/$PACKAGE_BASE}"
166                 uci_do_update "$CONFIG_FILENAME" "$updatestr" > "/tmp/.uci/$PACKAGE_BASE.new" && {
167                         mv -f "/tmp/.uci/$PACKAGE_BASE.new" "$CONFIG_FILENAME" && \
168                         rm -f "/tmp/.uci/$PACKAGE_BASE"
169                 } 
170         )
171         $LOCK -u "/tmp/.uci/$PACKAGE_BASE.lock"
172 }
173
174