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