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