parselist.awk revision 1.4 1 # $NetBSD: parselist.awk,v 1.4 2002/03/05 00:19:43 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 if (mode == "mtree") {
157 command="cd " $2 " ; find . -type d -print"
158 while (command | getline dir) {
159 gsub(/^\.\//, "", dir);
160 printf("./%s/%s type=dir mode=755\n", $3, dir);
161 }
162 close(command);
163 }
164 if (mode == "populate" || mode == "mtree") {
165 srcdir=$2;
166 destdir=$3;
167 command="cd " srcdir " ; find . -type f -print"
168 while (command | getline srcfile) {
169 gsub(/^\.\//, "", srcfile);
170 copy(srcdir "/" srcfile, destdir "/" srcfile, "");
171 }
172 close(command);
173 }
174 next;
175 }
176
177 $1 == "LIBS" || $1 == "SPECIAL" || $1 == "SRCDIRS" \
178 {
179 if (NF < 2)
180 err("Usage: " $1 " args...");
181 if (mode == "crunch") {
182 $1 = tolower($1);
183 print;
184 }
185 next;
186 }
187
188 $1 == "PROG" \
189 {
190 if (NF < 2)
191 err("Usage: PROG prog [link ...]");
192 if (mode == "crunch") {
193 prog = basename($2);
194 print "progs " prog;
195 for (i = 3; i <= NF; i++)
196 print "ln " prog " " basename($i);
197 } else {
198 for (i = 2; i <= NF; i++) {
199 if (crunchprog == "") {
200 crunchprog = $i;
201 copy("${OBJDIR}/${CRUNCHBIN}", crunchprog);
202 continue;
203 }
204 link(crunchprog, $i);
205 }
206 }
207 next;
208 }
209
210 $1 == "ARGVLN" \
211 {
212 if (NF != 3)
213 err("Usage: ARGVLN prog link");
214 if (mode == "crunch") {
215 $1 = "ln";
216 print;
217 }
218 next;
219 }
220
221 $1 == "LINK" \
222 {
223 if (NF < 3)
224 err("Usage: LINK prog link [...]");
225 if (mode == "populate" || mode == "mtree") {
226 for (i = 3; i <= NF; i++)
227 link($2, $i);
228 }
229 next;
230 }
231
232 $1 == "SYMLINK" \
233 {
234 if (NF < 3)
235 err("Usage: SYMLINK prog link [...]");
236 if (mode == "populate" || mode == "mtree") {
237 for (i = 3; i <= NF; i++)
238 symlink($2, $i);
239 }
240 next;
241 }
242
243 $1 == "CMD" \
244 {
245 if (NF < 2)
246 err("Usage: CMD ...");
247 if (mode == "populate") {
248 printf("(cd ${TARGDIR};");
249 for (i = 2; i <= NF; i++)
250 printf(" %s", $i);
251 print ")";
252 }
253 next;
254 }
255
256 {
257 err("Unknown keyword '" $1 "'");
258 }
259
260
261 function basename (file) \
262 {
263 gsub(/[^\/]+\//, "", file);
264 return file;
265 }
266
267 function copy (src, dest, perm) \
268 {
269 if (mode == "mtree") {
270 printf("./%s%s\n", dest, perm != "" ? " mode=" perm : "");
271 } else {
272 printf("rm -rf ${TARGDIR}/%s\n", dest);
273 printf("cp %s ${TARGDIR}/%s\n", src, dest);
274 if (perm != "")
275 printf("chmod %s ${TARGDIR}/%s\n", perm, dest);
276 }
277 }
278
279 function link (src, dest) \
280 {
281 if (mode == "mtree") {
282 printf("./%s\n", dest);
283 } else {
284 printf("rm -rf ${TARGDIR}/%s\n", dest);
285 printf("(cd ${TARGDIR}; ln %s %s)\n", src, dest);
286 }
287 }
288
289 function symlink (src, dest) \
290 {
291 if (mode == "mtree") {
292 printf("./%s type=link link=%s\n", dest, src);
293 } else {
294 printf("rm -rf ${TARGDIR}/%s\n", dest);
295 printf("(cd ${TARGDIR}; ln -s %s %s)\n", src, dest);
296 }
297 }
298
299 function err(msg) \
300 {
301 printf("%s at line %d of input.\n", msg, NR) >"/dev/stderr";
302 errexit=1;
303 exit 1;
304 }
305