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
29 # check for arguments:
30 print "Usage ./pci_db2c.awk pci.db (and make sure pci.db file exists first)";
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
52 print "#include <stddef.h>" > names_c_file
53 print "#include \"pci_names.h\"" > names_c_file
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
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") {
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
;
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
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
);
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
129 function print_guards_end
(out_file
)
131 guard_name = construct_guard_name
(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
;
143 function print_func_bodies
(out_file
)
146 print "const char *pci_vendor_name(unsigned short id)" > out_file
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
158 print "const char *pci_device_name(unsigned short vendor_id, unsigned short device_id)" > out_file
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
178 function kill_double_quoting
(fld
)
180 n =
split(fld
, phrases
, "[\"]");
182 for (i =
2; i
<= n
; i
++)
183 new_fld =
sprintf("%s\\\"%s", new_fld
, phrases
[i
])
187 function init_name_db
()
192 function init_device_db
()
194 # delete device_names
195 for (i in device_names
)
196 delete device_names
[i
];
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]");
212 for (i =
2; i
<= n
; i
++)
213 svendor =
sprintf("%s%s%s", svendor
, length(name
[i
]) ?
"_" : "", name
[i
]);
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
226 function get_short_device_name
(from_name
)
228 n =
split(from_name
, name
, "[ ]");
229 new_name =
toupper(name
[1]);
231 new_name =
sprintf("%s_%s", new_name
, toupper(name
[2]));
233 new_name =
sprintf("%s_%s", new_name
, toupper(name
[3]));
234 n =
split(new_name
, name
, "[^0-9A-Za-z]");
236 for (i =
2; i
<= n
; i
++)
237 sdevice =
sprintf("%s%s%s", sdevice
, length(name
[i
]) ?
"_" : "", name
[i
]);
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
250 function get_short_subdevice_name
(from_name
)
252 n =
split(from_name
, name
, "[ ]");
253 new_name =
toupper(name
[1]);
255 new_name =
sprintf("%s_%s", new_name
, toupper(name
[2]));
257 new_name =
sprintf("%s_%s", new_name
, toupper(name
[3]));
258 n =
split(new_name
, name
, "[^0-9A-Za-z]");
260 for (i =
2; i
<= n
; i
++)
261 ssdevice =
sprintf("%s%s%s", ssdevice
, length(name
[i
]) ?
"_" : "", name
[i
]);
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