| 1 | 1 | ▶ | use 5.008003; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package IPC::Run::Fused::Win32; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | our $VERSION = '1.000000'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # ABSTRACT: Implementation of IPC::Run::Fused for Win32 | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Module::Runtime; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | use IPC::Run::Fused qw(_fail); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 1 | ▶ | use Socket qw( AF_UNIX SOCK_STREAM PF_UNSPEC ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 1 | our @EXPORT_OK = qw( run_fused ); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | sub run_fused { | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | my ( undef, @params ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
run_fused: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if ( ref $params[0] and 'CODE' eq ref $params[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $params[0] and "CODE" eq ref $params[0]
MC/DC0%0/2
ref $params[0] and "CODE" eq ref $params[0]
ref $params[0]
"CODE" eq ref $params[0]
Truth table0%0/3
A and B
A= ref $params[0]B= "CODE" eq ref $params[0]
| |||||||||||||||||||||||||||||
| 50 | 0 | goto \&_run_fused_coderef; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 0 | goto \&_run_fused_job; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | sub _run_fused_job { ## no critic (Subroutines::RequireArgUnpacking) | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | my ( $read_handle, @params ) = ( \shift @_, @_ ); | ||||||||||||||||||||||||||
|
Subroutine
_run_fused_job: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | my $config = _run_fused_jobdecode(@params); | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | Module::Runtime::require_module('File::Which'); | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | $config->{which} = File::Which::which( $config->{executable} ); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | local $IPC::Run::Fused::FAIL_CONTEXT{which} = $config->{which}; | |||||||||||||||||||||||||||
| 65 | 0 | local $IPC::Run::Fused::FAIL_CONTEXT{executable} = $config->{executable}; | |||||||||||||||||||||||||||
| 66 | 0 | local $IPC::Run::Fused::FAIL_CONTEXT{command} = $config->{command}; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if ( not $config->{which} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | _fail('Failed to resolve executable to path'); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | Module::Runtime::require_module('Win32::Job'); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | pipe ${$read_handle}, my $writer; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ( my $pid = fork ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | return $pid; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | my $job = Win32::Job->new(); | |||||||||||||||||||||||||||
| 81 | $job->spawn( | ||||||||||||||||||||||||||||
| 82 | $config->{which}, | ||||||||||||||||||||||||||||
| 83 | $config->{command}, | ||||||||||||||||||||||||||||
| 84 | { | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | stdout => $writer, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | stderr => $writer, | ||||||||||||||||||||||||||||
| 87 | }, | ||||||||||||||||||||||||||||
| 88 | ) or _fail('Could not spawn job'); | ||||||||||||||||||||||||||||
| 89 | 0 | my $result = $job->run( -1, 0 ); | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | if ( not $result ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | my $status = $job->status(); | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ( exists $status->{exitcode} and 293 == $status->{exitcode} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $status->{"exitcode"} and 293 == $status->{"exitcode"}
MC/DC0%0/2
exists $status->{"exitcode"} and 293 == $status->{"exitcode"}
exists $status->{"exitcode"}
293 == $status->{"exitcode"}
Truth table0%0/3
A and B
A= exists $status->{"exitcode"}B= 293 == $status->{"exitcode"}
| |||||||||||||||||||||||||||||
| 93 | 0 | _fail('Process used more than allotted time'); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 0 | _fail( 'Child process terminated with exit code' . $status->{exitcode} ); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 0 | exit; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub _run_fused_jobdecode { | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | my (@params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_run_fused_jobdecode: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if ( ref $params[0] and 'SCALAR' eq ref $params[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $params[0] and "SCALAR" eq ref $params[0]
MC/DC0%0/2
ref $params[0] and "SCALAR" eq ref $params[0]
ref $params[0]
"SCALAR" eq ref $params[0]
Truth table0%0/3
A and B
A= ref $params[0]B= "SCALAR" eq ref $params[0]
| |||||||||||||||||||||||||||||
| 104 | 0 | my $command = ${ $params[0] }; | |||||||||||||||||||||||||||
| 105 | 0 | $command =~ s/\A\s*//msx; | |||||||||||||||||||||||||||
| 106 | return { | ||||||||||||||||||||||||||||
| 107 | 0 | command => $command, | |||||||||||||||||||||||||||
| 108 | executable => _win32_command_find_invocant($command), | ||||||||||||||||||||||||||||
| 109 | }; | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | return { | ||||||||||||||||||||||||||||
| 112 | 0 | executable => $params[0], | |||||||||||||||||||||||||||
| 113 | command => _win32_escape_command(@params), | ||||||||||||||||||||||||||||
| 114 | }; | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | sub _run_fused_coderef { ## no critic (Subroutines::RequireArgUnpacking) | ||||||||||||||||||||||||||||
| 118 | 0 | ▶ | my ( $read_handle, $code ) = ( \shift @_, @_ ); | ||||||||||||||||||||||||||
|
Subroutine
_run_fused_coderef: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 119 | 0 | my ( $reader, $writer ); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | socketpair $reader, $writer, AF_UNIX, SOCK_STREAM, PF_UNSPEC or _fail('creating socketpair'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | shutdown $reader, 1 or _fail('Cant close write to reader'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | shutdown $writer, 0 or _fail('Cant close read to writer'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | if ( my $pid = fork ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | ${$read_handle} = $reader; | |||||||||||||||||||||||||||
| 127 | 0 | return $pid; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | close *STDERR or _fail('Closing STDERR'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | close *STDOUT or _fail('Closing STDOUT'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | open *STDOUT, '>>&=', $writer or _fail('Assigning to STDOUT'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | open *STDERR, '>>&=', $writer or _fail('Assigning to STDERR'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | $code->(); | |||||||||||||||||||||||||||
| 135 | 0 | exit; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 1 | our $BACKSLASH = chr 92; | |||||||||||||||||||||||||||
| 140 | 1 | our $DBLBACKSLASH = $BACKSLASH x 2; | |||||||||||||||||||||||||||
| 141 | 1 | our $DOS_SPECIAL_CHARS = { | |||||||||||||||||||||||||||
| 142 | chr 92 => [ 'backslash ', $BACKSLASH x 2 ], | ||||||||||||||||||||||||||||
| 143 | chr 34 => [ 'double-quotes', $BACKSLASH . chr 34 ], | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | #chr(60) => ['open angle bracket', $backslash . chr(60)], | ||||||||||||||||||||||||||||
| 146 | #chr(62) => ['close angle bracket', $backslash . chr(62)], | ||||||||||||||||||||||||||||
| 147 | }; | ||||||||||||||||||||||||||||
| 148 | our $DOS_REV_CHARS = { | ||||||||||||||||||||||||||||
| 149 | 2 | map { ( $DOS_SPECIAL_CHARS->{$_}->[1], [ $DOS_SPECIAL_CHARS->{$_}->[0], $_ ] ) } | |||||||||||||||||||||||||||
| 150 | 1 | keys %{$DOS_SPECIAL_CHARS}, | |||||||||||||||||||||||||||
| 151 | }; | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | sub _win32_escape_command_char { | ||||||||||||||||||||||||||||
| 154 | 0 | ▶ | my ($char) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_win32_escape_command_char: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | return $char unless exists $DOS_SPECIAL_CHARS->{$char}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | return $DOS_SPECIAL_CHARS->{$char}->[1]; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | sub _win32_escape_command_token { | ||||||||||||||||||||||||||||
| 160 | ## no critic (RegularExpressions) | ||||||||||||||||||||||||||||
| 161 | 0 | ▶ | my $chars = join q{}, map { _win32_escape_command_char($_) } split //, shift; | ||||||||||||||||||||||||||
|
Subroutine
_win32_escape_command_token: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 162 | 0 | return qq{"$chars"}; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | sub _win32_escape_command { | ||||||||||||||||||||||||||||
| 166 | 0 | ▶ | my (@tokens) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_win32_escape_command: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 167 | 0 | return join q{ }, map { _win32_escape_command_token($_) } @tokens; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | sub _win32_command_find_invocant { | ||||||||||||||||||||||||||||
| 171 | 0 | ▶ | my ($command) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_win32_command_find_invocant: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||
| 172 | 0 | $command = "$command"; | |||||||||||||||||||||||||||
| 173 | 0 | my $first = q[]; | |||||||||||||||||||||||||||
| 174 | ## no critic (RegularExpressions) | ||||||||||||||||||||||||||||
| 175 | 0 | my @chars = split //, $command; | |||||||||||||||||||||||||||
| 176 | 0 | my $inquote; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | ▶ | while (@chars) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 0 | my $char = $chars[0]; | |||||||||||||||||||||||||||
| 180 | 0 | my $dchar = $chars[0] . $chars[1]; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | ▶ | if ( not $inquote and q["] eq $char ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $inquote and "\"" eq $char
MC/DC0%0/2
not $inquote and "\"" eq $char
not $inquote
"\"" eq $char
Truth table0%0/3
not(A) and B
A= not $inquoteB= "\"" eq $char
| |||||||||||||||||||||||||||||
| 183 | 0 | $inquote = 1; | |||||||||||||||||||||||||||
| 184 | 0 | shift @chars; | |||||||||||||||||||||||||||
| 185 | 0 | next; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | 0 | ▶ | if ( $inquote and q["] eq $char ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$inquote and "\"" eq $char
MC/DC0%0/2
$inquote and "\"" eq $char
$inquote
"\"" eq $char
Truth table0%0/3
A and B
A= $inquoteB= "\"" eq $char
| |||||||||||||||||||||||||||||
| 188 | 0 | $inquote = undef; | |||||||||||||||||||||||||||
| 189 | 0 | shift @chars; | |||||||||||||||||||||||||||
| 190 | 0 | next; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | 0 | ▶ | if ( exists $DOS_REV_CHARS->{$dchar} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 0 | $first .= $DOS_REV_CHARS->{$dchar}->[1]; | |||||||||||||||||||||||||||
| 194 | 0 | shift @chars; | |||||||||||||||||||||||||||
| 195 | 0 | shift @chars; | |||||||||||||||||||||||||||
| 196 | 0 | next; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | 0 | ▶ | if ( q[ ] eq $char and not $inquote ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
" " eq $char and not $inquote
MC/DC0%0/2
" " eq $char and not $inquote
" " eq $char
not $inquote
Truth table0%0/3
A and not(B)
A= " " eq $charB= not $inquote
| |||||||||||||||||||||||||||||
| 199 | 0 | ▶ | if ( not length $first ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 0 | shift @chars; | |||||||||||||||||||||||||||
| 201 | 0 | next; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | 0 | return $first; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | 0 | ▶ | if ( q[ ] eq $char and $inquote ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
" " eq $char and $inquote
MC/DC0%0/2
" " eq $char and $inquote
" " eq $char
$inquote
Truth table0%0/3
A and B
A= " " eq $charB= $inquote
| |||||||||||||||||||||||||||||
| 206 | 0 | $first .= $char; | |||||||||||||||||||||||||||
| 207 | 0 | shift @chars; | |||||||||||||||||||||||||||
| 208 | 0 | next; | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | 0 | $first .= $char; | |||||||||||||||||||||||||||
| 211 | 0 | shift @chars; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | 0 | ▶ | if ($inquote) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | 0 | _fail('Could not parse command from commandline'); | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | 0 | return $first; | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 1 | 1; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | __END__ | ||||||||||||||||||||||||||||