des_crypt.3: Fix typo.
[dragonfly.git] / sys / tools / usbdevs2h.awk
blob34e105b8486a03a157332e5828480f950ba7a943
1 #! /usr/bin/awk -f
2 #-
3 # $NetBSD: usb/devlist2h.awk,v 1.9 2001/01/18 20:28:22 jdolecek Exp $
4 # $FreeBSD$
6 # Copyright (c) 1995, 1996 Christopher G. Demetriou
7 # All rights reserved.
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
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.
35 function usage()
37 print "usage: usbdevs2h.awk <srcfile> [-d|-h]";
38 exit 1;
41 function header(file)
43 printf("/*\n") > file
44 printf(" * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.\n") \
45 > file
46 printf(" */\n") > file
49 function vendor(hfile)
51 nvendors++
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;
61 # comments
62 ocomment = oparen = 0
63 if (f <= NF) {
64 if (hfile)
65 printf("\t/* ") > hfile
66 ocomment = 1;
68 while (f <= NF) {
69 if ($f == "#") {
70 if (hfile)
71 printf("(") > hfile
72 oparen = 1
73 f++
74 continue
76 if (oparen) {
77 if (hfile)
78 printf("%s", $f) > hfile
79 if (f < NF && hfile)
80 printf(" ") > hfile
81 f++
82 continue
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++;
91 if (oparen && hfile)
92 printf(")") > hfile
93 if (ocomment && hfile)
94 printf(" */") > hfile
95 if (hfile)
96 printf("\n") > hfile
99 function product(hfile)
101 nproducts++
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
111 i=4; f = 5;
113 # comments
114 ocomment = oparen = 0
115 if (f <= NF) {
116 if (hfile)
117 printf("\t/* ") > hfile
118 ocomment = 1;
120 while (f <= NF) {
121 if ($f == "#") {
122 if (hfile)
123 printf("(") > hfile
124 oparen = 1
126 continue
128 if (oparen) {
129 if (hfile)
130 printf("%s", $f) > hfile
131 if (f < NF && hfile)
132 printf(" ") > hfile
134 continue
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++;
143 if (oparen && hfile)
144 printf(")") > hfile
145 if (ocomment && hfile)
146 printf(" */") > hfile
147 if (hfile)
148 printf("\n") > hfile
151 function dump_dfile(dfile)
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
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
174 printf("\",\n") > dfile
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
186 printf("\",\n") > dfile
187 printf("\t},\n") > dfile
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
203 printf("\",\n") > dfile
204 printf("\t NULL,\n") > dfile
205 printf("\t},\n") > dfile
207 printf("\t{ 0, 0, 0, NULL, NULL, }\n") > dfile
208 printf("};\n") > dfile
211 BEGIN {
213 nproducts = nvendors = 0
214 # Process the command line
215 for (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;
226 ARGC = 1;
227 line=0;
229 while ((getline < srcfile) > 0) {
230 line++;
231 if (line == 1) {
232 if (dfile)
233 header(dfile)
234 if (hfile)
235 header(hfile)
236 continue;
238 if ($1 == "vendor") {
239 vendor(hfile)
240 continue
242 if ($1 == "product") {
243 product(hfile)
244 continue
246 if ($0 == "")
247 blanklines++
248 if (hfile)
249 print $0 > hfile
250 if (blanklines < 2 && dfile)
251 print $0 > dfile
254 # print out the match tables
256 if (dfile)
257 dump_dfile(dfile)