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