xref: /netbsd-src/sys/dev/isapnp/devlist2h.awk (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1#! /usr/bin/awk -f
2#	$NetBSD: devlist2h.awk,v 1.8 2005/12/11 12:22:16 christos Exp $
3#
4# Copyright (c) 1998 The NetBSD Foundation, Inc.
5# All rights reserved.
6#
7# This code is derived from software contributed to The NetBSD Foundation
8# by Christos Zoulas.
9#
10# Redistribution and use in source and binary forms, with or without
11# modification, are permitted provided that the following conditions
12# are met:
13# 1. Redistributions of source code must retain the above copyright
14#    notice, this list of conditions and the following disclaimer.
15# 2. Redistributions in binary form must reproduce the above copyright
16#    notice, this list of conditions and the following disclaimer in the
17#    documentation and/or other materials provided with the distribution.
18# 3. All advertising materials mentioning features or use of this software
19#    must display the following acknowledgement:
20#        This product includes software developed by the NetBSD
21#        Foundation, Inc. and its contributors.
22# 4. Neither the name of The NetBSD Foundation nor the names of its
23#    contributors may be used to endorse or promote products derived
24#    from this software without specific prior written permission.
25#
26# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36# POSSIBILITY OF SUCH DAMAGE.
37#
38# Copyright (c) 1995, 1996 Christopher G. Demetriou
39# All rights reserved.
40#
41# Redistribution and use in source and binary forms, with or without
42# modification, are permitted provided that the following conditions
43# are met:
44# 1. Redistributions of source code must retain the above copyright
45#    notice, this list of conditions and the following disclaimer.
46# 2. Redistributions in binary form must reproduce the above copyright
47#    notice, this list of conditions and the following disclaimer in the
48#    documentation and/or other materials provided with the distribution.
49# 3. All advertising materials mentioning features or use of this software
50#    must display the following acknowledgement:
51#      This product includes software developed by Christopher G. Demetriou.
52#      This product includes software developed by Christos Zoulas
53# 4. The name of the author(s) may not be used to endorse or promote products
54#    derived from this software without specific prior written permission
55#
56# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
57# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
58# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
59# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
60# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
61# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
62# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
63# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
64# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
65# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66#
67function collectline(f) {
68	oparen = 0
69	line = ""
70	while (f <= NF) {
71		if ($f == "#") {
72			line = line "("
73			oparen = 1
74			f++
75			continue
76		}
77		if (oparen) {
78			line = line $f
79			if (f < NF)
80				line = line " "
81			f++
82			continue
83		}
84		line = line $f
85		if (f < NF)
86			line = line " "
87		f++
88	}
89	if (oparen)
90		line = line ")"
91	return line
92}
93function checkdecl() {
94	done = 1
95	if (!decl) {
96		decl = 1;
97		printf("struct isapnp_matchinfo {\n") > hfile
98		printf("\tconst char *name;\n") > hfile
99		printf("\tint variant;\n") > hfile
100		printf("};\n\n") > hfile
101		printf("struct isapnp_devinfo {\n") > hfile
102		printf("\tconst struct isapnp_matchinfo *devlogic;\n") > hfile
103		printf("\tint nlogic;\n") > hfile
104		printf("\tconst struct isapnp_matchinfo *devcompat;\n") > hfile
105		printf("\tint ncompat;\n") > hfile
106		printf("};\n\n") > hfile
107		printf("\n#include <sys/cdefs.h>\n__KERNEL_RCSID(0, \"$NetBSD" "$\");\n\n") > cfile
108		printf("#include <sys/param.h>\n") > cfile
109		printf("#include <dev/isapnp/isapnpdevs.h>\n\n") > cfile
110	}
111}
112BEGIN {
113	decl = done = ncompat = nlogicals = ndriver = blanklines = ncompats = 0
114	cfile="isapnpdevs.c"
115	hfile="isapnpdevs.h"
116}
117NR == 1 {
118	VERSION = $0
119	gsub("\\$", "", VERSION)
120	gsub(/ $/, "", VERSION)
121
122	printf("/*\t$NetBSD" "$\t*/\n\n") > cfile
123	printf("/*\n") > cfile
124	printf(" * THIS FILE IS AUTOMATICALLY GENERATED.  DO NOT EDIT.\n") \
125	    > cfile
126	printf(" *\n") > cfile
127	printf(" * generated from:\n") > cfile
128	printf(" *\t%s\n", VERSION) > cfile
129	printf(" */\n") > cfile
130
131	printf("/*\t$NetBSD" "$\t*/\n\n") > hfile
132	printf("/*\n") > hfile
133	printf(" * THIS FILE IS AUTOMATICALLY GENERATED.  DO NOT EDIT.\n") \
134	    > hfile
135	printf(" *\n") > hfile
136	printf(" * generated from:\n") > hfile
137	printf(" *\t%s\n", VERSION) > hfile
138	printf(" */\n") > hfile
139	printf("\n") > hfile
140	next
141}
142NF > 0 && $1 == "driver" {
143	checkdecl()
144	ndriver++
145
146	driverindex[$2] = ndriver;
147	driver[ndriver, 1] = $2;
148	driver[ndriver, 2] = collectline(3);
149	printf("/* %s */\n", driver[ndriver, 2]) > hfile
150	printf("extern const struct isapnp_devinfo isapnp_%s_devinfo;\n",
151	    driver[ndriver, 1]) > hfile
152	next
153}
154NF > 0 && $1 == "devlogic" {
155	checkdecl()
156	nlogicals++
157
158	logicals[nlogicals, 1] = $2;
159	logicals[nlogicals, 2] = $3;
160	logicals[nlogicals, 3] = $4;
161	logicals[nlogicals, 4] = collectline(5);
162	next
163}
164NF > 0 && $1 == "devcompat" {
165	checkdecl()
166	ncompats++
167
168	compats[ncompats, 1] = $2;
169	compats[ncompats, 2] = $3;
170	compats[ncompats, 3] = $4;
171	compats[ncompats, 4] = collectline(5);
172	next
173}
174{
175	if (!done) {
176		if ($0 == "")
177			blanklines++
178		print $0 > hfile
179		if (blanklines < 2)
180			print $0 > cfile
181	}
182}
183END {
184	# print out the match tables
185
186	printf("\n") > cfile
187
188	for (i = 1; i <= ndriver; i++) {
189		nlogical = ncompat = 0;
190		printf("/* %s */\n", driver[i, 2]) > cfile
191		for (j = 1; j <= nlogicals; j++) {
192			if (logicals[j, 1] == driver[i, 1]) {
193				if (nlogical == 0)
194					printf("static const struct isapnp_matchinfo isapnp_%s_devlogic[] = {\n",
195					    driver[i, 1]) > cfile
196				nlogical++;
197				printf("\t{\"%s\", %d},\t/* %s */\n",
198				    logicals[j, 2], logicals[j, 3],
199				    logicals[j, 4]) > cfile
200			}
201		}
202		if (nlogical != 0)
203			printf("};\n") > cfile
204		for (j = 1; j <= ncompats; j++) {
205			if (compats[j, 1] == driver[i, 1]) {
206				if (ncompat == 0)
207					printf("static const struct isapnp_matchinfo isapnp_%s_devcompat[] = {\n",
208					    driver[i, 1]) > cfile
209				ncompat++;
210				printf("\t{\"%s\", %d},\t/* %s */\n",
211				    compats[j, 2], compats[j, 3],
212				    compats[j, 4]) > cfile
213			}
214		}
215		if (ncompat != 0)
216			printf("};\n") > cfile
217		printf("const struct isapnp_devinfo isapnp_%s_devinfo = {\n",
218		    driver[i, 1]) > cfile
219		if (nlogical != 0)
220			printf("\tisapnp_%s_devlogic, %d,\n",
221			    driver[i, 1], nlogical) > cfile
222		else
223			printf("\tNULL, 0,\n") > cfile
224		if (ncompat != 0)
225			printf("\tisapnp_%s_devcompat, %d,\n",
226			    driver[i, 1], ncompat) > cfile
227		else
228			printf("\tNULL, 0,\n") > cfile
229		printf("};\n\n") > cfile;
230
231	}
232	close(cfile)
233	close(hfile)
234}
235