xref: /netbsd-src/sys/dev/isapnp/devlist2h.awk (revision 1a4bdd5296f5962dcad8b665d5787a4de3d3d875)
1bdb2269aSchristos#! /usr/bin/awk -f
2*1a4bdd52Schristos#	$NetBSD: devlist2h.awk,v 1.10 2017/06/03 14:46:29 christos Exp $
3bdb2269aSchristos#
4bd52ba3cSchristos# Copyright (c) 1998 The NetBSD Foundation, Inc.
5bd52ba3cSchristos# All rights reserved.
6bd52ba3cSchristos#
7bd52ba3cSchristos# This code is derived from software contributed to The NetBSD Foundation
8bd52ba3cSchristos# by Christos Zoulas.
9bd52ba3cSchristos#
10bd52ba3cSchristos# Redistribution and use in source and binary forms, with or without
11bd52ba3cSchristos# modification, are permitted provided that the following conditions
12bd52ba3cSchristos# are met:
13bd52ba3cSchristos# 1. Redistributions of source code must retain the above copyright
14bd52ba3cSchristos#    notice, this list of conditions and the following disclaimer.
15bd52ba3cSchristos# 2. Redistributions in binary form must reproduce the above copyright
16bd52ba3cSchristos#    notice, this list of conditions and the following disclaimer in the
17bd52ba3cSchristos#    documentation and/or other materials provided with the distribution.
18bd52ba3cSchristos#
19bd52ba3cSchristos# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20bd52ba3cSchristos# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21bd52ba3cSchristos# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22bd52ba3cSchristos# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23bd52ba3cSchristos# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24bd52ba3cSchristos# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25bd52ba3cSchristos# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26bd52ba3cSchristos# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27bd52ba3cSchristos# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28bd52ba3cSchristos# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29bd52ba3cSchristos# POSSIBILITY OF SUCH DAMAGE.
30bd52ba3cSchristos#
31bdb2269aSchristos# Copyright (c) 1995, 1996 Christopher G. Demetriou
32bdb2269aSchristos# All rights reserved.
33bdb2269aSchristos#
34bdb2269aSchristos# Redistribution and use in source and binary forms, with or without
35bdb2269aSchristos# modification, are permitted provided that the following conditions
36bdb2269aSchristos# are met:
37bdb2269aSchristos# 1. Redistributions of source code must retain the above copyright
38bdb2269aSchristos#    notice, this list of conditions and the following disclaimer.
39bdb2269aSchristos# 2. Redistributions in binary form must reproduce the above copyright
40bdb2269aSchristos#    notice, this list of conditions and the following disclaimer in the
41bdb2269aSchristos#    documentation and/or other materials provided with the distribution.
42bdb2269aSchristos# 3. All advertising materials mentioning features or use of this software
43bdb2269aSchristos#    must display the following acknowledgement:
44bdb2269aSchristos#      This product includes software developed by Christopher G. Demetriou.
45bdb2269aSchristos# 4. The name of the author(s) may not be used to endorse or promote products
46bdb2269aSchristos#    derived from this software without specific prior written permission
47bdb2269aSchristos#
48bdb2269aSchristos# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
49bdb2269aSchristos# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
50bdb2269aSchristos# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
51bdb2269aSchristos# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
52bdb2269aSchristos# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
53bdb2269aSchristos# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54bdb2269aSchristos# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55bdb2269aSchristos# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56bdb2269aSchristos# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
57bdb2269aSchristos# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58bdb2269aSchristos#
5943bf89bfSjmcfunction collectline(f) {
60bdb2269aSchristos	oparen = 0
61bdb2269aSchristos	line = ""
62bdb2269aSchristos	while (f <= NF) {
63bdb2269aSchristos		if ($f == "#") {
64bdb2269aSchristos			line = line "("
65bdb2269aSchristos			oparen = 1
66bdb2269aSchristos			f++
67bdb2269aSchristos			continue
68bdb2269aSchristos		}
69bdb2269aSchristos		if (oparen) {
70bdb2269aSchristos			line = line $f
71bdb2269aSchristos			if (f < NF)
72bdb2269aSchristos				line = line " "
73bdb2269aSchristos			f++
74bdb2269aSchristos			continue
75bdb2269aSchristos		}
76bdb2269aSchristos		line = line $f
77bdb2269aSchristos		if (f < NF)
78bdb2269aSchristos			line = line " "
79bdb2269aSchristos		f++
80bdb2269aSchristos	}
81bdb2269aSchristos	if (oparen)
82bdb2269aSchristos		line = line ")"
83bdb2269aSchristos	return line
84bdb2269aSchristos}
85bdb2269aSchristosfunction checkdecl() {
86bdb2269aSchristos	done = 1
87bdb2269aSchristos	if (!decl) {
88bdb2269aSchristos		decl = 1;
8977274e73Smycroft		printf("struct isapnp_matchinfo {\n") > hfile
9077274e73Smycroft		printf("\tconst char *name;\n") > hfile
9177274e73Smycroft		printf("\tint variant;\n") > hfile
9277274e73Smycroft		printf("};\n\n") > hfile
93bdb2269aSchristos		printf("struct isapnp_devinfo {\n") > hfile
9477274e73Smycroft		printf("\tconst struct isapnp_matchinfo *devlogic;\n") > hfile
9577274e73Smycroft		printf("\tint nlogic;\n") > hfile
9677274e73Smycroft		printf("\tconst struct isapnp_matchinfo *devcompat;\n") > hfile
9777274e73Smycroft		printf("\tint ncompat;\n") > hfile
98bdb2269aSchristos		printf("};\n\n") > hfile
9943bf89bfSjmc		printf("\n#include <sys/cdefs.h>\n__KERNEL_RCSID(0, \"$NetBSD" "$\");\n\n") > cfile
100bdb2269aSchristos		printf("#include <sys/param.h>\n") > cfile
101bdb2269aSchristos		printf("#include <dev/isapnp/isapnpdevs.h>\n\n") > cfile
102bdb2269aSchristos	}
103bdb2269aSchristos}
104bdb2269aSchristosBEGIN {
10543bf89bfSjmc	decl = done = ncompat = nlogicals = ndriver = blanklines = ncompats = 0
106bdb2269aSchristos	cfile="isapnpdevs.c"
107bdb2269aSchristos	hfile="isapnpdevs.h"
108bdb2269aSchristos}
109bdb2269aSchristosNR == 1 {
110bdb2269aSchristos	VERSION = $0
111bdb2269aSchristos	gsub("\\$", "", VERSION)
112a0ba57fbSperry	gsub(/ $/, "", VERSION)
113bdb2269aSchristos
11443bf89bfSjmc	printf("/*\t$NetBSD" "$\t*/\n\n") > cfile
115bdb2269aSchristos	printf("/*\n") > cfile
116cadb8496Saugustss	printf(" * THIS FILE IS AUTOMATICALLY GENERATED.  DO NOT EDIT.\n") \
117bdb2269aSchristos	    > cfile
118bdb2269aSchristos	printf(" *\n") > cfile
119bdb2269aSchristos	printf(" * generated from:\n") > cfile
120bdb2269aSchristos	printf(" *\t%s\n", VERSION) > cfile
121bdb2269aSchristos	printf(" */\n") > cfile
122bdb2269aSchristos
12343bf89bfSjmc	printf("/*\t$NetBSD" "$\t*/\n\n") > hfile
124bdb2269aSchristos	printf("/*\n") > hfile
125cadb8496Saugustss	printf(" * THIS FILE IS AUTOMATICALLY GENERATED.  DO NOT EDIT.\n") \
126bdb2269aSchristos	    > hfile
127bdb2269aSchristos	printf(" *\n") > hfile
128bdb2269aSchristos	printf(" * generated from:\n") > hfile
129bdb2269aSchristos	printf(" *\t%s\n", VERSION) > hfile
130bdb2269aSchristos	printf(" */\n") > hfile
131bdb2269aSchristos	printf("\n") > hfile
132bdb2269aSchristos	next
133bdb2269aSchristos}
13443bf89bfSjmcNF > 0 && $1 == "driver" {
135bdb2269aSchristos	checkdecl()
136bdb2269aSchristos	ndriver++
137bdb2269aSchristos
138bdb2269aSchristos	driverindex[$2] = ndriver;
139bdb2269aSchristos	driver[ndriver, 1] = $2;
14043bf89bfSjmc	driver[ndriver, 2] = collectline(3);
141bdb2269aSchristos	printf("/* %s */\n", driver[ndriver, 2]) > hfile
142bdb2269aSchristos	printf("extern const struct isapnp_devinfo isapnp_%s_devinfo;\n",
143bdb2269aSchristos	    driver[ndriver, 1]) > hfile
144bdb2269aSchristos	next
145bdb2269aSchristos}
14643bf89bfSjmcNF > 0 && $1 == "devlogic" {
147bdb2269aSchristos	checkdecl()
148bdb2269aSchristos	nlogicals++
149bdb2269aSchristos
150bdb2269aSchristos	logicals[nlogicals, 1] = $2;
151bdb2269aSchristos	logicals[nlogicals, 2] = $3;
15277274e73Smycroft	logicals[nlogicals, 3] = $4;
15343bf89bfSjmc	logicals[nlogicals, 4] = collectline(5);
154bdb2269aSchristos	next
155bdb2269aSchristos}
15643bf89bfSjmcNF > 0 && $1 == "devcompat" {
157bdb2269aSchristos	checkdecl()
158bdb2269aSchristos	ncompats++
159bdb2269aSchristos
160bdb2269aSchristos	compats[ncompats, 1] = $2;
161bdb2269aSchristos	compats[ncompats, 2] = $3;
16277274e73Smycroft	compats[ncompats, 3] = $4;
16343bf89bfSjmc	compats[ncompats, 4] = collectline(5);
164bdb2269aSchristos	next
165bdb2269aSchristos}
166bdb2269aSchristos{
167bdb2269aSchristos	if (!done) {
168bdb2269aSchristos		if ($0 == "")
169bdb2269aSchristos			blanklines++
170bdb2269aSchristos		print $0 > hfile
171bdb2269aSchristos		if (blanklines < 2)
172bdb2269aSchristos			print $0 > cfile
173bdb2269aSchristos	}
174bdb2269aSchristos}
175bdb2269aSchristosEND {
176bdb2269aSchristos	# print out the match tables
177bdb2269aSchristos
178bdb2269aSchristos	printf("\n") > cfile
179bdb2269aSchristos
180bdb2269aSchristos	for (i = 1; i <= ndriver; i++) {
18177274e73Smycroft		nlogical = ncompat = 0;
182bdb2269aSchristos		printf("/* %s */\n", driver[i, 2]) > cfile
183bdb2269aSchristos		for (j = 1; j <= nlogicals; j++) {
184bdb2269aSchristos			if (logicals[j, 1] == driver[i, 1]) {
18577274e73Smycroft				if (nlogical == 0)
18677274e73Smycroft					printf("static const struct isapnp_matchinfo isapnp_%s_devlogic[] = {\n",
187bdb2269aSchristos					    driver[i, 1]) > cfile
18877274e73Smycroft				nlogical++;
18977274e73Smycroft				printf("\t{\"%s\", %d},\t/* %s */\n",
19077274e73Smycroft				    logicals[j, 2], logicals[j, 3],
19177274e73Smycroft				    logicals[j, 4]) > cfile
19277274e73Smycroft			}
19377274e73Smycroft		}
19477274e73Smycroft		if (nlogical != 0)
19577274e73Smycroft			printf("};\n") > cfile
196bdb2269aSchristos		for (j = 1; j <= ncompats; j++) {
197bdb2269aSchristos			if (compats[j, 1] == driver[i, 1]) {
19877274e73Smycroft				if (ncompat == 0)
19977274e73Smycroft					printf("static const struct isapnp_matchinfo isapnp_%s_devcompat[] = {\n",
20077274e73Smycroft					    driver[i, 1]) > cfile
20177274e73Smycroft				ncompat++;
20277274e73Smycroft				printf("\t{\"%s\", %d},\t/* %s */\n",
20377274e73Smycroft				    compats[j, 2], compats[j, 3],
20477274e73Smycroft				    compats[j, 4]) > cfile
205bdb2269aSchristos			}
206bdb2269aSchristos		}
20777274e73Smycroft		if (ncompat != 0)
20877274e73Smycroft			printf("};\n") > cfile
209bdb2269aSchristos		printf("const struct isapnp_devinfo isapnp_%s_devinfo = {\n",
210bdb2269aSchristos		    driver[i, 1]) > cfile
21177274e73Smycroft		if (nlogical != 0)
21277274e73Smycroft			printf("\tisapnp_%s_devlogic, %d,\n",
21377274e73Smycroft			    driver[i, 1], nlogical) > cfile
21477274e73Smycroft		else
21577274e73Smycroft			printf("\tNULL, 0,\n") > cfile
21677274e73Smycroft		if (ncompat != 0)
21777274e73Smycroft			printf("\tisapnp_%s_devcompat, %d,\n",
21877274e73Smycroft			    driver[i, 1], ncompat) > cfile
21977274e73Smycroft		else
22077274e73Smycroft			printf("\tNULL, 0,\n") > cfile
22177274e73Smycroft		printf("};\n\n") > cfile;
222bdb2269aSchristos
223bdb2269aSchristos	}
22443bf89bfSjmc	close(cfile)
22543bf89bfSjmc	close(hfile)
226bdb2269aSchristos}
227