1 # Configuration update functions
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 read_file(filename, result) {
22 while ((getline <filename) == 1) {
23 result = result $0 "\n"
25 gsub(/\n*$/, "", result)
29 function cmd2option(str, tmp) {
30 if (match(str,"=")!=0) {
31 res = "\toption " substr(str,1,RSTART-1) "\t'" substr(str,RSTART+1) "'"
38 function cmd2config(atype, aname) {
39 return "config \"" atype "\" \"" aname "\""
42 function update_config(cfg, update, \
43 lines, line, l, n, i, i2, section, scnt, remove, tmp, aidx, rest) {
45 linecnt=split(cfg "\n", lines, "\n")
48 for (n = 1; n < linecnt; n++) {
49 # stupid parser for quoted arguments (e.g. for the type string).
50 # not to be used to gather variable values (backslash escaping doesn't work)
52 gsub(/^[ \t]*/, "", line)
53 gsub(/#.*$/, "", line)
57 while (length(rest)) {
58 if (match(rest, /[ \t\"]+/)) {
60 l[i2] = substr(rest,1,RSTART-1)
64 if (aidx>=RSTART && aidx<=RSTART+RLENGTH) {
65 rest=substr(rest,aidx+1)
66 # find the end of the string
68 l[i2]=substr(rest,1,RSTART-1)
71 rest=substr(rest,RSTART+RLENGTH)
80 # when a command wants to set a config value for the current
81 # section and a blank line is encountered before an option with
82 # the same name, insert it here to maintain some coherency between
83 # manually and automatically created option lines
84 # if an option with the same name appears after this point, simply
85 # ignore it, because it is already set.
86 if ((section != "") && (l[1] != "option")) {
87 if (line ~ /^[ \t]*$/) {
88 if (update ~ "^" section "\\.") {
89 gsub("^" section ".", "", update)
90 cfg = cfg cmd2option(update) "\n"
91 gsub(/=.*$/, "", update)
92 update = "-" section "." update
97 if (l[1] == "config") {
98 # look for all unset values
101 if (update ~ "^" section "\\.") {
103 gsub("^" section ".", "", update)
104 cfg = cfg cmd2option(update) "\n"
106 update = "-" section "." update
108 if (flag!=0) cfg = cfg "\n"
113 if (!length(section)) {
117 if (update == "-" section) {
120 } else if (update ~ "^@" section "=") {
122 } else if (update ~ "^&" section "=") {
123 gsub("^&" section "=", "", update)
124 line = cmd2config(l[2],update)
128 if (remove == "option") remove = ""
129 if (l[1] == "option") {
130 if (update ~ "^-" section "\\." l[2] "$") remove = "option"
131 # if a supplied config value already exists, replace the whole line
132 if (match(update, "^" section "." l[2] "=")) {
133 gsub("^" section ".", "", update)
134 line = cmd2option(update)
138 if (remove == "") cfg = cfg line "\n"
141 # any new options for the last section??
143 if (update ~ "^" section "\\.") {
144 gsub("^" section ".", "", update)
145 cfg = cfg cmd2option(update) "\n"
147 update = "-" section "." update
153 section = stype = substr(update,2)
154 gsub(/=.*$/,"",section)
155 gsub(/^.*=/,"",stype)
156 cfg = cfg "\nconfig \"" stype "\" \"" section "\"\n"