parselist.awk revision 1.9 1 1.9 lukem # $NetBSD: parselist.awk,v 1.9 2002/03/14 22:26:09 lukem Exp $
2 1.1 lukem #
3 1.1 lukem # Copyright (c) 2002 The NetBSD Foundation, Inc.
4 1.1 lukem # All rights reserved.
5 1.1 lukem #
6 1.1 lukem # This code is derived from software contributed to The NetBSD Foundation
7 1.1 lukem # by Luke Mewburn of Wasabi Systems.
8 1.1 lukem #
9 1.1 lukem # Redistribution and use in source and binary forms, with or without
10 1.1 lukem # modification, are permitted provided that the following conditions
11 1.1 lukem # are met:
12 1.1 lukem # 1. Redistributions of source code must retain the above copyright
13 1.1 lukem # notice, this list of conditions and the following disclaimer.
14 1.1 lukem # 2. Redistributions in binary form must reproduce the above copyright
15 1.1 lukem # notice, this list of conditions and the following disclaimer in the
16 1.1 lukem # documentation and/or other materials provided with the distribution.
17 1.1 lukem # 3. All advertising materials mentioning features or use of this software
18 1.1 lukem # must display the following acknowledgement:
19 1.1 lukem # This product includes software developed by the NetBSD
20 1.1 lukem # Foundation, Inc. and its contributors.
21 1.1 lukem # 4. Neither the name of The NetBSD Foundation nor the names of its
22 1.1 lukem # contributors may be used to endorse or promote products derived
23 1.1 lukem # from this software without specific prior written permission.
24 1.1 lukem #
25 1.1 lukem # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 1.1 lukem # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 lukem # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 lukem # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 1.1 lukem # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 lukem # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 lukem # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 lukem # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 lukem # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 lukem # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 lukem # POSSIBILITY OF SUCH DAMAGE.
36 1.1 lukem #
37 1.3 lukem
38 1.3 lukem #
39 1.3 lukem # awk -f parselist.awk -v mode=MODE [var=val ...] file1 [...]
40 1.1 lukem #
41 1.1 lukem # Parse list files file1 [...], generating different output,
42 1.7 lukem # depending upon the value of MODE:
43 1.9 lukem #
44 1.1 lukem # crunch crunchgen(1) config
45 1.9 lukem #
46 1.1 lukem # mtree mtree(8) specfile
47 1.9 lukem #
48 1.9 lukem # populate sh(1) commands to populate ${TARGETDIR} from ${CURDIR}
49 1.9 lukem # The following environment variables need to be set:
50 1.9 lukem # CRUNCHBIN Name of crunchgen(1) target binary
51 1.9 lukem # CURDIR Source directory; make(1)'s ${.CURDIR}
52 1.9 lukem # OBJDIR Object directory; make(1)'s ${.OBJDIR}
53 1.9 lukem # TARGETDIR Directory to populate
54 1.9 lukem #
55 1.1 lukem #
56 1.1 lukem # Each line of the input is either a comment (starts with `#'),
57 1.1 lukem # or contains one of the following keywords and arguments.
58 1.1 lukem #
59 1.9 lukem # Before each line is parsed for a keyword, words surrounded by
60 1.9 lukem # "${" and "}", and containing only letters, numbers, and `_'
61 1.9 lukem # will be replaced by the value of the environment variable of
62 1.9 lukem # the same name. I.e., "${MACHINE_ARCH}" will be replaced with the
63 1.9 lukem # value of ENVIRON["MACHINE_ARCH"].
64 1.3 lukem #
65 1.1 lukem # mode key operation
66 1.1 lukem # -------- ---------
67 1.1 lukem # C crunch
68 1.1 lukem # M mtree
69 1.1 lukem # P populate
70 1.1 lukem #
71 1.1 lukem # mode keyword arg1 [...] description
72 1.1 lukem # ---- ------------------ -----------
73 1.1 lukem #
74 1.1 lukem # C ARGVLN prog link as per crunchgen(1) `ln'
75 1.1 lukem #
76 1.1 lukem # P CMD arg1 [...] run CMD as a shell command
77 1.1 lukem #
78 1.4 lukem # M P COPY src dest [mode] copy src to dest
79 1.4 lukem #
80 1.4 lukem # M P COPYDIR src dest recursively copy files under src to
81 1.4 lukem # dest. for M, the directories in src
82 1.4 lukem # are listed first.
83 1.1 lukem #
84 1.1 lukem # C LIBS libspec ... as per crunchgen(1) `libs'
85 1.1 lukem #
86 1.1 lukem # M P LINK src d1 [d2 ...] hard link src to d1, d2, ...
87 1.1 lukem #
88 1.6 lukem # M MTREE arg1 [...] output arguments `as-is' to specfile
89 1.6 lukem #
90 1.1 lukem # C M P PROG prog [links...] program(s) to crunch/mtree/populate.
91 1.1 lukem # for M and P, the first prog listed
92 1.1 lukem # is copied from ${OBJDIR}/${CRUNCHBIN}
93 1.1 lukem # and then used as the name to link
94 1.1 lukem # all other PROG entries to.
95 1.1 lukem #
96 1.1 lukem # C SPECIAL prog cmd ... as per crunchgen(1) `special'
97 1.1 lukem #
98 1.1 lukem # C SRCDIRS dirname ... as per crunchgen(1) `srcdirs'
99 1.1 lukem #
100 1.1 lukem # M P SYMLINK src dest [...] symlink src to dest, [...]
101 1.1 lukem #
102 1.1 lukem
103 1.1 lukem BEGIN \
104 1.1 lukem {
105 1.1 lukem crunchprog = "";
106 1.1 lukem
107 1.3 lukem if (mode != "crunch" && mode != "mtree" && mode != "populate")
108 1.9 lukem errx("Unknown parselist mode '" mode "'");
109 1.9 lukem
110 1.9 lukem if (mode == "populate") {
111 1.9 lukem split("CRUNCHBIN CURDIR OBJDIR TARGETDIR", needvars);
112 1.9 lukem for (nv in needvars) {
113 1.9 lukem if (! (needvars[nv] in ENVIRON))
114 1.9 lukem errx("Environment variable " \
115 1.9 lukem needvars[nv] " not defined");
116 1.9 lukem }
117 1.9 lukem }
118 1.9 lukem
119 1.1 lukem print "#";
120 1.1 lukem print "# This file is automatically generated by";
121 1.1 lukem print "#\tparselist mode=" mode;
122 1.1 lukem print "#";
123 1.1 lukem print "";
124 1.1 lukem if (mode == "populate") {
125 1.9 lukem print "cd " ENVIRON["CURDIR"];
126 1.1 lukem print;
127 1.1 lukem } else if (mode == "mtree") {
128 1.2 lukem print "/unset\tall";
129 1.1 lukem print "/set\ttype=file uname=root gname=wheel";
130 1.1 lukem print;
131 1.1 lukem }
132 1.1 lukem }
133 1.1 lukem
134 1.1 lukem /^$/ || /^#/ \
135 1.1 lukem {
136 1.1 lukem print;
137 1.1 lukem next;
138 1.1 lukem }
139 1.1 lukem
140 1.9 lukem # replace ${FOO} with ENVIRON["FOO"]
141 1.9 lukem #
142 1.9 lukem /\${[A-Za-z0-9_]+}/ \
143 1.3 lukem {
144 1.9 lukem while (match($0, /\${[A-Za-z0-9_]+}/) > 0) {
145 1.9 lukem v = substr($0, RSTART + 2, RLENGTH - 3);
146 1.9 lukem if (! (v in ENVIRON))
147 1.9 lukem err("Variable " v " is not in the environment");
148 1.9 lukem else
149 1.9 lukem sub(/\${[A-Za-z0-9_]+}/, ENVIRON[v]);
150 1.9 lukem }
151 1.3 lukem }
152 1.3 lukem
153 1.1 lukem $1 == "COPY" \
154 1.1 lukem {
155 1.3 lukem if (NF < 3 || NF > 4)
156 1.3 lukem err("Usage: COPY src dest [mode]");
157 1.1 lukem if (mode == "populate" || mode == "mtree")
158 1.3 lukem copy($2, $3, $4);
159 1.1 lukem next;
160 1.1 lukem }
161 1.1 lukem
162 1.4 lukem $1 == "COPYDIR" \
163 1.4 lukem {
164 1.4 lukem if (NF != 3)
165 1.4 lukem err("Usage: COPYDIR src dest");
166 1.5 lukem srcdir=$2;
167 1.5 lukem destdir=$3;
168 1.4 lukem if (mode == "mtree") {
169 1.5 lukem printf("./%s type=dir mode=755\n", destdir);
170 1.8 lukem command="cd " srcdir " && find . -type d -print"
171 1.4 lukem while (command | getline dir) {
172 1.4 lukem gsub(/^\.\//, "", dir);
173 1.5 lukem if (dir == ".")
174 1.5 lukem continue;
175 1.5 lukem printf("./%s/%s type=dir mode=755\n", destdir, dir);
176 1.4 lukem }
177 1.4 lukem close(command);
178 1.4 lukem }
179 1.4 lukem if (mode == "populate" || mode == "mtree") {
180 1.8 lukem command="cd " srcdir " && find . -type f -print"
181 1.4 lukem while (command | getline srcfile) {
182 1.4 lukem gsub(/^\.\//, "", srcfile);
183 1.4 lukem copy(srcdir "/" srcfile, destdir "/" srcfile, "");
184 1.4 lukem }
185 1.4 lukem close(command);
186 1.4 lukem }
187 1.4 lukem next;
188 1.4 lukem }
189 1.4 lukem
190 1.1 lukem $1 == "LIBS" || $1 == "SPECIAL" || $1 == "SRCDIRS" \
191 1.1 lukem {
192 1.3 lukem if (NF < 2)
193 1.3 lukem err("Usage: " $1 " args...");
194 1.1 lukem if (mode == "crunch") {
195 1.1 lukem $1 = tolower($1);
196 1.1 lukem print;
197 1.1 lukem }
198 1.1 lukem next;
199 1.1 lukem }
200 1.1 lukem
201 1.1 lukem $1 == "PROG" \
202 1.1 lukem {
203 1.3 lukem if (NF < 2)
204 1.3 lukem err("Usage: PROG prog [link ...]");
205 1.1 lukem if (mode == "crunch") {
206 1.1 lukem prog = basename($2);
207 1.1 lukem print "progs " prog;
208 1.1 lukem for (i = 3; i <= NF; i++)
209 1.1 lukem print "ln " prog " " basename($i);
210 1.1 lukem } else {
211 1.1 lukem for (i = 2; i <= NF; i++) {
212 1.1 lukem if (crunchprog == "") {
213 1.1 lukem crunchprog = $i;
214 1.9 lukem copy(ENVIRON["OBJDIR"] "/" ENVIRON["CRUNCHBIN"],
215 1.9 lukem crunchprog);
216 1.1 lukem continue;
217 1.1 lukem }
218 1.1 lukem link(crunchprog, $i);
219 1.1 lukem }
220 1.1 lukem }
221 1.1 lukem next;
222 1.1 lukem }
223 1.1 lukem
224 1.1 lukem $1 == "ARGVLN" \
225 1.1 lukem {
226 1.3 lukem if (NF != 3)
227 1.3 lukem err("Usage: ARGVLN prog link");
228 1.1 lukem if (mode == "crunch") {
229 1.1 lukem $1 = "ln";
230 1.1 lukem print;
231 1.1 lukem }
232 1.1 lukem next;
233 1.1 lukem }
234 1.1 lukem
235 1.1 lukem $1 == "LINK" \
236 1.1 lukem {
237 1.3 lukem if (NF < 3)
238 1.3 lukem err("Usage: LINK prog link [...]");
239 1.1 lukem if (mode == "populate" || mode == "mtree") {
240 1.1 lukem for (i = 3; i <= NF; i++)
241 1.1 lukem link($2, $i);
242 1.1 lukem }
243 1.1 lukem next;
244 1.1 lukem }
245 1.1 lukem
246 1.1 lukem $1 == "SYMLINK" \
247 1.1 lukem {
248 1.3 lukem if (NF < 3)
249 1.3 lukem err("Usage: SYMLINK prog link [...]");
250 1.1 lukem if (mode == "populate" || mode == "mtree") {
251 1.1 lukem for (i = 3; i <= NF; i++)
252 1.1 lukem symlink($2, $i);
253 1.1 lukem }
254 1.1 lukem next;
255 1.1 lukem }
256 1.1 lukem
257 1.1 lukem $1 == "CMD" \
258 1.1 lukem {
259 1.3 lukem if (NF < 2)
260 1.3 lukem err("Usage: CMD ...");
261 1.1 lukem if (mode == "populate") {
262 1.9 lukem printf("(cd %s;", ENVIRON["TARGETDIR"]);
263 1.1 lukem for (i = 2; i <= NF; i++)
264 1.1 lukem printf(" %s", $i);
265 1.1 lukem print ")";
266 1.1 lukem }
267 1.1 lukem next;
268 1.1 lukem }
269 1.6 lukem
270 1.6 lukem $1 == "MTREE" \
271 1.6 lukem {
272 1.6 lukem if (NF < 2)
273 1.6 lukem err("Usage: MTREE ...");
274 1.6 lukem if (mode == "mtree") {
275 1.6 lukem sub(/^[^ \t]+[ \t]+/, ""); # strip first word ("MTREE")
276 1.6 lukem print;
277 1.6 lukem }
278 1.6 lukem next;
279 1.6 lukem }
280 1.6 lukem
281 1.1 lukem
282 1.1 lukem {
283 1.3 lukem err("Unknown keyword '" $1 "'");
284 1.1 lukem }
285 1.1 lukem
286 1.1 lukem
287 1.1 lukem function basename (file) \
288 1.1 lukem {
289 1.1 lukem gsub(/[^\/]+\//, "", file);
290 1.1 lukem return file;
291 1.1 lukem }
292 1.1 lukem
293 1.3 lukem function copy (src, dest, perm) \
294 1.1 lukem {
295 1.1 lukem if (mode == "mtree") {
296 1.3 lukem printf("./%s%s\n", dest, perm != "" ? " mode=" perm : "");
297 1.1 lukem } else {
298 1.9 lukem printf("rm -rf %s/%s\n", ENVIRON["TARGETDIR"], dest);
299 1.9 lukem printf("cp %s %s/%s\n", src, ENVIRON["TARGETDIR"], dest);
300 1.3 lukem if (perm != "")
301 1.9 lukem printf("chmod %s %s/%s\n", perm,
302 1.9 lukem ENVIRON["TARGETDIR"], dest);
303 1.1 lukem }
304 1.1 lukem }
305 1.1 lukem
306 1.1 lukem function link (src, dest) \
307 1.1 lukem {
308 1.1 lukem if (mode == "mtree") {
309 1.1 lukem printf("./%s\n", dest);
310 1.1 lukem } else {
311 1.9 lukem printf("rm -rf %s/%s\n", ENVIRON["TARGETDIR"], dest);
312 1.9 lukem printf("(cd %s; ln %s %s)\n", ENVIRON["TARGETDIR"], src, dest);
313 1.1 lukem }
314 1.1 lukem }
315 1.1 lukem
316 1.1 lukem function symlink (src, dest) \
317 1.1 lukem {
318 1.1 lukem if (mode == "mtree") {
319 1.1 lukem printf("./%s type=link link=%s\n", dest, src);
320 1.1 lukem } else {
321 1.9 lukem printf("rm -rf %s/%s\n", ENVIRON["TARGETDIR"], dest);
322 1.9 lukem printf("(cd %s; ln -s %s %s)\n", ENVIRON["TARGETDIR"],
323 1.9 lukem src, dest);
324 1.1 lukem }
325 1.3 lukem }
326 1.3 lukem
327 1.3 lukem function err(msg) \
328 1.3 lukem {
329 1.9 lukem printf("parselist: %s at line %d of input.\n", msg, NR) >"/dev/stderr";
330 1.9 lukem exit 1;
331 1.9 lukem }
332 1.9 lukem
333 1.9 lukem function errx(msg) \
334 1.9 lukem {
335 1.9 lukem printf("parselist: %s.\n", msg) >"/dev/stderr";
336 1.3 lukem exit 1;
337 1.1 lukem }
338