Drop main() prototype. Syncs with NetBSD-8
[minix.git] / distrib / common / parselist.awk
blobbadbc7ad5a0217fb5afd26e59fc41edccafd1e19
1 # $NetBSD: parselist.awk,v 1.16 2009/04/10 16:16:12 apb Exp $
3 # Copyright (c) 2002 The NetBSD Foundation, Inc.
4 # All rights reserved.
6 # This code is derived from software contributed to The NetBSD Foundation
7 # by Luke Mewburn of Wasabi Systems.
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
11 # are met:
12 # 1. Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # 2. Redistributions in binary form must reproduce the above copyright
15 # notice, this list of conditions and the following disclaimer in the
16 # documentation and/or other materials provided with the distribution.
18 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
32 # awk -f parselist.awk -v mode=MODE [var=val ...] file1 [...]
34 # Parse list files file1 [...], generating different output,
35 # depending upon the value of MODE:
37 # crunch crunchgen(1) config
39 # install make(1) Makefile to install commands into ${TARGETDIR},
40 # with an `install' target.
41 # The following environment variables need to be set:
42 # TARGETDIR Directory to populate
44 # mtree mtree(8) specfile
46 # populate sh(1) commands to populate ${TARGETDIR} from ${CURDIR}
47 # The following environment variables need to be set:
48 # CURDIR Source directory; make(1)'s ${.CURDIR}
49 # TARGETDIR Directory to populate
51 # The following environment variables need to be set for all modes:
52 # CRUNCHBIN Name of crunchgen(1) target binary
53 # OBJDIR Object directory; make(1)'s ${.OBJDIR}
55 # Each line of the input is either a comment (starts with `#'),
56 # or contains one of the following keywords and arguments.
58 # Before each line is parsed for a keyword, words surrounded by
59 # "${" and "}", and containing only letters, numbers, and `_'
60 # will be replaced by the value of the environment variable of
61 # the same name. I.e., "${MACHINE_ARCH}" will be replaced with the
62 # value of ENVIRON["MACHINE_ARCH"].
64 # mode key operation
65 # -------- ---------
66 # C crunch
67 # I install
68 # M mtree
69 # P populate
71 # mode keyword arg1 [...] description
72 # ---- ------------------ -----------
74 # C ARGVLN prog link as per crunchgen(1) `ln'
76 # P CMD arg1 [...] run CMD as a shell command
78 # IMP COPY src dest [perm] copy src to dest. perm defaults to 0444
80 # IMP COPYDIR src dest recursively copy files under src to
81 # dest. for M, dest is listed first,
82 # followed by the subdirectories in src.
83 # copied directories have mode 0755.
84 # copied files have mode 0444.
86 # C LIBS libspec ... as per crunchgen(1) `libs'
88 # IMP LINK src d1 [d2 ...] hard link src to d1, d2, ...
90 # M MTREE arg1 [...] output arguments `as-is' to specfile
92 # CIMP PROG prog [links...] program(s) to crunch/mtree/populate.
93 # for I, M & P, the first prog listed
94 # is copied from ${OBJDIR}/${CRUNCHBIN}
95 # and then used as the name to link
96 # all other PROG entries to.
98 # C SPECIAL prog cmd ... as per crunchgen(1) `special'
100 # C SRCDIRS dirname ... as per crunchgen(1) `srcdirs'
102 # IMP SYMLINK src dest [...] symlink src to dest, [...]
105 BEGIN \
107 crunchprog = "";
109 if (mode != "crunch" && mode != "install" &&
110 mode != "mtree" && mode != "populate")
111 errx("Unknown parselist mode '" mode "'");
113 needvars["CRUNCHBIN"]++
114 needvars["OBJDIR"]++
115 if (mode == "install") {
116 needvars["TARGETDIR"]++
118 else if (mode == "populate") {
119 needvars["CURDIR"]++
121 for (nv in needvars) {
122 if (! (nv in ENVIRON))
123 errx("Environment variable " nv " not defined");
126 print "#";
127 print "# This file is automatically generated by";
128 print "#\tparselist mode=" mode;
129 print "#";
130 print "";
131 if (mode == "install") {
132 print ".include <bsd.own.mk>"
133 print "install:"
134 } else if (mode == "mtree") {
135 print "/unset\tall";
136 print "/set\ttype=file uname=root gname=wheel";
137 print;
138 } else if (mode == "populate") {
139 print "cd " ENVIRON["CURDIR"];
140 print;
144 /^$/ || /^#/ \
146 print;
147 next;
150 # replace ${FOO} with ENVIRON["FOO"]
152 /\$\{[A-Za-z0-9_]+\}/ \
154 while (match($0, /\$\{[A-Za-z0-9_]+\}/) > 0) {
155 v = substr($0, RSTART + 2, RLENGTH - 3);
156 if (! (v in ENVIRON))
157 err("Variable " v " is not in the environment");
158 else
159 sub(/\$\{[A-Za-z0-9_]+\}/, ENVIRON[v]);
163 $1 == "COPY" \
165 if (NF < 3 || NF > 4)
166 err("Usage: COPY src dest [perm]");
167 if (mode == "install" || mode == "mtree" || mode == "populate")
168 copy($2, $3, $4);
169 next;
172 $1 == "COPYDIR" \
174 if (NF != 3)
175 err("Usage: COPYDIR src dest");
176 srcdir=$2;
177 destdir=$3;
178 if (mode == "mtree") {
179 printf("./%s type=dir mode=755\n", destdir);
180 command="cd " srcdir " && find . -type d -print"
181 while (command | getline dir) {
182 gsub(/^\.\//, "", dir);
183 if (dir == ".")
184 continue;
185 printf("./%s/%s type=dir mode=755\n", destdir, dir);
187 close(command);
189 if (mode == "install" || mode == "mtree" || mode == "populate") {
190 command="cd " srcdir " && find . -type f -print"
191 while (command | getline srcfile) {
192 gsub(/^\.\//, "", srcfile);
193 copy(srcdir "/" srcfile, destdir "/" srcfile, "");
195 close(command);
197 next;
200 $1 == "LIBS" || $1 == "SPECIAL" || $1 == "SRCDIRS" \
202 if (NF < 2)
203 err("Usage: " $1 " args...");
204 if (mode == "crunch") {
205 $1 = tolower($1);
206 print;
208 next;
211 $1 == "PROG" \
213 if (NF < 2)
214 err("Usage: PROG prog [link ...]");
215 if (mode == "crunch") {
216 prog = basename($2);
217 print "progs " prog;
218 for (i = 3; i <= NF; i++)
219 print "ln " prog " " basename($i);
220 } else {
221 for (i = 2; i <= NF; i++) {
222 if (crunchprog == "") {
223 crunchprog = $i;
224 copy(ENVIRON["OBJDIR"] "/" ENVIRON["CRUNCHBIN"],
225 crunchprog, 555);
226 continue;
228 link(crunchprog, $i, 555);
231 next;
234 $1 == "ARGVLN" \
236 if (NF != 3)
237 err("Usage: ARGVLN prog link");
238 if (mode == "crunch") {
239 $1 = "ln";
240 print;
242 next;
245 $1 == "LINK" \
247 if (NF < 3)
248 err("Usage: LINK prog link [...]");
249 if (mode == "install" || mode == "mtree" || mode == "populate") {
250 for (i = 3; i <= NF; i++)
251 link($2, $i, 444);
253 next;
256 $1 == "SYMLINK" \
258 if (NF < 3)
259 err("Usage: SYMLINK prog link [...]");
260 if (mode == "install" || mode == "mtree" || mode == "populate") {
261 for (i = 3; i <= NF; i++)
262 symlink($2, $i);
264 next;
267 $1 == "CMD" \
269 if (NF < 2)
270 err("Usage: CMD ...");
271 if (mode == "populate") {
272 printf("(cd %s;", ENVIRON["TARGETDIR"]);
273 for (i = 2; i <= NF; i++)
274 printf(" %s", $i);
275 print ") || exit 1";
277 next;
280 $1 == "MTREE" \
282 if (NF < 2)
283 err("Usage: MTREE ...");
284 if (mode == "mtree") {
285 sub(/^[^ \t]+[ \t]+/, ""); # strip first word ("MTREE")
286 print;
288 next;
293 err("Unknown keyword '" $1 "'");
297 function basename (file) \
299 gsub(/[^\/]+\//, "", file);
300 return file;
303 function copy (src, dest, perm) \
305 if (perm == "")
306 perm = 444;
307 if (mode == "install") {
308 printf("\t${INSTALL_FILE} -o ${BINOWN} -g ${BINGRP}" \
309 " -m %s %s %s/%s\n",
310 perm, src, ENVIRON["TARGETDIR"], dest)
311 } else if (mode == "mtree") {
312 printf("./%s mode=%s\n", dest, perm);
313 } else {
314 printf("rm -rf %s/%s\n", ENVIRON["TARGETDIR"], dest);
315 printf("cp %s %s/%s\n", src, ENVIRON["TARGETDIR"], dest);
316 printf("chmod %s %s/%s\n", perm, ENVIRON["TARGETDIR"], dest);
320 function link (src, dest, perm) \
322 if (mode == "install") {
323 printf("\t${INSTALL_LINK} -o ${BINOWN} -g ${BINGRP}" \
324 " -m %s %s/%s %s/%s\n",
325 perm, ENVIRON["TARGETDIR"], src, ENVIRON["TARGETDIR"], dest)
326 } else if (mode == "mtree") {
327 printf("./%s mode=%s\n", dest, perm);
328 } else {
329 printf("rm -rf %s/%s\n", ENVIRON["TARGETDIR"], dest);
330 printf("(cd %s; ln %s %s) || exit 1\n",
331 ENVIRON["TARGETDIR"], src, dest);
335 function symlink (src, dest) \
337 if (mode == "install") {
338 printf("\t${INSTALL_SYMLINK} %s/%s %s/%s\n",
339 ENVIRON["TARGETDIR"], src, ENVIRON["TARGETDIR"], dest)
340 } else if (mode == "mtree") {
341 printf("./%s type=link link=%s\n", dest, src);
342 } else {
343 printf("rm -rf %s/%s\n", ENVIRON["TARGETDIR"], dest);
344 printf("(cd %s; ln -s %s %s) || exit 1\n",
345 ENVIRON["TARGETDIR"], src, dest);
349 function err(msg) \
351 printf("parselist: %s at line %d of input.\n", msg, NR) >"/dev/stderr";
352 exit 1;
355 function errx(msg) \
357 printf("parselist: %s.\n", msg) >"/dev/stderr";
358 exit 1;