Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grep: allow repeated -e and -f #757

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 38 additions & 25 deletions bin/grep
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use File::Spec;
use File::Temp qw();
use Getopt::Std;

our $VERSION = '1.006';
our $VERSION = '1.007';

$| = 1; # autoflush output

Expand Down Expand Up @@ -216,39 +216,52 @@ sub parse_args {
unshift @ARGV, $_;
}

# multiple -e/-f options
my @tmparg;
while (@ARGV) {
my $arg = shift @ARGV;
if ($arg eq '-e') {
$pattern = shift @ARGV;
usage() unless defined $pattern;
push @patterns, $pattern;
}
elsif ($arg eq '-f') {
my $file = shift @ARGV;
usage() unless defined $file;
die "$Me: $file: is a directory\n" if -d $file;
my $fh;
open($fh, '<', $file) or die "$Me: Can't open '$file': $!\n";
my $line;
while (defined($line = <$fh>)) {
chomp $line;
$pattern = $line;
push @patterns, $pattern;
}
close $fh;

}
else {
push @tmparg, $arg;
}
}
@ARGV = @tmparg;

$opt{'p'} = $opt{'P'} = ''; # argument to print()
getopts('inCcwsxvHhe:f:Ll1gurpP:aqTF', \%opt) or usage();

$opt{'l'} = 0 if $opt{'L'};
my $no_re = $opt{F} || ( $Me =~ /\bfgrep\b/ );

if (defined $opt{'f'}) { # -f patfile
my $path = $opt{'f'};
my $patfile;
die "$Me: $path: is a directory\n" if -d $path;
open($patfile, '<', $path) or die "$Me: Can't open '$path': $!\n";

# make sure each pattern in file is valid
while ( defined( $pattern = <$patfile> ) ) {
chomp $pattern;
unless ($no_re) {
eval { 'foo' =~ /$pattern/, 1 }
or die "$Me: $path: $.: bad pattern: $@\n";
}
push @patterns, $pattern;
}
close $patfile;
}
else { # make sure pattern is valid
$pattern = $opt{'e'};
$pattern = shift(@ARGV) unless length $pattern;
usage() unless defined $pattern;
unless ($no_re) {
unless (length $pattern) {
$pattern = shift @ARGV;
push @patterns, $pattern;
}
unless ($no_re) {
foreach $pattern (@patterns) {
eval { 'foo' =~ /$pattern/, 1 }
or die "$Me: bad pattern: $@\n";
}
@patterns = ($pattern);
}
}
if ($opt{'H'}) {
$Mult = 1;
}
Expand Down
Loading