usbdevs2h.awk revision 139458
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 139458 2004-12-30 23:18:34Z 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#
33139458Simpfunction usage()
34139458Simp{
35139458Simp	print "usage: usbdevs2h.awk <srcfile> [-d|-h]";
36139458Simp	exit 1;
37139458Simp}
38139458Simp
3953383Sn_hibmaBEGIN {
40139458Simp
41139458Simpnproducts = nvendors = 0
42139458Simp# Process the command line
43139458Simpfor (i = 1; i < ARGC; i++) {
44139458Simp	arg = ARGV[i];
45139458Simp	if (arg !~ /^-[dh]+$/ && arg !~ /devs$/)
46139458Simp		usage();
47139458Simp	if (arg ~ /^-.*d/)
48139458Simp		dfile="usbdevs_data.h"
49139458Simp	if (arg ~ /^-.*h/)
50139458Simp		hfile="usbdevs.h"
51139458Simp	if (arg ~ /devs$/)
52139458Simp		srcfile = arg;
5353383Sn_hibma}
54139458SimpARGC = 1;
55139458Simpline=0;
56139458Simp
57139458Simpwhile ((getline < srcfile) > 0) {
58139458Simp    line++;
59139458Simp    if (line == 1) {
6053383Sn_hibma	VERSION = $0
6153383Sn_hibma	gsub("\\$", "", VERSION)
6253383Sn_hibma
63139458Simp	if (dfile) {
64139458Simp		if (os == "NetBSD")
65139458Simp			printf("/*\t\$NetBSD\$\t*/\n\n") > dfile
66139458Simp		else if (os == "FreeBSD")
67139458Simp			printf("/* \$FreeBSD\$ */\n\n") > dfile
68139458Simp		else if (os == "OpenBSD")
69139458Simp			printf("/*\t\$OpenBSD\$\t*/\n\n") > dfile
70139458Simp		else
71139458Simp			printf("/* ??? */\n\n") > dfile
72139458Simp		printf("/*\n") > dfile
73139458Simp		printf(" * THIS FILE IS AUTOMATICALLY GENERATED.  DO NOT EDIT.\n") \
74139458Simp		    > dfile
75139458Simp		printf(" *\n") > dfile
76139458Simp		printf(" * generated from:\n") > dfile
77139458Simp		printf(" *\t%s\n", VERSION) > dfile
78139458Simp		printf(" */\n") > dfile
79139458Simp	}
80139458Simp	
81139458Simp	if (hfile) {
82139458Simp		if (os == "NetBSD")
83139458Simp			printf("/*\t\$NetBSD\$\t*/\n\n") > hfile
84139458Simp		else if (os == "FreeBSD")
85139458Simp			printf("/* \$FreeBSD\$ */\n\n") > hfile
86139458Simp		else if (os == "OpenBSD")
87139458Simp			printf("/*\t\$OpenBSD\$\t*/\n\n") > hfile
88139458Simp		else
89139458Simp			printf("/* ??? */\n\n") > hfile
90139458Simp		printf("/*\n") > hfile
91139458Simp		printf(" * THIS FILE IS AUTOMATICALLY GENERATED.  DO NOT EDIT.\n") \
92139458Simp		    > hfile
93139458Simp		printf(" *\n") > hfile
94139458Simp		printf(" * generated from:\n") > hfile
95139458Simp		printf(" *\t%s\n", VERSION) > hfile
96139458Simp		printf(" */\n") > hfile
97139458Simp	}
98139458Simp	continue;
99139458Simp    }
100139458Simp   if ($1 == "vendor") {
10153383Sn_hibma	nvendors++
10253383Sn_hibma
10353383Sn_hibma	vendorindex[$2] = nvendors;		# record index for this name, for later.
10453383Sn_hibma	vendors[nvendors, 1] = $2;		# name
10553383Sn_hibma	vendors[nvendors, 2] = $3;		# id
106139458Simp	if (hfile)
107139458Simp		printf("#define\tUSB_VENDOR_%s\t%s\t", vendors[nvendors, 1],
108139458Simp		    vendors[nvendors, 2]) > hfile
10953383Sn_hibma	i = 3; f = 4;
11053383Sn_hibma
11153383Sn_hibma	# comments
11253383Sn_hibma	ocomment = oparen = 0
11353383Sn_hibma	if (f <= NF) {
114139458Simp		if (hfile)
115139458Simp			printf("\t/* ") > hfile
11653383Sn_hibma		ocomment = 1;
11753383Sn_hibma	}
11853383Sn_hibma	while (f <= NF) {
11953383Sn_hibma		if ($f == "#") {
120139458Simp			if (hfile)
121139458Simp				printf("(") > hfile
12253383Sn_hibma			oparen = 1
12353383Sn_hibma			f++
12453383Sn_hibma			continue
12553383Sn_hibma		}
12653383Sn_hibma		if (oparen) {
127139458Simp			if (hfile)
128139458Simp				printf("%s", $f) > hfile
129139458Simp			if (f < NF && hfile)
13053383Sn_hibma				printf(" ") > hfile
13153383Sn_hibma			f++
13253383Sn_hibma			continue
13353383Sn_hibma		}
13453383Sn_hibma		vendors[nvendors, i] = $f
135139458Simp		if (hfile)
136139458Simp			printf("%s", vendors[nvendors, i]) > hfile
137139458Simp		if (f < NF && hfile)
13853383Sn_hibma			printf(" ") > hfile
13953383Sn_hibma		i++; f++;
14053383Sn_hibma	}
141139458Simp	if (oparen && hfile)
14253383Sn_hibma		printf(")") > hfile
143139458Simp	if (ocomment && hfile)
14453383Sn_hibma		printf(" */") > hfile
145139458Simp	if (hfile)
146139458Simp		printf("\n") > hfile
14753383Sn_hibma
148139458Simp	continue;
149139458Simp    }
150139458Simp    if ($1 == "product") {
15153383Sn_hibma	nproducts++
15253383Sn_hibma
15353383Sn_hibma	products[nproducts, 1] = $2;		# vendor name
15453383Sn_hibma	products[nproducts, 2] = $3;		# product id
15553383Sn_hibma	products[nproducts, 3] = $4;		# id
156139458Simp	if (hfile)
157139458Simp		printf("#define\tUSB_PRODUCT_%s_%s\t%s\t", \
158139458Simp		  products[nproducts, 1], products[nproducts, 2], \
159139458Simp		  products[nproducts, 3]) > hfile
16053383Sn_hibma
16153383Sn_hibma	i=4; f = 5;
16253383Sn_hibma
16353383Sn_hibma	# comments
16453383Sn_hibma	ocomment = oparen = 0
16553383Sn_hibma	if (f <= NF) {
166139458Simp		if (hfile)
167139458Simp			printf("\t/* ") > hfile
16853383Sn_hibma		ocomment = 1;
16953383Sn_hibma	}
17053383Sn_hibma	while (f <= NF) {
17153383Sn_hibma		if ($f == "#") {
172139458Simp			if (hfile)
173139458Simp				printf("(") > hfile
17453383Sn_hibma			oparen = 1
17553383Sn_hibma			f++
17653383Sn_hibma			continue
17753383Sn_hibma		}
17853383Sn_hibma		if (oparen) {
179139458Simp			if (hfile)
180139458Simp				printf("%s", $f) > hfile
181139458Simp			if (f < NF && hfile)
18253383Sn_hibma				printf(" ") > hfile
18353383Sn_hibma			f++
18453383Sn_hibma			continue
18553383Sn_hibma		}
18653383Sn_hibma		products[nproducts, i] = $f
187139458Simp		if (hfile)
188139458Simp			printf("%s", products[nproducts, i]) > hfile
189139458Simp		if (f < NF && hfile)
19053383Sn_hibma			printf(" ") > hfile
19153383Sn_hibma		i++; f++;
19253383Sn_hibma	}
193139458Simp	if (oparen && hfile)
19453383Sn_hibma		printf(")") > hfile
195139458Simp	if (ocomment && hfile)
19653383Sn_hibma		printf(" */") > hfile
197139458Simp	if (hfile)
198139458Simp		printf("\n") > hfile
19953383Sn_hibma
200139458Simp	continue;
201139458Simp    }
20253383Sn_hibma	if ($0 == "")
20353383Sn_hibma		blanklines++
204139458Simp	if (hfile)
205139458Simp		print $0 > hfile
206139458Simp	if (blanklines < 2 && dfile)
207139458Simp	    print $0 > dfile
20853383Sn_hibma}
20953383Sn_hibma	# print out the match tables
21053383Sn_hibma
211139458Simp	if (dfile) {
212139458Simp		printf("\n") > dfile
21353383Sn_hibma
214139458Simp		printf("const struct usb_knowndev usb_knowndevs[] = {\n") > dfile
215139458Simp		for (i = 1; i <= nproducts; i++) {
216139458Simp			printf("\t{\n") > dfile
217139458Simp			printf("\t    USB_VENDOR_%s, USB_PRODUCT_%s_%s,\n",
218139458Simp			    products[i, 1], products[i, 1], products[i, 2]) \
219139458Simp			    > dfile
220139458Simp			printf("\t    ") > dfile
221139458Simp			printf("0") > dfile
222139458Simp			printf(",\n") > dfile
22353383Sn_hibma
224139458Simp			vendi = vendorindex[products[i, 1]];
225139458Simp			printf("\t    \"") > dfile
226139458Simp			j = 3;
227139458Simp			needspace = 0;
228139458Simp			while (vendors[vendi, j] != "") {
229139458Simp				if (needspace)
230139458Simp					printf(" ") > dfile
231139458Simp				printf("%s", vendors[vendi, j]) > dfile
232139458Simp				needspace = 1
233139458Simp				j++
234139458Simp			}
235139458Simp			printf("\",\n") > dfile
23653383Sn_hibma
237139458Simp			printf("\t    \"") > dfile
238139458Simp			j = 4;
239139458Simp			needspace = 0;
240139458Simp			while (products[i, j] != "") {
241139458Simp				if (needspace)
242139458Simp					printf(" ") > dfile
243139458Simp				printf("%s", products[i, j]) > dfile
244139458Simp				needspace = 1
245139458Simp				j++
246139458Simp			}
247139458Simp			printf("\",\n") > dfile
248139458Simp			printf("\t},\n") > dfile
24953383Sn_hibma		}
250139458Simp		for (i = 1; i <= nvendors; i++) {
251139458Simp			printf("\t{\n") > dfile
252139458Simp			printf("\t    USB_VENDOR_%s, 0,\n", vendors[i, 1]) \
253139458Simp			    > dfile
254139458Simp			printf("\t    USB_KNOWNDEV_NOPROD,\n") \
255139458Simp			    > dfile
256139458Simp			printf("\t    \"") > dfile
257139458Simp			j = 3;
258139458Simp			needspace = 0;
259139458Simp			while (vendors[i, j] != "") {
260139458Simp				if (needspace)
261139458Simp					printf(" ") > dfile
262139458Simp				printf("%s", vendors[i, j]) > dfile
263139458Simp				needspace = 1
264139458Simp				j++
265139458Simp			}
266139458Simp			printf("\",\n") > dfile
267139458Simp			printf("\t    NULL,\n") > dfile
268139458Simp			printf("\t},\n") > dfile
26953383Sn_hibma		}
270139458Simp		printf("\t{ 0, 0, 0, NULL, NULL, }\n") > dfile
271139458Simp		printf("};\n") > dfile
27253383Sn_hibma	}
27353383Sn_hibma}
274