Advertisement
samv

Grep for PowerBuilder Code

May 16th, 2014
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6.19 KB | None | 0 0
  1. # pbgrep.pl - Grep like utility for a PoweBuilder Application
  2. # Usage: pbgrep.pl ....
  3.  
  4. # Author: Sam Varadarajan
  5. # Date: 12/01/2009
  6. # Purpose: Script to grep PB export files to find patterns; Each find will show the function name and line #s.
  7.  
  8. # Modification History:
  9. #
  10. # Copyright, Sam Varadarajan, 2009 - 2014
  11.  
  12. #
  13. # Globals
  14. #
  15. use vars qw/ %opt /;
  16.  
  17. init();
  18. my $ptrn = $opt{'p'};
  19. my $dir = '';
  20. my $verbos = 0;
  21. $verbos = 1 if $opt{'v'};
  22. my $file_name = '';
  23.  
  24. if ($opt{'o'})
  25. {
  26.     $file_name = $opt{'o'};
  27.    
  28.     #oa for automatic file name
  29.     if (!$file_name || $file_name eq "a")
  30.     {
  31.         $file_name = "search_".$ptrn.".txt" ;
  32.     }
  33.     $file_name =~ s/ /_/g;
  34.     $file_name =~ s/[^a-zA-Z0-9_.\-]*//g; #derive the name from the pattern
  35. }
  36. else {
  37.     $file_name = '-'; #stdout      
  38. }
  39.  
  40. open FILE, ">$file_name";
  41.  
  42. #my $bare = 0;
  43. # see below $bare = 1 if $opt{'b'};
  44.  
  45. my $format = "REPORT"; # default Report
  46. $format = uc($opt{'F'}) if $opt{'F'};
  47.  
  48. my $delim = "\t";
  49. if ($format eq "CSV")
  50. {
  51.     $delim = ",";
  52. }
  53. elsif ($format eq "TAB")
  54. {
  55.     $delim = "\t" ;
  56. }
  57. elsif ($format eq "LIST")
  58. {
  59.     $delim = "\t" ;
  60. }
  61.  
  62. usage() if (!$ptrn);
  63.  
  64. print $ptrn."\n";
  65.  
  66. $dir = $opt{'d'} if $opt{'d'};
  67. if (!$dir)
  68. {
  69.     $dir = 'Z:\\';
  70. }
  71.  
  72. if ($ptrn)
  73. {
  74.         process_dir($dir, \&process_file, $ptrn);
  75. }
  76.  
  77. close(FILE);
  78. #end of main
  79.  
  80. sub process_dir {
  81.     my ($path, $code_ref, $ptrn) = @_;
  82.     my ($DH, $file);
  83.     #debug print $path;
  84.     if (-d $path) {
  85.         unless (opendir $DH, $path) {
  86.             warn "Couldn't open $path.\n$!\nSkipping!";
  87.             return;
  88.         }
  89.         while ($file = readdir $DH) {
  90.                 #DEBUG print "processing $file\n";
  91.             next if $file eq '.' || $file eq '..';
  92.             #recursion.
  93.             process_dir("$path/$file", $code_ref, $ptrn);
  94.         }
  95.     }
  96.     elsif (-f $path && (grep /\.sr[u|w|f]/, $path))
  97.     {
  98. #DEBUG      print $path." ".$ptrn."\n";
  99.         $code_ref->($path, $ptrn);
  100.     }
  101. }
  102.  
  103. sub process_file()
  104. {
  105.     my $path = @_[0];
  106.     my $ptrn = @_[1];
  107.     my $proto_done = 0;
  108.     my $curr_func = "";
  109.     my @words = ();
  110.     my $end_proto = 0;
  111.     my $func_line_nbr = 0;
  112.     my $file_line_nbr = 0;
  113.     my @lines = ();
  114.     my $in_proto = 0;
  115.     my $cnt = 0;
  116.    
  117.     open (SRFILE, $path)
  118.     or die "$0: Cannot open $path: $!\n";
  119.     #DEBUG  print "debug*** ".$path." ".$ptrn."\n";
  120.  
  121.     @lines = ();
  122.  
  123.     # print "processing $path\n";
  124.  
  125.     # this assumes every file will have forward prototypes
  126.     while (<SRFILE>) {
  127.         $file_line_nbr++;
  128.         next unless ($_ =~ /forward prototypes/);
  129.         $proto_found = 1;
  130.         last;
  131.     }
  132.     if ($proto_found)
  133.     {
  134.         while (<SRFILE>) {
  135.             $file_line_nbr++;
  136.             next unless ($_ =~ /end prototypes/);
  137.             last;
  138.         }
  139.     }
  140.    
  141.     LOOP1: while (<SRFILE>)
  142.     {
  143.         chomp;
  144.         s/\t+/   /g;
  145.         s/^[ ]*//g;
  146.         $file_line_nbr++;
  147.        
  148.         @words = split(/\s+/);
  149.  
  150. #       if (((/public/i || /protected/i || /private/i) &&  /function/i || /subroutine/i) || (/event/i))
  151. # SV, 3/17, a line with     inv_log.Event ue_closelogfile() caused it to think it was inside an event. Added ^ to event check.
  152. # REDTAG, may be we will have to treat them separately and also, what if there was a function statement or event that
  153. # was commented out?
  154.  
  155.         if (((@words[0] =~ /public/i || @words[0] =~ /protected/i || @words[0] =~ /private/i) &&  
  156.                 (@words[1] =~ /function/i || @words[1] =~ /subroutine/i)) ||
  157.             (@words[0] =~ /^event/i   && (@words[1] !~ /Post/i && @words[1] !~ /Trigger/i)))
  158.         {
  159.             if (@words[0] =~ /^event/i)
  160.             {
  161.                 $curr_func = @words[1];
  162.             }
  163.             else {
  164.                 $curr_func = @words[3];
  165.             }
  166. #           print "found func header@words[3]\n";
  167.             $func_line_nbr = 0;
  168.             next LOOP1;
  169.         }
  170.         if ($_ =~ /^[ |\t]*end function/i || $_ =~ /^[ |\t]*end event/i)
  171.         {
  172.             if ($cnt && ($format eq "LIST"))
  173.             {
  174.                     $path =~ s/sr.[ ]*$//g;
  175.                     print FILE $path.$curr_func."\n";
  176.             } else {
  177.                 if (@lines)
  178.                 {
  179.                     my $count = scalar(@lines);
  180.                     foreach (my $i=0; $i < $count; $i++)
  181.                     {
  182.                         my $li = $lines[$i];
  183.                         #DEBUG print "*****".{$lines[i]}."\n";
  184.                         if ($format eq "REPORT")
  185.                         {
  186.                             if ($ptrn eq $curr_func)
  187.                             {
  188.                                 print FILE "<<Function defition>>\n";
  189.                             }
  190.                             print FILE $path."(".$li->{'FILE_LINE'}."):\n   ".$curr_func."(".$li->{'FUNC_LINE'}."):\n      ".$li->{'LINE'}."\n\n";
  191.                         }
  192.                         else {
  193.                             print FILE $path.$delim.$curr_func.$delim.$li->{'FILE_LINE'}.$delim.$li->{'FUNC_LINE'}.$delim.$li->{'LINE'}."\n";
  194.                         }
  195.                     }
  196.                     #VERBOSE print "****# of items found = ".scalar(@lines)."\n\n";
  197.                 }  
  198.                 else {
  199.                     # VERBOSE print $path."\t".$curr_func." No match found*******\n\n";
  200.                 }
  201.             }
  202.             $curr_func = "";
  203.             $end_proto = 0;
  204.             $func_line_nbr = 0;
  205.             @lines = ();
  206.             $cnt = 0;
  207.             next LOOP1;
  208.         }
  209.         # all other lines
  210.         $func_line_nbr++;
  211.         if ($_ =~ /$ptrn/ig) {
  212.             $cnt++;
  213.             my $line_info = {};
  214.             #if (!$bare)
  215.             if ($format ne "LIST")
  216.             {
  217.                 # bare format - list of functions (obj.func)
  218.                 $line_info->{'FILE_LINE'} = $file_line_nbr;
  219.                 $line_info->{'FUNC_LINE'} = $func_line_nbr;
  220.                 $line_info->{'LINE'} = $_;
  221.             }
  222.             push(@lines, $line_info);
  223.         }
  224.         next LOOP1;
  225.     }
  226.     close(SRFILE);
  227. }
  228.  
  229. #
  230. # Command line options processing
  231. #
  232. sub init()
  233. {
  234.     use Getopt::Std;
  235.     my $opt_string = 'hvbd:p:F:o:';
  236.     getopts( "$opt_string", \%opt ) or usage();
  237. #   use Getopt::Long;
  238. #   GetOptions("h"=>\$opt{'h'},
  239. #       "v"=>\$opt{'v'},
  240. #       "b"=>\$opt{'b'},
  241. #       "optional:o"=>\$opt{'o'},
  242. #       "d"=> \$opt{'d'},
  243. #       "p"=> \$opt{'p'},
  244. #       "F"=> \$opt{'F'}
  245. #   );  
  246.  
  247.     usage() if $opt{'h'};
  248. }
  249.  
  250. #
  251. # Message about this program and how to use it
  252. #
  253. sub usage()
  254. {
  255.   print STDERR << "EOF";
  256.  
  257.     This program does...
  258.  
  259.     usage: $0 [-hv] [-d dir] [-p pattern] [-F TAB|CSV|REPORT|LIST*] [-o<filename>|-oa]
  260.  
  261.     -h          :   this (help) message
  262.     -d dir      :   Directory to search in
  263.     -p <pattern>:   Pattern to search
  264.    
  265.     -F <format>:    Output can be TAB or CSV delimited or LIST or Report (default)
  266.                         LIST outputs object name and function name where pattern was found.
  267.                        
  268.     -o <filename>: Optional, if missing, the output will be sent screen (STDOUT)
  269.     -oa             :   Optional, when present, pattern is used for name of the file (with special chars removed).
  270.    
  271.     example: $0 -v -d Z:\LA -p <pattern>
  272.     If <pattern> has spaces or is a regular expression, enclose it in double quotes
  273. EOF
  274.     exit;
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement