-#!/usr/bin/perl
+#!/usr/bin/perl -l
=head1 NAME
-cgrep - grep the output of a command, only printing stdout/stderr if match
+curiouscat - grep the output of a command, only printing stdout/stderr if match
=head1 SYNOPSIS
-cgrep [-v] PATTERN COMMAND...
+cgrep [-v] [-f PATTERNFILE | PATTERN ] COMMAND...
=head1 DESCRIPTION
-cgrep runs a command, buffering the output from both standard out and
-standard error, looking for a matching pattern. If a matching pattern
-is found, stdout and stderr are printed and cgrep exits with the exit
-code of COMMAND.
+curiouscat runs a command, buffering the output from both standard out
+and standard error, looking for a matching pattern. If a matching
+pattern is found, stdout and stderr are printed and curiouscat exits
+with the exit code of COMMAND.
-If the -v switch is passed, cgrep switches into reverse mode where it
-will print if there are any lines not matching PATTERN.
+If the -v switch is passed, curiouscat switches into reverse mode
+where it will print if there are any lines not matching PATTERN.
=head1 AUTHOR
-Copyright 2010 by Collabora Limited, written by Tollef Fog Heen
+Copyright 2010, 2013 by Collabora Limited, written by Tollef Fog Heen
<tollef.fog.heen@collabora.co.uk>
Licensed under the GNU GPL.
$|=1;
my $reverse = 0;
+my @patterns = ();
+my $patternfile;
use Getopt::Long;
-GetOptions("v" => \$reverse) || die "usage: cgrep [-v] PATTERN COMMAND...\n";
+GetOptions("v" => \$reverse,
+ "f=s" => \$patternfile) || die "usage: cgrep [-v] [-f PATTERNFILE | PATTERN ] COMMAND...\n";
-my $pattern = shift @ARGV;
+if (defined $patternfile) {
+ my $p;
+ open $p, "<", $patternfile or die "Can't open $patternfile: $!";
+ chomp(@patterns = <$p>);
+} else {
+ @patterns = (shift @ARGV);
+}
my @command = @ARGV;
my ($in, $out, $err);
$h->finish;
if ($reverse) {
- if ($out !~ /$pattern/m and $err !~ /$pattern/m) {
- print STDOUT $out;
- print STDERR $err;
+ for my $line (split(/$\//, $out), split(/$\//, $err)) {
+ my $nomatch = 1;
+ for my $p (@patterns) {
+ if ($line =~ /$p/m ) {
+ $nomatch = 0;
+ last;
+ }
+ }
+ if ($nomatch) {
+ print STDOUT $out;
+ print STDERR $err;
+ last;
+ }
}
} else {
- if ($out =~ /$pattern/m or $err =~ /$pattern/m) {
- print STDOUT $out;
- print STDERR $err;
+ for my $p (@patterns) {
+ if ($out =~ /$p/m or $err =~ /$p/m) {
+ print STDOUT $out;
+ print STDERR $err;
+ }
}
}
exit $h->result or 0;