finally move buildroot-ng to trunk
[openwrt.git] / package / base-files / default / lib / config / uci-update.awk
1 # Configuration update functions
2 #
3 # Copyright (C) 2006 by Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
4 # Copyright (C) 2006 by Felix Fietkau <nbd@openwrt.org>
5 #
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.
10 #
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.
15 #
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
19
20
21 function read_file(filename, result) {
22 while ((getline <filename) == 1) {
23 result = result $0 "\n"
24 }
25 gsub(/\n*$/, "", result)
26 return result
27 }
28
29 function cmd2option(str, tmp) {
30 if (match(str,"=")!=0) {
31 res = "\toption " substr(str,1,RSTART-1) "\t'" substr(str,RSTART+1) "'"
32 } else {
33 res= ""
34 }
35 return res
36 }
37
38 function cmd2config(atype, aname) {
39 return "config \"" atype "\" \"" aname "\""
40 }
41
42 function update_config(cfg, update, \
43 lines, line, l, n, i, i2, section, scnt, remove, tmp, aidx, rest) {
44 scnt = 1
45 linecnt=split(cfg "\n", lines, "\n")
46
47 cfg = ""
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)
51 line = lines[n]
52 gsub(/^[ \t]*/, "", line)
53 gsub(/#.*$/, "", line)
54 i2 = 1
55 delete l
56 rest = line
57 while (length(rest)) {
58 if (match(rest, /[ \t\"]+/)) {
59 if (RSTART>1) {
60 l[i2] = substr(rest,1,RSTART-1)
61 i2++
62 }
63 aidx=index(rest,"\"")
64 if (aidx>=RSTART && aidx<=RSTART+RLENGTH) {
65 rest=substr(rest,aidx+1)
66 # find the end of the string
67 match(rest,/\"/)
68 l[i2]=substr(rest,1,RSTART-1)
69 i2++
70 }
71 rest=substr(rest,RSTART+RLENGTH)
72 } else {
73 l[i2] = rest
74 i2++
75 rest = ""
76 }
77 }
78 line = lines[n]
79
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
93 }
94 }
95 }
96
97 if (l[1] == "config") {
98 # look for all unset values
99 if (section != "") {
100 flag=0
101 if (update ~ "^" section "\\.") {
102 flag=1
103 gsub("^" section ".", "", update)
104 cfg = cfg cmd2option(update) "\n"
105
106 update = "-" section "." update
107 }
108 if (flag!=0) cfg = cfg "\n"
109 }
110
111 remove = ""
112 section = l[3]
113 if (!length(section)) {
114 section = "cfg" scnt
115 }
116 scnt++
117 if (update == "-" section) {
118 remove = "section"
119 update = ""
120 } else if (update ~ "^@" section "=") {
121 update = ""
122 } else if (update ~ "^&" section "=") {
123 gsub("^&" section "=", "", update)
124 line = cmd2config(l[2],update)
125 update = ""
126 }
127 }
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)
135 update = ""
136 }
137 }
138 if (remove == "") cfg = cfg line "\n"
139 }
140
141 # any new options for the last section??
142 if (section != "") {
143 if (update ~ "^" section "\\.") {
144 gsub("^" section ".", "", update)
145 cfg = cfg cmd2option(update) "\n"
146
147 update = "-" section "." update
148 }
149 }
150
151 if (update ~ "^@") {
152 # new section
153 section = stype = substr(update,2)
154 gsub(/=.*$/,"",section)
155 gsub(/^.*=/,"",stype)
156 cfg = cfg "\nconfig \"" stype "\" \"" section "\"\n"
157 }
158
159 return cfg
160 }
This page took 0.049325 seconds and 5 git commands to generate.