| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Author: Petter Reinholdtsen |
| 4 |
# Date: 2006-08-12 |
| 5 |
# License: GNU General Public License |
| 6 |
# |
| 7 |
# Merge discover device XML files together, overriding earlier files |
| 8 |
# with information from the latter files in a list of files. |
| 9 |
# |
| 10 |
# Currently only able to update the model and submodel names. |
| 11 |
|
| 12 |
use warnings; |
| 13 |
use strict; |
| 14 |
|
| 15 |
use XML::LibXML; |
| 16 |
|
| 17 |
my $devices; # the current authorative data set |
| 18 |
|
| 19 |
my $parser = XML::LibXML->new(); |
| 20 |
|
| 21 |
sub load_file { |
| 22 |
my $filename = shift; |
| 23 |
unless (defined $devices) { |
| 24 |
$devices = $parser->parse_file($filename); |
| 25 |
return; |
| 26 |
} |
| 27 |
my $names = $parser->parse_file($filename); |
| 28 |
|
| 29 |
my $droot = $devices->getDocumentElement; |
| 30 |
my $nroot = $names->getDocumentElement; |
| 31 |
|
| 32 |
$devices->indexElements(); |
| 33 |
$names->indexElements(); |
| 34 |
|
| 35 |
foreach my $nnode ($nroot->findnodes('/device_list/device')) { |
| 36 |
|
| 37 |
my ($vendor,$model, $subvendor, $subdevice, $name, $subname); |
| 38 |
for my $attr ($nnode->attributes()) { |
| 39 |
$vendor = $attr->value if ("vendor" eq $attr->name); |
| 40 |
$model = $attr->value if ("model" eq $attr->name); |
| 41 |
$subvendor = $attr->value if ("subvendor" eq $attr->name); |
| 42 |
$subdevice = $attr->value if ("subdevice" eq $attr->name); |
| 43 |
$name = $attr->value if ("model_name" eq $attr->name); |
| 44 |
$subname = $attr->value if ("submodel_name" eq $attr->name); |
| 45 |
} |
| 46 |
my $searchstr; |
| 47 |
if ($subvendor) { |
| 48 |
$searchstr = |
| 49 |
"/device_list/device[\@vendor='$vendor' and \@model='$model' and \@subvendor='$subvendor' and \@subdevice='$subdevice']"; |
| 50 |
} else { |
| 51 |
$searchstr = |
| 52 |
# Hm, how can we specify those without subvendor and subdevice? |
| 53 |
"/device_list/device[\@vendor='$vendor' and \@model='$model' and not(\@subvendor)]"; |
| 54 |
} |
| 55 |
# print STDERR "Looking for vendor=$vendor model=$model '$searchstr'\n"; |
| 56 |
|
| 57 |
my $found = 0; |
| 58 |
my @nodes = $droot->findnodes($searchstr); |
| 59 |
foreach my $node (@nodes) { |
| 60 |
for my $attr ($node->attributes()) { |
| 61 |
if (defined $subvendor) { |
| 62 |
if ("submodel_name" eq $attr->name && |
| 63 |
$subname ne $attr->value) { |
| 64 |
$attr->setValue($subname); |
| 65 |
print STDERR "Updating submodel_name for $vendor:$model to '$subname'\n"; |
| 66 |
} |
| 67 |
} else { |
| 68 |
if ("model_name" eq $attr->name && |
| 69 |
$name ne $attr->value) { |
| 70 |
$attr->setValue($name); |
| 71 |
print STDERR "Updating model_name for $vendor:$model to '$name'\n"; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
$found = 1; |
| 76 |
# print STDERR $node->toString,"\n"; |
| 77 |
} |
| 78 |
unless ($found) { # Missing node, add it to devices |
| 79 |
print STDERR "Adding new node ",$nnode->toString,"\n"; |
| 80 |
$droot->addChild($nnode); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
load_file($ARGV[0] || 'pci-device.xml'); |
| 86 |
load_file($ARGV[1] || 'pci-device-pciids.xml'); |
| 87 |
$devices->toFile("-"); |