comparison libdha/pci_db2c.awk @ 4071:9e77ea7212a2

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