parselist.awk revision 1.3 1 # $NetBSD: parselist.awk,v 1.3 2002/02/07 11:39:17 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
38 #
39 # awk -f parselist.awk -v mode=MODE [var=val ...] file1 [...]
40 #
41 # Parse list files file1 [...], generating different output,
42 # depending upon the setting of MODE:
43 # crunch crunchgen(1) config
44 # mtree mtree(8) specfile
45 # populate sh(1) commands to populate ${TARGDIR} from ${CURDIR}
46 #
47 # Each line of the input is either a comment (starts with `#'),
48 # or contains one of the following keywords and arguments.
49 # In general, keywords in lowercase are crunchgen(1) keywords which
50 # might be also supported for the other operations.
51 #
52 # Before each line is parsed, the following strings are replaced with
53 # the appropriate value which is passed in on the command line:
54 #
55 # string value
56 # ------ -----
57 # @MACHINE_ARCH@ MACHINE_ARCH
58 # @MACHINE@ MACHINE
59 #
60 # mode key operation
61 # -------- ---------
62 # C crunch
63 # M mtree
64 # P populate
65 #
66 # mode keyword arg1 [...] description
67 # ---- ------------------ -----------
68 #
69 # C ARGVLN prog link as per crunchgen(1) `ln'
70 #
71 # P CMD arg1 [...] run CMD as a shell command
72 #
73 # M P COPY src dest copy src to dest
74 #
75 # C LIBS libspec ... as per crunchgen(1) `libs'
76 #
77 # M P LINK src d1 [d2 ...] hard link src to d1, d2, ...
78 #
79 # C M P PROG prog [links...] program(s) to crunch/mtree/populate.
80 # for M and P, the first prog listed
81 # is copied from ${OBJDIR}/${CRUNCHBIN}
82 # and then used as the name to link
83 # all other PROG entries to.
84 #
85 # C SPECIAL prog cmd ... as per crunchgen(1) `special'
86 #
87 # C SRCDIRS dirname ... as per crunchgen(1) `srcdirs'
88 #
89 # M P SYMLINK src dest [...] symlink src to dest, [...]
90 #
91
92 BEGIN \
93 {
94 errexit = 0;
95 crunchprog = "";
96
97 if (mode != "crunch" && mode != "mtree" && mode != "populate")
98 err("Unknown parselist mode '" mode "'");
99 print "#";
100 print "# This file is automatically generated by";
101 print "#\tparselist mode=" mode;
102 print "#";
103 print "";
104 if (mode == "populate") {
105 print "checkvarisset()";
106 print "{";
107 print " eval _v=\\$${1}";
108 print " if [ -z \"$_v\" ]; then";
109 print " echo 1>&2 \"Error: $1 is not defined\"";
110 print " exit 1";
111 print " fi";
112 print "}";
113 print;
114 print "checkvarisset CURDIR";
115 print "checkvarisset TARGDIR";
116 print "checkvarisset OBJDIR";
117 print "checkvarisset CRUNCHBIN";
118 print "cd ${CURDIR}";
119 print;
120 } else if (mode == "mtree") {
121 print "/unset\tall";
122 print "/set\ttype=file uname=root gname=wheel";
123 print;
124 }
125 }
126
127 /^$/ || /^#/ \
128 {
129 print;
130 next;
131 }
132
133 /@MACHINE(_ARCH)?@/ \
134 {
135 gsub(/@MACHINE_ARCH@/, MACHINE_ARCH);
136 gsub(/@MACHINE@/, MACHINE);
137 }
138
139 $1 == "COPY" \
140 {
141 if (NF < 3 || NF > 4)
142 err("Usage: COPY src dest [mode]");
143 if (mode == "populate" || mode == "mtree")
144 copy($2, $3, $4);
145 next;
146 }
147
148 $1 == "LIBS" || $1 == "SPECIAL" || $1 == "SRCDIRS" \
149 {
150 if (NF < 2)
151 err("Usage: " $1 " args...");
152 if (mode == "crunch") {
153 $1 = tolower($1);
154 print;
155 }
156 next;
157 }
158
159 $1 == "PROG" \
160 {
161 if (NF < 2)
162 err("Usage: PROG prog [link ...]");
163 if (mode == "crunch") {
164 prog = basename($2);
165 print "progs " prog;
166 for (i = 3; i <= NF; i++)
167 print "ln " prog " " basename($i);
168 } else {
169 for (i = 2; i <= NF; i++) {
170 if (crunchprog == "") {
171 crunchprog = $i;
172 copy("${OBJDIR}/${CRUNCHBIN}", crunchprog);
173 continue;
174 }
175 link(crunchprog, $i);
176 }
177 }
178 next;
179 }
180
181 $1 == "ARGVLN" \
182 {
183 if (NF != 3)
184 err("Usage: ARGVLN prog link");
185 if (mode == "crunch") {
186 $1 = "ln";
187 print;
188 }
189 next;
190 }
191
192 $1 == "LINK" \
193 {
194 if (NF < 3)
195 err("Usage: LINK prog link [...]");
196 if (mode == "populate" || mode == "mtree") {
197 for (i = 3; i <= NF; i++)
198 link($2, $i);
199 }
200 next;
201 }
202
203 $1 == "SYMLINK" \
204 {
205 if (NF < 3)
206 err("Usage: SYMLINK prog link [...]");
207 if (mode == "populate" || mode == "mtree") {
208 for (i = 3; i <= NF; i++)
209 symlink($2, $i);
210 }
211 next;
212 }
213
214 $1 == "CMD" \
215 {
216 if (NF < 2)
217 err("Usage: CMD ...");
218 if (mode == "populate") {
219 printf("(cd ${TARGDIR};");
220 for (i = 2; i <= NF; i++)
221 printf(" %s", $i);
222 print ")";
223 }
224 next;
225 }
226
227 {
228 err("Unknown keyword '" $1 "'");
229 }
230
231
232 function basename (file) \
233 {
234 gsub(/[^\/]+\//, "", file);
235 return file;
236 }
237
238 function copy (src, dest, perm) \
239 {
240 if (mode == "mtree") {
241 printf("./%s%s\n", dest, perm != "" ? " mode=" perm : "");
242 } else {
243 printf("rm -rf ${TARGDIR}/%s\n", dest);
244 printf("cp %s ${TARGDIR}/%s\n", src, dest);
245 if (perm != "")
246 printf("chmod %s ${TARGDIR}/%s\n", perm, dest);
247 }
248 }
249
250 function link (src, dest) \
251 {
252 if (mode == "mtree") {
253 # XXX printf("./%s type=hlink link=%s\n", dest, src);
254 printf("./%s\n", dest);
255 } else {
256 printf("rm -rf ${TARGDIR}/%s\n", dest);
257 printf("(cd ${TARGDIR}; ln %s %s)\n", src, dest);
258 }
259 }
260
261 function symlink (src, dest) \
262 {
263 if (mode == "mtree") {
264 printf("./%s type=link link=%s\n", dest, src);
265 } else {
266 printf("rm -rf ${TARGDIR}/%s\n", dest);
267 printf("(cd ${TARGDIR}; ln -s %s %s)\n", src, dest);
268 }
269 }
270
271 function err(msg) \
272 {
273 printf("%s at line %d of input.\n", msg, NR) >"/dev/stderr";
274 errexit=1;
275 exit 1;
276 }
277