]> err.no Git - moreutils/blob - curiouscat
Update name one more place
[moreutils] / curiouscat
1 #!/usr/bin/perl -l
2
3 =head1 NAME
4
5 curiouscat - grep the output of a command, only printing stdout/stderr if match
6
7 =head1 SYNOPSIS
8
9 cgrep [-v] [-f PATTERNFILE | PATTERN ] COMMAND...
10
11 =head1 DESCRIPTION
12
13 curiouscat runs a command, buffering the output from both standard out
14 and standard error, looking for a matching pattern.  If a matching
15 pattern is found, stdout and stderr are printed and curiouscat exits
16 with the exit code of COMMAND.
17
18 If the -v switch is passed, curiouscat switches into reverse mode
19 where it will print if there are any lines not matching PATTERN.
20
21 =head1 AUTHOR
22
23 Copyright 2010, 2013 by Collabora Limited, written by Tollef Fog Heen
24 <tollef.fog.heen@collabora.co.uk>
25
26 Licensed under the GNU GPL.
27
28 =cut
29
30 use warnings;
31 use strict;
32 use IPC::Run qw( start pump finish timeout );
33
34 $|=1;
35
36 my $reverse = 0;
37 my @patterns = ();
38 my $patternfile;
39 use Getopt::Long;
40 GetOptions("v" => \$reverse,
41         "f=s" => \$patternfile) || die "usage: curiouscat [-v] [-f PATTERNFILE | PATTERN ]  COMMAND...\n";
42
43 if (defined $patternfile) {
44         my $p;
45         open $p, "<", $patternfile or die "Can't open $patternfile: $!";
46         chomp(@patterns = <$p>);
47 } else {
48         @patterns = (shift @ARGV);
49 }
50 my @command = @ARGV;
51
52 my ($in, $out, $err);
53 my $h = IPC::Run::start \@command, \*STDIN, \$out, \$err;
54
55 $h->finish;
56
57 if ($reverse) {
58         for my $line (split(/$\//, $out), split(/$\//, $err)) {
59                 my $nomatch = 1;
60                 for my $p (@patterns) {
61                         if ($line =~ /$p/m ) {
62                                 $nomatch = 0;
63                                 last;
64                         }
65                 }
66                 if ($nomatch) {
67                         print STDOUT $out;
68                         print STDERR $err;
69                         last;
70                 }
71         }
72 } else {
73         for my $p (@patterns) {
74                 if ($out =~ /$p/m or $err =~ /$p/m) {
75                         print STDOUT $out;
76                         print STDERR $err;
77                 }
78         }
79 }
80 exit $h->result or 0;