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# Wireshark - Network traffic analyzer
10# By Gerald Combs <gerald@wireshark.org>
11# Copyright 2004 Gerald Combs
12#
13# SPDX-License-Identifier: GPL-2.0-or-later
14
15use strict;
16
17my $WSROOT = shift;
18
19die "'$WSROOT' is not a directory" unless -d $WSROOT;
20
21my $wtap_encaps_table = '';
22my $wtap_tsprecs_table = '';
23my $wtap_commenttypes_table = '';
24my $ft_types_table = '';
25my $frametypes_table = '';
26my $wtap_rec_types_table = '';
27my $wtap_presence_flags_table = '';
28my $bases_table = '';
29my $encodings = '';
30my $expert_pi = '';
31my $expert_pi_tbl = '';
32my $expert_pi_severity = '';
33my $expert_pi_group = '';
34my $menu_groups = '';
35
36my %replacements = %{{
37    WTAP_ENCAPS => \$wtap_encaps_table,
38    WTAP_TSPRECS => \$wtap_tsprecs_table,
39    WTAP_COMMENTTYPES => \$wtap_commenttypes_table,
40    FT_TYPES => \$ft_types_table,
41    FT_FRAME_TYPES => \$frametypes_table,
42    WTAP_REC_TYPES => \$wtap_rec_types_table,
43    WTAP_PRESENCE_FLAGS => \$wtap_presence_flags_table,
44    BASES => \$bases_table,
45    ENCODINGS => \$encodings,
46    EXPERT => \$expert_pi,
47    EXPERT_TABLE => \$expert_pi_tbl,
48    MENU_GROUPS => \$menu_groups,
49}};
50
51
52#
53# load template
54#
55my $template = '';
56my $template_filename = shift;
57
58open TEMPLATE, "< $template_filename" or die "could not open '$template_filename':  $!";
59$template .= $_ while(<TEMPLATE>);
60close TEMPLATE;
61
62#
63# Extract values from wiretap/wtap.h:
64#
65#   WTAP_FILE_  values
66#	WTAP_ENCAP_ values
67#   WTAP_HAS_   values
68#
69
70$wtap_encaps_table = "-- Wiretap encapsulations XXX\nwtap_encaps = {\n";
71$wtap_tsprecs_table = "-- Wiretap timestamp precision types\nwtap_tsprecs = {\n";
72$wtap_commenttypes_table = "-- Wiretap file comment types\nwtap_comments = {\n";
73$wtap_rec_types_table = "-- Wiretap record_types\nwtap_rec_types = {\n";
74$wtap_presence_flags_table = "-- Wiretap presence flags\nwtap_presence_flags = {\n";
75
76open WTAP_H, "< $WSROOT/wiretap/wtap.h" or die "cannot open '$WSROOT/wiretap/wtap.h':  $!";
77
78while(<WTAP_H>) {
79    if ( /^#define WTAP_ENCAP_([A-Z0-9_]+)\s+(-?\d+)/ ) {
80        $wtap_encaps_table .= "\t[\"$1\"] = $2,\n";
81    }
82
83    if ( /^#define WTAP_TSPREC_([A-Z0-9_]+)\s+(\d+)/ ) {
84        $wtap_tsprecs_table .= "\t[\"$1\"] = $2,\n";
85    }
86
87    if ( /^#define WTAP_COMMENT_([A-Z0-9_]+)\s+(0x\d+)/ ) {
88        $wtap_commenttypes_table .= "\t[\"$1\"] = $2,\n";
89    }
90
91    if ( /^#define REC_TYPE_([A-Z0-9_]+)\s+(\d+)\s+\/\*\*<([^\*]+)\*\// ) {
92        $wtap_rec_types_table .= "\t[\"$1\"] = $2,  --$3\n";
93    }
94
95    if ( /^#define WTAP_HAS_([A-Z0-9_]+)\s+(0x\d+)\s+\/\*\*<([^\*]+)\*\// ) {
96        my $num = hex($2);
97        $wtap_presence_flags_table .= "\t[\"$1\"] = $num,  --$3\n";
98    }
99}
100
101$wtap_encaps_table =~ s/,\n$/\n}\nwtap = wtap_encaps -- for bw compatibility\n/msi;
102$wtap_tsprecs_table =~ s/,\n$/\n}\n/msi;
103$wtap_commenttypes_table =~ s/,\n$/\n}\n/msi;
104# wtap_rec_types_table has comments at the end (not a comma),
105# but Lua doesn't care about extra commas so leave it in
106$wtap_rec_types_table =~ s/\n$/\n}\n/msi;
107# wtap_presence_flags_table has comments at the end (not a comma),
108# but Lua doesn't care about extra commas so leave it in
109$wtap_presence_flags_table =~ s/\n$/\n}\n/msi;
110
111#
112# Extract values from epan/ftypes/ftypes.h:
113#
114#	values from enum fttype
115#
116
117$ft_types_table = "-- Field Types\nftypes = {\n";
118$frametypes_table = "-- Field Type FRAMENUM Types\nframetype = {\n";
119
120my $ftype_num = 0;
121my $frametypes_num = 0;
122
123open FTYPES_H, "< $WSROOT/epan/ftypes/ftypes.h" or die "cannot open '$WSROOT/epan/ftypes/ftypes.h':  $!";
124while(<FTYPES_H>) {
125    if ( /^\s+FT_FRAMENUM_([A-Z0-9a-z_]+)\s*,/ ) {
126        $frametypes_table .= "\t[\"$1\"] = $frametypes_num,\n";
127        $frametypes_num++;
128    } elsif ( /^\s+FT_([A-Z0-9a-z_]+)\s*,/ ) {
129        $ft_types_table .= "\t[\"$1\"] = $ftype_num,\n";
130        $ftype_num++;
131    }
132}
133close FTYPES_H;
134
135$ft_types_table =~ s/,\n$/\n}\n/msi;
136$frametypes_table =~ s/,\n$/\n}\n/msi;
137
138#
139# Extract values from epan/proto.h:
140#
141#	values from enum base
142#	#defines for encodings and expert group and severity levels
143#
144
145$bases_table        = "-- Display Bases\nbase = {\n";
146$encodings          = "-- Encodings\n";
147$expert_pi          = "-- Expert flags and facilities (deprecated - see 'expert' table below)\n";
148$expert_pi_tbl      = "-- Expert flags and facilities\nexpert = {\n";
149$expert_pi_severity = "\t-- Expert severity levels\n\tseverity = {\n";
150$expert_pi_group    = "\t-- Expert event groups\n\tgroup = {\n";
151
152open PROTO_H, "< $WSROOT/epan/proto.h" or die "cannot open '$WSROOT/epan/proto.h':  $!";
153
154my $in_severity = 0;
155my $prev_comment;
156my $skip_this = 0;
157
158while(<PROTO_H>) {
159    $skip_this = 0;
160
161    if (/^\s+(?:BASE|STR|SEP)_([A-Z_]+)[ ]*=[ ]*([0-9]+)[,\s]+(?:\/\*\*< (.*?) \*\/)?/) {
162        $bases_table .= "\t[\"$1\"] = $2,  -- $3\n";
163    }
164
165    if (/^#define\s+BASE_(RANGE_STRING)[ ]*((?:0x)?[0-9]+)[ ]+(?:\/\*\*< (.*?) \*\/)?/) {
166        # Handle BASE_RANGE_STRING
167        my $num = hex($2);
168        $bases_table .= "\t[\"$1\"] = $num,  -- $3\n";
169    }
170
171    if (/^#define\s+BASE_(UNIT_STRING)[ ]*((?:0x)?[0-9]+)[ ]+(?:\/\*\*< (.*?) \*\/)?/) {
172        # Handle BASE_UNIT_STRING as a valid base value in Lua
173        my $num = hex($2);
174        $bases_table .= "\t[\"$1\"] = $num,  -- $3\n";
175    }
176
177    if (/^.define\s+PI_SEVERITY_MASK /) {
178        $in_severity = 1;
179        $skip_this = 1;
180    }
181
182    if (/^.define\s+PI_GROUP_MASK /) {
183        $in_severity = 2;
184        $skip_this = 1;
185    }
186
187    if ($in_severity && /\/\*\* (.*?) \*\//) {
188        $prev_comment = $1;
189    }
190
191    if ( /^.define\s+(PI_([A-Z_]+))\s+((0x)?[0-9A-Fa-f]+)/ ) {
192        my ($name, $abbr, $value) = ($1, $2, hex($3));
193        # I'm keeping this here for backwards-compatibility
194        $expert_pi .= "$name = $value\n";
195
196        if (!$skip_this && $in_severity == 1) {
197            $expert_pi_severity .= "\t\t-- $prev_comment\n";
198            $expert_pi_severity .= "\t\t\[\"$abbr\"\] = $value,\n";
199        }
200        elsif (!$skip_this && $in_severity == 2) {
201            $expert_pi_group .= "\t\t-- $prev_comment\n";
202            $expert_pi_group .= "\t\t\[\"$abbr\"\] = $value,\n";
203        }
204    }
205
206    if ( /^.define\s+(ENC_[A-Z0-9_]+)\s+((0x)?[0-9A-Fa-f]+)/ ) {
207        my ($name, $value) = ($1, hex($2));
208        $encodings .= "$name = $value\n";
209    }
210}
211close PROTO_H;
212
213#
214# Extract values from time_fmt.h:
215#
216#	ABSOLUTE_TIME_XXX values for absolute time bases
217#
218
219my $absolute_time_num = 0;
220
221open TIME_FMT_H, "< $WSROOT/epan/time_fmt.h" or die "cannot open '$WSROOT/epan/time_fmt.h':  $!";
222while(<TIME_FMT_H>) {
223    if (/^\s+ABSOLUTE_TIME_([A-Z_]+)[ ]*=[ ]*([0-9]+)[,\s]+(?:\/\* (.*?) \*\/)?/) {
224        $bases_table .= "\t[\"$1\"] = $2,  -- $3\n";
225        $absolute_time_num = $2 + 1;
226    } elsif (/^\s+ABSOLUTE_TIME_([A-Z_]+)[,\s]+(?:\/\* (.*?) \*\/)?/) {
227        $bases_table .= "\t[\"$1\"] = $absolute_time_num,  -- $2\n";
228        $absolute_time_num++;
229    }
230}
231close TIME_FTM_H;
232
233#
234# Extract values from stat_groups.h:
235#
236#	MENU_X_X values for register_stat_group_t
237#
238
239$menu_groups .= "-- menu groups for register_menu\n";
240my $menu_i = 0;
241
242open STAT_GROUPS, "< $WSROOT/epan/stat_groups.h" or die "cannot open '$WSROOT/epan/stat_groups.h':  $!";
243my $foundit = 0;
244while(<STAT_GROUPS>) {
245    # need to skip matching words in comments, and get to the enum
246    if (/^typedef enum \{/) { $foundit = 1; }
247    # the problem here is we need to pick carefully, so we don't break existing scripts
248    if ($foundit && /REGISTER_([A-Z]+)_GROUP_(CONVERSATION|RESPONSE|ENDPOINT|[A-Z0-9_]+)/) {
249        $menu_groups .= "MENU_$1_$2 = $menu_i\n";
250        $menu_groups =~ s/_NONE//;
251        $menu_i++;
252    }
253}
254close STAT_GROUPS;
255
256
257$bases_table .= "}\n";
258$encodings .= "\n";
259$expert_pi .= "\n";
260$expert_pi_severity .= "\t},\n";
261$expert_pi_group .= "\t},\n";
262$expert_pi_tbl .= $expert_pi_group . $expert_pi_severity . "}\n\n";
263
264for my $key (keys %replacements) {
265    $template =~ s/%$key%/${$replacements{$key}}/msig;
266}
267
268
269print $template;
270