usbdevs2h.awk revision 139825
153383Sn_hibma#! /usr/bin/awk -f 2139825Simp#- 393569Sjoe# $NetBSD: usb/devlist2h.awk,v 1.9 2001/01/18 20:28:22 jdolecek Exp $ 453383Sn_hibma# $FreeBSD: head/sys/tools/usbdevs2h.awk 139825 2005-01-07 02:29:27Z imp $ 553383Sn_hibma# 653383Sn_hibma# Copyright (c) 1995, 1996 Christopher G. Demetriou 753383Sn_hibma# All rights reserved. 853383Sn_hibma# 953383Sn_hibma# Redistribution and use in source and binary forms, with or without 1053383Sn_hibma# modification, are permitted provided that the following conditions 1153383Sn_hibma# are met: 1253383Sn_hibma# 1. Redistributions of source code must retain the above copyright 1353383Sn_hibma# notice, this list of conditions and the following disclaimer. 1453383Sn_hibma# 2. Redistributions in binary form must reproduce the above copyright 1553383Sn_hibma# notice, this list of conditions and the following disclaimer in the 1653383Sn_hibma# documentation and/or other materials provided with the distribution. 1753383Sn_hibma# 3. All advertising materials mentioning features or use of this software 1853383Sn_hibma# must display the following acknowledgement: 1953383Sn_hibma# This product includes software developed by Christopher G. Demetriou. 2053383Sn_hibma# 4. The name of the author may not be used to endorse or promote products 2153383Sn_hibma# derived from this software without specific prior written permission 2253383Sn_hibma# 2353383Sn_hibma# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2453383Sn_hibma# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2553383Sn_hibma# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2653383Sn_hibma# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2753383Sn_hibma# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2853383Sn_hibma# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2953383Sn_hibma# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3053383Sn_hibma# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3153383Sn_hibma# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3253383Sn_hibma# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3353383Sn_hibma# 34139513Simp 35139458Simpfunction usage() 36139458Simp{ 37139458Simp print "usage: usbdevs2h.awk <srcfile> [-d|-h]"; 38139458Simp exit 1; 39139458Simp} 40139458Simp 41139518Simpfunction header(file) 42139513Simp{ 43139513Simp if (os == "NetBSD") 44139518Simp printf("/*\t\$NetBSD\$\t*/\n\n") > file 45139513Simp else if (os == "FreeBSD") 46139518Simp printf("/* \$FreeBSD\$ */\n\n") > file 47139513Simp else if (os == "OpenBSD") 48139518Simp printf("/*\t\$OpenBSD\$\t*/\n\n") > file 49139513Simp else 50139518Simp printf("/* ??? */\n\n") > file 51139518Simp printf("/*\n") > file 52139513Simp printf(" * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.\n") \ 53139518Simp > file 54139518Simp printf(" *\n") > file 55139518Simp printf(" * generated from:\n") > file 56139518Simp printf(" *\t%s\n", VERSION) > file 57139518Simp printf(" */\n") > file 58139513Simp} 59139458Simp 60139513Simpfunction vendor(hfile) 61139513Simp{ 6253383Sn_hibma nvendors++ 6353383Sn_hibma 6453383Sn_hibma vendorindex[$2] = nvendors; # record index for this name, for later. 6553383Sn_hibma vendors[nvendors, 1] = $2; # name 6653383Sn_hibma vendors[nvendors, 2] = $3; # id 67139458Simp if (hfile) 68139458Simp printf("#define\tUSB_VENDOR_%s\t%s\t", vendors[nvendors, 1], 69139458Simp vendors[nvendors, 2]) > hfile 7053383Sn_hibma i = 3; f = 4; 7153383Sn_hibma 7253383Sn_hibma # comments 7353383Sn_hibma ocomment = oparen = 0 7453383Sn_hibma if (f <= NF) { 75139458Simp if (hfile) 76139458Simp printf("\t/* ") > hfile 7753383Sn_hibma ocomment = 1; 7853383Sn_hibma } 7953383Sn_hibma while (f <= NF) { 8053383Sn_hibma if ($f == "#") { 81139458Simp if (hfile) 82139458Simp printf("(") > hfile 8353383Sn_hibma oparen = 1 8453383Sn_hibma f++ 8553383Sn_hibma continue 8653383Sn_hibma } 8753383Sn_hibma if (oparen) { 88139458Simp if (hfile) 89139458Simp printf("%s", $f) > hfile 90139458Simp if (f < NF && hfile) 9153383Sn_hibma printf(" ") > hfile 9253383Sn_hibma f++ 9353383Sn_hibma continue 9453383Sn_hibma } 9553383Sn_hibma vendors[nvendors, i] = $f 96139458Simp if (hfile) 97139458Simp printf("%s", vendors[nvendors, i]) > hfile 98139458Simp if (f < NF && hfile) 9953383Sn_hibma printf(" ") > hfile 10053383Sn_hibma i++; f++; 10153383Sn_hibma } 102139458Simp if (oparen && hfile) 10353383Sn_hibma printf(")") > hfile 104139458Simp if (ocomment && hfile) 10553383Sn_hibma printf(" */") > hfile 106139458Simp if (hfile) 107139458Simp printf("\n") > hfile 108139513Simp} 10953383Sn_hibma 110139513Simpfunction product(hfile) 111139513Simp{ 11253383Sn_hibma nproducts++ 11353383Sn_hibma 11453383Sn_hibma products[nproducts, 1] = $2; # vendor name 11553383Sn_hibma products[nproducts, 2] = $3; # product id 11653383Sn_hibma products[nproducts, 3] = $4; # id 117139458Simp if (hfile) 118139458Simp printf("#define\tUSB_PRODUCT_%s_%s\t%s\t", \ 119139458Simp products[nproducts, 1], products[nproducts, 2], \ 120139458Simp products[nproducts, 3]) > hfile 12153383Sn_hibma 12253383Sn_hibma i=4; f = 5; 12353383Sn_hibma 12453383Sn_hibma # comments 12553383Sn_hibma ocomment = oparen = 0 12653383Sn_hibma if (f <= NF) { 127139458Simp if (hfile) 128139458Simp printf("\t/* ") > hfile 12953383Sn_hibma ocomment = 1; 13053383Sn_hibma } 13153383Sn_hibma while (f <= NF) { 13253383Sn_hibma if ($f == "#") { 133139458Simp if (hfile) 134139458Simp printf("(") > hfile 13553383Sn_hibma oparen = 1 13653383Sn_hibma f++ 13753383Sn_hibma continue 13853383Sn_hibma } 13953383Sn_hibma if (oparen) { 140139458Simp if (hfile) 141139458Simp printf("%s", $f) > hfile 142139458Simp if (f < NF && hfile) 14353383Sn_hibma printf(" ") > hfile 14453383Sn_hibma f++ 14553383Sn_hibma continue 14653383Sn_hibma } 14753383Sn_hibma products[nproducts, i] = $f 148139458Simp if (hfile) 149139458Simp printf("%s", products[nproducts, i]) > hfile 150139458Simp if (f < NF && hfile) 15153383Sn_hibma printf(" ") > hfile 15253383Sn_hibma i++; f++; 15353383Sn_hibma } 154139458Simp if (oparen && hfile) 15553383Sn_hibma printf(")") > hfile 156139458Simp if (ocomment && hfile) 15753383Sn_hibma printf(" */") > hfile 158139458Simp if (hfile) 159139458Simp printf("\n") > hfile 160139513Simp} 16153383Sn_hibma 162139513Simpfunction dump_dfile(dfile) 163139513Simp{ 164139513Simp printf("\n") > dfile 165139513Simp printf("const struct usb_knowndev usb_knowndevs[] = {\n") > dfile 166139513Simp for (i = 1; i <= nproducts; i++) { 167139513Simp printf("\t{\n") > dfile 168139513Simp printf("\t USB_VENDOR_%s, USB_PRODUCT_%s_%s,\n", 169139513Simp products[i, 1], products[i, 1], products[i, 2]) > dfile 170139513Simp printf("\t ") > dfile 171139513Simp printf("0") > dfile 172139513Simp printf(",\n") > dfile 173139513Simp 174139513Simp vendi = vendorindex[products[i, 1]]; 175139513Simp printf("\t \"") > dfile 176139513Simp j = 3; 177139513Simp needspace = 0; 178139513Simp while (vendors[vendi, j] != "") { 179139513Simp if (needspace) 180139513Simp printf(" ") > dfile 181139513Simp printf("%s", vendors[vendi, j]) > dfile 182139513Simp needspace = 1 183139513Simp j++ 184139513Simp } 185139513Simp printf("\",\n") > dfile 186139513Simp 187139513Simp printf("\t \"") > dfile 188139513Simp j = 4; 189139513Simp needspace = 0; 190139513Simp while (products[i, j] != "") { 191139513Simp if (needspace) 192139513Simp printf(" ") > dfile 193139513Simp printf("%s", products[i, j]) > dfile 194139513Simp needspace = 1 195139513Simp j++ 196139513Simp } 197139513Simp printf("\",\n") > dfile 198139513Simp printf("\t},\n") > dfile 199139513Simp } 200139513Simp for (i = 1; i <= nvendors; i++) { 201139513Simp printf("\t{\n") > dfile 202139513Simp printf("\t USB_VENDOR_%s, 0,\n", vendors[i, 1]) > dfile 203139513Simp printf("\t USB_KNOWNDEV_NOPROD,\n") > dfile 204139513Simp printf("\t \"") > dfile 205139513Simp j = 3; 206139513Simp needspace = 0; 207139513Simp while (vendors[i, j] != "") { 208139513Simp if (needspace) 209139513Simp printf(" ") > dfile 210139513Simp printf("%s", vendors[i, j]) > dfile 211139513Simp needspace = 1 212139513Simp j++ 213139513Simp } 214139513Simp printf("\",\n") > dfile 215139513Simp printf("\t NULL,\n") > dfile 216139513Simp printf("\t},\n") > dfile 217139513Simp } 218139513Simp printf("\t{ 0, 0, 0, NULL, NULL, }\n") > dfile 219139513Simp printf("};\n") > dfile 220139513Simp} 221139513Simp 222139513SimpBEGIN { 223139513Simp 224139513Simpnproducts = nvendors = 0 225139513Simp# Process the command line 226139513Simpfor (i = 1; i < ARGC; i++) { 227139513Simp arg = ARGV[i]; 228139513Simp if (arg !~ /^-[dh]+$/ && arg !~ /devs$/) 229139513Simp usage(); 230139513Simp if (arg ~ /^-.*d/) 231139513Simp dfile="usbdevs_data.h" 232139513Simp if (arg ~ /^-.*h/) 233139513Simp hfile="usbdevs.h" 234139513Simp if (arg ~ /devs$/) 235139513Simp srcfile = arg; 236139513Simp} 237139513SimpARGC = 1; 238139513Simpline=0; 239139513Simp 240139513Simpwhile ((getline < srcfile) > 0) { 241139513Simp line++; 242139513Simp if (line == 1) { 243139513Simp VERSION = $0 244139513Simp gsub("\\$", "", VERSION) 245139513Simp if (dfile) 246139518Simp header(dfile) 247139513Simp if (hfile) 248139518Simp header(hfile) 249139513Simp continue; 250139513Simp } 251139513Simp if ($1 == "vendor") { 252139513Simp vendor(hfile) 253139513Simp continue 254139513Simp } 255139513Simp if ($1 == "product") { 256139513Simp product(hfile) 257139513Simp continue 258139513Simp } 25953383Sn_hibma if ($0 == "") 26053383Sn_hibma blanklines++ 261139458Simp if (hfile) 262139458Simp print $0 > hfile 263139458Simp if (blanklines < 2 && dfile) 264139458Simp print $0 > dfile 26553383Sn_hibma} 26653383Sn_hibma 267139513Simp# print out the match tables 26853383Sn_hibma 269139513Simpif (dfile) 270139513Simp dump_dfile(dfile) 27153383Sn_hibma} 272