Add newlines to the AsciiDoc output.
[metze/wireshark/wip.git] / tools / convert_proto_tree_add_text.pl
1 #!/usr/bin/env perl
2 #
3 # Copyright 2013 Michael Mann (see AUTHORS file)
4 #
5 # A program to help convert proto_tree_add_text calls into filterable "items" that
6 # use proto_tree_add_item.  The program requires 2 passes.  "Pass 1" (generate) collects 
7 # the eligible proto_tree_add_text calls and outputs the necessary data into a delimited
8 # file.  "Pass 2" (fix-all) takes the data from the delimited file and replaces the
9 # proto_tree_add_text calls with proto_tree_add_item or "expert info" calls as well as 
10 # generating separate files for the hf and/or ei variable declarations and hf and/or ei array data.
11 # The hf "files" can be copy/pasted into the dissector where appropriate (until such time as 
12 # its done automatically)
13 #
14 # Note that the output from "Pass 1" won't always be a perfect conversion for "Pass 2", so
15 # "human interaction" is needed as an intermediary to verify and update the delimited file
16 # before "Pass 2" is done.
17 # It is also recommended to run checkhf.pl and checkAPIs.pl after "Pass 2" is completed.
18 #
19 # Delimited file field format:
20 # <convert proto_tree_add_text_call[0|1|10-13]><add hf or ei variable[0|1|2]><proto_tree var><hf var><tvb var><offset><length><encoding>
21 # <[FIELDNAME]><[FIELDTYPE]><[FIELDABBREV]><[FIELDDISPLAY]><[FIELDCONVERT]><[BITMASK]>
22 #
23 # convert proto_tree_add_text_call enumerations:
24 # 0  - no conversions
25 # 1  - proto_tree_add_item
26 # 10 - expert_add_info
27 # 11 - expert_add_info_format
28 # 12 - proto_tree_add_expert
29 # 13 - proto_tree_add_expert_format
30 #
31 # Usage: convert_proto_tree_add_text.pl action=<generate|fix-all> <file or files>
32 #
33 # Lots of code shamelessly borrowed from fix-encoding-args.pl (Thanks Bill!)
34 #
35 # $Id$
36 #
37 # Wireshark - Network traffic analyzer
38 # By Gerald Combs <gerald@wireshark.org>
39 # Copyright 1998 Gerald Combs
40 #
41 # This program is free software; you can redistribute it and/or
42 # modify it under the terms of the GNU General Public License
43 # as published by the Free Software Foundation; either version 2
44 # of the License, or (at your option) any later version.
45 #
46 # This program is distributed in the hope that it will be useful,
47 # but WITHOUT ANY WARRANTY; without even the implied warranty of
48 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
49 # GNU General Public License for more details.
50 #
51 # You should have received a copy of the GNU General Public License
52 # along with this program; if not, write to the Free Software
53 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
54 #
55
56 use strict;
57 use warnings;
58
59 use Getopt::Long;
60
61 my %DISPLAY_BASE = ('BASE_NONE' => "BASE_NONE",
62                                            'BASE_DEC' => "BASE_DEC",
63                                            'BASE_HEX' => "BASE_HEX",
64                                            'BASE_OCT' => "BASE_OCT",
65                                            'BASE_DEC_HEX' => "BASE_DEC_HEX",
66                                            'BASE_HEX_DEC' => "BASE_HEX_DEC",
67                                            'BASE_EXT_STRING' => "BASE_EXT_STRING",
68                                            'BASE_CUSTOM' => "BASE_CUSTOM");
69
70 my %ENCODINGS = ('ENC_BIG_ENDIAN' => "ENC_BIG_ENDIAN",
71                                            'ENC_LITTLE_ENDIAN' => "ENC_LITTLE_ENDIAN",
72                                            'ENC_TIME_TIMESPEC' => "ENC_TIME_TIMESPEC",
73                                            'ENC_TIME_NTP' => "ENC_TIME_NTP",
74                                            'ENC_ASCII' => "ENC_ASCII",
75                                            'ENC_UTF_8' => "ENC_UTF_8",
76                                            'ENC_UTF_16' => "ENC_UTF_16",
77                                            'ENC_UCS_2' => "ENC_UCS_2",
78                                            'ENC_EBCDIC' => "ENC_EBCDIC",
79                                            'ENC_NA' => "ENC_NA");
80
81 my %FIELD_TYPE = ('FT_NONE' => "FT_NONE", 'FT_PROTOCOL' => "FT_PROTOCOL", 'FT_BOOLEAN' => "FT_BOOLEAN",
82                                    'FT_UINT8' => "FT_UINT8", 'FT_UINT16' => "FT_UINT16", 'FT_UINT24' => "FT_UINT24", 'FT_UINT32' => "FT_UINT32", 'FT_UINT64' => "FT_UINT64",
83                                    'FT_INT8' => "FT_INT8", 'FT_INT16' => "FT_INT16", 'FT_INT24' => "FT_INT24", 'FT_INT32' => "FT_INT32", 'FT_INT64' => "FT_INT64",
84                                    'FT_FLOAT' => "FT_FLOAT", 'FT_DOUBLE' => "FT_DOUBLE",
85                                    'FT_ABSOLUTE_TIME' => "FT_ABSOLUTE_TIME", 'FT_RELATIVE_TIME' => "FT_RELATIVE_TIME",
86                                    'FT_STRING' => "FT_STRING", 'FT_STRINGZ' => "FT_STRINGZ", 'FT_UINT_STRING' => "FT_UINT_STRING",
87                                    'FT_ETHER' => "FT_ETHER", 'FT_BYTES' => "FT_BYTES", 'FT_UINT_BYTES' => "FT_UINT_BYTES",
88                                    'FT_IPv4' => "FT_IPv4", 'FT_IPv6' => "FT_IPv6", 'FT_IPXNET' => "FT_IPXNET", 'FT_AX25' => "FT_AX25", 'FT_VINES' => "FT_VINES",
89                                    'FT_FRAMENUM' => "FT_FRAMENUM", 'FT_PCRE' => "FT_PCRE", 'FT_GUID' => "FT_GUID", 'FT_OID' => "FT_OID", 'FT_EUI64' => "FT_EUI64");
90
91 my %EXPERT_SEVERITY = ('PI_COMMENT' => "PI_COMMENT",
92                                            'PI_CHAT' => "PI_CHAT",
93                                            'PI_NOTE' => "PI_NOTE",
94                                            'PI_WARN' => "PI_WARN",
95                                            'PI_ERROR' => "PI_ERROR");
96
97 my %EXPERT_GROUPS = ('PI_CHECKSUM' => "PI_CHECKSUM",
98                                            'PI_SEQUENCE' => "PI_SEQUENCE",
99                                            'PI_RESPONSE_CODE' => "PI_RESPONSE_CODE",
100                                            'PI_REQUEST_CODE' => "PI_REQUEST_CODE",
101                                            'PI_UNDECODED' => "PI_UNDECODED",
102                                            'PI_REASSEMBLE' => "PI_REASSEMBLE",
103                                            'PI_MALFORMED' => "PI_MALFORMED",
104                                            'PI_DEBUG' => "PI_DEBUG",
105                                            'PI_PROTOCOL' => "PI_PROTOCOL",
106                                            'PI_SECURITY' => "PI_SECURITY",
107                                            'PI_COMMENTS_GROUP' => "PI_COMMENTS_GROUP");
108
109 my @proto_tree_list;
110 my @expert_list;
111 my $protabbrev = "";
112
113 # Perl trim function to remove whitespace from the start and end of the string
114 sub trim($)
115 {
116         my $string = shift;
117         $string =~ s/^\s+//;
118         $string =~ s/\s+$//;
119         return $string;
120 }
121
122 # ---------------------------------------------------------------------
123 #
124 # MAIN
125 #
126 my $helpFlag  = '';
127 my $action    = 'generate';
128 my $encoding  = '';
129 my $expert  = '';
130
131 my $result = GetOptions(
132                                                 'action=s' => \$action,
133                                                 'encoding=s' => \$encoding,
134                                                 'expert'   => \$expert,
135                                                 'help|?'   => \$helpFlag
136                                                 );
137
138 if (!$result || $helpFlag || !$ARGV[0]) {
139         usage();
140 }
141
142 sub usage {
143         print "\nUsage: $0 [--action=generate|fix-all|find-all] [--encoding=ENC_BIG_ENDIAN|ENC_LITTLE_ENDIAN] FILENAME [...]\n\n";
144                 print "  --action = generate (default)\n";
145                 print "    generate - create a delimited file (FILENAME.proto_tree_input) with\n";
146                 print "               proto_tree_add_text fields in FILENAME(s)\n";
147                 print "    fix-all  - Use delimited file (FILENAME.proto_tree_input) to convert\n";
148                 print "               proto_tree_add_text to proto_tree_add_item\n";
149                 print "               Also generates FILENAME.hf and FILENAME.hf_array to be\n";
150                 print "               copy/pasted into the dissector where appropriate\n";
151                 print "    find-all - Output the number of eligible proto_tree_add_text calls\n";
152                 print "               for conversion\n\n";
153                 print "  --expert     (Optional) Includes proto_tree_add_text calls with no printf arguments in\n";
154                 print "               the .proto_tree_input file as they could be converted to expert info\n";
155                 print "               (otherwise they are ignored)\n";
156                 print "               Must be called for 'fix-all' if called on 'generate'\n";
157                 print "  --encoding   (Optional) Default encoding if one can't be determined\n";
158                 print "               (effective only for generate)\n";
159                 print "               If not specified, an encoding will not be auto-populated\n";
160                 print "               if undetermined\n\n";
161
162         exit(1);
163 }
164
165 #
166 # XXX Outline general algorithm here
167 #
168 my $found_total = 0;
169 my $protabbrev_index;
170 my $line_number = 0;
171
172 while (my $fileName = $ARGV[0]) {
173         shift;
174         my $fileContents = '';
175
176         die "No such file: \"$fileName\"\n" if (! -e $fileName);
177
178         # delete leading './'
179         $fileName =~ s{ ^ \. / } {}xo;
180
181         #determine PROTABBREV for dissector based on file name format of (dirs)/packet-PROTABBREV.c
182         $protabbrev_index = rindex($fileName, "packet-");
183         if ($protabbrev_index == -1) {
184                 print "$fileName doesn't fit format of packet-PROTABBREV.c\n";
185                 next;
186         }
187
188         $protabbrev = substr($fileName, $protabbrev_index+length("packet-"));
189         $protabbrev_index = rindex($protabbrev, ".");
190         if ($protabbrev_index == -1) {
191                 print "$fileName doesn't fit format of packet-PROTABBREV.c\n";
192                 next;
193         }
194         $protabbrev = lc(substr($protabbrev, 0, $protabbrev_index));
195
196         # Read in the file (ouch, but it's easier that way)
197         open(FCI, "<", $fileName) || die("Couldn't open $fileName");
198         while (<FCI>) {
199                 $fileContents .= $_;
200         }
201         close(FCI);
202
203         if ($action eq "generate") {
204                 generate_hfs(\$fileContents, $fileName);
205         }
206
207         if ($action eq "fix-all") {
208                 # Read in the hf "input" file
209                 $line_number = 0;
210                 my $errors = 0;
211                 open(FCI, "<", $fileName . ".proto_tree_input") || die("Couldn't open $fileName.proto_tree_input");
212                 while(my $line=<FCI>){
213                         my @proto_tree_item = split(/;|\n/, $line);
214
215                         $line_number++;
216                         $errors += verify_line(@proto_tree_item);
217
218                         push(@proto_tree_list, \@proto_tree_item);
219                         if ($proto_tree_item[1] eq "2") {
220                                 push(@expert_list, \@proto_tree_item);
221                         }
222                 }
223                 close(FCI);
224
225                 if ($errors > 0) {
226                         print "Aborting conversion.\n";
227                         exit(-1);
228                 }
229
230                 fix_proto_tree_add_text(\$fileContents, $fileName);
231
232                 # Write out the hf data
233                 output_hf_array($fileName);
234                 output_hf($fileName);
235
236                 # Write out the changed version to a file
237                 open(FCO, ">", $fileName . ".proto_tree_add_text");
238                 print FCO "$fileContents";
239                 close(FCO);
240         }
241
242         if ($action eq "find-all") {
243                 # Find all proto_tree_add_text() statements eligible for conversion
244                 $found_total += find_all(\$fileContents, $fileName);
245                 print "Found $found_total proto_tree_add_text calls eligible for conversion.\n";
246         }
247
248 } # while
249
250 exit $found_total;
251
252 # ---------------------------------------------------------------------
253 # Sanity check the data in the .proto_tree_input file
254 sub verify_line {
255         my( @proto_tree_item) = @_;
256         my $errors = 0;
257
258         #do some basic error checking of the file
259         if ($proto_tree_item[0] eq "1") {
260                 if (!($proto_tree_item[3] =~ /^hf_/)) {
261                         print "$line_number: Poorly formed hf_ variable ($proto_tree_item[3])!\n";
262                         $errors++;
263                 }
264
265                 foreach (split(/\|/, $proto_tree_item[7])) {
266                         if (!exists($ENCODINGS{$_})) {
267                                 print "$line_number: Encoding value '$_' unknown!\n";
268                                 $errors++;
269                         }
270                 }
271         } elsif (($proto_tree_item[0] eq "10") ||
272                          ($proto_tree_item[0] eq "11") ||
273                          ($proto_tree_item[0] eq "12") ||
274                          ($proto_tree_item[0] eq "13")) {
275                 #expert info conversions
276                 if (!($proto_tree_item[3] =~ /^ei_/)) {
277                         print "$line_number: Poorly formed ei_ variable ($proto_tree_item[3])!\n";
278                         $errors++;
279                 }
280         } elsif ($proto_tree_item[0] ne "0") {
281                 print "Bad conversion value!  Aborting conversion.\n";
282                 $errors++;
283         }
284
285         if ($proto_tree_item[1] eq "1") {
286                 if (!($proto_tree_item[3] =~ /^hf_/)) {
287                         print "$line_number: Poorly formed hf_ variable ($proto_tree_item[3])!\n";
288                         $errors++;
289                 }
290                 if (!exists($FIELD_TYPE{$proto_tree_item[9]})) {
291                         print "$line_number: Field type '$proto_tree_item[9]' unknown!\n";
292                         $errors++;
293                 }
294                 foreach (split(/\|/, $proto_tree_item[11])) {
295                         if ((!exists($DISPLAY_BASE{$_})) &&
296                                 (!($proto_tree_item[11] =~ /\d+/))) {
297                                 print "$line_number: Display base '$proto_tree_item[11]' unknown!\n";
298                                 $errors++;
299                         }
300                 }
301
302         } elsif ($proto_tree_item[1] eq "2") {
303                 if (!($proto_tree_item[3] =~ /^ei_/)) {
304                         print "$line_number: Poorly formed ei_ variable ($proto_tree_item[3])!\n";
305                         $errors++;
306                 }
307                 if (!exists($EXPERT_SEVERITY{$proto_tree_item[9]})) {
308                         print "$line_number: Expert severity value '$proto_tree_item[9]' unknown!\n";
309                         $errors++;
310                 }
311                 if (!exists($EXPERT_GROUPS{$proto_tree_item[7]})) {
312                         print "$line_number: Expert group value '$proto_tree_item[7]' unknown!\n";
313                         $errors++;
314                 }
315
316         } elsif ($proto_tree_item[1] ne "0") {
317                         print "$line_number: Bad hf/ei variable generation value!\n";
318                         $errors++;
319         }
320
321         return $errors;
322 }
323
324 sub generate_hfs {
325         my( $fileContentsRef, $fileName) = @_;
326         my @args;
327         my $num_items = 0;
328         my @temp;
329         my $str_temp;
330         my $pat;
331
332         if ($expert ne "") {
333                 $pat = qr /
334                                         (
335                                                  (?:proto_tree_add_text)\s* \(
336                                                  (([^[\,;])*\,){4,}
337                                                  [^;]*
338                                                  \s* \) \s* ;
339                                         )
340                                 /xs;
341         } else {
342                 $pat = qr /
343                                         (
344                                                  (?:proto_tree_add_text)\s* \(
345                                                  (([^[\,;])*\,){5,}
346                                                  [^;]*
347                                                  \s* \) \s* ;
348                                         )
349                                 /xs;
350         }
351
352         while ($$fileContentsRef =~ / $pat /xgso) {
353                 my @proto_tree_item = (1, 1, "tree", "hf_name", "tvb", "offset", "length", "encoding",
354                                                            "fieldfullname", "fieldtype", "fieldabbrevname", "BASE_NONE", "NULL", "0x0");
355                 my $str = "${1}\n";
356                 $str =~ tr/\t\n\r/ /d;
357                 $str =~ s/ \s+ / /xg;
358                 #print "$fileName: $str\n";
359
360                 @args = split(/,/, $str);
361                 #printf "ARGS(%d): %s\n", scalar @args, join("# ", @args);
362                 $args[0] =~ s/proto_tree_add_text\s*\(\s*//;
363                 $proto_tree_item[2] = $args[0];                 #tree
364                 $proto_tree_item[4] = trim($args[1]);   #tvb
365                 $proto_tree_item[5] = trim($args[2]);   #offset
366                 $proto_tree_item[6] = trim($args[3]);   #length
367                 if (scalar @args == 5) {
368                         #remove the "); at the end
369                         $args[4] =~ s/\"\s*\)\s*;$//;
370                 }
371
372                 #encoding
373                 if (scalar @args > 5) {
374                         if (($proto_tree_item[6] eq "1") ||
375                                 ($args[5] =~ /tvb_get_guint8/) ||
376                                 ($args[5] =~ /tvb_bytes_to_str/) ||
377                                 ($args[5] =~ /tvb_ether_to_str/) ||
378                                 ($args[5] =~ /tvb_vines_addr_to_str/))  {
379                                 $proto_tree_item[7] = "ENC_NA";
380                         } elsif ($args[5] =~ /tvb_get_ntoh/) {
381                                 $proto_tree_item[7] = "ENC_BIG_ENDIAN";
382                         } elsif ($args[5] =~ /tvb_get_letoh/) {
383                                 $proto_tree_item[7] = "ENC_LITTLE_ENDIAN";
384                         } elsif (($args[5] =~ /tvb_get_ephemeral_string/) || 
385                                          ($args[5] =~ /tvb_format_text/)){
386                                 $proto_tree_item[7] = "ENC_NA|ENC_ASCII";
387                         } elsif ($encoding ne "") {
388                                 $proto_tree_item[7] = $encoding;
389                         }
390                 }
391
392                 #field full name
393                 if (($expert ne "") || (scalar @args > 5)) {
394                         my @arg_temp = split(/=|:/, $args[4]);
395                         $proto_tree_item[8] = $arg_temp[0];
396                 } else {
397                         $proto_tree_item[8] = $args[4];
398                 }
399                 $proto_tree_item[8] =~ s/\"//;
400                 $proto_tree_item[8] = trim($proto_tree_item[8]);
401
402                 if ($proto_tree_item[8] eq "%s\"") {
403                         #assume proto_tree_add_text will not be converted
404                         $proto_tree_item[0] = 0;
405                         $proto_tree_item[1] = 0;
406                         $proto_tree_item[3] = sprintf("hf_%s_", $protabbrev);
407                         $proto_tree_item[10] = sprintf("%s.", $protabbrev);
408                 } else {
409                         #hf variable name
410                         $proto_tree_item[3] = sprintf("hf_%s_%s", $protabbrev, lc($proto_tree_item[8]));
411                         $proto_tree_item[3] =~ s/\s+|-|:/_/g;
412
413                         #field abbreviated name
414                         $proto_tree_item[10] = sprintf("%s.%s", $protabbrev, lc($proto_tree_item[8]));
415                         $proto_tree_item[10] =~ s/\s+|-|:/_/g;
416                 }
417
418                 #VALS
419                 if ($str =~ /val_to_str(_const)?\(\s*tvb_get_[^\(]*\([^\,]*,[^\)]*\)\s*\,\s*([^\,]*)\s*\,\s*([^\)]*)\)/) {
420                         $proto_tree_item[12] = sprintf("VALS(%s)", trim($2));
421                 } elsif ($str =~ /val_to_str(_const)?\([^\,]*\,([^\,]*)\,/) {
422                         $proto_tree_item[12] = sprintf("VALS(%s)", trim($2));
423                 } elsif ($str =~ /val_to_str_ext(_const)?\(\s*tvb_get_[^\(]*\([^\,]*,[^\)]*\)\s*\,\s*([^\,]*)\s*\,\s*([^\)]*)\)/) {
424                         $proto_tree_item[12] = trim($2);
425                 } elsif ($str =~ /val_to_str_ext(_const)?\([^\,]*\,([^\,]*)\,/) {
426                         $proto_tree_item[12] = trim($2);
427                 }
428
429                 #field type
430                 if (scalar @args > 5) {
431                         if ($args[5] =~ /tvb_get_guint8/) {
432                                 if ($args[4] =~ /%[0-9]*[di]/) {
433                                         $proto_tree_item[9] = "FT_INT8";
434                                 } else {
435                                         $proto_tree_item[9] = "FT_UINT8";
436                                 }
437                         } elsif ($args[5] =~ /tvb_get_(n|"le")tohs/) {
438                                 if ($args[4] =~ /%[0-9]*[di]/) {
439                                         $proto_tree_item[9] = "FT_INT16";
440                                 } else {
441                                         $proto_tree_item[9] = "FT_UINT16";
442                                 }
443                         } elsif ($args[5] =~ /tvb_get_(n|"le")toh24/) {
444                                 if ($args[4] =~ /%[0-9]*[di]/) {
445                                         $proto_tree_item[9] = "FT_INT24";
446                                 } else {
447                                         $proto_tree_item[9] = "FT_UINT24";
448                                 }
449                         } elsif ($args[5] =~ /tvb_get_(n|"le")tohl/) {
450                                 if ($args[4] =~ /%[0-9]*[di]/) {
451                                         $proto_tree_item[9] = "FT_INT32";
452                                 } else {
453                                         $proto_tree_item[9] = "FT_UINT32";
454                                 }
455                         } elsif ($args[5] =~ /tvb_get_(n|"le")toh("40"|"48"|"56"|"64")/) {
456                                 if ($args[4] =~ /%[0-9]*[di]/) {
457                                         $proto_tree_item[9] = "FT_INT64";
458                                 } else {
459                                         $proto_tree_item[9] = "FT_UINT64";
460                                 }
461                         } elsif (($args[5] =~ /tvb_get_(n|"le")tohieee_float/) ||
462                                          ($args[4] =~ /%[0-9\.]*[fFeEgG]/)) {
463                                 $proto_tree_item[9] = "FT_FLOAT";
464                         } elsif ($args[5] =~ /tvb_get_(n|"le")tohieee_double/) {
465                                 $proto_tree_item[9] = "FT_DOUBLE";
466                         } elsif (($args[5] =~ /tvb_get_ipv4/) ||
467                                          ($args[5] =~ /tvb_ip_to_str/)) {
468                                 $proto_tree_item[9] = "FT_IPv4";
469                         } elsif (($args[5] =~ /tvb_get_ipv6/) ||
470                                          ($args[5] =~ /tvb_ip6_to_str/)) {
471                                 $proto_tree_item[9] = "FT_IPv6";
472                         } elsif ($args[5] =~ /tvb_get_(n|"le")tohguid/) {
473                                 $proto_tree_item[9] = "FT_GUID";
474                         } elsif ($args[5] =~ /tvb_get_ephemeral_stringz/) {
475                                 $proto_tree_item[9] = "FT_STRINGZ";
476                         } elsif (($args[5] =~ /tvb_get_ephemeral_string/) || 
477                                          ($args[5] =~ /tvb_format_text/)){
478                                 $proto_tree_item[9] = "FT_STRING";
479                         } elsif ($args[5] =~ /tvb_bytes_to_str/) {
480                                 $proto_tree_item[9] = "FT_BYTES";
481                         } elsif ($args[5] =~ /tvb_ether_to_str/) {
482                                 $proto_tree_item[9] = "FT_ETHER";
483                         } elsif ($args[5] =~ /tvb_vines_addr_to_str/) {
484                                 $proto_tree_item[9] = "FT_VINES";
485                         }                       
486                 }
487
488                 #display base
489                 if ($args[4] =~ /%[0-9]*[xX]/) {
490                         $proto_tree_item[11] = "BASE_HEX";
491                 } elsif ($args[4] =~ /%[0-9]*[uld]/) {
492                         $proto_tree_item[11] = "BASE_DEC";
493                 } elsif ($args[4] =~ /%[0-9]*o/) {
494                         $proto_tree_item[11] = "BASE_OCT";
495                 }
496                 if ($str =~ /val_to_str_ext(_const)?\([^\,]*\,([^\,]*)\,/) {
497                         $proto_tree_item[11] .= "|BASE_EXT_STRING";
498                 }
499
500                 push(@proto_tree_list, \@proto_tree_item);
501
502                 $num_items += 1;
503         }
504
505         if ($num_items > 0) {
506                 open(FCO, ">", $fileName . ".proto_tree_input");
507                 for my $item (@proto_tree_list) {
508                         print FCO join(";", @{$item}), "\n";
509                 }
510                 close(FCO);
511         }
512 }
513
514 # ---------------------------------------------------------------------
515 # Find all proto_tree_add_text calls and replace them with the data
516 # found in proto_tree_list
517 sub fix_proto_tree_add_text {
518         my( $fileContentsRef, $fileName) = @_;
519         my $found = 0;
520         my $pat;
521
522         if ($expert ne "") {
523                 $pat = qr /
524                                         (
525                                                  (?:proto_tree_add_text)\s* \(
526                                                  (([^[\,;])*\,){4,}
527                                                  [^;]*
528                                                  \s* \) \s* ;
529                                         )
530                                 /xs;
531         } else {
532                 $pat = qr /
533                                         (
534                                                  (?:proto_tree_add_text)\s* \(
535                                                  (([^[\,;])*\,){5,}
536                                                  [^;]*
537                                                  \s* \) \s* ;
538                                         )
539                                 /xs;
540         }
541
542         $$fileContentsRef =~ s/ $pat /patsub($found, $1)/xges;
543 }
544
545 # ---------------------------------------------------------------------
546 # Format proto_tree_add_item or expert info functions with proto_tree_list data
547 sub patsub {
548         my $item_str;
549         if ($proto_tree_list[$_[0]][0] eq "1") {
550                 $item_str = sprintf("proto_tree_add_item(%s, %s, %s, %s, %s, %s);",
551                                                  $proto_tree_list[$_[0]][2], $proto_tree_list[$_[0]][3],
552                                                  $proto_tree_list[$_[0]][4], $proto_tree_list[$_[0]][5],
553                                                  $proto_tree_list[$_[0]][6], $proto_tree_list[$_[0]][7]);
554         } elsif ($proto_tree_list[$_[0]][0] eq "10") {
555                 $item_str = sprintf("expert_add_info(pinfo, %s, &%s);",
556                                                  $proto_tree_list[$_[0]][2], $proto_tree_list[$_[0]][3]);
557         } elsif ($proto_tree_list[$_[0]][0] eq "11") {
558                 $item_str = sprintf("expert_add_info_format(pinfo, %s, &%s, \"%s\"",
559                                                  $proto_tree_list[$_[0]][2], $proto_tree_list[$_[0]][3],
560                                                  $proto_tree_list[$_[0]][8]);
561                 if ($proto_tree_list[$_[0]][11] ne "") {
562                         $item_str .= ", $proto_tree_list[$_[0]][11]";
563                 }
564                 $item_str .= ");";
565         } elsif ($proto_tree_list[$_[0]][0] eq "12") {
566                 $item_str = sprintf("proto_tree_add_expert(%s, pinfo, &%s, %s, %s, %s);",
567                                                  $proto_tree_list[$_[0]][2], $proto_tree_list[$_[0]][3],
568                                                  $proto_tree_list[$_[0]][4], $proto_tree_list[$_[0]][5],
569                                                  $proto_tree_list[$_[0]][6]);
570         } elsif ($proto_tree_list[$_[0]][0] eq "13") {
571                 $item_str = sprintf("proto_tree_add_expert_format(%s, pinfo, &%s, %s, %s, %s, \"%s\"",
572                                                  $proto_tree_list[$_[0]][2], $proto_tree_list[$_[0]][3],
573                                                  $proto_tree_list[$_[0]][4], $proto_tree_list[$_[0]][5],
574                                                  $proto_tree_list[$_[0]][6], $proto_tree_list[$_[0]][8]);
575                 if ($proto_tree_list[$_[0]][11] ne "") {
576                         $item_str .= ", $proto_tree_list[$_[0]][11]";
577                 }
578                 $item_str .= ");";
579         } else {
580                 $item_str = $1;
581         }
582
583         $_[0] += 1;
584
585         return $item_str;
586 }
587
588 # ---------------------------------------------------------------------
589 # Output the hf variable declarations.  For now, write them to a file.
590 # XXX - Eventually find the right place to add it to the modified dissector file
591 sub output_hf {
592         my( $fileName) = @_;
593         my %hfs = ();
594         my %eis = ();
595         my $index;
596         my $key;
597
598         #add hfs to hash table to prevent against (accidental) duplicates
599         for ($index=0;$index<@proto_tree_list;$index++) {
600                 if ($proto_tree_list[$index][1] eq "1") {
601                         $hfs{$proto_tree_list[$index][3]} = $proto_tree_list[$index][3];
602                 } elsif ($proto_tree_list[$index][1] eq "2") {
603                         $eis{$proto_tree_list[$index][3]} = $proto_tree_list[$index][3];
604                 }
605         }
606
607         open(FCO, ">", $fileName . ".hf");
608
609         print FCO "/* Generated from convert_proto_tree_add_text.pl */\n";
610
611         foreach $key (keys %hfs) {
612                 print FCO "static int $key = -1;\n";
613         }
614
615         if (scalar keys %hfs > 0) {
616                 print FCO "\n\n";
617         }
618
619         print FCO "/* Generated from convert_proto_tree_add_text.pl */\n";
620
621         foreach $key (keys %eis) {
622                 print FCO "static expert_field $key = EI_INIT;\n";
623         }
624         close(FCO);
625
626 }
627
628 # ---------------------------------------------------------------------
629 # Output the hf array items.  For now, write them to a file.
630 # XXX - Eventually find the right place to add it to the modified dissector file
631 # (bonus points if formatting of hf array in dissector file is kept)
632 sub output_hf_array {
633         my( $fileName) = @_;
634         my $index;
635         my %hfs = ();
636         my %eis = ();
637
638         open(FCO, ">", $fileName . ".hf_array");
639
640         print FCO "      /* Generated from convert_proto_tree_add_text.pl */\n";
641
642         for ($index=0;$index<@proto_tree_list;$index++) {
643                 if ($proto_tree_list[$index][1] eq "1") {
644                         if (exists($hfs{$proto_tree_list[$index][3]})) {
645                                 print "duplicate hf entry '$proto_tree_list[$index][3]' found!  Aborting conversion.\n";
646                                 exit(-1);
647                         }
648                         $hfs{$proto_tree_list[$index][3]} = $proto_tree_list[$index][3];
649                         print FCO "      { &$proto_tree_list[$index][3], { \"$proto_tree_list[$index][8]\", \"$proto_tree_list[$index][10]\", ";
650                         print FCO "$proto_tree_list[$index][9], $proto_tree_list[$index][11], $proto_tree_list[$index][12], $proto_tree_list[$index][13], NULL, HFILL }},\r\n";
651                 }
652         }
653
654         if ($index > 0) {
655                 print FCO "\n\n";
656         }
657
658         print FCO "      /* Generated from convert_proto_tree_add_text.pl */\n";
659         for ($index=0;$index<@expert_list;$index++) {
660                 if (exists($eis{$expert_list[$index][3]})) {
661                         print "duplicate ei entry '$expert_list[$index][3]' found!  Aborting conversion.\n";
662                         exit(-1);
663                 }
664                 $eis{$expert_list[$index][3]} = $expert_list[$index][3];
665
666                 print FCO "      { &$expert_list[$index][3], { \"$expert_list[$index][10]\", $expert_list[$index][7], ";
667                 print FCO "$expert_list[$index][9], \"$expert_list[$index][8]\", EXPFILL }},\r\n";
668         }
669
670         close(FCO);
671 }
672
673 # ---------------------------------------------------------------------
674 # Find all proto_tree_add_text calls that have parameters passed in them
675 # and output number found
676
677 sub find_all {
678         my( $fileContentsRef, $fileName) = @_;
679
680         my $found = 0;
681         my $pat;
682
683         if ($expert ne "") {
684                 $pat = qr /
685                                         (
686                                                  (?:proto_tree_add_text)\s* \(
687                                                  (([^[\,;])*\,){4,}
688                                                  [^;]*
689                                                  \s* \) \s* ;
690                                         )
691                                 /xs;
692         } else {
693                 $pat = qr /
694                                         (
695                                                  (?:proto_tree_add_text)\s* \(
696                                                  (([^[\,;])*\,){5,}
697                                                  [^;]*
698                                                  \s* \) \s* ;
699                                         )
700                                 /xs;
701         }
702
703         while ($$fileContentsRef =~ / $pat /xgso) {
704                 my $str = "${1}\n";
705                 $str =~ tr/\t\n\r/ /d;
706                 $str =~ s/ \s+ / /xg;
707                 #print "$fileName: $str\n";
708
709                 $found += 1;
710         }
711
712         return $found;
713 }