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