lib/Proc/ShellSpawn/Builtins.pm

filter: Statementstmt 94.2% 131 / 139 Branchbran 85.1% 80 / 94 Conditioncond 64.5% 20 / 31 MC/DCmcdc 41.1% 7 / 17 Subroutinesub 94.7% 18 / 19 Podpod 0.0% 0 / 10 totaltotal 82.5% 256 / 310 CC 65 SCAR 21.1
CC
65
Cov
88%
b_type
13.1
b_jobs
12.3
b_ulimit
10.5
CRAP 8.2
« 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 547 our $VERSION = '0.13';
3 547 ▶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 547 ▶use Proc::ShellSpawn;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 # JU3 (post-0.12, the user, 2026-11-15): the shell layer's functions are called through the alias ProcSS (Proc::ShellSpawn
6 # sets it), not imported; lines longer than 132 columns because of it stay unwrapped (the user's word, an exception to JQ1)
7  
8 # A builtin counts as implemented when its entry is here; Proc::ShellSpawn looks command names up in
9 # @Proc::ShellSpawn::CMDS, where this table comes first. Each handler: NAME->(CONTEXT, ARGV, ENV, K), calls K once.
10 547 our %CMD = ((map +($_ => [__PACKAGE__ . "::b_$_"]), qw(alias unalias jobs fg bg hash type times ulimit)),
11 (map +($_ => [__PACKAGE__ . '::b_interactive']), qw(history fc bind help compopt logout)));
12 547 unshift @Proc::ShellSpawn::CMDS, \%CMD;
13  
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 ProcSS::put($c, 1, join('', map "$_=" . sq($al->{$_}) . "\n", sort keys %$al), ProcSS::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 ▶ ProcSS::put($c, 1, $out, sub { ProcSS::put($c, 2, $err, ProcSS::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 ProcSS::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 ▶ ProcSS::put($c, 2, $err, ProcSS::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' ? ProcSS::spawn($c, $c->{map}, $argv, $env, $k) : ProcSS::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 ▶ ProcSS::loop_async(sub { my $next = shift; 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 ? ProcSS::put($c, 1, $o->[1], $next) : ProcSS::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 ▶ ProcSS::msg($c, defined $j ? "$argv->[0]: job $j not created under job control" : "$argv->[0]: No current job", ProcSS::done($k, 2)) }
Branch: 1 branch
BranchTrueFalseTotal
defined $j ? : 1 2 3
59 547 *b_fg = \&b_bg;
60  
61 547 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 547 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 ProcSS::put($c, 1, join('', map "$h->{$_}\n", sort keys %$h), ProcSS::done($k, 0)) unless @a;
Branch: 1 branch
BranchTrueFalseTotal
unless @a 3 3 6
67 3 ▶ if ($a[0] eq '-r') { %$h = (); return ProcSS::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) = ProcSS::find_cmd($c, $_, ProcSS::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 ProcSS::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 ▶ ProcSS::loop_async(sub { my $next = shift; @bad ? ProcSS::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 6 ▶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 (!ProcSS::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) = ProcSS::has_dir($n) ? (-x ProcSS::path_in($c, $n) && !-d _ ? $n : undef)
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 : ProcSS::find_cmd($c, $n, ProcSS::var($c, 'PATH'));
84 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
85 5 ProcSS::put($c, 1, $t, ProcSS::done($k, $st)) }
86 1 ▶sub b_times { # the shell's own and its children's user and system time
Subroutine b_times: called CC=1 SCAR=6.9
Pod undocumented
87 0 my ($c, $argv, $env, $k) = @_; my @t = times;
88 0 ▶ my $f = sub { my $s = shift; sprintf '%dm%.6fs', int($s / 60), $s - 60 * int($s / 60) };
Subroutine __ANON__: called CC=1 SCAR=6.9
89 0 ProcSS::put($c, 1, join(' ', map $f->($_), @t[0, 1]) . "\n" . join(' ', map $f->($_), @t[2, 3]) . "\n", ProcSS::done($k, 0)) }
90  
91 # ulimit, reading only: dash's letters and units; the limits of this Perl process, from getrlimit (Proc::Spawner's
92 # XSUB; post-0.08: before, /proc/self/limits or a shell's ulimit). POSIX units: blocks of 512 bytes for -f and -c,
93 # KiB for -s -d -m -l -v.
94 547 my %UL = (t => ['CPU', 1], f => ['FSIZE', 512], d => ['DATA', 1024], s => ['STACK', 1024], c => ['CORE', 512],
95 m => ['RSS', 1024], l => ['MEMLOCK', 1024], n => ['NOFILE', 1], v => ['AS', 1024]);
96 13 ▶sub b_ulimit {
Subroutine b_ulimit: called CC=10 SCAR=23.5
Pod undocumented
97 12 my ($c, $argv, $env, $k) = @_; my ($hard, $r) = (0, 'f');
98 14 ▶ 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
99 14 my ($name, $div) = @{$UL{$r}};
100 14 ▶ my %res = defined &Proc::Spawner::__rlimits ? Proc::Spawner::__rlimits() : (); # (none on Windows)
Branch: 1 branch
BranchTrueFalseTotal
defined &Proc::Spawner::__rlimits ? : 12 0 12
101 14 ▶ my @v = defined $res{$name} ? Proc::Spawner::__getrlimit($res{$name}) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $res{$name} ? : 12 0 12
102 13 require Proc::ShellSpawn::Large; # (unsupported lives there; this module may run without it)
103 13 ▶ return Proc::ShellSpawn::Large::unsupported($c, "'ulimit -$r'") unless @v;
Branch: 1 branch
BranchTrueFalseTotal
unless @v 0 12 12
104 13 ▶ my $v = $v[$hard ? 1 : 0];
Branch: 1 branch
BranchTrueFalseTotal
$hard ? : 1 11 12
105 13 ▶ ProcSS::put($c, 1, ($v eq 'unlimited' ? $v : int($v / $div)) . "\n", ProcSS::done($k, 0)) }
Branch: 1 branch
BranchTrueFalseTotal
$v eq 'unlimited' ? : 5 7 12
106  
107 547 1;
108 __END__
« lib/Proc/ShellSpawn/Large.pm ↑ Summary lib/Proc/Spawner/Loop.pm »