MAKEDEV.awk revision 1.4
1#!/usr/bin/awk -
2#
3#	$NetBSD: MAKEDEV.awk,v 1.4 2003/10/17 19:01:49 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# Script to generate platform MAKEDEV script from MI template, MD
41# MAKEDEV.conf and MD/MI major lists
42#
43# Uses environment variables MACHINE/MACHINE_ARCH to select
44# appropriate files, and NETBSDSRCDIR to get root of source tree.
45
46BEGIN {
47	# top of source tree, used to find major number list in kernel
48	# sources
49	top = ENVIRON["NETBSDSRCDIR"]
50	if (!top)
51		top = ".."
52	top = top "/sys/"
53	if (system("test -d '" top "'") != 0) {
54		print "ERROR: can't find top of kernel source tree ('" top "' not a directory)" > "/dev/stderr"
55		exit 1
56	}
57
58	machine = ENVIRON["MACHINE"]
59	maarch = ENVIRON["MACHINE_ARCH"]
60	if (!machine || !maarch) {
61		print "ERROR: 'MACHINE' and 'MACHINE_ARCH' must be set in environment" > "/dev/stderr"
62		exit 1
63	}
64
65	# file with major definitions
66	majors[0] = "conf/majors"
67	if (maarch == "arm32")
68		majors[1] = "arch/arm/conf/majors.arm32";
69	else if (machine == "evbsh5") {
70		majors[1] = "arch/evbsh5/conf/majors.evbsh5";
71		majors[2] = "arch/sh5/conf/majors.sh5";
72	} else
73		majors[1] = "arch/" machine "/conf/majors." machine;
74
75	# process all files with majors and fill the chr[] and blk[]
76	# arrays, used in template processing
77	for (m in majors) {
78		file = top majors[m]
79		if (system("test -f '" file "'") != 0) {
80			print "ERROR: can't find majors file '" file "'" > "/dev/stderr"
81			exit 1
82		}
83			
84		while (getline < file) {
85			if ($1 == "device-major") {
86				if ($3 == "char") {
87					chr[$2] = $4
88					if ($5 == "block")
89						blk[$2] = $6
90				} else if ($3 == "block")
91					blk[$2] = $4
92			}
93		}
94	}
95
96	# read MD config file, and determine disk partitions
97	# and MD device list
98	cfgfile = "etc." machine "/MAKEDEV.conf"
99	MDDEV = 0		# MD device targets
100	MKDISK = ""		# routine to create disk devices
101	while (getline < cfgfile) {
102		if ($1 ~ "^DISKPARTITIONS=") {
103			sub(".*=[ \t]*", "")
104			MKDISK = "makedisk_p" $0
105		} else if (MDDEV) {
106			if (MDDEV == 1)
107				MDDEV = $0
108			else
109				MDDEV = MDDEV "\n" $0
110		} else if ($1 ~ "^MD_DEVICES=")
111			MDDEV = 1
112	}
113
114	# initially no substitutions
115	devsubst = 0
116	deventry = ""
117}
118
119/%MI_DEVICES_BEGIN%/ {
120	devsubst = 1;
121	next
122}
123
124/%MI_DEVICES_END%/ {
125	devsubst = 0;
126	next
127}
128
129# filter the two unneeded makedisk_p* routines, leave only
130# the one used
131/^makedisk_p8\(\) {/, /^}/ {
132	if (MKDISK != "makedisk_p8")
133		next;
134}
135/^makedisk_p16\(\) {/, /^}/ {
136	if (MKDISK != "makedisk_p16")
137		next;
138}
139/^makedisk_p16high\(\) {/, /^}/ {
140	if (MKDISK != "makedisk_p16high")
141		next;
142}
143
144# special cases aside, handle normal line 
145{
146	sub("^%MD_DEVICES%", MDDEV)
147	sub("%MKDISK%", MKDISK)
148
149	# if device substitutions are not active, do nothing more
150	if (!devsubst) {
151		print
152		next
153	}
154}
155
156# first line of device entry
157/^[a-z].*\)$/ {
158	if (length(deventry) > 0) {
159		# We have a previous entry to print. Replace all known
160		# character and block devices. If no unknown character
161		# or block device definition remains within the entry,
162		# print it to output, otherwise scrap it.
163		for (c in chr)
164			gsub("%" c "_chr%", chr[c], deventry)
165		for (b in blk)
166			gsub("%" b "_blk%", blk[b], deventry)
167
168		if (deventry !~ "%[a-z]*_chr%" && deventry !~ "%[a-z]*_blk%")
169			print deventry
170	}
171	deventry = $0
172	next
173}
174
175# template line within device substitution section - just keep appending
176# to the current entry
177{
178	deventry = deventry "\n" $0
179}
180