#!/usr/bin/perl

=head1 NAME
grepl - perl version of grep 

=head1 SYNOPSIS
grepl <RE> [file]...

=head1 DESCRIPTION
This is a perl-coded version of the usual Unix grep(1) command. Its advantage
is  that  the <RE> regular expression arg is a perl pattern.  If there are no
files listed on the command line, we read from STDIN, of course.

Which version of perl is used to do the  match  depends  on  which  you  have
installed in /usr/bin/perl (or /usr/local/bin/perl on some systems). 

=head1 OPTIONS
We recognize the grep -i and -l options.

=head1 SEE ALSO
	grep(1), egrep(1)

=head1 AUTHOR
  John Chambers <jc@trillian.mit.edu> 1999
=cut

$| = 1;
($me = $0) =~ s".*/"";
$V = $ENV{"V_$me"} || $ENV{"D_$me"} ||1;	# Verbose/debug setting
$ic = '';
while ($ARGV[0] =~ /^-(.*)$/) {	# Look for a couple of grep options
	$opt = $1; shift;
	while ($opt =~ s/^(.)//) {
		$o = lc($1);
		if ($o eq 'l') {
			$pathonly = 1;
		} elsif ($o eq 'i') {
			$ic = 'i';
		} else {
			print STDERR "$0: Unknown option '$o' ignored.\n";
		}
	}
}
$re = shift || die "Usage: $0 <RE> file..\n";
print "$me: re='$re'\n" if $V>1;
file:
for $file (@ARGV) {
	print "$file ...\n" if $V>1;
	unless (open(F,$file)) {
		print STDERR "$0: Can't read \"$file\"\n";
		next file;
	}
	line:
	for $l (<F>) {
		if (eval("\$l =~ m/\$re/$ic")) {
			if ($pathonly) {
				print "$file\n";
				next file;
			} else {
				print $file . ':' . $l;
			}
		}
	}
}
