241916652e2257eea729b32126e744d02df61799
[metze/wireshark/wip.git] / epan / wslua / make-init-lua.pl
1 #!/usr/bin/perl
2 #
3 # make-init-lua.pl
4 #
5 # create the init.lua file based on a template (stdin) 
6 #
7 # (c) 2006, Luis E. Garcia Onatnon <luis@ontanon.org>
8 #
9 # $Id$
10 #
11 # Wireshark - Network traffic analyzer
12 # By Gerald Combs <gerald@wireshark.org>
13 # Copyright 2004 Gerald Combs
14 #
15 # This program is free software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version 2
18 # of the License, or (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28
29 use strict;
30
31 my $WSROOT = shift;
32
33 die "'$WSROOT' is not a directory" unless -d $WSROOT;
34
35 my $wtap_encaps_table = '';
36 my $ft_types_table = '';
37 my $bases_table = '';
38 my $encodings = '';
39 my $expert_pi = '';
40 my $menu_groups = '';
41
42 my %replacements = %{{
43     WTAP_ENCAPS => \$wtap_encaps_table,
44     FT_TYPES => \$ft_types_table,
45     BASES => \$bases_table,
46     ENCODINGS => \$encodings,
47     EXPERT => \$expert_pi,
48     MENU_GROUPS => \$menu_groups,
49 }};
50
51
52 #
53 # load template
54 #
55 my $template = '';
56 my $template_filename = shift;
57
58 open TEMPLATE, "< $template_filename" or die "could not open '$template_filename':  $!";
59 $template .= $_ while(<TEMPLATE>);
60 close TEMPLATE;
61
62 #
63 # Extract values from wiretap/wtap.h:
64 #
65 #       WTAP_ENCAP_ values
66 #
67
68 $wtap_encaps_table = "-- Wiretap encapsulations\nwtap = {\n";
69
70 open WTAP_H, "< $WSROOT/wiretap/wtap.h" or die "cannot open '$WSROOT/wiretap/wtap.h':  $!";
71
72 while(<WTAP_H>) {
73     if ( /^#define WTAP_ENCAP_([A-Z0-9_]+)\s+(\d+)/ ) {
74         $wtap_encaps_table .= "\t[\"$1\"] = $2,\n";
75     }
76 }
77
78 $wtap_encaps_table =~ s/,\n$/\n}\n/msi;
79
80 #
81 # Extract values from epan/ftypes/ftypes.h:
82 #
83 #       values from enum fttype
84 #
85
86 $ft_types_table = " -- Field Types\nftypes = {\n";
87
88 my $ftype_num = 0;
89
90 open FTYPES_H, "< $WSROOT/epan/ftypes/ftypes.h" or die "cannot open '$WSROOT/epan/ftypes/ftypes.h':  $!";
91 while(<FTYPES_H>) {
92     if ( /^\s+FT_([A-Z0-9a-z_]+)\s*,/ ) {
93         $ft_types_table .= "\t[\"$1\"] = $ftype_num,\n";
94         $ftype_num++;
95     }
96 }
97 close FTYPES_H;
98
99 $ft_types_table =~ s/,\n$/\n}\n/msi;
100
101 #
102 # Extract values from epan/proto.h:
103 #
104 #       values from enum base
105 #       #defines for encodings and expert group and severity levels
106 #
107
108 $bases_table = "-- Display Bases\n base = {\n";
109 $encodings = "-- Encodings\n";
110 $expert_pi = "-- Expert flags and facilities\n";
111
112 my $base_num = 0;
113
114 open PROTO_H, "< $WSROOT/epan/proto.h" or die "cannot open '$WSROOT/epan/proto.h':  $!";
115 while(<PROTO_H>) {
116     if (/^\s+BASE_([A-Z_]+),/ ) {
117         $bases_table .= "\t[\"$1\"] = $base_num,\n";
118         $base_num++;
119     }
120
121     if ( /^.define\s+(PI_[A-Z_]+)\s+((0x)?[0-9A-Fa-f]+)/ ) {
122         my ($name, $value) = ($1, hex($2));
123         $expert_pi .= "$name = $value\n";
124     }
125
126     if ( /^.define\s+(ENC_[A-Z0-9_]+)\s+((0x)?[0-9A-Fa-f]+)/ ) {
127         my ($name, $value) = ($1, hex($2));
128         $encodings .= "$name = $value\n";
129     }
130 }
131 close PROTO_H;
132
133 #
134 # Extract values from stat_menu.h:
135 #
136 #       MENU_X_X values for register_stat_group_t
137 #
138
139 $menu_groups .= "-- menu groups for register_menu\n";
140 my $menu_i = 0;
141
142 open STAT_MENU, "< $WSROOT/stat_menu.h" or die "cannot open '$WSROOT/stat_menu.h':  $!";
143 while(<STAT_MENU>) {
144     if (/REGISTER_([A-Z]+)_GROUP_([A-Z]+)/) {
145         $menu_groups .= "MENU_$1_$2 = $menu_i\n";
146         $menu_groups =~ s/_NONE//;
147         $menu_i++;
148     }
149 }
150 close STAT_MENU;
151
152
153 $bases_table .= "}\n\n";
154 $encodings .= "\n\n";
155 $expert_pi .= "\n\n";
156
157 for my $key (keys %replacements) {
158     $template =~ s/%$key%/${$replacements{$key}}/msig;
159 }
160
161
162 print $template;