blib/lib/IPC/Run/Fused/Win32.pm untested

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