Skip to content

Instantly share code, notes, and snippets.

@seungwon0
Last active January 27, 2016 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seungwon0/80d1ff4318711bad1d62 to your computer and use it in GitHub Desktop.
Save seungwon0/80d1ff4318711bad1d62 to your computer and use it in GitHub Desktop.
looks up USB ID information
#!/usr/bin/env perl
#
# usb-lookup.pl - looks up USB ID information
#
# Looks up USB ID information from http://www.linux-usb.org/usb.ids.
#
# Seungwon Jeong <seungwon0@gmail.com>
#
# Copyright (C) 2014 by Seungwon Jeong
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see
# <http://www.gnu.org/licenses/>.
use perl5i::2;
use IO::All;
use Readonly;
if ( @ARGV < 1 ) {
say "Usage: ${PROGRAM_NAME} USB_ID ...";
exit 2;
}
# Readonly my $USB_IDS => 'http://www.linux-usb.org/usb.ids';
Readonly my $USB_IDS => '/usr/share/hwdata/usb.ids';
my @lines = io($USB_IDS)->chomp->slurp;
@lines = grep { !/ ^ \s* $ /xms } @lines; # Delete blank lines
@lines = grep { !/ ^ [#] /xms } @lines; # Delete comments
# Syntax:
# vendor vendor_name
# device device_name <-- single tab
# interface interface_name <-- two tabs
@lines = grep {/ ^ \t? [\da-f]{4} /xmsi} @lines;
my %usb_id;
my $current_vendor;
for my $line (@lines) {
if ( $line =~ / ^ \t /xms ) {
my ( $device, $device_name ) = split q{ }, $line, 2;
$usb_id{$current_vendor}{$device} = $device_name;
}
else {
my ( $vendor, $vendor_name ) = split q{ }, $line, 2;
$usb_id{$vendor}{vendor_name} = $vendor_name;
$current_vendor = $vendor;
}
}
my $first = 1;
for my $usb_id (@ARGV) {
if ($first) {
$first = 0;
}
else {
say q{}; # Blank line
}
my ( $vendor, $device ) = split / : /xms, $usb_id;
say $usb_id;
next if !defined $usb_id{$vendor};
say $usb_id{$vendor}{vendor_name};
if ( defined $device ) {
next if !defined $usb_id{$vendor}{$device};
say $usb_id{$vendor}{$device};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment