xref: /freebsd/sys/tools/usbdevs2h.awk (revision a90b9d0159070121c221b966469c3e36d912bf82)
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: usbdevs2h.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\tUSB_VENDOR_%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\tUSB_PRODUCT_%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 dump_dfile(dfile)
153{
154	printf("\n") > dfile
155	printf("const struct usb_knowndev usb_knowndevs[] = {\n") > dfile
156	for (i = 1; i <= nproducts; i++) {
157		printf("\t{\n") > dfile
158		printf("\t    USB_VENDOR_%s, USB_PRODUCT_%s_%s,\n",
159		    products[i, 1], products[i, 1], products[i, 2]) > dfile
160		printf("\t    ") > dfile
161		printf("0") > dfile
162		printf(",\n") > dfile
163
164		vendi = vendorindex[products[i, 1]];
165		printf("\t    \"") > dfile
166		j = 3;
167		needspace = 0;
168		while (vendors[vendi, j] != "") {
169			if (needspace)
170				printf(" ") > dfile
171			printf("%s", vendors[vendi, j]) > dfile
172			needspace = 1
173			j++
174		}
175		printf("\",\n") > dfile
176
177		printf("\t    \"") > dfile
178		j = 4;
179		needspace = 0;
180		while (products[i, j] != "") {
181			if (needspace)
182				printf(" ") > dfile
183			printf("%s", products[i, j]) > dfile
184			needspace = 1
185			j++
186		}
187		printf("\",\n") > dfile
188		printf("\t},\n") > dfile
189	}
190	for (i = 1; i <= nvendors; i++) {
191		printf("\t{\n") > dfile
192		printf("\t    USB_VENDOR_%s, 0,\n", vendors[i, 1]) > dfile
193		printf("\t    USB_KNOWNDEV_NOPROD,\n") > dfile
194		printf("\t    \"") > dfile
195		j = 3;
196		needspace = 0;
197		while (vendors[i, j] != "") {
198			if (needspace)
199				printf(" ") > dfile
200			printf("%s", vendors[i, j]) > dfile
201			needspace = 1
202			j++
203		}
204		printf("\",\n") > dfile
205		printf("\t    NULL,\n") > dfile
206		printf("\t},\n") > dfile
207	}
208	printf("\t{ 0, 0, 0, NULL, NULL, }\n") > dfile
209	printf("};\n") > dfile
210}
211
212BEGIN {
213
214nproducts = nvendors = 0
215# Process the command line
216for (i = 1; i < ARGC; i++) {
217	arg = ARGV[i];
218	if (arg !~ /^-[dh]+$/ && arg !~ /devs$/)
219		usage();
220	if (arg ~ /^-.*d/)
221		dfile="usbdevs_data.h"
222	if (arg ~ /^-.*h/)
223		hfile="usbdevs.h"
224	if (arg ~ /devs$/)
225		srcfile = arg;
226}
227ARGC = 1;
228line=0;
229
230while ((getline < srcfile) > 0) {
231	line++;
232	if (line == 1) {
233		if (dfile)
234			header(dfile)
235		if (hfile)
236			header(hfile)
237		continue;
238	}
239	if ($1 == "vendor") {
240		vendor(hfile)
241		continue
242	}
243	if ($1 == "product") {
244		product(hfile)
245		continue
246	}
247	if ($0 == "")
248		blanklines++
249	if (hfile)
250		print $0 > hfile
251	if (blanklines < 2 && dfile)
252	    print $0 > dfile
253}
254
255# print out the match tables
256
257if (dfile)
258	dump_dfile(dfile)
259}
260