1# USB Header script
2#
3# Copyright 2006-2012, Haiku.
4# Distributed under the terms of the MIT License.
5#
6# Authors:
7#		John Drinkwater, john@nextraweb.com
8#
9# Use with http://www.linux-usb.org/usb.ids
10# run as: awk -v HEADERFILE=usbhdr.h -f usb-header.awk usb.ids
11
12BEGIN {
13
14	# field separator, defining because user could have overridden
15	FS = " "
16
17	# Pass this in from outside with -v HEADERFILE=filenametouse
18	# we require usbhdr.h for our system
19	ofile = HEADERFILE
20
21	# possibly use this in the future
22	cleanvalues = "[^A-Za-z0-9{}\"'&@?!*.,:;+<> \\t\\/_\\[\\]=#()-]"
23	# ToDo: currently IDs aren't checked, we dumbly assume the source is clean
24
25	# descriptive output header
26	print "#if 0" > ofile
27	print "#\tUSBHDR.H: USB Vendors, Devices\n#" > ofile
28	print "#\tGenerated by usb-header.awk, source data from the following URI:\n#\thttp://www.linux-usb.org/usb.ids\n#" > ofile
29	print "#\tHeader created on " strftime( "%A, %d %b %Y %H:%M:%S %Z", systime() ) > ofile
30	print "#endif" > ofile
31
32	# and we start with vendors..
33	print "\ntypedef struct _USB_VENTABLE\n{\n\tunsigned short\tVenId ;\n\tconst char *\tVenName ;\n}  USB_VENTABLE, *PUSB_VENTABLE ;\n" > ofile
34	print "USB_VENTABLE\tUsbVenTable [] =\n{" > ofile
35}
36
37# matches vendor - starts with an id as first thing on the line
38# because this occurs first in the header file, we output it without worry
39/^[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]] / { 
40
41	if ( vendorcount++ > 0 ) { 
42		formatting = ",\n"
43	} else {
44		formatting = ""
45	}
46
47	# store vendor ID for possible devices afterwards
48	vendorid = $1
49	vendor = substr($0, 7)
50	gsub( /\"/, "&&", vendor )
51
52	printf formatting "\t{ 0x" vendorid ", \"" vendor "\" }" > ofile
53}
54
55# matches device 
56/^\t[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]] / { 
57
58	device = substr($0, 8)
59	gsub( /\\/, "&&", device )
60	gsub( /\"/, "&&", device )
61
62	# store device ID for possible devices afterwards
63	deviceid = $1
64	devicecount++
65	devices[devicecount, 1] = vendorid
66	devices[devicecount, 2] = $1
67	devices[devicecount, 3] = device 
68}
69
70# We've processed the file, now output.
71END {
72
73	print "\n};\n\n// Use this value for loop control during searching:\n#define\tUSB_VENTABLE_LEN\t(sizeof(UsbVenTable)/sizeof(USB_VENTABLE))\n" > ofile
74
75	if ( devicecount > 0 ) {
76
77		print "typedef struct _USB_DEVTABLE\n{\n\tunsigned short	VenId ;\n\tunsigned short	DevId ;\n\tconst char *\tChipDesc ;\n}  USB_DEVTABLE, *PUSB_DEVTABLE ;\n"  > ofile
78		print "USB_DEVTABLE\tUsbDevTable [] =\n{" > ofile
79		for (i = 1; i <= devicecount; i++) {
80
81			if (i != 1) {
82				formatting = ",\n"
83			} else {
84				formatting = ""
85			}
86			printf formatting "\t{ 0x" devices[i, 1] ", 0x" devices[i, 2] ", \"" devices[i, 3] "\" }" > ofile
87		}
88		print "\n} ;\n\n// Use this value for loop control during searching:\n#define	USB_DEVTABLE_LEN	(sizeof(UsbDevTable)/sizeof(USB_DEVTABLE))\n" > ofile
89
90	}
91	
92	close(ofile)
93}
94
95
96