1 # $NetBSD: parselist.awk,v 1.15 2008/04/30 13:10:48 martin Exp $
3 # Copyright (c) 2002 The NetBSD Foundation, Inc.
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
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"].
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, [...]
109 if (mode
!= "crunch" && mode
!= "install" &&
110 mode
!= "mtree" && mode
!= "populate")
111 errx
("Unknown parselist mode '" mode
"'");
113 needvars
["CRUNCHBIN"]++
115 if (mode ==
"install") {
116 needvars
["TARGETDIR"]++
118 else if (mode ==
"populate") {
121 for (nv in needvars
) {
122 if (!
(nv in
ENVIRON))
123 errx
("Environment variable " nv
" not defined");
127 print "# This file is automatically generated by";
128 print "#\tparselist mode=" mode
;
131 if (mode ==
"install") {
132 print ".include <bsd.own.mk>"
134 } else if (mode ==
"mtree") {
136 print "/set\ttype=file uname=root gname=wheel";
138 } else if (mode ==
"populate") {
139 print "cd " ENVIRON["CURDIR"];
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");
159 sub(/\$\
{[A
-Za
-z0
-9_
]+\
}/, ENVIRON[v
]);
165 if (NF < 3 || NF > 4)
166 err
("Usage: COPY src dest [perm]");
167 if (mode ==
"install" || mode ==
"mtree" || mode ==
"populate")
175 err
("Usage: COPYDIR src dest");
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
);
185 printf("./%s/%s type=dir mode=755\n", destdir
, dir
);
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
, "");
200 $
1 ==
"LIBS" || $
1 ==
"SPECIAL" || $
1 ==
"SRCDIRS" \
203 err
("Usage: " $
1 " args...");
204 if (mode ==
"crunch") {
214 err
("Usage: PROG prog [link ...]");
215 if (mode ==
"crunch") {
218 for (i =
3; i
<=
NF; i
++)
219 print "ln " prog
" " basename
($i
);
221 for (i =
2; i
<=
NF; i
++) {
222 if (crunchprog ==
"") {
224 copy
(ENVIRON["OBJDIR"] "/" ENVIRON["CRUNCHBIN"],
228 link
(crunchprog
, $i
, 555);
237 err
("Usage: ARGVLN prog link");
238 if (mode ==
"crunch") {
248 err
("Usage: LINK prog link [...]");
249 if (mode ==
"install" || mode ==
"mtree" || mode ==
"populate") {
250 for (i =
3; i
<=
NF; i
++)
259 err
("Usage: SYMLINK prog link [...]");
260 if (mode ==
"install" || mode ==
"mtree" || mode ==
"populate") {
261 for (i =
3; i
<=
NF; i
++)
270 err
("Usage: CMD ...");
271 if (mode ==
"populate") {
272 printf("(cd %s;", ENVIRON["TARGETDIR"]);
273 for (i =
2; i
<=
NF; i
++)
283 err
("Usage: MTREE ...");
284 if (mode ==
"mtree") {
285 sub(/^
[^
\t]+[ \t]+/, ""); # strip first word ("MTREE")
293 err
("Unknown keyword '" $
1 "'");
297 function basename
(file
) \
299 gsub(/[^\
/]+\
//, "", file
);
303 function copy
(src
, dest
, perm
) \
307 if (mode ==
"install") {
308 printf("\t${INSTALL_FILE} -o ${BINOWN} -g ${BINGRP}" \
310 perm
, src
, ENVIRON["TARGETDIR"], dest
)
311 } else if (mode ==
"mtree") {
312 printf("./%s mode=%s\n", dest
, perm
);
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
);
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
);
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
);
351 printf("parselist: %s at line %d of input.\n", msg
, NR) >"/dev/stderr";
357 printf("parselist: %s.\n", msg
) >"/dev/stderr";