lib/BATsh/SH.pm

filter: Statementstmt 84.7% 4338 / 5117 Branchbran 68.7% 2055 / 2990 Conditioncond 61.0% 957 / 1568 MC/DCmcdc 34.2% 303 / 884 Subroutinesub 96.1% 202 / 210 Podpod 0.0% 0 / 8 totaltotal 72.8% 7855 / 10777 SCAR 110.6
CC
2108
Cov
76%
_eval_test
557.9
_sh_exec_with_redirs
273.5
_parse_select
174.8
CRAP 63775.6
« blib/lib/BATsh/CMD.pm ↑ Summary blib/lib/BATsh.pm »
1 package BATsh::SH;
2 ######################################################################
3 #
4 # BATsh::SH - Pure Perl sh/bash interpreter
5 #
6 # Copyright (c) 2026 INABA Hitoshi <ina.cpan@gmail.com>
7 #
8 # Implements sh/bash command set in Perl.
9 # No external sh or bash required.
10 #
11 # Supported:
12 # Variable assignment: VAR=value
13 # export VAR=value, export VAR, unset VAR
14 # echo, printf
15 # if/then/elif/else/fi
16 # for VAR in list; do ... done
17 # while condition; do ... done
18 # until condition; do ... done
19 # case $var in pat|pat) ... ;; *) ... ;; esac (|, globs, [classes], ;& ;;&)
20 # test / [ ... ] (file, string, integer comparisons)
21 # cd, pwd, exit, true, false, :
22 # trap 'cmd' SIG... / trap - SIG / trap '' SIG / trap [-p] (EXIT + %SIG bridge)
23 # read VAR (reads one line from STDIN)
24 # shift [N] (shift positional parameters left)
25 # local VAR=value (function-scoped variable)
26 # $(( arithmetic )) -- +,-,*,/,%, and $1..$9 inside
27 # $( command ) and `command` (command substitution, nested)
28 # name() { ... }, function name { ... } (function definitions)
29 # cmd1 | cmd2 [| cmd3 ...] (pipeline via tmpfile, 5.005_03)
30 # cmd1 && cmd2, cmd1 || cmd2, cmd1 ; cmd2 (compound commands)
31 # > >> < 2> 2>> 2>&1 1>&2 (I/O redirection)
32 # cmd << DELIM ... DELIM, <<-DELIM, <<'DELIM' (here-document)
33 # cmd & (background execution of an external command; SH mode)
34 # $VAR, ${VAR}, $1..$9, $@, $*, $#, $?, $$, $0, $!
35 # ${VAR:-def}, ${VAR:=def}, ${VAR:+alt}
36 # ${VAR%pat}, ${VAR%%pat} (shortest/longest suffix removal)
37 # ${VAR#pat}, ${VAR##pat} (shortest/longest prefix removal)
38 # ${VAR/pat/rep}, ${VAR//pat/rep} (first/all substitution)
39 # ${VAR^^}, ${VAR^}, ${VAR,,}, ${VAR,} (case conversion)
40 # ${VAR:N:L}, ${VAR:N} (substring)
41 # ${#VAR} (string length)
42 # arr=(a b c), arr+=(d e) (indexed array assignment / append)
43 # arr[i]=v, arr[i]+=v (indexed element assignment / append)
44 # declare -a arr, declare -A map, typeset ... (array declaration)
45 # map=([k1]=v1 [k2]=v2), map[k]=v (associative array assignment)
46 # ${arr[i]}, ${map[key]} (element access; $arr == ${arr[0]})
47 # ${arr[@]}, ${arr[*]} (all elements)
48 # ${#arr[@]}, ${#map[@]} (element count)
49 # ${#arr[i]} (length of one element)
50 # ${!arr[@]}, ${!map[@]} (indices / keys)
51 # unset arr, unset arr[i] (whole array / single element)
52 # source / . file
53 # {a,b,c}, {1..5}, {a..e}[..step] (brace expansion, v0.07)
54 # shopt -s/-u extglob; ?(),*(),+(),@(),!() (extended pattern matching
55 # in case patterns and ${VAR%pat}-family patterns, v0.07)
56 # cmd <<< word (here-string, v0.07)
57 # <(cmd), >(cmd) (process substitution via temp file, v0.07)
58 # select VAR in list; do ... done (menu loop, v0.07)
59 # alias name=value, alias, unalias (v0.07)
60 # exec cmd, exec > file ... (v0.07)
61 # ( cmd1; cmd2 ) (subshell command group with isolated scope, v0.07)
62 #
63 ######################################################################
64  
65 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 36 ▶BEGIN { if ($] < 5.006 && !defined(&warnings::import)) { $INC{'warnings.pm'} = 'stub'; eval 'package warnings; sub import {}' } }
Subroutine BEGIN: called CC=3 SCAR=16.5
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.006" and not defined &warnings::import) 0 36 36
Condition33%1/3
$] < "5.006" and not defined &warnings::import
!ll&&!rl&&r
3600
MC/DC0%0/2
$] < "5.006" and not defined &warnings::import
$] < "5.006" not defined &warnings::import
Truth table33%1/3
A and not(B)
A= $] < "5.006"B= not defined &warnings::import
ABresult
0X0
100
111
67 36 ▶use warnings; local $^W = 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 36 ▶BEGIN { pop @INC if $INC[-1] eq '.' }
Subroutine BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $INC[-1] eq "." 0 36 36
69  
70 36 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
71 36 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
72 36 ▶use Fcntl qw(O_RDONLY O_WRONLY O_CREAT O_EXCL O_TRUNC O_APPEND);
Subroutine BEGIN: called CC=1 SCAR=0.0
73 36 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
74 36 $VERSION = '0.10';
75 36 $VERSION = $VERSION;
76  
77 36 require BATsh::MB;
78  
79 # Bareword filehandle globs for SH pipeline (Perl 5.005_03 compatible)
80 36 ▶use vars qw(*_SH_PIPE_SAVOUT *_SH_PIPE_SAVIN *_SH_PIPE_WFH *_SH_PIPE_RFH);
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 # Bareword filehandle globs for SH I/O redirection (Perl 5.005_03 compatible)
83 36 ▶use vars qw(*_SH_REDIR_SRC *_SH_REDIR_DST *_SH_REDIR_SAVOUT *_SH_REDIR_SAVERR *_SH_REDIR_SAVIN);
Subroutine BEGIN: called CC=1 SCAR=0.0
84  
85 # Bareword filehandle glob for here-document temp file (Perl 5.005_03 compatible)
86 36 ▶use vars qw(*_HD_TMP);
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 # Bareword directory handle for pathname expansion (_glob_paths). One
89 # directory level is read and closed at a time, so a single handle is
90 # enough and is never nested.
91 36 ▶use vars qw(*_GLOB_DH);
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 # Bareword filehandle globs for background-job PID temp file (Perl 5.005_03 compatible)
94 36 ▶use vars qw(*_BG_TMP *_BG_PIDFH);
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 # $! -- PID of the most recent background job (package-level so _expand and
97 # the test suite can read it). Empty string before any background job.
98 36 ▶use vars qw($_LAST_BG_PID);
Subroutine BEGIN: called CC=1 SCAR=0.0
99 36 $_LAST_BG_PID = '';
100  
101 # Active nesting depth of command substitution _cmd_subst(). Each active
102 # level uses a distinct capture temp file so that a nested $( ... $( ... ) )
103 # does not truncate/unlink the file the outer level is still capturing into.
104 # (Sequential, non-overlapping substitutions safely reuse the same depth.)
105 36 ▶use vars qw($_SUBST_DEPTH);
Subroutine BEGIN: called CC=1 SCAR=0.0
106 36 $_SUBST_DEPTH = 0;
107  
108 # Active nesting depth of pipeline execution _exec_sh_pipe(). A pipeline
109 # segment may itself contain a $(...) whose body is another pipeline; each
110 # active pipeline must use distinct stage temp files so the inner pipeline
111 # does not clobber/unlink the outer pipeline's stage file (which would leave
112 # the outer's final segment reading from the real STDIN and hanging).
113 36 ▶use vars qw($_PIPE_DEPTH);
Subroutine BEGIN: called CC=1 SCAR=0.0
114 36 $_PIPE_DEPTH = 0;
115  
116 # SH function registry -- must be package-level for access from _expand and _exec_line
117 36 ▶use vars qw(%_SH_FUNCTIONS);
Subroutine BEGIN: called CC=1 SCAR=0.0
118  
119 # SH alias registry (v0.07). NAME (case-sensitive, as bash) => raw
120 # replacement text. Populated by the "alias" builtin, consumed by
121 # _alias_expand_line() at the top of _exec_line().
122 36 ▶use vars qw(%_SH_ALIAS);
Subroutine BEGIN: called CC=1 SCAR=0.0
123  
124 # Variable attribute registries (v0.08). Keyed by the UPPERCASED variable
125 # name (matching BATsh::Env's case-insensitive store). %_SH_READONLY marks
126 # a variable read-only ("readonly" / "declare -r"); a later assignment is
127 # refused with a diagnostic and non-zero status. %_SH_INTATTR marks the
128 # integer attribute ("declare -i"); an assignment then evaluates its right
129 # hand side as shell arithmetic.
130 36 ▶use vars qw(%_SH_READONLY %_SH_INTATTR);
Subroutine BEGIN: called CC=1 SCAR=0.0
131  
132 # Shell umask (v0.08). Native Win32 Perl's umask() is effectively a no-op
133 # (it always reports 0), so the shell keeps its OWN notion of the mask so
134 # that "umask MODE; umask" round-trips on every platform. It is seeded
135 # once from the real process umask (meaningful on Unix-like systems) and,
136 # on a set, is also pushed to the OS umask so it still affects file modes
137 # where the OS honours it.
138 36 ▶use vars qw($_SH_UMASK $_SH_UMASK_INIT);
Subroutine BEGIN: called CC=1 SCAR=0.0
139 36 $_SH_UMASK_INIT = 0;
140  
141 # Process-substitution bookkeeping (v0.07). <(cmd) is captured eagerly
142 # into a temp file during _expand(); >(cmd) defers cmd until after the
143 # current simple command finishes. Both use unique sysopen(O_CREAT|
144 # O_EXCL) temp files, mirroring _hd_tempfile() / _subst_tempfile().
145 36 ▶use vars qw($_PROCSUB_SEQ @_PROCSUB_TMPFILES @_PROCSUB_DEFERRED);
Subroutine BEGIN: called CC=1 SCAR=0.0
146 36 $_PROCSUB_SEQ = 0;
147 36 @_PROCSUB_TMPFILES = ();
148 36 @_PROCSUB_DEFERRED = ();
149  
150 # SH array storage (v0.06). Indexed and associative arrays.
151 # %_SH_ARRAY : NAME (uppercased) => hashref { subscript => value }
152 # %_SH_ARRAY_TYPE : NAME (uppercased) => 'indexed' | 'assoc'
153 # Array names are case-insensitive (stored uppercase) to match the scalar
154 # store. Indexed arrays may be sparse; subscripts are integer strings.
155 # Associative arrays use arbitrary string subscripts. Element order for
156 # ${arr[@]} is ascending numeric index (indexed) or ascending key sort
157 # (assoc) -- the latter is chosen for deterministic output across Perl
158 # versions, since bash leaves associative-array order unspecified.
159 36 ▶use vars qw(%_SH_ARRAY %_SH_ARRAY_TYPE);
Subroutine BEGIN: called CC=1 SCAR=0.0
160  
161 # SH trap registry (v0.06). Maps a normalized signal name (e.g. INT, TERM,
162 # EXIT) to the raw command string to run when that signal/event fires. An
163 # empty string means "ignore"; absence means "default". Real signals are
164 # bridged to Perl's %SIG (see _sh_set_os_sig); the EXIT pseudo-signal is run
165 # internally when the script exits (see fire_exit_trap / _cmd_exit).
166 36 ▶use vars qw(%_SH_TRAP);
Subroutine BEGIN: called CC=1 SCAR=0.0
167  
168 # getopts state (v0.07). POSIX getopts tracks its position across the
169 # argument list with the OPTIND shell variable (1-based, stored in the
170 # Env), but the offset of the next option CHARACTER inside a clustered
171 # argument such as "-abc" is internal state that the shell does not
172 # expose. We keep that character offset in $_GETOPTS_CHARPOS and detect
173 # an external reset of the loop (the script setting OPTIND=1 before a
174 # fresh getopts loop) by remembering, in $_GETOPTS_LAST_OPTIND, the
175 # OPTIND value getopts itself last stored: if the OPTIND we read back
176 # differs, the caller reset the loop and the character offset restarts.
177 36 ▶use vars qw($_GETOPTS_CHARPOS $_GETOPTS_LAST_OPTIND);
Subroutine BEGIN: called CC=1 SCAR=0.0
178 36 $_GETOPTS_CHARPOS = 0;
179 36 $_GETOPTS_LAST_OPTIND = 0;
180  
181 # "command NAME ..." bypass flag (v0.08). The POSIX "command" builtin runs
182 # NAME as if no shell function of that name existed. _cmd_command() localizes
183 # this to 1 around the re-dispatch of the raw remainder so that the
184 # function-lookup gate in _exec_line_impl() is skipped for that one command
185 # (builtins and external programs are unaffected, exactly as bash does).
186 36 ▶use vars qw($_CMD_NO_FUNC);
Subroutine BEGIN: called CC=1 SCAR=0.0
187 36 $_CMD_NO_FUNC = 0;
188 # ----------------------------------------------------------------
189 36 my $LAST_STATUS = 0; # $?
190  
191 # Shell options (set -e / -u / -x), process-persistent like the Env store;
192 # reset by reset_sh_options() at the start of each top-level run.
193 36 my $_OPT_ERREXIT = 0; # set -e: exit on a failing simple command
194 36 my $_OPT_NOUNSET = 0; # set -u: error on expanding an unset variable
195 36 my $_OPT_XTRACE = 0; # set -x: trace each simple command to STDERR
196 36 my $_OPT_EXTGLOB = 0; # shopt -s extglob: ?(),*(),+(),@(),!() in patterns
197 36 my $_ERREXIT_HOLD = 0; # >0: -e suppressed (condition / non-final &&-|| member)
198 36 my $_ERREXIT_DONE = 0; # set by _exec_sh_compound: -e already adjudicated
199 36 my @FUNCTION_STACK = (); # for 'local' variable scoping
200  
201 # Signal: pending exit
202 36 my $_EXIT_CODE = undef; # undef = no exit pending
203 36 my $_BREAK = 0; # break out of loop
204 36 my $_CONTINUE = 0; # continue next iteration
205 36 my $_RETURN = 0; # return from function/source
206  
207 # Here-document state (Perl 5.005_03 compatible)
208 36 my $_HD_SEQ = 0; # per-process counter for unique temp names
209 36 my @_HD_TMPFILES = (); # tempfiles to remove on END (failsafe cleanup)
210  
211 # Background-job state (Perl 5.005_03 compatible)
212 36 my $_BG_SEQ = 0; # per-process counter for unique pidfile names
213 36 my @_BG_TMPFILES = (); # pidfiles to remove on END (failsafe cleanup)
214  
215 # Command-substitution capture state (Perl 5.005_03 compatible)
216 36 my $_SUBST_SEQ = 0; # per-process counter for unique capture names
217 36 my @_SUBST_TMPFILES = (); # capture files to remove on END (failsafe cleanup)
218  
219 # SH pipeline stage state (Perl 5.005_03 compatible)
220 36 my $_PIPE_SEQ = 0; # per-process counter for unique stage names
221 36 my @_SHP_TMPFILES = (); # stage files to remove on END (failsafe cleanup)
222  
223 # ----------------------------------------------------------------
224 # Public: execute an array of SH lines
225 # Returns exit status (0 = success)
226 # ----------------------------------------------------------------
227 # ----------------------------------------------------------------
228 # get_status / set_status: read or set the SH-side $? from outside
229 # the interpreter. Used by BATsh.pm to bridge $? <-> %ERRORLEVEL%
230 # at every CMD<->SH section boundary. Call as plain functions.
231 # ----------------------------------------------------------------
232 816 ▶sub get_status { return $LAST_STATUS }
Subroutine get_status: called CC=1 SCAR=0.0
Pod undocumented
233 sub set_status {
234 123 ▶ $LAST_STATUS = defined $_[0] ? int($_[0]) : 0;
Subroutine set_status: called CC=2 SCAR=7.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 123 0 123
235 123 return $LAST_STATUS;
236 }
237  
238 # ----------------------------------------------------------------
239 # exit_code_pending: the exit code set by the SH "exit" builtin in the
240 # most recent exec_block, or undef when no exit was executed. The value
241 # stays readable until the next exec_block resets it; BATsh.pm reads it
242 # right after each SH block to terminate the whole script.
243 # ----------------------------------------------------------------
244 383 ▶sub exit_code_pending { return $_EXIT_CODE }
Subroutine exit_code_pending: called CC=1 SCAR=0.0
Pod undocumented
245  
246 sub exec_block {
247 426 ▶ my ($class, $lines_ref, %opts) = @_;
Subroutine exec_block: called CC=2 SCAR=6.9
Pod undocumented
248 426 $_EXIT_CODE = undef;
249 426 $_BREAK = 0;
250 426 $_CONTINUE = 0;
251 426 $_RETURN = 0;
252  
253 426 my $status = _run_lines($class, $lines_ref, \%opts);
254 426 ▶ return defined $_EXIT_CODE ? $_EXIT_CODE : $status;
Branch: 1 branch
BranchTrueFalseTotal
defined $_EXIT_CODE ? : 16 410 426
255 }
256  
257 # ----------------------------------------------------------------
258 # Run an array of lines sequentially, handling multi-line blocks
259 # Returns last exit status
260 # ----------------------------------------------------------------
261 sub _run_lines {
262 801 ▶ my ($class, $lines_ref, $opts_ref) = @_;
Subroutine _run_lines: called CC=32 SCAR=34.9
263 801 my @lines = @{$lines_ref};
264 801 my $status = 0;
265 801 my $i = 0;
266  
267 801 ▶ while ($i <= $#lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#lines) 1279 787 2066
268 1279 ▶ return $status if defined $_EXIT_CODE;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_EXIT_CODE 14 1265 1279
269 1265 ▶ return $status if $_BREAK || $_RETURN;
Branch: 1 branch
BranchTrueFalseTotal
if $_BREAK or $_RETURN 0 1265 1265
Condition33%1/3
$_BREAK or $_RETURN
l!l&&r!l&&!r
001265
MC/DC0%0/2
$_BREAK or $_RETURN
$_BREAK $_RETURN
Truth table33%1/3
A or B
A= $_BREAKB= $_RETURN
ABresult
1X1
011
000
270  
271 1265 my $line = $lines[$i];
272 1265 $i++;
273 # Normalise
274 1265 $line =~ s/\r?\n\z//;
275 # Strip a trailing "# comment" (word-initial, unquoted) and write
276 # the cleaned line back so downstream block parsers see it too.
277 1265 my $nocmt = _strip_sh_comment($line);
278 1265 ▶ if ($nocmt ne $line) { $line = $nocmt; $lines[$i - 1] = $line; }
Branch: 1 branch
BranchTrueFalseTotal
if ($nocmt ne $line) 1 1264 1265
279 # Skip empty and comment lines
280 1265 ▶ next if $line =~ /\A\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\s*\z/ 179 1086 1265
281 1086 ▶ next if $line =~ /\A\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\s*#/ 0 1086 1086
282  
283 # Check for block-opening keywords
284 1086 my $stripped = $line;
285 1086 $stripped =~ s/\A\s+//;
286 1086 my $first = '';
287 1086 ($first) = ($stripped =~ /\A(\S+)/);
288 1086 ▶ $first = lc(defined($first) ? $first : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $first ? : 1086 0 1086
289  
290 1086 ▶ if ($first eq 'if') {
Branch: 1 branch
BranchTrueFalseTotal
if ($first eq "if") 16 1070 1086
291 16 ($status, $i) = _parse_if($class, \@lines, $i - 1, $opts_ref);
292 16 next;
293 }
294 1070 ▶ if ($first eq 'for') {
Branch: 1 branch
BranchTrueFalseTotal
if ($first eq "for") 19 1051 1070
295 19 ($status, $i) = _parse_for($class, \@lines, $i - 1, $opts_ref);
296 19 next;
297 }
298 1051 ▶ if ($first eq 'select') {
Branch: 1 branch
BranchTrueFalseTotal
if ($first eq "select") 3 1048 1051
299 3 ($status, $i) = _parse_select($class, \@lines, $i - 1, $opts_ref);
300 3 next;
301 }
302 1048 ▶ if ($first eq 'while' || $first eq 'until') {
Branch: 1 branch
BranchTrueFalseTotal
if ($first eq "while" or $first eq "until") 31 1017 1048
Condition100%3/3
$first eq "while" or $first eq "until"
l!l&&r!l&&!r
2921017
MC/DC100%2/2
$first eq "while" or $first eq "until"
$first eq "while" $first eq "until"
Truth table100%3/3
A or B
A= $first eq "while"B= $first eq "until"
ABresult
1X1
011
000
303 31 ($status, $i) = _parse_while($class, \@lines, $i - 1, $opts_ref);
304 31 next;
305 }
306 1017 ▶ if ($first eq 'case') {
Branch: 1 branch
BranchTrueFalseTotal
if ($first eq "case") 22 995 1017
307 22 ($status, $i) = _parse_case($class, \@lines, $i - 1, $opts_ref);
308 22 next;
309 }
310  
311 # Subshell command group: "( commands )" [redir]. A leading "(("
312 # is arithmetic-expansion-flavoured, not a subshell group, and is
313 # left alone (standalone "((...))" arithmetic commands are not
314 # implemented; only "$((...))" as an expansion is).
315 995 ▶ if ($stripped =~ /\A\(/ && $stripped !~ /\A\(\(/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stripped =~ /\A\(/ and not $stripped =~ /\A\(\(/) 8 987 995
Condition66%2/3
$stripped =~ /\A\(/ and not $stripped =~ /\A\(\(/
!ll&&!rl&&r
98708
MC/DC50%1/2
$stripped =~ /\A\(/ and not $stripped =~ /\A\(\(/
$stripped =~ /\A\(/ not $stripped =~ /\A\(\(/
Truth table66%2/3
A and not(B)
A= $stripped =~ /\A\(/B= not $stripped =~ /\A\(\(/
ABresult
0X0
100
111
316 8 ($status, $i) = _parse_subshell($class, \@lines, $i - 1, $opts_ref);
317 8 next;
318 }
319  
320 # Function definition: "name() {" or "function name {"
321 987 ▶ if ($stripped =~ /\A(?:function\s+[A-Za-z_]|[A-Za-z_][A-Za-z0-9_]*\s*\(\s*\))/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stripped =~ /\A(?:function\s+[A-Za-z_]|[A-Za-z_][A-Za-z0-9_]*\s*\(\s*\))/) 15 972 987
322 15 ($status, $i) = _parse_function($class, \@lines, $i - 1, $opts_ref);
323 15 next;
324 }
325  
326 # Here-document: cmd << [-] [QUOTE]DELIM[QUOTE]
327 # Detected on a simple command line; body is read from following
328 # lines up to a line equal to DELIM (after optional tab strip for <<-).
329 972 my @hd = _hd_detect($line);
330 972 ▶ if (@hd) {
Branch: 1 branch
BranchTrueFalseTotal
if (@hd) 13 959 972
331 13 my ($cmd_part, $dash, $delim, $quoted) = @hd;
332 13 my @body = ();
333 13 my $terminated = 0;
334 13 ▶ while ($i <= $#lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#lines) 29 1 30
335 29 my $bl = $lines[$i];
336 29 $i++;
337 29 $bl =~ s/\r?\n\z//;
338 29 my $probe = $bl;
339 29 ▶ $probe =~ s/\A\t+// if $dash; # <<- strips leading tabs
Branch: 1 branch
BranchTrueFalseTotal
if $dash 2 27 29
340 29 ▶ if ($probe eq $delim) { $terminated = 1; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($probe eq $delim) 12 17 29
341 17 ▶ $bl =~ s/\A\t+// if $dash; # also strip tabs from body
Branch: 1 branch
BranchTrueFalseTotal
if $dash 1 16 17
342 17 push @body, $bl;
343 }
344 13 ▶ if (!$terminated) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($terminated) 1 12 13
345 1 warn "sh: unexpected EOF while looking for here-document delimiter \`$delim'\n";
346 1 $LAST_STATUS = 2;
347 1 $status = 2;
348 1 next;
349 }
350 12 $status = _hd_run($class, $cmd_part, \@body, $quoted, $opts_ref);
351 12 next;
352 }
353  
354 # A simple-command prefix followed on the SAME physical line by a
355 # control structure introduced with ';' -- e.g.
356 # x=""; if [ -z "$x" ]; then echo empty; fi
357 # i=0; while [ $i -lt 3 ]; do ...; done
358 # v=cat; case $v in ...; esac
359 # The first token is not a control keyword, so the block-opener
360 # dispatch above did not fire. Run the prefix now, then rewrite
361 # the current slot to the control structure and reprocess it, so
362 # the proper _parse_* handles it (and may consume following
363 # physical lines for a multi-line body).
364 959 my $ctl_off = _find_control_split($line);
365 959 ▶ if (defined $ctl_off) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ctl_off) 5 954 959
366 5 my $prefix = substr($line, 0, $ctl_off);
367 5 my $rest = substr($line, $ctl_off);
368 5 $prefix =~ s/\s*;\s*\z//;
369 5 ▶ if ($prefix =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix =~ /\S/) 5 0 5
370 5 $_ERREXIT_DONE = 0;
371 5 $status = _exec_line($class, $prefix, $opts_ref);
372 5 ▶ _errexit_check($status) unless $_ERREXIT_DONE;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ERREXIT_DONE 5 0 5
373 5 $_ERREXIT_DONE = 0;
374 }
375 5 ▶ last if defined $_EXIT_CODE || $_BREAK || $_RETURN;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_EXIT_CODE or $_BREAK or $_RETURN 0 5 5
Condition33%2/6
defined $_EXIT_CODE or $_BREAK or $_RETURN
l!l&&r!l&&!r
005
defined $_EXIT_CODE or $_BREAK
l!l&&r!l&&!r
005
MC/DC0%0/3
defined $_EXIT_CODE or $_BREAK or $_RETURN
defined $_EXIT_CODE $_BREAK $_RETURN
Truth table25%1/4
A or B or C
A= defined $_EXIT_CODEB= $_BREAKC= $_RETURN
ABCresult
1XX1
01X1
0011
0000
376 5 $lines[$i - 1] = $rest;
377 5 $i--;
378 5 next;
379 }
380  
381 954 $_ERREXIT_DONE = 0;
382 954 $status = _exec_line($class, $line, $opts_ref);
383 954 ▶ _errexit_check($status) unless $_ERREXIT_DONE;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ERREXIT_DONE 913 41 954
384 954 $_ERREXIT_DONE = 0;
385 954 ▶ $_CONTINUE = 0 if $_CONTINUE;
Branch: 1 branch
BranchTrueFalseTotal
if $_CONTINUE 0 954 954
386 }
387 787 return $status;
388 }
389  
390 # ----------------------------------------------------------------
391 # Execute one SH line
392 # ----------------------------------------------------------------
393 # _exec_line: thin wrapper around _exec_line_impl() that drains any
394 # process-substitution bookkeeping (v0.07) added while executing THIS
395 # call's line -- running deferred >(cmd) jobs and removing <(cmd) temp
396 # files -- after the simple command has run. Because _exec_line_impl()
397 # itself calls back into _exec_line() (never _exec_line_impl() directly)
398 # for background execution, redirection wrapping, compound/pipeline
399 # segments, and exec, each nesting level drains only the entries it
400 # introduced, using the before/after array-length markers below.
401 # ----------------------------------------------------------------
402 sub _exec_line {
403 1145 ▶ my ($class, $raw, $opts_ref) = @_;
Subroutine _exec_line: called CC=6 SCAR=17.9
404 1145 my $tmp_before = scalar(@_PROCSUB_TMPFILES);
405 1145 my $def_before = scalar(@_PROCSUB_DEFERRED);
406 1145 my $rc = _exec_line_impl($class, $raw, $opts_ref);
407 1145 ▶ if (@_PROCSUB_DEFERRED > $def_before) {
Branch: 1 branch
BranchTrueFalseTotal
if (@BATsh::SH::_PROCSUB_DEFERRED > $def_before) 1 1144 1145
408 1 my @jobs = splice(@_PROCSUB_DEFERRED, $def_before);
409 1 for my $job (@jobs) {
410 1 my ($tmp, $cmd) = @{$job};
411 1 _sh_exec_with_redirs($class, $cmd, [[0, 0, $tmp]], $opts_ref);
412 }
413 }
414 1145 ▶ if (@_PROCSUB_TMPFILES > $tmp_before) {
Branch: 1 branch
BranchTrueFalseTotal
if (@BATsh::SH::_PROCSUB_TMPFILES > $tmp_before) 2 1143 1145
415 2 my @files = splice(@_PROCSUB_TMPFILES, $tmp_before);
416 2 ▶ for my $f (@files) { unlink $f if defined $f }
Branch: 1 branch
BranchTrueFalseTotal
if defined $f 2 0 2
417 }
418 1145 return $rc;
419 }
420  
421 sub _exec_line_impl {
422 1145 ▶ my ($class, $raw, $opts_ref) = @_;
Subroutine _exec_line_impl: called CC=72 SCAR=44.0
423  
424 1145 my $line = $raw;
425 1145 $line =~ s/\A\s+//;
426 1145 ▶ return 0 if $line =~ /\A\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\s*\z/ 0 1145 1145
427 1145 ▶ return 0 if $line =~ /\A\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\s*#/ 0 1145 1145
428  
429 # Shebang: treat as comment
430 1145 ▶ return 0 if $line =~ /\A#!/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A#!/ 0 1145 1145
431  
432 # Alias expansion (v0.07): replace a leading command word that names
433 # an active alias with its stored text, re-checking the new leading
434 # word up to a small bounded number of times so that alias chains
435 # (alias ll=ls; alias ls='ls -la') resolve. A name is expanded at
436 # most once per line to guard against a self-referential alias
437 # (alias ls=ls) looping forever. Only the first word of the line is
438 # considered -- bash's "trailing space in the alias value also makes
439 # the next word alias-eligible" refinement is not modelled.
440 1145 $line = _alias_expand_line($line);
441  
442 # Brace expansion (v0.07): {a,b,c} and {1..5}/{a..e} word-generation,
443 # performed lexically on the raw line before any other expansion, so
444 # that e.g. "echo a{b,c}d" becomes two arguments "abd acd". Regions
445 # that are quoted, or that belong to ${...}/$(...)/$((...))/`...`,
446 # are left untouched (their braces/parens are not brace-expansion
447 # syntax).
448 1145 $line = _brace_expand_line($line);
449  
450 # ----------------------------------------------------------------
451 # Background execution: an unquoted trailing & (v1).
452 # Detected here, BEFORE _split_sh_compound, so that the bare & is
453 # never mistaken for && and so that an internal & (e.g. in 2>&1 or
454 # >&2) is left untouched. Only the single & at the very end of the
455 # line is consumed. Builtins / functions / control words / variable
456 # assignments run in the FOREGROUND (the & is ignored); only external
457 # commands are launched asynchronously.
458 # ----------------------------------------------------------------
459 1145 my ($_is_bg, $_bg_line) = _split_trailing_bg($line);
460 1145 ▶ if ($_is_bg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_is_bg) 3 1142 1145
461 3 $line = $_bg_line;
462 3 my $probe = $line;
463 3 $probe =~ s/\A\s+//;
464 3 my $w0 = '';
465 3 ($w0) = ($probe =~ /\A(\S+)/);
466 3 ▶ $w0 = '' unless defined $w0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $w0 0 3 3
467 3 ▶ if (_sh_word_is_foreground($w0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_sh_word_is_foreground($w0)) 2 1 3
468 # & ignored: run the stripped line in the foreground.
469 2 return _exec_line($class, $line, $opts_ref);
470 }
471 1 my $exp = _expand($class, $line);
472 1 $exp =~ s/\A\s+//;
473 1 $exp =~ s/\s+\z//;
474 1 return _bg_launch($class, $exp);
475 }
476  
477 # Detect && / || / ; compound commands BEFORE expansion.
478 # These must be split before _expand so that short-circuit logic works.
479 1142 my @compound = _split_sh_compound($line);
480 1142 ▶ if (@compound > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@compound > 1) 46 1096 1142
481 46 return _exec_sh_compound($class, \@compound, $opts_ref);
482 }
483  
484 # set -x: trace the simple command to STDERR (the raw, pre-expansion
485 # line -- expanding a copy here would run $(...) substitutions twice).
486 1096 ▶ print STDERR '+ ', BATsh::MB::dec($line), "\n" if $_OPT_XTRACE;
Branch: 1 branch
BranchTrueFalseTotal
if $_OPT_XTRACE 1 1095 1096
487  
488 # Detect pipeline BEFORE variable expansion to avoid expanding
489 # pipe-like characters inside command substitutions prematurely.
490 # _split_sh_pipe returns >1 segment only when bare | is present.
491 1096 my @pipe_segs = _split_sh_pipe($line);
492 1096 ▶ if (@pipe_segs > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pipe_segs > 1) 24 1072 1096
493 24 return _exec_sh_pipe($class, \@pipe_segs, $opts_ref);
494 }
495  
496 # A single control/compound command (while/for/if/until/case/select or
497 # a "( subshell )") may reach here as a pipeline element or && / ||
498 # operand -- e.g. the right side of cmd | while read x; do ...; done,
499 # or true && for i in ..; do ..; done. Route it through the block
500 # runner so its inline ';'-separated body is parsed by the proper
501 # _parse_* handler instead of being split and exec'd as external words.
502 1072 ▶ if (_seg_is_control($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_seg_is_control($line)) 5 1067 1072
503 5 my @one = ($line);
504 5 return _run_lines($class, \@one, $opts_ref);
505 }
506  
507 # Array / associative-array operations (v0.06). Detected on the RAW line
508 # (before _expand) so that the "(a b c)" literal and the "[sub]" subscripts
509 # are not mangled by variable / command-substitution expansion.
510 {
511 1067 my @h = _sh_try_array_op($class, $line, $opts_ref);
512 1067 ▶ return $h[1] if @h;
Branch: 1 branch
BranchTrueFalseTotal
if @h 43 1024 1067
513 }
514  
515 # trap (v0.06). Detected on the RAW line so that the handler command is
516 # captured literally and (re-)expanded only when the trap fires, matching
517 # shell semantics for e.g. trap 'rm $tmp' EXIT.
518 {
519 1067 my $probe = $line;
520 1024 $probe =~ s/\A\s+//;
521 1024 ▶ if ($probe =~ /\Atrap(\s.*|)\z/is && $probe !~ /\Atrap\s*=/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($probe =~ /\Atrap(\s.*|)\z/is and not $probe =~ /\Atrap\s*=/) 17 1007 1024
Condition66%2/3
$probe =~ /\Atrap(\s.*|)\z/is and not $probe =~ /\Atrap\s*=/
!ll&&!rl&&r
1007017
MC/DC50%1/2
$probe =~ /\Atrap(\s.*|)\z/is and not $probe =~ /\Atrap\s*=/
$probe =~ /\Atrap(\s.*|)\z/is not $probe =~ /\Atrap\s*=/
Truth table66%2/3
A and not(B)
A= $probe =~ /\Atrap(\s.*|)\z/isB= not $probe =~ /\Atrap\s*=/
ABresult
0X0
100
111
522 17 return _cmd_trap($class, $1, $opts_ref);
523 }
524 }
525  
526 # POSIX assignment prefix on the RAW line: `VAR=value command args`.
527 # Detected before expansion so that a value containing $(...) or quoted
528 # spaces is not mistaken for a trailing command. Pure assignments (no
529 # command following) fall through to the post-expansion handler below.
530 {
531 1024 my ($pairs_ref, $remainder) = _sh_assign_prefix($line);
532 1007 ▶ if ($pairs_ref && defined $remainder && $remainder ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pairs_ref and defined $remainder and $remainder ne "") 2 1005 1007
Condition83%5/6
$pairs_ref and defined $remainder and $remainder ne ""
!ll&&!rl&&r
8241812
$pairs_ref and defined $remainder
!ll&&!rl&&r
8240183
MC/DC66%2/3
$pairs_ref and defined $remainder and $remainder ne ""
$pairs_ref defined $remainder $remainder ne ""
Truth table75%3/4
A and B and C
A= $pairs_refB= defined $remainderC= $remainder ne ""
ABCresult
0XX0
10X0
1100
1111
533 2 my $ro_fail = 0;
534 2 for my $p (@{$pairs_ref}) {
535 2 my ($var, $rawval) = @{$p};
536 2 my $val = _arr_dequote(_expand($class, $rawval));
537 2 ▶ $ro_fail = 1 unless _sh_store_scalar($var, $val);
Branch: 1 branch
BranchTrueFalseTotal
unless _sh_store_scalar($var, $val) 0 2 2
538 }
539 2 ▶ if ($ro_fail) { $LAST_STATUS = 1; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($ro_fail) 0 2 2
540 2 return _exec_line($class, $remainder, $opts_ref);
541 }
542 }
543  
544 # Keep the pre-expansion text around. It is used below (echo) to
545 # decide whether filename globbing should even be attempted: that
546 # decision must be based on glob metacharacters that were literally
547 # present in the source line, never on characters that merely ended
548 # up looking like "*", "?" or "[" because a variable's *value*
549 # happened to contain them (e.g. a getopts "?"/":" result). This is
550 # the same principle the tilde-prepass above already relies on:
551 # expansion results are not themselves subject to re-expansion.
552 1005 my $_raw_pre_expand = $line;
553  
554 # Expand variables and command substitutions
555 1005 $line = _expand($class, $line);
556  
557 # Strip trailing ;
558 1005 $line =~ s/\s*;\s*\z//;
559  
560 # exec (v0.07): "exec > file" (etc.) applies its redirections
561 # permanently to the current shell (no restore); "exec cmd ..." runs
562 # cmd with those redirections and then terminates the whole script
563 # with cmd's exit status, approximating process replacement without
564 # a real fork/exec (this interpreter has no fork, by design -- see
565 # the pipeline / background-job implementation notes above).
566 1005 ▶ if ($line =~ /\Aexec(?:\s+(.*)|\s*)\z/is) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\Aexec(?:\s+(.*)|\s*)\z/is) 2 1003 1005
567 2 ▶ my $exec_rest = defined $1 ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 2 0 2
568 2 return _cmd_exec($class, $exec_rest, $opts_ref);
569 }
570  
571 # Here-string: cmd <<< word (v0.07). Detected after expansion (the
572 # word is fully variable/command/arithmetic-expanded, like any other
573 # word) and turned into an ordinary "< tempfile" stdin redirection,
574 # reusing the here-document temp-file machinery.
575 {
576 1003 my ($hs_line, $hs_content) = _sh_strip_herestring($line);
577 1003 ▶ if (defined $hs_content) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hs_content) 2 1001 1003
578 2 my $tmp = _hd_tempfile($hs_content . "\n");
579 2 ▶ if (!defined $tmp) { $LAST_STATUS = 2; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tmp) 0 2 2
580 2 my ($clean_line, $redirs_ref) = _sh_strip_redirects($hs_line);
581 2 unshift @{$redirs_ref}, [0, 0, $tmp]
582 2 ▶ unless grep { $_->[0] == 0 } @{$redirs_ref};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_->[0] == 0;} @{$redirs_ref;} 2 0 2
583 2 my $rc = _sh_exec_with_redirs($class, $clean_line, $redirs_ref, $opts_ref);
584 2 unlink $tmp;
585 2 @_HD_TMPFILES = grep { $_ ne $tmp } @_HD_TMPFILES;
586 2 return $rc;
587 }
588 }
589  
590 # Detect I/O redirections: >, >>, <, 2>, 2>>, 2>&1
591 # Must be done after expansion so that variable-in-filename works.
592 1001 my ($clean_line, $sh_redirs_ref) = _sh_strip_redirects($line);
593 1001 ▶ if (@{$sh_redirs_ref}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$sh_redirs_ref;}) 20 981 1001
594 20 return _sh_exec_with_redirs($class, $clean_line, $sh_redirs_ref, $opts_ref);
595 }
596 981 $line = $clean_line;
597  
598 981 my ($cmd, $rest) = _split_sh($line);
599 981 ▶ return 0 unless defined $cmd && $cmd ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cmd and $cmd ne "" 0 981 981
Condition33%1/3
defined $cmd and $cmd ne ""
!ll&&!rl&&r
00981
MC/DC0%0/2
defined $cmd and $cmd ne ""
defined $cmd $cmd ne ""
Truth table33%1/3
A and B
A= defined $cmdB= $cmd ne ""
ABresult
0X0
100
111
600  
601 981 my $lc_cmd = lc($cmd);
602  
603 # Pure assignment: VAR=value (no spaces around =). Assignment prefixes
604 # of the form `VAR=value command` were already handled before expansion,
605 # so anything reaching here is a standalone assignment. Match the whole
606 # (expanded) line so values containing spaces are preserved in full.
607 981 ▶ if ($line =~ /\A([A-Za-z_][A-Za-z0-9_]*)=(.*)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A([A-Za-z_][A-Za-z0-9_]*)=(.*)\z/s) 181 800 981
608 181 my ($var, $val) = ($1, $2); # capture before $1 is clobbered
609 # Dequote the value the same way command words are dequoted, so
610 # concatenated quotes (a"b"c, x'y'z) and backslash escapes resolve
611 # correctly -- not just a single outermost "..." / '...' pair.
612 181 $val = _arr_dequote($val);
613 181 my $ok = _sh_store_scalar($var, $val);
614 181 ▶ $LAST_STATUS = $ok ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 179 2 181
615 181 ▶ return $ok ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 179 2 181
616 }
617  
618 800 ▶ if ($lc_cmd eq 'export') { return _cmd_export($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "export") 40 760 800
619 760 ▶ if ($lc_cmd eq 'unset') { return _cmd_unset($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "unset") 6 754 760
620 754 ▶ if ($lc_cmd eq 'echo') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "echo") 425 329 754
621 # Apply word-splitting and glob expansion to unquoted tokens
622 # (tilde expansion already happened earlier, in _expand()).
623 #
624 # Whether to glob at all is decided from the RAW, pre-expansion
625 # source text -- NOT from the already-expanded $rest. By this
626 # point $rest has had all $VAR / ${VAR} / $(...) substitutions
627 # applied, so a variable whose *value* happens to be "?" (as
628 # getopts sets $opt on an unknown option, or ":" on a missing
629 # argument) would otherwise be indistinguishable from a literal
630 # "?" glob pattern typed in the script -- and get run through
631 # _parse_args()/glob() against the current directory, silently
632 # replacing the echoed value with whatever 1-character filename
633 # happened to match. Checking the raw text instead means only
634 # a glob metacharacter that was actually written in the source
635 # (e.g. "echo *.txt") triggers globbing.
636 425 my @raw_parts = _split_sh($_raw_pre_expand);
637 425 my $raw_rest = $raw_parts[1];
638 425 ▶ $raw_rest = '' unless defined $raw_rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw_rest 0 425 425
639 425 ▶ if (_raw_has_glob($raw_rest)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_raw_has_glob($raw_rest)) 35 390 425
640 35 my @words = _parse_args($rest, 1);
641 35 $rest = join(' ', @words);
642 }
643 425 return _cmd_echo($rest);
644 }
645 329 ▶ if ($lc_cmd eq 'printf') { return _cmd_printf($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "printf") 24 305 329
646 305 ▶ if ($lc_cmd eq 'cd') { return _cmd_cd($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "cd") 9 296 305
647 296 ▶ if ($lc_cmd eq 'pwd') { print Cwd::cwd(), "\n"; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "pwd") 5 291 296
648 291 ▶ if ($lc_cmd eq 'exit') { return _cmd_exit($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "exit") 7 284 291
649 284 ▶ if ($lc_cmd eq 'true') { $LAST_STATUS = 0; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "true") 8 276 284
650 276 ▶ if ($lc_cmd eq 'false') { $LAST_STATUS = 1; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "false") 21 255 276
651 255 ▶ if ($lc_cmd eq ':') { $LAST_STATUS = 0; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq ":") 4 251 255
652 251 ▶ if ($lc_cmd eq 'read') { return _cmd_read($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "read") 27 224 251
653 224 ▶ if ($lc_cmd eq 'test' || $cmd eq '[') { return _cmd_test($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "test" or $cmd eq "[") 31 193 224
Condition100%3/3
$lc_cmd eq "test" or $cmd eq "["
l!l&&r!l&&!r
229193
MC/DC100%2/2
$lc_cmd eq "test" or $cmd eq "["
$lc_cmd eq "test" $cmd eq "["
Truth table100%3/3
A or B
A= $lc_cmd eq "test"B= $cmd eq "["
ABresult
1X1
011
000
654 193 ▶ if ($lc_cmd eq 'source' || $cmd eq '.') { return _cmd_source($class, $rest, $opts_ref) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "source" or $cmd eq ".") 0 193 193
Condition33%1/3
$lc_cmd eq "source" or $cmd eq "."
l!l&&r!l&&!r
00193
MC/DC0%0/2
$lc_cmd eq "source" or $cmd eq "."
$lc_cmd eq "source" $cmd eq "."
Truth table33%1/3
A or B
A= $lc_cmd eq "source"B= $cmd eq "."
ABresult
1X1
011
000
655 193 ▶ if ($lc_cmd eq 'return') { $_RETURN = 1; $LAST_STATUS = ($rest =~ /\A\s*(\d+)/) ? int($1) : 0; return $LAST_STATUS }
Branch: 2 branches
BranchTrueFalseTotal
if ($lc_cmd eq "return") 0 193 193
$rest =~ /\A\s*(\d+)/ ? : 0 0 0
656 193 ▶ if ($lc_cmd eq 'break') { $_BREAK = 1; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "break") 3 190 193
657 190 ▶ if ($lc_cmd eq 'continue') { $_CONTINUE = 1; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "continue") 0 190 190
658 190 ▶ if ($lc_cmd eq 'shift') { return _cmd_shift($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "shift") 6 184 190
659 184 ▶ if ($lc_cmd eq 'getopts') { return _cmd_getopts($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "getopts") 55 129 184
660 129 ▶ if ($lc_cmd eq 'local') { return _cmd_local($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "local") 2 127 129
661 127 ▶ if ($lc_cmd eq 'set') { return _cmd_set_sh($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "set") 27 100 127
662 100 ▶ if ($lc_cmd eq 'shopt') { return _cmd_shopt($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "shopt") 2 98 100
663 98 ▶ if ($lc_cmd eq 'alias') { return _cmd_alias($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "alias") 6 92 98
664 92 ▶ if ($lc_cmd eq 'unalias') { return _cmd_unalias($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "unalias") 1 91 92
665 91 ▶ if ($lc_cmd eq 'eval') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "eval") 2 89 91
666 # POSIX eval: the (already expanded) arguments are stripped of one
667 # level of quoting, concatenated, and executed as a new command
668 # line -- which re-parses and re-expands, giving the double
669 # expansion eval exists for. "eval" with no arguments is a noop.
670 2 ▶ if (!defined $rest || $rest =~ /\A\s*\z/) { $LAST_STATUS = 0; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rest or $rest =~ /\A\s*\z/) 0 2 2
Condition33%1/3
not defined $rest or $rest =~ /\A\s*\z/
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $rest or $rest =~ /\A\s*\z/
not defined $rest $rest =~ /\A\s*\z/
Truth table33%1/3
not(A) or B
A= not defined $restB= $rest =~ /\A\s*\z/
ABresult
1X1
011
000
671 2 my @words = _parse_args($rest);
672 2 return _exec_line($class, join(' ', @words), $opts_ref);
673 }
674 89 ▶ if ($lc_cmd eq 'let') { return _cmd_let($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "let") 8 81 89
675 81 ▶ if ($lc_cmd eq 'type') { return _cmd_type($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "type") 7 74 81
676 74 ▶ if ($lc_cmd eq 'command') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "command") 7 67 74
677 # The introspection forms (-v / -V) are pure lookups; the plain
678 # run form re-dispatches the RAW (pre-expansion) remainder so it is
679 # expanded exactly once and shell functions of the same name are
680 # bypassed. Passing $_raw_pre_expand keeps expansion single-pass.
681 7 return _cmd_command($class, $rest, $_raw_pre_expand, $opts_ref);
682 }
683 67 ▶ if ($lc_cmd eq 'umask') { return _cmd_umask($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "umask") 12 55 67
684 55 ▶ if ($lc_cmd eq 'hash') { return _cmd_hash($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "hash") 3 52 55
685 52 ▶ if ($lc_cmd eq 'readonly') { return _cmd_readonly($class, $rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "readonly") 5 47 52
686 47 ▶ if ($lc_cmd eq 'mapfile' || $lc_cmd eq 'readarray') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "mapfile" or $lc_cmd eq "readarray") 5 42 47
Condition100%3/3
$lc_cmd eq "mapfile" or $lc_cmd eq "readarray"
l!l&&r!l&&!r
4142
MC/DC100%2/2
$lc_cmd eq "mapfile" or $lc_cmd eq "readarray"
$lc_cmd eq "mapfile" $lc_cmd eq "readarray"
Truth table100%3/3
A or B
A= $lc_cmd eq "mapfile"B= $lc_cmd eq "readarray"
ABresult
1X1
011
000
687 5 return _cmd_mapfile($class, $rest);
688 }
689  
690 # Defined SH function (skipped while running under "command NAME ...").
691 42 ▶ if (!$_CMD_NO_FUNC && exists $_SH_FUNCTIONS{$cmd}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $BATsh::SH::_CMD_NO_FUNC and exists $BATsh::SH::_SH_FUNCTIONS{$cmd}) 13 29 42
Condition66%2/3
not $BATsh::SH::_CMD_NO_FUNC and exists $BATsh::SH::_SH_FUNCTIONS{$cmd}
!ll&&!rl&&r
02913
MC/DC50%1/2
not $BATsh::SH::_CMD_NO_FUNC and exists $BATsh::SH::_SH_FUNCTIONS{$cmd}
not $BATsh::SH::_CMD_NO_FUNC exists $BATsh::SH::_SH_FUNCTIONS{$cmd}
Truth table66%2/3
not(A) and B
A= not $BATsh::SH::_CMD_NO_FUNCB= exists $BATsh::SH::_SH_FUNCTIONS{$cmd}
ABresult
0X0
100
111
692 13 return _call_sh_function($class, $cmd, $rest, $opts_ref);
693 }
694  
695 # Unknown: try as external (runs via Perl system)
696 29 return _cmd_external($cmd, $rest);
697 }
698  
699 # ----------------------------------------------------------------
700 # Variable / arithmetic expansion
701 # ----------------------------------------------------------------
702 # ----------------------------------------------------------------
703 # Expansion-result protection
704 # ----------------------------------------------------------------
705 # BATsh expands a line first and removes quotes from it afterwards, so
706 # a backslash arriving as the RESULT of an expansion (a variable value,
707 # command-substitution output, or a tilde expansion) used to be re-read
708 # as a shell escape by the later quote-removal stage and silently
709 # deleted. On Windows that ate every path: with HOME set to
710 # C:\home\flower the line `cd ~` tried to enter C:homeflower, and
711 # `d="C:\Users\x"; cd $d` failed in the same way.
712 # POSIX quote removal applies to the source word only, never to what an
713 # expansion produced, so a backslash coming out of an expansion is
714 # stashed here behind a NUL-delimited sentinel (a NUL can never occur
715 # in shell source) and becomes a literal backslash again in
716 # _arr_dequote() -- or in _unprotect_lit(), for the few consumers that
717 # never dequote (external command lines, here-document bodies, the
718 # variable store).
719 # ----------------------------------------------------------------
720 sub _protect_lit {
721 556 ▶ my ($v) = @_;
Subroutine _protect_lit: called CC=2 SCAR=7.0
722 556 ▶ return '' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 556 556
723 556 $v =~ s/\\/\x00BATSH_LB\x00/g;
724 556 return $v;
725 }
726  
727 sub _unprotect_lit {
728 1361 ▶ my ($v) = @_;
Subroutine _unprotect_lit: called CC=2 SCAR=7.0
729 1361 ▶ return '' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 1361 1361
730 1361 $v =~ s/\x00BATSH_LB\x00/\\/g;
731 1361 return $v;
732 }
733  
734 sub _expand {
735 1190 ▶ my ($class, $str) = @_;
Subroutine _expand: called CC=42 SCAR=45.0
736  
737 1190 ▶ return '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 1190 1190
738  
739 # Tilde expansion (v0.07) MUST run before variable / command
740 # substitution below: POSIX expands ~word from the literal source
741 # text only, once, at the start of a word (or right after the '='
742 # of a leading NAME= assignment). A ~ that only appears because a
743 # $VAR happened to hold a string starting with "~" must NOT be
744 # re-expanded -- doing tilde expansion after $VAR substitution
745 # cannot tell the two cases apart, so it must happen first here.
746 1190 $str = _tilde_prepass($str);
747  
748 # Protect backslash escapes before any expansion. In POSIX shells the
749 # backslash inside double quotes keeps its special meaning only before
750 # $ ` " \ and newline, so "\$" is a literal dollar (NO expansion), "\`"
751 # is a literal backtick (NO command substitution) and "\\" is a literal
752 # backslash. Earlier releases performed the $-/`-substitutions globally
753 # with no escape awareness, so "\$_" expanded $_ to empty and left the
754 # stray backslash (giving e.g. perl -e "...uc(\)..." -> syntax error).
755 # We stash the escaped specials under NUL-delimited sentinels (a NUL can
756 # never occur in shell source) and restore them as literals at the end.
757 # Order matters: "\\" first so that "\\$" means backslash + expansion.
758 1190 $str =~ s/\\\\/\x00BATSH_BS\x00/g; # \\ -> literal backslash
759 1190 $str =~ s/\\\$/\x00BATSH_DL\x00/g; # \$ -> literal dollar (no expand)
760 1190 $str =~ s/\\`/\x00BATSH_BT\x00/g; # \` -> literal backtick (no subst)
761  
762 # $(( arithmetic ))
763 1190 $str =~ s/\$\(\(\s*(.*?)\s*\)\)/_eval_arith($1)/ge;
764  
765 # <(cmd) / >(cmd) process substitution (v0.07) -- MUST run before
766 # $(...) below, and before backtick substitution, so that <(...) is
767 # never mistaken for a stray "<" redirection operator by the later
768 # redirect-stripping stage.
769 1190 $str = _replace_process_subst($class, $str);
770  
771 # $( command ) substitution
772 # Use _extract_cmd_subst to correctly handle nested () and quoted ) chars.
773 1190 $str = _replace_cmd_subst($class, $str);
774  
775 # backtick command substitution: `cmd`
776 1190 $str =~ s/`([^`]*)`/_protect_lit(_cmd_subst($class, $1))/ge;
777  
778 # ---- Array expansions (v0.06) ----------------------------------
779 # These MUST precede the scalar ${#VAR} / ${VAR} rules below so that a
780 # subscripted reference is never mis-parsed as a plain variable.
781  
782 # ${#NAME[@]} / ${#NAME[*]} -- number of set elements
783 1190 $str =~ s/\$\{#([A-Za-z_][A-Za-z0-9_]*)\[[\@*]\]\}/
784 10 _arr_count($1)
785 /ge;
786  
787 # ${#NAME[SUB]} -- length of one element
788 1190 $str =~ s/\$\{#([A-Za-z_][A-Za-z0-9_]*)\[([^\]]*)\]\}/
789 2 do {
790 2 my $v = _arr_get_element($1, _arr_expand_sub($class, $2));
791 2 ▶ defined $v ? length($v) : 0
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 2 0 2
792 }
793 /ge;
794  
795 # ${!NAME[@]} / ${!NAME[*]} -- list of indices / keys
796 1190 $str =~ s/\$\{!([A-Za-z_][A-Za-z0-9_]*)\[[\@*]\]\}/
797 5 _protect_lit(join(' ', _arr_ordered_keys($1)))
798 /ge;
799  
800 # ${NAME[@]} / ${NAME[*]} -- all elements (space-joined word-split model)
801 1190 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)\[[\@*]\]\}/
802 8 _protect_lit(join(' ', _arr_values($1)))
803 /ge;
804  
805 # ${NAME[SUB]} -- single element
806 1190 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)\[([^\]]*)\]\}/
807 20 do {
808 20 my $v = _arr_get_element($1, _arr_expand_sub($class, $2));
809 20 ▶ defined $v ? _protect_lit($v) : ''
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 20 0 20
810 }
811 /ge;
812 # ----------------------------------------------------------------
813  
814 # ${#VAR} -- length of value (characters, not bytes, under DBCS guard)
815 1190 $str =~ s/\$\{#([A-Za-z_][A-Za-z0-9_]*)\}/
816 4 do { my $v = BATsh::Env->get($1);
817 4 ▶ defined $v ? BATsh::MB::mb_length($v) : 0 }
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 4 0 4
818 /ge;
819  
820 # ${VAR%%pattern} -- remove longest suffix (MUST be before single %)
821 1190 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)%%([^}]*)\}/
822 1 ▶ do { my $v = BATsh::Env->get($1); $v = defined $v ? $v : ''; _protect_lit(_sh_remove_suffix($v, $2, 1)) }
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 1 0 1
823 /ge;
824  
825 # ${VAR%pattern} -- remove shortest suffix (single %, not %%)
826 1190 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)%(?!%)([^}]*)\}/
827 1 ▶ do { my $v = BATsh::Env->get($1); $v = defined $v ? $v : ''; _protect_lit(_sh_remove_suffix($v, $2, 0)) }
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 1 0 1
828 /ge;
829  
830 # ${VAR##pattern} -- remove longest prefix (MUST be before single #)
831 1190 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)##([^}]*)\}/
832 1 ▶ do { my $v = BATsh::Env->get($1); $v = defined $v ? $v : ''; _protect_lit(_sh_remove_prefix($v, $2, 1)) }
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 1 0 1
833 /ge;
834  
835 # ${VAR#pattern} -- remove shortest prefix (single #, not ##)
836 1190 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)#(?!#)([^}]*)\}/
837 1 ▶ do { my $v = BATsh::Env->get($1); $v = defined $v ? $v : ''; _protect_lit(_sh_remove_prefix($v, $2, 0)) }
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 1 0 1
838 /ge;
839  
840 # ${VAR//pat/rep} -- replace all occurrences (MUST be before single /)
841 1190 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)\/\/([^\/}]*)\/([^}]*)\}/
842 1 ▶ do { my $v = BATsh::Env->get($1); $v = defined $v ? $v : ''; _protect_lit(_sh_replace($v, $2, $3, 1)) }
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 1 0 1
843 /ge;
844  
845 # ${VAR/pat/rep} -- replace first occurrence (single /, not //)
846 1190 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)\/(?!\/)([^\/}]*)\/([^}]*)\}/
847 1 ▶ do { my $v = BATsh::Env->get($1); $v = defined $v ? $v : ''; _protect_lit(_sh_replace($v, $2, $3, 0)) }
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 1 0 1
848 /ge;
849  
850 # ${VAR^^} -- uppercase all
851 1190 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)\^\^\}/
852 2 ▶ do { my $v = BATsh::Env->get($1); defined $v ? _protect_lit(uc($v)) : '' }
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 2 0 2
853 /ge;
854  
855 # ${VAR^} -- uppercase first char
856 1190 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)\^\}/
857 0 ▶ do { my $v = BATsh::Env->get($1); $v = defined $v ? $v : ''; _protect_lit(ucfirst($v)) }
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 0 0 0
858 /ge;
859  
860 # ${VAR,,} -- lowercase all
861 1190 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*),,\}/
862 1 ▶ do { my $v = BATsh::Env->get($1); defined $v ? _protect_lit(lc($v)) : '' }
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 1 0 1
863 /ge;
864  
865 # ${VAR,} -- lowercase first char
866 1190 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*),\}/
867 0 ▶ do { my $v = BATsh::Env->get($1); $v = defined $v ? $v : ''; _protect_lit(lcfirst($v)) }
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 0 0 0
868 /ge;
869  
870 # ${VAR:offset:length} and ${VAR:offset}
871 1190 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*):(-?\d+):(\d+)\}/
872 2 do {
873 2 ▶ my $v = BATsh::Env->get($1); $v = defined $v ? $v : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 2 0 2
874 2 _protect_lit(BATsh::MB::mb_substr($v, int($2), int($3)))
875 }
876 /ge;
877 1190 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*):(-?\d+)\}/
878 0 do {
879 0 ▶ my $v = BATsh::Env->get($1); $v = defined $v ? $v : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 0 0 0
880 0 _protect_lit(BATsh::MB::mb_substr($v, int($2)))
881 }
882 /ge;
883  
884 # ${VAR:-default} ${VAR:=default} ${VAR:+alt}
885 1190 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*):-(.*?)\}/
886 3 ▶ do { my $v = BATsh::Env->get($1); (defined $v && $v ne '') ? _protect_lit($v) : $2 }
Branch: 1 branch
BranchTrueFalseTotal
defined $v && $v ne '' ? : 0 3 3
Condition33%1/3
defined $v and $v ne ""
!ll&&!rl&&r
300
MC/DC0%0/2
defined $v and $v ne ""
defined $v $v ne ""
Truth table33%1/3
A and B
A= defined $vB= $v ne ""
ABresult
0X0
100
111
887 /ge;
888 1190 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*):=(.*?)\}/
889 0 do {
890 0 my $v = BATsh::Env->get($1);
891 0 ▶ if (!defined $v || $v eq '') { BATsh::Env->set($1, $2); $v = $2 }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $v or $v eq '') { } 0 0 0
Condition0%0/3
not defined $v or $v eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $v or $v eq ""
not defined $v $v eq ""
Truth table0%0/3
not(A) or B
A= not defined $vB= $v eq ""
ABresult
1X1
011
000
892 0 else { $v = _protect_lit($v) }
893 0 $v
894 }
895 /ge;
896 1190 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*):\+([^}]*)\}/
897 0 ▶ do { my $v = BATsh::Env->get($1); (defined $v && $v ne '') ? $2 : '' }
Branch: 1 branch
BranchTrueFalseTotal
defined $v && $v ne '' ? : 0 0 0
Condition0%0/3
defined $v and $v ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $v and $v ne ""
defined $v $v ne ""
Truth table0%0/3
A and B
A= defined $vB= $v ne ""
ABresult
0X0
100
111
898 /ge;
899  
900 # ${VAR} -- plain expansion (array name yields element 0)
901 1190 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/
902 47 do {
903 47 my $n = $1;
904 47 ▶ if (_arr_exists($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_arr_exists($n)) { } 0 47 47
905 0 ▶ my $v = _arr_get_element($n, 0); defined $v ? _protect_lit($v) : ''
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 0 0 0
906 }
907 else {
908 47 my $v = BATsh::Env->get($n);
909 47 ▶ _nounset_hit($n) if !defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 47 47
910 47 ▶ defined $v ? _protect_lit($v) : ''
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 47 0 47
911 }
912 }
913 /ge;
914  
915 # $? last status
916 1190 $str =~ s/\$\?/$LAST_STATUS/g;
917  
918 # $$ PID
919 1190 $str =~ s/\$\$/$$/g;
920  
921 # $! PID of the most recent background job (empty before any)
922 1190 $str =~ s/\$\!/$_LAST_BG_PID/g;
923  
924  
925 # $0 script name
926 1190 ▶ $str =~ s/\$0/do { my $v=BATsh::Env->get('%0'); defined $v ? _protect_lit($v) : '' }/ge;
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 0 0 0
927  
928 # $1..$9 positional parameters
929 1190 $str =~ s/\$([1-9])/
930 28 do {
931 28 my $n = $1;
932 28 my $v = BATsh::Env->get("%$n");
933 28 ▶ $v = BATsh::Env->get("BATSH_ARG$n") unless defined $v && $v ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and $v ne "" 1 27 28
Condition66%2/3
defined $v and $v ne ""
!ll&&!rl&&r
0127
MC/DC50%1/2
defined $v and $v ne ""
defined $v $v ne ""
Truth table66%2/3
A and B
A= defined $vB= $v ne ""
ABresult
0X0
100
111
934 28 ▶ defined $v ? _protect_lit($v) : ''
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 28 0 28
935 }
936 /ge;
937  
938 # $@ and $* all positional parameters (both join with a space here).
939 # Historically only $@ was substituted; $* fell through unexpanded and
940 # printed literally. Both now expand to the space-joined parameter
941 # list held in %*.
942 1190 ▶ $str =~ s/\$\@/do { my $v=BATsh::Env->get('%*'); defined $v ? _protect_lit($v) : '' }/ge;
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 1 0 1
943 1190 ▶ $str =~ s/\$\*/do { my $v=BATsh::Env->get('%*'); defined $v ? _protect_lit($v) : '' }/ge;
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 3 0 3
944  
945 # $# number of positional parameters
946 1190 $str =~ s/\$#/
947 4 do {
948 4 my $c = 0;
949 4 for my $nn (1..9) {
950 11 my $vv = BATsh::Env->get("%$nn");
951 11 ▶ $vv = BATsh::Env->get("BATSH_ARG$nn") unless defined $vv && $vv ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $vv and $vv ne "" 4 7 11
Condition66%2/3
defined $vv and $vv ne ""
!ll&&!rl&&r
047
MC/DC50%1/2
defined $vv and $vv ne ""
defined $vv $vv ne ""
Truth table66%2/3
A and B
A= defined $vvB= $vv ne ""
ABresult
0X0
100
111
952 11 ▶ last unless defined $vv && $vv ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $vv and $vv ne "" 4 7 11
Condition66%2/3
defined $vv and $vv ne ""
!ll&&!rl&&r
047
MC/DC50%1/2
defined $vv and $vv ne ""
defined $vv $vv ne ""
Truth table66%2/3
A and B
A= defined $vvB= $vv ne ""
ABresult
0X0
100
111
953 7 $c = $nn;
954 }
955 $c
956 4 }
957 /ge;
958  
959 # $VAR (array name yields element 0)
960 1190 $str =~ s/\$([A-Za-z_][A-Za-z0-9_]*)/
961 252 do {
962 252 my $n = $1;
963 252 ▶ if (_arr_exists($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_arr_exists($n)) { } 1 251 252
964 1 ▶ my $v = _arr_get_element($n, 0); defined $v ? _protect_lit($v) : ''
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 1 0 1
965 }
966 else {
967 251 my $v = BATsh::Env->get($n);
968 251 ▶ _nounset_hit($n) if !defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 2 249 251
969 251 ▶ defined $v ? _protect_lit($v) : ''
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 249 2 251
970 }
971 }
972 /ge;
973  
974 # Restore the escaped specials as literal characters (reverse order of
975 # protection is not required, the sentinels are disjoint).
976 1190 $str =~ s/\x00BATSH_DL\x00/\$/g;
977 1190 $str =~ s/\x00BATSH_BT\x00/`/g;
978 1190 $str =~ s/\x00BATSH_BS\x00/\\/g;
979  
980 1190 return $str;
981 }
982  
983 # ----------------------------------------------------------------
984 # Arithmetic evaluator
985 # ----------------------------------------------------------------
986 36 ▶use vars qw(@_A_TOK $_A_POS $_A_ERR);
Subroutine BEGIN: called CC=1 SCAR=0.0
987  
988 sub _eval_arith {
989 110 ▶ my ($expr) = @_;
Subroutine _eval_arith: called CC=5 SCAR=16.3
990 # Full shell-arithmetic evaluator (bash semantics, Perl 5.005_03 safe):
991 # , = += -= *= /= %= <<= >>= &= ^= |= ?: || && | ^ & == != < <= > >=
992 # << >> + - * / % ** (right assoc) unary ! ~ + - prefix/postfix ++ --
993 # Comparisons and logical operators yield 1/0; / and % truncate toward
994 # zero; assignments and ++/-- on a bare NAME write back to the variable
995 # store. $N and $NAME are resolved to their numeric values while
996 # tokenizing; a bare NAME stays assignable. Numbers may be decimal,
997 # hex (0x..), or octal (0..). On any syntax error or division by zero
998 # a warning is printed and 0 is returned (matching the old behaviour
999 # of returning 0 on an unsupported expression).
1000 110 ▶ local @_A_TOK = _arith_tokenize(defined $expr ? $expr : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $expr ? : 110 0 110
1001 110 local $_A_POS = 0;
1002 110 local $_A_ERR = 0;
1003 110 my ($v) = _a_comma();
1004 110 ▶ $_A_ERR = 1 if !$_A_ERR && $_A_POS < scalar(@_A_TOK); # trailing junk
Branch: 1 branch
BranchTrueFalseTotal
if not $BATsh::SH::_A_ERR and $BATsh::SH::_A_POS < scalar @BATsh::SH::_A_TOK 0 110 110
Condition66%2/3
not $BATsh::SH::_A_ERR and $BATsh::SH::_A_POS < scalar @BATsh::SH::_A_TOK
!ll&&!rl&&r
21080
MC/DC0%0/2
not $BATsh::SH::_A_ERR and $BATsh::SH::_A_POS < scalar @BATsh::SH::_A_TOK
not $BATsh::SH::_A_ERR $BATsh::SH::_A_POS < scalar @BATsh::SH::_A_TOK
Truth table66%2/3
not(A) and B
A= not $BATsh::SH::_A_ERRB= $BATsh::SH::_A_POS < scalar @BATsh::SH::_A_TOK
ABresult
0X0
100
111
1005 110 ▶ if ($_A_ERR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BATsh::SH::_A_ERR) 2 108 110
1006 2 warn "sh: arithmetic: syntax error or division by zero: $expr\n";
1007 2 return 0;
1008 }
1009 108 return int($v);
1010 }
1011  
1012 # Tokenize into [TYPE, VALUE] pairs: TYPE 'n' number, 'v' name, 'o' operator.
1013 sub _arith_tokenize {
1014 110 ▶ my ($s) = @_;
Subroutine _arith_tokenize: called CC=16 SCAR=28.0
1015 110 my @tok = ();
1016 110 my $i = 0;
1017 110 my $n = length($s);
1018 110 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 496 109 605
1019 496 my $c = substr($s, $i, 1);
1020 496 ▶ if ($c =~ /\s/) { $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /\s/) 150 346 496
1021 # $N positional / $NAME -- resolve to a numeric value now
1022 346 ▶ if ($c eq '$') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\$") 5 341 346
1023 5 ▶ if (substr($s, $i+1, 1) =~ /\A[1-9]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($s, $i + 1, 1) =~ /\A[1-9]\z/) 5 0 5
1024 5 push @tok, ['n', _arith_pos(substr($s, $i+1, 1))];
1025 5 $i += 2; next;
1026 }
1027 0 ▶ if (substr($s, $i+1) =~ /\A([A-Za-z_][A-Za-z0-9_]*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($s, $i + 1) =~ /\A([A-Za-z_][A-Za-z0-9_]*)/) 0 0 0
1028 0 push @tok, ['n', _arith_var($1)];
1029 0 $i += 1 + length($1); next;
1030 }
1031 0 return (['o', '?ERR?']); # lone $: force a syntax error
1032 }
1033 # Numbers: hex 0x.., octal 0.., decimal
1034 341 ▶ if ($c =~ /\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /\d/) 138 203 341
1035 138 ▶ if ($c eq '0' && substr($s, $i+1, 1) =~ /\A[xX]\z/
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq 0 and substr($s, $i + 1, 1) =~ /\A[xX]\z/ and substr($s, $i + 2) =~ /\A([0-9A-Fa-f]+)/) 1 137 138
Condition83%5/6
$c eq 0 and substr($s, $i + 1, 1) =~ /\A[xX]\z/ and substr($s, $i + 2) =~ /\A([0-9A-Fa-f]+)/
!ll&&!rl&&r
13701
$c eq 0 and substr($s, $i + 1, 1) =~ /\A[xX]\z/
!ll&&!rl&&r
13071
MC/DC66%2/3
$c eq 0 and substr($s, $i + 1, 1) =~ /\A[xX]\z/ and substr($s, $i + 2) =~ /\A([0-9A-Fa-f]+)/
$c eq 0 substr($s, $i + 1, 1) =~ /\A[xX]\z/ substr($s, $i + 2) =~ /\A([0-9A-Fa-f]+)/
Truth table75%3/4
A and B and C
A= $c eq 0B= substr($s, $i + 1, 1) =~ /\A[xX]\z/C= substr($s, $i + 2) =~ /\A([0-9A-Fa-f]+)/
ABCresult
0XX0
10X0
1100
1111
1036 && substr($s, $i+2) =~ /\A([0-9A-Fa-f]+)/) {
1037 1 push @tok, ['n', hex($1)];
1038 1 $i += 2 + length($1); next;
1039 }
1040 137 substr($s, $i) =~ /\A(\d+)/;
1041 137 my $num = $1;
1042 137 ▶ if ($num =~ /\A0[0-7]+\z/) { push @tok, ['n', oct($num)] }
Branch: 1 branch
BranchTrueFalseTotal
if ($num =~ /\A0[0-7]+\z/) { } 1 136 137
1043 136 else { push @tok, ['n', int($num)] }
1044 137 $i += length($num); next;
1045 }
1046 # Bare NAME (assignable lvalue)
1047 203 ▶ if ($c =~ /[A-Za-z_]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /[A-Za-z_]/) 79 124 203
1048 79 substr($s, $i) =~ /\A([A-Za-z_][A-Za-z0-9_]*)/;
1049 79 push @tok, ['v', $1];
1050 79 $i += length($1); next;
1051 }
1052 # Operators: longest match first
1053 124 my $three = substr($s, $i, 3);
1054 124 ▶ if ($three eq '<<=' || $three eq '>>=') {
Branch: 1 branch
BranchTrueFalseTotal
if ($three eq "<<=" or $three eq ">>=") 1 123 124
Condition66%2/3
$three eq "<<=" or $three eq ">>="
l!l&&r!l&&!r
10123
MC/DC50%1/2
$three eq "<<=" or $three eq ">>="
$three eq "<<=" $three eq ">>="
Truth table66%2/3
A or B
A= $three eq "<<="B= $three eq ">>="
ABresult
1X1
011
000
1055 1 push @tok, ['o', $three]; $i += 3; next;
1056 }
1057 123 my $two = substr($s, $i, 2);
1058 123 ▶ if ($two =~ /\A(?:\*\*|<<|>>|<=|>=|==|!=|&&|\|\||\+=|-=|\*=|\/=|%=|&=|\^=|\|=|\+\+|--)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($two =~ m[\A(?:\*\*|<<|>>|<=|>=|==|!=|&&|\|\||\+=|-=|\*=|/=|%=|&=|\^=|\|=|\+\+|--)\z]) 16 107 123
1059 16 push @tok, ['o', $two]; $i += 2; next;
1060 }
1061 107 ▶ if ($c =~ /\A[-+*\/%()<>=!&|^~?:,]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ m[\A[-+*/%()<>=!&|^~?:,]\z]) 106 1 107
1062 106 push @tok, ['o', $c]; $i++; next;
1063 }
1064 1 return (['o', '?ERR?']); # unknown character: force a syntax error
1065 }
1066 109 return @tok;
1067 }
1068  
1069 4694 ▶sub _a_peek { return $_A_POS < @_A_TOK ? $_A_TOK[$_A_POS] : undef }
Subroutine _a_peek: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$BATsh::SH::_A_POS < @BATsh::SH::_A_TOK ? : 2408 2286 4694
1070 sub _a_isop {
1071 4358 ▶ my ($op) = @_;
Subroutine _a_isop: called CC=3 SCAR=11.0
1072 4358 my $t = _a_peek();
1073 4358 ▶ return defined $t && $t->[0] eq 'o' && $t->[1] eq $op;
Condition100%6/6
defined $t && $t->[0] eq 'o' && $t->[1] eq $op
!ll&&!rl&&r
3534713111
defined $t and $t->[0] eq "o"
!ll&&!rl&&r
22861248824
MC/DC100%3/3
defined $t && $t->[0] eq 'o' && $t->[1] eq $op
defined $t $t->[0] eq "o" $t->[1] eq $op
Truth table100%4/4
A and B && C
A= defined $tB= $t->[0] eq "o"C= $t->[1] eq $op
ABCresult
0XX0
10X0
1100
1111
1074 }
1075 344 ▶sub _a_next { return $_A_TOK[$_A_POS++] }
Subroutine _a_next: called CC=1 SCAR=0.0
1076  
1077 # Each _a_* returns (value, lvalue_name_or_undef).
1078  
1079 sub _a_comma {
1080 111 ▶ my ($v, $lv) = _a_assign();
Subroutine _a_comma: called CC=2 SCAR=6.9
1081 111 ▶ while (_a_isop(',')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop(",")) 1 111 112
1082 1 _a_next();
1083 1 ($v, $lv) = _a_assign();
1084 }
1085 111 return ($v, undef);
1086 }
1087  
1088 sub _a_assign {
1089 # Lookahead: NAME followed by an assignment operator
1090 124 ▶ my $t = _a_peek();
Subroutine _a_assign: called CC=18 SCAR=38.6
1091 124 ▶ if (defined $t && $t->[0] eq 'v' && $_A_POS + 1 < @_A_TOK) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t and $t->[0] eq "v" and $BATsh::SH::_A_POS + 1 < @BATsh::SH::_A_TOK) 77 47 124
Condition66%4/6
defined $t and $t->[0] eq "v" and $BATsh::SH::_A_POS + 1 < @BATsh::SH::_A_TOK
!ll&&!rl&&r
47077
defined $t and $t->[0] eq "v"
!ll&&!rl&&r
04777
MC/DC33%1/3
defined $t and $t->[0] eq "v" and $BATsh::SH::_A_POS + 1 < @BATsh::SH::_A_TOK
defined $t $t->[0] eq "v" $BATsh::SH::_A_POS + 1 < @BATsh::SH::_A_TOK
Truth table50%2/4
A and B and C
A= defined $tB= $t->[0] eq "v"C= $BATsh::SH::_A_POS + 1 < @BATsh::SH::_A_TOK
ABCresult
0XX0
10X0
1100
1111
1092 77 my $op = $_A_TOK[$_A_POS + 1];
1093 77 ▶ if ($op->[0] eq 'o' && $op->[1] =~ /\A(?:=|\+=|-=|\*=|\/=|%=|<<=|>>=|&=|\^=|\|=)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->[0] eq "o" and $op->[1] =~ m[\A(?:=|\+=|-=|\*=|/=|%=|<<=|>>=|&=|\^=|\|=)\z]) 11 66 77
Condition66%2/3
$op->[0] eq "o" and $op->[1] =~ m[\A(?:=|\+=|-=|\*=|/=|%=|<<=|>>=|&=|\^=|\|=)\z]
!ll&&!rl&&r
06611
MC/DC50%1/2
$op->[0] eq "o" and $op->[1] =~ m[\A(?:=|\+=|-=|\*=|/=|%=|<<=|>>=|&=|\^=|\|=)\z]
$op->[0] eq "o" $op->[1] =~ m[\A(?:=|\+=|-=|\*=|/=|%=|<<=|>>=|&=|\^=|\|=)\z]
Truth table66%2/3
A and B
A= $op->[0] eq "o"B= $op->[1] =~ m[\A(?:=|\+=|-=|\*=|/=|%=|<<=|>>=|&=|\^=|\|=)\z]
ABresult
0X0
100
111
1094 11 my $name = $t->[1];
1095 11 _a_next(); _a_next();
1096 11 my ($rhs) = _a_assign();
1097 11 my $val;
1098 11 ▶ if ($op->[1] eq '=') { $val = $rhs }
Branch: 1 branch
BranchTrueFalseTotal
if ($op->[1] eq '=') { } 8 3 11
1099 else {
1100 3 my $cur = _arith_var($name);
1101 3 my $o = $op->[1];
1102 3 ▶ if ($o eq '+=') { $val = $cur + $rhs }
Branch: 9 branches
BranchTrueFalseTotal
if ($o eq '+=') { } 2 1 3
elsif ($o eq '-=') { } 0 1 1
elsif ($o eq '*=') { } 0 1 1
elsif ($o eq '/=') { } 0 1 1
elsif ($o eq '%=') { } 0 1 1
elsif ($o eq '<<=') { } 1 0 1
elsif ($o eq '>>=') { } 0 0 0
elsif ($o eq '&=') { } 0 0 0
elsif ($o eq '^=') { } 0 0 0
1103 0 elsif ($o eq '-=') { $val = $cur - $rhs }
1104 0 elsif ($o eq '*=') { $val = $cur * $rhs }
1105 0 ▶ elsif ($o eq '/=') { if ($rhs == 0) { $_A_ERR = 1; return (0, undef) }
Branch: 1 branch
BranchTrueFalseTotal
if ($rhs == 0) 0 0 0
1106 0 $val = int($cur / $rhs) }
1107 0 ▶ elsif ($o eq '%=') { if ($rhs == 0) { $_A_ERR = 1; return (0, undef) }
Branch: 1 branch
BranchTrueFalseTotal
if ($rhs == 0) 0 0 0
1108 0 $val = $cur % $rhs }
1109 1 elsif ($o eq '<<=') { $val = $cur << $rhs }
1110 0 elsif ($o eq '>>=') { $val = $cur >> $rhs }
1111 0 elsif ($o eq '&=') { $val = $cur & $rhs }
1112 0 elsif ($o eq '^=') { $val = $cur ^ $rhs }
1113 0 else { $val = $cur | $rhs }
1114 }
1115 11 $val = int($val);
1116 11 BATsh::Env->set($name, $val);
1117 11 return ($val, undef);
1118 }
1119 }
1120 113 return _a_ternary();
1121 }
1122  
1123 sub _a_ternary {
1124 114 ▶ my ($c) = _a_lor();
Subroutine _a_ternary: called CC=4 SCAR=14.4
1125 114 ▶ if (_a_isop('?')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("?")) 1 113 114
1126 1 _a_next();
1127 1 my ($tv) = _a_assign();
1128 1 ▶ if (!_a_isop(':')) { $_A_ERR = 1; return (0, undef) }
Branch: 1 branch
BranchTrueFalseTotal
unless (_a_isop(":")) 0 1 1
1129 1 _a_next();
1130 1 my ($fv) = _a_ternary();
1131 1 ▶ return (($c != 0) ? $tv : $fv, undef);
Branch: 1 branch
BranchTrueFalseTotal
$c != 0 ? : 1 0 1
1132 }
1133 113 return ($c, undef);
1134 }
1135  
1136 sub _a_lor {
1137 114 ▶ my ($v) = _a_land();
Subroutine _a_lor: called CC=4 SCAR=14.3
1138 114 ▶ while (_a_isop('||')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("||")) 1 114 115
1139 1 _a_next();
1140 1 my ($r) = _a_land();
1141 1 ▶ $v = (($v != 0) || ($r != 0)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$v != 0 || $r != 0 ? : 0 1 1
Condition33%1/3
$v != 0 or $r != 0
l!l&&r!l&&!r
001
MC/DC0%0/2
$v != 0 or $r != 0
$v != 0 $r != 0
Truth table33%1/3
A or B
A= $v != 0B= $r != 0
ABresult
1X1
011
000
1142 }
1143 114 return ($v, undef);
1144 }
1145  
1146 sub _a_land {
1147 115 ▶ my ($v) = _a_bor();
Subroutine _a_land: called CC=4 SCAR=14.3
1148 115 ▶ while (_a_isop('&&')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("&&")) 1 115 116
1149 1 _a_next();
1150 1 my ($r) = _a_bor();
1151 1 ▶ $v = (($v != 0) && ($r != 0)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$v != 0 && $r != 0 ? : 1 0 1
Condition33%1/3
$v != 0 and $r != 0
!ll&&!rl&&r
001
MC/DC0%0/2
$v != 0 and $r != 0
$v != 0 $r != 0
Truth table33%1/3
A and B
A= $v != 0B= $r != 0
ABresult
0X0
100
111
1152 }
1153 115 return ($v, undef);
1154 }
1155  
1156 sub _a_bor {
1157 116 ▶ my ($v) = _a_bxor();
Subroutine _a_bor: called CC=2 SCAR=6.9
1158 116 ▶ while (_a_isop('|')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("|")) 1 116 117
1159 1 _a_next();
1160 1 my ($r) = _a_bxor();
1161 1 $v = $v | $r;
1162 }
1163 116 return ($v, undef);
1164 }
1165  
1166 sub _a_bxor {
1167 117 ▶ my ($v) = _a_band();
Subroutine _a_bxor: called CC=2 SCAR=6.9
1168 117 ▶ while (_a_isop('^')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("^")) 1 117 118
1169 1 _a_next();
1170 1 my ($r) = _a_band();
1171 1 $v = $v ^ $r;
1172 }
1173 117 return ($v, undef);
1174 }
1175  
1176 sub _a_band {
1177 118 ▶ my ($v) = _a_eqne();
Subroutine _a_band: called CC=2 SCAR=6.9
1178 118 ▶ while (_a_isop('&')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("&")) 1 118 119
1179 1 _a_next();
1180 1 my ($r) = _a_eqne();
1181 1 $v = $v & $r;
1182 }
1183 118 return ($v, undef);
1184 }
1185  
1186 sub _a_eqne {
1187 119 ▶ my ($v) = _a_rel();
Subroutine _a_eqne: called CC=6 SCAR=19.3
1188 119 ▶ while (_a_isop('==') || _a_isop('!=')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("==") or _a_isop("!=")) 1 119 120
Condition66%2/3
_a_isop("==") or _a_isop("!=")
l!l&&r!l&&!r
01119
MC/DC50%1/2
_a_isop("==") or _a_isop("!=")
_a_isop("==") _a_isop("!=")
Truth table66%2/3
A or B
A= _a_isop("==")B= _a_isop("!=")
ABresult
1X1
011
000
1189 1 my $op = _a_next()->[1];
1190 1 my ($r) = _a_rel();
1191 1 ▶ $v = ($op eq '==') ? (($v == $r) ? 1 : 0) : (($v != $r) ? 1 : 0);
Branch: 3 branches
BranchTrueFalseTotal
$op eq '==' ? : 0 1 1
$v == $r ? : 0 0 0
$v != $r ? : 0 1 1
1192 }
1193 119 return ($v, undef);
1194 }
1195  
1196 sub _a_rel {
1197 120 ▶ my ($v) = _a_shiftop();
Subroutine _a_rel: called CC=12 SCAR=25.8
1198 120 ▶ while (_a_isop('<') || _a_isop('<=') || _a_isop('>') || _a_isop('>=')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("<") or _a_isop("<=") or _a_isop(">") or _a_isop(">=")) 3 120 123
Condition88%8/9
_a_isop("<") or _a_isop("<=") or _a_isop(">") or _a_isop(">=")
l!l&&r!l&&!r
41120
_a_isop("<") or _a_isop("<=") or _a_isop(">")
l!l&&r!l&&!r
22121
_a_isop("<") or _a_isop("<=")
l!l&&r!l&&!r
20123
MC/DC75%3/4
_a_isop("<") or _a_isop("<=") or _a_isop(">") or _a_isop(">=")
_a_isop("<") _a_isop("<=") _a_isop(">") _a_isop(">=")
Truth table80%4/5
A or B or C or D
A= _a_isop("<")B= _a_isop("<=")C= _a_isop(">")D= _a_isop(">=")
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1199 5 my $op = _a_next()->[1];
1200 5 my ($r) = _a_shiftop();
1201 5 ▶ if ($op eq '<') { $v = ($v < $r) ? 1 : 0 }
Branch: 4 branches
BranchTrueFalseTotal
if ($op eq '<') { } 2 3 5
elsif ($op eq '<=') { } 0 3 3
elsif ($op eq '>') { } 2 1 3
$v < $r ? : 1 1 2
1202 0 ▶ elsif ($op eq '<=') { $v = ($v <= $r) ? 1 : 0 }
Branch: 1 branch
BranchTrueFalseTotal
$v <= $r ? : 0 0 0
1203 2 ▶ elsif ($op eq '>') { $v = ($v > $r) ? 1 : 0 }
Branch: 1 branch
BranchTrueFalseTotal
$v > $r ? : 2 0 2
1204 1 ▶ else { $v = ($v >= $r) ? 1 : 0 }
Branch: 1 branch
BranchTrueFalseTotal
$v >= $r ? : 1 0 1
1205 }
1206 120 return ($v, undef);
1207 }
1208  
1209 sub _a_shiftop {
1210 125 ▶ my ($v) = _a_add();
Subroutine _a_shiftop: called CC=4 SCAR=13.9
1211 125 ▶ while (_a_isop('<<') || _a_isop('>>')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("<<") or _a_isop(">>")) 2 125 127
Condition100%3/3
_a_isop("<<") or _a_isop(">>")
l!l&&r!l&&!r
11125
MC/DC100%2/2
_a_isop("<<") or _a_isop(">>")
_a_isop("<<") _a_isop(">>")
Truth table100%3/3
A or B
A= _a_isop("<<")B= _a_isop(">>")
ABresult
1X1
011
000
1212 2 my $op = _a_next()->[1];
1213 2 my ($r) = _a_add();
1214 2 ▶ $v = ($op eq '<<') ? ($v << $r) : ($v >> $r);
Branch: 1 branch
BranchTrueFalseTotal
$op eq '<<' ? : 1 1 2
1215 }
1216 125 return ($v, undef);
1217 }
1218  
1219 sub _a_add {
1220 127 ▶ my ($v) = _a_mul();
Subroutine _a_add: called CC=4 SCAR=13.9
1221 127 ▶ while (_a_isop('+') || _a_isop('-')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("+") or _a_isop("-")) 72 127 199
Condition100%3/3
_a_isop("+") or _a_isop("-")
l!l&&r!l&&!r
702127
MC/DC100%2/2
_a_isop("+") or _a_isop("-")
_a_isop("+") _a_isop("-")
Truth table100%3/3
A or B
A= _a_isop("+")B= _a_isop("-")
ABresult
1X1
011
000
1222 72 my $op = _a_next()->[1];
1223 72 my ($r) = _a_mul();
1224 72 ▶ $v = ($op eq '+') ? ($v + $r) : ($v - $r);
Branch: 1 branch
BranchTrueFalseTotal
$op eq '+' ? : 70 2 72
1225 }
1226 127 return ($v, undef);
1227 }
1228  
1229 sub _a_mul {
1230 199 ▶ my ($v) = _a_pow();
Subroutine _a_mul: called CC=10 SCAR=23.1
1231 199 ▶ while (_a_isop('*') || _a_isop('/') || _a_isop('%')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("*") or _a_isop("/") or _a_isop("%")) 3 198 201
Condition100%6/6
_a_isop("*") or _a_isop("/") or _a_isop("%")
l!l&&r!l&&!r
91198
_a_isop("*") or _a_isop("/")
l!l&&r!l&&!r
72199
MC/DC100%3/3
_a_isop("*") or _a_isop("/") or _a_isop("%")
_a_isop("*") _a_isop("/") _a_isop("%")
Truth table100%4/4
A or B or C
A= _a_isop("*")B= _a_isop("/")C= _a_isop("%")
ABCresult
1XX1
01X1
0011
0000
1232 10 my $op = _a_next()->[1];
1233 10 my ($r) = _a_pow();
1234 10 ▶ if ($op eq '*') { $v = $v * $r }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq '*') { } 7 3 10
1235 else {
1236 3 ▶ if ($r == 0) { $_A_ERR = 1; return (0, undef) }
Branch: 1 branch
BranchTrueFalseTotal
if ($r == 0) 1 2 3
1237 # bash / and % truncate toward zero
1238 2 ▶ if ($op eq '/') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq '/') { } 1 1 2
1239 1 my $q = abs(int($v)) - (abs(int($v)) % abs(int($r)));
1240 1 $q /= abs(int($r));
1241 1 ▶ $q = -$q if (($v < 0) ne ($r < 0)) && $q != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $v < 0 ne $r < 0 and $q != 0 1 0 1
Condition33%1/3
$v < 0 ne $r < 0 and $q != 0
!ll&&!rl&&r
001
MC/DC0%0/2
$v < 0 ne $r < 0 and $q != 0
$v < 0 ne $r < 0 $q != 0
Truth table33%1/3
A and B
A= $v < 0 ne $r < 0B= $q != 0
ABresult
0X0
100
111
1242 1 $v = $q;
1243 }
1244 else {
1245 1 my $m = abs(int($v)) % abs(int($r));
1246 1 ▶ $m = -$m if $v < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $v < 0 1 0 1
1247 1 $v = $m;
1248 }
1249 }
1250 }
1251 198 return ($v, undef);
1252 }
1253  
1254 sub _a_pow {
1255 212 ▶ my ($v, $lv) = _a_unary();
Subroutine _a_pow: called CC=2 SCAR=6.9
1256 212 ▶ if (_a_isop('**')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("**")) 3 209 212
1257 3 _a_next();
1258 3 my ($r) = _a_pow(); # right associative
1259 3 return (int($v ** $r), undef);
1260 }
1261 209 return ($v, $lv);
1262 }
1263  
1264 sub _a_unary {
1265 216 ▶ if (_a_isop('!')) { _a_next(); my ($v) = _a_unary(); return (($v == 0) ? 1 : 0, undef) }
Subroutine _a_unary: called CC=11 SCAR=24.9
Branch: 2 branches
BranchTrueFalseTotal
if (_a_isop("!")) 1 215 216
$v == 0 ? : 1 0 1
1266 215 ▶ if (_a_isop('~')) { _a_next(); my ($v) = _a_unary(); return (-int($v) - 1, undef) }
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("~")) 1 214 215
1267 214 ▶ if (_a_isop('+')) { _a_next(); my ($v) = _a_unary(); return (+$v, undef) }
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("+")) 0 214 214
1268 214 ▶ if (_a_isop('-')) { _a_next(); my ($v) = _a_unary(); return (-$v, undef) }
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("-")) 2 212 214
1269 # Prefix ++NAME / --NAME
1270 212 ▶ if (_a_isop('++') || _a_isop('--')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("++") or _a_isop("--")) 2 210 212
Condition100%3/3
_a_isop("++") or _a_isop("--")
l!l&&r!l&&!r
11210
MC/DC100%2/2
_a_isop("++") or _a_isop("--")
_a_isop("++") _a_isop("--")
Truth table100%3/3
A or B
A= _a_isop("++")B= _a_isop("--")
ABresult
1X1
011
000
1271 2 my $op = _a_next()->[1];
1272 2 my $t = _a_peek();
1273 2 ▶ if (!defined $t || $t->[0] ne 'v') { $_A_ERR = 1; return (0, undef) }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $t or $t->[0] ne "v") 0 2 2
Condition33%1/3
not defined $t or $t->[0] ne "v"
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $t or $t->[0] ne "v"
not defined $t $t->[0] ne "v"
Truth table33%1/3
not(A) or B
A= not defined $tB= $t->[0] ne "v"
ABresult
1X1
011
000
1274 2 _a_next();
1275 2 ▶ my $val = _arith_var($t->[1]) + (($op eq '++') ? 1 : -1);
Branch: 1 branch
BranchTrueFalseTotal
$op eq '++' ? : 1 1 2
1276 2 BATsh::Env->set($t->[1], $val);
1277 2 return ($val, undef);
1278 }
1279 210 return _a_postfix();
1280 }
1281  
1282 sub _a_postfix {
1283 210 ▶ my ($v, $lv) = _a_primary();
Subroutine _a_postfix: called CC=5 SCAR=16.1
1284 210 ▶ if (defined $lv && (_a_isop('++') || _a_isop('--'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lv and _a_isop("++") || _a_isop("--")) 3 207 210
Condition100%6/6
defined $lv and _a_isop("++") || _a_isop("--")
!ll&&!rl&&r
144642
_a_isop("++") or _a_isop("--")
l!l&&r!l&&!r
2163
MC/DC66%2/3
defined $lv and _a_isop("++") || _a_isop("--")
defined $lv _a_isop("++") _a_isop("--")
Truth table75%3/4
A and B or C
A= defined $lvB= _a_isop("++")C= _a_isop("--")
ABCresult
0XX0
1000
11X1
1011
1285 3 my $op = _a_next()->[1];
1286 3 ▶ BATsh::Env->set($lv, $v + (($op eq '++') ? 1 : -1));
Branch: 1 branch
BranchTrueFalseTotal
$op eq '++' ? : 2 1 3
1287 3 return ($v, undef); # postfix returns the OLD value
1288 }
1289 207 return ($v, $lv);
1290 }
1291  
1292 sub _a_primary {
1293 210 ▶ my $t = _a_peek();
Subroutine _a_primary: called CC=7 SCAR=19.9
1294 210 ▶ if (!defined $t) { $_A_ERR = 1; return (0, undef) }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $t) 0 210 210
1295 210 ▶ if ($t->[0] eq 'o' && $t->[1] eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->[0] eq "o" and $t->[1] eq "(") 1 209 210
Condition100%3/3
$t->[0] eq "o" and $t->[1] eq "("
!ll&&!rl&&r
20811
MC/DC100%2/2
$t->[0] eq "o" and $t->[1] eq "("
$t->[0] eq "o" $t->[1] eq "("
Truth table100%3/3
A and B
A= $t->[0] eq "o"B= $t->[1] eq "("
ABresult
0X0
100
111
1296 1 _a_next();
1297 1 my ($v) = _a_comma();
1298 1 ▶ if (!_a_isop(')')) { $_A_ERR = 1; return (0, undef) }
Branch: 1 branch
BranchTrueFalseTotal
unless (_a_isop(")")) 0 1 1
1299 1 _a_next();
1300 1 return ($v, undef);
1301 }
1302 209 ▶ if ($t->[0] eq 'n') { _a_next(); return ($t->[1], undef) }
Branch: 1 branch
BranchTrueFalseTotal
if ($t->[0] eq "n") 142 67 209
1303 67 ▶ if ($t->[0] eq 'v') { _a_next(); return (_arith_var($t->[1]), $t->[1]) }
Branch: 1 branch
BranchTrueFalseTotal
if ($t->[0] eq "v") 66 1 67
1304 1 $_A_ERR = 1;
1305 1 return (0, undef);
1306 }
1307  
1308 sub _arith_pos {
1309 5 ▶ my ($n) = @_;
Subroutine _arith_pos: called CC=5 SCAR=19.4
1310 5 my $v = BATsh::Env->get("%$n");
1311 5 ▶ $v = BATsh::Env->get("BATSH_ARG$n") unless defined $v && $v ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and $v ne "" 0 5 5
Condition33%1/3
defined $v and $v ne ""
!ll&&!rl&&r
005
MC/DC0%0/2
defined $v and $v ne ""
defined $v $v ne ""
Truth table33%1/3
A and B
A= defined $vB= $v ne ""
ABresult
0X0
100
111
1312 5 ▶ return (defined $v && $v =~ /\A-?\d+\z/) ? $v : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $v && $v =~ /\A-?\d+\z/ ? : 5 0 5
Condition33%1/3
defined $v and $v =~ /\A-?\d+\z/
!ll&&!rl&&r
005
MC/DC0%0/2
defined $v and $v =~ /\A-?\d+\z/
defined $v $v =~ /\A-?\d+\z/
Truth table33%1/3
A and B
A= defined $vB= $v =~ /\A-?\d+\z/
ABresult
0X0
100
111
1313 }
1314  
1315 sub _arith_var {
1316 71 ▶ my ($name) = @_;
Subroutine _arith_var: called CC=3 SCAR=12.5
1317 71 my $v = BATsh::Env->get($name);
1318 71 ▶ return (defined $v && $v =~ /\A-?\d+\z/) ? $v : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $v && $v =~ /\A-?\d+\z/ ? : 71 0 71
Condition33%1/3
defined $v and $v =~ /\A-?\d+\z/
!ll&&!rl&&r
0071
MC/DC0%0/2
defined $v and $v =~ /\A-?\d+\z/
defined $v $v =~ /\A-?\d+\z/
Truth table33%1/3
A and B
A= defined $vB= $v =~ /\A-?\d+\z/
ABresult
0X0
100
111
1319 }
1320  
1321 # ----------------------------------------------------------------
1322 # Command substitution $( cmd )
1323 # ----------------------------------------------------------------
1324 # _replace_cmd_subst: replace all $(...) in $str with their output.
1325 # Unlike a simple [^)]* regex, this function tracks nesting depth
1326 # and quoted strings so that $(cmd | perl -e "...)" works correctly.
1327 # ----------------------------------------------------------------
1328 sub _replace_cmd_subst {
1329 1190 ▶ my ($class, $str) = @_;
Subroutine _replace_cmd_subst: called CC=22 SCAR=32.5
1330 1190 ▶ return '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 1190 1190
1331  
1332 1190 my $result = '';
1333 1190 my @chars = split //, $str;
1334 1190 my $n = scalar @chars;
1335 1190 my $i = 0;
1336  
1337 1190 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 13392 1190 14582
1338 13392 my $ch = $chars[$i];
1339  
1340 # $( ... ) -- find matching close paren respecting nesting and quotes
1341 13392 ▶ if ($ch eq '$' && $i+1 < $n && $chars[$i+1] eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "(") 20 13372 13392
Condition83%5/6
$ch eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "("
!ll&&!rl&&r
1295541720
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
129550437
MC/DC66%2/3
$ch eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "("
$ch eq "\$" $i + 1 < $n $chars[$i + 1] eq "("
Truth table75%3/4
A and B and C
A= $ch eq "\$"B= $i + 1 < $nC= $chars[$i + 1] eq "("
ABCresult
0XX0
10X0
1100
1111
1342 20 $i += 2; # skip $(
1343 20 my $depth = 1;
1344 20 my $body = '';
1345 20 my $in_sq = 0;
1346 20 my $in_dq = 0;
1347  
1348 20 ▶ while ($i < $n && $depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n and $depth > 0) 459 0 459
Condition33%1/3
$i < $n and $depth > 0
!ll&&!rl&&r
00459
MC/DC0%0/2
$i < $n and $depth > 0
$i < $n $depth > 0
Truth table33%1/3
A and B
A= $i < $nB= $depth > 0
ABresult
0X0
100
111
1349 459 my $c = $chars[$i];
1350  
1351 459 ▶ if ($in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq) 4 455 459
1352 4 ▶ if ($c eq "'") { $in_sq = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "'") 1 3 4
1353 4 $body .= $c; $i++; next;
1354 }
1355 455 ▶ if ($c eq "'" && !$in_dq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "'" and not $in_dq) 1 454 455
Condition66%2/3
$c eq "'" and not $in_dq
!ll&&!rl&&r
45401
MC/DC50%1/2
$c eq "'" and not $in_dq
$c eq "'" not $in_dq
Truth table66%2/3
A and not(B)
A= $c eq "'"B= not $in_dq
ABresult
0X0
100
111
1356 1 $in_sq = 1; $body .= $c; $i++; next;
1357 }
1358 454 ▶ if ($c eq '"' && !$in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\"" and not $in_sq) 24 430 454
Condition66%2/3
$c eq "\"" and not $in_sq
!ll&&!rl&&r
430024
MC/DC50%1/2
$c eq "\"" and not $in_sq
$c eq "\"" not $in_sq
Truth table66%2/3
A and not(B)
A= $c eq "\""B= not $in_sq
ABresult
0X0
100
111
1359 24 $in_dq = !$in_dq; $body .= $c; $i++; next;
1360 }
1361 430 ▶ if ($in_dq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_dq) 96 334 430
1362 96 ▶ if ($c eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\\") 0 96 96
1363 0 $body .= $c; $i++;
1364 0 ▶ $body .= $chars[$i] if $i < $n; $i++; next;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $n 0 0 0
1365 }
1366 96 $body .= $c; $i++; next;
1367 }
1368 334 ▶ if ($c eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\\") 0 334 334
1369 0 $body .= $c; $i++;
1370 0 ▶ $body .= $chars[$i] if $i < $n; $i++; next;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $n 0 0 0
1371 }
1372 334 ▶ if ($c eq '(') { $depth++; $body .= $c; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "(") 6 328 334
1373 328 ▶ if ($c eq ')') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq ")") 26 302 328
1374 26 $depth--;
1375 26 ▶ if ($depth == 0) { $i++; last } # closing )
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 0) 20 6 26
1376 6 $body .= $c; $i++; next;
1377 }
1378 302 $body .= $c; $i++;
1379 }
1380  
1381 20 $result .= _protect_lit(_cmd_subst($class, $body));
1382 20 next;
1383 }
1384  
1385 13372 $result .= $ch; $i++;
1386 }
1387  
1388 1190 return $result;
1389 }
1390  
1391 # ----------------------------------------------------------------
1392 sub _cmd_subst {
1393 21 ▶ my ($class, $cmd_str) = @_;
Subroutine _cmd_subst: called CC=6 SCAR=19.4
1394 # Capture stdout via temporary file (Perl 5.005_03 compatible).
1395 # We use _run_lines so that all BATsh::SH builtins, functions,
1396 # and pipelines work recursively inside $(...) and `...`.
1397 # Tag the capture file with the active nesting depth so a nested
1398 # $( ... $( ... ) ) gets a distinct file per level (the inner level
1399 # must not truncate/unlink the file the outer level captures into).
1400 21 local $_SUBST_DEPTH = $_SUBST_DEPTH + 1;
1401 21 local *_SUBST_SAVOUT;
1402 21 ▶ open(_SUBST_SAVOUT, '>&STDOUT') or return '';
Branch: 1 branch
BranchTrueFalseTotal
open _SUBST_SAVOUT, ">&STDOUT" or return "" 0 21 21
1403 21 local *_SUBST_CAPFH;
1404 21 my $tmpfile = _subst_tempfile();
1405 21 ▶ if (!defined $tmpfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tmpfile) 0 21 21
1406 0 open(STDOUT, '>&_SUBST_SAVOUT'); close(_SUBST_SAVOUT); return '';
1407 }
1408 open(STDOUT, '>&_SUBST_CAPFH')
1409 21 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&_SUBST_CAPFH" or do { close _SUBST_CAPFH; unlink $tmpfile; open STDOUT, ">&_SUBST_SAVOUT"; close _SUBST_SAVOUT; return "" } 0 21 21
1410 0 close(_SUBST_CAPFH); unlink $tmpfile;
1411 0 open(STDOUT, '>&_SUBST_SAVOUT'); close(_SUBST_SAVOUT); return '';
1412 };
1413 21 close(_SUBST_CAPFH);
1414 21 eval {
1415 # Use _run_lines for full recursive BATsh::SH execution.
1416 # $cmd_str may contain pipes, builtins, functions, etc.
1417 21 my @sub_lines = split /\n/, $cmd_str;
1418 21 _run_lines($class, \@sub_lines, {});
1419 };
1420 21 open(STDOUT, '>&_SUBST_SAVOUT');
1421 21 close(_SUBST_SAVOUT);
1422 21 my $output = '';
1423 21 local *_SUBST_READFH;
1424 21 ▶ if (open(_SUBST_READFH, "< $tmpfile")) {
Branch: 1 branch
BranchTrueFalseTotal
if (open _SUBST_READFH, "< $tmpfile") 21 0 21
1425 21 local $/;
1426 21 $output = <_SUBST_READFH>;
1427 21 close(_SUBST_READFH);
1428 }
1429 21 unlink $tmpfile;
1430 21 @_SUBST_TMPFILES = grep { $_ ne $tmpfile } @_SUBST_TMPFILES;
1431 21 ▶ $output = '' unless defined $output;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $output 0 21 21
1432 21 $output =~ s/\n+\z//; # strip trailing newlines (like shell)
1433 21 return BATsh::MB::enc($output);
1434 }
1435  
1436 # _subst_tempfile: create a unique, empty temp file (O_CREAT|O_EXCL to
1437 # avoid symlink races -- mirrors _bg_tempfile / _hd_tempfile) for capturing
1438 # one command-substitution's stdout output, and open it as the package
1439 # bareword filehandle _SUBST_CAPFH. Returns the path, or undef on failure.
1440 sub _subst_tempfile {
1441 22 ▶ my $dir = File::Spec->tmpdir();
Subroutine _subst_tempfile: called CC=5 SCAR=16.8
1442 22 ▶ $dir = '.' if !(-d $dir && -w $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir and -w $dir 0 22 22
Condition33%1/3
-d $dir and -w $dir
!ll&&!rl&&r
0022
MC/DC0%0/2
-d $dir and -w $dir
-d $dir -w $dir
Truth table33%1/3
A and B
A= -d $dirB= -w $dir
ABresult
0X0
100
111
1443  
1444 22 my $attempt = 0;
1445 22 ▶ while ($attempt < 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attempt < 1000) 23 0 23
1446 23 $_SUBST_SEQ++;
1447 23 $attempt++;
1448 23 my $path = File::Spec->catfile($dir,
1449 'batsh_cap_' . $$ . '_' . $_SUBST_DEPTH . '_' . $_SUBST_SEQ . '.tmp');
1450 23 ▶ if (sysopen(_SUBST_CAPFH, $path, O_WRONLY | O_CREAT | O_EXCL, 0600)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysopen _SUBST_CAPFH, $path, 193, 384) 22 1 23
1451 22 push @_SUBST_TMPFILES, $path;
1452 22 return $path;
1453 }
1454 # EEXIST or transient error: retry with next sequence number
1455 }
1456 0 return undef;
1457 }
1458  
1459 # ----------------------------------------------------------------
1460 # Process substitution (v0.07): <(cmd) and >(cmd).
1461 #
1462 # This interpreter never forks (see the pipeline / background-job notes
1463 # elsewhere in this file), so neither form uses a real named pipe.
1464 # Instead:
1465 #
1466 # <(cmd) cmd's stdout is captured into a temp file (exactly like
1467 # $(cmd), but the file is kept instead of being read back
1468 # into a scalar) and <(cmd) is replaced by that file's path.
1469 # This covers the common case of feeding a whole command's
1470 # output to something that wants a filename, e.g.
1471 # "diff <(sort a) <(sort b)".
1472 #
1473 # >(cmd) an empty temp file is created and >(cmd) is replaced by its
1474 # path immediately; cmd itself is deferred and run with that
1475 # file as its stdin only after the current simple command
1476 # finishes (see the _exec_line wrapper), e.g.
1477 # "generate | tee >(gzip > out.gz)". Because cmd runs after
1478 # (not concurrently with) the writer, this is a best-effort
1479 # approximation of real, streaming >(...) and does not suit
1480 # writers that expect the reader to keep up in real time.
1481 #
1482 # Both temp files are removed by the _exec_line wrapper once the current
1483 # simple command (and, for >(cmd), its deferred job) has finished; a
1484 # process substitution used INSIDE another command substitution / loop
1485 # condition is cleaned up at that inner level, not held open for the
1486 # rest of the script.
1487 # ----------------------------------------------------------------
1488 sub _replace_process_subst {
1489 1190 ▶ my ($class, $str) = @_;
Subroutine _replace_process_subst: called CC=27 SCAR=42.1
1490 1190 ▶ return $str unless defined $str && $str =~ /[<>]\(/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and $str =~ /[<>]\(/ 1188 2 1190
Condition66%2/3
defined $str and $str =~ /[<>]\(/
!ll&&!rl&&r
011882
MC/DC50%1/2
defined $str and $str =~ /[<>]\(/
defined $str $str =~ /[<>]\(/
Truth table66%2/3
A and B
A= defined $strB= $str =~ /[<>]\(/
ABresult
0X0
100
111
1491  
1492 2 my $result = '';
1493 2 my @chars = split //, $str;
1494 2 my $n = scalar @chars;
1495 2 my $i = 0;
1496 2 my $in_sq = 0;
1497 2 my $in_dq = 0;
1498  
1499 2 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 27 2 29
1500 27 my $ch = $chars[$i];
1501  
1502 27 ▶ if ($in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq) 0 27 27
1503 0 ▶ if ($ch eq "'") { $in_sq = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'") 0 0 0
1504 0 $result .= $ch; $i++; next;
1505 }
1506 27 ▶ if ($ch eq "'" && !$in_dq) { $in_sq = 1; $result .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $in_dq) 0 27 27
Condition33%1/3
$ch eq "'" and not $in_dq
!ll&&!rl&&r
2700
MC/DC0%0/2
$ch eq "'" and not $in_dq
$ch eq "'" not $in_dq
Truth table33%1/3
A and not(B)
A= $ch eq "'"B= not $in_dq
ABresult
0X0
100
111
1507 27 ▶ if ($ch eq '"' && !$in_sq) { $in_dq = !$in_dq; $result .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" and not $in_sq) 0 27 27
Condition33%1/3
$ch eq "\"" and not $in_sq
!ll&&!rl&&r
2700
MC/DC0%0/2
$ch eq "\"" and not $in_sq
$ch eq "\"" not $in_sq
Truth table33%1/3
A and not(B)
A= $ch eq "\""B= not $in_sq
ABresult
0X0
100
111
1508 27 ▶ if (!$in_dq && $ch eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_dq and $ch eq "\\") 0 27 27
Condition33%1/3
not $in_dq and $ch eq "\\"
!ll&&!rl&&r
0270
MC/DC0%0/2
not $in_dq and $ch eq "\\"
not $in_dq $ch eq "\\"
Truth table33%1/3
not(A) and B
A= not $in_dqB= $ch eq "\\"
ABresult
0X0
100
111
1509 0 $result .= $ch; $i++;
1510 0 ▶ $result .= $chars[$i] if $i < $n; $i++; next;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $n 0 0 0
1511 }
1512  
1513 27 ▶ if (!$in_sq && !$in_dq && ($ch eq '<' || $ch eq '>')
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_sq || $in_dq and $ch eq "<" || $ch eq ">" and $i + 1 < $n and $chars[$i + 1] eq "(") 2 25 27
Condition73%11/15
not $in_sq || $in_dq and $ch eq "<" || $ch eq ">" and $i + 1 < $n and $chars[$i + 1] eq "("
!ll&&!rl&&r
2322
not $in_sq || $in_dq and $ch eq "<" || $ch eq ">" and $i + 1 < $n
!ll&&!rl&&r
2304
not $in_sq || $in_dq and $ch eq "<" || $ch eq ">"
!ll&&!rl&&r
0234
$in_sq || $in_dq
l!l&&r!l&&!r
0027
$ch eq "<" or $ch eq ">"
l!l&&r!l&&!r
2223
MC/DC50%3/6
not $in_sq || $in_dq and $ch eq "<" || $ch eq ">" and $i + 1 < $n and $chars[$i + 1] eq "("
$in_sq $in_dq $ch eq "<" $ch eq ">" $i + 1 < $n $chars[$i + 1] eq "("
Truth table55%5/9
not(A || B) and C or D and E and F
A= $in_sqB= $in_dqC= $ch eq "<"D= $ch eq ">"E= $i + 1 < $nF= $chars[$i + 1] eq "("
ABCDEFresult
1XXXXX0
01XXXX0
0000XX0
001X0X0
00010X0
001X100
0001100
001X111
0001111
1514 && $i+1 < $n && $chars[$i+1] eq '(') {
1515 2 my $dir = $ch;
1516 2 my $depth = 1;
1517 2 my $j = $i + 2;
1518 2 my $body = '';
1519 2 ▶ while ($j < $n && $depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $depth > 0) 37 2 39
Condition66%2/3
$j < $n and $depth > 0
!ll&&!rl&&r
2037
MC/DC50%1/2
$j < $n and $depth > 0
$j < $n $depth > 0
Truth table66%2/3
A and B
A= $j < $nB= $depth > 0
ABresult
0X0
100
111
1520 37 my $c = $chars[$j];
1521 37 ▶ if ($c eq '(') { $depth++; $body .= $c; $j++ }
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq '(') { } 0 37 37
elsif ($c eq ')') { } 2 35 37
1522 2 ▶ elsif ($c eq ')') { $depth--; $j++; $body .= $c if $depth > 0 }
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 0 0 2 2
1523 35 else { $body .= $c; $j++ }
1524 }
1525 2 ▶ if ($depth != 0) { $result .= $ch; $i++; next } # unterminated: literal '<'/'>'
Branch: 1 branch
BranchTrueFalseTotal
if ($depth != 0) 0 2 2
1526  
1527 2 ▶ if ($dir eq '<') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq '<') { } 1 1 2
1528 # Quote the generated path: it is a literal filename that
1529 # must survive the dequoting applied later to redirection
1530 # targets and command words -- otherwise a Windows temp
1531 # path (C:\...\batsh_ps.tmp) would lose its backslashes,
1532 # or a space in the temp dir would split the word.
1533 1 $result .= '"' . _procsub_capture($class, $body) . '"';
1534 }
1535 else {
1536 1 my $tmp = _procsub_tempfile();
1537 1 ▶ if (defined $tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tmp) 1 0 1
1538 1 close(_PROCSUB_CAPFH);
1539 1 push @_PROCSUB_DEFERRED, [$tmp, $body];
1540 1 $result .= '"' . $tmp . '"';
1541 }
1542 }
1543 2 $i = $j; next;
1544 }
1545  
1546 25 $result .= $ch; $i++;
1547 }
1548  
1549 2 return $result;
1550 }
1551  
1552 # _procsub_capture: run $cmd_str now, capturing its stdout into a fresh
1553 # temp file kept on disk, and return the file's path. Mirrors
1554 # _cmd_subst() but keeps the file instead of reading it back.
1555 sub _procsub_capture {
1556 1 ▶ my ($class, $cmd_str) = @_;
Subroutine _procsub_capture: called CC=4 SCAR=16.9
1557 1 local *_PROCSUB_SAVOUT;
1558 1 ▶ open(_PROCSUB_SAVOUT, '>&STDOUT') or return '';
Branch: 1 branch
BranchTrueFalseTotal
open _PROCSUB_SAVOUT, ">&STDOUT" or return "" 0 1 1
1559 1 my $tmpfile = _procsub_tempfile();
1560 1 ▶ if (!defined $tmpfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tmpfile) 0 1 1
1561 0 open(STDOUT, '>&_PROCSUB_SAVOUT'); close(_PROCSUB_SAVOUT); return '';
1562 }
1563 open(STDOUT, '>&_PROCSUB_CAPFH')
1564 1 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&_PROCSUB_CAPFH" or do { close _PROCSUB_CAPFH; unlink $tmpfile; @BATsh::SH::_PROCSUB_TMPFILES = grep({$_ ne $tmpfile;} @BATsh::SH::_PROCSUB_TMPFILES); open STDOUT, ">&_PROCSUB_SAVOUT"; close _PROCSUB_SAVOUT; return "" } 0 1 1
1565 0 close(_PROCSUB_CAPFH); unlink $tmpfile;
1566 0 @_PROCSUB_TMPFILES = grep { $_ ne $tmpfile } @_PROCSUB_TMPFILES;
1567 0 open(STDOUT, '>&_PROCSUB_SAVOUT'); close(_PROCSUB_SAVOUT); return '';
1568 };
1569 1 close(_PROCSUB_CAPFH);
1570 1 eval {
1571 1 my @sub_lines = split /\n/, $cmd_str;
1572 1 _run_lines($class, \@sub_lines, {});
1573 };
1574 1 open(STDOUT, '>&_PROCSUB_SAVOUT');
1575 1 close(_PROCSUB_SAVOUT);
1576 1 return $tmpfile;
1577 }
1578  
1579 # _procsub_tempfile: create a unique, empty temp file (O_CREAT|O_EXCL to
1580 # avoid symlink races, mirroring _subst_tempfile()) and open it as the
1581 # package bareword filehandle _PROCSUB_CAPFH. Returns the path, or
1582 # undef on failure. The path is tracked in @_PROCSUB_TMPFILES for the
1583 # _exec_line wrapper (normal cleanup) and the END-block failsafe.
1584 sub _procsub_tempfile {
1585 2 ▶ my $dir = File::Spec->tmpdir();
Subroutine _procsub_tempfile: called CC=5 SCAR=17.4
1586 2 ▶ $dir = '.' if !(-d $dir && -w $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir and -w $dir 0 2 2
Condition33%1/3
-d $dir and -w $dir
!ll&&!rl&&r
002
MC/DC0%0/2
-d $dir and -w $dir
-d $dir -w $dir
Truth table33%1/3
A and B
A= -d $dirB= -w $dir
ABresult
0X0
100
111
1587  
1588 2 my $attempt = 0;
1589 2 ▶ while ($attempt < 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attempt < 1000) 2 0 2
1590 2 $_PROCSUB_SEQ++;
1591 2 $attempt++;
1592 2 my $path = File::Spec->catfile($dir,
1593 'batsh_ps_' . $$ . '_' . $_PROCSUB_SEQ . '.tmp');
1594 2 ▶ if (sysopen(_PROCSUB_CAPFH, $path, O_WRONLY | O_CREAT | O_EXCL, 0600)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysopen _PROCSUB_CAPFH, $path, 193, 384) 2 0 2
1595 2 push @_PROCSUB_TMPFILES, $path;
1596 2 return $path;
1597 }
1598 # EEXIST or transient error: retry with next sequence number
1599 }
1600 0 return undef;
1601 }
1602  
1603 # ----------------------------------------------------------------
1604 # export
1605 # ----------------------------------------------------------------
1606 sub _cmd_export {
1607 40 ▶ my ($rest) = @_;
Subroutine _cmd_export: called CC=6 SCAR=20.5
1608 40 $rest =~ s/\A\s+//;
1609 # export -p: print all
1610 40 ▶ if ($rest =~ /\A-p\s*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /\A-p\s*\z/) 0 40 40
1611 0 for my $k (sort keys %BATsh::Env::STORE) {
1612 0 my $v = $BATsh::Env::STORE{$k};
1613 0 $v =~ s/'/'\\''/g;
1614 0 print "export $k='$v'\n";
1615 }
1616 0 return 0;
1617 }
1618 # export VAR=value or export VAR. The value is split on UNQUOTED
1619 # whitespace and dequoted exactly like an ordinary assignment, so
1620 # `export V='C:\x'` exports C:\x rather than the quote characters
1621 # themselves, and `export V="a b"` keeps its embedded space.
1622 40 for my $item (_arr_split_words($rest)) {
1623 40 ▶ if ($item =~ /\A([A-Za-z_][A-Za-z0-9_]*)=(.*)\z/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item =~ /\A([A-Za-z_][A-Za-z0-9_]*)=(.*)\z/s) { } 39 1 40
elsif ($item =~ /\A([A-Za-z_][A-Za-z0-9_]*)\z/) { } 1 0 1
1624 39 _sh_store_scalar($1, _arr_dequote($2));
1625 }
1626 elsif ($item =~ /\A([A-Za-z_][A-Za-z0-9_]*)\z/) {
1627 # export existing variable (already in store; no-op)
1628 }
1629 }
1630 40 $LAST_STATUS = 0;
1631 40 return 0;
1632 }
1633  
1634 # ----------------------------------------------------------------
1635 # unset
1636 # ----------------------------------------------------------------
1637 sub _cmd_unset {
1638 6 ▶ my ($rest) = @_;
Subroutine _cmd_unset: called CC=9 SCAR=22.3
1639 6 my $status = 0;
1640 6 for my $var (split /\s+/, $rest) {
1641 6 $var =~ s/\A\s+//; $var =~ s/\s+\z//;
1642 6 ▶ next if $var eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $var eq "" 0 6 6
1643 # A readonly variable / element cannot be unset (bash: status 1).
1644 6 ▶ if ($_SH_READONLY{ uc($var) }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BATsh::SH::_SH_READONLY{uc $var}) 2 4 6
1645 2 print STDERR "sh: unset: $var: cannot unset: readonly variable\n";
1646 2 $status = 1;
1647 2 next;
1648 }
1649 # unset NAME[SUB] -- remove a single array element
1650 4 ▶ if ($var =~ /\A([A-Za-z_][A-Za-z0-9_]*)\[([^\]]*)\]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var =~ /\A([A-Za-z_][A-Za-z0-9_]*)\[([^\]]*)\]\z/) 2 2 4
1651 2 my ($name, $sub) = ($1, $2);
1652 2 my $k = _arr_name($name);
1653 2 ▶ if (exists $_SH_ARRAY{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $BATsh::SH::_SH_ARRAY{$k}) 2 0 2
1654 2 ▶ if ((defined $_SH_ARRAY_TYPE{$k} && $_SH_ARRAY_TYPE{$k} eq 'assoc')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $BATsh::SH::_SH_ARRAY_TYPE{$k} and $BATsh::SH::_SH_ARRAY_TYPE{$k} eq 'assoc') { } 0 2 2
Condition33%1/3
defined $BATsh::SH::_SH_ARRAY_TYPE{$k} and $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
!ll&&!rl&&r
020
MC/DC0%0/2
defined $BATsh::SH::_SH_ARRAY_TYPE{$k} and $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
defined $BATsh::SH::_SH_ARRAY_TYPE{$k} $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
Truth table33%1/3
A and B
A= defined $BATsh::SH::_SH_ARRAY_TYPE{$k}B= $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
ABresult
0X0
100
111
1655 0 delete $_SH_ARRAY{$k}{$sub};
1656 }
1657 else {
1658 2 delete $_SH_ARRAY{$k}{ _arr_index($sub) };
1659 }
1660 }
1661 2 next;
1662 }
1663 # unset NAME -- remove a whole array (and any scalar of the same name)
1664 2 my $k = _arr_name($var);
1665 2 ▶ if (exists $_SH_ARRAY{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $BATsh::SH::_SH_ARRAY{$k}) 1 1 2
1666 1 delete $_SH_ARRAY{$k};
1667 1 delete $_SH_ARRAY_TYPE{$k};
1668 }
1669 2 BATsh::Env->unset($var);
1670 }
1671 6 $LAST_STATUS = $status;
1672 6 return $status;
1673 }
1674  
1675 # ----------------------------------------------------------------
1676 # echo
1677 # ----------------------------------------------------------------
1678 sub _cmd_echo {
1679 425 ▶ my ($rest) = @_;
Subroutine _cmd_echo: called CC=5 SCAR=16.2
1680 425 $rest =~ s/\A\s+//;
1681 425 my $no_newline = 0;
1682 425 ▶ if ($rest =~ s/\A-n\s*//) { $no_newline = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/\A-n\s*//) 0 425 425
1683 # -e: enable escape sequences
1684 425 my $escape = 0;
1685 425 ▶ if ($rest =~ s/\A-e\s*//) { $escape = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/\A-e\s*//) 1 424 425
1686 425 ▶ if ($escape) {
Branch: 1 branch
BranchTrueFalseTotal
if ($escape) 1 424 425
1687 1 $rest =~ s/\\n/\n/g;
1688 1 $rest =~ s/\\t/\t/g;
1689 1 $rest =~ s/\\r/\r/g;
1690 1 $rest =~ s/\\\\/\\/g;
1691 # The same sequences arriving from an expansion (v='a\tb';
1692 # echo -e $v) still carry the protection sentinel in place of
1693 # their backslash, and are interpreted here as well; a protected
1694 # backslash that does not start a known escape stays protected,
1695 # so a Windows path keeps its separators.
1696 1 $rest =~ s/\x00BATSH_LB\x00n/\n/g;
1697 1 $rest =~ s/\x00BATSH_LB\x00t/\t/g;
1698 1 $rest =~ s/\x00BATSH_LB\x00r/\r/g;
1699 1 $rest =~ s/\x00BATSH_LB\x00\x00BATSH_LB\x00/\x00BATSH_LB\x00/g;
1700 }
1701 # Remove shell quoting structurally so that quotes anywhere in the
1702 # argument list are dropped (e.g. echo "${arr[@]}" tail), not only when
1703 # the whole string is wrapped in one pair of quotes.
1704 425 $rest = _arr_dequote($rest);
1705 425 $rest = BATsh::MB::dec($rest);
1706 425 ▶ if ($no_newline) { print $rest }
Branch: 1 branch
BranchTrueFalseTotal
if ($no_newline) { } 0 425 425
1707 425 else { print "$rest\n" }
1708 425 $LAST_STATUS = 0;
1709 425 return 0;
1710 }
1711  
1712 # ----------------------------------------------------------------
1713 # printf
1714 # ----------------------------------------------------------------
1715 # A faithful pure-Perl printf: POSIX/bash format-string escapes, the full
1716 # conversion set (%d %i %o %u %x %X %e %E %f %g %G %c %s %b %q %%), field
1717 # width / precision including the dynamic "*" form, argument RECYCLING (the
1718 # format is reused until the arguments are exhausted), the "%b" conversion
1719 # (backslash escapes interpreted in the argument, with "\c" ending output),
1720 # the "%q" conversion (shell-reusable quoting), and the "-v VAR" option
1721 # (store the result in a shell variable instead of printing). Perl 5.005_03
1722 # safe: no lexical filehandles, no 3-argument open, no post-5.005 sprintf
1723 # conversions (%a is deliberately not emitted).
1724 sub _cmd_printf {
1725 24 ▶ my ($rest) = @_;
Subroutine _cmd_printf: called CC=10 SCAR=23.3
1726 24 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 24 24
1727  
1728 24 my @tok = _arr_split_words($rest);
1729  
1730 # Options on the still-quoted tokens: -v VAR / -vVAR, and -- terminator.
1731 24 my $target;
1732 24 ▶ while (@tok) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tok) 26 0 26
1733 26 my $t0 = $tok[0];
1734 26 ▶ if ($t0 eq '--') { shift @tok; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($t0 eq "--") 1 25 26
1735 25 ▶ if ($t0 eq '-v') {
Branch: 1 branch
BranchTrueFalseTotal
if ($t0 eq "-v") 1 24 25
1736 1 shift @tok;
1737 1 ▶ my $vn = @tok ? _arr_dequote(shift @tok) : '';
Branch: 1 branch
BranchTrueFalseTotal
@tok ? : 1 0 1
1738 1 ▶ $target = $vn if $vn ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $vn ne "" 1 0 1
1739 1 next;
1740 }
1741 24 ▶ if ($t0 =~ /\A-v(.+)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t0 =~ /\A-v(.+)\z/s) 1 23 24
1742 1 shift @tok;
1743 1 $target = _arr_dequote($1);
1744 1 next;
1745 }
1746 23 last;
1747 }
1748 24 ▶ if (!@tok) { $LAST_STATUS = 0; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (@tok) 0 24 24
1749  
1750 24 my $fmt = _arr_dequote(shift @tok);
1751 24 my @args = map { _arr_dequote($_) } @tok;
1752 24 $fmt = BATsh::MB::dec($fmt);
1753 24 @args = map { BATsh::MB::dec($_) } @args;
1754  
1755 24 my ($fmt_x, $fmt_stop) = _printf_unescape($fmt, 1);
1756 24 my $out = _printf_format($fmt_x, $fmt_stop, @args);
1757  
1758 24 ▶ if (defined $target) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target) { } 2 22 24
1759 2 BATsh::Env->set($target, $out);
1760 }
1761 else {
1762 22 print $out;
1763 }
1764 24 $LAST_STATUS = 0;
1765 24 return 0;
1766 }
1767  
1768 # _printf_unescape(STR, ALLOW_C): interpret C/POSIX backslash escapes.
1769 # Returns (RESULT, STOP) where STOP is true when a "\c" (only recognised
1770 # when ALLOW_C is set) requested that output cease. Unknown escapes keep
1771 # their backslash, matching bash.
1772 sub _printf_unescape {
1773 27 ▶ my ($s, $allow_c) = @_;
Subroutine _printf_unescape: called CC=23 SCAR=42.0
1774 27 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 27 27
1775 27 my $out = '';
1776 27 my $stop = 0;
1777 27 my $i = 0;
1778 27 my $n = length $s;
1779 27 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 99 26 125
1780 99 my $c = substr($s, $i, 1);
1781 99 ▶ if ($c ne '\\') { $out .= $c; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c ne "\\") 71 28 99
1782 28 my $d = substr($s, $i + 1, 1);
1783 28 ▶ if ($d eq 'n') { $out .= "\n"; $i += 2 }
Branch: 15 branches
BranchTrueFalseTotal
if ($d eq 'n') { } 24 4 28
elsif ($d eq 't') { } 1 3 4
elsif ($d eq 'r') { } 0 3 3
elsif ($d eq '\\') { } 0 3 3
elsif ($d eq 'a') { } 0 3 3
elsif ($d eq 'b') { } 0 3 3
elsif ($d eq 'f') { } 0 3 3
elsif ($d eq 'v') { } 0 3 3
elsif ($d eq 'e' or $d eq 'E') { } 0 3 3
elsif ($d eq '"') { } 0 3 3
elsif ($d eq q[']) { } 0 3 3
elsif ($d eq 'c' and $allow_c) { } 1 2 3
elsif ($d eq 'x') { } 1 1 2
elsif ($d =~ /[0-7]/) { } 1 0 1
elsif ($d eq '') { } 0 0 0
1784 1 elsif ($d eq 't') { $out .= "\t"; $i += 2 }
1785 0 elsif ($d eq 'r') { $out .= "\r"; $i += 2 }
1786 0 elsif ($d eq '\\') { $out .= "\\"; $i += 2 }
1787 0 elsif ($d eq 'a') { $out .= chr(7); $i += 2 }
1788 0 elsif ($d eq 'b') { $out .= chr(8); $i += 2 }
1789 0 elsif ($d eq 'f') { $out .= chr(12); $i += 2 }
1790 0 elsif ($d eq 'v') { $out .= chr(11); $i += 2 }
1791 0 ▶ elsif ($d eq 'e' || $d eq 'E') { $out .= chr(27); $i += 2 }
Condition33%1/3
$d eq "e" or $d eq "E"
l!l&&r!l&&!r
003
MC/DC0%0/2
$d eq "e" or $d eq "E"
$d eq "e" $d eq "E"
Truth table33%1/3
A or B
A= $d eq "e"B= $d eq "E"
ABresult
1X1
011
000
1792 0 elsif ($d eq '"') { $out .= '"'; $i += 2 }
1793 0 elsif ($d eq "'") { $out .= "'"; $i += 2 }
1794 1 ▶ elsif ($d eq 'c' && $allow_c) { $stop = 1; last }
Condition66%2/3
$d eq "c" and $allow_c
!ll&&!rl&&r
201
MC/DC50%1/2
$d eq "c" and $allow_c
$d eq "c" $allow_c
Truth table66%2/3
A and B
A= $d eq "c"B= $allow_c
ABresult
0X0
100
111
1795 elsif ($d eq 'x') {
1796 1 my $h = substr($s, $i + 2);
1797 1 ▶ if ($h =~ /\A([0-9A-Fa-f]{1,2})/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h =~ /\A([0-9A-Fa-f]{1,2})/) { } 1 0 1
1798 1 $out .= chr(hex($1)); $i += 2 + length($1);
1799 }
1800 0 else { $out .= '\\x'; $i += 2 }
1801 }
1802 elsif ($d =~ /[0-7]/) {
1803 1 my $o = substr($s, $i + 1);
1804 1 ▶ if ($o =~ /\A(0?[0-7]{1,3})/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o =~ /\A(0?[0-7]{1,3})/) { } 1 0 1
1805 1 $out .= chr(oct($1) & 0xFF); $i += 1 + length($1);
1806 }
1807 0 else { $out .= '\\'; $i++ }
1808 }
1809 0 elsif ($d eq '') { $out .= '\\'; $i++ }
1810 0 else { $out .= '\\' . $d; $i += 2 }
1811 }
1812 27 return ($out, $stop);
1813 }
1814  
1815 # _printf_int(STR) / _printf_num(STR): coerce a printf argument to an
1816 # integer / a number for a numeric conversion. A leading ' or " selects
1817 # the numeric value of the following character (POSIX); otherwise a leading
1818 # numeric prefix (decimal, 0x hex, 0 octal) is used, and a non-numeric
1819 # argument yields 0 (bash warns and uses 0; we stay quiet).
1820 sub _printf_int {
1821 8 ▶ my ($s) = @_;
Subroutine _printf_int: called CC=8 SCAR=25.1
1822 8 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 8 8
1823 8 ▶ if ($s =~ /\A['"](.)/s) { return ord($1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A['"](.)/s) 1 7 8
1824 7 ▶ if ($s =~ /\A\s*([-+]?)0[xX]([0-9A-Fa-f]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A\s*([-+]?)0[xX]([0-9A-Fa-f]+)/) 0 7 7
1825 0 ▶ my $v = hex($2); return ($1 eq '-') ? -$v : $v;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '-' ? : 0 0 0
1826 }
1827 7 ▶ if ($s =~ /\A\s*([-+]?)0([0-7]+)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A\s*([-+]?)0([0-7]+)\z/) 0 7 7
1828 0 ▶ my $v = oct($2); return ($1 eq '-') ? -$v : $v;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '-' ? : 0 0 0
1829 }
1830 7 ▶ if ($s =~ /\A\s*([-+]?\d+)/) { return int($1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A\s*([-+]?\d+)/) 5 2 7
1831 2 return 0;
1832 }
1833  
1834 sub _printf_num {
1835 1 ▶ my ($s) = @_;
Subroutine _printf_num: called CC=6 SCAR=26.2
1836 1 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 1 1
1837 1 ▶ if ($s =~ /\A['"](.)/s) { return ord($1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A['"](.)/s) 0 1 1
1838 1 ▶ if ($s =~ /\A\s*([-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?)/) { return $1 + 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A\s*([-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?)/) 1 0 1
1839 0 ▶ if ($s =~ /\A\s*([-+]?)0[xX]([0-9A-Fa-f]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A\s*([-+]?)0[xX]([0-9A-Fa-f]+)/) 0 0 0
1840 0 ▶ my $v = hex($2); return ($1 eq '-') ? -$v : $v;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '-' ? : 0 0 0
1841 }
1842 0 return 0;
1843 }
1844  
1845 # _printf_shellquote(STR): the "%q" conversion -- quote STR so it reads back
1846 # as a single shell word. Safe characters are emitted bare; anything else
1847 # is single-quoted with embedded single quotes rendered as '\''.
1848 sub _printf_shellquote {
1849 2 ▶ my ($s) = @_;
Subroutine _printf_shellquote: called CC=4 SCAR=14.0
1850 2 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 2 2
1851 2 ▶ return "''" if $s eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "" 0 2 2
1852 2 ▶ return $s if $s =~ m{\A[-A-Za-z0-9_./:=@%+,]+\z};
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ m[\A[-A-Za-z0-9_./:=\@%+,]+\z] 1 1 2
1853 1 my $q = $s;
1854 1 $q =~ s/'/'\\''/g;
1855 1 return "'" . $q . "'";
1856 }
1857  
1858 # _sp(FMT, ARGS): sprintf with warnings suppressed (a non-numeric argument
1859 # already coerced to 0, an over-wide field, etc. must not print to STDERR).
1860 sub _sp {
1861 26 ▶ my ($f, @a) = @_;
Subroutine _sp: called CC=1 SCAR=0.0
1862 26 ▶ local $SIG{'__WARN__'} = sub { };
Subroutine __ANON__: not called CC=1 SCAR=0.0
1863 26 return sprintf($f, @a);
1864 }
1865  
1866 # _printf_format(FMT, FMT_STOP, ARGS): apply the (already escape-expanded)
1867 # format to the argument list, recycling the format until the arguments are
1868 # exhausted (bash semantics), and stopping early if a "\c" was seen.
1869 sub _printf_format {
1870 24 ▶ my ($fmt, $fmt_stop, @args) = @_;
Subroutine _printf_format: called CC=28 SCAR=33.8
1871 24 my $out = '';
1872 24 my $ai = 0;
1873 24 my $narg = scalar @args;
1874 24 my $guard = 0;
1875 24 while (1) {
1876 27 $guard++;
1877 27 ▶ last if $guard > 100000;
Branch: 1 branch
BranchTrueFalseTotal
if $guard > 100000 0 27 27
1878 27 my $consumed = 0;
1879 27 my $stop = 0;
1880 27 my $i = 0;
1881 27 my $len = length $fmt;
1882 27 ▶ while ($i < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 70 26 96
1883 70 my $c = substr($fmt, $i, 1);
1884 70 ▶ if ($c ne '%') { $out .= $c; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c ne "%") 43 27 70
1885 27 ▶ if (substr($fmt, $i + 1, 1) eq '%') { $out .= '%'; $i += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if (substr($fmt, $i + 1, 1) eq "%") 1 26 27
1886 26 my $tail = substr($fmt, $i);
1887 26 ▶ if ($tail =~ /\A(%[-+ 0#]*(?:\*|\d+)?(?:\.(?:\*|\d+))?)([diouxXeEfgGcsbq])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tail =~ /\A(%[-+ 0#]*(?:\*|\d+)?(?:\.(?:\*|\d+))?)([diouxXeEfgGcsbq])/) { } 26 0 26
1888 26 my $spec = $1;
1889 26 my $conv = $2;
1890 26 $i += length($1) + length($2);
1891  
1892 26 ▶ while ($spec =~ /\*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /\*/) 1 26 27
1893 1 ▶ my $val = ($ai < $narg) ? _printf_int($args[$ai]) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ai < $narg ? : 1 0 1
1894 1 ▶ if ($ai < $narg) { $ai++; $consumed++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($ai < $narg) 1 0 1
1895 1 ▶ $val = -$val if $val < 0; # negative field width -> abs
Branch: 1 branch
BranchTrueFalseTotal
if $val < 0 0 1 1
1896 1 $spec =~ s/\*/$val/;
1897 }
1898  
1899 26 my $have = ($ai < $narg);
1900 26 ▶ my $raw = $have ? $args[$ai] : '';
Branch: 1 branch
BranchTrueFalseTotal
$have ? : 25 1 26
1901  
1902 26 ▶ if ($conv eq 's') {
Branch: 5 branches
BranchTrueFalseTotal
if ($conv eq 's') { } 11 15 26
elsif ($conv eq 'b') { } 3 12 15
elsif ($conv eq 'q') { } 2 10 12
elsif ($conv eq 'c') { } 2 8 10
elsif ($conv =~ /[diouxX]/) { } 7 1 8
1903 11 $out .= _sp($spec . 's', $raw);
1904 11 ▶ if ($have) { $ai++; $consumed++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($have) 10 1 11
1905 }
1906 elsif ($conv eq 'b') {
1907 3 my ($ex, $st) = _printf_unescape($raw, 1);
1908 3 $out .= _sp($spec . 's', $ex);
1909 3 ▶ if ($have) { $ai++; $consumed++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($have) 3 0 3
1910 3 ▶ if ($st) { $stop = 1; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($st) 1 2 3
1911 }
1912 elsif ($conv eq 'q') {
1913 2 $out .= _sp($spec . 's', _printf_shellquote($raw));
1914 2 ▶ if ($have) { $ai++; $consumed++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($have) 2 0 2
1915 }
1916 elsif ($conv eq 'c') {
1917 2 ▶ my $ch = length($raw) ? substr($raw, 0, 1) : '';
Branch: 1 branch
BranchTrueFalseTotal
length $raw ? : 2 0 2
1918 2 $out .= _sp($spec . 's', $ch);
1919 2 ▶ if ($have) { $ai++; $consumed++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($have) 2 0 2
1920 }
1921 elsif ($conv =~ /[diouxX]/) {
1922 7 $out .= _sp($spec . $conv, _printf_int($raw));
1923 7 ▶ if ($have) { $ai++; $consumed++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($have) 7 0 7
1924 }
1925 else {
1926 1 $out .= _sp($spec . $conv, _printf_num($raw));
1927 1 ▶ if ($have) { $ai++; $consumed++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($have) 1 0 1
1928 }
1929 }
1930 0 else { $out .= '%'; $i++ }
1931 }
1932 27 ▶ last if $stop || $fmt_stop;
Branch: 1 branch
BranchTrueFalseTotal
if $stop or $fmt_stop 1 26 27
Condition66%2/3
$stop or $fmt_stop
l!l&&r!l&&!r
1026
MC/DC50%1/2
$stop or $fmt_stop
$stop $fmt_stop
Truth table66%2/3
A or B
A= $stopB= $fmt_stop
ABresult
1X1
011
000
1933 26 ▶ last unless ($ai < $narg && $consumed > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $ai < $narg and $consumed > 0 23 3 26
Condition66%2/3
$ai < $narg and $consumed > 0
!ll&&!rl&&r
2303
MC/DC50%1/2
$ai < $narg and $consumed > 0
$ai < $narg $consumed > 0
Truth table66%2/3
A and B
A= $ai < $nargB= $consumed > 0
ABresult
0X0
100
111
1934 }
1935 24 return $out;
1936 }
1937  
1938 # ----------------------------------------------------------------
1939 # Tilde expansion (v0.07): ~/path and ~user/path.
1940 #
1941 # POSIX word-initial tilde expansion. Only applied to a word that
1942 # begins with an UNQUOTED ~ (callers are responsible for that check);
1943 # the tilde-prefix itself is never subject to variable / command
1944 # substitution. Forms:
1945 # ~ -> $HOME
1946 # ~/rest -> $HOME . '/rest'
1947 # ~user -> user's home directory (getpwnam; Unix-like only)
1948 # ~user/rest -> that directory . '/rest'
1949 # If the login name is unknown (or getpwnam is unavailable, as on
1950 # Win32) the word is returned unchanged, matching bash's behaviour of
1951 # leaving an unresolvable ~name literal.
1952 # NOT implemented: colon-list tilde expansion in PATH-like assignments
1953 # (bash also tilde-expands after ':' in PATH=~/a:~/b); documented as a
1954 # limitation in the POD.
1955 # ----------------------------------------------------------------
1956 sub _tilde_expand {
1957 9 ▶ my ($word) = @_;
Subroutine _tilde_expand: called CC=13 SCAR=28.9
1958 9 ▶ return $word unless defined $word && $word =~ /\A~/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $word and $word =~ /\A~/ 0 9 9
Condition33%1/3
defined $word and $word =~ /\A~/
!ll&&!rl&&r
009
MC/DC0%0/2
defined $word and $word =~ /\A~/
defined $word $word =~ /\A~/
Truth table33%1/3
A and B
A= defined $wordB= $word =~ /\A~/
ABresult
0X0
100
111
1959 9 my ($tag, $rest) = ($word =~ /\A~([^\/]*)(.*)\z/s);
1960 9 ▶ $tag = '' unless defined $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag 0 9 9
1961 9 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 9 9
1962 9 my $home;
1963 9 ▶ if ($tag eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag eq '') { } 8 1 9
elsif (not $^O =~ /MSWin32/i) { } 1 0 1
1964 8 $home = $ENV{'HOME'};
1965 8 ▶ $home = BATsh::Env->get('HOME') unless defined $home && $home ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $home and $home ne "" 0 8 8
Condition33%1/3
defined $home and $home ne ""
!ll&&!rl&&r
008
MC/DC0%0/2
defined $home and $home ne ""
defined $home $home ne ""
Truth table33%1/3
A and B
A= defined $homeB= $home ne ""
ABresult
0X0
100
111
1966 }
1967 elsif ($^O !~ /MSWin32/i) {
1968 1 my @pw = eval { getpwnam($tag) };
1969 1 ▶ $home = (!$@ && @pw) ? $pw[7] : undef;
Branch: 1 branch
BranchTrueFalseTotal
!$@ && @pw ? : 0 1 1
Condition33%1/3
not $@ and @pw
!ll&&!rl&&r
010
MC/DC0%0/2
not $@ and @pw
not $@ @pw
Truth table33%1/3
not(A) and B
A= not $@B= @pw
ABresult
0X0
100
111
1970 }
1971 9 ▶ return $word unless defined $home && $home ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $home and $home ne "" 1 8 9
Condition66%2/3
defined $home and $home ne ""
!ll&&!rl&&r
108
MC/DC50%1/2
defined $home and $home ne ""
defined $home $home ne ""
Truth table66%2/3
A and B
A= defined $homeB= $home ne ""
ABresult
0X0
100
111
1972 8 return $home . $rest;
1973 }
1974  
1975 # _tilde_prepass: scan a RAW (not-yet-variable-expanded) string and
1976 # expand every unquoted, word-initial "~..." run using _tilde_expand().
1977 # A "word start" is: the very beginning of the string, any position
1978 # right after unquoted whitespace, or (once, at the very beginning of
1979 # the string only) the position right after the '=' of a leading
1980 # NAME= assignment token -- matching bash's extra tilde-expansion spot
1981 # for VAR=~/path. Quoted text (single or double quotes) is copied
1982 # through untouched and never considered a word start for tilde
1983 # purposes. Everything else in the string (variables, command
1984 # substitution, arithmetic, ...) is left as-is for the rest of
1985 # _expand() to process afterwards.
1986 # _tilde_protect: let an expanded tilde-prefix survive the rest of the
1987 # expansion pipeline unharmed. The replacement text is DATA, not shell
1988 # source: a Windows home directory such as C:\home\flower must not have
1989 # its backslashes eaten by quote removal, a "$" or "`" in it must not
1990 # start an expansion, and a home directory containing spaces must stay
1991 # a single word (POSIX: a tilde-expansion result is not field-split).
1992 # An unresolved ~name is returned untouched, exactly as bash leaves it.
1993 sub _tilde_protect {
1994 9 ▶ my ($expanded, $orig) = @_;
Subroutine _tilde_protect: called CC=4 SCAR=13.9
1995 9 ▶ return $orig unless defined $expanded;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expanded 0 9 9
1996 9 ▶ return $expanded if $expanded eq $orig;
Branch: 1 branch
BranchTrueFalseTotal
if $expanded eq $orig 1 8 9
1997 8 my $v = _protect_lit($expanded);
1998 8 $v =~ s/\$/\x00BATSH_DL\x00/g;
1999 8 $v =~ s/`/\x00BATSH_BT\x00/g;
2000 # Only a home directory containing whitespace needs the quotes;
2001 # quoting the usual case too would suppress globbing of ~/* words.
2002 8 ▶ return $v unless $v =~ /\s/;
Branch: 1 branch
BranchTrueFalseTotal
unless $v =~ /\s/ 6 2 8
2003 2 return '"' . $v . '"';
2004 }
2005  
2006 sub _tilde_prepass {
2007 1190 ▶ my ($str) = @_;
Subroutine _tilde_prepass: called CC=16 SCAR=27.7
2008  
2009 1190 ▶ return $str unless defined $str && $str =~ /~/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and $str =~ /~/ 1176 14 1190
Condition66%2/3
defined $str and $str =~ /~/
!ll&&!rl&&r
0117614
MC/DC50%1/2
defined $str and $str =~ /~/
defined $str $str =~ /~/
Truth table66%2/3
A and B
A= defined $strB= $str =~ /~/
ABresult
0X0
100
111
2010  
2011 14 my @chars = split //, $str;
2012 14 my $n = scalar @chars;
2013 14 my $out = '';
2014 14 my $i = 0;
2015  
2016 # Leading NAME= assignment token: the char right after '=' is also
2017 # a valid tilde-expansion start, but only for this one leading token.
2018 14 ▶ if ($str =~ /\A([A-Za-z_][A-Za-z0-9_]*=)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\A([A-Za-z_][A-Za-z0-9_]*=)/) 2 12 14
2019 2 my $prefix = $1;
2020 2 $out .= $prefix;
2021 2 $i = length($prefix);
2022 }
2023  
2024 14 my $at_word_start = 1;
2025 14 my $in_sq = 0;
2026 14 my $in_dq = 0;
2027 14 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 113 14 127
2028 113 my $c = $chars[$i];
2029 113 ▶ if ($in_sq) { $out .= $c; $in_sq = 0 if $c eq "'"; $i++; $at_word_start = 0; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($in_sq) 4 109 113
if $c eq "'" 1 3 4
2030 109 ▶ if ($in_dq) { $out .= $c; $in_dq = 0 if $c eq '"'; $i++; $at_word_start = 0; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($in_dq) 10 99 109
if $c eq "\"" 2 8 10
2031 99 ▶ if ($c eq "'") { $in_sq = 1; $out .= $c; $i++; $at_word_start = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "'") 1 98 99
2032 98 ▶ if ($c eq '"') { $in_dq = 1; $out .= $c; $i++; $at_word_start = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\"") 2 96 98
2033 96 ▶ if ($c =~ /\s/) { $out .= $c; $i++; $at_word_start = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /\s/) 12 84 96
2034 84 ▶ if ($at_word_start && $c eq '~') {
Branch: 1 branch
BranchTrueFalseTotal
if ($at_word_start and $c eq "~") 9 75 84
Condition100%3/3
$at_word_start and $c eq "~"
!ll&&!rl&&r
61149
MC/DC100%2/2
$at_word_start and $c eq "~"
$at_word_start $c eq "~"
Truth table100%3/3
A and B
A= $at_word_startB= $c eq "~"
ABresult
0X0
100
111
2035 9 my $j = $i + 1;
2036 9 my $tag = '';
2037 9 ▶ while ($j < $n && $chars[$j] !~ /[\s\/'"]/) { $tag .= $chars[$j]; $j++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and not $chars[$j] =~ m[[\s/'"]]) 25 9 34
Condition100%3/3
$j < $n and not $chars[$j] =~ m[[\s/'"]]
!ll&&!rl&&r
5425
MC/DC100%2/2
$j < $n and not $chars[$j] =~ m[[\s/'"]]
$j < $n not $chars[$j] =~ m[[\s/'"]]
Truth table100%3/3
A and not(B)
A= $j < $nB= not $chars[$j] =~ m[[\s/'"]]
ABresult
0X0
100
111
2038 9 $out .= _tilde_protect(_tilde_expand('~' . $tag), '~' . $tag);
2039 9 $i = $j;
2040 9 $at_word_start = 0;
2041 9 next;
2042 }
2043 75 $out .= $c;
2044 75 $i++;
2045 75 $at_word_start = 0;
2046 }
2047 14 return $out;
2048 }
2049  
2050 # ----------------------------------------------------------------
2051 # Brace expansion (v0.07): {a,b,c} and {1..5} / {a..e} [..step] word
2052 # generation. Runs on the RAW source line, lexically, before any other
2053 # expansion -- exactly like the tilde prepass above. Quoted text and
2054 # anything that is opaque shell syntax (${...}, $(...), $((...)), `...`,
2055 # <(...), >(...), and backslash-escaped characters) is protected behind
2056 # a NUL-delimited sentinel so its content is copied through untouched
2057 # and its internal whitespace never becomes a false word boundary.
2058 # ----------------------------------------------------------------
2059 sub _brace_expand_line {
2060 1148 ▶ my ($line) = @_;
Subroutine _brace_expand_line: called CC=41 SCAR=47.2
2061 1148 ▶ return $line unless defined $line && $line =~ /\{/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line and $line =~ /\{/ 1067 81 1148
Condition66%2/3
defined $line and $line =~ /\{/
!ll&&!rl&&r
0106781
MC/DC50%1/2
defined $line and $line =~ /\{/
defined $line $line =~ /\{/
Truth table66%2/3
A and B
A= defined $lineB= $line =~ /\{/
ABresult
0X0
100
111
2062  
2063 81 my @protected;
2064 81 my $safe = '';
2065 81 my @c = split //, $line;
2066 81 my $n = scalar @c;
2067 81 my $i = 0;
2068 81 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 533 81 614
2069 533 my $ch = $c[$i];
2070  
2071 533 ▶ if ($ch eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 0 533 533
2072 0 ▶ my $seg = $ch . (($i+1 < $n) ? $c[$i+1] : '');
Branch: 1 branch
BranchTrueFalseTotal
$i + 1 < $n ? : 0 0 0
2073 0 push @protected, $seg;
2074 0 $safe .= "\x00BR" . $#protected . "\x00";
2075 0 $i += 2; next;
2076 }
2077 533 ▶ if ($ch eq "'") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'") 2 531 533
2078 2 my $j = $i + 1;
2079 2 ▶ while ($j < $n && $c[$j] ne "'") { $j++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $c[$j] ne "'") 6 2 8
Condition66%2/3
$j < $n and $c[$j] ne "'"
!ll&&!rl&&r
026
MC/DC50%1/2
$j < $n and $c[$j] ne "'"
$j < $n $c[$j] ne "'"
Truth table66%2/3
A and B
A= $j < $nB= $c[$j] ne "'"
ABresult
0X0
100
111
2080 2 ▶ $j++ if $j < $n;
Branch: 1 branch
BranchTrueFalseTotal
if $j < $n 2 0 2
2081 2 my $seg = join('', @c[$i .. $j-1]);
2082 2 push @protected, $seg;
2083 2 $safe .= "\x00BR" . $#protected . "\x00";
2084 2 $i = $j; next;
2085 }
2086 531 ▶ if ($ch eq '"') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"") 51 480 531
2087 51 my $j = $i + 1;
2088 51 ▶ while ($j < $n && $c[$j] ne '"') {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $c[$j] ne "\"") 435 51 486
Condition66%2/3
$j < $n and $c[$j] ne "\""
!ll&&!rl&&r
051435
MC/DC50%1/2
$j < $n and $c[$j] ne "\""
$j < $n $c[$j] ne "\""
Truth table66%2/3
A and B
A= $j < $nB= $c[$j] ne "\""
ABresult
0X0
100
111
2089 435 ▶ $j += ($c[$j] eq '\\' && $j+1 < $n) ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$c[$j] eq '\\' && $j + 1 < $n ? : 1 434 435
Condition66%2/3
$c[$j] eq "\\" and $j + 1 < $n
!ll&&!rl&&r
43401
MC/DC50%1/2
$c[$j] eq "\\" and $j + 1 < $n
$c[$j] eq "\\" $j + 1 < $n
Truth table66%2/3
A and B
A= $c[$j] eq "\\"B= $j + 1 < $n
ABresult
0X0
100
111
2090 }
2091 51 ▶ $j++ if $j < $n;
Branch: 1 branch
BranchTrueFalseTotal
if $j < $n 51 0 51
2092 51 my $seg = join('', @c[$i .. $j-1]);
2093 51 push @protected, $seg;
2094 51 $safe .= "\x00BR" . $#protected . "\x00";
2095 51 $i = $j; next;
2096 }
2097 480 ▶ if ($ch eq '$' && $i+1 < $n && ($c[$i+1] eq '(' || $c[$i+1] eq '{')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "(" || $c[$i + 1] eq "{") 46 434 480
Condition77%7/9
$ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "(" || $c[$i + 1] eq "{"
!ll&&!rl&&r
434541
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
434046
$c[$i + 1] eq "(" or $c[$i + 1] eq "{"
l!l&&r!l&&!r
5410
MC/DC25%1/4
$ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "(" || $c[$i + 1] eq "{"
$ch eq "\$" $i + 1 < $n $c[$i + 1] eq "(" $c[$i + 1] eq "{"
Truth table40%2/5
A and B and C or D
A= $ch eq "\$"B= $i + 1 < $nC= $c[$i + 1] eq "("D= $c[$i + 1] eq "{"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
2098 46 my $openc = $c[$i+1];
2099 46 ▶ my $closec = ($openc eq '(') ? ')' : '}';
Branch: 1 branch
BranchTrueFalseTotal
$openc eq '(' ? : 5 41 46
2100 46 my $depth = 1;
2101 46 my $j = $i + 2;
2102 46 ▶ while ($j < $n && $depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $depth > 0) 351 46 397
Condition100%3/3
$j < $n and $depth > 0
!ll&&!rl&&r
379351
MC/DC100%2/2
$j < $n and $depth > 0
$j < $n $depth > 0
Truth table100%3/3
A and B
A= $j < $nB= $depth > 0
ABresult
0X0
100
111
2103 351 ▶ if ($c[$j] eq $openc) { $depth++ }
Branch: 2 branches
BranchTrueFalseTotal
if ($c[$j] eq $openc) { } 5 346 351
elsif ($c[$j] eq $closec) { } 51 295 346
2104 51 elsif ($c[$j] eq $closec) { $depth-- }
2105 351 $j++;
2106 }
2107 46 my $seg = join('', @c[$i .. $j-1]);
2108 46 push @protected, $seg;
2109 46 $safe .= "\x00BR" . $#protected . "\x00";
2110 46 $i = $j; next;
2111 }
2112 434 ▶ if (($ch eq '<' || $ch eq '>') && $i+1 < $n && $c[$i+1] eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "<" || $ch eq ">" and $i + 1 < $n and $c[$i + 1] eq "(") 0 434 434
Condition33%3/9
$ch eq "<" || $ch eq ">" and $i + 1 < $n and $c[$i + 1] eq "("
!ll&&!rl&&r
43400
$ch eq "<" || $ch eq ">" and $i + 1 < $n
!ll&&!rl&&r
43400
$ch eq "<" or $ch eq ">"
l!l&&r!l&&!r
00434
MC/DC0%0/4
$ch eq "<" || $ch eq ">" and $i + 1 < $n and $c[$i + 1] eq "("
$ch eq "<" $ch eq ">" $i + 1 < $n $c[$i + 1] eq "("
Truth table14%1/7
A or B and C and D
A= $ch eq "<"B= $ch eq ">"C= $i + 1 < $nD= $c[$i + 1] eq "("
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
2113 0 my $depth = 1;
2114 0 my $j = $i + 2;
2115 0 ▶ while ($j < $n && $depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $depth > 0) 0 0 0
Condition0%0/3
$j < $n and $depth > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$j < $n and $depth > 0
$j < $n $depth > 0
Truth table0%0/3
A and B
A= $j < $nB= $depth > 0
ABresult
0X0
100
111
2116 0 ▶ if ($c[$j] eq '(') { $depth++ }
Branch: 2 branches
BranchTrueFalseTotal
if ($c[$j] eq '(') { } 0 0 0
elsif ($c[$j] eq ')') { } 0 0 0
2117 0 elsif ($c[$j] eq ')') { $depth-- }
2118 0 $j++;
2119 }
2120 0 my $seg = join('', @c[$i .. $j-1]);
2121 0 push @protected, $seg;
2122 0 $safe .= "\x00BR" . $#protected . "\x00";
2123 0 $i = $j; next;
2124 }
2125 434 ▶ if ($ch eq '`') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "`") 0 434 434
2126 0 my $j = $i + 1;
2127 0 ▶ while ($j < $n && $c[$j] ne '`') { $j++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $c[$j] ne "`") 0 0 0
Condition0%0/3
$j < $n and $c[$j] ne "`"
!ll&&!rl&&r
000
MC/DC0%0/2
$j < $n and $c[$j] ne "`"
$j < $n $c[$j] ne "`"
Truth table0%0/3
A and B
A= $j < $nB= $c[$j] ne "`"
ABresult
0X0
100
111
2128 0 ▶ $j++ if $j < $n;
Branch: 1 branch
BranchTrueFalseTotal
if $j < $n 0 0 0
2129 0 my $seg = join('', @c[$i .. $j-1]);
2130 0 push @protected, $seg;
2131 0 $safe .= "\x00BR" . $#protected . "\x00";
2132 0 $i = $j; next;
2133 }
2134  
2135 434 $safe .= $ch; $i++;
2136 }
2137  
2138 81 my @pieces = split /(\s+)/, $safe;
2139 81 my $out = '';
2140 81 for my $w (@pieces) {
2141 229 ▶ if ($w eq '' || $w =~ /\A\s+\z/) { $out .= $w; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($w eq "" or $w =~ /\A\s+\z/) 74 155 229
Condition66%2/3
$w eq "" or $w =~ /\A\s+\z/
l!l&&r!l&&!r
074155
MC/DC50%1/2
$w eq "" or $w =~ /\A\s+\z/
$w eq "" $w =~ /\A\s+\z/
Truth table66%2/3
A or B
A= $w eq ""B= $w =~ /\A\s+\z/
ABresult
1X1
011
000
2142 155 ▶ my @exp = ($w =~ /\{/) ? _brace_expand_word($w) : ($w);
Branch: 1 branch
BranchTrueFalseTotal
$w =~ /\{/ ? : 6 149 155
2143 155 $out .= join(' ', @exp);
2144 }
2145  
2146 81 $out =~ s/\x00BR(\d+)\x00/$protected[$1]/ge;
2147 81 return $out;
2148 }
2149  
2150 # _brace_expand_word: expand all brace groups in one already-protected
2151 # word, returning the list of resulting words (a single-element list
2152 # containing the word unchanged when it holds no valid brace group).
2153 sub _brace_expand_word {
2154 30 ▶ my ($word) = @_;
Subroutine _brace_expand_word: called CC=7 SCAR=19.5
2155 30 ▶ return ($word) unless defined $word && $word =~ /\{/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $word and $word =~ /\{/ 24 6 30
Condition66%2/3
defined $word and $word =~ /\{/
!ll&&!rl&&r
0246
MC/DC50%1/2
defined $word and $word =~ /\{/
defined $word $word =~ /\{/
Truth table66%2/3
A and B
A= defined $wordB= $word =~ /\{/
ABresult
0X0
100
111
2156 6 my @found = _brace_find_group($word, 0);
2157 6 ▶ return ($word) unless @found;
Branch: 1 branch
BranchTrueFalseTotal
unless @found 1 5 6
2158 5 my ($open, $close, $alts_ref) = @found;
2159 5 my $prefix = substr($word, 0, $open);
2160 5 my $suffix = substr($word, $close + 1);
2161 5 my @suffix_words = _brace_expand_word($suffix);
2162 5 my @out;
2163 5 for my $a (@{$alts_ref}) {
2164 19 for my $aw (_brace_expand_word($a)) {
2165 19 for my $sw (@suffix_words) {
2166 19 push @out, $prefix . $aw . $sw;
2167 }
2168 }
2169 }
2170 5 return @out;
2171 }
2172  
2173 # _brace_find_group: locate the first VALID brace group at or after
2174 # $start (a comma-list or a range -- a bare "{word}" with neither is
2175 # left as literal text and scanning continues past it). Returns
2176 # ($open_pos, $close_pos, \@alternatives) or () when none remain.
2177 sub _brace_find_group {
2178 6 ▶ my ($word, $start) = @_;
Subroutine _brace_find_group: called CC=15 SCAR=28.1
2179 6 my @c = split //, $word;
2180 6 my $n = scalar @c;
2181 6 my $i = $start;
2182 6 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 13 1 14
2183 13 ▶ if ($c[$i] eq '\\') { $i += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c[$i] eq "\\") 0 13 13
2184 13 ▶ if ($c[$i] ne '{') { $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c[$i] ne "{") 7 6 13
2185  
2186 6 my $open = $i;
2187 6 my $depth = 1;
2188 6 my $j = $i + 1;
2189 6 my @commas;
2190 6 ▶ while ($j < $n && $depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $depth > 0) 32 6 38
Condition100%3/3
$j < $n and $depth > 0
!ll&&!rl&&r
4232
MC/DC100%2/2
$j < $n and $depth > 0
$j < $n $depth > 0
Truth table100%3/3
A and B
A= $j < $nB= $depth > 0
ABresult
0X0
100
111
2191 32 ▶ if ($c[$j] eq '\\') { $j += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c[$j] eq "\\") 0 32 32
2192 32 ▶ if ($c[$j] eq '{') { $depth++; $j++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c[$j] eq "{") 0 32 32
2193 32 ▶ if ($c[$j] eq '}') { $depth--; $j++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c[$j] eq "}") 6 26 32
2194 26 ▶ if ($c[$j] eq ',' && $depth == 1) { push @commas, $j }
Branch: 1 branch
BranchTrueFalseTotal
if ($c[$j] eq "," and $depth == 1) 2 24 26
Condition66%2/3
$c[$j] eq "," and $depth == 1
!ll&&!rl&&r
2402
MC/DC50%1/2
$c[$j] eq "," and $depth == 1
$c[$j] eq "," $depth == 1
Truth table66%2/3
A and B
A= $c[$j] eq ","B= $depth == 1
ABresult
0X0
100
111
2195 26 $j++;
2196 }
2197 6 ▶ if ($depth != 0) { $i = $open + 1; next } # unmatched '{'
Branch: 1 branch
BranchTrueFalseTotal
if ($depth != 0) 0 6 6
2198  
2199 6 my $close = $j - 1;
2200 6 my @alts;
2201 6 ▶ if (@commas) {
Branch: 1 branch
BranchTrueFalseTotal
if (@commas) { } 1 5 6
2202 1 my $prev = $open + 1;
2203 1 for my $pos (@commas) {
2204 2 push @alts, substr($word, $prev, $pos - $prev);
2205 2 $prev = $pos + 1;
2206 }
2207 1 push @alts, substr($word, $prev, $close - $prev);
2208 }
2209 else {
2210 5 @alts = _brace_expand_range(substr($word, $open + 1, $close - $open - 1));
2211 }
2212 6 ▶ return ($open, $close, \@alts) if @alts;
Branch: 1 branch
BranchTrueFalseTotal
if @alts 5 1 6
2213 1 $i = $open + 1; # not a valid group: keep scanning past it
2214 }
2215 1 return ();
2216 }
2217  
2218 # _brace_expand_range: expand "X..Y" or "X..Y..STEP" -- integer (with
2219 # optional zero-padding taken from the wider operand) or single-letter.
2220 # Returns () when $inner is not a recognised range expression.
2221 sub _brace_expand_range {
2222 5 ▶ my ($inner) = @_;
Subroutine _brace_expand_range: called CC=23 SCAR=33.8
2223 5 ▶ $inner = '' unless defined $inner;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $inner 0 5 5
2224  
2225 5 ▶ if ($inner =~ /\A(-?\d+)\.\.(-?\d+)(?:\.\.(-?\d+))?\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inner =~ /\A(-?\d+)\.\.(-?\d+)(?:\.\.(-?\d+))?\z/) 3 2 5
2226 3 my ($from, $to, $step) = ($1, $2, $3);
2227 3 my $pad = 0;
2228 3 ▶ if ($from =~ /\A-?0\d/ || $to =~ /\A-?0\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from =~ /\A-?0\d/ or $to =~ /\A-?0\d/) 1 2 3
Condition66%2/3
$from =~ /\A-?0\d/ or $to =~ /\A-?0\d/
l!l&&r!l&&!r
102
MC/DC50%1/2
$from =~ /\A-?0\d/ or $to =~ /\A-?0\d/
$from =~ /\A-?0\d/ $to =~ /\A-?0\d/
Truth table66%2/3
A or B
A= $from =~ /\A-?0\d/B= $to =~ /\A-?0\d/
ABresult
1X1
011
000
2229 1 ▶ my $w1 = length($from); $w1-- if substr($from, 0, 1) eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if substr($from, 0, 1) eq "-" 0 1 1
2230 1 ▶ my $w2 = length($to); $w2-- if substr($to, 0, 1) eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if substr($to, 0, 1) eq "-" 0 1 1
2231 1 ▶ $pad = ($w1 > $w2) ? $w1 : $w2;
Branch: 1 branch
BranchTrueFalseTotal
$w1 > $w2 ? : 0 1 1
2232 }
2233 3 ▶ $step = 1 unless defined $step && $step != 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $step and $step != 0 3 0 3
Condition33%1/3
defined $step and $step != 0
!ll&&!rl&&r
300
MC/DC0%0/2
defined $step and $step != 0
defined $step $step != 0
Truth table33%1/3
A and B
A= defined $stepB= $step != 0
ABresult
0X0
100
111
2234 3 ▶ $step = -$step if $step > 0 && $from > $to;
Branch: 1 branch
BranchTrueFalseTotal
if $step > 0 and $from > $to 1 2 3
Condition66%2/3
$step > 0 and $from > $to
!ll&&!rl&&r
021
MC/DC50%1/2
$step > 0 and $from > $to
$step > 0 $from > $to
Truth table66%2/3
A and B
A= $step > 0B= $from > $to
ABresult
0X0
100
111
2235 3 ▶ $step = -$step if $step < 0 && $from < $to;
Branch: 1 branch
BranchTrueFalseTotal
if $step < 0 and $from < $to 0 3 3
Condition66%2/3
$step < 0 and $from < $to
!ll&&!rl&&r
210
MC/DC0%0/2
$step < 0 and $from < $to
$step < 0 $from < $to
Truth table66%2/3
A and B
A= $step < 0B= $from < $to
ABresult
0X0
100
111
2236 3 my @out;
2237 3 ▶ if ($step > 0) { for (my $v=$from; $v<=$to; $v+=$step) { push @out, _brace_pad($v, $pad) } }
Branch: 2 branches
BranchTrueFalseTotal
if ($step > 0) { } 2 1 3
if ($v <= $to) 8 2 10
2238 1 ▶ else { for (my $v=$from; $v>=$to; $v+=$step) { push @out, _brace_pad($v, $pad) } }
Branch: 1 branch
BranchTrueFalseTotal
if ($v >= $to) 5 1 6
2239 3 return @out;
2240 }
2241 2 ▶ if ($inner =~ /\A([A-Za-z])\.\.([A-Za-z])(?:\.\.(-?\d+))?\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inner =~ /\A([A-Za-z])\.\.([A-Za-z])(?:\.\.(-?\d+))?\z/) 1 1 2
2242 1 my ($from, $to, $step) = ($1, $2, $3);
2243 1 ▶ $step = 1 unless defined $step && $step != 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $step and $step != 0 1 0 1
Condition33%1/3
defined $step and $step != 0
!ll&&!rl&&r
100
MC/DC0%0/2
defined $step and $step != 0
defined $step $step != 0
Truth table33%1/3
A and B
A= defined $stepB= $step != 0
ABresult
0X0
100
111
2244 1 $step = abs($step);
2245 1 my ($fo, $to_o) = (ord($from), ord($to));
2246 1 my @out;
2247 1 ▶ if ($fo <= $to_o) { for (my $v=$fo; $v<=$to_o; $v+=$step) { push @out, chr($v) } }
Branch: 2 branches
BranchTrueFalseTotal
if ($fo <= $to_o) { } 1 0 1
if ($v <= $to_o) 3 1 4
2248 0 ▶ else { for (my $v=$fo; $v>=$to_o; $v-=$step) { push @out, chr($v) } }
Branch: 1 branch
BranchTrueFalseTotal
if ($v >= $to_o) 0 0 0
2249 1 return @out;
2250 }
2251 1 return ();
2252 }
2253  
2254 sub _brace_pad {
2255 13 ▶ my ($v, $pad) = @_;
Subroutine _brace_pad: called CC=5 SCAR=16.7
2256 13 ▶ return $v unless $pad;
Branch: 1 branch
BranchTrueFalseTotal
unless $pad 10 3 13
2257 3 ▶ my $neg = ($v < 0) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$v < 0 ? : 0 3 3
2258 3 ▶ my $s = sprintf('%0' . $pad . 'd', $neg ? -$v : $v);
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 0 3 3
2259 3 ▶ return $neg ? "-$s" : $s;
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 0 3 3
2260 }
2261  
2262 # ----------------------------------------------------------------
2263 # cd
2264 # ----------------------------------------------------------------
2265 sub _cmd_cd {
2266 9 ▶ my ($rest) = @_;
Subroutine _cmd_cd: called CC=5 SCAR=17.4
2267 9 $rest =~ s/\A\s+//;
2268 9 $rest =~ s/\s+\z//;
2269 # Strip quotes the same way command words are dequoted, so
2270 # cd "a b" / cd 'dir' / cd "$HOME/x" reach chdir() as a plain path.
2271 9 $rest = _arr_dequote($rest);
2272 9 $rest = BATsh::MB::dec($rest);
2273 9 ▶ if ($rest eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest eq "") 0 9 9
2274 0 ▶ $rest = $ENV{'HOME'} || BATsh::Env->get('HOME') || '.';
Condition0%0/5
$ENV{'HOME'} || 'BATsh::Env'->get('HOME') || '.'
l!l
00
$ENV{"HOME"} or "BATsh::Env"->get("HOME")
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'HOME'} || 'BATsh::Env'->get('HOME') || '.'
$ENV{"HOME"} "BATsh::Env"->get("HOME")
Truth table0%0/3
A or B
A= $ENV{"HOME"}B= "BATsh::Env"->get("HOME")
ABresult
1X1
011
000
2275 }
2276 9 ▶ unless (chdir($rest)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chdir $rest) 1 8 9
2277 1 print STDERR "cd: $rest: No such file or directory\n";
2278 1 $LAST_STATUS = 1;
2279 1 return 1;
2280 }
2281 8 BATsh::Env->set('PWD', BATsh::MB::enc(Cwd::cwd()));
2282 8 $LAST_STATUS = 0;
2283 8 return 0;
2284 }
2285  
2286 # ----------------------------------------------------------------
2287 # exit
2288 # ----------------------------------------------------------------
2289 sub _cmd_exit {
2290 7 ▶ my ($rest) = @_;
Subroutine _cmd_exit: called CC=5 SCAR=16.5
2291 7 $rest =~ s/\A\s+//;
2292 7 ▶ my $code = ($rest =~ /\A(\d+)/) ? int($1) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rest =~ /\A(\d+)/ ? : 7 0 7
2293 # Run the EXIT trap (once) before exiting, while no exit is yet pending so
2294 # the trap body executes. Delete it first to avoid re-entry / double-fire.
2295 7 ▶ if (exists $_SH_TRAP{'EXIT'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $BATsh::SH::_SH_TRAP{"EXIT"}) 2 5 7
2296 2 my $cmd = delete $_SH_TRAP{'EXIT'};
2297 2 ▶ if (defined $cmd && $cmd ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cmd and $cmd ne "") 2 0 2
Condition33%1/3
defined $cmd and $cmd ne ""
!ll&&!rl&&r
002
MC/DC0%0/2
defined $cmd and $cmd ne ""
defined $cmd $cmd ne ""
Truth table33%1/3
A and B
A= defined $cmdB= $cmd ne ""
ABresult
0X0
100
111
2298 2 eval { _run_lines('BATsh::SH', [$cmd], {}) };
2299 }
2300 }
2301 7 $_EXIT_CODE = $code;
2302 7 $LAST_STATUS = $code;
2303 7 return $code;
2304 }
2305  
2306 # ----------------------------------------------------------------
2307 # exec (v0.07)
2308 #
2309 # exec > file / exec 2>&1 / ... apply redirection(s) permanently to
2310 # the current shell (no save/restore)
2311 # exec cmd args... run cmd (with its own redirections,
2312 # if any) then terminate the whole
2313 # script with cmd's exit status
2314 #
2315 # There is no real fork/exec here (this interpreter is Pure Perl and
2316 # never forks -- see the Background Execution / Compound Commands notes
2317 # above); "replacing the shell" is approximated by ending the script
2318 # immediately afterward, and redirections are applied by permanently
2319 # reassigning the STDIN/STDOUT/STDERR bareword globs instead of the
2320 # save-and-restore dance _sh_exec_with_redirs() uses for ordinary
2321 # commands.
2322 # ----------------------------------------------------------------
2323 sub _cmd_exec {
2324 2 ▶ my ($class, $rest, $opts_ref) = @_;
Subroutine _cmd_exec: called CC=12 SCAR=28.9
2325 2 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 2 2
2326 2 my ($clean, $redirs_ref) = _sh_strip_redirects($rest);
2327  
2328 2 for my $r (@{$redirs_ref}) {
2329 1 my ($fd, $append, $file) = @{$r};
2330 1 ▶ $file = BATsh::MB::dec($file) unless $file =~ /\A&[12]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /\A&[12]\z/ 1 0 1
2331 1 my $ok = 1;
2332 1 ▶ if ($fd == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fd == 0) { } 0 1 1
elsif ($fd == 1) { } 1 0 1
2333 # sysopen() takes the filename literally -- a leading '>' or a
2334 # trailing '|' is never treated as a mode or a pipe command.
2335 0 $ok = sysopen(STDIN, $file, O_RDONLY);
2336 }
2337 elsif ($fd == 1) {
2338 1 ▶ $ok = ($file eq '&2')
Branch: 2 branches
BranchTrueFalseTotal
$file eq '&2' ? : 0 1 1
$append ? : 0 1 1
2339 ? open(STDOUT, '>&STDERR')
2340 : sysopen(STDOUT, $file,
2341 O_WRONLY | O_CREAT | ($append ? O_APPEND : O_TRUNC), 0666);
2342 }
2343 else {
2344 0 ▶ $ok = ($file eq '&1')
Branch: 2 branches
BranchTrueFalseTotal
$file eq '&1' ? : 0 0 0
$append ? : 0 0 0
2345 ? open(STDERR, '>&STDOUT')
2346 : sysopen(STDERR, $file,
2347 O_WRONLY | O_CREAT | ($append ? O_APPEND : O_TRUNC), 0666);
2348 }
2349 1 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 1 1
2350 0 warn "sh: exec: $file: $!\n";
2351 0 $LAST_STATUS = 1;
2352 0 return 1;
2353 }
2354 }
2355  
2356 2 $clean =~ s/\A\s+//; $clean =~ s/\s+\z//;
2357 2 ▶ if ($clean eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($clean eq "") 1 1 2
2358 # "exec" with only redirections: they now apply for the rest of
2359 # the script; exec itself does not terminate anything.
2360 1 $LAST_STATUS = 0;
2361 1 return 0;
2362 }
2363  
2364 1 my $rc = _exec_line($class, $clean, $opts_ref);
2365 1 $_EXIT_CODE = $rc; # exec replaces the shell: end the script here
2366 1 $LAST_STATUS = $rc;
2367 1 return $rc;
2368 }
2369  
2370 # ----------------------------------------------------------------
2371 # trap -- signal / event handling (v0.06)
2372 #
2373 # Supported forms:
2374 # trap 'commands' SIGSPEC... register a handler
2375 # trap - SIGSPEC... reset to the default action
2376 # trap '' SIGSPEC... ignore the signal
2377 # trap / trap -p list the current traps
2378 #
2379 # SIGSPEC may be a name (with or without a leading SIG), a number, or the
2380 # EXIT pseudo-signal (also spelled 0). Real OS signals are bridged to
2381 # Perl's %SIG; EXIT is run internally when the script ends or on `exit`.
2382 # The handler command is stored unexpanded and (re-)expanded when it fires.
2383 # ----------------------------------------------------------------
2384 sub _cmd_trap {
2385 17 ▶ my ($class, $rest, $opts_ref) = @_;
Subroutine _cmd_trap: called CC=9 SCAR=22.1
2386 17 my ($mode, $cmd, $sigs) = _sh_parse_trap($rest);
2387  
2388 17 ▶ if ($mode eq 'list') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "list") 1 16 17
2389 1 ▶ my @names = @{$sigs} ? @{$sigs} : sort keys %_SH_TRAP;
Branch: 1 branch
BranchTrueFalseTotal
@{$sigs;} ? : 0 1 1
2390 1 for my $n (@names) {
2391 1 my $sig = _sh_normalize_sig($n);
2392 1 ▶ next unless exists $_SH_TRAP{$sig};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BATsh::SH::_SH_TRAP{$sig} 0 1 1
2393 1 print "trap -- '" . $_SH_TRAP{$sig} . "' $sig\n";
2394 }
2395 1 $LAST_STATUS = 0;
2396 1 return 0;
2397 }
2398  
2399 16 for my $spec (@{$sigs}) {
2400 17 my $sig = _sh_normalize_sig($spec);
2401 17 ▶ next if $sig eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $sig eq "" 0 17 17
2402 17 ▶ if ($mode eq 'reset') {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'reset') { } 2 15 17
elsif ($mode eq 'ignore') { } 1 14 15
2403 2 delete $_SH_TRAP{$sig};
2404 2 _sh_set_os_sig($sig, 'DEFAULT');
2405 }
2406 elsif ($mode eq 'ignore') {
2407 1 $_SH_TRAP{$sig} = '';
2408 1 _sh_set_os_sig($sig, 'IGNORE');
2409 }
2410 else { # set
2411 14 $_SH_TRAP{$sig} = $cmd;
2412 14 _sh_set_os_sig($sig, 'HANDLER');
2413 }
2414 }
2415 16 $LAST_STATUS = 0;
2416 16 return 0;
2417 }
2418  
2419 # Parse the (raw) argument string of a trap command.
2420 # Returns ($mode, $cmd, \@sigs) where $mode is 'list', 'reset', 'ignore'
2421 # or 'set'. For 'set', $cmd is the (still unexpanded) handler command.
2422 sub _sh_parse_trap {
2423 17 ▶ my ($s) = @_;
Subroutine _sh_parse_trap: called CC=11 SCAR=25.3
2424 17 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 17 17
2425 17 $s =~ s/\A\s+//; $s =~ s/\s+\z//;
2426 17 ▶ return ('list', undef, []) if $s eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "" 1 16 17
2427  
2428 16 ▶ if ($s =~ /\A-p\b\s*(.*)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A-p\b\s*(.*)\z/s) 0 16 16
2429 0 my @sigs = grep { length } split /\s+/, $1;
2430 0 return ('list', undef, \@sigs);
2431 }
2432  
2433 16 my ($action, $quoted, $rest);
2434 16 ▶ if ($s =~ /\A'([^']*)'\s*(.*)\z/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($s =~ /\A'([^']*)'\s*(.*)\z/s) { } 14 2 16
elsif ($s =~ /\A"((?:[^"\\]|\\.)*)"\s*(.*)\z/s) { } 0 2 2
2435 14 ($action, $quoted, $rest) = ($1, 1, $2);
2436 }
2437 elsif ($s =~ /\A"((?:[^"\\]|\\.)*)"\s*(.*)\z/s) {
2438 0 ($action, $quoted, $rest) = ($1, 1, $2);
2439 }
2440 else {
2441 2 ($action, $rest) = split /\s+/, $s, 2;
2442 2 $quoted = 0;
2443 2 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 2 2
2444 }
2445 16 my @sigs = grep { length } split /\s+/, $rest;
2446  
2447 16 ▶ return ('reset', undef, \@sigs) if !$quoted && $action eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if not $quoted and $action eq "-" 2 14 16
Condition66%2/3
not $quoted and $action eq "-"
!ll&&!rl&&r
1402
MC/DC50%1/2
not $quoted and $action eq "-"
not $quoted $action eq "-"
Truth table66%2/3
not(A) and B
A= not $quotedB= $action eq "-"
ABresult
0X0
100
111
2448 14 ▶ return ('ignore', '', \@sigs) if $quoted && $action eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $quoted and $action eq "" 1 13 14
Condition66%2/3
$quoted and $action eq ""
!ll&&!rl&&r
0131
MC/DC50%1/2
$quoted and $action eq ""
$quoted $action eq ""
Truth table66%2/3
A and B
A= $quotedB= $action eq ""
ABresult
0X0
100
111
2449 13 return ('set', $action, \@sigs);
2450 }
2451  
2452 # Normalize a signal spec to a bare name: strip a leading SIG, uppercase,
2453 # and map the common signal numbers to names.
2454 sub _sh_normalize_sig {
2455 18 ▶ my ($s) = @_;
Subroutine _sh_normalize_sig: called CC=5 SCAR=16.3
2456 18 ▶ return '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 18 18
2457 18 $s =~ s/\A\s+//; $s =~ s/\s+\z//;
2458 18 ▶ return '' if $s eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "" 0 18 18
2459 18 $s = uc($s);
2460 18 $s =~ s/\ASIG//;
2461 18 ▶ if ($s =~ /\A\d+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A\d+\z/) 1 17 18
2462 1 my %num = (0 => 'EXIT', 1 => 'HUP', 2 => 'INT', 3 => 'QUIT',
2463 6 => 'ABRT', 9 => 'KILL', 13 => 'PIPE', 14 => 'ALRM',
2464 15 => 'TERM');
2465 1 ▶ $s = exists $num{$s + 0} ? $num{$s + 0} : $s;
Branch: 1 branch
BranchTrueFalseTotal
exists $num{$s + 0} ? : 1 0 1
2466 }
2467 18 return $s;
2468 }
2469  
2470 # Bridge a trap to Perl's %SIG. Pseudo-signals (EXIT/ERR/DEBUG/RETURN) are
2471 # handled internally and never touch %SIG. Assignment is eval-guarded so an
2472 # unsupported signal name (e.g. on Windows) degrades quietly.
2473 sub _sh_set_os_sig {
2474 17 ▶ my ($sig, $what) = @_;
Subroutine _sh_set_os_sig: called CC=7 SCAR=21.6
2475 17 ▶ return if $sig eq 'EXIT' || $sig eq 'ERR'
Branch: 1 branch
BranchTrueFalseTotal
if $sig eq "EXIT" or $sig eq "ERR" or $sig eq "DEBUG" or $sig eq "RETURN" 0 10 10
Condition66%6/9
$sig eq "EXIT" or $sig eq "ERR" or $sig eq "DEBUG" or $sig eq "RETURN"
l!l&&r!l&&!r
7010
$sig eq "EXIT" or $sig eq "ERR" or $sig eq "DEBUG"
l!l&&r!l&&!r
7010
$sig eq "EXIT" or $sig eq "ERR"
l!l&&r!l&&!r
7010
MC/DC25%1/4
$sig eq "EXIT" or $sig eq "ERR" or $sig eq "DEBUG" or $sig eq "RETURN"
$sig eq "EXIT" $sig eq "ERR" $sig eq "DEBUG" $sig eq "RETURN"
Truth table40%2/5
A or B or C or D
A= $sig eq "EXIT"B= $sig eq "ERR"C= $sig eq "DEBUG"D= $sig eq "RETURN"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
2476 || $sig eq 'DEBUG' || $sig eq 'RETURN';
2477 # Some signals (e.g. HUP/USR1/USR2) do not exist on every platform --
2478 # notably Windows -- where assigning to %SIG for them emits a harmless
2479 # "No such signal" warning. Suppress just that warning so a portable
2480 # script trapping such a signal stays quiet; all other warnings pass
2481 # through, and the assignment itself still succeeds (best effort).
2482 local $SIG{__WARN__} = sub {
2483 0 ▶ my $w = defined $_[0] ? $_[0] : '';
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
2484 0 ▶ warn $w unless $w =~ /No such signal/;
Branch: 1 branch
BranchTrueFalseTotal
unless $w =~ /No such signal/ 0 0 0
2485 10 };
2486 10 eval {
2487 10 ▶ if ($what eq 'DEFAULT') { $SIG{$sig} = 'DEFAULT' }
Branch: 2 branches
BranchTrueFalseTotal
if ($what eq 'DEFAULT') { } 1 9 10
elsif ($what eq 'IGNORE') { } 1 8 9
2488 1 elsif ($what eq 'IGNORE') { $SIG{$sig} = 'IGNORE' }
2489 8 ▶ else { $SIG{$sig} = sub { _sh_run_trap($sig) } }
Subroutine __ANON__: called CC=1 SCAR=0.0
2490 };
2491 10 return;
2492 }
2493  
2494 # Run the handler command registered for $sig (no-op if unset or 'ignore').
2495 sub _sh_run_trap {
2496 2 ▶ my ($sig) = @_;
Subroutine _sh_run_trap: called CC=3 SCAR=11.8
2497 2 my $cmd = $_SH_TRAP{$sig};
2498 2 ▶ return unless defined $cmd && $cmd ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cmd and $cmd ne "" 0 2 2
Condition33%1/3
defined $cmd and $cmd ne ""
!ll&&!rl&&r
002
MC/DC0%0/2
defined $cmd and $cmd ne ""
defined $cmd $cmd ne ""
Truth table33%1/3
A and B
A= defined $cmdB= $cmd ne ""
ABresult
0X0
100
111
2499 2 eval { _run_lines('BATsh::SH', [$cmd], {}) };
2500 }
2501  
2502 # Run the EXIT trap (if any) exactly once, then clear it. Called by the
2503 # top-level run paths in BATsh.pm when the whole script has finished.
2504 sub fire_exit_trap {
2505 455 ▶ my ($class) = @_;
Subroutine fire_exit_trap: called CC=4 SCAR=14.2
Pod undocumented
2506 455 ▶ return unless exists $_SH_TRAP{'EXIT'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BATsh::SH::_SH_TRAP{"EXIT"} 452 3 455
2507 3 my $cmd = delete $_SH_TRAP{'EXIT'};
2508 3 ▶ return if !defined $cmd || $cmd eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cmd or $cmd eq "" 0 3 3
Condition33%1/3
not defined $cmd or $cmd eq ""
l!l&&r!l&&!r
003
MC/DC0%0/2
not defined $cmd or $cmd eq ""
not defined $cmd $cmd eq ""
Truth table33%1/3
not(A) or B
A= not defined $cmdB= $cmd eq ""
ABresult
1X1
011
000
2509 3 $_EXIT_CODE = undef; # let the trap body run to completion
2510 3 eval { _run_lines('BATsh::SH', [$cmd], {}) };
2511 3 return;
2512 }
2513  
2514 # ----------------------------------------------------------------
2515 # read
2516 # ----------------------------------------------------------------
2517 sub _cmd_read {
2518 27 ▶ my ($rest) = @_;
Subroutine _cmd_read: called CC=10 SCAR=25.1
2519 27 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 27 27
2520 27 $rest =~ s/\A\s+//;
2521 27 $rest =~ s/\s+\z//;
2522  
2523 # Drop option flags such as -r (we always read a raw line); only the
2524 # bareword names that follow are treated as target variables.
2525 27 ▶ my @vars = grep { length && !/\A-/ } split /\s+/, $rest;
Branch: 1 branch
BranchTrueFalseTotal
length $_ and not /\A-/ 27 0 27
2526  
2527 27 my $line = <STDIN>;
2528 27 ▶ $line = BATsh::MB::enc($line) if defined $line;
Branch: 1 branch
BranchTrueFalseTotal
if defined $line 22 5 27
2529 27 ▶ if (!defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 5 22 27
2530 # End of input. POSIX read returns non-zero at EOF so that a
2531 # 'while read VAR; do ...; done < file' loop terminates instead
2532 # of spinning forever.
2533 5 for my $v (@vars) { BATsh::Env->set($v, '') }
2534 5 $LAST_STATUS = 1;
2535 5 return 1;
2536 }
2537 22 chomp $line;
2538  
2539 22 ▶ if (@vars == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@vars == 1) { } 22 0 22
elsif (@vars > 1) { } 0 0 0
2540 22 BATsh::Env->set($vars[0], $line);
2541 }
2542 elsif (@vars > 1) {
2543 0 my @words = split /\s+/, $line, scalar(@vars);
2544 0 for my $i (0 .. $#vars) {
2545 0 ▶ BATsh::Env->set($vars[$i], defined($words[$i]) ? $words[$i] : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $words[$i] ? : 0 0 0
2546 }
2547 }
2548 22 $LAST_STATUS = 0;
2549 22 return 0;
2550 }
2551  
2552 # ----------------------------------------------------------------
2553 # shift
2554 # ----------------------------------------------------------------
2555 sub _cmd_shift {
2556 6 ▶ my ($rest) = @_;
Subroutine _cmd_shift: called CC=12 SCAR=25.0
2557 6 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 6 6
2558 6 $rest =~ s/\A\s+//;
2559  
2560 # Optional /N offset (bash: shift N shifts N positions)
2561 6 my $n_shift = 1;
2562 6 ▶ if ($rest =~ /\A(\d+)\s*\z/) { $n_shift = int($1); $n_shift = 1 if $n_shift < 1 }
Branch: 2 branches
BranchTrueFalseTotal
if ($rest =~ /\A(\d+)\s*\z/) 3 3 6
if $n_shift < 1 0 3 3
2563  
2564 6 for my $step (1 .. $n_shift) {
2565 # Shift BATSH_ARG* (legacy)
2566 12 for my $n (1 .. 8) {
2567 96 my $next = BATsh::Env->get('BATSH_ARG' . ($n + 1));
2568 96 ▶ BATsh::Env->set('BATSH_ARG' . $n, defined($next) ? $next : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $next ? : 80 16 96
2569 }
2570 12 BATsh::Env->set('BATSH_ARG9', '');
2571  
2572 # Shift %1..%9 (used by _expand $1..$9)
2573 12 for my $n (1 .. 8) {
2574 96 my $next = BATsh::Env->get('%' . ($n + 1));
2575 96 ▶ BATsh::Env->set('%' . $n, defined($next) ? $next : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $next ? : 96 0 96
2576 }
2577 12 BATsh::Env->set('%9', '');
2578  
2579 # Rebuild %*
2580 12 my @args;
2581 12 for my $n (1 .. 9) {
2582 42 my $v = BATsh::Env->get("%$n");
2583 42 ▶ last unless defined $v && $v ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and $v ne "" 12 30 42
Condition66%2/3
defined $v and $v ne ""
!ll&&!rl&&r
01230
MC/DC50%1/2
defined $v and $v ne ""
defined $v $v ne ""
Truth table66%2/3
A and B
A= defined $vB= $v ne ""
ABresult
0X0
100
111
2584 30 push @args, $v;
2585 }
2586 12 BATsh::Env->set('%*', join(' ', @args));
2587 }
2588 6 $LAST_STATUS = 0;
2589 6 return 0;
2590 }
2591  
2592 # ----------------------------------------------------------------
2593 # getopts optstring name [arg ...]
2594 # ----------------------------------------------------------------
2595 # POSIX option parser. Called once per option in a loop:
2596 #
2597 # while getopts "ab:c" opt; do
2598 # case $opt in
2599 # a) ... ;;
2600 # b) echo "$OPTARG" ;;
2601 # c) ... ;;
2602 # \?) echo "bad option" >&2 ;;
2603 # esac
2604 # done
2605 # shift $((OPTIND - 1))
2606 #
2607 # Sets the variable named by the second argument to the option letter
2608 # found (or '?' on an unknown option, or ':' on a missing argument when
2609 # the optstring begins with ':'). OPTARG receives an option's argument;
2610 # OPTIND advances to the next argument to be processed. Returns 0 while
2611 # an option was parsed, non-zero when the option list is exhausted.
2612 #
2613 # A leading ':' in optstring selects "silent" error reporting: getopts
2614 # does not print a diagnostic, sets the name variable to ':' for a
2615 # missing argument and '?' for an unknown option, and puts the offending
2616 # letter in OPTARG. Otherwise getopts prints a message to STDERR, sets
2617 # the name variable to '?', and unsets OPTARG.
2618 #
2619 # If no [arg ...] words are given, the positional parameters $1..$9 are
2620 # parsed (BATsh stores them as %1..%9; the same first-empty-terminates
2621 # convention used elsewhere in this module applies, so an intentionally
2622 # empty positional parameter is not representable).
2623 #
2624 # All Perl 5.005_03 compatible: substr/index scanning, no regex features
2625 # beyond \A \z, no prototypes, 2-argument state via package variables.
2626 sub _cmd_getopts {
2627 55 ▶ my ($rest) = @_;
Subroutine _cmd_getopts: called CC=30 SCAR=34.1
2628 55 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 55 55
2629 55 $rest =~ s/\A\s+//;
2630 55 $rest =~ s/\s+\z//;
2631  
2632 # Split off optstring and name; the remainder (if any) are explicit
2633 # args. _parse_args handles quoting/word-splitting consistently with
2634 # the rest of the interpreter.
2635 55 my @words = _parse_args($rest);
2636 55 ▶ if (@words < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@words < 2) 2 53 55
2637 2 print STDERR "getopts: usage: getopts optstring name [arg ...]\n";
2638 2 $LAST_STATUS = 2;
2639 2 return 2;
2640 }
2641 53 my $optstring = shift @words;
2642 53 my $name = shift @words;
2643  
2644 # Argument list: explicit words, else the positional parameters.
2645 53 my @args;
2646 53 ▶ if (@words) {
Branch: 1 branch
BranchTrueFalseTotal
if (@words) { } 39 14 53
2647 39 @args = @words;
2648 }
2649 else {
2650 14 for my $n (1 .. 9) {
2651 60 my $v = BATsh::Env->get("%$n");
2652 60 ▶ $v = BATsh::Env->get("BATSH_ARG$n") unless defined $v && $v ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and $v ne "" 14 46 60
Condition66%2/3
defined $v and $v ne ""
!ll&&!rl&&r
01446
MC/DC50%1/2
defined $v and $v ne ""
defined $v $v ne ""
Truth table66%2/3
A and B
A= defined $vB= $v ne ""
ABresult
0X0
100
111
2653 60 ▶ last unless defined $v && $v ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and $v ne "" 14 46 60
Condition66%2/3
defined $v and $v ne ""
!ll&&!rl&&r
01446
MC/DC50%1/2
defined $v and $v ne ""
defined $v $v ne ""
Truth table66%2/3
A and B
A= defined $vB= $v ne ""
ABresult
0X0
100
111
2654 46 push @args, $v;
2655 }
2656 }
2657  
2658 # Silent-error mode: a leading ':' in the optstring.
2659 53 my $silent = 0;
2660 53 ▶ if (index($optstring, ':') == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($optstring, ":") == 0) 8 45 53
2661 8 $silent = 1;
2662 8 $optstring = substr($optstring, 1);
2663 }
2664  
2665 # OPTIND is 1-based and defaults to 1. If the caller reset it (or it
2666 # has never been set), restart the intra-argument character offset.
2667 53 my $optind = BATsh::Env->get('OPTIND');
2668 53 ▶ $optind = 1 unless defined $optind && $optind =~ /\A\d+\z/ && $optind >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $optind and $optind =~ /\A\d+\z/ and $optind >= 1 0 32 32
Condition66%4/6
defined $optind and $optind =~ /\A\d+\z/ and $optind >= 1
!ll&&!rl&&r
21032
defined $optind and $optind =~ /\A\d+\z/
!ll&&!rl&&r
21032
MC/DC33%1/3
defined $optind and $optind =~ /\A\d+\z/ and $optind >= 1
defined $optind $optind =~ /\A\d+\z/ $optind >= 1
Truth table50%2/4
A and B and C
A= defined $optindB= $optind =~ /\A\d+\z/C= $optind >= 1
ABCresult
0XX0
10X0
1100
1111
2669 53 ▶ if ($optind != $_GETOPTS_LAST_OPTIND) {
Branch: 1 branch
BranchTrueFalseTotal
if ($optind != $BATsh::SH::_GETOPTS_LAST_OPTIND) 23 30 53
2670 23 $_GETOPTS_CHARPOS = 0; # fresh loop, or manual OPTIND change
2671 }
2672  
2673 # End of arguments?
2674 53 ▶ if ($optind > scalar(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($optind > scalar @args) 17 36 53
2675 17 $_GETOPTS_CHARPOS = 0;
2676 17 BATsh::Env->set('OPTIND', $optind);
2677 17 $_GETOPTS_LAST_OPTIND = $optind;
2678 17 $LAST_STATUS = 1;
2679 17 return 1;
2680 }
2681  
2682 36 my $cur = $args[$optind - 1];
2683 36 ▶ $cur = '' unless defined $cur;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cur 0 36 36
2684  
2685 # Starting a new argument: must begin with '-' and not be a bare '-'.
2686 36 ▶ if ($_GETOPTS_CHARPOS == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BATsh::SH::_GETOPTS_CHARPOS == 0) 34 2 36
2687 34 ▶ if (index($cur, '-') != 0 || $cur eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (index($cur, "-") != 0 or $cur eq "-") 4 30 34
Condition66%2/3
index($cur, "-") != 0 or $cur eq "-"
l!l&&r!l&&!r
4030
MC/DC50%1/2
index($cur, "-") != 0 or $cur eq "-"
index($cur, "-") != 0 $cur eq "-"
Truth table66%2/3
A or B
A= index($cur, "-") != 0B= $cur eq "-"
ABresult
1X1
011
000
2688 # Not an option word: stop (leave OPTIND on it).
2689 4 BATsh::Env->set('OPTIND', $optind);
2690 4 $_GETOPTS_LAST_OPTIND = $optind;
2691 4 $LAST_STATUS = 1;
2692 4 return 1;
2693 }
2694 30 ▶ if ($cur eq '--') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur eq "--") 1 29 30
2695 # Explicit end of options: consume it and stop.
2696 1 $optind++;
2697 1 BATsh::Env->set('OPTIND', $optind);
2698 1 $_GETOPTS_LAST_OPTIND = $optind;
2699 1 $_GETOPTS_CHARPOS = 0;
2700 1 $LAST_STATUS = 1;
2701 1 return 1;
2702 }
2703 29 $_GETOPTS_CHARPOS = 1; # skip the leading '-'
2704 }
2705  
2706 31 my $optchar = substr($cur, $_GETOPTS_CHARPOS, 1);
2707 31 $_GETOPTS_CHARPOS++;
2708 31 my $is_last_char = ($_GETOPTS_CHARPOS >= length($cur));
2709  
2710 # Advance OPTIND past $cur once its last option character is consumed.
2711 my $advance = sub {
2712 20 ▶ $optind++;
Subroutine __ANON__: called CC=1 SCAR=0.0
2713 20 $_GETOPTS_CHARPOS = 0;
2714 31 };
2715  
2716 # Look the option character up in the optstring. ':' and '?' are
2717 # never valid option letters.
2718 31 ▶ my $pos = ($optchar eq ':' || $optchar eq '?') ? -1
Branch: 1 branch
BranchTrueFalseTotal
$optchar eq ':' || $optchar eq '?' ? : 0 31 31
Condition33%1/3
$optchar eq ":" or $optchar eq "?"
l!l&&r!l&&!r
0031
MC/DC0%0/2
$optchar eq ":" or $optchar eq "?"
$optchar eq ":" $optchar eq "?"
Truth table33%1/3
A or B
A= $optchar eq ":"B= $optchar eq "?"
ABresult
1X1
011
000
2719 : index($optstring, $optchar);
2720  
2721 31 ▶ if ($pos < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < 0) 4 27 31
2722 # Unknown option.
2723 4 ▶ if ($silent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($silent) { } 2 2 4
2724 2 BATsh::Env->set($name, '?');
2725 2 BATsh::Env->set('OPTARG', $optchar);
2726 }
2727 else {
2728 2 print STDERR BATsh::MB::dec("getopts: illegal option -- $optchar") . "\n";
2729 2 BATsh::Env->set($name, '?');
2730 2 BATsh::Env->set('OPTARG', '');
2731 }
2732 4 ▶ $advance->() if $is_last_char;
Branch: 1 branch
BranchTrueFalseTotal
if $is_last_char 4 0 4
2733 4 BATsh::Env->set('OPTIND', $optind);
2734 4 $_GETOPTS_LAST_OPTIND = $optind;
2735 4 $LAST_STATUS = 0;
2736 4 return 0;
2737 }
2738  
2739 # Does this option take an argument? (a ':' follows it in optstring)
2740 27 my $needs_arg = (substr($optstring, $pos + 1, 1) eq ':');
2741  
2742 27 ▶ if ($needs_arg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needs_arg) { } 15 12 27
2743 15 ▶ if (!$is_last_char) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_last_char) { } 2 13 15
2744 # -oVALUE : remainder of the current word is the argument.
2745 2 BATsh::Env->set('OPTARG', substr($cur, $_GETOPTS_CHARPOS));
2746 2 BATsh::Env->set($name, $optchar);
2747 2 $advance->();
2748 }
2749 else {
2750 # -o VALUE : the next word is the argument.
2751 13 ▶ if ($optind + 1 > scalar(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($optind + 1 > scalar @args) 4 9 13
2752 # Missing argument.
2753 4 $advance->();
2754 4 ▶ if ($silent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($silent) { } 2 2 4
2755 2 BATsh::Env->set($name, ':');
2756 2 BATsh::Env->set('OPTARG', $optchar);
2757 }
2758 else {
2759 2 print STDERR BATsh::MB::dec("getopts: option requires an argument -- $optchar") . "\n";
2760 2 BATsh::Env->set($name, '?');
2761 2 BATsh::Env->set('OPTARG', '');
2762 }
2763 4 BATsh::Env->set('OPTIND', $optind);
2764 4 $_GETOPTS_LAST_OPTIND = $optind;
2765 4 $LAST_STATUS = 0;
2766 4 return 0;
2767 }
2768 9 BATsh::Env->set('OPTARG', $args[$optind]); # 0-based next word
2769 9 BATsh::Env->set($name, $optchar);
2770 9 $optind += 2;
2771 9 $_GETOPTS_CHARPOS = 0;
2772 }
2773 }
2774 else {
2775 # Flag option, no argument. bash unsets OPTARG; we clear it.
2776 12 BATsh::Env->set($name, $optchar);
2777 12 BATsh::Env->set('OPTARG', '');
2778 12 ▶ $advance->() if $is_last_char;
Branch: 1 branch
BranchTrueFalseTotal
if $is_last_char 10 2 12
2779 }
2780  
2781 23 BATsh::Env->set('OPTIND', $optind);
2782 23 $_GETOPTS_LAST_OPTIND = $optind;
2783 23 $LAST_STATUS = 0;
2784 23 return 0;
2785 }
2786  
2787 # ----------------------------------------------------------------
2788 # local
2789 # ----------------------------------------------------------------
2790 sub _cmd_local {
2791 2 ▶ my ($rest) = @_;
Subroutine _cmd_local: called CC=7 SCAR=20.2
2792 2 $rest =~ s/\A\s+//;
2793  
2794 2 my ($var, $val);
2795 2 ▶ if ($rest =~ /\A([A-Za-z_][A-Za-z0-9_]*)=(.*)\z/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rest =~ /\A([A-Za-z_][A-Za-z0-9_]*)=(.*)\z/s) { } 1 1 2
elsif ($rest =~ /\A([A-Za-z_][A-Za-z0-9_]*)\s*\z/) { } 1 0 1
2796 1 ($var, $val) = ($1, $2);
2797 # Strip surrounding quotes from value
2798 1 $val =~ s/\A"(.*)"\z/$1/s;
2799 1 $val =~ s/\A'(.*)'\z/$1/s;
2800 }
2801 elsif ($rest =~ /\A([A-Za-z_][A-Za-z0-9_]*)\s*\z/) {
2802 1 $var = $1;
2803 1 $val = BATsh::Env->get($var);
2804 1 ▶ $val = '' unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 1 1
2805 }
2806 else {
2807 0 $LAST_STATUS = 0;
2808 0 return 0;
2809 }
2810  
2811 # Save old value in innermost function scope so it can be restored on return
2812 2 ▶ if (@FUNCTION_STACK) {
Branch: 1 branch
BranchTrueFalseTotal
if (@FUNCTION_STACK) 2 0 2
2813 2 my $frame = $FUNCTION_STACK[-1];
2814 # Only save once per variable per frame (first local declaration wins)
2815 2 ▶ unless (exists $frame->{$var}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $frame->{$var}) 2 0 2
2816 2 my $old = BATsh::Env->get($var);
2817 2 ▶ $frame->{$var} = defined $old ? $old : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $old ? : 2 0 2
2818 }
2819 }
2820 2 BATsh::Env->set($var, $val);
2821 2 $LAST_STATUS = 0;
2822 2 return 0;
2823 }
2824  
2825 # ----------------------------------------------------------------
2826 # set (sh set options -- minimal implementation)
2827 # ----------------------------------------------------------------
2828 # _sh_set_positional: replace $1..$9 / $@ / $* with a new argument list.
2829 # The interpreter keeps the positional parameters in %1..%9 (mirrored in
2830 # BATSH_ARG1..9 for the legacy CMD-side view) and their space-joined form
2831 # in %*, exactly as a function call and "shift" do, so "set --" reuses
2832 # that representation instead of inventing a second one. OPTIND is
2833 # deliberately NOT reset: bash leaves it alone too, and a script that
2834 # re-parses a fresh list resets it itself.
2835 sub _sh_set_positional {
2836 12 ▶ my ($args_ref) = @_;
Subroutine _sh_set_positional: called CC=3 SCAR=11.0
2837 12 my @args = @{$args_ref};
2838 12 for my $n (1 .. 9) {
2839 108 ▶ my $v = defined $args[$n-1] ? $args[$n-1] : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $args[$n - 1] ? : 27 81 108
2840 108 BATsh::Env->set("%$n", $v);
2841 108 BATsh::Env->set("BATSH_ARG$n", $v);
2842 }
2843 12 BATsh::Env->set('%*', join(' ', @args));
2844 12 return 1;
2845 }
2846  
2847 sub _cmd_set_sh {
2848 27 ▶ my ($rest) = @_;
Subroutine _cmd_set_sh: called CC=16 SCAR=28.3
2849 27 $rest =~ s/\A\s+//;
2850 27 $rest =~ s/\s+\z//;
2851 # set -e/+e -u/+u -x/+x, combinable (-eux), and set -o/+o NAME
2852 # (errexit / nounset / xtrace). Unknown option letters are accepted
2853 # silently.
2854 #
2855 # POSIX operand handling (v0.09): "set -- [ARG ...]" replaces the
2856 # positional parameters with ARG ... (and clears them when no ARG is
2857 # given), and so does a first word that is not an option, as in
2858 # "set a b c". Until v0.09 every operand form was silently ignored,
2859 # so the common
2860 # set -- -f value ; while getopts f: opt ; do ... done
2861 # idiom saw an empty argument list. Words are split on unquoted
2862 # whitespace and dequoted, so "set -- 'a b' c" sets exactly two
2863 # parameters.
2864 27 my @words = map { _arr_dequote($_) } _arr_split_words($rest);
2865 27 my @pos = ();
2866 27 my $set_pos = 0;
2867 27 ▶ while (@words) {
Branch: 1 branch
BranchTrueFalseTotal
if (@words) 28 15 43
2868 28 my $w = shift @words;
2869 28 ▶ if ($w eq '--') {
Branch: 1 branch
BranchTrueFalseTotal
if ($w eq "--") 11 17 28
2870 11 $set_pos = 1;
2871 11 @pos = @words;
2872 11 @words = ();
2873 11 last;
2874 }
2875  
2876 17 ▶ if ($w =~ /\A([-+])o\z/ && @words) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w =~ /\A([-+])o\z/ and @words) 1 16 17
Condition66%2/3
$w =~ /\A([-+])o\z/ and @words
!ll&&!rl&&r
1601
MC/DC50%1/2
$w =~ /\A([-+])o\z/ and @words
$w =~ /\A([-+])o\z/ @words
Truth table66%2/3
A and B
A= $w =~ /\A([-+])o\z/B= @words
ABresult
0X0
100
111
2877 1 ▶ my $on = ($1 eq '-') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '-' ? : 1 0 1
2878 1 my $name = lc(shift @words);
2879 1 ▶ if ($name eq 'errexit') { $_OPT_ERREXIT = $on }
Branch: 3 branches
BranchTrueFalseTotal
if ($name eq 'errexit') { } 1 0 1
elsif ($name eq 'nounset') { } 0 0 0
elsif ($name eq 'xtrace') { } 0 0 0
2880 0 elsif ($name eq 'nounset') { $_OPT_NOUNSET = $on }
2881 0 elsif ($name eq 'xtrace') { $_OPT_XTRACE = $on }
2882 1 next;
2883 }
2884 16 ▶ if ($w =~ /\A([-+])([a-zA-Z]+)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w =~ /\A([-+])([a-zA-Z]+)\z/) 15 1 16
2885 15 ▶ my $on = ($1 eq '-') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '-' ? : 13 2 15
2886 15 for my $ch (split //, $2) {
2887 15 ▶ if ($ch eq 'e') { $_OPT_ERREXIT = $on }
Branch: 3 branches
BranchTrueFalseTotal
if ($ch eq 'e') { } 11 4 15
elsif ($ch eq 'u') { } 2 2 4
elsif ($ch eq 'x') { } 2 0 2
2888 2 elsif ($ch eq 'u') { $_OPT_NOUNSET = $on }
2889 2 elsif ($ch eq 'x') { $_OPT_XTRACE = $on }
2890 }
2891 15 next;
2892 }
2893 # Not an option: this word and every word after it are the new
2894 # positional parameters.
2895 1 $set_pos = 1;
2896 1 @pos = ($w, @words);
2897 1 @words = ();
2898 1 last;
2899 }
2900 27 ▶ _sh_set_positional(\@pos) if $set_pos;
Branch: 1 branch
BranchTrueFalseTotal
if $set_pos 12 15 27
2901 27 $LAST_STATUS = 0;
2902 27 return 0;
2903 }
2904  
2905 # ----------------------------------------------------------------
2906 # shopt -- bash shell option toggle (v0.07, minimal implementation)
2907 #
2908 # Recognised forms:
2909 # shopt -s extglob enable extended pattern matching operators
2910 # ?(...) *(...) +(...) @(...) !(...) in case
2911 # patterns and in ${VAR#pat}/${VAR%pat}/... patterns
2912 # shopt -u extglob disable it (the bash default)
2913 # shopt -p extglob print "shopt -s extglob" or "shopt -u extglob"
2914 # shopt extglob print "extglob on" / "extglob off"
2915 # shopt print the state of all known options
2916 #
2917 # Only "extglob" is modelled; any other option name is accepted silently
2918 # (queried as "off") so scripts that probe unrelated options do not abort.
2919 # ----------------------------------------------------------------
2920 sub _cmd_shopt {
2921 2 ▶ my ($rest) = @_;
Subroutine _cmd_shopt: called CC=15 SCAR=42.1
2922 2 $rest =~ s/\A\s+//; $rest =~ s/\s+\z//;
2923 2 my @words = split /\s+/, $rest;
2924 2 ▶ if (!@words) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@words) 0 2 2
2925 0 ▶ print "extglob " . ($_OPT_EXTGLOB ? 'on' : 'off') . "\n";
Branch: 1 branch
BranchTrueFalseTotal
$_OPT_EXTGLOB ? : 0 0 0
2926 0 $LAST_STATUS = 0;
2927 0 return 0;
2928 }
2929 2 my $mode = ''; # '-s' / '-u' / '-p' / '' (bare query)
2930 2 ▶ if ($words[0] =~ /\A-[sup]\z/) { $mode = shift(@words) }
Branch: 1 branch
BranchTrueFalseTotal
if ($words[0] =~ /\A-[sup]\z/) 2 0 2
2931 2 ▶ if (!@words) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@words) 0 2 2
2932 # "shopt -p" / "shopt -s" / "shopt -u" with no names: report/no-op
2933 0 ▶ if ($mode eq '-p') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "-p") 0 0 0
2934 0 ▶ print 'shopt -' . ($_OPT_EXTGLOB ? 's' : 'u') . " extglob\n";
Branch: 1 branch
BranchTrueFalseTotal
$_OPT_EXTGLOB ? : 0 0 0
2935 }
2936 0 $LAST_STATUS = 0;
2937 0 return 0;
2938 }
2939 2 my $status = 0;
2940 2 for my $name (@words) {
2941 2 my $lc_name = lc($name);
2942 2 ▶ if ($lc_name ne 'extglob') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_name ne "extglob") 0 2 2
2943 # Unknown option name: treated as always-off, matching bash's
2944 # exit-status-1-on-unknown-name behaviour without aborting.
2945 0 ▶ if ($mode eq '') { print "$name off\n" }
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "") 0 0 0
2946 0 $status = 1;
2947 0 next;
2948 }
2949 2 ▶ if ($mode eq '-s') { $_OPT_EXTGLOB = 1 }
Branch: 3 branches
BranchTrueFalseTotal
if ($mode eq '-s') { } 2 0 2
elsif ($mode eq '-u') { } 0 0 0
elsif ($mode eq '-p') { } 0 0 0
2950 0 elsif ($mode eq '-u') { $_OPT_EXTGLOB = 0 }
2951 0 ▶ elsif ($mode eq '-p') { print 'shopt -' . ($_OPT_EXTGLOB ? 's' : 'u') . " extglob\n" }
Branch: 1 branch
BranchTrueFalseTotal
$_OPT_EXTGLOB ? : 0 0 0
2952 0 ▶ else { print "extglob " . ($_OPT_EXTGLOB ? 'on' : 'off') . "\n" }
Branch: 1 branch
BranchTrueFalseTotal
$_OPT_EXTGLOB ? : 0 0 0
2953 }
2954 2 $LAST_STATUS = $status;
2955 2 return $status;
2956 }
2957  
2958 # ----------------------------------------------------------------
2959 # alias / unalias (v0.07, minimal implementation)
2960 #
2961 # alias list all aliases as alias NAME='VALUE'
2962 # alias NAME print one alias, or an error if not set
2963 # alias NAME=VALUE ... define one or more aliases (quote-aware)
2964 # unalias NAME ... remove one or more aliases
2965 # unalias -a remove all aliases
2966 # ----------------------------------------------------------------
2967 sub _cmd_alias {
2968 6 ▶ my ($rest) = @_;
Subroutine _cmd_alias: called CC=8 SCAR=27.7
2969 6 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 6 6
2970 6 $rest =~ s/\A\s+//; $rest =~ s/\s+\z//;
2971 6 ▶ if ($rest eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest eq "") 0 6 6
2972 0 for my $name (sort keys %_SH_ALIAS) {
2973 0 print "alias $name='" . $_SH_ALIAS{$name} . "'\n";
2974 }
2975 0 $LAST_STATUS = 0;
2976 0 return 0;
2977 }
2978 6 my @tok = _arr_split_words($rest);
2979 6 my $status = 0;
2980 6 for my $t (@tok) {
2981 6 ▶ if ($t =~ /\A([A-Za-z_][A-Za-z0-9_.:-]*)=(.*)\z/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t =~ /\A([A-Za-z_][A-Za-z0-9_.:-]*)=(.*)\z/s) { } 6 0 6
elsif ($t =~ /\A([A-Za-z_][A-Za-z0-9_.:-]*)\z/) { } 0 0 0
2982 6 my ($name, $val) = ($1, $2);
2983 6 $_SH_ALIAS{$name} = _arr_dequote($val);
2984 }
2985 elsif ($t =~ /\A([A-Za-z_][A-Za-z0-9_.:-]*)\z/) {
2986 0 my $name = $1;
2987 0 ▶ if (exists $_SH_ALIAS{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $BATsh::SH::_SH_ALIAS{$name}) { } 0 0 0
2988 0 print "alias $name='" . $_SH_ALIAS{$name} . "'\n";
2989 }
2990 else {
2991 0 print STDERR "sh: alias: $name: not found\n";
2992 0 $status = 1;
2993 }
2994 }
2995 }
2996 6 $LAST_STATUS = $status;
2997 6 return $status;
2998 }
2999  
3000 sub _cmd_unalias {
3001 1 ▶ my ($rest) = @_;
Subroutine _cmd_unalias: called CC=6 SCAR=20.4
3002 1 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 1 1
3003 1 $rest =~ s/\A\s+//; $rest =~ s/\s+\z//;
3004 1 ▶ if ($rest eq '-a') { %_SH_ALIAS = (); $LAST_STATUS = 0; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($rest eq "-a") 0 1 1
3005 1 my $status = 0;
3006 1 for my $name (split /\s+/, $rest) {
3007 1 ▶ next if $name eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "" 0 1 1
3008 1 ▶ if (exists $_SH_ALIAS{$name}) { delete $_SH_ALIAS{$name} }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $BATsh::SH::_SH_ALIAS{$name}) { } 1 0 1
3009 0 else { print STDERR "sh: unalias: $name: not found\n"; $status = 1 }
3010 }
3011 1 $LAST_STATUS = $status;
3012 1 return $status;
3013 }
3014  
3015 # _alias_expand_line: replace a leading alias name with its stored text.
3016 # See the call site in _exec_line() for the chaining / loop-guard notes.
3017 sub _alias_expand_line {
3018 1145 ▶ my ($line) = @_;
Subroutine _alias_expand_line: called CC=6 SCAR=18.0
3019 1145 ▶ return $line unless %_SH_ALIAS;
Branch: 1 branch
BranchTrueFalseTotal
unless %BATsh::SH::_SH_ALIAS 1105 40 1145
3020 40 my %seen;
3021 40 my $out = $line;
3022 40 my $guard = 0;
3023 40 ▶ while ($guard < 20) {
Branch: 1 branch
BranchTrueFalseTotal
if ($guard < 20) 43 0 43
3024 43 $guard++;
3025 43 ▶ last unless $out =~ /\A(\s*)(\S+)(.*)\z/s;
Branch: 1 branch
BranchTrueFalseTotal
unless $out =~ /\A(\s*)(\S+)(.*)\z/s 0 43 43
3026 43 my ($lead, $w0, $tail) = ($1, $2, $3);
3027 43 ▶ last unless exists $_SH_ALIAS{$w0};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BATsh::SH::_SH_ALIAS{$w0} 40 3 43
3028 3 ▶ last if $seen{$w0};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$w0} 0 3 3
3029 3 $seen{$w0} = 1;
3030 3 $out = $lead . $_SH_ALIAS{$w0} . $tail;
3031 }
3032 40 return $out;
3033 }
3034  
3035 # ----------------------------------------------------------------
3036 # reset_sh_options: restore default shell options. Called by BATsh.pm
3037 # at the start of each top-level run so one script's "set -e" does not
3038 # leak into the next. Call as a plain function.
3039 # ----------------------------------------------------------------
3040 sub reset_sh_options {
3041 455 ▶ $_OPT_ERREXIT = 0;
Subroutine reset_sh_options: called CC=1 SCAR=0.0
Pod undocumented
3042 455 $_OPT_NOUNSET = 0;
3043 455 $_OPT_XTRACE = 0;
3044 455 $_OPT_EXTGLOB = 0;
3045 455 $_ERREXIT_HOLD = 0;
3046 455 $_ERREXIT_DONE = 0;
3047 # getopts loop state (v0.07): a fresh top-level run restarts option
3048 # parsing from the first argument, so one script's half-finished
3049 # getopts loop cannot leak its position into the next run.
3050 455 $_GETOPTS_CHARPOS = 0;
3051 455 $_GETOPTS_LAST_OPTIND = 0;
3052 # Variable attributes (v0.08) are script-scoped, like the options
3053 # above: a fresh top-level run starts with no readonly / integer
3054 # markings so one script's "readonly X" cannot leak into the next.
3055 455 %_SH_READONLY = ();
3056 455 %_SH_INTATTR = ();
3057 455 return 0;
3058 }
3059  
3060 # ----------------------------------------------------------------
3061 # _errexit_check: under set -e, a failing simple command terminates the
3062 # script with its status. Suppressed while $_ERREXIT_HOLD > 0 (an if/
3063 # while/until condition, or a non-final member of a && / || list) and
3064 # when a control transfer (exit/break/continue/return) is already active.
3065 # ----------------------------------------------------------------
3066 sub _errexit_check {
3067 1005 ▶ my ($rc) = @_;
Subroutine _errexit_check: called CC=10 SCAR=25.9
3068 1005 ▶ return $rc unless $_OPT_ERREXIT;
Branch: 1 branch
BranchTrueFalseTotal
unless $_OPT_ERREXIT 982 23 1005
3069 23 ▶ return $rc if $_ERREXIT_HOLD;
Branch: 1 branch
BranchTrueFalseTotal
if $_ERREXIT_HOLD 1 22 23
3070 22 ▶ return $rc unless defined $rc && $rc =~ /\A-?\d+\z/ && $rc != 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rc and $rc =~ /\A-?\d+\z/ and $rc != 0 15 7 22
Condition50%3/6
defined $rc and $rc =~ /\A-?\d+\z/ and $rc != 0
!ll&&!rl&&r
0157
defined $rc and $rc =~ /\A-?\d+\z/
!ll&&!rl&&r
0022
MC/DC33%1/3
defined $rc and $rc =~ /\A-?\d+\z/ and $rc != 0
defined $rc $rc =~ /\A-?\d+\z/ $rc != 0
Truth table50%2/4
A and B and C
A= defined $rcB= $rc =~ /\A-?\d+\z/C= $rc != 0
ABCresult
0XX0
10X0
1100
1111
3071 7 ▶ return $rc if defined $_EXIT_CODE || $_BREAK || $_CONTINUE || $_RETURN;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_EXIT_CODE or $_BREAK or $_CONTINUE or $_RETURN 0 7 7
Condition33%3/9
defined $_EXIT_CODE or $_BREAK or $_CONTINUE or $_RETURN
l!l&&r!l&&!r
007
defined $_EXIT_CODE or $_BREAK or $_CONTINUE
l!l&&r!l&&!r
007
defined $_EXIT_CODE or $_BREAK
l!l&&r!l&&!r
007
MC/DC0%0/4
defined $_EXIT_CODE or $_BREAK or $_CONTINUE or $_RETURN
defined $_EXIT_CODE $_BREAK $_CONTINUE $_RETURN
Truth table20%1/5
A or B or C or D
A= defined $_EXIT_CODEB= $_BREAKC= $_CONTINUED= $_RETURN
ABCDresult
1XXX1
01XX1
001X1
00011
00000
3072 7 $_EXIT_CODE = $rc;
3073 7 $LAST_STATUS = $rc;
3074 7 return $rc;
3075 }
3076  
3077 # ----------------------------------------------------------------
3078 # _nounset_hit: under set -u, expanding an unset variable is an error
3079 # that terminates the script with status 1 (bash prints the message and
3080 # aborts; here the current command still completes with '' before the
3081 # script stops -- see BUGS AND LIMITATIONS). A noop while -u is off.
3082 # ----------------------------------------------------------------
3083 sub _nounset_hit {
3084 2 ▶ my ($name) = @_;
Subroutine _nounset_hit: called CC=3 SCAR=11.0
3085 2 ▶ return 0 unless $_OPT_NOUNSET;
Branch: 1 branch
BranchTrueFalseTotal
unless $_OPT_NOUNSET 1 1 2
3086 1 ▶ return 0 if defined $_EXIT_CODE; # report only the first hit
Branch: 1 branch
BranchTrueFalseTotal
if defined $_EXIT_CODE 0 1 1
3087 1 warn "sh: $name: unbound variable\n";
3088 1 $_EXIT_CODE = 1;
3089 1 $LAST_STATUS = 1;
3090 1 return 1;
3091 }
3092  
3093 # ----------------------------------------------------------------
3094 # source / .
3095 # ----------------------------------------------------------------
3096 sub _cmd_source {
3097 0 ▶ my ($class, $rest, $opts_ref) = @_;
Subroutine _cmd_source: not called CC=3 SCAR=24.8
3098 0 $rest =~ s/\A\s+//;
3099 0 $rest =~ s/\s+\z//;
3100 0 ▶ if (defined $opts_ref->{'_batsh'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts_ref->{"_batsh"}) 0 0 0
3101 0 eval { $opts_ref->{'_batsh'}->source_file($rest) };
3102 0 ▶ if ($@) { print STDERR "source: $rest: $@\n"; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
3103 }
3104 0 return 0;
3105 }
3106  
3107 # ----------------------------------------------------------------
3108 # test / [ ]
3109 # ----------------------------------------------------------------
3110 sub _cmd_test {
3111 31 ▶ my ($rest) = @_;
Subroutine _cmd_test: called CC=2 SCAR=6.9
3112 31 $rest =~ s/\A\s+//;
3113 31 $rest =~ s/\s*\]\s*\z//; # strip trailing ]
3114 31 my $result = _eval_test($rest);
3115 31 ▶ $LAST_STATUS = $result ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$result ? : 22 9 31
3116 31 return $LAST_STATUS;
3117 }
3118  
3119 sub _eval_test {
3120 31 ▶ my ($expr) = @_;
Subroutine _eval_test: called CC=59 SCAR=63.2
3121 31 $expr =~ s/\A\s+//;
3122 31 $expr =~ s/\s+\z//;
3123  
3124 # Compound: -a (AND), -o (OR)
3125 31 ▶ if ($expr =~ /^(.*)\s+-a\s+(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.*)\s+-a\s+(.*)$/) 0 31 31
3126 0 ▶ return _eval_test($1) && _eval_test($2);
Condition0%0/3
_eval_test($1) && _eval_test($2)
!ll&&!rl&&r
000
MC/DC0%0/2
_eval_test($1) && _eval_test($2)
_eval_test($1) _eval_test($2)
Truth table0%0/3
A && B
A= _eval_test($1)B= _eval_test($2)
ABresult
0X0
100
111
3127 }
3128 31 ▶ if ($expr =~ /^(.*)\s+-o\s+(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.*)\s+-o\s+(.*)$/) 0 31 31
3129 0 ▶ return _eval_test($1) || _eval_test($2);
Condition0%0/3
_eval_test($1) || _eval_test($2)
l!l&&r!l&&!r
000
MC/DC0%0/2
_eval_test($1) || _eval_test($2)
_eval_test($1) _eval_test($2)
Truth table0%0/3
A || B
A= _eval_test($1)B= _eval_test($2)
ABresult
1X1
011
000
3130 }
3131 # Negation
3132 31 ▶ if ($expr =~ /^!\s+(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^!\s+(.*)$/) 0 31 31
3133 0 return !_eval_test($1);
3134 }
3135  
3136 # File tests
3137 31 ▶ if ($expr =~ /\A(-[a-z])\s+(.+)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /\A(-[a-z])\s+(.+)\z/) 5 26 31
3138 5 my ($op, $path) = ($1, $2);
3139 # Quote removal (which is also what turns a backslash that came
3140 # out of an expansion back into a literal one) follows the same
3141 # rules as for command words: test compares its operands
3142 # literally, so a half-stripped "C:\dir"/sub would never name
3143 # an existing file.
3144 5 $path = _arr_dequote($path);
3145 5 $path = BATsh::MB::dec($path);
3146 5 ▶ if ($op eq '-e') { return -e $path ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-e") 0 5 5
-e $path ? : 0 0 0
3147 5 ▶ if ($op eq '-f') { return -f $path ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-f") 1 4 5
-f $path ? : 1 0 1
3148 4 ▶ if ($op eq '-d') { return -d $path ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-d") 1 3 4
-d $path ? : 1 0 1
3149 3 ▶ if ($op eq '-r') { return -r $path ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-r") 0 3 3
-r $path ? : 0 0 0
3150 3 ▶ if ($op eq '-w') { return -w $path ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-w") 0 3 3
-w $path ? : 0 0 0
3151 3 ▶ if ($op eq '-x') { return -x $path ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-x") 0 3 3
-x $path ? : 0 0 0
3152 3 ▶ if ($op eq '-s') { return (-s $path) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-s") 0 3 3
-s $path ? : 0 0 0
3153 3 ▶ if ($op eq '-z') { my $s = -s $path; return (!defined $s || $s == 0) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-z") 2 1 3
!defined($s) || $s == 0 ? : 2 0 2
Condition33%1/3
not defined $s or $s == 0
l!l&&r!l&&!r
200
MC/DC0%0/2
not defined $s or $s == 0
not defined $s $s == 0
Truth table33%1/3
not(A) or B
A= not defined $sB= $s == 0
ABresult
1X1
011
000
3154 1 ▶ if ($op eq '-n') { return (length($path) > 0) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-n") 1 0 1
length $path > 0 ? : 1 0 1
3155 0 ▶ if ($op eq '-L') { return -l $path ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-L") 0 0 0
-l $path ? : 0 0 0
3156 }
3157  
3158 # String comparisons: = == != < >
3159 26 ▶ if ($expr =~ /\A(.+?)\s+(=|==|!=|<|>)\s+(.+)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /\A(.+?)\s+(=|==|!=|<|>)\s+(.+)\z/) 2 24 26
3160 2 my ($a, $op, $b) = ($1, $2, $3);
3161 2 $a = _arr_dequote($a);
3162 2 $b = _arr_dequote($b);
3163 2 ▶ if ($op eq '=' || $op eq '==') { return ($a eq $b) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "=" or $op eq "==") 2 0 2
$a eq $b ? : 2 0 2
Condition33%1/3
$op eq "=" or $op eq "=="
l!l&&r!l&&!r
200
MC/DC0%0/2
$op eq "=" or $op eq "=="
$op eq "=" $op eq "=="
Truth table33%1/3
A or B
A= $op eq "="B= $op eq "=="
ABresult
1X1
011
000
3164 0 ▶ if ($op eq '!=') { return ($a ne $b) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "!=") 0 0 0
$a ne $b ? : 0 0 0
3165 0 ▶ if ($op eq '<') { return ($a lt $b) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "<") 0 0 0
$a lt $b ? : 0 0 0
3166 0 ▶ if ($op eq '>') { return ($a gt $b) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq ">") 0 0 0
$a gt $b ? : 0 0 0
3167 }
3168  
3169 # Integer comparisons: -eq -ne -lt -le -gt -ge
3170 24 ▶ if ($expr =~ /\A(.+?)\s+(-eq|-ne|-lt|-le|-gt|-ge)\s+(.+)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /\A(.+?)\s+(-eq|-ne|-lt|-le|-gt|-ge)\s+(.+)\z/) 24 0 24
3171 24 my ($a, $op, $b) = ($1, $2, $3);
3172 24 $a = _arr_dequote($a);
3173 24 $b = _arr_dequote($b);
3174 24 ▶ $a = int($a) if $a =~ /\A-?\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $a =~ /\A-?\d+\z/ 24 0 24
3175 24 ▶ $b = int($b) if $b =~ /\A-?\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $b =~ /\A-?\d+\z/ 24 0 24
3176 24 ▶ if ($op eq '-eq') { return ($a == $b) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-eq") 2 22 24
$a == $b ? : 1 1 2
3177 22 ▶ if ($op eq '-ne') { return ($a != $b) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-ne") 0 22 22
$a != $b ? : 0 0 0
3178 22 ▶ if ($op eq '-lt') { return ($a < $b) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-lt") 13 9 22
$a < $b ? : 10 3 13
3179 9 ▶ if ($op eq '-le') { return ($a <= $b) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-le") 0 9 9
$a <= $b ? : 0 0 0
3180 9 ▶ if ($op eq '-gt') { return ($a > $b) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-gt") 2 7 9
$a > $b ? : 2 0 2
3181 7 ▶ if ($op eq '-ge') { return ($a >= $b) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-ge") 7 0 7
$a >= $b ? : 2 5 7
3182 }
3183  
3184 # -n string (non-empty)
3185 0 ▶ if ($expr =~ /\A-n\s+(.+)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /\A-n\s+(.+)\z/) 0 0 0
3186 0 my $s = _arr_dequote($1);
3187 0 ▶ return length($s) > 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $s > 0 ? : 0 0 0
3188 }
3189 # -z string (empty)
3190 0 ▶ if ($expr =~ /\A-z\s+(.+)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /\A-z\s+(.+)\z/) 0 0 0
3191 0 my $s = _arr_dequote($1);
3192 0 ▶ return length($s) == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $s == 0 ? : 0 0 0
3193 }
3194  
3195 # bare string: true if non-empty
3196 0 $expr = _arr_dequote($expr);
3197 0 ▶ return (length($expr) > 0 && $expr ne '0') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $expr > 0 && $expr ne '0' ? : 0 0 0
Condition0%0/3
length $expr > 0 and $expr ne 0
!ll&&!rl&&r
000
MC/DC0%0/2
length $expr > 0 and $expr ne 0
length $expr > 0 $expr ne 0
Truth table0%0/3
A and B
A= length $expr > 0B= $expr ne 0
ABresult
0X0
100
111
3198 }
3199  
3200 # ----------------------------------------------------------------
3201 # if/then/else/elif/fi parser
3202 # ----------------------------------------------------------------
3203 sub _parse_if {
3204 20 ▶ my ($class, $lines_ref, $start, $opts_ref) = @_;
Subroutine _parse_if: called CC=20 SCAR=30.0
3205 20 my @lines = @{$lines_ref};
3206 20 my $i = $start;
3207  
3208 # Collect: if cond; then ... [elif cond; then ...] [else ...] fi
3209 # Build a structure: [ ['cond_lines'], ['body_lines'] ] ...
3210 20 my @branches = (); # [ [$cond_lines], [$body_lines] ]
3211 20 my $else_body = undef;
3212  
3213 # First line: if cond; then
3214 20 my $if_line = $lines[$i]; $i++;
3215 20 $if_line =~ s/\r?\n\z//; $if_line =~ s/\A\s+//;
3216  
3217 # Fully-inline form on ONE physical line:
3218 # if COND; then BODY... [elif COND; then BODY...] [else BODY...]; fi
3219 # Detected by a bare "fi" appearing as a top-level ';' segment. The
3220 # line is expanded into the logical-line shape the multi-line block
3221 # collector below already understands (with then/do/else peeled onto
3222 # their own lines), then re-parsed; this correctly handles elif and
3223 # else, which the previous narrow single-line regex silently dropped.
3224 20 ▶ if (_inline_has_terminator($if_line, 'fi')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_inline_has_terminator($if_line, "fi")) 4 16 20
3225 4 my @work = _inline_expand($if_line);
3226 4 my ($st) = _parse_if($class, \@work, 0, $opts_ref);
3227 4 return ($st, $i);
3228 }
3229  
3230 # Extract condition (after 'if', before 'then' or ';')
3231 16 my $cond_str = $if_line;
3232 16 $cond_str =~ s/\Aif\s+//i;
3233  
3234 16 $cond_str =~ s/\s*;\s*then\s*\z//i;
3235 16 $cond_str =~ s/\s+then\s*\z//i;
3236  
3237 16 my @cond_lines = ($cond_str);
3238 16 my @body_lines = ();
3239 16 my $in_else = 0; # collecting the else-branch body
3240 # Nested if/fi depth: a nested "if ... fi" inside a branch body (on
3241 # its own lines or written inline) must not have its "fi"/"elif"/
3242 # "else" mistaken for the outer if's. Only the depth-1 keywords are
3243 # the outer if's; deeper ones travel with the body and are re-parsed
3244 # when the body runs.
3245 16 my $depth = 1;
3246  
3247 16 ▶ while ($i <= $#lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#lines) 51 0 51
3248 51 my $l = $lines[$i]; $i++;
3249 51 $l =~ s/\r?\n\z//;
3250 51 my $ls = $l; $ls =~ s/\A\s+//;
3251 51 ▶ my $lc_first = lc( ($ls =~ /\A(\S+)/) ? $1 : '' );
Branch: 1 branch
BranchTrueFalseTotal
$ls =~ /\A(\S+)/ ? : 51 0 51
3252  
3253 51 ▶ if ($depth == 1 && $lc_first eq 'fi') {
Branch: 4 branches
BranchTrueFalseTotal
if ($depth == 1 and $lc_first eq 'fi') { } 16 35 51
elsif ($depth == 1 and not $in_else and $lc_first eq 'elif') { } 1 34 35
elsif ($depth == 1 and not $in_else and $lc_first eq 'else') { } 6 28 34
elsif ($depth == 1 and $lc_first eq 'then') { } 5 23 28
Condition66%2/3
$depth == 1 and $lc_first eq "fi"
!ll&&!rl&&r
03516
MC/DC50%1/2
$depth == 1 and $lc_first eq "fi"
$depth == 1 $lc_first eq "fi"
Truth table66%2/3
A and B
A= $depth == 1B= $lc_first eq "fi"
ABresult
0X0
100
111
3254 16 ▶ push @branches, [ [@cond_lines], [@body_lines] ] unless $in_else;
Branch: 1 branch
BranchTrueFalseTotal
unless $in_else 10 6 16
3255 16 ▶ $else_body = [@body_lines] if $in_else;
Branch: 1 branch
BranchTrueFalseTotal
if $in_else 6 10 16
3256 16 last;
3257 }
3258 ▶ elsif ($depth == 1 && !$in_else && $lc_first eq 'elif') {
Condition83%5/6
$depth == 1 and not $in_else and $lc_first eq "elif"
!ll&&!rl&&r
6281
$depth == 1 and not $in_else
!ll&&!rl&&r
0629
MC/DC66%2/3
$depth == 1 and not $in_else and $lc_first eq "elif"
$depth == 1 not $in_else $lc_first eq "elif"
Truth table75%3/4
A and not(B) and C
A= $depth == 1B= not $in_elseC= $lc_first eq "elif"
ABCresult
0XX0
10X0
1100
1111
3259 1 push @branches, [ [@cond_lines], [@body_lines] ];
3260 1 $cond_str = $ls;
3261 1 $cond_str =~ s/\Aelif\s+//i;
3262 1 $cond_str =~ s/\s*;\s*then\s*\z//i;
3263 1 $cond_str =~ s/\s+then\s*\z//i;
3264 1 @cond_lines = ($cond_str);
3265 1 @body_lines = ();
3266 }
3267 ▶ elsif ($depth == 1 && !$in_else && $lc_first eq 'else') {
Condition83%5/6
$depth == 1 and not $in_else and $lc_first eq "else"
!ll&&!rl&&r
6226
$depth == 1 and not $in_else
!ll&&!rl&&r
0628
MC/DC66%2/3
$depth == 1 and not $in_else and $lc_first eq "else"
$depth == 1 not $in_else $lc_first eq "else"
Truth table75%3/4
A and not(B) and C
A= $depth == 1B= not $in_elseC= $lc_first eq "else"
ABCresult
0XX0
10X0
1100
1111
3268 6 push @branches, [ [@cond_lines], [@body_lines] ];
3269 6 @body_lines = ();
3270 6 $in_else = 1;
3271 }
3272 ▶ elsif ($depth == 1 && $lc_first eq 'then') {
Condition66%2/3
$depth == 1 and $lc_first eq "then"
!ll&&!rl&&r
0235
MC/DC50%1/2
$depth == 1 and $lc_first eq "then"
$depth == 1 $lc_first eq "then"
Truth table66%2/3
A and B
A= $depth == 1B= $lc_first eq "then"
ABresult
0X0
100
111
3273 # 'then' on its own line: continue collecting body
3274 5 next;
3275 }
3276 else {
3277 23 push @body_lines, $l;
3278 23 $depth += _if_depth_delta($l);
3279 }
3280 }
3281  
3282 # Evaluate branches
3283 16 my $status = 0;
3284 16 my $executed = 0;
3285 16 for my $branch (@branches) {
3286 17 my ($cond_ref, $body_ref) = @{$branch};
3287 17 $_ERREXIT_HOLD++;
3288 17 my $cond_status = _run_lines($class, $cond_ref, $opts_ref);
3289 17 $_ERREXIT_HOLD--;
3290 17 ▶ if ($cond_status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cond_status == 0) 13 4 17
3291 13 $status = _run_lines($class, $body_ref, $opts_ref);
3292 13 $executed = 1;
3293 13 last;
3294 }
3295 }
3296 16 ▶ if (!$executed && defined $else_body) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $executed and defined $else_body) 2 14 16
Condition100%3/3
not $executed and defined $else_body
!ll&&!rl&&r
1312
MC/DC100%2/2
not $executed and defined $else_body
not $executed defined $else_body
Truth table100%3/3
not(A) and B
A= not $executedB= defined $else_body
ABresult
0X0
100
111
3297 2 $status = _run_lines($class, $else_body, $opts_ref);
3298 }
3299  
3300 16 return ($status, $i);
3301 }
3302  
3303 # ----------------------------------------------------------------
3304 # for VAR in list; do ... done
3305 # ----------------------------------------------------------------
3306 sub _parse_for {
3307 19 ▶ my ($class, $lines_ref, $start, $opts_ref) = @_;
Subroutine _parse_for: called CC=21 SCAR=34.2
3308 19 my @lines = @{$lines_ref};
3309 19 my $i = $start;
3310  
3311 19 my $for_line = $lines[$i]; $i++;
3312 19 $for_line =~ s/\r?\n\z//; $for_line =~ s/\A\s+//;
3313  
3314 # for VAR in LIST [; do [BODY [; done]]]
3315 #
3316 # The header may stand alone (do/done on following lines) or carry an
3317 # inline "; do ... ; done" tail all on one physical line. The inline
3318 # form is detected first; ";do" is matched with \b after "do" so that a
3319 # "; done" terminator is never mistaken for the "do" keyword.
3320 19 my ($var, $list_str) = ('', '');
3321 19 my $inline_body; # defined when the header has an inline "do" tail
3322 19 my $inline_closed = 0; # true when that tail also held the "done"
3323 19 ▶ if ($for_line =~ /\Afor\s+([A-Za-z_][A-Za-z0-9_]*)\s+in\s+(.*?)\s*;\s*do\b\s*(.*)\z/is) {
Branch: 2 branches
BranchTrueFalseTotal
if ($for_line =~ /\Afor\s+([A-Za-z_][A-Za-z0-9_]*)\s+in\s+(.*?)\s*;\s*do\b\s*(.*)\z/is) { } 19 0 19
elsif ($for_line =~ /\Afor\s+([A-Za-z_][A-Za-z0-9_]*)\s+in\s+(.*?)\s*\z/i) { } 0 0 0
3324 19 my $tail;
3325 19 ($var, $list_str, $tail) = ($1, $2, $3);
3326 19 $tail =~ s/\s+\z//;
3327 19 ▶ if ($tail eq '') {
Branch: 3 branches
BranchTrueFalseTotal
if ($tail eq '') { } 3 16 19
elsif ($tail =~ /\A(.*);\s*done\b\s*\z/s) { } 16 0 16
elsif ($tail eq 'done') { } 0 0 0
3328 3 $inline_body = undef; # "for ... ; do" -> body follows
3329 }
3330 elsif ($tail =~ /\A(.*);\s*done\b\s*\z/s) {
3331 16 $inline_body = $1; $inline_closed = 1; # fully inline; greedy = last done
3332 }
3333 elsif ($tail eq 'done') {
3334 0 $inline_body = ''; $inline_closed = 1; # empty inline body
3335 }
3336 else {
3337 0 $inline_body = $tail; # "for ... ; do BODY" (done later)
3338 }
3339 }
3340 elsif ($for_line =~ /\Afor\s+([A-Za-z_][A-Za-z0-9_]*)\s+in\s+(.*?)\s*\z/i) {
3341 0 ($var, $list_str) = ($1, $2); # header only; do/done on later lines
3342 }
3343  
3344 # Collect body until 'done' (skipped when the tail already closed the loop)
3345 19 my @body = ();
3346 19 ▶ if (defined $inline_body) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $inline_body) 16 3 19
3347 16 ▶ push @body, $inline_body unless $inline_body eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless $inline_body eq "" 16 0 16
3348 }
3349 19 ▶ if (!$inline_closed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($inline_closed) 3 16 19
3350 3 my $depth = 1;
3351 3 ▶ while ($i <= $#lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#lines) 6 0 6
3352 6 my $l = $lines[$i]; $i++;
3353 6 $l =~ s/\r?\n\z//;
3354 6 my $ls = $l; $ls =~ s/\A\s+//;
3355 6 ▶ my $lc_f = lc( ($ls =~ /\A(\S+)/) ? $1 : '' );
Branch: 1 branch
BranchTrueFalseTotal
$ls =~ /\A(\S+)/ ? : 6 0 6
3356 6 ▶ if ($lc_f eq 'for' || $lc_f eq 'while' || $lc_f eq 'until') { $depth++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_f eq "for" or $lc_f eq "while" or $lc_f eq "until") 0 6 6
Condition33%2/6
$lc_f eq "for" or $lc_f eq "while" or $lc_f eq "until"
l!l&&r!l&&!r
006
$lc_f eq "for" or $lc_f eq "while"
l!l&&r!l&&!r
006
MC/DC0%0/3
$lc_f eq "for" or $lc_f eq "while" or $lc_f eq "until"
$lc_f eq "for" $lc_f eq "while" $lc_f eq "until"
Truth table25%1/4
A or B or C
A= $lc_f eq "for"B= $lc_f eq "while"C= $lc_f eq "until"
ABCresult
1XX1
01X1
0011
0000
3357 6 ▶ if ($lc_f eq 'done') { $depth--; last if $depth == 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($lc_f eq "done") 3 3 6
if $depth == 0 3 0 3
3358 3 ▶ push @body, $l unless ($lc_f eq 'do' && $depth == 1);
Branch: 1 branch
BranchTrueFalseTotal
unless $lc_f eq "do" and $depth == 1 3 0 3
Condition33%1/3
$lc_f eq "do" and $depth == 1
!ll&&!rl&&r
300
MC/DC0%0/2
$lc_f eq "do" and $depth == 1
$lc_f eq "do" $depth == 1
Truth table33%1/3
A and B
A= $lc_f eq "do"B= $depth == 1
ABresult
0X0
100
111
3359 }
3360 }
3361  
3362 # Expand list items. _expand_word_list resolves variables and command
3363 # substitutions, applies filename globbing to unquoted glob words, and
3364 # expands a whole-word ${arr[@]} / ${arr[*]} reference (quoted or not) to
3365 # one item per array element.
3366 19 my @items = _expand_word_list($class, $list_str);
3367 19 my $status = 0;
3368 19 for my $val (@items) {
3369 122 BATsh::Env->set($var, $val);
3370 122 $_BREAK = 0; $_CONTINUE = 0;
3371 122 $status = _run_lines($class, \@body, $opts_ref);
3372 122 ▶ last if $_BREAK || defined $_EXIT_CODE;
Branch: 1 branch
BranchTrueFalseTotal
if $_BREAK or defined $_EXIT_CODE 0 122 122
Condition33%1/3
$_BREAK or defined $_EXIT_CODE
l!l&&r!l&&!r
00122
MC/DC0%0/2
$_BREAK or defined $_EXIT_CODE
$_BREAK defined $_EXIT_CODE
Truth table33%1/3
A or B
A= $_BREAKB= defined $_EXIT_CODE
ABresult
1X1
011
000
3373 }
3374 19 $_BREAK = 0;
3375  
3376 19 return ($status, $i);
3377 }
3378  
3379 # ----------------------------------------------------------------
3380 # select VAR in LIST ; do ... done (v0.07)
3381 #
3382 # Prints a numbered menu of LIST (one item per line, to STDERR), prompts
3383 # with $PS3 (default "#? "), and reads one line from STDIN into REPLY:
3384 # - a number in range 1..#items sets VAR to that item and runs BODY
3385 # - anything else (including a blank line) sets VAR to '' and still
3386 # runs BODY, matching bash (the menu is then reprinted next
3387 # iteration)
3388 # - end of input (STDIN closed) ends the loop, as does break
3389 # There is no multi-column menu layout (bash chooses columns based on
3390 # terminal width and item count); every item is printed on its own line.
3391 # ----------------------------------------------------------------
3392 sub _parse_select {
3393 3 ▶ my ($class, $lines_ref, $start, $opts_ref) = @_;
Subroutine _parse_select: called CC=32 SCAR=51.6
3394 3 my @lines = @{$lines_ref};
3395 3 my $i = $start;
3396  
3397 3 my $sel_line = $lines[$i]; $i++;
3398 3 $sel_line =~ s/\r?\n\z//; $sel_line =~ s/\A\s+//;
3399  
3400 3 my ($var, $list_str) = ('', '');
3401 3 my $inline_body;
3402 3 my $inline_closed = 0;
3403 3 ▶ if ($sel_line =~ /\Aselect\s+([A-Za-z_][A-Za-z0-9_]*)\s+in\s+(.*?)\s*;\s*do\b\s*(.*)\z/is) {
Branch: 3 branches
BranchTrueFalseTotal
if ($sel_line =~ /\Aselect\s+([A-Za-z_][A-Za-z0-9_]*)\s+in\s+(.*?)\s*;\s*do\b\s*(.*)\z/is) { } 3 0 3
elsif ($sel_line =~ /\Aselect\s+([A-Za-z_][A-Za-z0-9_]*)\s+in\s+(.*?)\s*\z/i) { } 0 0 0
elsif ($sel_line =~ /\Aselect\s+([A-Za-z_][A-Za-z0-9_]*)\s*\z/i) { } 0 0 0
3404 3 my $tail;
3405 3 ($var, $list_str, $tail) = ($1, $2, $3);
3406 3 $tail =~ s/\s+\z//;
3407 3 ▶ if ($tail eq '') { $inline_body = undef }
Branch: 3 branches
BranchTrueFalseTotal
if ($tail eq '') { } 0 3 3
elsif ($tail =~ /\A(.*);\s*done\b\s*\z/s) { } 3 0 3
elsif ($tail eq 'done') { } 0 0 0
3408 3 elsif ($tail =~ /\A(.*);\s*done\b\s*\z/s) { $inline_body = $1; $inline_closed = 1 }
3409 0 elsif ($tail eq 'done') { $inline_body = ''; $inline_closed = 1 }
3410 0 else { $inline_body = $tail }
3411 }
3412 elsif ($sel_line =~ /\Aselect\s+([A-Za-z_][A-Za-z0-9_]*)\s+in\s+(.*?)\s*\z/i) {
3413 0 ($var, $list_str) = ($1, $2);
3414 }
3415 elsif ($sel_line =~ /\Aselect\s+([A-Za-z_][A-Za-z0-9_]*)\s*\z/i) {
3416 0 ($var, $list_str) = ($1, '"$@"'); # "select VAR" alone: positional params
3417 }
3418 else {
3419 0 return (0, $i); # malformed header: nothing to do
3420 }
3421  
3422 3 my @body = ();
3423 3 ▶ if (defined $inline_body) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $inline_body) 3 0 3
3424 3 ▶ push @body, $inline_body unless $inline_body eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless $inline_body eq "" 3 0 3
3425 }
3426 3 ▶ if (!$inline_closed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($inline_closed) 0 3 3
3427 0 my $depth = 1;
3428 0 ▶ while ($i <= $#lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#lines) 0 0 0
3429 0 my $l = $lines[$i]; $i++;
3430 0 $l =~ s/\r?\n\z//;
3431 0 my $ls = $l; $ls =~ s/\A\s+//;
3432 0 ▶ my $lc_f = lc( ($ls =~ /\A(\S+)/) ? $1 : '' );
Branch: 1 branch
BranchTrueFalseTotal
$ls =~ /\A(\S+)/ ? : 0 0 0
3433 0 ▶ if ($lc_f eq 'for' || $lc_f eq 'while' || $lc_f eq 'until' || $lc_f eq 'select') { $depth++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_f eq "for" or $lc_f eq "while" or $lc_f eq "until" or $lc_f eq "select") 0 0 0
Condition0%0/9
$lc_f eq "for" or $lc_f eq "while" or $lc_f eq "until" or $lc_f eq "select"
l!l&&r!l&&!r
000
$lc_f eq "for" or $lc_f eq "while" or $lc_f eq "until"
l!l&&r!l&&!r
000
$lc_f eq "for" or $lc_f eq "while"
l!l&&r!l&&!r
000
MC/DC0%0/4
$lc_f eq "for" or $lc_f eq "while" or $lc_f eq "until" or $lc_f eq "select"
$lc_f eq "for" $lc_f eq "while" $lc_f eq "until" $lc_f eq "select"
Truth table0%0/5
A or B or C or D
A= $lc_f eq "for"B= $lc_f eq "while"C= $lc_f eq "until"D= $lc_f eq "select"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
3434 0 ▶ if ($lc_f eq 'done') { $depth--; last if $depth == 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($lc_f eq "done") 0 0 0
if $depth == 0 0 0 0
3435 0 ▶ push @body, $l unless ($lc_f eq 'do' && $depth == 1);
Branch: 1 branch
BranchTrueFalseTotal
unless $lc_f eq "do" and $depth == 1 0 0 0
Condition0%0/3
$lc_f eq "do" and $depth == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$lc_f eq "do" and $depth == 1
$lc_f eq "do" $depth == 1
Truth table0%0/3
A and B
A= $lc_f eq "do"B= $depth == 1
ABresult
0X0
100
111
3436 }
3437 }
3438  
3439 3 my @items = _expand_word_list($class, $list_str);
3440 3 my $status = 0;
3441 3 ▶ return ($status, $i) unless @items;
Branch: 1 branch
BranchTrueFalseTotal
unless @items 0 3 3
3442  
3443 3 my $ps3 = BATsh::Env->get('PS3');
3444 3 ▶ $ps3 = '#? ' unless defined $ps3 && $ps3 ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ps3 and $ps3 ne "" 3 0 3
Condition33%1/3
defined $ps3 and $ps3 ne ""
!ll&&!rl&&r
300
MC/DC0%0/2
defined $ps3 and $ps3 ne ""
defined $ps3 $ps3 ne ""
Truth table33%1/3
A and B
A= defined $ps3B= $ps3 ne ""
ABresult
0X0
100
111
3445  
3446 3 my $max_iter = 100_000; # safety guard, mirrors _parse_while
3447 3 ▶ while ($max_iter-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_iter-- > 0) 3 0 3
3448 3 ▶ last if defined $_EXIT_CODE;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_EXIT_CODE 0 3 3
3449 3 for my $idx (0 .. $#items) {
3450 7 print STDERR ($idx + 1) . ') ' . $items[$idx] . "\n";
3451 }
3452 3 print STDERR BATsh::MB::dec($ps3);
3453 3 my $answer = <STDIN>;
3454 3 ▶ last unless defined $answer; # EOF on STDIN ends the loop
Branch: 1 branch
BranchTrueFalseTotal
unless defined $answer 0 3 3
3455 3 $answer =~ s/\r?\n\z//;
3456 3 BATsh::Env->set('REPLY', BATsh::MB::enc($answer));
3457 3 ▶ if ($answer =~ /\A[0-9]+\z/ && $answer >= 1 && $answer <= scalar(@items)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer =~ /\A[0-9]+\z/ and $answer >= 1 and $answer <= scalar @items) { } 2 1 3
Condition50%3/6
$answer =~ /\A[0-9]+\z/ and $answer >= 1 and $answer <= scalar @items
!ll&&!rl&&r
012
$answer =~ /\A[0-9]+\z/ and $answer >= 1
!ll&&!rl&&r
003
MC/DC33%1/3
$answer =~ /\A[0-9]+\z/ and $answer >= 1 and $answer <= scalar @items
$answer =~ /\A[0-9]+\z/ $answer >= 1 $answer <= scalar @items
Truth table50%2/4
A and B and C
A= $answer =~ /\A[0-9]+\z/B= $answer >= 1C= $answer <= scalar @items
ABCresult
0XX0
10X0
1100
1111
3458 2 BATsh::Env->set($var, $items[$answer - 1]);
3459 }
3460 else {
3461 1 BATsh::Env->set($var, '');
3462 }
3463 3 $_BREAK = 0; $_CONTINUE = 0;
3464 3 $status = _run_lines($class, \@body, $opts_ref);
3465 3 ▶ last if $_BREAK || defined $_EXIT_CODE;
Branch: 1 branch
BranchTrueFalseTotal
if $_BREAK or defined $_EXIT_CODE 3 0 3
Condition33%1/3
$_BREAK or defined $_EXIT_CODE
l!l&&r!l&&!r
300
MC/DC0%0/2
$_BREAK or defined $_EXIT_CODE
$_BREAK defined $_EXIT_CODE
Truth table33%1/3
A or B
A= $_BREAKB= defined $_EXIT_CODE
ABresult
1X1
011
000
3466 }
3467 3 $_BREAK = 0;
3468  
3469 3 return ($status, $i);
3470 }
3471  
3472 # ----------------------------------------------------------------
3473 # while/until condition; do ... done
3474 # ----------------------------------------------------------------
3475 sub _parse_while {
3476 31 ▶ my ($class, $lines_ref, $start, $opts_ref) = @_;
Subroutine _parse_while: called CC=37 SCAR=40.2
3477 31 my @lines = @{$lines_ref};
3478 31 my $i = $start;
3479  
3480 31 my $while_line = $lines[$i]; $i++;
3481 31 $while_line =~ s/\r?\n\z//; $while_line =~ s/\A\s+//;
3482  
3483 31 ▶ my $is_until = ($while_line =~ /\Auntil\s/i) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$while_line =~ /\Auntil\s/i ? : 2 29 31
3484  
3485 # Extract condition, supporting an inline "COND; do BODY; done [REDIR]" form
3486 # as well as the multi-line header. As in _parse_for, ";do" is matched with
3487 # \b after "do" so that a "; done" terminator is never taken for "do".
3488 31 my $rest = $while_line;
3489 31 $rest =~ s/\A(?:while|until)\s+//i;
3490  
3491 31 my $cond_str;
3492 31 my @body = ();
3493 31 my $done_line = '';
3494 31 my $inline_body;
3495 31 my $inline_closed = 0;
3496  
3497 31 ▶ if ($rest =~ /\A(.*?)\s*;\s*do\b\s*(.*)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /\A(.*?)\s*;\s*do\b\s*(.*)\z/s) { } 31 0 31
3498 31 my $tail;
3499 31 ($cond_str, $tail) = ($1, $2);
3500 31 $tail =~ s/\s+\z//;
3501 31 ▶ if ($tail eq '') {
Branch: 3 branches
BranchTrueFalseTotal
if ($tail eq '') { } 3 28 31
elsif ($tail =~ /\A(.*);\s*done\b\s*(.*)\z/s) { } 28 0 28
elsif ($tail =~ /\Adone\b\s*(.*)\z/s) { } 0 0 0
3502 3 $inline_body = undef; # "while COND; do" -> body follows
3503 }
3504 elsif ($tail =~ /\A(.*);\s*done\b\s*(.*)\z/s) {
3505 28 $inline_body = $1; # greedy = last "; done"
3506 28 $inline_closed = 1;
3507 28 my $dr = $2; $dr =~ s/\A\s+//; $dr =~ s/\s+\z//;
3508 28 ▶ $done_line = ($dr ne '') ? "done $dr" : 'done';
Branch: 1 branch
BranchTrueFalseTotal
$dr ne '' ? : 1 27 28
3509 }
3510 elsif ($tail =~ /\Adone\b\s*(.*)\z/s) {
3511 0 $inline_body = ''; # empty inline body
3512 0 $inline_closed = 1;
3513 0 my $dr = $1; $dr =~ s/\A\s+//; $dr =~ s/\s+\z//;
3514 0 ▶ $done_line = ($dr ne '') ? "done $dr" : 'done';
Branch: 1 branch
BranchTrueFalseTotal
$dr ne '' ? : 0 0 0
3515 }
3516 else {
3517 0 $inline_body = $tail; # done on following lines
3518 }
3519 }
3520 else {
3521 0 $cond_str = $rest; # header only; do/done on later lines
3522 0 $cond_str =~ s/\s*;\s*do\s*\z//i;
3523 0 $cond_str =~ s/\s+do\s*\z//i;
3524 }
3525  
3526 # Collect body (skipped when the inline tail already closed the loop)
3527 31 ▶ if (defined $inline_body) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $inline_body) 28 3 31
3528 28 ▶ push @body, $inline_body unless $inline_body eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless $inline_body eq "" 28 0 28
3529 }
3530 31 ▶ if (!$inline_closed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($inline_closed) 3 28 31
3531 3 my $depth = 1;
3532 3 ▶ while ($i <= $#lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#lines) 6 0 6
3533 6 my $l = $lines[$i]; $i++;
3534 6 $l =~ s/\r?\n\z//;
3535 6 my $ls = $l; $ls =~ s/\A\s+//;
3536 6 ▶ my $lc_f = lc( ($ls =~ /\A(\S+)/) ? $1 : '' );
Branch: 1 branch
BranchTrueFalseTotal
$ls =~ /\A(\S+)/ ? : 6 0 6
3537 6 ▶ if ($lc_f eq 'for' || $lc_f eq 'while' || $lc_f eq 'until') { $depth++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_f eq "for" or $lc_f eq "while" or $lc_f eq "until") 0 6 6
Condition33%2/6
$lc_f eq "for" or $lc_f eq "while" or $lc_f eq "until"
l!l&&r!l&&!r
006
$lc_f eq "for" or $lc_f eq "while"
l!l&&r!l&&!r
006
MC/DC0%0/3
$lc_f eq "for" or $lc_f eq "while" or $lc_f eq "until"
$lc_f eq "for" $lc_f eq "while" $lc_f eq "until"
Truth table25%1/4
A or B or C
A= $lc_f eq "for"B= $lc_f eq "while"C= $lc_f eq "until"
ABCresult
1XX1
01X1
0011
0000
3538 6 ▶ if ($lc_f eq 'done') { $depth--; if ($depth == 0) { $done_line = $ls; last } }
Branch: 2 branches
BranchTrueFalseTotal
if ($lc_f eq "done") 3 3 6
if ($depth == 0) 3 0 3
3539 3 ▶ push @body, $l unless ($lc_f eq 'do' && $depth == 1);
Branch: 1 branch
BranchTrueFalseTotal
unless $lc_f eq "do" and $depth == 1 3 0 3
Condition33%1/3
$lc_f eq "do" and $depth == 1
!ll&&!rl&&r
300
MC/DC0%0/2
$lc_f eq "do" and $depth == 1
$lc_f eq "do" $depth == 1
Truth table33%1/3
A and B
A= $lc_f eq "do"B= $depth == 1
ABresult
0X0
100
111
3540 }
3541 }
3542  
3543 # Honor an input redirection on the `done' line, e.g.
3544 # while read LINE; do ...; done < FILE
3545 # by reopening STDIN from FILE for the duration of the loop so that the
3546 # loop's `read' built-in consumes the file. Perl 5.005_03 compatible:
3547 # 2-argument open and bareword filehandle duplication.
3548 31 my $saved_in = 0;
3549 31 ▶ if ($done_line ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($done_line ne "") 31 0 31
3550 31 my $done_rest = $done_line;
3551 31 $done_rest =~ s/\Adone\b\s*//i;
3552 31 ▶ if ($done_rest ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($done_rest ne "") 1 30 31
3553 1 my ($dc, $rd) = _sh_strip_redirects(_expand($class, $done_rest));
3554 1 my $in_file;
3555 1 for my $r (@{$rd}) {
3556 1 my ($fd, $append, $file) = @{$r};
3557 1 ▶ $in_file = $file if $fd == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $fd == 0 1 0 1
3558 }
3559 1 ▶ if (defined $in_file && $in_file ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $in_file and $in_file ne "") 1 0 1
Condition33%1/3
defined $in_file and $in_file ne ""
!ll&&!rl&&r
001
MC/DC0%0/2
defined $in_file and $in_file ne ""
defined $in_file $in_file ne ""
Truth table33%1/3
A and B
A= defined $in_fileB= $in_file ne ""
ABresult
0X0
100
111
3560 1 ▶ if (sysopen(_WH_REDIR_SRC, $in_file, O_RDONLY)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysopen _WH_REDIR_SRC, $in_file, 0) { } 1 0 1
3561 1 ▶ if (open(_WH_REDIR_SAVIN, '<&STDIN')) {
Branch: 1 branch
BranchTrueFalseTotal
if (open _WH_REDIR_SAVIN, "<&STDIN") 1 0 1
3562 1 ▶ if (open(STDIN, '<&_WH_REDIR_SRC')) { $saved_in = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if (open STDIN, "<&_WH_REDIR_SRC") 1 0 1
3563 }
3564 1 close(_WH_REDIR_SRC);
3565 }
3566 else {
3567 0 warn "sh: $in_file: $!\n";
3568 }
3569 }
3570 }
3571 }
3572  
3573 31 my $status = 0;
3574 31 my $max_iter = 100_000; # safety guard
3575 31 ▶ while ($max_iter-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_iter-- > 0) 85 0 85
3576 85 ▶ last if defined $_EXIT_CODE;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_EXIT_CODE 0 85 85
3577 85 $_ERREXIT_HOLD++;
3578 85 my $cond_status = _run_lines($class, [$cond_str], $opts_ref);
3579 85 $_ERREXIT_HOLD--;
3580 85 my $cond_true = ($cond_status == 0);
3581 85 ▶ last if $is_until && $cond_true;
Branch: 1 branch
BranchTrueFalseTotal
if $is_until and $cond_true 2 83 85
Condition100%3/3
$is_until and $cond_true
!ll&&!rl&&r
7852
MC/DC100%2/2
$is_until and $cond_true
$is_until $cond_true
Truth table100%3/3
A and B
A= $is_untilB= $cond_true
ABresult
0X0
100
111
3582 83 ▶ last if !$is_until && !$cond_true;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_until or $cond_true 29 54 83
Condition100%3/3
$is_until or $cond_true
l!l&&r!l&&!r
54929
MC/DC100%2/2
$is_until or $cond_true
$is_until $cond_true
Truth table100%3/3
A or B
A= $is_untilB= $cond_true
ABresult
1X1
011
000
3583 54 $_BREAK = 0; $_CONTINUE = 0;
3584 54 $status = _run_lines($class, \@body, $opts_ref);
3585 54 ▶ last if $_BREAK;
Branch: 1 branch
BranchTrueFalseTotal
if $_BREAK 0 54 54
3586 }
3587 31 $_BREAK = 0;
3588  
3589 31 ▶ if ($saved_in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_in) 1 30 31
3590 1 open(STDIN, '<&_WH_REDIR_SAVIN');
3591 1 close(_WH_REDIR_SAVIN);
3592 }
3593  
3594 31 return ($status, $i);
3595 }
3596  
3597 # ----------------------------------------------------------------
3598 # case $var in pattern) ... ;; esac
3599 # ----------------------------------------------------------------
3600 sub _parse_case {
3601 22 ▶ my ($class, $lines_ref, $start, $opts_ref) = @_;
Subroutine _parse_case: called CC=21 SCAR=30.9
3602 22 my @lines = @{$lines_ref};
3603 22 my $i = $start;
3604  
3605 22 my $case_line = $lines[$i]; $i++;
3606 22 $case_line =~ s/\r?\n\z//; $case_line =~ s/\A\s+//;
3607  
3608 # Header: case WORD in [inline clauses...]
3609 # The clauses (and even esac) may follow inline on the same physical line
3610 # -- e.g. "case $x in a) echo a ;; *) echo other ;; esac" -- or on the
3611 # following lines. $inline_rest captures anything after "in".
3612 22 my $word = '';
3613 22 my $inline_rest = '';
3614 22 ▶ if ($case_line =~ /\Acase\s+(.*?)\s+in\b\s*(.*)\z/is) {
Branch: 1 branch
BranchTrueFalseTotal
if ($case_line =~ /\Acase\s+(.*?)\s+in\b\s*(.*)\z/is) { } 22 0 22
3615 22 $word = _arr_dequote(_expand($class, $1));
3616 22 $inline_rest = $2;
3617 }
3618 else {
3619 0 return (0, $i); # malformed header: nothing to do
3620 }
3621  
3622 # Accumulate the clause region (everything between "in" and "esac"),
3623 # stopping as soon as the closing esac is seen. esac is only recognised
3624 # at a clause boundary (start of region, after a ;;/;&/;;& terminator, or
3625 # at the start of a line), so the word "esac" appearing inside a body
3626 # (e.g. echo esac) does not end the construct prematurely.
3627 22 my $region = '';
3628 22 my $found = 0;
3629 {
3630 22 my $pos = _case_top_esac_pos($inline_rest);
3631 22 ▶ if ($pos >= 0) { $region = substr($inline_rest, 0, $pos); $found = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($pos >= 0) { } 1 21 22
3632 21 else { $region = $inline_rest }
3633 }
3634 22 ▶ while (!$found && $i <= $#lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $found and $i <= $#lines) 73 1 74
Condition66%2/3
not $found and $i <= $#lines
!ll&&!rl&&r
1073
MC/DC50%1/2
not $found and $i <= $#lines
not $found $i <= $#lines
Truth table66%2/3
not(A) and B
A= not $foundB= $i <= $#lines
ABresult
0X0
100
111
3635 73 my $l = $lines[$i]; $i++;
3636 73 $l =~ s/\r?\n\z//;
3637 73 my $pos = _case_top_esac_pos($l);
3638 73 ▶ if ($pos >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos >= 0) 21 52 73
3639 21 $region .= "\n" . substr($l, 0, $pos);
3640 21 $found = 1;
3641 21 last;
3642 }
3643 52 $region .= "\n" . $l;
3644 }
3645  
3646 # Split the region into clauses, then evaluate with fall-through support:
3647 # ;; normal: stop after the first matching clause
3648 # ;& fall through: run the NEXT clause's body unconditionally
3649 # ;;& continue: keep testing the remaining clauses against the word
3650 22 my @clauses = _case_split_clauses($region);
3651  
3652 22 my $status = 0;
3653 22 my $stop = 0; # a ;; was reached after a match -- stop entirely
3654 22 my $fall = 0; # previous clause ended in ;& -- run this body no matter what
3655 22 for my $cl (@clauses) {
3656 48 ▶ last if $stop;
Branch: 1 branch
BranchTrueFalseTotal
if $stop 19 29 48
3657 29 my ($ctext, $term) = @{$cl};
3658 29 ▶ next unless $ctext =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $ctext =~ /\S/ 0 29 29
3659  
3660 29 my ($pattern_str, $body_text) = _case_parse_clause($ctext);
3661 29 ▶ next unless defined $pattern_str; # not a pattern) clause -- skip
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pattern_str 0 29 29
3662  
3663 29 my $run = 0;
3664 29 ▶ if ($fall) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fall) { } 2 27 29
3665 2 $run = 1;
3666 2 $fall = 0;
3667 }
3668 else {
3669 27 for my $pat (_case_split_patterns($pattern_str)) {
3670 29 $pat =~ s/\A\s+//; $pat =~ s/\s+\z//;
3671 29 ▶ next if $pat eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $pat eq "" 0 29 29
3672 29 ▶ if (_match_pattern($word, $pat)) { $run = 1; last }
Branch: 1 branch
BranchTrueFalseTotal
if (_match_pattern($word, $pat)) 23 6 29
3673 }
3674 }
3675  
3676 29 ▶ if ($run) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run) 25 4 29
3677 25 my @body = split /\n/, $body_text;
3678 25 $status = _run_lines($class, \@body, $opts_ref);
3679 25 ▶ last if $_BREAK || $_CONTINUE || $_RETURN || defined $_EXIT_CODE;
Branch: 1 branch
BranchTrueFalseTotal
if $_BREAK or $_CONTINUE or $_RETURN or defined $_EXIT_CODE 0 25 25
Condition33%3/9
$_BREAK or $_CONTINUE or $_RETURN or defined $_EXIT_CODE
l!l&&r!l&&!r
0025
$_BREAK or $_CONTINUE or $_RETURN
l!l&&r!l&&!r
0025
$_BREAK or $_CONTINUE
l!l&&r!l&&!r
0025
MC/DC0%0/4
$_BREAK or $_CONTINUE or $_RETURN or defined $_EXIT_CODE
$_BREAK $_CONTINUE $_RETURN defined $_EXIT_CODE
Truth table20%1/5
A or B or C or D
A= $_BREAKB= $_CONTINUEC= $_RETURND= defined $_EXIT_CODE
ABCDresult
1XXX1
01XX1
001X1
00011
00000
3680 25 ▶ if ($term eq ';;') { $stop = 1 }
Branch: 2 branches
BranchTrueFalseTotal
if ($term eq ';;') { } 22 3 25
elsif ($term eq ';&') { } 2 1 3
3681 2 elsif ($term eq ';&') { $fall = 1 } # next body unconditionally
3682 # ';;&' : neither stop nor fall -- keep testing later clauses
3683 }
3684 }
3685  
3686 22 return ($status, $i);
3687 }
3688  
3689 # ----------------------------------------------------------------
3690 # ( commands ) -- subshell command group with isolated scope (v0.07)
3691 #
3692 # There is no real fork here (this interpreter never forks -- see the
3693 # pipeline / background-job / exec notes elsewhere in this file), so
3694 # isolation is approximated by snapshotting every piece of state a real
3695 # subshell would not leak changes from -- variables, indexed/associative
3696 # arrays, function definitions, aliases, and the working directory --
3697 # running the body, and restoring all of it afterward regardless of how
3698 # the body finished. "exit" inside the group ends only the group (its
3699 # status becomes the group's status); break/continue/return still
3700 # propagate to an enclosing loop/function exactly as bash's ( ... ) does
3701 # NOT contain them.
3702 #
3703 # Both the single-line form "( cmd1; cmd2 )" and the multi-line form
3704 # (bare "(" ending the first line, matching bare ")" ending the last)
3705 # are recognised. A trailing ">" / ">>" / "<" redirection after the
3706 # closing ")" is honoured; other trailing redirections (2>, 2>&1, ...)
3707 # and a trailing "&" are not -- documented as known limitations.
3708 # ----------------------------------------------------------------
3709 sub _parse_subshell {
3710 8 ▶ my ($class, $lines_ref, $start, $opts_ref) = @_;
Subroutine _parse_subshell: called CC=49 SCAR=48.4
3711 8 my @lines = @{$lines_ref};
3712 8 my $i = $start;
3713  
3714 8 my $first_line = $lines[$i]; $i++;
3715 8 $first_line =~ s/\r?\n\z//;
3716 8 my $stripped = $first_line;
3717 8 $stripped =~ s/\A\s+//;
3718  
3719 8 my @body_lines;
3720 8 my $trailer = '';
3721 8 my $found_close = 0;
3722  
3723 # Try the single-line form first: a quote-aware paren-depth scan for
3724 # the ')' that matches this line's opening '('.
3725 {
3726 8 my @c = split //, $stripped;
3727 8 my $n = scalar @c;
3728 8 my $in_sq = 0;
3729 8 my $in_dq = 0;
3730 8 my $d = 0;
3731 8 my $close_pos;
3732 8 ▶ for (my $k = 0; $k < $n; $k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $n) 227 0 227
3733 227 my $ch = $c[$k];
3734 227 ▶ if ($in_sq) { $in_sq = 0 if $ch eq "'"; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($in_sq) 0 227 227
if $ch eq "'" 0 0 0
3735 227 ▶ if ($ch eq "'" && !$in_dq) { $in_sq = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $in_dq) 0 227 227
Condition33%1/3
$ch eq "'" and not $in_dq
!ll&&!rl&&r
22700
MC/DC0%0/2
$ch eq "'" and not $in_dq
$ch eq "'" not $in_dq
Truth table33%1/3
A and not(B)
A= $ch eq "'"B= not $in_dq
ABresult
0X0
100
111
3736 227 ▶ if ($ch eq '"' && !$in_sq) { $in_dq = !$in_dq; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" and not $in_sq) 4 223 227
Condition66%2/3
$ch eq "\"" and not $in_sq
!ll&&!rl&&r
22304
MC/DC50%1/2
$ch eq "\"" and not $in_sq
$ch eq "\"" not $in_sq
Truth table66%2/3
A and not(B)
A= $ch eq "\""B= not $in_sq
ABresult
0X0
100
111
3737 223 ▶ next if $in_sq || $in_dq;
Branch: 1 branch
BranchTrueFalseTotal
if $in_sq or $in_dq 63 160 223
Condition66%2/3
$in_sq or $in_dq
l!l&&r!l&&!r
063160
MC/DC50%1/2
$in_sq or $in_dq
$in_sq $in_dq
Truth table66%2/3
A or B
A= $in_sqB= $in_dq
ABresult
1X1
011
000
3738 160 ▶ if ($ch eq '(') { $d++ }
Branch: 2 branches
BranchTrueFalseTotal
if ($ch eq '(') { } 9 151 160
elsif ($ch eq ')') { } 9 142 151
3739 9 ▶ elsif ($ch eq ')') { $d--; if ($d == 0) { $close_pos = $k; last } }
Branch: 1 branch
BranchTrueFalseTotal
if ($d == 0) 8 1 9
3740 }
3741 8 ▶ if (defined $close_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $close_pos) 8 0 8
3742 8 push @body_lines, substr($stripped, 1, $close_pos - 1);
3743 8 $trailer = substr($stripped, $close_pos + 1);
3744 8 $found_close = 1;
3745 }
3746 }
3747  
3748 8 ▶ if (!$found_close) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found_close) 0 8 8
3749 # Multi-line form: accumulate lines, counting '(' / ')' globally
3750 # per line (not quote-aware, matching the existing _parse_function
3751 # brace-counter's level of rigor) until the depth returns to 0.
3752 0 my $accum = substr($stripped, 1);
3753 0 my $opens = () = ($accum =~ /\(/g);
3754 0 my $closes = () = ($accum =~ /\)/g);
3755 0 my $depth = 1 + $opens - $closes;
3756 0 ▶ push @body_lines, $accum if $accum =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $accum =~ /\S/ 0 0 0
3757 0 ▶ if ($depth <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth <= 0) { } 0 0 0
3758 0 $found_close = 1;
3759 0 ▶ if (@body_lines && $body_lines[-1] =~ /\A(.*)\)([^)]*)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if (@body_lines and $body_lines[-1] =~ /\A(.*)\)([^)]*)\z/s) 0 0 0
Condition0%0/3
@body_lines and $body_lines[-1] =~ /\A(.*)\)([^)]*)\z/s
!ll&&!rl&&r
000
MC/DC0%0/2
@body_lines and $body_lines[-1] =~ /\A(.*)\)([^)]*)\z/s
@body_lines $body_lines[-1] =~ /\A(.*)\)([^)]*)\z/s
Truth table0%0/3
A and B
A= @body_linesB= $body_lines[-1] =~ /\A(.*)\)([^)]*)\z/s
ABresult
0X0
100
111
3760 0 $body_lines[-1] = $1;
3761 0 $trailer = $2;
3762 }
3763 }
3764 else {
3765 0 ▶ while ($i <= $#lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#lines) 0 0 0
3766 0 my $l = $lines[$i]; $i++;
3767 0 $l =~ s/\r?\n\z//;
3768 0 my $o = () = ($l =~ /\(/g);
3769 0 my $c2 = () = ($l =~ /\)/g);
3770 0 $depth += $o - $c2;
3771 0 ▶ if ($depth <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth <= 0) 0 0 0
3772 0 ▶ if ($l =~ /\A(.*)\)([^)]*)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /\A(.*)\)([^)]*)\z/s) 0 0 0
3773 0 ▶ push @body_lines, $1 if $1 =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $1 =~ /\S/ 0 0 0
3774 0 $trailer = $2;
3775 }
3776 0 $found_close = 1;
3777 0 last;
3778 }
3779 0 push @body_lines, $l;
3780 }
3781 }
3782 }
3783  
3784 8 my @body;
3785 8 for my $bl (@body_lines) {
3786 8 for my $part (split /;/, $bl) {
3787 14 ▶ push @body, $part if $part =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $part =~ /\S/ 14 0 14
3788 }
3789 }
3790  
3791 # Trailing "> file" / ">> file" / "< file" on the closing ')' line.
3792 8 $trailer =~ s/\A\s+//; $trailer =~ s/\s+\z//;
3793 8 my ($subsh_out_file, $subsh_out_append, $subsh_in_file);
3794 8 ▶ if ($trailer ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($trailer ne "") 5 3 8
3795 5 my $texp = _expand($class, $trailer);
3796 # Reuse the main redirection parser so a quoted target ("a b",
3797 # ">name", "cmd|") is read and dequoted exactly as elsewhere.
3798 5 my @tr_parts = _sh_strip_redirects($texp);
3799 5 my $tr_redirs = $tr_parts[1];
3800 5 for my $r (@{$tr_redirs}) {
3801 5 my ($fd, $app, $file) = @{$r};
3802 5 ▶ next if $file =~ /\A&[12]\z/; # dup forms handled in body
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\A&[12]\z/ 0 5 5
3803 5 ▶ if ($fd == 0) { $subsh_in_file = $file }
Branch: 2 branches
BranchTrueFalseTotal
if ($fd == 0) { } 2 3 5
elsif ($fd == 1) { } 3 0 3
3804 3 elsif ($fd == 1) { $subsh_out_file = $file; $subsh_out_append = $app }
3805 }
3806 }
3807  
3808 8 my $saved_out = 0;
3809 8 my $saved_in = 0;
3810 8 my $redir_failed = 0;
3811 8 ▶ if (defined $subsh_out_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $subsh_out_file) 3 5 8
3812 3 ▶ if (sysopen(_SH_SUBSH_DST, BATsh::MB::dec($subsh_out_file),
Branch: 2 branches
BranchTrueFalseTotal
if (sysopen _SH_SUBSH_DST, BATsh::MB::dec($subsh_out_file), 65 | ($subsh_out_append ? 1024 : 512), 438) { } 3 0 3
$subsh_out_append ? : 1 2 3
3813 O_WRONLY | O_CREAT | ($subsh_out_append ? O_APPEND : O_TRUNC),
3814 0666)) {
3815 3 ▶ if (open(_SH_SUBSH_SAVOUT, '>&STDOUT')) {
Branch: 1 branch
BranchTrueFalseTotal
if (open _SH_SUBSH_SAVOUT, ">&STDOUT") 3 0 3
3816 3 ▶ if (open(STDOUT, '>&_SH_SUBSH_DST')) { $saved_out = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if (open STDOUT, ">&_SH_SUBSH_DST") 3 0 3
3817 }
3818 3 close(_SH_SUBSH_DST);
3819 }
3820 0 else { warn "sh: $subsh_out_file: $!\n"; $redir_failed = 1 }
3821 }
3822 8 ▶ if (defined $subsh_in_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $subsh_in_file) 2 6 8
3823 2 ▶ if (sysopen(_SH_SUBSH_SRC, BATsh::MB::dec($subsh_in_file), O_RDONLY)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysopen _SH_SUBSH_SRC, BATsh::MB::dec($subsh_in_file), 0) { } 1 1 2
3824 1 ▶ if (open(_SH_SUBSH_SAVIN, '<&STDIN')) {
Branch: 1 branch
BranchTrueFalseTotal
if (open _SH_SUBSH_SAVIN, "<&STDIN") 1 0 1
3825 1 ▶ if (open(STDIN, '<&_SH_SUBSH_SRC')) { $saved_in = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if (open STDIN, "<&_SH_SUBSH_SRC") 1 0 1
3826 }
3827 1 close(_SH_SUBSH_SRC);
3828 }
3829 1 else { warn "sh: $subsh_in_file: $!\n"; $redir_failed = 1 }
3830 }
3831  
3832 # A failed redirection aborts the group without running its body -- as
3833 # the shell does for "( cat ) < missing": it reports the error and does
3834 # NOT fall through to read the terminal (which would hang). Restore any
3835 # handle already swapped, then return failure.
3836 8 ▶ if ($redir_failed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($redir_failed) 1 7 8
3837 1 ▶ if ($saved_out) { open(STDOUT, '>&_SH_SUBSH_SAVOUT'); close(_SH_SUBSH_SAVOUT) }
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_out) 0 1 1
3838 1 ▶ if ($saved_in) { open(STDIN, '<&_SH_SUBSH_SAVIN'); close(_SH_SUBSH_SAVIN) }
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_in) 0 1 1
3839 1 $LAST_STATUS = 1;
3840 1 return (1, $i);
3841 }
3842  
3843 # Snapshot everything the subshell must not leak changes to.
3844 7 my $env_snap = BATsh::Env->snapshot();
3845 7 my %arr_snap = %_SH_ARRAY;
3846 7 my %arrty_snap = %_SH_ARRAY_TYPE;
3847 7 my %fn_snap = %_SH_FUNCTIONS;
3848 7 my %alias_snap = %_SH_ALIAS;
3849 7 ▶ my $cwd_snap = defined(&Cwd::cwd) ? Cwd::cwd() : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined &Cwd::cwd ? : 7 0 7
3850  
3851 7 $_BREAK = 0; $_CONTINUE = 0;
3852 7 my $status = _run_lines($class, \@body, $opts_ref);
3853 7 my $subshell_exit = $_EXIT_CODE; # "exit" inside ( ... ) ends only the group
3854 7 $_EXIT_CODE = undef;
3855 7 ▶ $status = $subshell_exit if defined $subshell_exit;
Branch: 1 branch
BranchTrueFalseTotal
if defined $subshell_exit 0 7 7
3856  
3857 7 BATsh::Env->restore($env_snap);
3858 7 %_SH_ARRAY = %arr_snap;
3859 7 %_SH_ARRAY_TYPE = %arrty_snap;
3860 7 %_SH_FUNCTIONS = %fn_snap;
3861 7 %_SH_ALIAS = %alias_snap;
3862 7 ▶ chdir($cwd_snap) if defined $cwd_snap && $cwd_snap ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $cwd_snap and $cwd_snap ne "" 4 3 7
Condition66%2/3
defined $cwd_snap and $cwd_snap ne ""
!ll&&!rl&&r
034
MC/DC50%1/2
defined $cwd_snap and $cwd_snap ne ""
defined $cwd_snap $cwd_snap ne ""
Truth table66%2/3
A and B
A= defined $cwd_snapB= $cwd_snap ne ""
ABresult
0X0
100
111
3863  
3864 7 ▶ if ($saved_out) { open(STDOUT, '>&_SH_SUBSH_SAVOUT'); close(_SH_SUBSH_SAVOUT) }
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_out) 3 4 7
3865 7 ▶ if ($saved_in) { open(STDIN, '<&_SH_SUBSH_SAVIN'); close(_SH_SUBSH_SAVIN) }
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_in) 1 6 7
3866  
3867 7 $LAST_STATUS = $status;
3868 7 return ($status, $i);
3869 }
3870  
3871 # Locate a clause-boundary "esac" at the top level of $s (outside quotes).
3872 # Returns its character offset, or -1. An esac is only at a clause boundary
3873 # when the preceding non-blank character is a ';', '&', or newline, or it is
3874 # at the very start -- so "echo esac" inside a body does not match.
3875 sub _case_top_esac_pos {
3876 95 ▶ my ($s) = @_;
Subroutine _case_top_esac_pos: called CC=23 SCAR=37.5
3877 95 ▶ return -1 unless defined $s && $s ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and $s ne "" 21 74 95
Condition66%2/3
defined $s and $s ne ""
!ll&&!rl&&r
02174
MC/DC50%1/2
defined $s and $s ne ""
defined $s $s ne ""
Truth table66%2/3
A and B
A= defined $sB= $s ne ""
ABresult
0X0
100
111
3878 74 my @c = split //, $s;
3879 74 my $n = scalar @c;
3880 74 my $i = 0; my $sq = 0; my $dq = 0;
3881 74 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 984 52 1036
3882 984 my $ch = $c[$i];
3883 984 ▶ if ($sq) { $sq = 0 if $ch eq "'"; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($sq) 0 984 984
if $ch eq "'" 0 0 0
3884 984 ▶ if ($ch eq "'" && !$dq) { $sq = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $dq) 0 984 984
Condition33%1/3
$ch eq "'" and not $dq
!ll&&!rl&&r
98400
MC/DC0%0/2
$ch eq "'" and not $dq
$ch eq "'" not $dq
Truth table33%1/3
A and not(B)
A= $ch eq "'"B= not $dq
ABresult
0X0
100
111
3885 984 ▶ if ($ch eq '"') { $dq = !$dq; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"") 8 976 984
3886 976 ▶ if (!$dq && lc(substr($s, $i, 4)) eq 'esac') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $dq and lc substr($s, $i, 4) eq "esac") 22 954 976
Condition100%3/3
not $dq and lc substr($s, $i, 4) eq "esac"
!ll&&!rl&&r
3791722
MC/DC100%2/2
not $dq and lc substr($s, $i, 4) eq "esac"
not $dq lc substr($s, $i, 4) eq "esac"
Truth table100%3/3
not(A) and B
A= not $dqB= lc substr($s, $i, 4) eq "esac"
ABresult
0X0
100
111
3887 22 ▶ my $after = ($i + 4 < $n) ? substr($s, $i + 4, 1) : '';
Branch: 1 branch
BranchTrueFalseTotal
$i + 4 < $n ? : 0 22 22
3888 22 ▶ my $aok = ($i + 4 >= $n || $after =~ /\s/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$i + 4 >= $n || $after =~ /\s/ ? : 22 0 22
Condition33%1/3
$i + 4 >= $n or $after =~ /\s/
l!l&&r!l&&!r
2200
MC/DC0%0/2
$i + 4 >= $n or $after =~ /\s/
$i + 4 >= $n $after =~ /\s/
Truth table33%1/3
A or B
A= $i + 4 >= $nB= $after =~ /\s/
ABresult
1X1
011
000
3889 22 my $j = $i - 1;
3890 22 ▶ $j-- while $j >= 0 && ($c[$j] eq ' ' || $c[$j] eq "\t");
Branch: 1 branch
BranchTrueFalseTotal
$j >= 0 and $c[$j] eq " " || $c[$j] eq "\t" and do { --$j } 1 22 23
Condition66%4/6
$j >= 0 and $c[$j] eq " " || $c[$j] eq "\t"
!ll&&!rl&&r
2120
$c[$j] eq " " or $c[$j] eq "\t"
l!l&&r!l&&!r
101
MC/DC0%0/3
$j >= 0 and $c[$j] eq " " || $c[$j] eq "\t"
$j >= 0 $c[$j] eq " " $c[$j] eq "\t"
Truth table50%2/4
A and B or C
A= $j >= 0B= $c[$j] eq " "C= $c[$j] eq "\t"
ABCresult
0XX0
1000
11X1
1011
3891 22 ▶ my $bok = ($j < 0 || $c[$j] eq ';' || $c[$j] eq '&'
Branch: 1 branch
BranchTrueFalseTotal
$j < 0 || $c[$j] eq ';' || $c[$j] eq '&' || $c[$j] eq "\n" ? : 22 0 22
Condition44%4/9
$j < 0 or $c[$j] eq ";" or $c[$j] eq "&" or $c[$j] eq "\n"
l!l&&r!l&&!r
2200
$j < 0 or $c[$j] eq ";" or $c[$j] eq "&"
l!l&&r!l&&!r
2200
$j < 0 or $c[$j] eq ";"
l!l&&r!l&&!r
2110
MC/DC0%0/4
$j < 0 or $c[$j] eq ";" or $c[$j] eq "&" or $c[$j] eq "\n"
$j < 0 $c[$j] eq ";" $c[$j] eq "&" $c[$j] eq "\n"
Truth table40%2/5
A or B or C or D
A= $j < 0B= $c[$j] eq ";"C= $c[$j] eq "&"D= $c[$j] eq "\n"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
3892 || $c[$j] eq "\n") ? 1 : 0;
3893 22 ▶ return $i if $aok && $bok;
Branch: 1 branch
BranchTrueFalseTotal
if $aok and $bok 22 0 22
Condition33%1/3
$aok and $bok
!ll&&!rl&&r
0022
MC/DC0%0/2
$aok and $bok
$aok $bok
Truth table33%1/3
A and B
A= $aokB= $bok
ABresult
0X0
100
111
3894 }
3895 954 $i++;
3896 }
3897 52 return -1;
3898 }
3899  
3900 # Split a case region into clauses on the terminators ;;& / ;; / ;& at the
3901 # top level (outside quotes). Returns a list of [clause_text, terminator].
3902 sub _case_split_clauses {
3903 22 ▶ my ($s) = @_;
Subroutine _case_split_clauses: called CC=13 SCAR=27.0
3904 22 my @out;
3905 22 my $cur = '';
3906 22 my @c = split //, $s;
3907 22 my $n = scalar @c;
3908 22 my $i = 0; my $sq = 0; my $dq = 0;
3909 22 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 984 22 1006
3910 984 my $ch = $c[$i];
3911 984 ▶ if ($sq) { $cur .= $ch; $sq = 0 if $ch eq "'"; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($sq) 0 984 984
if $ch eq "'" 0 0 0
3912 984 ▶ if ($ch eq "'" && !$dq) { $sq = 1; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $dq) 0 984 984
Condition33%1/3
$ch eq "'" and not $dq
!ll&&!rl&&r
98400
MC/DC0%0/2
$ch eq "'" and not $dq
$ch eq "'" not $dq
Truth table33%1/3
A and not(B)
A= $ch eq "'"B= not $dq
ABresult
0X0
100
111
3913 984 ▶ if ($ch eq '"') { $dq = !$dq; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"") 8 976 984
3914 976 ▶ if (!$sq && !$dq) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sq or $dq) 939 37 976
Condition66%2/3
$sq or $dq
l!l&&r!l&&!r
037939
MC/DC50%1/2
$sq or $dq
$sq $dq
Truth table66%2/3
A or B
A= $sqB= $dq
ABresult
1X1
011
000
3915 939 ▶ if (substr($s, $i, 3) eq ';;&') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($s, $i, 3) eq ";;&") 1 938 939
3916 1 push @out, [$cur, ';;&']; $cur = ''; $i += 3; next;
3917 }
3918 938 ▶ if (substr($s, $i, 2) eq ';;') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($s, $i, 2) eq ";;") 47 891 938
3919 47 push @out, [$cur, ';;']; $cur = ''; $i += 2; next;
3920 }
3921 891 ▶ if (substr($s, $i, 2) eq ';&') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($s, $i, 2) eq ";&") 2 889 891
3922 2 push @out, [$cur, ';&']; $cur = ''; $i += 2; next;
3923 }
3924 }
3925 926 $cur .= $ch; $i++;
3926 }
3927 22 ▶ push @out, [$cur, ';;'] if $cur =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $cur =~ /\S/ 0 22 22
3928 22 return @out;
3929 }
3930  
3931 # Parse a clause "pat1|pat2) body" into ($patterns, $body). A leading "("
3932 # is accepted (bash form). The "(" closing the pattern list is the first
3933 # top-level ")" outside quotes and outside a [...] class. Returns
3934 # (undef, undef) when no pattern ")" is present.
3935 sub _case_parse_clause {
3936 29 ▶ my ($s) = @_;
Subroutine _case_parse_clause: called CC=18 SCAR=31.0
3937 29 $s =~ s/\A[\s\n]+//;
3938 29 $s =~ s/\A\(//; # optional leading (
3939 29 my @c = split //, $s;
3940 29 my $n = scalar @c;
3941 29 my $i = 0; my $sq = 0; my $dq = 0; my $cls = 0; my $paren = 0;
3942 29 my $found = -1;
3943 29 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 134 0 134
3944 134 my $ch = $c[$i];
3945 134 ▶ if ($sq) { $sq = 0 if $ch eq "'"; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($sq) 0 134 134
if $ch eq "'" 0 0 0
3946 134 ▶ if ($ch eq "'" && !$dq) { $sq = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $dq) 0 134 134
Condition33%1/3
$ch eq "'" and not $dq
!ll&&!rl&&r
13400
MC/DC0%0/2
$ch eq "'" and not $dq
$ch eq "'" not $dq
Truth table33%1/3
A and not(B)
A= $ch eq "'"B= not $dq
ABresult
0X0
100
111
3947 134 ▶ if ($ch eq '"') { $dq = !$dq; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"") 2 132 134
3948 132 ▶ if (!$sq && !$dq) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sq or $dq) 131 1 132
Condition66%2/3
$sq or $dq
l!l&&r!l&&!r
01131
MC/DC50%1/2
$sq or $dq
$sq $dq
Truth table66%2/3
A or B
A= $sqB= $dq
ABresult
1X1
011
000
3949 131 ▶ if ($ch eq '[') { $cls = 1 }
Branch: 4 branches
BranchTrueFalseTotal
if ($ch eq '[') { } 3 128 131
elsif ($ch eq ']') { } 3 125 128
elsif ($ch eq '(' and not $cls) { } 3 122 125
elsif ($ch eq ')' and not $cls) { } 32 180 212
3950 3 elsif ($ch eq ']') { $cls = 0 }
3951 # An extglob group (?(...)/*(.../@(.../!(...)) contributes a
3952 # balanced '(' ... ')' pair inside the pattern text; only a
3953 # ')' at $paren == 0 is the clause terminator. Tracked
3954 # unconditionally, harmless when there is no '(' present.
3955 3 ▶ elsif ($ch eq '(' && !$cls) { $paren++ }
Condition66%2/3
$ch eq "(" and not $cls
!ll&&!rl&&r
12203
MC/DC50%1/2
$ch eq "(" and not $cls
$ch eq "(" not $cls
Truth table66%2/3
A and not(B)
A= $ch eq "("B= not $cls
ABresult
0X0
100
111
3956 ▶ elsif ($ch eq ')' && !$cls) {
Condition66%2/3
$ch eq ")" and not $cls
!ll&&!rl&&r
90032
MC/DC50%1/2
$ch eq ")" and not $cls
$ch eq ")" not $cls
Truth table66%2/3
A and not(B)
A= $ch eq ")"B= not $cls
ABresult
0X0
100
111
3957 32 ▶ if ($paren > 0) { $paren-- }
Branch: 1 branch
BranchTrueFalseTotal
if ($paren > 0) { } 3 29 32
3958 29 else { $found = $i; last }
3959 }
3960 }
3961 103 $i++;
3962 }
3963 29 ▶ return (undef, undef) if $found < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $found < 0 0 29 29
3964 29 my $patterns = substr($s, 0, $found);
3965 29 ▶ my $body = ($found + 1 <= length($s)) ? substr($s, $found + 1) : '';
Branch: 1 branch
BranchTrueFalseTotal
$found + 1 <= length $s ? : 29 0 29
3966 29 return ($patterns, $body);
3967 }
3968  
3969 # Split a pattern list on top-level "|" (outside quotes and [...] classes).
3970 sub _case_split_patterns {
3971 27 ▶ my ($s) = @_;
Subroutine _case_split_patterns: called CC=19 SCAR=31.4
3972 27 my @out;
3973 27 my $cur = '';
3974 27 my @c = split //, $s;
3975 27 my $n = scalar @c;
3976 27 my $i = 0; my $sq = 0; my $dq = 0; my $cls = 0; my $paren = 0;
3977 27 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 103 27 130
3978 103 my $ch = $c[$i];
3979 103 ▶ if ($sq) { $cur .= $ch; $sq = 0 if $ch eq "'"; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($sq) 0 103 103
if $ch eq "'" 0 0 0
3980 103 ▶ if ($ch eq "'" && !$dq) { $sq = 1; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $dq) 0 103 103
Condition33%1/3
$ch eq "'" and not $dq
!ll&&!rl&&r
10300
MC/DC0%0/2
$ch eq "'" and not $dq
$ch eq "'" not $dq
Truth table33%1/3
A and not(B)
A= $ch eq "'"B= not $dq
ABresult
0X0
100
111
3981 103 ▶ if ($ch eq '"') { $dq = !$dq; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"") 2 101 103
3982 101 ▶ if (!$sq && !$dq) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sq or $dq) 100 1 101
Condition66%2/3
$sq or $dq
l!l&&r!l&&!r
01100
MC/DC50%1/2
$sq or $dq
$sq $dq
Truth table66%2/3
A or B
A= $sqB= $dq
ABresult
1X1
011
000
3983 100 ▶ if ($ch eq '[') { $cls = 1 }
Branch: 5 branches
BranchTrueFalseTotal
if ($ch eq '[') { } 3 97 100
elsif ($ch eq ']') { } 3 94 97
elsif ($ch eq '(' and not $cls) { } 3 91 94
elsif ($ch eq ')' and not $cls and $paren > 0) { } 3 88 91
elsif ($ch eq '|' and not $cls and not $paren) { } 4 165 169
3984 3 elsif ($ch eq ']') { $cls = 0 }
3985 # Parenthesis depth: keeps an extglob group's internal '|'
3986 # (e.g. @(abc|def)) from being mistaken for a pattern
3987 # separator. Tracked unconditionally -- harmless when
3988 # extglob is off, since ordinary patterns rarely contain a
3989 # literal unescaped '('.
3990 3 ▶ elsif ($ch eq '(' && !$cls) { $paren++ }
Condition66%2/3
$ch eq "(" and not $cls
!ll&&!rl&&r
9103
MC/DC50%1/2
$ch eq "(" and not $cls
$ch eq "(" not $cls
Truth table66%2/3
A and not(B)
A= $ch eq "("B= not $cls
ABresult
0X0
100
111
3991 3 ▶ elsif ($ch eq ')' && !$cls && $paren > 0) { $paren-- }
Condition66%4/6
$ch eq ")" and not $cls and $paren > 0
!ll&&!rl&&r
8803
$ch eq ")" and not $cls
!ll&&!rl&&r
8803
MC/DC33%1/3
$ch eq ")" and not $cls and $paren > 0
$ch eq ")" not $cls $paren > 0
Truth table50%2/4
A and not(B) and C
A= $ch eq ")"B= not $clsC= $paren > 0
ABCresult
0XX0
10X0
1100
1111
3992 4 ▶ elsif ($ch eq '|' && !$cls && !$paren) { push @out, $cur; $cur = ''; $i++; next }
Condition83%5/6
$ch eq "|" and not $cls and not $paren
!ll&&!rl&&r
8134
$ch eq "|" and not $cls
!ll&&!rl&&r
8107
MC/DC66%2/3
$ch eq "|" and not $cls and not $paren
$ch eq "|" not $cls not $paren
Truth table75%3/4
A and not(B) and not(C)
A= $ch eq "|"B= not $clsC= not $paren
ABCresult
0XX0
10X0
1100
1111
3993 }
3994 97 $cur .= $ch; $i++;
3995 }
3996 27 push @out, $cur;
3997 27 return @out;
3998 }
3999  
4000 # Match a shell-glob case pattern against a word. Supports * ? and
4001 # character classes [abc] [a-z] [!abc]/[^abc], plus quoting and backslash
4002 # escapes (a quoted or escaped metacharacter is matched literally).
4003 sub _match_pattern {
4004 29 ▶ my ($word, $pat) = @_;
Subroutine _match_pattern: called CC=3 SCAR=11.0
4005 29 ▶ $word = '' unless defined $word;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $word 0 29 29
4006 29 my $re = _case_glob_to_re($pat);
4007 29 ▶ return ($word =~ /\A$re\z/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$word =~ /\A$re\z/ ? : 23 6 29
4008 }
4009  
4010 sub _case_glob_to_re {
4011 33 ▶ my ($pat) = @_;
Subroutine _case_glob_to_re: called CC=36 SCAR=46.8
4012 33 ▶ $pat = '' unless defined $pat;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pat 0 33 33
4013 33 my $re = '';
4014 33 my @c = split //, $pat;
4015 33 my $n = scalar @c;
4016 33 my $i = 0;
4017 33 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 76 33 109
4018 76 my $ch = $c[$i];
4019 76 ▶ if ($_OPT_EXTGLOB && $ch =~ /[?*+\@!]/ && $i+1 < $n && $c[$i+1] eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_OPT_EXTGLOB and $ch =~ /[?*+\@!]/ and $i + 1 < $n and $c[$i + 1] eq "(") 2 74 76
Condition88%8/9
$_OPT_EXTGLOB and $ch =~ /[?*+\@!]/ and $i + 1 < $n and $c[$i + 1] eq "("
!ll&&!rl&&r
7222
$_OPT_EXTGLOB and $ch =~ /[?*+\@!]/ and $i + 1 < $n
!ll&&!rl&&r
7204
$_OPT_EXTGLOB and $ch =~ /[?*+\@!]/
!ll&&!rl&&r
58144
MC/DC75%3/4
$_OPT_EXTGLOB and $ch =~ /[?*+\@!]/ and $i + 1 < $n and $c[$i + 1] eq "("
$_OPT_EXTGLOB $ch =~ /[?*+\@!]/ $i + 1 < $n $c[$i + 1] eq "("
Truth table80%4/5
A and B and C and D
A= $_OPT_EXTGLOBB= $ch =~ /[?*+\@!]/C= $i + 1 < $nD= $c[$i + 1] eq "("
ABCDresult
0XXX0
10XX0
110X0
11100
11111
4020 2 my @eg = _extglob_scan(\@c, $i, \&_case_glob_to_re);
4021 2 ▶ if (@eg) { $re .= $eg[1]; $i = $eg[0]; next }
Branch: 1 branch
BranchTrueFalseTotal
if (@eg) 2 0 2
4022 }
4023 74 ▶ if ($ch eq "'") { # literal single-quoted run
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'") 0 74 74
4024 0 $i++;
4025 0 ▶ while ($i < $n && $c[$i] ne "'") { $re .= quotemeta($c[$i]); $i++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n and $c[$i] ne "'") 0 0 0
Condition0%0/3
$i < $n and $c[$i] ne "'"
!ll&&!rl&&r
000
MC/DC0%0/2
$i < $n and $c[$i] ne "'"
$i < $n $c[$i] ne "'"
Truth table0%0/3
A and B
A= $i < $nB= $c[$i] ne "'"
ABresult
0X0
100
111
4026 0 $i++; next;
4027 }
4028 74 ▶ if ($ch eq '"') { # literal double-quoted run
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"") 1 73 74
4029 1 $i++;
4030 1 ▶ while ($i < $n && $c[$i] ne '"') {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n and $c[$i] ne "\"") 1 1 2
Condition66%2/3
$i < $n and $c[$i] ne "\""
!ll&&!rl&&r
011
MC/DC50%1/2
$i < $n and $c[$i] ne "\""
$i < $n $c[$i] ne "\""
Truth table66%2/3
A and B
A= $i < $nB= $c[$i] ne "\""
ABresult
0X0
100
111
4031 1 ▶ if ($c[$i] eq '\\' && $i + 1 < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c[$i] eq "\\" and $i + 1 < $n) 0 1 1
Condition33%1/3
$c[$i] eq "\\" and $i + 1 < $n
!ll&&!rl&&r
100
MC/DC0%0/2
$c[$i] eq "\\" and $i + 1 < $n
$c[$i] eq "\\" $i + 1 < $n
Truth table33%1/3
A and B
A= $c[$i] eq "\\"B= $i + 1 < $n
ABresult
0X0
100
111
4032 0 $i++; $re .= quotemeta($c[$i]); $i++; next;
4033 }
4034 1 $re .= quotemeta($c[$i]); $i++;
4035 }
4036 1 $i++; next;
4037 }
4038 73 ▶ if ($ch eq '\\') { # escaped literal
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 0 73 73
4039 0 ▶ $i++; $re .= quotemeta($c[$i]) if $i < $n; $i++; next;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $n 0 0 0
4040 }
4041 73 ▶ if ($ch eq '*') { $re .= '.*'; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "*") 7 66 73
4042 66 ▶ if ($ch eq '?') { $re .= '.'; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "?") 0 66 66
4043 66 ▶ if ($ch eq '[') { # character class
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "[") 3 63 66
4044 3 my $j = $i + 1;
4045 3 my $neg = 0;
4046 3 ▶ if ($j < $n && ($c[$j] eq '!' || $c[$j] eq '^')) { $neg = 1; $j++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $c[$j] eq "!" || $c[$j] eq "^") 1 2 3
Condition50%3/6
$j < $n and $c[$j] eq "!" || $c[$j] eq "^"
!ll&&!rl&&r
030
$c[$j] eq "!" or $c[$j] eq "^"
l!l&&r!l&&!r
102
MC/DC0%0/3
$j < $n and $c[$j] eq "!" || $c[$j] eq "^"
$j < $n $c[$j] eq "!" $c[$j] eq "^"
Truth table25%1/4
A and B or C
A= $j < $nB= $c[$j] eq "!"C= $c[$j] eq "^"
ABCresult
0XX0
1000
11X1
1011
4047 3 my $body = '';
4048 3 ▶ if ($j < $n && $c[$j] eq ']') { $body .= '\\]'; $j++ } # leading ] literal
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $c[$j] eq "]") 0 3 3
Condition33%1/3
$j < $n and $c[$j] eq "]"
!ll&&!rl&&r
030
MC/DC0%0/2
$j < $n and $c[$j] eq "]"
$j < $n $c[$j] eq "]"
Truth table33%1/3
A and B
A= $j < $nB= $c[$j] eq "]"
ABresult
0X0
100
111
4049 3 ▶ while ($j < $n && $c[$j] ne ']') {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $c[$j] ne "]") 9 3 12
Condition66%2/3
$j < $n and $c[$j] ne "]"
!ll&&!rl&&r
039
MC/DC50%1/2
$j < $n and $c[$j] ne "]"
$j < $n $c[$j] ne "]"
Truth table66%2/3
A and B
A= $j < $nB= $c[$j] ne "]"
ABresult
0X0
100
111
4050 9 my $cc = $c[$j];
4051 9 ▶ if ($cc eq '\\' && $j + 1 < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc eq "\\" and $j + 1 < $n) 0 9 9
Condition33%1/3
$cc eq "\\" and $j + 1 < $n
!ll&&!rl&&r
900
MC/DC0%0/2
$cc eq "\\" and $j + 1 < $n
$cc eq "\\" $j + 1 < $n
Truth table33%1/3
A and B
A= $cc eq "\\"B= $j + 1 < $n
ABresult
0X0
100
111
4052 0 $body .= '\\' . $c[$j + 1]; $j += 2; next;
4053 }
4054 9 ▶ if ($cc eq '\\' || $cc eq '^' || $cc eq ']') { $body .= '\\' . $cc }
Branch: 1 branch
BranchTrueFalseTotal
if ($cc eq '\\' or $cc eq '^' or $cc eq ']') { } 0 9 9
Condition33%2/6
$cc eq "\\" or $cc eq "^" or $cc eq "]"
l!l&&r!l&&!r
009
$cc eq "\\" or $cc eq "^"
l!l&&r!l&&!r
009
MC/DC0%0/3
$cc eq "\\" or $cc eq "^" or $cc eq "]"
$cc eq "\\" $cc eq "^" $cc eq "]"
Truth table25%1/4
A or B or C
A= $cc eq "\\"B= $cc eq "^"C= $cc eq "]"
ABCresult
1XX1
01X1
0011
0000
4055 9 else { $body .= $cc }
4056 9 $j++;
4057 }
4058 3 ▶ if ($j < $n && $c[$j] eq ']') {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $c[$j] eq "]") 3 0 3
Condition33%1/3
$j < $n and $c[$j] eq "]"
!ll&&!rl&&r
003
MC/DC0%0/2
$j < $n and $c[$j] eq "]"
$j < $n $c[$j] eq "]"
Truth table33%1/3
A and B
A= $j < $nB= $c[$j] eq "]"
ABresult
0X0
100
111
4059 3 ▶ $re .= '[' . ($neg ? '^' : '') . $body . ']';
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 1 2 3
4060 3 $i = $j + 1; next;
4061 }
4062 0 $re .= '\\['; $i++; next; # unterminated [ : literal
4063 }
4064 63 $re .= quotemeta($ch);
4065 63 $i++;
4066 }
4067 33 return $re;
4068 }
4069  
4070 # ----------------------------------------------------------------
4071 # extglob (v0.07): ?(list) *(list) +(list) @(list) !(list) pattern-list
4072 # operators, active only while "shopt -s extglob" is on. Shared by
4073 # _case_glob_to_re() (case patterns) and _glob_to_re() (${VAR%pat} and
4074 # friends). $convert_sub converts one pattern-list alternative (which
4075 # may itself contain nested extglob groups) to a regex fragment.
4076 #
4077 # Returns ($pos_after_close_paren, $regex_fragment), or () when the
4078 # text at $i is not a well-formed extglob group (extglob is then left
4079 # to fall through to its ordinary, literal meaning for that character).
4080 #
4081 # !(list) is approximated as "any run of characters that never forms a
4082 # complete match of one of the alternatives" via a negative lookahead
4083 # repeated per character; this matches the common "exclude these whole
4084 # patterns" usage (e.g. !(*.jpg|*.png)) but, unlike real extglob, is not
4085 # exact when !(...) is combined with more pattern after it in the same
4086 # glob -- documented as a known limitation.
4087 # ----------------------------------------------------------------
4088 sub _extglob_scan {
4089 2 ▶ my ($chars_ref, $i, $convert_sub) = @_;
Subroutine _extglob_scan: called CC=16 SCAR=32.4
4090 2 my @c = @{$chars_ref};
4091 2 my $n = scalar @c;
4092 2 ▶ return () unless $i+1 < $n && $c[$i+1] eq '(';
Branch: 1 branch
BranchTrueFalseTotal
unless $i + 1 < $n and $c[$i + 1] eq "(" 0 2 2
Condition33%1/3
$i + 1 < $n and $c[$i + 1] eq "("
!ll&&!rl&&r
002
MC/DC0%0/2
$i + 1 < $n and $c[$i + 1] eq "("
$i + 1 < $n $c[$i + 1] eq "("
Truth table33%1/3
A and B
A= $i + 1 < $nB= $c[$i + 1] eq "("
ABresult
0X0
100
111
4093 2 my $op = $c[$i];
4094 2 my $depth = 1;
4095 2 my $j = $i + 2;
4096 2 my $body = '';
4097 2 ▶ while ($j < $n && $depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $depth > 0) 20 2 22
Condition66%2/3
$j < $n and $depth > 0
!ll&&!rl&&r
2020
MC/DC50%1/2
$j < $n and $depth > 0
$j < $n $depth > 0
Truth table66%2/3
A and B
A= $j < $nB= $depth > 0
ABresult
0X0
100
111
4098 20 my $cc = $c[$j];
4099 20 ▶ if ($cc eq '(') { $depth++; $body .= $cc; $j++ }
Branch: 3 branches
BranchTrueFalseTotal
if ($cc eq '(') { } 0 20 20
elsif ($cc eq ')') { } 2 18 20
elsif ($cc eq '\\' and $j + 1 < $n) { } 0 18 18
4100 2 ▶ elsif ($cc eq ')') { $depth--; $j++; $body .= $cc if $depth > 0 }
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 0 0 2 2
4101 0 ▶ elsif ($cc eq '\\' && $j+1 < $n) { $body .= $cc . $c[$j+1]; $j += 2 }
Condition33%1/3
$cc eq "\\" and $j + 1 < $n
!ll&&!rl&&r
1800
MC/DC0%0/2
$cc eq "\\" and $j + 1 < $n
$cc eq "\\" $j + 1 < $n
Truth table33%1/3
A and B
A= $cc eq "\\"B= $j + 1 < $n
ABresult
0X0
100
111
4102 18 else { $body .= $cc; $j++ }
4103 }
4104 2 ▶ return () if $depth != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $depth != 0 0 2 2
4105  
4106 2 my @alts = _extglob_split_alts($body);
4107 2 my @re_alts = map { $convert_sub->($_) } @alts;
4108 2 my $inner = '(?:' . join('|', @re_alts) . ')';
4109 2 my $frag;
4110 2 ▶ if ($op eq '?') { $frag = $inner . '?' }
Branch: 5 branches
BranchTrueFalseTotal
if ($op eq '?') { } 0 2 2
elsif ($op eq '*') { } 0 2 2
elsif ($op eq '+') { } 0 2 2
elsif ($op eq '@') { } 1 1 2
elsif ($op eq '!') { } 1 0 1
4111 0 elsif ($op eq '*') { $frag = $inner . '*' }
4112 0 elsif ($op eq '+') { $frag = $inner . '+' }
4113 1 elsif ($op eq '@') { $frag = $inner }
4114 1 elsif ($op eq '!') { $frag = '(?:(?!' . $inner . ').)*' }
4115 0 else { return () }
4116 2 return ($j, $frag);
4117 }
4118  
4119 # _extglob_split_alts: split an extglob pattern-list body on top-level
4120 # '|' (respecting nested parens and backslash escapes).
4121 sub _extglob_split_alts {
4122 2 ▶ my ($body) = @_;
Subroutine _extglob_split_alts: called CC=8 SCAR=23.8
4123 2 my @out;
4124 2 my $cur = '';
4125 2 my @c = split //, $body;
4126 2 my $n = scalar @c;
4127 2 my $i = 0;
4128 2 my $depth = 0;
4129 2 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 18 2 20
4130 18 my $ch = $c[$i];
4131 18 ▶ if ($ch eq '\\' && $i+1 < $n) { $cur .= $ch . $c[$i+1]; $i += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\" and $i + 1 < $n) 0 18 18
Condition33%1/3
$ch eq "\\" and $i + 1 < $n
!ll&&!rl&&r
1800
MC/DC0%0/2
$ch eq "\\" and $i + 1 < $n
$ch eq "\\" $i + 1 < $n
Truth table33%1/3
A and B
A= $ch eq "\\"B= $i + 1 < $n
ABresult
0X0
100
111
4132 18 ▶ if ($ch eq '(') { $depth++; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "(") 0 18 18
4133 18 ▶ if ($ch eq ')') { $depth--; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ")") 0 18 18
4134 18 ▶ if ($ch eq '|' && $depth == 0) { push @out, $cur; $cur = ''; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "|" and $depth == 0) 2 16 18
Condition66%2/3
$ch eq "|" and $depth == 0
!ll&&!rl&&r
1602
MC/DC50%1/2
$ch eq "|" and $depth == 0
$ch eq "|" $depth == 0
Truth table66%2/3
A and B
A= $ch eq "|"B= $depth == 0
ABresult
0X0
100
111
4135 16 $cur .= $ch; $i++;
4136 }
4137 2 push @out, $cur;
4138 2 return @out;
4139 }
4140  
4141 # ----------------------------------------------------------------
4142 # External command
4143 # ----------------------------------------------------------------
4144 # ----------------------------------------------------------------
4145 # _split_sh_pipe: split a SH command line on bare | characters,
4146 # respecting single-quoted, double-quoted, and $(...) regions.
4147 # Returns a list of segment strings; length 1 means no pipe found.
4148 # ----------------------------------------------------------------
4149 # _split_sh_compound: split a SH line on bare && / || / ;
4150 # Returns list of { op => '', cmd => '...' } hashrefs.
4151 # Length 1 means no compound operator found.
4152 # Respects single-quotes, double-quotes, and $(...) nesting.
4153 # ----------------------------------------------------------------
4154 # _sh_strip_redirects: parse SH-style redirections from a command line.
4155 #
4156 # Recognized forms (processed right-to-left, last one wins per fd):
4157 # cmd > file stdout overwrite
4158 # cmd >> file stdout append
4159 # cmd < file stdin
4160 # cmd 2> file stderr overwrite
4161 # cmd 2>> file stderr append
4162 # cmd 2>&1 stderr to stdout (recorded as fd=2, file='&1')
4163 # cmd 1>&2 stdout to stderr (recorded as fd=1, file='&2')
4164 #
4165 # Returns ($clean_cmd, \@redirs) where each redir is [fd, append, file].
4166 # Parsing respects single-quotes, double-quotes, and backslash escapes.
4167 # ----------------------------------------------------------------
4168 # ----------------------------------------------------------------
4169 # _sh_strip_herestring: detect an unquoted "<<< word" (here-string) on
4170 # an already variable-expanded line. Returns ($line_without_it,
4171 # $dequoted_word) when found, or ($line, undef) otherwise. Only the
4172 # first occurrence on the line is honoured (one here-string per
4173 # command, matching the existing single-here-document limitation).
4174 # ----------------------------------------------------------------
4175 sub _sh_strip_herestring {
4176 1003 ▶ my ($line) = @_;
Subroutine _sh_strip_herestring: called CC=32 SCAR=37.5
4177 1003 my @c = split //, $line;
4178 1003 my $n = scalar @c;
4179 1003 my $in_sq = 0;
4180 1003 my $in_dq = 0;
4181 1003 my $i = 0;
4182 1003 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 12736 1001 13737
4183 12736 my $ch = $c[$i];
4184 12736 ▶ if ($in_sq) { $in_sq = 0 if $ch eq "'"; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($in_sq) 331 12405 12736
if $ch eq "'" 42 289 331
4185 12405 ▶ if ($ch eq "'" && !$in_dq) { $in_sq = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $in_dq) 42 12363 12405
Condition100%3/3
$ch eq "'" and not $in_dq
!ll&&!rl&&r
12362142
MC/DC100%2/2
$ch eq "'" and not $in_dq
$ch eq "'" not $in_dq
Truth table100%3/3
A and not(B)
A= $ch eq "'"B= not $in_dq
ABresult
0X0
100
111
4186 12363 ▶ if ($ch eq '"' && !$in_sq) { $in_dq = !$in_dq; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" and not $in_sq) 471 11892 12363
Condition66%2/3
$ch eq "\"" and not $in_sq
!ll&&!rl&&r
118920471
MC/DC50%1/2
$ch eq "\"" and not $in_sq
$ch eq "\"" not $in_sq
Truth table66%2/3
A and not(B)
A= $ch eq "\""B= not $in_sq
ABresult
0X0
100
111
4187 11892 ▶ if (!$in_sq && !$in_dq && $ch eq '\\') { $i += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_sq || $in_dq and $ch eq "\\") 3 11889 11892
Condition83%5/6
not $in_sq || $in_dq and $ch eq "\\"
!ll&&!rl&&r
1689102003
$in_sq || $in_dq
l!l&&r!l&&!r
0168910203
MC/DC66%2/3
not $in_sq || $in_dq and $ch eq "\\"
$in_sq $in_dq $ch eq "\\"
Truth table75%3/4
not(A || B) and C
A= $in_sqB= $in_dqC= $ch eq "\\"
ABCresult
1XX0
01X0
0000
0011
4188 11889 ▶ if (!$in_sq && !$in_dq && $ch eq '<' && $i+2 < $n
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_sq || $in_dq and $ch eq "<" and $i + 2 < $n and $c[$i + 1] eq "<" and $c[$i + 2] eq "<" and not $i + 3 < $n && $c[$i + 3] eq "<") 2 11887 11889
Condition71%15/21
not $in_sq || $in_dq and $ch eq "<" and $i + 2 < $n and $c[$i + 1] eq "<" and $c[$i + 2] eq "<" and not $i + 3 < $n && $c[$i + 3] eq "<"
!ll&&!rl&&r
1188702
not $in_sq || $in_dq and $ch eq "<" and $i + 2 < $n and $c[$i + 1] eq "<" and $c[$i + 2] eq "<"
!ll&&!rl&&r
1188702
not $in_sq || $in_dq and $ch eq "<" and $i + 2 < $n and $c[$i + 1] eq "<"
!ll&&!rl&&r
1187982
not $in_sq || $in_dq and $ch eq "<" and $i + 2 < $n
!ll&&!rl&&r
11879010
not $in_sq || $in_dq and $ch eq "<"
!ll&&!rl&&r
16891019010
$in_sq || $in_dq
l!l&&r!l&&!r
0168910200
$i + 3 < $n && $c[$i + 3] eq '<'
!ll&&!rl&&r
020
MC/DC37%3/8
not $in_sq || $in_dq and $ch eq "<" and $i + 2 < $n and $c[$i + 1] eq "<" and $c[$i + 2] eq "<" and not $i + 3 < $n && $c[$i + 3] eq "<"
$in_sq $in_dq $ch eq "<" $i + 2 < $n $c[$i + 1] eq "<" $c[$i + 2] eq "<" $i + 3 < $n $c[$i + 3] eq '<'
Truth table44%4/9
not(A || B) and C and D and E and F and not(G && H)
A= $in_sqB= $in_dqC= $ch eq "<"D= $i + 2 < $nE= $c[$i + 1] eq "<"F= $c[$i + 2] eq "<"G= $i + 3 < $nH= $c[$i + 3] eq '<'
ABCDEFGHresult
1XXXXXXX0
01XXXXXX0
000XXXXX0
0010XXXX0
00110XXX0
001110XX0
001111110
0011110X1
001111101
4189 && $c[$i+1] eq '<' && $c[$i+2] eq '<'
4190 && !($i+3 < $n && $c[$i+3] eq '<')) {
4191 2 my $before = join('', @c[0 .. $i-1]);
4192 2 my $j = $i + 3;
4193 2 ▶ $j++ while $j < $n && ($c[$j] eq ' ' || $c[$j] eq "\t");
Branch: 1 branch
BranchTrueFalseTotal
$j < $n and $c[$j] eq " " || $c[$j] eq "\t" and do { ++$j } 2 2 4
Condition50%3/6
$j < $n and $c[$j] eq " " || $c[$j] eq "\t"
!ll&&!rl&&r
040
$c[$j] eq " " or $c[$j] eq "\t"
l!l&&r!l&&!r
202
MC/DC0%0/3
$j < $n and $c[$j] eq " " || $c[$j] eq "\t"
$j < $n $c[$j] eq " " $c[$j] eq "\t"
Truth table25%1/4
A and B or C
A= $j < $nB= $c[$j] eq " "C= $c[$j] eq "\t"
ABCresult
0XX0
1000
11X1
1011
4194 2 my $wsq = 0;
4195 2 my $wdq = 0;
4196 2 my $word = '';
4197 2 ▶ while ($j < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 15 2 17
4198 15 my $cc = $c[$j];
4199 15 ▶ if ($wsq) { $word .= $cc; $wsq = 0 if $cc eq "'"; $j++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($wsq) 0 15 15
if $cc eq "'" 0 0 0
4200 15 ▶ if ($cc eq "'" && !$wdq) { $wsq = 1; $word .= $cc; $j++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($cc eq "'" and not $wdq) 0 15 15
Condition33%1/3
$cc eq "'" and not $wdq
!ll&&!rl&&r
1500
MC/DC0%0/2
$cc eq "'" and not $wdq
$cc eq "'" not $wdq
Truth table33%1/3
A and not(B)
A= $cc eq "'"B= not $wdq
ABresult
0X0
100
111
4201 15 ▶ if ($cc eq '"' && !$wsq) { $wdq = !$wdq; $word .= $cc; $j++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($cc eq "\"" and not $wsq) 2 13 15
Condition66%2/3
$cc eq "\"" and not $wsq
!ll&&!rl&&r
1302
MC/DC50%1/2
$cc eq "\"" and not $wsq
$cc eq "\"" not $wsq
Truth table66%2/3
A and not(B)
A= $cc eq "\""B= not $wsq
ABresult
0X0
100
111
4202 13 ▶ last if !$wsq && !$wdq && $cc =~ /\s/;
Branch: 1 branch
BranchTrueFalseTotal
if not $wsq || $wdq and $cc =~ /\s/ 0 13 13
Condition66%4/6
not $wsq || $wdq and $cc =~ /\s/
!ll&&!rl&&r
850
$wsq || $wdq
l!l&&r!l&&!r
085
MC/DC0%0/3
not $wsq || $wdq and $cc =~ /\s/
$wsq $wdq $cc =~ /\s/
Truth table50%2/4
not(A || B) and C
A= $wsqB= $wdqC= $cc =~ /\s/
ABCresult
1XX0
01X0
0000
0011
4203 13 $word .= $cc; $j++;
4204 }
4205 2 my $after = join('', @c[$j .. $n-1]);
4206 2 return ($before . $after, _arr_dequote($word));
4207 }
4208 11887 $i++;
4209 }
4210 1001 return ($line, undef);
4211 }
4212  
4213 # _read_redir_word: read a redirection target starting at index $start,
4214 # honouring '...' and "..." quoting and backslash escapes so that a
4215 # quoted space stays part of the single filename. Stops at the first
4216 # UNQUOTED whitespace or '<'/'>'. Returns ($raw, $next_index); $raw
4217 # still carries its quote characters -- pass it through _arr_dequote()
4218 # before use. Perl 5.005_03 compatible (index-based scan, no regex on
4219 # the whole word, no prototypes).
4220 sub _read_redir_word {
4221 27 ▶ my ($chars, $start, $n) = @_;
Subroutine _read_redir_word: called CC=13 SCAR=27.2
4222 27 my $raw = '';
4223 27 my $j = $start;
4224 27 my $sq = 0;
4225 27 my $dq = 0;
4226 27 ▶ while ($j < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 1243 27 1270
4227 1243 my $c = $chars->[$j];
4228 1243 ▶ if ($sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sq) 58 1185 1243
4229 58 $raw .= $c;
4230 58 ▶ $sq = 0 if $c eq "'";
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "'" 1 57 58
4231 58 $j++; next;
4232 }
4233 1185 ▶ if ($c eq "'" && !$dq) { $sq = 1; $raw .= $c; $j++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "'" and not $dq) 1 1184 1185
Condition66%2/3
$c eq "'" and not $dq
!ll&&!rl&&r
118401
MC/DC50%1/2
$c eq "'" and not $dq
$c eq "'" not $dq
Truth table66%2/3
A and not(B)
A= $c eq "'"B= not $dq
ABresult
0X0
100
111
4234 1184 ▶ if ($c eq '"' && !$sq) { $dq = !$dq; $raw .= $c; $j++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\"" and not $sq) 30 1154 1184
Condition66%2/3
$c eq "\"" and not $sq
!ll&&!rl&&r
1154030
MC/DC50%1/2
$c eq "\"" and not $sq
$c eq "\"" not $sq
Truth table66%2/3
A and not(B)
A= $c eq "\""B= not $sq
ABresult
0X0
100
111
4235 1154 ▶ if ($c eq "\\" && !$sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\\" and not $sq) 0 1154 1154
Condition33%1/3
$c eq "\\" and not $sq
!ll&&!rl&&r
115400
MC/DC0%0/2
$c eq "\\" and not $sq
$c eq "\\" not $sq
Truth table33%1/3
A and not(B)
A= $c eq "\\"B= not $sq
ABresult
0X0
100
111
4236 0 $raw .= $c; $j++;
4237 0 ▶ $raw .= $chars->[$j] if $j < $n;
Branch: 1 branch
BranchTrueFalseTotal
if $j < $n 0 0 0
4238 0 $j++; next;
4239 }
4240 1154 ▶ last if !$dq && $c =~ /[\s<>]/;
Branch: 1 branch
BranchTrueFalseTotal
if not $dq and $c =~ /[\s<>]/ 0 1154 1154
Condition66%2/3
not $dq and $c =~ /[\s<>]/
!ll&&!rl&&r
7394150
MC/DC0%0/2
not $dq and $c =~ /[\s<>]/
not $dq $c =~ /[\s<>]/
Truth table66%2/3
not(A) and B
A= not $dqB= $c =~ /[\s<>]/
ABresult
0X0
100
111
4241 1154 $raw .= $c; $j++;
4242 }
4243 27 return ($raw, $j);
4244 }
4245  
4246 sub _sh_strip_redirects {
4247 1011 ▶ my ($line) = @_;
Subroutine _sh_strip_redirects: called CC=41 SCAR=43.7
4248 1011 my @chars = split //, $line;
4249 1011 my $n = scalar @chars;
4250 1011 my @found;
4251 1011 my $clean = '';
4252 1011 my $in_sq = 0;
4253 1011 my $in_dq = 0;
4254 1011 my $i = 0;
4255  
4256 1011 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 11846 1011 12857
4257 11846 my $ch = $chars[$i];
4258  
4259 # Single-quote passthrough
4260 11846 ▶ if ($in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq) 273 11573 11846
4261 273 ▶ if ($ch eq "'") { $in_sq = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'") 41 232 273
4262 273 $clean .= $ch; $i++; next;
4263 }
4264 11573 ▶ if ($ch eq "'" && !$in_dq) { $in_sq = 1; $clean .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $in_dq) 41 11532 11573
Condition100%3/3
$ch eq "'" and not $in_dq
!ll&&!rl&&r
11531141
MC/DC100%2/2
$ch eq "'" and not $in_dq
$ch eq "'" not $in_dq
Truth table100%3/3
A and not(B)
A= $ch eq "'"B= not $in_dq
ABresult
0X0
100
111
4265  
4266 # Double-quote toggle
4267 11532 ▶ if ($ch eq '"' && !$in_sq) { $in_dq = !$in_dq; $clean .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" and not $in_sq) 450 11082 11532
Condition66%2/3
$ch eq "\"" and not $in_sq
!ll&&!rl&&r
110820450
MC/DC50%1/2
$ch eq "\"" and not $in_sq
$ch eq "\"" not $in_sq
Truth table66%2/3
A and not(B)
A= $ch eq "\""B= not $in_sq
ABresult
0X0
100
111
4268  
4269 # Inside double-quotes: only escape matters
4270 11082 ▶ if ($in_dq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_dq) 1223 9859 11082
4271 1223 ▶ if ($ch eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 15 1208 1223
4272 15 $clean .= $ch; $i++;
4273 15 ▶ $clean .= $chars[$i] if $i < $n; $i++; next;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $n 15 0 15
4274 }
4275 1208 $clean .= $ch; $i++; next;
4276 }
4277  
4278 # Backslash escape outside quotes
4279 9859 ▶ if ($ch eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 2 9857 9859
4280 2 $clean .= $ch; $i++;
4281 2 ▶ $clean .= $chars[$i] if $i < $n; $i++; next;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $n 2 0 2
4282 }
4283  
4284 # 2>&1 or 2>>&1 or 1>&2
4285 9857 ▶ if ($ch =~ /[012]/ && $i+2 < $n
Branch: 2 branches
BranchTrueFalseTotal
if ($ch =~ /[012]/ and $i + 2 < $n and $chars[$i + 1] eq ">" and $i + 3 < $n ? $chars[$i + 2] eq ">" : 0 and $chars[$i + 3] eq "&") 0 9857 9857
$i + 3 < $n ? : 0 0 0
Condition58%7/12
$ch =~ /[012]/ and $i + 2 < $n and $chars[$i + 1] eq ">" and $i + 3 < $n ? $chars[$i + 2] eq ">" : 0 and $chars[$i + 3] eq "&"
!ll&&!rl&&r
985700
$ch =~ /[012]/ and $i + 2 < $n and $chars[$i + 1] eq ">" and $i + 3 < $n ? $chars[$i + 2] eq ">" : 0
!ll&&!rl&&r
985700
$ch =~ /[012]/ and $i + 2 < $n and $chars[$i + 1] eq ">"
!ll&&!rl&&r
95792780
$ch =~ /[012]/ and $i + 2 < $n
!ll&&!rl&&r
9416163278
MC/DC0%0/5
$ch =~ /[012]/ and $i + 2 < $n and $chars[$i + 1] eq ">" and $i + 3 < $n ? $chars[$i + 2] eq ">" : 0 and $chars[$i + 3] eq "&"
$ch =~ /[012]/ $i + 2 < $n $chars[$i + 1] eq ">" $i + 3 < $n ? $chars[$i + 2] eq ">" : 0 $chars[$i + 3] eq "&"
Truth table50%3/6
A and B and C and D and E
A= $ch =~ /[012]/B= $i + 2 < $nC= $chars[$i + 1] eq ">"D= $i + 3 < $n ? $chars[$i + 2] eq ">" : 0E= $chars[$i + 3] eq "&"
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
4286 && $chars[$i+1] eq '>'
4287 && ($i+3 < $n ? $chars[$i+2] eq '>' : 0)
4288 && $chars[$i+3] eq '&') {
4289 # 2>>&1 form (rare but handle)
4290 0 my $fd = int($ch);
4291 0 my $j = $i + 4;
4292 0 my $tgt = '';
4293 0 ▶ while ($j < $n && $chars[$j] =~ /\S/) { $tgt .= $chars[$j]; $j++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $chars[$j] =~ /\S/) 0 0 0
Condition0%0/3
$j < $n and $chars[$j] =~ /\S/
!ll&&!rl&&r
000
MC/DC0%0/2
$j < $n and $chars[$j] =~ /\S/
$j < $n $chars[$j] =~ /\S/
Truth table0%0/3
A and B
A= $j < $nB= $chars[$j] =~ /\S/
ABresult
0X0
100
111
4294 0 push @found, [$fd, 0, "&$tgt"];
4295 0 $i = $j; next;
4296 }
4297 9857 ▶ if ($ch =~ /[012]/ && $i+2 < $n
Branch: 1 branch
BranchTrueFalseTotal
if ($ch =~ /[012]/ and $i + 2 < $n and $chars[$i + 1] eq ">" and $chars[$i + 2] eq "&") 0 9857 9857
Condition66%6/9
$ch =~ /[012]/ and $i + 2 < $n and $chars[$i + 1] eq ">" and $chars[$i + 2] eq "&"
!ll&&!rl&&r
985700
$ch =~ /[012]/ and $i + 2 < $n and $chars[$i + 1] eq ">"
!ll&&!rl&&r
95792780
$ch =~ /[012]/ and $i + 2 < $n
!ll&&!rl&&r
9416163278
MC/DC0%0/4
$ch =~ /[012]/ and $i + 2 < $n and $chars[$i + 1] eq ">" and $chars[$i + 2] eq "&"
$ch =~ /[012]/ $i + 2 < $n $chars[$i + 1] eq ">" $chars[$i + 2] eq "&"
Truth table60%3/5
A and B and C and D
A= $ch =~ /[012]/B= $i + 2 < $nC= $chars[$i + 1] eq ">"D= $chars[$i + 2] eq "&"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
4298 && $chars[$i+1] eq '>' && $chars[$i+2] eq '&') {
4299 0 my $fd = int($ch);
4300 0 my $j = $i + 3;
4301 0 my $tgt = '';
4302 0 ▶ while ($j < $n && $chars[$j] =~ /\S/) { $tgt .= $chars[$j]; $j++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $chars[$j] =~ /\S/) 0 0 0
Condition0%0/3
$j < $n and $chars[$j] =~ /\S/
!ll&&!rl&&r
000
MC/DC0%0/2
$j < $n and $chars[$j] =~ /\S/
$j < $n $chars[$j] =~ /\S/
Truth table0%0/3
A and B
A= $j < $nB= $chars[$j] =~ /\S/
ABresult
0X0
100
111
4303 0 push @found, [$fd, 0, "&$tgt"];
4304 0 $i = $j; next;
4305 }
4306  
4307 # fd> or fd>> (fd is 0,1,2; or implicit 1 when just > or >>)
4308 9857 my $redir_fd = undef;
4309 9857 ▶ if ($ch =~ /[012]/ && $i+1 < $n && $chars[$i+1] eq '>') {
Branch: 3 branches
BranchTrueFalseTotal
if ($ch =~ /[012]/ and $i + 1 < $n and $chars[$i + 1] eq '>') { } 0 9857 9857
elsif ($ch eq '<') { } 11 9846 9857
elsif ($ch eq '>') { } 16 9830 9846
Condition83%5/6
$ch =~ /[012]/ and $i + 1 < $n and $chars[$i + 1] eq ">"
!ll&&!rl&&r
95273300
$ch =~ /[012]/ and $i + 1 < $n
!ll&&!rl&&r
9416111330
MC/DC0%0/3
$ch =~ /[012]/ and $i + 1 < $n and $chars[$i + 1] eq ">"
$ch =~ /[012]/ $i + 1 < $n $chars[$i + 1] eq ">"
Truth table75%3/4
A and B and C
A= $ch =~ /[012]/B= $i + 1 < $nC= $chars[$i + 1] eq ">"
ABCresult
0XX0
10X0
1100
1111
4310 0 $redir_fd = int($ch); $i++;
4311 }
4312 elsif ($ch eq '<') {
4313 # < file (stdin)
4314 11 my $j = $i + 1;
4315 11 ▶ $j++ while $j < $n && $chars[$j] eq ' ';
Branch: 1 branch
BranchTrueFalseTotal
$j < $n and $chars[$j] eq " " and do { ++$j } 11 11 22
Condition66%2/3
$j < $n and $chars[$j] eq " "
!ll&&!rl&&r
01111
MC/DC50%1/2
$j < $n and $chars[$j] eq " "
$j < $n $chars[$j] eq " "
Truth table66%2/3
A and B
A= $j < $nB= $chars[$j] eq " "
ABresult
0X0
100
111
4316 11 my ($raw, $nj) = _read_redir_word(\@chars, $j, $n);
4317 11 ▶ push @found, [0, 0, _arr_dequote($raw)] if $raw ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $raw ne "" 11 0 11
4318 11 $i = $nj; next;
4319 }
4320 elsif ($ch eq '>') {
4321 16 $redir_fd = 1;
4322 }
4323  
4324 9846 ▶ if (defined $redir_fd) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $redir_fd) 16 9830 9846
4325 # Check for >>
4326 16 my $append = 0;
4327 16 ▶ if ($i+1 < $n && $chars[$i+1] eq '>') { $append = 1; $i++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 1 < $n and $chars[$i + 1] eq ">") 2 14 16
Condition66%2/3
$i + 1 < $n and $chars[$i + 1] eq ">"
!ll&&!rl&&r
0142
MC/DC50%1/2
$i + 1 < $n and $chars[$i + 1] eq ">"
$i + 1 < $n $chars[$i + 1] eq ">"
Truth table66%2/3
A and B
A= $i + 1 < $nB= $chars[$i + 1] eq ">"
ABresult
0X0
100
111
4328 # Skip spaces
4329 16 $i++;
4330 16 ▶ $i++ while $i < $n && $chars[$i] eq ' ';
Branch: 1 branch
BranchTrueFalseTotal
$i < $n and $chars[$i] eq " " and do { ++$i } 16 16 32
Condition66%2/3
$i < $n and $chars[$i] eq " "
!ll&&!rl&&r
01616
MC/DC50%1/2
$i < $n and $chars[$i] eq " "
$i < $n $chars[$i] eq " "
Truth table66%2/3
A and B
A= $i < $nB= $chars[$i] eq " "
ABresult
0X0
100
111
4331 # Read filename, honouring quotes (so a quoted space stays in
4332 # the name), then strip the quotes.
4333 16 my ($raw, $nj) = _read_redir_word(\@chars, $i, $n);
4334 16 ▶ push @found, [$redir_fd, $append, _arr_dequote($raw)] if $raw ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $raw ne "" 16 0 16
4335 16 $i = $nj;
4336 16 next;
4337 }
4338  
4339 9830 $clean .= $ch; $i++;
4340 }
4341  
4342 1011 $clean =~ s/\s+\z//;
4343 1011 return ($clean, \@found);
4344 }
4345  
4346 # ----------------------------------------------------------------
4347 # _sh_exec_with_redirs: apply I/O redirections then execute a SH line.
4348 # Perl 5.005_03 compatible: fixed bareword FHs, 2-argument open.
4349 # Supports: > >> < 2> 2>> 2>&1 1>&2
4350 # ----------------------------------------------------------------
4351 sub _sh_exec_with_redirs {
4352 35 ▶ my ($class, $line, $redirs_ref, $opts_ref) = @_;
Subroutine _sh_exec_with_redirs: called CC=44 SCAR=56.1
4353  
4354 # Collect per-fd: stdin, stdout, stderr
4355 35 my ($in_file, $out_file, $out_app, $err_file, $err_app);
4356 35 my $err_to_stdout = 0; # 2>&1
4357 35 my $out_to_stderr = 0; # 1>&2
4358  
4359 35 for my $r (@{$redirs_ref}) {
4360 35 my ($fd, $append, $file) = @{$r};
4361 35 ▶ $file = BATsh::MB::dec($file) unless $file =~ /\A&[12]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /\A&[12]\z/ 35 0 35
4362 35 ▶ if ($fd == 0) { $in_file = $file; }
Branch: 2 branches
BranchTrueFalseTotal
if ($fd == 0) { } 23 12 35
elsif ($fd == 1) { } 12 0 12
4363 elsif ($fd == 1) {
4364 12 ▶ if ($file eq '&2') { $out_to_stderr = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq '&2') { } 0 12 12
4365 12 else { $out_file = $file; $out_app = $append }
4366 }
4367 else { # fd == 2
4368 0 ▶ if ($file eq '&1') { $err_to_stdout = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq '&1') { } 0 0 0
4369 0 else { $err_file = $file; $err_app = $append }
4370 }
4371 }
4372  
4373 35 my $ok = 1;
4374 35 my ($saved_in, $saved_out, $saved_err) = (0, 0, 0);
4375  
4376 # --- stdin ---
4377 35 ▶ if (defined $in_file && $ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $in_file and $ok) 23 12 35
Condition66%2/3
defined $in_file and $ok
!ll&&!rl&&r
12023
MC/DC50%1/2
defined $in_file and $ok
defined $in_file $ok
Truth table66%2/3
A and B
A= defined $in_fileB= $ok
ABresult
0X0
100
111
4378 sysopen(_SH_REDIR_SRC, $in_file, O_RDONLY)
4379 23 ▶ or do { warn "sh: $in_file: $!\n"; $ok = 0 };
Branch: 1 branch
BranchTrueFalseTotal
sysopen _SH_REDIR_SRC, $in_file, 0 or do { warn "sh: ${in_file}: $!\n"; $ok = 0 } 1 22 23
4380 23 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 22 1 23
4381 22 ▶ open(_SH_REDIR_SAVIN, '<&STDIN') or do { $ok = 0 };
Branch: 1 branch
BranchTrueFalseTotal
open _SH_REDIR_SAVIN, "<&STDIN" or do { $ok = 0 } 0 22 22
4382 }
4383 23 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 22 1 23
4384 22 ▶ open(STDIN, '<&_SH_REDIR_SRC') or do { $ok = 0 };
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<&_SH_REDIR_SRC" or do { $ok = 0 } 0 22 22
4385 22 close(_SH_REDIR_SRC);
4386 22 $saved_in = 1;
4387 }
4388 }
4389  
4390 # --- stdout ---
4391 35 ▶ if (defined $out_file && $ok) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $out_file and $ok) { } 12 23 35
elsif ($out_to_stderr and $ok) { } 0 46 46
Condition66%2/3
defined $out_file and $ok
!ll&&!rl&&r
23012
MC/DC50%1/2
defined $out_file and $ok
defined $out_file $ok
Truth table66%2/3
A and B
A= defined $out_fileB= $ok
ABresult
0X0
100
111
4392 sysopen(_SH_REDIR_DST, $out_file,
4393 O_WRONLY | O_CREAT | ($out_app ? O_APPEND : O_TRUNC), 0666)
4394 12 ▶ or do { warn "sh: $out_file: $!\n"; $ok = 0 };
Branch: 2 branches
BranchTrueFalseTotal
sysopen _SH_REDIR_DST, $out_file, 65 | ($out_app ? 1024 : 512), 438 or do { warn "sh: ${out_file}: $!\n"; $ok = 0 } 0 12 12
$out_app ? : 1 11 12
4395 12 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 12 0 12
4396 12 ▶ open(_SH_REDIR_SAVOUT, '>&STDOUT') or do { $ok = 0 };
Branch: 1 branch
BranchTrueFalseTotal
open _SH_REDIR_SAVOUT, ">&STDOUT" or do { $ok = 0 } 0 12 12
4397 }
4398 12 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 12 0 12
4399 12 ▶ open(STDOUT, '>&_SH_REDIR_DST') or do { $ok = 0 };
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&_SH_REDIR_DST" or do { $ok = 0 } 0 12 12
4400 12 close(_SH_REDIR_DST);
4401 12 $saved_out = 1;
4402 }
4403 }
4404 ▶ elsif ($out_to_stderr && $ok) {
Condition33%1/3
$out_to_stderr and $ok
!ll&&!rl&&r
2300
MC/DC0%0/2
$out_to_stderr and $ok
$out_to_stderr $ok
Truth table33%1/3
A and B
A= $out_to_stderrB= $ok
ABresult
0X0
100
111
4405 0 ▶ open(_SH_REDIR_SAVOUT, '>&STDOUT') or do { $ok = 0 };
Branch: 1 branch
BranchTrueFalseTotal
open _SH_REDIR_SAVOUT, ">&STDOUT" or do { $ok = 0 } 0 0 0
4406 0 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 0 0 0
4407 0 ▶ open(STDOUT, '>&STDERR') or do { $ok = 0 };
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&STDERR" or do { $ok = 0 } 0 0 0
4408 0 $saved_out = 1;
4409 }
4410 }
4411  
4412 # --- stderr ---
4413 35 ▶ if (defined $err_file && $ok) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $err_file and $ok) { } 0 35 35
elsif ($err_to_stdout and $ok) { } 0 70 70
Condition33%1/3
defined $err_file and $ok
!ll&&!rl&&r
3500
MC/DC0%0/2
defined $err_file and $ok
defined $err_file $ok
Truth table33%1/3
A and B
A= defined $err_fileB= $ok
ABresult
0X0
100
111
4414 sysopen(_SH_REDIR_DST, $err_file,
4415 O_WRONLY | O_CREAT | ($err_app ? O_APPEND : O_TRUNC), 0666)
4416 0 ▶ or do { warn "sh: $err_file: $!\n"; $ok = 0 };
Branch: 2 branches
BranchTrueFalseTotal
sysopen _SH_REDIR_DST, $err_file, 65 | ($err_app ? 1024 : 512), 438 or do { warn "sh: ${err_file}: $!\n"; $ok = 0 } 0 0 0
$err_app ? : 0 0 0
4417 0 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 0 0 0
4418 0 ▶ open(_SH_REDIR_SAVERR, '>&STDERR') or do { $ok = 0 };
Branch: 1 branch
BranchTrueFalseTotal
open _SH_REDIR_SAVERR, ">&STDERR" or do { $ok = 0 } 0 0 0
4419 }
4420 0 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 0 0 0
4421 0 ▶ open(STDERR, '>&_SH_REDIR_DST') or do { $ok = 0 };
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&_SH_REDIR_DST" or do { $ok = 0 } 0 0 0
4422 0 close(_SH_REDIR_DST);
4423 0 $saved_err = 1;
4424 }
4425 }
4426 ▶ elsif ($err_to_stdout && $ok) {
Condition33%1/3
$err_to_stdout and $ok
!ll&&!rl&&r
3500
MC/DC0%0/2
$err_to_stdout and $ok
$err_to_stdout $ok
Truth table33%1/3
A and B
A= $err_to_stdoutB= $ok
ABresult
0X0
100
111
4427 # Redirect stderr to the current STDOUT (which may itself be redirected)
4428 0 ▶ open(_SH_REDIR_SAVERR, '>&STDERR') or do { $ok = 0 };
Branch: 1 branch
BranchTrueFalseTotal
open _SH_REDIR_SAVERR, ">&STDERR" or do { $ok = 0 } 0 0 0
4429 0 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 0 0 0
4430 0 ▶ open(STDERR, '>&STDOUT') or do { $ok = 0 };
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&STDOUT" or do { $ok = 0 } 0 0 0
4431 0 $saved_err = 1;
4432 }
4433 }
4434  
4435 35 my $rc = 0;
4436 35 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 34 1 35
4437 34 $rc = _exec_line($class, $line, $opts_ref);
4438 }
4439  
4440 # Restore in reverse order
4441 35 ▶ if ($saved_err) { open(STDERR, '>&_SH_REDIR_SAVERR'); close(_SH_REDIR_SAVERR) }
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_err) 0 35 35
4442 35 ▶ if ($saved_out) { open(STDOUT, '>&_SH_REDIR_SAVOUT'); close(_SH_REDIR_SAVOUT) }
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_out) 12 23 35
4443 35 ▶ if ($saved_in) { open(STDIN, '<&_SH_REDIR_SAVIN'); close(_SH_REDIR_SAVIN) }
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_in) 22 13 35
4444  
4445 35 return $rc;
4446 }
4447  
4448 # ----------------------------------------------------------------
4449 # _split_top_semi: split a physical line on TOP-LEVEL ';' only, keeping
4450 # single/double quotes, backticks, $(...) / ${...} / <(...) / >(...)
4451 # and plain (...) groups opaque. Returns the ';'-separated segments
4452 # (the ';' itself is not included). Used by the inline-control
4453 # expander (_inline_expand) and by _inline_has_terminator. Pure Perl
4454 # 5.005_03 (hand-rolled char scan; no regex features).
4455 # ----------------------------------------------------------------
4456 sub _split_top_semi {
4457 47 ▶ my ($line) = @_;
Subroutine _split_top_semi: called CC=26 SCAR=39.9
4458 47 my @segs;
4459 47 my $cur = '';
4460 47 my $in_sq = 0;
4461 47 my $in_dq = 0;
4462 47 my $in_bt = 0; # backtick `...`
4463 47 my $pdep = 0; # ( ) depth (covers $( <( >( and plain ( )
4464 47 my $bdep = 0; # ${ } depth
4465 47 my @c = split //, $line;
4466 47 my $n = scalar @c;
4467 47 my $i = 0;
4468  
4469 47 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 988 47 1035
4470 988 my $ch = $c[$i];
4471  
4472 988 ▶ if ($in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq) 12 976 988
4473 12 ▶ $cur .= $ch; $in_sq = 0 if $ch eq "'"; $i++; next;
Branch: 1 branch
BranchTrueFalseTotal
if $ch eq "'" 3 9 12
4474 }
4475 976 ▶ if ($ch eq "'" && !$in_dq && !$in_bt) { $in_sq = 1; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $in_dq and not $in_bt) 3 973 976
Condition66%4/6
$ch eq "'" and not $in_dq and not $in_bt
!ll&&!rl&&r
97303
$ch eq "'" and not $in_dq
!ll&&!rl&&r
97303
MC/DC33%1/3
$ch eq "'" and not $in_dq and not $in_bt
$ch eq "'" not $in_dq not $in_bt
Truth table50%2/4
A and not(B) and not(C)
A= $ch eq "'"B= not $in_dqC= not $in_bt
ABCresult
0XX0
10X0
1100
1111
4476 973 ▶ if ($ch eq '"' && !$in_bt) { $in_dq = !$in_dq; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" and not $in_bt) 20 953 973
Condition66%2/3
$ch eq "\"" and not $in_bt
!ll&&!rl&&r
953020
MC/DC50%1/2
$ch eq "\"" and not $in_bt
$ch eq "\"" not $in_bt
Truth table66%2/3
A and not(B)
A= $ch eq "\""B= not $in_bt
ABresult
0X0
100
111
4477  
4478 # Backslash escape: copy the next char verbatim
4479 953 ▶ if ($ch eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 0 953 953
4480 0 $cur .= $ch; $i++;
4481 0 ▶ $cur .= $c[$i] if $i < $n; $i++; next;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $n 0 0 0
4482 }
4483  
4484 953 ▶ if ($ch eq '`') { $in_bt = !$in_bt; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "`") 0 953 953
4485  
4486 953 ▶ if (!$in_dq) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_dq) 929 24 953
4487 929 ▶ if ($ch eq '$' && $i+1 < $n && $c[$i+1] eq '{') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "{") 0 929 929
Condition66%4/6
$ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "{"
!ll&&!rl&&r
92630
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
92603
MC/DC0%0/3
$ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "{"
$ch eq "\$" $i + 1 < $n $c[$i + 1] eq "{"
Truth table50%2/4
A and B and C
A= $ch eq "\$"B= $i + 1 < $nC= $c[$i + 1] eq "{"
ABCresult
0XX0
10X0
1100
1111
4488 0 $bdep++; $cur .= '${'; $i += 2; next;
4489 }
4490 929 ▶ if ($bdep > 0 && $ch eq '}') { $bdep--; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($bdep > 0 and $ch eq "}") 0 929 929
Condition33%1/3
$bdep > 0 and $ch eq "}"
!ll&&!rl&&r
92900
MC/DC0%0/2
$bdep > 0 and $ch eq "}"
$bdep > 0 $ch eq "}"
Truth table33%1/3
A and B
A= $bdep > 0B= $ch eq "}"
ABresult
0X0
100
111
4491 929 ▶ if ($ch eq '(') { $pdep++; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "(") 0 929 929
4492 929 ▶ if ($ch eq ')' && $pdep > 0) { $pdep--; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ")" and $pdep > 0) 0 929 929
Condition33%1/3
$ch eq ")" and $pdep > 0
!ll&&!rl&&r
92900
MC/DC0%0/2
$ch eq ")" and $pdep > 0
$ch eq ")" $pdep > 0
Truth table33%1/3
A and B
A= $ch eq ")"B= $pdep > 0
ABresult
0X0
100
111
4493 }
4494  
4495 953 ▶ if (!$in_dq && !$in_bt && $pdep == 0 && $bdep == 0 && $ch eq ';') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_dq || $in_bt and $pdep == 0 and $bdep == 0 and $ch eq ";") 33 920 953
Condition75%9/12
not $in_dq || $in_bt and $pdep == 0 and $bdep == 0 and $ch eq ";"
!ll&&!rl&&r
2489633
not $in_dq || $in_bt and $pdep == 0 and $bdep == 0
!ll&&!rl&&r
240929
not $in_dq || $in_bt and $pdep == 0
!ll&&!rl&&r
240929
$in_dq || $in_bt
l!l&&r!l&&!r
240929
MC/DC40%2/5
not $in_dq || $in_bt and $pdep == 0 and $bdep == 0 and $ch eq ";"
$in_dq $in_bt $pdep == 0 $bdep == 0 $ch eq ";"
Truth table50%3/6
not(A || B) and C and D and E
A= $in_dqB= $in_btC= $pdep == 0D= $bdep == 0E= $ch eq ";"
ABCDEresult
1XXXX0
01XXX0
000XX0
0010X0
001100
001111
4496 33 push @segs, $cur; $cur = ''; $i++;
4497 # collapse a following ';' (e.g. ";;") into the same split so
4498 # empty segments are not produced for the common cases
4499 33 next;
4500 }
4501  
4502 920 $cur .= $ch; $i++;
4503 }
4504 47 push @segs, $cur;
4505 47 return @segs;
4506 }
4507  
4508 # _inline_has_terminator: does the SINGLE physical line hold, as a
4509 # top-level ';'-delimited segment, a bare terminator word ($term, e.g.
4510 # 'fi' / 'done' / 'esac')? Used to detect a fully-inline control
4511 # structure written on one physical line.
4512 sub _inline_has_terminator {
4513 20 ▶ my ($line, $term) = @_;
Subroutine _inline_has_terminator: called CC=4 SCAR=13.9
4514 20 ▶ return 0 unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 20 20
4515 20 for my $seg (_split_top_semi($line)) {
4516 41 my $s = $seg;
4517 41 $s =~ s/\A\s+//; $s =~ s/\s+\z//;
4518 41 ▶ return 1 if lc($s) eq lc($term);
Branch: 1 branch
BranchTrueFalseTotal
if lc $s eq lc $term 4 37 41
4519 }
4520 16 return 0;
4521 }
4522  
4523 # _inline_expand: turn a fully-inline control-structure physical line
4524 # into the list of "logical lines" the multi-line block parsers expect.
4525 # It splits on top-level ';' and then peels a leading 'then'/'do'/'else'
4526 # keyword off its segment onto its own logical line (so "then echo a"
4527 # becomes "then" + "echo a"), which is exactly the shape _parse_if /
4528 # _parse_for / _parse_while consume line-by-line.
4529 sub _inline_expand {
4530 4 ▶ my ($line) = @_;
Subroutine _inline_expand: called CC=6 SCAR=18.3
4531 4 my @out;
4532 4 for my $seg (_split_top_semi($line)) {
4533 14 my $s = $seg;
4534 14 $s =~ s/\A\s+//; $s =~ s/\s+\z//;
4535 14 ▶ next if $s eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "" 0 14 14
4536 14 ▶ if ($s =~ /\A(then|do|else)\b\s*(.*)\z/is) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A(then|do|else)\b\s*(.*)\z/is) { } 6 8 14
4537 6 my ($kw, $tail) = (lc($1), $2);
4538 6 push @out, $kw;
4539 6 ▶ push @out, $tail if defined $tail && $tail =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tail and $tail =~ /\S/ 6 0 6
Condition33%1/3
defined $tail and $tail =~ /\S/
!ll&&!rl&&r
006
MC/DC0%0/2
defined $tail and $tail =~ /\S/
defined $tail $tail =~ /\S/
Truth table33%1/3
A and B
A= defined $tailB= $tail =~ /\S/
ABresult
0X0
100
111
4540 }
4541 else {
4542 8 push @out, $s;
4543 }
4544 }
4545 4 return @out;
4546 }
4547  
4548 # _strip_sh_comment: remove a trailing "# ..." comment from one SH
4549 # physical line. A '#' introduces a comment only when it is unquoted,
4550 # outside any $(...)/${...}/`...` region, and begins a word (preceded by
4551 # the start of line or by whitespace / ; / & / | / '(' ). This leaves
4552 # parameter forms such as $#, ${#var}, ${var#pat} and an in-word '#'
4553 # (echo a#b, http://h#frag) untouched, matching POSIX shells. Pure Perl
4554 # 5.005_03 (hand-rolled scan; no regex features).
4555 sub _strip_sh_comment {
4556 1265 ▶ my ($line) = @_;
Subroutine _strip_sh_comment: called CC=36 SCAR=42.8
4557 1265 ▶ return $line unless defined $line && index($line, '#') >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line and index($line, "#") != -1 1244 21 1265
Condition66%2/3
defined $line and index($line, "#") != -1
!ll&&!rl&&r
0124421
MC/DC50%1/2
defined $line and index($line, "#") != -1
defined $line index($line, "#") != -1
Truth table66%2/3
A and B
A= defined $lineB= index($line, "#") != -1
ABresult
0X0
100
111
4558 21 my @c = split //, $line;
4559 21 my $n = scalar @c;
4560 21 my $in_sq = 0;
4561 21 my $in_dq = 0;
4562 21 my $in_bt = 0;
4563 21 my $pdep = 0;
4564 21 my $bdep = 0;
4565 21 my $prev = ''; # previous scanned char (for word-boundary test)
4566 21 my $i = 0;
4567 21 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 472 20 492
4568 472 my $ch = $c[$i];
4569 472 ▶ if ($in_sq) { $in_sq = 0 if $ch eq "'"; $prev = $ch; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($in_sq) 0 472 472
if $ch eq "'" 0 0 0
4570 472 ▶ if ($ch eq "'" && !$in_dq && !$in_bt) { $in_sq = 1; $prev = $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $in_dq and not $in_bt) 0 472 472
Condition33%2/6
$ch eq "'" and not $in_dq and not $in_bt
!ll&&!rl&&r
47200
$ch eq "'" and not $in_dq
!ll&&!rl&&r
47200
MC/DC0%0/3
$ch eq "'" and not $in_dq and not $in_bt
$ch eq "'" not $in_dq not $in_bt
Truth table25%1/4
A and not(B) and not(C)
A= $ch eq "'"B= not $in_dqC= not $in_bt
ABCresult
0XX0
10X0
1100
1111
4571 472 ▶ if ($ch eq '"' && !$in_bt) { $in_dq = !$in_dq; $prev = $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" and not $in_bt) 16 456 472
Condition66%2/3
$ch eq "\"" and not $in_bt
!ll&&!rl&&r
456016
MC/DC50%1/2
$ch eq "\"" and not $in_bt
$ch eq "\"" not $in_bt
Truth table66%2/3
A and not(B)
A= $ch eq "\""B= not $in_bt
ABresult
0X0
100
111
4572 456 ▶ if ($ch eq '\\') { $prev = 'x'; $i += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 1 455 456
4573 455 ▶ if ($ch eq '`') { $in_bt = !$in_bt; $prev = $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "`") 0 455 455
4574 455 ▶ if (!$in_dq && !$in_bt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_dq or $in_bt) 397 58 455
Condition66%2/3
$in_dq or $in_bt
l!l&&r!l&&!r
580397
MC/DC50%1/2
$in_dq or $in_bt
$in_dq $in_bt
Truth table66%2/3
A or B
A= $in_dqB= $in_bt
ABresult
1X1
011
000
4575 397 ▶ if ($ch eq '$' && $i+1 < $n && $c[$i+1] eq '{') { $bdep++; $prev = '{'; $i += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "{") 19 378 397
Condition83%5/6
$ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "{"
!ll&&!rl&&r
3681019
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
368029
MC/DC66%2/3
$ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "{"
$ch eq "\$" $i + 1 < $n $c[$i + 1] eq "{"
Truth table75%3/4
A and B and C
A= $ch eq "\$"B= $i + 1 < $nC= $c[$i + 1] eq "{"
ABCresult
0XX0
10X0
1100
1111
4576 378 ▶ if ($bdep > 0 && $ch eq '}') { $bdep--; $prev = $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($bdep > 0 and $ch eq "}") 19 359 378
Condition100%3/3
$bdep > 0 and $ch eq "}"
!ll&&!rl&&r
25810119
MC/DC100%2/2
$bdep > 0 and $ch eq "}"
$bdep > 0 $ch eq "}"
Truth table100%3/3
A and B
A= $bdep > 0B= $ch eq "}"
ABresult
0X0
100
111
4577 359 ▶ if ($ch eq '(') { $pdep++; $prev = $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "(") 0 359 359
4578 359 ▶ if ($ch eq ')' && $pdep > 0) { $pdep--; $prev = $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ")" and $pdep > 0) 0 359 359
Condition33%1/3
$ch eq ")" and $pdep > 0
!ll&&!rl&&r
35900
MC/DC0%0/2
$ch eq ")" and $pdep > 0
$ch eq ")" $pdep > 0
Truth table33%1/3
A and B
A= $ch eq ")"B= $pdep > 0
ABresult
0X0
100
111
4579 }
4580 417 ▶ if ($ch eq '#' && !$in_sq && !$in_dq && !$in_bt && $pdep == 0 && $bdep == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "#" and not $in_sq and not $in_dq and not $in_bt and $pdep == 0 and $bdep == 0) 6 411 417
Condition80%12/15
$ch eq "#" and not $in_sq and not $in_dq and not $in_bt and $pdep == 0 and $bdep == 0
!ll&&!rl&&r
397146
$ch eq "#" and not $in_sq and not $in_dq and not $in_bt and $pdep == 0
!ll&&!rl&&r
397020
$ch eq "#" and not $in_sq and not $in_dq and not $in_bt
!ll&&!rl&&r
397020
$ch eq "#" and not $in_sq and not $in_dq
!ll&&!rl&&r
395220
$ch eq "#" and not $in_sq
!ll&&!rl&&r
395022
MC/DC50%3/6
$ch eq "#" and not $in_sq and not $in_dq and not $in_bt and $pdep == 0 and $bdep == 0
$ch eq "#" not $in_sq not $in_dq not $in_bt $pdep == 0 $bdep == 0
Truth table57%4/7
A and not(B) and not(C) and not(D) and E and F
A= $ch eq "#"B= not $in_sqC= not $in_dqD= not $in_btE= $pdep == 0F= $bdep == 0
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
4581 6 ▶ if ($prev eq '' || $prev =~ /\s/
Branch: 1 branch
BranchTrueFalseTotal
if ($prev eq "" or $prev =~ /\s/ or $prev eq ";" or $prev eq "&" or $prev eq "|" or $prev eq "(") 0 5 5
Condition66%10/15
$prev eq "" or $prev =~ /\s/ or $prev eq ";" or $prev eq "&" or $prev eq "|" or $prev eq "("
l!l&&r!l&&!r
105
$prev eq "" or $prev =~ /\s/ or $prev eq ";" or $prev eq "&" or $prev eq "|"
l!l&&r!l&&!r
105
$prev eq "" or $prev =~ /\s/ or $prev eq ";" or $prev eq "&"
l!l&&r!l&&!r
105
$prev eq "" or $prev =~ /\s/ or $prev eq ";"
l!l&&r!l&&!r
105
$prev eq "" or $prev =~ /\s/
l!l&&r!l&&!r
015
MC/DC16%1/6
$prev eq "" or $prev =~ /\s/ or $prev eq ";" or $prev eq "&" or $prev eq "|" or $prev eq "("
$prev eq "" $prev =~ /\s/ $prev eq ";" $prev eq "&" $prev eq "|" $prev eq "("
Truth table28%2/7
A or B or C or D or E or F
A= $prev eq ""B= $prev =~ /\s/C= $prev eq ";"D= $prev eq "&"E= $prev eq "|"F= $prev eq "("
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
4582 || $prev eq ';' || $prev eq '&' || $prev eq '|' || $prev eq '(') {
4583 1 ▶ my $out = ($i > 0) ? join('', @c[0 .. $i-1]) : '';
Branch: 1 branch
BranchTrueFalseTotal
$i > 0 ? : 1 0 1
4584 1 $out =~ s/\s+\z//;
4585 1 return $out;
4586 }
4587 }
4588 416 $prev = $ch; $i++;
4589 }
4590 20 return $line;
4591 }
4592  
4593 # _if_depth_delta: net change in if/fi nesting contributed by one
4594 # physical line, counting only command-position 'if' openers (+1) and
4595 # 'fi' closers (-1). A fully-inline "if ...; then ...; fi" nets to 0.
4596 # Used by _parse_if's body collector so a nested if is not closed by the
4597 # outer 'fi'. Other block types (for/while/case) use different
4598 # terminators (done/esac) and so never affect the if/fi balance.
4599 sub _if_depth_delta {
4600 23 ▶ my ($line) = @_;
Subroutine _if_depth_delta: called CC=5 SCAR=16.1
4601 23 my $d = 0;
4602 23 for my $seg (_split_top_semi($line)) {
4603 25 my $s = $seg;
4604 25 $s =~ s/\A\s+//;
4605 25 $s =~ s/\A(?:then|do|else)\b\s*//i; # peel a leading block keyword
4606 25 my ($w) = ($s =~ /\A(\S+)/);
4607 25 ▶ $w = defined($w) ? lc($w) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $w ? : 25 0 25
4608 25 ▶ $d++ if $w eq 'if';
Branch: 1 branch
BranchTrueFalseTotal
if $w eq "if" 1 24 25
4609 25 ▶ $d-- if $w eq 'fi';
Branch: 1 branch
BranchTrueFalseTotal
if $w eq "fi" 1 24 25
4610 }
4611 23 return $d;
4612 }
4613  
4614 # _find_control_split: when a physical line does NOT begin with a
4615 # control-structure keyword but a control opener (if/for/while/until/
4616 # case/select) appears later in COMMAND position immediately after a
4617 # top-level ';' separator (e.g. x=""; if [ -z "$x" ]; then ...; fi),
4618 # return the byte offset at which that opener begins so the caller can
4619 # run the prefix and then re-dispatch the control structure through the
4620 # normal block parser. Only a ';' separator is honoured (sequential,
4621 # so splitting is semantically safe); a control opener after && / || is
4622 # left alone. Returns undef when there is nothing to split.
4623 sub _find_control_split {
4624 959 ▶ my ($line) = @_;
Subroutine _find_control_split: called CC=39 SCAR=36.7
4625 959 ▶ return undef unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 959 959
4626 959 my @c = split //, $line;
4627 959 my $n = scalar @c;
4628 959 my $in_sq = 0;
4629 959 my $in_dq = 0;
4630 959 my $in_bt = 0;
4631 959 my $pdep = 0;
4632 959 my $bdep = 0;
4633 959 my $i = 0;
4634 959 my $cmd_pos = 1; # are we at the start of a command word?
4635  
4636 959 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 14750 954 15704
4637 14750 my $ch = $c[$i];
4638  
4639 14750 ▶ if ($in_sq) { $in_sq = 0 if $ch eq "'"; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($in_sq) 491 14259 14750
if $ch eq "'" 60 431 491
4640 14259 ▶ if ($ch eq "'" && !$in_dq && !$in_bt) { $in_sq = 1; $cmd_pos = 0; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $in_dq and not $in_bt) 60 14199 14259
Condition83%5/6
$ch eq "'" and not $in_dq and not $in_bt
!ll&&!rl&&r
14199060
$ch eq "'" and not $in_dq
!ll&&!rl&&r
14198160
MC/DC66%2/3
$ch eq "'" and not $in_dq and not $in_bt
$ch eq "'" not $in_dq not $in_bt
Truth table75%3/4
A and not(B) and not(C)
A= $ch eq "'"B= not $in_dqC= not $in_bt
ABCresult
0XX0
10X0
1100
1111
4641 14199 ▶ if ($ch eq '"' && !$in_bt) { $in_dq = !$in_dq; $cmd_pos = 0; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" and not $in_bt) 492 13707 14199
Condition66%2/3
$ch eq "\"" and not $in_bt
!ll&&!rl&&r
137070492
MC/DC50%1/2
$ch eq "\"" and not $in_bt
$ch eq "\"" not $in_bt
Truth table66%2/3
A and not(B)
A= $ch eq "\""B= not $in_bt
ABresult
0X0
100
111
4642 13707 ▶ if ($ch eq '\\') { $i += 2; $cmd_pos = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 18 13689 13707
4643 13689 ▶ if ($ch eq '`') { $in_bt = !$in_bt; $cmd_pos = 0; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "`") 2 13687 13689
4644  
4645 13687 ▶ if (!$in_dq && !$in_bt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_dq or $in_bt) 11780 1907 13687
Condition100%3/3
$in_dq or $in_bt
l!l&&r!l&&!r
18971011780
MC/DC100%2/2
$in_dq or $in_bt
$in_dq $in_bt
Truth table100%3/3
A or B
A= $in_dqB= $in_bt
ABresult
1X1
011
000
4646 11780 ▶ if ($ch eq '$' && $i+1 < $n && $c[$i+1] eq '{') { $bdep++; $i += 2; $cmd_pos = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "{") 38 11742 11780
Condition83%5/6
$ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "{"
!ll&&!rl&&r
1142531738
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
114250355
MC/DC66%2/3
$ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "{"
$ch eq "\$" $i + 1 < $n $c[$i + 1] eq "{"
Truth table75%3/4
A and B and C
A= $ch eq "\$"B= $i + 1 < $nC= $c[$i + 1] eq "{"
ABCresult
0XX0
10X0
1100
1111
4647 11742 ▶ if ($bdep > 0 && $ch eq '}') { $bdep--; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($bdep > 0 and $ch eq "}") 38 11704 11742
Condition100%3/3
$bdep > 0 and $ch eq "}"
!ll&&!rl&&r
1145225238
MC/DC100%2/2
$bdep > 0 and $ch eq "}"
$bdep > 0 $ch eq "}"
Truth table100%3/3
A and B
A= $bdep > 0B= $ch eq "}"
ABresult
0X0
100
111
4648 11704 ▶ if ($ch eq '(') { $pdep++; $i++; $cmd_pos = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "(") 245 11459 11704
4649 11459 ▶ if ($ch eq ')' && $pdep > 0) { $pdep--; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ")" and $pdep > 0) 245 11214 11459
Condition66%2/3
$ch eq ")" and $pdep > 0
!ll&&!rl&&r
112140245
MC/DC50%1/2
$ch eq ")" and $pdep > 0
$ch eq ")" $pdep > 0
Truth table66%2/3
A and B
A= $ch eq ")"B= $pdep > 0
ABresult
0X0
100
111
4650 }
4651  
4652 13121 ▶ if (!$in_dq && !$in_bt && $pdep == 0 && $bdep == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_dq || $in_bt and $pdep == 0 and $bdep == 0) 9864 3257 13121
Condition100%9/9
not $in_dq || $in_bt and $pdep == 0 and $bdep == 0
!ll&&!rl&&r
30052529864
not $in_dq || $in_bt and $pdep == 0
!ll&&!rl&&r
1907109810116
$in_dq || $in_bt
l!l&&r!l&&!r
18971011214
MC/DC100%4/4
not $in_dq || $in_bt and $pdep == 0 and $bdep == 0
$in_dq $in_bt $pdep == 0 $bdep == 0
Truth table100%5/5
not(A || B) and C and D
A= $in_dqB= $in_btC= $pdep == 0D= $bdep == 0
ABCDresult
1XXX0
01XX0
000X0
00100
00111
4653 9864 ▶ if ($ch eq ';') { $cmd_pos = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ";") 58 9806 9864
4654 9806 ▶ if ($ch eq '&' && $i+1 < $n && $c[$i+1] eq '&') { $cmd_pos = 0; $i += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "&" and $i + 1 < $n and $c[$i + 1] eq "&") 6 9800 9806
Condition83%5/6
$ch eq "&" and $i + 1 < $n and $c[$i + 1] eq "&"
!ll&&!rl&&r
980006
$ch eq "&" and $i + 1 < $n
!ll&&!rl&&r
979736
MC/DC66%2/3
$ch eq "&" and $i + 1 < $n and $c[$i + 1] eq "&"
$ch eq "&" $i + 1 < $n $c[$i + 1] eq "&"
Truth table75%3/4
A and B and C
A= $ch eq "&"B= $i + 1 < $nC= $c[$i + 1] eq "&"
ABCresult
0XX0
10X0
1100
1111
4655 9800 ▶ if ($ch eq '|' && $i+1 < $n && $c[$i+1] eq '|') { $cmd_pos = 0; $i += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "|" and $i + 1 < $n and $c[$i + 1] eq "|") 2 9798 9800
Condition83%5/6
$ch eq "|" and $i + 1 < $n and $c[$i + 1] eq "|"
!ll&&!rl&&r
9772262
$ch eq "|" and $i + 1 < $n
!ll&&!rl&&r
9772028
MC/DC66%2/3
$ch eq "|" and $i + 1 < $n and $c[$i + 1] eq "|"
$ch eq "|" $i + 1 < $n $c[$i + 1] eq "|"
Truth table75%3/4
A and B and C
A= $ch eq "|"B= $i + 1 < $nC= $c[$i + 1] eq "|"
ABCresult
0XX0
10X0
1100
1111
4656 9798 ▶ if ($ch eq '|' || $ch eq '&') { $cmd_pos = 0; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "|" or $ch eq "&") 29 9769 9798
Condition100%3/3
$ch eq "|" or $ch eq "&"
l!l&&r!l&&!r
2639769
MC/DC100%2/2
$ch eq "|" or $ch eq "&"
$ch eq "|" $ch eq "&"
Truth table100%3/3
A or B
A= $ch eq "|"B= $ch eq "&"
ABresult
1X1
011
000
4657 9769 ▶ if ($ch =~ /\s/) { $i++; next } # whitespace keeps command position
Branch: 1 branch
BranchTrueFalseTotal
if ($ch =~ /\s/) 1603 8166 9769
4658  
4659 # A non-space, non-separator byte: if we are at command
4660 # position and NOT at the very start (offset 0), test for a
4661 # control opener keyword here.
4662 8166 ▶ if ($cmd_pos && $i > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd_pos and $i > 0) 167 7999 8166
Condition100%3/3
$cmd_pos and $i > 0
!ll&&!rl&&r
7150849167
MC/DC100%2/2
$cmd_pos and $i > 0
$cmd_pos $i > 0
Truth table100%3/3
A and B
A= $cmd_posB= $i > 0
ABresult
0X0
100
111
4663 167 my $tail = join('', @c[$i .. $n-1]);
4664 167 ▶ if ($tail =~ /\A(if|for|while|until|case|select)\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tail =~ /\A(if|for|while|until|case|select)\b/i) 5 162 167
4665 5 return $i;
4666 }
4667 }
4668 8161 $cmd_pos = 0; $i++; next;
4669 }
4670  
4671 3257 $i++;
4672 }
4673 954 return undef;
4674 }
4675  
4676 # ----------------------------------------------------------------
4677 sub _split_sh_compound {
4678 1142 ▶ my ($line) = @_;
Subroutine _split_sh_compound: called CC=63 SCAR=42.0
4679 1142 my @parts;
4680 1142 my $cur = '';
4681 1142 my $in_sq = 0;
4682 1142 my $in_dq = 0;
4683 1142 my $depth = 0; # $( / <( / >( nesting
4684 1142 my $ctl = 0; # control-structure nesting: if/for/while/until/case/
4685 # select ... fi/done/esac. While >0, a ';' / && / ||
4686 # belongs to the compound command and is NOT a
4687 # top-level separator (so cmd | while x; do y; done
4688 # and a && for i in ..; do ..; done stay intact).
4689 1142 my $cmdpos = 1; # true when the next word is in command position
4690 1142 my @chars = split //, $line;
4691 1142 my $n = scalar @chars;
4692 1142 my $i = 0;
4693  
4694 1142 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 13690 1142 14832
4695 13690 my $ch = $chars[$i];
4696  
4697 # Single-quote region
4698 13690 ▶ if ($in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq) 521 13169 13690
4699 521 ▶ if ($ch eq "'") { $in_sq = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'") 67 454 521
4700 521 $cur .= $ch; $i++; next;
4701 }
4702 13169 ▶ if ($ch eq "'" && !$in_dq) { $in_sq = 1; $cur .= $ch; $i++; $cmdpos = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $in_dq) 67 13102 13169
Condition100%3/3
$ch eq "'" and not $in_dq
!ll&&!rl&&r
13101167
MC/DC100%2/2
$ch eq "'" and not $in_dq
$ch eq "'" not $in_dq
Truth table100%3/3
A and not(B)
A= $ch eq "'"B= not $in_dq
ABresult
0X0
100
111
4703  
4704 # Double-quote toggle
4705 13102 ▶ if ($ch eq '"' && !$in_sq) { $in_dq = !$in_dq; $cur .= $ch; $i++; $cmdpos = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" and not $in_sq) 611 12491 13102
Condition66%2/3
$ch eq "\"" and not $in_sq
!ll&&!rl&&r
124910611
MC/DC50%1/2
$ch eq "\"" and not $in_sq
$ch eq "\"" not $in_sq
Truth table66%2/3
A and not(B)
A= $ch eq "\""B= not $in_sq
ABresult
0X0
100
111
4706  
4707 # $( nesting inside double-quotes
4708 12491 ▶ if ($in_dq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_dq) 2321 10170 12491
4709 2321 ▶ if ($ch eq '$' && $i+1 < $n && $chars[$i+1] eq '(') { $depth++ }
Branch: 2 branches
BranchTrueFalseTotal
if ($ch eq '$' and $i + 1 < $n and $chars[$i + 1] eq '(') { } 0 2321 2321
elsif ($ch eq ')' and $depth > 0) { } 0 4641 4641
Condition66%4/6
$ch eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "("
!ll&&!rl&&r
21401810
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
21400181
MC/DC0%0/3
$ch eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "("
$ch eq "\$" $i + 1 < $n $chars[$i + 1] eq "("
Truth table50%2/4
A and B and C
A= $ch eq "\$"B= $i + 1 < $nC= $chars[$i + 1] eq "("
ABCresult
0XX0
10X0
1100
1111
4710 0 ▶ elsif ($ch eq ')' && $depth > 0) { $depth-- }
Condition66%2/3
$ch eq ")" and $depth > 0
!ll&&!rl&&r
232010
MC/DC0%0/2
$ch eq ")" and $depth > 0
$ch eq ")" $depth > 0
Truth table66%2/3
A and B
A= $ch eq ")"B= $depth > 0
ABresult
0X0
100
111
4711 2321 $cur .= $ch; $i++; next;
4712 }
4713  
4714 # Track $( nesting outside quotes
4715 10170 ▶ if ($ch eq '$' && $i+1 < $n && $chars[$i+1] eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "(") 133 10037 10170
Condition83%5/6
$ch eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "("
!ll&&!rl&&r
9757280133
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
97570413
MC/DC66%2/3
$ch eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "("
$ch eq "\$" $i + 1 < $n $chars[$i + 1] eq "("
Truth table75%3/4
A and B and C
A= $ch eq "\$"B= $i + 1 < $nC= $chars[$i + 1] eq "("
ABCresult
0XX0
10X0
1100
1111
4716 133 $depth++; $cur .= $ch; $i++; $cmdpos = 0; next;
4717 }
4718  
4719 # Track <( / >( process-substitution nesting outside quotes
4720 10037 ▶ if (($ch eq '<' || $ch eq '>') && $i+1 < $n && $chars[$i+1] eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "<" || $ch eq ">" and $i + 1 < $n and $chars[$i + 1] eq "(") 2 10035 10037
Condition88%8/9
$ch eq "<" || $ch eq ">" and $i + 1 < $n and $chars[$i + 1] eq "("
!ll&&!rl&&r
9996392
$ch eq "<" || $ch eq ">" and $i + 1 < $n
!ll&&!rl&&r
9996041
$ch eq "<" or $ch eq ">"
l!l&&r!l&&!r
21209996
MC/DC75%3/4
$ch eq "<" || $ch eq ">" and $i + 1 < $n and $chars[$i + 1] eq "("
$ch eq "<" $ch eq ">" $i + 1 < $n $chars[$i + 1] eq "("
Truth table71%5/7
A or B and C and D
A= $ch eq "<"B= $ch eq ">"C= $i + 1 < $nD= $chars[$i + 1] eq "("
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
4721 2 $depth++; $cur .= $ch; $i++; $cmdpos = 0; next;
4722 }
4723 10035 ▶ if ($ch eq ')' && $depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ")" and $depth > 0) 135 9900 10035
Condition100%3/3
$ch eq ")" and $depth > 0
!ll&&!rl&&r
9772128135
MC/DC100%2/2
$ch eq ")" and $depth > 0
$ch eq ")" $depth > 0
Truth table100%3/3
A and B
A= $ch eq ")"B= $depth > 0
ABresult
0X0
100
111
4724 135 $depth--; $cur .= $ch; $i++; $cmdpos = 0; next;
4725 }
4726  
4727 # Inside $(...) don't split on operators or track control words
4728 9900 ▶ if ($depth > 0) { $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($depth > 0) 1287 8613 9900
4729  
4730 # Backslash escape
4731 8613 ▶ if ($ch eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 4 8609 8613
4732 4 $cur .= $ch; $i++;
4733 4 ▶ $cur .= $chars[$i] if $i < $n; $i++; $cmdpos = 0; next;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $n 4 0 4
4734 }
4735  
4736 # && operator
4737 8609 ▶ if ($ch eq '&' && $i+1 < $n && $chars[$i+1] eq '&') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "&" and $i + 1 < $n and $chars[$i + 1] eq "&") 6 8603 8609
Condition66%4/6
$ch eq "&" and $i + 1 < $n and $chars[$i + 1] eq "&"
!ll&&!rl&&r
860306
$ch eq "&" and $i + 1 < $n
!ll&&!rl&&r
860306
MC/DC33%1/3
$ch eq "&" and $i + 1 < $n and $chars[$i + 1] eq "&"
$ch eq "&" $i + 1 < $n $chars[$i + 1] eq "&"
Truth table50%2/4
A and B and C
A= $ch eq "&"B= $i + 1 < $nC= $chars[$i + 1] eq "&"
ABCresult
0XX0
10X0
1100
1111
4738 6 ▶ if ($ctl == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctl == 0) { } 6 0 6
4739 6 push @parts, { op => '', cmd => $cur };
4740 6 push @parts, { op => '&&', cmd => '' };
4741 6 $cur = '';
4742 }
4743 0 else { $cur .= '&&' }
4744 6 $i += 2; $cmdpos = 1; next;
4745 }
4746  
4747 # || operator
4748 8603 ▶ if ($ch eq '|' && $i+1 < $n && $chars[$i+1] eq '|') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "|" and $i + 1 < $n and $chars[$i + 1] eq "|") 2 8601 8603
Condition83%5/6
$ch eq "|" and $i + 1 < $n and $chars[$i + 1] eq "|"
!ll&&!rl&&r
8572292
$ch eq "|" and $i + 1 < $n
!ll&&!rl&&r
8572031
MC/DC66%2/3
$ch eq "|" and $i + 1 < $n and $chars[$i + 1] eq "|"
$ch eq "|" $i + 1 < $n $chars[$i + 1] eq "|"
Truth table75%3/4
A and B and C
A= $ch eq "|"B= $i + 1 < $nC= $chars[$i + 1] eq "|"
ABCresult
0XX0
10X0
1100
1111
4749 2 ▶ if ($ctl == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctl == 0) { } 2 0 2
4750 2 push @parts, { op => '', cmd => $cur };
4751 2 push @parts, { op => '||', cmd => '' };
4752 2 $cur = '';
4753 }
4754 0 else { $cur .= '||' }
4755 2 $i += 2; $cmdpos = 1; next;
4756 }
4757  
4758 # ; separator (not inside any quote or subst)
4759 8601 ▶ if ($ch eq ';') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ";") 66 8535 8601
4760 66 ▶ if ($ctl == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctl == 0) { } 44 22 66
4761 44 push @parts, { op => '', cmd => $cur };
4762 44 push @parts, { op => ';', cmd => '' };
4763 44 $cur = '';
4764 }
4765 22 else { $cur .= ';' }
4766 66 $i++; $cmdpos = 1; next;
4767 }
4768  
4769 # Single | (pipe): not a compound separator here, but the word that
4770 # follows it is in command position (so x | while ... is tracked).
4771 8535 ▶ if ($ch eq '|') { $cur .= $ch; $i++; $cmdpos = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "|") 29 8506 8535
4772  
4773 # '(' opens a group; the next word is in command position.
4774 8506 ▶ if ($ch eq '(') { $cur .= $ch; $i++; $cmdpos = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "(") 27 8479 8506
4775  
4776 # Whitespace: preserved; does not itself change command position.
4777 8479 ▶ if ($ch =~ /\s/) { $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch =~ /\s/) 1692 6787 8479
4778  
4779 # A word starting in command position may be a control keyword.
4780 6787 ▶ if ($cmdpos && $ch =~ /[A-Za-z]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmdpos and $ch =~ /[A-Za-z]/) 1243 5544 6787
Condition100%3/3
$cmdpos and $ch =~ /[A-Za-z]/
!ll&&!rl&&r
5507371243
MC/DC100%2/2
$cmdpos and $ch =~ /[A-Za-z]/
$cmdpos $ch =~ /[A-Za-z]/
Truth table100%3/3
A and B
A= $cmdposB= $ch =~ /[A-Za-z]/
ABresult
0X0
100
111
4781 1243 my $j = $i;
4782 1243 my $w = '';
4783 1243 ▶ while ($j < $n && $chars[$j] =~ /[A-Za-z]/) { $w .= $chars[$j]; $j++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $chars[$j] =~ /[A-Za-z]/) 4651 1243 5894
Condition100%3/3
$j < $n and $chars[$j] =~ /[A-Za-z]/
!ll&&!rl&&r
6211814651
MC/DC100%2/2
$j < $n and $chars[$j] =~ /[A-Za-z]/
$j < $n $chars[$j] =~ /[A-Za-z]/
Truth table100%3/3
A and B
A= $j < $nB= $chars[$j] =~ /[A-Za-z]/
ABresult
0X0
100
111
4784 1243 ▶ my $after = ($j < $n) ? $chars[$j] : '';
Branch: 1 branch
BranchTrueFalseTotal
$j < $n ? : 1181 62 1243
4785 # A full word only if the next char terminates it.
4786 1243 ▶ if ($after eq '' || $after =~ /[\s;&|)]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($after eq "" or $after =~ /[\s;&|)]/) 997 246 1243
Condition100%3/3
$after eq "" or $after =~ /[\s;&|)]/
l!l&&r!l&&!r
62935246
MC/DC100%2/2
$after eq "" or $after =~ /[\s;&|)]/
$after eq "" $after =~ /[\s;&|)]/
Truth table100%3/3
A or B
A= $after eq ""B= $after =~ /[\s;&|)]/
ABresult
1X1
011
000
4787 997 my $lw = lc $w;
4788 997 ▶ if ( $lw eq 'if' || $lw eq 'for' || $lw eq 'while'
Branch: 2 branches
BranchTrueFalseTotal
if ($lw eq 'if' or $lw eq 'for' or $lw eq 'while' or $lw eq 'until' or $lw eq 'case' or $lw eq 'select') { } 11 986 997
elsif ($lw eq 'fi' or $lw eq 'done' or $lw eq 'esac') { } 11 975 986
Condition73%11/15
$lw eq "if" or $lw eq "for" or $lw eq "while" or $lw eq "until" or $lw eq "case" or $lw eq "select"
l!l&&r!l&&!r
110986
$lw eq "if" or $lw eq "for" or $lw eq "while" or $lw eq "until" or $lw eq "case"
l!l&&r!l&&!r
110986
$lw eq "if" or $lw eq "for" or $lw eq "while" or $lw eq "until"
l!l&&r!l&&!r
110986
$lw eq "if" or $lw eq "for" or $lw eq "while"
l!l&&r!l&&!r
47986
$lw eq "if" or $lw eq "for"
l!l&&r!l&&!r
04993
MC/DC33%2/6
$lw eq "if" or $lw eq "for" or $lw eq "while" or $lw eq "until" or $lw eq "case" or $lw eq "select"
$lw eq "if" $lw eq "for" $lw eq "while" $lw eq "until" $lw eq "case" $lw eq "select"
Truth table42%3/7
A or B or C or D or E or F
A= $lw eq "if"B= $lw eq "for"C= $lw eq "while"D= $lw eq "until"E= $lw eq "case"F= $lw eq "select"
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
4789 || $lw eq 'until'|| $lw eq 'case' || $lw eq 'select') {
4790 11 $ctl++;
4791 }
4792 ▶ elsif ($lw eq 'fi' || $lw eq 'done' || $lw eq 'esac') {
Condition66%4/6
$lw eq "fi" or $lw eq "done" or $lw eq "esac"
l!l&&r!l&&!r
110975
$lw eq "fi" or $lw eq "done"
l!l&&r!l&&!r
011975
MC/DC33%1/3
$lw eq "fi" or $lw eq "done" or $lw eq "esac"
$lw eq "fi" $lw eq "done" $lw eq "esac"
Truth table50%2/4
A or B or C
A= $lw eq "fi"B= $lw eq "done"C= $lw eq "esac"
ABCresult
1XX1
01X1
0011
0000
4793 11 ▶ $ctl-- if $ctl > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ctl > 0 11 0 11
4794 }
4795 997 $cur .= $w;
4796 997 $i = $j;
4797 # do/then/else/elif introduce a fresh command position.
4798 997 ▶ $cmdpos = ($lw eq 'do' || $lw eq 'then'
Branch: 1 branch
BranchTrueFalseTotal
$lw eq 'do' || $lw eq 'then' || $lw eq 'else' || $lw eq 'elif' ? : 11 986 997
Condition66%6/9
$lw eq "do" or $lw eq "then" or $lw eq "else" or $lw eq "elif"
l!l&&r!l&&!r
110986
$lw eq "do" or $lw eq "then" or $lw eq "else"
l!l&&r!l&&!r
110986
$lw eq "do" or $lw eq "then"
l!l&&r!l&&!r
110986
MC/DC25%1/4
$lw eq "do" or $lw eq "then" or $lw eq "else" or $lw eq "elif"
$lw eq "do" $lw eq "then" $lw eq "else" $lw eq "elif"
Truth table40%2/5
A or B or C or D
A= $lw eq "do"B= $lw eq "then"C= $lw eq "else"D= $lw eq "elif"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
4799 || $lw eq 'else' || $lw eq 'elif') ? 1 : 0;
4800 997 next;
4801 }
4802 }
4803  
4804 # Ordinary character (part of a word / argument).
4805 5790 $cur .= $ch; $i++; $cmdpos = 0;
4806 }
4807 1142 push @parts, { op => '', cmd => $cur };
4808  
4809 # If only one cmd part with no operators, return single element
4810 1142 my $has_op = 0;
4811 1142 ▶ for my $p (@parts) { $has_op = 1 if $p->{op} ne '' }
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"op"} ne "" 52 1194 1246
4812 1142 ▶ return @parts if $has_op;
Branch: 1 branch
BranchTrueFalseTotal
if $has_op 46 1096 1142
4813 1096 return ({ op => '', cmd => $line });
4814 }
4815  
4816 # ----------------------------------------------------------------
4817 # _exec_sh_compound: execute && / || / ; compound SH commands
4818 # ----------------------------------------------------------------
4819 sub _exec_sh_compound {
4820 46 ▶ my ($class, $parts, $opts_ref) = @_;
Subroutine _exec_sh_compound: called CC=24 SCAR=32.2
4821 46 my $pending_op = '';
4822 46 my $rc = 0;
4823  
4824 # set -e semantics for lists: a command is EXEMPT when a && or ||
4825 # operator appears anywhere AFTER it in the list (bash: every command
4826 # of a && / || list except the one following the final && or ||).
4827 # Commands separated only by ; are ordinary statements and DO trigger.
4828 # Adjudication happens here, per member, so _run_lines must not check
4829 # the list's overall status again: signal that via $_ERREXIT_DONE.
4830 46 my $ncmd = 0;
4831 46 ▶ for my $part (@{$parts}) { $ncmd++ if $part->{op} eq '' }
Branch: 1 branch
BranchTrueFalseTotal
if $part->{"op"} eq "" 98 52 150
4832 46 ▶ $_ERREXIT_DONE = 1 if $ncmd > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $ncmd > 1 46 0 46
4833  
4834 46 my $seen_cmds = 0;
4835 46 for my $k (0 .. $#{$parts}) {
4836 149 my $part = $parts->[$k];
4837 149 my $op = $part->{op};
4838 149 my $cmd = $part->{cmd};
4839 149 $cmd =~ s/\A\s+//; $cmd =~ s/\s+\z//;
4840  
4841 # A prior member executed exit / errexit / break / return: stop.
4842 149 ▶ last if defined $_EXIT_CODE || $_BREAK || $_RETURN;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_EXIT_CODE or $_BREAK or $_RETURN 0 148 148
Condition66%4/6
defined $_EXIT_CODE or $_BREAK or $_RETURN
l!l&&r!l&&!r
10148
defined $_EXIT_CODE or $_BREAK
l!l&&r!l&&!r
10148
MC/DC33%1/3
defined $_EXIT_CODE or $_BREAK or $_RETURN
defined $_EXIT_CODE $_BREAK $_RETURN
Truth table50%2/4
A or B or C
A= defined $_EXIT_CODEB= $_BREAKC= $_RETURN
ABCresult
1XX1
01X1
0011
0000
4843  
4844 148 ▶ if ($op eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq '') { } 97 51 148
4845 97 $seen_cmds++;
4846 # Exempt from set -e when a && or || follows later in the list
4847 97 my $exempt = 0;
4848 97 for my $j ($k + 1 .. $#{$parts}) {
4849 106 my $later = $parts->[$j]{op};
4850 106 ▶ if ($later eq '&&' || $later eq '||') { $exempt = 1; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($later eq "&&" or $later eq "||") 8 98 106
Condition100%3/3
$later eq "&&" or $later eq "||"
l!l&&r!l&&!r
6298
MC/DC100%2/2
$later eq "&&" or $later eq "||"
$later eq "&&" $later eq "||"
Truth table100%3/3
A or B
A= $later eq "&&"B= $later eq "||"
ABresult
1X1
011
000
4851 }
4852 97 my $ran = 0;
4853 # Execute according to pending operator
4854 97 ▶ if ($pending_op eq '') {
Branch: 4 branches
BranchTrueFalseTotal
if ($pending_op eq '') { } 46 51 97
elsif ($pending_op eq '&&') { } 6 45 51
elsif ($pending_op eq '||') { } 2 43 45
elsif ($pending_op eq ';') { } 43 0 43
4855 46 ▶ if ($cmd =~ /\S/) { $ran = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /\S/) 46 0 46
4856 }
4857 elsif ($pending_op eq '&&') {
4858 6 ▶ if ($LAST_STATUS == 0 && $cmd =~ /\S/) { $ran = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($LAST_STATUS == 0 and $cmd =~ /\S/) 3 3 6
Condition66%2/3
$LAST_STATUS == 0 and $cmd =~ /\S/
!ll&&!rl&&r
303
MC/DC50%1/2
$LAST_STATUS == 0 and $cmd =~ /\S/
$LAST_STATUS == 0 $cmd =~ /\S/
Truth table66%2/3
A and B
A= $LAST_STATUS == 0B= $cmd =~ /\S/
ABresult
0X0
100
111
4859 }
4860 elsif ($pending_op eq '||') {
4861 2 ▶ if ($LAST_STATUS != 0 && $cmd =~ /\S/) { $ran = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($LAST_STATUS != 0 and $cmd =~ /\S/) 2 0 2
Condition33%1/3
$LAST_STATUS != 0 and $cmd =~ /\S/
!ll&&!rl&&r
002
MC/DC0%0/2
$LAST_STATUS != 0 and $cmd =~ /\S/
$LAST_STATUS != 0 $cmd =~ /\S/
Truth table33%1/3
A and B
A= $LAST_STATUS != 0B= $cmd =~ /\S/
ABresult
0X0
100
111
4862 }
4863 elsif ($pending_op eq ';') {
4864 43 ▶ if ($cmd =~ /\S/) { $ran = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /\S/) 43 0 43
4865 }
4866 97 ▶ if ($ran) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ran) 94 3 97
4867 94 ▶ if ($exempt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exempt) { } 7 87 94
4868 7 $_ERREXIT_HOLD++;
4869 7 $rc = _exec_line($class, $cmd, $opts_ref);
4870 7 $_ERREXIT_HOLD--;
4871 }
4872 else {
4873 87 $rc = _exec_line($class, $cmd, $opts_ref);
4874 87 _errexit_check($rc);
4875 }
4876 }
4877 97 $pending_op = '';
4878 }
4879 else {
4880 51 $pending_op = $op;
4881 }
4882 }
4883 46 return $rc;
4884 }
4885  
4886 # ----------------------------------------------------------------
4887 sub _split_sh_pipe {
4888 1096 ▶ my ($line) = @_;
Subroutine _split_sh_pipe: called CC=32 SCAR=35.7
4889 1096 my @segs;
4890 1096 my $cur = '';
4891 1096 my $in_sq = 0; # inside single quotes
4892 1096 my $in_dq = 0; # inside double quotes
4893 1096 my $depth = 0; # $( nesting depth
4894 1096 my @chars = split //, $line;
4895 1096 my $n = scalar @chars;
4896 1096 my $i = 0;
4897  
4898 1096 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 15587 1096 16683
4899 15587 my $ch = $chars[$i];
4900  
4901 # Single-quote region: nothing special until closing '
4902 15587 ▶ if ($in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq) 495 15092 15587
4903 495 ▶ if ($ch eq "'") { $in_sq = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'") 61 434 495
4904 495 $cur .= $ch; $i++; next;
4905 }
4906  
4907 # Toggle double-quote
4908 15092 ▶ if ($ch eq '"' && !$in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" and not $in_sq) 554 14538 15092
Condition66%2/3
$ch eq "\"" and not $in_sq
!ll&&!rl&&r
145380554
MC/DC50%1/2
$ch eq "\"" and not $in_sq
$ch eq "\"" not $in_sq
Truth table66%2/3
A and not(B)
A= $ch eq "\""B= not $in_sq
ABresult
0X0
100
111
4909 554 $in_dq = !$in_dq;
4910 554 $cur .= $ch; $i++; next;
4911 }
4912  
4913 # Inside double-quotes only $( nesting matters
4914 14538 ▶ if ($in_dq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_dq) 2140 12398 14538
4915 2140 ▶ if ($ch eq '$' && $i+1 < $n && $chars[$i+1] eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "(") 0 2140 2140
Condition66%4/6
$ch eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "("
!ll&&!rl&&r
19791610
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
19790161
MC/DC0%0/3
$ch eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "("
$ch eq "\$" $i + 1 < $n $chars[$i + 1] eq "("
Truth table50%2/4
A and B and C
A= $ch eq "\$"B= $i + 1 < $nC= $chars[$i + 1] eq "("
ABCresult
0XX0
10X0
1100
1111
4916 0 $depth++; $cur .= $ch; $i++; next;
4917 }
4918 2140 ▶ if ($ch eq ')' && $depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ")" and $depth > 0) 0 2140 2140
Condition66%2/3
$ch eq ")" and $depth > 0
!ll&&!rl&&r
213910
MC/DC0%0/2
$ch eq ")" and $depth > 0
$ch eq ")" $depth > 0
Truth table66%2/3
A and B
A= $ch eq ")"B= $depth > 0
ABresult
0X0
100
111
4919 0 $depth--; $cur .= $ch; $i++; next;
4920 }
4921 # backslash escape inside "
4922 2140 ▶ if ($ch eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 20 2120 2140
4923 20 $cur .= $ch; $i++;
4924 20 ▶ $cur .= $chars[$i] if $i < $n; $i++; next;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $n 20 0 20
4925 }
4926 2120 $cur .= $ch; $i++; next;
4927 }
4928  
4929 # Enter single-quote
4930 12398 ▶ if ($ch eq "'") { $in_sq = 1; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'") 61 12337 12398
4931  
4932 # $( command substitution: consume BOTH characters and bump the
4933 # nesting depth exactly ONCE here, so the standalone-'(' handler
4934 # below does not double-count the '(' of a $( and leave depth
4935 # stuck at 1 after the matching ')'. (That stale depth made a
4936 # bare '|' following a nested $(...) fail to split as a pipe.)
4937 12337 ▶ if ($ch eq '$' && $i+1 < $n && $chars[$i+1] eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "(") 124 12213 12337
Condition83%5/6
$ch eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "("
!ll&&!rl&&r
11974239124
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
119740363
MC/DC66%2/3
$ch eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "("
$ch eq "\$" $i + 1 < $n $chars[$i + 1] eq "("
Truth table75%3/4
A and B and C
A= $ch eq "\$"B= $i + 1 < $nC= $chars[$i + 1] eq "("
ABCresult
0XX0
10X0
1100
1111
4938 124 $depth++; $cur .= '$('; $i += 2; next;
4939 }
4940  
4941 # <( / >( process substitution (v0.07): same "consume both
4942 # characters, bump depth exactly once" treatment as $( above, so
4943 # a '|' inside its body is not mistaken for the outer pipeline's
4944 # separator.
4945 12213 ▶ if (($ch eq '<' || $ch eq '>') && $i+1 < $n && $chars[$i+1] eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "<" || $ch eq ">" and $i + 1 < $n and $chars[$i + 1] eq "(") 2 12211 12213
Condition88%8/9
$ch eq "<" || $ch eq ">" and $i + 1 < $n and $chars[$i + 1] eq "("
!ll&&!rl&&r
12172392
$ch eq "<" || $ch eq ">" and $i + 1 < $n
!ll&&!rl&&r
12172041
$ch eq "<" or $ch eq ">"
l!l&&r!l&&!r
212012172
MC/DC75%3/4
$ch eq "<" || $ch eq ">" and $i + 1 < $n and $chars[$i + 1] eq "("
$ch eq "<" $ch eq ">" $i + 1 < $n $chars[$i + 1] eq "("
Truth table71%5/7
A or B and C and D
A= $ch eq "<"B= $ch eq ">"C= $i + 1 < $nD= $chars[$i + 1] eq "("
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
4946 2 $depth++; $cur .= $ch . '('; $i += 2; next;
4947 }
4948 12211 ▶ if ($ch eq '(' ) { $depth++ if $depth > 0; $cur .= $ch; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($ch eq "(") 122 12089 12211
if $depth > 0 96 26 122
4949 12089 ▶ if ($ch eq ')' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ")") 248 11841 12089
4950 248 ▶ if ($depth > 0) { $depth-- }
Branch: 1 branch
BranchTrueFalseTotal
if ($depth > 0) 222 26 248
4951 248 $cur .= $ch; $i++; next;
4952 }
4953  
4954 # Bare | outside any quote/subst => pipeline separator
4955 11841 ▶ if ($ch eq '|' && $depth == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "|" and $depth == 0) 26 11815 11841
Condition100%3/3
$ch eq "|" and $depth == 0
!ll&&!rl&&r
117981726
MC/DC100%2/2
$ch eq "|" and $depth == 0
$ch eq "|" $depth == 0
Truth table100%3/3
A and B
A= $ch eq "|"B= $depth == 0
ABresult
0X0
100
111
4956 # Peek: || is logical-or, not a pipe
4957 26 ▶ if ($i+1 < $n && $chars[$i+1] eq '|') {
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 1 < $n and $chars[$i + 1] eq "|") 0 26 26
Condition33%1/3
$i + 1 < $n and $chars[$i + 1] eq "|"
!ll&&!rl&&r
0260
MC/DC0%0/2
$i + 1 < $n and $chars[$i + 1] eq "|"
$i + 1 < $n $chars[$i + 1] eq "|"
Truth table33%1/3
A and B
A= $i + 1 < $nB= $chars[$i + 1] eq "|"
ABresult
0X0
100
111
4958 0 $cur .= '||'; $i += 2; next;
4959 }
4960 26 push @segs, $cur;
4961 26 $cur = '';
4962 26 $i++; next;
4963 }
4964  
4965 # Backslash escape (outside quotes)
4966 11815 ▶ if ($ch eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 3 11812 11815
4967 3 $cur .= $ch; $i++;
4968 3 ▶ $cur .= $chars[$i] if $i < $n; $i++; next;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $n 3 0 3
4969 }
4970  
4971 11812 $cur .= $ch; $i++;
4972 }
4973 1096 push @segs, $cur;
4974 1096 return @segs;
4975 }
4976  
4977 # ----------------------------------------------------------------
4978 # _seg_is_control: true when a pipeline segment is a compound/control
4979 # construct that must be run through _run_lines (the block dispatcher)
4980 # rather than _exec_line (which would try to exec "while"/"for"/... as an
4981 # external command). Recognises the command-position control keywords and
4982 # a "( subshell )" group. Enables the common cmd | while read ... ; done
4983 # and cmd | for x in ...; done idioms. Pure Perl 5.005_03.
4984 sub _seg_is_control {
4985 1072 ▶ my ($seg) = @_;
Subroutine _seg_is_control: called CC=11 SCAR=26.3
4986 1072 ▶ my $s = defined($seg) ? $seg : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $seg ? : 1072 0 1072
4987 1072 $s =~ s/\A\s+//;
4988 1072 my ($w) = ($s =~ /\A(\S+)/);
4989 1072 ▶ return 0 unless defined $w;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $w 0 1072 1072
4990 1072 $w = lc $w;
4991 1072 ▶ return 1 if $w eq 'while' || $w eq 'until' || $w eq 'for'
Branch: 1 branch
BranchTrueFalseTotal
if $w eq "while" or $w eq "until" or $w eq "for" or $w eq "if" or $w eq "case" or $w eq "select" 0 1067 1067
Condition73%11/15
$w eq "while" or $w eq "until" or $w eq "for" or $w eq "if" or $w eq "case" or $w eq "select"
l!l&&r!l&&!r
501067
$w eq "while" or $w eq "until" or $w eq "for" or $w eq "if" or $w eq "case"
l!l&&r!l&&!r
501067
$w eq "while" or $w eq "until" or $w eq "for" or $w eq "if"
l!l&&r!l&&!r
501067
$w eq "while" or $w eq "until" or $w eq "for"
l!l&&r!l&&!r
321067
$w eq "while" or $w eq "until"
l!l&&r!l&&!r
301069
MC/DC33%2/6
$w eq "while" or $w eq "until" or $w eq "for" or $w eq "if" or $w eq "case" or $w eq "select"
$w eq "while" $w eq "until" $w eq "for" $w eq "if" $w eq "case" $w eq "select"
Truth table42%3/7
A or B or C or D or E or F
A= $w eq "while"B= $w eq "until"C= $w eq "for"D= $w eq "if"E= $w eq "case"F= $w eq "select"
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
4992 || $w eq 'if' || $w eq 'case' || $w eq 'select';
4993 1067 ▶ return 1 if $s =~ /\A\(/ && $s !~ /\A\(\(/; # ( subshell ) group
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /\A\(/ and not $s =~ /\A\(\(/ 0 1067 1067
Condition33%1/3
$s =~ /\A\(/ and not $s =~ /\A\(\(/
!ll&&!rl&&r
106700
MC/DC0%0/2
$s =~ /\A\(/ and not $s =~ /\A\(\(/
$s =~ /\A\(/ not $s =~ /\A\(\(/
Truth table33%1/3
A and not(B)
A= $s =~ /\A\(/B= not $s =~ /\A\(\(/
ABresult
0X0
100
111
4994 1067 return 0;
4995 }
4996  
4997 # _exec_sh_pipe: run a SH pipeline via temporary files.
4998 # Each segment's stdout feeds the next segment's stdin.
4999 # Perl 5.005_03 compatible: bareword FHs, 2-arg open.
5000 # ----------------------------------------------------------------
5001 sub _exec_sh_pipe {
5002 24 ▶ my ($class, $segs_ref, $opts_ref) = @_;
Subroutine _exec_sh_pipe: called CC=20 SCAR=38.6
5003 24 my @segs = @{$segs_ref};
5004 24 my $n_segs = scalar @segs;
5005 # Tag the stage files with the active pipeline-nesting depth so a nested
5006 # pipeline (reached when a segment contains a $(...) that is itself a
5007 # pipeline) gets distinct stage files and does not clobber this one.
5008 24 local $_PIPE_DEPTH = $_PIPE_DEPTH + 1;
5009 24 my $base = File::Spec->catfile(File::Spec->tmpdir(),
5010 'batsh_shp_' . $$ . '_' . $_PIPE_DEPTH);
5011 # Localize the dup/stage filehandle globs so that a nested pipeline
5012 # (a segment whose $(...) body is itself a pipeline) does not overwrite
5013 # this pipeline's saved STDOUT/STDIN handles. Perl 5.005_03 compatible.
5014 24 local (*_SH_PIPE_RFH, *_SH_PIPE_WFH, *_SH_PIPE_SAVIN, *_SH_PIPE_SAVOUT);
5015 24 my $rc = 0;
5016 24 my $input_f = undef; # tmpfile that feeds this segment's STDIN
5017  
5018 24 for my $idx (0 .. $n_segs - 1) {
5019 50 my $seg = $segs[$idx];
5020 50 $seg =~ s/\A\s+//; $seg =~ s/\s+\z//;
5021 50 ▶ next unless $seg =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $seg =~ /\S/ 0 50 50
5022  
5023 50 ▶ my $is_last = ($idx == $n_segs - 1) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$idx == $n_segs - 1 ? : 24 26 50
5024 50 my $output_f = undef;
5025  
5026 # --- redirect STDIN from previous segment's output ---
5027 50 my $saved_in = 0;
5028 50 ▶ if (defined $input_f && -f $input_f) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input_f and -f $input_f) 26 24 50
Condition66%2/3
defined $input_f and -f $input_f
!ll&&!rl&&r
24026
MC/DC50%1/2
defined $input_f and -f $input_f
defined $input_f -f $input_f
Truth table66%2/3
A and B
A= defined $input_fB= -f $input_f
ABresult
0X0
100
111
5029 open(_SH_PIPE_RFH, $input_f)
5030 26 ▶ or do { warn "SH pipe: open $input_f: $!\n"; last };
Branch: 1 branch
BranchTrueFalseTotal
open _SH_PIPE_RFH, $input_f or do { warn "SH pipe: open ${input_f}: $!\n"; last } 0 26 26
5031 open(_SH_PIPE_SAVIN, '<&STDIN')
5032 26 ▶ or do { close(_SH_PIPE_RFH); last };
Branch: 1 branch
BranchTrueFalseTotal
open _SH_PIPE_SAVIN, "<&STDIN" or do { close _SH_PIPE_RFH; last } 0 26 26
5033 open(STDIN, '<&_SH_PIPE_RFH')
5034 26 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<&_SH_PIPE_RFH" or do { close _SH_PIPE_RFH; open STDIN, "<&_SH_PIPE_SAVIN"; close _SH_PIPE_SAVIN; last } 0 26 26
5035 0 close(_SH_PIPE_RFH);
5036 0 open(STDIN, '<&_SH_PIPE_SAVIN'); close(_SH_PIPE_SAVIN);
5037 0 last;
5038 };
5039 26 close(_SH_PIPE_RFH);
5040 26 $saved_in = 1;
5041 }
5042  
5043 # --- redirect STDOUT to next segment's input file ---
5044 50 my $saved_out = 0;
5045 50 ▶ unless ($is_last) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_last) 26 24 50
5046 26 $output_f = _shp_tempfile("${base}_${idx}");
5047 26 ▶ if (!defined $output_f) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $output_f) 0 26 26
5048 0 ▶ if ($saved_in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_in) 0 0 0
5049 0 open(STDIN, '<&_SH_PIPE_SAVIN'); close(_SH_PIPE_SAVIN);
5050 }
5051 0 warn "SH pipe: cannot create stage temp file\n";
5052 0 last;
5053 }
5054 open(_SH_PIPE_SAVOUT, '>&STDOUT')
5055 26 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
open _SH_PIPE_SAVOUT, ">&STDOUT" or do { close _SH_PIPE_WFH; if ($saved_in) { open STDIN, "<&_SH_PIPE_SAVIN"; close _SH_PIPE_SAVIN; } ; last } 0 26 26
5056 0 close(_SH_PIPE_WFH);
5057 0 ▶ if ($saved_in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_in) 0 0 0
5058 0 open(STDIN, '<&_SH_PIPE_SAVIN'); close(_SH_PIPE_SAVIN);
5059 }
5060 0 last;
5061 };
5062 open(STDOUT, '>&_SH_PIPE_WFH')
5063 26 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&_SH_PIPE_WFH" or do { close _SH_PIPE_WFH; open STDOUT, ">&_SH_PIPE_SAVOUT"; close _SH_PIPE_SAVOUT; if ($saved_in) { open STDIN, "<&_SH_PIPE_SAVIN"; close _SH_PIPE_SAVIN; } ; last } 0 26 26
5064 0 close(_SH_PIPE_WFH);
5065 0 open(STDOUT, '>&_SH_PIPE_SAVOUT'); close(_SH_PIPE_SAVOUT);
5066 0 ▶ if ($saved_in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_in) 0 0 0
5067 0 open(STDIN, '<&_SH_PIPE_SAVIN'); close(_SH_PIPE_SAVIN);
5068 }
5069 0 last;
5070 };
5071 26 close(_SH_PIPE_WFH);
5072 26 $saved_out = 1;
5073 }
5074  
5075 # --- execute the segment as a SH line ---
5076 # (_exec_line_impl routes a control/compound segment such as
5077 # "while read ...; do ...; done" through the block runner, so the
5078 # common cmd | while read ... idiom works.)
5079 50 $rc = _exec_line($class, $seg, $opts_ref);
5080  
5081 # --- restore STDOUT ---
5082 50 ▶ if ($saved_out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_out) 26 24 50
5083 26 open(STDOUT, '>&_SH_PIPE_SAVOUT');
5084 26 close(_SH_PIPE_SAVOUT);
5085 }
5086  
5087 # --- restore STDIN and remove input tmpfile ---
5088 50 ▶ if ($saved_in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_in) 26 24 50
5089 26 open(STDIN, '<&_SH_PIPE_SAVIN');
5090 26 close(_SH_PIPE_SAVIN);
5091 26 unlink $input_f;
5092 26 @_SHP_TMPFILES = grep { $_ ne $input_f } @_SHP_TMPFILES;
5093 }
5094  
5095 50 $input_f = $output_f;
5096 }
5097  
5098 24 ▶ if (defined $input_f && -f $input_f) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input_f and -f $input_f) 0 24 24
Condition33%1/3
defined $input_f and -f $input_f
!ll&&!rl&&r
2400
MC/DC0%0/2
defined $input_f and -f $input_f
defined $input_f -f $input_f
Truth table33%1/3
A and B
A= defined $input_fB= -f $input_f
ABresult
0X0
100
111
5099 0 unlink $input_f;
5100 0 @_SHP_TMPFILES = grep { $_ ne $input_f } @_SHP_TMPFILES;
5101 }
5102 24 return $rc;
5103 }
5104  
5105 # _shp_tempfile: create a unique, empty temp file for one pipeline stage's
5106 # stdout->stdin bridge, using sysopen(...O_CREAT|O_EXCL...) to avoid symlink
5107 # races (mirrors _bg_tempfile / _hd_tempfile / _subst_tempfile). Opens the
5108 # package bareword filehandle _SH_PIPE_WFH and returns the final path, or
5109 # undef on failure. $stub is the depth/index-tagged path prefix so stage
5110 # files stay distinguishable for debugging; a sequence number is appended
5111 # to make the final name unique and unpredictable.
5112 sub _shp_tempfile {
5113 27 ▶ my ($stub) = @_;
Subroutine _shp_tempfile: called CC=3 SCAR=11.1
5114 27 my $attempt = 0;
5115 27 ▶ while ($attempt < 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attempt < 1000) 28 0 28
5116 28 $_PIPE_SEQ++;
5117 28 $attempt++;
5118 28 my $path = $stub . '_' . $_PIPE_SEQ . '.tmp';
5119 28 ▶ if (sysopen(_SH_PIPE_WFH, $path, O_WRONLY | O_CREAT | O_EXCL, 0600)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysopen _SH_PIPE_WFH, $path, 193, 384) 27 1 28
5120 27 push @_SHP_TMPFILES, $path;
5121 27 return $path;
5122 }
5123 # EEXIST or transient error: retry with next sequence number
5124 }
5125 0 return undef;
5126 }
5127  
5128 # ----------------------------------------------------------------
5129 # Pattern helpers for ${var%pat}, ${var#pat}, ${var/pat/rep}
5130 # Converts glob-style pattern to Perl regex (*, ?, [abc]).
5131 # ----------------------------------------------------------------
5132 # ----------------------------------------------------------------
5133 # _glob_expand: expand a single word that contains unquoted glob
5134 # metacharacters (* ? [...]) into a sorted list of matching pathnames.
5135 # Returns the original word unchanged if no matches are found (POSIX
5136 # "nullglob off" behaviour, which is the shell default).
5137 # Only words that were NOT wrapped in quotes are eligible; the caller
5138 # is responsible for passing only unquoted words.
5139 # ----------------------------------------------------------------
5140 sub _glob_expand {
5141 46 ▶ my ($word) = @_;
Subroutine _glob_expand: called CC=3 SCAR=11.0
5142 # Fast path: no metacharacters
5143 46 ▶ return ($word) unless $word =~ /[*?\[]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $word =~ /[*?\[]/ 0 46 46
5144 # The matcher has to see the real pathname, so a backslash still held
5145 # behind its expansion-result sentinel (a Windows path that came out
5146 # of a variable) is restored first. What comes back is data again and
5147 # is re-protected, so the quote-removal stage downstream cannot eat
5148 # the separators of the very names it just matched.
5149 46 my $pat = _unprotect_lit($word);
5150 46 my @matches = map { _protect_lit(BATsh::MB::enc($_)) }
5151 _glob_paths(BATsh::MB::dec($pat));
5152 46 ▶ return @matches ? @matches : ($word);
Branch: 1 branch
BranchTrueFalseTotal
@matches ? : 14 32 46
5153 }
5154  
5155 # ----------------------------------------------------------------
5156 # _glob_paths: pathname expansion in pure Perl (v0.09)
5157 # ----------------------------------------------------------------
5158 # Perl's built-in glob() cannot serve a shell that has to run on Windows:
5159 # it reads a backslash as an ESCAPE character, so the perfectly ordinary
5160 # pattern C:\dir\*.txt is understood as C:dir*.txt and matches
5161 # nothing, and when a pattern matches nothing glob() hands it back with
5162 # those backslashes already deleted. File::Glob's GLOB_NOESCAPE would
5163 # avoid that, but File::Glob arrived in Perl 5.6 and BATsh supports
5164 # 5.005_03. glob() also offers no extglob support and no say over
5165 # dot-files. The pattern is therefore matched here, one path segment at
5166 # a time, with this module's own _glob_to_re() translator:
5167 #
5168 # * the pathname separator is "/" everywhere, and "\" as well on
5169 # Windows, where that is simply how a path is written. On Unix a
5170 # backslash is an ordinary character in a filename and stays one, so
5171 # a directory really named "a\b" keeps working;
5172 # * a segment holding no metacharacter is appended literally;
5173 # * a leading "." is matched only by a pattern that starts with "." --
5174 # the shell rule, which glob() follows too;
5175 # * every segment but the last has to name a directory;
5176 # * the separators the caller wrote come back unchanged, so a Windows
5177 # pattern yields Windows pathnames;
5178 # * a negated class "[!abc]" negates, and only names that exist are
5179 # returned, sorted, with case ignored on Windows.
5180 #
5181 # Returns the (possibly empty) list of matching pathnames. The caller
5182 # decides what an empty list means; POSIX leaves the word untouched.
5183 # Perl 5.005_03 safe: bareword directory handle, no File::Glob, no
5184 # File::Spec, and one directory level is read and closed at a time so the
5185 # single global handle is never nested.
5186 # ----------------------------------------------------------------
5187 sub _glob_paths {
5188 48 ▶ my ($pat) = @_;
Subroutine _glob_paths: called CC=47 SCAR=43.1
5189 48 ▶ return () unless defined $pat && $pat ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pat and $pat ne "" 0 48 48
Condition33%1/3
defined $pat and $pat ne ""
!ll&&!rl&&r
0048
MC/DC0%0/2
defined $pat and $pat ne ""
defined $pat $pat ne ""
Truth table33%1/3
A and B
A= defined $patB= $pat ne ""
ABresult
0X0
100
111
5190 48 ▶ my $win = ($^O =~ /MSWin32|dos|os2|cygwin/i) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /MSWin32|dos|os2|cygwin/i ? : 0 48 48
5191 48 ▶ my $sepclass = $win ? '[\\\\/]' : '[/]';
Branch: 1 branch
BranchTrueFalseTotal
$win ? : 0 48 48
5192 48 ▶ my $nonsep = $win ? '[^\\\\/]' : '[^/]';
Branch: 1 branch
BranchTrueFalseTotal
$win ? : 0 48 48
5193  
5194 # The leading root -- a Windows drive letter and/or separators -- is
5195 # never a pattern and is copied through as written.
5196 48 my $rest = $pat;
5197 48 my $prefix = '';
5198 48 ▶ if ($win && $rest =~ s/\A([A-Za-z]:)//) { $prefix = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($win and $rest =~ s/\A([A-Za-z]:)//) 0 48 48
Condition33%1/3
$win and $rest =~ s/\A([A-Za-z]:)//
!ll&&!rl&&r
4800
MC/DC0%0/2
$win and $rest =~ s/\A([A-Za-z]:)//
$win $rest =~ s/\A([A-Za-z]:)//
Truth table33%1/3
A and B
A= $winB= $rest =~ s/\A([A-Za-z]:)//
ABresult
0X0
100
111
5199 48 ▶ if ($rest =~ s/\A($sepclass+)//) { $prefix .= $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/\A($sepclass+)//) 2 46 48
5200  
5201 # Split the remainder into [segment, following separators] pairs.
5202 48 my @parts = ();
5203 48 ▶ while ($rest ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest ne "") 77 48 125
5204 77 ▶ if ($rest =~ s/\A($nonsep+)($sepclass+)//) { push @parts, [$1, $2] }
Branch: 3 branches
BranchTrueFalseTotal
if ($rest =~ s/\A($nonsep+)($sepclass+)//) { } 30 47 77
elsif ($rest =~ s/\A($nonsep+)\z//) { } 47 0 47
elsif ($rest =~ s/\A($sepclass+)//) { } 0 0 0
5205 47 elsif ($rest =~ s/\A($nonsep+)\z//) { push @parts, [$1, ''] }
5206 0 elsif ($rest =~ s/\A($sepclass+)//) { next }
5207 0 else { last }
5208 }
5209 48 ▶ return () unless @parts;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts 0 48 48
5210  
5211 48 my @cur = ($prefix);
5212 48 my $done = 0;
5213 48 my $total = scalar(@parts);
5214 48 for my $p (@parts) {
5215 77 my ($seg, $sep) = @{$p};
5216 77 $done++;
5217 77 ▶ my $last = ($done == $total) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$done == $total ? : 48 29 77
5218  
5219 # Is this segment a pattern at all? An unbalanced "[" is not:
5220 # treating it as a character class would let a stray bracket in
5221 # "echo [$dir]" match a one-character filename.
5222 77 my $is_pat = 0;
5223 77 ▶ $is_pat = 1 if $seg =~ /[*?]/;
Branch: 1 branch
BranchTrueFalseTotal
if $seg =~ /[*?]/ 20 57 77
5224 77 ▶ $is_pat = 1 if $seg =~ /\[[^\]]+\]/;
Branch: 1 branch
BranchTrueFalseTotal
if $seg =~ /\[[^\]]+\]/ 23 54 77
5225 77 ▶ $is_pat = 1 if $_OPT_EXTGLOB && $seg =~ /[?*+\@!]\(/;
Branch: 1 branch
BranchTrueFalseTotal
if $_OPT_EXTGLOB and $seg =~ /[?*+\@!]\(/ 0 77 77
Condition33%1/3
$_OPT_EXTGLOB and $seg =~ /[?*+\@!]\(/
!ll&&!rl&&r
7700
MC/DC0%0/2
$_OPT_EXTGLOB and $seg =~ /[?*+\@!]\(/
$_OPT_EXTGLOB $seg =~ /[?*+\@!]\(/
Truth table33%1/3
A and B
A= $_OPT_EXTGLOBB= $seg =~ /[?*+\@!]\(/
ABresult
0X0
100
111
5226  
5227 77 ▶ if (!$is_pat) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_pat) 35 42 77
5228 35 my @next = ();
5229 35 for my $c (@cur) { push @next, $c . $seg . $sep }
5230 35 @cur = @next;
5231 35 next;
5232 }
5233  
5234 42 my $re = _glob_to_re($seg, 1);
5235 42 ▶ $re = $win ? "(?i)\\A(?:$re)\\z" : "\\A(?:$re)\\z";
Branch: 1 branch
BranchTrueFalseTotal
$win ? : 0 42 42
5236 # A pattern that cannot be compiled (an empty or reversed
5237 # character class, say) is taken literally, as the shell does.
5238 # The warning handler keeps such a pattern from printing a Perl
5239 # diagnostic on the script's STDERR.
5240 42 my $re_bad = 0;
5241 {
5242 42 ▶ local $SIG{'__WARN__'} = sub { 1 };
Subroutine __ANON__: not called CC=1 SCAR=0.4
5243 42 eval { my $probe = 'x'; $probe =~ /$re/ };
5244 42 ▶ $re_bad = 1 if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 42 42
5245 }
5246 42 ▶ if ($re_bad) {
Branch: 1 branch
BranchTrueFalseTotal
if ($re_bad) 0 42 42
5247 0 my @lit = ();
5248 0 for my $c (@cur) { push @lit, $c . $seg . $sep }
5249 0 @cur = @lit;
5250 0 next;
5251 }
5252 42 ▶ my $want_dot = (index($seg, '.') == 0) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
index($seg, '.') == 0 ? : 1 41 42
5253 42 my @next = ();
5254 42 for my $c (@cur) {
5255 # opendir() gets the directory without its trailing separator:
5256 # Win32 turns "dir\\" into a "dir\\\\*" search pattern.
5257 45 my $dir = $c;
5258 45 ▶ if ($dir eq '') { $dir = '.' }
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq '') { } 27 18 45
5259 else {
5260 18 my $trim = $dir;
5261 18 $trim =~ s/$sepclass+\z//;
5262 18 ▶ $dir = $trim unless $trim eq ''
Branch: 1 branch
BranchTrueFalseTotal
unless $trim eq "" or $win and $trim =~ /\A[A-Za-z]:\z/ 18 0 18
Condition33%2/6
$trim eq "" or $win and $trim =~ /\A[A-Za-z]:\z/
l!l&&r!l&&!r
0018
$win and $trim =~ /\A[A-Za-z]:\z/
!ll&&!rl&&r
1800
MC/DC0%0/3
$trim eq "" or $win and $trim =~ /\A[A-Za-z]:\z/
$trim eq "" $win $trim =~ /\A[A-Za-z]:\z/
Truth table0%0/4
A or B and C
A= $trim eq ""B= $winC= $trim =~ /\A[A-Za-z]:\z/
ABCresult
1XX1
0111
00X0
0100
5263 || ($win && $trim =~ /\A[A-Za-z]:\z/);
5264 }
5265 45 my @names = ();
5266 45 ▶ if (opendir(_GLOB_DH, $dir)) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir _GLOB_DH, $dir) 43 2 45
5267 43 @names = readdir(_GLOB_DH);
5268 43 closedir(_GLOB_DH);
5269 }
5270 45 ▶ local $SIG{'__WARN__'} = sub { 1 };
Subroutine __ANON__: not called CC=1 SCAR=1.2
5271 45 for my $nm (sort @names) {
5272 767 ▶ next if !$want_dot && index($nm, '.') == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $want_dot and index($nm, ".") == 0 90 677 767
Condition100%3/3
not $want_dot and index($nm, ".") == 0
!ll&&!rl&&r
567290
MC/DC100%2/2
not $want_dot and index($nm, ".") == 0
not $want_dot index($nm, ".") == 0
Truth table100%3/3
not(A) and B
A= not $want_dotB= index($nm, ".") == 0
ABresult
0X0
100
111
5273 677 my $hit = 0;
5274 677 ▶ eval { $hit = ($nm =~ /$re/) ? 1 : 0 };
Branch: 1 branch
BranchTrueFalseTotal
$nm =~ /$re/ ? : 161 516 677
5275 677 ▶ next unless $hit;
Branch: 1 branch
BranchTrueFalseTotal
unless $hit 516 161 677
5276 161 my $full = $c . $nm;
5277 161 ▶ next if !$last && !-d $full;
Branch: 1 branch
BranchTrueFalseTotal
unless $last or -d $full 0 161 161
Condition66%2/3
$last or -d $full
l!l&&r!l&&!r
15380
MC/DC0%0/2
$last or -d $full
$last -d $full
Truth table66%2/3
A or B
A= $lastB= -d $full
ABresult
1X1
011
000
5278 161 push @next, $full . $sep;
5279 }
5280 }
5281 42 @cur = @next;
5282 42 ▶ return () unless @cur;
Branch: 1 branch
BranchTrueFalseTotal
unless @cur 25 17 42
5283 }
5284  
5285 # Only existing names are matches. A trailing separator is dropped
5286 # for the probe, since Win32 dislikes "C:\dir\" in a file test.
5287 23 my @out = ();
5288 23 for my $c (@cur) {
5289 164 my $probe = $c;
5290 164 $probe =~ s/$sepclass+\z//;
5291 164 ▶ $probe = $c if $probe eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $probe eq "" 0 164 164
5292 164 ▶ $probe = $c if $win && $probe =~ /\A[A-Za-z]:\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $win and $probe =~ /\A[A-Za-z]:\z/ 0 164 164
Condition33%1/3
$win and $probe =~ /\A[A-Za-z]:\z/
!ll&&!rl&&r
16400
MC/DC0%0/2
$win and $probe =~ /\A[A-Za-z]:\z/
$win $probe =~ /\A[A-Za-z]:\z/
Truth table33%1/3
A and B
A= $winB= $probe =~ /\A[A-Za-z]:\z/
ABresult
0X0
100
111
5293 164 ▶ next unless -e $probe || -l $probe;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $probe or -l $probe 10 154 164
Condition66%2/3
-e $probe or -l $probe
l!l&&r!l&&!r
154010
MC/DC50%1/2
-e $probe or -l $probe
-e $probe -l $probe
Truth table66%2/3
A or B
A= -e $probeB= -l $probe
ABresult
1X1
011
000
5294 154 push @out, $c;
5295 }
5296 23 return sort @out;
5297 }
5298  
5299  
5300 # ----------------------------------------------------------------
5301 # _raw_has_glob: true when the RAW (pre-expansion) argument text contains
5302 # a genuine, eligible filename-glob metacharacter (*, ?, [) -- one that
5303 # was actually written in the script and would trigger pathname
5304 # expansion. Used by the "echo" builtin to decide whether to run its
5305 # arguments through glob expansion, WITHOUT being fooled by:
5306 # * a metacharacter inside single or double quotes (echo "*.txt" is
5307 # literal in bash),
5308 # * the '*' of the $* / "$*" special parameter or the '?' of $? (these
5309 # are parameter references, not globs), or
5310 # * a metacharacter inside a ${...} parameter expansion (${arr[*]},
5311 # ${VAR?}), which is likewise not a glob.
5312 # The previous test -- a plain /[*?\[]/ match on the raw text -- fired on
5313 # all of these, so "echo \" x: $*\"" was needlessly re-parsed and its
5314 # leading whitespace and internal spacing collapsed.
5315 # Perl 5.005_03 compatible: single character scan, no regex features.
5316 # ----------------------------------------------------------------
5317 sub _raw_has_glob {
5318 425 ▶ my ($raw) = @_;
Subroutine _raw_has_glob: called CC=20 SCAR=30.0
5319 425 ▶ return 0 unless defined $raw && $raw =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw and $raw =~ /\S/ 0 425 425
Condition33%1/3
defined $raw and $raw =~ /\S/
!ll&&!rl&&r
00425
MC/DC0%0/2
defined $raw and $raw =~ /\S/
defined $raw $raw =~ /\S/
Truth table33%1/3
A and B
A= defined $rawB= $raw =~ /\S/
ABresult
0X0
100
111
5320 425 my @c = split //, $raw;
5321 425 my $n = scalar @c;
5322 425 my $in_sq = 0; my $in_dq = 0;
5323 425 my $bdep = 0; # depth inside ${ ... }
5324 425 my $i = 0;
5325 425 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 2875 390 3265
5326 2875 my $ch = $c[$i];
5327 2875 ▶ if ($in_sq) { $in_sq = 0 if $ch eq "'"; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($in_sq) 16 2859 2875
if $ch eq "'" 4 12 16
5328 2859 ▶ if ($ch eq "'" && !$in_dq) { $in_sq = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $in_dq) 4 2855 2859
Condition66%2/3
$ch eq "'" and not $in_dq
!ll&&!rl&&r
285504
MC/DC50%1/2
$ch eq "'" and not $in_dq
$ch eq "'" not $in_dq
Truth table66%2/3
A and not(B)
A= $ch eq "'"B= not $in_dq
ABresult
0X0
100
111
5329 2855 ▶ if ($ch eq '"') { $in_dq = !$in_dq; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"") 200 2655 2855
5330 2655 ▶ if ($ch eq '\\') { $i += 2; next } # backslash escape
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 4 2651 2655
5331 # Parameter expansion: $* $? $@ ... and ${ ... }
5332 2651 ▶ if ($ch eq '$' && $i + 1 < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\$" and $i + 1 < $n) 315 2336 2651
Condition66%2/3
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
23360315
MC/DC50%1/2
$ch eq "\$" and $i + 1 < $n
$ch eq "\$" $i + 1 < $n
Truth table66%2/3
A and B
A= $ch eq "\$"B= $i + 1 < $n
ABresult
0X0
100
111
5333 315 ▶ if ($c[$i+1] eq '{') { $bdep++; $i += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c[$i + 1] eq "{") 52 263 315
5334 # $ followed by a single special char (including * and ?) is a
5335 # parameter reference, not a glob -- skip both characters.
5336 263 $i += 2; next;
5337 }
5338 2336 ▶ if ($bdep > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bdep > 0) 376 1960 2336
5339 376 ▶ $bdep-- if $ch eq '}';
Branch: 1 branch
BranchTrueFalseTotal
if $ch eq "}" 52 324 376
5340 376 $i++; next;
5341 }
5342 # An unquoted, non-parameter glob metacharacter counts.
5343 1960 ▶ if (!$in_sq && !$in_dq && ($ch eq '*' || $ch eq '?' || $ch eq '[')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_sq || $in_dq and $ch eq "*" || $ch eq "?" || $ch eq "[") 18 1925 1943
Condition91%11/12
not $in_sq || $in_dq and $ch eq "*" || $ch eq "?" || $ch eq "["
!ll&&!rl&&r
450149317
$in_sq || $in_dq
l!l&&r!l&&!r
04501510
$ch eq "*" or $ch eq "?" or $ch eq "["
l!l&&r!l&&!r
19161475
$ch eq "*" or $ch eq "?"
l!l&&r!l&&!r
1721491
MC/DC40%2/5
not $in_sq || $in_dq and $ch eq "*" || $ch eq "?" || $ch eq "["
$in_sq $in_dq $ch eq "*" $ch eq "?" $ch eq "["
Truth table50%3/6
not(A || B) and C or D or E
A= $in_sqB= $in_dqC= $ch eq "*"D= $ch eq "?"E= $ch eq "["
ABCDEresult
1XXXX0
01XXX0
000000
001XX1
0001X1
000011
5344 35 return 1;
5345 }
5346 1925 $i++;
5347 }
5348 390 return 0;
5349 }
5350  
5351 # ----------------------------------------------------------------
5352 # _glob_expand_args: apply filename globbing to each unquoted word in
5353 # an already-split argument list. Words that were originally quoted
5354 # (single or double) must already have their quotes stripped by the
5355 # caller; we cannot distinguish them at this stage, so we re-check
5356 # for glob metacharacters and call _glob_expand only when present.
5357 # ----------------------------------------------------------------
5358 sub _glob_expand_args {
5359 0 ▶ my (@words) = @_;
Subroutine _glob_expand_args: not called CC=2 SCAR=17.9
5360 0 my @result;
5361 0 for my $w (@words) {
5362 0 push @result, _glob_expand($w);
5363 }
5364 0 return @result;
5365 }
5366  
5367 sub _glob_to_re {
5368 48 ▶ my ($pat, $greedy) = @_;
Subroutine _glob_to_re: called CC=19 SCAR=34.1
5369 48 my $re = '';
5370 48 my @chars = split //, $pat;
5371 48 my $n = scalar @chars;
5372 48 my $i = 0;
5373 48 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 139 48 187
5374 139 my $c = $chars[$i];
5375 139 ▶ if ($_OPT_EXTGLOB && $c =~ /[?*+\@!]/ && $i+1 < $n && $chars[$i+1] eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_OPT_EXTGLOB and $c =~ /[?*+\@!]/ and $i + 1 < $n and $chars[$i + 1] eq "(") 0 139 139
Condition33%3/9
$_OPT_EXTGLOB and $c =~ /[?*+\@!]/ and $i + 1 < $n and $chars[$i + 1] eq "("
!ll&&!rl&&r
13900
$_OPT_EXTGLOB and $c =~ /[?*+\@!]/ and $i + 1 < $n
!ll&&!rl&&r
13900
$_OPT_EXTGLOB and $c =~ /[?*+\@!]/
!ll&&!rl&&r
13900
MC/DC0%0/4
$_OPT_EXTGLOB and $c =~ /[?*+\@!]/ and $i + 1 < $n and $chars[$i + 1] eq "("
$_OPT_EXTGLOB $c =~ /[?*+\@!]/ $i + 1 < $n $chars[$i + 1] eq "("
Truth table20%1/5
A and B and C and D
A= $_OPT_EXTGLOBB= $c =~ /[?*+\@!]/C= $i + 1 < $nD= $chars[$i + 1] eq "("
ABCDresult
0XXX0
10XX0
110X0
11100
11111
5376 0 ▶ my @eg = _extglob_scan(\@chars, $i, sub { _glob_to_re($_[0], $greedy) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
5377 0 ▶ if (@eg) { $re .= $eg[1]; $i = $eg[0]; next }
Branch: 1 branch
BranchTrueFalseTotal
if (@eg) 0 0 0
5378 }
5379 139 ▶ if ($c eq '*') {
Branch: 3 branches
BranchTrueFalseTotal
if ($c eq '*') { } 24 115 139
elsif ($c eq '?') { } 2 113 115
elsif ($c eq '[') { } 23 90 113
5380 24 ▶ $re .= $greedy ? '.*' : '.*?';
Branch: 1 branch
BranchTrueFalseTotal
$greedy ? : 23 1 24
5381 }
5382 2 elsif ($c eq '?') { $re .= '.' }
5383 elsif ($c eq '[') {
5384 # POSIX character class. A leading ! (or ^) negates it -- the
5385 # shell spells the negation "[!abc]", which Perl would read as
5386 # the class of "!", "a", "b" and "c" -- and a leading ] and an
5387 # embedded backslash are literal members of the class.
5388 23 my $cls = '[';
5389 23 $i++;
5390 23 ▶ if ($i < $n && ($chars[$i] eq '!' || $chars[$i] eq '^')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n and $chars[$i] eq "!" || $chars[$i] eq "^") 1 22 23
Condition50%3/6
$i < $n and $chars[$i] eq "!" || $chars[$i] eq "^"
!ll&&!rl&&r
0230
$chars[$i] eq "!" or $chars[$i] eq "^"
l!l&&r!l&&!r
1022
MC/DC0%0/3
$i < $n and $chars[$i] eq "!" || $chars[$i] eq "^"
$i < $n $chars[$i] eq "!" $chars[$i] eq "^"
Truth table25%1/4
A and B or C
A= $i < $nB= $chars[$i] eq "!"C= $chars[$i] eq "^"
ABCresult
0XX0
1000
11X1
1011
5391 1 $cls .= '^';
5392 1 $i++;
5393 }
5394 23 ▶ if ($i < $n && $chars[$i] eq ']') { $cls .= '\\]'; $i++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n and $chars[$i] eq "]") 0 23 23
Condition33%1/3
$i < $n and $chars[$i] eq "]"
!ll&&!rl&&r
0230
MC/DC0%0/2
$i < $n and $chars[$i] eq "]"
$i < $n $chars[$i] eq "]"
Truth table33%1/3
A and B
A= $i < $nB= $chars[$i] eq "]"
ABresult
0X0
100
111
5395 23 ▶ while ($i < $n && $chars[$i] ne ']') {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n and $chars[$i] ne "]") 32 23 55
Condition66%2/3
$i < $n and $chars[$i] ne "]"
!ll&&!rl&&r
02332
MC/DC50%1/2
$i < $n and $chars[$i] ne "]"
$i < $n $chars[$i] ne "]"
Truth table66%2/3
A and B
A= $i < $nB= $chars[$i] ne "]"
ABresult
0X0
100
111
5396 32 ▶ $cls .= ($chars[$i] eq '\\') ? '\\\\' : $chars[$i];
Branch: 1 branch
BranchTrueFalseTotal
$chars[$i] eq '\\' ? : 3 29 32
5397 32 $i++;
5398 }
5399 23 $cls .= ']';
5400 23 $re .= $cls;
5401 }
5402 90 else { $re .= quotemeta($c) }
5403 139 $i++;
5404 }
5405 48 return $re;
5406 }
5407  
5408 sub _sh_remove_suffix {
5409 2 ▶ my ($val, $pat, $greedy) = @_;
Subroutine _sh_remove_suffix: called CC=4 SCAR=14.1
5410 # % (greedy=0, shortest suffix): keep longest prefix
5411 # => /\A(.*) PATTERN \z/s with greedy prefix => $1
5412 # %% (greedy=1, longest suffix): keep shortest prefix
5413 # => /\A(.*?)PATTERN \z/s with lazy prefix => $1
5414 2 my $re = _glob_to_re($pat, 1); # pattern itself is always greedy for suffix
5415 2 ▶ if ($greedy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($greedy) { } 1 1 2
5416 # longest suffix removed: lazy prefix
5417 1 ▶ return ($val =~ /\A(.*?)$re\z/s) ? $1 : $val;
Branch: 1 branch
BranchTrueFalseTotal
$val =~ /\A(.*?)$re\z/s ? : 1 0 1
5418 }
5419 else {
5420 # shortest suffix removed: greedy prefix
5421 1 ▶ return ($val =~ /\A(.*)$re\z/s) ? $1 : $val;
Branch: 1 branch
BranchTrueFalseTotal
$val =~ /\A(.*)$re\z/s ? : 1 0 1
5422 }
5423 }
5424  
5425 sub _sh_remove_prefix {
5426 2 ▶ my ($val, $pat, $greedy) = @_;
Subroutine _sh_remove_prefix: called CC=2 SCAR=7.1
5427 # # (greedy=0, shortest prefix): keep longest suffix
5428 # => /\A PATTERN(.*) \z/s with lazy pattern => $1
5429 # ## (greedy=1, longest prefix): keep shortest suffix
5430 # => /\A PATTERN(.*) \z/s with greedy pattern => $1
5431 2 my $re = _glob_to_re($pat, $greedy);
5432 2 ▶ return ($val =~ /\A$re(.*)\z/s) ? $1 : $val;
Branch: 1 branch
BranchTrueFalseTotal
$val =~ /\A$re(.*)\z/s ? : 2 0 2
5433 }
5434  
5435 sub _sh_replace {
5436 2 ▶ my ($val, $pat, $rep, $global) = @_;
Subroutine _sh_replace: called CC=2 SCAR=6.9
5437 2 my $re = _glob_to_re($pat, 1);
5438 2 ▶ if ($global) { $val =~ s/$re/$rep/g }
Branch: 1 branch
BranchTrueFalseTotal
if ($global) { } 1 1 2
5439 1 else { $val =~ s/$re/$rep/ }
5440 2 return $val;
5441 }
5442  
5443 # ----------------------------------------------------------------
5444 # Shell function registry { name => \@body_lines }
5445 # ----------------------------------------------------------------
5446  
5447 # ----------------------------------------------------------------
5448 # _inline_body_has_control: true when a single-line function body
5449 # (the text between the braces of "name() { ... }") contains a shell
5450 # control-structure keyword in command position -- if/for/while/until/
5451 # case/select as the first word, or after a ';', '&&', '||' or '|'.
5452 # Such a body must not be torn apart on ';' (that would split
5453 # "while C; do B; done" into unusable fragments); the caller keeps it
5454 # as one line so _run_lines()'s inline-control handling parses it.
5455 # Quotes, $(...), `...` and ${...} are skipped so a keyword appearing
5456 # only inside them (echo "done", VAR=$(case ...)) does not count.
5457 # Perl 5.005_03 compatible: character scan, no regex features beyond
5458 # \A and \b.
5459 # ----------------------------------------------------------------
5460 sub _inline_body_has_control {
5461 8 ▶ my ($body) = @_;
Subroutine _inline_body_has_control: called CC=42 SCAR=48.4
5462 8 ▶ return 0 unless defined $body && $body =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body and $body =~ /\S/ 0 8 8
Condition33%1/3
defined $body and $body =~ /\S/
!ll&&!rl&&r
008
MC/DC0%0/2
defined $body and $body =~ /\S/
defined $body $body =~ /\S/
Truth table33%1/3
A and B
A= defined $bodyB= $body =~ /\S/
ABresult
0X0
100
111
5463 8 my @c = split //, $body;
5464 8 my $n = scalar @c;
5465 8 my $in_sq = 0; my $in_dq = 0; my $in_bt = 0;
5466 8 my $pdep = 0; my $bdep = 0;
5467 8 my $i = 0;
5468 8 my $cmd_pos = 1;
5469 8 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 98 6 104
5470 98 my $ch = $c[$i];
5471 98 ▶ if ($in_sq) { $in_sq = 0 if $ch eq "'"; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($in_sq) 0 98 98
if $ch eq "'" 0 0 0
5472 98 ▶ if ($ch eq "'" && !$in_dq && !$in_bt) { $in_sq = 1; $cmd_pos = 0; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $in_dq and not $in_bt) 0 98 98
Condition33%2/6
$ch eq "'" and not $in_dq and not $in_bt
!ll&&!rl&&r
9800
$ch eq "'" and not $in_dq
!ll&&!rl&&r
9800
MC/DC0%0/3
$ch eq "'" and not $in_dq and not $in_bt
$ch eq "'" not $in_dq not $in_bt
Truth table25%1/4
A and not(B) and not(C)
A= $ch eq "'"B= not $in_dqC= not $in_bt
ABCresult
0XX0
10X0
1100
1111
5473 98 ▶ if ($ch eq '"' && !$in_bt) { $in_dq = !$in_dq; $cmd_pos = 0; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" and not $in_bt) 6 92 98
Condition66%2/3
$ch eq "\"" and not $in_bt
!ll&&!rl&&r
9206
MC/DC50%1/2
$ch eq "\"" and not $in_bt
$ch eq "\"" not $in_bt
Truth table66%2/3
A and not(B)
A= $ch eq "\""B= not $in_bt
ABresult
0X0
100
111
5474 92 ▶ if ($ch eq '\\') { $i += 2; $cmd_pos = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 0 92 92
5475 92 ▶ if ($ch eq '`') { $in_bt = !$in_bt; $cmd_pos = 0; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "`") 0 92 92
5476 92 ▶ if (!$in_dq && !$in_bt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_dq or $in_bt) 69 23 92
Condition66%2/3
$in_dq or $in_bt
l!l&&r!l&&!r
23069
MC/DC50%1/2
$in_dq or $in_bt
$in_dq $in_bt
Truth table66%2/3
A or B
A= $in_dqB= $in_bt
ABresult
1X1
011
000
5477 69 ▶ if ($ch eq '$' && $i+1 < $n && $c[$i+1] eq '{') { $bdep++; $i += 2; $cmd_pos = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "{") 0 69 69
Condition66%4/6
$ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "{"
!ll&&!rl&&r
6720
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
6702
MC/DC0%0/3
$ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "{"
$ch eq "\$" $i + 1 < $n $c[$i + 1] eq "{"
Truth table50%2/4
A and B and C
A= $ch eq "\$"B= $i + 1 < $nC= $c[$i + 1] eq "{"
ABCresult
0XX0
10X0
1100
1111
5478 69 ▶ if ($bdep > 0 && $ch eq '}') { $bdep--; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($bdep > 0 and $ch eq "}") 0 69 69
Condition33%1/3
$bdep > 0 and $ch eq "}"
!ll&&!rl&&r
6900
MC/DC0%0/2
$bdep > 0 and $ch eq "}"
$bdep > 0 $ch eq "}"
Truth table33%1/3
A and B
A= $bdep > 0B= $ch eq "}"
ABresult
0X0
100
111
5479 69 ▶ if ($ch eq '$' && $i+1 < $n && $c[$i+1] eq '(') { $pdep++; $i += 2; $cmd_pos = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "(") 1 68 69
Condition83%5/6
$ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "("
!ll&&!rl&&r
6711
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
6702
MC/DC66%2/3
$ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "("
$ch eq "\$" $i + 1 < $n $c[$i + 1] eq "("
Truth table75%3/4
A and B and C
A= $ch eq "\$"B= $i + 1 < $nC= $c[$i + 1] eq "("
ABCresult
0XX0
10X0
1100
1111
5480 68 ▶ if ($ch eq '(') { $pdep++; $i++; $cmd_pos = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "(") 1 67 68
5481 67 ▶ if ($ch eq ')' && $pdep > 0) { $pdep--; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ")" and $pdep > 0) 2 65 67
Condition66%2/3
$ch eq ")" and $pdep > 0
!ll&&!rl&&r
6502
MC/DC50%1/2
$ch eq ")" and $pdep > 0
$ch eq ")" $pdep > 0
Truth table66%2/3
A and B
A= $ch eq ")"B= $pdep > 0
ABresult
0X0
100
111
5482 }
5483 88 ▶ if (!$in_dq && !$in_bt && $pdep == 0 && $bdep == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_dq || $in_bt and $pdep == 0 and $bdep == 0) 57 31 88
Condition77%7/9
not $in_dq || $in_bt and $pdep == 0 and $bdep == 0
!ll&&!rl&&r
31057
not $in_dq || $in_bt and $pdep == 0
!ll&&!rl&&r
23857
$in_dq || $in_bt
l!l&&r!l&&!r
23065
MC/DC50%2/4
not $in_dq || $in_bt and $pdep == 0 and $bdep == 0
$in_dq $in_bt $pdep == 0 $bdep == 0
Truth table60%3/5
not(A || B) and C and D
A= $in_dqB= $in_btC= $pdep == 0D= $bdep == 0
ABCDresult
1XXX0
01XX0
000X0
00100
00111
5484 57 ▶ if ($ch eq ';') { $cmd_pos = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ";") 2 55 57
5485 55 ▶ if ($ch eq '&' && $i+1 < $n && $c[$i+1] eq '&') { $cmd_pos = 1; $i += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "&" and $i + 1 < $n and $c[$i + 1] eq "&") 0 55 55
Condition33%2/6
$ch eq "&" and $i + 1 < $n and $c[$i + 1] eq "&"
!ll&&!rl&&r
5500
$ch eq "&" and $i + 1 < $n
!ll&&!rl&&r
5500
MC/DC0%0/3
$ch eq "&" and $i + 1 < $n and $c[$i + 1] eq "&"
$ch eq "&" $i + 1 < $n $c[$i + 1] eq "&"
Truth table25%1/4
A and B and C
A= $ch eq "&"B= $i + 1 < $nC= $c[$i + 1] eq "&"
ABCresult
0XX0
10X0
1100
1111
5486 55 ▶ if ($ch eq '|' && $i+1 < $n && $c[$i+1] eq '|') { $cmd_pos = 1; $i += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "|" and $i + 1 < $n and $c[$i + 1] eq "|") 0 55 55
Condition33%2/6
$ch eq "|" and $i + 1 < $n and $c[$i + 1] eq "|"
!ll&&!rl&&r
5500
$ch eq "|" and $i + 1 < $n
!ll&&!rl&&r
5500
MC/DC0%0/3
$ch eq "|" and $i + 1 < $n and $c[$i + 1] eq "|"
$ch eq "|" $i + 1 < $n $c[$i + 1] eq "|"
Truth table25%1/4
A and B and C
A= $ch eq "|"B= $i + 1 < $nC= $c[$i + 1] eq "|"
ABCresult
0XX0
10X0
1100
1111
5487 55 ▶ if ($ch eq '|') { $cmd_pos = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "|") 0 55 55
5488 55 ▶ if ($ch eq '&') { $cmd_pos = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "&") 0 55 55
5489 55 ▶ if ($ch =~ /\s/) { $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch =~ /\s/) 9 46 55
5490 46 ▶ if ($cmd_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd_pos) 10 36 46
5491 10 my $tail = join('', @c[$i .. $n-1]);
5492 10 ▶ if ($tail =~ /\A(?:if|for|while|until|case|select)\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tail =~ /\A(?:if|for|while|until|case|select)\b/i) 2 8 10
5493 2 return 1;
5494 }
5495 }
5496 44 $cmd_pos = 0; $i++; next;
5497 }
5498 31 $i++;
5499 }
5500 6 return 0;
5501 }
5502  
5503 # ----------------------------------------------------------------
5504 # _parse_function: parse "name() {" or "function name {" blocks
5505 # Returns ($status, $new_i).
5506 # ----------------------------------------------------------------
5507 sub _parse_function {
5508 15 ▶ my ($class, $lines_ref, $start, $opts_ref) = @_;
Subroutine _parse_function: called CC=16 SCAR=31.2
5509 15 my @lines = @{$lines_ref};
5510 15 my $line = $lines[$start];
5511 15 $line =~ s/\r?\n\z//;
5512 15 $line =~ s/\A\s+//;
5513  
5514 15 my $name = '';
5515 15 ▶ if ($line =~ /\A([A-Za-z_][A-Za-z0-9_]*)\s*\(\s*\)\s*(?:\{.*)?\z/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /\A([A-Za-z_][A-Za-z0-9_]*)\s*\(\s*\)\s*(?:\{.*)?\z/) { } 15 0 15
elsif ($line =~ /\Afunction\s+([A-Za-z_][A-Za-z0-9_]*)\s*(?:\(\s*\))?\s*(?:\{.*)?\z/i) { } 0 0 0
5516 15 $name = $1;
5517 }
5518 elsif ($line =~ /\Afunction\s+([A-Za-z_][A-Za-z0-9_]*)\s*(?:\(\s*\))?\s*(?:\{.*)?\z/i) {
5519 0 $name = $1;
5520 }
5521 else {
5522 0 return (0, $start + 1);
5523 }
5524  
5525 15 my @body;
5526 15 ▶ my $depth = ($line =~ /\{/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /\{/ ? : 15 0 15
5527 15 my $i = $start + 1;
5528  
5529 # Check if the function body is on the same line as the definition
5530 # e.g. "name() { cmd1; cmd2; }"
5531 15 ▶ if ($depth >= 1 && $line =~ /\{(.*)\}\s*\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth >= 1 and $line =~ /\{(.*)\}\s*\z/s) 8 7 15
Condition66%2/3
$depth >= 1 and $line =~ /\{(.*)\}\s*\z/s
!ll&&!rl&&r
078
MC/DC50%1/2
$depth >= 1 and $line =~ /\{(.*)\}\s*\z/s
$depth >= 1 $line =~ /\{(.*)\}\s*\z/s
Truth table66%2/3
A and B
A= $depth >= 1B= $line =~ /\{(.*)\}\s*\z/s
ABresult
0X0
100
111
5532 8 my $inline = $1;
5533 8 $inline =~ s/\A\s+//; $inline =~ s/\s+\z//;
5534 # A trailing ';' just before the closing brace (name() { ...; })
5535 # is an empty separator; drop it so an inline control structure's
5536 # "; done"/"; fi"/"; esac" terminator sits at end-of-string where
5537 # the block parsers expect it.
5538 8 $inline =~ s/;\s*\z//;
5539 8 $inline =~ s/\s+\z//;
5540 # A naive split on ';' is correct for a body of simple commands
5541 # (name() { cmd1; cmd2; }) but WRONG for an inline control
5542 # structure: "while C; do B; done" would be torn into the four
5543 # pieces "while C" / "do B" / ... / "done", and the reassembled
5544 # multi-line form ("do" glued to a command on one line) is not a
5545 # shape the block parsers accept, so the loop silently ran
5546 # nothing. When the inline body contains a control-structure
5547 # keyword in command position, keep it as a SINGLE body line and
5548 # let _run_lines() apply the same inline-control handling it uses
5549 # for a control structure typed directly on one physical line
5550 # (this also drives the "prefix; control" split via
5551 # _find_control_split, so "cmd; while ...; do ...; done" works).
5552 8 ▶ if (_inline_body_has_control($inline)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_inline_body_has_control($inline)) { } 2 6 8
5553 2 ▶ push @body, $inline if $inline =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $inline =~ /\S/ 2 0 2
5554 }
5555 else {
5556 # Split on ; to get individual commands
5557 6 for my $part (split /;/, $inline) {
5558 7 $part =~ s/\A\s+//; $part =~ s/\s+\z//;
5559 7 ▶ push @body, $part if $part =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $part =~ /\S/ 7 0 7
5560 }
5561 }
5562 8 $_SH_FUNCTIONS{$name} = [ @body ];
5563 8 return (0, $i);
5564 }
5565  
5566 7 ▶ if ($depth == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 0) 0 7 7
5567 0 ▶ while ($i <= $#lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#lines) 0 0 0
5568 0 my $l = $lines[$i]; $l =~ s/\r?\n\z//; $l =~ s/\A\s+//;
5569 0 $i++;
5570 0 ▶ if ($l =~ /\{/) { $depth = 1; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /\{/) 0 0 0
5571 }
5572 }
5573  
5574 7 ▶ while ($i <= $#lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#lines) 18 0 18
5575 18 my $l = $lines[$i]; $l =~ s/\r?\n\z//;
5576 18 $i++;
5577 18 my $opens = () = ($l =~ /\{/g);
5578 18 my $closes = () = ($l =~ /\}/g);
5579 18 $depth += $opens - $closes;
5580 18 ▶ if ($depth <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth <= 0) 7 11 18
5581 7 my $before = $l;
5582 7 $before =~ s/\}\s*\z//;
5583 7 ▶ push @body, $before if $before =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $before =~ /\S/ 0 7 7
5584 7 last;
5585 }
5586 11 push @body, $l;
5587 }
5588  
5589 7 $_SH_FUNCTIONS{$name} = [ @body ];
5590 7 return (0, $i);
5591 }
5592  
5593 # ----------------------------------------------------------------
5594 # _call_sh_function: execute a registered SH function
5595 # ----------------------------------------------------------------
5596 sub _call_sh_function {
5597 13 ▶ my ($class, $name, $args_str, $opts_ref) = @_;
Subroutine _call_sh_function: called CC=14 SCAR=26.5
5598 13 ▶ return 1 unless exists $_SH_FUNCTIONS{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BATsh::SH::_SH_FUNCTIONS{$name} 0 13 13
5599  
5600 13 my @args = _parse_args($args_str);
5601  
5602 13 my @saved_arg;
5603 13 for my $n (1 .. 9) {
5604 117 push @saved_arg, BATsh::Env->get("BATSH_ARG$n");
5605 117 ▶ BATsh::Env->set("BATSH_ARG$n",
Branch: 1 branch
BranchTrueFalseTotal
defined $args[$n - 1] ? : 27 90 117
5606 defined($args[$n-1]) ? $args[$n-1] : '');
5607 }
5608 13 my @saved_pct;
5609 13 for my $n (1 .. 9) {
5610 117 push @saved_pct, BATsh::Env->get("%$n");
5611 117 ▶ BATsh::Env->set("%$n", defined($args[$n-1]) ? $args[$n-1] : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $args[$n - 1] ? : 27 90 117
5612 }
5613 13 my $saved_star = BATsh::Env->get('%*');
5614 13 BATsh::Env->set('%*', join(' ', @args));
5615  
5616 13 push @FUNCTION_STACK, {};
5617 13 my $saved_ret = $_RETURN;
5618 13 $_RETURN = 0;
5619  
5620 13 my $rc = _run_lines($class, $_SH_FUNCTIONS{$name}, $opts_ref);
5621  
5622 13 $_RETURN = $saved_ret;
5623  
5624 # Restore local variables saved in this function's scope
5625 13 ▶ if (@FUNCTION_STACK) {
Branch: 1 branch
BranchTrueFalseTotal
if (@FUNCTION_STACK) 13 0 13
5626 13 my $frame = $FUNCTION_STACK[-1];
5627 13 for my $var (keys %{$frame}) {
5628 2 my $old = $frame->{$var};
5629 2 ▶ if (defined $old) { BATsh::Env->set($var, $old) }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old) { } 2 0 2
5630 0 else { BATsh::Env->unset($var) }
5631 }
5632 }
5633 13 pop @FUNCTION_STACK;
5634  
5635 13 for my $n (1 .. 9) {
5636 117 my $v = $saved_arg[$n-1];
5637 117 ▶ BATsh::Env->set("BATSH_ARG$n", defined $v ? $v : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 9 108 117
5638 }
5639 13 for my $n (1 .. 9) {
5640 117 my $v = $saved_pct[$n-1];
5641 117 ▶ BATsh::Env->set("%$n", defined $v ? $v : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 10 107 117
5642 }
5643 13 ▶ BATsh::Env->set('%*', defined $saved_star ? $saved_star : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $saved_star ? : 1 12 13
5644  
5645 13 $LAST_STATUS = $rc;
5646 13 return $rc;
5647 }
5648  
5649 # ----------------------------------------------------------------
5650 # _parse_args: split a string into arguments respecting quotes
5651 # ----------------------------------------------------------------
5652 sub _parse_args {
5653 # $protect_quoted is set by a caller that joins the words back into a
5654 # string and hands that string to a builtin which dequotes it again.
5655 # The quotes are gone by then, so a backslash that was INSIDE them
5656 # would be read as an escape by that second pass and disappear:
5657 # echo "C:\dir"/*.txt printed C:dir/*.txt. Protecting it marks it as
5658 # data, exactly as an expansion result is marked, while a backslash
5659 # written outside quotes stays an escape, as in bash.
5660 105 ▶ my ($str, $protect_quoted) = @_;
Subroutine _parse_args: called CC=23 SCAR=36.0
5661 105 ▶ $str = '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 105 105
5662 105 $str =~ s/\A\s+//; $str =~ s/\s+\z//;
5663 105 ▶ return () unless $str =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /\S/ 2 103 105
5664 103 my @args;
5665 my @quoted; # parallel array: 1 if word was quoted, 0 if bare
5666 103 my $cur = '';
5667 103 my $word_quoted = 0;
5668 103 my $in_sq = 0;
5669 103 my $in_dq = 0;
5670 103 for my $ch (split //, $str) {
5671 1082 ▶ if ($in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq) 0 1082 1082
5672 0 ▶ if ($ch eq "'") { $in_sq = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq q[']) { } 0 0 0
5673 else {
5674 0 ▶ $cur .= $protect_quoted ? _protect_lit($ch) : $ch;
Branch: 1 branch
BranchTrueFalseTotal
$protect_quoted ? : 0 0 0
5675 0 $word_quoted = 1;
5676 }
5677 0 next;
5678 }
5679 1082 ▶ if ($ch eq "'" && !$in_dq) { $in_sq = 1; $word_quoted = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $in_dq) 0 1082 1082
Condition33%1/3
$ch eq "'" and not $in_dq
!ll&&!rl&&r
108200
MC/DC0%0/2
$ch eq "'" and not $in_dq
$ch eq "'" not $in_dq
Truth table33%1/3
A and not(B)
A= $ch eq "'"B= not $in_dq
ABresult
0X0
100
111
5680 1082 ▶ if ($ch eq '"' && !$in_sq) { $in_dq = !$in_dq; $word_quoted = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" and not $in_sq) 104 978 1082
Condition66%2/3
$ch eq "\"" and not $in_sq
!ll&&!rl&&r
9780104
MC/DC50%1/2
$ch eq "\"" and not $in_sq
$ch eq "\"" not $in_sq
Truth table66%2/3
A and not(B)
A= $ch eq "\""B= not $in_sq
ABresult
0X0
100
111
5681 978 ▶ if ($ch =~ /\s/ && !$in_sq && !$in_dq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch =~ /\s/ and not $in_sq and not $in_dq) 156 822 978
Condition66%4/6
$ch =~ /\s/ and not $in_sq and not $in_dq
!ll&&!rl&&r
8220156
$ch =~ /\s/ and not $in_sq
!ll&&!rl&&r
8220156
MC/DC33%1/3
$ch =~ /\s/ and not $in_sq and not $in_dq
$ch =~ /\s/ not $in_sq not $in_dq
Truth table50%2/4
A and not(B) and not(C)
A= $ch =~ /\s/B= not $in_sqC= not $in_dq
ABCresult
0XX0
10X0
1100
1111
5682 156 push @args, $cur;
5683 156 push @quoted, $word_quoted;
5684 156 $cur = ''; $word_quoted = 0;
5685 156 next;
5686 }
5687 822 ▶ $cur .= ($protect_quoted && $in_dq) ? _protect_lit($ch) : $ch;
Branch: 1 branch
BranchTrueFalseTotal
$protect_quoted && $in_dq ? : 3 819 822
Condition100%3/3
$protect_quoted and $in_dq
!ll&&!rl&&r
4243953
MC/DC100%2/2
$protect_quoted and $in_dq
$protect_quoted $in_dq
Truth table100%3/3
A and B
A= $protect_quotedB= $in_dq
ABresult
0X0
100
111
5688 }
5689 103 ▶ push @args, $cur if $cur ne '' || @args;
Branch: 1 branch
BranchTrueFalseTotal
if $cur ne "" or @args 103 0 103
Condition33%1/3
$cur ne "" or @args
l!l&&r!l&&!r
10300
MC/DC0%0/2
$cur ne "" or @args
$cur ne "" @args
Truth table33%1/3
A or B
A= $cur ne ""B= @args
ABresult
1X1
011
000
5690 103 ▶ push @quoted, $word_quoted if $cur ne '' || @quoted;
Branch: 1 branch
BranchTrueFalseTotal
if $cur ne "" or @quoted 103 0 103
Condition33%1/3
$cur ne "" or @quoted
l!l&&r!l&&!r
10300
MC/DC0%0/2
$cur ne "" or @quoted
$cur ne "" @quoted
Truth table33%1/3
A or B
A= $cur ne ""B= @quoted
ABresult
1X1
011
000
5691 # Apply filename globbing to unquoted words containing metacharacters
5692 103 my @result;
5693 103 for my $i (0 .. $#args) {
5694 259 ▶ if (!$quoted[$i] && $args[$i] =~ /[*?\[]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $quoted[$i] and $args[$i] =~ /[*?\[]/) { } 43 216 259
Condition100%3/3
not $quoted[$i] and $args[$i] =~ /[*?\[]/
!ll&&!rl&&r
5216443
MC/DC100%2/2
not $quoted[$i] and $args[$i] =~ /[*?\[]/
not $quoted[$i] $args[$i] =~ /[*?\[]/
Truth table100%3/3
not(A) and B
A= not $quoted[$i]B= $args[$i] =~ /[*?\[]/
ABresult
0X0
100
111
5695 43 push @result, _glob_expand($args[$i]);
5696 }
5697 else {
5698 216 push @result, $args[$i];
5699 }
5700 }
5701 103 return @result;
5702 }
5703  
5704 # ----------------------------------------------------------------
5705 # ----------------------------------------------------------------
5706 sub _cmd_external {
5707 29 ▶ my ($cmd, $rest) = @_;
Subroutine _cmd_external: called CC=6 SCAR=18.3
5708 29 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 29 29
5709 29 $rest =~ s/\A\s+//;
5710 29 ▶ my $full = $rest ne '' ? "$cmd $rest" : $cmd;
Branch: 1 branch
BranchTrueFalseTotal
$rest ne '' ? : 27 2 29
5711 # The command line goes to the OS verbatim, so a protected backslash
5712 # has to be a real backslash again here.
5713 29 $full = _unprotect_lit($full);
5714 29 $full = BATsh::MB::dec($full);
5715 29 BATsh::Env->sync_to_env();
5716 29 my $rc = system($full);
5717 29 ▶ if ($rc == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc == -1) { } 2 27 29
5718 # system() could not launch the command (not found / exec failed).
5719 # bash reports 127 for "command not found"; without this guard the
5720 # old ($rc >> 8) arithmetic on -1 produced a huge garbage status.
5721 2 $LAST_STATUS = 127;
5722 }
5723 else {
5724 27 ▶ $LAST_STATUS = ($rc == 0) ? 0 : (($rc >> 8) || 1);
Branch: 1 branch
BranchTrueFalseTotal
$rc == 0 ? : 27 0 27
Condition0%0/2
$rc >> 8 or 1
l!l
00
MC/DC0%0/1
$rc >> 8 or 1
$rc >> 8
Truth table0%0/2
A
A= $rc >> 8
Aresult
11
00
5725 }
5726 29 return $LAST_STATUS;
5727 }
5728  
5729 # ----------------------------------------------------------------
5730 # let / type / command (v0.08)
5731 # ----------------------------------------------------------------
5732 # Prior to v0.08 these three POSIX/bash builtins fell through to the
5733 # external-command path and were handed to whatever real shell (if any)
5734 # happened to exist -- which failed on systems without that shell, and
5735 # defeated BATsh's "no external shell required" design. They are now
5736 # evaluated internally in pure Perl.
5737  
5738 # let EXPR [EXPR ...]
5739 # Each argument is evaluated as a shell-arithmetic expression (reusing
5740 # _eval_arith, so assignments and ++/-- write back to the variable store).
5741 # The exit status is 0 when the LAST expression is non-zero, else 1 --
5742 # the inverse-of-truth convention bash's "let" and "(( ))" both use.
5743 # Arguments are quote-stripped but NOT filename-globbed, so `let "x=1*2"`
5744 # and `let x=1+2` both behave as arithmetic rather than pathname patterns.
5745 sub _cmd_let {
5746 8 ▶ my ($rest) = @_;
Subroutine _cmd_let: called CC=5 SCAR=16.1
5747 8 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 8 8
5748 8 my @exprs = map { _arr_dequote($_) } _arr_split_words($rest);
5749 8 ▶ if (!@exprs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@exprs) 1 7 8
5750 1 print STDERR "sh: let: expression expected\n";
5751 1 $LAST_STATUS = 1;
5752 1 return 1;
5753 }
5754 7 my $last = 0;
5755 7 for my $e (@exprs) {
5756 8 $last = _eval_arith($e);
5757 }
5758 7 ▶ $LAST_STATUS = ($last != 0) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$last != 0 ? : 5 2 7
5759 7 return $LAST_STATUS;
5760 }
5761  
5762 # _sh_name_kind(NAME): classify NAME the way "type" / "command -v" do,
5763 # in bash's precedence order (alias, keyword, function, builtin, file).
5764 # Returns (KIND, DETAIL) where KIND is one of
5765 # 'alias' (DETAIL = alias body), 'keyword', 'function', 'builtin',
5766 # 'file' (DETAIL = resolved path), or '' (not found; DETAIL undef).
5767 sub _sh_name_kind {
5768 13 ▶ my ($name) = @_;
Subroutine _sh_name_kind: called CC=11 SCAR=25.0
5769 13 ▶ return ('', undef) unless defined $name && $name ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and $name ne "" 0 13 13
Condition33%1/3
defined $name and $name ne ""
!ll&&!rl&&r
0013
MC/DC0%0/2
defined $name and $name ne ""
defined $name $name ne ""
Truth table33%1/3
A and B
A= defined $nameB= $name ne ""
ABresult
0X0
100
111
5770  
5771 13 ▶ return ('alias', $_SH_ALIAS{$name}) if exists $_SH_ALIAS{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $BATsh::SH::_SH_ALIAS{$name} 2 11 13
5772  
5773 11 my $lc = lc($name);
5774 11 my %kw = map { ($_ => 1) } qw(
5775 if then else elif fi for while until do done
5776 case esac function in select time
5777 );
5778 11 ▶ return ('keyword', undef) if $kw{$lc};
Branch: 1 branch
BranchTrueFalseTotal
if $kw{$lc} 2 9 11
5779  
5780 9 ▶ return ('function', undef) if exists $_SH_FUNCTIONS{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $BATsh::SH::_SH_FUNCTIONS{$name} 1 8 9
5781  
5782 8 ▶ return ('builtin', undef) if $name eq '[' || $name eq ':' || $name eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "[" or $name eq ":" or $name eq "." 0 8 8
Condition33%2/6
$name eq "[" or $name eq ":" or $name eq "."
l!l&&r!l&&!r
008
$name eq "[" or $name eq ":"
l!l&&r!l&&!r
008
MC/DC0%0/3
$name eq "[" or $name eq ":" or $name eq "."
$name eq "[" $name eq ":" $name eq "."
Truth table25%1/4
A or B or C
A= $name eq "["B= $name eq ":"C= $name eq "."
ABCresult
1XX1
01X1
0011
0000
5783 8 my %bi = map { ($_ => 1) } qw(
5784 alias break cd command continue declare echo eval exec exit export
5785 false getopts hash let local mapfile printf pwd read readarray
5786 readonly return set shift shopt source test true trap type typeset
5787 umask unalias unset
5788 );
5789 8 ▶ return ('builtin', undef) if $bi{$lc};
Branch: 1 branch
BranchTrueFalseTotal
if $bi{$lc} 3 5 8
5790  
5791 5 my $path = _sh_find_on_path($name);
5792 5 ▶ return ('file', $path) if defined $path;
Branch: 1 branch
BranchTrueFalseTotal
if defined $path 3 2 5
5793  
5794 2 return ('', undef);
5795 }
5796  
5797 # _sh_find_on_path(NAME): locate an executable NAME the way a shell would.
5798 # A NAME containing a directory separator is tested directly; otherwise each
5799 # PATH element is searched (';'-separated and PATHEXT-aware on Windows-like
5800 # systems, ':'-separated and -x-tested elsewhere). Returns the full path,
5801 # or undef when not found. Pure-Perl and Perl 5.005_03 safe.
5802 sub _sh_find_on_path {
5803 7 ▶ my ($name) = @_;
Subroutine _sh_find_on_path: called CC=25 SCAR=49.0
5804 7 ▶ return undef unless defined $name && $name ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and $name ne "" 0 7 7
Condition33%1/3
defined $name and $name ne ""
!ll&&!rl&&r
007
MC/DC0%0/2
defined $name and $name ne ""
defined $name $name ne ""
Truth table33%1/3
A and B
A= defined $nameB= $name ne ""
ABresult
0X0
100
111
5805 7 ▶ my $is_win = ($^O =~ /MSWin32|dos|os2|cygwin/i) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /MSWin32|dos|os2|cygwin/i ? : 0 7 7
5806  
5807 7 ▶ if ($name =~ m{[/\\]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ m([/\\])) 0 7 7
5808 0 ▶ return $name if -f $name && ($is_win || -x $name);
Branch: 1 branch
BranchTrueFalseTotal
if -f $name and $is_win || -x $name 0 0 0
Condition0%0/6
-f $name and $is_win || -x $name
!ll&&!rl&&r
000
$is_win or -x $name
l!l&&r!l&&!r
000
MC/DC0%0/3
-f $name and $is_win || -x $name
-f $name $is_win -x $name
Truth table0%0/4
A and B or C
A= -f $nameB= $is_winC= -x $name
ABCresult
0XX0
1000
11X1
1011
5809 0 return undef;
5810 }
5811  
5812 7 my $path = BATsh::Env->get('PATH');
5813 7 ▶ $path = $ENV{'PATH'} unless defined $path && $path ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and $path ne "" 0 7 7
Condition33%1/3
defined $path and $path ne ""
!ll&&!rl&&r
007
MC/DC0%0/2
defined $path and $path ne ""
defined $path $path ne ""
Truth table33%1/3
A and B
A= defined $pathB= $path ne ""
ABresult
0X0
100
111
5814 7 ▶ return undef unless defined $path && $path ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and $path ne "" 0 7 7
Condition33%1/3
defined $path and $path ne ""
!ll&&!rl&&r
007
MC/DC0%0/2
defined $path and $path ne ""
defined $path $path ne ""
Truth table33%1/3
A and B
A= defined $pathB= $path ne ""
ABresult
0X0
100
111
5815  
5816 7 ▶ my $sep = $is_win ? ';' : ':';
Branch: 1 branch
BranchTrueFalseTotal
$is_win ? : 0 7 7
5817 7 my @exts = ('');
5818 7 ▶ if ($is_win) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_win) 0 7 7
5819 0 my $pe = BATsh::Env->get('PATHEXT');
5820 0 ▶ $pe = $ENV{'PATHEXT'} unless defined $pe && $pe ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pe and $pe ne "" 0 0 0
Condition0%0/3
defined $pe and $pe ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $pe and $pe ne ""
defined $pe $pe ne ""
Truth table0%0/3
A and B
A= defined $peB= $pe ne ""
ABresult
0X0
100
111
5821 0 ▶ $pe = '.COM;.EXE;.BAT;.CMD' unless defined $pe && $pe ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pe and $pe ne "" 0 0 0
Condition0%0/3
defined $pe and $pe ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $pe and $pe ne ""
defined $pe $pe ne ""
Truth table0%0/3
A and B
A= defined $peB= $pe ne ""
ABresult
0X0
100
111
5822 0 push @exts, split(/;/, $pe);
5823 }
5824  
5825 7 my @dirs = split(/\Q$sep\E/, $path);
5826 7 for my $dir (@dirs) {
5827 39 ▶ $dir = '.' if $dir eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq "" 0 39 39
5828 39 for my $ext (@exts) {
5829 39 my $cand = File::Spec->catfile($dir, $name . $ext);
5830 39 ▶ if ($is_win) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_win) { } 0 39 39
5831 0 ▶ return $cand if -f $cand;
Branch: 1 branch
BranchTrueFalseTotal
if -f $cand 0 0 0
5832 }
5833 else {
5834 39 ▶ return $cand if -f $cand && -x $cand;
Branch: 1 branch
BranchTrueFalseTotal
if -f $cand and -x $cand 4 35 39
Condition66%2/3
-f $cand and -x $cand
!ll&&!rl&&r
3504
MC/DC50%1/2
-f $cand and -x $cand
-f $cand -x $cand
Truth table66%2/3
A and B
A= -f $candB= -x $cand
ABresult
0X0
100
111
5835 }
5836 }
5837 }
5838 3 return undef;
5839 }
5840  
5841 # _print_type_verbose(NAME, KIND, DETAIL): emit the "type NAME" / "command -V
5842 # NAME" description line. Returns 1 when found, 0 (with a STDERR diagnostic)
5843 # when NAME is unknown.
5844 sub _print_type_verbose {
5845 3 ▶ my ($name, $kind, $detail) = @_;
Subroutine _print_type_verbose: called CC=7 SCAR=24.5
5846 3 ▶ if ($kind eq 'alias') {
Branch: 5 branches
BranchTrueFalseTotal
if ($kind eq 'alias') { } 0 3 3
elsif ($kind eq 'keyword') { } 0 3 3
elsif ($kind eq 'function') { } 0 3 3
elsif ($kind eq 'builtin') { } 2 1 3
elsif ($kind eq 'file') { } 0 1 1
5847 0 ▶ print "$name is aliased to `" . (defined $detail ? $detail : '') . "'\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $detail ? : 0 0 0
5848 }
5849 0 elsif ($kind eq 'keyword') { print "$name is a shell keyword\n" }
5850 0 elsif ($kind eq 'function') { print "$name is a function\n" }
5851 2 elsif ($kind eq 'builtin') { print "$name is a shell builtin\n" }
5852 0 elsif ($kind eq 'file') { print "$name is $detail\n" }
5853 else {
5854 1 print STDERR "sh: type: $name: not found\n";
5855 1 return 0;
5856 }
5857 2 return 1;
5858 }
5859  
5860 # type [-t|-p|-a] NAME [NAME ...]
5861 # Default: describe how each NAME would be interpreted.
5862 # -t: print only the one-word kind (alias/keyword/function/builtin/file).
5863 # -p: print only the path when NAME is an external file (else nothing).
5864 # -a: describe all matches (this interpreter reports the single primary
5865 # match, so -a behaves like the default form).
5866 # Exit status is non-zero when any NAME is not found.
5867 sub _cmd_type {
5868 7 ▶ my ($rest) = @_;
Subroutine _cmd_type: called CC=15 SCAR=28.7
5869 7 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 7 7
5870 7 my @w = map { _arr_dequote($_) } _arr_split_words($rest);
5871  
5872 7 my $mode = ''; # '', 't', 'p', 'a'
5873 7 ▶ while (@w && $w[0] =~ /\A-[atpP]+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@w and $w[0] =~ /\A-[atpP]+\z/) 5 7 12
Condition66%2/3
@w and $w[0] =~ /\A-[atpP]+\z/
!ll&&!rl&&r
075
MC/DC50%1/2
@w and $w[0] =~ /\A-[atpP]+\z/
@w $w[0] =~ /\A-[atpP]+\z/
Truth table66%2/3
A and B
A= @wB= $w[0] =~ /\A-[atpP]+\z/
ABresult
0X0
100
111
5874 5 my $opt = shift @w;
5875 5 ▶ $mode = 'a' if $opt =~ /a/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt =~ /a/ 0 5 5
5876 5 ▶ $mode = 't' if $opt =~ /t/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt =~ /t/ 4 1 5
5877 5 ▶ $mode = 'p' if $opt =~ /[pP]/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt =~ /[pP]/ 1 4 5
5878 }
5879 7 ▶ if (!@w) { $LAST_STATUS = 0; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (@w) 0 7 7
5880  
5881 7 my $status = 0;
5882 7 for my $name (@w) {
5883 7 my ($kind, $detail) = _sh_name_kind($name);
5884 7 ▶ if ($mode eq 't') {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 't') { } 4 3 7
elsif ($mode eq 'p') { } 1 2 3
5885 4 ▶ if ($kind eq '') { $status = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq '') { } 0 4 4
5886 4 else { print $kind, "\n" }
5887 }
5888 elsif ($mode eq 'p') {
5889 1 ▶ if ($kind eq 'file') { print $detail, "\n" }
Branch: 2 branches
BranchTrueFalseTotal
if ($kind eq 'file') { } 1 0 1
elsif ($kind eq '') { } 0 0 0
5890 0 elsif ($kind eq '') { $status = 1 }
5891 # builtin/function/keyword/alias: print nothing (bash -p)
5892 }
5893 else {
5894 2 ▶ _print_type_verbose($name, $kind, $detail) or $status = 1;
Branch: 1 branch
BranchTrueFalseTotal
_print_type_verbose($name, $kind, $detail) or $status = 1 1 1 2
5895 }
5896 }
5897 7 $LAST_STATUS = $status;
5898 7 return $status;
5899 }
5900  
5901 # command [-p] [-v|-V] NAME [ARG ...]
5902 # -v: print how NAME would be invoked (name for a builtin/function/keyword,
5903 # "alias NAME='...'" for an alias, full path for an external file);
5904 # exit non-zero if NAME is not found -- the ubiquitous feature-detection
5905 # idiom `if command -v foo >/dev/null; then ...`.
5906 # -V: verbose description, like "type NAME".
5907 # plain: run NAME bypassing any shell function of that name.
5908 # -p is accepted and ignored (PATH is always searched).
5909 sub _cmd_command {
5910 7 ▶ my ($class, $rest, $raw_pre, $opts_ref) = @_;
Subroutine _cmd_command: called CC=22 SCAR=33.6
5911 7 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 7 7
5912 7 my @w = map { _arr_dequote($_) } _arr_split_words($rest);
5913  
5914 7 my $mode = ''; # '', 'v', 'V'
5915 7 ▶ while (@w && $w[0] =~ /\A-[pvV]+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@w and $w[0] =~ /\A-[pvV]+\z/) 6 7 13
Condition66%2/3
@w and $w[0] =~ /\A-[pvV]+\z/
!ll&&!rl&&r
076
MC/DC50%1/2
@w and $w[0] =~ /\A-[pvV]+\z/
@w $w[0] =~ /\A-[pvV]+\z/
Truth table66%2/3
A and B
A= @wB= $w[0] =~ /\A-[pvV]+\z/
ABresult
0X0
100
111
5916 6 my $opt = shift @w;
5917 6 ▶ $mode = 'v' if $opt =~ /v/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt =~ /v/ 5 1 6
5918 6 ▶ $mode = 'V' if $opt =~ /V/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt =~ /V/ 1 5 6
5919 # -p: accepted, no effect
5920 }
5921 # A bare "--" ends option parsing.
5922 7 ▶ shift @w if @w && $w[0] eq '--';
Branch: 1 branch
BranchTrueFalseTotal
if @w and $w[0] eq "--" 0 7 7
Condition33%1/3
@w and $w[0] eq "--"
!ll&&!rl&&r
070
MC/DC0%0/2
@w and $w[0] eq "--"
@w $w[0] eq "--"
Truth table33%1/3
A and B
A= @wB= $w[0] eq "--"
ABresult
0X0
100
111
5923  
5924 7 ▶ if (!@w) { $LAST_STATUS = ($mode ne '') ? 1 : 0; return $LAST_STATUS }
Branch: 2 branches
BranchTrueFalseTotal
unless (@w) 0 7 7
$mode ne '' ? : 0 0 0
5925  
5926 7 ▶ if ($mode eq 'v' || $mode eq 'V') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "v" or $mode eq "V") 6 1 7
Condition100%3/3
$mode eq "v" or $mode eq "V"
l!l&&r!l&&!r
511
MC/DC100%2/2
$mode eq "v" or $mode eq "V"
$mode eq "v" $mode eq "V"
Truth table100%3/3
A or B
A= $mode eq "v"B= $mode eq "V"
ABresult
1X1
011
000
5927 6 my $status = 0;
5928 6 for my $name (@w) {
5929 6 my ($kind, $detail) = _sh_name_kind($name);
5930 6 ▶ if ($mode eq 'V') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq 'V') { } 1 5 6
5931 1 ▶ _print_type_verbose($name, $kind, $detail) or $status = 1;
Branch: 1 branch
BranchTrueFalseTotal
_print_type_verbose($name, $kind, $detail) or $status = 1 0 1 1
5932 }
5933 else { # -v
5934 5 ▶ if ($kind eq 'alias') { print "alias $name='" . (defined $detail ? $detail : '') . "'\n" }
Branch: 4 branches
BranchTrueFalseTotal
if ($kind eq 'alias') { } 1 4 5
elsif ($kind eq 'file') { } 1 3 4
elsif ($kind eq '') { } 1 2 3
defined $detail ? : 1 0 1
5935 1 elsif ($kind eq 'file') { print $detail, "\n" }
5936 1 elsif ($kind eq '') { $status = 1 }
5937 2 else { print $name, "\n" } # keyword/function/builtin
5938 }
5939 }
5940 6 $LAST_STATUS = $status;
5941 6 return $status;
5942 }
5943  
5944 # Plain run form: re-dispatch the raw remainder with function lookup
5945 # suppressed. Recover the text after the leading "command" word (and any
5946 # -p / -- options) from the pre-expansion source so _expand() runs once.
5947 1 ▶ my $raw = defined $raw_pre ? $raw_pre : $rest;
Branch: 1 branch
BranchTrueFalseTotal
defined $raw_pre ? : 1 0 1
5948 1 $raw =~ s/\A\s*//;
5949 1 $raw =~ s/\A[Cc][Oo][Mm][Mm][Aa][Nn][Dd]\b//;
5950 1 $raw =~ s/\A\s+//;
5951 1 ▶ while ($raw =~ /\A(-[pvV]+|--)\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw =~ /\A(-[pvV]+|--)\s/) 0 1 1
5952 0 my $tok = $1;
5953 0 $raw =~ s/\A(?:-[pvV]+|--)\s+//;
5954 0 ▶ last if $tok eq '--';
Branch: 1 branch
BranchTrueFalseTotal
if $tok eq "--" 0 0 0
5955 }
5956 1 local $_CMD_NO_FUNC = 1;
5957 1 return _exec_line($class, $raw, $opts_ref);
5958 }
5959  
5960 # ----------------------------------------------------------------
5961 # Background execution helpers (v1)
5962 # ----------------------------------------------------------------
5963 # ----------------------------------------------------------------
5964 # umask / hash / readonly / mapfile (v0.08)
5965 # ----------------------------------------------------------------
5966 # _sh_is_readonly(NAME): true when NAME carries the readonly attribute.
5967 sub _sh_is_readonly {
5968 8 ▶ my ($name) = @_;
Subroutine _sh_is_readonly: called CC=3 SCAR=11.7
5969 8 ▶ return 0 unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 8 8
5970 8 ▶ return $_SH_READONLY{ uc($name) } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$BATsh::SH::_SH_READONLY{uc $name} ? : 0 8 8
5971 }
5972  
5973 # _sh_store_scalar(NAME, VALUE): the single choke point for a plain scalar
5974 # assignment. Honours the readonly attribute (refused, status source
5975 # returns 0) and the integer attribute (VALUE evaluated as arithmetic).
5976 # Returns 1 when the value was stored, 0 when a readonly variable blocked
5977 # it. All the ordinary assignment paths (VAR=val, prefix VAR=val cmd,
5978 # export VAR=val) funnel through here so the attributes are enforced
5979 # uniformly.
5980 sub _sh_store_scalar {
5981 222 ▶ my ($name, $val) = @_;
Subroutine _sh_store_scalar: called CC=5 SCAR=16.2
5982 222 ▶ $val = _unprotect_lit($val) if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 222 0 222
5983 222 my $ik = uc($name);
5984 222 ▶ if ($_SH_READONLY{$ik}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BATsh::SH::_SH_READONLY{$ik}) 2 220 222
5985 2 print STDERR "sh: $name: readonly variable\n";
5986 2 return 0;
5987 }
5988 220 ▶ if ($_SH_INTATTR{$ik}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BATsh::SH::_SH_INTATTR{$ik}) 1 219 220
5989 1 ▶ $val = _eval_arith(defined $val ? $val : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 1 0 1
5990 }
5991 220 BATsh::Env->set($name, $val);
5992 220 return 1;
5993 }
5994  
5995 # umask [-S] [MODE]
5996 # With no MODE, print the current file-creation mask (octal, or symbolic
5997 # with -S). A numeric (octal) MODE sets the mask. Uses Perl's umask, so
5998 # it is a genuine process mask on Unix-like systems (a no-op reflecting
5999 # whatever the C library reports on Win32).
6000 sub _cmd_umask {
6001 12 ▶ my ($rest) = @_;
Subroutine _cmd_umask: called CC=9 SCAR=22.2
6002 12 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 12 12
6003 12 $rest =~ s/\A\s+//; $rest =~ s/\s+\z//;
6004  
6005 # Seed the shell mask from the real process umask on first use.
6006 12 ▶ if (!$_SH_UMASK_INIT) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($BATsh::SH::_SH_UMASK_INIT) 1 11 12
6007 1 my $u = umask();
6008 1 ▶ $_SH_UMASK = defined $u ? $u : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $u ? : 1 0 1
6009 1 $_SH_UMASK_INIT = 1;
6010 }
6011  
6012 12 my $symbolic = 0;
6013 12 ▶ if ($rest =~ s/\A-S\b\s*//) { $symbolic = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/\A-S\b\s*//) 1 11 12
6014  
6015 12 ▶ if ($rest eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest eq "") 5 7 12
6016 5 ▶ if ($symbolic) { print _umask_symbolic($_SH_UMASK), "\n" }
Branch: 1 branch
BranchTrueFalseTotal
if ($symbolic) { } 1 4 5
6017 4 else { printf "%04o\n", $_SH_UMASK }
6018 5 $LAST_STATUS = 0;
6019 5 return 0;
6020 }
6021 7 ▶ if ($rest =~ /\A[0-7]+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /\A[0-7]+\z/) 5 2 7
6022 5 $_SH_UMASK = oct($rest) & 0777;
6023 5 umask($_SH_UMASK); # honoured on Unix, harmless where it is not
6024 5 $LAST_STATUS = 0;
6025 5 return 0;
6026 }
6027 # Symbolic mode: "u=rwx,g=rx,o=rx" (and +/- ops). The operand names the
6028 # permissions to LEAVE enabled; the mask is their complement.
6029 2 my $sym = _umask_apply_symbolic($_SH_UMASK, $rest);
6030 2 ▶ if (defined $sym) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sym) 2 0 2
6031 2 $_SH_UMASK = $sym;
6032 2 umask($_SH_UMASK);
6033 2 $LAST_STATUS = 0;
6034 2 return 0;
6035 }
6036 0 print STDERR "sh: umask: $rest: invalid mask\n";
6037 0 $LAST_STATUS = 1;
6038 0 return 1;
6039 }
6040  
6041 # _umask_apply_symbolic(MASK, SPEC): apply a chmod-style symbolic SPEC
6042 # ("[ugoa]*[-+=][rwx]*", comma-separated) to the numeric umask MASK, in
6043 # terms of the permissions the mask leaves ENABLED. Returns the new
6044 # numeric mask, or undef when SPEC is not valid symbolic syntax.
6045 sub _umask_apply_symbolic {
6046 2 ▶ my ($mask, $spec) = @_;
Subroutine _umask_apply_symbolic: called CC=14 SCAR=27.6
6047 2 ▶ return undef unless defined $spec && $spec ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec and $spec ne "" 0 2 2
Condition33%1/3
defined $spec and $spec ne ""
!ll&&!rl&&r
002
MC/DC0%0/2
defined $spec and $spec ne ""
defined $spec $spec ne ""
Truth table33%1/3
A and B
A= defined $specB= $spec ne ""
ABresult
0X0
100
111
6048 2 my @allow = (
6049 (~ (($mask >> 6) & 7)) & 7,
6050 (~ (($mask >> 3) & 7)) & 7,
6051 (~ ($mask & 7)) & 7,
6052 );
6053 2 my %idx = ('u' => 0, 'g' => 1, 'o' => 2);
6054 2 for my $clause (split /,/, $spec) {
6055 4 ▶ return undef unless $clause =~ /\A([ugoa]*)([-+=])([rwx]*)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $clause =~ /\A([ugoa]*)([-+=])([rwx]*)\z/ 0 4 4
6056 4 my ($who, $op, $pstr) = ($1, $2, $3);
6057 4 my $perm = 0;
6058 4 ▶ $perm |= 4 if $pstr =~ /r/;
Branch: 1 branch
BranchTrueFalseTotal
if $pstr =~ /r/ 2 2 4
6059 4 ▶ $perm |= 2 if $pstr =~ /w/;
Branch: 1 branch
BranchTrueFalseTotal
if $pstr =~ /w/ 1 3 4
6060 4 ▶ $perm |= 1 if $pstr =~ /x/;
Branch: 1 branch
BranchTrueFalseTotal
if $pstr =~ /x/ 1 3 4
6061 4 my @classes;
6062 4 ▶ if ($who eq '' || $who =~ /a/) { @classes = (0, 1, 2) }
Branch: 1 branch
BranchTrueFalseTotal
if ($who eq '' or $who =~ /a/) { } 0 4 4
Condition33%1/3
$who eq "" or $who =~ /a/
l!l&&r!l&&!r
004
MC/DC0%0/2
$who eq "" or $who =~ /a/
$who eq "" $who =~ /a/
Truth table33%1/3
A or B
A= $who eq ""B= $who =~ /a/
ABresult
1X1
011
000
6063 else {
6064 4 for my $w (split //, $who) { push @classes, $idx{$w} }
6065 }
6066 4 for my $ci (@classes) {
6067 4 ▶ if ($op eq '=') { $allow[$ci] = $perm }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq '=') { } 3 1 4
elsif ($op eq '+') { } 0 1 1
6068 0 elsif ($op eq '+') { $allow[$ci] |= $perm }
6069 1 else { $allow[$ci] &= (~ $perm) & 7 }
6070 }
6071 }
6072 2 my $newmask = 0;
6073 2 $newmask |= ((~ $allow[0]) & 7) << 6;
6074 2 $newmask |= ((~ $allow[1]) & 7) << 3;
6075 2 $newmask |= (~ $allow[2]) & 7;
6076 2 return $newmask & 0777;
6077 }
6078  
6079 # _umask_symbolic(MASK): render a numeric umask as the bash-style symbolic
6080 # form "u=rwx,g=rx,o=rx" (the permissions the mask LEAVES enabled).
6081 sub _umask_symbolic {
6082 1 ▶ my ($mask) = @_;
Subroutine _umask_symbolic: called CC=5 SCAR=16.2
6083 1 my @cls = ('u', 'g', 'o');
6084 1 my @sh = (($mask >> 6) & 7, ($mask >> 3) & 7, $mask & 7);
6085 1 my @out;
6086 1 for my $i (0 .. 2) {
6087 3 my $allow = (~ $sh[$i]) & 7;
6088 3 my $s = '';
6089 3 ▶ $s .= 'r' if $allow & 4;
Branch: 1 branch
BranchTrueFalseTotal
if $allow & 4 3 0 3
6090 3 ▶ $s .= 'w' if $allow & 2;
Branch: 1 branch
BranchTrueFalseTotal
if $allow & 2 1 2 3
6091 3 ▶ $s .= 'x' if $allow & 1;
Branch: 1 branch
BranchTrueFalseTotal
if $allow & 1 3 0 3
6092 3 push @out, $cls[$i] . '=' . $s;
6093 }
6094 1 return join(',', @out);
6095 }
6096  
6097 # hash [-r] [-l] [-t] [-d name] [-p path name] [NAME ...]
6098 # BATsh resolves commands through PATH on every call and keeps no
6099 # location cache, so the maintenance forms (-r clear, -l/-t list, -d/-p)
6100 # are accepted as successful no-ops. "hash NAME ..." verifies each NAME
6101 # is found on PATH (status 1 if any is missing), matching the observable
6102 # result of bash caching a lookup.
6103 sub _cmd_hash {
6104 3 ▶ my ($rest) = @_;
Subroutine _cmd_hash: called CC=8 SCAR=21.0
6105 3 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 3 3
6106 3 $rest =~ s/\A\s+//; $rest =~ s/\s+\z//;
6107 3 ▶ return _sh_set_status(0) if $rest eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $rest eq "" 0 3 3
6108 3 ▶ return _sh_set_status(0) if $rest =~ /\A-[rlt]\b\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $rest =~ /\A-[rlt]\b\s*\z/ 1 2 3
6109 2 ▶ return _sh_set_status(0) if $rest =~ /\A-[dp]\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $rest =~ /\A-[dp]\b/ 0 2 2
6110  
6111 2 my @tok = map { _arr_dequote($_) } _arr_split_words($rest);
6112 2 my $status = 0;
6113 2 for my $name (@tok) {
6114 2 ▶ next if $name =~ /\A-/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\A-/ 0 2 2
6115 2 ▶ if (!defined _sh_find_on_path($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined _sh_find_on_path($name)) 1 1 2
6116 1 print STDERR "sh: hash: $name: not found\n";
6117 1 $status = 1;
6118 }
6119 }
6120 2 return _sh_set_status($status);
6121 }
6122  
6123 sub _sh_set_status {
6124 13 ▶ my ($s) = @_;
Subroutine _sh_set_status: called CC=1 SCAR=0.0
6125 13 $LAST_STATUS = $s;
6126 13 return $s;
6127 }
6128  
6129 # readonly [-p] [NAME[=VALUE] ...]
6130 # Mark each NAME read-only (optionally assigning VALUE first). With no
6131 # NAME, or with -p, list the current readonly variables in a form that
6132 # can be re-read.
6133 sub _cmd_readonly {
6134 5 ▶ my ($class, $rest) = @_;
Subroutine _cmd_readonly: called CC=12 SCAR=34.0
6135 5 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 5 5
6136 5 $rest =~ s/\A\s+//; $rest =~ s/\s+\z//;
6137  
6138 5 ▶ if ($rest eq '' || $rest eq '-p') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest eq "" or $rest eq "-p") 0 5 5
Condition33%1/3
$rest eq "" or $rest eq "-p"
l!l&&r!l&&!r
005
MC/DC0%0/2
$rest eq "" or $rest eq "-p"
$rest eq "" $rest eq "-p"
Truth table33%1/3
A or B
A= $rest eq ""B= $rest eq "-p"
ABresult
1X1
011
000
6139 0 for my $ik (sort keys %_SH_READONLY) {
6140 0 my $v = BATsh::Env->get($ik);
6141 0 ▶ $v = '' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
6142 0 $v =~ s/'/'\\''/g;
6143 0 print "readonly $ik='$v'\n";
6144 }
6145 0 return _sh_set_status(0);
6146 }
6147  
6148 5 my @tok = map { _arr_dequote($_) } _arr_split_words($rest);
6149 5 my $status = 0;
6150 5 for my $t (@tok) {
6151 5 ▶ next if $t eq '-p' || $t eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $t eq "-p" or $t eq "" 0 5 5
Condition33%1/3
$t eq "-p" or $t eq ""
l!l&&r!l&&!r
005
MC/DC0%0/2
$t eq "-p" or $t eq ""
$t eq "-p" $t eq ""
Truth table33%1/3
A or B
A= $t eq "-p"B= $t eq ""
ABresult
1X1
011
000
6152 5 ▶ if ($t =~ /\A([A-Za-z_][A-Za-z0-9_]*)=(.*)\z/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t =~ /\A([A-Za-z_][A-Za-z0-9_]*)=(.*)\z/s) { } 5 0 5
elsif ($t =~ /\A([A-Za-z_][A-Za-z0-9_]*)\z/) { } 0 0 0
6153 5 my ($name, $val) = ($1, $2);
6154 5 ▶ if (_sh_is_readonly($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_sh_is_readonly($name)) 0 5 5
6155 0 print STDERR "sh: $name: readonly variable\n";
6156 0 $status = 1;
6157 0 next;
6158 }
6159 5 BATsh::Env->set($name, $val);
6160 5 $_SH_READONLY{ uc($name) } = 1;
6161 }
6162 elsif ($t =~ /\A([A-Za-z_][A-Za-z0-9_]*)\z/) {
6163 0 $_SH_READONLY{ uc($1) } = 1;
6164 }
6165 }
6166 5 return _sh_set_status($status);
6167 }
6168  
6169 # mapfile / readarray [-t] [-d DELIM] [-n COUNT] [-O ORIGIN] [-s SKIP] [ARRAY]
6170 # Read lines from standard input into the indexed array ARRAY (default
6171 # MAPFILE). -t strips the trailing delimiter from each line; -d sets the
6172 # line delimiter (default newline); -n COUNT copies at most COUNT lines
6173 # (0 = all); -s SKIP discards the first SKIP lines; -O ORIGIN stores the
6174 # first line at index ORIGIN. STDIN is whatever the surrounding
6175 # redirection supplied (e.g. "mapfile arr < file").
6176 sub _cmd_mapfile {
6177 5 ▶ my ($class, $rest) = @_;
Subroutine _cmd_mapfile: called CC=31 SCAR=40.4
6178 5 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 5 5
6179 5 my @tok = map { _arr_dequote($_) } _arr_split_words($rest);
6180  
6181 5 my $strip = 0;
6182 5 my $delim = "\n";
6183 5 my $count = 0; # 0 = unlimited
6184 5 my $skip = 0;
6185 5 my $origin = 0;
6186 5 ▶ while (@tok) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tok) 13 0 13
6187 13 my $t = $tok[0];
6188 13 ▶ if ($t eq '-t') { shift @tok; $strip = 1 }
Branch: 8 branches
BranchTrueFalseTotal
if ($t eq '-t') { } 5 8 13
elsif ($t eq '-n') { } 1 7 8
elsif ($t eq '-s') { } 1 6 7
elsif ($t eq '-O') { } 1 5 6
elsif ($t eq '-d') { } 0 5 5
elsif ($t =~ /\A-d(.+)\z/s) { } 0 5 5
elsif ($t eq '-u' or $t eq '-c' or $t eq '-C') { } 0 5 5
elsif ($t =~ /\A-/) { } 0 5 5
6189 1 ▶ elsif ($t eq '-n') { shift @tok; $count = @tok ? int(_arr_num(shift @tok)) : 0 }
Branch: 1 branch
BranchTrueFalseTotal
@tok ? : 1 0 1
6190 1 ▶ elsif ($t eq '-s') { shift @tok; $skip = @tok ? int(_arr_num(shift @tok)) : 0 }
Branch: 1 branch
BranchTrueFalseTotal
@tok ? : 1 0 1
6191 1 ▶ elsif ($t eq '-O') { shift @tok; $origin = @tok ? int(_arr_num(shift @tok)) : 0 }
Branch: 1 branch
BranchTrueFalseTotal
@tok ? : 1 0 1
6192 0 ▶ elsif ($t eq '-d') { shift @tok; $delim = @tok ? shift @tok : "\n" }
Branch: 1 branch
BranchTrueFalseTotal
@tok ? : 0 0 0
6193 0 elsif ($t =~ /\A-d(.+)\z/s) { shift @tok; $delim = $1 }
6194 0 ▶ elsif ($t eq '-u' || $t eq '-c' || $t eq '-C') { shift @tok; shift @tok if @tok }
Branch: 1 branch
BranchTrueFalseTotal
if @tok 0 0 0
Condition33%2/6
$t eq "-u" or $t eq "-c" or $t eq "-C"
l!l&&r!l&&!r
005
$t eq "-u" or $t eq "-c"
l!l&&r!l&&!r
005
MC/DC0%0/3
$t eq "-u" or $t eq "-c" or $t eq "-C"
$t eq "-u" $t eq "-c" $t eq "-C"
Truth table25%1/4
A or B or C
A= $t eq "-u"B= $t eq "-c"C= $t eq "-C"
ABCresult
1XX1
01X1
0011
0000
6195 0 elsif ($t =~ /\A-/) { shift @tok } # unknown flag: ignore
6196 5 else { last }
6197 }
6198 5 ▶ my $name = @tok ? shift @tok : 'MAPFILE';
Branch: 1 branch
BranchTrueFalseTotal
@tok ? : 5 0 5
6199 5 ▶ $name = 'MAPFILE' unless defined $name && $name ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and $name ne "" 0 5 5
Condition33%1/3
defined $name and $name ne ""
!ll&&!rl&&r
005
MC/DC0%0/2
defined $name and $name ne ""
defined $name $name ne ""
Truth table33%1/3
A and B
A= defined $nameB= $name ne ""
ABresult
0X0
100
111
6200  
6201 5 ▶ my $sep = (defined $delim && length $delim) ? substr($delim, 0, 1) : "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $delim && length $delim ? : 5 0 5
Condition33%1/3
defined $delim and length $delim
!ll&&!rl&&r
005
MC/DC0%0/2
defined $delim and length $delim
defined $delim length $delim
Truth table33%1/3
A and B
A= defined $delimB= length $delim
ABresult
0X0
100
111
6202 5 my @lines;
6203 {
6204 5 local $/ = $sep;
6205 5 ▶ while (defined(my $l = <STDIN>)) { push @lines, $l }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $l = readline STDIN)) 15 5 20
6206 }
6207  
6208 5 ▶ if ($skip > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip > 0) 1 4 5
6209 1 ▶ if ($skip >= @lines) { @lines = () }
Branch: 1 branch
BranchTrueFalseTotal
if ($skip >= @lines) { } 0 1 1
6210 1 else { splice(@lines, 0, $skip) }
6211 }
6212 5 ▶ if ($count > 0 && @lines > $count) { @lines = @lines[0 .. $count - 1] }
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0 and @lines > $count) 1 4 5
Condition66%2/3
$count > 0 and @lines > $count
!ll&&!rl&&r
401
MC/DC50%1/2
$count > 0 and @lines > $count
$count > 0 @lines > $count
Truth table66%2/3
A and B
A= $count > 0B= @lines > $count
ABresult
0X0
100
111
6213 5 ▶ if ($strip) { for my $l (@lines) { $l =~ s/\Q$sep\E\z// } }
Branch: 1 branch
BranchTrueFalseTotal
if ($strip) 5 0 5
6214 5 @lines = map { BATsh::MB::enc($_) } @lines;
6215  
6216 5 my $k = _arr_name($name);
6217 5 $_SH_ARRAY{$k} = {};
6218 5 $_SH_ARRAY_TYPE{$k} = 'indexed';
6219 5 BATsh::Env->unset($name);
6220 5 my $ix = $origin;
6221 5 for my $l (@lines) { $_SH_ARRAY{$k}{$ix} = $l; $ix++ }
6222  
6223 5 return _sh_set_status(0);
6224 }
6225  
6226 # _arr_num(S): integer value of a leading numeric prefix (0 otherwise).
6227 sub _arr_num {
6228 3 ▶ my ($s) = @_;
Subroutine _arr_num: called CC=3 SCAR=11.7
6229 3 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 3 3
6230 3 ▶ return ($s =~ /\A\s*([-+]?\d+)/) ? int($1) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /\A\s*([-+]?\d+)/ ? : 3 0 3
6231 }
6232  
6233  
6234 # Rules:
6235 # * only the last non-space character may be the background &
6236 # * it must not be part of && (i.e. preceding char must not be &)
6237 # * it must not be an fd-duplication >& (preceding char must not be >)
6238 # * it must be outside single/double quotes
6239 # * the remaining command must be non-empty
6240 sub _split_trailing_bg {
6241 1152 ▶ my ($line) = @_;
Subroutine _split_trailing_bg: called CC=22 SCAR=34.7
6242 1152 ▶ return (0, $line) unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 1152 1152
6243  
6244 # Find the index of the last character, ignoring trailing whitespace.
6245 1152 my $rtrim = $line;
6246 1152 $rtrim =~ s/\s+\z//;
6247 1152 ▶ return (0, $line) if $rtrim eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $rtrim eq "" 0 1152 1152
6248  
6249 1152 my @chars = split //, $rtrim;
6250 1152 my $last = $#chars;
6251 1152 ▶ return (0, $line) unless $chars[$last] eq '&';
Branch: 1 branch
BranchTrueFalseTotal
unless $chars[$last] eq "&" 1144 8 1152
6252  
6253 # && is a compound operator, not background.
6254 8 ▶ return (0, $line) if $last >= 1 && $chars[$last-1] eq '&';
Branch: 1 branch
BranchTrueFalseTotal
if $last >= 1 and $chars[$last - 1] eq "&" 0 8 8
Condition66%2/3
$last >= 1 and $chars[$last - 1] eq "&"
!ll&&!rl&&r
170
MC/DC0%0/2
$last >= 1 and $chars[$last - 1] eq "&"
$last >= 1 $chars[$last - 1] eq "&"
Truth table66%2/3
A and B
A= $last >= 1B= $chars[$last - 1] eq "&"
ABresult
0X0
100
111
6255 # >& is fd duplication, not background.
6256 8 ▶ return (0, $line) if $last >= 1 && $chars[$last-1] eq '>';
Branch: 1 branch
BranchTrueFalseTotal
if $last >= 1 and $chars[$last - 1] eq ">" 0 8 8
Condition66%2/3
$last >= 1 and $chars[$last - 1] eq ">"
!ll&&!rl&&r
170
MC/DC0%0/2
$last >= 1 and $chars[$last - 1] eq ">"
$last >= 1 $chars[$last - 1] eq ">"
Truth table66%2/3
A and B
A= $last >= 1B= $chars[$last - 1] eq ">"
ABresult
0X0
100
111
6257  
6258 # Verify the trailing & is outside quotes and not backslash-escaped by
6259 # scanning up to it. $esc_last becomes true if the char at $last is
6260 # escaped by an immediately preceding (unquoted) backslash.
6261 8 my $in_sq = 0;
6262 8 my $in_dq = 0;
6263 8 my $esc_last = 0;
6264 8 my $i = 0;
6265 8 ▶ while ($i < $last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $last) 87 8 95
6266 87 my $ch = $chars[$i];
6267 87 ▶ if ($in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq) 0 87 87
6268 0 ▶ $in_sq = 0 if $ch eq "'";
Branch: 1 branch
BranchTrueFalseTotal
if $ch eq "'" 0 0 0
6269 0 $i++; next;
6270 }
6271 87 ▶ if ($ch eq '\\' && !$in_sq) { # backslash escapes next char
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\" and not $in_sq) 1 86 87
Condition66%2/3
$ch eq "\\" and not $in_sq
!ll&&!rl&&r
8601
MC/DC50%1/2
$ch eq "\\" and not $in_sq
$ch eq "\\" not $in_sq
Truth table66%2/3
A and not(B)
A= $ch eq "\\"B= not $in_sq
ABresult
0X0
100
111
6272 1 ▶ $esc_last = 1 if $i + 1 == $last;
Branch: 1 branch
BranchTrueFalseTotal
if $i + 1 == $last 1 0 1
6273 1 $i += 2; next;
6274 }
6275 86 ▶ if ($ch eq "'" && !$in_dq) { $in_sq = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $in_dq) 0 86 86
Condition33%1/3
$ch eq "'" and not $in_dq
!ll&&!rl&&r
8600
MC/DC0%0/2
$ch eq "'" and not $in_dq
$ch eq "'" not $in_dq
Truth table33%1/3
A and not(B)
A= $ch eq "'"B= not $in_dq
ABresult
0X0
100
111
6276 86 ▶ if ($ch eq '"' && !$in_sq) { $in_dq = !$in_dq; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" and not $in_sq) 2 84 86
Condition66%2/3
$ch eq "\"" and not $in_sq
!ll&&!rl&&r
8402
MC/DC50%1/2
$ch eq "\"" and not $in_sq
$ch eq "\"" not $in_sq
Truth table66%2/3
A and not(B)
A= $ch eq "\""B= not $in_sq
ABresult
0X0
100
111
6277 84 $i++;
6278 }
6279 8 ▶ return (0, $line) if $in_sq || $in_dq; # & is inside a quote
Branch: 1 branch
BranchTrueFalseTotal
if $in_sq or $in_dq 0 8 8
Condition33%1/3
$in_sq or $in_dq
l!l&&r!l&&!r
008
MC/DC0%0/2
$in_sq or $in_dq
$in_sq $in_dq
Truth table33%1/3
A or B
A= $in_sqB= $in_dq
ABresult
1X1
011
000
6280 8 ▶ return (0, $line) if $esc_last; # & is backslash-escaped
Branch: 1 branch
BranchTrueFalseTotal
if $esc_last 1 7 8
6281  
6282 # Strip the trailing & (and surrounding whitespace before it).
6283 7 my $stripped = join('', @chars[0 .. $last-1]);
6284 7 $stripped =~ s/\s+\z//;
6285 7 ▶ return (0, $line) if $stripped eq ''; # nothing to run
Branch: 1 branch
BranchTrueFalseTotal
if $stripped eq "" 1 6 7
6286  
6287 6 return (1, $stripped);
6288 }
6289  
6290 # _sh_word_is_foreground: true when the first word of a backgrounded line
6291 # is a BATsh builtin, defined SH function, control keyword, or a variable
6292 # assignment. Such commands run in the foreground and the trailing &
6293 # is ignored (documented limitation: only external commands background).
6294 sub _sh_word_is_foreground {
6295 6 ▶ my ($w) = @_;
Subroutine _sh_word_is_foreground: called CC=10 SCAR=25.7
6296 6 ▶ return 0 unless defined $w && $w ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $w and $w ne "" 0 6 6
Condition33%1/3
defined $w and $w ne ""
!ll&&!rl&&r
006
MC/DC0%0/2
defined $w and $w ne ""
defined $w $w ne ""
Truth table33%1/3
A and B
A= defined $wB= $w ne ""
ABresult
0X0
100
111
6297  
6298 # VAR=value assignment
6299 6 ▶ return 1 if $w =~ /\A[A-Za-z_][A-Za-z0-9_]*=/;
Branch: 1 branch
BranchTrueFalseTotal
if $w =~ /\A[A-Za-z_][A-Za-z0-9_]*=/ 2 4 6
6300  
6301 # test bracket and no-op
6302 4 ▶ return 1 if $w eq '[' || $w eq ':' || $w eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $w eq "[" or $w eq ":" or $w eq "." 0 4 4
Condition33%2/6
$w eq "[" or $w eq ":" or $w eq "."
l!l&&r!l&&!r
004
$w eq "[" or $w eq ":"
l!l&&r!l&&!r
004
MC/DC0%0/3
$w eq "[" or $w eq ":" or $w eq "."
$w eq "[" $w eq ":" $w eq "."
Truth table25%1/4
A or B or C
A= $w eq "["B= $w eq ":"C= $w eq "."
ABCresult
1XX1
01X1
0011
0000
6303  
6304 4 my $lc = lc($w);
6305  
6306 4 my %builtin = (
6307 export => 1, unset => 1, echo => 1, printf => 1, cd => 1,
6308 pwd => 1, exit => 1, 'true' => 1, 'false' => 1, read => 1,
6309 test => 1, source => 1, 'return' => 1, 'break' => 1,
6310 'continue' => 1, shift => 1, local => 1, set => 1,
6311 let => 1, type => 1, command => 1,
6312 umask => 1, hash => 1, readonly => 1, mapfile => 1, readarray => 1,
6313 );
6314 4 ▶ return 1 if $builtin{$lc};
Branch: 1 branch
BranchTrueFalseTotal
if $builtin{$lc} 2 2 4
6315  
6316 # Control keywords (defensive; these are normally handled in _run_lines)
6317 2 my %kw = (
6318 'if' => 1, then => 1, 'else' => 1, elif => 1, fi => 1,
6319 'for' => 1, 'while' => 1, until => 1, 'do' => 1, done => 1,
6320 case => 1, esac => 1, function => 1, in => 1,
6321 );
6322 2 ▶ return 1 if $kw{$lc};
Branch: 1 branch
BranchTrueFalseTotal
if $kw{$lc} 0 2 2
6323  
6324 # Defined SH function (case-sensitive, as in _exec_line dispatch)
6325 2 ▶ return 1 if exists $_SH_FUNCTIONS{$w};
Branch: 1 branch
BranchTrueFalseTotal
if exists $BATsh::SH::_SH_FUNCTIONS{$w} 0 2 2
6326  
6327 2 return 0;
6328 }
6329  
6330 # _bg_tempfile: create a unique, empty temp file (O_CREAT|O_EXCL to avoid
6331 # symlink races) for capturing a background job's PID on Unix-like systems.
6332 # Returns the path, or undef on failure.
6333 sub _bg_tempfile {
6334 1 ▶ my $dir = $ENV{'TMPDIR'} || $ENV{'TEMP'} || $ENV{'TMP'} || '';
Subroutine _bg_tempfile: called CC=11 SCAR=29.3
Condition37%3/8
$ENV{'TMPDIR'} || $ENV{'TEMP'} || $ENV{'TMP'} || ''
l!l
01
$ENV{"TMPDIR"} or $ENV{"TEMP"} or $ENV{"TMP"}
l!l&&r!l&&!r
001
$ENV{"TMPDIR"} or $ENV{"TEMP"}
l!l&&r!l&&!r
001
MC/DC0%0/3
$ENV{'TMPDIR'} || $ENV{'TEMP'} || $ENV{'TMP'} || ''
$ENV{"TMPDIR"} $ENV{"TEMP"} $ENV{"TMP"}
Truth table25%1/4
A or B or C
A= $ENV{"TMPDIR"}B= $ENV{"TEMP"}C= $ENV{"TMP"}
ABCresult
1XX1
01X1
0011
0000
6335 1 ▶ $dir = '/tmp' if $dir eq '' && -d '/tmp';
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq "" and -d "/tmp" 1 0 1
Condition33%1/3
$dir eq "" and -d "/tmp"
!ll&&!rl&&r
001
MC/DC0%0/2
$dir eq "" and -d "/tmp"
$dir eq "" -d "/tmp"
Truth table33%1/3
A and B
A= $dir eq ""B= -d "/tmp"
ABresult
0X0
100
111
6336 1 ▶ $dir = '.' if $dir eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq "" 0 1 1
6337 1 $dir =~ s{[\\/]+\z}{};
6338 1 ▶ $dir = '.' if !(-d $dir && -w $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir and -w $dir 0 1 1
Condition33%1/3
-d $dir and -w $dir
!ll&&!rl&&r
001
MC/DC0%0/2
-d $dir and -w $dir
-d $dir -w $dir
Truth table33%1/3
A and B
A= -d $dirB= -w $dir
ABresult
0X0
100
111
6339  
6340 1 my $attempt = 0;
6341 1 ▶ while ($attempt < 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attempt < 1000) 1 0 1
6342 1 $_BG_SEQ++;
6343 1 $attempt++;
6344 1 my $path = $dir . '/' . 'batsh_bg_' . $$ . '_' . $_BG_SEQ;
6345 1 ▶ if (sysopen(_BG_TMP, $path, O_WRONLY | O_CREAT | O_EXCL, 0600)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysopen _BG_TMP, $path, 193, 384) 1 0 1
6346 1 close(_BG_TMP);
6347 1 push @_BG_TMPFILES, $path;
6348 1 return $path;
6349 }
6350 # EEXIST or transient error: retry with next sequence number
6351 }
6352 0 warn "sh: cannot create background pidfile in $dir: $!\n";
6353 0 return undef;
6354 }
6355  
6356 # _bg_launch: start $cmdline asynchronously.
6357 # Win32 : system(1, STRING) spawns via the command shell (P_NOWAIT)
6358 # and returns the PID directly.
6359 # Unix-like : delegate to /bin/sh so the job is backgrounded without a
6360 # Perl fork; the shell's $! (the job PID) is written to a
6361 # temp file and read back into BATsh's own $!.
6362 # On a successful launch $? (LAST_STATUS) is 0; the exit code of the
6363 # background job itself is not awaited (sh semantics).
6364 # _bg_launch: un-guard the command line, then hand it to the platform-
6365 # specific spawner below (system(1,...) on Win32, "&" on POSIX).
6366 sub _bg_launch {
6367 1 ▶ my ($class, $cmdline) = @_;
Subroutine _bg_launch: called CC=1 SCAR=0.0
6368 1 return _bg_launch_decoded($class, BATsh::MB::dec($cmdline));
6369 }
6370  
6371 sub _bg_launch_decoded {
6372 1 ▶ my ($class, $cmdline) = @_;
Subroutine _bg_launch_decoded: called CC=13 SCAR=33.1
6373 1 ▶ $cmdline = '' unless defined $cmdline;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cmdline 0 1 1
6374 1 ▶ return 0 if $cmdline =~ /\A\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $cmdline =~ /\A\s*\z/ 0 1 1
6375 1 BATsh::Env->sync_to_env();
6376  
6377 1 ▶ if ($^O =~ /MSWin32/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /MSWin32/i) 0 1 1
6378 0 my $pid = system(1, $cmdline);
6379 0 ▶ if (defined $pid && $pid > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pid and $pid > 0) { } 0 0 0
Condition0%0/3
defined $pid and $pid > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $pid and $pid > 0
defined $pid $pid > 0
Truth table0%0/3
A and B
A= defined $pidB= $pid > 0
ABresult
0X0
100
111
6380 0 $_LAST_BG_PID = $pid;
6381 0 $LAST_STATUS = 0;
6382 }
6383 else {
6384 0 warn "sh: failed to start background process\n";
6385 0 $LAST_STATUS = 1;
6386 }
6387 0 return $LAST_STATUS;
6388 }
6389  
6390 # Unix-like
6391 1 my $pidfile = _bg_tempfile();
6392 1 my $rc;
6393 1 ▶ if (defined $pidfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pidfile) { } 1 0 1
6394 # Group the command so that the whole list (pipelines, &&, ...) is
6395 # backgrounded as a unit, then echo the job PID ($!) to the file.
6396 1 $rc = system("{ $cmdline ; } & echo \$! > '$pidfile'");
6397 1 ▶ if (open(_BG_PIDFH, "< $pidfile")) {
Branch: 1 branch
BranchTrueFalseTotal
if (open _BG_PIDFH, "< $pidfile") 1 0 1
6398 1 local $/;
6399 1 my $buf = <_BG_PIDFH>;
6400 1 close(_BG_PIDFH);
6401 1 ▶ $buf = '' unless defined $buf;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $buf 0 1 1
6402 1 my $pid = '';
6403 1 ($pid) = ($buf =~ /(\d+)/);
6404 1 ▶ $_LAST_BG_PID = $pid if defined $pid && $pid ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $pid and $pid ne "" 1 0 1
Condition33%1/3
defined $pid and $pid ne ""
!ll&&!rl&&r
001
MC/DC0%0/2
defined $pid and $pid ne ""
defined $pid $pid ne ""
Truth table33%1/3
A and B
A= defined $pidB= $pid ne ""
ABresult
0X0
100
111
6405 }
6406 1 unlink $pidfile;
6407 1 @_BG_TMPFILES = grep { $_ ne $pidfile } @_BG_TMPFILES;
6408 }
6409 else {
6410 0 $rc = system("{ $cmdline ; } &");
6411 }
6412 1 ▶ $LAST_STATUS = (defined $rc && $rc != -1) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $rc && $rc != -1 ? : 1 0 1
Condition33%1/3
defined $rc and $rc != -1
!ll&&!rl&&r
001
MC/DC0%0/2
defined $rc and $rc != -1
defined $rc $rc != -1
Truth table33%1/3
A and B
A= defined $rcB= $rc != -1
ABresult
0X0
100
111
6413 1 return $LAST_STATUS;
6414 }
6415  
6416  
6417 # ----------------------------------------------------------------
6418 # Split "cmd rest" honouring quoted strings
6419 # ----------------------------------------------------------------
6420 sub _split_sh {
6421 1406 ▶ my ($line) = @_;
Subroutine _split_sh: called CC=2 SCAR=7.6
6422 1406 ▶ if ($line =~ /\A(\S+)\s*(.*)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A(\S+)\s*(.*)\z/s) 1406 0 1406
6423 1406 return ($1, $2);
6424 }
6425 0 return ($line, '');
6426 }
6427  
6428 # ----------------------------------------------------------------
6429 # _sh_assign_prefix: detect POSIX assignment prefixes on a RAW (un-expanded)
6430 # command line, e.g. `IFS= read -r LINE` or `LC_ALL=C sort file`.
6431 #
6432 # Parses one or more leading VAR=VALUE words, where VALUE is read with
6433 # quote / $(...) / backtick awareness so that an assignment whose value
6434 # merely *contains* spaces (e.g. UPPER=$(echo "a b")) is NOT mistaken for
6435 # a prefix followed by a command.
6436 #
6437 # Returns (\@pairs, $remainder):
6438 # - @pairs is a list of [VAR, RAW_VALUE] (value still un-expanded)
6439 # - $remainder is the rest of the line (the command to run), '' if none
6440 # Returns () when the line does not begin with an assignment.
6441 # ----------------------------------------------------------------
6442 sub _sh_assign_prefix {
6443 1007 ▶ my ($line) = @_;
Subroutine _sh_assign_prefix: called CC=50 SCAR=39.8
6444 1007 my @chars = split //, $line;
6445 1007 my $n = scalar @chars;
6446 1007 my $i = 0;
6447 1007 my @pairs = ();
6448  
6449 1007 while (1) {
6450 # Skip leading spaces between successive assignments.
6451 1007 ▶ $i++ while $i < $n && ($chars[$i] eq ' ' || $chars[$i] eq "\t");
Branch: 1 branch
BranchTrueFalseTotal
$i < $n and $chars[$i] eq " " || $chars[$i] eq "\t" and do { ++$i } 0 1007 1007
Condition33%2/6
$i < $n and $chars[$i] eq " " || $chars[$i] eq "\t"
!ll&&!rl&&r
010070
$chars[$i] eq " " or $chars[$i] eq "\t"
l!l&&r!l&&!r
001007
MC/DC0%0/3
$i < $n and $chars[$i] eq " " || $chars[$i] eq "\t"
$i < $n $chars[$i] eq " " $chars[$i] eq "\t"
Truth table25%1/4
A and B or C
A= $i < $nB= $chars[$i] eq " "C= $chars[$i] eq "\t"
ABCresult
0XX0
1000
11X1
1011
6452 1007 ▶ last if $i >= $n;
Branch: 1 branch
BranchTrueFalseTotal
if $i >= $n 0 1007 1007
6453  
6454 # Match a variable name followed by '='.
6455 1007 my $j = $i;
6456 1007 my $name = '';
6457 1007 ▶ if ($chars[$j] =~ /[A-Za-z_]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chars[$j] =~ /[A-Za-z_]/) 974 33 1007
6458 974 $name .= $chars[$j]; $j++;
6459 974 ▶ while ($j < $n && $chars[$j] =~ /[A-Za-z0-9_]/) { $name .= $chars[$j]; $j++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $chars[$j] =~ /[A-Za-z0-9_]/) 2788 974 3762
Condition100%3/3
$j < $n and $chars[$j] =~ /[A-Za-z0-9_]/
!ll&&!rl&&r
499252788
MC/DC100%2/2
$j < $n and $chars[$j] =~ /[A-Za-z0-9_]/
$j < $n $chars[$j] =~ /[A-Za-z0-9_]/
Truth table100%3/3
A and B
A= $j < $nB= $chars[$j] =~ /[A-Za-z0-9_]/
ABresult
0X0
100
111
6460 }
6461 1007 ▶ last unless length($name) && $j < $n && $chars[$j] eq '=';
Branch: 1 branch
BranchTrueFalseTotal
unless length $name and $j < $n and $chars[$j] eq "=" 791 183 974
Condition100%6/6
length $name and $j < $n and $chars[$j] eq "="
!ll&&!rl&&r
82742183
length $name and $j < $n
!ll&&!rl&&r
3349925
MC/DC100%3/3
length $name and $j < $n and $chars[$j] eq "="
length $name $j < $n $chars[$j] eq "="
Truth table100%4/4
A and B and C
A= length $nameB= $j < $nC= $chars[$j] eq "="
ABCresult
0XX0
10X0
1100
1111
6462 183 $j++; # consume '='
6463  
6464 # Read the value with quote / $() / backtick awareness.
6465 183 my $val = '';
6466 183 my $in_sq = 0;
6467 183 my $in_dq = 0;
6468 183 my $depth = 0; # $( ) nesting
6469 183 my $in_bt = 0; # backticks
6470 183 ▶ while ($j < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 1285 181 1466
6471 1285 my $c = $chars[$j];
6472 1285 ▶ if ($in_sq) { $val .= $c; $in_sq = 0 if $c eq "'"; $j++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($in_sq) 17 1268 1285
if $c eq "'" 5 12 17
6473 # Backslash escape (outside single quotes): the backslash and
6474 # the char it protects are both kept verbatim -- so "\ " does
6475 # not end the value word. _arr_dequote() resolves them later.
6476 1268 ▶ if ($c eq "\\" && !$in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\\" and not $in_sq) 7 1261 1268
Condition66%2/3
$c eq "\\" and not $in_sq
!ll&&!rl&&r
126107
MC/DC50%1/2
$c eq "\\" and not $in_sq
$c eq "\\" not $in_sq
Truth table66%2/3
A and not(B)
A= $c eq "\\"B= not $in_sq
ABresult
0X0
100
111
6477 7 $val .= $c; $j++;
6478 7 ▶ $val .= $chars[$j] if $j < $n;
Branch: 1 branch
BranchTrueFalseTotal
if $j < $n 7 0 7
6479 7 $j++; next;
6480 }
6481 1261 ▶ if ($c eq "'" && !$in_dq && !$in_bt) { $in_sq = 1; $val .= $c; $j++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "'" and not $in_dq and not $in_bt) 5 1256 1261
Condition66%4/6
$c eq "'" and not $in_dq and not $in_bt
!ll&&!rl&&r
125605
$c eq "'" and not $in_dq
!ll&&!rl&&r
125605
MC/DC33%1/3
$c eq "'" and not $in_dq and not $in_bt
$c eq "'" not $in_dq not $in_bt
Truth table50%2/4
A and not(B) and not(C)
A= $c eq "'"B= not $in_dqC= not $in_bt
ABCresult
0XX0
10X0
1100
1111
6482 1256 ▶ if ($c eq '"' && !$in_bt) { $in_dq = !$in_dq; $val .= $c; $j++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\"" and not $in_bt) 88 1168 1256
Condition66%2/3
$c eq "\"" and not $in_bt
!ll&&!rl&&r
1168088
MC/DC50%1/2
$c eq "\"" and not $in_bt
$c eq "\"" not $in_bt
Truth table66%2/3
A and not(B)
A= $c eq "\""B= not $in_bt
ABresult
0X0
100
111
6483 1168 ▶ if (!$in_dq && $c eq '`') { $in_bt = !$in_bt; $val .= $c; $j++; next }
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_dq and $c eq "`") 2 1166 1168
Condition100%3/3
not $in_dq and $c eq "`"
!ll&&!rl&&r
2349322
MC/DC100%2/2
not $in_dq and $c eq "`"
not $in_dq $c eq "`"
Truth table100%3/3
not(A) and B
A= not $in_dqB= $c eq "`"
ABresult
0X0
100
111
6484 1166 ▶ if (!$in_dq && !$in_bt && $c eq '$' && $j + 1 < $n && $chars[$j+1] eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_dq || $in_bt and $c eq "\$" and $j + 1 < $n and $chars[$j + 1] eq "(") 64 1102 1166
Condition91%11/12
not $in_dq || $in_bt and $c eq "\$" and $j + 1 < $n and $chars[$j + 1] eq "("
!ll&&!rl&&r
1101164
not $in_dq || $in_bt and $c eq "\$" and $j + 1 < $n
!ll&&!rl&&r
1101065
not $in_dq || $in_bt and $c eq "\$"
!ll&&!rl&&r
24485765
$in_dq || $in_bt
l!l&&r!l&&!r
23410922
MC/DC80%4/5
not $in_dq || $in_bt and $c eq "\$" and $j + 1 < $n and $chars[$j + 1] eq "("
$in_dq $in_bt $c eq "\$" $j + 1 < $n $chars[$j + 1] eq "("
Truth table83%5/6
not(A || B) and C and D and E
A= $in_dqB= $in_btC= $c eq "\$"D= $j + 1 < $nE= $chars[$j + 1] eq "("
ABCDEresult
1XXXX0
01XXX0
000XX0
0010X0
001100
001111
6485 64 $depth++; $val .= '$('; $j += 2; next;
6486 }
6487 1102 ▶ if (!$in_dq && !$in_bt && $depth > 0 && $c eq ')') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_dq || $in_bt and $depth > 0 and $c eq ")") 64 1038 1102
Condition100%9/9
not $in_dq || $in_bt and $depth > 0 and $c eq ")"
!ll&&!rl&&r
52751164
not $in_dq || $in_bt and $depth > 0
!ll&&!rl&&r
244283575
$in_dq || $in_bt
l!l&&r!l&&!r
23410858
MC/DC100%4/4
not $in_dq || $in_bt and $depth > 0 and $c eq ")"
$in_dq $in_bt $depth > 0 $c eq ")"
Truth table100%5/5
not(A || B) and C and D
A= $in_dqB= $in_btC= $depth > 0D= $c eq ")"
ABCDresult
1XXX0
01XX0
000X0
00100
00111
6488 64 $depth--; $val .= ')'; $j++; next;
6489 }
6490 1038 ▶ if (!$in_sq && !$in_dq && !$in_bt && $depth == 0
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_sq || $in_dq || $in_bt and $depth == 0 and $c eq " " || $c eq "\t") 2 1036 1038
Condition80%12/15
not $in_sq || $in_dq || $in_bt and $depth == 0 and $c eq " " || $c eq "\t"
!ll&&!rl&&r
7552830
not $in_sq || $in_dq || $in_bt and $depth == 0
!ll&&!rl&&r
244511283
$in_sq || $in_dq || $in_bt
l!l&&r!l&&!r
23410794
$in_sq or $in_dq
l!l&&r!l&&!r
0234804
$c eq " " or $c eq "\t"
l!l&&r!l&&!r
20281
MC/DC0%0/6
not $in_sq || $in_dq || $in_bt and $depth == 0 and $c eq " " || $c eq "\t"
$in_sq $in_dq $in_bt $depth == 0 $c eq " " $c eq "\t"
Truth table57%4/7
not(A or B || C) and D and E or F
A= $in_sqB= $in_dqC= $in_btD= $depth == 0E= $c eq " "F= $c eq "\t"
ABCDEFresult
1XXXXX0
01XXXX0
001XXX0
0000XX0
0001000
00011X1
0001011
6491 && ($c eq ' ' || $c eq "\t")) {
6492 2 last; # end of this value word
6493 }
6494 1036 $val .= $c; $j++;
6495 }
6496  
6497 183 push @pairs, [$name, $val];
6498 183 $i = $j;
6499  
6500 # Peek: is there a following non-space token?
6501 183 my $k = $i;
6502 183 ▶ $k++ while $k < $n && ($chars[$k] eq ' ' || $chars[$k] eq "\t");
Branch: 1 branch
BranchTrueFalseTotal
$k < $n and $chars[$k] eq " " || $chars[$k] eq "\t" and do { ++$k } 2 183 185
Condition66%4/6
$k < $n and $chars[$k] eq " " || $chars[$k] eq "\t"
!ll&&!rl&&r
18140
$chars[$k] eq " " or $chars[$k] eq "\t"
l!l&&r!l&&!r
202
MC/DC0%0/3
$k < $n and $chars[$k] eq " " || $chars[$k] eq "\t"
$k < $n $chars[$k] eq " " $chars[$k] eq "\t"
Truth table50%2/4
A and B or C
A= $k < $nB= $chars[$k] eq " "C= $chars[$k] eq "\t"
ABCresult
0XX0
1000
11X1
1011
6503 183 ▶ last if $k >= $n; # nothing follows: trailing pure assignment(s)
Branch: 1 branch
BranchTrueFalseTotal
if $k >= $n 181 2 183
6504  
6505 # Is the next token another assignment? If so, loop; otherwise the
6506 # remainder is the command.
6507 2 my $m = $k;
6508 2 my $nm2 = '';
6509 2 ▶ if ($chars[$m] =~ /[A-Za-z_]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chars[$m] =~ /[A-Za-z_]/) 2 0 2
6510 2 $nm2 .= $chars[$m]; $m++;
6511 2 ▶ while ($m < $n && $chars[$m] =~ /[A-Za-z0-9_]/) { $nm2 .= $chars[$m]; $m++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($m < $n and $chars[$m] =~ /[A-Za-z0-9_]/) 6 2 8
Condition100%3/3
$m < $n and $chars[$m] =~ /[A-Za-z0-9_]/
!ll&&!rl&&r
116
MC/DC100%2/2
$m < $n and $chars[$m] =~ /[A-Za-z0-9_]/
$m < $n $chars[$m] =~ /[A-Za-z0-9_]/
Truth table100%3/3
A and B
A= $m < $nB= $chars[$m] =~ /[A-Za-z0-9_]/
ABresult
0X0
100
111
6512 }
6513 2 ▶ if (length($nm2) && $m < $n && $chars[$m] eq '=') {
Branch: 1 branch
BranchTrueFalseTotal
if (length $nm2 and $m < $n and $chars[$m] eq "=") 0 2 2
Condition66%4/6
length $nm2 and $m < $n and $chars[$m] eq "="
!ll&&!rl&&r
110
length $nm2 and $m < $n
!ll&&!rl&&r
011
MC/DC0%0/3
length $nm2 and $m < $n and $chars[$m] eq "="
length $nm2 $m < $n $chars[$m] eq "="
Truth table50%2/4
A and B and C
A= length $nm2B= $m < $nC= $chars[$m] eq "="
ABCresult
0XX0
10X0
1100
1111
6514 0 $i = $k; # next assignment; continue the loop
6515 0 next;
6516 }
6517  
6518 # Remainder is a command.
6519 2 my $remainder = join('', @chars[$k .. $n-1]);
6520 2 return (\@pairs, $remainder);
6521 }
6522  
6523 # No command remainder: either not an assignment at all, or pure
6524 # assignment(s) which the normal post-expansion path handles.
6525 1005 ▶ return () unless @pairs;
Branch: 1 branch
BranchTrueFalseTotal
unless @pairs 824 181 1005
6526 181 return (\@pairs, '');
6527 }
6528  
6529 # ----------------------------------------------------------------
6530 # Here-document support (Perl 5.005_03 compatible)
6531 # ----------------------------------------------------------------
6532 # _hd_detect: scan a command line for an *unquoted* << operator.
6533 # Returns () if none found, otherwise:
6534 # ($cmd_part, $dash, $delim, $quoted)
6535 # where $cmd_part is the command with the "<< DELIM" token removed
6536 # (text after the delimiter, e.g. trailing redirections, is preserved),
6537 # $dash is 1 for <<- (strip leading tabs), $delim is the delimiter word,
6538 # and $quoted is 1 when the delimiter was quoted (suppresses expansion).
6539 sub _hd_detect {
6540 1656 ▶ my ($line) = @_;
Subroutine _hd_detect: called CC=43 SCAR=38.2
6541 1656 my @chars = split //, $line;
6542 1656 my $n = scalar @chars;
6543 1656 my $in_sq = 0;
6544 1656 my $in_dq = 0;
6545 1656 my $arith = 0; # inside $(( ... )): << there is a shift, not a heredoc
6546 1656 my $i = 0;
6547  
6548 1656 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 27651 1630 29281
6549 27651 my $ch = $chars[$i];
6550  
6551 27651 ▶ if ($in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq) 982 26669 27651
6552 982 ▶ $in_sq = 0 if $ch eq "'";
Branch: 1 branch
BranchTrueFalseTotal
if $ch eq "'" 120 862 982
6553 982 $i++; next;
6554 }
6555 26669 ▶ if ($ch eq "'" && !$in_dq) { $in_sq = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $in_dq) 120 26549 26669
Condition100%3/3
$ch eq "'" and not $in_dq
!ll&&!rl&&r
265472120
MC/DC100%2/2
$ch eq "'" and not $in_dq
$ch eq "'" not $in_dq
Truth table100%3/3
A and not(B)
A= $ch eq "'"B= not $in_dq
ABresult
0X0
100
111
6556 26549 ▶ if ($ch eq '"' && !$in_sq) { $in_dq = !$in_dq; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" and not $in_sq) 840 25709 26549
Condition66%2/3
$ch eq "\"" and not $in_sq
!ll&&!rl&&r
257090840
MC/DC50%1/2
$ch eq "\"" and not $in_sq
$ch eq "\"" not $in_sq
Truth table66%2/3
A and not(B)
A= $ch eq "\""B= not $in_sq
ABresult
0X0
100
111
6557 25709 ▶ if ($ch eq '\\') { $i += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 37 25672 25709
6558  
6559 # Arithmetic expansion $(( ... )): skip its body entirely
6560 25672 ▶ if ($ch eq '$' && $i+2 < $n && $chars[$i+1] eq '(' && $chars[$i+2] eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\$" and $i + 2 < $n and $chars[$i + 1] eq "(" and $chars[$i + 2] eq "(") 139 25533 25672
Condition100%9/9
$ch eq "\$" and $i + 2 < $n and $chars[$i + 1] eq "(" and $chars[$i + 2] eq "("
!ll&&!rl&&r
2548845139
$ch eq "\$" and $i + 2 < $n and $chars[$i + 1] eq "("
!ll&&!rl&&r
25026462184
$ch eq "\$" and $i + 2 < $n
!ll&&!rl&&r
24859167646
MC/DC100%4/4
$ch eq "\$" and $i + 2 < $n and $chars[$i + 1] eq "(" and $chars[$i + 2] eq "("
$ch eq "\$" $i + 2 < $n $chars[$i + 1] eq "(" $chars[$i + 2] eq "("
Truth table100%5/5
A and B and C and D
A= $ch eq "\$"B= $i + 2 < $nC= $chars[$i + 1] eq "("D= $chars[$i + 2] eq "("
ABCDresult
0XXX0
10XX0
110X0
11100
11111
6561 139 $arith++; $i += 3; next;
6562 }
6563 25533 ▶ if ($arith && $ch eq ')' && $i+1 < $n && $chars[$i+1] eq ')') {
Branch: 1 branch
BranchTrueFalseTotal
if ($arith and $ch eq ")" and $i + 1 < $n and $chars[$i + 1] eq ")") 139 25394 25533
Condition77%7/9
$arith and $ch eq ")" and $i + 1 < $n and $chars[$i + 1] eq ")"
!ll&&!rl&&r
253940139
$arith and $ch eq ")" and $i + 1 < $n
!ll&&!rl&&r
253940139
$arith and $ch eq ")"
!ll&&!rl&&r
24573821139
MC/DC50%2/4
$arith and $ch eq ")" and $i + 1 < $n and $chars[$i + 1] eq ")"
$arith $ch eq ")" $i + 1 < $n $chars[$i + 1] eq ")"
Truth table60%3/5
A and B and C and D
A= $arithB= $ch eq ")"C= $i + 1 < $nD= $chars[$i + 1] eq ")"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
6564 139 $arith--; $i += 2; next;
6565 }
6566 25394 ▶ if ($arith) { $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($arith) 821 24573 25394
6567  
6568 # Unquoted << starts a here-document; unquoted <<< is a
6569 # here-string (v0.07: handled later, after expansion, by
6570 # _sh_strip_herestring -- skip all three characters here so the
6571 # scan does not re-match starting at the second '<').
6572 24573 ▶ if (!$in_dq && $ch eq '<' && $i+1 < $n && $chars[$i+1] eq '<'
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_dq and $ch eq "<" and $i + 1 < $n and $chars[$i + 1] eq "<" and $i + 2 < $n and $chars[$i + 2] eq "<") 4 24569 24573
Condition86%13/15
not $in_dq and $ch eq "<" and $i + 1 < $n and $chars[$i + 1] eq "<" and $i + 2 < $n and $chars[$i + 2] eq "<"
!ll&&!rl&&r
24543264
not $in_dq and $ch eq "<" and $i + 1 < $n and $chars[$i + 1] eq "<" and $i + 2 < $n
!ll&&!rl&&r
24543030
not $in_dq and $ch eq "<" and $i + 1 < $n and $chars[$i + 1] eq "<"
!ll&&!rl&&r
245222130
not $in_dq and $ch eq "<" and $i + 1 < $n
!ll&&!rl&&r
24522051
not $in_dq and $ch eq "<"
!ll&&!rl&&r
36192090351
MC/DC66%4/6
not $in_dq and $ch eq "<" and $i + 1 < $n and $chars[$i + 1] eq "<" and $i + 2 < $n and $chars[$i + 2] eq "<"
not $in_dq $ch eq "<" $i + 1 < $n $chars[$i + 1] eq "<" $i + 2 < $n $chars[$i + 2] eq "<"
Truth table71%5/7
not(A) and B and C and D and E and F
A= not $in_dqB= $ch eq "<"C= $i + 1 < $nD= $chars[$i + 1] eq "<"E= $i + 2 < $nF= $chars[$i + 2] eq "<"
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
6573 && $i+2 < $n && $chars[$i+2] eq '<') {
6574 4 $i += 3; next;
6575 }
6576 24569 ▶ if (!$in_dq && $ch eq '<' && $i+1 < $n && $chars[$i+1] eq '<') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_dq and $ch eq "<" and $i + 1 < $n and $chars[$i + 1] eq "<") 26 24543 24569
Condition88%8/9
not $in_dq and $ch eq "<" and $i + 1 < $n and $chars[$i + 1] eq "<"
!ll&&!rl&&r
245222126
not $in_dq and $ch eq "<" and $i + 1 < $n
!ll&&!rl&&r
24522047
not $in_dq and $ch eq "<"
!ll&&!rl&&r
36192090347
MC/DC75%3/4
not $in_dq and $ch eq "<" and $i + 1 < $n and $chars[$i + 1] eq "<"
not $in_dq $ch eq "<" $i + 1 < $n $chars[$i + 1] eq "<"
Truth table80%4/5
not(A) and B and C and D
A= not $in_dqB= $ch eq "<"C= $i + 1 < $nD= $chars[$i + 1] eq "<"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
6577 26 my $cmd_part = join('', @chars[0 .. $i-1]);
6578 26 my $j = $i + 2;
6579 26 my $dash = 0;
6580 26 ▶ if ($j < $n && $chars[$j] eq '-') { $dash = 1; $j++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $chars[$j] eq "-") 2 24 26
Condition66%2/3
$j < $n and $chars[$j] eq "-"
!ll&&!rl&&r
0242
MC/DC50%1/2
$j < $n and $chars[$j] eq "-"
$j < $n $chars[$j] eq "-"
Truth table66%2/3
A and B
A= $j < $nB= $chars[$j] eq "-"
ABresult
0X0
100
111
6581 26 ▶ $j++ while $j < $n && ($chars[$j] eq ' ' || $chars[$j] eq "\t");
Branch: 1 branch
BranchTrueFalseTotal
$j < $n and $chars[$j] eq " " || $chars[$j] eq "\t" and do { ++$j } 0 26 26
Condition33%2/6
$j < $n and $chars[$j] eq " " || $chars[$j] eq "\t"
!ll&&!rl&&r
0260
$chars[$j] eq " " or $chars[$j] eq "\t"
l!l&&r!l&&!r
0026
MC/DC0%0/3
$j < $n and $chars[$j] eq " " || $chars[$j] eq "\t"
$j < $n $chars[$j] eq " " $chars[$j] eq "\t"
Truth table25%1/4
A and B or C
A= $j < $nB= $chars[$j] eq " "C= $chars[$j] eq "\t"
ABCresult
0XX0
1000
11X1
1011
6582  
6583 26 my $quoted = 0;
6584 26 my $q = '';
6585 26 ▶ if ($j < $n && ($chars[$j] eq "'" || $chars[$j] eq '"')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $chars[$j] eq "'" || $chars[$j] eq "\"") 2 24 26
Condition50%3/6
$j < $n and $chars[$j] eq "'" || $chars[$j] eq "\""
!ll&&!rl&&r
0260
$chars[$j] eq "'" or $chars[$j] eq "\""
l!l&&r!l&&!r
2024
MC/DC0%0/3
$j < $n and $chars[$j] eq "'" || $chars[$j] eq "\""
$j < $n $chars[$j] eq "'" $chars[$j] eq "\""
Truth table25%1/4
A and B or C
A= $j < $nB= $chars[$j] eq "'"C= $chars[$j] eq "\""
ABCresult
0XX0
1000
11X1
1011
6586 2 $quoted = 1; $q = $chars[$j]; $j++;
6587 }
6588 26 my $delim = '';
6589 26 ▶ if ($quoted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quoted) { } 2 24 26
6590 2 ▶ while ($j < $n && $chars[$j] ne $q) { $delim .= $chars[$j]; $j++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $chars[$j] ne $q) 6 2 8
Condition66%2/3
$j < $n and $chars[$j] ne $q
!ll&&!rl&&r
026
MC/DC50%1/2
$j < $n and $chars[$j] ne $q
$j < $n $chars[$j] ne $q
Truth table66%2/3
A and B
A= $j < $nB= $chars[$j] ne $q
ABresult
0X0
100
111
6591 2 ▶ $j++ if $j < $n; # skip closing quote
Branch: 1 branch
BranchTrueFalseTotal
if $j < $n 2 0 2
6592 }
6593 else {
6594 24 ▶ while ($j < $n && $chars[$j] =~ /\w/) { $delim .= $chars[$j]; $j++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $chars[$j] =~ /\w/) 72 24 96
Condition66%2/3
$j < $n and $chars[$j] =~ /\w/
!ll&&!rl&&r
24072
MC/DC50%1/2
$j < $n and $chars[$j] =~ /\w/
$j < $n $chars[$j] =~ /\w/
Truth table66%2/3
A and B
A= $j < $nB= $chars[$j] =~ /\w/
ABresult
0X0
100
111
6595 }
6596 26 ▶ return () if $delim eq ''; # malformed; treat as ordinary line
Branch: 1 branch
BranchTrueFalseTotal
if $delim eq "" 0 26 26
6597  
6598 26 my $rest = join('', @chars[$j .. $n-1]);
6599 26 $cmd_part .= $rest; # preserve trailing tokens
6600 26 return ($cmd_part, $dash, $delim, $quoted);
6601 }
6602  
6603 24543 $i++;
6604 }
6605 1630 return ();
6606 }
6607  
6608 # _hd_tempfile: write $body to a uniquely-named temp file using
6609 # sysopen(... O_CREAT|O_EXCL ...) to avoid symlink races.
6610 # Returns the path, or undef on failure.
6611 sub _hd_tempfile {
6612 14 ▶ my ($body) = @_;
Subroutine _hd_tempfile: called CC=11 SCAR=28.5
6613  
6614 14 ▶ my $dir = $ENV{'TMPDIR'} || $ENV{'TEMP'} || $ENV{'TMP'} || '';
Condition37%3/8
$ENV{'TMPDIR'} || $ENV{'TEMP'} || $ENV{'TMP'} || ''
l!l
014
$ENV{"TMPDIR"} or $ENV{"TEMP"} or $ENV{"TMP"}
l!l&&r!l&&!r
0014
$ENV{"TMPDIR"} or $ENV{"TEMP"}
l!l&&r!l&&!r
0014
MC/DC0%0/3
$ENV{'TMPDIR'} || $ENV{'TEMP'} || $ENV{'TMP'} || ''
$ENV{"TMPDIR"} $ENV{"TEMP"} $ENV{"TMP"}
Truth table25%1/4
A or B or C
A= $ENV{"TMPDIR"}B= $ENV{"TEMP"}C= $ENV{"TMP"}
ABCresult
1XX1
01X1
0011
0000
6615 14 ▶ $dir = '/tmp' if $dir eq '' && -d '/tmp';
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq "" and -d "/tmp" 14 0 14
Condition33%1/3
$dir eq "" and -d "/tmp"
!ll&&!rl&&r
0014
MC/DC0%0/2
$dir eq "" and -d "/tmp"
$dir eq "" -d "/tmp"
Truth table33%1/3
A and B
A= $dir eq ""B= -d "/tmp"
ABresult
0X0
100
111
6616 14 ▶ $dir = '.' if $dir eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq "" 0 14 14
6617 14 $dir =~ s{[\\/]+\z}{};
6618 14 ▶ $dir = '.' if !(-d $dir && -w $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir and -w $dir 0 14 14
Condition33%1/3
-d $dir and -w $dir
!ll&&!rl&&r
0014
MC/DC0%0/2
-d $dir and -w $dir
-d $dir -w $dir
Truth table33%1/3
A and B
A= -d $dirB= -w $dir
ABresult
0X0
100
111
6619  
6620 14 my $attempt = 0;
6621 14 ▶ while ($attempt < 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attempt < 1000) 14 0 14
6622 14 $_HD_SEQ++;
6623 14 $attempt++;
6624 14 my $path = $dir . '/' . 'batsh_hd_' . $$ . '_' . $_HD_SEQ;
6625 14 ▶ if (sysopen(_HD_TMP, $path, O_WRONLY | O_CREAT | O_EXCL, 0600)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysopen _HD_TMP, $path, 193, 384) 14 0 14
6626 14 binmode(_HD_TMP);
6627 14 print _HD_TMP $body;
6628 14 close(_HD_TMP);
6629 14 push @_HD_TMPFILES, $path;
6630 14 return $path;
6631 }
6632 # EEXIST or transient error: retry with next sequence number
6633 }
6634 0 warn "sh: cannot create here-document temp file in $dir: $!\n";
6635 0 return undef;
6636 }
6637  
6638 # _hd_run: materialise the here-document body and run the command with
6639 # its STDIN connected to the body, reusing the existing redirect path.
6640 sub _hd_run {
6641 12 ▶ my ($class, $cmd_part, $body_ref, $quoted, $opts_ref) = @_;
Subroutine _hd_run: called CC=5 SCAR=16.2
6642  
6643 12 my @body = @{$body_ref};
6644 12 ▶ if (!$quoted) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($quoted) 11 1 12
6645 11 for my $b (@body) { $b = _unprotect_lit(_expand($class, $b)) }
6646 }
6647 12 my $text = '';
6648 12 for my $b (@body) { $text .= BATsh::MB::dec($b) . "\n" }
6649  
6650 12 my $tmp = _hd_tempfile($text);
6651 12 ▶ if (!defined $tmp) { $LAST_STATUS = 2; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tmp) 0 12 12
6652  
6653 12 my @redir = ( [0, 0, $tmp] ); # fd=0 (stdin), append=0, source=tmp
6654 12 my $rc = _sh_exec_with_redirs($class, $cmd_part, \@redir, $opts_ref);
6655  
6656 12 unlink $tmp;
6657 12 @_HD_TMPFILES = grep { $_ ne $tmp } @_HD_TMPFILES;
6658 12 return $rc;
6659 }
6660  
6661 # Failsafe: remove any here-document temp files left behind on abnormal exit.
6662 36 ▶END { for my $f (@_HD_TMPFILES) { unlink $f if defined $f } }
Subroutine END: called CC=3 SCAR=19.2
Branch: 1 branch
BranchTrueFalseTotal
if defined $f 0 0 0
6663 36 ▶END { for my $f (@_SUBST_TMPFILES) { unlink $f if defined $f } }
Subroutine END: called CC=3 SCAR=11.4
Branch: 1 branch
BranchTrueFalseTotal
if defined $f 1 0 1
6664 36 ▶END { for my $f (@_SHP_TMPFILES) { unlink $f if defined $f } }
Subroutine END: called CC=3 SCAR=11.4
Branch: 1 branch
BranchTrueFalseTotal
if defined $f 1 0 1
6665 36 ▶END { for my $f (@_PROCSUB_TMPFILES) { unlink $f if defined $f } }
Subroutine END: called CC=3 SCAR=19.2
Branch: 1 branch
BranchTrueFalseTotal
if defined $f 0 0 0
6666  
6667 # Failsafe: remove any background-job pidfiles left behind on abnormal exit.
6668 36 ▶END { for my $f (@_BG_TMPFILES) { unlink $f if defined $f } }
Subroutine END: called CC=3 SCAR=19.2
Branch: 1 branch
BranchTrueFalseTotal
if defined $f 0 0 0
6669  
6670  
6671 # ----------------------------------------------------------------
6672 # Array / associative-array support (v0.06)
6673 # ----------------------------------------------------------------
6674 # Array names are case-insensitive, matching the scalar store: the key
6675 # stored in %_SH_ARRAY is always the uppercased name.
6676 438 ▶sub _arr_name { return uc($_[0]) }
Subroutine _arr_name: called CC=1 SCAR=0.0
6677  
6678 sub _arr_exists {
6679 302 ▶ my ($name) = @_;
Subroutine _arr_exists: called CC=2 SCAR=6.9
6680 302 ▶ return exists $_SH_ARRAY{ _arr_name($name) } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $BATsh::SH::_SH_ARRAY{_arr_name($name)} ? : 4 298 302
6681 }
6682  
6683 sub _arr_is_assoc {
6684 26 ▶ my ($name) = @_;
Subroutine _arr_is_assoc: called CC=3 SCAR=11.0
6685 26 my $k = _arr_name($name);
6686 26 ▶ return (exists $_SH_ARRAY_TYPE{$k} && $_SH_ARRAY_TYPE{$k} eq 'assoc') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $BATsh::SH::_SH_ARRAY_TYPE{$k} && $BATsh::SH::_SH_ARRAY_TYPE{$k} eq 'assoc' ? : 3 23 26
Condition100%3/3
exists $BATsh::SH::_SH_ARRAY_TYPE{$k} and $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
!ll&&!rl&&r
2033
MC/DC100%2/2
exists $BATsh::SH::_SH_ARRAY_TYPE{$k} and $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
exists $BATsh::SH::_SH_ARRAY_TYPE{$k} $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
Truth table100%3/3
A and B
A= exists $BATsh::SH::_SH_ARRAY_TYPE{$k}B= $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
ABresult
0X0
100
111
6687 }
6688  
6689 # Evaluate an indexed-array subscript as an integer. Plain integers are
6690 # taken verbatim; anything else is run through the arithmetic evaluator so
6691 # that subscripts such as "1+1" or a bare variable name work like bash.
6692 sub _arr_index {
6693 20 ▶ my ($s) = @_;
Subroutine _arr_index: called CC=5 SCAR=16.4
6694 20 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 20 20
6695 20 $s =~ s/\A\s+//; $s =~ s/\s+\z//;
6696 20 ▶ return 0 if $s eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "" 0 20 20
6697 20 ▶ return int($s) if $s =~ /\A-?\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /\A-?\d+\z/ 19 1 20
6698 1 my $v = _eval_arith($s);
6699 1 ▶ return ($v =~ /\A-?\d+\z/) ? int($v) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$v =~ /\A-?\d+\z/ ? : 1 0 1
6700 }
6701  
6702 # Resolve $VAR / ${VAR} inside a subscript (no command substitution, no
6703 # arithmetic -- those are applied later for indexed subscripts).
6704 sub _arr_expand_sub {
6705 30 ▶ my ($class, $s) = @_;
Subroutine _arr_expand_sub: called CC=4 SCAR=16.3
6706 30 ▶ return '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 30 30
6707 30 $s =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/
6708 0 ▶ do { my $v = BATsh::Env->get($1); defined $v ? $v : '' }
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 0 0 0
6709 /ge;
6710 30 $s =~ s/\$([A-Za-z_][A-Za-z0-9_]*)/
6711 3 ▶ do { my $v = BATsh::Env->get($1); defined $v ? $v : '' }
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 3 0 3
6712 /ge;
6713 30 return $s;
6714 }
6715  
6716 # Element keys in display order: ascending numeric (indexed) or sorted
6717 # string (assoc).
6718 sub _arr_ordered_keys {
6719 17 ▶ my ($name) = @_;
Subroutine _arr_ordered_keys: called CC=4 SCAR=13.9
6720 17 my $k = _arr_name($name);
6721 17 ▶ return () unless exists $_SH_ARRAY{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BATsh::SH::_SH_ARRAY{$k} 0 17 17
6722 17 my $h = $_SH_ARRAY{$k};
6723 17 ▶ if ((defined $_SH_ARRAY_TYPE{$k} && $_SH_ARRAY_TYPE{$k} eq 'assoc')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $BATsh::SH::_SH_ARRAY_TYPE{$k} and $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc") 2 15 17
Condition66%2/3
defined $BATsh::SH::_SH_ARRAY_TYPE{$k} and $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
!ll&&!rl&&r
0152
MC/DC50%1/2
defined $BATsh::SH::_SH_ARRAY_TYPE{$k} and $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
defined $BATsh::SH::_SH_ARRAY_TYPE{$k} $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
Truth table66%2/3
A and B
A= defined $BATsh::SH::_SH_ARRAY_TYPE{$k}B= $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
ABresult
0X0
100
111
6724 2 return sort keys %{$h};
6725 }
6726 15 return sort { $a <=> $b } keys %{$h};
6727 }
6728  
6729 sub _arr_values {
6730 10 ▶ my ($name) = @_;
Subroutine _arr_values: called CC=2 SCAR=7.0
6731 10 my $k = _arr_name($name);
6732 10 ▶ return () unless exists $_SH_ARRAY{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BATsh::SH::_SH_ARRAY{$k} 0 10 10
6733 10 my $h = $_SH_ARRAY{$k};
6734 10 return map { $h->{$_} } _arr_ordered_keys($name);
6735 }
6736  
6737 sub _arr_count {
6738 10 ▶ my ($name) = @_;
Subroutine _arr_count: called CC=2 SCAR=6.9
6739 10 my $k = _arr_name($name);
6740 10 ▶ return 0 unless exists $_SH_ARRAY{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BATsh::SH::_SH_ARRAY{$k} 1 9 10
6741 9 return scalar keys %{$_SH_ARRAY{$k}};
6742 }
6743  
6744 # Fetch a single element. $sub is already $VAR-expanded by the caller.
6745 # Returns undef when the element is unset.
6746 sub _arr_get_element {
6747 23 ▶ my ($name, $sub) = @_;
Subroutine _arr_get_element: called CC=9 SCAR=22.7
6748 23 my $k = _arr_name($name);
6749 23 ▶ return undef unless exists $_SH_ARRAY{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BATsh::SH::_SH_ARRAY{$k} 0 23 23
6750 23 my $h = $_SH_ARRAY{$k};
6751 23 ▶ if ((defined $_SH_ARRAY_TYPE{$k} && $_SH_ARRAY_TYPE{$k} eq 'assoc')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $BATsh::SH::_SH_ARRAY_TYPE{$k} and $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc") 9 14 23
Condition66%2/3
defined $BATsh::SH::_SH_ARRAY_TYPE{$k} and $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
!ll&&!rl&&r
0149
MC/DC50%1/2
defined $BATsh::SH::_SH_ARRAY_TYPE{$k} and $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
defined $BATsh::SH::_SH_ARRAY_TYPE{$k} $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
Truth table66%2/3
A and B
A= defined $BATsh::SH::_SH_ARRAY_TYPE{$k}B= $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
ABresult
0X0
100
111
6752 9 ▶ return exists $h->{$sub} ? $h->{$sub} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $h->{$sub} ? : 9 0 9
6753 }
6754 14 my $idx = _arr_index($sub);
6755 14 ▶ if ($idx < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < 0) 1 13 14
6756 # Negative subscript: count back over the ordered set of set indices.
6757 1 my @keys = _arr_ordered_keys($name);
6758 1 ▶ return undef unless @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys 0 1 1
6759 1 my $kk = $keys[$idx];
6760 1 ▶ return defined $kk ? $h->{$kk} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $kk ? : 1 0 1
6761 }
6762 13 ▶ return exists $h->{$idx} ? $h->{$idx} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $h->{$idx} ? : 13 0 13
6763 }
6764  
6765 # Remove shell quoting from a value: drop unescaped quote characters while
6766 # honouring single- vs double-quote regions. Handles whole-token quotes
6767 # ("a b") and partial quotes (foo"a b"bar) alike.
6768 sub _arr_dequote {
6769 1049 ▶ my ($v) = @_;
Subroutine _arr_dequote: called CC=17 SCAR=28.7
6770 1049 ▶ return '' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 1049 1049
6771 1049 my $out = '';
6772 1049 my $in_sq = 0;
6773 1049 my $in_dq = 0;
6774 1049 my @c = split //, $v;
6775 1049 my $n = scalar @c;
6776 1049 my $i = 0;
6777 1049 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 7726 1049 8775
6778 7726 my $c = $c[$i];
6779 # Single quotes: everything literal, no escapes, until the closer.
6780 7726 ▶ if ($in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq) 341 7385 7726
6781 341 ▶ if ($c eq "'") { $in_sq = 0 } else { $out .= $c }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq q[']) { } 45 296 341
6782 341 $i++; next;
6783 }
6784 7385 ▶ if ($c eq "'" && !$in_dq) { $in_sq = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "'" and not $in_dq) 45 7340 7385
Condition100%3/3
$c eq "'" and not $in_dq
!ll&&!rl&&r
7339145
MC/DC100%2/2
$c eq "'" and not $in_dq
$c eq "'" not $in_dq
Truth table100%3/3
A and not(B)
A= $c eq "'"B= not $in_dq
ABresult
0X0
100
111
6785 # Backslash: escape processing (POSIX).
6786 7340 ▶ if ($c eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\\") 17 7323 7340
6787 17 ▶ my $nx = ($i+1 < $n) ? $c[$i+1] : '';
Branch: 1 branch
BranchTrueFalseTotal
$i + 1 < $n ? : 17 0 17
6788 17 ▶ if ($in_dq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_dq) 15 2 17
6789 # Inside "..." backslash is literal EXCEPT before $ ` " \.
6790 15 ▶ if ($nx eq '"' || $nx eq '\\' || $nx eq '`' || $nx eq '$') {
Branch: 1 branch
BranchTrueFalseTotal
if ($nx eq "\"" or $nx eq "\\" or $nx eq "`" or $nx eq "\$") 0 13 13
Condition66%6/9
$nx eq "\"" or $nx eq "\\" or $nx eq "`" or $nx eq "\$"
l!l&&r!l&&!r
2013
$nx eq "\"" or $nx eq "\\" or $nx eq "`"
l!l&&r!l&&!r
2013
$nx eq "\"" or $nx eq "\\"
l!l&&r!l&&!r
2013
MC/DC25%1/4
$nx eq "\"" or $nx eq "\\" or $nx eq "`" or $nx eq "\$"
$nx eq "\"" $nx eq "\\" $nx eq "`" $nx eq "\$"
Truth table40%2/5
A or B or C or D
A= $nx eq "\""B= $nx eq "\\"C= $nx eq "`"D= $nx eq "\$"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
6791 2 $out .= $nx; $i += 2; next;
6792 }
6793 13 $out .= '\\'; $i++; next;
6794 }
6795 # Unquoted: backslash quotes the following char (so \" is a
6796 # literal quote and does NOT open a quoted region).
6797 2 ▶ if ($nx ne '') { $out .= $nx; $i += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($nx ne "") 2 0 2
6798 0 $out .= '\\'; $i++; next;
6799 }
6800 7323 ▶ if ($c eq '"' && !$in_sq) { $in_dq = !$in_dq; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\"" and not $in_sq) 334 6989 7323
Condition66%2/3
$c eq "\"" and not $in_sq
!ll&&!rl&&r
69890334
MC/DC50%1/2
$c eq "\"" and not $in_sq
$c eq "\"" not $in_sq
Truth table66%2/3
A and not(B)
A= $c eq "\""B= not $in_sq
ABresult
0X0
100
111
6801 6989 $out .= $c; $i++;
6802 }
6803 # A backslash that came out of an expansion is literal data, never a
6804 # shell escape: restore it now that quote removal has finished.
6805 1049 return _unprotect_lit($out);
6806 }
6807  
6808 # Split a string on unquoted whitespace, KEEPING the quote characters in
6809 # each returned token (so the caller can tell whether a token was quoted).
6810 sub _arr_split_words {
6811 179 ▶ my ($s) = @_;
Subroutine _arr_split_words: called CC=30 SCAR=36.2
6812 179 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 179 179
6813 179 my @words;
6814 179 my $cur = '';
6815 179 my $have = 0;
6816 179 my $in_sq = 0;
6817 179 my $in_dq = 0;
6818 179 my $in_bt = 0; # backtick `...`
6819 179 my $pdep = 0; # $( ... and <( >( and plain ( ) depth
6820 179 my $bdep = 0; # ${ ... } depth
6821 179 my @chars = split //, $s;
6822 179 my $n = scalar @chars;
6823 179 my $i = 0;
6824 179 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 1551 179 1730
6825 1551 my $c = $chars[$i];
6826 1551 ▶ if ($in_sq) { $cur .= $c; $in_sq = 0 if $c eq "'"; $have = 1; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($in_sq) 197 1354 1551
if $c eq "'" 34 163 197
6827 1354 ▶ if ($c eq "'" && !$in_dq && !$in_bt) { $in_sq = 1; $cur .= $c; $have = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "'" and not $in_dq and not $in_bt) 34 1320 1354
Condition83%5/6
$c eq "'" and not $in_dq and not $in_bt
!ll&&!rl&&r
1320034
$c eq "'" and not $in_dq
!ll&&!rl&&r
1319134
MC/DC66%2/3
$c eq "'" and not $in_dq and not $in_bt
$c eq "'" not $in_dq not $in_bt
Truth table75%3/4
A and not(B) and not(C)
A= $c eq "'"B= not $in_dqC= not $in_bt
ABCresult
0XX0
10X0
1100
1111
6828 1320 ▶ if ($c eq '"' && !$in_bt) { $in_dq = !$in_dq; $cur .= $c; $have = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\"" and not $in_bt) 26 1294 1320
Condition66%2/3
$c eq "\"" and not $in_bt
!ll&&!rl&&r
1294026
MC/DC50%1/2
$c eq "\"" and not $in_bt
$c eq "\"" not $in_bt
Truth table66%2/3
A and not(B)
A= $c eq "\""B= not $in_bt
ABresult
0X0
100
111
6829 # Backslash escape keeps the next char attached to this word.
6830 1294 ▶ if ($c eq '\\') { $cur .= $c; $have = 1; $i++; $cur .= $chars[$i] if $i < $n; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq "\\") 7 1287 1294
if $i < $n 7 0 7
6831 1287 ▶ if ($c eq '`') { $in_bt = !$in_bt; $cur .= $c; $have = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "`") 0 1287 1287
6832 # Command / parameter substitution regions are opaque so their
6833 # internal whitespace does not word-split (e.g. $(echo a b c)).
6834 1287 ▶ if (!$in_dq) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_dq) 1216 71 1287
6835 1216 ▶ if ($c eq '$' && $i+1 < $n && $chars[$i+1] eq '{') { $bdep++; $cur .= '${'; $have = 1; $i += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "{") 0 1216 1216
Condition66%4/6
$c eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "{"
!ll&&!rl&&r
121330
$c eq "\$" and $i + 1 < $n
!ll&&!rl&&r
121303
MC/DC0%0/3
$c eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "{"
$c eq "\$" $i + 1 < $n $chars[$i + 1] eq "{"
Truth table50%2/4
A and B and C
A= $c eq "\$"B= $i + 1 < $nC= $chars[$i + 1] eq "{"
ABCresult
0XX0
10X0
1100
1111
6836 1216 ▶ if ($bdep > 0 && $c eq '}') { $bdep--; $cur .= $c; $have = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($bdep > 0 and $c eq "}") 0 1216 1216
Condition33%1/3
$bdep > 0 and $c eq "}"
!ll&&!rl&&r
121600
MC/DC0%0/2
$bdep > 0 and $c eq "}"
$bdep > 0 $c eq "}"
Truth table33%1/3
A and B
A= $bdep > 0B= $c eq "}"
ABresult
0X0
100
111
6837 1216 ▶ if ($c eq '(') { $pdep++; $cur .= $c; $have = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "(") 2 1214 1216
6838 1214 ▶ if ($c eq ')' && $pdep > 0) { $pdep--; $cur .= $c; $have = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq ")" and $pdep > 0) 2 1212 1214
Condition66%2/3
$c eq ")" and $pdep > 0
!ll&&!rl&&r
121202
MC/DC50%1/2
$c eq ")" and $pdep > 0
$c eq ")" $pdep > 0
Truth table66%2/3
A and B
A= $c eq ")"B= $pdep > 0
ABresult
0X0
100
111
6839 }
6840 1283 ▶ if (!$in_sq && !$in_dq && !$in_bt && $pdep == 0 && $bdep == 0 && $c =~ /\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_sq || $in_dq || $in_bt and $pdep == 0 and $bdep == 0 and $c =~ /\s/) 143 1140 1283
Condition80%12/15
not $in_sq || $in_dq || $in_bt and $pdep == 0 and $bdep == 0 and $c =~ /\s/
!ll&&!rl&&r
891051143
not $in_sq || $in_dq || $in_bt and $pdep == 0 and $bdep == 0
!ll&&!rl&&r
8901194
not $in_sq || $in_dq || $in_bt and $pdep == 0
!ll&&!rl&&r
71181194
$in_sq || $in_dq || $in_bt
l!l&&r!l&&!r
7101212
$in_sq or $in_dq
l!l&&r!l&&!r
0711212
MC/DC50%3/6
not $in_sq || $in_dq || $in_bt and $pdep == 0 and $bdep == 0 and $c =~ /\s/
$in_sq $in_dq $in_bt $pdep == 0 $bdep == 0 $c =~ /\s/
Truth table57%4/7
not(A or B || C) and D and E and F
A= $in_sqB= $in_dqC= $in_btD= $pdep == 0E= $bdep == 0F= $c =~ /\s/
ABCDEFresult
1XXXXX0
01XXXX0
001XXX0
0000XX0
00010X0
0001100
0001111
6841 143 ▶ if ($have) { push @words, $cur; $cur = ''; $have = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($have) 143 0 143
6842 143 $i++; next;
6843 }
6844 1140 $cur .= $c; $have = 1; $i++;
6845 }
6846 179 ▶ push @words, $cur if $have;
Branch: 1 branch
BranchTrueFalseTotal
if $have 178 1 179
6847 179 return @words;
6848 }
6849  
6850 # Parse the body of a (...) array literal into a list of [subscript, value]
6851 # pairs. $subscript is undef for positional elements and a string for
6852 # explicit [sub]=value elements.
6853 sub _arr_parse_elements {
6854 26 ▶ my ($class, $body) = @_;
Subroutine _arr_parse_elements: called CC=9 SCAR=23.0
6855 26 my @raw = _arr_split_words($body);
6856 26 my @out;
6857 26 for my $tok (@raw) {
6858 69 my ($sub, $vpart);
6859 69 ▶ if ($tok =~ /\A\[(.*?)\]=(.*)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tok =~ /\A\[(.*?)\]=(.*)\z/s) { } 9 60 69
6860 9 ($sub, $vpart) = ($1, $2);
6861 }
6862 else {
6863 60 ($sub, $vpart) = (undef, $tok);
6864 }
6865 69 ▶ my $raw_has_quote = ($vpart =~ /['"]/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$vpart =~ /['"]/ ? : 4 65 69
6866 69 my $exp = _expand($class, $vpart);
6867 69 ▶ if (defined $sub) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $sub) { } 9 60 69
elsif (not $raw_has_quote and $exp =~ /\s/) { } 0 60 60
6868 9 $sub = _arr_dequote(_expand($class, $sub));
6869 9 push @out, [$sub, _arr_dequote($exp)];
6870 }
6871 ▶ elsif (!$raw_has_quote && $exp =~ /\s/) {
Condition66%2/3
not $raw_has_quote and $exp =~ /\s/
!ll&&!rl&&r
4560
MC/DC0%0/2
not $raw_has_quote and $exp =~ /\s/
not $raw_has_quote $exp =~ /\s/
Truth table66%2/3
not(A) and B
A= not $raw_has_quoteB= $exp =~ /\s/
ABresult
0X0
100
111
6872 # Unquoted expansion is subject to word splitting.
6873 0 for my $w (split /\s+/, $exp) {
6874 0 ▶ next if $w eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $w eq "" 0 0 0
6875 0 push @out, [undef, $w];
6876 }
6877 }
6878 else {
6879 60 push @out, [undef, _arr_dequote($exp)];
6880 }
6881 }
6882 26 return @out;
6883 }
6884  
6885 # arr=( ... ) / arr+=( ... ) whole-array assignment or append.
6886 sub _arr_assign_literal {
6887 26 ▶ my ($class, $name, $body, $append) = @_;
Subroutine _arr_assign_literal: called CC=15 SCAR=27.4
6888 26 my $k = _arr_name($name);
6889 26 my $assoc = _arr_is_assoc($name); # honour a prior 'declare -A'
6890  
6891 26 ▶ if (!$append) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $append) { } 24 2 26
6892 24 $_SH_ARRAY{$k} = {};
6893 24 ▶ $_SH_ARRAY_TYPE{$k} = $assoc ? 'assoc' : 'indexed';
Branch: 1 branch
BranchTrueFalseTotal
$assoc ? : 3 21 24
6894 }
6895 else {
6896 2 ▶ $_SH_ARRAY{$k} = {} unless exists $_SH_ARRAY{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BATsh::SH::_SH_ARRAY{$k} 0 2 2
6897 $_SH_ARRAY_TYPE{$k} = ($assoc ? 'assoc' : 'indexed')
6898 2 ▶ unless exists $_SH_ARRAY_TYPE{$k};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $BATsh::SH::_SH_ARRAY_TYPE{$k} 0 2 2
$assoc ? : 0 0 0
6899 }
6900 26 BATsh::Env->unset($name); # a name is array OR scalar, not both
6901  
6902 26 my $is_assoc = ($_SH_ARRAY_TYPE{$k} eq 'assoc');
6903 26 my @elems = _arr_parse_elements($class, $body);
6904  
6905 26 ▶ if ($is_assoc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_assoc) { } 3 23 26
6906 3 for my $e (@elems) {
6907 7 my ($sub, $val) = @{$e};
6908 7 ▶ $sub = '' unless defined $sub;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sub 0 7 7
6909 7 $_SH_ARRAY{$k}{$sub} = $val;
6910 }
6911 }
6912 else {
6913 23 my $next = 0;
6914 23 for my $ix (keys %{$_SH_ARRAY{$k}}) {
6915 4 ▶ $next = $ix + 1 if $ix =~ /\A-?\d+\z/ && $ix + 1 > $next;
Branch: 1 branch
BranchTrueFalseTotal
if $ix =~ /\A-?\d+\z/ and $ix + 1 > $next 3 1 4
Condition66%2/3
$ix =~ /\A-?\d+\z/ and $ix + 1 > $next
!ll&&!rl&&r
013
MC/DC50%1/2
$ix =~ /\A-?\d+\z/ and $ix + 1 > $next
$ix =~ /\A-?\d+\z/ $ix + 1 > $next
Truth table66%2/3
A and B
A= $ix =~ /\A-?\d+\z/B= $ix + 1 > $next
ABresult
0X0
100
111
6916 }
6917 23 for my $e (@elems) {
6918 62 my ($sub, $val) = @{$e};
6919 62 ▶ if (defined $sub && $sub ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sub and $sub ne '') { } 2 60 62
Condition66%2/3
defined $sub and $sub ne ""
!ll&&!rl&&r
6002
MC/DC50%1/2
defined $sub and $sub ne ""
defined $sub $sub ne ""
Truth table66%2/3
A and B
A= defined $subB= $sub ne ""
ABresult
0X0
100
111
6920 2 my $ix = _arr_index($sub);
6921 2 $_SH_ARRAY{$k}{$ix} = $val;
6922 2 $next = $ix + 1;
6923 }
6924 else {
6925 60 $_SH_ARRAY{$k}{$next} = $val;
6926 60 $next++;
6927 }
6928 }
6929 }
6930 26 $LAST_STATUS = 0;
6931 26 return 0;
6932 }
6933  
6934 # arr[sub]=value / arr[sub]+=value single-element assignment or append.
6935 sub _arr_assign_element {
6936 8 ▶ my ($class, $name, $sub, $rawval, $append) = @_;
Subroutine _arr_assign_element: called CC=6 SCAR=18.3
6937 8 my $val = _expand($class, $rawval);
6938 8 $val =~ s/\A"(.*)"\z/$1/s;
6939 8 $val =~ s/\A'(.*)'\z/$1/s;
6940 8 my $k = _arr_name($name);
6941 8 $sub = _arr_expand_sub($class, $sub);
6942 8 ▶ if (!exists $_SH_ARRAY{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $BATsh::SH::_SH_ARRAY{$k}) 0 8 8
6943 0 $_SH_ARRAY{$k} = {};
6944 0 $_SH_ARRAY_TYPE{$k} = 'indexed';
6945 }
6946 8 BATsh::Env->unset($name);
6947 8 ▶ my $key = (defined $_SH_ARRAY_TYPE{$k} && $_SH_ARRAY_TYPE{$k} eq 'assoc')
Branch: 1 branch
BranchTrueFalseTotal
defined $BATsh::SH::_SH_ARRAY_TYPE{$k} && $BATsh::SH::_SH_ARRAY_TYPE{$k} eq 'assoc' ? : 6 2 8
Condition66%2/3
defined $BATsh::SH::_SH_ARRAY_TYPE{$k} and $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
!ll&&!rl&&r
026
MC/DC50%1/2
defined $BATsh::SH::_SH_ARRAY_TYPE{$k} and $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
defined $BATsh::SH::_SH_ARRAY_TYPE{$k} $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
Truth table66%2/3
A and B
A= defined $BATsh::SH::_SH_ARRAY_TYPE{$k}B= $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
ABresult
0X0
100
111
6948 ? $sub : _arr_index($sub);
6949 8 ▶ if ($append) {
Branch: 1 branch
BranchTrueFalseTotal
if ($append) { } 1 7 8
6950 1 ▶ my $old = exists $_SH_ARRAY{$k}{$key} ? $_SH_ARRAY{$k}{$key} : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $BATsh::SH::_SH_ARRAY{$k}{$key} ? : 1 0 1
6951 1 $_SH_ARRAY{$k}{$key} = $old . $val;
6952 }
6953 else {
6954 7 $_SH_ARRAY{$k}{$key} = $val;
6955 }
6956 8 $LAST_STATUS = 0;
6957 8 return 0;
6958 }
6959  
6960 # declare / typeset [-aA] NAME[=(...)] ... array (and scalar) declaration.
6961 sub _cmd_declare {
6962 11 ▶ my ($class, $rest) = @_;
Subroutine _cmd_declare: called CC=27 SCAR=36.7
6963 11 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 11 11
6964 11 $rest =~ s/\A\s+//;
6965  
6966 11 my $type; # 'assoc' | 'indexed' | undef
6967 11 my $intattr = 0;
6968 11 my $roattr = 0;
6969 11 ▶ while ($rest =~ s/\A(-[A-Za-z]+)\s+//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/\A(-[A-Za-z]+)\s+//) 11 11 22
6970 11 my $flag = $1;
6971 11 ▶ if ($flag =~ /A/) { $type = 'assoc' }
Branch: 2 branches
BranchTrueFalseTotal
if ($flag =~ /A/) { } 7 4 11
elsif ($flag =~ /a/) { } 0 4 4
6972 0 ▶ elsif ($flag =~ /a/) { $type = 'indexed' unless defined $type }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 0 0
6973 11 ▶ $intattr = 1 if $flag =~ /i/;
Branch: 1 branch
BranchTrueFalseTotal
if $flag =~ /i/ 4 7 11
6974 11 ▶ $roattr = 1 if $flag =~ /r/;
Branch: 1 branch
BranchTrueFalseTotal
if $flag =~ /r/ 0 11 11
6975 }
6976  
6977 11 ▶ while ($rest ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest ne "") 11 11 22
6978 11 $rest =~ s/\A\s+//;
6979 11 ▶ last if $rest eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $rest eq "" 0 11 11
6980 11 ▶ if ($rest =~ /\A([A-Za-z_][A-Za-z0-9_]*)\+?=\((.*)\)\s*(.*)\z/s) {
Branch: 3 branches
BranchTrueFalseTotal
if ($rest =~ /\A([A-Za-z_][A-Za-z0-9_]*)\+?=\((.*)\)\s*(.*)\z/s) { } 2 9 11
elsif ($rest =~ /\A([A-Za-z_][A-Za-z0-9_]*)=((?:"[^"]*"|'[^']*'|\S)*)\s*(.*)\z/s) { } 3 6 9
elsif ($rest =~ /\A([A-Za-z_][A-Za-z0-9_]*)\s*(.*)\z/s) { } 6 0 6
6981 2 my ($name, $body, $tail) = ($1, $2, $3);
6982 2 my $k = _arr_name($name);
6983 2 ▶ $_SH_ARRAY_TYPE{$k} = $type if defined $type;
Branch: 1 branch
BranchTrueFalseTotal
if defined $type 2 0 2
6984 2 _arr_assign_literal($class, $name, $body, 0);
6985 2 $rest = $tail;
6986 }
6987 elsif ($rest =~ /\A([A-Za-z_][A-Za-z0-9_]*)=((?:"[^"]*"|'[^']*'|\S)*)\s*(.*)\z/s) {
6988 3 my ($name, $val, $tail) = ($1, $2, $3);
6989 3 ▶ if (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) { } 0 3 3
6990 # Typed array declared with a scalar initialiser: seed [0].
6991 0 my $k = _arr_name($name);
6992 0 $_SH_ARRAY_TYPE{$k} = $type;
6993 0 _arr_assign_element($class, $name, '0', $val, 0);
6994 }
6995 else {
6996 3 $val = _expand($class, $val);
6997 3 $val =~ s/\A"(.*)"\z/$1/s;
6998 3 $val =~ s/\A'(.*)'\z/$1/s;
6999 3 my $ik = uc($name);
7000 3 ▶ $_SH_INTATTR{$ik} = 1 if $intattr;
Branch: 1 branch
BranchTrueFalseTotal
if $intattr 3 0 3
7001 # "declare -i x=EXPR" evaluates the right hand side as
7002 # arithmetic; _sh_store_scalar re-applies this for any
7003 # later plain "x=..." assignment because the attribute
7004 # sticks in %_SH_INTATTR.
7005 3 ▶ $val = _eval_arith($val) if $_SH_INTATTR{$ik};
Branch: 1 branch
BranchTrueFalseTotal
if $BATsh::SH::_SH_INTATTR{$ik} 3 0 3
7006 3 ▶ if (_sh_is_readonly($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_sh_is_readonly($name)) { } 0 3 3
7007 0 print STDERR "sh: $name: readonly variable\n";
7008 0 $LAST_STATUS = 1;
7009 }
7010 else {
7011 3 BATsh::Env->set($name, $val);
7012 }
7013 3 ▶ $_SH_READONLY{$ik} = 1 if $roattr;
Branch: 1 branch
BranchTrueFalseTotal
if $roattr 0 3 3
7014 }
7015 3 $rest = $tail;
7016 }
7017 elsif ($rest =~ /\A([A-Za-z_][A-Za-z0-9_]*)\s*(.*)\z/s) {
7018 6 my ($name, $tail) = ($1, $2);
7019 6 ▶ if (($intattr || $roattr) && !defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($intattr || $roattr and not defined $type) { } 1 5 6
Condition66%4/6
$intattr || $roattr and not defined $type
!ll&&!rl&&r
501
$intattr or $roattr
l!l&&r!l&&!r
105
MC/DC33%1/3
$intattr || $roattr and not defined $type
$intattr $roattr not defined $type
Truth table40%2/5
A or B and not(C)
A= $intattrB= $roattrC= not defined $type
ABCresult
00X0
1X00
0100
1X11
0111
7020 # Attribute-only declaration: mark the variable, do NOT
7021 # turn it into an (empty) array.
7022 1 my $ik = uc($name);
7023 1 ▶ $_SH_INTATTR{$ik} = 1 if $intattr;
Branch: 1 branch
BranchTrueFalseTotal
if $intattr 1 0 1
7024 1 ▶ $_SH_READONLY{$ik} = 1 if $roattr;
Branch: 1 branch
BranchTrueFalseTotal
if $roattr 0 1 1
7025 }
7026 else {
7027 5 my $k = _arr_name($name);
7028 5 ▶ $_SH_ARRAY{$k} = {} unless exists $_SH_ARRAY{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BATsh::SH::_SH_ARRAY{$k} 5 0 5
7029 5 ▶ $_SH_ARRAY_TYPE{$k} = (defined $type ? $type : 'indexed');
Branch: 1 branch
BranchTrueFalseTotal
defined $type ? : 5 0 5
7030 }
7031 6 $rest = $tail;
7032 }
7033 else {
7034 0 last;
7035 }
7036 }
7037 11 ▶ $LAST_STATUS = 0 unless $LAST_STATUS;
Branch: 1 branch
BranchTrueFalseTotal
unless $LAST_STATUS 11 0 11
7038 11 return $LAST_STATUS;
7039 }
7040  
7041 # _sh_try_array_op: detect and perform an array operation on the RAW line.
7042 # Returns (1, $status) when it handled the line, or () otherwise.
7043 sub _sh_try_array_op {
7044 1067 ▶ my ($class, $line, $opts_ref) = @_;
Subroutine _sh_try_array_op: called CC=11 SCAR=24.0
7045 1067 my $s = $line;
7046 1067 $s =~ s/\A\s+//; $s =~ s/\s+\z//;
7047 1067 $s =~ s/\s*;\s*\z//; # tolerate one trailing ';'
7048  
7049 # declare / typeset / local with array semantics
7050 1067 ▶ if ($s =~ /\A(declare|typeset|local)\b\s*(.*)\z/is) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A(declare|typeset|local)\b\s*(.*)\z/is) 13 1054 1067
7051 13 my ($kw, $args) = ($1, $2);
7052 13 my $is_local = (lc($kw) eq 'local');
7053 13 ▶ my $has_arr_flag = ($args =~ /\A-[A-Za-z]*[aA]/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args =~ /\A-[A-Za-z]*[aA]/ ? : 7 6 13
7054 13 ▶ my $has_arr_init = ($args =~ /=\(/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args =~ /=\(/ ? : 2 11 13
7055 13 ▶ if (!$is_local || $has_arr_flag || $has_arr_init) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_local or $has_arr_flag or $has_arr_init) 0 2 2
Condition66%4/6
not $is_local or $has_arr_flag or $has_arr_init
l!l&&r!l&&!r
1102
not $is_local or $has_arr_flag
l!l&&r!l&&!r
1102
MC/DC33%1/3
not $is_local or $has_arr_flag or $has_arr_init
not $is_local $has_arr_flag $has_arr_init
Truth table50%2/4
not(A) or B or C
A= not $is_localB= $has_arr_flagC= $has_arr_init
ABCresult
1XX1
01X1
0011
0000
7056 11 return (1, _cmd_declare($class, $args));
7057 }
7058 2 return (); # plain 'local x=1' -> handled by _cmd_local
7059 }
7060  
7061 # NAME=( ... ) or NAME+=( ... ) -- whole-line array literal
7062 1054 ▶ if ($s =~ /\A([A-Za-z_][A-Za-z0-9_]*)(\+?)=\((.*)\)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A([A-Za-z_][A-Za-z0-9_]*)(\+?)=\((.*)\)\z/s) 24 1030 1054
7063 24 my ($name, $plus, $body) = ($1, $2, $3);
7064 24 ▶ return (1, _arr_assign_literal($class, $name, $body,
Branch: 1 branch
BranchTrueFalseTotal
$plus eq '+' ? : 2 22 24
7065 ($plus eq '+') ? 1 : 0));
7066 }
7067  
7068 # NAME[SUB]=VALUE or NAME[SUB]+=VALUE -- single-element assignment
7069 1030 ▶ if ($s =~ /\A([A-Za-z_][A-Za-z0-9_]*)\[([^\]]*)\](\+?)=(.*)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A([A-Za-z_][A-Za-z0-9_]*)\[([^\]]*)\](\+?)=(.*)\z/s) 8 1022 1030
7070 8 my ($name, $sub, $plus, $val) = ($1, $2, $3, $4);
7071 8 ▶ return (1, _arr_assign_element($class, $name, $sub, $val,
Branch: 1 branch
BranchTrueFalseTotal
$plus eq '+' ? : 1 7 8
7072 ($plus eq '+') ? 1 : 0));
7073 }
7074  
7075 1022 return ();
7076 }
7077  
7078 # _expand_word_list: turn a for-loop list string into a list of items.
7079 # Resolves variables / command substitution, applies filename globbing to
7080 # unquoted glob words, and expands a whole-word ${arr[@]} / ${arr[*]}
7081 # reference (quoted or not) to one item per array element.
7082 sub _expand_word_list {
7083 22 ▶ my ($class, $list_str) = @_;
Subroutine _expand_word_list: called CC=13 SCAR=25.7
7084 22 ▶ $list_str = _brace_expand_line($list_str) if defined $list_str && $list_str =~ /\{/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $list_str and $list_str =~ /\{/ 3 19 22
Condition66%2/3
defined $list_str and $list_str =~ /\{/
!ll&&!rl&&r
0193
MC/DC50%1/2
defined $list_str and $list_str =~ /\{/
defined $list_str $list_str =~ /\{/
Truth table66%2/3
A and B
A= defined $list_strB= $list_str =~ /\{/
ABresult
0X0
100
111
7085 22 my @raw = _arr_split_words($list_str);
7086 22 my @items;
7087 22 for my $tok (@raw) {
7088 # Whole-word ${arr[@]} / ${arr[*]} -> one item per element value.
7089 40 ▶ if ($tok =~ /\A"?\$\{([A-Za-z_][A-Za-z0-9_]*)\[[\@*]\]\}"?\z/
Branch: 1 branch
BranchTrueFalseTotal
if ($tok =~ /\A"?\$\{([A-Za-z_][A-Za-z0-9_]*)\[[\@*]\]\}"?\z/ and _arr_exists($1)) 2 38 40
Condition66%2/3
$tok =~ /\A"?\$\{([A-Za-z_][A-Za-z0-9_]*)\[[\@*]\]\}"?\z/ and _arr_exists($1)
!ll&&!rl&&r
3802
MC/DC50%1/2
$tok =~ /\A"?\$\{([A-Za-z_][A-Za-z0-9_]*)\[[\@*]\]\}"?\z/ and _arr_exists($1)
$tok =~ /\A"?\$\{([A-Za-z_][A-Za-z0-9_]*)\[[\@*]\]\}"?\z/ _arr_exists($1)
Truth table66%2/3
A and B
A= $tok =~ /\A"?\$\{([A-Za-z_][A-Za-z0-9_]*)\[[\@*]\]\}"?\z/B= _arr_exists($1)
ABresult
0X0
100
111
7090 && _arr_exists($1)) {
7091 2 push @items, _arr_values($1);
7092 2 next;
7093 }
7094 # Whole-word ${!arr[@]} / ${!arr[*]} -> one item per index / key.
7095 38 ▶ if ($tok =~ /\A"?\$\{!([A-Za-z_][A-Za-z0-9_]*)\[[\@*]\]\}"?\z/
Branch: 1 branch
BranchTrueFalseTotal
if ($tok =~ /\A"?\$\{!([A-Za-z_][A-Za-z0-9_]*)\[[\@*]\]\}"?\z/ and _arr_exists($1)) 1 37 38
Condition66%2/3
$tok =~ /\A"?\$\{!([A-Za-z_][A-Za-z0-9_]*)\[[\@*]\]\}"?\z/ and _arr_exists($1)
!ll&&!rl&&r
3701
MC/DC50%1/2
$tok =~ /\A"?\$\{!([A-Za-z_][A-Za-z0-9_]*)\[[\@*]\]\}"?\z/ and _arr_exists($1)
$tok =~ /\A"?\$\{!([A-Za-z_][A-Za-z0-9_]*)\[[\@*]\]\}"?\z/ _arr_exists($1)
Truth table66%2/3
A and B
A= $tok =~ /\A"?\$\{!([A-Za-z_][A-Za-z0-9_]*)\[[\@*]\]\}"?\z/B= _arr_exists($1)
ABresult
0X0
100
111
7096 && _arr_exists($1)) {
7097 1 push @items, _arr_ordered_keys($1);
7098 1 next;
7099 }
7100 37 ▶ my $raw_has_quote = ($tok =~ /['"]/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$tok =~ /['"]/ ? : 1 36 37
7101 37 my $exp = _arr_dequote(_expand($class, $tok));
7102 37 ▶ if (!$raw_has_quote) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $raw_has_quote) { } 36 1 37
7103 36 ▶ if ($exp =~ /[*?\[]/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($exp =~ /[*?\[]/) { } 3 33 36
elsif ($exp =~ /\s/) { } 2 31 33
elsif ($exp ne '') { } 31 0 31
7104 3 push @items, _glob_expand($exp);
7105 }
7106 elsif ($exp =~ /\s/) {
7107 2 push @items, grep { $_ ne '' } split /\s+/, $exp;
7108 }
7109 elsif ($exp ne '') {
7110 31 push @items, $exp;
7111 }
7112 # an empty unquoted word expands to nothing
7113 }
7114 else {
7115 1 push @items, $exp;
7116 }
7117 }
7118 22 return @items;
7119 }
7120  
7121 # ----------------------------------------------------------------
7122 # Accessors
7123 # ----------------------------------------------------------------
7124 2 ▶sub last_status { return $LAST_STATUS }
Subroutine last_status: called CC=1 SCAR=0.0
Pod undocumented
7125 0 ▶sub set_last_status { $LAST_STATUS = $_[1] }
Subroutine set_last_status: not called CC=1 SCAR=6.9
Pod undocumented
7126  
7127 # Need Cwd
7128 BEGIN {
7129 36 ▶ eval { require Cwd };
Subroutine BEGIN: called CC=2 SCAR=7.6
7130 36 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 36 36
7131 0 eval 'sub Cwd::cwd { return $ENV{PWD} || "." }';
7132 }
7133 }
7134  
7135 36 1;
7136  
7137 __END__
« blib/lib/BATsh/CMD.pm ↑ Summary blib/lib/BATsh.pm »