lib/Chj/IO/CommandCommon.pm

filter: Statementstmt 24.7% 29 / 117 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 36.0% 9 / 25 Podpod 0.0% 0 / 10 totaltotal 20.3% 38 / 187 CC 18 SCAR 30.4
CC
18
Cov
19%
_launch
76.8
__ANON__
20.0
xxfinish
12.0
CRAP 21.0
« lib/Chj/singlequote.pm ↑ Summary .htmlgen/FunctionalPerl/Htmlgen/Sourcelang.pm »
1 #
2 # Copyright (c) 2003-2022 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 # Depends: ()
11  
12 =head1 NAME
13  
14 Chj::IO::CommandCommon
15  
16 =head1 SYNOPSIS
17  
18 - not to be used directly -
19  
20 =head1 DESCRIPTION
21  
22 Common superclass ('mixin'?) for Chj::IO::Command and Chj::IO::CommandBidirectional.
23  
24 =head1 NOTE
25  
26 This is alpha software! Read the status section in the package README
27 or on the L<website|http://functional-perl.org/>.
28  
29 =cut
30  
31 #'
32  
33 package Chj::IO::CommandCommon;
34  
35 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 3 ▶use Chj::xperlfunc;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use Chj::xpipe;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 3 ▶use NEXT;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 3 ▶use Chj::singlequote 'singlequote_sh';
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 3 my %metadata; # numified => [pid, cmd+args]
46  
47 sub PID () {0}
48 sub CMD_ARGS () {1}
49  
50 0 ▶sub pid {
Subroutine pid: not called CC=2 SCAR=17.9
Pod undocumented
51 0 my $self = shift;
52 0 ▶ my $meta = $metadata{ pack "I", $self }
Condition0%0/2
$metadata{pack 'I', $self} || die('missing metadata')
l!l
00
MC/DC0%0/1
$metadata{pack 'I', $self} || die('missing metadata')
$metadata{pack 'I', $self}
Truth table0%0/2
A
A= $metadata{pack 'I', $self}
Aresult
11
00
53 || die "missing metadata"; # || []; why?
54 0 $$meta[PID]
55 }
56  
57 0 ▶sub name {
Subroutine name: not called CC=2 SCAR=17.9
Pod undocumented
58 0 my $self = shift;
59 0 ▶ my $meta = $metadata{ pack "I", $self } || die "missing metadata";
Condition0%0/2
$metadata{pack 'I', $self} || die('missing metadata')
l!l
00
MC/DC0%0/1
$metadata{pack 'I', $self} || die('missing metadata')
$metadata{pack 'I', $self}
Truth table0%0/2
A
A= $metadata{pack 'I', $self}
Aresult
11
00
60 0 $$meta[CMD_ARGS][0] # XX ok? what use is it?
61 }
62  
63 0 ▶sub quotedname {
Subroutine quotedname: not called CC=2 SCAR=17.9
Pod undocumented
64 0 my $self = shift;
65 0 ▶ my $meta = $metadata{ pack "I", $self } || die "missing metadata";
Condition0%0/2
$metadata{pack 'I', $self} || die('missing metadata')
l!l
00
MC/DC0%0/1
$metadata{pack 'I', $self} || die('missing metadata')
$metadata{pack 'I', $self}
Truth table0%0/2
A
A= $metadata{pack 'I', $self}
Aresult
11
00
66 0 join(" ", map { singlequote_sh $_ } @{ $$meta[CMD_ARGS] })
67 }
68  
69 0 ▶sub _launch {
Subroutine _launch: not called CC=9 SCAR=43.4
70 0 my ($subname, $otherendclose, $closeinchild, $self, $cmd) = @_;
71 0 my $maybe_env;
72 0 ▶ if (ref($$cmd[0]) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cmd->[0] eq "HASH") 0 0 0
73  
74 # env settings (since with "use threads" $ENV does not work!)
75 0 $maybe_env = shift @$cmd;
76 }
77 0 ▶ @$cmd or die "$subname: missing cmd arguments";
Branch: 1 branch
BranchTrueFalseTotal
@$cmd or die "${subname}: missing cmd arguments" 0 0 0
78 0 my ($readerr, $writeerr) = do {
79 0 local $^F = 0;
80 0 xpipe
81 };
82 0 ▶ if (my $pid = xfork) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = xfork()) { } 0 0 0
83 0 $metadata{ pack "I", $self } = [$pid, $cmd];
84 0 &$otherendclose;
85 0 $writeerr->xclose; #" here it's clear that it's needed."
86 # close all handles that have been given for redirections?
87 # or leave that to the user? the latter.
88 0 my $err = $readerr->xcontent;
89 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
90 croak( __PACKAGE__
91 . "::$subname: could not execute "
92 0 . join(" ", map { singlequote_sh $_} @$cmd)
93 . ": $err");
94 }
95 0 return $self
96 } else {
97 0 &$closeinchild;
98 0 ▶ if (defined $maybe_env) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maybe_env) 0 0 0
99 0 my @newcmd = ("/usr/bin/env");
100 0 my $env = $maybe_env;
101 0 for my $k (keys %$env) {
102 0 ▶ die "invalid env key starting with '-': " . singlequote_sh($k)
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^-/ 0 0 0
103 if $k =~ /^-/;
104 0 push @newcmd, "$k = $$env{$k}";
105 }
106 0 push @newcmd, @$cmd;
107 0 $cmd = \@newcmd;
108 }
109 0 ▶ if (ref($$cmd[0]) eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cmd->[0] eq 'CODE') { } 0 0 0
110 0 my $code = shift @$cmd;
111 0 eval {
112 0 $code->(@$cmd);
113 0 die "coderf did return";
114 };
115 0 $writeerr->xprint($@); # [well serialize it?..tja..]
116 } else {
117 3 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
118 0 exec @$cmd;
119 0 $writeerr->xprint($!);
120 }
121 0 exit;
122 }
123 }
124  
125 0 ▶sub xlaunch {
Subroutine xlaunch: not called CC=1 SCAR=6.9
Pod undocumented
126 0 my $self = shift;
127 0 my ($otherend, $hdl, @cmd) = @_;
128 _launch(
129 "xlaunch",
130 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
131 0 $otherend->xclose;
132  
133 # ^important; seems like it's not cleaned up and destroyed
134 # otherwise upon return from new_* methods [soon enough],
135 # xcontent and the like would block outside.
136 },
137 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 0 $otherend->xdup2($hdl);
139 },
140 0 $self,
141 \@cmd
142 )
143 }
144  
145 0 ▶sub xlaunch3 {
Subroutine xlaunch3: not called CC=1 SCAR=6.9
Pod undocumented
146 0 my $self = shift;
147 0 my ($in, $out, $err, @cmd) = @_;
148 _launch(
149 "xlaunch3",
150 0 ▶ sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
151 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
152 0 ▶ $in->xdup2(0) if $in;
Branch: 1 branch
BranchTrueFalseTotal
if $in 0 0 0
153 0 ▶ $out->xdup2(1) if $out;
Branch: 1 branch
BranchTrueFalseTotal
if $out 0 0 0
154 0 ▶ $err->xdup2(2) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
155 },
156 0 $self,
157 \@cmd
158 )
159 }
160  
161 0 ▶sub wait {
Subroutine wait: not called CC=1 SCAR=6.9
Pod undocumented
162 0 my $s = shift;
163 0 waitpid $s->pid, 0;
164 0 $?
165 }
166  
167 0 ▶sub finish {
Subroutine finish: not called CC=1 SCAR=6.9
Pod undocumented
168 0 my $s = shift;
169 0 $s->close;
170 0 my $rv = $s->wait;
171 0 delete $metadata{ pack "I", $s };
172 0 $rv;
173 }
174  
175 0 ▶sub finish_nowait {
Subroutine finish_nowait: not called CC=1 SCAR=6.9
Pod undocumented
176 0 my $s = shift;
177 0 $s->close;
178 0 delete $metadata{ pack "I", $s };
179 }
180  
181 0 ▶sub xfinish { # Note: does not throw on error exit codes. Just throws
Subroutine xfinish: not called CC=1 SCAR=6.9
Pod undocumented
182 # on errors closing.
183 0 my $self = shift;
184 0 $self->xclose;
185 0 waitpid $self->pid, 0;
186 0 delete $metadata{ pack "I", $self };
187 0 $?
188 }
189  
190 0 ▶sub xxfinish { # does also throw on error exit codes.
Subroutine xxfinish: not called CC=3 SCAR=24.8
Pod undocumented
191 0 my $self = shift;
192 0 $self->xclose;
193 0 waitpid $self->pid, 0;
194 0 ▶ if ($? == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) { } 0 0 0
195 0 delete $metadata{ pack "I", $self };
196 } else {
197 0 my $qn = $self->quotedname;
198 0 delete $metadata{ pack "I", $self }; # [XX really? or only in DESTROY?]
199 0 ▶ if ($? & 127) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? & 127) { } 0 0 0
200 0 croak( "xxfinish on "
201 . $qn
202 . ": subcommand has been killed with signal "
203 . ($? & 127));
204 } else {
205 0 croak(
206 "xxfinish on " . $qn . ": subcommand gave error " . ($? >> 8));
207 }
208 }
209 }
210  
211 0 ▶sub DESTROY { # no exceptions thrown from here
Subroutine DESTROY: not called CC=2 SCAR=17.9
212 0 my $self = shift;
213 0 local ($@, $!, $?, $_);
214 0 ▶ if (defined $metadata{ pack "I", $self }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $metadata{pack "I", $self}) 0 0 0
215 0 $self->finish;
216 }
217 0 $self->NEXT::DESTROY;
218 }
219  
220 3 1
« lib/Chj/singlequote.pm ↑ Summary .htmlgen/FunctionalPerl/Htmlgen/Sourcelang.pm »