1 # Configuration update functions - AWK API
3 # Copyright (C) 2006 by Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
4 # Copyright (C) 2006 by Felix Fietkau <nbd@openwrt.org>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 function config_load(package, var) {
22 while (("/bin/ash -c '. /etc/functions.sh; unset NO_EXPORT; config_load \""package"\"; env | grep \"^CONFIG_\"'" | getline) == 1) {
24 if (match($0, "=") == 0) {
25 if (var != "") CONFIG[var] = CONFIG[var] "\n" $0
28 var=substr($0, 1, RSTART-1)
29 CONFIG[var] = substr($0, RSTART+1, length($0) - RSTART)
34 function config_get(package, option) {
35 return CONFIG[package "_" option]
39 function config_get_bool(package, option, default, var) {
40 var = config_get(package, option);
41 if ((var == "enabled") || (var == "true") || (var == "1") || (var == "on")) return 1
42 if ((var == "disabled") || (var == "false") || (var == "0") || (var == "off")) return 1
43 return (var && var != "0" ? 1 : 0)
47 function read_file(filename, result) {
48 while ((getline <filename) == 1) {
49 result = result $0 "\n"
51 gsub(/\n*$/, "", result)
55 function uci_cmd2option(str, tmp) {
56 if (match(str,"=")!=0) {
57 res = "\toption " substr(str,1,RSTART-1) "\t'" substr(str,RSTART+1) "'"
64 function uci_cmd2config(atype, aname) {
65 return "config \"" atype "\" \"" aname "\""
68 function uci_update_config(cfg, update, \
69 lines, line, l, n, i, i2, section, scnt, remove, tmp, aidx, rest) {
71 linecnt=split(cfg "\n", lines, "\n")
74 for (n = 1; n < linecnt; n++) {
75 # stupid parser for quoted arguments (e.g. for the type string).
76 # not to be used to gather variable values (backslash escaping doesn't work)
78 gsub(/^[ \t]*/, "", line)
79 gsub(/#.*$/, "", line)
83 while (length(rest)) {
84 if (match(rest, /[ \t\"]+/)) {
86 l[i2] = substr(rest,1,RSTART-1)
90 if (aidx>=RSTART && aidx<=RSTART+RLENGTH) {
91 rest=substr(rest,aidx+1)
92 # find the end of the string
94 l[i2]=substr(rest,1,RSTART-1)
97 rest=substr(rest,RSTART+RLENGTH)
106 # when a command wants to set a config value for the current
107 # section and a blank line is encountered before an option with
108 # the same name, insert it here to maintain some coherency between
109 # manually and automatically created option lines
110 # if an option with the same name appears after this point, simply
111 # ignore it, because it is already set.
112 if ((section != "") && (l[1] != "option")) {
113 if (line ~ /^[ \t]*$/) {
114 if (update ~ "^" section "\\.") {
115 gsub("^" section ".", "", update)
116 cfg = cfg uci_cmd2option(update) "\n"
117 gsub(/=.*$/, "", update)
118 update = "-" section "." update
123 if (l[1] == "config") {
124 # look for all unset values
127 if (update ~ "^" section "\\.") {
129 gsub("^" section ".", "", update)
130 cfg = cfg uci_cmd2option(update) "\n"
132 update = "-" section "." update
134 if (flag!=0) cfg = cfg "\n"
139 if (!length(section)) {
143 if (update == "-" section) {
146 } else if (update ~ "^@" section "=") {
148 } else if (update ~ "^&" section "=") {
149 gsub("^&" section "=", "", update)
150 line = uci_cmd2config(l[2],update)
154 if (remove == "option") remove = ""
155 if (l[1] == "option") {
156 if (update ~ "^-" section "\\." l[2] "$") remove = "option"
157 # if a supplied config value already exists, replace the whole line
158 if (match(update, "^" section "." l[2] "=")) {
159 gsub("^" section ".", "", update)
160 line = uci_cmd2option(update)
164 if (remove == "") cfg = cfg line "\n"
167 # any new options for the last section??
169 if (update ~ "^" section "\\.") {
170 gsub("^" section ".", "", update)
171 cfg = cfg uci_cmd2option(update) "\n"
173 update = "-" section "." update
179 section = stype = substr(update,2)
180 gsub(/=.*$/,"",section)
181 gsub(/^.*=/,"",stype)
182 cfg = cfg "\nconfig \"" stype "\" \"" section "\"\n"