blib/lib/IO/Pty/Easy.pm

filter: Statementstmt 88.2% 165 / 187 Branchbran 69.1% 47 / 68 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 95.0% 19 / 20 Podpod 100% 10 / 10 totaltotal 82.3% 243 / 295 CC 37 SCAR 20.9
CC
37
Cov
82%
spawn
20.0
__ANON__
12.0
is_active
8.0
CRAP 8.1
↑ Summary
1 package IO::Pty::Easy;
2 15 our $AUTHORITY = 'cpan:DOY';
3 15 $IO::Pty::Easy::VERSION = '0.10';
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: Easy interface to IO::Pty
7  
8 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 15 ▶use base 'IO::Pty';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15  
16 16 ▶sub new {
Subroutine new: called CC=5 SCAR=16.2
Pod documented
17 16 my $class = shift;
18 16 my %args = @_;
19  
20 16 my $handle_pty_size = 1;
21 $handle_pty_size = delete $args{handle_pty_size}
22 16 ▶ if exists $args{handle_pty_size};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"handle_pty_size"} 0 16 16
23 16 ▶ $handle_pty_size = 0 unless POSIX::isatty(*STDIN);
Branch: 1 branch
BranchTrueFalseTotal
unless POSIX::isatty(*STDIN) 16 0 16
24 16 my $def_max_read_chars = 8192;
25 $def_max_read_chars = delete $args{def_max_read_chars}
26 16 ▶ if exists $args{def_max_read_chars};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"def_max_read_chars"} 0 16 16
27 16 my $raw = 1;
28 $raw = delete $args{raw}
29 16 ▶ if exists $args{raw};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"raw"} 0 16 16
30  
31 16 my $self = $class->SUPER::new(%args);
32 16 bless $self, $class;
33 16 $self->handle_pty_size($handle_pty_size);
34 16 $self->def_max_read_chars($def_max_read_chars);
35 16 ${*{$self}}{io_pty_easy_raw} = $raw;
36 16 ${*{$self}}{io_pty_easy_final_output} = '';
37 16 ${*{$self}}{io_pty_easy_did_handle_pty_size} = 0;
38  
39 16 return $self;
40 }
41  
42  
43 20 ▶sub spawn {
Subroutine spawn: called CC=12 SCAR=30.0
Pod documented
44 20 my $self = shift;
45 20 my $slave = $self->slave;
46  
47 20 ▶ croak "Attempt to spawn a subprocess when one is already running"
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_active 0 20 20
48 if $self->is_active;
49  
50 # set up a pipe to use for keeping track of the child process during exec
51 20 my ($readp, $writep);
52 20 ▶ unless (pipe($readp, $writep)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (pipe $readp, $writep) 0 20 20
53 0 croak "Failed to create a pipe";
54 }
55 20 $writep->autoflush(1);
56  
57 # fork a child process
58 # if the exec fails, signal the parent by sending the errno across the pipe
59 # if the exec succeeds, perl will close the pipe, and the sysread will
60 # return due to EOF
61 20 ${*{$self}}{io_pty_easy_pid} = fork;
62 20 ▶ unless ($self->pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->pid) 8 12 20
63 8 close $readp;
64 8 $self->make_slave_controlling_terminal;
65 8 close $self;
66 8 ▶ $slave->clone_winsize_from(\*STDIN) if $self->handle_pty_size;
Branch: 1 branch
BranchTrueFalseTotal
if $self->handle_pty_size 0 8 8
67 8 ▶ $slave->set_raw if ${*{$self}}{io_pty_easy_raw};
Branch: 1 branch
BranchTrueFalseTotal
if do { *{$self;} }->{"io_pty_easy_raw"} 8 0 8
68 # reopen the standard file descriptors in the child to point to the
69 # pty rather than wherever they have been pointing during the script's
70 # execution
71 8 ▶ open(STDIN, '<&', $slave->fileno)
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<&", $slave->fileno or carp("Couldn't reopen STDIN for reading") 8 0 8
72 or carp "Couldn't reopen STDIN for reading";
73 8 ▶ open(STDOUT, '>&', $slave->fileno)
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $slave->fileno or carp("Couldn't reopen STDOUT for writing") 8 0 8
74 or carp "Couldn't reopen STDOUT for writing";
75 8 ▶ open(STDERR, '>&', $slave->fileno)
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $slave->fileno or carp("Couldn't reopen STDERR for writing") 8 0 8
76 or carp "Couldn't reopen STDERR for writing";
77 8 close $slave;
78 8 { exec(@_) };
79 0 print $writep $! + 0;
80 0 carp "Cannot exec(@_): $!";
81 0 exit 1;
82 }
83  
84 12 close $writep;
85 12 $self->close_slave;
86 # this sysread will block until either we get an EOF from the other end of
87 # the pipe being closed due to the exec, or until the child process sends
88 # us the errno of the exec call after it fails
89 12 my $errno;
90 12 my $read_bytes = sysread($readp, $errno, 256);
91 12 ▶ unless (defined $read_bytes) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $read_bytes) 0 12 12
92 # XXX: should alarm here and follow up with SIGKILL if the process
93 # refuses to die
94 0 kill TERM => $self->pid;
95 0 close $readp;
96 0 $self->_wait_for_inactive;
97 0 croak "Cannot sync with child: $!";
98 }
99 12 close $readp;
100 12 ▶ if ($read_bytes > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read_bytes > 0) 1 11 12
101 1 $errno = $errno + 0;
102 1 $self->_wait_for_inactive;
103 1 croak "Cannot exec(@_): $errno";
104 }
105  
106 11 ▶ if ($self->handle_pty_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->handle_pty_size) 0 11 11
107 0 my $weakself = weaken($self);
108 0 ▶ $SIG{WINCH} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
109 0 ▶ return unless $weakself;
Branch: 1 branch
BranchTrueFalseTotal
unless $weakself 0 0 0
110 0 $weakself->slave->clone_winsize_from(\*STDIN);
111 0 ▶ kill WINCH => $weakself->pid if $weakself->is_active;
Branch: 1 branch
BranchTrueFalseTotal
if $weakself->is_active 0 0 0
112 0 };
113 0 ${*{$self}}{io_pty_easy_did_handle_pty_size} = 1;
114 }
115 }
116  
117  
118 7 ▶sub read {
Subroutine read: called CC=6 SCAR=20.1
Pod documented
119 7 my $self = shift;
120 7 my ($timeout, $max_chars) = @_;
121 7 ▶ $max_chars ||= $self->def_max_read_chars;
Condition33%1/3
$max_chars ||= $self->def_max_read_chars
l!l&&r!l&&!r
070
MC/DC0%0/2
$max_chars ||= $self->def_max_read_chars
$max_chars $self->def_max_read_chars
Truth table33%1/3
A ||= B
A= $max_charsB= $self->def_max_read_chars
ABresult
1X1
011
000
122  
123 7 my $rin = '';
124 7 vec($rin, fileno($self), 1) = 1;
125 7 my $nfound = select($rin, undef, undef, $timeout);
126 7 my $buf;
127 7 ▶ if ($nfound > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nfound > 0) 4 3 7
128 4 my $nchars = sysread($self, $buf, $max_chars);
129 4 ▶ $buf = '' if defined($nchars) && $nchars == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $nchars and $nchars == 0 0 4 4
Condition33%1/3
defined $nchars and $nchars == 0
!ll&&!rl&&r
040
MC/DC0%0/2
defined $nchars and $nchars == 0
defined $nchars $nchars == 0
Truth table33%1/3
A and B
A= defined $ncharsB= $nchars == 0
ABresult
0X0
100
111
130 }
131 7 ▶ if (length(${*{$self}}{io_pty_easy_final_output}) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length do { *{$self;} }->{"io_pty_easy_final_output"} > 0) 0 7 7
132 15 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
133 0 $buf = ${*{$self}}{io_pty_easy_final_output} . $buf;
134 0 ${*{$self}}{io_pty_easy_final_output} = '';
135 }
136 7 return $buf;
137 }
138  
139  
140 8 ▶sub write {
Subroutine write: called CC=2 SCAR=6.9
Pod documented
141 8 my $self = shift;
142 8 my ($text, $timeout) = @_;
143  
144 8 my $win = '';
145 8 vec($win, fileno($self), 1) = 1;
146 8 my $nfound = select(undef, $win, undef, $timeout);
147 8 my $nchars;
148 8 ▶ if ($nfound > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nfound > 0) 6 2 8
149 6 $nchars = syswrite($self, $text);
150 }
151 7 return $nchars;
152 }
153  
154  
155 86 ▶sub is_active {
Subroutine is_active: called CC=8 SCAR=20.8
Pod documented
156 86 my $self = shift;
157  
158 86 ▶ return 0 unless defined $self->pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->pid 48 38 86
159  
160 38 ▶ if (defined(my $fd = fileno($self))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $fd = fileno $self)) 30 8 38
161 # XXX FreeBSD 7.0 will not allow a session leader to exit until the
162 # kernel tty output buffer is empty. Make it so.
163 30 my $rin = '';
164 30 vec($rin, $fd, 1) = 1;
165 30 my $nfound = select($rin, undef, undef, 0);
166 30 ▶ if ($nfound > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nfound > 0) 8 22 30
167 8 sysread($self, ${*{$self}}{io_pty_easy_final_output},
168 $self->def_max_read_chars,
169 8 length ${*{$self}}{io_pty_easy_final_output});
170 }
171 }
172  
173 38 my $active = kill 0 => $self->pid;
174 38 ▶ if ($active) {
Branch: 1 branch
BranchTrueFalseTotal
if ($active) 38 0 38
175 38 my $pid = waitpid($self->pid, POSIX::WNOHANG());
176 38 ▶ $active = 0 if $pid == $self->pid;
Branch: 1 branch
BranchTrueFalseTotal
if $pid == $self->pid 12 26 38
177 }
178 38 ▶ if (!$active) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($active) 12 26 38
179 $SIG{WINCH} = 'DEFAULT'
180 12 ▶ if ${*{$self}}{io_pty_easy_did_handle_pty_size};
Branch: 1 branch
BranchTrueFalseTotal
if do { *{$self;} }->{"io_pty_easy_did_handle_pty_size"} 0 12 12
181 12 ${*{$self}}{io_pty_easy_did_handle_pty_size} = 0;
182 12 delete ${*{$self}}{io_pty_easy_pid};
183 }
184 38 return $active;
185 }
186  
187  
188 25 ▶sub kill {
Subroutine kill: called CC=4 SCAR=13.9
Pod documented
189 25 my $self = shift;
190 25 my ($sig, $non_blocking) = @_;
191 25 ▶ $sig = "TERM" unless defined $sig;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sig 23 2 25
192  
193 25 my $kills;
194 25 ▶ $kills = kill $sig => $self->pid if $self->is_active;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_active 13 12 25
195 25 ▶ $self->_wait_for_inactive unless $non_blocking;
Branch: 1 branch
BranchTrueFalseTotal
unless $non_blocking 23 2 25
196  
197 25 return $kills;
198 }
199  
200  
201 15 ▶sub close {
Subroutine close: called CC=1 SCAR=0.0
Pod documented
202 15 my $self = shift;
203  
204 15 close $self;
205 15 $self->kill;
206 }
207  
208  
209 35 ▶sub handle_pty_size {
Subroutine handle_pty_size: called CC=2 SCAR=6.9
Pod documented
210 35 my $self = shift;
211 35 ▶ ${*{$self}}{io_pty_easy_handle_pty_size} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 16 19 35
212 35 ${*{$self}}{io_pty_easy_handle_pty_size};
213 }
214  
215  
216 31 ▶sub def_max_read_chars {
Subroutine def_max_read_chars: called CC=2 SCAR=6.9
Pod documented
217 31 my $self = shift;
218 31 ▶ ${*{$self}}{io_pty_easy_def_max_read_chars} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 16 15 31
219 31 ${*{$self}}{io_pty_easy_def_max_read_chars};
220 }
221  
222  
223 233 ▶sub pid {
Subroutine pid: called CC=1 SCAR=0.0
Pod documented
224 233 my $self = shift;
225 233 ${*{$self}}{io_pty_easy_pid};
226 }
227  
228 24 ▶sub _wait_for_inactive {
Subroutine _wait_for_inactive: called CC=2 SCAR=6.9
229 24 my $self = shift;
230  
231 24 ▶ select(undef, undef, undef, 0.01) while $self->is_active;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_active and do { select undef, undef, undef, "0.01" } 11 24 35
232 }
233  
234 8 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
235 8 my $self = shift;
236 8 local $@;
237 8 local $?;
238 8 $self->close;
239 }
240  
241  
242 15 1;
243  
244 __END__
↑ Summary