#!/tools/local/perl -w
use strict;
use vars qw($file);
use Data::Dumper;

use File::Find;
use Getopt::Std;


my %opt;
getopts("Smlce:",\%opt);
$File::Find::dont_use_nlink = 1 if $opt{'S'};

my $expr = (defined $opt{'e'}) ? $opt{'e'} : shift;
warn "Matching '$expr'\n";

my $count = 0;

sub match
{
 if (/$expr/o)
  {
   if ($opt{'l'})
    {
     print "$File::Find::name\n";
     return 1;
    }
   $count++;
   unless ($opt{'c'})
    {
     print "$File::Find::name:$.: $_"
    }
  }
 return 0;
}

sub wanted
{
 $File::Find::prune = 0;
 if (-T $_ && !/%$/ && /\.([chC]|cpp|cxx|cc)$/)
  {
   local $file   = ($_);
   local ($_);
   no strict 'refs';
   open($file,"<$file") || die "Cannot open $file:$!";
   while (<$file>)
    {
     last if &match;
    }
   close($file);
   if ($opt{'c'} && $count)
    {
     print "$File::Find::name: $count\n"
    }
  }
 elsif (-d $_)
  {
   $File::Find::prune = 1 if ($_ eq 'mTk' && $opt{'m'});
  }
}

@ARGV = '.' unless (@ARGV);

find(\&wanted,@ARGV);
