lib/Proc/ShellSpawn/Builtins.pm

filter: Statementstmt 94.5% 139 / 147 Branchbran 85.1% 80 / 94 Conditioncond 64.5% 20 / 31 MC/DCmcdc 41.1% 7 / 17 Subroutinesub 86.9% 20 / 23 Podpod 0.0% 0 / 11 totaltotal 82.3% 266 / 323 CC 66 SCAR 20.6
CC
66
Cov
88%
b_type
13.1
b_jobs
12.3
b_ulimit
10.5
CRAP 7.8
« lib/Proc/ShellSpawn/Large.pm ↑ Summary lib/Proc/Spawner/Loop.pm »
1 package Proc::ShellSpawn::Builtins; # builtins run by the large subset: one function per builtin (B1-B3 of "Shell Corpus Gaps")
2 529 our $VERSION = '0.12';
3 529 ▶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
4 529 ▶use Proc::ShellSpawn;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 529 ▶BEGIN { no strict 'refs'; *$_ = \&{"Proc::ShellSpawn::$_"} for qw(defer fail msg put var spawn find_cmd) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=2 SCAR=6.9
6  
7 # A builtin counts as implemented when its entry is here; Proc::ShellSpawn looks command names up in
8 # @Proc::ShellSpawn::CMDS, where this table comes first. Each handler: NAME->(CONTEXT, ARGV, ENV, K), calls K once.
9 529 our %CMD = ((map +($_ => [__PACKAGE__ . "::b_$_"]), qw(alias unalias jobs fg bg hash type times ulimit)),
10 (map +($_ => [__PACKAGE__ . '::b_interactive']), qw(history fc bind help compopt logout)));
11 529 unshift @Proc::ShellSpawn::CMDS, \%CMD;
12  
13 52 ▶sub done { my ($k, $st) = @_; sub { $k->($st) } }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine done: called CC=1 SCAR=0.0
Pod undocumented
14 5 ▶sub sq { my $v = shift; my $o = ''; # as dash quotes values in listings: 'a'"'"'b'; a leading quote gives ''"'"...
Subroutine sq: called CC=3 SCAR=11.0
Pod undocumented
15 5 ▶ while (1) { $v =~ /\G([^']*)/gc; $o .= "'$1'"; last unless $v =~ /\G('+)/gc; $o .= qq("$1"); last if pos $v == length $v }
Branch: 2 branches
BranchTrueFalseTotal
unless $v =~ /\G('+)/cg 3 5 8
if pos $v == length $v 2 3 5
16 5 $o }
17  
18 # ------------------------------------------------------------------ B1: alias, unalias
19 # The parser expands aliases in the lines after their definition; here the table is kept for eval and '.' texts.
20 26 ▶sub b_alias {
Subroutine b_alias: called CC=6 SCAR=17.9
Pod undocumented
21 26 my ($c, $argv, $env, $k) = @_; my $al = $c->{sh}{alias}; my @a = @$argv[1 .. $#$argv];
22 26 ▶ return Proc::ShellSpawn::Large::unsupported($c, "'alias' in the text of eval or '.'") if $c->{sh}{intext};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"sh"}{"intext"} 0 26 26
23 26 ▶ return put($c, 1, join('', map "$_=" . sq($al->{$_}) . "\n", sort keys %$al), done($k, 0)) unless @a; # dash: hash order
Branch: 1 branch
BranchTrueFalseTotal
unless @a 3 23 26
24 23 my ($out, $err) = ('', '');
25 23 ▶ for (@a) { if (/^([^=]+)=(.*)\z/s) { $al->{$1} = $2 } elsif (exists $al->{$_}) { $out .= "$_=" . sq($al->{$_}) . "\n" }
Branch: 2 branches
BranchTrueFalseTotal
if (/^([^=]+)=(.*)\z/s) { } 22 4 26
elsif (exists $al->{$_}) { } 3 1 4
26 1 else { $err .= "alias: $_ not found\n" } }
27 23 ▶ put($c, 1, $out, sub { put($c, 2, $err, done($k, $err ne '' ? 1 : 0)) }) }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$err ne '' ? : 1 22 23
28 4 ▶sub b_unalias {
Subroutine b_unalias: called CC=6 SCAR=17.9
Pod undocumented
29 4 my ($c, $argv, $env, $k) = @_; my $al = $c->{sh}{alias}; my @a = @$argv[1 .. $#$argv];
30 4 ▶ return Proc::ShellSpawn::Large::unsupported($c, "'unalias' in the text of eval or '.'") if $c->{sh}{intext};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"sh"}{"intext"} 0 4 4
31 4 ▶ if ($a[0] eq '-a') { %$al = (); return defer($k, 0) }
Branch: 1 branch
BranchTrueFalseTotal
if ($a[0] eq "-a") 1 3 4
32 3 ▶ my $err = ''; for (@a) { exists $al->{$_} ? delete $al->{$_} : ($err .= "unalias: $_ not found\n") }
Branch: 1 branch
BranchTrueFalseTotal
exists $al->{$_} ? : 2 1 3
33 3 ▶ put($c, 2, $err, done($k, $err ne '' ? 1 : 0)) }
Branch: 1 branch
BranchTrueFalseTotal
$err ne '' ? : 1 2 3
34  
35 # ------------------------------------------------------------------ B2: interactive-only builtins
36 0 ▶sub b_interactive { # nothing (the default), or as dash, which has none of them: a program by that name, or not found
Subroutine b_interactive: not called CC=2 SCAR=17.9
Pod undocumented
37 0 my ($c, $argv, $env, $k) = @_;
38 0 ▶ $c->{sh}{interactive} eq 'dash' ? spawn($c, $c->{map}, $argv, $env, $k) : defer($k, 0) }
Branch: 1 branch
BranchTrueFalseTotal
$c->{'sh'}{'interactive'} eq 'dash' ? : 0 0 0
39  
40 # ------------------------------------------------------------------ B3: as dash
41 11 ▶sub b_jobs { # dash's format in sh -c: "[N] + Running", padded to column 33, without the command's text; -l adds the
Subroutine b_jobs: called CC=12 SCAR=25.1
Pod undocumented
42 # process ID, -p prints it alone. In a pipeline element or $( ) the shell's jobs are listed too, as bash does (rule E)
43 # Operands %N, %%, %+, %- (post-0.04) list those jobs, in that order; one that does not exist: dash's message, status 2.
44 11 my ($c, $argv, $env, $k) = @_; my @a = @$argv[1 .. $#$argv]; my $f = '';
45 11 ▶ $f .= $1 while @a && $a[0] =~ /^-([lp]+)\z/ && shift @a;
Branch: 1 branch
BranchTrueFalseTotal
@a and $a[0] =~ /^-([lp]+)\z/ and shift @a and do { $f .= $1 } 3 11 14
Condition83%5/6
@a and $a[0] =~ /^-([lp]+)\z/ and shift @a
!ll&&!rl&&r
1103
@a and $a[0] =~ /^-([lp]+)\z/
!ll&&!rl&&r
653
MC/DC66%2/3
@a and $a[0] =~ /^-([lp]+)\z/ and shift @a
@a $a[0] =~ /^-([lp]+)\z/ shift @a
Truth table75%3/4
A and B and C
A= @aB= $a[0] =~ /^-([lp]+)\z/C= shift @a
ABCresult
0XX0
10X0
1100
1111
46 11 ▶ my @j = (@{$c->{sh}{ljobs} // []}, @{$c->{sh}{jobl}}); my $st = 0;
Condition100%2/2
$c->{"sh"}{"ljobs"} // []
l!l
38
MC/DC100%1/1
$c->{"sh"}{"ljobs"} // []
$c->{"sh"}{"ljobs"}
Truth table100%2/2
A
A= $c->{"sh"}{"ljobs"}
Aresult
11
00
47 19 ▶ my $line = sub { my $i = shift; my $pid = $j[$i]{first} // $j[$i]{pids}[0] // 0; return "$pid\n" if $f =~ /p/;
Subroutine __ANON__: called CC=7 SCAR=20.7
Branch: 1 branch
BranchTrueFalseTotal
if $f =~ /p/ 5 14 19
Condition40%2/5
($j[$i]{"first"} // $j[$i]{"pids"}[0]) // 0
l!l
190
$j[$i]{"first"} // $j[$i]{"pids"}[0]
l!l&&r!l&&!r
1900
MC/DC0%0/2
($j[$i]{"first"} // $j[$i]{"pids"}[0]) // 0
$j[$i]{"first"} $j[$i]{"pids"}[0]
Truth table33%1/3
A // B
A= $j[$i]{"first"}B= $j[$i]{"pids"}[0]
ABresult
1X1
011
000
48 14 ▶ my $h = sprintf "[%d] %s %s", $i + 1, $i == $#j ? '+' : $i == $#j - 1 ? '-' : ' ', $f =~ /l/ ? "$pid " : '';
Branch: 3 branches
BranchTrueFalseTotal
$i == $#j ? : 8 6 14
$i == $#j - 1 ? : 6 0 6
$f =~ /l/ ? : 0 14 14
49 11 sprintf "%s%-*s\n", $h, 33 - length $h, 'Running' };
50 11 my @out = map +[1, $line->($_)], grep !defined $j[$_]{st}, 0 .. $#j;
51 11 ▶ if (@a) { @out = ();
Branch: 1 branch
BranchTrueFalseTotal
if (@a) 5 6 11
52 5 ▶ for (@a) { my $i = /^%([0-9]+)\z/ ? $1 - 1 : $_ eq '%-' ? $#j - 1 : $#j;
Branch: 2 branches
BranchTrueFalseTotal
/^%([0-9]+)\z/ ? : 5 1 6
$_ eq '%-' ? : 1 0 1
53 6 ▶ if ($i >= 0 && $i <= $#j && !defined $j[$i]{st}) { push @out, [1, $line->($i)] }
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0 and $i <= $#j and not defined $j[$i]{'st'}) { } 5 1 6
Condition66%4/6
$i >= 0 and $i <= $#j and not defined $j[$i]{"st"}
!ll&&!rl&&r
105
$i >= 0 and $i <= $#j
!ll&&!rl&&r
015
MC/DC33%1/3
$i >= 0 and $i <= $#j and not defined $j[$i]{"st"}
$i >= 0 $i <= $#j not defined $j[$i]{"st"}
Truth table50%2/4
A and B and not(C)
A= $i >= 0B= $i <= $#jC= not defined $j[$i]{"st"}
ABCresult
0XX0
10X0
1100
1111
54 1 else { push @out, [2, "jobs: No such job: $_"]; $st = 2 } } }
55 21 ▶ sub { my $next = __SUB__; my $o = shift @out or return $k->($st);
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
my $o = shift @out or return &$k($st) 10 11 21
56 11 ▶ $o->[0] == 1 ? put($c, 1, $o->[1], $next) : msg($c, $o->[1], $next) }->() }
Branch: 1 branch
BranchTrueFalseTotal
$o->[0] == 1 ? : 9 1 10
57 3 ▶sub b_bg { my ($c, $argv, $env, $k) = @_; my $j = $argv->[1];
Subroutine b_bg: called CC=2 SCAR=6.9
Pod undocumented
58 3 ▶ msg($c, defined $j ? "$argv->[0]: job $j not created under job control" : "$argv->[0]: No current job", done($k, 2)) }
Branch: 1 branch
BranchTrueFalseTotal
defined $j ? : 1 2 3
59 529 *b_fg = \&b_bg;
60  
61 529 my %DASHBI = map +($_ => 1), qw(. : [ alias bg break cd chdir command continue echo eval exec exit export false fg getopts hash
62 jobs kill local printf pwd read readonly return set shift test times trap true type ulimit umask unalias unset wait);
63 529 my %DASHKW = map +($_ => 1), qw(! { } case do done elif else esac fi for if in then until while);
64 6 ▶sub b_hash {
Subroutine b_hash: called CC=9 SCAR=22.1
Pod undocumented
65 6 my ($c, $argv, $env, $k) = @_; my $h = $c->{sh}{hash}; my @a = @$argv[1 .. $#$argv];
66 6 ▶ return put($c, 1, join('', map "$h->{$_}\n", sort keys %$h), done($k, 0)) unless @a;
Branch: 1 branch
BranchTrueFalseTotal
unless @a 3 3 6
67 3 ▶ if ($a[0] eq '-r') { %$h = (); return defer($k, 0) }
Branch: 1 branch
BranchTrueFalseTotal
if ($a[0] eq "-r") 1 2 3
68 2 my @bad;
69 2 ▶ for (@a) { next if $DASHBI{$_} || $c->{sh}{cmd}{$_}; my ($p) = find_cmd($c, $_, var($c, 'PATH'));
Branch: 1 branch
BranchTrueFalseTotal
if $DASHBI{$_} or $c->{"sh"}{"cmd"}{$_} 0 2 2
Condition33%1/3
$DASHBI{$_} or $c->{"sh"}{"cmd"}{$_}
l!l&&r!l&&!r
002
MC/DC0%0/2
$DASHBI{$_} or $c->{"sh"}{"cmd"}{$_}
$DASHBI{$_} $c->{"sh"}{"cmd"}{$_}
Truth table33%1/3
A or B
A= $DASHBI{$_}B= $c->{"sh"}{"cmd"}{$_}
ABresult
1X1
011
000
70 2 ▶ if (defined $p) { $h->{$_} = $p unless Proc::ShellSpawn::has_dir($_) } else { push @bad, $_ } }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $p) { } 1 1 2
unless Proc::ShellSpawn::has_dir($_) 1 0 1
71 2 ▶ my $st = @bad ? 1 : 0; # one message per name not found, each after the one before
Branch: 1 branch
BranchTrueFalseTotal
@bad ? : 1 1 2
72 2 ▶ sub { my $next = __SUB__; @bad ? msg($c, "hash: " . shift(@bad) . ": not found", $next) : $k->($st) }->() }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@bad ? : 1 2 3
73 5 ▶sub b_type {
Subroutine b_type: called CC=13 SCAR=25.7
Pod undocumented
74 5 my ($c, $argv, $env, $k) = @_; my $sh = $c->{sh}; my ($t, $st) = ('', 0);
75 5 for my $n (@$argv[1 .. $#$argv]) {
76 9 ▶ if ($DASHKW{$n}) { $t .= "$n is a shell keyword\n" }
Branch: 6 branches
BranchTrueFalseTotal
if ($DASHKW{$n}) { } 1 8 9
elsif (exists $sh->{'alias'}{$n}) { } 1 7 8
elsif ($sh->{'cmd'}{$n}) { } 1 6 7
elsif ($Proc::ShellSpawn::SPECIAL{$n}) { } 1 5 6
elsif ($DASHBI{$n}) { } 1 4 5
elsif (not Proc::ShellSpawn::has_dir($n) and $sh->{'hash'}{$n}) { } 1 3 4
77 1 elsif (exists $sh->{alias}{$n}) { $t .= "$n is an alias for $sh->{alias}{$n}\n" }
78 1 elsif ($sh->{cmd}{$n}) { $t .= "$n is a shell function\n" }
79 1 elsif ($Proc::ShellSpawn::SPECIAL{$n}) { $t .= "$n is a special shell builtin\n" }
80 1 elsif ($DASHBI{$n}) { $t .= "$n is a shell builtin\n" }
81 1 ▶ elsif (!Proc::ShellSpawn::has_dir($n) && $sh->{hash}{$n}) { $t .= "$n is a tracked alias for $sh->{hash}{$n}\n" }
Condition100%3/3
not Proc::ShellSpawn::has_dir($n) and $sh->{"hash"}{$n}
!ll&&!rl&&r
121
MC/DC100%2/2
not Proc::ShellSpawn::has_dir($n) and $sh->{"hash"}{$n}
not Proc::ShellSpawn::has_dir($n) $sh->{"hash"}{$n}
Truth table100%3/3
not(A) and B
A= not Proc::ShellSpawn::has_dir($n)B= $sh->{"hash"}{$n}
ABresult
0X0
100
111
82 3 ▶ else { my ($p) = Proc::ShellSpawn::has_dir($n) ? (-x Proc::ShellSpawn::path_in($c, $n) && !-d _ ? $n : undef) : find_cmd($c, $n, var($c, 'PATH'));
Branch: 2 branches
BranchTrueFalseTotal
Proc::ShellSpawn::has_dir($n) ? : 1 2 3
-x Proc::ShellSpawn::path_in($c, $n) && !-d(_) ? : 0 1 1
Condition33%1/3
-x Proc::ShellSpawn::path_in($c, $n) and not -d _
!ll&&!rl&&r
100
MC/DC0%0/2
-x Proc::ShellSpawn::path_in($c, $n) and not -d _
-x Proc::ShellSpawn::path_in($c, $n) not -d _
Truth table33%1/3
A and not(B)
A= -x Proc::ShellSpawn::path_in($c, $n)B= not -d _
ABresult
0X0
100
111
83 3 ▶ if (defined $p) { $t .= "$n is $p\n" } else { $t .= "$n: not found\n"; $st = 127 } } }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p) { } 1 2 3
84 5 put($c, 1, $t, done($k, $st)) }
85 0 ▶sub b_times { # the shell's own and its children's user and system time
Subroutine b_times: not called CC=1 SCAR=6.9
Pod undocumented
86 0 my ($c, $argv, $env, $k) = @_; my @t = times;
87 0 ▶ my $f = sub { my $s = shift; sprintf '%dm%.6fs', int($s / 60), $s - 60 * int($s / 60) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
88 0 put($c, 1, join(' ', map $f->($_), @t[0, 1]) . "\n" . join(' ', map $f->($_), @t[2, 3]) . "\n", done($k, 0)) }
89  
90 # ulimit, reading only: dash's letters and units; the limits of this Perl process, from getrlimit (Proc::Spawner's
91 # XSUB; post-0.08: before, /proc/self/limits or a shell's ulimit). POSIX units: blocks of 512 bytes for -f and -c,
92 # KiB for -s -d -m -l -v.
93 529 my %UL = (t => ['CPU', 1], f => ['FSIZE', 512], d => ['DATA', 1024], s => ['STACK', 1024], c => ['CORE', 512],
94 m => ['RSS', 1024], l => ['MEMLOCK', 1024], n => ['NOFILE', 1], v => ['AS', 1024]);
95 12 ▶sub b_ulimit {
Subroutine b_ulimit: called CC=10 SCAR=23.5
Pod undocumented
96 12 my ($c, $argv, $env, $k) = @_; my ($hard, $r) = (0, 'f');
97 12 ▶ for (@$argv[1 .. $#$argv]) { $hard = 1 if /H[a-zA-Z]*\z/ && !/S[^H]*\z/; $r = $1 if /([nsctvlmdf])/ }
Branch: 2 branches
BranchTrueFalseTotal
if /H[a-zA-Z]*\z/ and not /S[^H]*\z/ 1 10 11
if /([nsctvlmdf])/ 11 0 11
Condition66%2/3
/H[a-zA-Z]*\z/ and not /S[^H]*\z/
!ll&&!rl&&r
1001
MC/DC50%1/2
/H[a-zA-Z]*\z/ and not /S[^H]*\z/
/H[a-zA-Z]*\z/ not /S[^H]*\z/
Truth table66%2/3
A and not(B)
A= /H[a-zA-Z]*\z/B= not /S[^H]*\z/
ABresult
0X0
100
111
98 12 my ($name, $div) = @{$UL{$r}};
99 12 ▶ my %res = defined &Proc::Spawner::__rlimits ? Proc::Spawner::__rlimits() : (); # (none on Windows)
Branch: 1 branch
BranchTrueFalseTotal
defined &Proc::Spawner::__rlimits ? : 12 0 12
100 12 ▶ my @v = defined $res{$name} ? Proc::Spawner::__getrlimit($res{$name}) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $res{$name} ? : 12 0 12
101 12 require Proc::ShellSpawn::Large; # (unsupported lives there; this module may run without it)
102 12 ▶ return Proc::ShellSpawn::Large::unsupported($c, "'ulimit -$r'") unless @v;
Branch: 1 branch
BranchTrueFalseTotal
unless @v 0 12 12
103 12 ▶ my $v = $v[$hard ? 1 : 0];
Branch: 1 branch
BranchTrueFalseTotal
$hard ? : 1 11 12
104 12 ▶ put($c, 1, ($v eq 'unlimited' ? $v : int($v / $div)) . "\n", done($k, 0)) }
Branch: 1 branch
BranchTrueFalseTotal
$v eq 'unlimited' ? : 5 7 12
105  
106 529 1;
107 __END__
« lib/Proc/ShellSpawn/Large.pm ↑ Summary lib/Proc/Spawner/Loop.pm »