1#! /usr/bin/awk -f
2#-
3#	$NetBSD: usb/devlist2h.awk,v 1.9 2001/01/18 20:28:22 jdolecek Exp $
4#
5# SPDX-License-Identifier: BSD-4-Clause
6#
7# Copyright (c) 1995, 1996 Christopher G. Demetriou
8# All rights reserved.
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 Christopher G. Demetriou.
21# 4. The name of the author may not be used to endorse or promote products
22#    derived from this software without specific prior written permission
23#
24# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34#
35
36function usage()
37{
38	print "usage: sdiodevs2h.awk <srcfile> [-d|-h]";
39	exit 1;
40}
41
42function header(file)
43{
44	printf("/*\n") > file
45	printf(" * THIS FILE IS AUTOMATICALLY GENERATED.  DO NOT EDIT.\n") \
46	    > file
47	printf(" *\n") > file
48	printf(" * generated from:\n") > file
49	printf(" *\t%s\n", VERSION) > file
50	printf(" */\n") > file
51}
52
53function vendor(hfile)
54{
55	nvendors++
56
57	vendorindex[$2] = nvendors;		# record index for this name, for later.
58	vendors[nvendors, 1] = $2;		# name
59	vendors[nvendors, 2] = $3;		# id
60	if (hfile)
61		printf("#define\tSDIO_VENDOR_%s\t%s\t", vendors[nvendors, 1],
62		    vendors[nvendors, 2]) > hfile
63	i = 3; f = 4;
64
65	# comments
66	ocomment = oparen = 0
67	if (f <= NF) {
68		if (hfile)
69			printf("\t/* ") > hfile
70		ocomment = 1;
71	}
72	while (f <= NF) {
73		if ($f == "#") {
74			if (hfile)
75				printf("(") > hfile
76			oparen = 1
77			f++
78			continue
79		}
80		if (oparen) {
81			if (hfile)
82				printf("%s", $f) > hfile
83			if (f < NF && hfile)
84				printf(" ") > hfile
85			f++
86			continue
87		}
88		vendors[nvendors, i] = $f
89		if (hfile)
90			printf("%s", vendors[nvendors, i]) > hfile
91		if (f < NF && hfile)
92			printf(" ") > hfile
93		i++; f++;
94	}
95	if (oparen && hfile)
96		printf(")") > hfile
97	if (ocomment && hfile)
98		printf(" */") > hfile
99	if (hfile)
100		printf("\n") > hfile
101}
102
103function product(hfile)
104{
105	nproducts++
106
107	products[nproducts, 1] = $2;		# vendor name
108	products[nproducts, 2] = $3;		# product id
109	products[nproducts, 3] = $4;		# id
110	if (hfile)
111		printf("#define\tSDIO_PRODUCT_%s_%s\t%s\t", \
112		  products[nproducts, 1], products[nproducts, 2], \
113		  products[nproducts, 3]) > hfile
114
115	i=4; f = 5;
116
117	# comments
118	ocomment = oparen = 0
119	if (f <= NF) {
120		if (hfile)
121			printf("\t/* ") > hfile
122		ocomment = 1;
123	}
124	while (f <= NF) {
125		if ($f == "#") {
126			if (hfile)
127				printf("(") > hfile
128			oparen = 1
129			f++
130			continue
131		}
132		if (oparen) {
133			if (hfile)
134				printf("%s", $f) > hfile
135			if (f < NF && hfile)
136				printf(" ") > hfile
137			f++
138			continue
139		}
140		products[nproducts, i] = $f
141		if (hfile)
142			printf("%s", products[nproducts, i]) > hfile
143		if (f < NF && hfile)
144			printf(" ") > hfile
145		i++; f++;
146	}
147	if (oparen && hfile)
148		printf(")") > hfile
149	if (ocomment && hfile)
150		printf(" */") > hfile
151	if (hfile)
152		printf("\n") > hfile
153}
154
155function dump_dfile(dfile)
156{
157	printf("\n") > dfile
158	printf("const struct sdio_knowndev sdio_knowndevs[] = {\n") > dfile
159	for (i = 1; i <= nproducts; i++) {
160		printf("\t{\n") > dfile
161		printf("\t    SDIO_VENDOR_%s, SDIO_PRODUCT_%s_%s,\n",
162		    products[i, 1], products[i, 1], products[i, 2]) > dfile
163		printf("\t    ") > dfile
164		printf("0") > dfile
165		printf(",\n") > dfile
166
167		vendi = vendorindex[products[i, 1]];
168		printf("\t    \"") > dfile
169		j = 3;
170		needspace = 0;
171		while (vendors[vendi, j] != "") {
172			if (needspace)
173				printf(" ") > dfile
174			printf("%s", vendors[vendi, j]) > dfile
175			needspace = 1
176			j++
177		}
178		printf("\",\n") > dfile
179
180		printf("\t    \"") > dfile
181		j = 4;
182		needspace = 0;
183		while (products[i, j] != "") {
184			if (needspace)
185				printf(" ") > dfile
186			printf("%s", products[i, j]) > dfile
187			needspace = 1
188			j++
189		}
190		printf("\",\n") > dfile
191		printf("\t},\n") > dfile
192	}
193	for (i = 1; i <= nvendors; i++) {
194		printf("\t{\n") > dfile
195		printf("\t    SDIO_VENDOR_%s, 0,\n", vendors[i, 1]) > dfile
196		printf("\t    SDIO_KNOWNDEV_NOPROD,\n") > dfile
197		printf("\t    \"") > dfile
198		j = 3;
199		needspace = 0;
200		while (vendors[i, j] != "") {
201			if (needspace)
202				printf(" ") > dfile
203			printf("%s", vendors[i, j]) > dfile
204			needspace = 1
205			j++
206		}
207		printf("\",\n") > dfile
208		printf("\t    NULL,\n") > dfile
209		printf("\t},\n") > dfile
210	}
211	printf("\t{ 0, 0, 0, NULL, NULL, }\n") > dfile
212	printf("};\n") > dfile
213}
214
215BEGIN {
216
217nproducts = nvendors = 0
218# Process the command line
219for (i = 1; i < ARGC; i++) {
220	arg = ARGV[i];
221	if (arg !~ /^-[dh]+$/ && arg !~ /devs$/)
222		usage();
223	if (arg ~ /^-.*d/)
224		dfile="sdiodevs_data.h"
225	if (arg ~ /^-.*h/)
226		hfile="sdiodevs.h"
227	if (arg ~ /devs$/)
228		srcfile = arg;
229}
230ARGC = 1;
231line=0;
232
233while ((getline < srcfile) > 0) {
234	line++;
235	if (line == 1) {
236		VERSION = $0
237		gsub("\\$", "", VERSION)
238		if (dfile)
239			header(dfile)
240		if (hfile)
241			header(hfile)
242		continue;
243	}
244	if ($1 == "vendor") {
245		vendor(hfile)
246		continue
247	}
248	if ($1 == "product") {
249		product(hfile)
250		continue
251	}
252	if ($0 == "")
253		blanklines++
254	if (hfile)
255		print $0 > hfile
256	if (blanklines < 2 && dfile)
257	    print $0 > dfile
258}
259
260# print out the match tables
261
262if (dfile)
263	dump_dfile(dfile)
264}
265