xref: /freebsd/sys/tools/sdiodevs2h.awk (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
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}
49
50function vendor(hfile)
51{
52	nvendors++
53
54	vendorindex[$2] = nvendors;		# record index for this name, for later.
55	vendors[nvendors, 1] = $2;		# name
56	vendors[nvendors, 2] = $3;		# id
57	if (hfile)
58		printf("#define\tSDIO_VENDOR_ID_%s\t%s\t", vendors[nvendors, 1],
59		    vendors[nvendors, 2]) > hfile
60	i = 3; f = 4;
61
62	# comments
63	ocomment = oparen = 0
64	if (f <= NF) {
65		if (hfile)
66			printf("\t/* ") > hfile
67		ocomment = 1;
68	}
69	while (f <= NF) {
70		if ($f == "#") {
71			if (hfile)
72				printf("(") > hfile
73			oparen = 1
74			f++
75			continue
76		}
77		if (oparen) {
78			if (hfile)
79				printf("%s", $f) > hfile
80			if (f < NF && hfile)
81				printf(" ") > hfile
82			f++
83			continue
84		}
85		vendors[nvendors, i] = $f
86		if (hfile)
87			printf("%s", vendors[nvendors, i]) > hfile
88		if (f < NF && hfile)
89			printf(" ") > hfile
90		i++; f++;
91	}
92	if (oparen && hfile)
93		printf(")") > hfile
94	if (ocomment && hfile)
95		printf(" */") > hfile
96	if (hfile)
97		printf("\n") > hfile
98}
99
100function product(hfile)
101{
102	nproducts++
103
104	products[nproducts, 1] = $2;		# vendor name
105	products[nproducts, 2] = $3;		# product id
106	products[nproducts, 3] = $4;		# id
107	if (hfile)
108		printf("#define\tSDIO_DEVICE_ID_%s_%s\t%s\t", \
109		  products[nproducts, 1], products[nproducts, 2], \
110		  products[nproducts, 3]) > hfile
111
112	i=4; f = 5;
113
114	# comments
115	ocomment = oparen = 0
116	if (f <= NF) {
117		if (hfile)
118			printf("\t/* ") > hfile
119		ocomment = 1;
120	}
121	while (f <= NF) {
122		if ($f == "#") {
123			if (hfile)
124				printf("(") > hfile
125			oparen = 1
126			f++
127			continue
128		}
129		if (oparen) {
130			if (hfile)
131				printf("%s", $f) > hfile
132			if (f < NF && hfile)
133				printf(" ") > hfile
134			f++
135			continue
136		}
137		products[nproducts, i] = $f
138		if (hfile)
139			printf("%s", products[nproducts, i]) > hfile
140		if (f < NF && hfile)
141			printf(" ") > hfile
142		i++; f++;
143	}
144	if (oparen && hfile)
145		printf(")") > hfile
146	if (ocomment && hfile)
147		printf(" */") > hfile
148	if (hfile)
149		printf("\n") > hfile
150}
151
152function palias(hfile)
153{
154	nproducts++
155
156	products[nproducts, 1] = $2;		# vendor name
157	products[nproducts, 2] = $3;		# product id
158	products[nproducts, 3] = $4;		# id
159	if (hfile)
160		printf("#define\tSDIO_DEVICE_ID_%s\tSDIO_DEVICE_ID_%s\n", \
161		  $2, $3) > hfile
162}
163
164function dump_dfile(dfile)
165{
166	printf("\n") > dfile
167	printf("const struct sdio_knowndev sdio_knowndevs[] = {\n") > dfile
168	for (i = 1; i <= nproducts; i++) {
169		printf("\t{\n") > dfile
170		printf("\t    SDIO_VENDOR_ID_%s, SDIO_DEVICE_ID_%s_%s,\n",
171		    products[i, 1], products[i, 1], products[i, 2]) > dfile
172		printf("\t    ") > dfile
173		printf("0") > dfile
174		printf(",\n") > dfile
175
176		vendi = vendorindex[products[i, 1]];
177		printf("\t    \"") > dfile
178		j = 3;
179		needspace = 0;
180		while (vendors[vendi, j] != "") {
181			if (needspace)
182				printf(" ") > dfile
183			printf("%s", vendors[vendi, j]) > dfile
184			needspace = 1
185			j++
186		}
187		printf("\",\n") > dfile
188
189		printf("\t    \"") > dfile
190		j = 4;
191		needspace = 0;
192		while (products[i, j] != "") {
193			if (needspace)
194				printf(" ") > dfile
195			printf("%s", products[i, j]) > dfile
196			needspace = 1
197			j++
198		}
199		printf("\",\n") > dfile
200		printf("\t},\n") > dfile
201	}
202	for (i = 1; i <= nvendors; i++) {
203		printf("\t{\n") > dfile
204		printf("\t    SDIO_VENDOR_ID_%s, 0,\n", vendors[i, 1]) > dfile
205		printf("\t    SDIO_KNOWNDEV_NOPROD,\n") > dfile
206		printf("\t    \"") > dfile
207		j = 3;
208		needspace = 0;
209		while (vendors[i, j] != "") {
210			if (needspace)
211				printf(" ") > dfile
212			printf("%s", vendors[i, j]) > dfile
213			needspace = 1
214			j++
215		}
216		printf("\",\n") > dfile
217		printf("\t    NULL,\n") > dfile
218		printf("\t},\n") > dfile
219	}
220	printf("\t{ 0, 0, 0, NULL, NULL, }\n") > dfile
221	printf("};\n") > dfile
222}
223
224BEGIN {
225
226nproducts = nvendors = 0
227# Process the command line
228for (i = 1; i < ARGC; i++) {
229	arg = ARGV[i];
230	if (arg !~ /^-[dh]+$/ && arg !~ /devs$/)
231		usage();
232	if (arg ~ /^-.*d/)
233		dfile="sdiodevs_data.h"
234	if (arg ~ /^-.*h/)
235		hfile="sdiodevs.h"
236	if (arg ~ /devs$/)
237		srcfile = arg;
238}
239ARGC = 1;
240line=0;
241
242while ((getline < srcfile) > 0) {
243	line++;
244	if (line == 1) {
245		if (dfile)
246			header(dfile)
247		if (hfile)
248			header(hfile)
249	}
250	if ($1 == "vendor") {
251		vendor(hfile)
252		continue
253	}
254	if ($1 == "product") {
255		product(hfile)
256		continue
257	}
258	if ($1 == "palias") {
259		palias(hfile)
260		continue
261	}
262	if ($0 == "")
263		blanklines++
264	if (hfile)
265		print $0 > hfile
266	if (blanklines < 2 && dfile)
267	    print $0 > dfile
268}
269
270# print out the match tables
271
272if (dfile)
273	dump_dfile(dfile)
274}
275