43bb981cc6bb0102236e5082d403648e29158acb
[openwrt/svn-archive/archive.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_do_update() {
32 local FILENAME="$1"
33 local UPDATE="$2"
34 awk -f $UCI_ROOT/lib/config/uci-update.awk -f - <<EOF
35 BEGIN {
36 config = read_file("$FILENAME")
37 $UPDATE
38 print config
39 }
40 EOF
41 }
42
43 uci_add_update() {
44 local PACKAGE="$1"
45 local UPDATE="$2"
46 local PACKAGE_BASE="$(basename "$PACKAGE")"
47
48 # FIXME: add locking?
49 mkdir -p "/tmp/.uci"
50 echo "$UPDATE" >> "/tmp/.uci/${PACKAGE_BASE}"
51 }
52
53 uci_set() {
54 local PACKAGE="$1"
55 local CONFIG="$2"
56 local OPTION="$3"
57 local VALUE="$4"
58
59 ( # spawn a subshell so you don't mess up the current environment
60 uci_load "$PACKAGE"
61 config_get type "$CONFIG" TYPE
62 [ -z "$type" ]
63 ) || uci_add_update "$PACKAGE" "CONFIG_SECTION='$CONFIG'${N}option '$OPTION' '$VALUE'"
64 }
65
66 uci_add() {
67 local PACKAGE="$1"
68 local TYPE="$2"
69 local CONFIG="$3"
70
71 uci_add_update "$PACKAGE" "config '$TYPE' '$CONFIG'"
72 }
73
74 uci_rename() {
75 local PACKAGE="$1"
76 local CONFIG="$2"
77 local VALUE="$3"
78
79 uci_add_update "$PACKAGE" "config_rename '$CONFIG' '$VALUE'"
80 }
81
82 uci_remove() {
83 local PACKAGE="$1"
84 local CONFIG="$2"
85 local OPTION="$3"
86
87 if [ -z "$OPTION" ]; then
88 uci_add_update "$PACKAGE" "config_clear '$CONFIG'"
89 else
90 uci_add_update "$PACKAGE" "config_unset '$CONFIG' '$OPTION'"
91 fi
92 }
93
94 uci_commit() {
95 local PACKAGE="$1"
96 local PACKAGE_BASE="$(basename "$PACKAGE")"
97
98 mkdir -p /tmp/.uci
99 LOCK=`which lock` || LOCK=:
100 $LOCK "/tmp/.uci/$PACKAGE_BASE.lock"
101 [ -f "/tmp/.uci/$PACKAGE_BASE" ] && (
102 updatestr=""
103
104 # replace handlers
105 config() {
106 append updatestr "config = update_config(config, \"@$2=$1\")" "$N"
107 }
108 option() {
109 append updatestr "config = update_config(config, \"$CONFIG_SECTION.$1=$2\")" "$N"
110 }
111 config_rename() {
112 append updatestr "config = update_config(config, \"&$1=$2\")" "$N"
113 }
114 config_unset() {
115 append updatestr "config = update_config(config, \"-$1.$2\")" "$N"
116 }
117 config_clear() {
118 append updatestr "config = update_config(config, \"-$1\")" "$N"
119 }
120
121 . "/tmp/.uci/$PACKAGE_BASE"
122
123 # completely disable handlers so that they don't get in the way
124 config() {
125 return 0
126 }
127 option() {
128 return 0
129 }
130
131 config_load "$PACKAGE"
132 CONFIG_FILENAME="${CONFIG_FILENAME:-$UCI_ROOT/etc/config/$PACKAGE_BASE}"
133 uci_do_update "$CONFIG_FILENAME" "$updatestr" > "/tmp/.uci/$PACKAGE_BASE.new" && {
134 mv -f "/tmp/.uci/$PACKAGE_BASE.new" "$CONFIG_FILENAME" && \
135 rm -f "/tmp/.uci/$PACKAGE_BASE"
136 }
137 )
138 $LOCK -u "/tmp/.uci/$PACKAGE_BASE.lock"
139 }
140
141