lib/Proc/ShellSpawn/Large.pm

filter: Statementstmt 93.0% 376 / 404 Branchbran 76.9% 174 / 226 Conditioncond 61.3% 100 / 163 MC/DCmcdc 31.8% 28 / 88 Subroutinesub 89.5% 60 / 67 Podpod 0.0% 0 / 29 totaltotal 75.5% 738 / 977 CC 191 SCAR 24.3
CC
191
Cov
82%
b_command
30.5
b_kill
24.4
b_getopts
19.4
CRAP 11.3
« lib/Proc/ShellSubset/Small.pm ↑ Summary lib/Proc/ShellSpawn/Builtins.pm »
1 package Proc::ShellSpawn::Large;
2 170 our $VERSION = '0.11';
3 # EXPERIMENTAL, awaiting approval: what Proc::ShellSpawn needs to run Proc::ShellSubset::Large trees
4 # (doc/large-subset/large-subset-spec.md, sections 4 and 5), as handlers named in Proc::ShellSpawn's tables:
5 # x_NAME(CONTEXT, NODE, TAIL, K) command nodes { func | export | readonly | local | exec => ... }
6 # b_NAME(CONTEXT, ARGV, ENV, K, DATA) builtins, and call() for functions
7 # Each calls K exactly once, or ends the shell (Proc::ShellSpawn::leave or Proc::ShellSpawn::fail), and never waits.
8 170 ▶use strict; use warnings; use feature 'current_sub';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
9 170 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 170 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 170 ▶use Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 170 ▶use Proc::ShellSpawn;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 170 ▶BEGIN { no strict 'refs'; *$_ = \&{"Proc::ShellSpawn::$_"} for qw(defer leave fail msg put var setvar expand_str each_async x_list x_cmd
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=2 SCAR=6.9
15 redirect hold release release_ctx release_all find_cmd spawn handler) }
16  
17 170 our $UNSUPPORTED = 248; # A4: the status when something outside the subset shows up at run time (250 - 2: sh's syntax error status, moved out of the common range; doc/large-subset/large-subset-spec.md, section 5)
18 0 ▶sub unsupported { # A4: something outside the subset, found at run time
Subroutine unsupported: not called CC=1 SCAR=6.9
Pod undocumented
19 0 ▶ my ($c, $what) = @_; put($c, 2, "Proc::ShellSubset::Large: $what is not supported\n", sub { leave($c, $UNSUPPORTED) }) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
20 17 ▶sub run_text { # eval, ., trap actions: parse with the large grammar and run in this shell
Subroutine run_text: called CC=5 SCAR=16.1
Pod undocumented
21 17 my ($c, $text, $what, $k) = @_;
22 17 ▶ my $sh = $c->{sh}; my $t = $sh->{parse}->($text, %{$sh->{alias}} ? (aliases => { %{$sh->{alias}} }) : (),
Branch: 2 branches
BranchTrueFalseTotal
%{$sh->{'alias'};} ? : 1 16 17
$what =~ /^the text of '\. / ? : 2 15 17
23 $what =~ /^the text of '\. / ? () : (nolineno => 1)); # the run's own parser; $LINENO in eval or trap text: A4 (G8)
24 17 ▶ return unsupported($c, $what) if ref $t ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $t ne "ARRAY" 0 17 17
25 17 ▶ return defer($k, 0) unless @$t;
Branch: 1 branch
BranchTrueFalseTotal
unless @$t 3 14 17
26 14 ▶ $sh->{intext}++; x_list($c, $t, undef, sub { $sh->{intext}--; $k->(@_) }) } # alias in this text: B1, A4
Subroutine __ANON__: called CC=1 SCAR=0.0
27 0 ▶sub b_unsupported { my ($c, $argv) = @_; unsupported($c, "'$argv->[0]'") } # a builtin left to sh, named at run time
Subroutine b_unsupported: not called CC=1 SCAR=6.9
Pod undocumented
28 2 ▶sub x_rest { # P1 (post2-0.04): the rest of the text after a line with an alias definition known only when it ran: parsed now,
Subroutine x_rest: called CC=4 SCAR=14.2
Pod undocumented
29 # with the aliases defined by now, as both shells read a text one complete command at a time; a form outside the subset: A4
30 2 my ($c, $x, $tail, $k) = @_; my $sh = $c->{sh};
31 2 ▶ my $t = $sh->{parse}->($x->{rest}, %{$sh->{alias}} ? (aliases => { %{$sh->{alias}} }) : (), lineoff => $x->{lineoff});
Branch: 1 branch
BranchTrueFalseTotal
%{$sh->{'alias'};} ? : 2 0 2
32 2 ▶ return unsupported($c, "a command after an alias definition") if ref $t ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $t ne "ARRAY" 0 2 2
33 2 ▶ return defer($k, $sh->{q}) unless @$t;
Branch: 1 branch
BranchTrueFalseTotal
unless @$t 0 2 2
34 2 x_list($c, $t, $tail, $k) }
35 5 ▶sub b_late { # 3A (post-0.04): a command whose name the text also defines as a function, which is not defined (yet):
Subroutine b_late: called CC=2 SCAR=7.0
Pod undocumented
36 # parsed again from its expanded words, quoted, so that the builtin's own checks apply as they would have at parse time
37 5 ▶ my ($c, $argv, $env, $k) = @_; my $q = sub { (my $w = shift) =~ s/'/'\\''/g; "'$w'" };
Subroutine __ANON__: called CC=1 SCAR=0.0
38 5 my $text = join ' ', (map "$_=" . $q->($env->{$_}), sort keys %$env), map $q->($_), @$argv;
39 5 my $t = $c->{sh}{parse}->($text);
40 5 ▶ return unsupported($c, "'$argv->[0]' in this form") if ref $t ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $t ne "ARRAY" 0 5 5
41 5 x_list($c, $t, undef, $k) }
42  
43 # ------------------------------------------------------------------ functions
44 33 ▶sub x_func { my ($c, $x, $tail, $k) = @_; $c->{sh}{cmd}{$x->{func}} = ['Proc::ShellSpawn::Large::call', $x->{body}]; defer($k, 0) }
Subroutine x_func: called CC=1 SCAR=0.0
Pod undocumented
45 33 ▶sub call { # NAME ARGS...: the body in this shell, with its own positional parameters and frame
Subroutine call: called CC=3 SCAR=11.0
Pod undocumented
46 33 my ($c, $argv, $env, $k, $body) = @_; my $sh = $c->{sh};
47 33 my ($old, %saved) = $sh->{args}; # settings before the call: for its duration, exported (dash)
48 33 for (sort keys %$env) { $saved{$_} = [exists $sh->{vars}{$_}, $sh->{vars}{$_}, $sh->{exp}{$_}];
49 1 ▶ setvar($c, $_, $env->{$_}) or return; $sh->{exp}{$_} = 1 }
Branch: 1 branch
BranchTrueFalseTotal
setvar($c, $_, $env->{$_}) or return 1 0 1
50 33 $sh->{args} = [@$argv[1 .. $#$argv]];
51 33 my $saved = \%saved;
52 33 ▶ my $ret = sub { my $st = shift; restore($sh, $saved); $sh->{args} = $old; defer($k, $st) };
Subroutine __ANON__: called CC=1 SCAR=0.0
53 33 x_cmd({ %$c, loops => [], held => 0, frame => { saved => $saved, ret => $ret } }, $body, undef, $ret) }
54 32 ▶sub restore { # put back variables a function changed with local, or settings before its call
Subroutine restore: called CC=4 SCAR=14.0
Pod undocumented
55 32 my ($sh, $saved) = @_;
56 32 for my $n (keys %$saved) { my ($had, $v, $exp) = @{delete $saved->{$n}};
57 2 ▶ if ($had) { $sh->{vars}{$n} = $v } else { delete $sh->{vars}{$n} }
Branch: 1 branch
BranchTrueFalseTotal
if ($had) { } 1 1 2
58 0 ▶ if ($exp) { $sh->{exp}{$n} = 1 } else { delete $sh->{exp}{$n} } } }
Branch: 1 branch
BranchTrueFalseTotal
if ($exp) { } 0 2 2
59 3 ▶sub b_return {
Subroutine b_return: called CC=4 SCAR=14.0
Pod undocumented
60 3 ▶ my ($c, $argv) = @_; my $st = $argv->[1] // $c->{sh}{q};
Condition66%2/3
$argv->[1] // $c->{"sh"}{"q"}
l!l&&r!l&&!r
210
MC/DC0%0/2
$argv->[1] // $c->{"sh"}{"q"}
$argv->[1] $c->{"sh"}{"q"}
Truth table66%2/3
A // B
A= $argv->[1]B= $c->{"sh"}{"q"}
ABresult
1X1
011
000
61 3 ▶ my $fr = $c->{frame} or return $c->{sh}{intext} ? leave($c, $st) # outside a function, in eval or . text: as exit
Branch: 2 branches
BranchTrueFalseTotal
my $fr = $c->{"frame"} or return $c->{"sh"}{"intext"} ? leave($c, $st) : Proc::ShellSpawn::fail($c, 2, "return: can only `return' from a function or sourced script") 2 1 3
$c->{'sh'}{'intext'} ? : 0 1 1
62 : Proc::ShellSpawn::fail($c, 2, "return: can only `return' from a function or sourced script"); # else an error, as bash (rule E)
63 2 release_ctx($c); release_all(map $_->{scope}, @{$c->{loops}}); $fr->{ret}->($st) }
64  
65 # ------------------------------------------------------------------ declarations
66 7 ▶sub declare { # export, readonly, local: [NAME, Word or undef, ...]; $each->(NAME) after the value is set
Subroutine declare: called CC=2 SCAR=8.5
Pod undocumented
67 7 ▶ my ($c, $list, $k, $each, $what) = @_; my @l = @$list; my @pairs; push @pairs, [splice @l, 0, 2] while @l;
Branch: 1 branch
BranchTrueFalseTotal
@l and do { push @pairs, [splice(@l, 0, 2)] } 7 7 14
68 7 my @log; # NAME or NAME=VALUE as set, for $Proc::ShellSpawn::ONBUILTIN (measurement)
69 7 ▶ each_async(\@pairs, sub { my ($p, $next) = @_; my ($n, $w) = @$p;
Subroutine __ANON__: called CC=3 SCAR=15.0
70 0 ▶ return expand_str($c, $n, sub { my $m = shift; # export $v[=value] (parser item P4): the name known only now
Subroutine __ANON__: not called CC=3 SCAR=22.3
71 0 ▶ Proc::ShellSubset::settable($m) or return unsupported($c, "the name '$m' in 'export'"); # post-0.04: the old test let every name pass
Branch: 1 branch
BranchTrueFalseTotal
Proc::ShellSubset::settable($m) or return unsupported($c, "the name '${m}' in 'export'") 0 0 0
72 0 ▶ return push(@log, $m), $each->($m), $next->() unless defined $w;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $w 0 0 0
73 0 ▶ expand_str($c, $w, sub { my $v = shift; push @log, "$m=$v"; setvar($c, $m, $v) or return; $each->($m); $next->() }) }) if ref $n;
Subroutine __ANON__: not called CC=2 SCAR=14.3
Branch: 2 branches
BranchTrueFalseTotal
if ref $n 0 7 7
setvar($c, $m, $v) or return 0 0 0
74 7 ▶ return push(@log, $n), $each->($n), $next->() unless defined $w;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $w 1 6 7
75 6 ▶ expand_str($c, $w, sub { my $v = shift; push @log, "$n=$v"; setvar($c, $n, $v) or return; $each->($n); $next->() }) },
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
setvar($c, $n, $v) or return 6 0 6
76 7 ▶ sub { $Proc::ShellSpawn::ONBUILTIN->("x_$what", [$what, @log], {}) if $what && $Proc::ShellSpawn::ONBUILTIN; defer($k, 0) }) }
Subroutine __ANON__: called CC=3 SCAR=12.5
Branch: 1 branch
BranchTrueFalseTotal
if $what and $Proc::ShellSpawn::ONBUILTIN 0 7 7
Condition33%1/3
$what and $Proc::ShellSpawn::ONBUILTIN
!ll&&!rl&&r
070
MC/DC0%0/2
$what and $Proc::ShellSpawn::ONBUILTIN
$what $Proc::ShellSpawn::ONBUILTIN
Truth table33%1/3
A and B
A= $whatB= $Proc::ShellSpawn::ONBUILTIN
ABresult
0X0
100
111
77 2 ▶sub x_export { my ($c, $x, $tail, $k) = @_; declare($c, $x->{export}, $k, sub { $c->{sh}{exp}{$_[0]} = 1 }, 'export') }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine x_export: called CC=1 SCAR=0.0
Pod undocumented
78 4 ▶sub x_readonly { my ($c, $x, $tail, $k) = @_; declare($c, $x->{readonly}, $k, sub { $c->{sh}{ro}{$_[0]} = 1 }, 'readonly') }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine x_readonly: called CC=1 SCAR=0.0
Pod undocumented
79 1 ▶sub x_local {
Subroutine x_local: called CC=4 SCAR=14.0
Pod undocumented
80 1 my ($c, $x, $tail, $k) = @_; my $fr = $c->{frame};
81 1 ▶ return fail($c, 2, "local: not in a function") unless $fr; # A5 (reached only dynamically)
Branch: 1 branch
BranchTrueFalseTotal
unless $fr 0 1 1
82 1 my $sh = $c->{sh}; my %l = @{$x->{local}};
83 1 ▶ $fr->{saved}{$_} //= [exists $sh->{vars}{$_}, $sh->{vars}{$_}, $sh->{exp}{$_}] for keys %l;
Condition50%1/2
$fr->{"saved"}{$_} //= [exists $sh->{"vars"}{$_}, $sh->{"vars"}{$_}, $sh->{"exp"}{$_}]
l!l
01
MC/DC0%0/1
$fr->{"saved"}{$_} //= [exists $sh->{"vars"}{$_}, $sh->{"vars"}{$_}, $sh->{"exp"}{$_}]
$fr->{"saved"}{$_}
Truth table50%1/2
A
A= $fr->{"saved"}{$_}
Aresult
11
00
84 1 ▶ declare($c, $x->{local}, $k, sub {}, 'local') }
Subroutine __ANON__: called CC=1 SCAR=0.0
85  
86 # ------------------------------------------------------------------ exec with redirections only: for the rest of the shell
87 2 ▶sub x_exec {
Subroutine x_exec: called CC=2 SCAR=7.3
Pod undocumented
88 2 my ($c, $x, $tail, $k) = @_;
89 2 ▶ redirect($c, $x->{redir} // [], sub {
Subroutine __ANON__: called CC=8 SCAR=22.3
90 2 ▶ my ($m, $opened) = @_ or return leave($c, 2); # a special builtin: a failed redirection ends the shell
Branch: 1 branch
BranchTrueFalseTotal
my($m, $opened) = or return leave($c, 2) 2 0 2
91 2 hold($_) for grep defined, values %$m; release($_) for @$opened;
92 2 ▶ my $old = $c->{held} && !$c->{released} ? $c->{map} : {};
Branch: 1 branch
BranchTrueFalseTotal
$c->{'held'} && !$c->{'released'} ? : 0 2 2
Condition33%1/3
$c->{"held"} and not $c->{"released"}
!ll&&!rl&&r
200
MC/DC0%0/2
$c->{"held"} and not $c->{"released"}
$c->{"held"} not $c->{"released"}
Truth table33%1/3
A and not(B)
A= $c->{"held"}B= not $c->{"released"}
ABresult
0X0
100
111
93 2 $c->{map} = $m; release($_) for grep defined, values %$old;
94 2 ▶ push @{$c->{shell}{scope}}, $c unless $c->{held}; # released when the shell ends
Branch: 1 branch
BranchTrueFalseTotal
unless $c->{"held"} 2 0 2
95 2 ▶ @$c{qw(held released)} = (1, 0); defer($k, 0) }) }
Condition50%1/2
$x->{"redir"} // []
l!l
20
MC/DC0%0/1
$x->{"redir"} // []
$x->{"redir"}
Truth table50%1/2
A
A= $x->{"redir"}
Aresult
11
00
96  
97 # ------------------------------------------------------------------ builtins
98 3 ▶sub b_shift {
Subroutine b_shift: called CC=3 SCAR=11.0
Pod undocumented
99 3 ▶ my ($c, $argv, $env, $k) = @_; my $n = $argv->[1] // 1; my $a = $c->{sh}{args};
Condition100%2/2
$argv->[1] // 1
l!l
21
MC/DC100%1/1
$argv->[1] // 1
$argv->[1]
Truth table100%2/2
A
A= $argv->[1]
Aresult
11
00
100 3 ▶ return fail($c, 2, "shift: can't shift that many") if $n > @$a;
Branch: 1 branch
BranchTrueFalseTotal
if $n > @$a 1 2 3
101 2 splice @$a, 0, $n; defer($k, 0) }
102  
103 170 my %OPT = (errexit => 'e', nounset => 'u', noglob => 'f', noclobber => 'C', allexport => 'a',
104 notify => 'b', ignoreeof => 'ignoreeof', nolog => 'nolog'); # the last three, and +m, do nothing here (P6)
105 40 ▶sub b_set {
Subroutine b_set: called CC=15 SCAR=27.8
Pod undocumented
106 40 my ($c, $argv, $env, $k) = @_; my @a = @$argv[1 .. $#$argv]; my $o = $c->{sh}{opt}; my $ops;
107 40 ▶ while (@a) {
Branch: 1 branch
BranchTrueFalseTotal
if (@a) 40 23 63
108 40 my $x = $a[0];
109 40 ▶ if ($x eq '--') { shift @a; $ops = 1; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($x eq "--") 17 23 40
110 23 ▶ last unless $x =~ /^([-+])(.+)/; shift @a; my ($on, $f) = ($1 eq '-', $2);
Branch: 1 branch
BranchTrueFalseTotal
unless $x =~ /^([-+])(.+)/ 0 23 23
111 return fail($c, 2, "set: Illegal option -o history") # B2: as dash, when the caller asked for dash's interactive builtins
112 23 ▶ if $f eq 'o' && ($a[0] // '') eq 'history' && $c->{sh}{interactive} eq 'dash';
Branch: 1 branch
BranchTrueFalseTotal
if $f eq "o" and ($a[0] // "") eq "history" and $c->{"sh"}{"interactive"} eq "dash" 0 23 23
Condition50%4/8
$f eq "o" and ($a[0] // "") eq "history" and $c->{"sh"}{"interactive"} eq "dash"
!ll&&!rl&&r
2300
$f eq "o" and ($a[0] // "") eq "history"
!ll&&!rl&&r
1580
$a[0] // ""
l!l
80
MC/DC0%0/3
$f eq "o" and ($a[0] // "") eq "history" and $c->{"sh"}{"interactive"} eq "dash"
$f eq "o" $a[0] $c->{"sh"}{"interactive"} eq "dash"
Truth table25%1/4
A and B and C
A= $f eq "o"B= $a[0]C= $c->{"sh"}{"interactive"} eq "dash"
ABCresult
0XX0
10X0
1100
1111
113 23 ▶ for my $l ($f eq 'o' ? $OPT{shift @a // ''} // '' : split //, $f) { $on ? ($o->{$l} = 1) : delete $o->{$l} } }
Branch: 2 branches
BranchTrueFalseTotal
$f eq 'o' ? : 8 15 23
$on ? : 21 2 23
Condition75%3/4
$OPT{shift @a // ""} // ""
l!l
62
shift @a // ""
l!l
80
MC/DC100%1/1
$OPT{shift @a // ""} // ""
$OPT{shift @a // ""}
MC/DC0%0/1
shift @a // ""
shift @a
Truth table100%2/2
A
A= $OPT{shift @a // ""}
Aresult
11
00
Truth table50%1/2
A
A= shift @a
Aresult
11
00
114 40 ▶ $c->{sh}{args} = [@a] if $ops || @a;
Branch: 1 branch
BranchTrueFalseTotal
if $ops or @a 17 23 40
Condition66%2/3
$ops or @a
l!l&&r!l&&!r
17023
MC/DC50%1/2
$ops or @a
$ops @a
Truth table66%2/3
A or B
A= $opsB= @a
ABresult
1X1
011
000
115 40 defer($k, 0) }
116  
117 6 ▶sub b_eval { my ($c, $argv, $env, $k) = @_; run_text($c, join(' ', @$argv[1 .. $#$argv]), "'eval' text", $k) }
Subroutine b_eval: called CC=1 SCAR=0.0
Pod undocumented
118 3 ▶sub b_dot {
Subroutine b_dot: called CC=7 SCAR=21.2
Pod undocumented
119 3 ▶ my ($c, $argv, $env, $k) = @_; my $f = $argv->[1] // '';
Condition50%1/2
$argv->[1] // ""
l!l
30
MC/DC0%0/1
$argv->[1] // ""
$argv->[1]
Truth table50%1/2
A
A= $argv->[1]
Aresult
11
00
120 3 ▶ my ($p) = Proc::ShellSpawn::has_dir($f) ? $f : grep -f Proc::ShellSpawn::path_in($c, $_), map +($_ eq '' ? '.' : $_) . "/$f",
Branch: 2 branches
BranchTrueFalseTotal
Proc::ShellSpawn::has_dir($f) ? : 3 0 3
$_ eq '' ? : 0 0 0
121 Proc::ShellSpawn::path_list(var($c, 'PATH'));
122 3 ▶ $p = Proc::ShellSpawn::path_in($c, $p) if defined $p;
Branch: 1 branch
BranchTrueFalseTotal
if defined $p 3 0 3
123 3 ▶ open my $fh, '<', $p // '' or return fail($c, 2, ".: cannot open $f: No such file");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $p // "" or return fail($c, 2, ".: cannot open ${f}: No such file") 2 1 3
Condition50%1/2
$p // ""
l!l
30
MC/DC0%0/1
$p // ""
$p
Truth table50%1/2
A
A= $p
Aresult
11
00
124 2 run_text($c, do { local $/; <$fh> }, "the text of '. $f'", $k) }
125  
126 170 my %SIGNAME; { my @n = split ' ', $Config::Config{sig_name}; @SIGNAME{split ' ', $Config::Config{sig_num}} = @n; $SIGNAME{0} = 'EXIT' }
127 170 my %SIGNUM; @SIGNUM{split ' ', $Config::Config{sig_name}} = split ' ', $Config::Config{sig_num};
128 23 ▶sub b_trap { # names in any case, or numbers (P6); KILL and STOP cannot be caught: nothing to do, status 0, as both shells
Subroutine b_trap: called CC=13 SCAR=25.9
Pod undocumented
129 23 my ($c, $argv, $env, $k) = @_; my ($act, @sigs) = @$argv[1 .. $#$argv]; my $sh = $c->{sh};
130 23 ▶ for my $s (map /^[0-9]+\z/ ? $SIGNAME{$_} // $_ : uc, @sigs) {
Branch: 1 branch
BranchTrueFalseTotal
/^[0-9]+\z/ ? : 6 18 24
Condition33%1/3
$SIGNAME{$_} // $_
l!l&&r!l&&!r
600
MC/DC0%0/2
$SIGNAME{$_} // $_
$SIGNAME{$_} $_
Truth table33%1/3
A // B
A= $SIGNAME{$_}B= $_
ABresult
1X1
011
000
131 24 ▶ next if $s eq 'KILL' || $s eq 'STOP';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "KILL" or $s eq "STOP" 2 22 24
Condition100%3/3
$s eq "KILL" or $s eq "STOP"
l!l&&r!l&&!r
1122
MC/DC100%2/2
$s eq "KILL" or $s eq "STOP"
$s eq "KILL" $s eq "STOP"
Truth table100%3/3
A or B
A= $s eq "KILL"B= $s eq "STOP"
ABresult
1X1
011
000
132 22 ▶ if ($act eq '-') { delete $sh->{trap}{$s} } else { $sh->{trap}{$s} = $act }
Branch: 1 branch
BranchTrueFalseTotal
if ($act eq '-') { } 2 20 22
133 22 ▶ next if $s eq 'EXIT';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "EXIT" 11 11 22
134 11 my $w = $c; weaken $w;
135 11 ▶ my $h = $sh->{tsub}{$s} = sub { return unless $w; push @{$w->{sh}{pend}}, $s; defer(\&run_pending, $w) };
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
unless $w 0 3 3
136 11 ▶ next unless exists $SIGNUM{$s}; # a signal Perl does not have here (Windows: USR1, ...): only kill ... $$ sends it
Branch: 1 branch
BranchTrueFalseTotal
unless exists $SIGNUM{$s} 0 11 11
137 11 ▶ $Proc::ShellSpawn::TRAPSAVE{$s} //= $SIG{$s} // 'DEFAULT';
Condition75%3/4
$Proc::ShellSpawn::TRAPSAVE{$s} //= $SIG{$s} // "DEFAULT"
l!l
011
$SIG{$s} // "DEFAULT"
l!l
74
MC/DC0%0/1
$Proc::ShellSpawn::TRAPSAVE{$s} //= $SIG{$s} // "DEFAULT"
$Proc::ShellSpawn::TRAPSAVE{$s}
Truth table50%1/2
A
A= $Proc::ShellSpawn::TRAPSAVE{$s}
Aresult
11
00
138 11 ▶ $SIG{$s} = $act eq '-' ? $Proc::ShellSpawn::TRAPSAVE{$s} : $act eq '' ? 'IGNORE' : $h } # deferred: the action runs between
Branch: 2 branches
BranchTrueFalseTotal
$act eq '-' ? : 0 11 11
$act eq '' ? : 2 9 11
139 # two reactions, and at the latest before the next command of a list (X19: as dash, before what follows the command)
140 23 defer($k, 0) }
141 5 ▶sub run_pending { # the actions of the signals caught since the last time, in order; then $k (and every other waiter)
Subroutine run_pending: called CC=3 SCAR=11.0
Pod undocumented
142 5 my ($c, $k) = @_; my $sh = $c->{sh};
143 5 ▶ push @{$sh->{twait}}, $k if $k; return if $sh->{tbusy}; $sh->{tbusy} = 1;
Branch: 2 branches
BranchTrueFalseTotal
if $k 2 3 5
if $sh->{"tbusy"} 2 3 5
144 6 ▶ sub { my $p = $sh->{pend};
Subroutine __ANON__: called CC=5 SCAR=16.1
145 6 ▶ return run_trap($c, shift @$p, __SUB__) if $p && @$p;
Branch: 1 branch
BranchTrueFalseTotal
if $p and @$p 3 3 6
Condition66%2/3
$p and @$p
!ll&&!rl&&r
033
MC/DC50%1/2
$p and @$p
$p @$p
Truth table66%2/3
A and B
A= $pB= @$p
ABresult
0X0
100
111
146 3 ▶ $sh->{tbusy} = 0; my @w = @{$sh->{twait} // []}; $sh->{twait} = []; $_->() for @w }->() }
Condition100%2/2
$sh->{"twait"} // []
l!l
21
MC/DC100%1/1
$sh->{"twait"} // []
$sh->{"twait"}
Truth table100%2/2
A
A= $sh->{"twait"}
Aresult
11
00
147 9 ▶sub run_trap { # a trap's action; $? is kept
Subroutine run_trap: called CC=4 SCAR=16.3
Pod undocumented
148 9 my ($c, $s, $k) = @_; my $a = $c->{sh}{trap}{$s}; my $q = $c->{sh}{q};
149 9 ▶ return $k && $k->(0) unless defined $a && $a ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a and $a ne "" 0 9 9
Condition16%1/6
defined $a and $a ne ""
!ll&&!rl&&r
009
$k && &$k(0)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $a and $a ne ""
defined $a $a ne ""
MC/DC0%0/2
$k && &$k(0)
$k &$k(0)
Truth table33%1/3
A and B
A= defined $aB= $a ne ""
ABresult
0X0
100
111
Truth table0%0/3
A && B
A= $kB= &$k(0)
ABresult
0X0
100
111
150 9 ▶ run_text({ %$c, loops => [], held => 0, intrap => 1 }, $a, "the action of 'trap'", sub { $c->{sh}{q} = $q; $k->(0) if $k }) }
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
if $k 9 0 9
151  
152 12 ▶sub b_wait {
Subroutine b_wait: called CC=7 SCAR=19.5
Pod undocumented
153 12 my ($c, $argv, $env, $k) = @_; my @pid = @$argv[1 .. $#$argv]; my @j = @{$c->{sh}{jobl}};
154 12 ▶ if (@pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pid) 1 11 12
155 1 ▶ my $p = $pid[-1]; my ($j) = grep +($_->{first} // '') eq $p, @j;
Condition50%1/2
$_->{"first"} // ""
l!l
10
MC/DC0%0/1
$_->{"first"} // ""
$_->{"first"}
Truth table50%1/2
A
A= $_->{"first"}
Aresult
11
00
156 1 ▶ return defer($k, 127) unless $j;
Branch: 1 branch
BranchTrueFalseTotal
unless $j 0 1 1
157 1 ▶ return defer($k, $j->{st}) if defined $j->{st};
Branch: 1 branch
BranchTrueFalseTotal
if defined $j->{"st"} 0 1 1
158 1 ▶ return push @{$j->{waiters}}, sub { defer($k, $j->{st}) } }
Subroutine __ANON__: called CC=1 SCAR=0.0
159 11 my @left = grep !defined $_->{st}, @j; my $n = @left;
160 11 ▶ return defer($k, 0) unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 1 10 11
161 10 ▶ push @{$_->{waiters}}, sub { defer($k, 0) unless --$n } for @left }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless --$n 10 1 11
162  
163 20 ▶sub b_umask {
Subroutine b_umask: called CC=10 SCAR=24.1
Pod undocumented
164 20 my ($c, $argv, $env, $k) = @_;
165 20 ▶ return put($c, 1, sprintf("%04o\n", $c->{sh}{umask}), sub { $k->(0) }) unless defined(my $m = $argv->[1]);
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $m = $argv->[1]) 12 8 20
166 8 ▶ if ($m eq '-S') { # umask -S (parser item P4): u=rwx,g=rx,o=rx
Branch: 1 branch
BranchTrueFalseTotal
if ($m eq "-S") 0 8 8
167 0 my $u = $c->{sh}{umask}; my @w = map { my $b = 7 & ~($u >> $_); join '', grep $b & {r => 4, w => 2, x => 1}->{$_}, qw(r w x) } 6, 3, 0;
168 0 ▶ return put($c, 1, "u=$w[0],g=$w[1],o=$w[2]\n", sub { $k->(0) }) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
169 8 ▶ if ($m =~ /^[ugoa]*[-+=]/) { # symbolic, as both shells (post4-0.03): on the permissions the mask leaves
Branch: 1 branch
BranchTrueFalseTotal
if ($m =~ /^[ugoa]*[-+=]/) 3 5 8
170 3 my $p = ~$c->{sh}{umask} & 0777;
171 3 for (split /,/, $m) { my ($who, $op, $perm) = /^([ugoa]*)([-+=])([rwx]*)\z/;
172 4 ▶ my $w = 0; $w |= { u => 0700, g => 070, o => 07, a => 0777 }->{$_} for split //, $who || 'a';
Condition50%1/2
$who || 'a'
l!l
40
MC/DC0%0/1
$who || 'a'
$who
Truth table50%1/2
A
A= $who
Aresult
11
00
173 4 my $b = 0; $b |= { r => 0444, w => 0222, x => 0111 }->{$_} for split //, $perm; $b &= $w;
174 4 ▶ $p = $op eq '+' ? $p | $b : $op eq '-' ? $p & ~$b : ($p & ~$w) | $b }
Branch: 2 branches
BranchTrueFalseTotal
$op eq '+' ? : 1 3 4
$op eq '-' ? : 1 2 3
175 3 $c->{sh}{umask} = ~$p & 0777; return defer($k, 0) }
176 5 $c->{sh}{umask} = oct $m; defer($k, 0) }
177  
178 170 our $DEFPATH = '/usr/bin:/bin'; # command -p: bash's (glibc confstr _CS_PATH, in its order); dash adds /usr/sbin:/sbin
179 170 my %ASPROG = map +($_ => 1), qw(echo printf test [ kill true false pwd cd); # command -v prints the name (spec, section 5)
180 22 ▶sub b_pwd { # X23 (the user's choice C, post2-0.03): the program, as '/bin/pwd -L', with PWD set to the shell's own
Subroutine b_pwd: called CC=4 SCAR=13.9
Pod undocumented
181 # directory, so that it prints what both shells print (the directory as reached, also through symbolic links);
182 # operands are dropped, as both shells ignore them (the program would warn); options as given, the last counts
183 22 my ($c, $argv, $env, $k) = @_; my (undef, @a) = @$argv; my @o;
184 22 ▶ while (@a && $a[0] =~ /^-/) { my $o = shift @a; last if $o eq '--'; push @o, $o }
Branch: 2 branches
BranchTrueFalseTotal
if (@a and $a[0] =~ /^-/) 7 22 29
if $o eq "--" 0 7 7
Condition100%3/3
@a and $a[0] =~ /^-/
!ll&&!rl&&r
2117
MC/DC100%2/2
@a and $a[0] =~ /^-/
@a $a[0] =~ /^-/
Truth table100%3/3
A and B
A= @aB= $a[0] =~ /^-/
ABresult
0X0
100
111
185 22 spawn($c, $c->{map}, ['pwd', '-L', @o], { %$env, PWD => $c->{sh}{cwd} }, $k) }
186 43 ▶sub b_test { # rule F (post2-0.04): 's1 < s2', 's1 > s2' (also after '!'), which the program refuses, compared here by
Subroutine b_test: called CC=11 SCAR=24.2
Pod undocumented
187 # bytes, as both shells (POSIX 2024: "collates before/after ... in the current locale"; the same in the C locale);
188 # every other form: the program, as before (P5)
189 43 my ($c, $argv, $env, $k) = @_; my ($n, @a) = @$argv;
190 43 ▶ if ($n eq '[') { return spawn($c, $c->{map}, $argv, $env, $k) unless @a && $a[-1] eq ']'; pop @a }
Branch: 2 branches
BranchTrueFalseTotal
if ($n eq "[") 36 7 43
unless @a and $a[-1] eq "]" 0 36 36
Condition33%1/3
@a and $a[-1] eq "]"
!ll&&!rl&&r
0036
MC/DC0%0/2
@a and $a[-1] eq "]"
@a $a[-1] eq "]"
Truth table33%1/3
A and B
A= @aB= $a[-1] eq "]"
ABresult
0X0
100
111
191 43 ▶ my $not = @a == 4 && $a[0] eq '!' and shift @a;
Branch: 1 branch
BranchTrueFalseTotal
my $not = @a == 4 && $a[0] eq "!" and shift @a 1 42 43
Condition66%2/3
@a == 4 && $a[0] eq '!'
!ll&&!rl&&r
4201
MC/DC50%1/2
@a == 4 && $a[0] eq '!'
@a == 4 $a[0] eq '!'
Truth table66%2/3
A && B
A= @a == 4B= $a[0] eq '!'
ABresult
0X0
100
111
192 43 ▶ return spawn($c, $c->{map}, $argv, $env, $k) unless @a == 3 && $a[1] =~ /^[<>]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless @a == 3 and $a[1] =~ /^[<>]\z/ 39 4 43
Condition100%3/3
@a == 3 and $a[1] =~ /^[<>]\z/
!ll&&!rl&&r
8314
MC/DC100%2/2
@a == 3 and $a[1] =~ /^[<>]\z/
@a == 3 $a[1] =~ /^[<>]\z/
Truth table100%3/3
A and B
A= @a == 3B= $a[1] =~ /^[<>]\z/
ABresult
0X0
100
111
193 4 ▶ my $r = $a[1] eq '<' ? $a[0] lt $a[2] : $a[0] gt $a[2]; $r = !$r if $not;
Branch: 2 branches
BranchTrueFalseTotal
$a[1] eq '<' ? : 3 1 4
if $not 1 3 4
194 4 ▶ defer($k, $r ? 0 : 1) }
Branch: 1 branch
BranchTrueFalseTotal
$r ? : 2 2 4
195 17 ▶sub b_command {
Subroutine b_command: called CC=20 SCAR=34.2
Pod undocumented
196 17 my ($c, $argv, $env, $k) = @_; my (undef, @a) = @$argv;
197 17 ▶ if (($a[0] // '') eq '-v') {
Branch: 1 branch
BranchTrueFalseTotal
if (($a[0] // "") eq "-v") 12 5 17
Condition50%1/2
$a[0] // ""
l!l
170
MC/DC0%0/1
$a[0] // ""
$a[0]
Truth table50%1/2
A
A= $a[0]
Aresult
11
00
198 12 ▶ my $n = $a[1] // ''; my $sh = $c->{sh};
Condition50%1/2
$a[1] // ""
l!l
120
MC/DC0%0/1
$a[1] // ""
$a[1]
Truth table50%1/2
A
A= $a[1]
Aresult
11
00
199 7 ▶ return put($c, 1, "$n\n", sub { $k->(0) })
Subroutine __ANON__: called CC=1 SCAR=0.0
200 12 ▶ if $sh->{cmd}{$n} || (Proc::ShellSpawn::cmd_of($n) // ['unsupported'])->[0] !~ /unsupported/ || $Proc::ShellSpawn::SPECIAL{$n} || $ASPROG{$n} || $Proc::ShellSubset::BI{$n};
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"cmd"}{$n} or not +(Proc::ShellSpawn::cmd_of($n) // ["unsupported"])->[0] =~ /unsupported/ or $Proc::ShellSpawn::SPECIAL{$n} or $ASPROG{$n} or $Proc::ShellSubset::BI{$n} 7 5 12
Condition85%12/14
$sh->{"cmd"}{$n} or not +(Proc::ShellSpawn::cmd_of($n) // ["unsupported"])->[0] =~ /unsupported/ or $Proc::ShellSpawn::SPECIAL{$n} or $ASPROG{$n} or $Proc::ShellSubset::BI{$n}
l!l&&r!l&&!r
705
$sh->{"cmd"}{$n} or not +(Proc::ShellSpawn::cmd_of($n) // ["unsupported"])->[0] =~ /unsupported/ or $Proc::ShellSpawn::SPECIAL{$n} or $ASPROG{$n}
l!l&&r!l&&!r
345
$sh->{"cmd"}{$n} or not +(Proc::ShellSpawn::cmd_of($n) // ["unsupported"])->[0] =~ /unsupported/ or $Proc::ShellSpawn::SPECIAL{$n}
l!l&&r!l&&!r
309
$sh->{"cmd"}{$n} or not +(Proc::ShellSpawn::cmd_of($n) // ["unsupported"])->[0] =~ /unsupported/
l!l&&r!l&&!r
129
Proc::ShellSpawn::cmd_of($n) // ["unsupported"]
l!l
29
MC/DC0%0/5
$sh->{"cmd"}{$n} or not +(Proc::ShellSpawn::cmd_of($n) // ["unsupported"])->[0] =~ /unsupported/ or $Proc::ShellSpawn::SPECIAL{$n} or $ASPROG{$n} or $Proc::ShellSubset::BI{$n}
$sh->{"cmd"}{$n} Proc::ShellSpawn::cmd_of($n) $Proc::ShellSpawn::SPECIAL{$n} $ASPROG{$n} $Proc::ShellSubset::BI{$n}
Truth table16%1/6
A or not(B) or C or D or E
A= $sh->{"cmd"}{$n}B= Proc::ShellSpawn::cmd_of($n)C= $Proc::ShellSpawn::SPECIAL{$n}D= $ASPROG{$n}E= $Proc::ShellSubset::BI{$n}
ABCDEresult
1XXXX1
00XXX1
011XX1
0101X1
010011
010000
201 5 my ($p) = find_cmd($c, $n, var($c, 'PATH'));
202 5 ▶ $p //= $n if $Proc::ShellSpawn::INPROC{$n}; # run inside this process, no file (Windows): its name, as for a builtin
Branch: 1 branch
BranchTrueFalseTotal
if $Proc::ShellSpawn::INPROC{$n} 0 5 5
Condition0%0/3
$p //= $n
l!l&&r!l&&!r
000
MC/DC0%0/2
$p //= $n
$p $n
Truth table0%0/3
A //= B
A= $pB= $n
ABresult
1X1
011
000
203 5 ▶ return defer($k, 127) unless defined $p;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p 2 3 5
204 3 ▶ return put($c, 1, "$p\n", sub { $k->(0) }) }
Subroutine __ANON__: called CC=1 SCAR=0.0
205 5 ▶ return defer($k, 0) unless @a;
Branch: 1 branch
BranchTrueFalseTotal
unless @a 0 5 5
206 5 ▶ if ($a[0] eq '-p') { # command -p PROGRAM ... (parser item P4): looked up in a default PATH, run with the PATH it has
Branch: 1 branch
BranchTrueFalseTotal
if ($a[0] eq "-p") 0 5 5
207 0 shift @a; return (Proc::ShellSpawn::cmd_of($a[0]) // [''])->[0] !~ /b_test\z|^\z/ || $c->{sh}{cmd}{$a[0]} ? unsupported($c, "'command -p $a[0]'")
208 0 ▶ : spawn($c, $c->{map}, \@a, $env, $k, $DEFPATH) }
Branch: 1 branch
BranchTrueFalseTotal
!(+(Proc::ShellSpawn::cmd_of($a[0]) // [''])->[0] =~ /b_test\z|^\z/) || $c->{'sh'}{'cmd'}{$a[0]} ? : 0 0 0
Condition0%0/5
not +(Proc::ShellSpawn::cmd_of($a[0]) // [""])->[0] =~ /b_test\z|^\z/ or $c->{"sh"}{"cmd"}{$a[0]}
l!l&&r!l&&!r
000
Proc::ShellSpawn::cmd_of($a[0]) // [""]
l!l
00
MC/DC0%0/2
not +(Proc::ShellSpawn::cmd_of($a[0]) // [""])->[0] =~ /b_test\z|^\z/ or $c->{"sh"}{"cmd"}{$a[0]}
Proc::ShellSpawn::cmd_of($a[0]) $c->{"sh"}{"cmd"}{$a[0]}
Truth table0%0/3
not(A) or B
A= Proc::ShellSpawn::cmd_of($a[0])B= $c->{"sh"}{"cmd"}{$a[0]}
ABresult
0X1
111
100
209 5 ▶ return b_late($c, \@a, $env, $k) if $Proc::ShellSubset::REGBI{$a[0]}; # a builtin, not a function: its checks, then it (3A)
Branch: 1 branch
BranchTrueFalseTotal
if $Proc::ShellSubset::REGBI{$a[0]} 3 2 5
210 2 ▶ if (my $e = Proc::ShellSpawn::cmd_of($a[0])) { return handler($e->[0])->($c, \@a, $env, $k, $e->[1]) } # a builtin, not a function
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = Proc::ShellSpawn::cmd_of($a[0])) 1 1 2
211 1 spawn($c, $c->{map}, \@a, $env, $k) }
212  
213 21 ▶sub b_getopts { # OPTSTRING NAME [ARGS]: as dash, also OPTIND inside grouped options (A5)
Subroutine b_getopts: called CC=19 SCAR=29.6
Pod undocumented
214 21 my ($c, $argv, $env, $k) = @_; my (undef, $os, $name, @a) = @$argv; my $sh = $c->{sh};
215 21 ▶ @a = @{$sh->{args}} unless @$argv > 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @$argv > 3 2 19 21
216 21 my $silent = $os =~ s/^://;
217 21 ▶ my $ind = var($c, 'OPTIND'); $ind = 1 if $ind !~ /^[1-9][0-9]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $ind =~ /^[1-9][0-9]*\z/ 0 21 21
218 21 ▶ my $off = ($sh->{optlast} // 0) eq $ind ? $sh->{optoff} // 0 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($sh->{'optlast'} // 0) eq $ind ? : 10 11 21
Condition75%3/4
$sh->{"optlast"} // 0
l!l
1011
$sh->{"optoff"} // 0
l!l
100
MC/DC100%1/1
$sh->{"optlast"} // 0
$sh->{"optlast"}
MC/DC0%0/1
$sh->{"optoff"} // 0
$sh->{"optoff"}
Truth table100%2/2
A
A= $sh->{"optlast"}
Aresult
11
00
Truth table50%1/2
A
A= $sh->{"optoff"}
Aresult
11
00
219 21 ▶ $ind = 1, $off = 0 if $ind > @a + 1; # past the end of these arguments: dash starts over (L7)
Branch: 1 branch
BranchTrueFalseTotal
if $ind > @a + 1 1 20 21
220 21 ▶ my $set = sub { my ($v, $st, @arg) = @_; # @arg undef: OPTARG unset (rule E: POSIX, as bash; dash keeps it, L8)
Subroutine __ANON__: called CC=6 SCAR=19.3
221 21 ▶ setvar($c, $name, $v) && setvar($c, OPTIND => $ind) or return;
Branch: 1 branch
BranchTrueFalseTotal
setvar($c, $name, $v) and setvar($c, "OPTIND", $ind) or return 21 0 21
Condition33%1/3
setvar($c, $name, $v) and setvar($c, "OPTIND", $ind)
!ll&&!rl&&r
0021
MC/DC0%0/2
setvar($c, $name, $v) and setvar($c, "OPTIND", $ind)
setvar($c, $name, $v) setvar($c, "OPTIND", $ind)
Truth table33%1/3
A and B
A= setvar($c, $name, $v)B= setvar($c, "OPTIND", $ind)
ABresult
0X0
100
111
222 21 ▶ !@arg ? () : defined $arg[0] ? setvar($c, OPTARG => $arg[0]) || return : delete $sh->{vars}{OPTARG};
Branch: 2 branches
BranchTrueFalseTotal
!@arg ? : 0 21 21
defined $arg[0] ? : 8 13 21
Condition50%1/2
setvar($c, "OPTARG", $arg[0]) or return
l!l
80
MC/DC0%0/1
setvar($c, "OPTARG", $arg[0]) or return
setvar($c, "OPTARG", $arg[0])
Truth table50%1/2
A
A= setvar($c, "OPTARG", $arg[0])
Aresult
11
00
223 21 @$sh{qw(optlast optoff)} = ($ind, $off); defer($k, $st) };
224 21 my $cur;
225 21 ▶ if ($off) { $cur = $a[$ind - 2] }
Branch: 1 branch
BranchTrueFalseTotal
if ($off) { } 1 20 21
226 else {
227 20 $cur = $a[$ind - 1];
228 20 ▶ return $set->('?', 1, undef) if !defined $cur || $cur !~ /^-./;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cur and $cur =~ /^-./ 5 15 20
Condition100%3/3
defined $cur and $cur =~ /^-./
!ll&&!rl&&r
3215
MC/DC100%2/2
defined $cur and $cur =~ /^-./
defined $cur $cur =~ /^-./
Truth table100%3/3
A and B
A= defined $curB= $cur =~ /^-./
ABresult
0X0
100
111
229 15 ▶ $ind++; return $set->('?', 1, undef) if $cur eq '--';
Branch: 1 branch
BranchTrueFalseTotal
if $cur eq "--" 0 15 15
230 15 $off = 1 }
231 16 ▶ my $ch = substr $cur, $off++, 1; my $last = $off >= length $cur; $off = 0 if $last;
Branch: 1 branch
BranchTrueFalseTotal
if $last 14 2 16
232 16 my $i = index $os, $ch;
233 16 ▶ if ($ch eq ':' or $i < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ":" or $i < 0) 4 12 16
Condition66%2/3
$ch eq ":" or $i < 0
l!l&&r!l&&!r
0412
MC/DC50%1/2
$ch eq ":" or $i < 0
$ch eq ":" $i < 0
Truth table66%2/3
A or B
A= $ch eq ":"B= $i < 0
ABresult
1X1
011
000
234 4 ▶ return $silent ? $set->('?', 0, $ch) : msg($c, "Illegal option -$ch", sub { $set->('?', 0, undef) }) }
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$silent ? : 2 2 4
235 12 ▶ return $set->($ch, 0, undef) unless substr($os, $i + 1, 1) eq ':'; # POSIX: OPTARG unset (rule E; dash: empty)
Branch: 1 branch
BranchTrueFalseTotal
unless substr($os, $i + 1, 1) eq ":" 5 7 12
236 7 ▶ unless ($last) { my $arg = substr $cur, $off; $off = 0; return $set->($ch, 0, $arg) }
Branch: 1 branch
BranchTrueFalseTotal
unless ($last) 1 6 7
237 6 ▶ return $set->($ch, 0, $a[$ind++ - 1]) if defined $a[$ind - 1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $a[$ind - 1] 4 2 6
238 2 ▶ $silent ? $set->(':', 0, $ch) : msg($c, "No arg for -$ch option", sub { $set->('?', 0, undef) }) }
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$silent ? : 1 1 2
239  
240 12 ▶sub b_unset {
Subroutine b_unset: called CC=8 SCAR=20.9
Pod undocumented
241 12 my ($c, $argv, $env, $k) = @_; my (undef, @n) = @$argv; my $sh = $c->{sh};
242 12 ▶ my $f = ($n[0] // '') =~ /^-([vf])\z/ && (shift @n, $1)[1] // 'v';
Condition57%4/7
($n[0] // "") =~ /^-([vf])\z/ && (shift @n, $1)[1] // "v"
l!l
120
($n[0] // "") =~ /^-([vf])\z/ and (shift @n, $1)[1]
!ll&&!rl&&r
804
$n[0] // ""
l!l
120
MC/DC0%0/2
($n[0] // "") =~ /^-([vf])\z/ && (shift @n, $1)[1] // "v"
$n[0] (shift @n, $1)[1]
Truth table33%1/3
A and B
A= $n[0]B= (shift @n, $1)[1]
ABresult
111
0X0
100
243 12 for (@n) {
244 13 ▶ if ($f eq 'f') { delete $sh->{cmd}{$_}; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($f eq "f") 4 9 13
245 # X35 (the user, 2026-11-08: as dash): a word that is not a name, from an expansion (a literal one the parser
246 # rejects), stops the shell with status 2, as any error of a special builtin; the names before it are already unset
247 9 ▶ return fail($c, 2, "unset: $_: bad variable name") unless /^[A-Za-z_][A-Za-z0-9_]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^[A-Za-z_][A-Za-z0-9_]*\z/ 4 5 9
248 5 ▶ return fail($c, 2, "unset: $_: is read only") if $sh->{ro}{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"ro"}{$_} 1 4 5
249 4 delete $sh->{vars}{$_}; delete $sh->{exp}{$_} }
250 7 defer($k, 0) }
251  
252 22 ▶sub b_kill { # kill ... $$: the shell takes the signal itself (F9); other process IDs go to the program kill
Subroutine b_kill: called CC=20 SCAR=32.0
Pod undocumented
253 22 my ($c, $argv, $env, $k) = @_; my (undef, @a) = @$argv; my $sh = $c->{sh};
254 22 my @o = @a; # K0 (the user, 2026-11-05): a signal (-N, -NAME, -s NAME), maybe '--', and no process ID: as both shells,
255 # status 2, with dash's text (the program kill: status 1, a long usage text). 'kill -s' alone: the program
256 22 ▶ if (@o && $o[0] eq '-s') { splice @o, 0, 2 } elsif (@o && $o[0] =~ /^-[0-9A-Za-z]+\z/ && $o[0] !~ /^-l/) { shift @o }
Branch: 2 branches
BranchTrueFalseTotal
if (@o and $o[0] eq '-s') { } 3 19 22
elsif (@o and $o[0] =~ /^-[0-9A-Za-z]+\z/ and not $o[0] =~ /^-l/) { } 16 6 22
Condition88%8/9
@o and $o[0] eq "-s"
!ll&&!rl&&r
2173
@o and $o[0] =~ /^-[0-9A-Za-z]+\z/ and not $o[0] =~ /^-l/
!ll&&!rl&&r
3016
@o and $o[0] =~ /^-[0-9A-Za-z]+\z/
!ll&&!rl&&r
2116
MC/DC100%2/2
@o and $o[0] eq "-s"
@o $o[0] eq "-s"
MC/DC66%2/3
@o and $o[0] =~ /^-[0-9A-Za-z]+\z/ and not $o[0] =~ /^-l/
@o $o[0] =~ /^-[0-9A-Za-z]+\z/ not $o[0] =~ /^-l/
Truth table100%3/3
A and B
A= @oB= $o[0] eq "-s"
ABresult
0X0
100
111
Truth table75%3/4
A and B and not(C)
A= @oB= $o[0] =~ /^-[0-9A-Za-z]+\z/C= not $o[0] =~ /^-l/
ABCresult
0XX0
10X0
1100
1111
257 22 ▶ shift @o if @o && $o[0] eq '--';
Branch: 1 branch
BranchTrueFalseTotal
if @o and $o[0] eq "--" 1 21 22
Condition100%3/3
@o and $o[0] eq "--"
!ll&&!rl&&r
4171
MC/DC100%2/2
@o and $o[0] eq "--"
@o $o[0] eq "--"
Truth table100%3/3
A and B
A= @oB= $o[0] eq "--"
ABresult
0X0
100
111
258 5 ▶ return msg($c, "kill: Usage: kill [-s sigspec | -signum | -sigspec] [pid | job]... or\nkill -l [exitstatus]", sub { $k->(2) })
Subroutine __ANON__: called CC=1 SCAR=0.0
259 22 ▶ if !@o && "@a" ne '-s';
Branch: 1 branch
BranchTrueFalseTotal
if not @o and join($", @a) ne "-s" 5 17 22
Condition66%2/3
not @o and join($", @a) ne "-s"
!ll&&!rl&&r
1705
MC/DC50%1/2
not @o and join($", @a) ne "-s"
not @o join($", @a) ne "-s"
Truth table66%2/3
not(A) and B
A= not @oB= join($", @a) ne "-s"
ABresult
0X0
100
111
260 17 ▶ return spawn($c, $c->{map}, $argv, $env, $k) unless grep $_ eq $sh->{pid}, @a;
Branch: 1 branch
BranchTrueFalseTotal
unless grep(($_ eq $sh->{"pid"}), @a) 1 16 17
261 16 my $s = 'TERM';
262 16 ▶ if (@a && $a[0] =~ /^-(?:s)?\z/ && @a > 1) { shift @a; $s = shift @a }
Branch: 2 branches
BranchTrueFalseTotal
if (@a and $a[0] =~ /^-(?:s)?\z/ and @a > 1) { } 2 14 16
elsif (@a and $a[0] =~ /^-(\w+)\z/) { } 14 0 14
Condition66%4/6
@a and $a[0] =~ /^-(?:s)?\z/ and @a > 1
!ll&&!rl&&r
1402
@a and $a[0] =~ /^-(?:s)?\z/
!ll&&!rl&&r
0142
MC/DC33%1/3
@a and $a[0] =~ /^-(?:s)?\z/ and @a > 1
@a $a[0] =~ /^-(?:s)?\z/ @a > 1
Truth table50%2/4
A and B and C
A= @aB= $a[0] =~ /^-(?:s)?\z/C= @a > 1
ABCresult
0XX0
10X0
1100
1111
263 14 ▶ elsif (@a && $a[0] =~ /^-(\w+)\z/) { shift @a; $s = $1 }
Condition33%1/3
@a and $a[0] =~ /^-(\w+)\z/
!ll&&!rl&&r
0014
MC/DC0%0/2
@a and $a[0] =~ /^-(\w+)\z/
@a $a[0] =~ /^-(\w+)\z/
Truth table33%1/3
A and B
A= @aB= $a[0] =~ /^-(\w+)\z/
ABresult
0X0
100
111
264 16 ▶ $s = $SIGNAME{$s} // $s if $s =~ /^[0-9]+\z/; $s =~ s/^SIG//;
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /^[0-9]+\z/ 2 14 16
Condition33%1/3
$SIGNAME{$s} // $s
l!l&&r!l&&!r
200
MC/DC0%0/2
$SIGNAME{$s} // $s
$SIGNAME{$s} $s
Truth table33%1/3
A // B
A= $SIGNAME{$s}B= $s
ABresult
1X1
011
000
265 16 my $harmless = $s =~ /^(?:CHLD|CLD|URG|WINCH|CONT)\z/; # post4-0.04: by default these do nothing to the shell (0 is EXIT here)
266 16 my @other = grep $_ ne $sh->{pid}, @a;
267 16 ▶ my $m = $sh->{main} // $sh; # $$ is the main shell, also in a subshell: its trap counts (L6)
Condition66%2/3
$sh->{"main"} // $sh
l!l&&r!l&&!r
4120
MC/DC0%0/2
$sh->{"main"} // $sh
$sh->{"main"} $sh
Truth table66%2/3
A // B
A= $sh->{"main"}B= $sh
ABresult
1X1
011
000
268 16 ▶ my $self = sub {
Subroutine __ANON__: called CC=12 SCAR=27.8
269 16 my $t = $m->{trap}{$s};
270 16 ▶ return defer($k, 0) if $s eq 'EXIT' || $harmless && !defined $t; # kill -0 $$ only tests; as both shells, status 0
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "EXIT" or $harmless and not defined $t 5 11 16
Condition83%5/6
$s eq "EXIT" or $harmless and not defined $t
l!l&&r!l&&!r
2311
$harmless and not defined $t
!ll&&!rl&&r
1103
MC/DC66%2/3
$s eq "EXIT" or $harmless and not defined $t
$s eq "EXIT" $harmless not defined $t
Truth table75%3/4
A or B and not(C)
A= $s eq "EXIT"B= $harmlessC= not defined $t
ABCresult
1XX1
0111
00X0
0100
271 11 ▶ return defer($k, 0) if defined $t && $t eq ''; # ignored
Branch: 1 branch
BranchTrueFalseTotal
if defined $t and $t eq "" 2 9 11
Condition66%2/3
defined $t and $t eq ""
!ll&&!rl&&r
092
MC/DC50%1/2
defined $t and $t eq ""
defined $t $t eq ""
Truth table66%2/3
A and B
A= defined $tB= $t eq ""
ABresult
0X0
100
111
272 9 ▶ return run_trap($c, $s, sub { defer($k, 0) }) if defined $t && $m == $sh;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if defined $t and $m == $sh 6 3 9
Condition66%2/3
defined $t and $m == $sh
!ll&&!rl&&r
036
MC/DC50%1/2
defined $t and $m == $sh
defined $t $m == $sh
Truth table66%2/3
A and B
A= defined $tB= $m == $sh
ABresult
0X0
100
111
273 3 ▶ if (defined $t) { my $h = $m->{tsub}{$s}; $h->($s) if $h; return defer($k, 0) } # as a real signal: the trap runs in the main shell
Branch: 2 branches
BranchTrueFalseTotal
if (defined $t) 3 0 3
if $h 3 0 3
274 0 ▶ $m->{trap}{EXIT} = '' if exists $m->{trap}{EXIT}; # a death by the signal runs no EXIT trap
Branch: 1 branch
BranchTrueFalseTotal
if exists $m->{"trap"}{"EXIT"} 0 0 0
275 0 $Proc::ShellSpawn::SIGNALLED = $s; # the caller may die by it too (try.pl does)
276 0 ▶ $sh->{die}->(128 + ($SIGNUM{$s} // 15)) }; # the whole string ends, as sh -c would
Condition0%0/2
$SIGNUM{$s} // 15
l!l
00
MC/DC0%0/1
$SIGNUM{$s} // 15
$SIGNUM{$s}
Truth table0%0/2
A
A= $SIGNUM{$s}
Aresult
11
00
277 16 ▶ return $self->() unless @other;
Branch: 1 branch
BranchTrueFalseTotal
unless @other 16 0 16
278 0 ▶ spawn($c, $c->{map}, ['kill', "-$s", @other], $env, sub { $self->() }) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
279  
280 170 1;
« lib/Proc/ShellSubset/Small.pm ↑ Summary lib/Proc/ShellSpawn/Builtins.pm »