From Xiao Xiangquan:
[obnox/wireshark/wip.git] / tools / fix-encoding-args.pl
1 #!/usr/bin/perl -w
2 #
3 # Copyright 2011, William Meier <wmeier[AT]newsguy.com>
4 #
5 # A program to fix encoding args for certain Wireshark API function calls
6 #   from TRUE/FALSE to ENC_?? as appropriate (and possible)
7 #   - proto_tree_add_item
8 #   - proto_tree_add_bits_item
9 #   - proto_tree_add_bits_ret_val
10 #   - proto_tree_add_bitmask
11 #   - proto_tree_add_bitmask_text  !! ToDo: encoding arg not last arg
12 #   - tvb_get_bits
13 #   - tvb_get_bits16
14 #   - tvb_get_bits24
15 #   - tvb_get_bits32
16 #   - tvb_get_bits64
17 #   - ptvcursor_add
18 #   - ptvcursor_add_no_advance
19 #   - ptvcursor_add_with_subtree   !! ToDo: encoding arg not last arg
20 #
21 #
22 # $Id$
23 #
24 # Wireshark - Network traffic analyzer
25 # By Gerald Combs <gerald@wireshark.org>
26 # Copyright 1998 Gerald Combs
27 #
28 # This program is free software; you can redistribute it and/or
29 # modify it under the terms of the GNU General Public License
30 # as published by the Free Software Foundation; either version 2
31 # of the License, or (at your option) any later version.
32 #
33 # This program is distributed in the hope that it will be useful,
34 # but WITHOUT ANY WARRANTY; without even the implied warranty of
35 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36 # GNU General Public License for more details.
37 #
38 # You should have received a copy of the GNU General Public License
39 # along with this program; if not, write to the Free Software
40 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
41 #
42
43 use strict;
44 use Getopt::Long;
45
46 # Conversion "Requests"
47
48 # Standard conversions
49 my $searchReplaceFalseTrueHRef =
50   {
51    "FALSE"              => "ENC_BIG_ENDIAN",
52    "0"                  => "ENC_BIG_ENDIAN",
53    "TRUE"               => "ENC_LITTLE_ENDIAN",
54    "1"                  => "ENC_LITTLE_ENDIAN"
55   };
56
57 my $searchReplaceEncNAHRef =
58    {
59     "FALSE"             => "ENC_NA",
60     "0"                 => "ENC_NA",
61     "TRUE"              => "ENC_NA",
62     "1"                 => "ENC_NA",
63     "ENC_LITTLE_ENDIAN" => "ENC_NA",
64     "ENC_BIG_ENDIAN"    => "ENC_NA"
65    };
66
67
68 # ---------------------------------------------------------------------
69 # Conversion "request" structure
70 # (
71 #   [ <list of field types for which this conversion request applies> ],
72 #   { <hash of desired encoding arg conversions> }
73 # }
74
75 my @types_NA  =
76   (
77    [ qw (FT_NONE FT_BYTES FT_ETHER FT_IPv6 FT_IPXNET FT_OID)],
78    $searchReplaceEncNAHRef
79   );
80
81 my @types_INT =
82   (
83    [ qw (FT_UINT8 FT_UINT16 FT_UINT24 FT_UINT32 FT_UINT64 FT_INT8
84          FT_INT16 FT_INT24 FT_INT32 FT_INT64 FT_FLOAT FT_DOUBLE)],
85    $searchReplaceFalseTrueHRef
86   );
87
88 my @types_MISC =
89   (
90    [ qw (FT_BOOLEAN FT_IPv4 FT_GUID FT_EUI64)],
91    $searchReplaceFalseTrueHRef
92   );
93
94 my @types_STRING =
95   (
96    [qw (FT_STRING FT_STRINGZ)],
97    {
98     "FALSE"                        => "ENC_ASCII|ENC_NA",
99     "0"                            => "ENC_ASCII|ENC_NA",
100     "TRUE"                         => "ENC_ASCII|ENC_NA",
101     "1"                            => "ENC_ASCII|ENC_NA",
102     "ENC_LITTLE_ENDIAN"            => "ENC_ASCII|ENC_NA",
103     "ENC_BIG_ENDIAN"               => "ENC_ASCII|ENC_NA",
104     "ENC_NA"                       => "ENC_ASCII|ENC_NA",
105
106     "ENC_ASCII"                    => "ENC_ASCII|ENC_NA",
107     "ENC_ASCII|ENC_LITTLE_ENDIAN"  => "ENC_ASCII|ENC_NA",
108     "ENC_ASCII|ENC_BIG_ENDIAN"     => "ENC_ASCII|ENC_NA",
109
110     "ENC_UTF_8"                    => "ENC_UTF_8|ENC_NA",
111     "ENC_UTF_8|ENC_LITTLE_ENDIAN"  => "ENC_UTF_8|ENC_NA",
112     "ENC_UTF_8|ENC_BIG_ENDIAN"     => "ENC_UTF_8|ENC_NA",
113
114     "ENC_EBCDIC"                   => "ENC_EBCDIC|ENC_NA",
115     "ENC_EBCDIC|ENC_LITTLE_ENDIAN" => "ENC_EBCDIC|ENC_NA",
116     "ENC_EBCDIC|ENC_BIG_ENDIAN"    => "ENC_EBCDIC|ENC_NA",
117    }
118   );
119
120 my @types_UINT_STRING =
121   (
122    [qw (FT_UINT_STRING)],
123    {
124     "FALSE"                   => "ENC_ASCII|ENC_BIG_ENDIAN",
125     "0"                       => "ENC_ASCII|ENC_BIG_ENDIAN",
126     "TRUE"                    => "ENC_ASCII|ENC_LITTLE_ENDIAN",
127     "1"                       => "ENC_ASCII|ENC_LITTLE_ENDIAN",
128     "ENC_BIG_ENDIAN"          => "ENC_ASCII|ENC_BIG_ENDIAN",
129     "ENC_LITTLE_ENDIAN"       => "ENC_ASCII|ENC_LITTLE_ENDIAN",
130    }
131   );
132
133 my @types_REG_PROTO  =
134   (
135    [ qw (REG_PROTO)],
136    $searchReplaceEncNAHRef
137   );
138
139 # ---------------------------------------------------------------------
140 # For searching (and doing no substitutions) (obsolete ?)
141
142 my @types_TIME =  (
143                     [qw (FT_ABSOLUTE_TIME FT_RELATIVE_TIME)],
144                     {}
145                    );
146
147 my @types_ALL =
148   (
149    [qw (
150            FT_NONE
151            FT_PROTOCOL
152            FT_BOOLEAN
153            FT_UINT8
154            FT_UINT16
155            FT_UINT24
156            FT_UINT32
157            FT_UINT64
158            FT_INT8
159            FT_INT16
160            FT_INT24
161            FT_INT32
162            FT_INT64
163            FT_FLOAT
164            FT_DOUBLE
165            FT_ABSOLUTE_TIME
166            FT_RELATIVE_TIME
167            FT_STRING
168            FT_STRINGZ
169            FT_UINT_STRING
170            FT_ETHER
171            FT_BYTES
172            FT_UINT_BYTES
173            FT_IPv4
174            FT_IPv6
175            FT_IPXNET
176            FT_FRAMENUM
177            FT_PCRE
178            FT_GUID
179            FT_OID
180            FT_EUI64
181       )],
182    {# valid encoding args
183     "a"=>"ENC_NA",
184     "b"=>"ENC_LITTLE_ENDIAN",
185     "c"=>"ENC_BIG_ENDIAN",
186
187     "d"=>"ENC_ASCII|ENC_NA",
188     "e"=>"ENC_ASCII|ENC_LITTLE_ENDIAN",
189     "f"=>"ENC_ASCII|ENC_BIG_ENDIAN",
190
191     "g"=>"ENC_UTF_8|ENC_NA",
192     "h"=>"ENC_UTF_8|ENC_LITTLE_ENDIAN",
193     "i"=>"ENC_UTF_8|ENC_BIG_ENDIAN",
194
195     "j"=>"ENC_EBCDIC|ENC_NA",
196     "k"=>"ENC_EBCDIC|ENC_LITTLE_ENDIAN",
197     "l"=>"ENC_EBCDIC|ENC_BIG_ENDIAN",
198    }
199   );
200
201 # ---------------------------------------------------------------------
202
203 my @findAllFunctionList =
204 ##         proto_tree_add_bitmask_text  !! ToDo: encoding arg not last arg
205 ##         ptvcursor_add_with_subtree   !! ToDo: encoding Arg not last arg
206   qw (
207          proto_tree_add_item
208          proto_tree_add_bits_item
209          proto_tree_add_bits_ret_val
210          proto_tree_add_bitmask
211          tvb_get_bits
212          tvb_get_bits16
213          tvb_get_bits24
214          tvb_get_bits32
215          tvb_get_bits64
216          ptvcursor_add
217          ptvcursor_add_no_advance
218     );
219
220 # ---------------------------------------------------------------------
221 #
222 # MAIN
223 #
224 my $writeFlag = '';
225 my $helpFlag  = '';
226 my $action    = 'fix-all';
227
228 my $result = GetOptions(
229                         'action=s' => \$action,
230                         'write'    => \$writeFlag,
231                         'help|?'   => \$helpFlag
232                        );
233
234 if (!$result || $helpFlag || !$ARGV[0]) {
235     usage();
236 }
237
238 if (($action ne 'fix-all') && ($action ne 'find-all')) {
239     usage();
240 }
241
242 sub usage {
243         print "\nUsage: $0 [--action=fix-all|find-all] [--write] FILENAME [...]\n\n";
244         print "  --action = fix-all (default)\n";
245         print "    Fix <certain-fcn-names>() encoding arg when possible in FILENAME(s)\n";
246         print "    Fixes (if any) are listed on stdout)\n\n";
247         print "  --write     create FILENAME.encoding-arg-fixes (original file with fixes)\n";
248         print "              (effective only for fix-all)\n";
249         print "\n";
250         print "  --action = find-all\n";
251         print "    Find all occurrences of <certain-fcn-names>() statements)\n";
252         print "     highlighting the 'encoding' arg\n";
253         exit(1);
254 }
255
256 # Read through the files; fix up encoding parameter of proto_tree_add_item() calls
257 # Essentially:
258 #  For each file {
259 #  .  Create a hash of the hf_index_names & associated field types from the entries in hf[]
260 #  .  For each requested "conversion request" {
261 #  .  .  For each hf[] entry hf_index_name with a field type in a set of specified field types {
262 #  .  .  .  For each proto_tree_add_item() statement
263 #  .  .  .  .  - replace encoding arg in proto_tree_add_item(..., hf_index_name, ..., 'encoding-arg')
264 #                  specific values ith new values
265 #  .  .  .  .  - print the statement showing the change
266 #  .  .  .  }
267 #  .  .  }
268 #  .  }
269 #  .  If requested and if replacements done: write new file "orig-filename.encoding-arg-fixes"
270 #  }
271 #
272 # Note: The proto_tree_add_item() encoding arg will be converted only if
273 #        the hf_index_name referenced is in one of the entries in hf[] in the same file
274
275 my $found_total = 0;
276
277 while (my $fileName = $ARGV[0]) {
278     shift;
279     my $fileContents = '';
280
281     die "No such file: \"$fileName\"\n" if (! -e $fileName);
282
283     # delete leading './'
284     $fileName =~ s{ ^ \. / } {}xo;
285
286     # Read in the file (ouch, but it's easier that way)
287     open(FCI, "<", $fileName) || die("Couldn't open $fileName");
288     while (<FCI>) {
289         $fileContents .= $_;
290     }
291     close(FCI);
292
293     # Create a hash of the hf[] entries (name_index_name=>field_type)
294     my $hfArrayEntryFieldTypeHRef = find_hf_array_entries(\$fileContents, $fileName);
295
296     if ($action eq "fix-all") {
297
298         # Find and replace: <fcn_name_pattern>() encoding arg in $fileContents for:
299         #     - hf[] entries with specified field types;
300         #     - 'proto' as returned from proto_register_protocol()
301         my $fcn_name = "(?:proto_tree_add_item|ptvcursor_add(?:_no_advance)?)";
302         my $found = 0;
303         $found += fix_encoding_args_by_hf_type(1, \@types_NA,          $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
304         $found += fix_encoding_args_by_hf_type(1, \@types_INT,         $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
305         $found += fix_encoding_args_by_hf_type(1, \@types_MISC,        $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
306         $found += fix_encoding_args_by_hf_type(1, \@types_STRING,      $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
307         $found += fix_encoding_args_by_hf_type(1, \@types_UINT_STRING, $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
308         $found += fix_encoding_args_by_hf_type(1, \@types_REG_PROTO,   $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
309
310         # Find and replace: alters <fcn_name>() encoding arg in $fileContents
311         $found += fix_encoding_args(1, $searchReplaceFalseTrueHRef, "proto_tree_add_bits_(?:item|ret_val)",      \$fileContents, $fileName);
312         $found += fix_encoding_args(1, $searchReplaceFalseTrueHRef, "proto_tree_add_bitmask",                    \$fileContents, $fileName);
313         $found += fix_encoding_args(1, $searchReplaceFalseTrueHRef, "tvb_get_bits(?:16|24|32|64)?",              \$fileContents, $fileName);
314         $found += fix_encoding_args(1, $searchReplaceFalseTrueHRef, "tvb_get_(?:ephemeral_)?unicode_string[z]?", \$fileContents, $fileName);
315
316         # If desired and if any changes, write out the changed version to a file
317         if (($writeFlag) && ($found > 0)) {
318             open(FCO, ">", $fileName . ".encoding-arg-fixes");
319 #        open(FCO, ">", $fileName );
320             print FCO "$fileContents";
321             close(FCO);
322         }
323         $found_total += $found;
324     }
325
326     if ($action eq "find-all") {
327         # Find all proto_tree_add_item() statements
328         #  and output same highlighting the encoding arg
329         $found_total += find_all(\@findAllFunctionList, \$fileContents, $fileName);
330     }
331
332 # Optional searches: (kind of obsolete ?)
333 # search for (and output) proto_tree_add_item() statements with invalid encoding arg for specified field types
334 #    $fcn_name = "proto_tree_add_item";
335 #    fix_encoding_args(2, \@types_NA,          $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
336 #    fix_encoding_args(2, \@types_INT,         $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
337 #    fix_encoding_args(2, \@types_MISC,        $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
338 #    fix_encoding_args(2, \@types_STRING,      $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
339 #    fix_encoding_args(2, \@types_UINT_STRING, $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
340 #    fix_encoding_args(2, \@types_ALL,         $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
341 # search for (and output) proto_tree_add_item()$fcn_name,  statements with any encoding arg for specified field types
342 #    fix_encoding_args(3, \@types_TIME,        $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
343 #
344
345 } # while
346
347 exit $found_total;
348
349 # ---------------------------------------------------------------------
350 # Create a hash containing an entry (hf_index_name => field_type) for each hf[]entry.
351 # also: create an entry in the hash for the 'protocol name' variable (proto... => FT_PROTOCOL)
352 # returns: ref to the hash
353
354 sub find_hf_array_entries {
355     my ($fileContentsRef, $fileName) = @_;
356
357     # The below Regexp is based on one from:
358     # http://aspn.activestate.com/ASPN/Cookbook/Rx/Recipe/59811
359     # It is in the public domain.
360     # A complicated regex which matches C-style comments.
361     my $CCommentRegEx = qr{ / [*] [^*]* [*]+ (?: [^/*] [^*]* [*]+ )* / }xo;
362
363     # hf[] entry regex (to extract an hf_index_name and associated field type)
364     my $hfArrayFieldTypeRegEx = qr {
365                                        \{
366                                        \s*
367                                        &\s*([A-Z0-9_\[\]-]+)                # &hf
368                                        \s*,\s*
369                                        \{\s*
370                                        .+?                                  # (a bit dangerous)
371                                        \s*,\s*
372                                        (FT_[A-Z0-9_]+)                      # field type
373                                        \s*,\s*
374                                        .+?
375                                        \s*,\s*
376                                        HFILL                                # HFILL
377                                }xios;
378
379     # create a copy of $fileContents with comments removed
380     my $fileContentsWithoutComments = $$fileContentsRef;
381     $fileContentsWithoutComments =~ s {$CCommentRegEx} []xg;
382
383     # find all the hf[] entries (searching $fileContentsWithoutComments).
384     # Create a hash keyed by the hf_index_name with the associated value being the field_type
385     my %hfArrayEntryFieldType;
386     while ($fileContentsWithoutComments =~ m{ $hfArrayFieldTypeRegEx }xgis) {
387 #        print "$1 $2\n";
388         if (exists $hfArrayEntryFieldType{$1}) {
389             printf "%-35.35s: ? duplicate hf[] entry: no fixes done for: $1; manual action may be req'd\n", $fileName;
390             $hfArrayEntryFieldType{$1} = "???"; # prevent any substitutions for this hf_index_name
391         } else {
392             $hfArrayEntryFieldType{$1} = $2;
393         }
394     }
395
396     # RegEx to get "proto" variable name
397     my $protoRegEx = qr /
398                             ^ \s*                     # note m modifier below
399                             (
400                                 [a-zA-Z0-9_]+
401                             )
402                             \s*
403                             =
404                             \s*
405                             proto_register_protocol
406                             \s*
407                             \(
408                         /xoms;
409
410     # Find all registered protocols
411     while ($fileContentsWithoutComments =~ m { $protoRegEx }xgioms ) {
412         ##print "$1\n";
413         if (exists $hfArrayEntryFieldType{$1}) {
414             printf "%-35.35s: ? duplicate 'proto': no fixes done for: $1; manual action may be req'd\n", $fileName;
415             $hfArrayEntryFieldType{$1} = "???"; # prevent any substitutions for this protocol
416         } else {
417             $hfArrayEntryFieldType{$1} = "REG_PROTO";
418         }
419     }
420
421     return \%hfArrayEntryFieldType;
422 }
423
424 # ---------------------------------------------------------------------
425 # fix_encoding_args
426 # Substitute new values for the specified <fcn_name>() encoding arg values
427 #    when the encoding arg is the *last* arg of the call to fcn_name
428 # args:
429 #   substitute_flag: 1: replace specified encoding arg values by a new value (keys/values in search hash);
430 #   ref to hash containing search (keys) and replacement (values) for encoding arg
431 #   fcn_name string
432 #   ref to string containing file contents
433 #   filename string
434 #
435 { # block begin
436
437     # shared variables
438     my $fileName;
439     my $searchReplaceHRef;
440     my $found;
441
442     sub fix_encoding_args {
443         (my $subFlag, $searchReplaceHRef, my $fcn_name, my $fileContentsRef, $fileName) = @_;
444
445         my $encArgPat;
446
447         if ($subFlag == 1) {
448             # just match for <fcn_name>() statements which have an encoding arg matching one of the
449             #   keys in the searchReplace hash.
450             # Escape any "|" characters in the keys
451             #  and then create "alternatives" string containing all the values (A|B|C\|D|...)
452             $encArgPat = join "|",  map { s{ ( \| ) }{\\$1}gx; $_ } keys %$searchReplaceHRef;
453         } elsif ($subFlag == 3) {
454             # match for <fcn_name>() statements for any value of the encoding parameter
455             # IOW: find all the <fcn_name> statements
456             $encArgPat = qr / [^,)]+? /x;
457         }
458
459         # build the complete pattern
460         my $patRegEx = qr /
461                               ( # part 1: $1
462                                   (?:^|=)            # don't try to handle fcn_name call when arg of another fcn call
463                                   \s*
464                                   $fcn_name \s* \(
465                                   [^;]+?             # a bit dangerous
466                                   ,\s*
467                               )
468                               ( # part 2: $2
469                                   $encArgPat
470                               )
471                               ( # part 3: $3
472                                   \s* \)
473                                   \s* ;
474                               )
475                           /xms;  # m for ^ above
476
477         ##print "$patRegEx\n";
478
479         ## Match and substitute as specified
480         $found = 0;
481
482         $$fileContentsRef =~ s/ $patRegEx /patsubx($1,$2,$3)/xges;
483
484         return $found;
485     }
486
487     # Called from fix_encoding_args to determine replacement string when a regex match is encountered
488     #  $_[0]: part 1
489     #  $_[1]: part 2: encoding arg
490     #  $_[2]: part 3
491     #  lookup the desired replacement value for the encoding arg
492     #  print match string showing and highlighting the encoding arg replacement
493     #  return "replacement" string
494     sub patsubx {
495         $found += 1;
496         my $substr = exists $$searchReplaceHRef{$_[1]} ? $$searchReplaceHRef{$_[1]} : "???";
497         my $str = sprintf("%s[[%s]-->[%s]]%s", $_[0], $_[1], $substr,  $_[2]);
498         $str =~ tr/\t\n\r/ /d;
499         printf "%s: $str\n", $fileName;
500         return $_[0] . $substr . $_[2];
501     }
502 } # block end
503
504 # ---------------------------------------------------------------------
505 # fix_encoding_args_by_hf_type
506 #
507 # Substitute new values for certain proto_tree_add_item() encoding arg
508 #     values (for specified hf field types)
509 #  Variants: search for and display for "exceptions" to allowed encoding arg values;
510 #            search for and display all encoding arg values
511 # args:
512 #   substitute_flag: 1: replace specified encoding arg values by a new value (keys/values in search hash);
513 #                    2: search for "exceptions" to allowed encoding arg values (values in search hash);
514 #                    3: search for all encoding arg values
515 #   ref to array containing two elements:
516 #      - ref to array containing hf[] types to be processed (FT_STRING, etc)
517 #      - ref to hash containing search (keys) and replacement (values) for encoding arg
518 #   fcn_name string
519 #   ref to hfArrayEntries hash (key: hf name; value: field type)
520 #   ref to string containing file contents
521 #   filename string
522
523 {  # block begin
524
525 # shared variables
526     my $fileName;
527     my $searchReplaceHRef;
528     my $found;
529     my $hf_field_type;
530
531     sub fix_encoding_args_by_hf_type {
532
533         (my $subFlag, my $mapArg, my $fcn_name, my $fileContentsRef, my $hfArrayEntryFieldTypeHRef, $fileName) = @_;
534
535         my $hf_index_name;
536         my $hfTypesARef;
537         my $encArgPat;
538
539         $hfTypesARef       = $$mapArg[0];
540         $searchReplaceHRef = $$mapArg[1];
541
542         my %hfTypes;
543         @hfTypes{@$hfTypesARef}=();
544
545         # set up the encoding arg match pattern
546         if ($subFlag == 1) {
547             # just match for <fcn_name>() statements which have an encoding arg matching one of the
548             #   keys in the searchReplace hash.
549             # Escape any "|" characters in the keys
550             #  and then create "alternatives" string containing all the values (A|B|C\|D|...)
551             $encArgPat = join "|",  map { s{ ( \| ) }{\\$1}gx; $_ } keys %$searchReplaceHRef;
552         } elsif ($subFlag == 2) {
553             # Find all the <fcn_name>() statements wherein the encoding arg is a value other than
554             #      one of the "replace" values.
555             #  Uses zero-length negative-lookahead to find <fcn_name>() statements for which the encoding
556             #    arg is something other than one of the the provided replace values.
557             # Escape any "|" characters in the values to be matched
558             #  and then create "alternatives" string containing all the values (A|B|C\|D|...)
559             my $match_str = join "|",  map { s{ ( \| ) }{\\$1}gx; $_ } values %$searchReplaceHRef;
560             $encArgPat = qr /
561                                 (?!                  # negative zero-length look-ahead
562                                     \s*
563                                     (?: $match_str ) # alternatives we don't want to match
564                                     \s*
565                                 )
566                                 [^,)]+?              # OK: enoding arg is other than one of the alternatives:
567                                                      #   match to end of the arg
568                             /x;
569         } elsif ($subFlag == 3) {
570             # match for <fcn_name>() statements for any value of the encoding parameter
571             # IOW: find all the proto_tree_add_item statements with an hf entry of the desired types
572             $encArgPat = qr / [^,)]+? /x;
573         }
574
575         # For each hf[] entry which matches a type in %hfTypes do replacements
576         $found = 0;
577         foreach my $key (keys %$hfArrayEntryFieldTypeHRef) {
578             $hf_index_name = $key;
579             $hf_index_name =~ s{ ( \[ | \] ) }{\\$1}xg;     # escape any "[" or "]" characters
580             $hf_field_type = $$hfArrayEntryFieldTypeHRef{$key};
581             ##printf "--> %-35.35s: %s\n", $hf_index_name,  $hf_field_type;
582
583             next unless exists $hfTypes{$hf_field_type};    # Do we want to process for this hf[] entry type ?
584
585             # build the complete pattern
586             my $patRegEx = qr /
587                                   ( # part 1: $1
588                                       $fcn_name \s* \(
589                                       [^;]+?
590                                       ,\s*
591                                       $hf_index_name
592                                       \s*,
593                                       [^;]+
594                                       ,\s*
595                                   )
596                                   ( # part 2: $2
597                                       $encArgPat
598                                   )
599                                   ( # part 3: $3
600                                       \s* \)
601                                       \s* ;
602                                   )
603                               /xs;
604
605             ##print "\n$hf_index_name $hf_field_type\n";
606
607             ## Match and substitute as specified
608             $$fileContentsRef =~ s/ $patRegEx /patsub($1,$2,$3)/xges;
609
610         }
611
612         return $found;
613     }
614
615     # Called from fix_encoding_args to determine replacement string when a regex match is encountered
616     #  $_[0]: part 1
617     #  $_[1]: part 2: encoding arg
618     #  $_[2]: part 3
619     #  lookup the desired replacement value for the encoding arg
620     #  print match string showing and highlighting the encoding arg replacement
621     #  return "replacement" string
622     sub patsub {
623         $found += 1;
624         my $substr = exists $$searchReplaceHRef{$_[1]} ? $$searchReplaceHRef{$_[1]} : "???";
625         my $str = sprintf("%s[[%s]-->[%s]]%s", $_[0], $_[1], $substr,  $_[2]);
626         $str =~ tr/\t\n\r/ /d;
627         printf "%s:  %-17.17s $str\n", $fileName, $hf_field_type . ":";
628         return $_[0] . $substr . $_[2];
629     }
630 }  # block end
631
632 # ---------------------------------------------------------------------
633 # Find all <fcnList> statements
634 #  and output same highlighting the encoding arg
635 #  Currently: encoding arg is matched as the *last* arg of the function call
636
637 sub find_all {
638     my( $fcnListARef, $fileContentsRef, $fileName) = @_;
639
640     my $found = 0;
641     my $fcnListPat = join "|", @$fcnListARef;
642     my $pat = qr /
643                      (
644                          (?:$fcnListPat) \s* \(
645                          [^;]+
646                          , \s*
647                      )
648                      (
649                          [^ \t,)]+?
650                      )
651                      (
652                          \s* \)
653                          \s* ;
654                      )
655                  /xs;
656
657     while ($$fileContentsRef =~ / $pat /xgso) {
658         my $str = "${1}[[${2}]]${3}\n";
659         $str =~ tr/\t\n\r/ /d;
660         $str =~ s/ \s+ / /xg;
661         print "$fileName: $str\n";
662         $found += 1;
663     }
664     return $found;
665 }
666