]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - tools/tools/pciid/mk_pci_vendors.pl
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / tools / tools / pciid / mk_pci_vendors.pl
1 #!/usr/bin/perl -w
2 #
3 # Copyright (C) 2001 Sheldon Hearn.  All rights reserved.
4
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 # 1. Redistributions of source code must retain the above copyright
9 #    notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 #    notice, this list of conditions and the following disclaimer in the
12 #    documentation and/or other materials provided with the distribution.
13
14 # THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 # SUCH DAMAGE.
25
26 # $FreeBSD$
27 #
28 # usage: mk_pci_vendors [-lq] [-p pcidevs.txt] [-v vendors.txt]
29 #
30 # Generate src/share/misc/pci_vendors from the Hart and Boemler lists,
31 # currently available at:
32 #
33 # Boemler:      http://www.pcidatabase.com/reports.php?type=tab-delimeted
34 # Hart:         http://members.datafast.net.au/dft0802/downloads/pcidevs.txt
35 #
36 # -l    Where an entry is found in both input lists, use the entry with
37 #       the longest description.  The default is for the Boemler file to
38 #       override the Hart file.
39 # -q    Do not print diagnostics.
40 # -p    Specify the pathname of the Hart file. (Default ./pcidevs.txt)
41 # -v    Specify the pathname of the Boemler file. (Default ./vendors.txt)
42 #
43 use strict;
44 use Getopt::Std;
45
46 my $PROGNAME = 'mk_pci_vendors';
47 my $VENDORS_FILE = 'vendors.txt';
48 my $PCIDEVS_FILE = 'pcidevs.txt';
49
50 my $cur_vendor;
51 my %opts;
52 my %vendors;
53 my ($descr, $existing, $id, $line, $rv, $winner, $optlused);
54
55 my $IS_VENDOR = 1;
56 my $IS_DEVICE = 2;
57 my $V_DESCR = 0;
58 my $V_DEVSL = 1;
59 my $W_NOCONTEST = 0;
60 my $W_VENDORS = 1;
61 my $W_PCIDEVS = 2;
62
63 sub clean_descr($);
64 sub vendors_parse($\$\$);
65 sub pcidevs_parse($\$\$);
66
67 if (not getopts('lp:qv:', \%opts) or @ARGV > 0) {
68         print STDERR "usage: $PROGNAME [-lq] [-p pcidevs.txt] [-v vendors.txt]\n";
69         exit 1;
70 }
71
72 if (not defined($opts{p})) {
73         $opts{p} = $PCIDEVS_FILE;
74 }
75 if (not defined($opts{v})) {
76         $opts{v} = $VENDORS_FILE;
77 }
78 foreach (('l', 'q')) {
79         if (not exists($opts{$_})) {
80                 $opts{$_} = 0;
81         } else {
82                 $opts{$_} = 1;
83         }
84 }
85
86 open(VENDORS, "< $opts{v}") or
87     die "$PROGNAME: $opts{v}: $!\n";
88 while ($line = <VENDORS>) {
89         chomp($line);
90         $rv = vendors_parse($line, $id, $descr);
91         if ($rv == $IS_VENDOR) {
92                 if (exists($vendors{$id})) {
93                         die "$PROGNAME: $id: duplicate vendor ID\n";
94                 }
95                 $vendors{$id} = [$descr, {}];
96                 $cur_vendor = $id;
97         } elsif ($rv == $IS_DEVICE) {
98                 ${$vendors{$cur_vendor}->[$V_DEVSL]}{$id} = $descr;
99         }
100 }
101 close(VENDORS);
102
103 open(PCIDEVS, "< $opts{p}") or
104     die "$PROGNAME: $opts{p}: $!\n";
105 while ($line = <PCIDEVS>) {
106         chomp($line);
107         $rv = pcidevs_parse($line, $id, $descr);
108         if ($rv == $IS_VENDOR) {
109                 if (not exists($vendors{$id})) {
110                         $vendors{$id} = [$descr, {}];
111                         $winner = $W_NOCONTEST;
112                 } elsif ($opts{l}) {
113                         $existing = $vendors{$id}->[$V_DESCR];
114                         if (length($existing) < length($descr)) {
115                                 $vendors{$id}->[$V_DESCR] = $descr;
116                                 $winner = $W_PCIDEVS;
117                         } else {
118                                 $winner = $W_VENDORS;
119                         }
120                 } else {
121                         $winner = $W_VENDORS;
122                 }
123                 $cur_vendor = $id;
124                 if (not $opts{q} and $winner != $W_NOCONTEST) {
125                         $existing = $vendors{$id}->[$V_DESCR];
126                         print STDERR "$PROGNAME: ",
127                             $winner == $W_VENDORS ? "Boemler" : "Hart",
128                             " vendor wins: $id\t$existing\n";
129                 }
130         } elsif ($rv == $IS_DEVICE) {
131                 if (not exists(${$vendors{$cur_vendor}->[$V_DEVSL]}{$id})) {
132                         ${$vendors{$cur_vendor}->[$V_DEVSL]}{$id} = $descr;
133                         $winner = $W_NOCONTEST;
134                 } elsif ($opts{l}) {
135                         $existing = ${$vendors{$cur_vendor}->[$V_DEVSL]}{$id};
136                         if (length($existing) < length($descr)) {
137                                 ${$vendors{$cur_vendor}->[$V_DEVSL]}{$id} =
138                                     $descr;
139                                 $winner = $W_PCIDEVS;
140                         } else {
141                                 $winner = $W_VENDORS;
142                         }
143                 } else {
144                         $winner = $W_VENDORS;
145                 }
146                 if (not $opts{q} and $winner != $W_NOCONTEST) {
147                         $existing = ${$vendors{$cur_vendor}->[$V_DEVSL]}{$id};
148                         print STDERR "$PROGNAME: ",
149                             $winner == $W_VENDORS ? "Boemler" : "Hart",
150                             " device wins: $id\t$existing\n";
151                 }
152         }
153 }
154 close(PCIDEVS);
155
156 $optlused = $opts{l} ? "with" : "without";
157 print <<HEADER_END;
158 ; \$FreeBSD\$
159 ;
160 ; Automatically generated by src/tools/tools/pciid/mk_pci_vendors.pl
161 ; ($optlused the -l option), using the following source lists:
162 ;
163 ;       http://www.pcidatabase.com/reports.php?type=tab-delimeted
164 ;       http://members.datafast.net.au/dft0802/downloads/pcidevs.txt
165 ;
166 ; Manual edits on this file will be lost!
167 ;
168 HEADER_END
169
170 foreach $cur_vendor (sort keys %vendors) {
171         $id = $cur_vendor;
172         $descr = $vendors{$id}->[$V_DESCR];
173         print "$id\t$descr\n";
174         foreach $id (sort keys %{$vendors{$cur_vendor}->[$V_DEVSL]}) {
175                 $descr = ${$vendors{$cur_vendor}->[$V_DEVSL]}{$id};
176                 print "\t$id\t$descr\n";
177         }
178 }
179 exit 0;
180
181
182 # Parse a line from the Boemler file and place the ID and description
183 # in the scalars referenced by $id_ref and $descr_ref.
184 #
185 # On success, returns $IS_VENDOR if the line represents a vendor entity
186 # or $IS_DEVICE if the line represents a device entity.
187 #
188 # Returns 0 on failure.
189 #
190 sub vendors_parse($\$\$)
191 {
192         my ($line, $id_ref, $descr_ref) = @_;
193
194         if ($line =~ /^([A-Fa-f0-9]{4})\t([^\t].+?)\s*$/) {
195                 ($$id_ref, $$descr_ref) = (uc($1), clean_descr($2));
196                 return $IS_VENDOR;
197         } elsif ($line =~ /^\t([A-Fa-f0-9]{4})\t([^\t].+?)\s*$/) {
198                 ($$id_ref, $$descr_ref) = (uc($1), clean_descr($2));
199                 return $IS_DEVICE;
200         } elsif (not $opts{q} and
201             $line !~ /^\s*$/ and $line !~ /^;/) {
202                 chomp($line);
203                 print STDERR "$PROGNAME: ignored Boemler: $line\n";
204         }
205
206         return 0;
207 }
208
209 # Parse a line from the Hart file and place the ID and description
210 # in the scalars referenced by $id_ref and $descr_ref.
211 #
212 # On success, returns $IS_VENDOR if the line represents a vendor entity
213 # or $IS_DEVICE if the line represents a device entity.
214 #
215 # Returns 0 on failure.
216 #
217 sub pcidevs_parse($\$\$)
218 {
219         my ($line, $id_ref, $descr_ref) = @_;
220         my $descr;
221
222         if ($line =~ /^V\t([A-Fa-f0-9]{4})\t([^\t].+?)\s*$/) {
223                 ($$id_ref, $$descr_ref) = (uc($1), clean_descr($2));
224                 return $IS_VENDOR;
225         } elsif ($line =~ /^D\t([A-Fa-f0-9]{4})\t([^\t].+?)\s*$/) {
226                 ($$id_ref, $$descr_ref) = (uc($1), clean_descr($2));
227                 return $IS_DEVICE;
228         } elsif (not $opts{q} and
229             $line !~ /^\s*$/ and $line !~ /^[;ORSX]/) {
230                 print STDERR "$PROGNAME: ignored Hart: $line\n";
231         }
232
233         return 0;
234 }
235
236 sub clean_descr($)
237 {
238         my ($descr) = @_;
239
240         return $descr;
241 }