xref: /freebsd/sys/tools/usbdevs2h.awk (revision 8fc257994d0ce2396196d7a06d50d20c8015f4b7)
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	if (os == "NetBSD")
44		printf("/*\t\$NetBSD\$\t*/\n\n") > file
45	else if (os == "FreeBSD")
46		printf("/* \$FreeBSD\$ */\n\n") > file
47	else if (os == "OpenBSD")
48		printf("/*\t\$OpenBSD\$\t*/\n\n") > file
49	else
50		printf("/* ??? */\n\n") > file
51	printf("/*\n") > file
52	printf(" * THIS FILE IS AUTOMATICALLY GENERATED.  DO NOT EDIT.\n") \
53	    > file
54	printf(" *\n") > file
55	printf(" * generated from:\n") > file
56	printf(" *\t%s\n", VERSION) > file
57	printf(" */\n") > file
58}
59
60function vendor(hfile)
61{
62	nvendors++
63
64	vendorindex[$2] = nvendors;		# record index for this name, for later.
65	vendors[nvendors, 1] = $2;		# name
66	vendors[nvendors, 2] = $3;		# id
67	if (hfile)
68		printf("#define\tUSB_VENDOR_%s\t%s\t", vendors[nvendors, 1],
69		    vendors[nvendors, 2]) > hfile
70	i = 3; f = 4;
71
72	# comments
73	ocomment = oparen = 0
74	if (f <= NF) {
75		if (hfile)
76			printf("\t/* ") > hfile
77		ocomment = 1;
78	}
79	while (f <= NF) {
80		if ($f == "#") {
81			if (hfile)
82				printf("(") > hfile
83			oparen = 1
84			f++
85			continue
86		}
87		if (oparen) {
88			if (hfile)
89				printf("%s", $f) > hfile
90			if (f < NF && hfile)
91				printf(" ") > hfile
92			f++
93			continue
94		}
95		vendors[nvendors, i] = $f
96		if (hfile)
97			printf("%s", vendors[nvendors, i]) > hfile
98		if (f < NF && hfile)
99			printf(" ") > hfile
100		i++; f++;
101	}
102	if (oparen && hfile)
103		printf(")") > hfile
104	if (ocomment && hfile)
105		printf(" */") > hfile
106	if (hfile)
107		printf("\n") > hfile
108}
109
110function product(hfile)
111{
112	nproducts++
113
114	products[nproducts, 1] = $2;		# vendor name
115	products[nproducts, 2] = $3;		# product id
116	products[nproducts, 3] = $4;		# id
117	if (hfile)
118		printf("#define\tUSB_PRODUCT_%s_%s\t%s\t", \
119		  products[nproducts, 1], products[nproducts, 2], \
120		  products[nproducts, 3]) > hfile
121
122	i=4; f = 5;
123
124	# comments
125	ocomment = oparen = 0
126	if (f <= NF) {
127		if (hfile)
128			printf("\t/* ") > hfile
129		ocomment = 1;
130	}
131	while (f <= NF) {
132		if ($f == "#") {
133			if (hfile)
134				printf("(") > hfile
135			oparen = 1
136			f++
137			continue
138		}
139		if (oparen) {
140			if (hfile)
141				printf("%s", $f) > hfile
142			if (f < NF && hfile)
143				printf(" ") > hfile
144			f++
145			continue
146		}
147		products[nproducts, i] = $f
148		if (hfile)
149			printf("%s", products[nproducts, i]) > hfile
150		if (f < NF && hfile)
151			printf(" ") > hfile
152		i++; f++;
153	}
154	if (oparen && hfile)
155		printf(")") > hfile
156	if (ocomment && hfile)
157		printf(" */") > hfile
158	if (hfile)
159		printf("\n") > hfile
160}
161
162function dump_dfile(dfile)
163{
164	printf("\n") > dfile
165	printf("const struct usb_knowndev usb_knowndevs[] = {\n") > dfile
166	for (i = 1; i <= nproducts; i++) {
167		printf("\t{\n") > dfile
168		printf("\t    USB_VENDOR_%s, USB_PRODUCT_%s_%s,\n",
169		    products[i, 1], products[i, 1], products[i, 2]) > dfile
170		printf("\t    ") > dfile
171		printf("0") > dfile
172		printf(",\n") > dfile
173
174		vendi = vendorindex[products[i, 1]];
175		printf("\t    \"") > dfile
176		j = 3;
177		needspace = 0;
178		while (vendors[vendi, j] != "") {
179			if (needspace)
180				printf(" ") > dfile
181			printf("%s", vendors[vendi, j]) > dfile
182			needspace = 1
183			j++
184		}
185		printf("\",\n") > dfile
186
187		printf("\t    \"") > dfile
188		j = 4;
189		needspace = 0;
190		while (products[i, j] != "") {
191			if (needspace)
192				printf(" ") > dfile
193			printf("%s", products[i, j]) > dfile
194			needspace = 1
195			j++
196		}
197		printf("\",\n") > dfile
198		printf("\t},\n") > dfile
199	}
200	for (i = 1; i <= nvendors; i++) {
201		printf("\t{\n") > dfile
202		printf("\t    USB_VENDOR_%s, 0,\n", vendors[i, 1]) > dfile
203		printf("\t    USB_KNOWNDEV_NOPROD,\n") > dfile
204		printf("\t    \"") > dfile
205		j = 3;
206		needspace = 0;
207		while (vendors[i, j] != "") {
208			if (needspace)
209				printf(" ") > dfile
210			printf("%s", vendors[i, j]) > dfile
211			needspace = 1
212			j++
213		}
214		printf("\",\n") > dfile
215		printf("\t    NULL,\n") > dfile
216		printf("\t},\n") > dfile
217	}
218	printf("\t{ 0, 0, 0, NULL, NULL, }\n") > dfile
219	printf("};\n") > dfile
220}
221
222BEGIN {
223
224nproducts = nvendors = 0
225# Process the command line
226for (i = 1; i < ARGC; i++) {
227	arg = ARGV[i];
228	if (arg !~ /^-[dh]+$/ && arg !~ /devs$/)
229		usage();
230	if (arg ~ /^-.*d/)
231		dfile="usbdevs_data.h"
232	if (arg ~ /^-.*h/)
233		hfile="usbdevs.h"
234	if (arg ~ /devs$/)
235		srcfile = arg;
236}
237ARGC = 1;
238line=0;
239
240while ((getline < srcfile) > 0) {
241	line++;
242	if (line == 1) {
243		VERSION = $0
244		gsub("\\$", "", VERSION)
245		if (dfile)
246			header(dfile)
247		if (hfile)
248			header(hfile)
249		continue;
250	}
251	if ($1 == "vendor") {
252		vendor(hfile)
253		continue
254	}
255	if ($1 == "product") {
256		product(hfile)
257		continue
258	}
259	if ($0 == "")
260		blanklines++
261	if (hfile)
262		print $0 > hfile
263	if (blanklines < 2 && dfile)
264	    print $0 > dfile
265}
266
267# print out the match tables
268
269if (dfile)
270	dump_dfile(dfile)
271}
272