| 1 | # Copyrights 2011-2020 by [Mark Overmeer <markov@cpan.org>]. | ||||||||||||
| 2 | # For other contributors see ChangeLog. | ||||||||||||
| 3 | # See the manual pages for details on the licensing terms. | ||||||||||||
| 4 | # Pod stripped from pm file by OODoc 2.02. | ||||||||||||
| 5 | # This code is part of distribution IOMux. Meta-POD processed with OODoc | ||||||||||||
| 6 | # into POD and HTML manual-pages. See README.md | ||||||||||||
| 7 | # Copyright Mark Overmeer. Licensed under the same terms as Perl itself. | ||||||||||||
| 8 | |||||||||||||
| 9 | package IOMux::Pipe::Read; | ||||||||||||
| 10 | 5 | ▶ | use vars '$VERSION'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 5 | $VERSION = '1.01'; | |||||||||||
| 12 | |||||||||||||
| 13 | 5 | ▶ | use base 'IOMux::Handler::Read'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | 5 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 5 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | |||||||||||||
| 18 | 5 | ▶ | use Log::Report 'iomux'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 5 | ▶ | use Fcntl; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 5 | ▶ | use POSIX qw/:errno_h :sys_wait_h/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | 5 | ▶ | use File::Basename 'basename'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | |||||||||||||
| 23 | |||||||||||||
| 24 | sub init($) | ||||||||||||
| 25 | 2 | ▶ | { my ($self, $args) = @_; | ||||||||||
|
Subroutine
init: called CC=9 SCAR=22.8
Pod
undocumented
| |||||||||||||
| 26 | my $command = $args->{command} | ||||||||||||
| 27 | 2 | ▶ | or error __x"no command to run specified in {pkg}", pkg => __PACKAGE__; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 28 | |||||||||||||
| 29 | 2 | ▶ | my ($cmd, @cmdopts) = ref $command eq 'ARRAY' ? @$command : $command; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 30 | 2 | my $name = $args->{name} = (basename $cmd)."|"; | |||||||||||
| 31 | |||||||||||||
| 32 | 2 | my ($rh, $wh); | |||||||||||
| 33 | 2 | ▶ | pipe $rh, $wh | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 34 | or fault __x"cannot create pipe for {cmd}", cmd => $name; | ||||||||||||
| 35 | |||||||||||||
| 36 | 2 | my $pid = fork; | |||||||||||
| 37 | 2 | ▶ | defined $pid | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | or fault __x"failed to fork for pipe {cmd}", cmd => $name; | ||||||||||||
| 39 | |||||||||||||
| 40 | 2 | ▶ | if($pid==0) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | { # client | ||||||||||||
| 42 | 1 | close $rh; | |||||||||||
| 43 | 1 | open STDIN, '<', File::Spec->devnull; | |||||||||||
| 44 | 1 | ▶ | open STDOUT, '>&', $wh | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | or fault __x"failed to redirect STDOUT for pipe {cmd}", cmd=>$name; | ||||||||||||
| 46 | 1 | open STDERR, '>', File::Spec->devnull; | |||||||||||
| 47 | |||||||||||||
| 48 | 1 | ▶ | exec $cmd, @cmdopts | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 49 | or fault __x"failed to exec for pipe {cmd}", cmd => $name; | ||||||||||||
| 50 | } | ||||||||||||
| 51 | |||||||||||||
| 52 | # parent | ||||||||||||
| 53 | |||||||||||||
| 54 | 1 | $self->{IMPR_pid} = $pid; | |||||||||||
| 55 | 1 | ▶ | $args->{read_size} ||= 4096; # Unix typical BUFSIZ | ||||||||||
|
Condition50%1/2
$args->{"read_size"} ||= 4096
MC/DC0%0/1
$args->{"read_size"} ||= 4096
$args->{"read_size"}
Truth table50%1/2
A
A= $args->{"read_size"}
| |||||||||||||
| 56 | |||||||||||||
| 57 | 1 | close $wh; | |||||||||||
| 58 | 1 | fcntl $rh, F_SETFL, O_NONBLOCK; | |||||||||||
| 59 | 1 | $args->{fh} = $rh; | |||||||||||
| 60 | |||||||||||||
| 61 | 1 | $self->SUPER::init($args); | |||||||||||
| 62 | 1 | $self; | |||||||||||
| 63 | } | ||||||||||||
| 64 | |||||||||||||
| 65 | |||||||||||||
| 66 | sub bare($%) | ||||||||||||
| 67 | 2 | ▶ | { my ($class, %args) = @_; | ||||||||||
|
Subroutine
bare: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||
| 68 | 2 | my $self = bless {}, $class; | |||||||||||
| 69 | |||||||||||||
| 70 | 2 | my ($rh, $wh); | |||||||||||
| 71 | 2 | ▶ | pipe $rh, $wh | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 72 | or fault __x"cannot create bare pipe reader"; | ||||||||||||
| 73 | |||||||||||||
| 74 | 2 | ▶ | $args{read_size} ||= 4096; # Unix typical BUFSIZ | ||||||||||
|
Condition50%1/2
$args{"read_size"} ||= 4096
MC/DC0%0/1
$args{"read_size"} ||= 4096
$args{"read_size"}
Truth table50%1/2
A
A= $args{"read_size"}
| |||||||||||||
| 75 | |||||||||||||
| 76 | 2 | fcntl $rh, F_SETFL, O_NONBLOCK; | |||||||||||
| 77 | 2 | $args{fh} = $rh; | |||||||||||
| 78 | |||||||||||||
| 79 | 2 | $self->SUPER::init(\%args); | |||||||||||
| 80 | 2 | ($self, $wh); | |||||||||||
| 81 | } | ||||||||||||
| 82 | |||||||||||||
| 83 | |||||||||||||
| 84 | sub open($$@) | ||||||||||||
| 85 | 0 | ▶ | { my ($class, $mode, $cmd) = (shift, shift, shift); | ||||||||||
|
Subroutine
open: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 86 | 0 | ▶ | ref $cmd eq 'ARRAY' | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 87 | ? $class->new(command => $cmd, mode => $mode, @_) | ||||||||||||
| 88 | : $class->new(command => [$cmd, @_] , mode => $mode); | ||||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | #------------------- | ||||||||||||
| 92 | |||||||||||||
| 93 | 0 | ▶ | sub mode() {shift->{IMPR_mode}} | ||||||||||
|
Subroutine
mode: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 94 | 0 | ▶ | sub childPid() {shift->{IMPR_pid}} | ||||||||||
|
Subroutine
childPid: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 95 | |||||||||||||
| 96 | #------------------- | ||||||||||||
| 97 | |||||||||||||
| 98 | sub close($) | ||||||||||||
| 99 | 2 | ▶ | { my ($self, $cb) = @_; | ||||||||||
|
Subroutine
close: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 100 | my $pid = $self->{IMPR_pid} | ||||||||||||
| 101 | 2 | ▶ | or return $self->SUPER::close($cb); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 102 | |||||||||||||
| 103 | 1 | waitpid $pid, WNOHANG; | |||||||||||
| 104 | 1 | local $?; | |||||||||||
| 105 | 1 | $self->SUPER::close($cb); | |||||||||||
| 106 | } | ||||||||||||
| 107 | |||||||||||||
| 108 | 5 | 1; | |||||||||||