From Andrew Feren:
[obnox/wireshark/wip.git] / epan / make-sminmpec.pl
1 #!/usr/bin/perl -w
2 # create the sminmpec.c file from 
3 # http://www.iana.org/assignments/enterprise-numbers
4 #
5 # $Id$
6 #
7 # Wireshark - Network traffic analyzer
8 # By Gerald Combs <gerald@wireshark.org>
9 # Copyright 2004 Gerald Combs
10 #
11 # This program is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU General Public License
13 # as published by the Free Software Foundation; either version 2
14 # of the License, or (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24 use strict;
25 use Getopt::Long;
26
27 my $opt_gen;
28 GetOptions("gen" => \$opt_gen );
29
30 my $in = shift;
31 my $out = shift;
32
33 if (! defined $in && ! defined $out and $opt_gen) {
34                 $in = "http://www.iana.org/assignments/enterprise-numbers";
35                 $out = "sminmpec.c";
36 }
37
38 my @in_lines;
39
40 open OUT, "> $out";
41
42 if($in =~ m/^http:/i) {
43   eval "require LWP::UserAgent;";
44   if ( $@ ) {
45     die "LWP isn't installed. It is part of the standard Perl\n" .
46       " module libwww.  Bailing.\n";
47   }
48   my $agent    = LWP::UserAgent->new;
49
50   warn "starting to fetch $in ...\n";
51   
52   my $request  = HTTP::Request->new(GET => $in);
53   my $result   = $agent->request($request);
54
55   warn "done fetching $in ...\n";
56
57   @in_lines = split /\n/, $result->content;
58 } else {
59   open IN, "< $in";
60   @in_lines = <IN>;
61   close IN;
62 }
63
64 if ($opt_gen) {
65   for (@in_lines) {
66     chomp;
67     print OUT "$_\n";
68   }
69   exit;
70 }
71
72
73 my $body = '';
74 my $code;
75
76 sub escape_non_ascii {
77     my $val = unpack 'C', $_[0];
78     return sprintf '\0%.3o',$val;
79 }
80
81 for(@in_lines) {
82         s/[\000-\037]//g;
83         s/\\/\\\\/g;
84         s/"/\\"/g;
85         s/([\x80-\xFF])/escape_non_ascii($1)/ge;
86         
87         if (/^(\d+)/) {
88                 $code = $1;
89         } if (/^  (\S.*)/ ) {
90                 my $name = $1;
91                 $body .= "\t{ $code,\t\"$name\" },\n";
92         }
93 }
94
95 print OUT <<"_SMINMPEC";
96 /*
97  * THIS FILE IS AUTOGENERATED, DO NOT EDIT
98  * generated from http://www.iana.org/assignments/enterprise-numbers
99  * run "epan/make-sminmspec <infile> <outfile>" to regenerate
100  */
101 #ifdef HAVE_CONFIG_H
102 # include "config.h"
103 #endif
104
105 #include <glib.h>
106
107 #include <epan/value_string.h>
108 #include <epan/sminmpec.h>
109
110 const value_string sminmpec_values[] = {
111
112 $body
113         {0, NULL}
114 };
115
116 _SMINMPEC
117
118 close OUT;