| 1 |
# This file converts given pci.db to "C" source and header files |
| 2 |
# For latest version of pci ids see: http://pciids.sf.net |
| 3 |
# Copyright 2002 Nick Kurshev |
| 4 |
# |
| 5 |
# Usage: awk -f pci_db2c.awk pci.db |
| 6 |
# |
| 7 |
# Tested with Gawk v 3.0.x and Mawk 1.3.3 |
| 8 |
# But it should work with standard Awk implementations (hopefully). |
| 9 |
# (Nobody tested it with Nawk, but it should work, too). |
| 10 |
# |
| 11 |
|
| 12 |
BEGIN { |
| 13 |
|
| 14 |
if(ARGC != 2) { |
| 15 |
# check for arguments: |
| 16 |
print "Usage awk -f pci_db2c.awk pci.db (and make sure pci.db file exists first)"; |
| 17 |
exit(1); |
| 18 |
} |
| 19 |
in_file = ARGV[1]; |
| 20 |
vendor_file = "pci_vendors.h"; |
| 21 |
ids_file = "pci_ids.h" |
| 22 |
name_file = "pci_names.c" |
| 23 |
name_h_file = "pci_names.h" |
| 24 |
dev_ids_file = "pci_dev_ids.c" |
| 25 |
line=0; |
| 26 |
# print out head lines |
| 27 |
print_head(vendor_file); |
| 28 |
print_head(ids_file); |
| 29 |
print_head(name_file); |
| 30 |
print_head(name_h_file); |
| 31 |
print_head(dev_ids_file); |
| 32 |
print "#ifndef PCI_VENDORS_INCLUDED" >vendor_file |
| 33 |
print "#define PCI_VENDORS_INCLUDED 1">vendor_file |
| 34 |
print "" >vendor_file |
| 35 |
print "#ifndef PCI_IDS_INCLUDED" >ids_file |
| 36 |
print "#define PCI_IDS_INCLUDED 1">ids_file |
| 37 |
print "" >ids_file |
| 38 |
print "#include \"pci_vendors.h\"">ids_file |
| 39 |
print "" >ids_file |
| 40 |
|
| 41 |
print "#ifndef PCI_NAMES_INCLUDED" >name_h_file |
| 42 |
print "#define PCI_NAMES_INCLUDED 1">name_h_file |
| 43 |
print "" >name_h_file |
| 44 |
print_name_struct(name_h_file); |
| 45 |
print "#include <stddef.h>">name_file |
| 46 |
print "#include \"pci_names.h\"">name_file |
| 47 |
print "#include \"pci_dev_ids.c\"">name_file |
| 48 |
print "">name_file |
| 49 |
print "static struct vendor_id_s vendor_ids[] = {">name_file |
| 50 |
first_pass=1; |
| 51 |
init_name_db(); |
| 52 |
while(getline <in_file) |
| 53 |
{ |
| 54 |
# count up lines |
| 55 |
line++; |
| 56 |
n=split($0, field, "[\t]"); |
| 57 |
name_field = kill_double_quoting(field[3]) |
| 58 |
if(field[1] == "v" && length(field[3])>0 && field[4] == "0") |
| 59 |
{ |
| 60 |
init_device_db() |
| 61 |
svend_name = get_short_vendor_name(field[3]) |
| 62 |
printf("#define VENDOR_%s\t", svend_name) >vendor_file; |
| 63 |
if(length(svend_name) < 9) printf("\t") >vendor_file; |
| 64 |
printf("0x%s /*%s*/\n",field[2], name_field) >vendor_file; |
| 65 |
printf("{ 0x%s, \"%s\", dev_lst_%s },\n",field[2], name_field, field[2]) >name_file; |
| 66 |
printf("/* Vendor: %s: %s */\n", field[2], name_field) > ids_file |
| 67 |
if(first_pass == 1) { first_pass=0; } |
| 68 |
else { print "{ 0xFFFF, NULL }\n};" >dev_ids_file; } |
| 69 |
printf("static const struct device_id_s dev_lst_%s[]={\n", field[2])>dev_ids_file |
| 70 |
} |
| 71 |
if(field[1] == "d" && length(field[3])>0 && field[4] == "0") |
| 72 |
{ |
| 73 |
sdev_name = get_short_device_name(field[3]) |
| 74 |
full_name = sprintf("#define DEVICE_%s_%s", svend_name, sdev_name); |
| 75 |
printf("%s\t", full_name) >ids_file |
| 76 |
if(length(full_name) < 9) printf("\t") >ids_file; |
| 77 |
if(length(full_name) < 17) printf("\t") >ids_file; |
| 78 |
if(length(full_name) < 25) printf("\t") >ids_file; |
| 79 |
if(length(full_name) < 32) printf("\t") >ids_file; |
| 80 |
if(length(full_name) < 40) printf("\t") >ids_file; |
| 81 |
if(length(full_name) < 48) printf("\t") >ids_file; |
| 82 |
printf("0x%s /*%s*/\n", substr(field[2], 5), name_field) >ids_file |
| 83 |
printf("{ 0x%s, \"%s\" },\n", substr(field[2], 5), name_field) >dev_ids_file |
| 84 |
} |
| 85 |
if(field[1] == "s" && length(field[3])>0 && field[4] == "0") |
| 86 |
{ |
| 87 |
subdev_name = get_short_subdevice_name(field[3]) |
| 88 |
full_name = sprintf("#define SUBDEVICE_%s_%s", svend_name, subdev_name) |
| 89 |
printf("\t%s\t", full_name) >ids_file |
| 90 |
if(length(full_name) < 9) printf("\t") >ids_file; |
| 91 |
if(length(full_name) < 17) printf("\t") >ids_file; |
| 92 |
if(length(full_name) < 25) printf("\t") >ids_file; |
| 93 |
if(length(full_name) < 32) printf("\t") >ids_file; |
| 94 |
if(length(full_name) < 40) printf("\t") >ids_file; |
| 95 |
printf("0x%s /*%s*/\n", substr(field[2], 9), name_field) >ids_file |
| 96 |
} |
| 97 |
} |
| 98 |
print "Total lines parsed:", line; |
| 99 |
print "">vendor_file |
| 100 |
print "#endif/*PCI_VENDORS_INCLUDED*/">vendor_file |
| 101 |
print "">ids_file |
| 102 |
print "#endif/*PCI_IDS_INCLUDED*/">ids_file |
| 103 |
print "">name_h_file |
| 104 |
print "#endif/*PCI_NAMES_INCLUDED*/">name_h_file |
| 105 |
print "};">name_file |
| 106 |
print "{ 0xFFFF, NULL }" >dev_ids_file; |
| 107 |
print "};">dev_ids_file |
| 108 |
print_func_bodies(name_file); |
| 109 |
} |
| 110 |
|
| 111 |
function print_head( out_file) |
| 112 |
{ |
| 113 |
print "/*" >out_file; |
| 114 |
printf(" * File: %s\n", out_file) >out_file; |
| 115 |
printf(" * This file was generated automatically. Don't modify it.\n") >out_file; |
| 116 |
print "*/" >out_file; |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
function print_name_struct(out_file) |
| 121 |
{ |
| 122 |
print "#ifdef __cplusplus" >out_file |
| 123 |
print "extern \"C\" {" >out_file |
| 124 |
print "#endif" >out_file |
| 125 |
print "">out_file |
| 126 |
print "struct device_id_s" >out_file |
| 127 |
print "{" >out_file |
| 128 |
print "\tunsigned short\tid;" >out_file |
| 129 |
print "\tconst char *\tname;" >out_file |
| 130 |
print "};" >out_file |
| 131 |
print "">out_file |
| 132 |
print "struct vendor_id_s" >out_file |
| 133 |
print "{" >out_file |
| 134 |
print "\tunsigned short\tid;" >out_file |
| 135 |
print "\tconst char *\tname;" >out_file |
| 136 |
print "\tconst struct device_id_s *\tdev_list;" >out_file |
| 137 |
print "};" >out_file |
| 138 |
print "extern const char *pci_vendor_name(unsigned short id);">out_file |
| 139 |
print "extern const char *pci_device_name(unsigned short vendor_id, unsigned short device_id);">out_file |
| 140 |
print "">out_file |
| 141 |
print "#ifdef __cplusplus" >out_file |
| 142 |
print "}" >out_file |
| 143 |
print "#endif" >out_file |
| 144 |
return |
| 145 |
} |
| 146 |
|
| 147 |
function print_func_bodies(out_file) |
| 148 |
{ |
| 149 |
print "">out_file |
| 150 |
print "const char *pci_vendor_name(unsigned short id)" >out_file |
| 151 |
print "{" >out_file |
| 152 |
print " unsigned i;" >out_file |
| 153 |
print " for(i=0;i<sizeof(vendor_ids)/sizeof(struct vendor_id_s);i++)">out_file |
| 154 |
print " {" >out_file |
| 155 |
print "\tif(vendor_ids[i].id == id) return vendor_ids[i].name;" >out_file |
| 156 |
print " }" >out_file |
| 157 |
print " return NULL;" >out_file |
| 158 |
print "}">out_file |
| 159 |
print "" >out_file |
| 160 |
print "const char *pci_device_name(unsigned short vendor_id, unsigned short device_id)" >out_file |
| 161 |
print "{" >out_file |
| 162 |
print " unsigned i, j;" >out_file |
| 163 |
print " for(i=0;i<sizeof(vendor_ids)/sizeof(struct vendor_id_s);i++)">out_file |
| 164 |
print " {" >out_file |
| 165 |
print "\tif(vendor_ids[i].id == vendor_id)" >out_file |
| 166 |
print "\t{" >out_file |
| 167 |
print "\t j=0;" >out_file |
| 168 |
print "\t while(vendor_ids[i].dev_list[j].id != 0xFFFF)" >out_file |
| 169 |
print "\t {">out_file |
| 170 |
print "\t\tif(vendor_ids[i].dev_list[j].id == device_id) return vendor_ids[i].dev_list[j].name;">out_file |
| 171 |
print "\t\tj++;">out_file |
| 172 |
print "\t };">out_file |
| 173 |
print "\t break;" >out_file |
| 174 |
print "\t}" >out_file |
| 175 |
print " }" >out_file |
| 176 |
print " return NULL;">out_file |
| 177 |
print "}">out_file |
| 178 |
return |
| 179 |
} |
| 180 |
|
| 181 |
function kill_double_quoting(fld) |
| 182 |
{ |
| 183 |
n=split(fld,phrases, "[\"]"); |
| 184 |
new_fld = phrases[1] |
| 185 |
for(i=2;i<=n;i++) new_fld = sprintf("%s\\\"%s", new_fld, phrases[i]) |
| 186 |
return new_fld |
| 187 |
} |
| 188 |
|
| 189 |
function init_name_db() |
| 190 |
{ |
| 191 |
vendor_names[1]="" |
| 192 |
} |
| 193 |
|
| 194 |
function init_device_db() |
| 195 |
{ |
| 196 |
# delete device_names |
| 197 |
for( i in device_names ) delete device_names[i]; |
| 198 |
device_names[1]="" |
| 199 |
# delete subdevice_names |
| 200 |
for( i in subdevice_names ) delete subdevice_names[i]; |
| 201 |
subdevice_names[1] = "" |
| 202 |
} |
| 203 |
|
| 204 |
function get_short_vendor_name(from) |
| 205 |
{ |
| 206 |
n=split(from, name, "[ ]"); |
| 207 |
new_name = toupper(name[1]); |
| 208 |
if(length(new_name)<3) new_name = sprintf("%s_%s", new_name, toupper(name[2])); |
| 209 |
n=split(new_name, name, "[^0-9A-Za-z]"); |
| 210 |
svendor = name[1]; |
| 211 |
for(i=2;i<=n;i++) svendor=sprintf("%s%s%s", svendor, length(name[i])?"_":"", name[i]); |
| 212 |
new_name = svendor; |
| 213 |
vend_suffix = 2; |
| 214 |
# check for unique |
| 215 |
while(new_name in vendor_names) |
| 216 |
{ |
| 217 |
new_name = sprintf("%s%u", svendor, vend_suffix) |
| 218 |
vend_suffix = vend_suffix + 1; |
| 219 |
} |
| 220 |
# Add new name in array of vendor's names |
| 221 |
vendor_names[new_name] = new_name |
| 222 |
return new_name; |
| 223 |
} |
| 224 |
|
| 225 |
function get_short_device_name(from_name) |
| 226 |
{ |
| 227 |
n=split(from_name, name, "[ ]"); |
| 228 |
new_name = toupper(name[1]); |
| 229 |
if(length(name[2])) new_name = sprintf("%s_%s", new_name, toupper(name[2])); |
| 230 |
if(length(name[3])) new_name = sprintf("%s_%s", new_name, toupper(name[3])); |
| 231 |
n=split(new_name, name, "[^0-9A-Za-z]"); |
| 232 |
sdevice = name[1]; |
| 233 |
for(i=2;i<=n;i++) sdevice=sprintf("%s%s%s", sdevice, length(name[i])?"_":"", name[i]); |
| 234 |
new_name = sdevice; |
| 235 |
dev_suffix = 2; |
| 236 |
# check for unique |
| 237 |
while(new_name in device_names) |
| 238 |
{ |
| 239 |
new_name = sprintf("%s%u", sdevice, dev_suffix) |
| 240 |
dev_suffix = dev_suffix + 1; |
| 241 |
} |
| 242 |
# Add new name in array of device names |
| 243 |
device_names[new_name] = new_name |
| 244 |
return new_name; |
| 245 |
} |
| 246 |
|
| 247 |
function get_short_subdevice_name(from_name) |
| 248 |
{ |
| 249 |
n=split(from_name, name, "[ ]"); |
| 250 |
new_name = toupper(name[1]); |
| 251 |
if(length(name[2])) new_name = sprintf("%s_%s", new_name, toupper(name[2])); |
| 252 |
if(length(name[3])) new_name = sprintf("%s_%s", new_name, toupper(name[3])); |
| 253 |
n=split(new_name, name, "[^0-9A-Za-z]"); |
| 254 |
ssdevice = name[1]; |
| 255 |
for(i=2;i<=n;i++) ssdevice=sprintf("%s%s%s", ssdevice, length(name[i])?"_":"", name[i]); |
| 256 |
new_name = ssdevice; |
| 257 |
sdev_suffix = 2; |
| 258 |
# check for unique |
| 259 |
while(new_name in subdevice_names) |
| 260 |
{ |
| 261 |
new_name = sprintf("%s%u", ssdevice, sdev_suffix) |
| 262 |
sdev_suffix = sdev_suffix + 1; |
| 263 |
} |
| 264 |
# Add new name in array of subdevice names |
| 265 |
subdevice_names[new_name] = new_name |
| 266 |
return new_name; |
| 267 |
} |