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