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