vo_gl: Add initial stereo support
[mplayer/glamo.git] / vidix / pci_db2c.awk
blob8a1cb69cbb74363fae41f559b4f38c1852315ad8
1 #!/usr/bin/awk -f
2 # This file converts given pci.db to "C" source and header files
3 # For latest version of pci ids see: http://pciids.sf.net
4 # Copyright 2002 Nick Kurshev
6 # Tested with Gawk v 3.0.x and Mawk 1.3.3
7 # But it should work with standard Awk implementations (hopefully).
8 # (Nobody tested it with Nawk, but it should work, too).
10 # This file is part of MPlayer.
12 # MPlayer is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
17 # MPlayer is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License along
23 # with MPlayer; if not, write to the Free Software Foundation, Inc.,
24 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 BEGIN {
28 if (ARGC != 3) {
29 # check for arguments:
30 print "Usage ./pci_db2c.awk pci.db (and make sure pci.db file exists first)";
31 exit(1);
33 in_file = ARGV[1];
34 with_pci_db = ARGV[2];
35 dev_ids_c_file = "vidix/pci_dev_ids.c"
36 ids_h_file = "vidix/pci_ids.h"
37 names_c_file = "vidix/pci_names.c"
38 vendors_h_file = "vidix/pci_vendors.h";
39 # print out head lines
40 print_head(vendors_h_file);
41 print_head(ids_h_file);
42 print_head(names_c_file);
43 print_head(dev_ids_c_file);
44 print "#include <stdlib.h>" > dev_ids_c_file;
45 print "#include \"pci_names.h\"" > dev_ids_c_file;
47 print_guards_start(vendors_h_file);
48 print_guards_start(ids_h_file);
49 print "#include \"pci_vendors.h\"" > ids_h_file
50 print "" > ids_h_file
52 print "#include <stddef.h>" > names_c_file
53 print "#include \"pci_names.h\"" > names_c_file
54 if (with_pci_db) {
55 print "#include \"pci_dev_ids.c\"" > names_c_file
56 print "" > names_c_file
57 print "static struct vendor_id_s vendor_ids[] = {" > names_c_file
59 first_pass = 1;
60 init_name_db();
61 while (getline < in_file) {
62 n = split($0, field, "[\t]");
63 name_field = kill_double_quoting(field[3])
64 if (field[1] == "v" && length(field[3]) > 0 && field[4] == "0") {
65 init_device_db()
66 svend_name = get_short_vendor_name(field[3])
67 printf("#define VENDOR_%s\t", svend_name) > vendors_h_file;
68 if (length(svend_name) < 9)
69 printf("\t") > vendors_h_file;
70 printf("0x%s /*%s*/\n", field[2], name_field) > vendors_h_file;
71 if (with_pci_db)
72 printf("{ 0x%s, \"%s\", dev_lst_%s },\n", field[2], name_field, field[2]) > names_c_file;
73 printf("/* Vendor: %s: %s */\n", field[2], name_field) > ids_h_file
74 if (first_pass == 1)
75 first_pass = 0;
76 else
77 print "{ 0xFFFF, NULL }\n};" > dev_ids_c_file;
78 printf("static const struct device_id_s dev_lst_%s[] = {\n", field[2]) > dev_ids_c_file
80 if (field[1] == "d" && length(field[3]) > 0 && field[4] == "0") {
81 sdev_name = get_short_device_name(field[3])
82 full_name = sprintf("#define DEVICE_%s_%s", svend_name, sdev_name);
83 printf("%s\t", full_name) > ids_h_file
84 if (length(full_name) < 9) printf("\t") > ids_h_file;
85 if (length(full_name) < 17) printf("\t") > ids_h_file;
86 if (length(full_name) < 25) printf("\t") > ids_h_file;
87 if (length(full_name) < 32) printf("\t") > ids_h_file;
88 if (length(full_name) < 40) printf("\t") > ids_h_file;
89 if (length(full_name) < 48) printf("\t") > ids_h_file;
90 printf("0x%s /*%s*/\n", substr(field[2], 5), name_field) > ids_h_file
91 printf("{ 0x%s, \"%s\" },\n", substr(field[2], 5), name_field) > dev_ids_c_file
93 if (field[1] == "s" && length(field[3]) > 0 && field[4] == "0") {
94 subdev_name = get_short_subdevice_name(field[3])
95 full_name = sprintf("#define SUBDEVICE_%s_%s", svend_name, subdev_name)
96 printf("\t%s\t", full_name) > ids_h_file
97 if (length(full_name) < 9) printf("\t") > ids_h_file;
98 if (length(full_name) < 17) printf("\t") > ids_h_file;
99 if (length(full_name) < 25) printf("\t") > ids_h_file;
100 if (length(full_name) < 32) printf("\t") > ids_h_file;
101 if (length(full_name) < 40) printf("\t") > ids_h_file;
102 printf("0x%s /*%s*/\n", substr(field[2], 9), name_field) > ids_h_file
105 print_guards_end(vendors_h_file);
106 print_guards_end(ids_h_file);
107 if (with_pci_db)
108 print "};" > names_c_file
109 print "{ 0xFFFF, NULL }" > dev_ids_c_file;
110 print "};" > dev_ids_c_file
111 print_func_bodies(names_c_file);
114 function construct_guard_name(out_file)
116 split(out_file, path_components, "/");
117 sub(".h","_h", path_components[2]);
118 return "MPLAYER_" toupper(path_components[2]);
121 function print_guards_start(out_file)
123 guard_name = construct_guard_name(out_file);
124 printf("#ifndef %s\n", guard_name) > out_file
125 printf("#define %s\n", guard_name) > out_file
126 print "" > out_file
129 function print_guards_end(out_file)
131 guard_name = construct_guard_name(out_file);
132 print "" > out_file
133 printf("#endif /* %s */\n", guard_name) > out_file
136 function print_head(out_file)
138 printf("/* File: %s\n", out_file) > out_file;
139 printf(" * This file was generated automatically. Don't modify it. */\n") > out_file;
140 print "" > out_file
143 function print_func_bodies(out_file)
145 print "" > out_file
146 print "const char *pci_vendor_name(unsigned short id)" > out_file
147 print "{" > out_file
148 if (with_pci_db) {
149 print " unsigned i;" > out_file
150 print " for (i = 0; i < sizeof(vendor_ids) / sizeof(struct vendor_id_s); i++) {" > out_file
151 print " if (vendor_ids[i].id == id)" > out_file
152 print " return vendor_ids[i].name;" > out_file
153 print " }" > out_file
155 print " return NULL;" > out_file
156 print "}" > out_file
157 print "" > out_file
158 print "const char *pci_device_name(unsigned short vendor_id, unsigned short device_id)" > out_file
159 print "{" > out_file
160 if (with_pci_db) {
161 print " unsigned i, j;" > out_file
162 print " for (i = 0; i < sizeof(vendor_ids) / sizeof(struct vendor_id_s); i++) {" > out_file
163 print " if (vendor_ids[i].id == vendor_id) {" > out_file
164 print " j = 0;" > out_file
165 print " while (vendor_ids[i].dev_list[j].id != 0xFFFF) {" > out_file
166 print " if (vendor_ids[i].dev_list[j].id == device_id)" > out_file
167 print " return vendor_ids[i].dev_list[j].name;" > out_file
168 print " j++;" > out_file
169 print " };" > out_file
170 print " break;" > out_file
171 print " }" > out_file
172 print " }" > out_file
174 print " return NULL;" > out_file
175 print "}" > out_file
178 function kill_double_quoting(fld)
180 n = split(fld, phrases, "[\"]");
181 new_fld = phrases[1]
182 for (i = 2; i <= n; i++)
183 new_fld = sprintf("%s\\\"%s", new_fld, phrases[i])
184 return new_fld
187 function init_name_db()
189 vendor_names[1] = ""
192 function init_device_db()
194 # delete device_names
195 for (i in device_names)
196 delete device_names[i];
197 device_names[1] = ""
198 # delete subdevice_names
199 for (i in subdevice_names)
200 delete subdevice_names[i];
201 subdevice_names[1] = ""
204 function get_short_vendor_name(from)
206 n = split(from, name, "[ ]");
207 new_name = toupper(name[1]);
208 if (length(new_name) < 3)
209 new_name = sprintf("%s_%s", new_name, toupper(name[2]));
210 n = split(new_name, name, "[^0-9A-Za-z]");
211 svendor = name[1];
212 for (i = 2; i <= n; i++)
213 svendor = sprintf("%s%s%s", svendor, length(name[i]) ? "_" : "", name[i]);
214 new_name = svendor;
215 vend_suffix = 2;
216 # check for unique
217 while (new_name in vendor_names) {
218 new_name = sprintf("%s%u", svendor, vend_suffix)
219 vend_suffix = vend_suffix + 1;
221 # Add new name in array of vendor's names
222 vendor_names[new_name] = new_name
223 return new_name;
226 function get_short_device_name(from_name)
228 n = split(from_name, name, "[ ]");
229 new_name = toupper(name[1]);
230 if (length(name[2]))
231 new_name = sprintf("%s_%s", new_name, toupper(name[2]));
232 if (length(name[3]))
233 new_name = sprintf("%s_%s", new_name, toupper(name[3]));
234 n = split(new_name, name, "[^0-9A-Za-z]");
235 sdevice = name[1];
236 for (i = 2; i <= n; i++)
237 sdevice = sprintf("%s%s%s", sdevice, length(name[i]) ? "_" : "", name[i]);
238 new_name = sdevice;
239 dev_suffix = 2;
240 # check for unique
241 while (new_name in device_names) {
242 new_name = sprintf("%s%u", sdevice, dev_suffix)
243 dev_suffix = dev_suffix + 1;
245 # Add new name in array of device names
246 device_names[new_name] = new_name
247 return new_name;
250 function get_short_subdevice_name(from_name)
252 n = split(from_name, name, "[ ]");
253 new_name = toupper(name[1]);
254 if (length(name[2]))
255 new_name = sprintf("%s_%s", new_name, toupper(name[2]));
256 if (length(name[3]))
257 new_name = sprintf("%s_%s", new_name, toupper(name[3]));
258 n = split(new_name, name, "[^0-9A-Za-z]");
259 ssdevice = name[1];
260 for (i = 2; i <= n; i++)
261 ssdevice = sprintf("%s%s%s", ssdevice, length(name[i]) ? "_" : "", name[i]);
262 new_name = ssdevice;
263 sdev_suffix = 2;
264 # check for unique
265 while (new_name in subdevice_names) {
266 new_name = sprintf("%s%u", ssdevice, sdev_suffix)
267 sdev_suffix = sdev_suffix + 1;
269 # Add new name in array of subdevice names
270 subdevice_names[new_name] = new_name
271 return new_name;