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