Comment out the correct #endif directive.
[mplayer/greg.git] / vidix / pci_db2c.awk
blobf822fba7f89e9f972ebf2f270c790bfddfadcf1b
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
5 # Usage: awk -f pci_db2c.awk pci.db
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).
11 # This file is part of MPlayer.
13 # MPlayer is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
18 # MPlayer is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with MPlayer; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 BEGIN {
29 if(ARGC != 3) {
30 # check for arguments:
31 print "Usage awk -f pci_db2c.awk pci.db (and make sure pci.db file exists first)";
32 exit(1);
34 in_file = ARGV[1];
35 with_pci_db = ARGV[2];
36 vendor_file = "pci_vendors.h";
37 ids_file = "pci_ids.h"
38 name_file = "pci_names.c"
39 name_h_file = "pci_names.h"
40 dev_ids_file = "pci_dev_ids.c"
41 line=0;
42 # print out head lines
43 print_head(vendor_file);
44 print_head(ids_file);
45 print_head(name_file);
46 print_head(name_h_file);
47 print_head(dev_ids_file);
48 print_includes(dev_ids_file);
49 print "#ifndef PCI_VENDORS_INCLUDED" >vendor_file
50 print "#define PCI_VENDORS_INCLUDED 1">vendor_file
51 print "" >vendor_file
52 print "#ifndef PCI_IDS_INCLUDED" >ids_file
53 print "#define PCI_IDS_INCLUDED 1">ids_file
54 print "" >ids_file
55 print "#include \"pci_vendors.h\"">ids_file
56 print "" >ids_file
58 print "#ifndef PCI_NAMES_INCLUDED" >name_h_file
59 print "#define PCI_NAMES_INCLUDED 1">name_h_file
60 print "" >name_h_file
61 print_name_struct(name_h_file);
62 print "#include <stddef.h>">name_file
63 print "#include \"pci_names.h\"">name_file
64 if (with_pci_db) {
65 print "#include \"pci_dev_ids.c\"">name_file
66 print "">name_file
67 print "static struct vendor_id_s vendor_ids[] = {">name_file
69 first_pass=1;
70 init_name_db();
71 while(getline <in_file)
73 # count up lines
74 line++;
75 n=split($0, field, "[\t]");
76 name_field = kill_double_quoting(field[3])
77 if(field[1] == "v" && length(field[3])>0 && field[4] == "0")
79 init_device_db()
80 svend_name = get_short_vendor_name(field[3])
81 printf("#define VENDOR_%s\t", svend_name) >vendor_file;
82 if(length(svend_name) < 9) printf("\t") >vendor_file;
83 printf("0x%s /*%s*/\n",field[2], name_field) >vendor_file;
84 if (with_pci_db) printf("{ 0x%s, \"%s\", dev_lst_%s },\n",field[2], name_field, field[2]) >name_file;
85 printf("/* Vendor: %s: %s */\n", field[2], name_field) > ids_file
86 if(first_pass == 1) { first_pass=0; }
87 else { print "{ 0xFFFF, NULL }\n};" >dev_ids_file; }
88 printf("static const struct device_id_s dev_lst_%s[]={\n", field[2])>dev_ids_file
90 if(field[1] == "d" && length(field[3])>0 && field[4] == "0")
92 sdev_name = get_short_device_name(field[3])
93 full_name = sprintf("#define DEVICE_%s_%s", svend_name, sdev_name);
94 printf("%s\t", full_name) >ids_file
95 if(length(full_name) < 9) printf("\t") >ids_file;
96 if(length(full_name) < 17) printf("\t") >ids_file;
97 if(length(full_name) < 25) printf("\t") >ids_file;
98 if(length(full_name) < 32) printf("\t") >ids_file;
99 if(length(full_name) < 40) printf("\t") >ids_file;
100 if(length(full_name) < 48) printf("\t") >ids_file;
101 printf("0x%s /*%s*/\n", substr(field[2], 5), name_field) >ids_file
102 printf("{ 0x%s, \"%s\" },\n", substr(field[2], 5), name_field) >dev_ids_file
104 if(field[1] == "s" && length(field[3])>0 && field[4] == "0")
106 subdev_name = get_short_subdevice_name(field[3])
107 full_name = sprintf("#define SUBDEVICE_%s_%s", svend_name, subdev_name)
108 printf("\t%s\t", full_name) >ids_file
109 if(length(full_name) < 9) printf("\t") >ids_file;
110 if(length(full_name) < 17) printf("\t") >ids_file;
111 if(length(full_name) < 25) printf("\t") >ids_file;
112 if(length(full_name) < 32) printf("\t") >ids_file;
113 if(length(full_name) < 40) printf("\t") >ids_file;
114 printf("0x%s /*%s*/\n", substr(field[2], 9), name_field) >ids_file
117 #print "Total lines parsed:", line;
118 print "">vendor_file
119 print "#endif/*PCI_VENDORS_INCLUDED*/">vendor_file
120 print "">ids_file
121 print "#endif/*PCI_IDS_INCLUDED*/">ids_file
122 print "">name_h_file
123 print "#endif/*PCI_NAMES_INCLUDED*/">name_h_file
124 if (with_pci_db) print "};">name_file
125 print "{ 0xFFFF, NULL }" >dev_ids_file;
126 print "};">dev_ids_file
127 print_func_bodies(name_file);
130 function print_includes(out_file)
132 print "#include <stdlib.h>" >out_file;
133 print "#include \"pci_names.h\"" >out_file;
134 return;
137 function print_head( out_file)
139 print "/*" >out_file;
140 printf(" * File: %s\n", out_file) >out_file;
141 printf(" * This file was generated automatically. Don't modify it.\n") >out_file;
142 print "*/" >out_file;
143 return;
146 function print_name_struct(out_file)
148 print "#ifdef __cplusplus" >out_file
149 print "extern \"C\" {" >out_file
150 print "#endif" >out_file
151 print "">out_file
152 print "struct device_id_s" >out_file
153 print "{" >out_file
154 print "\tunsigned short\tid;" >out_file
155 print "\tconst char *\tname;" >out_file
156 print "};" >out_file
157 print "">out_file
158 print "struct vendor_id_s" >out_file
159 print "{" >out_file
160 print "\tunsigned short\tid;" >out_file
161 print "\tconst char *\tname;" >out_file
162 print "\tconst struct device_id_s *\tdev_list;" >out_file
163 print "};" >out_file
164 print "extern const char *pci_vendor_name(unsigned short id);">out_file
165 print "extern const char *pci_device_name(unsigned short vendor_id, unsigned short device_id);">out_file
166 print "">out_file
167 print "#ifdef __cplusplus" >out_file
168 print "}" >out_file
169 print "#endif" >out_file
170 return
173 function print_func_bodies(out_file)
175 print "">out_file
176 print "const char *pci_vendor_name(unsigned short id)" >out_file
177 print "{" >out_file
178 if (with_pci_db) {
179 print " unsigned i;" >out_file
180 print " for(i=0;i<sizeof(vendor_ids)/sizeof(struct vendor_id_s);i++)">out_file
181 print " {" >out_file
182 print "\tif(vendor_ids[i].id == id) return vendor_ids[i].name;" >out_file
183 print " }" >out_file
185 print " return NULL;" >out_file
186 print "}">out_file
187 print "" >out_file
188 print "const char *pci_device_name(unsigned short vendor_id, unsigned short device_id)" >out_file
189 print "{" >out_file
190 if (with_pci_db) {
191 print " unsigned i, j;" >out_file
192 print " for(i=0;i<sizeof(vendor_ids)/sizeof(struct vendor_id_s);i++)">out_file
193 print " {" >out_file
194 print "\tif(vendor_ids[i].id == vendor_id)" >out_file
195 print "\t{" >out_file
196 print "\t j=0;" >out_file
197 print "\t while(vendor_ids[i].dev_list[j].id != 0xFFFF)" >out_file
198 print "\t {">out_file
199 print "\t\tif(vendor_ids[i].dev_list[j].id == device_id) return vendor_ids[i].dev_list[j].name;">out_file
200 print "\t\tj++;">out_file
201 print "\t };">out_file
202 print "\t break;" >out_file
203 print "\t}" >out_file
204 print " }" >out_file
206 print " return NULL;">out_file
207 print "}">out_file
208 return
211 function kill_double_quoting(fld)
213 n=split(fld,phrases, "[\"]");
214 new_fld = phrases[1]
215 for(i=2;i<=n;i++) new_fld = sprintf("%s\\\"%s", new_fld, phrases[i])
216 return new_fld
219 function init_name_db()
221 vendor_names[1]=""
224 function init_device_db()
226 # delete device_names
227 for( i in device_names ) delete device_names[i];
228 device_names[1]=""
229 # delete subdevice_names
230 for( i in subdevice_names ) delete subdevice_names[i];
231 subdevice_names[1] = ""
234 function get_short_vendor_name(from)
236 n=split(from, name, "[ ]");
237 new_name = toupper(name[1]);
238 if(length(new_name)<3) new_name = sprintf("%s_%s", new_name, toupper(name[2]));
239 n=split(new_name, name, "[^0-9A-Za-z]");
240 svendor = name[1];
241 for(i=2;i<=n;i++) svendor=sprintf("%s%s%s", svendor, length(name[i])?"_":"", name[i]);
242 new_name = svendor;
243 vend_suffix = 2;
244 # check for unique
245 while(new_name in vendor_names)
247 new_name = sprintf("%s%u", svendor, vend_suffix)
248 vend_suffix = vend_suffix + 1;
250 # Add new name in array of vendor's names
251 vendor_names[new_name] = new_name
252 return new_name;
255 function get_short_device_name(from_name)
257 n=split(from_name, name, "[ ]");
258 new_name = toupper(name[1]);
259 if(length(name[2])) new_name = sprintf("%s_%s", new_name, toupper(name[2]));
260 if(length(name[3])) new_name = sprintf("%s_%s", new_name, toupper(name[3]));
261 n=split(new_name, name, "[^0-9A-Za-z]");
262 sdevice = name[1];
263 for(i=2;i<=n;i++) sdevice=sprintf("%s%s%s", sdevice, length(name[i])?"_":"", name[i]);
264 new_name = sdevice;
265 dev_suffix = 2;
266 # check for unique
267 while(new_name in device_names)
269 new_name = sprintf("%s%u", sdevice, dev_suffix)
270 dev_suffix = dev_suffix + 1;
272 # Add new name in array of device names
273 device_names[new_name] = new_name
274 return new_name;
277 function get_short_subdevice_name(from_name)
279 n=split(from_name, name, "[ ]");
280 new_name = toupper(name[1]);
281 if(length(name[2])) new_name = sprintf("%s_%s", new_name, toupper(name[2]));
282 if(length(name[3])) new_name = sprintf("%s_%s", new_name, toupper(name[3]));
283 n=split(new_name, name, "[^0-9A-Za-z]");
284 ssdevice = name[1];
285 for(i=2;i<=n;i++) ssdevice=sprintf("%s%s%s", ssdevice, length(name[i])?"_":"", name[i]);
286 new_name = ssdevice;
287 sdev_suffix = 2;
288 # check for unique
289 while(new_name in subdevice_names)
291 new_name = sprintf("%s%u", ssdevice, sdev_suffix)
292 sdev_suffix = sdev_suffix + 1;
294 # Add new name in array of subdevice names
295 subdevice_names[new_name] = new_name
296 return new_name;