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