lib/App/MrShell.pm

filter: Statementstmt 74.1% 241 / 325 Branchbran 54.0% 67 / 124 Conditioncond 21.4% 6 / 28 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 79.5% 35 / 44 Podpod 60.0% 12 / 20 totaltotal 65.1% 362 / 556 CC 89 SCAR 34.3
CC
89
Cov
66%
set_group_option
210.0
read_config
35.8
error_event
20.0
CRAP 30.8
↑ Summary script/mrsh »
1 package App::MrShell;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Config::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use POE qw( Wheel::Run );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Term::ANSIColor qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Text::Balanced;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 our $VERSION = '2.0207';
14 8 our @DEFAULT_SHELL_COMMAND = (ssh => '-o', 'BatchMode yes', '-o', 'StrictHostKeyChecking no', '-o', 'ConnectTimeout 20', '[%u]-l', '[]%u', '%h');
15  
16 # new {{{
17 6 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
18 6 my $class = shift;
19 6 my $this = bless { hosts=>[], cmd=>[], _shell_cmd=>[@DEFAULT_SHELL_COMMAND] }, $class;
20  
21 6 return $this;
22 }
23 # }}}
24  
25 # _process_space_delimited {{{
26 1 ▶sub _process_space_delimited {
Subroutine _process_space_delimited: called CC=5 SCAR=16.2
27 1 my $this = shift;
28 1 my $that = shift;
29  
30 1 my @output;
31 1 ▶ while( $that ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($that) 5 1 6
32 5 ▶ if( $that =~ m/^\s*['"]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($that =~ /^\s*['"]/) { } 4 1 5
33 4 my ($tok, $rem) = Text::Balanced::extract_delimited($that, qr(["']));
34  
35 4 ▶ ($tok =~ s/^(['"])// and $tok =~ s/$1$//) or die "internal error processing space delimited";
Branch: 1 branch
BranchTrueFalseTotal
$tok =~ s/^(['"])// and $tok =~ s/$1$// or die "internal error processing space delimited" 4 0 4
Condition33%1/3
$tok =~ s/^(['"])// and $tok =~ s/$1$//
!ll&&!rl&&r
004
MC/DC0%0/2
$tok =~ s/^(['"])// and $tok =~ s/$1$//
$tok =~ s/^(['"])// $tok =~ s/$1$//
Truth table33%1/3
A and B
A= $tok =~ s/^(['"])//B= $tok =~ s/$1$//
ABresult
0X0
100
111
36  
37 4 push @output, $tok;
38 4 $that = $rem;
39  
40 } else {
41 1 my ($tok, $rem) = split ' ', $that, 2;
42  
43 1 push @output, $tok;
44 1 $that = $rem;
45 }
46 }
47  
48 return @output
49 1 }
50 # }}}
51 # _process_hosts {{{
52 4 ▶sub _process_hosts {
Subroutine _process_hosts: called CC=9 SCAR=23.0
53 4 my $this = shift;
54  
55 4 my @h = do {
56 0 ▶ my @tmp = map { my $k = $_; $k =~ s/^\@// ? @{$this->{groups}{$k} or die "couldn't find group: \@$k\n"} : $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
$k =~ s/^\@// ? : 0 6 6
Condition0%0/2
$this->{'groups'}{$k} || die("couldn't find group: \@$k\n")
l!l
00
MC/DC0%0/1
$this->{'groups'}{$k} || die("couldn't find group: \@$k\n")
$this->{'groups'}{$k}
Truth table0%0/2
A
A= $this->{'groups'}{$k}
Aresult
11
00
57 4 my %h; @h{@tmp} = ();
58 4 for(keys %h) {
59 6 ▶ if(my ($k) = m/^\-(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($k) = /^\-(.+)/) 0 6 6
60 0 delete $h{$_};
61 0 delete $h{$k};
62 }
63 }
64 4 sort keys %h;
65 };
66  
67 4 ▶ my $o = my $l = $this->{_host_width} || 0;
Condition100%2/2
$this->{'_host_width'} || 0
l!l
13
MC/DC100%1/1
$this->{'_host_width'} || 0
$this->{'_host_width'}
Truth table100%2/2
A
A= $this->{'_host_width'}
Aresult
11
00
68 4 for( map { length $this->_host_route_to_nick($_) } @h ) {
69 6 ▶ $l = $_ if $_>$l
Branch: 1 branch
BranchTrueFalseTotal
if $_ > $l 3 3 6
70 }
71  
72 4 ▶ $this->{_host_width} = $l if $l != $o;
Branch: 1 branch
BranchTrueFalseTotal
if $l != $o 3 1 4
73  
74 4 return @h;
75 }
76 # }}}
77 # _host_route_to_nick {{{
78 20 ▶sub _host_route_to_nick {
Subroutine _host_route_to_nick: called CC=1 SCAR=0.0
79 20 my $this = shift;
80  
81 20 return join "", shift =~ m/(?:!|[^!]+$)/g
82 }
83 # }}}
84  
85 # set_shell_command_option {{{
86 3 ▶sub set_shell_command_option {
Subroutine set_shell_command_option: called CC=3 SCAR=11.0
Pod documented
87 3 my $this = shift;
88 3 my $arg = shift;
89  
90 3 ▶ if( ref($arg) eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'ARRAY') { } 2 1 3
91 2 $this->{_shell_cmd} = [ @$arg ]; # make a real copy
92  
93 } else {
94 1 ▶ $this->{_shell_cmd} = [ $this->_process_space_delimited($arg||"") ];
Condition50%1/2
$arg || ''
l!l
10
MC/DC0%0/1
$arg || ''
$arg
Truth table50%1/2
A
A= $arg
Aresult
11
00
95 }
96  
97 3 return $this;
98 }
99 # }}}
100 # set_group_option {{{
101 0 ▶sub set_group_option {
Subroutine set_group_option: not called CC=14 SCAR=53.5
Pod documented
102 0 my $this = shift;
103 0 ▶ my $groups = ($this->{groups} ||= {});
Condition0%0/2
$this->{"groups"} ||= {}
l!l
00
MC/DC0%0/1
$this->{"groups"} ||= {}
$this->{"groups"}
Truth table0%0/2
A
A= $this->{"groups"}
Aresult
11
00
104  
105 0 my ($name, $value);
106 0 ▶ while( ($name, $value) = splice @_, 0, 2 and $name and $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2) and $name and $value) 0 0 0
Condition0%0/6
() = splice(@_, 0, 2) and $name and $value
!ll&&!rl&&r
000
() = splice(@_, 0, 2) and $name
!ll&&!rl&&r
000
MC/DC0%0/3
() = splice(@_, 0, 2) and $name and $value
() = splice(@_, 0, 2) $name $value
Truth table0%0/4
A and B and C
A= () = splice(@_, 0, 2)B= $nameC= $value
ABCresult
0XX0
10X0
1100
1111
107 0 ▶ if( ref($value) eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 0 0
108 0 $groups->{$name} = [ @$value ]; # make a real copy
109  
110 } else {
111 0 $groups->{$name} = [ $this->_process_space_delimited( $value ) ];
112 }
113 }
114  
115 0 my @groups = keys %{ $this->{groups} };
116 0 my $replace_limit = 30;
117 REPLACE_GROPUS: {
118 0 my $replaced = 0;
119  
120 0 for my $group (@groups) {
121 0 my $hosts = $groups->{$group};
122  
123 0 my $r = 0;
124 0 for(@$hosts) {
125 0 ▶ if( m/^@(.+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^@(.+)/) 0 0 0
126 0 ▶ if( my $g = $groups->{$1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $g = $groups->{$1}) 0 0 0
127 0 $_ = $g;
128  
129 0 $r ++;
130 }
131 }
132 }
133  
134 0 ▶ if( $r ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) 0 0 0
135 0 my %h;
136 0 ▶ @h{ map {ref $_ ? @$_ : $_} @$hosts } = ();
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
137 0 $groups->{$group} = [ keys %h ];
138 0 $replaced ++;
139 }
140 }
141  
142 0 $replace_limit --;
143 0 ▶ last if $replace_limit < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $replace_limit < 1 0 0 0
144 0 ▶ redo if $replaced;
Branch: 1 branch
BranchTrueFalseTotal
if $replaced 0 0 0
145 }
146  
147 0 return $this;
148 }
149 # }}}
150 # set_logfile_option {{{
151 1 ▶sub set_logfile_option {
Subroutine set_logfile_option: called CC=5 SCAR=16.3
Pod documented
152 1 my $this = shift;
153 1 my $file = shift;
154 1 my $trunc = shift;
155  
156 1 ▶ unless( our $already_compiled++ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (our $already_compiled++) 1 0 1
157 1 my $load_ansi_filter_package = q {
158 package App::MrShell::ANSIFilter;
159 use Symbol;
160 use Tie::Handle;
161 use base 'Tie::StdHandle';
162  
163 my %orig;
164  
165 sub PRINT {
166 my $this = shift;
167 my @them = @_;
168 s/\e\[[\d;]+m//g for @them;
169 print {$orig{$this}} @them;
170 }
171  
172 sub filtered_handle {
173 my $pfft = gensym();
174 my $it = tie *{$pfft}, __PACKAGE__ or die $!;
175 $orig{$it} = shift;
176 $pfft;
177 }
178  
179 1};
180  
181 1 ▶ eval $load_ansi_filter_package or die $@; ## no critic -- sometimes this kind of eval is ok
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval $load_ansi_filter_package or die $@ 1 0 1
182 # (This probably isn't one of them.)
183 }
184  
185 1 ▶ open my $log, ($trunc ? ">" : ">>"), $file or croak "couldn't open $file for write: $!"; ## no critic -- I mean to pass this around, shut up
Branch: 2 branches
BranchTrueFalseTotal
open my $log, $trunc ? ">" : ">>", $file or croak("couldn't open $file for write: $!") 1 0 1
$trunc ? : 1 0 1
186  
187 1 $this->{_log_fh} = App::MrShell::ANSIFilter::filtered_handle($log);
188  
189 1 return $this;
190 }
191 # }}}
192 # set_debug_option {{{
193 3 ▶sub set_debug_option {
Subroutine set_debug_option: called CC=3 SCAR=11.6
Pod documented
194 3 my $this = shift;
195 3 my $val = shift;
196  
197 # -d 0 and -d 1 are the same
198 # -d 2 is a level up, -d 4 is even more
199 # $val==undef clears the setting
200  
201 3 ▶ if( not defined $val ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 1 0 1
202 3 delete $this->{debug};
203 3 return $this;
204 }
205  
206 1 ▶ $this->{debug} = $val ? $val : 1;
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 0 0 0
207  
208 1 return $this;
209 }
210 # }}}
211 # set_no_command_escapes_option {{{
212 1 ▶sub set_no_command_escapes_option {
Subroutine set_no_command_escapes_option: called CC=2 SCAR=8.1
Pod documented
213 1 my $this = shift;
214  
215 1 ▶ $this->{no_command_escapes} = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
216  
217 1 return $this;
218 }
219 # }}}
220  
221 # groups {{{
222 0 ▶sub groups {
Subroutine groups: not called CC=3 SCAR=24.8
Pod undocumented
223 0 my $this = shift;
224  
225 0 ▶ return unless $this->{groups};
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"groups"} 0 0 0
226 0 ▶ return wantarray ? %{$this->{groups}} : $this->{groups};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
227 }
228 # }}}
229  
230 # set_usage_error($&) {{{
231 1 ▶sub set_usage_error($&) { ## no critic -- prototypes are bad how again?
Subroutine set_usage_error: called CC=1 SCAR=0.0
Pod documented
232 1 my $this = shift;
233 1 my $func = shift;
234 1 my $pack = caller;
235 1 my $name = $pack . "::$func";
236 1 my @args = @_;
237  
238 0 ▶ $this->{_usage_error} = sub {
Subroutine __ANON__: not called CC=1 SCAR=0.2
239 8 ▶ no strict 'refs'; ## no critic -- how would you call this by name without this?
Subroutine BEGIN: called CC=1 SCAR=0.0
240 0 $name->(@args)
241 1 };
242  
243 1 return $this;
244 }
245 # }}}
246 # read_config {{{
247 1 ▶sub read_config {
Subroutine read_config: called CC=10 SCAR=35.8
Pod documented
248 1 my ($this, $that) = @_;
249  
250 1 ▶ $this->{_conf} = Config::Tiny->read($that) if -f $that;
Branch: 1 branch
BranchTrueFalseTotal
if -f $that 0 1 1
251  
252 1 for my $group (keys %{ $this->{_conf}{groups} }) {
253 0 $this->set_group_option( $group => $this->{_conf}{groups}{$group} );
254 }
255  
256 1 ▶ if( my $c = $this->{_conf}{options}{'shell-command'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $this->{"_conf"}{"options"}{"shell-command"}) 0 1 1
257 0 $this->set_shell_command_option( $c );
258 }
259  
260 1 ▶ if( my $c = $this->{_conf}{options}{'logfile'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $this->{"_conf"}{"options"}{"logfile"}) 0 1 1
261 0 my $t = $this->{_conf}{options}{'truncate-logfile'};
262 0 ▶ my $v = ($t ? 1:0);
Branch: 1 branch
BranchTrueFalseTotal
$t ? : 0 0 0
263 0 ▶ $v = 0 if $t =~ m/(?:no|false)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /(?:no|false)/i 0 0 0
264  
265 0 $this->set_logfile_option($c, $v);
266 }
267  
268 1 ▶ if( my $c = $this->{_conf}{options}{'no-command-escapes'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $this->{"_conf"}{"options"}{"no-command-escapes"}) 0 1 1
269 0 ▶ my $v = ($c ? 1:0);
Branch: 1 branch
BranchTrueFalseTotal
$c ? : 0 0 0
270 0 ▶ $v = 0 if $c =~ m/(?:no|false)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $c =~ /(?:no|false)/i 0 0 0
271  
272 0 $this->set_no_command_escapes_option( $v );
273 }
274  
275 1 return $this;
276 }
277 # }}}
278 # set_hosts {{{
279 4 ▶sub set_hosts {
Subroutine set_hosts: called CC=1 SCAR=0.0
Pod documented
280 4 my $this = shift;
281  
282 4 $this->{hosts} = [ $this->_process_hosts(@_) ];
283  
284 4 return $this;
285 }
286 # }}}
287 # queue_command {{{
288 5 ▶sub queue_command {
Subroutine queue_command: called CC=5 SCAR=22.1
Pod documented
289 5 my $this = shift;
290 5 my @hosts = @{$this->{hosts}};
291  
292 5 ▶ unless( @hosts ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@hosts) 0 5 5
293 0 ▶ if( my $h = $this->{_conf}{options}{'default-hosts'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $h = $this->{'_conf'}{'options'}{'default-hosts'}) { } 0 0 0
294 0 @hosts = $this->_process_hosts( $this->_process_space_delimited($h) );
295  
296 } else {
297 0 ▶ if( my $e = $this->{_usage_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $this->{'_usage_error'}) { } 0 0 0
298 0 warn "Error: no hosts specified\n";
299 0 $e->();
300  
301 } else {
302 0 croak "set_hosts before issuing queue_command";
303 }
304 }
305 }
306  
307 5 for my $h (@hosts) {
308 8 push @{$this->{_cmd_queue}{$h}}, [@_]; # make a real copy
309 }
310  
311 5 return $this;
312 }
313 # }}}
314 # run_queue {{{
315 3 ▶sub run_queue {
Subroutine run_queue: called CC=1 SCAR=0.3
Pod documented
316 3 my $this = shift;
317  
318 $this->{_session} = POE::Session->create( inline_states => {
319 3 ▶ _start => sub { $this->poe_start(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
320 0 ▶ child_stdout => sub { $this->line(1, @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
321 0 ▶ child_stderr => sub { $this->line(2, @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
322 8 ▶ child_signal => sub { $this->sigchld(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
323 40 ▶ stall_close => sub { $this->_close(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
324 0 ▶ ErrorEvent => sub { $this->error_event },
Subroutine __ANON__: not called CC=1 SCAR=6.9
325 3 });
326  
327 3 POE::Kernel->run();
328  
329 3 return $this;
330 }
331 # }}}
332  
333 # std_msg {{{
334 14 ▶sub std_msg {
Subroutine std_msg: called CC=6 SCAR=18.0
Pod undocumented
335 14 my $this = shift;
336 14 my $host = shift;
337 14 my $cmdno = shift;
338 14 my $fh = shift;
339 14 my $msg = shift;
340  
341 14 ▶ my $host_msg = $host ? $this->_host_route_to_nick($host) . ": " : "";
Branch: 1 branch
BranchTrueFalseTotal
$host ? : 12 2 14
342 14 my $time_str = strftime('%H:%M:%S', localtime);
343  
344 print $time_str,
345 14 ▶ sprintf(' %-*s', $this->{_host_width}+2, $host_msg),
Branch: 1 branch
BranchTrueFalseTotal
$fh == 2 ? : 0 14 14
346 ( $fh==2 ? ('[',BOLD,YELLOW,'stderr',RESET,'] ') : () ), $msg, RESET, "\n";
347  
348 14 ▶ if( $this->{_log_fh} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_log_fh"}) 3 11 14
349 3 $time_str = strftime('%Y-%m-%d %H:%M:%S', localtime);
350  
351 # No point in printing colors, stripped anyway. Formatting columns is
352 # equally silly -- in append mode anyway.
353 3 ▶ $host_msg = $host ? "$host: " : "";
Branch: 1 branch
BranchTrueFalseTotal
$host ? : 2 1 3
354 3 ▶ print {$this->{_log_fh}} "$time_str $host_msg", ($fh==2 ? "[stderr] " : ""), $msg, "\n";
Branch: 1 branch
BranchTrueFalseTotal
$fh == 2 ? : 0 3 3
355 }
356  
357 14 return $this;
358 }
359 # }}}
360  
361 # line {{{
362 0 ▶sub line {
Subroutine line: not called CC=1 SCAR=6.9
Pod undocumented
363 0 my $this = shift;
364 0 my $fh = shift;
365 0 my ($line, $wid) = @_[ ARG0, ARG1 ];
366 0 my ($kid, $host, $cmdno, $lineno) = @{$this->{_wid}{$wid}};
367  
368 0 $$lineno ++;
369 0 $this->std_msg($host, $cmdno, $fh, $line);
370  
371 0 return;
372 }
373 # }}}
374  
375 # sigchld {{{
376 0 ▶sub _sigchld_exit_error {
Subroutine _sigchld_exit_error: not called CC=1 SCAR=6.9
377 0 my $this = shift;
378 0 my ($pid, $exit) = @_[ ARG1, ARG2 ];
379 0 $exit >>= 8;
380  
381 0 $this->std_msg("?", -1, 0, BOLD.RED."-- sigchld received for untracked pid($pid, $exit), probably a bug in Mr. Shell --");
382  
383 0 return;
384 }
385  
386 8 ▶sub sigchld {
Subroutine sigchld: called CC=3 SCAR=13.6
Pod undocumented
387 8 my $this = shift; # ARG0 is the signal name string
388 8 ▶ my ($kid, $host, $cmdno, @c) = @{ $this->{_pid}{ $_[ARG1] } || return $this->_sigchld_exit_error(@_) };
Condition50%1/2
$this->{'_pid'}{$_[11]} || (return $this->_sigchld_exit_error(@_))
l!l
80
MC/DC0%0/1
$this->{'_pid'}{$_[11]} || (return $this->_sigchld_exit_error(@_))
$this->{'_pid'}{$_[11]}
Truth table50%1/2
A
A= $this->{'_pid'}{$_[11]}
Aresult
11
00
389  
390 # NOTE: this usually isn't an error, sometimes the sigchild will arrive
391 # before the handles are "closed" in the traditional sense. We get error
392 # eveents for errors.
393 #### # $this->std_msg($host, $cmdno, 0, RED.'-- error: unexpected child exit --');
394  
395 # NOTE: though, the exit value may indicate an actual error.
396 8 ▶ if( (my $exit = $_[ARG2]) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $exit = $_[12]) != 0) 0 8 8
397 # XXX: I'd like to do more here but I'm waiting to see what Paul
398 # Fenwick has to say about it.
399 0 $exit >>= 8;
400  
401 0 my $reset = RESET;
402 0 my $black = BOLD.BLACK;
403 0 my $red = RESET.RED;
404  
405 0 $this->std_msg($host, $cmdno, 0, "$black-- shell exited with nonzero status: $red$exit$black --");
406 }
407  
408 8 $_[KERNEL]->yield( stall_close => $kid->ID, 0 );
409  
410 8 return;
411 }
412 # }}}
413 # _close {{{
414 40 ▶sub _close {
Subroutine _close: called CC=5 SCAR=16.1
415 40 my $this = shift;
416 40 my ($wid, $count) = @_[ ARG0, ARG1 ];
417  
418 40 ▶ return unless $this->{_wid}{$wid}; # sometimes we'll get a sigchild *and* a close event
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"_wid"}{$wid} 0 40 40
419  
420 # NOTE: I was getting erratic results with some fast running commands and
421 # guessed that I was sometimes getting the close event before the stdout
422 # event. Waiting through the kernel loop once is probably enough, but I
423 # used 3 because it does't hurt either.
424  
425 40 ▶ if( $count > 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 3) { } 8 32 40
426 8 my ($kid, $host, $cmdno, $lineno, @c) = @{ delete $this->{_wid}{$wid} };
427  
428 8 ▶ $this->std_msg($host, $cmdno++, 0, BOLD.BLACK.'-- eof --') if $$lineno == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $$lineno == 0 8 0 8
429 8 ▶ if( @c ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@c) 2 6 8
430 2 $this->start_queue_on_host($_[KERNEL] => $host, $cmdno, @c);
431 2 $this->std_msg($host, $cmdno, 0, BOLD.BLACK."-- starting: @{$c[0]} --");
432 }
433  
434 8 delete $this->{_pid}{ $kid->PID };
435  
436 } else {
437 32 $_[KERNEL]->yield( stall_close => $wid, $count+1 );
438 }
439  
440 40 return;
441 }
442 # }}}
443 # error_event {{{
444 0 ▶sub error_event {
Subroutine error_event: not called CC=4 SCAR=30.0
Pod undocumented
445 0 my $this = shift;
446 0 my ($operation, $errnum, $errstr, $wid) = @_[ARG0 .. ARG3];
447 0 ▶ my ($kid, $host, $cmdno, @c) = @{ delete $this->{_wid}{$wid} || return };
Condition0%0/2
delete $this->{'_wid'}{$wid} || (return)
l!l
00
MC/DC0%0/1
delete $this->{'_wid'}{$wid} || (return)
delete $this->{'_wid'}{$wid}
Truth table0%0/2
A
A= delete $this->{'_wid'}{$wid}
Aresult
11
00
448 0 delete $this->{_pid}{ $kid->PID };
449  
450 0 ▶ $errstr = "remote end closed" if $operation eq "read" and not $errnum;
Branch: 1 branch
BranchTrueFalseTotal
if $operation eq "read" and not $errnum 0 0 0
Condition0%0/3
$operation eq "read" and not $errnum
!ll&&!rl&&r
000
MC/DC0%0/2
$operation eq "read" and not $errnum
$operation eq "read" not $errnum
Truth table0%0/3
A and not(B)
A= $operation eq "read"B= not $errnum
ABresult
0X0
100
111
451 0 $this->std_msg($host, $cmdno, 0, RED."-- $operation error $errnum: $errstr --");
452  
453 0 return;
454 }
455 # }}}
456  
457 # set_subst_vars {{{
458 19 ▶sub set_subst_vars {
Subroutine set_subst_vars: called CC=3 SCAR=11.0
Pod documented
459 19 my $this = shift;
460  
461 19 ▶ while( my ($k,$v) = splice @_, 0, 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 27 19 46
462 27 ▶ $this->{_subst}{$k} = $v unless exists $this->{_subst}{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $this->{"_subst"}{$k} 27 0 27
463 }
464  
465 19 return $this;
466 }
467 # }}}
468 # subst_cmd_vars {{{
469 19 ▶sub subst_cmd_vars {
Subroutine subst_cmd_vars: called CC=19 SCAR=29.5
Pod documented
470 19 my $this = shift;
471 19 ▶ my %h = %{ delete($this->{_subst}) || {} };
Condition50%1/2
delete $this->{'_subst'} || {}
l!l
190
MC/DC0%0/1
delete $this->{'_subst'} || {}
delete $this->{'_subst'}
Truth table50%1/2
A
A= delete $this->{'_subst'}
Aresult
11
00
472 19 my $host = $h{'%h'};
473  
474 19 my @c = @_; # copy this so it doesn't get altered upstream
475 # (I'd swear I shoulnd't need to do this at all, but it's
476 # proovably true that I do.)
477  
478 19 ▶ if( $host =~ m/\b(?!<\\)!/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /\b(?!<\\)!/) { } 7 12 19
479 7 my @hosts = split '!', $host;
480  
481 7 my @indexes_of_replacements;
482 7 ▶ for(my $i=0; $i<@c; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @c) 149 7 156
483 149 ▶ if( $c[$i] eq '%h' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c[$i] eq "%h") 9 140 149
484 9 splice @c, $i, 1, $hosts[0];
485  
486 9 push @indexes_of_replacements, $i;
487  
488 9 for my $h (reverse @hosts[1 .. $#hosts]) {
489 15 splice @c, $i+1, 0, @c[0 .. $i-1] => $h;
490 15 push @indexes_of_replacements, $i+1 + $indexes_of_replacements[-1];
491  
492 15 ▶ unless( $this->{no_command_escapes} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"no_command_escapes"}) 15 0 15
493 15 for my $arg (@c[$i+1 .. $#c]) {
494  
495 # NOTE: This escaping is going to be an utter pain to maintain...
496  
497 170 $arg =~ s/([`\$])/\\$1/g;
498  
499 170 ▶ if( $arg =~ m/[\s()]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /[\s()]/) 25 145 170
500 25 $arg =~ s/([\\"])/\\$1/g;
501 25 $arg = "\"$arg\"";
502 }
503 }
504 }
505 }
506 }
507 }
508  
509 7 my $beg = 0;
510 7 for my $i (@indexes_of_replacements) {
511 24 ▶ if( $c[$i] =~ s/^([\w.\-_]+)@// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c[$i] =~ s/^([\w.\-_]+)@//) { } 5 19 24
512 5 my $u = $1;
513 5 for(@c[$beg .. $i-1]) {
514 24 s/^(\[\%u\]|\[\](?=\%u))//;
515 24 ▶ $_ = $u if $_ eq '%u';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "%u" 8 16 24
516 }
517  
518 } else {
519 # NOTE: there's really no need to go through and remove [%u]
520 # conditional options, they'll automatically get nuked below
521 19 $c[$i] =~ s/\\@/@/g;
522 }
523 24 $beg = $i+1;
524 }
525  
526 7 delete $h{'%h'};
527  
528 } else {
529 12 $h{'%h'} =~ s/\\!/!/g;
530 }
531  
532 19 ▶ if( $h{'%h'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h{"%h"}) 12 7 19
533 12 ▶ $h{'%u'} = $1 if $h{'%h'} =~ s/^([\w.\-_]+)@//;
Branch: 1 branch
BranchTrueFalseTotal
if $h{"%h"} =~ s/^([\w.\-_]+)@// 2 10 12
534 12 $h{'%h'} =~ s/\\@/@/g;
535 }
536  
537 206 ▶ @c = map {exists $h{$_} ? $h{$_} : $_}
Branch: 1 branch
BranchTrueFalseTotal
exists $h{$_} ? : 23 183 206
538 232 ▶ map { m/^\[([^\[\]]+)\]/ ? ($h{$1} ? do{s/^\[\Q$1\E\]//; $_} : ()) : ($_) } ## no critic: why on earth not?
Branch: 2 branches
BranchTrueFalseTotal
/^\[([^\[\]]+)\]/ ? : 30 202 232
$h{$1} ? : 4 26 30
539 19 map { s/\[\]\%(\w+)/[\%$1]\%$1/; $_ } ## no critic: why on earth not?
540 @c;
541  
542 19 ▶ if( $this->{debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"debug"}) 0 19 19
543 0 local $" = ")(";
544 0 $this->std_msg($host, $h{'%n'}, 0, BOLD.BLACK."DEBUG: exec(@c)");
545 }
546  
547 19 return @c;
548 }
549 # }}}
550 # start_queue_on_host {{{
551 8 ▶sub start_queue_on_host {
Subroutine start_queue_on_host: called CC=1 SCAR=0.0
Pod undocumented
552 8 my ($this, $kernel => $host, $cmdno, $cmd, @next) = @_;
553  
554 # NOTE: used (and deleted) by subst_cmd_vars
555 8 $this->set_subst_vars(
556 '%h' => $host,
557 '%n' => $cmdno,
558 );
559  
560 my $kid = POE::Wheel::Run->new(
561 8 Program => [ my @debug_rq = ($this->subst_cmd_vars(@{$this->{_shell_cmd}} => @$cmd)) ],
562 StdoutEvent => "child_stdout",
563 StderrEvent => "child_stderr",
564 CloseEvent => "child_close",
565 );
566  
567 8 $kernel->sig_child( $kid->PID, "child_signal" );
568  
569 8 my $lineno = 0;
570 8 my $info = [ $kid, $host, $cmdno, \$lineno, @next ];
571 8 $this->{_wid}{ $kid->ID } = $this->{_pid}{ $kid->PID } = $info;
572  
573 8 return;
574 }
575 # }}}
576  
577 # poe_start {{{
578 3 ▶sub poe_start {
Subroutine poe_start: called CC=4 SCAR=13.9
Pod undocumented
579 3 my $this = shift;
580  
581 3 my %starting;
582 3 my @hosts = keys %{ $this->{_cmd_queue} };
583 3 for my $host (@hosts) {
584 6 my @c = @{ $this->{_cmd_queue}{$host} };
585  
586 6 $this->start_queue_on_host($_[KERNEL] => $host, 1, @c);
587 6 push @{$starting{"@{$c[0]}"}}, $host;
588 }
589  
590 3 for my $message (keys %starting) {
591 4 my @hosts = @{ $starting{$message} };
592  
593 4 ▶ if( @hosts == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@hosts == 1) { } 2 2 4
594 2 $this->std_msg($this->_host_route_to_nick($hosts[0]), 1, 0, BOLD.BLACK."-- starting: $message --");
595  
596 } else {
597 2 $this->std_msg("", 1, 0, BOLD.BLACK."-- starting: $message on @hosts --");
598 }
599 }
600  
601 3 delete $this->{_cmd_queue};
602  
603 3 return;
604 }
605 # }}}
606  
607 8 1;
↑ Summary script/mrsh »