usbdevs2h.awk revision 139513
153383Sn_hibma#! /usr/bin/awk -f
293569Sjoe#	$NetBSD: usb/devlist2h.awk,v 1.9 2001/01/18 20:28:22 jdolecek Exp $
353383Sn_hibma#  $FreeBSD: head/sys/tools/usbdevs2h.awk 139513 2004-12-31 21:02:43Z imp $
453383Sn_hibma#
553383Sn_hibma# Copyright (c) 1995, 1996 Christopher G. Demetriou
653383Sn_hibma# All rights reserved.
753383Sn_hibma#
853383Sn_hibma# Redistribution and use in source and binary forms, with or without
953383Sn_hibma# modification, are permitted provided that the following conditions
1053383Sn_hibma# are met:
1153383Sn_hibma# 1. Redistributions of source code must retain the above copyright
1253383Sn_hibma#    notice, this list of conditions and the following disclaimer.
1353383Sn_hibma# 2. Redistributions in binary form must reproduce the above copyright
1453383Sn_hibma#    notice, this list of conditions and the following disclaimer in the
1553383Sn_hibma#    documentation and/or other materials provided with the distribution.
1653383Sn_hibma# 3. All advertising materials mentioning features or use of this software
1753383Sn_hibma#    must display the following acknowledgement:
1853383Sn_hibma#      This product includes software developed by Christopher G. Demetriou.
1953383Sn_hibma# 4. The name of the author may not be used to endorse or promote products
2053383Sn_hibma#    derived from this software without specific prior written permission
2153383Sn_hibma#
2253383Sn_hibma# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2353383Sn_hibma# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2453383Sn_hibma# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2553383Sn_hibma# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2653383Sn_hibma# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2753383Sn_hibma# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2853383Sn_hibma# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2953383Sn_hibma# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3053383Sn_hibma# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3153383Sn_hibma# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3253383Sn_hibma#
33139513Simp
34139458Simpfunction usage()
35139458Simp{
36139458Simp	print "usage: usbdevs2h.awk <srcfile> [-d|-h]";
37139458Simp	exit 1;
38139458Simp}
39139458Simp
40139513Simpfunction dheader(dfile)
41139513Simp{
42139513Simp	if (os == "NetBSD")
43139513Simp		printf("/*\t\$NetBSD\$\t*/\n\n") > dfile
44139513Simp	else if (os == "FreeBSD")
45139513Simp		printf("/* \$FreeBSD\$ */\n\n") > dfile
46139513Simp	else if (os == "OpenBSD")
47139513Simp		printf("/*\t\$OpenBSD\$\t*/\n\n") > dfile
48139513Simp	else
49139513Simp		printf("/* ??? */\n\n") > dfile
50139513Simp	printf("/*\n") > dfile
51139513Simp	printf(" * THIS FILE IS AUTOMATICALLY GENERATED.  DO NOT EDIT.\n") \
52139513Simp	    > dfile
53139513Simp	printf(" *\n") > dfile
54139513Simp	printf(" * generated from:\n") > dfile
55139513Simp	printf(" *\t%s\n", VERSION) > dfile
56139513Simp	printf(" */\n") > dfile
57139513Simp}
58139458Simp
59139513Simpfunction hheader(hfile)
60139513Simp{
61139513Simp	if (os == "NetBSD")
62139513Simp		printf("/*\t\$NetBSD\$\t*/\n\n") > hfile
63139513Simp	else if (os == "FreeBSD")
64139513Simp		printf("/* \$FreeBSD\$ */\n\n") > hfile
65139513Simp	else if (os == "OpenBSD")
66139513Simp		printf("/*\t\$OpenBSD\$\t*/\n\n") > hfile
67139513Simp	else
68139513Simp		printf("/* ??? */\n\n") > hfile
69139513Simp	printf("/*\n") > hfile
70139513Simp	printf(" * THIS FILE IS AUTOMATICALLY GENERATED.  DO NOT EDIT.\n") \
71139513Simp	    > hfile
72139513Simp	printf(" *\n") > hfile
73139513Simp	printf(" * generated from:\n") > hfile
74139513Simp	printf(" *\t%s\n", VERSION) > hfile
75139513Simp	printf(" */\n") > hfile
7653383Sn_hibma}
77139458Simp
7853383Sn_hibma
79139513Simpfunction vendor(hfile)
80139513Simp{
8153383Sn_hibma	nvendors++
8253383Sn_hibma
8353383Sn_hibma	vendorindex[$2] = nvendors;		# record index for this name, for later.
8453383Sn_hibma	vendors[nvendors, 1] = $2;		# name
8553383Sn_hibma	vendors[nvendors, 2] = $3;		# id
86139458Simp	if (hfile)
87139458Simp		printf("#define\tUSB_VENDOR_%s\t%s\t", vendors[nvendors, 1],
88139458Simp		    vendors[nvendors, 2]) > hfile
8953383Sn_hibma	i = 3; f = 4;
9053383Sn_hibma
9153383Sn_hibma	# comments
9253383Sn_hibma	ocomment = oparen = 0
9353383Sn_hibma	if (f <= NF) {
94139458Simp		if (hfile)
95139458Simp			printf("\t/* ") > hfile
9653383Sn_hibma		ocomment = 1;
9753383Sn_hibma	}
9853383Sn_hibma	while (f <= NF) {
9953383Sn_hibma		if ($f == "#") {
100139458Simp			if (hfile)
101139458Simp				printf("(") > hfile
10253383Sn_hibma			oparen = 1
10353383Sn_hibma			f++
10453383Sn_hibma			continue
10553383Sn_hibma		}
10653383Sn_hibma		if (oparen) {
107139458Simp			if (hfile)
108139458Simp				printf("%s", $f) > hfile
109139458Simp			if (f < NF && hfile)
11053383Sn_hibma				printf(" ") > hfile
11153383Sn_hibma			f++
11253383Sn_hibma			continue
11353383Sn_hibma		}
11453383Sn_hibma		vendors[nvendors, i] = $f
115139458Simp		if (hfile)
116139458Simp			printf("%s", vendors[nvendors, i]) > hfile
117139458Simp		if (f < NF && hfile)
11853383Sn_hibma			printf(" ") > hfile
11953383Sn_hibma		i++; f++;
12053383Sn_hibma	}
121139458Simp	if (oparen && hfile)
12253383Sn_hibma		printf(")") > hfile
123139458Simp	if (ocomment && hfile)
12453383Sn_hibma		printf(" */") > hfile
125139458Simp	if (hfile)
126139458Simp		printf("\n") > hfile
127139513Simp}
12853383Sn_hibma
129139513Simpfunction product(hfile)
130139513Simp{
13153383Sn_hibma	nproducts++
13253383Sn_hibma
13353383Sn_hibma	products[nproducts, 1] = $2;		# vendor name
13453383Sn_hibma	products[nproducts, 2] = $3;		# product id
13553383Sn_hibma	products[nproducts, 3] = $4;		# id
136139458Simp	if (hfile)
137139458Simp		printf("#define\tUSB_PRODUCT_%s_%s\t%s\t", \
138139458Simp		  products[nproducts, 1], products[nproducts, 2], \
139139458Simp		  products[nproducts, 3]) > hfile
14053383Sn_hibma
14153383Sn_hibma	i=4; f = 5;
14253383Sn_hibma
14353383Sn_hibma	# comments
14453383Sn_hibma	ocomment = oparen = 0
14553383Sn_hibma	if (f <= NF) {
146139458Simp		if (hfile)
147139458Simp			printf("\t/* ") > hfile
14853383Sn_hibma		ocomment = 1;
14953383Sn_hibma	}
15053383Sn_hibma	while (f <= NF) {
15153383Sn_hibma		if ($f == "#") {
152139458Simp			if (hfile)
153139458Simp				printf("(") > hfile
15453383Sn_hibma			oparen = 1
15553383Sn_hibma			f++
15653383Sn_hibma			continue
15753383Sn_hibma		}
15853383Sn_hibma		if (oparen) {
159139458Simp			if (hfile)
160139458Simp				printf("%s", $f) > hfile
161139458Simp			if (f < NF && hfile)
16253383Sn_hibma				printf(" ") > hfile
16353383Sn_hibma			f++
16453383Sn_hibma			continue
16553383Sn_hibma		}
16653383Sn_hibma		products[nproducts, i] = $f
167139458Simp		if (hfile)
168139458Simp			printf("%s", products[nproducts, i]) > hfile
169139458Simp		if (f < NF && hfile)
17053383Sn_hibma			printf(" ") > hfile
17153383Sn_hibma		i++; f++;
17253383Sn_hibma	}
173139458Simp	if (oparen && hfile)
17453383Sn_hibma		printf(")") > hfile
175139458Simp	if (ocomment && hfile)
17653383Sn_hibma		printf(" */") > hfile
177139458Simp	if (hfile)
178139458Simp		printf("\n") > hfile
179139513Simp}
18053383Sn_hibma
181139513Simpfunction dump_dfile(dfile)
182139513Simp{
183139513Simp	printf("\n") > dfile
184139513Simp	printf("const struct usb_knowndev usb_knowndevs[] = {\n") > dfile
185139513Simp	for (i = 1; i <= nproducts; i++) {
186139513Simp		printf("\t{\n") > dfile
187139513Simp		printf("\t    USB_VENDOR_%s, USB_PRODUCT_%s_%s,\n",
188139513Simp		    products[i, 1], products[i, 1], products[i, 2]) > dfile
189139513Simp		printf("\t    ") > dfile
190139513Simp		printf("0") > dfile
191139513Simp		printf(",\n") > dfile
192139513Simp
193139513Simp		vendi = vendorindex[products[i, 1]];
194139513Simp		printf("\t    \"") > dfile
195139513Simp		j = 3;
196139513Simp		needspace = 0;
197139513Simp		while (vendors[vendi, j] != "") {
198139513Simp			if (needspace)
199139513Simp				printf(" ") > dfile
200139513Simp			printf("%s", vendors[vendi, j]) > dfile
201139513Simp			needspace = 1
202139513Simp			j++
203139513Simp		}
204139513Simp		printf("\",\n") > dfile
205139513Simp
206139513Simp		printf("\t    \"") > dfile
207139513Simp		j = 4;
208139513Simp		needspace = 0;
209139513Simp		while (products[i, j] != "") {
210139513Simp			if (needspace)
211139513Simp				printf(" ") > dfile
212139513Simp			printf("%s", products[i, j]) > dfile
213139513Simp			needspace = 1
214139513Simp			j++
215139513Simp		}
216139513Simp		printf("\",\n") > dfile
217139513Simp		printf("\t},\n") > dfile
218139513Simp	}
219139513Simp	for (i = 1; i <= nvendors; i++) {
220139513Simp		printf("\t{\n") > dfile
221139513Simp		printf("\t    USB_VENDOR_%s, 0,\n", vendors[i, 1]) > dfile
222139513Simp		printf("\t    USB_KNOWNDEV_NOPROD,\n") > dfile
223139513Simp		printf("\t    \"") > dfile
224139513Simp		j = 3;
225139513Simp		needspace = 0;
226139513Simp		while (vendors[i, j] != "") {
227139513Simp			if (needspace)
228139513Simp				printf(" ") > dfile
229139513Simp			printf("%s", vendors[i, j]) > dfile
230139513Simp			needspace = 1
231139513Simp			j++
232139513Simp		}
233139513Simp		printf("\",\n") > dfile
234139513Simp		printf("\t    NULL,\n") > dfile
235139513Simp		printf("\t},\n") > dfile
236139513Simp	}
237139513Simp	printf("\t{ 0, 0, 0, NULL, NULL, }\n") > dfile
238139513Simp	printf("};\n") > dfile
239139513Simp}
240139513Simp
241139513SimpBEGIN {
242139513Simp
243139513Simpnproducts = nvendors = 0
244139513Simp# Process the command line
245139513Simpfor (i = 1; i < ARGC; i++) {
246139513Simp	arg = ARGV[i];
247139513Simp	if (arg !~ /^-[dh]+$/ && arg !~ /devs$/)
248139513Simp		usage();
249139513Simp	if (arg ~ /^-.*d/)
250139513Simp		dfile="usbdevs_data.h"
251139513Simp	if (arg ~ /^-.*h/)
252139513Simp		hfile="usbdevs.h"
253139513Simp	if (arg ~ /devs$/)
254139513Simp		srcfile = arg;
255139513Simp}
256139513SimpARGC = 1;
257139513Simpline=0;
258139513Simp
259139513Simpwhile ((getline < srcfile) > 0) {
260139513Simp	line++;
261139513Simp	if (line == 1) {
262139513Simp		VERSION = $0
263139513Simp		gsub("\\$", "", VERSION)
264139513Simp
265139513Simp		if (dfile)
266139513Simp			dheader(dfile)
267139513Simp
268139513Simp		if (hfile)
269139513Simp			hheader(hfile)
270139513Simp		continue;
271139513Simp	}
272139513Simp	if ($1 == "vendor") {
273139513Simp		vendor(hfile)
274139513Simp		continue
275139513Simp	}
276139513Simp	if ($1 == "product") {
277139513Simp		product(hfile)
278139513Simp		continue
279139513Simp	}
28053383Sn_hibma	if ($0 == "")
28153383Sn_hibma		blanklines++
282139458Simp	if (hfile)
283139458Simp		print $0 > hfile
284139458Simp	if (blanklines < 2 && dfile)
285139458Simp	    print $0 > dfile
28653383Sn_hibma}
28753383Sn_hibma
288139513Simp# print out the match tables
28953383Sn_hibma
290139513Simpif (dfile)
291139513Simp	dump_dfile(dfile)
29253383Sn_hibma}
293