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