summaryrefslogtreecommitdiffstats
path: root/generate_usbclass.pl
blob: 7e2dad32149e5c5bbd661bc8b13bb9f1dbd67db8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/perl

print q(/* This is auto-generated from </usr/share/usb.ids>, don't modify! */

struct {
  unsigned long id;
  const char *name;
} usbclasses[] = {
);

while (<>) {
    chomp;
    if (/^C\s+([\da-f]+)\s+(.*)/) {  #- I want alphanumeric but I can't get this [:alnum:] to work :-(
	($cat, $cat_descr) = ($1, $2);
    } elsif (/^\t([\da-f]+)\s+(.*)/ && defined $cat) {
	($sub, $sub_descr) = ($1, $2);
	$sub =~ /^[\da-f]{2}$/ or die "bad line $.: sub category number badly formatted ($_)\n";
	push @without_prot, [ "0x$cat$sub"."00", "/* $. */  { 0x$cat$sub".qq(00, "$cat_descr|$sub_descr" },\n) ];
    } elsif (/^\t\t([\da-f]+)\s+(.*)/ && defined $cat) {
	push @everything, qq(/* $. */  { 0x$cat$sub$1, "$cat_descr|$sub_descr|$2" },\n);
    } elsif (/^\S/) {
      undef $cat;
    }
}

foreach $l (@without_prot) {
    grep { /{ $l->[0], / } @everything or push @everything, $l->[1];
}

print sort @everything;

print '
};

int nb_usbclasses = sizeof(usbclasses) / sizeof(*usbclasses);

extern const char *usb_class2text(unsigned long class_) {
  int i;
  for (i = 0; i < nb_usbclasses; i++)
    if (usbclasses[i].id == class_) return usbclasses[i].name;

  return "";
}

';