finally move buildroot-ng to trunk
[openwrt/staging/stintel.git] / package / base-files / default / lib / config / uci.sh
1 #!/bin/sh
2 # Shell script defining macros for manipulating config files
3 #
4 # Copyright (C) 2006 by Fokus Fraunhofer <carsten.tittel@fokus.fraumhofer.de>
5 # Copyright (C) 2006 by 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 "$PACKAGE"
23 local PACKAGE_BASE="$(basename "$PACKAGE")"
24 [ -f "/tmp/.uci/${PACKAGE_BASE}" ] && {
25 . "/tmp/.uci/${PACKAGE_BASE}"
26 config_cb
27 }
28 }
29
30 uci_do_update() {
31 local FILENAME="$1"
32 local UPDATE="$2"
33 awk -f /lib/config/uci-update.awk -f - <<EOF
34 BEGIN {
35 config = read_file("$FILENAME")
36 $UPDATE
37 print config
38 }
39 EOF
40 }
41
42 uci_add_update() {
43 local PACKAGE="$1"
44 local UPDATE="$2"
45 local PACKAGE_BASE="$(basename "$PACKAGE")"
46
47 # FIXME: add locking?
48 mkdir -p "/tmp/.uci"
49 echo "$UPDATE" >> "/tmp/.uci/${PACKAGE_BASE}"
50 }
51
52 uci_set() {
53 local PACKAGE="$1"
54 local CONFIG="$2"
55 local OPTION="$3"
56 local VALUE="$4"
57
58 ( # spawn a subshell so you don't mess up the current environment
59 uci_load "$PACKAGE"
60 config_get type "$CONFIG" TYPE
61 [ -z "$type" ]
62 ) || uci_add_update "$PACKAGE" "CONFIG_SECTION='$CONFIG'${N}option '$OPTION' '$VALUE'"
63 }
64
65 uci_add() {
66 local PACKAGE="$1"
67 local TYPE="$2"
68 local CONFIG="$3"
69
70 uci_add_update "$PACKAGE" "config '$TYPE' '$CONFIG'"
71 }
72
73 uci_rename() {
74 local PACKAGE="$1"
75 local CONFIG="$2"
76 local VALUE="$3"
77
78 uci_add_update "$PACKAGE" "config_rename '$CONFIG' '$VALUE'"
79 }
80
81 uci_remove() {
82 local PACKAGE="$1"
83 local CONFIG="$2"
84 local OPTION="$3"
85
86 if [ -z "$OPTION" ]; then
87 uci_add_update "$PACKAGE" "config_clear '$CONFIG'"
88 else
89 uci_add_update "$PACKAGE" "config_unset '$CONFIG' '$OPTION'"
90 fi
91 }
92
93 uci_commit() {
94 local PACKAGE="$1"
95 local PACKAGE_BASE="$(basename "$PACKAGE")"
96
97 mkdir -p /tmp/.uci
98 lock "/tmp/.uci/$PACKAGE_BASE.lock"
99 [ -f "/tmp/.uci/$PACKAGE_BASE" ] && (
100 updatestr=""
101
102 # replace handlers
103 config() {
104 append updatestr "config = update_config(config, \"@$2=$1\")" "$N"
105 }
106 option() {
107 append updatestr "config = update_config(config, \"$CONFIG_SECTION.$1=$2\")" "$N"
108 }
109 config_rename() {
110 append updatestr "config = update_config(config, \"&$1=$2\")" "$N"
111 }
112 config_unset() {
113 append updatestr "config = update_config(config, \"-$1.$2\")" "$N"
114 }
115 config_clear() {
116 append updatestr "config = update_config(config, \"-$1\")" "$N"
117 }
118
119 . "/tmp/.uci/$PACKAGE_BASE"
120
121 # completely disable handlers so that they don't get in the way
122 config() {
123 return 0
124 }
125 option() {
126 return 0
127 }
128
129 config_load "$PACKAGE" || CONFIG_FILENAME="$ROOT/etc/config/$PACKAGE_BASE"
130 uci_do_update "$CONFIG_FILENAME" "$updatestr" > "/tmp/.uci/$PACKAGE_BASE.new" && {
131 mv -f "/tmp/.uci/$PACKAGE_BASE.new" "$CONFIG_FILENAME" && \
132 rm -f "/tmp/.uci/$PACKAGE_BASE"
133 }
134 )
135 lock -u "/tmp/.uci/$PACKAGE_BASE.lock"
136 }
137
138