/[soft]/ldetect/trunk/generate_usbclass.pl
ViewVC logotype

Contents of /ldetect/trunk/generate_usbclass.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1357 - (show annotations) (download)
Tue May 17 09:21:22 2011 UTC (10 years ago) by dmorgan
File MIME type: text/plain
File size: 2112 byte(s)
Import  ldetect
1 #!/usr/bin/perl
2
3 print q(/* This is auto-generated from </usr/share/usb.ids>, don't modify! */
4
5 #define NULL 0
6
7 struct node {
8 int id;
9 const char *name;
10 int nb_subnodes;
11 struct node *subnodes;
12 };
13
14 );
15
16 my (@l, $sub, $subsub);
17 while (<>) {
18 chomp;
19 if (/^C\s+([\da-f]+)\s+(.*)/) { #- I want alphanumeric but I can't get this [:alnum:] to work :-(
20 push @l, [ $1, $2, $sub = [] ];
21 undef $subsub;
22 } elsif (/^\t([\da-f]+)\s+(.*)/ && defined $sub) {
23 my ($sub_id, $sub_descr) = ($1, $2);
24 $sub_id =~ /^[\da-f]{2}$/ or die "bad line $.: sub category number badly formatted ($_)\n";
25 push @$sub, [ $sub_id, $sub_descr, $subsub = [] ];
26 } elsif (/^\t\t([\da-f]+)\s+(.*)/ && defined $subsub) {
27 push @$subsub, [ $1, $2, [] ];
28 } elsif (/^\S/) {
29 undef $sub;
30 undef $subsub;
31 }
32 }
33
34 sub dump_it {
35 my ($l, $name, $prefix) = @_;
36
37 my @l = sort { $a->[0] cmp $b->[0] } @$l or return;
38
39 dump_it($_->[2], $name . '_' . $_->[0], "$prefix ") foreach @l;
40
41 print "${prefix}static struct node $name\[] = {\n";
42 foreach (@l) {
43 my $nb = @{$_->[2]};
44 my $sub = $nb ? $name . '_' . $_->[0] : 'NULL';
45 printf qq($prefix { 0x%x, "%s", $nb, $sub },\n), hex($_->[0]), $_->[1];
46 }
47 print "$prefix};\n";
48 }
49
50 dump_it(\@l, "classes", '');
51
52 print '
53
54 static int nb_classes = sizeof(classes) / sizeof(*classes);
55
56 static void lookup(const char **p, int *a_class, int kind, int nb_nodes, struct node *nodes) {
57 int i;
58 for (i = 0; i < nb_nodes; i++)
59 if (nodes[i].id == a_class[kind]) {
60 p[kind] = nodes[i].name;
61 return lookup(p, a_class, kind + 1, nodes[i].nb_subnodes, nodes[i].subnodes);
62 }
63 }
64
65 struct class_text {
66 const char *class_text;
67 const char *sub_text;
68 const char *prot_text;
69 };
70
71 extern struct class_text __attribute__ ((visibility("default"))) usb_class2text(unsigned long class_id) {
72 const char *p[3] = { NULL, NULL, NULL };
73 int a_class[3] = { (class_id >> 16) & 0xff, (class_id >> 8) & 0xff, class_id & 0xff };
74 if (a_class[0] != 0xff) lookup(p, a_class, 0, nb_classes, classes);
75 {
76 struct class_text r = { p[0], p[1], p[2] };
77 return r;
78 }
79 }
80
81 ';

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.28