parselist.awk revision 1.5 1 # $NetBSD: parselist.awk,v 1.5 2002/03/06 04:47:58 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 [mode] copy src to dest
74 #
75 # M P COPYDIR src dest recursively copy files under src to
76 # dest. for M, the directories in src
77 # are listed first.
78 #
79 # C LIBS libspec ... as per crunchgen(1) `libs'
80 #
81 # M P LINK src d1 [d2 ...] hard link src to d1, d2, ...
82 #
83 # C M P PROG prog [links...] program(s) to crunch/mtree/populate.
84 # for M and P, the first prog listed
85 # is copied from ${OBJDIR}/${CRUNCHBIN}
86 # and then used as the name to link
87 # all other PROG entries to.
88 #
89 # C SPECIAL prog cmd ... as per crunchgen(1) `special'
90 #
91 # C SRCDIRS dirname ... as per crunchgen(1) `srcdirs'
92 #
93 # M P SYMLINK src dest [...] symlink src to dest, [...]
94 #
95
96 BEGIN \
97 {
98 errexit = 0;
99 crunchprog = "";
100
101 if (mode != "crunch" && mode != "mtree" && mode != "populate")
102 err("Unknown parselist mode '" mode "'");
103 print "#";
104 print "# This file is automatically generated by";
105 print "#\tparselist mode=" mode;
106 print "#";
107 print "";
108 if (mode == "populate") {
109 print "checkvarisset()";
110 print "{";
111 print " eval _v=\\$${1}";
112 print " if [ -z \"$_v\" ]; then";
113 print " echo 1>&2 \"Error: $1 is not defined\"";
114 print " exit 1";
115 print " fi";
116 print "}";
117 print;
118 print "checkvarisset CURDIR";
119 print "checkvarisset TARGDIR";
120 print "checkvarisset OBJDIR";
121 print "checkvarisset CRUNCHBIN";
122 print "cd ${CURDIR}";
123 print;
124 } else if (mode == "mtree") {
125 print "/unset\tall";
126 print "/set\ttype=file uname=root gname=wheel";
127 print;
128 }
129 }
130
131 /^$/ || /^#/ \
132 {
133 print;
134 next;
135 }
136
137 /@MACHINE(_ARCH)?@/ \
138 {
139 gsub(/@MACHINE_ARCH@/, MACHINE_ARCH);
140 gsub(/@MACHINE@/, MACHINE);
141 }
142
143 $1 == "COPY" \
144 {
145 if (NF < 3 || NF > 4)
146 err("Usage: COPY src dest [mode]");
147 if (mode == "populate" || mode == "mtree")
148 copy($2, $3, $4);
149 next;
150 }
151
152 $1 == "COPYDIR" \
153 {
154 if (NF != 3)
155 err("Usage: COPYDIR src dest");
156 srcdir=$2;
157 destdir=$3;
158 if (mode == "mtree") {
159 printf("./%s type=dir mode=755\n", destdir);
160 command="cd " srcdir " ; find . -type d -print"
161 while (command | getline dir) {
162 gsub(/^\.\//, "", dir);
163 if (dir == ".")
164 continue;
165 printf("./%s/%s type=dir mode=755\n", destdir, dir);
166 }
167 close(command);
168 }
169 if (mode == "populate" || mode == "mtree") {
170 command="cd " srcdir " ; find . -type f -print"
171 while (command | getline srcfile) {
172 gsub(/^\.\//, "", srcfile);
173 copy(srcdir "/" srcfile, destdir "/" srcfile, "");
174 }
175 close(command);
176 }
177 next;
178 }
179
180 $1 == "LIBS" || $1 == "SPECIAL" || $1 == "SRCDIRS" \
181 {
182 if (NF < 2)
183 err("Usage: " $1 " args...");
184 if (mode == "crunch") {
185 $1 = tolower($1);
186 print;
187 }
188 next;
189 }
190
191 $1 == "PROG" \
192 {
193 if (NF < 2)
194 err("Usage: PROG prog [link ...]");
195 if (mode == "crunch") {
196 prog = basename($2);
197 print "progs " prog;
198 for (i = 3; i <= NF; i++)
199 print "ln " prog " " basename($i);
200 } else {
201 for (i = 2; i <= NF; i++) {
202 if (crunchprog == "") {
203 crunchprog = $i;
204 copy("${OBJDIR}/${CRUNCHBIN}", crunchprog);
205 continue;
206 }
207 link(crunchprog, $i);
208 }
209 }
210 next;
211 }
212
213 $1 == "ARGVLN" \
214 {
215 if (NF != 3)
216 err("Usage: ARGVLN prog link");
217 if (mode == "crunch") {
218 $1 = "ln";
219 print;
220 }
221 next;
222 }
223
224 $1 == "LINK" \
225 {
226 if (NF < 3)
227 err("Usage: LINK prog link [...]");
228 if (mode == "populate" || mode == "mtree") {
229 for (i = 3; i <= NF; i++)
230 link($2, $i);
231 }
232 next;
233 }
234
235 $1 == "SYMLINK" \
236 {
237 if (NF < 3)
238 err("Usage: SYMLINK prog link [...]");
239 if (mode == "populate" || mode == "mtree") {
240 for (i = 3; i <= NF; i++)
241 symlink($2, $i);
242 }
243 next;
244 }
245
246 $1 == "CMD" \
247 {
248 if (NF < 2)
249 err("Usage: CMD ...");
250 if (mode == "populate") {
251 printf("(cd ${TARGDIR};");
252 for (i = 2; i <= NF; i++)
253 printf(" %s", $i);
254 print ")";
255 }
256 next;
257 }
258
259 {
260 err("Unknown keyword '" $1 "'");
261 }
262
263
264 function basename (file) \
265 {
266 gsub(/[^\/]+\//, "", file);
267 return file;
268 }
269
270 function copy (src, dest, perm) \
271 {
272 if (mode == "mtree") {
273 printf("./%s%s\n", dest, perm != "" ? " mode=" perm : "");
274 } else {
275 printf("rm -rf ${TARGDIR}/%s\n", dest);
276 printf("cp %s ${TARGDIR}/%s\n", src, dest);
277 if (perm != "")
278 printf("chmod %s ${TARGDIR}/%s\n", perm, dest);
279 }
280 }
281
282 function link (src, dest) \
283 {
284 if (mode == "mtree") {
285 printf("./%s\n", dest);
286 } else {
287 printf("rm -rf ${TARGDIR}/%s\n", dest);
288 printf("(cd ${TARGDIR}; ln %s %s)\n", src, dest);
289 }
290 }
291
292 function symlink (src, dest) \
293 {
294 if (mode == "mtree") {
295 printf("./%s type=link link=%s\n", dest, src);
296 } else {
297 printf("rm -rf ${TARGDIR}/%s\n", dest);
298 printf("(cd ${TARGDIR}; ln -s %s %s)\n", src, dest);
299 }
300 }
301
302 function err(msg) \
303 {
304 printf("%s at line %d of input.\n", msg, NR) >"/dev/stderr";
305 errexit=1;
306 exit 1;
307 }
308