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