#!/usr/local/bin/perl -- -*- Perl -*- # Written by: # Matz Kindahl (matkin@docs.uu.se) require 'getopts.pl'; sub Usage { "Usage : $pname [ -cdhilnsv ] [ -e exp ] [ -f file ] [ strings ] [ file ] ...\n"; } ($pname) = ($0 =~ m|/([^/]+)/?$|); &Getopts("cdhilnpsvxe:f:"); if ($opt_f) { open(FD, $opt_f) || die "Cannot open $opt_f, stopped"; chop(@regexp = ); for (@regexp) { $_ = '(' . $_ . ')'; } $regexp = join('|',@regexp); } else { $regexp = $opt_e ? $opt_e : shift; } die &Usage unless $regexp; $regexp =~ s#/#\\/#g; $regopt = $opt_i ? "i" : ""; $regexp = "^" . $regexp . "\$" if $opt_x; $if_statement = $opt_v ? "unless" : "if"; $error_handling = $opt_s ? "" : 'print $@ if $@'; $initial_commands = '$found = 1;'; $match_commands = '$found = 0;'; if ($opt_c) { $initial_commands .= '$count = 0;'; $match_commands .= '$count++;'; $on_eof_commands = 'print ' . ($#ARGV < 1 ? "" : '"$ARGV:" . ') . '"$count\n"; $count = 0;'; } else { if ($opt_l) { $match_commands .= 'print "$ARGV\n"; next file;'; } else { if (! $opt_h && $#ARGV > 0) { $head = '$ARGV:'; } $head .= $opt_n ? '$.:' : ""; $match_commands .= 'print "' . $head . '$_";'; } } if ($opt_d) { $loop_command = 'while ($ARGV = shift)'; $initial_commands .= q[unshift(\@ARGV, '-') if \$#ARGV < 0;]; } else { local($") = (', '); unshift(@ARGV, '-') if $#ARGV < 0; for (@ARGV) { $_ = "'$_'"; } $loop_command = "for \$ARGV (@ARGV)"; } $program = <<"END"; $initial_commands file: $loop_command { if (-d \$ARGV) { warn "$pname: \$ARGV: Is a directory!\\n"; next file; } unless (open(ARGV, \$ARGV)) { warn "$pname: \$!\\n"; next file; } line: while () { do { $match_commands } $if_statement /$regexp/$regopt; $error_handling; if (eof(ARGV)) { $on_eof_commands close(ARGV); next file; } } } exit $found; END if ($opt_p) { print $program; } else { eval $program; }