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