home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Updates / Perl / Non-RPC / !Perl / scripts / grep < prev    next >
Text File  |  1998-11-21  |  1KB  |  60 lines

  1. #! perl -w
  2. # If you want to modify grep to do something specific see SimpleGrep.
  3. # This version of grep is written for speed not clarity.
  4. use RISCOS::Throwback 'throwback_info';
  5. use RISCOS::Filespec;
  6. use RISCOS::File 'globlist';
  7. use Getopt::Std;
  8.  
  9. use vars qw($opt_c $opt_i $opt_t $opt_v $count);
  10.  
  11. delete $SIG{'__DIE__'};
  12. delete $SIG{'__WARN__'};
  13.  
  14. if (defined $ARGV[0] and $ARGV[0] eq '-throwback') {
  15.     $throw = 1;
  16.     $pattern = shift;
  17. } else {
  18.     $throw = 0;
  19. }
  20. getopts ('icvt');
  21.  
  22. $pattern = shift @ARGV;
  23.  
  24. die "$0 [-throwback] [-c] [-v] [-i] pattern [files]" unless $pattern;
  25.  
  26. @ARGV = globlist( @ARGV );
  27.  
  28. $count = 0;
  29. $throw = 1 if $opt_t;
  30.  
  31. $lots = @ARGV > 1;
  32.  
  33. if ($lots)
  34. {
  35.     # Only print the filename if more than one
  36.     $printit = "print \"\$ARGV: \$_\";";
  37.     $printc =  "print \"\$ARGV: \$count\n\"";
  38. } else {
  39.     $printit = 'print;';
  40.     $printc =  "print \"\$count\n\"";
  41. }
  42.  
  43.  
  44. $prog = '
  45. while( <> )
  46. {
  47.     next ' . ($opt_v ? 'if' : 'unless') . '/$pattern/o' . ($opt_i ? 'i' : '') .
  48.     ';' . ($opt_c ? '$count++;' : $printit) .
  49.   
  50. ($throw ? 'chomp; throwback_info( $ARGV, $., $_ );' : '') . '
  51. } continue {
  52.   if (eof) {
  53. ' . ($opt_c ? "if (\$count) {$printc; \$count = 0; }" : '') . '
  54.     close(ARGV); # Reset line numbers for each file
  55.   }
  56. }
  57. 1';
  58.  
  59. eval $prog or die $@;    # Run the beastie
  60.