| 1 | # IO::Pipe.pm | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Copyright (c) 1996-8 Graham Barr <gbarr@pobox.com>. All rights reserved. | ||||||||||||||||||||||||||||
| 4 | # This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 5 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | package IO::Pipe; | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use 5.008_001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use Symbol; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 4 | our $VERSION = "1.55"; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 8 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 8 | my $type = shift; | |||||||||||||||||||||||||||
| 20 | 8 | ▶ | my $class = ref($type) || $type || "IO::Pipe"; | ||||||||||||||||||||||||||
|
Condition40%2/5
ref $type || $type || 'IO::Pipe'
ref $type or $type
MC/DC0%0/2
ref $type || $type || 'IO::Pipe'
ref $type
$type
Truth table33%1/3
A or B
A= ref $typeB= $type
| |||||||||||||||||||||||||||||
| 21 | 8 | ▶ | @_ == 0 || @_ == 2 or croak "usage: $class->([READFH, WRITEFH])"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ == 0 or @_ == 2
MC/DC0%0/2
@_ == 0 or @_ == 2
@_ == 0
@_ == 2
Truth table33%1/3
A or B
A= @_ == 0B= @_ == 2
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 8 | my $me = bless gensym(), $class; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 8 | ▶ | my($readfh,$writefh) = @_ ? @_ : $me->handles; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 8 | ▶ | pipe($readfh, $writefh) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | or return undef; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 8 | @{*$me} = ($readfh, $writefh); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 8 | $me; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 8 | ▶ | sub handles { | ||||||||||||||||||||||||||
|
Subroutine
handles: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 8 | ▶ | @_ == 1 or croak 'usage: $pipe->handles()'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 8 | (IO::Pipe::End->new(), IO::Pipe::End->new()); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 4 | ▶ | my $do_spawn = $^O eq 'os2' || $^O eq 'MSWin32'; | ||||||||||||||||||||||||||
|
Condition33%1/3
$^O eq 'os2' || $^O eq 'MSWin32'
MC/DC0%0/2
$^O eq 'os2' || $^O eq 'MSWin32'
$^O eq 'os2'
$^O eq 'MSWin32'
Truth table33%1/3
A || B
A= $^O eq 'os2'B= $^O eq 'MSWin32'
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 5 | ▶ | sub _doit { | ||||||||||||||||||||||||||
|
Subroutine
_doit: called CC=16 SCAR=40.9
| |||||||||||||||||||||||||||||
| 43 | 5 | my $me = shift; | |||||||||||||||||||||||||||
| 44 | 5 | my $rw = shift; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 5 | ▶ | my $pid = $do_spawn ? 0 : fork(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 5 | ▶ | if($pid) { # Parent | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 49 | 3 | return $pid; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | elsif(defined $pid) { # Child or spawn | ||||||||||||||||||||||||||||
| 52 | 2 | my $fh; | |||||||||||||||||||||||||||
| 53 | 2 | ▶ | my $io = $rw ? \*STDIN : \*STDOUT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 2 | ▶ | my ($mode, $save) = $rw ? "r" : "w"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 2 | ▶ | if ($do_spawn) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | require Fcntl; | |||||||||||||||||||||||||||
| 57 | 0 | $save = IO::Handle->new_from_fd($io, $mode); | |||||||||||||||||||||||||||
| 58 | 0 | my $handle = shift; | |||||||||||||||||||||||||||
| 59 | # Close in child: | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | unless ($^O eq 'MSWin32') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | fcntl($handle, Fcntl::F_SETFD(), 1) or croak "fcntl: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | $fh = $rw ? ${*$me}[0] : ${*$me}[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | } else { | ||||||||||||||||||||||||||||
| 65 | 2 | shift; | |||||||||||||||||||||||||||
| 66 | 2 | ▶ | $fh = $rw ? $me->reader() : $me->writer(); # close the other end | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 2 | bless $io, "IO::Handle"; | |||||||||||||||||||||||||||
| 69 | 2 | $io->fdopen($fh, $mode); | |||||||||||||||||||||||||||
| 70 | 2 | $fh->close; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 2 | ▶ | if ($do_spawn) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | $pid = eval { system 1, @_ }; # 1 == P_NOWAIT | |||||||||||||||||||||||||||
| 74 | 0 | my $err = $!; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | $io->fdopen($save, $mode); | |||||||||||||||||||||||||||
| 77 | 0 | ▶ | $save->close or croak "Cannot close $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | croak "IO::Pipe: Cannot spawn-NOWAIT: $err" if not $pid or $pid < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $pid or $pid < 0
MC/DC0%0/2
not $pid or $pid < 0
not $pid
$pid < 0
Truth table0%0/3
not(A) or B
A= not $pidB= $pid < 0
| |||||||||||||||||||||||||||||
| 79 | 0 | return $pid; | |||||||||||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||||||||||
| 81 | 2 | ▶ | exec @_ or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | croak "IO::Pipe: Cannot exec: $!"; | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||||||||||
| 86 | 0 | croak "IO::Pipe: Cannot fork: $!"; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # NOT Reached | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 5 | ▶ | sub reader { | ||||||||||||||||||||||||||
|
Subroutine
reader: called CC=7 SCAR=19.8
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 5 | ▶ | @_ >= 1 or croak 'usage: $pipe->reader( [SUB_COMMAND_ARGS] )'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 5 | my $me = shift; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | return undef | ||||||||||||||||||||||||||||
| 97 | 5 | ▶ | unless(ref($me) || ref($me = $me->new)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $me or ref($me = $me->new)
MC/DC0%0/2
ref $me or ref($me = $me->new)
ref $me
ref($me = $me->new)
Truth table33%1/3
A or B
A= ref $meB= ref($me = $me->new)
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 5 | my $fh = ${*$me}[0]; | |||||||||||||||||||||||||||
| 100 | 5 | my $pid; | |||||||||||||||||||||||||||
| 101 | 5 | ▶ | $pid = $me->_doit(0, $fh, @_) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | if(@_); | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 4 | close ${*$me}[1]; | |||||||||||||||||||||||||||
| 105 | 4 | bless $me, ref($fh); | |||||||||||||||||||||||||||
| 106 | 4 | *$me = *$fh; # Alias self to handle | |||||||||||||||||||||||||||
| 107 | 4 | ▶ | $me->fdopen($fh->fileno,"r") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | unless defined($me->fileno); | ||||||||||||||||||||||||||||
| 109 | 4 | bless $fh; # Really wan't un-bless here | |||||||||||||||||||||||||||
| 110 | 4 | ▶ | ${*$me}{'io_pipe_pid'} = $pid | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | if defined $pid; | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 4 | $me; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 5 | ▶ | sub writer { | ||||||||||||||||||||||||||
|
Subroutine
writer: called CC=7 SCAR=19.8
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 5 | ▶ | @_ >= 1 or croak 'usage: $pipe->writer( [SUB_COMMAND_ARGS] )'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 5 | my $me = shift; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | return undef | ||||||||||||||||||||||||||||
| 121 | 5 | ▶ | unless(ref($me) || ref($me = $me->new)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $me or ref($me = $me->new)
MC/DC0%0/2
ref $me or ref($me = $me->new)
ref $me
ref($me = $me->new)
Truth table33%1/3
A or B
A= ref $meB= ref($me = $me->new)
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 5 | my $fh = ${*$me}[1]; | |||||||||||||||||||||||||||
| 124 | 5 | my $pid; | |||||||||||||||||||||||||||
| 125 | 5 | ▶ | $pid = $me->_doit(1, $fh, @_) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | if(@_); | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 4 | close ${*$me}[0]; | |||||||||||||||||||||||||||
| 129 | 4 | bless $me, ref($fh); | |||||||||||||||||||||||||||
| 130 | 4 | *$me = *$fh; # Alias self to handle | |||||||||||||||||||||||||||
| 131 | 4 | ▶ | $me->fdopen($fh->fileno,"w") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | unless defined($me->fileno); | ||||||||||||||||||||||||||||
| 133 | 4 | bless $fh; # Really wan't un-bless here | |||||||||||||||||||||||||||
| 134 | 4 | ▶ | ${*$me}{'io_pipe_pid'} = $pid | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | if defined $pid; | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 4 | $me; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | package IO::Pipe::End; | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 4 | our(@ISA); | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 4 | @ISA = qw(IO::Handle); | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 8 | ▶ | sub close { | ||||||||||||||||||||||||||
|
Subroutine
close: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 147 | 8 | my $fh = shift; | |||||||||||||||||||||||||||
| 148 | 8 | my $r = $fh->SUPER::close(@_); | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 3 | waitpid(${*$fh}{'io_pipe_pid'},0) | |||||||||||||||||||||||||||
| 151 | 8 | ▶ | if(defined ${*$fh}{'io_pipe_pid'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 8 | $r; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 4 | 1; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | __END__ | ||||||||||||||||||||||||||||