parselist.awk revision 1.2
1#	$NetBSD: parselist.awk,v 1.2 2002/02/05 23:16:51 lukem Exp $
2#
3# Copyright (c) 2002 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# This code is derived from software contributed to The NetBSD Foundation
7# by Luke Mewburn of Wasabi Systems.
8#
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.
17# 3. All advertising materials mentioning features or use of this software
18#    must display the following acknowledgement:
19#        This product includes software developed by the NetBSD
20#        Foundation, Inc. and its contributors.
21# 4. Neither the name of The NetBSD Foundation nor the names of its
22#    contributors may be used to endorse or promote products derived
23#    from this software without specific prior written permission.
24#
25# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35# POSSIBILITY OF SUCH DAMAGE.
36#
37# awk -f parselist.awk -v mode=MODE file1 [...]
38#
39#	Parse list files file1 [...], generating different output,
40#	depending upon the setting of MODE:
41#	    crunch	crunchgen(1) config
42#	    mtree	mtree(8) specfile
43#	    populate	sh(1) commands to populate ${TARGDIR} from ${CURDIR}
44#
45# 	Each line of the input is either a comment (starts with `#'),
46#	or contains one of the following keywords and arguments.
47#	In general, keywords in lowercase are crunchgen(1) keywords which
48#	might be also supported for the other operations.
49#
50#	mode key	operation
51#	--------	---------
52#	C		crunch
53#	M		mtree
54#	P		populate
55#
56#	mode	keyword arg1 [...]	description
57#	----	------------------	-----------
58#
59#	C	ARGVLN	prog link	as per crunchgen(1) `ln'
60#
61#	P	CMD	arg1 [...]	run CMD as a shell command
62#
63#	M P	COPY	src dest	copy src to dest
64#
65#	C	LIBS	libspec ...	as per crunchgen(1) `libs'
66#
67#	M P	LINK	src d1 [d2 ...]	hard link src to d1, d2, ...
68#
69#	C M P	PROG	prog [links...]	program(s) to crunch/mtree/populate.
70#					for M and P, the first prog listed
71#					is copied from ${OBJDIR}/${CRUNCHBIN}
72#					and then used as the name to link
73#					all other PROG entries to.
74#
75#	C	SPECIAL	prog cmd ...	as per crunchgen(1) `special'
76#
77#	C	SRCDIRS	dirname ...	as per crunchgen(1) `srcdirs'
78#
79#	M P	SYMLINK src dest [...]	symlink src to dest, [...]
80#
81
82BEGIN \
83{
84	crunchprog = "";
85
86	if (mode != "crunch" && mode != "mtree" && mode != "populate") {
87		printf("Unknown parselist mode '%s'\n", mode) > "/dev/stderr";
88		exit 1;
89	}
90	print "#";
91	print "# This file is automatically generated by";
92	print "#\tparselist mode=" mode;
93	print "#";
94	print "";
95	if (mode == "populate") {
96		print "checkvarisset()";
97		print "{";
98		print "	eval _v=\\$${1}";
99		print "	if [ -z \"$_v\" ]; then";
100		print "		echo 1>&2 \"Error: $1 is not defined\"";
101		print "		exit 1";
102		print "	fi";
103		print "}";
104		print;
105		print "checkvarisset CURDIR";
106		print "checkvarisset TARGDIR";
107		print "checkvarisset OBJDIR";
108		print "checkvarisset CRUNCHBIN";
109		print "cd ${CURDIR}";
110		print;
111	} else if (mode == "mtree") {
112		print "/unset\tall";
113		print "/set\ttype=file uname=root gname=wheel";
114		print;
115	}
116}
117
118/^$/ || /^#/ \
119{
120	print;
121	next;
122}
123
124$1 == "COPY" \
125{
126	if (mode == "populate" || mode == "mtree")
127		copy($2, $3);
128	next;
129}
130
131$1 == "LIBS" || $1 == "SPECIAL" || $1 == "SRCDIRS" \
132{
133	if (mode == "crunch") {
134		$1 = tolower($1);
135		print;
136	}
137	next;
138}
139
140$1 == "PROG" \
141{
142	if (mode == "crunch") {
143		prog = basename($2);
144		print "progs " prog;
145		for (i = 3; i <= NF; i++)
146			print "ln " prog " " basename($i);
147	} else {
148		for (i = 2; i <= NF; i++) {
149			if (crunchprog == "") {
150				crunchprog = $i;
151				copy("${OBJDIR}/${CRUNCHBIN}", crunchprog);
152				continue;
153			}
154			link(crunchprog, $i);
155		}
156	}
157	next;
158}
159
160$1 == "ARGVLN" \
161{
162	if (mode == "crunch") {
163		$1 = "ln";
164		print;
165	}
166	next;
167}
168
169$1 == "LINK" \
170{
171	if (mode == "populate" || mode == "mtree") {
172		for (i = 3; i <= NF; i++)
173			link($2, $i);
174	}
175	next;
176}
177
178$1 == "SYMLINK" \
179{
180	if (mode == "populate" || mode == "mtree") {
181		for (i = 3; i <= NF; i++)
182			symlink($2, $i);
183	}
184	next;
185}
186
187$1 == "CMD" \
188{
189	if (mode == "populate") {
190		printf("(cd ${TARGDIR};");
191		for (i = 2; i <= NF; i++)
192			printf(" %s", $i);
193		print ")";
194	}
195	next;
196}
197
198{
199	printf("Unknown keyword '%s' at line %d of input.\n", $1, NR) \
200	    >"/dev/stderr";
201	exit 1;
202}
203
204
205function basename (file) \
206{
207	gsub(/[^\/]+\//, "", file);
208	return file;
209}
210
211function copy (src, dest) \
212{
213	if (mode == "mtree") {
214		printf("./%s\n", dest);
215	} else {
216		printf("rm -rf ${TARGDIR}/%s\n", dest);
217		printf("cp %s ${TARGDIR}/%s\n", src, dest);
218	}
219}
220
221function link (src, dest) \
222{
223	if (mode == "mtree") {
224# XXX		printf("./%s type=hlink link=%s\n", dest, src);
225		printf("./%s\n", dest);
226	} else {
227		printf("rm -rf ${TARGDIR}/%s\n", dest);
228		printf("(cd ${TARGDIR}; ln %s %s)\n", src, dest);
229	}
230}
231
232function symlink (src, dest) \
233{
234	if (mode == "mtree") {
235		printf("./%s type=link link=%s\n", dest, src);
236	} else {
237		printf("rm -rf ${TARGDIR}/%s\n", dest);
238		printf("(cd ${TARGDIR}; ln -s %s %s)\n", src, dest);
239	}
240}
241