MAKEDEV.awk revision 1.19.4.2       1  1.19.4.2  garbled #!/usr/bin/awk -
      2  1.19.4.2  garbled #
      3  1.19.4.2  garbled #	$NetBSD: MAKEDEV.awk,v 1.19.4.2 2007/12/10 17:57:25 garbled Exp $
      4  1.19.4.2  garbled #
      5  1.19.4.2  garbled # Copyright (c) 2003 The NetBSD Foundation, Inc.
      6  1.19.4.2  garbled # All rights reserved.
      7  1.19.4.2  garbled #
      8  1.19.4.2  garbled # This code is derived from software contributed to The NetBSD Foundation
      9  1.19.4.2  garbled # by Jaromir Dolecek.
     10  1.19.4.2  garbled #
     11  1.19.4.2  garbled # Redistribution and use in source and binary forms, with or without
     12  1.19.4.2  garbled # modification, are permitted provided that the following conditions
     13  1.19.4.2  garbled # are met:
     14  1.19.4.2  garbled # 1. Redistributions of source code must retain the above copyright
     15  1.19.4.2  garbled #    notice, this list of conditions and the following disclaimer.
     16  1.19.4.2  garbled # 2. Redistributions in binary form must reproduce the above copyright
     17  1.19.4.2  garbled #    notice, this list of conditions and the following disclaimer in the
     18  1.19.4.2  garbled #    documentation and/or other materials provided with the distribution.
     19  1.19.4.2  garbled # 3. All advertising materials mentioning features or use of this software
     20  1.19.4.2  garbled #    must display the following acknowledgement:
     21  1.19.4.2  garbled #        This product includes software developed by the NetBSD
     22  1.19.4.2  garbled #        Foundation, Inc. and its contributors.
     23  1.19.4.2  garbled # 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.19.4.2  garbled #    contributors may be used to endorse or promote products derived
     25  1.19.4.2  garbled #    from this software without specific prior written permission.
     26  1.19.4.2  garbled #
     27  1.19.4.2  garbled # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.19.4.2  garbled # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.19.4.2  garbled # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.19.4.2  garbled # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.19.4.2  garbled # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.19.4.2  garbled # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.19.4.2  garbled # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.19.4.2  garbled # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.19.4.2  garbled # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.19.4.2  garbled # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.19.4.2  garbled # POSSIBILITY OF SUCH DAMAGE.
     38  1.19.4.2  garbled #
     39  1.19.4.2  garbled 
     40  1.19.4.2  garbled # Script to generate platform MAKEDEV script from MI template, MD
     41  1.19.4.2  garbled # MAKEDEV.conf and MD/MI major lists
     42  1.19.4.2  garbled #
     43  1.19.4.2  garbled # Uses environment variables MACHINE/MACHINE_ARCH to select
     44  1.19.4.2  garbled # appropriate files, and NETBSDSRCDIR to get root of source tree.
     45  1.19.4.2  garbled 
     46  1.19.4.2  garbled BEGIN {
     47  1.19.4.2  garbled 	# top of source tree, used to find major number list in kernel
     48  1.19.4.2  garbled 	# sources
     49  1.19.4.2  garbled 	machine = ENVIRON["MACHINE"]
     50  1.19.4.2  garbled 	maarch = ENVIRON["MACHINE_ARCH"]
     51  1.19.4.2  garbled 	srcdir = ENVIRON["NETBSDSRCDIR"]
     52  1.19.4.2  garbled 	if (!machine || !maarch || !srcdir) {
     53  1.19.4.2  garbled 		print "ERROR: 'MACHINE', 'MACHINE_ARCH' and 'NETBSDSRCDIR' must be set in environment" > "/dev/stderr"
     54  1.19.4.2  garbled 		exit 1
     55  1.19.4.2  garbled 	}
     56  1.19.4.2  garbled 	top = srcdir "/sys/"
     57  1.19.4.2  garbled 	if (system("test -d '" top "'") != 0) {
     58  1.19.4.2  garbled 		print "ERROR: can't find top of kernel source tree ('" top "' not a directory)" > "/dev/stderr"
     59  1.19.4.2  garbled 		exit 1
     60  1.19.4.2  garbled 	}
     61  1.19.4.2  garbled 
     62  1.19.4.2  garbled 
     63  1.19.4.2  garbled 	# file with major definitions
     64  1.19.4.2  garbled 	majors[0] = "conf/majors"
     65  1.19.4.2  garbled 	if ((maarch == "arm" || maarch == "armeb") && system("test -f '" top "arch/" machine "/conf/majors." machine "'") != 0)
     66  1.19.4.2  garbled 		majors[1] = "arch/arm/conf/majors.arm32";
     67  1.19.4.2  garbled 	else if (machine == "sbmips")
     68  1.19.4.2  garbled 		majors[1] = "arch/evbmips/conf/majors.evbmips";
     69  1.19.4.2  garbled 	else if ((maarch == "powerpc") && system("test -f '" top "arch/" machine "/conf/majors." machine "'") != 0)
     70  1.19.4.2  garbled 		majors[1] = "arch/powerpc/conf/majors.powerpc";
     71  1.19.4.2  garbled 	else
     72  1.19.4.2  garbled 		majors[1] = "arch/" machine "/conf/majors." machine;
     73  1.19.4.2  garbled 
     74  1.19.4.2  garbled 	# process all files with majors and fill the chr[] and blk[]
     75  1.19.4.2  garbled 	# arrays, used in template processing
     76  1.19.4.2  garbled 	for (m in majors) {
     77  1.19.4.2  garbled 		file = top majors[m]
     78  1.19.4.2  garbled 		if (system("test -f '" file "'") != 0) {
     79  1.19.4.2  garbled 			print "ERROR: can't find majors file '" file "'" > "/dev/stderr"
     80  1.19.4.2  garbled 			exit 1
     81  1.19.4.2  garbled 		}
     82  1.19.4.2  garbled 		while (getline < file) {
     83  1.19.4.2  garbled 			if ($1 == "device-major") {
     84  1.19.4.2  garbled 				if ($3 == "char") {
     85  1.19.4.2  garbled 					chr[$2] = $4
     86  1.19.4.2  garbled 					if ($5 == "block")
     87  1.19.4.2  garbled 						blk[$2] = $6
     88  1.19.4.2  garbled 				} else if ($3 == "block")
     89  1.19.4.2  garbled 					blk[$2] = $4
     90  1.19.4.2  garbled 			}
     91  1.19.4.2  garbled 		}
     92  1.19.4.2  garbled 		close(file)
     93  1.19.4.2  garbled 	}
     94  1.19.4.2  garbled 	CONSOLE_CMAJOR = chr["cons"]
     95  1.19.4.2  garbled 	if (CONSOLE_CMAJOR == "") {
     96  1.19.4.2  garbled 		print "ERROR: no entry for 'cons' in majors file" > "/dev/stderr"
     97  1.19.4.2  garbled 		exit 1
     98  1.19.4.2  garbled 	}
     99  1.19.4.2  garbled 
    100  1.19.4.2  garbled 	# read MD config file for MD device targets
    101  1.19.4.2  garbled 	cfgfile = srcdir "/etc/etc." machine "/MAKEDEV.conf"
    102  1.19.4.2  garbled 	if (system("test -f '" cfgfile "'") != 0) {
    103  1.19.4.2  garbled 		print "ERROR: no platform MAKEDEV.conf - '" cfgfile "' doesn't exist" > "/dev/stderr"
    104  1.19.4.2  garbled 		exit 1
    105  1.19.4.2  garbled 	}
    106  1.19.4.2  garbled 	# skip first two lines
    107  1.19.4.2  garbled 	getline CONFRCSID < cfgfile	# RCS Id
    108  1.19.4.2  garbled 	getline < cfgfile		# blank line
    109  1.19.4.2  garbled 	MDDEV = 0		# MD device targets
    110  1.19.4.2  garbled 	while (getline < cfgfile) {
    111  1.19.4.2  garbled 		if (MDDEV)
    112  1.19.4.2  garbled 			MDDEV = MDDEV "\n" $0
    113  1.19.4.2  garbled 		else
    114  1.19.4.2  garbled 			MDDEV = $0
    115  1.19.4.2  garbled 	}
    116  1.19.4.2  garbled 	close(cfgfile)
    117  1.19.4.2  garbled 
    118  1.19.4.2  garbled 	# determine number of partitions used by platform
    119  1.19.4.2  garbled 	# there are three variants in tree:
    120  1.19.4.2  garbled 	# 1. MAXPARTITIONS = 8
    121  1.19.4.2  garbled 	# 2. MAXPARTITIONS = 16 with no back compat mapping
    122  1.19.4.2  garbled 	# 3. MAXPARTITIONS = 16 with back compat with old limit of 8
    123  1.19.4.2  garbled 	# currently all archs, which moved from 8->16 use same
    124  1.19.4.2  garbled 	# scheme for mapping disk minors, high minor offset
    125  1.19.4.2  garbled 	# if this changes, the below needs to be adjusted and
    126  1.19.4.2  garbled 	# additional makedisk_p16foo needs to be added
    127  1.19.4.2  garbled 	incdir = machine
    128  1.19.4.2  garbled 	diskpartitions = 0
    129  1.19.4.2  garbled 	diskbackcompat = 0
    130  1.19.4.2  garbled 	while (1) {
    131  1.19.4.2  garbled 		inc = top "arch/" incdir "/include/disklabel.h"
    132  1.19.4.2  garbled 		if (system("test -f '" inc "'") != 0) {
    133  1.19.4.2  garbled 			print "ERROR: can't find kernel include file '" inc "'" > "/dev/stderr"
    134  1.19.4.2  garbled 			exit 1
    135  1.19.4.2  garbled 		}
    136  1.19.4.2  garbled 		incdir = 0
    137  1.19.4.2  garbled 		while (getline < inc) {
    138  1.19.4.2  garbled 			if ($1 == "#define" && $2 == "MAXPARTITIONS")
    139  1.19.4.2  garbled 				diskpartitions = $3
    140  1.19.4.2  garbled 			else if ($1 == "#define" && $2 == "OLDMAXPARTITIONS")
    141  1.19.4.2  garbled 				diskbackcompat = $3
    142  1.19.4.2  garbled 			else if ($1 == "#define" && $2 == "RAW_PART")
    143  1.19.4.2  garbled 				RAWDISK_OFF = $3
    144  1.19.4.2  garbled 			else if ($1 == "#include" && 
    145  1.19.4.2  garbled 				 $2 ~ "<.*/disklabel.h>" &&
    146  1.19.4.2  garbled 				 $2 !~ ".*nbinclude.*")
    147  1.19.4.2  garbled 			{
    148  1.19.4.2  garbled 				# wrapper, switch to the right file
    149  1.19.4.2  garbled 				incdir = substr($2, 2)
    150  1.19.4.2  garbled 				sub("/.*", "", incdir)
    151  1.19.4.2  garbled 				break;
    152  1.19.4.2  garbled 			}
    153  1.19.4.2  garbled 		}
    154  1.19.4.2  garbled 		close(inc)
    155  1.19.4.2  garbled 
    156  1.19.4.2  garbled 		if (diskpartitions)
    157  1.19.4.2  garbled 			break;
    158  1.19.4.2  garbled 
    159  1.19.4.2  garbled 		if (!incdir) {
    160  1.19.4.2  garbled 			print "ERROR: can't determine MAXPARTITIONS from include file '" inc "'" > "/dev/stderr"
    161  1.19.4.2  garbled 			exit 1
    162  1.19.4.2  garbled 		}
    163  1.19.4.2  garbled 	}
    164  1.19.4.2  garbled 	MKDISK = "makedisk_p" diskpartitions	# routine to create disk devs
    165  1.19.4.2  garbled 	DISKMINOROFFSET = diskpartitions
    166  1.19.4.2  garbled 	if (diskbackcompat) {
    167  1.19.4.2  garbled 		MKDISK = MKDISK "high"
    168  1.19.4.2  garbled 		DISKMINOROFFSET = diskbackcompat
    169  1.19.4.2  garbled 	}
    170  1.19.4.2  garbled 	RAWDISK_NAME = sprintf("%c", 97 + RAWDISK_OFF)		# a+offset
    171  1.19.4.2  garbled 
    172  1.19.4.2  garbled 	# read etc/master.passwd for user name->UID mapping
    173  1.19.4.2  garbled 	idfile = srcdir "/etc/master.passwd"
    174  1.19.4.2  garbled 	if (system("test -f '" idfile "'") != 0) {
    175  1.19.4.2  garbled 		print "ERROR: can't find password file '" idfile "'" > "/dev/stderr"
    176  1.19.4.2  garbled 		exit 1
    177  1.19.4.2  garbled 	}
    178  1.19.4.2  garbled 	oldFS=FS
    179  1.19.4.2  garbled 	FS=":"
    180  1.19.4.2  garbled 	while (getline < idfile) {
    181  1.19.4.2  garbled 		uid[$1] = $3
    182  1.19.4.2  garbled 	}
    183  1.19.4.2  garbled 	close(idfile)
    184  1.19.4.2  garbled 	FS=oldFS
    185  1.19.4.2  garbled 
    186  1.19.4.2  garbled 	# read etc/group for group name->GID mapping
    187  1.19.4.2  garbled 	idfile = srcdir "/etc/group"
    188  1.19.4.2  garbled 	if (system("test -f '" idfile "'") != 0) {
    189  1.19.4.2  garbled 		print "ERROR: can't find group file '" idfile "'" > "/dev/stderr"
    190  1.19.4.2  garbled 		exit 1
    191  1.19.4.2  garbled 	}
    192  1.19.4.2  garbled 	oldFS=FS
    193  1.19.4.2  garbled 	FS=":"
    194  1.19.4.2  garbled 	while (getline < idfile) {
    195  1.19.4.2  garbled 		gid[$1] = $3
    196  1.19.4.2  garbled 	}
    197  1.19.4.2  garbled 	close(idfile)
    198  1.19.4.2  garbled 	FS=oldFS
    199  1.19.4.2  garbled 
    200  1.19.4.2  garbled 	# initially no substitutions
    201  1.19.4.2  garbled 	devsubst = 0
    202  1.19.4.2  garbled 	deventry = ""
    203  1.19.4.2  garbled }
    204  1.19.4.2  garbled 
    205  1.19.4.2  garbled /%MI_DEVICES_BEGIN%/ {
    206  1.19.4.2  garbled 	devsubst = 1;
    207  1.19.4.2  garbled 	next
    208  1.19.4.2  garbled }
    209  1.19.4.2  garbled 
    210  1.19.4.2  garbled /%MI_DEVICES_END%/ {
    211  1.19.4.2  garbled 	devsubst = 0;
    212  1.19.4.2  garbled 	next
    213  1.19.4.2  garbled }
    214  1.19.4.2  garbled 
    215  1.19.4.2  garbled # output 'Generated from' lines
    216  1.19.4.2  garbled /\$[N]etBSD/ {
    217  1.19.4.2  garbled 	print "#"
    218  1.19.4.2  garbled 	print "# Generated from:"
    219  1.19.4.2  garbled 
    220  1.19.4.2  garbled 	# MAKEDEV.awk (this script) RCS Id
    221  1.19.4.2  garbled 	ARCSID = "$NetBSD: MAKEDEV.awk,v 1.19.4.2 2007/12/10 17:57:25 garbled Exp $"
    222  1.19.4.2  garbled 	gsub(/\$/, "", ARCSID)
    223  1.19.4.2  garbled 	print "#	" ARCSID
    224  1.19.4.2  garbled 	
    225  1.19.4.2  garbled 	# MAKEDEV.tmpl RCS Id
    226  1.19.4.2  garbled 	gsub(/\$/, "")
    227  1.19.4.2  garbled 	print $0
    228  1.19.4.2  garbled 
    229  1.19.4.2  garbled 	# MD MAKEDEV.conf RCS Id
    230  1.19.4.2  garbled 	# strip leading hash and insert machine subdirectory name
    231  1.19.4.2  garbled 	gsub(/\$/, "", CONFRCSID)
    232  1.19.4.2  garbled 	sub(/^\# /, "", CONFRCSID)
    233  1.19.4.2  garbled 	sub(/MAKEDEV.conf/, "etc." machine "/MAKEDEV.conf", CONFRCSID)
    234  1.19.4.2  garbled 	print "#	" CONFRCSID
    235  1.19.4.2  garbled 
    236  1.19.4.2  garbled 	next # don't print the RCS Id line again
    237  1.19.4.2  garbled }
    238  1.19.4.2  garbled 
    239  1.19.4.2  garbled # filter the 'PLEASE RUN ...' paragraph
    240  1.19.4.2  garbled /^\#   PLEASE RUN/, /^\#\#\#\#\#\#/ {
    241  1.19.4.2  garbled 	next
    242  1.19.4.2  garbled }
    243  1.19.4.2  garbled  
    244  1.19.4.2  garbled # filter the device list
    245  1.19.4.2  garbled /^\# Tapes/,/^$/ {
    246  1.19.4.2  garbled 	next
    247  1.19.4.2  garbled }
    248  1.19.4.2  garbled 
    249  1.19.4.2  garbled # filter the two unneeded makedisk_p* routines, leave only
    250  1.19.4.2  garbled # the one used
    251  1.19.4.2  garbled /^makedisk_p8\(\) \{/, /^\}/ {
    252  1.19.4.2  garbled 	if (MKDISK != "makedisk_p8")
    253  1.19.4.2  garbled 		next;
    254  1.19.4.2  garbled }
    255  1.19.4.2  garbled /^makedisk_p16\(\) \{/, /^\}/ {
    256  1.19.4.2  garbled 	if (MKDISK != "makedisk_p16")
    257  1.19.4.2  garbled 		next;
    258  1.19.4.2  garbled }
    259  1.19.4.2  garbled /^makedisk_p16high\(\) \{/, /^\}/ {
    260  1.19.4.2  garbled 	if (MKDISK != "makedisk_p16high")
    261  1.19.4.2  garbled 		next;
    262  1.19.4.2  garbled }
    263  1.19.4.2  garbled 
    264  1.19.4.2  garbled # special cases aside, handle normal line
    265  1.19.4.2  garbled {
    266  1.19.4.2  garbled 	sub(/^%MD_DEVICES%/, MDDEV)
    267  1.19.4.2  garbled 	sub(/%MKDISK%/, MKDISK)
    268  1.19.4.2  garbled 	sub(/%DISKMINOROFFSET%/, DISKMINOROFFSET)
    269  1.19.4.2  garbled 	sub(/%RAWDISK_OFF%/, RAWDISK_OFF)
    270  1.19.4.2  garbled 	sub(/%RAWDISK_NAME%/, RAWDISK_NAME)
    271  1.19.4.2  garbled 	sub(/%CONSOLE_CMAJOR%/, CONSOLE_CMAJOR)
    272  1.19.4.2  garbled 	parsed = ""
    273  1.19.4.2  garbled 	line = $0
    274  1.19.4.2  garbled 	while (match(line, /%[gu]id_[a-z]*%/)) {
    275  1.19.4.2  garbled 		typ = substr(line, RSTART + 1, 3);
    276  1.19.4.2  garbled 		nam = substr(line, RSTART + 5, RLENGTH - 6);
    277  1.19.4.2  garbled 		if (typ == "uid") {
    278  1.19.4.2  garbled 			if (!(nam in uid)) {
    279  1.19.4.2  garbled 				print "ERROR unmatched uid in `" $0 "'" > \
    280  1.19.4.2  garbled 				    "/dev/stderr"
    281  1.19.4.2  garbled 				exit 1
    282  1.19.4.2  garbled 			} else
    283  1.19.4.2  garbled 				id = uid[nam];
    284  1.19.4.2  garbled 		} else {
    285  1.19.4.2  garbled 			if (!(nam in gid)) {
    286  1.19.4.2  garbled 				print "ERROR unmatched gid in `" $0 "'" > \
    287  1.19.4.2  garbled 				    "/dev/stderr"
    288  1.19.4.2  garbled 				exit 1
    289  1.19.4.2  garbled 			} else
    290  1.19.4.2  garbled 				id = gid[nam];
    291  1.19.4.2  garbled 		}
    292  1.19.4.2  garbled 		parsed = parsed substr(line, 1, RSTART - 1) id
    293  1.19.4.2  garbled 		line = substr(line, RSTART + RLENGTH)
    294  1.19.4.2  garbled 	}
    295  1.19.4.2  garbled 	$0 = parsed line
    296  1.19.4.2  garbled 
    297  1.19.4.2  garbled 	# if device substitutions are not active, do nothing more
    298  1.19.4.2  garbled 	if (!devsubst) {
    299  1.19.4.2  garbled 		print
    300  1.19.4.2  garbled 		next
    301  1.19.4.2  garbled 	}
    302  1.19.4.2  garbled }
    303  1.19.4.2  garbled 
    304  1.19.4.2  garbled # first line of device entry
    305  1.19.4.2  garbled /^[a-z].*\)$/ {
    306  1.19.4.2  garbled 	if (length(deventry) > 0) {
    307  1.19.4.2  garbled 		# We have a previous entry to print. Replace all known
    308  1.19.4.2  garbled 		# character and block devices. If no unknown character
    309  1.19.4.2  garbled 		# or block device definition remains within the entry,
    310  1.19.4.2  garbled 		# print it to output, otherwise scrap it.
    311  1.19.4.2  garbled 		parsed = ""
    312  1.19.4.2  garbled 		while (match(deventry, /%[a-z]*_(blk|chr)%/)) {
    313  1.19.4.2  garbled 			nam = substr(deventry, RSTART + 1, RLENGTH - 6);
    314  1.19.4.2  garbled 			typ = substr(deventry, RSTART + RLENGTH - 4, 3);
    315  1.19.4.2  garbled 			if (typ == "blk") {
    316  1.19.4.2  garbled 				if (!(nam in blk)) {
    317  1.19.4.2  garbled 					deventry = $0
    318  1.19.4.2  garbled 					next
    319  1.19.4.2  garbled 				} else
    320  1.19.4.2  garbled 					dev = blk[nam];
    321  1.19.4.2  garbled 			} else {
    322  1.19.4.2  garbled 				if (!(nam in chr)) {
    323  1.19.4.2  garbled 					deventry = $0
    324  1.19.4.2  garbled 					next
    325  1.19.4.2  garbled 				} else
    326  1.19.4.2  garbled 					dev = chr[nam];
    327  1.19.4.2  garbled 			}
    328  1.19.4.2  garbled 			parsed = parsed substr(deventry, 1, RSTART - 1) dev
    329  1.19.4.2  garbled 			deventry = substr(deventry, RSTART + RLENGTH)
    330  1.19.4.2  garbled 		}
    331  1.19.4.2  garbled 
    332  1.19.4.2  garbled 		print parsed deventry
    333  1.19.4.2  garbled 	}
    334  1.19.4.2  garbled 	deventry = $0
    335  1.19.4.2  garbled 	next
    336  1.19.4.2  garbled }
    337  1.19.4.2  garbled 
    338  1.19.4.2  garbled # template line within device substitution section - just keep appending
    339  1.19.4.2  garbled # to the current entry
    340  1.19.4.2  garbled {
    341  1.19.4.2  garbled 	deventry = deventry "\n" $0
    342  1.19.4.2  garbled }
    343