1#! /usr/bin/awk -f
2#	$NetBSD: devlist2h.awk,v 1.12 2008/05/02 18:11:06 martin Exp $
3#
4# Copyright (c) 1998, 2004 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#
19# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29# POSSIBILITY OF SUCH DAMAGE.
30#
31# Copyright (c) 1995, 1996 Christopher G. Demetriou
32# All rights reserved.
33#
34# Redistribution and use in source and binary forms, with or without
35# modification, are permitted provided that the following conditions
36# are met:
37# 1. Redistributions of source code must retain the above copyright
38#    notice, this list of conditions and the following disclaimer.
39# 2. Redistributions in binary form must reproduce the above copyright
40#    notice, this list of conditions and the following disclaimer in the
41#    documentation and/or other materials provided with the distribution.
42# 3. All advertising materials mentioning features or use of this software
43#    must display the following acknowledgement:
44#      This product includes software developed by Christopher G. Demetriou.
45#      This product includes software developed by Christos Zoulas
46# 4. The name of the author(s) may not be used to endorse or promote products
47#    derived from this software without specific prior written permission
48#
49# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59#
60function collectline(_f, _line) {
61	_oparen = 0
62	_line = ""
63	while (_f <= NF) {
64		if ($_f == "#") {
65			_line = _line "("
66			_oparen = 1
67			_f++
68			continue
69		}
70		if (_oparen) {
71			_line = _line $_f
72			if (_f < NF)
73				_line = _line " "
74			_f++
75			continue
76		}
77		_line = _line $_f
78		if (_f < NF)
79			_line = _line " "
80		_f++
81	}
82	if (_oparen)
83		_line = _line ")"
84	return _line
85}
86BEGIN {
87	nproducts = nvendors = blanklines = 0
88	dfile="pcmciadevs_data.h"
89	hfile="pcmciadevs.h"
90	line=""
91}
92NR == 1 {
93	VERSION = $0
94	gsub("\\$", "", VERSION)
95	gsub(/ $/, "", VERSION)
96
97	printf("/*\t$NetBSD" "$\t*/\n\n") > dfile
98	printf("/*\n") > dfile
99	printf(" * THIS FILE AUTOMATICALLY GENERATED.  DO NOT EDIT.\n") \
100	    > dfile
101	printf(" *\n") > dfile
102	printf(" * generated from:\n") > dfile
103	printf(" *\t%s\n", VERSION) > dfile
104	printf(" */\n") > dfile
105
106	printf("/*\t$NetBSD" "$\t*/\n\n") > hfile
107	printf("/*\n") > hfile
108	printf(" * THIS FILE AUTOMATICALLY GENERATED.  DO NOT EDIT.\n") \
109	    > hfile
110	printf(" *\n") > hfile
111	printf(" * generated from:\n") > hfile
112	printf(" *\t%s\n", VERSION) > hfile
113	printf(" */\n") > hfile
114
115	next
116}
117NF > 0 && $1 == "vendor" {
118	nvendors++
119
120	vendorindex[$2] = nvendors;		# record index for this name, for later.
121	vendors[nvendors, 1] = $2;		# name
122	vendors[nvendors, 2] = $3;		# id
123	printf("#define\tPCMCIA_VENDOR_%s\t%s\t", vendors[nvendors, 1],
124	    vendors[nvendors, 2]) > hfile
125	vendors[nvendors, 3] = collectline(4, line)
126	printf("/* %s */\n", vendors[nvendors, 3]) > hfile
127	next
128}
129NF > 0 && $1 == "product" {
130	nproducts++
131
132	products[nproducts, 1] = $2;		# vendor name
133	products[nproducts, 2] = $3;		# product id
134	products[nproducts, 3] = $4;		# id
135
136	f = 5;
137
138	if ($4 == "{") {
139		products[nproducts, 3] = -1
140		z = "{ "
141		for (i = 0; i < 4; i++) {
142			if (f <= NF) {
143				gsub("&sp", " ", $f)
144				gsub("&tab", "\t", $f)
145				gsub("&nl", "\n", $f)
146				z = z $f " "
147				f++
148			}
149			else {
150				if (i == 3)
151					z = z "NULL "
152				else
153					z = z "NULL, "
154			}
155		}
156		products[nproducts, 4] = z $f
157		f++
158	}
159	else {
160		products[nproducts, 4] = "{ NULL, NULL, NULL, NULL }"
161	}
162	printf("#define\tPCMCIA_CIS_%s_%s\t%s\n",
163	    products[nproducts, 1], products[nproducts, 2],
164	    products[nproducts, 4]) > hfile
165	printf("#define\tPCMCIA_PRODUCT_%s_%s\t%s\n", products[nproducts, 1],
166	    products[nproducts, 2], products[nproducts, 3]) > hfile
167
168	products[nproducts, 5] = collectline(f, line)
169
170	next
171}
172{
173	if ($0 == "")
174		blanklines++
175	print $0 > hfile
176	if (blanklines < 2)
177		print $0 > dfile
178}
179END {
180	# print out the match tables
181
182	printf("\n") > dfile
183
184	printf("struct pcmcia_knowndev {\n") > dfile
185	printf("\tint vendorid;\n") > dfile
186	printf("\tint productid;\n") > dfile
187	printf("\tstruct pcmcia_knowndev_cis {\n") > dfile
188	printf("\t\tchar *vendor;\n") > dfile
189	printf("\t\tchar *product;\n") > dfile
190	printf("\t\tchar *version;\n") > dfile
191	printf("\t\tchar *revision;\n") > dfile
192	printf("\t}cis;\n") > dfile
193	printf("\tint flags;\n") > dfile
194	printf("\tchar *vendorname;\n") > dfile
195	printf("\tchar *devicename;\n") > dfile
196	printf("\tint reserve;\n") > dfile
197	printf("};\n\n") > dfile
198	printf("#define	PCMCIA_CIS_INVALID\t\t{ NULL, NULL, NULL, NULL }\n") > dfile
199	printf("#define	PCMCIA_KNOWNDEV_NOPROD\t\t0\n\n") > dfile
200	printf("struct pcmcia_knowndev pcmcia_knowndevs[] = {\n") > dfile
201	for (i = 1; i <= nproducts; i++) {
202		printf("\t{\n") > dfile
203		if (products[i, 3] == -1) {
204			printf("\t    PCMCIA_VENDOR_UNKNOWN, PCMCIA_PRODUCT_%s_%s,\n",
205			    products[i, 1], products[i, 2]) > dfile
206		} else {
207			printf("\t    PCMCIA_VENDOR_%s, PCMCIA_PRODUCT_%s_%s,\n",
208			    products[i, 1], products[i, 1], products[i, 2]) > dfile
209		}
210		printf("\t    PCMCIA_CIS_%s_%s,\n",
211		    products[i, 1], products[i, 2]) > dfile
212		printf("\t    ") > dfile
213		printf("0") > dfile
214		printf(",\n") > dfile
215
216		if (products[i, 1] in vendorindex) {
217			vendi = vendorindex[products[i, 1]];
218			vendname = vendors[vendi, 3]
219		}
220		else
221			vendname = ""
222		printf("\t    \"%s\",\n", vendname) > dfile
223		printf("\t    \"%s\",\t}\n", products[i, 5]) > dfile
224		printf("\t,\n") > dfile
225	}
226	for (i = 1; i <= nvendors; i++) {
227		printf("\t{\n") > dfile
228		printf("\t    PCMCIA_VENDOR_%s,\n", vendors[i, 1]) > dfile
229		printf("\t    PCMCIA_KNOWNDEV_NOPROD,\n") > dfile
230		printf("\t    PCMCIA_CIS_INVALID,\n") > dfile
231		printf("\t    0,\n") > dfile
232		printf("\t    \"%s\",\n", vendors[i, 3]) > dfile
233		printf("\t    NULL,\n") > dfile
234		printf("\t},\n") > dfile
235	}
236	printf("\t{ 0, 0, { NULL, NULL, NULL, NULL }, 0, NULL, NULL, }\n") > dfile
237	printf("};\n") > dfile
238	close(dfile)
239	close(hfile)
240}
241