Home | History | Annotate | Line # | Download | only in common
parselist.awk revision 1.4
      1  1.4  lukem #	$NetBSD: parselist.awk,v 1.4 2002/03/05 00:19:43 lukem Exp $
      2  1.1  lukem #
      3  1.1  lukem # Copyright (c) 2002 The NetBSD Foundation, Inc.
      4  1.1  lukem # All rights reserved.
      5  1.1  lukem #
      6  1.1  lukem # This code is derived from software contributed to The NetBSD Foundation
      7  1.1  lukem # by Luke Mewburn of Wasabi Systems.
      8  1.1  lukem #
      9  1.1  lukem # Redistribution and use in source and binary forms, with or without
     10  1.1  lukem # modification, are permitted provided that the following conditions
     11  1.1  lukem # are met:
     12  1.1  lukem # 1. Redistributions of source code must retain the above copyright
     13  1.1  lukem #    notice, this list of conditions and the following disclaimer.
     14  1.1  lukem # 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  lukem #    notice, this list of conditions and the following disclaimer in the
     16  1.1  lukem #    documentation and/or other materials provided with the distribution.
     17  1.1  lukem # 3. All advertising materials mentioning features or use of this software
     18  1.1  lukem #    must display the following acknowledgement:
     19  1.1  lukem #        This product includes software developed by the NetBSD
     20  1.1  lukem #        Foundation, Inc. and its contributors.
     21  1.1  lukem # 4. Neither the name of The NetBSD Foundation nor the names of its
     22  1.1  lukem #    contributors may be used to endorse or promote products derived
     23  1.1  lukem #    from this software without specific prior written permission.
     24  1.1  lukem #
     25  1.1  lukem # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  1.1  lukem # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  1.1  lukem # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  1.1  lukem # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  1.1  lukem # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  1.1  lukem # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  1.1  lukem # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  1.1  lukem # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  1.1  lukem # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  1.1  lukem # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  1.1  lukem # POSSIBILITY OF SUCH DAMAGE.
     36  1.1  lukem #
     37  1.3  lukem 
     38  1.3  lukem #
     39  1.3  lukem # awk -f parselist.awk -v mode=MODE [var=val ...] file1 [...]
     40  1.1  lukem #
     41  1.1  lukem #	Parse list files file1 [...], generating different output,
     42  1.1  lukem #	depending upon the setting of MODE:
     43  1.1  lukem #	    crunch	crunchgen(1) config
     44  1.1  lukem #	    mtree	mtree(8) specfile
     45  1.1  lukem #	    populate	sh(1) commands to populate ${TARGDIR} from ${CURDIR}
     46  1.1  lukem #
     47  1.1  lukem # 	Each line of the input is either a comment (starts with `#'),
     48  1.1  lukem #	or contains one of the following keywords and arguments.
     49  1.1  lukem #	In general, keywords in lowercase are crunchgen(1) keywords which
     50  1.1  lukem #	might be also supported for the other operations.
     51  1.1  lukem #
     52  1.3  lukem #	Before each line is parsed, the following strings are replaced with
     53  1.3  lukem #	the appropriate value which is passed in on the command line:
     54  1.3  lukem #
     55  1.3  lukem #	string		value
     56  1.3  lukem #	------		-----
     57  1.3  lukem #	@MACHINE_ARCH@	MACHINE_ARCH
     58  1.3  lukem #	@MACHINE@	MACHINE
     59  1.3  lukem #
     60  1.1  lukem #	mode key	operation
     61  1.1  lukem #	--------	---------
     62  1.1  lukem #	C		crunch
     63  1.1  lukem #	M		mtree
     64  1.1  lukem #	P		populate
     65  1.1  lukem #
     66  1.1  lukem #	mode	keyword arg1 [...]	description
     67  1.1  lukem #	----	------------------	-----------
     68  1.1  lukem #
     69  1.1  lukem #	C	ARGVLN	prog link	as per crunchgen(1) `ln'
     70  1.1  lukem #
     71  1.1  lukem #	P	CMD	arg1 [...]	run CMD as a shell command
     72  1.1  lukem #
     73  1.4  lukem #	M P	COPY	src dest [mode]	copy src to dest
     74  1.4  lukem #
     75  1.4  lukem #	M P	COPYDIR	src dest	recursively copy files under src to
     76  1.4  lukem #					dest.  for M, the directories in src
     77  1.4  lukem #					are listed first.
     78  1.1  lukem #
     79  1.1  lukem #	C	LIBS	libspec ...	as per crunchgen(1) `libs'
     80  1.1  lukem #
     81  1.1  lukem #	M P	LINK	src d1 [d2 ...]	hard link src to d1, d2, ...
     82  1.1  lukem #
     83  1.1  lukem #	C M P	PROG	prog [links...]	program(s) to crunch/mtree/populate.
     84  1.1  lukem #					for M and P, the first prog listed
     85  1.1  lukem #					is copied from ${OBJDIR}/${CRUNCHBIN}
     86  1.1  lukem #					and then used as the name to link
     87  1.1  lukem #					all other PROG entries to.
     88  1.1  lukem #
     89  1.1  lukem #	C	SPECIAL	prog cmd ...	as per crunchgen(1) `special'
     90  1.1  lukem #
     91  1.1  lukem #	C	SRCDIRS	dirname ...	as per crunchgen(1) `srcdirs'
     92  1.1  lukem #
     93  1.1  lukem #	M P	SYMLINK src dest [...]	symlink src to dest, [...]
     94  1.1  lukem #
     95  1.1  lukem 
     96  1.1  lukem BEGIN \
     97  1.1  lukem {
     98  1.3  lukem 	errexit = 0;
     99  1.1  lukem 	crunchprog = "";
    100  1.1  lukem 
    101  1.3  lukem 	if (mode != "crunch" && mode != "mtree" && mode != "populate")
    102  1.3  lukem 		err("Unknown parselist mode '" mode "'");
    103  1.1  lukem 	print "#";
    104  1.1  lukem 	print "# This file is automatically generated by";
    105  1.1  lukem 	print "#\tparselist mode=" mode;
    106  1.1  lukem 	print "#";
    107  1.1  lukem 	print "";
    108  1.1  lukem 	if (mode == "populate") {
    109  1.1  lukem 		print "checkvarisset()";
    110  1.1  lukem 		print "{";
    111  1.1  lukem 		print "	eval _v=\\$${1}";
    112  1.1  lukem 		print "	if [ -z \"$_v\" ]; then";
    113  1.1  lukem 		print "		echo 1>&2 \"Error: $1 is not defined\"";
    114  1.1  lukem 		print "		exit 1";
    115  1.1  lukem 		print "	fi";
    116  1.1  lukem 		print "}";
    117  1.1  lukem 		print;
    118  1.1  lukem 		print "checkvarisset CURDIR";
    119  1.1  lukem 		print "checkvarisset TARGDIR";
    120  1.1  lukem 		print "checkvarisset OBJDIR";
    121  1.1  lukem 		print "checkvarisset CRUNCHBIN";
    122  1.1  lukem 		print "cd ${CURDIR}";
    123  1.1  lukem 		print;
    124  1.1  lukem 	} else if (mode == "mtree") {
    125  1.2  lukem 		print "/unset\tall";
    126  1.1  lukem 		print "/set\ttype=file uname=root gname=wheel";
    127  1.1  lukem 		print;
    128  1.1  lukem 	}
    129  1.1  lukem }
    130  1.1  lukem 
    131  1.1  lukem /^$/ || /^#/ \
    132  1.1  lukem {
    133  1.1  lukem 	print;
    134  1.1  lukem 	next;
    135  1.1  lukem }
    136  1.1  lukem 
    137  1.3  lukem /@MACHINE(_ARCH)?@/ \
    138  1.3  lukem {
    139  1.3  lukem 	gsub(/@MACHINE_ARCH@/, MACHINE_ARCH);
    140  1.3  lukem 	gsub(/@MACHINE@/, MACHINE);
    141  1.3  lukem }
    142  1.3  lukem 
    143  1.1  lukem $1 == "COPY" \
    144  1.1  lukem {
    145  1.3  lukem 	if (NF < 3 || NF > 4)
    146  1.3  lukem 		err("Usage: COPY src dest [mode]");
    147  1.1  lukem 	if (mode == "populate" || mode == "mtree")
    148  1.3  lukem 		copy($2, $3, $4);
    149  1.1  lukem 	next;
    150  1.1  lukem }
    151  1.1  lukem 
    152  1.4  lukem $1 == "COPYDIR" \
    153  1.4  lukem {
    154  1.4  lukem 	if (NF != 3)
    155  1.4  lukem 		err("Usage: COPYDIR src dest");
    156  1.4  lukem 	if (mode == "mtree") {
    157  1.4  lukem 		command="cd " $2 " ; find . -type d -print"
    158  1.4  lukem 		while (command | getline dir) {
    159  1.4  lukem 			gsub(/^\.\//, "", dir);
    160  1.4  lukem 			printf("./%s/%s type=dir mode=755\n", $3, dir);
    161  1.4  lukem 		}
    162  1.4  lukem 		close(command);
    163  1.4  lukem 	}
    164  1.4  lukem 	if (mode == "populate" || mode == "mtree") {
    165  1.4  lukem 		srcdir=$2;
    166  1.4  lukem 		destdir=$3;
    167  1.4  lukem 		command="cd " srcdir " ; find . -type f -print"
    168  1.4  lukem 		while (command | getline srcfile) {
    169  1.4  lukem 			gsub(/^\.\//, "", srcfile);
    170  1.4  lukem 			copy(srcdir "/" srcfile, destdir "/" srcfile, "");
    171  1.4  lukem 		}
    172  1.4  lukem 		close(command);
    173  1.4  lukem 	}
    174  1.4  lukem 	next;
    175  1.4  lukem }
    176  1.4  lukem 
    177  1.1  lukem $1 == "LIBS" || $1 == "SPECIAL" || $1 == "SRCDIRS" \
    178  1.1  lukem {
    179  1.3  lukem 	if (NF < 2)
    180  1.3  lukem 		err("Usage: " $1 " args...");
    181  1.1  lukem 	if (mode == "crunch") {
    182  1.1  lukem 		$1 = tolower($1);
    183  1.1  lukem 		print;
    184  1.1  lukem 	}
    185  1.1  lukem 	next;
    186  1.1  lukem }
    187  1.1  lukem 
    188  1.1  lukem $1 == "PROG" \
    189  1.1  lukem {
    190  1.3  lukem 	if (NF < 2)
    191  1.3  lukem 		err("Usage: PROG prog [link ...]");
    192  1.1  lukem 	if (mode == "crunch") {
    193  1.1  lukem 		prog = basename($2);
    194  1.1  lukem 		print "progs " prog;
    195  1.1  lukem 		for (i = 3; i <= NF; i++)
    196  1.1  lukem 			print "ln " prog " " basename($i);
    197  1.1  lukem 	} else {
    198  1.1  lukem 		for (i = 2; i <= NF; i++) {
    199  1.1  lukem 			if (crunchprog == "") {
    200  1.1  lukem 				crunchprog = $i;
    201  1.1  lukem 				copy("${OBJDIR}/${CRUNCHBIN}", crunchprog);
    202  1.1  lukem 				continue;
    203  1.1  lukem 			}
    204  1.1  lukem 			link(crunchprog, $i);
    205  1.1  lukem 		}
    206  1.1  lukem 	}
    207  1.1  lukem 	next;
    208  1.1  lukem }
    209  1.1  lukem 
    210  1.1  lukem $1 == "ARGVLN" \
    211  1.1  lukem {
    212  1.3  lukem 	if (NF != 3)
    213  1.3  lukem 		err("Usage: ARGVLN prog link");
    214  1.1  lukem 	if (mode == "crunch") {
    215  1.1  lukem 		$1 = "ln";
    216  1.1  lukem 		print;
    217  1.1  lukem 	}
    218  1.1  lukem 	next;
    219  1.1  lukem }
    220  1.1  lukem 
    221  1.1  lukem $1 == "LINK" \
    222  1.1  lukem {
    223  1.3  lukem 	if (NF < 3)
    224  1.3  lukem 		err("Usage: LINK prog link [...]");
    225  1.1  lukem 	if (mode == "populate" || mode == "mtree") {
    226  1.1  lukem 		for (i = 3; i <= NF; i++)
    227  1.1  lukem 			link($2, $i);
    228  1.1  lukem 	}
    229  1.1  lukem 	next;
    230  1.1  lukem }
    231  1.1  lukem 
    232  1.1  lukem $1 == "SYMLINK" \
    233  1.1  lukem {
    234  1.3  lukem 	if (NF < 3)
    235  1.3  lukem 		err("Usage: SYMLINK prog link [...]");
    236  1.1  lukem 	if (mode == "populate" || mode == "mtree") {
    237  1.1  lukem 		for (i = 3; i <= NF; i++)
    238  1.1  lukem 			symlink($2, $i);
    239  1.1  lukem 	}
    240  1.1  lukem 	next;
    241  1.1  lukem }
    242  1.1  lukem 
    243  1.1  lukem $1 == "CMD" \
    244  1.1  lukem {
    245  1.3  lukem 	if (NF < 2)
    246  1.3  lukem 		err("Usage: CMD ...");
    247  1.1  lukem 	if (mode == "populate") {
    248  1.1  lukem 		printf("(cd ${TARGDIR};");
    249  1.1  lukem 		for (i = 2; i <= NF; i++)
    250  1.1  lukem 			printf(" %s", $i);
    251  1.1  lukem 		print ")";
    252  1.1  lukem 	}
    253  1.1  lukem 	next;
    254  1.1  lukem }
    255  1.1  lukem 
    256  1.1  lukem {
    257  1.3  lukem 	err("Unknown keyword '" $1 "'");
    258  1.1  lukem }
    259  1.1  lukem 
    260  1.1  lukem 
    261  1.1  lukem function basename (file) \
    262  1.1  lukem {
    263  1.1  lukem 	gsub(/[^\/]+\//, "", file);
    264  1.1  lukem 	return file;
    265  1.1  lukem }
    266  1.1  lukem 
    267  1.3  lukem function copy (src, dest, perm) \
    268  1.1  lukem {
    269  1.1  lukem 	if (mode == "mtree") {
    270  1.3  lukem 		printf("./%s%s\n", dest, perm != "" ? " mode=" perm : "");
    271  1.1  lukem 	} else {
    272  1.1  lukem 		printf("rm -rf ${TARGDIR}/%s\n", dest);
    273  1.1  lukem 		printf("cp %s ${TARGDIR}/%s\n", src, dest);
    274  1.3  lukem 		if (perm != "")
    275  1.3  lukem 			printf("chmod %s ${TARGDIR}/%s\n", perm, dest);
    276  1.1  lukem 	}
    277  1.1  lukem }
    278  1.1  lukem 
    279  1.1  lukem function link (src, dest) \
    280  1.1  lukem {
    281  1.1  lukem 	if (mode == "mtree") {
    282  1.1  lukem 		printf("./%s\n", dest);
    283  1.1  lukem 	} else {
    284  1.1  lukem 		printf("rm -rf ${TARGDIR}/%s\n", dest);
    285  1.1  lukem 		printf("(cd ${TARGDIR}; ln %s %s)\n", src, dest);
    286  1.1  lukem 	}
    287  1.1  lukem }
    288  1.1  lukem 
    289  1.1  lukem function symlink (src, dest) \
    290  1.1  lukem {
    291  1.1  lukem 	if (mode == "mtree") {
    292  1.1  lukem 		printf("./%s type=link link=%s\n", dest, src);
    293  1.1  lukem 	} else {
    294  1.1  lukem 		printf("rm -rf ${TARGDIR}/%s\n", dest);
    295  1.1  lukem 		printf("(cd ${TARGDIR}; ln -s %s %s)\n", src, dest);
    296  1.1  lukem 	}
    297  1.3  lukem }
    298  1.3  lukem 
    299  1.3  lukem function err(msg) \
    300  1.3  lukem {
    301  1.3  lukem 	printf("%s at line %d of input.\n", msg, NR) >"/dev/stderr";
    302  1.3  lukem 	errexit=1;
    303  1.3  lukem 	exit 1;
    304  1.1  lukem }
    305