| 1 | =head1 NAME | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | IPC::Filter - filter data through an external process | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | use IPC::Filter qw(filter); | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | $compressed_data = filter($data, "bzip2"); | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | The C<filter> function provided by this module passes data through an | ||||||||||||||||||||||||||||
| 14 | external command, thus providing filtering in non-pipeline situations. | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | package IPC::Filter; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | { use 5.006; } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use Errno 1.00 qw(EPIPE); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | use IPC::Open3 1.01 qw(open3); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 2 | ▶ | use IPC::Signal 1.00 qw(sig_name); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | use IO::Handle 1.12; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 2 | ▶ | use IO::Poll 0.01 qw(POLLIN POLLOUT POLLERR POLLHUP); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 2 | ▶ | use POSIX qw(_exit); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 2 | ▶ | use Symbol qw(gensym); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 2 | our $VERSION = "0.004"; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | ▶ | use parent "Exporter"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 2 | our @EXPORT_OK = qw(filter); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =over | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =item filter(DATA, SHELL_COMMAND) | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =item filter(DATA, PROGRAM, ARGS ...) | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | The SHELL_COMMAND, or the PROGRAM with ARGS if more arguments are | ||||||||||||||||||||||||||||
| 46 | supplied, is executed as a separate process. (The arguments other | ||||||||||||||||||||||||||||
| 47 | than DATA are ultimately passed to C<exec>; see L<perlfunc(1)/exec> | ||||||||||||||||||||||||||||
| 48 | for explanation of the choice between the two forms.) The DATA (which | ||||||||||||||||||||||||||||
| 49 | must be either a simple string or a reference to a string) is supplied | ||||||||||||||||||||||||||||
| 50 | to the process on its standard input, and the process's standard output | ||||||||||||||||||||||||||||
| 51 | is captured and returned (as a simple string). | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | If the process exits with a non-zero exit code or on a signal, the | ||||||||||||||||||||||||||||
| 54 | function will C<die>. In the case of a non-zero exit code, the C<die> | ||||||||||||||||||||||||||||
| 55 | message will duplicate the process's standard error output; in any other | ||||||||||||||||||||||||||||
| 56 | case, the error output is discarded. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 2 | my $chunksize = 4096; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | sub filter($@) { | ||||||||||||||||||||||||||||
| 63 | 9 | ▶ | my $data = \shift(@_); | ||||||||||||||||||||||||||
|
Subroutine
filter: called CC=21 SCAR=32.4
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | 9 | ▶ | if(@_ == 0 || $_[0] eq "-") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ == 0 or $_[0] eq "-"
MC/DC100%2/2
@_ == 0 or $_[0] eq "-"
@_ == 0
$_[0] eq "-"
Truth table100%3/3
A or B
A= @_ == 0B= $_[0] eq "-"
| |||||||||||||||||||||||||||||
| 65 | 2 | die "filter: invalid command\n"; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 7 | ▶ | if(ref($data) eq "REF") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | $data = $$data; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 7 | my $stdin = gensym; | |||||||||||||||||||||||||||
| 71 | 7 | my $stdout = gensym; | |||||||||||||||||||||||||||
| 72 | 7 | my $stderr = gensym; | |||||||||||||||||||||||||||
| 73 | # Note: perl bug (bug in IPC::Open3 version 1.0106, bug ID | ||||||||||||||||||||||||||||
| 74 | # #32198): if the exec fails in the subprocess created by open3(), | ||||||||||||||||||||||||||||
| 75 | # it uses die() to emit its error message and terminate. If an | ||||||||||||||||||||||||||||
| 76 | # exception handler is installed using eval {}, execution in the | ||||||||||||||||||||||||||||
| 77 | # subprocess continues there instead of the process terminating. | ||||||||||||||||||||||||||||
| 78 | # We avoid nastiness by catching the exception ourselves and | ||||||||||||||||||||||||||||
| 79 | # doing the right thing. | ||||||||||||||||||||||||||||
| 80 | 7 | my $parent_pid = $$; | |||||||||||||||||||||||||||
| 81 | 7 | my $child_pid = eval { local $SIG{__DIE__}; | |||||||||||||||||||||||||||
| 82 | 7 | open3($stdin, $stdout, $stderr, @_); | |||||||||||||||||||||||||||
| 83 | }; | ||||||||||||||||||||||||||||
| 84 | 7 | ▶ | if($@ ne "") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | my $err = $@; | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | die $err if $$ == $parent_pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | print STDERR $err; | |||||||||||||||||||||||||||
| 88 | 0 | _exit 255; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 7 | local $SIG{PIPE} = "IGNORE"; | |||||||||||||||||||||||||||
| 91 | 7 | my $poll = IO::Poll->new; | |||||||||||||||||||||||||||
| 92 | 7 | my $datalen = length($$data); | |||||||||||||||||||||||||||
| 93 | 7 | ▶ | if($datalen == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | $stdin->close; | |||||||||||||||||||||||||||
| 95 | } else { | ||||||||||||||||||||||||||||
| 96 | 7 | $poll->mask($stdin => POLLOUT | POLLERR | POLLHUP); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 7 | $poll->mask($stdout => POLLIN | POLLERR | POLLHUP); | |||||||||||||||||||||||||||
| 99 | 7 | $poll->mask($stderr => POLLIN | POLLERR | POLLHUP); | |||||||||||||||||||||||||||
| 100 | 7 | my $datapos = 0; | |||||||||||||||||||||||||||
| 101 | 7 | my @out; | |||||||||||||||||||||||||||
| 102 | my @err; | ||||||||||||||||||||||||||||
| 103 | 7 | ▶ | while($poll->handles) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 21 | $poll->poll; | |||||||||||||||||||||||||||
| 105 | 21 | ▶ | if($datapos != $datalen && $poll->events($stdin)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$datapos != $datalen and $poll->events($stdin)
MC/DC50%1/2
$datapos != $datalen and $poll->events($stdin)
$datapos != $datalen
$poll->events($stdin)
Truth table66%2/3
A and B
A= $datapos != $datalenB= $poll->events($stdin)
| |||||||||||||||||||||||||||||
| 106 | 7 | my $n = $stdin->syswrite($$data, $chunksize, $datapos); | |||||||||||||||||||||||||||
| 107 | 7 | ▶ | if(defined $n) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 108 | 7 | $datapos += $n; | |||||||||||||||||||||||||||
| 109 | } elsif($! == EPIPE) { | ||||||||||||||||||||||||||||
| 110 | 0 | $datapos = $datalen; | |||||||||||||||||||||||||||
| 111 | } else { | ||||||||||||||||||||||||||||
| 112 | 0 | die "filter: stdin: $!\n"; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 7 | ▶ | if($datapos == $datalen) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 7 | $poll->remove($stdin); | |||||||||||||||||||||||||||
| 116 | 7 | $stdin->close; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 21 | ▶ | if($poll->events($stdout)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 12 | my $output; | |||||||||||||||||||||||||||
| 121 | 12 | ▶ | unless(defined $stdout->sysread($output, $chunksize)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | die "filter: stdout: $!\n"; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 12 | ▶ | if($output eq "") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 7 | $poll->remove($stdout); | |||||||||||||||||||||||||||
| 126 | } else { | ||||||||||||||||||||||||||||
| 127 | 5 | push @out, $output; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 21 | ▶ | if($poll->events($stderr)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 9 | my $output; | |||||||||||||||||||||||||||
| 132 | 9 | ▶ | unless(defined $stderr->sysread($output, $chunksize)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | die "filter: stderr: $!\n"; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 9 | ▶ | if($output eq "") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 7 | $poll->remove($stderr); | |||||||||||||||||||||||||||
| 137 | } else { | ||||||||||||||||||||||||||||
| 138 | 2 | push @err, $output; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | 7 | waitpid $child_pid, 0; | |||||||||||||||||||||||||||
| 143 | 7 | my $status = $?; | |||||||||||||||||||||||||||
| 144 | 7 | ▶ | if($status == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 4 | return join("", @out); | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | 3 | ▶ | if($status & 127) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 1 | die "filter: process died on SIG".sig_name($status & 127)."\n"; | |||||||||||||||||||||||||||
| 149 | } else { | ||||||||||||||||||||||||||||
| 150 | 2 | die join("", "filter: process exited with status ", | |||||||||||||||||||||||||||
| 151 | $status >> 8, "\n", @err); | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =back | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | L<IPC::Open2> | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | Andrew Main (Zefram) <zefram@fysh.org> | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Copyright (C) 2004, 2007, 2010, 2011 | ||||||||||||||||||||||||||||
| 168 | Andrew Main (Zefram) <zefram@fysh.org> | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | This module is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 173 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =cut | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 2 | 1; | |||||||||||||||||||||||||||