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