blib/lib/Zoidberg/Contractor.pm

filter: Statementstmt 63.8% 337 / 528 Branchbran 44.4% 135 / 304 Conditioncond 34.6% 34 / 98 MC/DCmcdc 12.0% 7 / 58 Subroutinesub 66.0% 35 / 53 Podpod 90.0% 9 / 10 totaltotal 53.0% 557 / 1051 CC 211 SCAR 41.4
CC
211
Cov
54%
status_string
342.0
job_by_spec
306.0
reap_jobs
90.0
CRAP 62.9
« blib/lib/Zoidberg/Fish.pm ↑ Summary blib/lib/Zoidberg/StringParser.pm »
1 package Zoidberg::Contractor;
2  
3 64 our $VERSION = '0.981';
4  
5 64 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 64 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 64 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 64 ▶use Zoidberg::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 64 ▶no warnings; # yes, undefined == '' == 0
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Zoidberg::Contractor - Module to manage jobs
14  
15 =head1 SYNOPSIS
16  
17 use Zoidberg::Contractor;
18 my $c = Zoidberg::Contractor->new();
19
20 $c->shell_list( [qw(cat ./log)], '|', [qw(grep -i error)] );
21  
22 =head1 DESCRIPTION
23  
24 Zoidberg inherits from this module, it manages jobs.
25  
26 It uses Zoidberg::StringParser.
27  
28 Also it defines Zoidberg::Job and subclasses.
29  
30 FIXME lots of documentation
31  
32 =head1 METHODS
33  
34 =over 4
35  
36 =item new()
37  
38 Simple constructor, calls C<shell_init()>.
39  
40 =cut
41  
42 0 ▶sub new { # stub, to be overloaded
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
43 0 my $class = shift;
44 0 shell_init(bless {@_}, $class);
45 }
46  
47 =item shell_init()
48  
49 Initialises things like hashes with signal names and sets terminal control.
50 Should be called before usage when the constructor is overloaded.
51  
52 =cut
53  
54 # Job control code adapted from example code
55 # in the glibc manual <http://www.gnu.org/software/libc/manual>
56 # also some snippets from this manual include as comment blocks
57  
58 # A subshell that runs non-interactively cannot and should not support job control.
59  
60 61 ▶sub shell_init {
Subroutine shell_init: called CC=6 SCAR=21.9
Pod documented
61 61 my $self = shift;
62 61 ▶ bug 'Contractor can\'t live without a shell' unless $$self{shell};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"shell"} 0 61 61
63  
64 ## jobs stuff
65 61 $self->{jobs} = [];
66 61 $self->{_sighash} = {};
67 61 $self->{terminal} = fileno(STDIN);
68  
69 61 my @sno = split /[, ]/, $Config{sig_num};
70 61 my @sna = split /[, ]/, $Config{sig_name};
71 61 $self->{_sighash}{$sno[$_]} = $sna[$_] for (0..$#sno);
72  
73 61 ▶ if ($self->{shell}{settings}{interactive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'shell'}{'settings'}{'interactive'}) { } 0 61 61
74 # Loop check until we are in the foreground.
75 0 ▶ while (POSIX::tcgetpgrp($self->{terminal}) != ($self->{pgid} = getpgrp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (POSIX::tcgetpgrp($self->{"terminal"}) != ($self->{"pgid"} = getpgrp)) 0 0 0
76 # FIXME is this logic allright !??
77 0 CORE::kill($$self{_sighash}{TTIN}, -$self->{pgid}); # stop ourselfs
78 }
79 # ignore interactive and job control signals
80 0 $SIG{$_} = 'IGNORE' for qw/INT QUIT TSTP TTIN TTOU/;
81  
82 # And get terminal control
83 0 POSIX::tcsetpgrp($self->{terminal}, $self->{pgid});
84 0 $self->{tmodes} = POSIX::Termios->new;
85 0 $self->{tmodes}->getattr;
86 }
87 61 else { $self->{pgid} = getpgrp }
88  
89 61 return $self;
90 }
91  
92 =item round_up()
93  
94 Recursively calls the C<round_up()> function of all current jobs.
95  
96 =cut
97  
98 3 ▶sub round_up { $_->round_up() for @{$_[0]->{jobs}} }
Subroutine round_up: called CC=2 SCAR=6.9
Pod documented
99  
100 =item shell_list(@blocks)
101  
102 Executes a list of jobs and logic operators.
103  
104 =cut
105  
106 1301 ▶sub shell_list {
Subroutine shell_list: called CC=12 SCAR=26.3
Pod documented
107 1301 my ($self, @list) = grep {defined $_} @_;
108  
109 1301 my $save_fg_job = $$self{shell}{fg_job}; # could be undef
110  
111 1301 ▶ my $meta = (ref($list[0]) eq 'HASH') ? shift(@list) : {} ;
Branch: 1 branch
BranchTrueFalseTotal
ref $list[0] eq 'HASH' ? : 1265 36 1301
112 1301 ▶ return unless @list;
Branch: 1 branch
BranchTrueFalseTotal
unless @list 0 1301 1301
113  
114 1301 my @re;
115 1301 ▶ for (@list) { $_ = $$self{shell}->prepare_block($_) if ref $_ }
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ 1721 1464 3185
116 PARSE_LIST:
117 1337 ▶ return unless @list;
Branch: 1 branch
BranchTrueFalseTotal
unless @list 0 1337 1337
118 1337 ▶ if (ref $list[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $list[0]) { } 1301 36 1337
elsif (@{$self->{'jobs'};}) { } 0 36 36
119 1301 eval {
120 1301 my $j = Zoidberg::Job->new(%$meta, boss => $self, tree => \@list);
121 1299 ▶ @list = @{$$j{tree}} and goto PARSE_LIST if $$j{empty};
Branch: 1 branch
BranchTrueFalseTotal
if $j->{"empty"} 36 1263 1299
Condition60%3/5
$j->{'empty'} && ((@list = @{$j->{'tree'};}) && (goto PARSE_LIST))
!ll&&!rl&&r
1263360
@list = @{$j->{"tree"};} and goto PARSE_LIST
!ll
036
MC/DC0%0/2
$j->{'empty'} && ((@list = @{$j->{'tree'};}) && (goto PARSE_LIST))
$j->{'empty'} @list = @{$j->{"tree"};}
Truth table33%1/3
A && B
A= $j->{'empty'}B= @list = @{$j->{"tree"};}
ABresult
0X0
100
111
122 1263 ▶ unless ( $$meta{prepare} ) { @re = $j->exec() }
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{'prepare'}) { } 0 1263 1263
123 else {
124 0 $j->bg(); # put it in @jobs
125 0 $$j{bg} = 0;
126 }
127 };
128 1181 ▶ complain $@ if $@; # FIXME FIXME check eval {} blocks for redundancy
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1181 1181
129 }
130 36 elsif (@{$$self{jobs}}) {
131 0 debug 'enqueuing '.scalar(@list).' blocks';
132 0 push @{$$self{jobs}[-1]{tree}}, @list;
133 }
134 else {
135 36 debug 'no job to enqueu in, trying logic';
136 36 @list = $self->_logic($$self{shell}{error}, @list);
137 36 @re = $self->shell_list(@list);
138 }
139  
140 1215 $$self{shell}{fg_job} = $save_fg_job;
141  
142 1215 return @re;
143 }
144  
145 =item shell_job($block)
146  
147 Executes a single job.
148  
149 =cut
150  
151 0 ▶sub shell_job {
Subroutine shell_job: not called CC=3 SCAR=24.8
Pod documented
152 0 my ($self, $meta, $block) = @_;
153 0 ▶ $block = $meta unless ref($meta) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $meta eq "HASH" 0 0 0
154 0 my $save_fg_job = $$self{shell}{fg_job}; # could be undef
155 0 my @re;
156 0 eval {
157 0 my $j = Zoidberg::Job->new(%$meta, boss => $self, procs => [$block]);
158 0 @re = $j->exec()
159 };
160 0 ▶ complain $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
161 0 $$self{shell}{fg_job} = $save_fg_job;
162 0 return @re;
163 }
164  
165 =item reap_jobs()
166  
167 Checks for jobs that are finished and removes them from the job list.
168  
169 =cut
170  
171 0 ▶sub reap_jobs {
Subroutine reap_jobs: not called CC=9 SCAR=45.0
Pod documented
172 0 my $self = shift;
173 0 ▶ return unless @{$self->{jobs}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"jobs"};} 0 0 0
174 0 my (@completed, @running);
175 #debug 'reaping jobs';
176 0 for ( @{$self->{jobs}} ) {
177 0 ▶ next unless ref($_) =~ /Job/; # prohibit autogenerated faults
Branch: 1 branch
BranchTrueFalseTotal
unless ref($_) =~ /Job/ 0 0 0
178 0 $_->update_status;
179 0 ▶ if ($_->completed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->completed) { } 0 0 0
180 0 ▶ if (@{$$_{tree}}) { $self->reinc_job($_) } # reincarnate it
Branch: 1 branch
BranchTrueFalseTotal
if (@{$_->{'tree'};}) { } 0 0 0
181 0 else { push @completed, $_ }
182 }
183 0 else { push @running, $_ }
184 }
185 0 $self->{jobs} = \@running;
186 #debug 'body count: '.scalar(@completed);
187 0 ▶ if ($$self{shell}{settings}{interactive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"shell"}{"settings"}{"interactive"}) 0 0 0
188 ++$$_{completed} and message $_->status_string
189 0 ▶ for sort {$$a{id} <=> $$b{id}} grep {! $$_{no_notify}} @completed;
Branch: 1 branch
BranchTrueFalseTotal
++$_->{"completed"} and message($_->status_string) 0 0 0
190 }
191 }
192  
193 996 ▶sub reinc_job { # reincarnate
Subroutine reinc_job: called CC=2 SCAR=6.9
Pod undocumented
194 996 my ($self, $job) = @_;
195 996 debug "job \%$$job{id} reincarnates (error: $$job{error})";
196 996 my @b = $self->_logic($$job{error}, @{$$job{tree}});
197 996 $$job{tree} = [];
198 996 ▶ return unless @b;
Branch: 1 branch
BranchTrueFalseTotal
unless @b 721 275 996
199 275 debug @b. ' blocks left';
200 275 $self->shell_list({ map {($_ => $$job{$_})} qw#bg id capture wantarray# }, @b);
201 }
202  
203 1032 ▶sub _logic {
Subroutine _logic: called CC=7 SCAR=19.5
204 1032 my ($self, $error, @list) = @_;
205 1032 ▶ my $op = ref( $list[0] ) ? 'EOS' : shift @list ;
Branch: 1 branch
BranchTrueFalseTotal
ref $list[0] ? : 0 1032 1032
206 # mind that logic grouping for AND and OR isn't the same, OR is stronger
207 1032 ▶ while ( $error ? ( $op eq 'AND' ) : ( $op eq 'OR' ) ) { # skip
Branch: 2 branches
BranchTrueFalseTotal
if ($error ? $op eq "AND" : $op eq "OR") 76 1032 1108
$error ? : 202 906 1108
208 76 my $i = 0;
209 76 ▶ while ( ref $list[0] or $list[0] eq 'AND' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $list[0] or $list[0] eq "AND") 76 76 152
Condition66%2/3
ref $list[0] or $list[0] eq "AND"
l!l&&r!l&&!r
76076
MC/DC50%1/2
ref $list[0] or $list[0] eq "AND"
ref $list[0] $list[0] eq "AND"
Truth table66%2/3
A or B
A= ref $list[0]B= $list[0] eq "AND"
ABresult
1X1
011
000
210 76 shift @list;
211 76 $i++;
212 }
213 76 ▶ debug( ($error ? 'error' : 'no error') . " => $i blocks skipped" );
Branch: 1 branch
BranchTrueFalseTotal
$error ? : 74 2 76
214 76 $op = shift @list;
215 }
216 1032 return @list;
217 }
218  
219 # ############# #
220 # info routines #
221 # ############# #
222  
223 =item job_by_id($id)
224  
225 Returns a job object based on the (numeric) id.
226  
227 (Note that the job list is un-ordered,
228 so the id and the index are not usually identical.)
229
230 =item job_by_spec($string)
231  
232 Returns a job object based on a string.
233 The following formats are supported:
234  
235 =over 4
236  
237 =item %I<integer>
238  
239 Job with id I<integer>
240  
241 =item %+
242  
243 Current job
244  
245 =item %-
246  
247 Previous job
248  
249 =item %?I<string>
250  
251 Last job matching I<string>
252  
253 =item %I<string>
254  
255 Last job starting with I<string>
256  
257 =back
258  
259 =item sig_by_spec($string)
260  
261 Returns the signal number for a named signal
262 or undef if no such signal exists.
263  
264 =cut
265  
266 0 ▶sub job_by_id {
Subroutine job_by_id: not called CC=3 SCAR=24.8
Pod documented
267 0 my ($self, $id) = @_;
268 0 ▶ for (@{$$self{jobs}}) { return $_ if $$_{id} eq $id }
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"id"} eq $id 0 0 0
269 0 return undef;
270 }
271  
272 0 ▶sub job_by_spec {
Subroutine job_by_spec: not called CC=17 SCAR=57.2
Pod documented
273 0 my ($self, $spec) = @_;
274 0 ▶ return @{$$self{jobs}} ? $$self{jobs}[-1] : undef unless $spec;
Branch: 2 branches
BranchTrueFalseTotal
unless $spec 0 0 0
@{$self->{'jobs'};} ? : 0 0 0
275 # see posix 1003.2 speculation for arbitrary cruft
276 0 ▶ $spec = '%+' if $spec eq '%%' or $spec eq '%';
Branch: 1 branch
BranchTrueFalseTotal
if $spec eq "%%" or $spec eq "%" 0 0 0
Condition0%0/3
$spec eq "%%" or $spec eq "%"
l!l&&r!l&&!r
000
MC/DC0%0/2
$spec eq "%%" or $spec eq "%"
$spec eq "%%" $spec eq "%"
Truth table0%0/3
A or B
A= $spec eq "%%"B= $spec eq "%"
ABresult
1X1
011
000
277 0 $spec =~ /^ \%? (?: (\d+) | ([\+\-\?]?) (.*) ) $/x;
278 0 my ($id, $q, $string) = ($1, $2, $3);
279 0 ▶ if ($id) {
Branch: 4 branches
BranchTrueFalseTotal
if ($id) { } 0 0 0
elsif ($q eq '+') { } 0 0 0
elsif ($q eq '-') { } 0 0 0
elsif ($q eq '?') { } 0 0 0
280 0 ▶ for (@{$$self{jobs}}) { return $_ if $$_{id} == $id }
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"id"} == $id 0 0 0
281 }
282 0 ▶ elsif ($q eq '+') { return $$self{jobs}[-1] if @{$$self{jobs}} }
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"jobs"};} 0 0 0
283 0 ▶ elsif ($q eq '-') { return $$self{jobs}[-2] if @{$$self{jobs}} > 1 }
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"jobs"};} > 1 0 0 0
284 elsif ($q eq '?') {
285 0 ▶ for (reverse @{$$self{jobs}}) { return $_ if $$_{zoidcmd} =~ /$string/ }
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"zoidcmd"} =~ /$string/ 0 0 0
286 }
287 else {
288 0 ▶ for (reverse @{$$self{jobs}}) { return $_ if $$_{zoidcmd} =~ /^\W*$string/ }
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"zoidcmd"} =~ /^\W*$string/ 0 0 0
289 }
290 0 return undef;
291 }
292  
293 972 ▶sub sig_by_spec {
Subroutine sig_by_spec: called CC=4 SCAR=14.0
Pod documented
294 972 my ($self, $z) = @_;
295 972 ▶ return $z if exists $$self{_sighash}{$z};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_sighash"}{$z} 0 972 972
296 972 $z =~ s{^(sig)?(.*)$}{uc($2)}ei;
297 972 ▶ while (my ($k, $v) = each %{$$self{_sighash}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"_sighash"};}) 63180 972 64152
298 63180 ▶ return $k if $v eq $z
Branch: 1 branch
BranchTrueFalseTotal
if $v eq $z 0 63180 63180
299 }
300 972 return undef;
301 }
302  
303  
304 # ########### #
305 # Job objects #
306 # ########### #
307  
308 package Zoidberg::Job;
309  
310 64 our $VERSION = '0.981';
311  
312 64 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
313 64 ▶use vars '$AUTOLOAD';
Subroutine BEGIN: called CC=1 SCAR=0.0
314 64 ▶use POSIX qw/:sys_wait_h :signal_h/;
Subroutine BEGIN: called CC=1 SCAR=0.0
315 64 ▶use Zoidberg::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
316  
317 use overload
318 0 ▶ '@{}' => sub { $_[0]->{tree} },
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=6.9
319 64 fallback => 'TRUE';
320  
321 64 our @ISA = qw/Zoidberg::Contractor/;
322  
323 =back
324  
325 =head1 JOBS
326  
327 Jobs are objects of the class C<Zoidberg::Job> or a subclass of this class.
328  
329 This object AUTOLOADS methods to process signals. For example:
330  
331 $job->TERM(); # is identical to
332 $job->kill('TERM');
333  
334 =head2 Methods
335  
336 The job obbjects have the following methods:
337  
338 =over 4
339  
340 =item new()
341  
342 Simple constructor.
343  
344 =item exec()
345  
346 Execute the job.
347  
348 =item round_up()
349  
350 Recursively kill the job, ends all child processes forcefully.
351  
352 =cut
353  
354 1301 ▶sub new { # @_ should at least contain 'boss' and either 'proc' or 'tree'
Subroutine new: called CC=19 SCAR=31.3
355 1301 shift; # class
356 1301 my $self = { new => 1, id => 0, procs => [], @_ };
357 1301 ▶ $$self{shell} ||= $$self{boss}{shell};
Condition33%1/3
$self->{"shell"} ||= $self->{"boss"}{"shell"}
l!l&&r!l&&!r
013010
MC/DC0%0/2
$self->{"shell"} ||= $self->{"boss"}{"shell"}
$self->{"shell"} $self->{"boss"}{"shell"}
Truth table33%1/3
A ||= B
A= $self->{"shell"}B= $self->{"boss"}{"shell"}
ABresult
1X1
011
000
358 1301 ▶ $$self{$_} ||= [] for qw/jobs tree/;
Condition100%2/2
$self->{$_} ||= []
l!l
13011301
MC/DC100%1/1
$self->{$_} ||= []
$self->{$_}
Truth table100%2/2
A
A= $self->{$_}
Aresult
11
00
359 1301 $$self{$_} = $$self{boss}{$_} for qw/_sighash terminal/; # FIXME check this
360  
361 1301 ▶ if ($$self{tree}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tree"}) 1301 0 1301
362 1301 ▶ while ( ref $$self{tree}[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"tree"}[0]) 1309 1299 2608
363 1309 my @b = grep {defined $_} $$self{shell}->parse_block(shift @{$$self{tree}}); # FIXME breaks interface, should be a hook
364 0 ▶ if (@b > 1) { unshift @{$$self{tree}}, @b } # probably macro expansion
Branch: 1 branch
BranchTrueFalseTotal
if (@b > 1) { } 0 1307 1307
365 1307 else { push @{$$self{procs}}, @b }
366 }
367 1299 ▶ $$self{bg}++ if $$self{tree}[0] eq 'EOS_BG';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tree"}[0] eq "EOS_BG" 0 1299 1299
368 }
369  
370 1299 ▶ return bless {%$self, empty => 1}, 'Zoidberg::Job' unless @{$$self{procs}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"procs"};} 36 1263 1299
371 1263 debug 'blocks in job ', $$self{procs};
372 1263 my $pipe = @{$$self{procs}} > 1;
373 1263 ▶ $$self{string} ||= ($pipe ? '|' : '') . $$self{procs}[-1][0]{string};
Branch: 1 branch
BranchTrueFalseTotal
$pipe ? : 4 1259 1263
Condition66%2/3
$self->{"string"} ||= ($pipe ? "|" : "") . $self->{"procs"}[-1][0]{"string"}
l!l&&r!l&&!r
0119766
MC/DC50%1/2
$self->{"string"} ||= ($pipe ? "|" : "") . $self->{"procs"}[-1][0]{"string"}
$self->{"string"} ($pipe ? "|" : "") . $self->{"procs"}[-1][0]{"string"}
Truth table66%2/3
A ||= B
A= $self->{"string"}B= ($pipe ? "|" : "") . $self->{"procs"}[-1][0]{"string"}
ABresult
1X1
011
000
374 1263 ▶ $$self{zoidcmd} ||= $$self{procs}[-1][0]{zoidcmd};
Condition33%1/3
$self->{"zoidcmd"} ||= $self->{"procs"}[-1][0]{"zoidcmd"}
l!l&&r!l&&!r
012630
MC/DC0%0/2
$self->{"zoidcmd"} ||= $self->{"procs"}[-1][0]{"zoidcmd"}
$self->{"zoidcmd"} $self->{"procs"}[-1][0]{"zoidcmd"}
Truth table33%1/3
A ||= B
A= $self->{"zoidcmd"}B= $self->{"procs"}[-1][0]{"zoidcmd"}
ABresult
1X1
011
000
375 1263 ▶ $$self{pwd} ||= $$self{procs}[0][0]{env}{pwd} || $ENV{PWD};
Condition33%2/6
$self->{"pwd"} ||= $self->{"procs"}[0][0]{"env"}{"pwd"} || $ENV{"PWD"}
l!l&&r!l&&!r
012630
$self->{'procs'}[0][0]{'env'}{'pwd'} || $ENV{'PWD'}
l!l&&r!l&&!r
126300
MC/DC0%0/3
$self->{"pwd"} ||= $self->{"procs"}[0][0]{"env"}{"pwd"} || $ENV{"PWD"}
$self->{"pwd"} $self->{'procs'}[0][0]{'env'}{'pwd'} $ENV{'PWD'}
Truth table0%0/4
A ||= B || C
A= $self->{"pwd"}B= $self->{'procs'}[0][0]{'env'}{'pwd'}C= $ENV{'PWD'}
ABCresult
1XX1
01X1
0011
0000
376
377 1263 my $meta = $$self{procs}[0][0];
378 1263 ▶ unless ($pipe || ( defined($$meta{fork_job}) ? $$meta{fork_job} : 0 ) || $$self{bg}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pipe or defined $meta->{'fork_job'} ? $meta->{'fork_job'} : 0 or $self->{'bg'}) { } 1017 246 1263
defined $meta->{'fork_job'} ? : 1013 246 1259
Condition83%5/6
$pipe or defined $meta->{"fork_job"} ? $meta->{"fork_job"} : 0 or $self->{"bg"}
l!l&&r!l&&!r
10170246
$pipe or defined $meta->{"fork_job"} ? $meta->{"fork_job"} : 0
l!l&&r!l&&!r
41013246
MC/DC66%2/3
$pipe or defined $meta->{"fork_job"} ? $meta->{"fork_job"} : 0 or $self->{"bg"}
$pipe defined $meta->{"fork_job"} ? $meta->{"fork_job"} : 0 $self->{"bg"}
Truth table75%3/4
A or B or C
A= $pipeB= defined $meta->{"fork_job"} ? $meta->{"fork_job"} : 0C= $self->{"bg"}
ABCresult
1XX1
01X1
0011
0000
379 246 bless $self, 'Zoidberg::Job::builtin'
380 }
381 1017 else { bless $self, 'Zoidberg::Job' }
382  
383 1263 return $self;
384 }
385  
386 1263 ▶sub exec {
Subroutine exec: called CC=12 SCAR=27.8
387 1263 ▶ die unless ref($_[0]); # check against deprecated api
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 0 1263 1263
388 1263 my $self = shift;
389 0 ▶ if (ref $_[0]) { %$self = (%$self, %{$_[0]}) }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) 0 1263 1263
390  
391 1263 ▶ message $self->status_string('Running') if $$self{prepare};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"prepare"} 0 1263 1263
392 1263 $$self{new} = 0;
393  
394 1263 ▶ return unless @{$$self{procs}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"procs"};} 0 1263 1263
395 1263 local $ENV{ZOIDREF} = "$$self{shell}";
396  
397 1263 my @re = eval { $self->_run };
398 1182 ▶ if ($$self{error}) { $$self{shell}{error} = $$self{error} }
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'error'}) { } 118 1064 1182
elsif ($@) { } 10 1054 1064
399 elsif ($@) {
400 10 complain;
401 10 ▶ my $error = ref($@) ? $@ : bless { string => ($@ || 'Error') }, 'Zoidberg::Utils::Error';
Branch: 1 branch
BranchTrueFalseTotal
ref $@ ? : 10 0 10
Condition0%0/2
$@ || 'Error'
l!l
00
MC/DC0%0/1
$@ || 'Error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
402 10 $error->PROPAGATE(); # just for the record
403 10 $$self{error} = $$self{shell}{error} = $error;
404 }
405 1054 else { delete $$self{shell}{error} }
406  
407 1182 ▶ if ($self->completed()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->completed) 1182 0 1182
408 1182 $$self{shell}->broadcast('envupdate'); # FIXME breaks interface
409 1182 ▶ $$self{boss}->reinc_job($self) if @{ $$self{tree} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"tree"};} 164 1018 1182
410 }
411  
412 1181 ▶ if ( $$self{tree}[0] eq 'EOS_BG' ) { # step over it - FIXME conflicts with fg_job
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tree"}[0] eq "EOS_BG") 0 1181 1181
413 0 shift @{$$self{tree}};
414 0 my $ref = $$self{tree};
415 0 $$self{tree} = [];
416 0 $$self{boss}->shell_list(@$ref);
417 }
418  
419 1181 return @re;
420 }
421  
422 0 ▶sub round_up {
Subroutine round_up: not called CC=2 SCAR=17.9
423 0 $_[0]->kill('HUP', 'WIPE');
424 0 $_->round_up() for @{$_[0]->{jobs}};
425 }
426  
427 # ######## #
428 # Run code #
429 # ######## #
430  
431 # As each process is forked, it should put itself in the new process group by calling setpgid
432 # The shell should also call setpgid to put each of its child processes into the new process
433 # group. This is because there is a potential timing problem: each child process must be put
434 # in the process group before it begins executing a new program, and the shell depends on
435 # having all the child processes in the group before it continues executing. If both the child
436 # processes and the shell call setpgid, this ensures that the right things happen no matter
437 # which process gets to it first.
438  
439 # If the job is being launched as a foreground job, the new process group also needs to be
440 # put into the foreground on the controlling terminal using tcsetpgrp. Again, this should be
441 # done by the shell as well as by each of its child processes, to avoid race conditions.
442  
443 1017 ▶sub _run {
Subroutine _run: called CC=22 SCAR=32.1
444 1017 my $self = shift;
445 1017 $$self{shell}{fg_job} = $self;
446  
447 1017 $self->{tmodes} = POSIX::Termios->new;
448  
449 1017 ▶ $self->{procs}[-1][0]{last}++ unless $$self{capture}; # don't close the pipeline if capturing
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"capture"} 985 32 1017
450  
451 1017 my ($pid, @pipe, $stdin, $stdout);
452 1017 my $zoidpid = $$;
453 1017 $stdin = fileno STDIN;
454  
455 # use pgid of boss when boss is part of a pipeline
456 1017 ▶ $$self{pgid} = $$self{boss}{pgid} unless $$self{shell}{settings}{interactive};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"shell"}{"settings"}{"interactive"} 1017 0 1017
457  
458 1017 my $i = 0;
459 1017 for my $proc (@{$self->{procs}}) {
460 1022 $i++;
461 1022 ▶ if ($$proc[0]{last}) { $stdout = fileno STDOUT }
Branch: 1 branch
BranchTrueFalseTotal
if ($proc->[0]{'last'}) { } 983 39 1022
462 else { # open pipe to next process
463 39 @pipe = POSIX::pipe;
464 39 $stdout = $pipe[1];
465 }
466  
467 1022 $pid = fork; # fork process
468 1022 ▶ if ($pid) { # parent process
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 958 64 1022
469 # set pid and pgid
470 958 $$proc[0]{pid} = $pid;
471 958 ▶ $self->{pgid} ||= $pid ;
Condition33%1/3
$self->{"pgid"} ||= $pid
l!l&&r!l&&!r
95800
MC/DC0%0/2
$self->{"pgid"} ||= $pid
$self->{"pgid"} $pid
Truth table33%1/3
A ||= B
A= $self->{"pgid"}B= $pid
ABresult
1X1
011
000
472 958 POSIX::setpgid($pid, $self->{pgid});
473 958 debug "job \%$$self{id} part $i has pid $pid and pgid $$self{pgid}";
474 # set terminal control
475 POSIX::tcsetpgrp($self->{shell}{terminal}, $self->{pgid})
476 958 ▶ if $$self{shell}{settings}{interactive} && ! $$self{bg};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shell"}{"settings"}{"interactive"} and not $self->{"bg"} 0 958 958
Condition33%1/3
$self->{"shell"}{"settings"}{"interactive"} and not $self->{"bg"}
!ll&&!rl&&r
95800
MC/DC0%0/2
$self->{"shell"}{"settings"}{"interactive"} and not $self->{"bg"}
$self->{"shell"}{"settings"}{"interactive"} not $self->{"bg"}
Truth table33%1/3
A and not(B)
A= $self->{"shell"}{"settings"}{"interactive"}B= not $self->{"bg"}
ABresult
0X0
100
111
477 }
478 else { # child process
479 # set pgid
480 64 ▶ $self->{pgid} ||= $$; # after first pgid is set allready
Condition33%1/3
$self->{"pgid"} ||= $$
l!l&&r!l&&!r
6400
MC/DC0%0/2
$self->{"pgid"} ||= $$
$self->{"pgid"} $$
Truth table33%1/3
A ||= B
A= $self->{"pgid"}B= $$
ABresult
1X1
011
000
481 64 POSIX::setpgid($$, $self->{pgid});
482 # set terminal control
483 POSIX::tcsetpgrp($self->{shell}{terminal}, $self->{pgid})
484 64 ▶ if $$self{shell}{settings}{interactive} && ! $$self{bg};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shell"}{"settings"}{"interactive"} and not $self->{"bg"} 0 64 64
Condition33%1/3
$self->{"shell"}{"settings"}{"interactive"} and not $self->{"bg"}
!ll&&!rl&&r
6400
MC/DC0%0/2
$self->{"shell"}{"settings"}{"interactive"} and not $self->{"bg"}
$self->{"shell"}{"settings"}{"interactive"} not $self->{"bg"}
Truth table33%1/3
A and not(B)
A= $self->{"shell"}{"settings"}{"interactive"}B= not $self->{"bg"}
ABresult
0X0
100
111
485 # and run child
486 64 $ENV{ZOIDPID} = $zoidpid;
487 64 eval { $self->_run_child($proc, $stdin, $stdout) };
488 7 ▶ my $error = $@ || 0;
Condition100%2/2
$@ || 0
l!l
25
MC/DC100%1/1
$@ || 0
$@
Truth table100%2/2
A
A= $@
Aresult
11
00
489 7 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 2 5 7
490 2 complain;
491 2 ▶ $error = ref($error) ? ($$error{exit_status} || 1) : 1 if $error;
Branch: 2 branches
BranchTrueFalseTotal
if $error 2 0 2
ref $error ? : 2 0 2
Condition50%1/2
$error->{"exit_status"} or 1
l!l
02
MC/DC0%0/1
$error->{"exit_status"} or 1
$error->{"exit_status"}
Truth table50%1/2
A
A= $error->{"exit_status"}
Aresult
11
00
492 }
493 7 exit $error; # exit child process
494 }
495  
496 958 ▶ POSIX::close($stdin) unless $stdin == fileno STDIN ;
Branch: 1 branch
BranchTrueFalseTotal
unless $stdin == fileno STDIN 3 955 958
497 958 ▶ POSIX::close($stdout) unless $stdout == fileno STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
unless $stdout == fileno STDOUT 32 926 958
498 958 ▶ $stdin = $pipe[0] unless $$proc[0]{last} ;
Branch: 1 branch
BranchTrueFalseTotal
unless $proc->[0]{"last"} 32 926 958
499 }
500  
501 my @re = $$self{bg} ? $self->bg
502 953 ▶ : $$self{capture} ? ($self->_capture($stdin)) : $self->fg ;
Branch: 2 branches
BranchTrueFalseTotal
$self->{'bg'} ? : 0 953 953
$self->{'capture'} ? : 27 926 953
503
504 # postrun
505 936 POSIX::tcsetpgrp($$self{shell}{terminal}, $$self{shell}{pgid});
506  
507 936 return @re;
508 }
509  
510 27 ▶sub _capture { # called in parent when capturing
Subroutine _capture: called CC=5 SCAR=16.5
511 27 my ($self, $stdin) = @_;
512 local $/ = (exists $ENV{RS} and defined $ENV{RS})
513 27 ▶ ? $ENV{RS} : "\n" ; # Record Separator
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'RS'} && defined $ENV{'RS'} ? : 0 27 27
Condition33%1/3
exists $ENV{"RS"} and defined $ENV{"RS"}
!ll&&!rl&&r
2700
MC/DC0%0/2
exists $ENV{"RS"} and defined $ENV{"RS"}
exists $ENV{"RS"} defined $ENV{"RS"}
Truth table33%1/3
A and B
A= exists $ENV{"RS"}B= defined $ENV{"RS"}
ABresult
0X0
100
111
514 27 debug "capturing output from fd $stdin, \$/ = '$/'";
515 27 open IN, "<&=$stdin"; # open file descriptor
516 27 my @re = (<IN>);
517 27 close IN;
518 27 ▶ POSIX::close($stdin) unless $stdin == fileno STDIN ;
Branch: 1 branch
BranchTrueFalseTotal
unless $stdin == fileno STDIN 27 0 27
519 27 $self->wait_job; # job should be dead by now
520 27 ▶ return $$self{wantarray} ? (map {chomp $_; $_} @re) : (join '', @re);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'wantarray'} ? : 9 18 27
521 }
522  
523 64 ▶sub _run_child { # called in child process
Subroutine _run_child: called CC=3 SCAR=11.0
524 64 my $self = shift;
525 64 my ($block, $stdin, $stdout) = @_;
526  
527 64 $self->{shell}{round_up} = 0;
528 64 $self->{shell}{settings}{interactive} = 0;
529 64 map { $SIG{$_} = 'DEFAULT' } qw{INT QUIT TSTP TTIN TTOU};
530  
531 # make sure stdin and stdout are right, else dup them
532 64 for ([$stdin, fileno STDIN], [$stdout, fileno STDOUT]) {
533 128 ▶ next if $_->[0] == $_->[1];
Branch: 1 branch
BranchTrueFalseTotal
if $_->[0] == $_->[1] 119 9 128
534 9 POSIX::dup2(@$_);
535 9 POSIX::close($_->[0]);
536 }
537  
538 64 $self->_set_env($block);
539  
540 # here we go ... finally
541 64 $$self{shell}->eval_block($block); # FIXME should be hook
542 }
543  
544 # ##################### #
545 # Execution environment #
546 # ##################### #
547  
548 310 ▶sub _set_env {
Subroutine _set_env: called CC=22 SCAR=38.5
549 310 my ($self, $block) = @_;
550  
551 # check the pwd we want
552 310 my $pwd = $$block[0]{env}{pwd};
553 310 ▶ if ($pwd and $pwd ne $ENV{PWD}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pwd and $pwd ne $ENV{"PWD"}) 0 310 310
Condition33%1/3
$pwd and $pwd ne $ENV{"PWD"}
!ll&&!rl&&r
03100
MC/DC0%0/2
$pwd and $pwd ne $ENV{"PWD"}
$pwd $pwd ne $ENV{"PWD"}
Truth table33%1/3
A and B
A= $pwdB= $pwd ne $ENV{"PWD"}
ABresult
0X0
100
111
554 0 debug "Changing pwd to: $pwd";
555 0 ▶ chdir $pwd or error "Could not change dir to: $pwd";
Branch: 1 branch
BranchTrueFalseTotal
chdir $pwd or error("Could not change dir to: $pwd") 0 0 0
556 0 $$self{pwd} = $pwd;
557 }
558  
559 # variables
560 310 my @save_env;
561 310 ▶ while (my ($env, $val) = each %{$$block[0]{env}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$block[0]{"env"};}) 662 310 972
562 662 debug "env $env, val $val";
563 662 push @save_env, [$env, $ENV{$env}];
564 662 $ENV{$env} = $val;
565 }
566 310 ▶ return [\@save_env, []] unless $$block[0]{fd};
Branch: 1 branch
BranchTrueFalseTotal
unless $block->[0]{"fd"} 287 23 310
567  
568 # redirection
569 23 my @save_fd;
570 23 for my $fd (@{$$block[0]{fd}}) { # FIXME allow for IO objects
571 9 my $newfd;
572 9 ▶ $fd =~ m#^(\w*)(\W+)(.*)# or error "wrongly formatted redirection: $fd";
Branch: 1 branch
BranchTrueFalseTotal
$fd =~ /^(\w*)(\W+)(.*)/ or error("wrongly formatted redirection: $fd") 9 0 9
573 9 my ($n, $op, $f) = ($1, $2, $3);
574 9 ▶ $n ||= ($op =~ />/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$op =~ />/ ? : 0 0 0
Condition33%1/3
$n ||= $op =~ />/ ? 1 : 0
l!l&&r!l&&!r
900
MC/DC0%0/2
$n ||= $op =~ />/ ? 1 : 0
$n $op =~ />/ ? 1 : 0
Truth table33%1/3
A ||= B
A= $nB= $op =~ />/ ? 1 : 0
ABresult
1X1
011
000
575 9 ▶ if ($op =~ /&=?$/) { # our dupping logic differs from open()
Branch: 1 branch
BranchTrueFalseTotal
if ($op =~ /&=?$/) { } 2 7 9
576 0 ▶ if (! $f) { $newfd = 1 }
Branch: 2 branches
BranchTrueFalseTotal
if (not $f) { } 0 2 2
elsif ($f =~ /^\d+$/) { } 2 0 2
577 2 elsif ($f =~ /^\d+$/) { $newfd = $f }
578 else {
579 64 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
580 my $class = $$self{shell}{settings}{perl}{namespace}
581 0 ▶ || 'Zoidberg::Eval';
Condition0%0/2
$self->{'shell'}{'settings'}{'perl'}{'namespace'} || 'Zoidberg::Eval'
l!l
00
MC/DC0%0/1
$self->{'shell'}{'settings'}{'perl'}{'namespace'} || 'Zoidberg::Eval'
$self->{'shell'}{'settings'}{'perl'}{'namespace'}
Truth table0%0/2
A
A= $self->{'shell'}{'settings'}{'perl'}{'namespace'}
Aresult
11
00
582 0 $newfd = fileno *{$class.'::'.$f};
583 0 ▶ error $f.': no such filehandle' unless $newfd;
Branch: 1 branch
BranchTrueFalseTotal
unless $newfd 0 0 0
584 }
585 }
586 else {
587 7 ▶ error 'redirection needs argument' unless $f;
Branch: 1 branch
BranchTrueFalseTotal
unless $f 0 7 7
588 error $f.': cannot overwrite existing file'
589 if $op eq '>'
590 and $$self{shell}{settings}{noclobber}
591 7 ▶ and -e $f;
Branch: 1 branch
BranchTrueFalseTotal
if $op eq ">" and $self->{"shell"}{"settings"}{"noclobber"} and -e $f 0 7 7
Condition50%3/6
$op eq ">" and $self->{"shell"}{"settings"}{"noclobber"} and -e $f
!ll&&!rl&&r
700
$op eq ">" and $self->{"shell"}{"settings"}{"noclobber"}
!ll&&!rl&&r
160
MC/DC0%0/3
$op eq ">" and $self->{"shell"}{"settings"}{"noclobber"} and -e $f
$op eq ">" $self->{"shell"}{"settings"}{"noclobber"} -e $f
Truth table50%2/4
A and B and C
A= $op eq ">"B= $self->{"shell"}{"settings"}{"noclobber"}C= -e $f
ABCresult
0XX0
10X0
1100
1111
592 7 ▶ $op = '>' if $op eq '>!';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq ">!" 0 7 7
593 7 debug "redirecting fd $n to $op$f";
594 7 my $fh; # undefined scalar => new anonymous filehandle on open()
595 7 ▶ open($fh, $op.$f) || error "Failed to open $op$f";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $op . $f or error("Failed to open $op$f") 7 0 7
596 7 ($f, $newfd) = ($fh, fileno $fh); # re-using $f to have object in outer scope
597 }
598 9 debug "dupping fd $newfd to $n";
599 9 push @save_fd, [POSIX::dup($n), $n];
600 9 ▶ POSIX::dup2($newfd, $n) || error "Failed to dup $newfd to $n";
Branch: 1 branch
BranchTrueFalseTotal
POSIX::dup2($newfd, $n) or error("Failed to dup $newfd to $n") 9 0 9
601 }
602  
603 23 return [\@save_env, \@save_fd];
604 }
605  
606 246 ▶sub _restore_env {
Subroutine _restore_env: called CC=5 SCAR=20.9
607 246 my ($save_env, $save_fd) = @{ pop @_ };
608  
609 246 for (@$save_fd) {
610 0 POSIX::dup2(@$_);
611 0 POSIX::close($_->[0]);
612 }
613  
614 246 ▶ if (my ($PWD) = grep {$$_[0] eq 'PWD'} @$save_env) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($PWD) = grep({$_->[0] eq "PWD";} @$save_env)) 0 246 246
615 0 debug "Changing pwd back to: $$PWD[1]";
616 0 ▶ chdir $$PWD[1] or error "Could not change dir to: $$PWD[1]";
Branch: 1 branch
BranchTrueFalseTotal
chdir $PWD->[1] or error("Could not change dir to: $PWD->[1]") 0 0 0
617 }
618 246 $ENV{$$_[0]} = $$_[1] for @$save_env;
619 }
620  
621 # ########### #
622 # Signal code #
623 # ########### #
624  
625 =item fg()
626  
627 Take terminal control and run this job in the foreground.
628  
629 =item bg()
630  
631 Run this job in the background.
632  
633 =cut
634  
635 926 ▶sub fg {
Subroutine fg: called CC=12 SCAR=32.2
636 926 my $self = shift;
637  
638 926 ▶ if ($$self{new}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"new"}) 0 926 926
639 0 unshift @_, $self;
640 0 goto &exec;
641 }
642  
643 926 ▶ message $self->status_string('Running') if $$self{bg};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"bg"} 0 926 926
644 926 $$self{bg} = 0;
645  
646 0 @{$$self{boss}{jobs}} = grep {$_ ne $self} @{$$self{boss}{jobs}};
647 926 $$self{shell}{fg_job} = $self;
648  
649 POSIX::tcsetpgrp($self->{shell}{terminal}, $self->{pgid})
650 926 ▶ if $self->{shell}{settings}{interactive};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shell"}{"settings"}{"interactive"} 0 926 926
651  
652 926 ▶ if ($self->{stopped}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stopped"}) 0 926 926
653 0 CORE::kill(SIGCONT, -$self->{pgid});
654 0 $self->{stopped} = 0;
655 }
656 926 $self->wait_job;
657  
658 POSIX::tcsetpgrp($self->{shell}{terminal}, $self->{shell}{pgid})
659 926 ▶ if $self->{shell}{settings}{interactive};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shell"}{"settings"}{"interactive"} 0 926 926
660
661 926 ▶ if ($$self{stopped} or $$self{terminated}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stopped"} or $self->{"terminated"}) 0 926 926
Condition33%1/3
$self->{"stopped"} or $self->{"terminated"}
l!l&&r!l&&!r
00926
MC/DC0%0/2
$self->{"stopped"} or $self->{"terminated"}
$self->{"stopped"} $self->{"terminated"}
Truth table33%1/3
A or B
A= $self->{"stopped"}B= $self->{"terminated"}
ABresult
1X1
011
000
662 0 ▶ if ($$self{stopped} and $$self{shell}{settings}{notify_verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'stopped'} and $self->{'shell'}{'settings'}{'notify_verbose'}) { } 0 0 0
Condition0%0/3
$self->{"stopped"} and $self->{"shell"}{"settings"}{"notify_verbose"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"stopped"} and $self->{"shell"}{"settings"}{"notify_verbose"}
$self->{"stopped"} $self->{"shell"}{"settings"}{"notify_verbose"}
Truth table0%0/3
A and B
A= $self->{"stopped"}B= $self->{"shell"}{"settings"}{"notify_verbose"}
ABresult
0X0
100
111
663 0 $$self{shell}->jobs();
664 }
665 else {
666 0 message $self->status_string;
667 }
668 }
669  
670 926 ▶ if ($self->completed()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->completed) 926 0 926
671 926 $$self{shell}->broadcast('envupdate'); # FIXME breaks interface
672 926 ▶ $$self{boss}->reinc_job($self) if @{ $$self{tree} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"tree"};} 832 94 926
673 }
674 }
675  
676 0 ▶sub bg {
Subroutine bg: not called CC=2 SCAR=17.9
677 0 my $self = shift;
678 0 $self->_register_bg;
679  
680 0 ▶ if ($self->{stopped}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stopped"}) 0 0 0
681 0 CORE::kill(SIGCONT => -$self->{pgid});
682 0 $self->{stopped} = 0;
683 }
684  
685 0 message $self->status_string;
686 }
687  
688 0 ▶sub _register_bg { # register oneself as a background job
Subroutine _register_bg: not called CC=4 SCAR=30.0
689 0 my $self = shift;
690  
691 0 ▶ unless ($$self{id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"id"}) 0 0 0
692 0 ▶ $$_{id} > $$self{id} and $$self{id} = $$_{id} for @{$$self{boss}{jobs}};
Branch: 1 branch
BranchTrueFalseTotal
$_->{"id"} > $self->{"id"} and $self->{"id"} = $_->{"id"} 0 0 0
693 0 $$self{id}++;
694 }
695  
696 0 @{$$self{boss}{jobs}} = grep {$_ ne $self} @{$$self{boss}{jobs}};
697 0 push @{$$self{boss}{jobs}}, $self;
698  
699 0 $self->{bg} = 1;
700 }
701  
702 # FIXME wait code when not interactive
703  
704 953 ▶sub wait_job {
Subroutine wait_job: called CC=4 SCAR=13.9
705 953 my $self = shift;
706 953 ▶ while ( ! $self->{stopped} && ! $self->completed ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"stopped"} || $self->completed) 955 953 1908
Condition66%2/3
$self->{'stopped'} || $self->completed
l!l&&r!l&&!r
0953955
MC/DC50%1/2
$self->{'stopped'} || $self->completed
$self->{'stopped'} $self->completed
Truth table66%2/3
A || B
A= $self->{'stopped'}B= $self->completed
ABresult
1X1
011
000
707 955 my $pid;
708 955 ▶ until ($pid = waitpid(-$self->{pgid}, WUNTRACED|WNOHANG)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid = waitpid(-$self->{"pgid"}, 3)) 1523650 955 1524605
709 1523650 $self->{shell}->broadcast('ipc_poll');
710 1523650 select(undef, undef, undef, 0.001);
711 }
712 955 $self->_update_child($pid, $?);
713 }
714 }
715  
716 0 ▶sub update_status {
Subroutine update_status: not called CC=4 SCAR=30.0
717 0 my $self = shift;
718 0 ▶ return if $$self{new};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"new"} 0 0 0
719 0 ▶ while (my $pid = waitpid(-$self->{pgid}, WUNTRACED|WNOHANG)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = waitpid(-$self->{"pgid"}, 3)) 0 0 0
720 0 $self->_update_child($pid, $?);
721 0 ▶ last unless $pid > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid > 0 0 0 0
722 }
723 }
724  
725 4723 ▶sub completed { ! grep { ! $$_[0]{completed} } @{$_[0]{procs}} }
Subroutine completed: called CC=1 SCAR=0.0
726  
727 955 ▶sub _update_child {
Subroutine _update_child: called CC=16 SCAR=36.7
728 955 my ($self, $pid, $status) = @_;
729 955 ▶ return unless $pid; # 0 == all is well
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 955 955
730 955 debug "pid: $pid returned: $status";
731  
732 955 ▶ if ($pid == -1) { # -1 == all processes in group ended
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == -1) { } 0 955 955
733 0 CORE::kill(SIGTERM => -$self->{pgid} ); # just to be sure
734 0 debug "group $$self{pgid} has disappeared:($!)";
735 0 $$_[0]{completed}++ for @{$self->{procs}};
736 }
737 else {
738 955 my ($child) = grep {$$_[0]{pid} == $pid} @{$$self{procs}};
739 955 ▶ bug "Don't know this pid: $pid" unless $child;
Branch: 1 branch
BranchTrueFalseTotal
unless $child 0 955 955
740 955 $$child[0]{exit_status} = $status;
741 955 ▶ if (WIFSTOPPED($status)) { # STOP TSTP TTIN TTOUT
Branch: 1 branch
BranchTrueFalseTotal
if (WIFSTOPPED($status)) { } 0 955 955
742 0 $$self{stopped} = 1;
743 0 ▶ if ( ! $$self{bg} and (
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'bg'} and WSTOPSIG($status) == 21 || WSTOPSIG($status) == 22) { } 0 0 0
Condition0%0/6
not $self->{"bg"} and WSTOPSIG($status) == 21 || WSTOPSIG($status) == 22
!ll&&!rl&&r
000
WSTOPSIG($status) == 21 or WSTOPSIG($status) == 22
l!l&&r!l&&!r
000
MC/DC0%0/3
not $self->{"bg"} and WSTOPSIG($status) == 21 || WSTOPSIG($status) == 22
not $self->{"bg"} WSTOPSIG($status) == 21 WSTOPSIG($status) == 22
Truth table0%0/4
not(A) and B or C
A= not $self->{"bg"}B= WSTOPSIG($status) == 21C= WSTOPSIG($status) == 22
ABCresult
0XX0
1000
11X1
1011
744 WSTOPSIG($status) == SIGTTIN or
745 WSTOPSIG($status) == SIGTTOU
746 0 ) ) { $self->fg } # FIXME not sure why but this proves nescessary
747 0 else { $self->_register_bg }
748 }
749 else {
750 955 $$child[0]{completed} = 1;
751 955 ▶ if ($pid == $$self{procs}[-1][0]{pid}) { # the end of the line ..
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == $self->{"procs"}[-1][0]{"pid"}) 953 2 955
752 953 $$self{exit_status} = $status;
753 953 ▶ if ($status) { # parse error codes
Branch: 1 branch
BranchTrueFalseTotal
if ($status) 124 829 953
754 # bitmasks for return status of system commands
755 # exit_value = $? >> 8;
756 # signal_num = $? & 127;
757 # dumped_core = $? & 128;
758 124 my $signal = $status & 127;
759 124 ▶ $$self{terminated}++ if $signal; # was terminated by a signal
Branch: 1 branch
BranchTrueFalseTotal
if $signal 0 124 124
760 124 ▶ $$self{core_dump}++ if $status & 128;
Branch: 1 branch
BranchTrueFalseTotal
if $status & 128 0 124 124
761 $$self{error} = bless {
762 silent => 1,
763 string => $status >> 8,
764 exit_value => $status > 8,
765 core_dump => $$self{core_dump},
766 124 ▶ ( $signal ? (signal => $$self{_sighash}{$signal}) : () ),
Branch: 1 branch
BranchTrueFalseTotal
$signal ? : 0 124 124
767 }, 'Zoidberg::Utils::Error';
768 124 ▶ $$self{shell}{error} = $$self{error} unless $$self{bg};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"bg"} 124 0 124
769 }
770 953 ▶ unless ($self->completed) { # kill the pipeline
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->completed) 0 953 953
771 0 local $SIG{PIPE} = 'IGNORE'; # just to be sure
772 0 $self->kill(SIGPIPE);
773 }
774 }
775 }
776 }
777 }
778  
779 # TODO
780 # don't set shell exitstatus etc. if bg !
781 # run condition between the clean up and the kill for non interactive mode ?
782 # job seems not to get reaped whille stopped - should be continued at kill
783  
784 # ###### #
785 # OO api #
786 # ###### #
787  
788 =item kill($signal, $wipe_list)
789  
790 Sends $signal (numeric or named) to all child processes belonging to this job;
791 $signal defaults to SIGTERM.
792  
793 If the boolean $wipe_list is set all jobs pending in the same logic list are
794 removed.
795  
796 =cut
797  
798 972 ▶sub kill {
Subroutine kill: called CC=6 SCAR=30.2
799 972 my ($self, $sig_s, $kill_tree) = @_;
800 972 ▶ my $sig = defined($sig_s) ? $$self{shell}->sig_by_spec($sig_s) : SIGTERM;
Branch: 1 branch
BranchTrueFalseTotal
defined $sig_s ? : 972 0 972
801 972 ▶ error "$sig_s: no such signal" unless $sig;
Branch: 1 branch
BranchTrueFalseTotal
unless $sig 972 0 972
802 0 ▶ @{$$self{tree}} = () if $kill_tree;
Branch: 1 branch
BranchTrueFalseTotal
if $kill_tree 0 0 0
803 0 ▶ if ($self->{shell}{settings}{interactive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'shell'}{'settings'}{'interactive'}) { } 0 0 0
804 0 CORE::kill( $sig => -$$self{pgid} );
805 }
806 else {
807 CORE::kill( $sig => $_ )
808 0 for map { $$_[0]{pid} } @{$$self{procs}};
809 }
810 0 $self->update_status();
811 }
812  
813 =item env(\%env)
814  
815 Set local environment for the current job.
816 Can't be set after the job has started.
817  
818 =item fd(\@redir)
819  
820 Set redirections for the current job.
821 Can't be set after the job has started.
822  
823 =cut
824  
825 0 ▶sub env {
Subroutine env: not called CC=5 SCAR=34.0
826 0 my $self = shift;
827 0 ▶ my $env = ref($_[0]) ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
828 0 ▶ error "to late to set env, job is already running" unless $$self{new};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"new"} 0 0 0
829 0 for (@{$$self{procs}}) {
830 0 ▶ $$_[0]{env} = $$_[0]{env} ? { %{$$_[0]{env}}, %$env } : $env;
Branch: 1 branch
BranchTrueFalseTotal
$_->[0]{'env'} ? : 0 0 0
831 }
832 }
833  
834 0 ▶sub fd {
Subroutine fd: not called CC=6 SCAR=37.4
835 0 my $self = shift;
836 0 ▶ my $fd = ref($_[0]) ? shift : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
837 0 ▶ error "to late to set fd, job is already running" unless $$self{new};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"new"} 0 0 0
838 0 for (@$fd) {
839 0 ▶ my $block = /^[0<]/ ? $$self{procs}[0] : $$self{procs}[-1]; # in- or output
Branch: 1 branch
BranchTrueFalseTotal
/^[0<]/ ? : 0 0 0
840 0 ▶ $$block[0]{fd} ||= [];
Condition0%0/2
$block->[0]{"fd"} ||= []
l!l
00
MC/DC0%0/1
$block->[0]{"fd"} ||= []
$block->[0]{"fd"}
Truth table0%0/2
A
A= $block->[0]{"fd"}
Aresult
11
00
841 0 push @{$$block[0]{fd}}, $_;
842 }
843 }
844  
845 1217 ▶sub AUTOLOAD { # autoload signals - bo args
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
846 1217 my $self = shift;
847 1217 $AUTOLOAD =~ s/.*:://;
848 1217 $self->kill($AUTOLOAD);
849 }
850  
851 # ############ #
852 # Notification #
853 # ############ #
854  
855 0 ▶sub status_string {
Subroutine status_string: not called CC=18 SCAR=58.3
856 # POSIX: "[%d]%c %s %s\n", <job-number>, <current>, <status>, <job-name>
857 0 my ($self, $status, $list) = @_;
858  
859 0 my $pref = '';
860 0 ▶ if ($$self{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"id"}) 0 0 0
861 $pref = "[$$self{id}]" . (
862 ($self eq $$self{boss}{jobs}[-1]) ? '+ ' :
863 0 ▶ ($self eq $$self{boss}{jobs}[-2]) ? '- ' : ' ' );
Branch: 2 branches
BranchTrueFalseTotal
$self eq $self->{'boss'}{'jobs'}[-1] ? : 0 0 0
$self eq $self->{'boss'}{'jobs'}[-2] ? : 0 0 0
864 }
865  
866 $status ||=
867 $$self{new} ? 'New' :
868 $$self{stopped} ? 'Stopped' :
869 $$self{core_dump} ? 'Core dumped' :
870 $$self{terminated} ? 'Terminated' :
871 0 ▶ $$self{completed} ? 'Done' : 'Running' ;
Branch: 5 branches
BranchTrueFalseTotal
$self->{'new'} ? : 0 0 0
$self->{'stopped'} ? : 0 0 0
$self->{'core_dump'} ? : 0 0 0
$self->{'terminated'} ? : 0 0 0
$self->{'completed'} ? : 0 0 0
Condition0%0/3
$status ||= $self->{"new"} ? "New" : ($self->{"stopped"} ? "Stopped" : ($self->{"core_dump"} ? "Core dumped" : ($self->{"terminated"} ? "Terminated" : ($self->{"completed"} ? "Done" : "Running"))))
l!l&&r!l&&!r
000
MC/DC0%0/2
$status ||= $self->{"new"} ? "New" : ($self->{"stopped"} ? "Stopped" : ($self->{"core_dump"} ? "Core dumped" : ($self->{"terminated"} ? "Terminated" : ($self->{"completed"} ? "Done" : "Running"))))
$status $self->{"new"} ? "New" : ($self->{"stopped"} ? "Stopped" : ($self->{"core_dump"} ? "Core dumped" : ($self->{"terminated"} ? "Terminated" : ($self->{"completed"} ? "Done" : "Running"))))
Truth table0%0/3
A ||= B
A= $statusB= $self->{"new"} ? "New" : ($self->{"stopped"} ? "Stopped" : ($self->{"core_dump"} ? "Core dumped" : ($self->{"terminated"} ? "Terminated" : ($self->{"completed"} ? "Done" : "Running"))))
ABresult
1X1
011
000
872  
873 0 my $string = $$self{string};
874 0 chomp $string;
875 $string .= " \t(pwd: $$self{pwd})"
876 0 ▶ if $$self{pwd} and $$self{pwd} ne $ENV{PWD};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pwd"} and $self->{"pwd"} ne $ENV{"PWD"} 0 0 0
Condition0%0/3
$self->{"pwd"} and $self->{"pwd"} ne $ENV{"PWD"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"pwd"} and $self->{"pwd"} ne $ENV{"PWD"}
$self->{"pwd"} $self->{"pwd"} ne $ENV{"PWD"}
Truth table0%0/3
A and B
A= $self->{"pwd"}B= $self->{"pwd"} ne $ENV{"PWD"}
ABresult
0X0
100
111
877  
878 0 ▶ if ($list) { # more verbose output for `jobs --list`
Branch: 1 branch
BranchTrueFalseTotal
if ($list) { } 0 0 0
879 # FIXME this can no doubt be handled more gracefully
880 0 my ($t, @stack) = ($$self{tree});
881 0 ▶ for (my $i = 0; $i < @$t; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$t) 0 0 0
882 0 my $string;
883 0 ▶ until (ref $$t[$i] or $i >= @$t) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $t->[$i] or $i >= @$t) 0 0 0
Condition0%0/3
ref $t->[$i] or $i >= @$t
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $t->[$i] or $i >= @$t
ref $t->[$i] $i >= @$t
Truth table0%0/3
A or B
A= ref $t->[$i]B= $i >= @$t
ABresult
1X1
011
000
884 0 $string .= $$t[$i] . ' ';
885 0 $i++
886 }
887 0 ▶ if (ref $$t[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $t->[$i]) { } 0 0 0
888 0 ▶ $string .= $$t[$i][0]{string} || $$t[$i][-1];
Condition0%0/3
$t->[$i][0]{'string'} || $t->[$i][-1]
l!l&&r!l&&!r
000
MC/DC0%0/2
$t->[$i][0]{'string'} || $t->[$i][-1]
$t->[$i][0]{'string'} $t->[$i][-1]
Truth table0%0/3
A || B
A= $t->[$i][0]{'string'}B= $t->[$i][-1]
ABresult
1X1
011
000
889 0 chomp $string;
890 0 $string =~ s/\n/\n\t/g;
891 }
892 0 else { $string .= $$t[$i] }
893 0 push @stack, $string;
894 }
895
896 0 $string = join "\n\t", $string, grep /\S/, @stack;
897 0 return $pref . $$self{pgid} . " $status\t$string";
898 }
899 0 else { return $pref . $status . "\t$string" }
900 }
901  
902 package Zoidberg::Job::builtin;
903  
904 64 our $VERSION = '0.981';
905  
906 64 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
907 64 ▶use Zoidberg::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
908  
909 64 our @ISA = qw/Zoidberg::Job/;
910  
911 0 ▶sub round_up { $_->round_up() for @{$_[0]->{jobs}} }
Subroutine round_up: not called CC=2 SCAR=17.9
912  
913 246 ▶sub _run { # TODO something about capturing :(
Subroutine _run: called CC=5 SCAR=16.6
914 246 my $self = shift;
915 246 my $block = $self->{procs}[0];
916 246 $$self{shell}{fg_job} = $self;
917  
918 246 my $saveint = $SIG{INT};
919 246 ▶ if ($self->{settings}{interactive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'settings'}{'interactive'}) { } 0 246 246
920 0 my $ii = 0;
921 0 ▶ $SIG{INT} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
922 0 ▶ if (++$ii < 3) { message "[$$self{id}] instruction terminated by SIGINT" }
Branch: 1 branch
BranchTrueFalseTotal
if (++$ii < 3) { } 0 0 0
923 0 else { die "Got SIGINT 3 times, killing native scuddle\n" }
924 0 };
925 }
926 0 ▶ else { $SIG{INT} = sub { die "[SIGINT]\n" } }
Subroutine __ANON__: not called CC=1 SCAR=1.2
927  
928 246 my $save_capt;
929 246 ▶ if ($$self{capture}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"capture"}) 12 234 246
930 12 debug "trying to capture a builtin";
931 12 $save_capt = $$self{shell}{_builtin_output};
932 12 $$self{shell}{_builtin_output} = [];
933 }
934 246 my $save_env = $self->_set_env($block);
935  
936 # here we go !
937 246 eval { $$self{shell}->eval_block($block) };
938 # FIXME should be hook
939 # VUNZig om hier een eval te moeten gebruiken
940  
941 246 $self->_restore_env($save_env);
942 246 my @re;
943 246 ▶ if ($$self{capture}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"capture"}) 12 234 246
944 12 @re = @{ $$self{shell}{_builtin_output} };
945 12 $$self{shell}{_builtin_output} = $save_capt;
946 }
947  
948 # restore other stuff
949 246 $SIG{INT} = $saveint;
950 246 $self->{completed}++;
951
952 246 ▶ die if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 10 236 246
953  
954 236 return @re;
955 }
956  
957 245 ▶sub kill { error q#Can't kill a builtin# }
Subroutine kill: called CC=1 SCAR=0.0
958 0 ▶sub bg { error q#Can't put builtin in the background# }
Subroutine bg: not called CC=1 SCAR=6.9
959 0 ▶sub fg { error q#Can't put builtin in the foreground# }
Subroutine fg: not called CC=1 SCAR=6.9
960  
961 246 ▶sub completed { $_[0]->{completed} }
Subroutine completed: called CC=1 SCAR=0.0
962  
963 64 1;
964  
965 __END__
« blib/lib/Zoidberg/Fish.pm ↑ Summary blib/lib/Zoidberg/StringParser.pm »