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