1*de81e71eSTim Marsland#! /usr/bin/awk -f 2*de81e71eSTim Marsland 3*de81e71eSTim Marsland# Copyright 2009 Sun Microsystems, Inc. All rights reserved. 4*de81e71eSTim Marsland# Use is subject to license terms. 5*de81e71eSTim Marsland 6*de81e71eSTim Marsland# CDDL HEADER START 7*de81e71eSTim Marsland# 8*de81e71eSTim Marsland# The contents of this file are subject to the terms of the 9*de81e71eSTim Marsland# Common Development and Distribution License (the "License"). 10*de81e71eSTim Marsland# You may not use this file except in compliance with the License. 11*de81e71eSTim Marsland# 12*de81e71eSTim Marsland# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 13*de81e71eSTim Marsland# or http://www.opensolaris.org/os/licensing. 14*de81e71eSTim Marsland# See the License for the specific language governing permissions 15*de81e71eSTim Marsland# and limitations under the License. 16*de81e71eSTim Marsland# 17*de81e71eSTim Marsland# When distributing Covered Code, include this CDDL HEADER in each 18*de81e71eSTim Marsland# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 19*de81e71eSTim Marsland# If applicable, add the following below this CDDL HEADER, with the 20*de81e71eSTim Marsland# fields enclosed by brackets "[]" replaced with your own identifying 21*de81e71eSTim Marsland# information: Portions Copyright [yyyy] [name of copyright owner] 22*de81e71eSTim Marsland# 23*de81e71eSTim Marsland# CDDL HEADER END 24*de81e71eSTim Marsland 25*de81e71eSTim Marsland#- 26*de81e71eSTim Marsland# $NetBSD: usb/devlist2h.awk,v 1.9 2001/01/18 20:28:22 jdolecek Exp $ 27*de81e71eSTim Marsland# $FreeBSD: src/sys/tools/usbdevs2h.awk,v 1.7 2005/01/07 02:29:25 imp Exp $ 28*de81e71eSTim Marsland# 29*de81e71eSTim Marsland# Copyright (c) 1995, 1996 Christopher G. Demetriou 30*de81e71eSTim Marsland# All rights reserved. 31*de81e71eSTim Marsland# 32*de81e71eSTim Marsland# Redistribution and use in source and binary forms, with or without 33*de81e71eSTim Marsland# modification, are permitted provided that the following conditions 34*de81e71eSTim Marsland# are met: 35*de81e71eSTim Marsland# 1. Redistributions of source code must retain the above copyright 36*de81e71eSTim Marsland# notice, this list of conditions and the following disclaimer. 37*de81e71eSTim Marsland# 2. Redistributions in binary form must reproduce the above copyright 38*de81e71eSTim Marsland# notice, this list of conditions and the following disclaimer in the 39*de81e71eSTim Marsland# documentation and/or other materials provided with the distribution. 40*de81e71eSTim Marsland# 3. All advertising materials mentioning features or use of this software 41*de81e71eSTim Marsland# must display the following acknowledgement: 42*de81e71eSTim Marsland# This product includes software developed by Christopher G. Demetriou. 43*de81e71eSTim Marsland# 4. The name of the author may not be used to endorse or promote products 44*de81e71eSTim Marsland# derived from this software without specific prior written permission 45*de81e71eSTim Marsland# 46*de81e71eSTim Marsland# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 47*de81e71eSTim Marsland# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 48*de81e71eSTim Marsland# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 49*de81e71eSTim Marsland# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 50*de81e71eSTim Marsland# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 51*de81e71eSTim Marsland# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 52*de81e71eSTim Marsland# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 53*de81e71eSTim Marsland# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 54*de81e71eSTim Marsland# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 55*de81e71eSTim Marsland# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 56*de81e71eSTim Marsland# 57*de81e71eSTim Marsland 58*de81e71eSTim Marslandfunction usage() 59*de81e71eSTim Marsland{ 60*de81e71eSTim Marsland print "usage: usbdevs2h.awk <srcfile> [-d|-D|-h|-H]"; 61*de81e71eSTim Marsland exit 1; 62*de81e71eSTim Marsland} 63*de81e71eSTim Marsland 64*de81e71eSTim Marslandfunction header(file) 65*de81e71eSTim Marsland{ 66*de81e71eSTim Marsland if (os == "NetBSD") 67*de81e71eSTim Marsland printf("/*\t\$NetBSD\$\t*/\n\n") > file 68*de81e71eSTim Marsland else if (os == "FreeBSD") 69*de81e71eSTim Marsland printf("/* \$FreeBSD\$ */\n\n") > file 70*de81e71eSTim Marsland else if (os == "OpenBSD") 71*de81e71eSTim Marsland printf("/*\t\$OpenBSD\$\t*/\n\n") > file 72*de81e71eSTim Marsland 73*de81e71eSTim Marsland printf("/*\n") > file 74*de81e71eSTim Marsland printf(" * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.\n") \ 75*de81e71eSTim Marsland > file 76*de81e71eSTim Marsland printf(" *\n") > file 77*de81e71eSTim Marsland printf(" * generated from:\n") > file 78*de81e71eSTim Marsland printf(" * %s\n", VERSION) > file 79*de81e71eSTim Marsland printf(" */\n") > file 80*de81e71eSTim Marsland} 81*de81e71eSTim Marsland 82*de81e71eSTim Marslandfunction header_guard(file) 83*de81e71eSTim Marsland{ 84*de81e71eSTim Marsland printf("\n") > file 85*de81e71eSTim Marsland printf("#ifndef\t_SYS_USB_USBDEVS_H\n") > file 86*de81e71eSTim Marsland printf("#define\t_SYS_USB_USBDEVS_H\n\n") > file 87*de81e71eSTim Marsland} 88*de81e71eSTim Marsland 89*de81e71eSTim Marslandfunction header_trailer(file) 90*de81e71eSTim Marsland{ 91*de81e71eSTim Marsland printf("\n") > file 92*de81e71eSTim Marsland printf("#endif\t/* _SYS_USB_USBDEVS_H */\n") > file 93*de81e71eSTim Marsland} 94*de81e71eSTim Marsland 95*de81e71eSTim Marslandfunction vendor(hfile) 96*de81e71eSTim Marsland{ 97*de81e71eSTim Marsland nvendors++ 98*de81e71eSTim Marsland 99*de81e71eSTim Marsland vendorindex[$2] = nvendors; # record index for this name, for later. 100*de81e71eSTim Marsland vendors[nvendors, 1] = $2; # name 101*de81e71eSTim Marsland vendors[nvendors, 2] = $3; # id 102*de81e71eSTim Marsland if (hfile) 103*de81e71eSTim Marsland printf("#define\tUSB_VENDOR_%s\t%s\t", vendors[nvendors, 1], 104*de81e71eSTim Marsland vendors[nvendors, 2]) > hfile 105*de81e71eSTim Marsland 106*de81e71eSTim Marsland i = 3; f = 4; 107*de81e71eSTim Marsland 108*de81e71eSTim Marsland # comments 109*de81e71eSTim Marsland ocomment = oparen = 0 110*de81e71eSTim Marsland if (f <= NF) { 111*de81e71eSTim Marsland if (hfile) 112*de81e71eSTim Marsland printf("\t/* ") > hfile 113*de81e71eSTim Marsland ocomment = 1; 114*de81e71eSTim Marsland } 115*de81e71eSTim Marsland while (f <= NF) { 116*de81e71eSTim Marsland if ($f == "#") { 117*de81e71eSTim Marsland if (hfile) 118*de81e71eSTim Marsland printf("(") > hfile 119*de81e71eSTim Marsland oparen = 1 120*de81e71eSTim Marsland f++ 121*de81e71eSTim Marsland continue 122*de81e71eSTim Marsland } 123*de81e71eSTim Marsland if (oparen) { 124*de81e71eSTim Marsland if (hfile) 125*de81e71eSTim Marsland printf("%s", $f) > hfile 126*de81e71eSTim Marsland if (f < NF && hfile) 127*de81e71eSTim Marsland printf(" ") > hfile 128*de81e71eSTim Marsland f++ 129*de81e71eSTim Marsland continue 130*de81e71eSTim Marsland } 131*de81e71eSTim Marsland vendors[nvendors, i] = $f 132*de81e71eSTim Marsland if (hfile) 133*de81e71eSTim Marsland printf("%s", vendors[nvendors, i]) > hfile 134*de81e71eSTim Marsland if (f < NF && hfile) 135*de81e71eSTim Marsland printf(" ") > hfile 136*de81e71eSTim Marsland i++; f++; 137*de81e71eSTim Marsland } 138*de81e71eSTim Marsland if (oparen && hfile) 139*de81e71eSTim Marsland printf(")") > hfile 140*de81e71eSTim Marsland if (ocomment && hfile) 141*de81e71eSTim Marsland printf(" */") > hfile 142*de81e71eSTim Marsland if (hfile) 143*de81e71eSTim Marsland printf("\n") > hfile 144*de81e71eSTim Marsland} 145*de81e71eSTim Marsland 146*de81e71eSTim Marslandfunction product(hfile) 147*de81e71eSTim Marsland{ 148*de81e71eSTim Marsland nproducts++ 149*de81e71eSTim Marsland 150*de81e71eSTim Marsland products[nproducts, 1] = $2; # vendor name 151*de81e71eSTim Marsland products[nproducts, 2] = $3; # product id 152*de81e71eSTim Marsland products[nproducts, 3] = $4; # id 153*de81e71eSTim Marsland if (hfile) 154*de81e71eSTim Marsland printf("#define\tUSB_PRODUCT_%s_%s\t%s\t", \ 155*de81e71eSTim Marsland products[nproducts, 1], products[nproducts, 2], \ 156*de81e71eSTim Marsland products[nproducts, 3]) > hfile 157*de81e71eSTim Marsland 158*de81e71eSTim Marsland i=4; f = 5; 159*de81e71eSTim Marsland 160*de81e71eSTim Marsland # comments 161*de81e71eSTim Marsland ocomment = oparen = 0 162*de81e71eSTim Marsland if (f <= NF) { 163*de81e71eSTim Marsland if (hfile) 164*de81e71eSTim Marsland printf("\t/* ") > hfile 165*de81e71eSTim Marsland ocomment = 1; 166*de81e71eSTim Marsland } 167*de81e71eSTim Marsland while (f <= NF) { 168*de81e71eSTim Marsland if ($f == "#") { 169*de81e71eSTim Marsland if (hfile) 170*de81e71eSTim Marsland printf("(") > hfile 171*de81e71eSTim Marsland oparen = 1 172*de81e71eSTim Marsland f++ 173*de81e71eSTim Marsland continue 174*de81e71eSTim Marsland } 175*de81e71eSTim Marsland if (oparen) { 176*de81e71eSTim Marsland if (hfile) 177*de81e71eSTim Marsland printf("%s", $f) > hfile 178*de81e71eSTim Marsland if (f < NF && hfile) 179*de81e71eSTim Marsland printf(" ") > hfile 180*de81e71eSTim Marsland f++ 181*de81e71eSTim Marsland continue 182*de81e71eSTim Marsland } 183*de81e71eSTim Marsland products[nproducts, i] = $f 184*de81e71eSTim Marsland if (hfile) 185*de81e71eSTim Marsland printf("%s", products[nproducts, i]) > hfile 186*de81e71eSTim Marsland if (f < NF && hfile) 187*de81e71eSTim Marsland printf(" ") > hfile 188*de81e71eSTim Marsland i++; f++; 189*de81e71eSTim Marsland } 190*de81e71eSTim Marsland if (oparen && hfile) 191*de81e71eSTim Marsland printf(")") > hfile 192*de81e71eSTim Marsland if (ocomment && hfile) 193*de81e71eSTim Marsland printf(" */") > hfile 194*de81e71eSTim Marsland if (hfile) 195*de81e71eSTim Marsland printf("\n") > hfile 196*de81e71eSTim Marsland} 197*de81e71eSTim Marsland 198*de81e71eSTim Marslandfunction dump_dfile(dfile) 199*de81e71eSTim Marsland{ 200*de81e71eSTim Marsland printf("\n") > dfile 201*de81e71eSTim Marsland printf("const struct usb_knowndev usb_knowndevs[] = {\n") > dfile 202*de81e71eSTim Marsland for (i = 1; i <= nproducts; i++) { 203*de81e71eSTim Marsland printf("\t{\n") > dfile 204*de81e71eSTim Marsland printf("\t USB_VENDOR_%s, USB_PRODUCT_%s_%s,\n", 205*de81e71eSTim Marsland products[i, 1], products[i, 1], products[i, 2]) > dfile 206*de81e71eSTim Marsland printf("\t ") > dfile 207*de81e71eSTim Marsland printf("0") > dfile 208*de81e71eSTim Marsland printf(",\n") > dfile 209*de81e71eSTim Marsland 210*de81e71eSTim Marsland vendi = vendorindex[products[i, 1]]; 211*de81e71eSTim Marsland printf("\t \"") > dfile 212*de81e71eSTim Marsland j = 3; 213*de81e71eSTim Marsland needspace = 0; 214*de81e71eSTim Marsland while (vendors[vendi, j] != "") { 215*de81e71eSTim Marsland if (needspace) 216*de81e71eSTim Marsland printf(" ") > dfile 217*de81e71eSTim Marsland printf("%s", vendors[vendi, j]) > dfile 218*de81e71eSTim Marsland needspace = 1 219*de81e71eSTim Marsland j++ 220*de81e71eSTim Marsland } 221*de81e71eSTim Marsland printf("\",\n") > dfile 222*de81e71eSTim Marsland 223*de81e71eSTim Marsland printf("\t \"") > dfile 224*de81e71eSTim Marsland j = 4; 225*de81e71eSTim Marsland needspace = 0; 226*de81e71eSTim Marsland while (products[i, j] != "") { 227*de81e71eSTim Marsland if (needspace) 228*de81e71eSTim Marsland printf(" ") > dfile 229*de81e71eSTim Marsland printf("%s", products[i, j]) > dfile 230*de81e71eSTim Marsland needspace = 1 231*de81e71eSTim Marsland j++ 232*de81e71eSTim Marsland } 233*de81e71eSTim Marsland printf("\",\n") > dfile 234*de81e71eSTim Marsland printf("\t},\n") > dfile 235*de81e71eSTim Marsland } 236*de81e71eSTim Marsland for (i = 1; i <= nvendors; i++) { 237*de81e71eSTim Marsland printf("\t{\n") > dfile 238*de81e71eSTim Marsland printf("\t USB_VENDOR_%s, 0,\n", vendors[i, 1]) > dfile 239*de81e71eSTim Marsland printf("\t USB_KNOWNDEV_NOPROD,\n") > dfile 240*de81e71eSTim Marsland printf("\t \"") > dfile 241*de81e71eSTim Marsland j = 3; 242*de81e71eSTim Marsland needspace = 0; 243*de81e71eSTim Marsland while (vendors[i, j] != "") { 244*de81e71eSTim Marsland if (needspace) 245*de81e71eSTim Marsland printf(" ") > dfile 246*de81e71eSTim Marsland printf("%s", vendors[i, j]) > dfile 247*de81e71eSTim Marsland needspace = 1 248*de81e71eSTim Marsland j++ 249*de81e71eSTim Marsland } 250*de81e71eSTim Marsland printf("\",\n") > dfile 251*de81e71eSTim Marsland printf("\t NULL,\n") > dfile 252*de81e71eSTim Marsland printf("\t},\n") > dfile 253*de81e71eSTim Marsland } 254*de81e71eSTim Marsland printf("\t{ 0, 0, 0, NULL, NULL, }\n") > dfile 255*de81e71eSTim Marsland printf("};\n") > dfile 256*de81e71eSTim Marsland} 257*de81e71eSTim Marsland 258*de81e71eSTim MarslandBEGIN { 259*de81e71eSTim Marsland 260*de81e71eSTim Marslandnproducts = nvendors = 0 261*de81e71eSTim Marsland# Process the command line 262*de81e71eSTim Marslandfor (i = 1; i < ARGC; i++) { 263*de81e71eSTim Marsland arg = ARGV[i]; 264*de81e71eSTim Marsland if (arg !~ /^-[dDhH]+$/ && arg !~ /devs$/) 265*de81e71eSTim Marsland usage(); 266*de81e71eSTim Marsland if (arg ~ /^-.*d/) 267*de81e71eSTim Marsland dfile="usbdevs_data.h" 268*de81e71eSTim Marsland if (arg ~ /^-.*D/) 269*de81e71eSTim Marsland dfile="/dev/stdout" 270*de81e71eSTim Marsland if (arg ~ /^-.*h/) 271*de81e71eSTim Marsland hfile="usbdevs.h" 272*de81e71eSTim Marsland if (arg ~ /^-.*H/) 273*de81e71eSTim Marsland hfile="/dev/stdout" 274*de81e71eSTim Marsland if (arg ~ /devs$/) 275*de81e71eSTim Marsland srcfile = arg; 276*de81e71eSTim Marsland} 277*de81e71eSTim MarslandARGC = 1; 278*de81e71eSTim Marslandline=0; 279*de81e71eSTim Marsland 280*de81e71eSTim Marslandwhile ((getline < srcfile) > 0) { 281*de81e71eSTim Marsland line++; 282*de81e71eSTim Marsland if (line == 1) { 283*de81e71eSTim Marsland VERSION = $0 284*de81e71eSTim Marsland gsub("\\$", "", VERSION) 285*de81e71eSTim Marsland if (dfile) 286*de81e71eSTim Marsland header(dfile) 287*de81e71eSTim Marsland if (hfile) { 288*de81e71eSTim Marsland header(hfile) 289*de81e71eSTim Marsland header_guard(hfile) 290*de81e71eSTim Marsland } 291*de81e71eSTim Marsland continue; 292*de81e71eSTim Marsland } 293*de81e71eSTim Marsland if ($1 == "vendor") { 294*de81e71eSTim Marsland vendor(hfile) 295*de81e71eSTim Marsland continue 296*de81e71eSTim Marsland } 297*de81e71eSTim Marsland if ($1 == "product") { 298*de81e71eSTim Marsland product(hfile) 299*de81e71eSTim Marsland continue 300*de81e71eSTim Marsland } 301*de81e71eSTim Marsland if ($0 == "") 302*de81e71eSTim Marsland blanklines++ 303*de81e71eSTim Marsland if (hfile) 304*de81e71eSTim Marsland print $0 > hfile 305*de81e71eSTim Marsland if (blanklines < 2 && dfile) 306*de81e71eSTim Marsland print $0 > dfile 307*de81e71eSTim Marsland} 308*de81e71eSTim Marsland 309*de81e71eSTim Marsland# close off the header guard 310*de81e71eSTim Marsland 311*de81e71eSTim Marslandif (hfile) 312*de81e71eSTim Marsland header_trailer(hfile) 313*de81e71eSTim Marsland 314*de81e71eSTim Marsland# print out the match tables 315*de81e71eSTim Marsland 316*de81e71eSTim Marslandif (dfile) 317*de81e71eSTim Marsland dump_dfile(dfile) 318*de81e71eSTim Marsland} 319