MAKEDEV.awk revision 1.1       1 #!/usr/bin/awk -
      2 #
      3 #	$NetBSD: MAKEDEV.awk,v 1.1 2003/10/13 09:37:45 jdolecek Exp $
      4 #
      5 # Copyright (c) 2003 The NetBSD Foundation, Inc.
      6 # All rights reserved.
      7 #
      8 # This code is derived from software contributed to The NetBSD Foundation
      9 # by Jaromir Dolecek.
     10 #
     11 # Redistribution and use in source and binary forms, with or without
     12 # modification, are permitted provided that the following conditions
     13 # are met:
     14 # 1. Redistributions of source code must retain the above copyright
     15 #    notice, this list of conditions and the following disclaimer.
     16 # 2. Redistributions in binary form must reproduce the above copyright
     17 #    notice, this list of conditions and the following disclaimer in the
     18 #    documentation and/or other materials provided with the distribution.
     19 # 3. All advertising materials mentioning features or use of this software
     20 #    must display the following acknowledgement:
     21 #        This product includes software developed by the NetBSD
     22 #        Foundation, Inc. and its contributors.
     23 # 4. Neither the name of The NetBSD Foundation nor the names of its
     24 #    contributors may be used to endorse or promote products derived
     25 #    from this software without specific prior written permission.
     26 #
     27 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37 # POSSIBILITY OF SUCH DAMAGE.
     38 #
     39 
     40 BEGIN {
     41 	# top of source tree, used to find major number list in kernel
     42 	# sources
     43 	top = ENVIRON["NETBSDSRCDIR"]
     44 	if (!top)
     45 		top = ".."
     46 	top = top "/sys/"
     47 	if (system("test -d '" top "'") != 0) {
     48 		print "ERROR: didn't find top of kernel tree ('" top "' not a directory)" > "/dev/stderr"
     49 		exit 1
     50 	}
     51 
     52 	machine = ENVIRON["MACHINE"]
     53 	if (!machine)
     54 		machine = "i386"	# XXX for testing
     55 	maarch = ENVIRON["MACHINE_ARCH"]
     56 
     57 	# file with major definitions
     58 	majors[0] = "conf/majors"
     59 	if (maarch == "arm32")
     60 		majors[1] = "arch/arm/conf/majors.arm32";
     61 	else if (machine == "evbsh5") {
     62 		majors[1] = "arch/evbsh5/conf/majors.evbsh5";
     63 		majors[2] = "arch/sh5/conf/majors.sh5";
     64 	} else
     65 		majors[1] = "arch/" machine "/conf/majors." machine;
     66 
     67 	# process all files with majors and fill the chr[] and blk[]
     68 	# arrays, used in template processing
     69 	for(m in majors) {
     70 		file = top majors[m]
     71 		while (getline < file) {
     72 			if ($1 == "device-major") {
     73 				if ($3 == "char") {
     74 					chr[$2] = $4
     75 					if ($5 == "block")
     76 						blk[$2] = $6
     77 				} else if ($3 == "block")
     78 					blk[$2] = $4
     79 			}
     80 		}
     81 	}
     82 
     83 	# initially no substitutions
     84 	devsubst = 0
     85 	deventry = ""
     86 }
     87 
     88 /%MI_DEVICES_BEGIN%/ {
     89 	devsubst = 1;
     90 	next
     91 }
     92 
     93 /%MI_DEVICES_END%/ {
     94 	devsubst = 0;
     95 	next
     96 }
     97 
     98 {
     99 	# if device substitutions are not active, do nothing more
    100 	if (!devsubst) {
    101 		print
    102 		next
    103 	}
    104 }
    105 
    106 # first line of device entry
    107 /^[a-z].*\)$/ {
    108 	if (length(deventry) > 0) {
    109 		# We have a previous entry to print. Replace all known
    110 		# character and block devices. If no unknown character
    111 		# or block device definition remains within the entry,
    112 		# print it to output, otherwise scrap it.
    113 		for(c in chr)
    114 			gsub("%" c "_chr%", chr[c], deventry)
    115 		for(b in blk)
    116 			gsub("%" b "_blk%", blk[b], deventry)
    117 
    118 		if (deventry !~ "%[a-z]*_chr%" && deventry !~ "%[a-z]*_blk%")
    119 			print deventry
    120 	}
    121 	deventry = $0
    122 	next
    123 }
    124 
    125 # template line within device substitution section - just keep appending
    126 # to the current entry
    127 {
    128 	deventry = deventry "\n" $0
    129 }
    130