usbdevs2h.awk revision 139518
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 139518 2004-12-31 21:12:17Z 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
40139518Simpfunction header(file)
41139513Simp{
42139513Simp	if (os == "NetBSD")
43139518Simp		printf("/*\t\$NetBSD\$\t*/\n\n") > file
44139513Simp	else if (os == "FreeBSD")
45139518Simp		printf("/* \$FreeBSD\$ */\n\n") > file
46139513Simp	else if (os == "OpenBSD")
47139518Simp		printf("/*\t\$OpenBSD\$\t*/\n\n") > file
48139513Simp	else
49139518Simp		printf("/* ??? */\n\n") > file
50139518Simp	printf("/*\n") > file
51139513Simp	printf(" * THIS FILE IS AUTOMATICALLY GENERATED.  DO NOT EDIT.\n") \
52139518Simp	    > file
53139518Simp	printf(" *\n") > file
54139518Simp	printf(" * generated from:\n") > file
55139518Simp	printf(" *\t%s\n", VERSION) > file
56139518Simp	printf(" */\n") > file
57139513Simp}
58139458Simp
59139513Simpfunction vendor(hfile)
60139513Simp{
6153383Sn_hibma	nvendors++
6253383Sn_hibma
6353383Sn_hibma	vendorindex[$2] = nvendors;		# record index for this name, for later.
6453383Sn_hibma	vendors[nvendors, 1] = $2;		# name
6553383Sn_hibma	vendors[nvendors, 2] = $3;		# id
66139458Simp	if (hfile)
67139458Simp		printf("#define\tUSB_VENDOR_%s\t%s\t", vendors[nvendors, 1],
68139458Simp		    vendors[nvendors, 2]) > hfile
6953383Sn_hibma	i = 3; f = 4;
7053383Sn_hibma
7153383Sn_hibma	# comments
7253383Sn_hibma	ocomment = oparen = 0
7353383Sn_hibma	if (f <= NF) {
74139458Simp		if (hfile)
75139458Simp			printf("\t/* ") > hfile
7653383Sn_hibma		ocomment = 1;
7753383Sn_hibma	}
7853383Sn_hibma	while (f <= NF) {
7953383Sn_hibma		if ($f == "#") {
80139458Simp			if (hfile)
81139458Simp				printf("(") > hfile
8253383Sn_hibma			oparen = 1
8353383Sn_hibma			f++
8453383Sn_hibma			continue
8553383Sn_hibma		}
8653383Sn_hibma		if (oparen) {
87139458Simp			if (hfile)
88139458Simp				printf("%s", $f) > hfile
89139458Simp			if (f < NF && hfile)
9053383Sn_hibma				printf(" ") > hfile
9153383Sn_hibma			f++
9253383Sn_hibma			continue
9353383Sn_hibma		}
9453383Sn_hibma		vendors[nvendors, i] = $f
95139458Simp		if (hfile)
96139458Simp			printf("%s", vendors[nvendors, i]) > hfile
97139458Simp		if (f < NF && hfile)
9853383Sn_hibma			printf(" ") > hfile
9953383Sn_hibma		i++; f++;
10053383Sn_hibma	}
101139458Simp	if (oparen && hfile)
10253383Sn_hibma		printf(")") > hfile
103139458Simp	if (ocomment && hfile)
10453383Sn_hibma		printf(" */") > hfile
105139458Simp	if (hfile)
106139458Simp		printf("\n") > hfile
107139513Simp}
10853383Sn_hibma
109139513Simpfunction product(hfile)
110139513Simp{
11153383Sn_hibma	nproducts++
11253383Sn_hibma
11353383Sn_hibma	products[nproducts, 1] = $2;		# vendor name
11453383Sn_hibma	products[nproducts, 2] = $3;		# product id
11553383Sn_hibma	products[nproducts, 3] = $4;		# id
116139458Simp	if (hfile)
117139458Simp		printf("#define\tUSB_PRODUCT_%s_%s\t%s\t", \
118139458Simp		  products[nproducts, 1], products[nproducts, 2], \
119139458Simp		  products[nproducts, 3]) > hfile
12053383Sn_hibma
12153383Sn_hibma	i=4; f = 5;
12253383Sn_hibma
12353383Sn_hibma	# comments
12453383Sn_hibma	ocomment = oparen = 0
12553383Sn_hibma	if (f <= NF) {
126139458Simp		if (hfile)
127139458Simp			printf("\t/* ") > hfile
12853383Sn_hibma		ocomment = 1;
12953383Sn_hibma	}
13053383Sn_hibma	while (f <= NF) {
13153383Sn_hibma		if ($f == "#") {
132139458Simp			if (hfile)
133139458Simp				printf("(") > hfile
13453383Sn_hibma			oparen = 1
13553383Sn_hibma			f++
13653383Sn_hibma			continue
13753383Sn_hibma		}
13853383Sn_hibma		if (oparen) {
139139458Simp			if (hfile)
140139458Simp				printf("%s", $f) > hfile
141139458Simp			if (f < NF && hfile)
14253383Sn_hibma				printf(" ") > hfile
14353383Sn_hibma			f++
14453383Sn_hibma			continue
14553383Sn_hibma		}
14653383Sn_hibma		products[nproducts, i] = $f
147139458Simp		if (hfile)
148139458Simp			printf("%s", products[nproducts, i]) > hfile
149139458Simp		if (f < NF && hfile)
15053383Sn_hibma			printf(" ") > hfile
15153383Sn_hibma		i++; f++;
15253383Sn_hibma	}
153139458Simp	if (oparen && hfile)
15453383Sn_hibma		printf(")") > hfile
155139458Simp	if (ocomment && hfile)
15653383Sn_hibma		printf(" */") > hfile
157139458Simp	if (hfile)
158139458Simp		printf("\n") > hfile
159139513Simp}
16053383Sn_hibma
161139513Simpfunction dump_dfile(dfile)
162139513Simp{
163139513Simp	printf("\n") > dfile
164139513Simp	printf("const struct usb_knowndev usb_knowndevs[] = {\n") > dfile
165139513Simp	for (i = 1; i <= nproducts; i++) {
166139513Simp		printf("\t{\n") > dfile
167139513Simp		printf("\t    USB_VENDOR_%s, USB_PRODUCT_%s_%s,\n",
168139513Simp		    products[i, 1], products[i, 1], products[i, 2]) > dfile
169139513Simp		printf("\t    ") > dfile
170139513Simp		printf("0") > dfile
171139513Simp		printf(",\n") > dfile
172139513Simp
173139513Simp		vendi = vendorindex[products[i, 1]];
174139513Simp		printf("\t    \"") > dfile
175139513Simp		j = 3;
176139513Simp		needspace = 0;
177139513Simp		while (vendors[vendi, j] != "") {
178139513Simp			if (needspace)
179139513Simp				printf(" ") > dfile
180139513Simp			printf("%s", vendors[vendi, j]) > dfile
181139513Simp			needspace = 1
182139513Simp			j++
183139513Simp		}
184139513Simp		printf("\",\n") > dfile
185139513Simp
186139513Simp		printf("\t    \"") > dfile
187139513Simp		j = 4;
188139513Simp		needspace = 0;
189139513Simp		while (products[i, j] != "") {
190139513Simp			if (needspace)
191139513Simp				printf(" ") > dfile
192139513Simp			printf("%s", products[i, j]) > dfile
193139513Simp			needspace = 1
194139513Simp			j++
195139513Simp		}
196139513Simp		printf("\",\n") > dfile
197139513Simp		printf("\t},\n") > dfile
198139513Simp	}
199139513Simp	for (i = 1; i <= nvendors; i++) {
200139513Simp		printf("\t{\n") > dfile
201139513Simp		printf("\t    USB_VENDOR_%s, 0,\n", vendors[i, 1]) > dfile
202139513Simp		printf("\t    USB_KNOWNDEV_NOPROD,\n") > dfile
203139513Simp		printf("\t    \"") > dfile
204139513Simp		j = 3;
205139513Simp		needspace = 0;
206139513Simp		while (vendors[i, j] != "") {
207139513Simp			if (needspace)
208139513Simp				printf(" ") > dfile
209139513Simp			printf("%s", vendors[i, j]) > dfile
210139513Simp			needspace = 1
211139513Simp			j++
212139513Simp		}
213139513Simp		printf("\",\n") > dfile
214139513Simp		printf("\t    NULL,\n") > dfile
215139513Simp		printf("\t},\n") > dfile
216139513Simp	}
217139513Simp	printf("\t{ 0, 0, 0, NULL, NULL, }\n") > dfile
218139513Simp	printf("};\n") > dfile
219139513Simp}
220139513Simp
221139513SimpBEGIN {
222139513Simp
223139513Simpnproducts = nvendors = 0
224139513Simp# Process the command line
225139513Simpfor (i = 1; i < ARGC; i++) {
226139513Simp	arg = ARGV[i];
227139513Simp	if (arg !~ /^-[dh]+$/ && arg !~ /devs$/)
228139513Simp		usage();
229139513Simp	if (arg ~ /^-.*d/)
230139513Simp		dfile="usbdevs_data.h"
231139513Simp	if (arg ~ /^-.*h/)
232139513Simp		hfile="usbdevs.h"
233139513Simp	if (arg ~ /devs$/)
234139513Simp		srcfile = arg;
235139513Simp}
236139513SimpARGC = 1;
237139513Simpline=0;
238139513Simp
239139513Simpwhile ((getline < srcfile) > 0) {
240139513Simp	line++;
241139513Simp	if (line == 1) {
242139513Simp		VERSION = $0
243139513Simp		gsub("\\$", "", VERSION)
244139513Simp		if (dfile)
245139518Simp			header(dfile)
246139513Simp		if (hfile)
247139518Simp			header(hfile)
248139513Simp		continue;
249139513Simp	}
250139513Simp	if ($1 == "vendor") {
251139513Simp		vendor(hfile)
252139513Simp		continue
253139513Simp	}
254139513Simp	if ($1 == "product") {
255139513Simp		product(hfile)
256139513Simp		continue
257139513Simp	}
25853383Sn_hibma	if ($0 == "")
25953383Sn_hibma		blanklines++
260139458Simp	if (hfile)
261139458Simp		print $0 > hfile
262139458Simp	if (blanklines < 2 && dfile)
263139458Simp	    print $0 > dfile
26453383Sn_hibma}
26553383Sn_hibma
266139513Simp# print out the match tables
26753383Sn_hibma
268139513Simpif (dfile)
269139513Simp	dump_dfile(dfile)
27053383Sn_hibma}
271