lib/BATsh/SH.pm

filter: Statementstmt 86.2% 4591 / 5325 Branchbran 70.7% 2208 / 3122 Conditioncond 64.1% 1061 / 1655 MC/DCmcdc 40.9% 381 / 930 Subroutinesub 96.7% 207 / 214 Podpod 0.0% 0 / 8 totaltotal 75.0% 8448 / 11254 CC 2205 SCAR 39.3
CC
2205
Cov
78%
_eval_test
367.0
_parse_select
174.8
_parse_subshell
127.0
CRAP 50.8
↑ Summary lib/BATsh/CMD.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 64 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 0 ▶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 64 64
Condition33%1/3
$] < "5.006" and not defined &warnings::import
!ll&&!rl&&r
6400
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 64 ▶use warnings; local $^W = 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 64 ▶BEGIN { pop @INC if $INC[-1] eq '.' }
Subroutine BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $INC[-1] eq "." 0 64 64
69  
70 64 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
71 64 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
72 64 ▶use Fcntl qw(O_RDONLY O_WRONLY O_CREAT O_EXCL O_TRUNC O_APPEND);
Subroutine BEGIN: called CC=1 SCAR=0.0
73 64 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
74 64 $VERSION = '0.11';
75 64 $VERSION = $VERSION;
76  
77 64 require BATsh::MB;
78  
79 # Bareword filehandle globs for SH pipeline (Perl 5.005_03 compatible)
80 64 ▶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 64 ▶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 64 ▶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 64 ▶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 64 ▶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 64 ▶use vars qw($_LAST_BG_PID);
Subroutine BEGIN: called CC=1 SCAR=0.0
99 64 $_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 64 ▶use vars qw($_SUBST_DEPTH);
Subroutine BEGIN: called CC=1 SCAR=0.0
106 64 $_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 64 ▶use vars qw($_PIPE_DEPTH);
Subroutine BEGIN: called CC=1 SCAR=0.0
114 64 $_PIPE_DEPTH = 0;
115  
116 # SH function registry -- must be package-level for access from _expand and _exec_line
117 64 ▶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 64 ▶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 64 ▶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 64 ▶use vars qw($_SH_UMASK $_SH_UMASK_INIT);
Subroutine BEGIN: called CC=1 SCAR=0.0
139 64 $_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 64 ▶use vars qw($_PROCSUB_SEQ @_PROCSUB_TMPFILES @_PROCSUB_DEFERRED);
Subroutine BEGIN: called CC=1 SCAR=0.0
146 64 $_PROCSUB_SEQ = 0;
147 64 @_PROCSUB_TMPFILES = ();
148 64 @_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 64 ▶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 64 ▶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 64 ▶use vars qw($_GETOPTS_CHARPOS $_GETOPTS_LAST_OPTIND);
Subroutine BEGIN: called CC=1 SCAR=0.0
178 64 $_GETOPTS_CHARPOS = 0;
179 64 $_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 64 ▶use vars qw($_CMD_NO_FUNC);
Subroutine BEGIN: called CC=1 SCAR=0.0
187 64 $_CMD_NO_FUNC = 0;
188 # ----------------------------------------------------------------
189 64 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 64 my $_OPT_ERREXIT = 0; # set -e: exit on a failing simple command
194 64 my $_OPT_NOUNSET = 0; # set -u: error on expanding an unset variable
195 64 my $_OPT_XTRACE = 0; # set -x: trace each simple command to STDERR
196 64 my $_OPT_EXTGLOB = 0; # shopt -s extglob: ?(),*(),+(),@(),!() in patterns
197 64 my $_ERREXIT_HOLD = 0; # >0: -e suppressed (condition / non-final &&-|| member)
198 64 my $_ERREXIT_DONE = 0; # set by _exec_sh_compound: -e already adjudicated
199 64 my @FUNCTION_STACK = (); # for 'local' variable scoping
200  
201 # Signal: pending exit
202 64 my $_EXIT_CODE = undef; # undef = no exit pending
203 64 my $_BREAK = 0; # break out of loop
204 64 my $_CONTINUE = 0; # continue next iteration
205 64 my $_RETURN = 0; # return from function/source
206  
207 # Here-document state (Perl 5.005_03 compatible)
208 64 my $_HD_SEQ = 0; # per-process counter for unique temp names
209 64 my @_HD_TMPFILES = (); # tempfiles to remove on END (failsafe cleanup)
210  
211 # Background-job state (Perl 5.005_03 compatible)
212 64 my $_BG_SEQ = 0; # per-process counter for unique pidfile names
213 64 my @_BG_TMPFILES = (); # pidfiles to remove on END (failsafe cleanup)
214  
215 # Command-substitution capture state (Perl 5.005_03 compatible)
216 64 my $_SUBST_SEQ = 0; # per-process counter for unique capture names
217 64 my @_SUBST_TMPFILES = (); # capture files to remove on END (failsafe cleanup)
218  
219 # SH pipeline stage state (Perl 5.005_03 compatible)
220 64 my $_PIPE_SEQ = 0; # per-process counter for unique stage names
221 64 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 1028 ▶sub get_status { return $LAST_STATUS }
Subroutine get_status: called CC=1 SCAR=0.0
Pod undocumented
233 158 ▶sub set_status {
Subroutine set_status: called CC=2 SCAR=7.2
Pod undocumented
234 158 ▶ $LAST_STATUS = defined $_[0] ? int($_[0]) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 158 0 158
235 158 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 501 ▶sub exit_code_pending { return $_EXIT_CODE }
Subroutine exit_code_pending: called CC=1 SCAR=0.0
Pod undocumented
245  
246 544 ▶sub exec_block {
Subroutine exec_block: called CC=2 SCAR=6.9
Pod undocumented
247 544 my ($class, $lines_ref, %opts) = @_;
248 544 $_EXIT_CODE = undef;
249 544 $_BREAK = 0;
250 544 $_CONTINUE = 0;
251 544 $_RETURN = 0;
252  
253 544 my $status = _run_lines($class, $lines_ref, \%opts);
254 544 ▶ return defined $_EXIT_CODE ? $_EXIT_CODE : $status;
Branch: 1 branch
BranchTrueFalseTotal
defined $_EXIT_CODE ? : 26 518 544
255 }
256  
257 # ----------------------------------------------------------------
258 # Run an array of lines sequentially, handling multi-line blocks
259 # Returns last exit status
260 # ----------------------------------------------------------------
261 1190 ▶sub _run_lines {
Subroutine _run_lines: called CC=70 SCAR=44.1
262 1190 my ($class, $lines_ref, $opts_ref) = @_;
263 1190 my @lines = @{$lines_ref};
264 1190 my $status = 0;
265 1190 my $i = 0;
266  
267 1190 ▶ while ($i <= $#lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#lines) 2221 1170 3391
268 2221 ▶ return $status if defined $_EXIT_CODE;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_EXIT_CODE 18 2203 2221
269 2203 ▶ return $status if $_BREAK || $_RETURN;
Branch: 1 branch
BranchTrueFalseTotal
if $_BREAK or $_RETURN 1 2202 2203
Condition66%2/3
$_BREAK or $_RETURN
l!l&&r!l&&!r
102202
MC/DC50%1/2
$_BREAK or $_RETURN
$_BREAK $_RETURN
Truth table66%2/3
A or B
A= $_BREAKB= $_RETURN
ABresult
1X1
011
000
270  
271 2202 my $line = $lines[$i];
272 2202 $i++;
273 # Normalise
274 2202 $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 2202 my $nocmt = _strip_sh_comment($line);
278 2202 ▶ if ($nocmt ne $line) { $line = $nocmt; $lines[$i - 1] = $line; }
Branch: 1 branch
BranchTrueFalseTotal
if ($nocmt ne $line) 7 2195 2202
279 # Skip empty and comment lines
280 2202 ▶ next if $line =~ /\A\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\s*\z/ 430 1772 2202
281 1772 ▶ next if $line =~ /\A\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\s*#/ 0 1772 1772
282  
283 # Check for block-opening keywords
284 1772 my $stripped = $line;
285 1772 $stripped =~ s/\A\s+//;
286 1772 my $first = '';
287 1772 ($first) = ($stripped =~ /\A(\S+)/);
288 1772 ▶ $first = lc(defined($first) ? $first : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $first ? : 1772 0 1772
289  
290 # A control structure written entirely on this physical line and
291 # followed by more commands -- "if ...; fi; echo B", "for ...; done
292 # && echo ok", "case ...; esac || echo no". Run the structure by
293 # itself, then put what follows back into this slot and reprocess
294 # it, so a multi-line opener in the tail still works. For '&&' and
295 # '||' the structure's status is carried into the tail by a leading
296 # "true"/"false", which keeps a longer chain (a && b || c) working.
297 1772 ▶ if ( $first eq 'if' || $first eq 'for' || $first eq 'while'
Branch: 1 branch
BranchTrueFalseTotal
if ($first eq "if" or $first eq "for" or $first eq "while" or $first eq "until" or $first eq "case" or $first eq "select") 206 1566 1772
Condition100%15/15
$first eq "if" or $first eq "for" or $first eq "while" or $first eq "until" or $first eq "case" or $first eq "select"
l!l&&r!l&&!r
20331566
$first eq "if" or $first eq "for" or $first eq "while" or $first eq "until" or $first eq "case"
l!l&&r!l&&!r
149541569
$first eq "if" or $first eq "for" or $first eq "while" or $first eq "until"
l!l&&r!l&&!r
14631623
$first eq "if" or $first eq "for" or $first eq "while"
l!l&&r!l&&!r
104421626
$first eq "if" or $first eq "for"
l!l&&r!l&&!r
60441668
MC/DC100%6/6
$first eq "if" or $first eq "for" or $first eq "while" or $first eq "until" or $first eq "case" or $first eq "select"
$first eq "if" $first eq "for" $first eq "while" $first eq "until" $first eq "case" $first eq "select"
Truth table100%7/7
A or B or C or D or E or F
A= $first eq "if"B= $first eq "for"C= $first eq "while"D= $first eq "until"E= $first eq "case"F= $first eq "select"
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
298 || $first eq 'until' || $first eq 'case' || $first eq 'select') {
299 # "for ...; done > file" / "if ...; fi 2> log". A redirection
300 # written after the terminator applies to the whole structure,
301 # as in bash. The block parsers anchor their terminator at
302 # end-of-line, so before 0.11 the redirection was left in place,
303 # "done > file" was run as a command, and the output went to the
304 # terminal instead of the file. Input redirection is left to
305 # the existing "while read x; do ...; done < file" handling.
306 206 ▶ my $term = ($first eq 'if') ? 'fi'
Branch: 2 branches
BranchTrueFalseTotal
$first eq 'if' ? : 60 146 206
$first eq 'case' ? : 54 92 146
307 : ($first eq 'case') ? 'esac'
308 : 'done';
309 206 my ($clean, $rd) = _sh_strip_redirects($line);
310 206 ▶ if (@{$rd} && _inline_has_terminator($clean, $term)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$rd;} and _inline_has_terminator($clean, $term)) 2 204 206
Condition66%2/3
@{$rd;} and _inline_has_terminator($clean, $term)
!ll&&!rl&&r
20402
MC/DC50%1/2
@{$rd;} and _inline_has_terminator($clean, $term)
@{$rd;} _inline_has_terminator($clean, $term)
Truth table66%2/3
A and B
A= @{$rd;}B= _inline_has_terminator($clean, $term)
ABresult
0X0
100
111
311 2 my @out_rd = grep { $_->[0] != 0 } @{$rd};
312 2 ▶ if (scalar(@out_rd) == scalar(@{$rd})) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @out_rd == scalar @{$rd;}) 1 1 2
313 1 $status = _run_inline_control_redir($class, $clean, $rd,
314 $opts_ref);
315 1 ▶ last if defined $_EXIT_CODE || $_BREAK || $_RETURN;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_EXIT_CODE or $_BREAK or $_RETURN 0 1 1
Condition33%2/6
defined $_EXIT_CODE or $_BREAK or $_RETURN
l!l&&r!l&&!r
001
defined $_EXIT_CODE or $_BREAK
l!l&&r!l&&!r
001
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
316 1 next;
317 }
318 }
319  
320 205 my ($head, $op, $tail) = _split_inline_control_tail($line);
321 205 ▶ if (defined $tail && $op eq '|') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tail and $op eq "|") 2 203 205
Condition100%3/3
defined $tail and $op eq "|"
!ll&&!rl&&r
191122
MC/DC100%2/2
defined $tail and $op eq "|"
defined $tail $op eq "|"
Truth table100%3/3
A and B
A= defined $tailB= $op eq "|"
ABresult
0X0
100
111
322 2 $status = _run_inline_control_pipe($class, $head, $tail,
323 $opts_ref);
324 2 ▶ last if defined $_EXIT_CODE || $_BREAK || $_RETURN;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_EXIT_CODE or $_BREAK or $_RETURN 0 2 2
Condition33%2/6
defined $_EXIT_CODE or $_BREAK or $_RETURN
l!l&&r!l&&!r
002
defined $_EXIT_CODE or $_BREAK
l!l&&r!l&&!r
002
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
325 2 next;
326 }
327 203 ▶ if (defined $tail) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tail) 12 191 203
328 12 $status = _run_lines($class, [$head], $opts_ref);
329 12 ▶ last if defined $_EXIT_CODE || $_BREAK || $_RETURN;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_EXIT_CODE or $_BREAK or $_RETURN 1 11 12
Condition66%4/6
defined $_EXIT_CODE or $_BREAK or $_RETURN
l!l&&r!l&&!r
1011
defined $_EXIT_CODE or $_BREAK
l!l&&r!l&&!r
0111
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
330 11 ▶ if ($op eq '&&' || $op eq '||') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq '&&' or $op eq '||') { } 3 8 11
Condition100%3/3
$op eq "&&" or $op eq "||"
l!l&&r!l&&!r
218
MC/DC100%2/2
$op eq "&&" or $op eq "||"
$op eq "&&" $op eq "||"
Truth table100%3/3
A or B
A= $op eq "&&"B= $op eq "||"
ABresult
1X1
011
000
331 3 ▶ $lines[$i - 1] = (($status == 0) ? 'true' : 'false')
Branch: 1 branch
BranchTrueFalseTotal
$status == 0 ? : 3 0 3
332 . " $op $tail";
333 }
334 else {
335 8 $lines[$i - 1] = $tail;
336 }
337 11 $i--;
338 11 next;
339 }
340 }
341  
342 1757 ▶ if ($first eq 'if') {
Branch: 1 branch
BranchTrueFalseTotal
if ($first eq "if") 52 1705 1757
343 52 ($status, $i) = _parse_if($class, \@lines, $i - 1, $opts_ref);
344 52 next;
345 }
346 1705 ▶ if ($first eq 'for') {
Branch: 1 branch
BranchTrueFalseTotal
if ($first eq "for") 40 1665 1705
347 40 ($status, $i) = _parse_for($class, \@lines, $i - 1, $opts_ref);
348 40 next;
349 }
350 1665 ▶ if ($first eq 'select') {
Branch: 1 branch
BranchTrueFalseTotal
if ($first eq "select") 3 1662 1665
351 3 ($status, $i) = _parse_select($class, \@lines, $i - 1, $opts_ref);
352 3 next;
353 }
354 1662 ▶ if ($first eq 'while' || $first eq 'until') {
Branch: 1 branch
BranchTrueFalseTotal
if ($first eq "while" or $first eq "until") 43 1619 1662
Condition100%3/3
$first eq "while" or $first eq "until"
l!l&&r!l&&!r
4031619
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
355 43 ($status, $i) = _parse_while($class, \@lines, $i - 1, $opts_ref);
356 43 next;
357 }
358 1619 ▶ if ($first eq 'case') {
Branch: 1 branch
BranchTrueFalseTotal
if ($first eq "case") 53 1566 1619
359 53 ($status, $i) = _parse_case($class, \@lines, $i - 1, $opts_ref);
360 53 next;
361 }
362  
363 # Subshell command group: "( commands )" [redir]. A leading "(("
364 # is arithmetic-expansion-flavoured, not a subshell group, and is
365 # left alone (standalone "((...))" arithmetic commands are not
366 # implemented; only "$((...))" as an expansion is).
367 1566 ▶ if ($stripped =~ /\A\(/ && $stripped !~ /\A\(\(/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stripped =~ /\A\(/ and not $stripped =~ /\A\(\(/) 12 1554 1566
Condition66%2/3
$stripped =~ /\A\(/ and not $stripped =~ /\A\(\(/
!ll&&!rl&&r
1554012
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
368 12 my ($h, $o, $t) = _split_inline_brace_tail($line, '(', ')');
369 12 ▶ if (defined $t) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t) 2 10 12
370 2 $status = _run_lines($class, [$h], $opts_ref);
371 2 ▶ last if defined $_EXIT_CODE || $_BREAK || $_RETURN;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_EXIT_CODE or $_BREAK or $_RETURN 0 2 2
Condition33%2/6
defined $_EXIT_CODE or $_BREAK or $_RETURN
l!l&&r!l&&!r
002
defined $_EXIT_CODE or $_BREAK
l!l&&r!l&&!r
002
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
372 2 ▶ $lines[$i - 1] = ($o eq ';')
Branch: 2 branches
BranchTrueFalseTotal
$o eq ';' ? : 1 1 2
$status == 0 ? : 1 0 1
373 ? $t
374 : (($status == 0) ? 'true' : 'false') . " $o $t";
375 2 $i--;
376 2 next;
377 }
378 10 ($status, $i) = _parse_subshell($class, \@lines, $i - 1, $opts_ref);
379 10 next;
380 }
381  
382 # Function definition: "name() {" or "function name {"
383 1554 ▶ 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*\))/) 28 1526 1554
384 # "f(){ ...; }; more" -- the brace group closes on this line and
385 # is followed by further commands. Define the function first,
386 # then reprocess the tail. Without this the closing-brace test
387 # in _parse_function (which anchors '}' at end of line) failed,
388 # the body collector went hunting for '}' on the FOLLOWING lines,
389 # and the rest of the script was swallowed.
390 28 my ($h, $o, $t) = _split_inline_brace_tail($line, '{', '}');
391 28 ▶ if (defined $t) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t) 3 25 28
392 3 $status = _run_lines($class, [$h], $opts_ref);
393 3 ▶ last if defined $_EXIT_CODE || $_BREAK || $_RETURN;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_EXIT_CODE or $_BREAK or $_RETURN 0 3 3
Condition33%2/6
defined $_EXIT_CODE or $_BREAK or $_RETURN
l!l&&r!l&&!r
003
defined $_EXIT_CODE or $_BREAK
l!l&&r!l&&!r
003
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
394 3 ▶ $lines[$i - 1] = ($o eq ';')
Branch: 2 branches
BranchTrueFalseTotal
$o eq ';' ? : 3 0 3
$status == 0 ? : 0 0 0
395 ? $t
396 : (($status == 0) ? 'true' : 'false') . " $o $t";
397 3 $i--;
398 3 next;
399 }
400 25 ($status, $i) = _parse_function($class, \@lines, $i - 1, $opts_ref);
401 25 next;
402 }
403  
404 # Here-document: cmd << [-] [QUOTE]DELIM[QUOTE]
405 # Detected on a simple command line; body is read from following
406 # lines up to a line equal to DELIM (after optional tab strip for <<-).
407 1526 my @hd = _hd_detect($line);
408 1526 ▶ if (@hd) {
Branch: 1 branch
BranchTrueFalseTotal
if (@hd) 13 1513 1526
409 13 my ($cmd_part, $dash, $delim, $quoted) = @hd;
410 13 my @body = ();
411 13 my $terminated = 0;
412 13 ▶ while ($i <= $#lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#lines) 29 1 30
413 29 my $bl = $lines[$i];
414 29 $i++;
415 29 $bl =~ s/\r?\n\z//;
416 29 my $probe = $bl;
417 29 ▶ $probe =~ s/\A\t+// if $dash; # <<- strips leading tabs
Branch: 1 branch
BranchTrueFalseTotal
if $dash 2 27 29
418 29 ▶ if ($probe eq $delim) { $terminated = 1; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($probe eq $delim) 12 17 29
419 17 ▶ $bl =~ s/\A\t+// if $dash; # also strip tabs from body
Branch: 1 branch
BranchTrueFalseTotal
if $dash 1 16 17
420 17 push @body, $bl;
421 }
422 13 ▶ if (!$terminated) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($terminated) 1 12 13
423 1 warn "sh: unexpected EOF while looking for here-document delimiter \`$delim'\n";
424 1 $LAST_STATUS = 2;
425 1 $status = 2;
426 1 next;
427 }
428 12 $status = _hd_run($class, $cmd_part, \@body, $quoted, $opts_ref);
429 12 next;
430 }
431  
432 # A simple-command prefix followed on the SAME physical line by a
433 # control structure introduced with ';' -- e.g.
434 # x=""; if [ -z "$x" ]; then echo empty; fi
435 # i=0; while [ $i -lt 3 ]; do ...; done
436 # v=cat; case $v in ...; esac
437 # The first token is not a control keyword, so the block-opener
438 # dispatch above did not fire. Run the prefix now, then rewrite
439 # the current slot to the control structure and reprocess it, so
440 # the proper _parse_* handles it (and may consume following
441 # physical lines for a multi-line body).
442 1513 my $ctl_off = _find_control_split($line);
443 1513 ▶ if (defined $ctl_off) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ctl_off) 5 1508 1513
444 5 my $prefix = substr($line, 0, $ctl_off);
445 5 my $rest = substr($line, $ctl_off);
446 5 $prefix =~ s/\s*;\s*\z//;
447 5 ▶ if ($prefix =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix =~ /\S/) 5 0 5
448 5 $_ERREXIT_DONE = 0;
449 5 $status = _exec_line($class, $prefix, $opts_ref);
450 5 ▶ _errexit_check($status) unless $_ERREXIT_DONE;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ERREXIT_DONE 5 0 5
451 5 $_ERREXIT_DONE = 0;
452 }
453 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
454 5 $lines[$i - 1] = $rest;
455 5 $i--;
456 5 next;
457 }
458  
459 1508 $_ERREXIT_DONE = 0;
460 1508 $status = _exec_line($class, $line, $opts_ref);
461 1508 ▶ _errexit_check($status) unless $_ERREXIT_DONE;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ERREXIT_DONE 1453 55 1508
462 1508 $_ERREXIT_DONE = 0;
463 1508 ▶ $_CONTINUE = 0 if $_CONTINUE;
Branch: 1 branch
BranchTrueFalseTotal
if $_CONTINUE 1 1507 1508
464 }
465 1171 return $status;
466 }
467  
468 # ----------------------------------------------------------------
469 # Execute one SH line
470 # ----------------------------------------------------------------
471 # _exec_line: thin wrapper around _exec_line_impl() that drains any
472 # process-substitution bookkeeping (v0.07) added while executing THIS
473 # call's line -- running deferred >(cmd) jobs and removing <(cmd) temp
474 # files -- after the simple command has run. Because _exec_line_impl()
475 # itself calls back into _exec_line() (never _exec_line_impl() directly)
476 # for background execution, redirection wrapping, compound/pipeline
477 # segments, and exec, each nesting level drains only the entries it
478 # introduced, using the before/after array-length markers below.
479 # ----------------------------------------------------------------
480 1744 ▶sub _exec_line {
Subroutine _exec_line: called CC=6 SCAR=17.9
481 1744 my ($class, $raw, $opts_ref) = @_;
482 1744 my $tmp_before = scalar(@_PROCSUB_TMPFILES);
483 1744 my $def_before = scalar(@_PROCSUB_DEFERRED);
484 1744 my $rc = _exec_line_impl($class, $raw, $opts_ref);
485 1744 ▶ if (@_PROCSUB_DEFERRED > $def_before) {
Branch: 1 branch
BranchTrueFalseTotal
if (@BATsh::SH::_PROCSUB_DEFERRED > $def_before) 1 1743 1744
486 1 my @jobs = splice(@_PROCSUB_DEFERRED, $def_before);
487 1 for my $job (@jobs) {
488 1 my ($tmp, $cmd) = @{$job};
489 1 _sh_exec_with_redirs($class, $cmd, [[0, 0, $tmp]], $opts_ref);
490 }
491 }
492 1744 ▶ if (@_PROCSUB_TMPFILES > $tmp_before) {
Branch: 1 branch
BranchTrueFalseTotal
if (@BATsh::SH::_PROCSUB_TMPFILES > $tmp_before) 2 1742 1744
493 2 my @files = splice(@_PROCSUB_TMPFILES, $tmp_before);
494 2 ▶ for my $f (@files) { unlink $f if defined $f }
Branch: 1 branch
BranchTrueFalseTotal
if defined $f 2 0 2
495 }
496 1744 return $rc;
497 }
498  
499 1744 ▶sub _exec_line_impl {
Subroutine _exec_line_impl: called CC=72 SCAR=43.2
500 1744 my ($class, $raw, $opts_ref) = @_;
501  
502 1744 my $line = $raw;
503 1744 $line =~ s/\A\s+//;
504 1744 ▶ return 0 if $line =~ /\A\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\s*\z/ 0 1744 1744
505 1744 ▶ return 0 if $line =~ /\A\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\s*#/ 0 1744 1744
506  
507 # Shebang: treat as comment
508 1744 ▶ return 0 if $line =~ /\A#!/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A#!/ 0 1744 1744
509  
510 # Alias expansion (v0.07): replace a leading command word that names
511 # an active alias with its stored text, re-checking the new leading
512 # word up to a small bounded number of times so that alias chains
513 # (alias ll=ls; alias ls='ls -la') resolve. A name is expanded at
514 # most once per line to guard against a self-referential alias
515 # (alias ls=ls) looping forever. Only the first word of the line is
516 # considered -- bash's "trailing space in the alias value also makes
517 # the next word alias-eligible" refinement is not modelled.
518 1744 $line = _alias_expand_line($line);
519  
520 # Brace expansion (v0.07): {a,b,c} and {1..5}/{a..e} word-generation,
521 # performed lexically on the raw line before any other expansion, so
522 # that e.g. "echo a{b,c}d" becomes two arguments "abd acd". Regions
523 # that are quoted, or that belong to ${...}/$(...)/$((...))/`...`,
524 # are left untouched (their braces/parens are not brace-expansion
525 # syntax).
526 1744 $line = _brace_expand_line($line);
527  
528 # ----------------------------------------------------------------
529 # Background execution: an unquoted trailing & (v1).
530 # Detected here, BEFORE _split_sh_compound, so that the bare & is
531 # never mistaken for && and so that an internal & (e.g. in 2>&1 or
532 # >&2) is left untouched. Only the single & at the very end of the
533 # line is consumed. Builtins / functions / control words / variable
534 # assignments run in the FOREGROUND (the & is ignored); only external
535 # commands are launched asynchronously.
536 # ----------------------------------------------------------------
537 1744 my ($_is_bg, $_bg_line) = _split_trailing_bg($line);
538 1744 ▶ if ($_is_bg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_is_bg) 7 1737 1744
539 7 $line = $_bg_line;
540 7 my $probe = $line;
541 7 $probe =~ s/\A\s+//;
542 7 my $w0 = '';
543 7 ($w0) = ($probe =~ /\A(\S+)/);
544 7 ▶ $w0 = '' unless defined $w0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $w0 0 7 7
545 7 ▶ if (_sh_word_is_foreground($w0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_sh_word_is_foreground($w0)) 6 1 7
546 # & ignored: run the stripped line in the foreground.
547 6 return _exec_line($class, $line, $opts_ref);
548 }
549 1 my $exp = _expand($class, $line);
550 1 $exp =~ s/\A\s+//;
551 1 $exp =~ s/\s+\z//;
552 1 return _bg_launch($class, $exp);
553 }
554  
555 # Detect && / || / ; compound commands BEFORE expansion.
556 # These must be split before _expand so that short-circuit logic works.
557 1737 my @compound = _split_sh_compound($line);
558 1737 ▶ if (@compound > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@compound > 1) 61 1676 1737
559 61 return _exec_sh_compound($class, \@compound, $opts_ref);
560 }
561  
562 # set -x: trace the simple command to STDERR (the raw, pre-expansion
563 # line -- expanding a copy here would run $(...) substitutions twice).
564 1676 ▶ print STDERR '+ ', BATsh::MB::dec($line), "\n" if $_OPT_XTRACE;
Branch: 1 branch
BranchTrueFalseTotal
if $_OPT_XTRACE 1 1675 1676
565  
566 # Detect pipeline BEFORE variable expansion to avoid expanding
567 # pipe-like characters inside command substitutions prematurely.
568 # _split_sh_pipe returns >1 segment only when bare | is present.
569 1676 my @pipe_segs = _split_sh_pipe($line);
570 1676 ▶ if (@pipe_segs > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pipe_segs > 1) 26 1650 1676
571 26 return _exec_sh_pipe($class, \@pipe_segs, $opts_ref);
572 }
573  
574 # A single control/compound command (while/for/if/until/case/select or
575 # a "( subshell )") may reach here as a pipeline element or && / ||
576 # operand -- e.g. the right side of cmd | while read x; do ...; done,
577 # or true && for i in ..; do ..; done. Route it through the block
578 # runner so its inline ';'-separated body is parsed by the proper
579 # _parse_* handler instead of being split and exec'd as external words.
580 1650 ▶ if (_seg_is_control($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_seg_is_control($line)) 5 1645 1650
581 5 my @one = ($line);
582 5 return _run_lines($class, \@one, $opts_ref);
583 }
584  
585 # Array / associative-array operations (v0.06). Detected on the RAW line
586 # (before _expand) so that the "(a b c)" literal and the "[sub]" subscripts
587 # are not mangled by variable / command-substitution expansion.
588 {
589 1645 my @h = _sh_try_array_op($class, $line, $opts_ref);
590 1645 ▶ return $h[1] if @h;
Branch: 1 branch
BranchTrueFalseTotal
if @h 53 1592 1645
591 }
592  
593 # trap (v0.06). Detected on the RAW line so that the handler command is
594 # captured literally and (re-)expanded only when the trap fires, matching
595 # shell semantics for e.g. trap 'rm $tmp' EXIT.
596 {
597 1645 my $probe = $line;
598 1592 $probe =~ s/\A\s+//;
599 1592 ▶ if ($probe =~ /\Atrap(\s.*|)\z/is && $probe !~ /\Atrap\s*=/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($probe =~ /\Atrap(\s.*|)\z/is and not $probe =~ /\Atrap\s*=/) 22 1570 1592
Condition66%2/3
$probe =~ /\Atrap(\s.*|)\z/is and not $probe =~ /\Atrap\s*=/
!ll&&!rl&&r
1570022
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
600 22 return _cmd_trap($class, $1, $opts_ref);
601 }
602 }
603  
604 # POSIX assignment prefix on the RAW line: `VAR=value command args`.
605 # Detected before expansion so that a value containing $(...) or quoted
606 # spaces is not mistaken for a trailing command. Pure assignments (no
607 # command following) fall through to the post-expansion handler below.
608 {
609 1592 my ($pairs_ref, $remainder) = _sh_assign_prefix($line);
610 1570 ▶ if ($pairs_ref && defined $remainder && $remainder ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pairs_ref and defined $remainder and $remainder ne "") 5 1565 1570
Condition83%5/6
$pairs_ref and defined $remainder and $remainder ne ""
!ll&&!rl&&r
13002655
$pairs_ref and defined $remainder
!ll&&!rl&&r
13000270
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
611 5 my $ro_fail = 0;
612 5 for my $p (@{$pairs_ref}) {
613 5 my ($var, $rawval) = @{$p};
614 5 my $val = _arr_dequote(_expand($class, $rawval));
615 5 ▶ $ro_fail = 1 unless _sh_store_scalar($var, $val);
Branch: 1 branch
BranchTrueFalseTotal
unless _sh_store_scalar($var, $val) 0 5 5
616 }
617 0 ▶ if ($ro_fail) { $LAST_STATUS = 1; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($ro_fail) 0 5 5
618 5 return _exec_line($class, $remainder, $opts_ref);
619 }
620 }
621  
622 # Keep the pre-expansion text around. It is used below (echo) to
623 # decide whether filename globbing should even be attempted: that
624 # decision must be based on glob metacharacters that were literally
625 # present in the source line, never on characters that merely ended
626 # up looking like "*", "?" or "[" because a variable's *value*
627 # happened to contain them (e.g. a getopts "?"/":" result). This is
628 # the same principle the tilde-prepass above already relies on:
629 # expansion results are not themselves subject to re-expansion.
630 1565 my $_raw_pre_expand = $line;
631  
632 # Expand variables and command substitutions
633 1565 $line = _expand($class, $line);
634  
635 # Strip trailing ;
636 1565 $line =~ s/\s*;\s*\z//;
637  
638 # exec (v0.07): "exec > file" (etc.) applies its redirections
639 # permanently to the current shell (no restore); "exec cmd ..." runs
640 # cmd with those redirections and then terminates the whole script
641 # with cmd's exit status, approximating process replacement without
642 # a real fork/exec (this interpreter has no fork, by design -- see
643 # the pipeline / background-job implementation notes above).
644 1565 ▶ if ($line =~ /\Aexec(?:\s+(.*)|\s*)\z/is) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\Aexec(?:\s+(.*)|\s*)\z/is) 2 1563 1565
645 2 ▶ my $exec_rest = defined $1 ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 2 0 2
646 2 return _cmd_exec($class, $exec_rest, $opts_ref);
647 }
648  
649 # Here-string: cmd <<< word (v0.07). Detected after expansion (the
650 # word is fully variable/command/arithmetic-expanded, like any other
651 # word) and turned into an ordinary "< tempfile" stdin redirection,
652 # reusing the here-document temp-file machinery.
653 {
654 1563 my ($hs_line, $hs_content) = _sh_strip_herestring($line);
655 1563 ▶ if (defined $hs_content) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hs_content) 2 1561 1563
656 2 my $tmp = _hd_tempfile($hs_content . "\n");
657 0 ▶ if (!defined $tmp) { $LAST_STATUS = 2; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tmp) 0 2 2
658 2 my ($clean_line, $redirs_ref) = _sh_strip_redirects($hs_line);
659 2 unshift @{$redirs_ref}, [0, 0, $tmp]
660 0 ▶ unless grep { $_->[0] == 0 } @{$redirs_ref};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_->[0] == 0;} @{$redirs_ref;} 2 0 2
661 2 my $rc = _sh_exec_with_redirs($class, $clean_line, $redirs_ref, $opts_ref);
662 2 unlink $tmp;
663 2 @_HD_TMPFILES = grep { $_ ne $tmp } @_HD_TMPFILES;
664 2 return $rc;
665 }
666 }
667  
668 # Detect I/O redirections: >, >>, <, 2>, 2>>, 2>&1
669 # Must be done after expansion so that variable-in-filename works.
670 1561 my ($clean_line, $sh_redirs_ref) = _sh_strip_redirects($line);
671 1561 ▶ if (@{$sh_redirs_ref}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$sh_redirs_ref;}) 25 1536 1561
672 25 return _sh_exec_with_redirs($class, $clean_line, $sh_redirs_ref, $opts_ref);
673 }
674 1536 $line = $clean_line;
675  
676 1536 my ($cmd, $rest) = _split_sh($line);
677 1536 ▶ return 0 unless defined $cmd && $cmd ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cmd and $cmd ne "" 0 1536 1536
Condition33%1/3
defined $cmd and $cmd ne ""
!ll&&!rl&&r
001536
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
678  
679 1536 my $lc_cmd = lc($cmd);
680  
681 # Pure assignment: VAR=value (no spaces around =). Assignment prefixes
682 # of the form `VAR=value command` were already handled before expansion,
683 # so anything reaching here is a standalone assignment. Match the whole
684 # (expanded) line so values containing spaces are preserved in full.
685 1536 ▶ 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) 265 1271 1536
686 265 my ($var, $val) = ($1, $2); # capture before $1 is clobbered
687 # Dequote the value the same way command words are dequoted, so
688 # concatenated quotes (a"b"c, x'y'z) and backslash escapes resolve
689 # correctly -- not just a single outermost "..." / '...' pair.
690 265 $val = _arr_dequote($val);
691 265 my $ok = _sh_store_scalar($var, $val);
692 265 ▶ $LAST_STATUS = $ok ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 263 2 265
693 265 ▶ return $ok ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 263 2 265
694 }
695  
696 1271 ▶ if ($lc_cmd eq 'export') { return _cmd_export($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "export") 49 1222 1271
697 1222 ▶ if ($lc_cmd eq 'unset') { return _cmd_unset($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "unset") 9 1213 1222
698 1213 ▶ if ($lc_cmd eq 'echo') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "echo") 700 513 1213
699 # Apply word-splitting and glob expansion to unquoted tokens
700 # (tilde expansion already happened earlier, in _expand()).
701 #
702 # Whether to glob at all is decided from the RAW, pre-expansion
703 # source text -- NOT from the already-expanded $rest. By this
704 # point $rest has had all $VAR / ${VAR} / $(...) substitutions
705 # applied, so a variable whose *value* happens to be "?" (as
706 # getopts sets $opt on an unknown option, or ":" on a missing
707 # argument) would otherwise be indistinguishable from a literal
708 # "?" glob pattern typed in the script -- and get run through
709 # _parse_args()/glob() against the current directory, silently
710 # replacing the echoed value with whatever 1-character filename
711 # happened to match. Checking the raw text instead means only
712 # a glob metacharacter that was actually written in the source
713 # (e.g. "echo *.txt") triggers globbing.
714 700 my @raw_parts = _split_sh($_raw_pre_expand);
715 700 my $raw_rest = $raw_parts[1];
716 700 ▶ $raw_rest = '' unless defined $raw_rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw_rest 0 700 700
717 700 ▶ if (_raw_has_glob($raw_rest)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_raw_has_glob($raw_rest)) 35 665 700
718 35 my @words = _parse_args($rest, 1);
719 35 $rest = join(' ', @words);
720 }
721 700 return _cmd_echo($rest);
722 }
723 513 ▶ if ($lc_cmd eq 'printf') { return _cmd_printf($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "printf") 25 488 513
724 488 ▶ if ($lc_cmd eq 'cd') { return _cmd_cd($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "cd") 9 479 488
725 479 ▶ if ($lc_cmd eq 'pwd') { print Cwd::cwd(), "\n"; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "pwd") 6 473 479
726 473 ▶ if ($lc_cmd eq 'exit') { return _cmd_exit($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "exit") 13 460 473
727 460 ▶ if ($lc_cmd eq 'true') { $LAST_STATUS = 0; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "true") 21 439 460
728 439 ▶ if ($lc_cmd eq 'false') { $LAST_STATUS = 1; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "false") 29 410 439
729 410 ▶ if ($lc_cmd eq ':') { $LAST_STATUS = 0; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq ":") 5 405 410
730 405 ▶ if ($lc_cmd eq 'read') { return _cmd_read($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "read") 30 375 405
731 375 ▶ if ($lc_cmd eq 'test' || $cmd eq '[') { return _cmd_test($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "test" or $cmd eq "[") 77 298 375
Condition100%3/3
$lc_cmd eq "test" or $cmd eq "["
l!l&&r!l&&!r
275298
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
732 298 ▶ 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 ".") 4 294 298
Condition100%3/3
$lc_cmd eq "source" or $cmd eq "."
l!l&&r!l&&!r
31294
MC/DC100%2/2
$lc_cmd eq "source" or $cmd eq "."
$lc_cmd eq "source" $cmd eq "."
Truth table100%3/3
A or B
A= $lc_cmd eq "source"B= $cmd eq "."
ABresult
1X1
011
000
733 294 ▶ 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") 1 293 294
$rest =~ /\A\s*(\d+)/ ? : 1 0 1
734 293 ▶ if ($lc_cmd eq 'break') { $_BREAK = 1; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "break") 5 288 293
735 288 ▶ if ($lc_cmd eq 'continue') { $_CONTINUE = 1; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "continue") 1 287 288
736 287 ▶ if ($lc_cmd eq 'shift') { return _cmd_shift($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "shift") 9 278 287
737 278 ▶ if ($lc_cmd eq 'getopts') { return _cmd_getopts($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "getopts") 70 208 278
738 208 ▶ if ($lc_cmd eq 'local') { return _cmd_local($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "local") 3 205 208
739 205 ▶ if ($lc_cmd eq 'set') { return _cmd_set_sh($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "set") 27 178 205
740 178 ▶ if ($lc_cmd eq 'shopt') { return _cmd_shopt($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "shopt") 4 174 178
741 174 ▶ if ($lc_cmd eq 'alias') { return _cmd_alias($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "alias") 8 166 174
742 166 ▶ if ($lc_cmd eq 'unalias') { return _cmd_unalias($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "unalias") 1 165 166
743 165 ▶ if ($lc_cmd eq 'eval') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "eval") 2 163 165
744 # POSIX eval: the (already expanded) arguments are stripped of one
745 # level of quoting, concatenated, and executed as a new command
746 # line -- which re-parses and re-expands, giving the double
747 # expansion eval exists for. "eval" with no arguments is a noop.
748 0 ▶ 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
749 2 my @words = _parse_args($rest);
750 2 return _exec_line($class, join(' ', @words), $opts_ref);
751 }
752 163 ▶ if ($lc_cmd eq 'let') { return _cmd_let($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "let") 8 155 163
753 155 ▶ if ($lc_cmd eq 'type') { return _cmd_type($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "type") 58 97 155
754 97 ▶ if ($lc_cmd eq 'command') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "command") 7 90 97
755 # The introspection forms (-v / -V) are pure lookups; the plain
756 # run form re-dispatches the RAW (pre-expansion) remainder so it is
757 # expanded exactly once and shell functions of the same name are
758 # bypassed. Passing $_raw_pre_expand keeps expansion single-pass.
759 7 return _cmd_command($class, $rest, $_raw_pre_expand, $opts_ref);
760 }
761 90 ▶ if ($lc_cmd eq 'umask') { return _cmd_umask($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "umask") 12 78 90
762 78 ▶ if ($lc_cmd eq 'hash') { return _cmd_hash($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "hash") 3 75 78
763 75 ▶ if ($lc_cmd eq 'readonly') { return _cmd_readonly($class, $rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "readonly") 5 70 75
764 70 ▶ if ($lc_cmd eq 'mapfile' || $lc_cmd eq 'readarray') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_cmd eq "mapfile" or $lc_cmd eq "readarray") 5 65 70
Condition100%3/3
$lc_cmd eq "mapfile" or $lc_cmd eq "readarray"
l!l&&r!l&&!r
4165
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
765 5 return _cmd_mapfile($class, $rest);
766 }
767  
768 # Defined SH function (skipped while running under "command NAME ...").
769 65 ▶ 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}) 29 36 65
Condition66%2/3
not $BATsh::SH::_CMD_NO_FUNC and exists $BATsh::SH::_SH_FUNCTIONS{$cmd}
!ll&&!rl&&r
03629
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
770 29 return _call_sh_function($class, $cmd, $rest, $opts_ref);
771 }
772  
773 # Unknown: try as external (runs via Perl system)
774 36 return _cmd_external($cmd, $rest);
775 }
776  
777 # ----------------------------------------------------------------
778 # Variable / arithmetic expansion
779 # ----------------------------------------------------------------
780 # ----------------------------------------------------------------
781 # Expansion-result protection
782 # ----------------------------------------------------------------
783 # BATsh expands a line first and removes quotes from it afterwards, so
784 # a backslash arriving as the RESULT of an expansion (a variable value,
785 # command-substitution output, or a tilde expansion) used to be re-read
786 # as a shell escape by the later quote-removal stage and silently
787 # deleted. On Windows that ate every path: with HOME set to
788 # C:\home\flower the line `cd ~` tried to enter C:homeflower, and
789 # `d="C:\Users\x"; cd $d` failed in the same way.
790 # POSIX quote removal applies to the source word only, never to what an
791 # expansion produced, so a backslash coming out of an expansion is
792 # stashed here behind a NUL-delimited sentinel (a NUL can never occur
793 # in shell source) and becomes a literal backslash again in
794 # _arr_dequote() -- or in _unprotect_lit(), for the few consumers that
795 # never dequote (external command lines, here-document bodies, the
796 # variable store).
797 # ----------------------------------------------------------------
798 812 ▶sub _protect_lit {
Subroutine _protect_lit: called CC=2 SCAR=7.0
799 812 my ($v) = @_;
800 812 ▶ return '' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 812 812
801 812 $v =~ s/\\/\x00BATSH_LB\x00/g;
802 812 return $v;
803 }
804  
805 2139 ▶sub _unprotect_lit {
Subroutine _unprotect_lit: called CC=2 SCAR=7.0
806 2139 my ($v) = @_;
807 2139 ▶ return '' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 2139 2139
808 2139 $v =~ s/\x00BATSH_LB\x00/\\/g;
809 2139 return $v;
810 }
811  
812 1867 ▶sub _expand {
Subroutine _expand: called CC=42 SCAR=43.3
813 1867 my ($class, $str) = @_;
814  
815 1867 ▶ return '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 1867 1867
816  
817 # Tilde expansion (v0.07) MUST run before variable / command
818 # substitution below: POSIX expands ~word from the literal source
819 # text only, once, at the start of a word (or right after the '='
820 # of a leading NAME= assignment). A ~ that only appears because a
821 # $VAR happened to hold a string starting with "~" must NOT be
822 # re-expanded -- doing tilde expansion after $VAR substitution
823 # cannot tell the two cases apart, so it must happen first here.
824 1867 $str = _tilde_prepass($str);
825  
826 # Protect backslash escapes before any expansion. In POSIX shells the
827 # backslash inside double quotes keeps its special meaning only before
828 # $ ` " \ and newline, so "\$" is a literal dollar (NO expansion), "\`"
829 # is a literal backtick (NO command substitution) and "\\" is a literal
830 # backslash. Earlier releases performed the $-/`-substitutions globally
831 # with no escape awareness, so "\$_" expanded $_ to empty and left the
832 # stray backslash (giving e.g. perl -e "...uc(\)..." -> syntax error).
833 # We stash the escaped specials under NUL-delimited sentinels (a NUL can
834 # never occur in shell source) and restore them as literals at the end.
835 # Order matters: "\\" first so that "\\$" means backslash + expansion.
836 1867 $str =~ s/\\\\/\x00BATSH_BS\x00/g; # \\ -> literal backslash
837 1867 $str =~ s/\\\$/\x00BATSH_DL\x00/g; # \$ -> literal dollar (no expand)
838 1867 $str =~ s/\\`/\x00BATSH_BT\x00/g; # \` -> literal backtick (no subst)
839  
840 # $(( arithmetic ))
841 1867 $str =~ s/\$\(\(\s*(.*?)\s*\)\)/_eval_arith($1)/ge;
842  
843 # <(cmd) / >(cmd) process substitution (v0.07) -- MUST run before
844 # $(...) below, and before backtick substitution, so that <(...) is
845 # never mistaken for a stray "<" redirection operator by the later
846 # redirect-stripping stage.
847 1867 $str = _replace_process_subst($class, $str);
848  
849 # $( command ) substitution
850 # Use _extract_cmd_subst to correctly handle nested () and quoted ) chars.
851 1867 $str = _replace_cmd_subst($class, $str);
852  
853 # backtick command substitution: `cmd`
854 1867 $str =~ s/`([^`]*)`/_protect_lit(_cmd_subst($class, $1))/ge;
855  
856 # ---- Array expansions (v0.06) ----------------------------------
857 # These MUST precede the scalar ${#VAR} / ${VAR} rules below so that a
858 # subscripted reference is never mis-parsed as a plain variable.
859  
860 # ${#NAME[@]} / ${#NAME[*]} -- number of set elements
861 1867 $str =~ s/\$\{#([A-Za-z_][A-Za-z0-9_]*)\[[\@*]\]\}/
862 14 _arr_count($1)
863 /ge;
864  
865 # ${#NAME[SUB]} -- length of one element
866 1867 $str =~ s/\$\{#([A-Za-z_][A-Za-z0-9_]*)\[([^\]]*)\]\}/
867 3 do {
868 3 my $v = _arr_get_element($1, _arr_expand_sub($class, $2));
869 3 ▶ defined $v ? length($v) : 0
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 3 0 3
870 }
871 /ge;
872  
873 # ${!NAME[@]} / ${!NAME[*]} -- list of indices / keys
874 1867 $str =~ s/\$\{!([A-Za-z_][A-Za-z0-9_]*)\[[\@*]\]\}/
875 7 _protect_lit(join(' ', _arr_ordered_keys($1)))
876 /ge;
877  
878 # ${NAME[@]} / ${NAME[*]} -- all elements (space-joined word-split model)
879 1867 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)\[[\@*]\]\}/
880 10 _protect_lit(join(' ', _arr_values($1)))
881 /ge;
882  
883 # ${NAME[SUB]} -- single element
884 1867 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)\[([^\]]*)\]\}/
885 27 do {
886 27 my $v = _arr_get_element($1, _arr_expand_sub($class, $2));
887 27 ▶ defined $v ? _protect_lit($v) : ''
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 27 0 27
888 }
889 /ge;
890 # ----------------------------------------------------------------
891  
892 # ${#VAR} -- length of value (characters, not bytes, under DBCS guard)
893 1867 $str =~ s/\$\{#([A-Za-z_][A-Za-z0-9_]*)\}/
894 4 do { my $v = BATsh::Env->get($1);
895 4 ▶ defined $v ? BATsh::MB::mb_length($v) : 0 }
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 4 0 4
896 /ge;
897  
898 # ${VAR%%pattern} -- remove longest suffix (MUST be before single %)
899 1867 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)%%([^}]*)\}/
900 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
901 /ge;
902  
903 # ${VAR%pattern} -- remove shortest suffix (single %, not %%)
904 1867 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)%(?!%)([^}]*)\}/
905 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
906 /ge;
907  
908 # ${VAR##pattern} -- remove longest prefix (MUST be before single #)
909 1867 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)##([^}]*)\}/
910 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
911 /ge;
912  
913 # ${VAR#pattern} -- remove shortest prefix (single #, not ##)
914 1867 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)#(?!#)([^}]*)\}/
915 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
916 /ge;
917  
918 # ${VAR//pat/rep} -- replace all occurrences (MUST be before single /)
919 1867 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)\/\/([^\/}]*)\/([^}]*)\}/
920 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
921 /ge;
922  
923 # ${VAR/pat/rep} -- replace first occurrence (single /, not //)
924 1867 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)\/(?!\/)([^\/}]*)\/([^}]*)\}/
925 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
926 /ge;
927  
928 # ${VAR^^} -- uppercase all
929 1867 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)\^\^\}/
930 2 ▶ do { my $v = BATsh::Env->get($1); defined $v ? _protect_lit(uc($v)) : '' }
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 2 0 2
931 /ge;
932  
933 # ${VAR^} -- uppercase first char
934 1867 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)\^\}/
935 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
936 /ge;
937  
938 # ${VAR,,} -- lowercase all
939 1867 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*),,\}/
940 1 ▶ do { my $v = BATsh::Env->get($1); defined $v ? _protect_lit(lc($v)) : '' }
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 1 0 1
941 /ge;
942  
943 # ${VAR,} -- lowercase first char
944 1867 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*),\}/
945 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
946 /ge;
947  
948 # ${VAR:offset:length} and ${VAR:offset}
949 1867 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*):(-?\d+):(\d+)\}/
950 2 do {
951 2 ▶ my $v = BATsh::Env->get($1); $v = defined $v ? $v : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 2 0 2
952 2 _protect_lit(BATsh::MB::mb_substr($v, int($2), int($3)))
953 }
954 /ge;
955 1867 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*):(-?\d+)\}/
956 0 do {
957 0 ▶ my $v = BATsh::Env->get($1); $v = defined $v ? $v : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 0 0 0
958 0 _protect_lit(BATsh::MB::mb_substr($v, int($2)))
959 }
960 /ge;
961  
962 # ${VAR:-default} ${VAR:=default} ${VAR:+alt}
963 1867 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*):-(.*?)\}/
964 6 ▶ do { my $v = BATsh::Env->get($1); (defined $v && $v ne '') ? _protect_lit($v) : $2 }
Branch: 1 branch
BranchTrueFalseTotal
defined $v && $v ne '' ? : 0 6 6
Condition33%1/3
defined $v and $v ne ""
!ll&&!rl&&r
600
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
965 /ge;
966 1867 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*):=(.*?)\}/
967 1 do {
968 1 my $v = BATsh::Env->get($1);
969 1 ▶ if (!defined $v || $v eq '') { BATsh::Env->set($1, $2); $v = $2 }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $v or $v eq '') { } 1 0 1
Condition33%1/3
not defined $v or $v eq ""
l!l&&r!l&&!r
100
MC/DC0%0/2
not defined $v or $v eq ""
not defined $v $v eq ""
Truth table33%1/3
not(A) or B
A= not defined $vB= $v eq ""
ABresult
1X1
011
000
970 0 else { $v = _protect_lit($v) }
971 1 $v
972 }
973 /ge;
974 1867 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*):\+([^}]*)\}/
975 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
976 /ge;
977  
978 # ${VAR} -- plain expansion (array name yields element 0)
979 1867 $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/
980 71 do {
981 71 my $n = $1;
982 71 ▶ if (_arr_exists($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_arr_exists($n)) { } 0 71 71
983 0 ▶ my $v = _arr_get_element($n, 0); defined $v ? _protect_lit($v) : ''
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 0 0 0
984 }
985 else {
986 71 my $v = BATsh::Env->get($n);
987 71 ▶ _nounset_hit($n) if !defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 71 71
988 71 ▶ defined $v ? _protect_lit($v) : ''
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 71 0 71
989 }
990 }
991 /ge;
992  
993 # $? last status
994 1867 $str =~ s/\$\?/$LAST_STATUS/g;
995  
996 # $$ PID
997 1867 $str =~ s/\$\$/$$/g;
998  
999 # $! PID of the most recent background job (empty before any)
1000 1867 $str =~ s/\$\!/$_LAST_BG_PID/g;
1001  
1002  
1003 # $0 script name
1004 0 ▶ $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
1005  
1006 # $1..$9 positional parameters
1007 1867 $str =~ s/\$([1-9])/
1008 39 do {
1009 39 my $n = $1;
1010 39 my $v = BATsh::Env->get("%$n");
1011 39 ▶ $v = BATsh::Env->get("BATSH_ARG$n") unless defined $v && $v ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and $v ne "" 1 38 39
Condition66%2/3
defined $v and $v ne ""
!ll&&!rl&&r
0138
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
1012 39 ▶ defined $v ? _protect_lit($v) : ''
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 39 0 39
1013 }
1014 /ge;
1015  
1016 # $@ and $* all positional parameters (both join with a space here).
1017 # Historically only $@ was substituted; $* fell through unexpanded and
1018 # printed literally. Both now expand to the space-joined parameter
1019 # list held in %*.
1020 1867 ▶ $str =~ s/\$\@/do { my $v=BATsh::Env->get('%*'); defined $v ? _protect_lit($v) : '' }/ge;
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 1 0 1
1021 1867 ▶ $str =~ s/\$\*/do { my $v=BATsh::Env->get('%*'); defined $v ? _protect_lit($v) : '' }/ge;
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 6 0 6
1022  
1023 # $# number of positional parameters
1024 1867 $str =~ s/\$#/
1025 4 do {
1026 4 my $c = 0;
1027 4 for my $nn (1..9) {
1028 11 my $vv = BATsh::Env->get("%$nn");
1029 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
1030 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
1031 7 $c = $nn;
1032 }
1033 $c
1034 4 }
1035 /ge;
1036  
1037 # $VAR (array name yields element 0)
1038 1867 $str =~ s/\$([A-Za-z_][A-Za-z0-9_]*)/
1039 444 do {
1040 444 my $n = $1;
1041 444 ▶ if (_arr_exists($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_arr_exists($n)) { } 2 442 444
1042 2 ▶ my $v = _arr_get_element($n, 0); defined $v ? _protect_lit($v) : ''
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 2 0 2
1043 }
1044 else {
1045 442 my $v = BATsh::Env->get($n);
1046 442 ▶ _nounset_hit($n) if !defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 2 440 442
1047 442 ▶ defined $v ? _protect_lit($v) : ''
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 440 2 442
1048 }
1049 }
1050 /ge;
1051  
1052 # Restore the escaped specials as literal characters (reverse order of
1053 # protection is not required, the sentinels are disjoint).
1054 1867 $str =~ s/\x00BATSH_DL\x00/\$/g;
1055 1867 $str =~ s/\x00BATSH_BT\x00/`/g;
1056 1867 $str =~ s/\x00BATSH_BS\x00/\\/g;
1057  
1058 1867 return $str;
1059 }
1060  
1061 # ----------------------------------------------------------------
1062 # Arithmetic evaluator
1063 # ----------------------------------------------------------------
1064 64 ▶use vars qw(@_A_TOK $_A_POS $_A_ERR);
Subroutine BEGIN: called CC=1 SCAR=0.0
1065  
1066 149 ▶sub _eval_arith {
Subroutine _eval_arith: called CC=5 SCAR=16.3
1067 149 my ($expr) = @_;
1068 # Full shell-arithmetic evaluator (bash semantics, Perl 5.005_03 safe):
1069 # , = += -= *= /= %= <<= >>= &= ^= |= ?: || && | ^ & == != < <= > >=
1070 # << >> + - * / % ** (right assoc) unary ! ~ + - prefix/postfix ++ --
1071 # Comparisons and logical operators yield 1/0; / and % truncate toward
1072 # zero; assignments and ++/-- on a bare NAME write back to the variable
1073 # store. $N and $NAME are resolved to their numeric values while
1074 # tokenizing; a bare NAME stays assignable. Numbers may be decimal,
1075 # hex (0x..), or octal (0..). On any syntax error or division by zero
1076 # a warning is printed and 0 is returned (matching the old behaviour
1077 # of returning 0 on an unsupported expression).
1078 149 ▶ local @_A_TOK = _arith_tokenize(defined $expr ? $expr : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $expr ? : 149 0 149
1079 149 local $_A_POS = 0;
1080 149 local $_A_ERR = 0;
1081 149 my ($v) = _a_comma();
1082 149 ▶ $_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 149 149
Condition66%2/3
not $BATsh::SH::_A_ERR and $BATsh::SH::_A_POS < scalar @BATsh::SH::_A_TOK
!ll&&!rl&&r
21470
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
1083 149 ▶ if ($_A_ERR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BATsh::SH::_A_ERR) 2 147 149
1084 2 warn "sh: arithmetic: syntax error or division by zero: $expr\n";
1085 2 return 0;
1086 }
1087 147 return int($v);
1088 }
1089  
1090 # Tokenize into [TYPE, VALUE] pairs: TYPE 'n' number, 'v' name, 'o' operator.
1091 149 ▶sub _arith_tokenize {
Subroutine _arith_tokenize: called CC=16 SCAR=28.0
1092 149 my ($s) = @_;
1093 149 my @tok = ();
1094 149 my $i = 0;
1095 149 my $n = length($s);
1096 149 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 695 148 843
1097 695 my $c = substr($s, $i, 1);
1098 695 ▶ if ($c =~ /\s/) { $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /\s/) 228 467 695
1099 # $N positional / $NAME -- resolve to a numeric value now
1100 467 ▶ if ($c eq '$') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\$") 13 454 467
1101 13 ▶ if (substr($s, $i+1, 1) =~ /\A[1-9]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($s, $i + 1, 1) =~ /\A[1-9]\z/) 13 0 13
1102 13 push @tok, ['n', _arith_pos(substr($s, $i+1, 1))];
1103 13 $i += 2; next;
1104 }
1105 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
1106 0 push @tok, ['n', _arith_var($1)];
1107 0 $i += 1 + length($1); next;
1108 }
1109 0 return (['o', '?ERR?']); # lone $: force a syntax error
1110 }
1111 # Numbers: hex 0x.., octal 0.., decimal
1112 454 ▶ if ($c =~ /\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /\d/) 168 286 454
1113 168 ▶ 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 167 168
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
16701
$c eq 0 and substr($s, $i + 1, 1) =~ /\A[xX]\z/
!ll&&!rl&&r
16071
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
1114 && substr($s, $i+2) =~ /\A([0-9A-Fa-f]+)/) {
1115 1 push @tok, ['n', hex($1)];
1116 1 $i += 2 + length($1); next;
1117 }
1118 167 substr($s, $i) =~ /\A(\d+)/;
1119 167 my $num = $1;
1120 167 ▶ if ($num =~ /\A0[0-7]+\z/) { push @tok, ['n', oct($num)] }
Branch: 1 branch
BranchTrueFalseTotal
if ($num =~ /\A0[0-7]+\z/) { } 1 166 167
1121 166 else { push @tok, ['n', int($num)] }
1122 167 $i += length($num); next;
1123 }
1124 # Bare NAME (assignable lvalue)
1125 286 ▶ if ($c =~ /[A-Za-z_]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /[A-Za-z_]/) 120 166 286
1126 120 substr($s, $i) =~ /\A([A-Za-z_][A-Za-z0-9_]*)/;
1127 120 push @tok, ['v', $1];
1128 120 $i += length($1); next;
1129 }
1130 # Operators: longest match first
1131 166 my $three = substr($s, $i, 3);
1132 166 ▶ if ($three eq '<<=' || $three eq '>>=') {
Branch: 1 branch
BranchTrueFalseTotal
if ($three eq "<<=" or $three eq ">>=") 1 165 166
Condition66%2/3
$three eq "<<=" or $three eq ">>="
l!l&&r!l&&!r
10165
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
1133 1 push @tok, ['o', $three]; $i += 3; next;
1134 }
1135 165 my $two = substr($s, $i, 2);
1136 165 ▶ if ($two =~ /\A(?:\*\*|<<|>>|<=|>=|==|!=|&&|\|\||\+=|-=|\*=|\/=|%=|&=|\^=|\|=|\+\+|--)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($two =~ m[\A(?:\*\*|<<|>>|<=|>=|==|!=|&&|\|\||\+=|-=|\*=|/=|%=|&=|\^=|\|=|\+\+|--)\z]) 16 149 165
1137 16 push @tok, ['o', $two]; $i += 2; next;
1138 }
1139 149 ▶ if ($c =~ /\A[-+*\/%()<>=!&|^~?:,]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ m[\A[-+*/%()<>=!&|^~?:,]\z]) 148 1 149
1140 148 push @tok, ['o', $c]; $i++; next;
1141 }
1142 1 return (['o', '?ERR?']); # unknown character: force a syntax error
1143 }
1144 148 return @tok;
1145 }
1146  
1147 6402 ▶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 ? : 3279 3123 6402
1148 5946 ▶sub _a_isop {
Subroutine _a_isop: called CC=3 SCAR=11.0
1149 5946 my ($op) = @_;
1150 5946 my $t = _a_peek();
1151 5946 ▶ 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
4845949152
defined $t and $t->[0] eq "o"
!ll&&!rl&&r
312317221101
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
1152 }
1153 465 ▶sub _a_next { return $_A_TOK[$_A_POS++] }
Subroutine _a_next: called CC=1 SCAR=0.0
1154  
1155 # Each _a_* returns (value, lvalue_name_or_undef).
1156  
1157 151 ▶sub _a_comma {
Subroutine _a_comma: called CC=2 SCAR=6.9
1158 151 my ($v, $lv) = _a_assign();
1159 151 ▶ while (_a_isop(',')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop(",")) 1 151 152
1160 1 _a_next();
1161 1 ($v, $lv) = _a_assign();
1162 }
1163 151 return ($v, undef);
1164 }
1165  
1166 164 ▶sub _a_assign {
Subroutine _a_assign: called CC=18 SCAR=38.6
1167 # Lookahead: NAME followed by an assignment operator
1168 164 my $t = _a_peek();
1169 164 ▶ 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) 108 56 164
Condition66%4/6
defined $t and $t->[0] eq "v" and $BATsh::SH::_A_POS + 1 < @BATsh::SH::_A_TOK
!ll&&!rl&&r
560108
defined $t and $t->[0] eq "v"
!ll&&!rl&&r
056108
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
1170 108 my $op = $_A_TOK[$_A_POS + 1];
1171 108 ▶ 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 97 108
Condition66%2/3
$op->[0] eq "o" and $op->[1] =~ m[\A(?:=|\+=|-=|\*=|/=|%=|<<=|>>=|&=|\^=|\|=)\z]
!ll&&!rl&&r
09711
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
1172 11 my $name = $t->[1];
1173 11 _a_next(); _a_next();
1174 11 my ($rhs) = _a_assign();
1175 11 my $val;
1176 11 ▶ if ($op->[1] eq '=') { $val = $rhs }
Branch: 1 branch
BranchTrueFalseTotal
if ($op->[1] eq '=') { } 8 3 11
1177 else {
1178 3 my $cur = _arith_var($name);
1179 3 my $o = $op->[1];
1180 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
1181 0 elsif ($o eq '-=') { $val = $cur - $rhs }
1182 0 elsif ($o eq '*=') { $val = $cur * $rhs }
1183 0 ▶ elsif ($o eq '/=') { if ($rhs == 0) { $_A_ERR = 1; return (0, undef) }
Branch: 1 branch
BranchTrueFalseTotal
if ($rhs == 0) 0 0 0
1184 0 $val = int($cur / $rhs) }
1185 0 ▶ elsif ($o eq '%=') { if ($rhs == 0) { $_A_ERR = 1; return (0, undef) }
Branch: 1 branch
BranchTrueFalseTotal
if ($rhs == 0) 0 0 0
1186 0 $val = $cur % $rhs }
1187 1 elsif ($o eq '<<=') { $val = $cur << $rhs }
1188 0 elsif ($o eq '>>=') { $val = $cur >> $rhs }
1189 0 elsif ($o eq '&=') { $val = $cur & $rhs }
1190 0 elsif ($o eq '^=') { $val = $cur ^ $rhs }
1191 0 else { $val = $cur | $rhs }
1192 }
1193 11 $val = int($val);
1194 11 BATsh::Env->set($name, $val);
1195 11 return ($val, undef);
1196 }
1197 }
1198 153 return _a_ternary();
1199 }
1200  
1201 154 ▶sub _a_ternary {
Subroutine _a_ternary: called CC=4 SCAR=14.4
1202 154 my ($c) = _a_lor();
1203 154 ▶ if (_a_isop('?')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("?")) 1 153 154
1204 1 _a_next();
1205 1 my ($tv) = _a_assign();
1206 0 ▶ if (!_a_isop(':')) { $_A_ERR = 1; return (0, undef) }
Branch: 1 branch
BranchTrueFalseTotal
unless (_a_isop(":")) 0 1 1
1207 1 _a_next();
1208 1 my ($fv) = _a_ternary();
1209 1 ▶ return (($c != 0) ? $tv : $fv, undef);
Branch: 1 branch
BranchTrueFalseTotal
$c != 0 ? : 1 0 1
1210 }
1211 153 return ($c, undef);
1212 }
1213  
1214 154 ▶sub _a_lor {
Subroutine _a_lor: called CC=4 SCAR=14.3
1215 154 my ($v) = _a_land();
1216 154 ▶ while (_a_isop('||')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("||")) 1 154 155
1217 1 _a_next();
1218 1 my ($r) = _a_land();
1219 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
1220 }
1221 154 return ($v, undef);
1222 }
1223  
1224 155 ▶sub _a_land {
Subroutine _a_land: called CC=4 SCAR=14.3
1225 155 my ($v) = _a_bor();
1226 155 ▶ while (_a_isop('&&')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("&&")) 1 155 156
1227 1 _a_next();
1228 1 my ($r) = _a_bor();
1229 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
1230 }
1231 155 return ($v, undef);
1232 }
1233  
1234 156 ▶sub _a_bor {
Subroutine _a_bor: called CC=2 SCAR=6.9
1235 156 my ($v) = _a_bxor();
1236 156 ▶ while (_a_isop('|')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("|")) 1 156 157
1237 1 _a_next();
1238 1 my ($r) = _a_bxor();
1239 1 $v = $v | $r;
1240 }
1241 156 return ($v, undef);
1242 }
1243  
1244 157 ▶sub _a_bxor {
Subroutine _a_bxor: called CC=2 SCAR=6.9
1245 157 my ($v) = _a_band();
1246 157 ▶ while (_a_isop('^')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("^")) 1 157 158
1247 1 _a_next();
1248 1 my ($r) = _a_band();
1249 1 $v = $v ^ $r;
1250 }
1251 157 return ($v, undef);
1252 }
1253  
1254 158 ▶sub _a_band {
Subroutine _a_band: called CC=2 SCAR=6.9
1255 158 my ($v) = _a_eqne();
1256 158 ▶ while (_a_isop('&')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("&")) 1 158 159
1257 1 _a_next();
1258 1 my ($r) = _a_eqne();
1259 1 $v = $v & $r;
1260 }
1261 158 return ($v, undef);
1262 }
1263  
1264 159 ▶sub _a_eqne {
Subroutine _a_eqne: called CC=6 SCAR=19.3
1265 159 my ($v) = _a_rel();
1266 159 ▶ while (_a_isop('==') || _a_isop('!=')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("==") or _a_isop("!=")) 1 159 160
Condition66%2/3
_a_isop("==") or _a_isop("!=")
l!l&&r!l&&!r
01159
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
1267 1 my $op = _a_next()->[1];
1268 1 my ($r) = _a_rel();
1269 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
1270 }
1271 159 return ($v, undef);
1272 }
1273  
1274 160 ▶sub _a_rel {
Subroutine _a_rel: called CC=12 SCAR=25.8
1275 160 my ($v) = _a_shiftop();
1276 160 ▶ 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(">=")) 5 160 165
Condition88%8/9
_a_isop("<") or _a_isop("<=") or _a_isop(">") or _a_isop(">=")
l!l&&r!l&&!r
41160
_a_isop("<") or _a_isop("<=") or _a_isop(">")
l!l&&r!l&&!r
22161
_a_isop("<") or _a_isop("<=")
l!l&&r!l&&!r
20163
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
1277 5 my $op = _a_next()->[1];
1278 5 my ($r) = _a_shiftop();
1279 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
1280 0 ▶ elsif ($op eq '<=') { $v = ($v <= $r) ? 1 : 0 }
Branch: 1 branch
BranchTrueFalseTotal
$v <= $r ? : 0 0 0
1281 2 ▶ elsif ($op eq '>') { $v = ($v > $r) ? 1 : 0 }
Branch: 1 branch
BranchTrueFalseTotal
$v > $r ? : 2 0 2
1282 1 ▶ else { $v = ($v >= $r) ? 1 : 0 }
Branch: 1 branch
BranchTrueFalseTotal
$v >= $r ? : 1 0 1
1283 }
1284 160 return ($v, undef);
1285 }
1286  
1287 165 ▶sub _a_shiftop {
Subroutine _a_shiftop: called CC=4 SCAR=13.9
1288 165 my ($v) = _a_add();
1289 165 ▶ while (_a_isop('<<') || _a_isop('>>')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("<<") or _a_isop(">>")) 2 165 167
Condition100%3/3
_a_isop("<<") or _a_isop(">>")
l!l&&r!l&&!r
11165
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
1290 2 my $op = _a_next()->[1];
1291 2 my ($r) = _a_add();
1292 2 ▶ $v = ($op eq '<<') ? ($v << $r) : ($v >> $r);
Branch: 1 branch
BranchTrueFalseTotal
$op eq '<<' ? : 1 1 2
1293 }
1294 165 return ($v, undef);
1295 }
1296  
1297 167 ▶sub _a_add {
Subroutine _a_add: called CC=4 SCAR=13.9
1298 167 my ($v) = _a_mul();
1299 167 ▶ while (_a_isop('+') || _a_isop('-')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("+") or _a_isop("-")) 104 167 271
Condition100%3/3
_a_isop("+") or _a_isop("-")
l!l&&r!l&&!r
968167
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
1300 104 my $op = _a_next()->[1];
1301 104 my ($r) = _a_mul();
1302 104 ▶ $v = ($op eq '+') ? ($v + $r) : ($v - $r);
Branch: 1 branch
BranchTrueFalseTotal
$op eq '+' ? : 96 8 104
1303 }
1304 167 return ($v, undef);
1305 }
1306  
1307 271 ▶sub _a_mul {
Subroutine _a_mul: called CC=10 SCAR=23.0
1308 271 my ($v) = _a_pow();
1309 271 ▶ while (_a_isop('*') || _a_isop('/') || _a_isop('%')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("*") or _a_isop("/") or _a_isop("%")) 18 270 288
Condition100%6/6
_a_isop("*") or _a_isop("/") or _a_isop("%")
l!l&&r!l&&!r
162270
_a_isop("*") or _a_isop("/")
l!l&&r!l&&!r
133272
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
1310 18 my $op = _a_next()->[1];
1311 18 my ($r) = _a_pow();
1312 18 ▶ if ($op eq '*') { $v = $v * $r }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq '*') { } 13 5 18
1313 else {
1314 5 ▶ if ($r == 0) { $_A_ERR = 1; return (0, undef) }
Branch: 1 branch
BranchTrueFalseTotal
if ($r == 0) 1 4 5
1315 # bash / and % truncate toward zero
1316 4 ▶ if ($op eq '/') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq '/') { } 2 2 4
1317 2 my $q = abs(int($v)) - (abs(int($v)) % abs(int($r)));
1318 2 $q /= abs(int($r));
1319 2 ▶ $q = -$q if (($v < 0) ne ($r < 0)) && $q != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $v < 0 ne $r < 0 and $q != 0 1 1 2
Condition66%2/3
$v < 0 ne $r < 0 and $q != 0
!ll&&!rl&&r
101
MC/DC50%1/2
$v < 0 ne $r < 0 and $q != 0
$v < 0 ne $r < 0 $q != 0
Truth table66%2/3
A and B
A= $v < 0 ne $r < 0B= $q != 0
ABresult
0X0
100
111
1320 2 $v = $q;
1321 }
1322 else {
1323 2 my $m = abs(int($v)) % abs(int($r));
1324 2 ▶ $m = -$m if $v < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $v < 0 1 1 2
1325 2 $v = $m;
1326 }
1327 }
1328 }
1329 270 return ($v, undef);
1330 }
1331  
1332 292 ▶sub _a_pow {
Subroutine _a_pow: called CC=2 SCAR=6.9
1333 292 my ($v, $lv) = _a_unary();
1334 292 ▶ if (_a_isop('**')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("**")) 3 289 292
1335 3 _a_next();
1336 3 my ($r) = _a_pow(); # right associative
1337 3 return (int($v ** $r), undef);
1338 }
1339 289 return ($v, $lv);
1340 }
1341  
1342 296 ▶sub _a_unary {
Subroutine _a_unary: called CC=11 SCAR=24.9
1343 296 ▶ if (_a_isop('!')) { _a_next(); my ($v) = _a_unary(); return (($v == 0) ? 1 : 0, undef) }
Branch: 2 branches
BranchTrueFalseTotal
if (_a_isop("!")) 1 295 296
$v == 0 ? : 1 0 1
1344 295 ▶ if (_a_isop('~')) { _a_next(); my ($v) = _a_unary(); return (-int($v) - 1, undef) }
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("~")) 1 294 295
1345 0 ▶ if (_a_isop('+')) { _a_next(); my ($v) = _a_unary(); return (+$v, undef) }
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("+")) 0 294 294
1346 294 ▶ if (_a_isop('-')) { _a_next(); my ($v) = _a_unary(); return (-$v, undef) }
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("-")) 2 292 294
1347 # Prefix ++NAME / --NAME
1348 292 ▶ if (_a_isop('++') || _a_isop('--')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_a_isop("++") or _a_isop("--")) 2 290 292
Condition100%3/3
_a_isop("++") or _a_isop("--")
l!l&&r!l&&!r
11290
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
1349 2 my $op = _a_next()->[1];
1350 2 my $t = _a_peek();
1351 0 ▶ 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
1352 2 _a_next();
1353 2 ▶ my $val = _arith_var($t->[1]) + (($op eq '++') ? 1 : -1);
Branch: 1 branch
BranchTrueFalseTotal
$op eq '++' ? : 1 1 2
1354 2 BATsh::Env->set($t->[1], $val);
1355 2 return ($val, undef);
1356 }
1357 290 return _a_postfix();
1358 }
1359  
1360 290 ▶sub _a_postfix {
Subroutine _a_postfix: called CC=5 SCAR=16.1
1361 290 my ($v, $lv) = _a_primary();
1362 290 ▶ if (defined $lv && (_a_isop('++') || _a_isop('--'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lv and _a_isop("++") || _a_isop("--")) 3 287 290
Condition100%6/6
defined $lv and _a_isop("++") || _a_isop("--")
!ll&&!rl&&r
1831043
_a_isop("++") or _a_isop("--")
l!l&&r!l&&!r
21104
MC/DC100%3/3
defined $lv and _a_isop("++") || _a_isop("--")
defined $lv _a_isop("++") _a_isop("--")
Truth table100%4/4
A and B or C
A= defined $lvB= _a_isop("++")C= _a_isop("--")
ABCresult
0XX0
1000
11X1
1011
1363 3 my $op = _a_next()->[1];
1364 3 ▶ BATsh::Env->set($lv, $v + (($op eq '++') ? 1 : -1));
Branch: 1 branch
BranchTrueFalseTotal
$op eq '++' ? : 2 1 3
1365 3 return ($v, undef); # postfix returns the OLD value
1366 }
1367 287 return ($v, $lv);
1368 }
1369  
1370 290 ▶sub _a_primary {
Subroutine _a_primary: called CC=7 SCAR=19.9
1371 290 my $t = _a_peek();
1372 0 ▶ if (!defined $t) { $_A_ERR = 1; return (0, undef) }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $t) 0 290 290
1373 290 ▶ if ($t->[0] eq 'o' && $t->[1] eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->[0] eq "o" and $t->[1] eq "(") 2 288 290
Condition100%3/3
$t->[0] eq "o" and $t->[1] eq "("
!ll&&!rl&&r
28712
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
1374 2 _a_next();
1375 2 my ($v) = _a_comma();
1376 0 ▶ if (!_a_isop(')')) { $_A_ERR = 1; return (0, undef) }
Branch: 1 branch
BranchTrueFalseTotal
unless (_a_isop(")")) 0 2 2
1377 2 _a_next();
1378 2 return ($v, undef);
1379 }
1380 288 ▶ if ($t->[0] eq 'n') { _a_next(); return ($t->[1], undef) }
Branch: 1 branch
BranchTrueFalseTotal
if ($t->[0] eq "n") 180 108 288
1381 108 ▶ if ($t->[0] eq 'v') { _a_next(); return (_arith_var($t->[1]), $t->[1]) }
Branch: 1 branch
BranchTrueFalseTotal
if ($t->[0] eq "v") 107 1 108
1382 1 $_A_ERR = 1;
1383 1 return (0, undef);
1384 }
1385  
1386 13 ▶sub _arith_pos {
Subroutine _arith_pos: called CC=5 SCAR=19.4
1387 13 my ($n) = @_;
1388 13 my $v = BATsh::Env->get("%$n");
1389 13 ▶ $v = BATsh::Env->get("BATSH_ARG$n") unless defined $v && $v ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and $v ne "" 0 13 13
Condition33%1/3
defined $v and $v ne ""
!ll&&!rl&&r
0013
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
1390 13 ▶ return (defined $v && $v =~ /\A-?\d+\z/) ? $v : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $v && $v =~ /\A-?\d+\z/ ? : 13 0 13
Condition33%1/3
defined $v and $v =~ /\A-?\d+\z/
!ll&&!rl&&r
0013
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
1391 }
1392  
1393 112 ▶sub _arith_var {
Subroutine _arith_var: called CC=3 SCAR=12.5
1394 112 my ($name) = @_;
1395 112 my $v = BATsh::Env->get($name);
1396 112 ▶ return (defined $v && $v =~ /\A-?\d+\z/) ? $v : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $v && $v =~ /\A-?\d+\z/ ? : 112 0 112
Condition33%1/3
defined $v and $v =~ /\A-?\d+\z/
!ll&&!rl&&r
00112
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
1397 }
1398  
1399 # ----------------------------------------------------------------
1400 # Command substitution $( cmd )
1401 # ----------------------------------------------------------------
1402 # _replace_cmd_subst: replace all $(...) in $str with their output.
1403 # Unlike a simple [^)]* regex, this function tracks nesting depth
1404 # and quoted strings so that $(cmd | perl -e "...)" works correctly.
1405 # ----------------------------------------------------------------
1406 1867 ▶sub _replace_cmd_subst {
Subroutine _replace_cmd_subst: called CC=22 SCAR=32.3
1407 1867 my ($class, $str) = @_;
1408 1867 ▶ return '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 1867 1867
1409  
1410 1867 my $result = '';
1411 1867 my @chars = split //, $str;
1412 1867 my $n = scalar @chars;
1413 1867 my $i = 0;
1414  
1415 1867 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 24120 1867 25987
1416 24120 my $ch = $chars[$i];
1417  
1418 # $( ... ) -- find matching close paren respecting nesting and quotes
1419 24120 ▶ 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 "(") 25 24095 24120
Condition83%5/6
$ch eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "("
!ll&&!rl&&r
2341967625
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
234190701
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
1420 25 $i += 2; # skip $(
1421 25 my $depth = 1;
1422 25 my $body = '';
1423 25 my $in_sq = 0;
1424 25 my $in_dq = 0;
1425  
1426 25 ▶ while ($i < $n && $depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n and $depth > 0) 679 0 679
Condition33%1/3
$i < $n and $depth > 0
!ll&&!rl&&r
00679
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
1427 679 my $c = $chars[$i];
1428  
1429 679 ▶ if ($in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq) 4 675 679
1430 4 ▶ if ($c eq "'") { $in_sq = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "'") 1 3 4
1431 4 $body .= $c; $i++; next;
1432 }
1433 675 ▶ if ($c eq "'" && !$in_dq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "'" and not $in_dq) 1 674 675
Condition100%3/3
$c eq "'" and not $in_dq
!ll&&!rl&&r
67221
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
1434 1 $in_sq = 1; $body .= $c; $i++; next;
1435 }
1436 674 ▶ if ($c eq '"' && !$in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\"" and not $in_sq) 36 638 674
Condition66%2/3
$c eq "\"" and not $in_sq
!ll&&!rl&&r
638036
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
1437 36 $in_dq = !$in_dq; $body .= $c; $i++; next;
1438 }
1439 638 ▶ if ($in_dq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_dq) 260 378 638
1440 260 ▶ if ($c eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\\") 0 260 260
1441 0 $body .= $c; $i++;
1442 0 ▶ $body .= $chars[$i] if $i < $n; $i++; next;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $n 0 0 0
1443 }
1444 260 $body .= $c; $i++; next;
1445 }
1446 378 ▶ if ($c eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\\") 0 378 378
1447 0 $body .= $c; $i++;
1448 0 ▶ $body .= $chars[$i] if $i < $n; $i++; next;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $n 0 0 0
1449 }
1450 378 ▶ if ($c eq '(') { $depth++; $body .= $c; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "(") 6 372 378
1451 372 ▶ if ($c eq ')') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq ")") 31 341 372
1452 31 $depth--;
1453 31 ▶ if ($depth == 0) { $i++; last } # closing )
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 0) 25 6 31
1454 6 $body .= $c; $i++; next;
1455 }
1456 341 $body .= $c; $i++;
1457 }
1458  
1459 25 $result .= _protect_lit(_cmd_subst($class, $body));
1460 25 next;
1461 }
1462  
1463 24095 $result .= $ch; $i++;
1464 }
1465  
1466 1867 return $result;
1467 }
1468  
1469 # ----------------------------------------------------------------
1470 26 ▶sub _cmd_subst {
Subroutine _cmd_subst: called CC=6 SCAR=19.4
1471 26 my ($class, $cmd_str) = @_;
1472 # Capture stdout via temporary file (Perl 5.005_03 compatible).
1473 # We use _run_lines so that all BATsh::SH builtins, functions,
1474 # and pipelines work recursively inside $(...) and `...`.
1475 # Tag the capture file with the active nesting depth so a nested
1476 # $( ... $( ... ) ) gets a distinct file per level (the inner level
1477 # must not truncate/unlink the file the outer level captures into).
1478 26 local $_SUBST_DEPTH = $_SUBST_DEPTH + 1;
1479 26 local *_SUBST_SAVOUT;
1480 26 ▶ open(_SUBST_SAVOUT, '>&STDOUT') or return '';
Branch: 1 branch
BranchTrueFalseTotal
open _SUBST_SAVOUT, ">&STDOUT" or return "" 26 0 26
1481 26 local *_SUBST_CAPFH;
1482 26 my $tmpfile = _subst_tempfile();
1483 26 ▶ if (!defined $tmpfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tmpfile) 0 26 26
1484 0 open(STDOUT, '>&_SUBST_SAVOUT'); close(_SUBST_SAVOUT); return '';
1485 }
1486 open(STDOUT, '>&_SUBST_CAPFH')
1487 26 ▶ 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 "" } 26 0 26
1488 0 close(_SUBST_CAPFH); unlink $tmpfile;
1489 0 open(STDOUT, '>&_SUBST_SAVOUT'); close(_SUBST_SAVOUT); return '';
1490 };
1491 26 close(_SUBST_CAPFH);
1492 26 eval {
1493 # Use _run_lines for full recursive BATsh::SH execution.
1494 # $cmd_str may contain pipes, builtins, functions, etc.
1495 26 my @sub_lines = split /\n/, $cmd_str;
1496 26 _run_lines($class, \@sub_lines, {});
1497 };
1498 26 open(STDOUT, '>&_SUBST_SAVOUT');
1499 26 close(_SUBST_SAVOUT);
1500 26 my $output = '';
1501 26 local *_SUBST_READFH;
1502 26 ▶ if (open(_SUBST_READFH, "< $tmpfile")) {
Branch: 1 branch
BranchTrueFalseTotal
if (open _SUBST_READFH, "< $tmpfile") 26 0 26
1503 26 local $/;
1504 26 $output = <_SUBST_READFH>;
1505 26 close(_SUBST_READFH);
1506 }
1507 26 unlink $tmpfile;
1508 26 @_SUBST_TMPFILES = grep { $_ ne $tmpfile } @_SUBST_TMPFILES;
1509 26 ▶ $output = '' unless defined $output;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $output 0 26 26
1510 26 $output =~ s/\n+\z//; # strip trailing newlines (like shell)
1511 26 return BATsh::MB::enc($output);
1512 }
1513  
1514 # _subst_tempfile: create a unique, empty temp file (O_CREAT|O_EXCL to
1515 # avoid symlink races -- mirrors _bg_tempfile / _hd_tempfile) for capturing
1516 # one command-substitution's stdout output, and open it as the package
1517 # bareword filehandle _SUBST_CAPFH. Returns the path, or undef on failure.
1518 27 ▶sub _subst_tempfile {
Subroutine _subst_tempfile: called CC=5 SCAR=16.8
1519 27 my $dir = File::Spec->tmpdir();
1520 27 ▶ $dir = '.' if !(-d $dir && -w $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir and -w $dir 0 27 27
Condition33%1/3
-d $dir and -w $dir
!ll&&!rl&&r
0027
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
1521  
1522 27 my $attempt = 0;
1523 27 ▶ while ($attempt < 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attempt < 1000) 28 0 28
1524 28 $_SUBST_SEQ++;
1525 28 $attempt++;
1526 28 my $path = File::Spec->catfile($dir,
1527 'batsh_cap_' . $$ . '_' . $_SUBST_DEPTH . '_' . $_SUBST_SEQ . '.tmp');
1528 28 ▶ if (sysopen(_SUBST_CAPFH, $path, O_WRONLY | O_CREAT | O_EXCL, 0600)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysopen _SUBST_CAPFH, $path, 193, 384) 27 1 28
1529 27 push @_SUBST_TMPFILES, $path;
1530 27 return $path;
1531 }
1532 # EEXIST or transient error: retry with next sequence number
1533 }
1534 0 return undef;
1535 }
1536  
1537 # ----------------------------------------------------------------
1538 # Process substitution (v0.07): <(cmd) and >(cmd).
1539 #
1540 # This interpreter never forks (see the pipeline / background-job notes
1541 # elsewhere in this file), so neither form uses a real named pipe.
1542 # Instead:
1543 #
1544 # <(cmd) cmd's stdout is captured into a temp file (exactly like
1545 # $(cmd), but the file is kept instead of being read back
1546 # into a scalar) and <(cmd) is replaced by that file's path.
1547 # This covers the common case of feeding a whole command's
1548 # output to something that wants a filename, e.g.
1549 # "diff <(sort a) <(sort b)".
1550 #
1551 # >(cmd) an empty temp file is created and >(cmd) is replaced by its
1552 # path immediately; cmd itself is deferred and run with that
1553 # file as its stdin only after the current simple command
1554 # finishes (see the _exec_line wrapper), e.g.
1555 # "generate | tee >(gzip > out.gz)". Because cmd runs after
1556 # (not concurrently with) the writer, this is a best-effort
1557 # approximation of real, streaming >(...) and does not suit
1558 # writers that expect the reader to keep up in real time.
1559 #
1560 # Both temp files are removed by the _exec_line wrapper once the current
1561 # simple command (and, for >(cmd), its deferred job) has finished; a
1562 # process substitution used INSIDE another command substitution / loop
1563 # condition is cleaned up at that inner level, not held open for the
1564 # rest of the script.
1565 # ----------------------------------------------------------------
1566 1867 ▶sub _replace_process_subst {
Subroutine _replace_process_subst: called CC=27 SCAR=42.1
1567 1867 my ($class, $str) = @_;
1568 1867 ▶ return $str unless defined $str && $str =~ /[<>]\(/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and $str =~ /[<>]\(/ 1865 2 1867
Condition66%2/3
defined $str and $str =~ /[<>]\(/
!ll&&!rl&&r
018652
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
1569  
1570 2 my $result = '';
1571 2 my @chars = split //, $str;
1572 2 my $n = scalar @chars;
1573 2 my $i = 0;
1574 2 my $in_sq = 0;
1575 2 my $in_dq = 0;
1576  
1577 2 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 27 2 29
1578 27 my $ch = $chars[$i];
1579  
1580 27 ▶ if ($in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq) 0 27 27
1581 0 ▶ if ($ch eq "'") { $in_sq = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'") 0 0 0
1582 0 $result .= $ch; $i++; next;
1583 }
1584 0 ▶ 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
1585 0 ▶ 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
1586 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
1587 0 $result .= $ch; $i++;
1588 0 ▶ $result .= $chars[$i] if $i < $n; $i++; next;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $n 0 0 0
1589 }
1590  
1591 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
1592 && $i+1 < $n && $chars[$i+1] eq '(') {
1593 2 my $dir = $ch;
1594 2 my $depth = 1;
1595 2 my $j = $i + 2;
1596 2 my $body = '';
1597 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
1598 37 my $c = $chars[$j];
1599 0 ▶ if ($c eq '(') { $depth++; $body .= $c; $j++ }
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq '(') { } 0 37 37
elsif ($c eq ')') { } 2 35 37
1600 2 ▶ elsif ($c eq ')') { $depth--; $j++; $body .= $c if $depth > 0 }
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 0 0 2 2
1601 35 else { $body .= $c; $j++ }
1602 }
1603 0 ▶ if ($depth != 0) { $result .= $ch; $i++; next } # unterminated: literal '<'/'>'
Branch: 1 branch
BranchTrueFalseTotal
if ($depth != 0) 0 2 2
1604  
1605 2 ▶ if ($dir eq '<') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq '<') { } 1 1 2
1606 # Quote the generated path: it is a literal filename that
1607 # must survive the dequoting applied later to redirection
1608 # targets and command words -- otherwise a Windows temp
1609 # path (C:\...\batsh_ps.tmp) would lose its backslashes,
1610 # or a space in the temp dir would split the word.
1611 1 $result .= '"' . _procsub_capture($class, $body) . '"';
1612 }
1613 else {
1614 1 my $tmp = _procsub_tempfile();
1615 1 ▶ if (defined $tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tmp) 1 0 1
1616 1 close(_PROCSUB_CAPFH);
1617 1 push @_PROCSUB_DEFERRED, [$tmp, $body];
1618 1 $result .= '"' . $tmp . '"';
1619 }
1620 }
1621 2 $i = $j; next;
1622 }
1623  
1624 25 $result .= $ch; $i++;
1625 }
1626  
1627 2 return $result;
1628 }
1629  
1630 # _procsub_capture: run $cmd_str now, capturing its stdout into a fresh
1631 # temp file kept on disk, and return the file's path. Mirrors
1632 # _cmd_subst() but keeps the file instead of reading it back.
1633 1 ▶sub _procsub_capture {
Subroutine _procsub_capture: called CC=4 SCAR=16.9
1634 1 my ($class, $cmd_str) = @_;
1635 1 local *_PROCSUB_SAVOUT;
1636 1 ▶ open(_PROCSUB_SAVOUT, '>&STDOUT') or return '';
Branch: 1 branch
BranchTrueFalseTotal
open _PROCSUB_SAVOUT, ">&STDOUT" or return "" 1 0 1
1637 1 my $tmpfile = _procsub_tempfile();
1638 1 ▶ if (!defined $tmpfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tmpfile) 0 1 1
1639 0 open(STDOUT, '>&_PROCSUB_SAVOUT'); close(_PROCSUB_SAVOUT); return '';
1640 }
1641 open(STDOUT, '>&_PROCSUB_CAPFH')
1642 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 "" } 1 0 1
1643 0 close(_PROCSUB_CAPFH); unlink $tmpfile;
1644 0 @_PROCSUB_TMPFILES = grep { $_ ne $tmpfile } @_PROCSUB_TMPFILES;
1645 0 open(STDOUT, '>&_PROCSUB_SAVOUT'); close(_PROCSUB_SAVOUT); return '';
1646 };
1647 1 close(_PROCSUB_CAPFH);
1648 1 eval {
1649 1 my @sub_lines = split /\n/, $cmd_str;
1650 1 _run_lines($class, \@sub_lines, {});
1651 };
1652 1 open(STDOUT, '>&_PROCSUB_SAVOUT');
1653 1 close(_PROCSUB_SAVOUT);
1654 1 return $tmpfile;
1655 }
1656  
1657 # _procsub_tempfile: create a unique, empty temp file (O_CREAT|O_EXCL to
1658 # avoid symlink races, mirroring _subst_tempfile()) and open it as the
1659 # package bareword filehandle _PROCSUB_CAPFH. Returns the path, or
1660 # undef on failure. The path is tracked in @_PROCSUB_TMPFILES for the
1661 # _exec_line wrapper (normal cleanup) and the END-block failsafe.
1662 2 ▶sub _procsub_tempfile {
Subroutine _procsub_tempfile: called CC=5 SCAR=17.4
1663 2 my $dir = File::Spec->tmpdir();
1664 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
1665  
1666 2 my $attempt = 0;
1667 2 ▶ while ($attempt < 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attempt < 1000) 2 0 2
1668 2 $_PROCSUB_SEQ++;
1669 2 $attempt++;
1670 2 my $path = File::Spec->catfile($dir,
1671 'batsh_ps_' . $$ . '_' . $_PROCSUB_SEQ . '.tmp');
1672 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
1673 2 push @_PROCSUB_TMPFILES, $path;
1674 2 return $path;
1675 }
1676 # EEXIST or transient error: retry with next sequence number
1677 }
1678 0 return undef;
1679 }
1680  
1681 # ----------------------------------------------------------------
1682 # export
1683 # ----------------------------------------------------------------
1684 49 ▶sub _cmd_export {
Subroutine _cmd_export: called CC=6 SCAR=20.5
1685 49 my ($rest) = @_;
1686 49 $rest =~ s/\A\s+//;
1687 # export -p: print all
1688 49 ▶ if ($rest =~ /\A-p\s*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /\A-p\s*\z/) 0 49 49
1689 0 for my $k (sort keys %BATsh::Env::STORE) {
1690 0 my $v = $BATsh::Env::STORE{$k};
1691 0 $v =~ s/'/'\\''/g;
1692 0 print "export $k='$v'\n";
1693 }
1694 0 return 0;
1695 }
1696 # export VAR=value or export VAR. The value is split on UNQUOTED
1697 # whitespace and dequoted exactly like an ordinary assignment, so
1698 # `export V='C:\x'` exports C:\x rather than the quote characters
1699 # themselves, and `export V="a b"` keeps its embedded space.
1700 49 for my $item (_arr_split_words($rest)) {
1701 49 ▶ 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) { } 43 6 49
elsif ($item =~ /\A([A-Za-z_][A-Za-z0-9_]*)\z/) { } 6 0 6
1702 43 _sh_store_scalar($1, _arr_dequote($2));
1703 }
1704 elsif ($item =~ /\A([A-Za-z_][A-Za-z0-9_]*)\z/) {
1705 # export existing variable (already in store; no-op)
1706 }
1707 }
1708 49 $LAST_STATUS = 0;
1709 49 return 0;
1710 }
1711  
1712 # ----------------------------------------------------------------
1713 # unset
1714 # ----------------------------------------------------------------
1715 9 ▶sub _cmd_unset {
Subroutine _cmd_unset: called CC=9 SCAR=22.0
1716 9 my ($rest) = @_;
1717 9 my $status = 0;
1718 9 for my $var (split /\s+/, $rest) {
1719 9 $var =~ s/\A\s+//; $var =~ s/\s+\z//;
1720 9 ▶ next if $var eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $var eq "" 0 9 9
1721 # A readonly variable / element cannot be unset (bash: status 1).
1722 9 ▶ if ($_SH_READONLY{ uc($var) }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BATsh::SH::_SH_READONLY{uc $var}) 2 7 9
1723 2 print STDERR "sh: unset: $var: cannot unset: readonly variable\n";
1724 2 $status = 1;
1725 2 next;
1726 }
1727 # unset NAME[SUB] -- remove a single array element
1728 7 ▶ 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/) 3 4 7
1729 3 my ($name, $sub) = ($1, $2);
1730 3 my $k = _arr_name($name);
1731 3 ▶ if (exists $_SH_ARRAY{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $BATsh::SH::_SH_ARRAY{$k}) 3 0 3
1732 3 ▶ 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') { } 1 2 3
Condition66%2/3
defined $BATsh::SH::_SH_ARRAY_TYPE{$k} and $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
!ll&&!rl&&r
021
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
1733 1 delete $_SH_ARRAY{$k}{$sub};
1734 }
1735 else {
1736 2 delete $_SH_ARRAY{$k}{ _arr_index($sub) };
1737 }
1738 }
1739 3 next;
1740 }
1741 # unset NAME -- remove a whole array (and any scalar of the same name)
1742 4 my $k = _arr_name($var);
1743 4 ▶ if (exists $_SH_ARRAY{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $BATsh::SH::_SH_ARRAY{$k}) 2 2 4
1744 2 delete $_SH_ARRAY{$k};
1745 2 delete $_SH_ARRAY_TYPE{$k};
1746 }
1747 4 BATsh::Env->unset($var);
1748 }
1749 9 $LAST_STATUS = $status;
1750 9 return $status;
1751 }
1752  
1753 # ----------------------------------------------------------------
1754 # echo
1755 # ----------------------------------------------------------------
1756 700 ▶sub _cmd_echo {
Subroutine _cmd_echo: called CC=5 SCAR=16.2
1757 700 my ($rest) = @_;
1758 700 $rest =~ s/\A\s+//;
1759 700 my $no_newline = 0;
1760 0 ▶ if ($rest =~ s/\A-n\s*//) { $no_newline = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/\A-n\s*//) 0 700 700
1761 # -e: enable escape sequences
1762 700 my $escape = 0;
1763 700 ▶ if ($rest =~ s/\A-e\s*//) { $escape = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/\A-e\s*//) 1 699 700
1764 700 ▶ if ($escape) {
Branch: 1 branch
BranchTrueFalseTotal
if ($escape) 1 699 700
1765 1 $rest =~ s/\\n/\n/g;
1766 1 $rest =~ s/\\t/\t/g;
1767 1 $rest =~ s/\\r/\r/g;
1768 1 $rest =~ s/\\\\/\\/g;
1769 # The same sequences arriving from an expansion (v='a\tb';
1770 # echo -e $v) still carry the protection sentinel in place of
1771 # their backslash, and are interpreted here as well; a protected
1772 # backslash that does not start a known escape stays protected,
1773 # so a Windows path keeps its separators.
1774 1 $rest =~ s/\x00BATSH_LB\x00n/\n/g;
1775 1 $rest =~ s/\x00BATSH_LB\x00t/\t/g;
1776 1 $rest =~ s/\x00BATSH_LB\x00r/\r/g;
1777 1 $rest =~ s/\x00BATSH_LB\x00\x00BATSH_LB\x00/\x00BATSH_LB\x00/g;
1778 }
1779 # Remove shell quoting structurally so that quotes anywhere in the
1780 # argument list are dropped (e.g. echo "${arr[@]}" tail), not only when
1781 # the whole string is wrapped in one pair of quotes.
1782 700 $rest = _arr_dequote($rest);
1783 700 $rest = BATsh::MB::dec($rest);
1784 0 ▶ if ($no_newline) { print $rest }
Branch: 1 branch
BranchTrueFalseTotal
if ($no_newline) { } 0 700 700
1785 700 else { print "$rest\n" }
1786 700 $LAST_STATUS = 0;
1787 700 return 0;
1788 }
1789  
1790 # ----------------------------------------------------------------
1791 # printf
1792 # ----------------------------------------------------------------
1793 # A faithful pure-Perl printf: POSIX/bash format-string escapes, the full
1794 # conversion set (%d %i %o %u %x %X %e %E %f %g %G %c %s %b %q %%), field
1795 # width / precision including the dynamic "*" form, argument RECYCLING (the
1796 # format is reused until the arguments are exhausted), the "%b" conversion
1797 # (backslash escapes interpreted in the argument, with "\c" ending output),
1798 # the "%q" conversion (shell-reusable quoting), and the "-v VAR" option
1799 # (store the result in a shell variable instead of printing). Perl 5.005_03
1800 # safe: no lexical filehandles, no 3-argument open, no post-5.005 sprintf
1801 # conversions (%a is deliberately not emitted).
1802 25 ▶sub _cmd_printf {
Subroutine _cmd_printf: called CC=10 SCAR=23.3
1803 25 my ($rest) = @_;
1804 25 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 25 25
1805  
1806 25 my @tok = _arr_split_words($rest);
1807  
1808 # Options on the still-quoted tokens: -v VAR / -vVAR, and -- terminator.
1809 25 my $target;
1810 25 ▶ while (@tok) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tok) 27 0 27
1811 27 my $t0 = $tok[0];
1812 27 ▶ if ($t0 eq '--') { shift @tok; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($t0 eq "--") 1 26 27
1813 26 ▶ if ($t0 eq '-v') {
Branch: 1 branch
BranchTrueFalseTotal
if ($t0 eq "-v") 1 25 26
1814 1 shift @tok;
1815 1 ▶ my $vn = @tok ? _arr_dequote(shift @tok) : '';
Branch: 1 branch
BranchTrueFalseTotal
@tok ? : 1 0 1
1816 1 ▶ $target = $vn if $vn ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $vn ne "" 1 0 1
1817 1 next;
1818 }
1819 25 ▶ if ($t0 =~ /\A-v(.+)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t0 =~ /\A-v(.+)\z/s) 1 24 25
1820 1 shift @tok;
1821 1 $target = _arr_dequote($1);
1822 1 next;
1823 }
1824 24 last;
1825 }
1826 0 ▶ if (!@tok) { $LAST_STATUS = 0; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (@tok) 0 25 25
1827  
1828 25 my $fmt = _arr_dequote(shift @tok);
1829 25 my @args = map { _arr_dequote($_) } @tok;
1830 25 $fmt = BATsh::MB::dec($fmt);
1831 25 @args = map { BATsh::MB::dec($_) } @args;
1832  
1833 25 my ($fmt_x, $fmt_stop) = _printf_unescape($fmt, 1);
1834 25 my $out = _printf_format($fmt_x, $fmt_stop, @args);
1835  
1836 25 ▶ if (defined $target) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target) { } 2 23 25
1837 2 BATsh::Env->set($target, $out);
1838 }
1839 else {
1840 23 print $out;
1841 }
1842 25 $LAST_STATUS = 0;
1843 25 return 0;
1844 }
1845  
1846 # _printf_unescape(STR, ALLOW_C): interpret C/POSIX backslash escapes.
1847 # Returns (RESULT, STOP) where STOP is true when a "\c" (only recognised
1848 # when ALLOW_C is set) requested that output cease. Unknown escapes keep
1849 # their backslash, matching bash.
1850 28 ▶sub _printf_unescape {
Subroutine _printf_unescape: called CC=23 SCAR=42.0
1851 28 my ($s, $allow_c) = @_;
1852 28 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 28 28
1853 28 my $out = '';
1854 28 my $stop = 0;
1855 28 my $i = 0;
1856 28 my $n = length $s;
1857 28 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 117 27 144
1858 117 my $c = substr($s, $i, 1);
1859 117 ▶ if ($c ne '\\') { $out .= $c; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c ne "\\") 88 29 117
1860 29 my $d = substr($s, $i + 1, 1);
1861 29 ▶ if ($d eq 'n') { $out .= "\n"; $i += 2 }
Branch: 15 branches
BranchTrueFalseTotal
if ($d eq 'n') { } 25 4 29
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
1862 1 elsif ($d eq 't') { $out .= "\t"; $i += 2 }
1863 0 elsif ($d eq 'r') { $out .= "\r"; $i += 2 }
1864 0 elsif ($d eq '\\') { $out .= "\\"; $i += 2 }
1865 0 elsif ($d eq 'a') { $out .= chr(7); $i += 2 }
1866 0 elsif ($d eq 'b') { $out .= chr(8); $i += 2 }
1867 0 elsif ($d eq 'f') { $out .= chr(12); $i += 2 }
1868 0 elsif ($d eq 'v') { $out .= chr(11); $i += 2 }
1869 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
1870 0 elsif ($d eq '"') { $out .= '"'; $i += 2 }
1871 0 elsif ($d eq "'") { $out .= "'"; $i += 2 }
1872 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
1873 elsif ($d eq 'x') {
1874 1 my $h = substr($s, $i + 2);
1875 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
1876 1 $out .= chr(hex($1)); $i += 2 + length($1);
1877 }
1878 0 else { $out .= '\\x'; $i += 2 }
1879 }
1880 elsif ($d =~ /[0-7]/) {
1881 1 my $o = substr($s, $i + 1);
1882 1 ▶ if ($o =~ /\A(0?[0-7]{1,3})/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o =~ /\A(0?[0-7]{1,3})/) { } 1 0 1
1883 1 $out .= chr(oct($1) & 0xFF); $i += 1 + length($1);
1884 }
1885 0 else { $out .= '\\'; $i++ }
1886 }
1887 0 elsif ($d eq '') { $out .= '\\'; $i++ }
1888 0 else { $out .= '\\' . $d; $i += 2 }
1889 }
1890 28 return ($out, $stop);
1891 }
1892  
1893 # _printf_int(STR) / _printf_num(STR): coerce a printf argument to an
1894 # integer / a number for a numeric conversion. A leading ' or " selects
1895 # the numeric value of the following character (POSIX); otherwise a leading
1896 # numeric prefix (decimal, 0x hex, 0 octal) is used, and a non-numeric
1897 # argument yields 0 (bash warns and uses 0; we stay quiet).
1898 9 ▶sub _printf_int {
Subroutine _printf_int: called CC=8 SCAR=25.1
1899 9 my ($s) = @_;
1900 9 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 9 9
1901 9 ▶ if ($s =~ /\A['"](.)/s) { return ord($1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A['"](.)/s) 1 8 9
1902 8 ▶ 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 8 8
1903 0 ▶ my $v = hex($2); return ($1 eq '-') ? -$v : $v;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '-' ? : 0 0 0
1904 }
1905 8 ▶ if ($s =~ /\A\s*([-+]?)0([0-7]+)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A\s*([-+]?)0([0-7]+)\z/) 0 8 8
1906 0 ▶ my $v = oct($2); return ($1 eq '-') ? -$v : $v;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '-' ? : 0 0 0
1907 }
1908 8 ▶ if ($s =~ /\A\s*([-+]?\d+)/) { return int($1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A\s*([-+]?\d+)/) 6 2 8
1909 2 return 0;
1910 }
1911  
1912 1 ▶sub _printf_num {
Subroutine _printf_num: called CC=6 SCAR=26.2
1913 1 my ($s) = @_;
1914 1 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 1 1
1915 0 ▶ if ($s =~ /\A['"](.)/s) { return ord($1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A['"](.)/s) 0 1 1
1916 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
1917 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
1918 0 ▶ my $v = hex($2); return ($1 eq '-') ? -$v : $v;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '-' ? : 0 0 0
1919 }
1920 0 return 0;
1921 }
1922  
1923 # _printf_shellquote(STR): the "%q" conversion -- quote STR so it reads back
1924 # as a single shell word. Safe characters are emitted bare; anything else
1925 # is single-quoted with embedded single quotes rendered as '\''.
1926 2 ▶sub _printf_shellquote {
Subroutine _printf_shellquote: called CC=4 SCAR=14.0
1927 2 my ($s) = @_;
1928 2 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 2 2
1929 2 ▶ return "''" if $s eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "" 0 2 2
1930 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
1931 1 my $q = $s;
1932 1 $q =~ s/'/'\\''/g;
1933 1 return "'" . $q . "'";
1934 }
1935  
1936 # _sp(FMT, ARGS): sprintf with warnings suppressed (a non-numeric argument
1937 # already coerced to 0, an over-wide field, etc. must not print to STDERR).
1938 28 ▶sub _sp {
Subroutine _sp: called CC=1 SCAR=0.0
1939 28 my ($f, @a) = @_;
1940 28 ▶ local $SIG{'__WARN__'} = sub { };
Subroutine __ANON__: not called CC=1 SCAR=0.0
1941 28 return sprintf($f, @a);
1942 }
1943  
1944 # _printf_format(FMT, FMT_STOP, ARGS): apply the (already escape-expanded)
1945 # format to the argument list, recycling the format until the arguments are
1946 # exhausted (bash semantics), and stopping early if a "\c" was seen.
1947 25 ▶sub _printf_format {
Subroutine _printf_format: called CC=28 SCAR=33.8
1948 25 my ($fmt, $fmt_stop, @args) = @_;
1949 25 my $out = '';
1950 25 my $ai = 0;
1951 25 my $narg = scalar @args;
1952 25 my $guard = 0;
1953 25 while (1) {
1954 28 $guard++;
1955 28 ▶ last if $guard > 100000;
Branch: 1 branch
BranchTrueFalseTotal
if $guard > 100000 0 28 28
1956 28 my $consumed = 0;
1957 28 my $stop = 0;
1958 28 my $i = 0;
1959 28 my $len = length $fmt;
1960 28 ▶ while ($i < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 86 27 113
1961 86 my $c = substr($fmt, $i, 1);
1962 86 ▶ if ($c ne '%') { $out .= $c; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c ne "%") 57 29 86
1963 29 ▶ if (substr($fmt, $i + 1, 1) eq '%') { $out .= '%'; $i += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if (substr($fmt, $i + 1, 1) eq "%") 1 28 29
1964 28 my $tail = substr($fmt, $i);
1965 28 ▶ if ($tail =~ /\A(%[-+ 0#]*(?:\*|\d+)?(?:\.(?:\*|\d+))?)([diouxXeEfgGcsbq])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tail =~ /\A(%[-+ 0#]*(?:\*|\d+)?(?:\.(?:\*|\d+))?)([diouxXeEfgGcsbq])/) { } 28 0 28
1966 28 my $spec = $1;
1967 28 my $conv = $2;
1968 28 $i += length($1) + length($2);
1969  
1970 28 ▶ while ($spec =~ /\*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /\*/) 1 28 29
1971 1 ▶ my $val = ($ai < $narg) ? _printf_int($args[$ai]) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ai < $narg ? : 1 0 1
1972 1 ▶ if ($ai < $narg) { $ai++; $consumed++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($ai < $narg) 1 0 1
1973 1 ▶ $val = -$val if $val < 0; # negative field width -> abs
Branch: 1 branch
BranchTrueFalseTotal
if $val < 0 0 1 1
1974 1 $spec =~ s/\*/$val/;
1975 }
1976  
1977 28 my $have = ($ai < $narg);
1978 28 ▶ my $raw = $have ? $args[$ai] : '';
Branch: 1 branch
BranchTrueFalseTotal
$have ? : 27 1 28
1979  
1980 28 ▶ if ($conv eq 's') {
Branch: 5 branches
BranchTrueFalseTotal
if ($conv eq 's') { } 12 16 28
elsif ($conv eq 'b') { } 3 13 16
elsif ($conv eq 'q') { } 2 11 13
elsif ($conv eq 'c') { } 2 9 11
elsif ($conv =~ /[diouxX]/) { } 8 1 9
1981 12 $out .= _sp($spec . 's', $raw);
1982 12 ▶ if ($have) { $ai++; $consumed++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($have) 11 1 12
1983 }
1984 elsif ($conv eq 'b') {
1985 3 my ($ex, $st) = _printf_unescape($raw, 1);
1986 3 $out .= _sp($spec . 's', $ex);
1987 3 ▶ if ($have) { $ai++; $consumed++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($have) 3 0 3
1988 3 ▶ if ($st) { $stop = 1; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($st) 1 2 3
1989 }
1990 elsif ($conv eq 'q') {
1991 2 $out .= _sp($spec . 's', _printf_shellquote($raw));
1992 2 ▶ if ($have) { $ai++; $consumed++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($have) 2 0 2
1993 }
1994 elsif ($conv eq 'c') {
1995 2 ▶ my $ch = length($raw) ? substr($raw, 0, 1) : '';
Branch: 1 branch
BranchTrueFalseTotal
length $raw ? : 2 0 2
1996 2 $out .= _sp($spec . 's', $ch);
1997 2 ▶ if ($have) { $ai++; $consumed++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($have) 2 0 2
1998 }
1999 elsif ($conv =~ /[diouxX]/) {
2000 8 $out .= _sp($spec . $conv, _printf_int($raw));
2001 8 ▶ if ($have) { $ai++; $consumed++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($have) 8 0 8
2002 }
2003 else {
2004 1 $out .= _sp($spec . $conv, _printf_num($raw));
2005 1 ▶ if ($have) { $ai++; $consumed++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($have) 1 0 1
2006 }
2007 }
2008 0 else { $out .= '%'; $i++ }
2009 }
2010 28 ▶ last if $stop || $fmt_stop;
Branch: 1 branch
BranchTrueFalseTotal
if $stop or $fmt_stop 1 27 28
Condition66%2/3
$stop or $fmt_stop
l!l&&r!l&&!r
1027
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
2011 27 ▶ last unless ($ai < $narg && $consumed > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $ai < $narg and $consumed > 0 24 3 27
Condition66%2/3
$ai < $narg and $consumed > 0
!ll&&!rl&&r
2403
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
2012 }
2013 25 return $out;
2014 }
2015  
2016 # ----------------------------------------------------------------
2017 # Tilde expansion (v0.07): ~/path and ~user/path.
2018 #
2019 # POSIX word-initial tilde expansion. Only applied to a word that
2020 # begins with an UNQUOTED ~ (callers are responsible for that check);
2021 # the tilde-prefix itself is never subject to variable / command
2022 # substitution. Forms:
2023 # ~ -> $HOME
2024 # ~/rest -> $HOME . '/rest'
2025 # ~user -> user's home directory (getpwnam; Unix-like only)
2026 # ~user/rest -> that directory . '/rest'
2027 # If the login name is unknown (or getpwnam is unavailable, as on
2028 # Win32) the word is returned unchanged, matching bash's behaviour of
2029 # leaving an unresolvable ~name literal.
2030 # NOT implemented: colon-list tilde expansion in PATH-like assignments
2031 # (bash also tilde-expands after ':' in PATH=~/a:~/b); documented as a
2032 # limitation in the POD.
2033 # ----------------------------------------------------------------
2034 9 ▶sub _tilde_expand {
Subroutine _tilde_expand: called CC=13 SCAR=28.9
2035 9 my ($word) = @_;
2036 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
2037 9 my ($tag, $rest) = ($word =~ /\A~([^\/]*)(.*)\z/s);
2038 9 ▶ $tag = '' unless defined $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag 0 9 9
2039 9 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 9 9
2040 9 my $home;
2041 9 ▶ if ($tag eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag eq '') { } 8 1 9
elsif (not $^O =~ /MSWin32/i) { } 1 0 1
2042 8 $home = $ENV{'HOME'};
2043 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
2044 }
2045 elsif ($^O !~ /MSWin32/i) {
2046 1 my @pw = eval { getpwnam($tag) };
2047 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
2048 }
2049 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
2050 8 return $home . $rest;
2051 }
2052  
2053 # _tilde_prepass: scan a RAW (not-yet-variable-expanded) string and
2054 # expand every unquoted, word-initial "~..." run using _tilde_expand().
2055 # A "word start" is: the very beginning of the string, any position
2056 # right after unquoted whitespace, or (once, at the very beginning of
2057 # the string only) the position right after the '=' of a leading
2058 # NAME= assignment token -- matching bash's extra tilde-expansion spot
2059 # for VAR=~/path. Quoted text (single or double quotes) is copied
2060 # through untouched and never considered a word start for tilde
2061 # purposes. Everything else in the string (variables, command
2062 # substitution, arithmetic, ...) is left as-is for the rest of
2063 # _expand() to process afterwards.
2064 # _tilde_protect: let an expanded tilde-prefix survive the rest of the
2065 # expansion pipeline unharmed. The replacement text is DATA, not shell
2066 # source: a Windows home directory such as C:\home\flower must not have
2067 # its backslashes eaten by quote removal, a "$" or "`" in it must not
2068 # start an expansion, and a home directory containing spaces must stay
2069 # a single word (POSIX: a tilde-expansion result is not field-split).
2070 # An unresolved ~name is returned untouched, exactly as bash leaves it.
2071 9 ▶sub _tilde_protect {
Subroutine _tilde_protect: called CC=4 SCAR=13.9
2072 9 my ($expanded, $orig) = @_;
2073 9 ▶ return $orig unless defined $expanded;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expanded 0 9 9
2074 9 ▶ return $expanded if $expanded eq $orig;
Branch: 1 branch
BranchTrueFalseTotal
if $expanded eq $orig 1 8 9
2075 8 my $v = _protect_lit($expanded);
2076 8 $v =~ s/\$/\x00BATSH_DL\x00/g;
2077 8 $v =~ s/`/\x00BATSH_BT\x00/g;
2078 # Only a home directory containing whitespace needs the quotes;
2079 # quoting the usual case too would suppress globbing of ~/* words.
2080 8 ▶ return $v unless $v =~ /\s/;
Branch: 1 branch
BranchTrueFalseTotal
unless $v =~ /\s/ 6 2 8
2081 2 return '"' . $v . '"';
2082 }
2083  
2084 1867 ▶sub _tilde_prepass {
Subroutine _tilde_prepass: called CC=16 SCAR=27.7
2085 1867 my ($str) = @_;
2086  
2087 1867 ▶ return $str unless defined $str && $str =~ /~/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and $str =~ /~/ 1853 14 1867
Condition66%2/3
defined $str and $str =~ /~/
!ll&&!rl&&r
0185314
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
2088  
2089 14 my @chars = split //, $str;
2090 14 my $n = scalar @chars;
2091 14 my $out = '';
2092 14 my $i = 0;
2093  
2094 # Leading NAME= assignment token: the char right after '=' is also
2095 # a valid tilde-expansion start, but only for this one leading token.
2096 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
2097 2 my $prefix = $1;
2098 2 $out .= $prefix;
2099 2 $i = length($prefix);
2100 }
2101  
2102 14 my $at_word_start = 1;
2103 14 my $in_sq = 0;
2104 14 my $in_dq = 0;
2105 14 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 113 14 127
2106 113 my $c = $chars[$i];
2107 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
2108 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
2109 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
2110 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
2111 96 ▶ if ($c =~ /\s/) { $out .= $c; $i++; $at_word_start = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /\s/) 12 84 96
2112 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
2113 9 my $j = $i + 1;
2114 9 my $tag = '';
2115 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
2116 9 $out .= _tilde_protect(_tilde_expand('~' . $tag), '~' . $tag);
2117 9 $i = $j;
2118 9 $at_word_start = 0;
2119 9 next;
2120 }
2121 75 $out .= $c;
2122 75 $i++;
2123 75 $at_word_start = 0;
2124 }
2125 14 return $out;
2126 }
2127  
2128 # ----------------------------------------------------------------
2129 # Brace expansion (v0.07): {a,b,c} and {1..5} / {a..e} [..step] word
2130 # generation. Runs on the RAW source line, lexically, before any other
2131 # expansion -- exactly like the tilde prepass above. Quoted text and
2132 # anything that is opaque shell syntax (${...}, $(...), $((...)), `...`,
2133 # <(...), >(...), and backslash-escaped characters) is protected behind
2134 # a NUL-delimited sentinel so its content is copied through untouched
2135 # and its internal whitespace never becomes a false word boundary.
2136 # ----------------------------------------------------------------
2137 1749 ▶sub _brace_expand_line {
Subroutine _brace_expand_line: called CC=41 SCAR=47.5
2138 1749 my ($line) = @_;
2139 1749 ▶ return $line unless defined $line && $line =~ /\{/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line and $line =~ /\{/ 1637 112 1749
Condition66%2/3
defined $line and $line =~ /\{/
!ll&&!rl&&r
01637112
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
2140  
2141 112 my @protected;
2142 112 my $safe = '';
2143 112 my @c = split //, $line;
2144 112 my $n = scalar @c;
2145 112 my $i = 0;
2146 112 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 719 112 831
2147 719 my $ch = $c[$i];
2148  
2149 719 ▶ if ($ch eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 0 719 719
2150 0 ▶ my $seg = $ch . (($i+1 < $n) ? $c[$i+1] : '');
Branch: 1 branch
BranchTrueFalseTotal
$i + 1 < $n ? : 0 0 0
2151 0 push @protected, $seg;
2152 0 $safe .= "\x00BR" . $#protected . "\x00";
2153 0 $i += 2; next;
2154 }
2155 719 ▶ if ($ch eq "'") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'") 2 717 719
2156 2 my $j = $i + 1;
2157 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
2158 2 ▶ $j++ if $j < $n;
Branch: 1 branch
BranchTrueFalseTotal
if $j < $n 2 0 2
2159 2 my $seg = join('', @c[$i .. $j-1]);
2160 2 push @protected, $seg;
2161 2 $safe .= "\x00BR" . $#protected . "\x00";
2162 2 $i = $j; next;
2163 }
2164 717 ▶ if ($ch eq '"') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"") 82 635 717
2165 82 my $j = $i + 1;
2166 82 ▶ while ($j < $n && $c[$j] ne '"') {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $c[$j] ne "\"") 1276 82 1358
Condition66%2/3
$j < $n and $c[$j] ne "\""
!ll&&!rl&&r
0821276
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
2167 1276 ▶ $j += ($c[$j] eq '\\' && $j+1 < $n) ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$c[$j] eq '\\' && $j + 1 < $n ? : 1 1275 1276
Condition66%2/3
$c[$j] eq "\\" and $j + 1 < $n
!ll&&!rl&&r
127501
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
2168 }
2169 82 ▶ $j++ if $j < $n;
Branch: 1 branch
BranchTrueFalseTotal
if $j < $n 82 0 82
2170 82 my $seg = join('', @c[$i .. $j-1]);
2171 82 push @protected, $seg;
2172 82 $safe .= "\x00BR" . $#protected . "\x00";
2173 82 $i = $j; next;
2174 }
2175 635 ▶ 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 589 635
Condition66%6/9
$ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "(" || $c[$i + 1] eq "{"
!ll&&!rl&&r
589046
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
589046
$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 table60%3/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
2176 46 my $openc = $c[$i+1];
2177 46 ▶ my $closec = ($openc eq '(') ? ')' : '}';
Branch: 1 branch
BranchTrueFalseTotal
$openc eq '(' ? : 5 41 46
2178 46 my $depth = 1;
2179 46 my $j = $i + 2;
2180 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
2181 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
2182 51 elsif ($c[$j] eq $closec) { $depth-- }
2183 351 $j++;
2184 }
2185 46 my $seg = join('', @c[$i .. $j-1]);
2186 46 push @protected, $seg;
2187 46 $safe .= "\x00BR" . $#protected . "\x00";
2188 46 $i = $j; next;
2189 }
2190 589 ▶ 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 589 589
Condition33%3/9
$ch eq "<" || $ch eq ">" and $i + 1 < $n and $c[$i + 1] eq "("
!ll&&!rl&&r
58900
$ch eq "<" || $ch eq ">" and $i + 1 < $n
!ll&&!rl&&r
58900
$ch eq "<" or $ch eq ">"
l!l&&r!l&&!r
00589
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
2191 0 my $depth = 1;
2192 0 my $j = $i + 2;
2193 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
2194 0 ▶ if ($c[$j] eq '(') { $depth++ }
Branch: 2 branches
BranchTrueFalseTotal
if ($c[$j] eq '(') { } 0 0 0
elsif ($c[$j] eq ')') { } 0 0 0
2195 0 elsif ($c[$j] eq ')') { $depth-- }
2196 0 $j++;
2197 }
2198 0 my $seg = join('', @c[$i .. $j-1]);
2199 0 push @protected, $seg;
2200 0 $safe .= "\x00BR" . $#protected . "\x00";
2201 0 $i = $j; next;
2202 }
2203 589 ▶ if ($ch eq '`') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "`") 0 589 589
2204 0 my $j = $i + 1;
2205 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
2206 0 ▶ $j++ if $j < $n;
Branch: 1 branch
BranchTrueFalseTotal
if $j < $n 0 0 0
2207 0 my $seg = join('', @c[$i .. $j-1]);
2208 0 push @protected, $seg;
2209 0 $safe .= "\x00BR" . $#protected . "\x00";
2210 0 $i = $j; next;
2211 }
2212  
2213 589 $safe .= $ch; $i++;
2214 }
2215  
2216 112 my @pieces = split /(\s+)/, $safe;
2217 112 my $out = '';
2218 112 for my $w (@pieces) {
2219 300 ▶ if ($w eq '' || $w =~ /\A\s+\z/) { $out .= $w; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($w eq "" or $w =~ /\A\s+\z/) 94 206 300
Condition66%2/3
$w eq "" or $w =~ /\A\s+\z/
l!l&&r!l&&!r
094206
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
2220 206 ▶ my @exp = ($w =~ /\{/) ? _brace_expand_word($w) : ($w);
Branch: 1 branch
BranchTrueFalseTotal
$w =~ /\{/ ? : 6 200 206
2221 206 $out .= join(' ', @exp);
2222 }
2223  
2224 112 $out =~ s/\x00BR(\d+)\x00/$protected[$1]/ge;
2225 112 return $out;
2226 }
2227  
2228 # _brace_expand_word: expand all brace groups in one already-protected
2229 # word, returning the list of resulting words (a single-element list
2230 # containing the word unchanged when it holds no valid brace group).
2231 30 ▶sub _brace_expand_word {
Subroutine _brace_expand_word: called CC=7 SCAR=19.5
2232 30 my ($word) = @_;
2233 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
2234 6 my @found = _brace_find_group($word, 0);
2235 6 ▶ return ($word) unless @found;
Branch: 1 branch
BranchTrueFalseTotal
unless @found 1 5 6
2236 5 my ($open, $close, $alts_ref) = @found;
2237 5 my $prefix = substr($word, 0, $open);
2238 5 my $suffix = substr($word, $close + 1);
2239 5 my @suffix_words = _brace_expand_word($suffix);
2240 5 my @out;
2241 5 for my $a (@{$alts_ref}) {
2242 19 for my $aw (_brace_expand_word($a)) {
2243 19 for my $sw (@suffix_words) {
2244 19 push @out, $prefix . $aw . $sw;
2245 }
2246 }
2247 }
2248 5 return @out;
2249 }
2250  
2251 # _brace_find_group: locate the first VALID brace group at or after
2252 # $start (a comma-list or a range -- a bare "{word}" with neither is
2253 # left as literal text and scanning continues past it). Returns
2254 # ($open_pos, $close_pos, \@alternatives) or () when none remain.
2255 6 ▶sub _brace_find_group {
Subroutine _brace_find_group: called CC=15 SCAR=28.1
2256 6 my ($word, $start) = @_;
2257 6 my @c = split //, $word;
2258 6 my $n = scalar @c;
2259 6 my $i = $start;
2260 6 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 13 1 14
2261 0 ▶ if ($c[$i] eq '\\') { $i += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c[$i] eq "\\") 0 13 13
2262 13 ▶ if ($c[$i] ne '{') { $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c[$i] ne "{") 7 6 13
2263  
2264 6 my $open = $i;
2265 6 my $depth = 1;
2266 6 my $j = $i + 1;
2267 6 my @commas;
2268 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
2269 0 ▶ if ($c[$j] eq '\\') { $j += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c[$j] eq "\\") 0 32 32
2270 0 ▶ if ($c[$j] eq '{') { $depth++; $j++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c[$j] eq "{") 0 32 32
2271 32 ▶ if ($c[$j] eq '}') { $depth--; $j++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c[$j] eq "}") 6 26 32
2272 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
2273 26 $j++;
2274 }
2275 0 ▶ if ($depth != 0) { $i = $open + 1; next } # unmatched '{'
Branch: 1 branch
BranchTrueFalseTotal
if ($depth != 0) 0 6 6
2276  
2277 6 my $close = $j - 1;
2278 6 my @alts;
2279 6 ▶ if (@commas) {
Branch: 1 branch
BranchTrueFalseTotal
if (@commas) { } 1 5 6
2280 1 my $prev = $open + 1;
2281 1 for my $pos (@commas) {
2282 2 push @alts, substr($word, $prev, $pos - $prev);
2283 2 $prev = $pos + 1;
2284 }
2285 1 push @alts, substr($word, $prev, $close - $prev);
2286 }
2287 else {
2288 5 @alts = _brace_expand_range(substr($word, $open + 1, $close - $open - 1));
2289 }
2290 6 ▶ return ($open, $close, \@alts) if @alts;
Branch: 1 branch
BranchTrueFalseTotal
if @alts 5 1 6
2291 1 $i = $open + 1; # not a valid group: keep scanning past it
2292 }
2293 1 return ();
2294 }
2295  
2296 # _brace_expand_range: expand "X..Y" or "X..Y..STEP" -- integer (with
2297 # optional zero-padding taken from the wider operand) or single-letter.
2298 # Returns () when $inner is not a recognised range expression.
2299 5 ▶sub _brace_expand_range {
Subroutine _brace_expand_range: called CC=23 SCAR=33.8
2300 5 my ($inner) = @_;
2301 5 ▶ $inner = '' unless defined $inner;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $inner 0 5 5
2302  
2303 5 ▶ if ($inner =~ /\A(-?\d+)\.\.(-?\d+)(?:\.\.(-?\d+))?\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inner =~ /\A(-?\d+)\.\.(-?\d+)(?:\.\.(-?\d+))?\z/) 3 2 5
2304 3 my ($from, $to, $step) = ($1, $2, $3);
2305 3 my $pad = 0;
2306 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
2307 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
2308 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
2309 1 ▶ $pad = ($w1 > $w2) ? $w1 : $w2;
Branch: 1 branch
BranchTrueFalseTotal
$w1 > $w2 ? : 0 1 1
2310 }
2311 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
2312 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
2313 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
2314 3 my @out;
2315 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
2316 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
2317 3 return @out;
2318 }
2319 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
2320 1 my ($from, $to, $step) = ($1, $2, $3);
2321 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
2322 1 $step = abs($step);
2323 1 my ($fo, $to_o) = (ord($from), ord($to));
2324 1 my @out;
2325 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
2326 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
2327 1 return @out;
2328 }
2329 1 return ();
2330 }
2331  
2332 13 ▶sub _brace_pad {
Subroutine _brace_pad: called CC=5 SCAR=16.7
2333 13 my ($v, $pad) = @_;
2334 13 ▶ return $v unless $pad;
Branch: 1 branch
BranchTrueFalseTotal
unless $pad 10 3 13
2335 3 ▶ my $neg = ($v < 0) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$v < 0 ? : 0 3 3
2336 3 ▶ my $s = sprintf('%0' . $pad . 'd', $neg ? -$v : $v);
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 0 3 3
2337 3 ▶ return $neg ? "-$s" : $s;
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 0 3 3
2338 }
2339  
2340 # ----------------------------------------------------------------
2341 # cd
2342 # ----------------------------------------------------------------
2343 9 ▶sub _cmd_cd {
Subroutine _cmd_cd: called CC=5 SCAR=17.4
2344 9 my ($rest) = @_;
2345 9 $rest =~ s/\A\s+//;
2346 9 $rest =~ s/\s+\z//;
2347 # Strip quotes the same way command words are dequoted, so
2348 # cd "a b" / cd 'dir' / cd "$HOME/x" reach chdir() as a plain path.
2349 9 $rest = _arr_dequote($rest);
2350 9 $rest = BATsh::MB::dec($rest);
2351 9 ▶ if ($rest eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest eq "") 0 9 9
2352 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
2353 }
2354 9 ▶ unless (chdir($rest)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chdir $rest) 1 8 9
2355 1 print STDERR "cd: $rest: No such file or directory\n";
2356 1 $LAST_STATUS = 1;
2357 1 return 1;
2358 }
2359 8 BATsh::Env->set('PWD', BATsh::MB::enc(Cwd::cwd()));
2360 8 $LAST_STATUS = 0;
2361 8 return 0;
2362 }
2363  
2364 # ----------------------------------------------------------------
2365 # exit
2366 # ----------------------------------------------------------------
2367 13 ▶sub _cmd_exit {
Subroutine _cmd_exit: called CC=5 SCAR=16.5
2368 13 my ($rest) = @_;
2369 13 $rest =~ s/\A\s+//;
2370 13 ▶ my $code = ($rest =~ /\A(\d+)/) ? int($1) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rest =~ /\A(\d+)/ ? : 13 0 13
2371 # Run the EXIT trap (once) before exiting, while no exit is yet pending so
2372 # the trap body executes. Delete it first to avoid re-entry / double-fire.
2373 13 ▶ if (exists $_SH_TRAP{'EXIT'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $BATsh::SH::_SH_TRAP{"EXIT"}) 2 11 13
2374 2 my $cmd = delete $_SH_TRAP{'EXIT'};
2375 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
2376 2 eval { _run_lines('BATsh::SH', [$cmd], {}) };
2377 }
2378 }
2379 13 $_EXIT_CODE = $code;
2380 13 $LAST_STATUS = $code;
2381 13 return $code;
2382 }
2383  
2384 # ----------------------------------------------------------------
2385 # exec (v0.07)
2386 #
2387 # exec > file / exec 2>&1 / ... apply redirection(s) permanently to
2388 # the current shell (no save/restore)
2389 # exec cmd args... run cmd (with its own redirections,
2390 # if any) then terminate the whole
2391 # script with cmd's exit status
2392 #
2393 # There is no real fork/exec here (this interpreter is Pure Perl and
2394 # never forks -- see the Background Execution / Compound Commands notes
2395 # above); "replacing the shell" is approximated by ending the script
2396 # immediately afterward, and redirections are applied by permanently
2397 # reassigning the STDIN/STDOUT/STDERR bareword globs instead of the
2398 # save-and-restore dance _sh_exec_with_redirs() uses for ordinary
2399 # commands.
2400 # ----------------------------------------------------------------
2401 2 ▶sub _cmd_exec {
Subroutine _cmd_exec: called CC=12 SCAR=28.9
2402 2 my ($class, $rest, $opts_ref) = @_;
2403 2 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 2 2
2404 2 my ($clean, $redirs_ref) = _sh_strip_redirects($rest);
2405  
2406 2 for my $r (@{$redirs_ref}) {
2407 1 my ($fd, $append, $file) = @{$r};
2408 1 ▶ $file = BATsh::MB::dec($file) unless $file =~ /\A&[12]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /\A&[12]\z/ 1 0 1
2409 1 my $ok = 1;
2410 1 ▶ if ($fd == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fd == 0) { } 0 1 1
elsif ($fd == 1) { } 1 0 1
2411 # sysopen() takes the filename literally -- a leading '>' or a
2412 # trailing '|' is never treated as a mode or a pipe command.
2413 0 $ok = sysopen(STDIN, $file, O_RDONLY);
2414 }
2415 elsif ($fd == 1) {
2416 1 ▶ $ok = ($file eq '&2')
Branch: 2 branches
BranchTrueFalseTotal
$file eq '&2' ? : 0 1 1
$append ? : 0 1 1
2417 ? open(STDOUT, '>&STDERR')
2418 : sysopen(STDOUT, $file,
2419 O_WRONLY | O_CREAT | ($append ? O_APPEND : O_TRUNC), 0666);
2420 }
2421 else {
2422 0 ▶ $ok = ($file eq '&1')
Branch: 2 branches
BranchTrueFalseTotal
$file eq '&1' ? : 0 0 0
$append ? : 0 0 0
2423 ? open(STDERR, '>&STDOUT')
2424 : sysopen(STDERR, $file,
2425 O_WRONLY | O_CREAT | ($append ? O_APPEND : O_TRUNC), 0666);
2426 }
2427 1 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 1 1
2428 0 warn "sh: exec: $file: $!\n";
2429 0 $LAST_STATUS = 1;
2430 0 return 1;
2431 }
2432 }
2433  
2434 2 $clean =~ s/\A\s+//; $clean =~ s/\s+\z//;
2435 2 ▶ if ($clean eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($clean eq "") 1 1 2
2436 # "exec" with only redirections: they now apply for the rest of
2437 # the script; exec itself does not terminate anything.
2438 1 $LAST_STATUS = 0;
2439 1 return 0;
2440 }
2441  
2442 1 my $rc = _exec_line($class, $clean, $opts_ref);
2443 1 $_EXIT_CODE = $rc; # exec replaces the shell: end the script here
2444 1 $LAST_STATUS = $rc;
2445 1 return $rc;
2446 }
2447  
2448 # ----------------------------------------------------------------
2449 # trap -- signal / event handling (v0.06)
2450 #
2451 # Supported forms:
2452 # trap 'commands' SIGSPEC... register a handler
2453 # trap - SIGSPEC... reset to the default action
2454 # trap '' SIGSPEC... ignore the signal
2455 # trap / trap -p list the current traps
2456 #
2457 # SIGSPEC may be a name (with or without a leading SIG), a number, or the
2458 # EXIT pseudo-signal (also spelled 0). Real OS signals are bridged to
2459 # Perl's %SIG; EXIT is run internally when the script ends or on `exit`.
2460 # The handler command is stored unexpanded and (re-)expanded when it fires.
2461 # ----------------------------------------------------------------
2462 22 ▶sub _cmd_trap {
Subroutine _cmd_trap: called CC=9 SCAR=22.1
2463 22 my ($class, $rest, $opts_ref) = @_;
2464 22 my ($mode, $cmd, $sigs) = _sh_parse_trap($rest);
2465  
2466 22 ▶ if ($mode eq 'list') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "list") 2 20 22
2467 0 ▶ my @names = @{$sigs} ? @{$sigs} : sort keys %_SH_TRAP;
Branch: 1 branch
BranchTrueFalseTotal
@{$sigs;} ? : 0 2 2
2468 2 for my $n (@names) {
2469 3 my $sig = _sh_normalize_sig($n);
2470 3 ▶ next unless exists $_SH_TRAP{$sig};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BATsh::SH::_SH_TRAP{$sig} 0 3 3
2471 3 print "trap -- '" . $_SH_TRAP{$sig} . "' $sig\n";
2472 }
2473 2 $LAST_STATUS = 0;
2474 2 return 0;
2475 }
2476  
2477 20 for my $spec (@{$sigs}) {
2478 21 my $sig = _sh_normalize_sig($spec);
2479 21 ▶ next if $sig eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $sig eq "" 0 21 21
2480 21 ▶ if ($mode eq 'reset') {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'reset') { } 3 18 21
elsif ($mode eq 'ignore') { } 2 16 18
2481 3 delete $_SH_TRAP{$sig};
2482 3 _sh_set_os_sig($sig, 'DEFAULT');
2483 }
2484 elsif ($mode eq 'ignore') {
2485 2 $_SH_TRAP{$sig} = '';
2486 2 _sh_set_os_sig($sig, 'IGNORE');
2487 }
2488 else { # set
2489 16 $_SH_TRAP{$sig} = $cmd;
2490 16 _sh_set_os_sig($sig, 'HANDLER');
2491 }
2492 }
2493 20 $LAST_STATUS = 0;
2494 20 return 0;
2495 }
2496  
2497 # Parse the (raw) argument string of a trap command.
2498 # Returns ($mode, $cmd, \@sigs) where $mode is 'list', 'reset', 'ignore'
2499 # or 'set'. For 'set', $cmd is the (still unexpanded) handler command.
2500 22 ▶sub _sh_parse_trap {
Subroutine _sh_parse_trap: called CC=11 SCAR=25.3
2501 22 my ($s) = @_;
2502 22 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 22 22
2503 22 $s =~ s/\A\s+//; $s =~ s/\s+\z//;
2504 22 ▶ return ('list', undef, []) if $s eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "" 2 20 22
2505  
2506 20 ▶ if ($s =~ /\A-p\b\s*(.*)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A-p\b\s*(.*)\z/s) 0 20 20
2507 0 my @sigs = grep { length } split /\s+/, $1;
2508 0 return ('list', undef, \@sigs);
2509 }
2510  
2511 20 my ($action, $quoted, $rest);
2512 20 ▶ if ($s =~ /\A'([^']*)'\s*(.*)\z/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($s =~ /\A'([^']*)'\s*(.*)\z/s) { } 17 3 20
elsif ($s =~ /\A"((?:[^"\\]|\\.)*)"\s*(.*)\z/s) { } 0 3 3
2513 17 ($action, $quoted, $rest) = ($1, 1, $2);
2514 }
2515 elsif ($s =~ /\A"((?:[^"\\]|\\.)*)"\s*(.*)\z/s) {
2516 0 ($action, $quoted, $rest) = ($1, 1, $2);
2517 }
2518 else {
2519 3 ($action, $rest) = split /\s+/, $s, 2;
2520 3 $quoted = 0;
2521 3 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 3 3
2522 }
2523 20 my @sigs = grep { length } split /\s+/, $rest;
2524  
2525 20 ▶ return ('reset', undef, \@sigs) if !$quoted && $action eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if not $quoted and $action eq "-" 3 17 20
Condition66%2/3
not $quoted and $action eq "-"
!ll&&!rl&&r
1703
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
2526 17 ▶ return ('ignore', '', \@sigs) if $quoted && $action eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $quoted and $action eq "" 2 15 17
Condition66%2/3
$quoted and $action eq ""
!ll&&!rl&&r
0152
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
2527 15 return ('set', $action, \@sigs);
2528 }
2529  
2530 # Normalize a signal spec to a bare name: strip a leading SIG, uppercase,
2531 # and map the common signal numbers to names.
2532 24 ▶sub _sh_normalize_sig {
Subroutine _sh_normalize_sig: called CC=5 SCAR=16.3
2533 24 my ($s) = @_;
2534 24 ▶ return '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 24 24
2535 24 $s =~ s/\A\s+//; $s =~ s/\s+\z//;
2536 24 ▶ return '' if $s eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "" 0 24 24
2537 24 $s = uc($s);
2538 24 $s =~ s/\ASIG//;
2539 24 ▶ if ($s =~ /\A\d+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A\d+\z/) 1 23 24
2540 1 my %num = (0 => 'EXIT', 1 => 'HUP', 2 => 'INT', 3 => 'QUIT',
2541 6 => 'ABRT', 9 => 'KILL', 13 => 'PIPE', 14 => 'ALRM',
2542 15 => 'TERM');
2543 1 ▶ $s = exists $num{$s + 0} ? $num{$s + 0} : $s;
Branch: 1 branch
BranchTrueFalseTotal
exists $num{$s + 0} ? : 1 0 1
2544 }
2545 24 return $s;
2546 }
2547  
2548 # Bridge a trap to Perl's %SIG. Pseudo-signals (EXIT/ERR/DEBUG/RETURN) are
2549 # handled internally and never touch %SIG. Assignment is eval-guarded so an
2550 # unsupported signal name (e.g. on Windows) degrades quietly.
2551 21 ▶sub _sh_set_os_sig {
Subroutine _sh_set_os_sig: called CC=7 SCAR=21.0
2552 21 my ($sig, $what) = @_;
2553 21 ▶ 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" 8 13 21
Condition66%6/9
$sig eq "EXIT" or $sig eq "ERR" or $sig eq "DEBUG" or $sig eq "RETURN"
l!l&&r!l&&!r
8013
$sig eq "EXIT" or $sig eq "ERR" or $sig eq "DEBUG"
l!l&&r!l&&!r
8013
$sig eq "EXIT" or $sig eq "ERR"
l!l&&r!l&&!r
8013
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
2554 || $sig eq 'DEBUG' || $sig eq 'RETURN';
2555 # Some signals (e.g. HUP/USR1/USR2) do not exist on every platform --
2556 # notably Windows -- where assigning to %SIG for them emits a harmless
2557 # "No such signal" warning. Suppress just that warning so a portable
2558 # script trapping such a signal stays quiet; all other warnings pass
2559 # through, and the assignment itself still succeeds (best effort).
2560 0 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
2561 0 ▶ my $w = defined $_[0] ? $_[0] : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
2562 0 ▶ warn $w unless $w =~ /No such signal/;
Branch: 1 branch
BranchTrueFalseTotal
unless $w =~ /No such signal/ 0 0 0
2563 13 };
2564 13 eval {
2565 13 ▶ if ($what eq 'DEFAULT') { $SIG{$sig} = 'DEFAULT' }
Branch: 2 branches
BranchTrueFalseTotal
if ($what eq 'DEFAULT') { } 2 11 13
elsif ($what eq 'IGNORE') { } 2 9 11
2566 2 elsif ($what eq 'IGNORE') { $SIG{$sig} = 'IGNORE' }
2567 9 ▶ else { $SIG{$sig} = sub { _sh_run_trap($sig) } }
Subroutine __ANON__: called CC=1 SCAR=0.0
2568 };
2569 13 return;
2570 }
2571  
2572 # Run the handler command registered for $sig (no-op if unset or 'ignore').
2573 2 ▶sub _sh_run_trap {
Subroutine _sh_run_trap: called CC=3 SCAR=11.8
2574 2 my ($sig) = @_;
2575 2 my $cmd = $_SH_TRAP{$sig};
2576 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
2577 2 eval { _run_lines('BATsh::SH', [$cmd], {}) };
2578 }
2579  
2580 # Run the EXIT trap (if any) exactly once, then clear it. Called by the
2581 # top-level run paths in BATsh.pm when the whole script has finished.
2582 549 ▶sub fire_exit_trap {
Subroutine fire_exit_trap: called CC=4 SCAR=14.2
Pod undocumented
2583 549 my ($class) = @_;
2584 549 ▶ return unless exists $_SH_TRAP{'EXIT'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BATsh::SH::_SH_TRAP{"EXIT"} 545 4 549
2585 4 my $cmd = delete $_SH_TRAP{'EXIT'};
2586 4 ▶ return if !defined $cmd || $cmd eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cmd or $cmd eq "" 0 4 4
Condition33%1/3
not defined $cmd or $cmd eq ""
l!l&&r!l&&!r
004
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
2587 4 $_EXIT_CODE = undef; # let the trap body run to completion
2588 4 eval { _run_lines('BATsh::SH', [$cmd], {}) };
2589 4 return;
2590 }
2591  
2592 # ----------------------------------------------------------------
2593 # read
2594 # ----------------------------------------------------------------
2595 30 ▶sub _cmd_read {
Subroutine _cmd_read: called CC=10 SCAR=25.0
2596 30 my ($rest) = @_;
2597 30 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 30 30
2598 30 $rest =~ s/\A\s+//;
2599 30 $rest =~ s/\s+\z//;
2600  
2601 # Drop option flags such as -r (we always read a raw line); only the
2602 # bareword names that follow are treated as target variables.
2603 30 ▶ my @vars = grep { length && !/\A-/ } split /\s+/, $rest;
Condition66%2/3
length $_ && !/\A-/
!ll&&!rl&&r
0330
MC/DC50%1/2
length $_ && !/\A-/
length $_ !/\A-/
Truth table66%2/3
A && not(B)
A= length $_B= !/\A-/
ABresult
0X0
100
111
2604  
2605 30 my $line = <STDIN>;
2606 30 ▶ $line = BATsh::MB::enc($line) if defined $line;
Branch: 1 branch
BranchTrueFalseTotal
if defined $line 24 6 30
2607 30 ▶ if (!defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 6 24 30
2608 # End of input. POSIX read returns non-zero at EOF so that a
2609 # 'while read VAR; do ...; done < file' loop terminates instead
2610 # of spinning forever.
2611 6 for my $v (@vars) { BATsh::Env->set($v, '') }
2612 6 $LAST_STATUS = 1;
2613 6 return 1;
2614 }
2615 24 chomp $line;
2616  
2617 24 ▶ if (@vars == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@vars == 1) { } 24 0 24
elsif (@vars > 1) { } 0 0 0
2618 24 BATsh::Env->set($vars[0], $line);
2619 }
2620 elsif (@vars > 1) {
2621 0 my @words = split /\s+/, $line, scalar(@vars);
2622 0 for my $i (0 .. $#vars) {
2623 0 ▶ BATsh::Env->set($vars[$i], defined($words[$i]) ? $words[$i] : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $words[$i] ? : 0 0 0
2624 }
2625 }
2626 24 $LAST_STATUS = 0;
2627 24 return 0;
2628 }
2629  
2630 # ----------------------------------------------------------------
2631 # shift
2632 # ----------------------------------------------------------------
2633 9 ▶sub _cmd_shift {
Subroutine _cmd_shift: called CC=12 SCAR=25.0
2634 9 my ($rest) = @_;
2635 9 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 9 9
2636 9 $rest =~ s/\A\s+//;
2637  
2638 # Optional /N offset (bash: shift N shifts N positions)
2639 9 my $n_shift = 1;
2640 9 ▶ 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/) 6 3 9
if $n_shift < 1 0 6 6
2641  
2642 9 for my $step (1 .. $n_shift) {
2643 # Shift BATSH_ARG* (legacy)
2644 17 for my $n (1 .. 8) {
2645 136 my $next = BATsh::Env->get('BATSH_ARG' . ($n + 1));
2646 136 ▶ BATsh::Env->set('BATSH_ARG' . $n, defined($next) ? $next : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $next ? : 120 16 136
2647 }
2648 17 BATsh::Env->set('BATSH_ARG9', '');
2649  
2650 # Shift %1..%9 (used by _expand $1..$9)
2651 17 for my $n (1 .. 8) {
2652 136 my $next = BATsh::Env->get('%' . ($n + 1));
2653 136 ▶ BATsh::Env->set('%' . $n, defined($next) ? $next : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $next ? : 136 0 136
2654 }
2655 17 BATsh::Env->set('%9', '');
2656  
2657 # Rebuild %*
2658 17 my @args;
2659 17 for my $n (1 .. 9) {
2660 58 my $v = BATsh::Env->get("%$n");
2661 58 ▶ last unless defined $v && $v ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and $v ne "" 17 41 58
Condition66%2/3
defined $v and $v ne ""
!ll&&!rl&&r
01741
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
2662 41 push @args, $v;
2663 }
2664 17 BATsh::Env->set('%*', join(' ', @args));
2665 }
2666 9 $LAST_STATUS = 0;
2667 9 return 0;
2668 }
2669  
2670 # ----------------------------------------------------------------
2671 # getopts optstring name [arg ...]
2672 # ----------------------------------------------------------------
2673 # POSIX option parser. Called once per option in a loop:
2674 #
2675 # while getopts "ab:c" opt; do
2676 # case $opt in
2677 # a) ... ;;
2678 # b) echo "$OPTARG" ;;
2679 # c) ... ;;
2680 # \?) echo "bad option" >&2 ;;
2681 # esac
2682 # done
2683 # shift $((OPTIND - 1))
2684 #
2685 # Sets the variable named by the second argument to the option letter
2686 # found (or '?' on an unknown option, or ':' on a missing argument when
2687 # the optstring begins with ':'). OPTARG receives an option's argument;
2688 # OPTIND advances to the next argument to be processed. Returns 0 while
2689 # an option was parsed, non-zero when the option list is exhausted.
2690 #
2691 # A leading ':' in optstring selects "silent" error reporting: getopts
2692 # does not print a diagnostic, sets the name variable to ':' for a
2693 # missing argument and '?' for an unknown option, and puts the offending
2694 # letter in OPTARG. Otherwise getopts prints a message to STDERR, sets
2695 # the name variable to '?', and unsets OPTARG.
2696 #
2697 # If no [arg ...] words are given, the positional parameters $1..$9 are
2698 # parsed (BATsh stores them as %1..%9; the same first-empty-terminates
2699 # convention used elsewhere in this module applies, so an intentionally
2700 # empty positional parameter is not representable).
2701 #
2702 # All Perl 5.005_03 compatible: substr/index scanning, no regex features
2703 # beyond \A \z, no prototypes, 2-argument state via package variables.
2704 70 ▶sub _cmd_getopts {
Subroutine _cmd_getopts: called CC=30 SCAR=34.1
2705 70 my ($rest) = @_;
2706 70 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 70 70
2707 70 $rest =~ s/\A\s+//;
2708 70 $rest =~ s/\s+\z//;
2709  
2710 # Split off optstring and name; the remainder (if any) are explicit
2711 # args. _parse_args handles quoting/word-splitting consistently with
2712 # the rest of the interpreter.
2713 70 my @words = _parse_args($rest);
2714 70 ▶ if (@words < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@words < 2) 2 68 70
2715 2 print STDERR "getopts: usage: getopts optstring name [arg ...]\n";
2716 2 $LAST_STATUS = 2;
2717 2 return 2;
2718 }
2719 68 my $optstring = shift @words;
2720 68 my $name = shift @words;
2721  
2722 # Argument list: explicit words, else the positional parameters.
2723 68 my @args;
2724 68 ▶ if (@words) {
Branch: 1 branch
BranchTrueFalseTotal
if (@words) { } 39 29 68
2725 39 @args = @words;
2726 }
2727 else {
2728 29 for my $n (1 .. 9) {
2729 113 my $v = BATsh::Env->get("%$n");
2730 113 ▶ $v = BATsh::Env->get("BATSH_ARG$n") unless defined $v && $v ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and $v ne "" 29 84 113
Condition66%2/3
defined $v and $v ne ""
!ll&&!rl&&r
02984
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
2731 113 ▶ last unless defined $v && $v ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and $v ne "" 29 84 113
Condition66%2/3
defined $v and $v ne ""
!ll&&!rl&&r
02984
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
2732 84 push @args, $v;
2733 }
2734 }
2735  
2736 # Silent-error mode: a leading ':' in the optstring.
2737 68 my $silent = 0;
2738 68 ▶ if (index($optstring, ':') == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($optstring, ":") == 0) 15 53 68
2739 15 $silent = 1;
2740 15 $optstring = substr($optstring, 1);
2741 }
2742  
2743 # OPTIND is 1-based and defaults to 1. If the caller reset it (or it
2744 # has never been set), restart the intra-argument character offset.
2745 68 my $optind = BATsh::Env->get('OPTIND');
2746 68 ▶ $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 21 47 68
Condition66%4/6
defined $optind and $optind =~ /\A\d+\z/ and $optind >= 1
!ll&&!rl&&r
21047
defined $optind and $optind =~ /\A\d+\z/
!ll&&!rl&&r
21047
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
2747 68 ▶ if ($optind != $_GETOPTS_LAST_OPTIND) {
Branch: 1 branch
BranchTrueFalseTotal
if ($optind != $BATsh::SH::_GETOPTS_LAST_OPTIND) 29 39 68
2748 29 $_GETOPTS_CHARPOS = 0; # fresh loop, or manual OPTIND change
2749 }
2750  
2751 # End of arguments?
2752 68 ▶ if ($optind > scalar(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($optind > scalar @args) 20 48 68
2753 20 $_GETOPTS_CHARPOS = 0;
2754 20 BATsh::Env->set('OPTIND', $optind);
2755 20 $_GETOPTS_LAST_OPTIND = $optind;
2756 20 $LAST_STATUS = 1;
2757 20 return 1;
2758 }
2759  
2760 48 my $cur = $args[$optind - 1];
2761 48 ▶ $cur = '' unless defined $cur;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cur 0 48 48
2762  
2763 # Starting a new argument: must begin with '-' and not be a bare '-'.
2764 48 ▶ if ($_GETOPTS_CHARPOS == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BATsh::SH::_GETOPTS_CHARPOS == 0) 45 3 48
2765 45 ▶ if (index($cur, '-') != 0 || $cur eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (index($cur, "-") != 0 or $cur eq "-") 7 38 45
Condition66%2/3
index($cur, "-") != 0 or $cur eq "-"
l!l&&r!l&&!r
7038
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
2766 # Not an option word: stop (leave OPTIND on it).
2767 7 BATsh::Env->set('OPTIND', $optind);
2768 7 $_GETOPTS_LAST_OPTIND = $optind;
2769 7 $LAST_STATUS = 1;
2770 7 return 1;
2771 }
2772 38 ▶ if ($cur eq '--') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur eq "--") 1 37 38
2773 # Explicit end of options: consume it and stop.
2774 1 $optind++;
2775 1 BATsh::Env->set('OPTIND', $optind);
2776 1 $_GETOPTS_LAST_OPTIND = $optind;
2777 1 $_GETOPTS_CHARPOS = 0;
2778 1 $LAST_STATUS = 1;
2779 1 return 1;
2780 }
2781 37 $_GETOPTS_CHARPOS = 1; # skip the leading '-'
2782 }
2783  
2784 40 my $optchar = substr($cur, $_GETOPTS_CHARPOS, 1);
2785 40 $_GETOPTS_CHARPOS++;
2786 40 my $is_last_char = ($_GETOPTS_CHARPOS >= length($cur));
2787  
2788 # Advance OPTIND past $cur once its last option character is consumed.
2789 26 ▶ my $advance = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2790 26 $optind++;
2791 26 $_GETOPTS_CHARPOS = 0;
2792 40 };
2793  
2794 # Look the option character up in the optstring. ':' and '?' are
2795 # never valid option letters.
2796 40 ▶ my $pos = ($optchar eq ':' || $optchar eq '?') ? -1
Branch: 1 branch
BranchTrueFalseTotal
$optchar eq ':' || $optchar eq '?' ? : 0 40 40
Condition33%1/3
$optchar eq ":" or $optchar eq "?"
l!l&&r!l&&!r
0040
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
2797 : index($optstring, $optchar);
2798  
2799 40 ▶ if ($pos < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < 0) 5 35 40
2800 # Unknown option.
2801 5 ▶ if ($silent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($silent) { } 3 2 5
2802 3 BATsh::Env->set($name, '?');
2803 3 BATsh::Env->set('OPTARG', $optchar);
2804 }
2805 else {
2806 2 print STDERR BATsh::MB::dec("getopts: illegal option -- $optchar") . "\n";
2807 2 BATsh::Env->set($name, '?');
2808 2 BATsh::Env->set('OPTARG', '');
2809 }
2810 5 ▶ $advance->() if $is_last_char;
Branch: 1 branch
BranchTrueFalseTotal
if $is_last_char 5 0 5
2811 5 BATsh::Env->set('OPTIND', $optind);
2812 5 $_GETOPTS_LAST_OPTIND = $optind;
2813 5 $LAST_STATUS = 0;
2814 5 return 0;
2815 }
2816  
2817 # Does this option take an argument? (a ':' follows it in optstring)
2818 35 my $needs_arg = (substr($optstring, $pos + 1, 1) eq ':');
2819  
2820 35 ▶ if ($needs_arg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needs_arg) { } 19 16 35
2821 19 ▶ if (!$is_last_char) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_last_char) { } 3 16 19
2822 # -oVALUE : remainder of the current word is the argument.
2823 3 BATsh::Env->set('OPTARG', substr($cur, $_GETOPTS_CHARPOS));
2824 3 BATsh::Env->set($name, $optchar);
2825 3 $advance->();
2826 }
2827 else {
2828 # -o VALUE : the next word is the argument.
2829 16 ▶ if ($optind + 1 > scalar(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($optind + 1 > scalar @args) 5 11 16
2830 # Missing argument.
2831 5 $advance->();
2832 5 ▶ if ($silent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($silent) { } 3 2 5
2833 3 BATsh::Env->set($name, ':');
2834 3 BATsh::Env->set('OPTARG', $optchar);
2835 }
2836 else {
2837 2 print STDERR BATsh::MB::dec("getopts: option requires an argument -- $optchar") . "\n";
2838 2 BATsh::Env->set($name, '?');
2839 2 BATsh::Env->set('OPTARG', '');
2840 }
2841 5 BATsh::Env->set('OPTIND', $optind);
2842 5 $_GETOPTS_LAST_OPTIND = $optind;
2843 5 $LAST_STATUS = 0;
2844 5 return 0;
2845 }
2846 11 BATsh::Env->set('OPTARG', $args[$optind]); # 0-based next word
2847 11 BATsh::Env->set($name, $optchar);
2848 11 $optind += 2;
2849 11 $_GETOPTS_CHARPOS = 0;
2850 }
2851 }
2852 else {
2853 # Flag option, no argument. bash unsets OPTARG; we clear it.
2854 16 BATsh::Env->set($name, $optchar);
2855 16 BATsh::Env->set('OPTARG', '');
2856 16 ▶ $advance->() if $is_last_char;
Branch: 1 branch
BranchTrueFalseTotal
if $is_last_char 13 3 16
2857 }
2858  
2859 30 BATsh::Env->set('OPTIND', $optind);
2860 30 $_GETOPTS_LAST_OPTIND = $optind;
2861 30 $LAST_STATUS = 0;
2862 30 return 0;
2863 }
2864  
2865 # ----------------------------------------------------------------
2866 # local
2867 # ----------------------------------------------------------------
2868 3 ▶sub _cmd_local {
Subroutine _cmd_local: called CC=7 SCAR=20.2
2869 3 my ($rest) = @_;
2870 3 $rest =~ s/\A\s+//;
2871  
2872 3 my ($var, $val);
2873 3 ▶ 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) { } 2 1 3
elsif ($rest =~ /\A([A-Za-z_][A-Za-z0-9_]*)\s*\z/) { } 1 0 1
2874 2 ($var, $val) = ($1, $2);
2875 # Strip surrounding quotes from value
2876 2 $val =~ s/\A"(.*)"\z/$1/s;
2877 2 $val =~ s/\A'(.*)'\z/$1/s;
2878 }
2879 elsif ($rest =~ /\A([A-Za-z_][A-Za-z0-9_]*)\s*\z/) {
2880 1 $var = $1;
2881 1 $val = BATsh::Env->get($var);
2882 1 ▶ $val = '' unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 1 1
2883 }
2884 else {
2885 0 $LAST_STATUS = 0;
2886 0 return 0;
2887 }
2888  
2889 # Save old value in innermost function scope so it can be restored on return
2890 3 ▶ if (@FUNCTION_STACK) {
Branch: 1 branch
BranchTrueFalseTotal
if (@FUNCTION_STACK) 3 0 3
2891 3 my $frame = $FUNCTION_STACK[-1];
2892 # Only save once per variable per frame (first local declaration wins)
2893 3 ▶ unless (exists $frame->{$var}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $frame->{$var}) 3 0 3
2894 3 my $old = BATsh::Env->get($var);
2895 3 ▶ $frame->{$var} = defined $old ? $old : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $old ? : 3 0 3
2896 }
2897 }
2898 3 BATsh::Env->set($var, $val);
2899 3 $LAST_STATUS = 0;
2900 3 return 0;
2901 }
2902  
2903 # ----------------------------------------------------------------
2904 # set (sh set options -- minimal implementation)
2905 # ----------------------------------------------------------------
2906 # _sh_set_positional: replace $1..$9 / $@ / $* with a new argument list.
2907 # The interpreter keeps the positional parameters in %1..%9 (mirrored in
2908 # BATSH_ARG1..9 for the legacy CMD-side view) and their space-joined form
2909 # in %*, exactly as a function call and "shift" do, so "set --" reuses
2910 # that representation instead of inventing a second one. OPTIND is
2911 # deliberately NOT reset: bash leaves it alone too, and a script that
2912 # re-parses a fresh list resets it itself.
2913 12 ▶sub _sh_set_positional {
Subroutine _sh_set_positional: called CC=3 SCAR=11.0
2914 12 my ($args_ref) = @_;
2915 12 my @args = @{$args_ref};
2916 12 for my $n (1 .. 9) {
2917 108 ▶ my $v = defined $args[$n-1] ? $args[$n-1] : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $args[$n - 1] ? : 27 81 108
2918 108 BATsh::Env->set("%$n", $v);
2919 108 BATsh::Env->set("BATSH_ARG$n", $v);
2920 }
2921 12 BATsh::Env->set('%*', join(' ', @args));
2922 12 return 1;
2923 }
2924  
2925 27 ▶sub _cmd_set_sh {
Subroutine _cmd_set_sh: called CC=16 SCAR=28.3
2926 27 my ($rest) = @_;
2927 27 $rest =~ s/\A\s+//;
2928 27 $rest =~ s/\s+\z//;
2929 # set -e/+e -u/+u -x/+x, combinable (-eux), and set -o/+o NAME
2930 # (errexit / nounset / xtrace). Unknown option letters are accepted
2931 # silently.
2932 #
2933 # POSIX operand handling (v0.09): "set -- [ARG ...]" replaces the
2934 # positional parameters with ARG ... (and clears them when no ARG is
2935 # given), and so does a first word that is not an option, as in
2936 # "set a b c". Until v0.09 every operand form was silently ignored,
2937 # so the common
2938 # set -- -f value ; while getopts f: opt ; do ... done
2939 # idiom saw an empty argument list. Words are split on unquoted
2940 # whitespace and dequoted, so "set -- 'a b' c" sets exactly two
2941 # parameters.
2942 27 my @words = map { _arr_dequote($_) } _arr_split_words($rest);
2943 27 my @pos = ();
2944 27 my $set_pos = 0;
2945 27 ▶ while (@words) {
Branch: 1 branch
BranchTrueFalseTotal
if (@words) 28 15 43
2946 28 my $w = shift @words;
2947 28 ▶ if ($w eq '--') {
Branch: 1 branch
BranchTrueFalseTotal
if ($w eq "--") 11 17 28
2948 11 $set_pos = 1;
2949 11 @pos = @words;
2950 11 @words = ();
2951 11 last;
2952 }
2953  
2954 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
2955 1 ▶ my $on = ($1 eq '-') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '-' ? : 1 0 1
2956 1 my $name = lc(shift @words);
2957 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
2958 0 elsif ($name eq 'nounset') { $_OPT_NOUNSET = $on }
2959 0 elsif ($name eq 'xtrace') { $_OPT_XTRACE = $on }
2960 1 next;
2961 }
2962 16 ▶ if ($w =~ /\A([-+])([a-zA-Z]+)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w =~ /\A([-+])([a-zA-Z]+)\z/) 15 1 16
2963 15 ▶ my $on = ($1 eq '-') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '-' ? : 13 2 15
2964 15 for my $ch (split //, $2) {
2965 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
2966 2 elsif ($ch eq 'u') { $_OPT_NOUNSET = $on }
2967 2 elsif ($ch eq 'x') { $_OPT_XTRACE = $on }
2968 }
2969 15 next;
2970 }
2971 # Not an option: this word and every word after it are the new
2972 # positional parameters.
2973 1 $set_pos = 1;
2974 1 @pos = ($w, @words);
2975 1 @words = ();
2976 1 last;
2977 }
2978 27 ▶ _sh_set_positional(\@pos) if $set_pos;
Branch: 1 branch
BranchTrueFalseTotal
if $set_pos 12 15 27
2979 27 $LAST_STATUS = 0;
2980 27 return 0;
2981 }
2982  
2983 # ----------------------------------------------------------------
2984 # shopt -- bash shell option toggle (v0.07, minimal implementation)
2985 #
2986 # Recognised forms:
2987 # shopt -s extglob enable extended pattern matching operators
2988 # ?(...) *(...) +(...) @(...) !(...) in case
2989 # patterns and in ${VAR#pat}/${VAR%pat}/... patterns
2990 # shopt -u extglob disable it (the bash default)
2991 # shopt -p extglob print "shopt -s extglob" or "shopt -u extglob"
2992 # shopt extglob print "extglob on" / "extglob off"
2993 # shopt print the state of all known options
2994 #
2995 # Only "extglob" is modelled; any other option name is accepted silently
2996 # (queried as "off") so scripts that probe unrelated options do not abort.
2997 # ----------------------------------------------------------------
2998 4 ▶sub _cmd_shopt {
Subroutine _cmd_shopt: called CC=15 SCAR=38.7
2999 4 my ($rest) = @_;
3000 4 $rest =~ s/\A\s+//; $rest =~ s/\s+\z//;
3001 4 my @words = split /\s+/, $rest;
3002 4 ▶ if (!@words) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@words) 1 3 4
3003 1 ▶ print "extglob " . ($_OPT_EXTGLOB ? 'on' : 'off') . "\n";
Branch: 1 branch
BranchTrueFalseTotal
$_OPT_EXTGLOB ? : 1 0 1
3004 1 $LAST_STATUS = 0;
3005 1 return 0;
3006 }
3007 3 my $mode = ''; # '-s' / '-u' / '-p' / '' (bare query)
3008 3 ▶ if ($words[0] =~ /\A-[sup]\z/) { $mode = shift(@words) }
Branch: 1 branch
BranchTrueFalseTotal
if ($words[0] =~ /\A-[sup]\z/) 3 0 3
3009 3 ▶ if (!@words) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@words) 0 3 3
3010 # "shopt -p" / "shopt -s" / "shopt -u" with no names: report/no-op
3011 0 ▶ if ($mode eq '-p') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "-p") 0 0 0
3012 0 ▶ print 'shopt -' . ($_OPT_EXTGLOB ? 's' : 'u') . " extglob\n";
Branch: 1 branch
BranchTrueFalseTotal
$_OPT_EXTGLOB ? : 0 0 0
3013 }
3014 0 $LAST_STATUS = 0;
3015 0 return 0;
3016 }
3017 3 my $status = 0;
3018 3 for my $name (@words) {
3019 3 my $lc_name = lc($name);
3020 3 ▶ if ($lc_name ne 'extglob') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_name ne "extglob") 0 3 3
3021 # Unknown option name: treated as always-off, matching bash's
3022 # exit-status-1-on-unknown-name behaviour without aborting.
3023 0 ▶ if ($mode eq '') { print "$name off\n" }
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "") 0 0 0
3024 0 $status = 1;
3025 0 next;
3026 }
3027 3 ▶ if ($mode eq '-s') { $_OPT_EXTGLOB = 1 }
Branch: 3 branches
BranchTrueFalseTotal
if ($mode eq '-s') { } 3 0 3
elsif ($mode eq '-u') { } 0 0 0
elsif ($mode eq '-p') { } 0 0 0
3028 0 elsif ($mode eq '-u') { $_OPT_EXTGLOB = 0 }
3029 0 ▶ elsif ($mode eq '-p') { print 'shopt -' . ($_OPT_EXTGLOB ? 's' : 'u') . " extglob\n" }
Branch: 1 branch
BranchTrueFalseTotal
$_OPT_EXTGLOB ? : 0 0 0
3030 0 ▶ else { print "extglob " . ($_OPT_EXTGLOB ? 'on' : 'off') . "\n" }
Branch: 1 branch
BranchTrueFalseTotal
$_OPT_EXTGLOB ? : 0 0 0
3031 }
3032 3 $LAST_STATUS = $status;
3033 3 return $status;
3034 }
3035  
3036 # ----------------------------------------------------------------
3037 # alias / unalias (v0.07, minimal implementation)
3038 #
3039 # alias list all aliases as alias NAME='VALUE'
3040 # alias NAME print one alias, or an error if not set
3041 # alias NAME=VALUE ... define one or more aliases (quote-aware)
3042 # unalias NAME ... remove one or more aliases
3043 # unalias -a remove all aliases
3044 # ----------------------------------------------------------------
3045 8 ▶sub _cmd_alias {
Subroutine _cmd_alias: called CC=8 SCAR=23.0
3046 8 my ($rest) = @_;
3047 8 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 8 8
3048 8 $rest =~ s/\A\s+//; $rest =~ s/\s+\z//;
3049 8 ▶ if ($rest eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest eq "") 0 8 8
3050 0 for my $name (sort keys %_SH_ALIAS) {
3051 0 print "alias $name='" . $_SH_ALIAS{$name} . "'\n";
3052 }
3053 0 $LAST_STATUS = 0;
3054 0 return 0;
3055 }
3056 8 my @tok = _arr_split_words($rest);
3057 8 my $status = 0;
3058 8 for my $t (@tok) {
3059 8 ▶ 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) { } 7 1 8
elsif ($t =~ /\A([A-Za-z_][A-Za-z0-9_.:-]*)\z/) { } 1 0 1
3060 7 my ($name, $val) = ($1, $2);
3061 7 $_SH_ALIAS{$name} = _arr_dequote($val);
3062 }
3063 elsif ($t =~ /\A([A-Za-z_][A-Za-z0-9_.:-]*)\z/) {
3064 1 my $name = $1;
3065 1 ▶ if (exists $_SH_ALIAS{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $BATsh::SH::_SH_ALIAS{$name}) { } 1 0 1
3066 1 print "alias $name='" . $_SH_ALIAS{$name} . "'\n";
3067 }
3068 else {
3069 0 print STDERR "sh: alias: $name: not found\n";
3070 0 $status = 1;
3071 }
3072 }
3073 }
3074 8 $LAST_STATUS = $status;
3075 8 return $status;
3076 }
3077  
3078 1 ▶sub _cmd_unalias {
Subroutine _cmd_unalias: called CC=6 SCAR=20.4
3079 1 my ($rest) = @_;
3080 1 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 1 1
3081 1 $rest =~ s/\A\s+//; $rest =~ s/\s+\z//;
3082 0 ▶ if ($rest eq '-a') { %_SH_ALIAS = (); $LAST_STATUS = 0; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($rest eq "-a") 0 1 1
3083 1 my $status = 0;
3084 1 for my $name (split /\s+/, $rest) {
3085 1 ▶ next if $name eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "" 0 1 1
3086 1 ▶ if (exists $_SH_ALIAS{$name}) { delete $_SH_ALIAS{$name} }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $BATsh::SH::_SH_ALIAS{$name}) { } 1 0 1
3087 0 else { print STDERR "sh: unalias: $name: not found\n"; $status = 1 }
3088 }
3089 1 $LAST_STATUS = $status;
3090 1 return $status;
3091 }
3092  
3093 # _alias_expand_line: replace a leading alias name with its stored text.
3094 # See the call site in _exec_line() for the chaining / loop-guard notes.
3095 1744 ▶sub _alias_expand_line {
Subroutine _alias_expand_line: called CC=6 SCAR=18.0
3096 1744 my ($line) = @_;
3097 1744 ▶ return $line unless %_SH_ALIAS;
Branch: 1 branch
BranchTrueFalseTotal
unless %BATsh::SH::_SH_ALIAS 1643 101 1744
3098 101 my %seen;
3099 101 my $out = $line;
3100 101 my $guard = 0;
3101 101 ▶ while ($guard < 20) {
Branch: 1 branch
BranchTrueFalseTotal
if ($guard < 20) 104 0 104
3102 104 $guard++;
3103 104 ▶ last unless $out =~ /\A(\s*)(\S+)(.*)\z/s;
Branch: 1 branch
BranchTrueFalseTotal
unless $out =~ /\A(\s*)(\S+)(.*)\z/s 0 104 104
3104 104 my ($lead, $w0, $tail) = ($1, $2, $3);
3105 104 ▶ last unless exists $_SH_ALIAS{$w0};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BATsh::SH::_SH_ALIAS{$w0} 101 3 104
3106 3 ▶ last if $seen{$w0};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$w0} 0 3 3
3107 3 $seen{$w0} = 1;
3108 3 $out = $lead . $_SH_ALIAS{$w0} . $tail;
3109 }
3110 101 return $out;
3111 }
3112  
3113 # ----------------------------------------------------------------
3114 # reset_sh_options: restore default shell options. Called by BATsh.pm
3115 # at the start of each top-level run so one script's "set -e" does not
3116 # leak into the next. Call as a plain function.
3117 # ----------------------------------------------------------------
3118 554 ▶sub reset_sh_options {
Subroutine reset_sh_options: called CC=1 SCAR=0.0
Pod undocumented
3119 554 $_OPT_ERREXIT = 0;
3120 554 $_OPT_NOUNSET = 0;
3121 554 $_OPT_XTRACE = 0;
3122 554 $_OPT_EXTGLOB = 0;
3123 554 $_ERREXIT_HOLD = 0;
3124 554 $_ERREXIT_DONE = 0;
3125 # getopts loop state (v0.07): a fresh top-level run restarts option
3126 # parsing from the first argument, so one script's half-finished
3127 # getopts loop cannot leak its position into the next run.
3128 554 $_GETOPTS_CHARPOS = 0;
3129 554 $_GETOPTS_LAST_OPTIND = 0;
3130 # Variable attributes (v0.08) are script-scoped, like the options
3131 # above: a fresh top-level run starts with no readonly / integer
3132 # markings so one script's "readonly X" cannot leak into the next.
3133 554 %_SH_READONLY = ();
3134 554 %_SH_INTATTR = ();
3135 554 return 0;
3136 }
3137  
3138 # ----------------------------------------------------------------
3139 # _errexit_check: under set -e, a failing simple command terminates the
3140 # script with its status. Suppressed while $_ERREXIT_HOLD > 0 (an if/
3141 # while/until condition, or a non-final member of a && / || list) and
3142 # when a control transfer (exit/break/continue/return) is already active.
3143 # ----------------------------------------------------------------
3144 1562 ▶sub _errexit_check {
Subroutine _errexit_check: called CC=10 SCAR=25.9
3145 1562 my ($rc) = @_;
3146 1562 ▶ return $rc unless $_OPT_ERREXIT;
Branch: 1 branch
BranchTrueFalseTotal
unless $_OPT_ERREXIT 1539 23 1562
3147 23 ▶ return $rc if $_ERREXIT_HOLD;
Branch: 1 branch
BranchTrueFalseTotal
if $_ERREXIT_HOLD 1 22 23
3148 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
3149 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
3150 7 $_EXIT_CODE = $rc;
3151 7 $LAST_STATUS = $rc;
3152 7 return $rc;
3153 }
3154  
3155 # ----------------------------------------------------------------
3156 # _nounset_hit: under set -u, expanding an unset variable is an error
3157 # that terminates the script with status 1 (bash prints the message and
3158 # aborts; here the current command still completes with '' before the
3159 # script stops -- see BUGS AND LIMITATIONS). A noop while -u is off.
3160 # ----------------------------------------------------------------
3161 2 ▶sub _nounset_hit {
Subroutine _nounset_hit: called CC=3 SCAR=11.0
3162 2 my ($name) = @_;
3163 2 ▶ return 0 unless $_OPT_NOUNSET;
Branch: 1 branch
BranchTrueFalseTotal
unless $_OPT_NOUNSET 1 1 2
3164 1 ▶ return 0 if defined $_EXIT_CODE; # report only the first hit
Branch: 1 branch
BranchTrueFalseTotal
if defined $_EXIT_CODE 0 1 1
3165 1 warn "sh: $name: unbound variable\n";
3166 1 $_EXIT_CODE = 1;
3167 1 $LAST_STATUS = 1;
3168 1 return 1;
3169 }
3170  
3171 # ----------------------------------------------------------------
3172 # source / .
3173 # ----------------------------------------------------------------
3174 4 ▶sub _cmd_source {
Subroutine _cmd_source: called CC=3 SCAR=11.5
3175 4 my ($class, $rest, $opts_ref) = @_;
3176 4 $rest =~ s/\A\s+//;
3177 4 $rest =~ s/\s+\z//;
3178 # Strip quotes exactly as _cmd_cd does, so that
3179 # source "$dir/init.batsh" . 'my script.batsh'
3180 # reach source_file() as plain paths. Before 0.11 the operand was
3181 # passed through with its quote characters still attached and the
3182 # open failed with them embedded in the name -- which made the two
3183 # builtins disagree: cd accepted a quoted operand and source did
3184 # not, so the obvious way to write a path containing a space was
3185 # correct for one and wrong for the other. An UNQUOTED operand
3186 # keeps working as before, spaces and all: this dequotes, it does
3187 # not word-split.
3188 4 $rest = _arr_dequote($rest);
3189 4 ▶ if (defined $opts_ref->{'_batsh'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts_ref->{"_batsh"}) 4 0 4
3190 4 eval { $opts_ref->{'_batsh'}->source_file($rest) };
3191 0 ▶ if ($@) { print STDERR "source: $rest: $@\n"; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
3192 }
3193 4 return 0;
3194 }
3195  
3196 # ----------------------------------------------------------------
3197 # test / [ ]
3198 # ----------------------------------------------------------------
3199 77 ▶sub _cmd_test {
Subroutine _cmd_test: called CC=2 SCAR=6.9
3200 77 my ($rest) = @_;
3201 77 $rest =~ s/\A\s+//;
3202 77 $rest =~ s/\s*\]\s*\z//; # strip trailing ]
3203 77 my $result = _eval_test($rest);
3204 77 ▶ $LAST_STATUS = $result ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$result ? : 51 26 77
3205 77 return $LAST_STATUS;
3206 }
3207  
3208 77 ▶sub _eval_test {
Subroutine _eval_test: called CC=59 SCAR=59.1
3209 77 my ($expr) = @_;
3210 77 $expr =~ s/\A\s+//;
3211 77 $expr =~ s/\s+\z//;
3212  
3213 # Compound: -a (AND), -o (OR)
3214 77 ▶ if ($expr =~ /^(.*)\s+-a\s+(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.*)\s+-a\s+(.*)$/) 0 77 77
3215 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
3216 }
3217 77 ▶ if ($expr =~ /^(.*)\s+-o\s+(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.*)\s+-o\s+(.*)$/) 0 77 77
3218 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
3219 }
3220 # Negation
3221 77 ▶ if ($expr =~ /^!\s+(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^!\s+(.*)$/) 0 77 77
3222 0 return !_eval_test($1);
3223 }
3224  
3225 # File tests
3226 77 ▶ if ($expr =~ /\A(-[a-z])\s+(.+)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /\A(-[a-z])\s+(.+)\z/) 9 68 77
3227 9 my ($op, $path) = ($1, $2);
3228 # Quote removal (which is also what turns a backslash that came
3229 # out of an expansion back into a literal one) follows the same
3230 # rules as for command words: test compares its operands
3231 # literally, so a half-stripped "C:\dir"/sub would never name
3232 # an existing file.
3233 9 $path = _arr_dequote($path);
3234 9 $path = BATsh::MB::dec($path);
3235 0 ▶ if ($op eq '-e') { return -e $path ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-e") 0 9 9
-e $path ? : 0 0 0
3236 9 ▶ if ($op eq '-f') { return -f $path ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-f") 2 7 9
-f $path ? : 2 0 2
3237 7 ▶ if ($op eq '-d') { return -d $path ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-d") 2 5 7
-d $path ? : 2 0 2
3238 0 ▶ if ($op eq '-r') { return -r $path ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-r") 0 5 5
-r $path ? : 0 0 0
3239 0 ▶ if ($op eq '-w') { return -w $path ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-w") 0 5 5
-w $path ? : 0 0 0
3240 0 ▶ if ($op eq '-x') { return -x $path ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-x") 0 5 5
-x $path ? : 0 0 0
3241 0 ▶ if ($op eq '-s') { return (-s $path) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-s") 0 5 5
-s $path ? : 0 0 0
3242 5 ▶ if ($op eq '-z') { my $s = -s $path; return (!defined $s || $s == 0) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-z") 3 2 5
!defined($s) || $s == 0 ? : 3 0 3
Condition33%1/3
not defined $s or $s == 0
l!l&&r!l&&!r
300
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
3243 2 ▶ if ($op eq '-n') { return (length($path) > 0) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-n") 2 0 2
length $path > 0 ? : 2 0 2
3244 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
3245 }
3246  
3247 # String comparisons: = == != < >
3248 68 ▶ if ($expr =~ /\A(.+?)\s+(=|==|!=|<|>)\s+(.+)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /\A(.+?)\s+(=|==|!=|<|>)\s+(.+)\z/) 7 61 68
3249 7 my ($a, $op, $b) = ($1, $2, $3);
3250 7 $a = _arr_dequote($a);
3251 7 $b = _arr_dequote($b);
3252 7 ▶ if ($op eq '=' || $op eq '==') { return ($a eq $b) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "=" or $op eq "==") 6 1 7
$a eq $b ? : 5 1 6
Condition66%2/3
$op eq "=" or $op eq "=="
l!l&&r!l&&!r
601
MC/DC50%1/2
$op eq "=" or $op eq "=="
$op eq "=" $op eq "=="
Truth table66%2/3
A or B
A= $op eq "="B= $op eq "=="
ABresult
1X1
011
000
3253 1 ▶ if ($op eq '!=') { return ($a ne $b) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "!=") 1 0 1
$a ne $b ? : 1 0 1
3254 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
3255 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
3256 }
3257  
3258 # Integer comparisons: -eq -ne -lt -le -gt -ge
3259 61 ▶ 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/) 61 0 61
3260 61 my ($a, $op, $b) = ($1, $2, $3);
3261 61 $a = _arr_dequote($a);
3262 61 $b = _arr_dequote($b);
3263 61 ▶ $a = int($a) if $a =~ /\A-?\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $a =~ /\A-?\d+\z/ 61 0 61
3264 61 ▶ $b = int($b) if $b =~ /\A-?\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $b =~ /\A-?\d+\z/ 61 0 61
3265 61 ▶ if ($op eq '-eq') { return ($a == $b) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-eq") 17 44 61
$a == $b ? : 6 11 17
3266 44 ▶ if ($op eq '-ne') { return ($a != $b) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-ne") 1 43 44
$a != $b ? : 1 0 1
3267 43 ▶ if ($op eq '-lt') { return ($a < $b) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-lt") 14 29 43
$a < $b ? : 11 3 14
3268 29 ▶ if ($op eq '-le') { return ($a <= $b) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-le") 17 12 29
$a <= $b ? : 12 5 17
3269 12 ▶ if ($op eq '-gt') { return ($a > $b) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-gt") 4 8 12
$a > $b ? : 3 1 4
3270 8 ▶ if ($op eq '-ge') { return ($a >= $b) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-ge") 8 0 8
$a >= $b ? : 3 5 8
3271 }
3272  
3273 # -n string (non-empty)
3274 0 ▶ if ($expr =~ /\A-n\s+(.+)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /\A-n\s+(.+)\z/) 0 0 0
3275 0 my $s = _arr_dequote($1);
3276 0 ▶ return length($s) > 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $s > 0 ? : 0 0 0
3277 }
3278 # -z string (empty)
3279 0 ▶ if ($expr =~ /\A-z\s+(.+)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /\A-z\s+(.+)\z/) 0 0 0
3280 0 my $s = _arr_dequote($1);
3281 0 ▶ return length($s) == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $s == 0 ? : 0 0 0
3282 }
3283  
3284 # bare string: true if non-empty
3285 0 $expr = _arr_dequote($expr);
3286 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
3287 }
3288  
3289 # ----------------------------------------------------------------
3290 # if/then/else/elif/fi parser
3291 # ----------------------------------------------------------------
3292 82 ▶sub _parse_if {
Subroutine _parse_if: called CC=20 SCAR=30.0
3293 82 my ($class, $lines_ref, $start, $opts_ref) = @_;
3294 82 my @lines = @{$lines_ref};
3295 82 my $i = $start;
3296  
3297 # Collect: if cond; then ... [elif cond; then ...] [else ...] fi
3298 # Build a structure: [ ['cond_lines'], ['body_lines'] ] ...
3299 82 my @branches = (); # [ [$cond_lines], [$body_lines] ]
3300 82 my $else_body = undef;
3301  
3302 # First line: if cond; then
3303 82 my $if_line = $lines[$i]; $i++;
3304 82 $if_line =~ s/\r?\n\z//; $if_line =~ s/\A\s+//;
3305  
3306 # Fully-inline form on ONE physical line:
3307 # if COND; then BODY... [elif COND; then BODY...] [else BODY...]; fi
3308 # Detected by a bare "fi" appearing as a top-level ';' segment. The
3309 # line is expanded into the logical-line shape the multi-line block
3310 # collector below already understands (with then/do/else peeled onto
3311 # their own lines), then re-parsed; this correctly handles elif and
3312 # else, which the previous narrow single-line regex silently dropped.
3313 82 ▶ if (_inline_has_terminator($if_line, 'fi')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_inline_has_terminator($if_line, "fi")) 30 52 82
3314 30 my @work = _inline_expand($if_line);
3315 30 my ($st) = _parse_if($class, \@work, 0, $opts_ref);
3316 30 return ($st, $i);
3317 }
3318  
3319 # Extract condition (after 'if', before 'then' or ';')
3320 52 my $cond_str = $if_line;
3321 52 $cond_str =~ s/\Aif\s+//i;
3322  
3323 52 $cond_str =~ s/\s*;\s*then\s*\z//i;
3324 52 $cond_str =~ s/\s+then\s*\z//i;
3325  
3326 52 my @cond_lines = ($cond_str);
3327 52 my @body_lines = ();
3328 52 my $in_else = 0; # collecting the else-branch body
3329 # Nested if/fi depth: a nested "if ... fi" inside a branch body (on
3330 # its own lines or written inline) must not have its "fi"/"elif"/
3331 # "else" mistaken for the outer if's. Only the depth-1 keywords are
3332 # the outer if's; deeper ones travel with the body and are re-parsed
3333 # when the body runs.
3334 52 my $depth = 1;
3335  
3336 52 ▶ while ($i <= $#lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#lines) 169 0 169
3337 169 my $l = $lines[$i]; $i++;
3338 169 $l =~ s/\r?\n\z//;
3339 169 my $ls = $l; $ls =~ s/\A\s+//;
3340 169 ▶ my $lc_first = lc( ($ls =~ /\A(\S+)/) ? $1 : '' );
Branch: 1 branch
BranchTrueFalseTotal
$ls =~ /\A(\S+)/ ? : 169 0 169
3341  
3342 169 ▶ if ($depth == 1 && $lc_first eq 'fi') {
Branch: 4 branches
BranchTrueFalseTotal
if ($depth == 1 and $lc_first eq 'fi') { } 52 117 169
elsif ($depth == 1 and not $in_else and $lc_first eq 'elif') { } 5 112 117
elsif ($depth == 1 and not $in_else and $lc_first eq 'else') { } 12 100 112
elsif ($depth == 1 and $lc_first eq 'then') { } 31 69 100
Condition66%2/3
$depth == 1 and $lc_first eq "fi"
!ll&&!rl&&r
011752
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
3343 52 ▶ push @branches, [ [@cond_lines], [@body_lines] ] unless $in_else;
Branch: 1 branch
BranchTrueFalseTotal
unless $in_else 40 12 52
3344 52 ▶ $else_body = [@body_lines] if $in_else;
Branch: 1 branch
BranchTrueFalseTotal
if $in_else 12 40 52
3345 52 last;
3346 }
3347 ▶ 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
121005
$depth == 1 and not $in_else
!ll&&!rl&&r
012105
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
3348 5 push @branches, [ [@cond_lines], [@body_lines] ];
3349 5 $cond_str = $ls;
3350 5 $cond_str =~ s/\Aelif\s+//i;
3351 5 $cond_str =~ s/\s*;\s*then\s*\z//i;
3352 5 $cond_str =~ s/\s+then\s*\z//i;
3353 5 @cond_lines = ($cond_str);
3354 5 @body_lines = ();
3355 }
3356 ▶ 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
128812
$depth == 1 and not $in_else
!ll&&!rl&&r
012100
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
3357 12 push @branches, [ [@cond_lines], [@body_lines] ];
3358 12 @body_lines = ();
3359 12 $in_else = 1;
3360 }
3361 ▶ elsif ($depth == 1 && $lc_first eq 'then') {
Condition66%2/3
$depth == 1 and $lc_first eq "then"
!ll&&!rl&&r
06931
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
3362 # 'then' on its own line: continue collecting body
3363 31 next;
3364 }
3365 else {
3366 69 push @body_lines, $l;
3367 69 $depth += _if_depth_delta($l);
3368 }
3369 }
3370  
3371 # Evaluate branches
3372 52 my $status = 0;
3373 52 my $executed = 0;
3374 52 for my $branch (@branches) {
3375 55 my ($cond_ref, $body_ref) = @{$branch};
3376 55 $_ERREXIT_HOLD++;
3377 55 my $cond_status = _run_lines($class, $cond_ref, $opts_ref);
3378 55 $_ERREXIT_HOLD--;
3379 55 ▶ if ($cond_status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cond_status == 0) 37 18 55
3380 37 $status = _run_lines($class, $body_ref, $opts_ref);
3381 37 $executed = 1;
3382 37 last;
3383 }
3384 }
3385 52 ▶ if (!$executed && defined $else_body) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $executed and defined $else_body) 4 48 52
Condition100%3/3
not $executed and defined $else_body
!ll&&!rl&&r
37114
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
3386 4 $status = _run_lines($class, $else_body, $opts_ref);
3387 }
3388  
3389 52 return ($status, $i);
3390 }
3391  
3392 # ----------------------------------------------------------------
3393 # for VAR in list; do ... done
3394 # ----------------------------------------------------------------
3395 40 ▶sub _parse_for {
Subroutine _parse_for: called CC=21 SCAR=32.1
3396 40 my ($class, $lines_ref, $start, $opts_ref) = @_;
3397 40 my @lines = @{$lines_ref};
3398 40 my $i = $start;
3399  
3400 40 my $for_line = $lines[$i]; $i++;
3401 40 $for_line =~ s/\r?\n\z//; $for_line =~ s/\A\s+//;
3402  
3403 # for VAR in LIST [; do [BODY [; done]]]
3404 #
3405 # The header may stand alone (do/done on following lines) or carry an
3406 # inline "; do ... ; done" tail all on one physical line. The inline
3407 # form is detected first; ";do" is matched with \b after "do" so that a
3408 # "; done" terminator is never mistaken for the "do" keyword.
3409 40 my ($var, $list_str) = ('', '');
3410 40 my $inline_body; # defined when the header has an inline "do" tail
3411 40 my $inline_closed = 0; # true when that tail also held the "done"
3412 40 ▶ 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) { } 40 0 40
elsif ($for_line =~ /\Afor\s+([A-Za-z_][A-Za-z0-9_]*)\s+in\s+(.*?)\s*\z/i) { } 0 0 0
3413 40 my $tail;
3414 40 ($var, $list_str, $tail) = ($1, $2, $3);
3415 40 $tail =~ s/\s+\z//;
3416 40 ▶ if ($tail eq '') {
Branch: 3 branches
BranchTrueFalseTotal
if ($tail eq '') { } 19 21 40
elsif ($tail =~ /\A(.*);\s*done\b\s*\z/s) { } 21 0 21
elsif ($tail eq 'done') { } 0 0 0
3417 19 $inline_body = undef; # "for ... ; do" -> body follows
3418 }
3419 elsif ($tail =~ /\A(.*);\s*done\b\s*\z/s) {
3420 21 $inline_body = $1; $inline_closed = 1; # fully inline; greedy = last done
3421 }
3422 elsif ($tail eq 'done') {
3423 0 $inline_body = ''; $inline_closed = 1; # empty inline body
3424 }
3425 else {
3426 0 $inline_body = $tail; # "for ... ; do BODY" (done later)
3427 }
3428 }
3429 elsif ($for_line =~ /\Afor\s+([A-Za-z_][A-Za-z0-9_]*)\s+in\s+(.*?)\s*\z/i) {
3430 0 ($var, $list_str) = ($1, $2); # header only; do/done on later lines
3431 }
3432  
3433 # Collect body until 'done' (skipped when the tail already closed the loop)
3434 40 my @body = ();
3435 40 ▶ if (defined $inline_body) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $inline_body) 21 19 40
3436 21 ▶ push @body, $inline_body unless $inline_body eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless $inline_body eq "" 21 0 21
3437 }
3438 40 ▶ if (!$inline_closed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($inline_closed) 19 21 40
3439 19 my $depth = 1;
3440 19 ▶ while ($i <= $#lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#lines) 59 0 59
3441 59 my $l = $lines[$i]; $i++;
3442 59 $l =~ s/\r?\n\z//;
3443 59 my $ls = $l; $ls =~ s/\A\s+//;
3444 59 ▶ my $lc_f = lc( ($ls =~ /\A(\S+)/) ? $1 : '' );
Branch: 1 branch
BranchTrueFalseTotal
$ls =~ /\A(\S+)/ ? : 59 0 59
3445 59 ▶ 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") 1 58 59
Condition66%4/6
$lc_f eq "for" or $lc_f eq "while" or $lc_f eq "until"
l!l&&r!l&&!r
1058
$lc_f eq "for" or $lc_f eq "while"
l!l&&r!l&&!r
1058
MC/DC33%1/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 table50%2/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
3446 59 ▶ if ($lc_f eq 'done') { $depth--; last if $depth == 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($lc_f eq "done") 20 39 59
if $depth == 0 19 1 20
3447 40 ▶ push @body, $l unless ($lc_f eq 'do' && $depth == 1);
Branch: 1 branch
BranchTrueFalseTotal
unless $lc_f eq "do" and $depth == 1 40 0 40
Condition33%1/3
$lc_f eq "do" and $depth == 1
!ll&&!rl&&r
4000
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
3448 }
3449 }
3450  
3451 # Expand list items. _expand_word_list resolves variables and command
3452 # substitutions, applies filename globbing to unquoted glob words, and
3453 # expands a whole-word ${arr[@]} / ${arr[*]} reference (quoted or not) to
3454 # one item per array element.
3455 40 my @items = _expand_word_list($class, $list_str);
3456 40 my $status = 0;
3457 40 for my $val (@items) {
3458 192 BATsh::Env->set($var, $val);
3459 192 $_BREAK = 0; $_CONTINUE = 0;
3460 192 $status = _run_lines($class, \@body, $opts_ref);
3461 192 ▶ last if $_BREAK || defined $_EXIT_CODE;
Branch: 1 branch
BranchTrueFalseTotal
if $_BREAK or defined $_EXIT_CODE 2 190 192
Condition66%2/3
$_BREAK or defined $_EXIT_CODE
l!l&&r!l&&!r
20190
MC/DC50%1/2
$_BREAK or defined $_EXIT_CODE
$_BREAK defined $_EXIT_CODE
Truth table66%2/3
A or B
A= $_BREAKB= defined $_EXIT_CODE
ABresult
1X1
011
000
3462 }
3463 40 $_BREAK = 0;
3464  
3465 40 return ($status, $i);
3466 }
3467  
3468 # ----------------------------------------------------------------
3469 # select VAR in LIST ; do ... done (v0.07)
3470 #
3471 # Prints a numbered menu of LIST (one item per line, to STDERR), prompts
3472 # with $PS3 (default "#? "), and reads one line from STDIN into REPLY:
3473 # - a number in range 1..#items sets VAR to that item and runs BODY
3474 # - anything else (including a blank line) sets VAR to '' and still
3475 # runs BODY, matching bash (the menu is then reprinted next
3476 # iteration)
3477 # - end of input (STDIN closed) ends the loop, as does break
3478 # There is no multi-column menu layout (bash chooses columns based on
3479 # terminal width and item count); every item is printed on its own line.
3480 # ----------------------------------------------------------------
3481 3 ▶sub _parse_select {
Subroutine _parse_select: called CC=32 SCAR=51.6
3482 3 my ($class, $lines_ref, $start, $opts_ref) = @_;
3483 3 my @lines = @{$lines_ref};
3484 3 my $i = $start;
3485  
3486 3 my $sel_line = $lines[$i]; $i++;
3487 3 $sel_line =~ s/\r?\n\z//; $sel_line =~ s/\A\s+//;
3488  
3489 3 my ($var, $list_str) = ('', '');
3490 3 my $inline_body;
3491 3 my $inline_closed = 0;
3492 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
3493 3 my $tail;
3494 3 ($var, $list_str, $tail) = ($1, $2, $3);
3495 3 $tail =~ s/\s+\z//;
3496 0 ▶ 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
3497 3 elsif ($tail =~ /\A(.*);\s*done\b\s*\z/s) { $inline_body = $1; $inline_closed = 1 }
3498 0 elsif ($tail eq 'done') { $inline_body = ''; $inline_closed = 1 }
3499 0 else { $inline_body = $tail }
3500 }
3501 elsif ($sel_line =~ /\Aselect\s+([A-Za-z_][A-Za-z0-9_]*)\s+in\s+(.*?)\s*\z/i) {
3502 0 ($var, $list_str) = ($1, $2);
3503 }
3504 elsif ($sel_line =~ /\Aselect\s+([A-Za-z_][A-Za-z0-9_]*)\s*\z/i) {
3505 0 ($var, $list_str) = ($1, '"$@"'); # "select VAR" alone: positional params
3506 }
3507 else {
3508 0 return (0, $i); # malformed header: nothing to do
3509 }
3510  
3511 3 my @body = ();
3512 3 ▶ if (defined $inline_body) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $inline_body) 3 0 3
3513 3 ▶ push @body, $inline_body unless $inline_body eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless $inline_body eq "" 3 0 3
3514 }
3515 3 ▶ if (!$inline_closed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($inline_closed) 0 3 3
3516 0 my $depth = 1;
3517 0 ▶ while ($i <= $#lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#lines) 0 0 0
3518 0 my $l = $lines[$i]; $i++;
3519 0 $l =~ s/\r?\n\z//;
3520 0 my $ls = $l; $ls =~ s/\A\s+//;
3521 0 ▶ my $lc_f = lc( ($ls =~ /\A(\S+)/) ? $1 : '' );
Branch: 1 branch
BranchTrueFalseTotal
$ls =~ /\A(\S+)/ ? : 0 0 0
3522 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
3523 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
3524 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
3525 }
3526 }
3527  
3528 3 my @items = _expand_word_list($class, $list_str);
3529 3 my $status = 0;
3530 3 ▶ return ($status, $i) unless @items;
Branch: 1 branch
BranchTrueFalseTotal
unless @items 0 3 3
3531  
3532 3 my $ps3 = BATsh::Env->get('PS3');
3533 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
3534  
3535 3 my $max_iter = 100_000; # safety guard, mirrors _parse_while
3536 3 ▶ while ($max_iter-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_iter-- > 0) 3 0 3
3537 3 ▶ last if defined $_EXIT_CODE;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_EXIT_CODE 0 3 3
3538 3 for my $idx (0 .. $#items) {
3539 7 print STDERR ($idx + 1) . ') ' . $items[$idx] . "\n";
3540 }
3541 3 print STDERR BATsh::MB::dec($ps3);
3542 3 my $answer = <STDIN>;
3543 3 ▶ last unless defined $answer; # EOF on STDIN ends the loop
Branch: 1 branch
BranchTrueFalseTotal
unless defined $answer 0 3 3
3544 3 $answer =~ s/\r?\n\z//;
3545 3 BATsh::Env->set('REPLY', BATsh::MB::enc($answer));
3546 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
3547 2 BATsh::Env->set($var, $items[$answer - 1]);
3548 }
3549 else {
3550 1 BATsh::Env->set($var, '');
3551 }
3552 3 $_BREAK = 0; $_CONTINUE = 0;
3553 3 $status = _run_lines($class, \@body, $opts_ref);
3554 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
3555 }
3556 3 $_BREAK = 0;
3557  
3558 3 return ($status, $i);
3559 }
3560  
3561 # ----------------------------------------------------------------
3562 # while/until condition; do ... done
3563 # ----------------------------------------------------------------
3564 43 ▶sub _parse_while {
Subroutine _parse_while: called CC=37 SCAR=40.2
3565 43 my ($class, $lines_ref, $start, $opts_ref) = @_;
3566 43 my @lines = @{$lines_ref};
3567 43 my $i = $start;
3568  
3569 43 my $while_line = $lines[$i]; $i++;
3570 43 $while_line =~ s/\r?\n\z//; $while_line =~ s/\A\s+//;
3571  
3572 43 ▶ my $is_until = ($while_line =~ /\Auntil\s/i) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$while_line =~ /\Auntil\s/i ? : 3 40 43
3573  
3574 # Extract condition, supporting an inline "COND; do BODY; done [REDIR]" form
3575 # as well as the multi-line header. As in _parse_for, ";do" is matched with
3576 # \b after "do" so that a "; done" terminator is never taken for "do".
3577 43 my $rest = $while_line;
3578 43 $rest =~ s/\A(?:while|until)\s+//i;
3579  
3580 43 my $cond_str;
3581 43 my @body = ();
3582 43 my $done_line = '';
3583 43 my $inline_body;
3584 43 my $inline_closed = 0;
3585  
3586 43 ▶ if ($rest =~ /\A(.*?)\s*;\s*do\b\s*(.*)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /\A(.*?)\s*;\s*do\b\s*(.*)\z/s) { } 43 0 43
3587 43 my $tail;
3588 43 ($cond_str, $tail) = ($1, $2);
3589 43 $tail =~ s/\s+\z//;
3590 43 ▶ if ($tail eq '') {
Branch: 3 branches
BranchTrueFalseTotal
if ($tail eq '') { } 13 30 43
elsif ($tail =~ /\A(.*);\s*done\b\s*(.*)\z/s) { } 30 0 30
elsif ($tail =~ /\Adone\b\s*(.*)\z/s) { } 0 0 0
3591 13 $inline_body = undef; # "while COND; do" -> body follows
3592 }
3593 elsif ($tail =~ /\A(.*);\s*done\b\s*(.*)\z/s) {
3594 30 $inline_body = $1; # greedy = last "; done"
3595 30 $inline_closed = 1;
3596 30 my $dr = $2; $dr =~ s/\A\s+//; $dr =~ s/\s+\z//;
3597 30 ▶ $done_line = ($dr ne '') ? "done $dr" : 'done';
Branch: 1 branch
BranchTrueFalseTotal
$dr ne '' ? : 1 29 30
3598 }
3599 elsif ($tail =~ /\Adone\b\s*(.*)\z/s) {
3600 0 $inline_body = ''; # empty inline body
3601 0 $inline_closed = 1;
3602 0 my $dr = $1; $dr =~ s/\A\s+//; $dr =~ s/\s+\z//;
3603 0 ▶ $done_line = ($dr ne '') ? "done $dr" : 'done';
Branch: 1 branch
BranchTrueFalseTotal
$dr ne '' ? : 0 0 0
3604 }
3605 else {
3606 0 $inline_body = $tail; # done on following lines
3607 }
3608 }
3609 else {
3610 0 $cond_str = $rest; # header only; do/done on later lines
3611 0 $cond_str =~ s/\s*;\s*do\s*\z//i;
3612 0 $cond_str =~ s/\s+do\s*\z//i;
3613 }
3614  
3615 # Collect body (skipped when the inline tail already closed the loop)
3616 43 ▶ if (defined $inline_body) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $inline_body) 30 13 43
3617 30 ▶ push @body, $inline_body unless $inline_body eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless $inline_body eq "" 30 0 30
3618 }
3619 43 ▶ if (!$inline_closed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($inline_closed) 13 30 43
3620 13 my $depth = 1;
3621 13 ▶ while ($i <= $#lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#lines) 59 0 59
3622 59 my $l = $lines[$i]; $i++;
3623 59 $l =~ s/\r?\n\z//;
3624 59 my $ls = $l; $ls =~ s/\A\s+//;
3625 59 ▶ my $lc_f = lc( ($ls =~ /\A(\S+)/) ? $1 : '' );
Branch: 1 branch
BranchTrueFalseTotal
$ls =~ /\A(\S+)/ ? : 59 0 59
3626 0 ▶ 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 59 59
Condition33%2/6
$lc_f eq "for" or $lc_f eq "while" or $lc_f eq "until"
l!l&&r!l&&!r
0059
$lc_f eq "for" or $lc_f eq "while"
l!l&&r!l&&!r
0059
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
3627 59 ▶ if ($lc_f eq 'done') { $depth--; if ($depth == 0) { $done_line = $ls; last } }
Branch: 2 branches
BranchTrueFalseTotal
if ($lc_f eq "done") 13 46 59
if ($depth == 0) 13 0 13
3628 46 ▶ push @body, $l unless ($lc_f eq 'do' && $depth == 1);
Branch: 1 branch
BranchTrueFalseTotal
unless $lc_f eq "do" and $depth == 1 46 0 46
Condition33%1/3
$lc_f eq "do" and $depth == 1
!ll&&!rl&&r
4600
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
3629 }
3630 }
3631  
3632 # Honor an input redirection on the `done' line, e.g.
3633 # while read LINE; do ...; done < FILE
3634 # by reopening STDIN from FILE for the duration of the loop so that the
3635 # loop's `read' built-in consumes the file. Perl 5.005_03 compatible:
3636 # 2-argument open and bareword filehandle duplication.
3637 43 my $saved_in = 0;
3638 43 ▶ if ($done_line ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($done_line ne "") 43 0 43
3639 43 my $done_rest = $done_line;
3640 43 $done_rest =~ s/\Adone\b\s*//i;
3641 43 ▶ if ($done_rest ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($done_rest ne "") 2 41 43
3642 2 my ($dc, $rd) = _sh_strip_redirects(_expand($class, $done_rest));
3643 2 my $in_file;
3644 2 for my $r (@{$rd}) {
3645 2 my ($fd, $append, $file) = @{$r};
3646 2 ▶ $in_file = $file if $fd == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $fd == 0 2 0 2
3647 }
3648 2 ▶ if (defined $in_file && $in_file ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $in_file and $in_file ne "") 2 0 2
Condition33%1/3
defined $in_file and $in_file ne ""
!ll&&!rl&&r
002
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
3649 2 ▶ if (sysopen(_WH_REDIR_SRC, $in_file, O_RDONLY)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysopen _WH_REDIR_SRC, $in_file, 0) { } 2 0 2
3650 2 ▶ if (open(_WH_REDIR_SAVIN, '<&STDIN')) {
Branch: 1 branch
BranchTrueFalseTotal
if (open _WH_REDIR_SAVIN, "<&STDIN") 2 0 2
3651 2 ▶ if (open(STDIN, '<&_WH_REDIR_SRC')) { $saved_in = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if (open STDIN, "<&_WH_REDIR_SRC") 2 0 2
3652 }
3653 2 close(_WH_REDIR_SRC);
3654 }
3655 else {
3656 0 warn "sh: $in_file: $!\n";
3657 }
3658 }
3659 }
3660 }
3661  
3662 43 my $status = 0;
3663 43 my $max_iter = 100_000; # safety guard
3664 43 ▶ while ($max_iter-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_iter-- > 0) 121 0 121
3665 121 ▶ last if defined $_EXIT_CODE;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_EXIT_CODE 0 121 121
3666 121 $_ERREXIT_HOLD++;
3667 121 my $cond_status = _run_lines($class, [$cond_str], $opts_ref);
3668 121 $_ERREXIT_HOLD--;
3669 121 my $cond_true = ($cond_status == 0);
3670 121 ▶ last if $is_until && $cond_true;
Branch: 1 branch
BranchTrueFalseTotal
if $is_until and $cond_true 3 118 121
Condition100%3/3
$is_until and $cond_true
!ll&&!rl&&r
11083
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
3671 118 ▶ last if !$is_until && !$cond_true;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_until or $cond_true 40 78 118
Condition100%3/3
$is_until or $cond_true
l!l&&r!l&&!r
87040
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
3672 78 $_BREAK = 0; $_CONTINUE = 0;
3673 78 $status = _run_lines($class, \@body, $opts_ref);
3674 78 ▶ last if $_BREAK;
Branch: 1 branch
BranchTrueFalseTotal
if $_BREAK 0 78 78
3675 }
3676 43 $_BREAK = 0;
3677  
3678 43 ▶ if ($saved_in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_in) 2 41 43
3679 2 open(STDIN, '<&_WH_REDIR_SAVIN');
3680 2 close(_WH_REDIR_SAVIN);
3681 }
3682  
3683 43 return ($status, $i);
3684 }
3685  
3686 # ----------------------------------------------------------------
3687 # case $var in pattern) ... ;; esac
3688 # ----------------------------------------------------------------
3689 53 ▶sub _parse_case {
Subroutine _parse_case: called CC=21 SCAR=30.9
3690 53 my ($class, $lines_ref, $start, $opts_ref) = @_;
3691 53 my @lines = @{$lines_ref};
3692 53 my $i = $start;
3693  
3694 53 my $case_line = $lines[$i]; $i++;
3695 53 $case_line =~ s/\r?\n\z//; $case_line =~ s/\A\s+//;
3696  
3697 # Header: case WORD in [inline clauses...]
3698 # The clauses (and even esac) may follow inline on the same physical line
3699 # -- e.g. "case $x in a) echo a ;; *) echo other ;; esac" -- or on the
3700 # following lines. $inline_rest captures anything after "in".
3701 53 my $word = '';
3702 53 my $inline_rest = '';
3703 53 ▶ 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) { } 53 0 53
3704 53 $word = _arr_dequote(_expand($class, $1));
3705 53 $inline_rest = $2;
3706 }
3707 else {
3708 0 return (0, $i); # malformed header: nothing to do
3709 }
3710  
3711 # Accumulate the clause region (everything between "in" and "esac"),
3712 # stopping as soon as the closing esac is seen. esac is only recognised
3713 # at a clause boundary (start of region, after a ;;/;&/;;& terminator, or
3714 # at the start of a line), so the word "esac" appearing inside a body
3715 # (e.g. echo esac) does not end the construct prematurely.
3716 53 my $region = '';
3717 53 my $found = 0;
3718 {
3719 53 my $pos = _case_top_esac_pos($inline_rest);
3720 53 ▶ if ($pos >= 0) { $region = substr($inline_rest, 0, $pos); $found = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($pos >= 0) { } 3 50 53
3721 50 else { $region = $inline_rest }
3722 }
3723 53 ▶ while (!$found && $i <= $#lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $found and $i <= $#lines) 202 3 205
Condition66%2/3
not $found and $i <= $#lines
!ll&&!rl&&r
30202
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
3724 202 my $l = $lines[$i]; $i++;
3725 202 $l =~ s/\r?\n\z//;
3726 202 my $pos = _case_top_esac_pos($l);
3727 202 ▶ if ($pos >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos >= 0) 50 152 202
3728 50 $region .= "\n" . substr($l, 0, $pos);
3729 50 $found = 1;
3730 50 last;
3731 }
3732 152 $region .= "\n" . $l;
3733 }
3734  
3735 # Split the region into clauses, then evaluate with fall-through support:
3736 # ;; normal: stop after the first matching clause
3737 # ;& fall through: run the NEXT clause's body unconditionally
3738 # ;;& continue: keep testing the remaining clauses against the word
3739 53 my @clauses = _case_split_clauses($region);
3740  
3741 53 my $status = 0;
3742 53 my $stop = 0; # a ;; was reached after a match -- stop entirely
3743 53 my $fall = 0; # previous clause ended in ;& -- run this body no matter what
3744 53 for my $cl (@clauses) {
3745 136 ▶ last if $stop;
Branch: 1 branch
BranchTrueFalseTotal
if $stop 43 93 136
3746 93 my ($ctext, $term) = @{$cl};
3747 93 ▶ next unless $ctext =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $ctext =~ /\S/ 0 93 93
3748  
3749 93 my ($pattern_str, $body_text) = _case_parse_clause($ctext);
3750 93 ▶ next unless defined $pattern_str; # not a pattern) clause -- skip
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pattern_str 0 93 93
3751  
3752 93 my $run = 0;
3753 93 ▶ if ($fall) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fall) { } 3 90 93
3754 3 $run = 1;
3755 3 $fall = 0;
3756 }
3757 else {
3758 90 for my $pat (_case_split_patterns($pattern_str)) {
3759 94 $pat =~ s/\A\s+//; $pat =~ s/\s+\z//;
3760 94 ▶ next if $pat eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $pat eq "" 0 94 94
3761 94 ▶ if (_match_pattern($word, $pat)) { $run = 1; last }
Branch: 1 branch
BranchTrueFalseTotal
if (_match_pattern($word, $pat)) 55 39 94
3762 }
3763 }
3764  
3765 93 ▶ if ($run) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run) 58 35 93
3766 58 my @body = split /\n/, $body_text;
3767 58 $status = _run_lines($class, \@body, $opts_ref);
3768 58 ▶ last if $_BREAK || $_CONTINUE || $_RETURN || defined $_EXIT_CODE;
Branch: 1 branch
BranchTrueFalseTotal
if $_BREAK or $_CONTINUE or $_RETURN or defined $_EXIT_CODE 0 58 58
Condition33%3/9
$_BREAK or $_CONTINUE or $_RETURN or defined $_EXIT_CODE
l!l&&r!l&&!r
0058
$_BREAK or $_CONTINUE or $_RETURN
l!l&&r!l&&!r
0058
$_BREAK or $_CONTINUE
l!l&&r!l&&!r
0058
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
3769 58 ▶ if ($term eq ';;') { $stop = 1 }
Branch: 2 branches
BranchTrueFalseTotal
if ($term eq ';;') { } 53 5 58
elsif ($term eq ';&') { } 3 2 5
3770 3 elsif ($term eq ';&') { $fall = 1 } # next body unconditionally
3771 # ';;&' : neither stop nor fall -- keep testing later clauses
3772 }
3773 }
3774  
3775 53 return ($status, $i);
3776 }
3777  
3778 # ----------------------------------------------------------------
3779 # ( commands ) -- subshell command group with isolated scope (v0.07)
3780 #
3781 # There is no real fork here (this interpreter never forks -- see the
3782 # pipeline / background-job / exec notes elsewhere in this file), so
3783 # isolation is approximated by snapshotting every piece of state a real
3784 # subshell would not leak changes from -- variables, indexed/associative
3785 # arrays, function definitions, aliases, and the working directory --
3786 # running the body, and restoring all of it afterward regardless of how
3787 # the body finished. "exit" inside the group ends only the group (its
3788 # status becomes the group's status); break/continue/return still
3789 # propagate to an enclosing loop/function exactly as bash's ( ... ) does
3790 # NOT contain them.
3791 #
3792 # Both the single-line form "( cmd1; cmd2 )" and the multi-line form
3793 # (bare "(" ending the first line, matching bare ")" ending the last)
3794 # are recognised. A trailing ">" / ">>" / "<" redirection after the
3795 # closing ")" is honoured; other trailing redirections (2>, 2>&1, ...)
3796 # and a trailing "&" are not -- documented as known limitations.
3797 # ----------------------------------------------------------------
3798 10 ▶sub _parse_subshell {
Subroutine _parse_subshell: called CC=49 SCAR=48.4
3799 10 my ($class, $lines_ref, $start, $opts_ref) = @_;
3800 10 my @lines = @{$lines_ref};
3801 10 my $i = $start;
3802  
3803 10 my $first_line = $lines[$i]; $i++;
3804 10 $first_line =~ s/\r?\n\z//;
3805 10 my $stripped = $first_line;
3806 10 $stripped =~ s/\A\s+//;
3807  
3808 10 my @body_lines;
3809 10 my $trailer = '';
3810 10 my $found_close = 0;
3811  
3812 # Try the single-line form first: a quote-aware paren-depth scan for
3813 # the ')' that matches this line's opening '('.
3814 {
3815 10 my @c = split //, $stripped;
3816 10 my $n = scalar @c;
3817 10 my $in_sq = 0;
3818 10 my $in_dq = 0;
3819 10 my $d = 0;
3820 10 my $close_pos;
3821 10 ▶ for (my $k = 0; $k < $n; $k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $n) 247 0 247
3822 247 my $ch = $c[$k];
3823 0 ▶ if ($in_sq) { $in_sq = 0 if $ch eq "'"; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($in_sq) 0 247 247
if $ch eq "'" 0 0 0
3824 0 ▶ if ($ch eq "'" && !$in_dq) { $in_sq = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $in_dq) 0 247 247
Condition33%1/3
$ch eq "'" and not $in_dq
!ll&&!rl&&r
24700
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
3825 247 ▶ if ($ch eq '"' && !$in_sq) { $in_dq = !$in_dq; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" and not $in_sq) 4 243 247
Condition66%2/3
$ch eq "\"" and not $in_sq
!ll&&!rl&&r
24304
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
3826 243 ▶ next if $in_sq || $in_dq;
Branch: 1 branch
BranchTrueFalseTotal
if $in_sq or $in_dq 63 180 243
Condition66%2/3
$in_sq or $in_dq
l!l&&r!l&&!r
063180
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
3827 180 ▶ if ($ch eq '(') { $d++ }
Branch: 2 branches
BranchTrueFalseTotal
if ($ch eq '(') { } 11 169 180
elsif ($ch eq ')') { } 11 158 169
3828 11 ▶ elsif ($ch eq ')') { $d--; if ($d == 0) { $close_pos = $k; last } }
Branch: 1 branch
BranchTrueFalseTotal
if ($d == 0) 10 1 11
3829 }
3830 10 ▶ if (defined $close_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $close_pos) 10 0 10
3831 10 push @body_lines, substr($stripped, 1, $close_pos - 1);
3832 10 $trailer = substr($stripped, $close_pos + 1);
3833 10 $found_close = 1;
3834 }
3835 }
3836  
3837 10 ▶ if (!$found_close) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found_close) 0 10 10
3838 # Multi-line form: accumulate lines, counting '(' / ')' globally
3839 # per line (not quote-aware, matching the existing _parse_function
3840 # brace-counter's level of rigor) until the depth returns to 0.
3841 0 my $accum = substr($stripped, 1);
3842 0 my $opens = () = ($accum =~ /\(/g);
3843 0 my $closes = () = ($accum =~ /\)/g);
3844 0 my $depth = 1 + $opens - $closes;
3845 0 ▶ push @body_lines, $accum if $accum =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $accum =~ /\S/ 0 0 0
3846 0 ▶ if ($depth <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth <= 0) { } 0 0 0
3847 0 $found_close = 1;
3848 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
3849 0 $body_lines[-1] = $1;
3850 0 $trailer = $2;
3851 }
3852 }
3853 else {
3854 0 ▶ while ($i <= $#lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#lines) 0 0 0
3855 0 my $l = $lines[$i]; $i++;
3856 0 $l =~ s/\r?\n\z//;
3857 0 my $o = () = ($l =~ /\(/g);
3858 0 my $c2 = () = ($l =~ /\)/g);
3859 0 $depth += $o - $c2;
3860 0 ▶ if ($depth <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth <= 0) 0 0 0
3861 0 ▶ if ($l =~ /\A(.*)\)([^)]*)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /\A(.*)\)([^)]*)\z/s) 0 0 0
3862 0 ▶ push @body_lines, $1 if $1 =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $1 =~ /\S/ 0 0 0
3863 0 $trailer = $2;
3864 }
3865 0 $found_close = 1;
3866 0 last;
3867 }
3868 0 push @body_lines, $l;
3869 }
3870 }
3871 }
3872  
3873 10 my @body;
3874 10 for my $bl (@body_lines) {
3875 10 for my $part (split /;/, $bl) {
3876 16 ▶ push @body, $part if $part =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $part =~ /\S/ 16 0 16
3877 }
3878 }
3879  
3880 # Trailing "> file" / ">> file" / "< file" on the closing ')' line.
3881 10 $trailer =~ s/\A\s+//; $trailer =~ s/\s+\z//;
3882 10 my ($subsh_out_file, $subsh_out_append, $subsh_in_file);
3883 10 ▶ if ($trailer ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($trailer ne "") 5 5 10
3884 5 my $texp = _expand($class, $trailer);
3885 # Reuse the main redirection parser so a quoted target ("a b",
3886 # ">name", "cmd|") is read and dequoted exactly as elsewhere.
3887 5 my @tr_parts = _sh_strip_redirects($texp);
3888 5 my $tr_redirs = $tr_parts[1];
3889 5 for my $r (@{$tr_redirs}) {
3890 5 my ($fd, $app, $file) = @{$r};
3891 5 ▶ next if $file =~ /\A&[12]\z/; # dup forms handled in body
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\A&[12]\z/ 0 5 5
3892 5 ▶ if ($fd == 0) { $subsh_in_file = $file }
Branch: 2 branches
BranchTrueFalseTotal
if ($fd == 0) { } 2 3 5
elsif ($fd == 1) { } 3 0 3
3893 3 elsif ($fd == 1) { $subsh_out_file = $file; $subsh_out_append = $app }
3894 }
3895 }
3896  
3897 10 my $saved_out = 0;
3898 10 my $saved_in = 0;
3899 10 my $redir_failed = 0;
3900 10 ▶ if (defined $subsh_out_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $subsh_out_file) 3 7 10
3901 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
3902 O_WRONLY | O_CREAT | ($subsh_out_append ? O_APPEND : O_TRUNC),
3903 0666)) {
3904 3 ▶ if (open(_SH_SUBSH_SAVOUT, '>&STDOUT')) {
Branch: 1 branch
BranchTrueFalseTotal
if (open _SH_SUBSH_SAVOUT, ">&STDOUT") 3 0 3
3905 3 ▶ if (open(STDOUT, '>&_SH_SUBSH_DST')) { $saved_out = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if (open STDOUT, ">&_SH_SUBSH_DST") 3 0 3
3906 }
3907 3 close(_SH_SUBSH_DST);
3908 }
3909 0 else { warn "sh: $subsh_out_file: $!\n"; $redir_failed = 1 }
3910 }
3911 10 ▶ if (defined $subsh_in_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $subsh_in_file) 2 8 10
3912 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
3913 1 ▶ if (open(_SH_SUBSH_SAVIN, '<&STDIN')) {
Branch: 1 branch
BranchTrueFalseTotal
if (open _SH_SUBSH_SAVIN, "<&STDIN") 1 0 1
3914 1 ▶ if (open(STDIN, '<&_SH_SUBSH_SRC')) { $saved_in = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if (open STDIN, "<&_SH_SUBSH_SRC") 1 0 1
3915 }
3916 1 close(_SH_SUBSH_SRC);
3917 }
3918 1 else { warn "sh: $subsh_in_file: $!\n"; $redir_failed = 1 }
3919 }
3920  
3921 # A failed redirection aborts the group without running its body -- as
3922 # the shell does for "( cat ) < missing": it reports the error and does
3923 # NOT fall through to read the terminal (which would hang). Restore any
3924 # handle already swapped, then return failure.
3925 10 ▶ if ($redir_failed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($redir_failed) 1 9 10
3926 0 ▶ if ($saved_out) { open(STDOUT, '>&_SH_SUBSH_SAVOUT'); close(_SH_SUBSH_SAVOUT) }
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_out) 0 1 1
3927 0 ▶ if ($saved_in) { open(STDIN, '<&_SH_SUBSH_SAVIN'); close(_SH_SUBSH_SAVIN) }
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_in) 0 1 1
3928 1 $LAST_STATUS = 1;
3929 1 return (1, $i);
3930 }
3931  
3932 # Snapshot everything the subshell must not leak changes to.
3933 9 my $env_snap = BATsh::Env->snapshot();
3934 9 my %arr_snap = %_SH_ARRAY;
3935 9 my %arrty_snap = %_SH_ARRAY_TYPE;
3936 9 my %fn_snap = %_SH_FUNCTIONS;
3937 9 my %alias_snap = %_SH_ALIAS;
3938 9 ▶ my $cwd_snap = defined(&Cwd::cwd) ? Cwd::cwd() : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined &Cwd::cwd ? : 9 0 9
3939  
3940 9 $_BREAK = 0; $_CONTINUE = 0;
3941 9 my $status = _run_lines($class, \@body, $opts_ref);
3942 9 my $subshell_exit = $_EXIT_CODE; # "exit" inside ( ... ) ends only the group
3943 9 $_EXIT_CODE = undef;
3944 9 ▶ $status = $subshell_exit if defined $subshell_exit;
Branch: 1 branch
BranchTrueFalseTotal
if defined $subshell_exit 0 9 9
3945  
3946 9 BATsh::Env->restore($env_snap);
3947 9 %_SH_ARRAY = %arr_snap;
3948 9 %_SH_ARRAY_TYPE = %arrty_snap;
3949 9 %_SH_FUNCTIONS = %fn_snap;
3950 9 %_SH_ALIAS = %alias_snap;
3951 9 ▶ chdir($cwd_snap) if defined $cwd_snap && $cwd_snap ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $cwd_snap and $cwd_snap ne "" 6 3 9
Condition66%2/3
defined $cwd_snap and $cwd_snap ne ""
!ll&&!rl&&r
036
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
3952  
3953 9 ▶ if ($saved_out) { open(STDOUT, '>&_SH_SUBSH_SAVOUT'); close(_SH_SUBSH_SAVOUT) }
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_out) 3 6 9
3954 9 ▶ if ($saved_in) { open(STDIN, '<&_SH_SUBSH_SAVIN'); close(_SH_SUBSH_SAVIN) }
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_in) 1 8 9
3955  
3956 9 $LAST_STATUS = $status;
3957 9 return ($status, $i);
3958 }
3959  
3960 # Locate a clause-boundary "esac" at the top level of $s (outside quotes).
3961 # Returns its character offset, or -1. An esac is only at a clause boundary
3962 # when the preceding non-blank character is a ';', '&', or newline, or it is
3963 # at the very start -- so "echo esac" inside a body does not match.
3964 255 ▶sub _case_top_esac_pos {
Subroutine _case_top_esac_pos: called CC=23 SCAR=36.6
3965 255 my ($s) = @_;
3966 255 ▶ return -1 unless defined $s && $s ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and $s ne "" 50 205 255
Condition66%2/3
defined $s and $s ne ""
!ll&&!rl&&r
050205
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
3967 205 my @c = split //, $s;
3968 205 my $n = scalar @c;
3969 205 my $i = 0; my $sq = 0; my $dq = 0;
3970 205 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 5590 152 5742
3971 5590 my $ch = $c[$i];
3972 0 ▶ if ($sq) { $sq = 0 if $ch eq "'"; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($sq) 0 5590 5590
if $ch eq "'" 0 0 0
3973 0 ▶ if ($ch eq "'" && !$dq) { $sq = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $dq) 0 5590 5590
Condition66%2/3
$ch eq "'" and not $dq
!ll&&!rl&&r
558280
MC/DC0%0/2
$ch eq "'" and not $dq
$ch eq "'" not $dq
Truth table66%2/3
A and not(B)
A= $ch eq "'"B= not $dq
ABresult
0X0
100
111
3974 5590 ▶ if ($ch eq '"') { $dq = !$dq; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"") 184 5406 5590
3975 5406 ▶ if (!$dq && lc(substr($s, $i, 4)) eq 'esac') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $dq and lc substr($s, $i, 4) eq "esac") 53 5353 5406
Condition100%3/3
not $dq and lc substr($s, $i, 4) eq "esac"
!ll&&!rl&&r
1767358653
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
3976 53 ▶ my $after = ($i + 4 < $n) ? substr($s, $i + 4, 1) : '';
Branch: 1 branch
BranchTrueFalseTotal
$i + 4 < $n ? : 0 53 53
3977 53 ▶ my $aok = ($i + 4 >= $n || $after =~ /\s/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$i + 4 >= $n || $after =~ /\s/ ? : 53 0 53
Condition33%1/3
$i + 4 >= $n or $after =~ /\s/
l!l&&r!l&&!r
5300
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
3978 53 my $j = $i - 1;
3979 53 ▶ $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 } 135 53 188
Condition83%5/6
$j >= 0 and $c[$j] eq " " || $c[$j] eq "\t"
!ll&&!rl&&r
503135
$c[$j] eq " " or $c[$j] eq "\t"
l!l&&r!l&&!r
13503
MC/DC66%2/3
$j >= 0 and $c[$j] eq " " || $c[$j] eq "\t"
$j >= 0 $c[$j] eq " " $c[$j] eq "\t"
Truth table75%3/4
A and B or C
A= $j >= 0B= $c[$j] eq " "C= $c[$j] eq "\t"
ABCresult
0XX0
1000
11X1
1011
3980 53 ▶ 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" ? : 53 0 53
Condition44%4/9
$j < 0 or $c[$j] eq ";" or $c[$j] eq "&" or $c[$j] eq "\n"
l!l&&r!l&&!r
5300
$j < 0 or $c[$j] eq ";" or $c[$j] eq "&"
l!l&&r!l&&!r
5300
$j < 0 or $c[$j] eq ";"
l!l&&r!l&&!r
5030
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
3981 || $c[$j] eq "\n") ? 1 : 0;
3982 53 ▶ return $i if $aok && $bok;
Branch: 1 branch
BranchTrueFalseTotal
if $aok and $bok 53 0 53
Condition33%1/3
$aok and $bok
!ll&&!rl&&r
0053
MC/DC0%0/2
$aok and $bok
$aok $bok
Truth table33%1/3
A and B
A= $aokB= $bok
ABresult
0X0
100
111
3983 }
3984 5353 $i++;
3985 }
3986 152 return -1;
3987 }
3988  
3989 # Split a case region into clauses on the terminators ;;& / ;; / ;& at the
3990 # top level (outside quotes). Returns a list of [clause_text, terminator].
3991 53 ▶sub _case_split_clauses {
Subroutine _case_split_clauses: called CC=13 SCAR=26.8
3992 53 my ($s) = @_;
3993 53 my @out;
3994 53 my $cur = '';
3995 53 my @c = split //, $s;
3996 53 my $n = scalar @c;
3997 53 my $i = 0; my $sq = 0; my $dq = 0;
3998 53 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 5583 53 5636
3999 5583 my $ch = $c[$i];
4000 0 ▶ if ($sq) { $cur .= $ch; $sq = 0 if $ch eq "'"; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($sq) 0 5583 5583
if $ch eq "'" 0 0 0
4001 0 ▶ if ($ch eq "'" && !$dq) { $sq = 1; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $dq) 0 5583 5583
Condition66%2/3
$ch eq "'" and not $dq
!ll&&!rl&&r
557580
MC/DC0%0/2
$ch eq "'" and not $dq
$ch eq "'" not $dq
Truth table66%2/3
A and not(B)
A= $ch eq "'"B= not $dq
ABresult
0X0
100
111
4002 5583 ▶ if ($ch eq '"') { $dq = !$dq; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"") 184 5399 5583
4003 5399 ▶ if (!$sq && !$dq) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sq or $dq) 3632 1767 5399
Condition66%2/3
$sq or $dq
l!l&&r!l&&!r
017673632
MC/DC50%1/2
$sq or $dq
$sq $dq
Truth table66%2/3
A or B
A= $sqB= $dq
ABresult
1X1
011
000
4004 3632 ▶ if (substr($s, $i, 3) eq ';;&') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($s, $i, 3) eq ";;&") 2 3630 3632
4005 2 push @out, [$cur, ';;&']; $cur = ''; $i += 3; next;
4006 }
4007 3630 ▶ if (substr($s, $i, 2) eq ';;') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($s, $i, 2) eq ";;") 149 3481 3630
4008 149 push @out, [$cur, ';;']; $cur = ''; $i += 2; next;
4009 }
4010 3481 ▶ if (substr($s, $i, 2) eq ';&') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($s, $i, 2) eq ";&") 3 3478 3481
4011 3 push @out, [$cur, ';&']; $cur = ''; $i += 2; next;
4012 }
4013 }
4014 5245 $cur .= $ch; $i++;
4015 }
4016 53 ▶ push @out, [$cur, ';;'] if $cur =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $cur =~ /\S/ 0 53 53
4017 53 return @out;
4018 }
4019  
4020 # Parse a clause "pat1|pat2) body" into ($patterns, $body). A leading "("
4021 # is accepted (bash form). The "(" closing the pattern list is the first
4022 # top-level ")" outside quotes and outside a [...] class. Returns
4023 # (undef, undef) when no pattern ")" is present.
4024 93 ▶sub _case_parse_clause {
Subroutine _case_parse_clause: called CC=18 SCAR=31.0
4025 93 my ($s) = @_;
4026 93 $s =~ s/\A[\s\n]+//;
4027 93 $s =~ s/\A\(//; # optional leading (
4028 93 my @c = split //, $s;
4029 93 my $n = scalar @c;
4030 93 my $i = 0; my $sq = 0; my $dq = 0; my $cls = 0; my $paren = 0;
4031 93 my $found = -1;
4032 93 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 441 0 441
4033 441 my $ch = $c[$i];
4034 0 ▶ if ($sq) { $sq = 0 if $ch eq "'"; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($sq) 0 441 441
if $ch eq "'" 0 0 0
4035 0 ▶ if ($ch eq "'" && !$dq) { $sq = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $dq) 0 441 441
Condition33%1/3
$ch eq "'" and not $dq
!ll&&!rl&&r
44100
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
4036 441 ▶ if ($ch eq '"') { $dq = !$dq; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"") 2 439 441
4037 439 ▶ if (!$sq && !$dq) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sq or $dq) 438 1 439
Condition66%2/3
$sq or $dq
l!l&&r!l&&!r
01438
MC/DC50%1/2
$sq or $dq
$sq $dq
Truth table66%2/3
A or B
A= $sqB= $dq
ABresult
1X1
011
000
4038 438 ▶ if ($ch eq '[') { $cls = 1 }
Branch: 4 branches
BranchTrueFalseTotal
if ($ch eq '[') { } 15 423 438
elsif ($ch eq ']') { } 15 408 423
elsif ($ch eq '(' and not $cls) { } 3 405 408
elsif ($ch eq ')' and not $cls) { } 96 618 714
4039 15 elsif ($ch eq ']') { $cls = 0 }
4040 # An extglob group (?(...)/*(.../@(.../!(...)) contributes a
4041 # balanced '(' ... ')' pair inside the pattern text; only a
4042 # ')' at $paren == 0 is the clause terminator. Tracked
4043 # unconditionally, harmless when there is no '(' present.
4044 3 ▶ elsif ($ch eq '(' && !$cls) { $paren++ }
Condition66%2/3
$ch eq "(" and not $cls
!ll&&!rl&&r
40503
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
4045 ▶ elsif ($ch eq ')' && !$cls) {
Condition66%2/3
$ch eq ")" and not $cls
!ll&&!rl&&r
309096
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
4046 96 ▶ if ($paren > 0) { $paren-- }
Branch: 1 branch
BranchTrueFalseTotal
if ($paren > 0) { } 3 93 96
4047 93 else { $found = $i; last }
4048 }
4049 }
4050 346 $i++;
4051 }
4052 93 ▶ return (undef, undef) if $found < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $found < 0 0 93 93
4053 93 my $patterns = substr($s, 0, $found);
4054 93 ▶ my $body = ($found + 1 <= length($s)) ? substr($s, $found + 1) : '';
Branch: 1 branch
BranchTrueFalseTotal
$found + 1 <= length $s ? : 93 0 93
4055 93 return ($patterns, $body);
4056 }
4057  
4058 # Split a pattern list on top-level "|" (outside quotes and [...] classes).
4059 90 ▶sub _case_split_patterns {
Subroutine _case_split_patterns: called CC=19 SCAR=31.4
4060 90 my ($s) = @_;
4061 90 my @out;
4062 90 my $cur = '';
4063 90 my @c = split //, $s;
4064 90 my $n = scalar @c;
4065 90 my $i = 0; my $sq = 0; my $dq = 0; my $cls = 0; my $paren = 0;
4066 90 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 340 90 430
4067 340 my $ch = $c[$i];
4068 0 ▶ if ($sq) { $cur .= $ch; $sq = 0 if $ch eq "'"; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($sq) 0 340 340
if $ch eq "'" 0 0 0
4069 0 ▶ if ($ch eq "'" && !$dq) { $sq = 1; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $dq) 0 340 340
Condition33%1/3
$ch eq "'" and not $dq
!ll&&!rl&&r
34000
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
4070 340 ▶ if ($ch eq '"') { $dq = !$dq; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"") 2 338 340
4071 338 ▶ if (!$sq && !$dq) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sq or $dq) 337 1 338
Condition66%2/3
$sq or $dq
l!l&&r!l&&!r
01337
MC/DC50%1/2
$sq or $dq
$sq $dq
Truth table66%2/3
A or B
A= $sqB= $dq
ABresult
1X1
011
000
4072 337 ▶ if ($ch eq '[') { $cls = 1 }
Branch: 5 branches
BranchTrueFalseTotal
if ($ch eq '[') { } 15 322 337
elsif ($ch eq ']') { } 15 307 322
elsif ($ch eq '(' and not $cls) { } 3 304 307
elsif ($ch eq ')' and not $cls and $paren > 0) { } 3 301 304
elsif ($ch eq '|' and not $cls and not $paren) { } 7 585 592
4073 15 elsif ($ch eq ']') { $cls = 0 }
4074 # Parenthesis depth: keeps an extglob group's internal '|'
4075 # (e.g. @(abc|def)) from being mistaken for a pattern
4076 # separator. Tracked unconditionally -- harmless when
4077 # extglob is off, since ordinary patterns rarely contain a
4078 # literal unescaped '('.
4079 3 ▶ elsif ($ch eq '(' && !$cls) { $paren++ }
Condition66%2/3
$ch eq "(" and not $cls
!ll&&!rl&&r
30403
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
4080 3 ▶ elsif ($ch eq ')' && !$cls && $paren > 0) { $paren-- }
Condition66%4/6
$ch eq ")" and not $cls and $paren > 0
!ll&&!rl&&r
30103
$ch eq ")" and not $cls
!ll&&!rl&&r
30103
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
4081 7 ▶ 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
29137
$ch eq "|" and not $cls
!ll&&!rl&&r
291010
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
4082 }
4083 331 $cur .= $ch; $i++;
4084 }
4085 90 push @out, $cur;
4086 90 return @out;
4087 }
4088  
4089 # Match a shell-glob case pattern against a word. Supports * ? and
4090 # character classes [abc] [a-z] [!abc]/[^abc], plus quoting and backslash
4091 # escapes (a quoted or escaped metacharacter is matched literally).
4092 94 ▶sub _match_pattern {
Subroutine _match_pattern: called CC=3 SCAR=11.0
4093 94 my ($word, $pat) = @_;
4094 94 ▶ $word = '' unless defined $word;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $word 0 94 94
4095 94 my $re = _case_glob_to_re($pat);
4096 94 ▶ return ($word =~ /\A$re\z/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$word =~ /\A$re\z/ ? : 55 39 94
4097 }
4098  
4099 98 ▶sub _case_glob_to_re {
Subroutine _case_glob_to_re: called CC=36 SCAR=44.5
4100 98 my ($pat) = @_;
4101 98 ▶ $pat = '' unless defined $pat;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pat 0 98 98
4102 98 my $re = '';
4103 98 my @c = split //, $pat;
4104 98 my $n = scalar @c;
4105 98 my $i = 0;
4106 98 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 249 98 347
4107 249 my $ch = $c[$i];
4108 249 ▶ 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 247 249
Condition88%8/9
$_OPT_EXTGLOB and $ch =~ /[?*+\@!]/ and $i + 1 < $n and $c[$i + 1] eq "("
!ll&&!rl&&r
24522
$_OPT_EXTGLOB and $ch =~ /[?*+\@!]/ and $i + 1 < $n
!ll&&!rl&&r
24504
$_OPT_EXTGLOB and $ch =~ /[?*+\@!]/
!ll&&!rl&&r
231144
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
4109 2 my @eg = _extglob_scan(\@c, $i, \&_case_glob_to_re);
4110 2 ▶ if (@eg) { $re .= $eg[1]; $i = $eg[0]; next }
Branch: 1 branch
BranchTrueFalseTotal
if (@eg) 2 0 2
4111 }
4112 247 ▶ if ($ch eq "'") { # literal single-quoted run
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'") 0 247 247
4113 0 $i++;
4114 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
4115 0 $i++; next;
4116 }
4117 247 ▶ if ($ch eq '"') { # literal double-quoted run
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"") 1 246 247
4118 1 $i++;
4119 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
4120 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
4121 0 $i++; $re .= quotemeta($c[$i]); $i++; next;
4122 }
4123 1 $re .= quotemeta($c[$i]); $i++;
4124 }
4125 1 $i++; next;
4126 }
4127 246 ▶ if ($ch eq '\\') { # escaped literal
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 1 245 246
4128 1 ▶ $i++; $re .= quotemeta($c[$i]) if $i < $n; $i++; next;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $n 1 0 1
4129 }
4130 245 ▶ if ($ch eq '*') { $re .= '.*'; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "*") 17 228 245
4131 0 ▶ if ($ch eq '?') { $re .= '.'; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "?") 0 228 228
4132 228 ▶ if ($ch eq '[') { # character class
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "[") 15 213 228
4133 15 my $j = $i + 1;
4134 15 my $neg = 0;
4135 15 ▶ 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 14 15
Condition66%4/6
$j < $n and $c[$j] eq "!" || $c[$j] eq "^"
!ll&&!rl&&r
0141
$c[$j] eq "!" or $c[$j] eq "^"
l!l&&r!l&&!r
1014
MC/DC33%1/3
$j < $n and $c[$j] eq "!" || $c[$j] eq "^"
$j < $n $c[$j] eq "!" $c[$j] eq "^"
Truth table50%2/4
A and B or C
A= $j < $nB= $c[$j] eq "!"C= $c[$j] eq "^"
ABCresult
0XX0
1000
11X1
1011
4136 15 my $body = '';
4137 0 ▶ if ($j < $n && $c[$j] eq ']') { $body .= '\\]'; $j++ } # leading ] literal
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $c[$j] eq "]") 0 15 15
Condition33%1/3
$j < $n and $c[$j] eq "]"
!ll&&!rl&&r
0150
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
4138 15 ▶ while ($j < $n && $c[$j] ne ']') {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $c[$j] ne "]") 51 15 66
Condition66%2/3
$j < $n and $c[$j] ne "]"
!ll&&!rl&&r
01551
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
4139 51 my $cc = $c[$j];
4140 51 ▶ if ($cc eq '\\' && $j + 1 < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc eq "\\" and $j + 1 < $n) 0 51 51
Condition33%1/3
$cc eq "\\" and $j + 1 < $n
!ll&&!rl&&r
5100
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
4141 0 $body .= '\\' . $c[$j + 1]; $j += 2; next;
4142 }
4143 0 ▶ if ($cc eq '\\' || $cc eq '^' || $cc eq ']') { $body .= '\\' . $cc }
Branch: 1 branch
BranchTrueFalseTotal
if ($cc eq '\\' or $cc eq '^' or $cc eq ']') { } 0 51 51
Condition33%2/6
$cc eq "\\" or $cc eq "^" or $cc eq "]"
l!l&&r!l&&!r
0051
$cc eq "\\" or $cc eq "^"
l!l&&r!l&&!r
0051
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
4144 51 else { $body .= $cc }
4145 51 $j++;
4146 }
4147 15 ▶ if ($j < $n && $c[$j] eq ']') {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $c[$j] eq "]") 15 0 15
Condition33%1/3
$j < $n and $c[$j] eq "]"
!ll&&!rl&&r
0015
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
4148 15 ▶ $re .= '[' . ($neg ? '^' : '') . $body . ']';
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 1 14 15
4149 15 $i = $j + 1; next;
4150 }
4151 0 $re .= '\\['; $i++; next; # unterminated [ : literal
4152 }
4153 213 $re .= quotemeta($ch);
4154 213 $i++;
4155 }
4156 98 return $re;
4157 }
4158  
4159 # ----------------------------------------------------------------
4160 # extglob (v0.07): ?(list) *(list) +(list) @(list) !(list) pattern-list
4161 # operators, active only while "shopt -s extglob" is on. Shared by
4162 # _case_glob_to_re() (case patterns) and _glob_to_re() (${VAR%pat} and
4163 # friends). $convert_sub converts one pattern-list alternative (which
4164 # may itself contain nested extglob groups) to a regex fragment.
4165 #
4166 # Returns ($pos_after_close_paren, $regex_fragment), or () when the
4167 # text at $i is not a well-formed extglob group (extglob is then left
4168 # to fall through to its ordinary, literal meaning for that character).
4169 #
4170 # !(list) is approximated as "any run of characters that never forms a
4171 # complete match of one of the alternatives" via a negative lookahead
4172 # repeated per character; this matches the common "exclude these whole
4173 # patterns" usage (e.g. !(*.jpg|*.png)) but, unlike real extglob, is not
4174 # exact when !(...) is combined with more pattern after it in the same
4175 # glob -- documented as a known limitation.
4176 # ----------------------------------------------------------------
4177 2 ▶sub _extglob_scan {
Subroutine _extglob_scan: called CC=16 SCAR=32.4
4178 2 my ($chars_ref, $i, $convert_sub) = @_;
4179 2 my @c = @{$chars_ref};
4180 2 my $n = scalar @c;
4181 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
4182 2 my $op = $c[$i];
4183 2 my $depth = 1;
4184 2 my $j = $i + 2;
4185 2 my $body = '';
4186 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
4187 20 my $cc = $c[$j];
4188 0 ▶ 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
4189 2 ▶ elsif ($cc eq ')') { $depth--; $j++; $body .= $cc if $depth > 0 }
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 0 0 2 2
4190 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
4191 18 else { $body .= $cc; $j++ }
4192 }
4193 2 ▶ return () if $depth != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $depth != 0 0 2 2
4194  
4195 2 my @alts = _extglob_split_alts($body);
4196 2 my @re_alts = map { $convert_sub->($_) } @alts;
4197 2 my $inner = '(?:' . join('|', @re_alts) . ')';
4198 2 my $frag;
4199 0 ▶ 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
4200 0 elsif ($op eq '*') { $frag = $inner . '*' }
4201 0 elsif ($op eq '+') { $frag = $inner . '+' }
4202 1 elsif ($op eq '@') { $frag = $inner }
4203 1 elsif ($op eq '!') { $frag = '(?:(?!' . $inner . ').)*' }
4204 0 else { return () }
4205 2 return ($j, $frag);
4206 }
4207  
4208 # _extglob_split_alts: split an extglob pattern-list body on top-level
4209 # '|' (respecting nested parens and backslash escapes).
4210 2 ▶sub _extglob_split_alts {
Subroutine _extglob_split_alts: called CC=8 SCAR=23.8
4211 2 my ($body) = @_;
4212 2 my @out;
4213 2 my $cur = '';
4214 2 my @c = split //, $body;
4215 2 my $n = scalar @c;
4216 2 my $i = 0;
4217 2 my $depth = 0;
4218 2 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 18 2 20
4219 18 my $ch = $c[$i];
4220 0 ▶ 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
4221 0 ▶ if ($ch eq '(') { $depth++; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "(") 0 18 18
4222 0 ▶ if ($ch eq ')') { $depth--; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ")") 0 18 18
4223 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
4224 16 $cur .= $ch; $i++;
4225 }
4226 2 push @out, $cur;
4227 2 return @out;
4228 }
4229  
4230 # ----------------------------------------------------------------
4231 # External command
4232 # ----------------------------------------------------------------
4233 # ----------------------------------------------------------------
4234 # _split_sh_pipe: split a SH command line on bare | characters,
4235 # respecting single-quoted, double-quoted, and $(...) regions.
4236 # Returns a list of segment strings; length 1 means no pipe found.
4237 # ----------------------------------------------------------------
4238 # _split_sh_compound: split a SH line on bare && / || / ;
4239 # Returns list of { op => '', cmd => '...' } hashrefs.
4240 # Length 1 means no compound operator found.
4241 # Respects single-quotes, double-quotes, and $(...) nesting.
4242 # ----------------------------------------------------------------
4243 # _sh_strip_redirects: parse SH-style redirections from a command line.
4244 #
4245 # Recognized forms (processed right-to-left, last one wins per fd):
4246 # cmd > file stdout overwrite
4247 # cmd >> file stdout append
4248 # cmd < file stdin
4249 # cmd 2> file stderr overwrite
4250 # cmd 2>> file stderr append
4251 # cmd 2>&1 stderr to stdout (recorded as fd=2, file='&1')
4252 # cmd 1>&2 stdout to stderr (recorded as fd=1, file='&2')
4253 #
4254 # Returns ($clean_cmd, \@redirs) where each redir is [fd, append, file].
4255 # Parsing respects single-quotes, double-quotes, and backslash escapes.
4256 # ----------------------------------------------------------------
4257 # ----------------------------------------------------------------
4258 # _sh_strip_herestring: detect an unquoted "<<< word" (here-string) on
4259 # an already variable-expanded line. Returns ($line_without_it,
4260 # $dequoted_word) when found, or ($line, undef) otherwise. Only the
4261 # first occurrence on the line is honoured (one here-string per
4262 # command, matching the existing single-here-document limitation).
4263 # ----------------------------------------------------------------
4264 1563 ▶sub _sh_strip_herestring {
Subroutine _sh_strip_herestring: called CC=32 SCAR=37.3
4265 1563 my ($line) = @_;
4266 1563 my @c = split //, $line;
4267 1563 my $n = scalar @c;
4268 1563 my $in_sq = 0;
4269 1563 my $in_dq = 0;
4270 1563 my $i = 0;
4271 1563 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 22843 1561 24404
4272 22843 my $ch = $c[$i];
4273 22843 ▶ if ($in_sq) { $in_sq = 0 if $ch eq "'"; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($in_sq) 391 22452 22843
if $ch eq "'" 43 348 391
4274 22452 ▶ if ($ch eq "'" && !$in_dq) { $in_sq = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $in_dq) 43 22409 22452
Condition100%3/3
$ch eq "'" and not $in_dq
!ll&&!rl&&r
22404543
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
4275 22409 ▶ if ($ch eq '"' && !$in_sq) { $in_dq = !$in_dq; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" and not $in_sq) 1053 21356 22409
Condition66%2/3
$ch eq "\"" and not $in_sq
!ll&&!rl&&r
2135601053
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
4276 21356 ▶ if (!$in_sq && !$in_dq && $ch eq '\\') { $i += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_sq || $in_dq and $ch eq "\\") 3 21353 21356
Condition83%5/6
not $in_sq || $in_dq and $ch eq "\\"
!ll&&!rl&&r
7109142443
$in_sq || $in_dq
l!l&&r!l&&!r
0710914247
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
4277 21353 ▶ 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 21351 21353
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
2135102
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
2135102
not $in_sq || $in_dq and $ch eq "<" and $i + 2 < $n and $c[$i + 1] eq "<"
!ll&&!rl&&r
2134382
not $in_sq || $in_dq and $ch eq "<" and $i + 2 < $n
!ll&&!rl&&r
21343010
not $in_sq || $in_dq and $ch eq "<"
!ll&&!rl&&r
71091423410
$in_sq || $in_dq
l!l&&r!l&&!r
0710914244
$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
4278 && $c[$i+1] eq '<' && $c[$i+2] eq '<'
4279 && !($i+3 < $n && $c[$i+3] eq '<')) {
4280 2 my $before = join('', @c[0 .. $i-1]);
4281 2 my $j = $i + 3;
4282 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
Condition66%4/6
$j < $n and $c[$j] eq " " || $c[$j] eq "\t"
!ll&&!rl&&r
022
$c[$j] eq " " or $c[$j] eq "\t"
l!l&&r!l&&!r
202
MC/DC33%1/3
$j < $n and $c[$j] eq " " || $c[$j] eq "\t"
$j < $n $c[$j] eq " " $c[$j] eq "\t"
Truth table50%2/4
A and B or C
A= $j < $nB= $c[$j] eq " "C= $c[$j] eq "\t"
ABCresult
0XX0
1000
11X1
1011
4283 2 my $wsq = 0;
4284 2 my $wdq = 0;
4285 2 my $word = '';
4286 2 ▶ while ($j < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 15 2 17
4287 15 my $cc = $c[$j];
4288 0 ▶ 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
4289 0 ▶ 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
4290 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
4291 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
4292 13 $word .= $cc; $j++;
4293 }
4294 2 my $after = join('', @c[$j .. $n-1]);
4295 2 return ($before . $after, _arr_dequote($word));
4296 }
4297 21351 $i++;
4298 }
4299 1561 return ($line, undef);
4300 }
4301  
4302 # _read_redir_word: read a redirection target starting at index $start,
4303 # honouring '...' and "..." quoting and backslash escapes so that a
4304 # quoted space stays part of the single filename. Stops at the first
4305 # UNQUOTED whitespace or '<'/'>'. Returns ($raw, $next_index); $raw
4306 # still carries its quote characters -- pass it through _arr_dequote()
4307 # before use. Perl 5.005_03 compatible (index-based scan, no regex on
4308 # the whole word, no prototypes).
4309 34 ▶sub _read_redir_word {
Subroutine _read_redir_word: called CC=13 SCAR=26.6
4310 34 my ($chars, $start, $n) = @_;
4311 34 my $raw = '';
4312 34 my $j = $start;
4313 34 my $sq = 0;
4314 34 my $dq = 0;
4315 34 ▶ while ($j < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 1425 33 1458
4316 1425 my $c = $chars->[$j];
4317 1425 ▶ if ($sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sq) 58 1367 1425
4318 58 $raw .= $c;
4319 58 ▶ $sq = 0 if $c eq "'";
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "'" 1 57 58
4320 58 $j++; next;
4321 }
4322 1367 ▶ if ($c eq "'" && !$dq) { $sq = 1; $raw .= $c; $j++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "'" and not $dq) 1 1366 1367
Condition66%2/3
$c eq "'" and not $dq
!ll&&!rl&&r
136601
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
4323 1366 ▶ if ($c eq '"' && !$sq) { $dq = !$dq; $raw .= $c; $j++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\"" and not $sq) 44 1322 1366
Condition66%2/3
$c eq "\"" and not $sq
!ll&&!rl&&r
1322044
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
4324 1322 ▶ if ($c eq "\\" && !$sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\\" and not $sq) 0 1322 1322
Condition33%1/3
$c eq "\\" and not $sq
!ll&&!rl&&r
132200
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
4325 0 $raw .= $c; $j++;
4326 0 ▶ $raw .= $chars->[$j] if $j < $n;
Branch: 1 branch
BranchTrueFalseTotal
if $j < $n 0 0 0
4327 0 $j++; next;
4328 }
4329 1322 ▶ last if !$dq && $c =~ /[\s<>]/;
Branch: 1 branch
BranchTrueFalseTotal
if not $dq and $c =~ /[\s<>]/ 1 1321 1322
Condition100%3/3
not $dq and $c =~ /[\s<>]/
!ll&&!rl&&r
11551661
MC/DC100%2/2
not $dq and $c =~ /[\s<>]/
not $dq $c =~ /[\s<>]/
Truth table100%3/3
not(A) and B
A= not $dqB= $c =~ /[\s<>]/
ABresult
0X0
100
111
4330 1321 $raw .= $c; $j++;
4331 }
4332 34 return ($raw, $j);
4333 }
4334  
4335 1778 ▶sub _sh_strip_redirects {
Subroutine _sh_strip_redirects: called CC=41 SCAR=38.5
4336 1778 my ($line) = @_;
4337 1778 my @chars = split //, $line;
4338 1778 my $n = scalar @chars;
4339 1778 my @found;
4340 1778 my $clean = '';
4341 1778 my $in_sq = 0;
4342 1778 my $in_dq = 0;
4343 1778 my $i = 0;
4344  
4345 1778 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 28067 1778 29845
4346 28067 my $ch = $chars[$i];
4347  
4348 # Single-quote passthrough
4349 28067 ▶ if ($in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq) 337 27730 28067
4350 337 ▶ if ($ch eq "'") { $in_sq = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'") 43 294 337
4351 337 $clean .= $ch; $i++; next;
4352 }
4353 27730 ▶ if ($ch eq "'" && !$in_dq) { $in_sq = 1; $clean .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $in_dq) 43 27687 27730
Condition100%3/3
$ch eq "'" and not $in_dq
!ll&&!rl&&r
27682543
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
4354  
4355 # Double-quote toggle
4356 27687 ▶ if ($ch eq '"' && !$in_sq) { $in_dq = !$in_dq; $clean .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" and not $in_sq) 1230 26457 27687
Condition66%2/3
$ch eq "\"" and not $in_sq
!ll&&!rl&&r
2645701230
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
4357  
4358 # Inside double-quotes: only escape matters
4359 26457 ▶ if ($in_dq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_dq) 6943 19514 26457
4360 6943 ▶ if ($ch eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 19 6924 6943
4361 19 $clean .= $ch; $i++;
4362 19 ▶ $clean .= $chars[$i] if $i < $n; $i++; next;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $n 19 0 19
4363 }
4364 6924 $clean .= $ch; $i++; next;
4365 }
4366  
4367 # Backslash escape outside quotes
4368 19514 ▶ if ($ch eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 2 19512 19514
4369 2 $clean .= $ch; $i++;
4370 2 ▶ $clean .= $chars[$i] if $i < $n; $i++; next;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $n 2 0 2
4371 }
4372  
4373 # 2>&1 or 2>>&1 or 1>&2
4374 19512 ▶ 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 19512 19512
$i + 3 < $n ? : 3 0 3
Condition75%9/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
1951200
$ch =~ /[012]/ and $i + 2 < $n and $chars[$i + 1] eq ">" and $i + 3 < $n ? $chars[$i + 2] eq ">" : 0
!ll&&!rl&&r
1950930
$ch =~ /[012]/ and $i + 2 < $n and $chars[$i + 1] eq ">"
!ll&&!rl&&r
191373723
$ch =~ /[012]/ and $i + 2 < $n
!ll&&!rl&&r
18919218375
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 table66%4/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
4375 && $chars[$i+1] eq '>'
4376 && ($i+3 < $n ? $chars[$i+2] eq '>' : 0)
4377 && $chars[$i+3] eq '&') {
4378 # 2>>&1 form (rare but handle)
4379 0 my $fd = int($ch);
4380 0 my $j = $i + 4;
4381 0 my $tgt = '';
4382 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
4383 0 push @found, [$fd, 0, "&$tgt"];
4384 0 $i = $j; next;
4385 }
4386 19512 ▶ 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 "&") 2 19510 19512
Condition100%9/9
$ch =~ /[012]/ and $i + 2 < $n and $chars[$i + 1] eq ">" and $chars[$i + 2] eq "&"
!ll&&!rl&&r
1950912
$ch =~ /[012]/ and $i + 2 < $n and $chars[$i + 1] eq ">"
!ll&&!rl&&r
191373723
$ch =~ /[012]/ and $i + 2 < $n
!ll&&!rl&&r
18919218375
MC/DC100%4/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 table100%5/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
4387 && $chars[$i+1] eq '>' && $chars[$i+2] eq '&') {
4388 2 my $fd = int($ch);
4389 2 my $j = $i + 3;
4390 2 my $tgt = '';
4391 2 ▶ while ($j < $n && $chars[$j] =~ /\S/) { $tgt .= $chars[$j]; $j++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n and $chars[$j] =~ /\S/) 2 2 4
Condition66%2/3
$j < $n and $chars[$j] =~ /\S/
!ll&&!rl&&r
202
MC/DC50%1/2
$j < $n and $chars[$j] =~ /\S/
$j < $n $chars[$j] =~ /\S/
Truth table66%2/3
A and B
A= $j < $nB= $chars[$j] =~ /\S/
ABresult
0X0
100
111
4392 2 push @found, [$fd, 0, "&$tgt"];
4393 2 $i = $j; next;
4394 }
4395  
4396 # fd> or fd>> (fd is 0,1,2; or implicit 1 when just > or >>)
4397 19510 my $redir_fd = undef;
4398 19510 ▶ 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 '>') { } 1 19509 19510
elsif ($ch eq '<') { } 13 19496 19509
elsif ($ch eq '>') { } 20 19476 19496
Condition100%6/6
$ch =~ /[012]/ and $i + 1 < $n and $chars[$i + 1] eq ">"
!ll&&!rl&&r
190764331
$ch =~ /[012]/ and $i + 1 < $n
!ll&&!rl&&r
18919157434
MC/DC100%3/3
$ch =~ /[012]/ and $i + 1 < $n and $chars[$i + 1] eq ">"
$ch =~ /[012]/ $i + 1 < $n $chars[$i + 1] eq ">"
Truth table100%4/4
A and B and C
A= $ch =~ /[012]/B= $i + 1 < $nC= $chars[$i + 1] eq ">"
ABCresult
0XX0
10X0
1100
1111
4399 1 $redir_fd = int($ch); $i++;
4400 }
4401 elsif ($ch eq '<') {
4402 # < file (stdin)
4403 13 my $j = $i + 1;
4404 13 ▶ $j++ while $j < $n && $chars[$j] eq ' ';
Branch: 1 branch
BranchTrueFalseTotal
$j < $n and $chars[$j] eq " " and do { ++$j } 13 13 26
Condition66%2/3
$j < $n and $chars[$j] eq " "
!ll&&!rl&&r
01313
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
4405 13 my ($raw, $nj) = _read_redir_word(\@chars, $j, $n);
4406 13 ▶ push @found, [0, 0, _arr_dequote($raw)] if $raw ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $raw ne "" 13 0 13
4407 13 $i = $nj; next;
4408 }
4409 elsif ($ch eq '>') {
4410 20 $redir_fd = 1;
4411 }
4412  
4413 19497 ▶ if (defined $redir_fd) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $redir_fd) 21 19476 19497
4414 # Check for >>
4415 21 my $append = 0;
4416 21 ▶ if ($i+1 < $n && $chars[$i+1] eq '>') { $append = 1; $i++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 1 < $n and $chars[$i + 1] eq ">") 3 18 21
Condition66%2/3
$i + 1 < $n and $chars[$i + 1] eq ">"
!ll&&!rl&&r
0183
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
4417 # Skip spaces
4418 21 $i++;
4419 21 ▶ $i++ while $i < $n && $chars[$i] eq ' ';
Branch: 1 branch
BranchTrueFalseTotal
$i < $n and $chars[$i] eq " " and do { ++$i } 20 21 41
Condition66%2/3
$i < $n and $chars[$i] eq " "
!ll&&!rl&&r
02120
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
4420 # Read filename, honouring quotes (so a quoted space stays in
4421 # the name), then strip the quotes.
4422 21 my ($raw, $nj) = _read_redir_word(\@chars, $i, $n);
4423 21 ▶ push @found, [$redir_fd, $append, _arr_dequote($raw)] if $raw ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $raw ne "" 21 0 21
4424 21 $i = $nj;
4425 21 next;
4426 }
4427  
4428 19476 $clean .= $ch; $i++;
4429 }
4430  
4431 1778 $clean =~ s/\s+\z//;
4432 1778 return ($clean, \@found);
4433 }
4434  
4435 # ----------------------------------------------------------------
4436 # _sh_exec_with_redirs: apply I/O redirections then execute a SH line.
4437 # Perl 5.005_03 compatible: fixed bareword FHs, 2-argument open.
4438 # Supports: > >> < 2> 2>> 2>&1 1>&2
4439 # ----------------------------------------------------------------
4440 42 ▶sub _sh_exec_with_redirs {
Subroutine _sh_exec_with_redirs: called CC=44 SCAR=44.9
4441 42 my ($class, $line, $redirs_ref, $opts_ref) = @_;
4442  
4443 # Collect per-fd: stdin, stdout, stderr
4444 42 my ($in_file, $out_file, $out_app, $err_file, $err_app);
4445 42 my $err_to_stdout = 0; # 2>&1
4446 42 my $out_to_stderr = 0; # 1>&2
4447  
4448 42 for my $r (@{$redirs_ref}) {
4449 43 my ($fd, $append, $file) = @{$r};
4450 43 ▶ $file = BATsh::MB::dec($file) unless $file =~ /\A&[12]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /\A&[12]\z/ 41 2 43
4451 43 ▶ if ($fd == 0) { $in_file = $file; }
Branch: 2 branches
BranchTrueFalseTotal
if ($fd == 0) { } 25 18 43
elsif ($fd == 1) { } 15 3 18
4452 elsif ($fd == 1) {
4453 0 ▶ if ($file eq '&2') { $out_to_stderr = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq '&2') { } 0 15 15
4454 15 else { $out_file = $file; $out_app = $append }
4455 }
4456 else { # fd == 2
4457 3 ▶ if ($file eq '&1') { $err_to_stdout = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq '&1') { } 2 1 3
4458 1 else { $err_file = $file; $err_app = $append }
4459 }
4460 }
4461  
4462 42 my $ok = 1;
4463 42 my ($saved_in, $saved_out, $saved_err) = (0, 0, 0);
4464  
4465 # --- stdin ---
4466 42 ▶ if (defined $in_file && $ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $in_file and $ok) 25 17 42
Condition66%2/3
defined $in_file and $ok
!ll&&!rl&&r
17025
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
4467 sysopen(_SH_REDIR_SRC, $in_file, O_RDONLY)
4468 25 ▶ 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 } 24 1 25
4469 25 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 24 1 25
4470 0 ▶ open(_SH_REDIR_SAVIN, '<&STDIN') or do { $ok = 0 };
Branch: 1 branch
BranchTrueFalseTotal
open _SH_REDIR_SAVIN, "<&STDIN" or do { $ok = 0 } 24 0 24
4471 }
4472 25 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 24 1 25
4473 0 ▶ open(STDIN, '<&_SH_REDIR_SRC') or do { $ok = 0 };
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<&_SH_REDIR_SRC" or do { $ok = 0 } 24 0 24
4474 24 close(_SH_REDIR_SRC);
4475 24 $saved_in = 1;
4476 }
4477 }
4478  
4479 # --- stdout ---
4480 42 ▶ if (defined $out_file && $ok) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $out_file and $ok) { } 15 27 42
elsif ($out_to_stderr and $ok) { } 0 54 54
Condition66%2/3
defined $out_file and $ok
!ll&&!rl&&r
27015
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
4481 sysopen(_SH_REDIR_DST, $out_file,
4482 O_WRONLY | O_CREAT | ($out_app ? O_APPEND : O_TRUNC), 0666)
4483 0 ▶ 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 } 15 0 15
$out_app ? : 2 13 15
4484 15 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 15 0 15
4485 0 ▶ open(_SH_REDIR_SAVOUT, '>&STDOUT') or do { $ok = 0 };
Branch: 1 branch
BranchTrueFalseTotal
open _SH_REDIR_SAVOUT, ">&STDOUT" or do { $ok = 0 } 15 0 15
4486 }
4487 15 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 15 0 15
4488 0 ▶ open(STDOUT, '>&_SH_REDIR_DST') or do { $ok = 0 };
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&_SH_REDIR_DST" or do { $ok = 0 } 15 0 15
4489 15 close(_SH_REDIR_DST);
4490 15 $saved_out = 1;
4491 }
4492 }
4493 ▶ elsif ($out_to_stderr && $ok) {
Condition33%1/3
$out_to_stderr and $ok
!ll&&!rl&&r
2700
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
4494 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
4495 0 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 0 0 0
4496 0 ▶ open(STDOUT, '>&STDERR') or do { $ok = 0 };
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&STDERR" or do { $ok = 0 } 0 0 0
4497 0 $saved_out = 1;
4498 }
4499 }
4500  
4501 # --- stderr ---
4502 42 ▶ if (defined $err_file && $ok) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $err_file and $ok) { } 1 41 42
elsif ($err_to_stdout and $ok) { } 2 78 80
Condition66%2/3
defined $err_file and $ok
!ll&&!rl&&r
4101
MC/DC50%1/2
defined $err_file and $ok
defined $err_file $ok
Truth table66%2/3
A and B
A= defined $err_fileB= $ok
ABresult
0X0
100
111
4503 sysopen(_SH_REDIR_DST, $err_file,
4504 O_WRONLY | O_CREAT | ($err_app ? O_APPEND : O_TRUNC), 0666)
4505 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 } 1 0 1
$err_app ? : 0 1 1
4506 1 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 1 0 1
4507 0 ▶ open(_SH_REDIR_SAVERR, '>&STDERR') or do { $ok = 0 };
Branch: 1 branch
BranchTrueFalseTotal
open _SH_REDIR_SAVERR, ">&STDERR" or do { $ok = 0 } 1 0 1
4508 }
4509 1 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 1 0 1
4510 0 ▶ open(STDERR, '>&_SH_REDIR_DST') or do { $ok = 0 };
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&_SH_REDIR_DST" or do { $ok = 0 } 1 0 1
4511 1 close(_SH_REDIR_DST);
4512 1 $saved_err = 1;
4513 }
4514 }
4515 ▶ elsif ($err_to_stdout && $ok) {
Condition66%2/3
$err_to_stdout and $ok
!ll&&!rl&&r
3902
MC/DC50%1/2
$err_to_stdout and $ok
$err_to_stdout $ok
Truth table66%2/3
A and B
A= $err_to_stdoutB= $ok
ABresult
0X0
100
111
4516 # Redirect stderr to the current STDOUT (which may itself be redirected)
4517 0 ▶ open(_SH_REDIR_SAVERR, '>&STDERR') or do { $ok = 0 };
Branch: 1 branch
BranchTrueFalseTotal
open _SH_REDIR_SAVERR, ">&STDERR" or do { $ok = 0 } 2 0 2
4518 2 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 2 0 2
4519 0 ▶ open(STDERR, '>&STDOUT') or do { $ok = 0 };
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&STDOUT" or do { $ok = 0 } 2 0 2
4520 2 $saved_err = 1;
4521 }
4522 }
4523  
4524 42 my $rc = 0;
4525 42 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 41 1 42
4526 41 $rc = _exec_line($class, $line, $opts_ref);
4527 }
4528  
4529 # Restore in reverse order
4530 42 ▶ if ($saved_err) { open(STDERR, '>&_SH_REDIR_SAVERR'); close(_SH_REDIR_SAVERR) }
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_err) 3 39 42
4531 42 ▶ if ($saved_out) { open(STDOUT, '>&_SH_REDIR_SAVOUT'); close(_SH_REDIR_SAVOUT) }
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_out) 15 27 42
4532 42 ▶ if ($saved_in) { open(STDIN, '<&_SH_REDIR_SAVIN'); close(_SH_REDIR_SAVIN) }
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_in) 24 18 42
4533  
4534 42 return $rc;
4535 }
4536  
4537 # ----------------------------------------------------------------
4538 # _split_top_semi: split a physical line on TOP-LEVEL ';' only, keeping
4539 # single/double quotes, backticks, $(...) / ${...} / <(...) / >(...)
4540 # and plain (...) groups opaque. Returns the ';'-separated segments
4541 # (the ';' itself is not included). Used by the inline-control
4542 # expander (_inline_expand) and by _inline_has_terminator. Pure Perl
4543 # 5.005_03 (hand-rolled char scan; no regex features).
4544 # ----------------------------------------------------------------
4545 183 ▶sub _split_top_semi {
Subroutine _split_top_semi: called CC=26 SCAR=36.1
4546 183 my ($line) = @_;
4547 183 my @segs;
4548 183 my $cur = '';
4549 183 my $in_sq = 0;
4550 183 my $in_dq = 0;
4551 183 my $in_bt = 0; # backtick `...`
4552 183 my $pdep = 0; # ( ) depth (covers $( <( >( and plain ( )
4553 183 my $bdep = 0; # ${ } depth
4554 183 my @c = split //, $line;
4555 183 my $n = scalar @c;
4556 183 my $i = 0;
4557  
4558 183 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 4316 183 4499
4559 4316 my $ch = $c[$i];
4560  
4561 4316 ▶ if ($in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq) 12 4304 4316
4562 12 ▶ $cur .= $ch; $in_sq = 0 if $ch eq "'"; $i++; next;
Branch: 1 branch
BranchTrueFalseTotal
if $ch eq "'" 3 9 12
4563 }
4564 4304 ▶ 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 4301 4304
Condition66%4/6
$ch eq "'" and not $in_dq and not $in_bt
!ll&&!rl&&r
430103
$ch eq "'" and not $in_dq
!ll&&!rl&&r
430103
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
4565 4301 ▶ if ($ch eq '"' && !$in_bt) { $in_dq = !$in_dq; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" and not $in_bt) 156 4145 4301
Condition66%2/3
$ch eq "\"" and not $in_bt
!ll&&!rl&&r
41450156
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
4566  
4567 # Backslash escape: copy the next char verbatim
4568 4145 ▶ if ($ch eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 0 4145 4145
4569 0 $cur .= $ch; $i++;
4570 0 ▶ $cur .= $c[$i] if $i < $n; $i++; next;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $n 0 0 0
4571 }
4572  
4573 0 ▶ if ($ch eq '`') { $in_bt = !$in_bt; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "`") 0 4145 4145
4574  
4575 4145 ▶ if (!$in_dq) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_dq) 3358 787 4145
4576 3358 ▶ 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 3358 3358
Condition66%4/6
$ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "{"
!ll&&!rl&&r
3302560
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
3302056
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
4577 0 $bdep++; $cur .= '${'; $i += 2; next;
4578 }
4579 0 ▶ if ($bdep > 0 && $ch eq '}') { $bdep--; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($bdep > 0 and $ch eq "}") 0 3358 3358
Condition33%1/3
$bdep > 0 and $ch eq "}"
!ll&&!rl&&r
335800
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
4580 3358 ▶ if ($ch eq '(') { $pdep++; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "(") 2 3356 3358
4581 3356 ▶ if ($ch eq ')' && $pdep > 0) { $pdep--; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ")" and $pdep > 0) 2 3354 3356
Condition66%2/3
$ch eq ")" and $pdep > 0
!ll&&!rl&&r
335402
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
4582 }
4583  
4584 4141 ▶ 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 ";") 153 3988 4141
Condition83%10/12
not $in_dq || $in_bt and $pdep == 0 and $bdep == 0 and $ch eq ";"
!ll&&!rl&&r
7943194153
not $in_dq || $in_bt and $pdep == 0 and $bdep == 0
!ll&&!rl&&r
79403347
not $in_dq || $in_bt and $pdep == 0
!ll&&!rl&&r
78773347
$in_dq || $in_bt
l!l&&r!l&&!r
78703354
MC/DC60%3/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 table66%4/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
4585 153 push @segs, $cur; $cur = ''; $i++;
4586 # collapse a following ';' (e.g. ";;") into the same split so
4587 # empty segments are not produced for the common cases
4588 153 next;
4589 }
4590  
4591 3988 $cur .= $ch; $i++;
4592 }
4593 183 push @segs, $cur;
4594 183 return @segs;
4595 }
4596  
4597 # _inline_has_terminator: does the SINGLE physical line hold, as a
4598 # top-level ';'-delimited segment, a bare terminator word ($term, e.g.
4599 # 'fi' / 'done' / 'esac')? Used to detect a fully-inline control
4600 # structure written on one physical line.
4601 84 ▶sub _inline_has_terminator {
Subroutine _inline_has_terminator: called CC=4 SCAR=13.9
4602 84 my ($line, $term) = @_;
4603 84 ▶ return 0 unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 84 84
4604 84 for my $seg (_split_top_semi($line)) {
4605 172 my $s = $seg;
4606 172 $s =~ s/\A\s+//; $s =~ s/\s+\z//;
4607 172 ▶ return 1 if lc($s) eq lc($term);
Branch: 1 branch
BranchTrueFalseTotal
if lc $s eq lc $term 32 140 172
4608 }
4609 52 return 0;
4610 }
4611  
4612 # _inline_expand: turn a fully-inline control-structure physical line
4613 # into the list of "logical lines" the multi-line block parsers expect.
4614 # It splits on top-level ';' and then peels a leading 'then'/'do'/'else'
4615 # keyword off its segment onto its own logical line (so "then echo a"
4616 # becomes "then" + "echo a"), which is exactly the shape _parse_if /
4617 # _parse_for / _parse_while consume line-by-line.
4618 30 ▶sub _inline_expand {
Subroutine _inline_expand: called CC=6 SCAR=18.3
4619 30 my ($line) = @_;
4620 30 my @out;
4621 30 for my $seg (_split_top_semi($line)) {
4622 93 my $s = $seg;
4623 93 $s =~ s/\A\s+//; $s =~ s/\s+\z//;
4624 93 ▶ next if $s eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "" 0 93 93
4625 93 ▶ if ($s =~ /\A(then|do|else)\b\s*(.*)\z/is) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A(then|do|else)\b\s*(.*)\z/is) { } 33 60 93
4626 33 my ($kw, $tail) = (lc($1), $2);
4627 33 push @out, $kw;
4628 33 ▶ push @out, $tail if defined $tail && $tail =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tail and $tail =~ /\S/ 33 0 33
Condition33%1/3
defined $tail and $tail =~ /\S/
!ll&&!rl&&r
0033
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
4629 }
4630 else {
4631 60 push @out, $s;
4632 }
4633 }
4634 30 return @out;
4635 }
4636  
4637 # _strip_sh_comment: remove a trailing "# ..." comment from one SH
4638 # physical line. A '#' introduces a comment only when it is unquoted,
4639 # outside any $(...)/${...}/`...` region, and begins a word (preceded by
4640 # the start of line or by whitespace / ; / & / | / '(' ). This leaves
4641 # parameter forms such as $#, ${#var}, ${var#pat} and an in-word '#'
4642 # (echo a#b, http://h#frag) untouched, matching POSIX shells. Pure Perl
4643 # 5.005_03 (hand-rolled scan; no regex features).
4644 2202 ▶sub _strip_sh_comment {
Subroutine _strip_sh_comment: called CC=36 SCAR=41.8
4645 2202 my ($line) = @_;
4646 2202 ▶ return $line unless defined $line && index($line, '#') >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line and index($line, "#") != -1 2170 32 2202
Condition66%2/3
defined $line and index($line, "#") != -1
!ll&&!rl&&r
0217032
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
4647 32 my @c = split //, $line;
4648 32 my $n = scalar @c;
4649 32 my $in_sq = 0;
4650 32 my $in_dq = 0;
4651 32 my $in_bt = 0;
4652 32 my $pdep = 0;
4653 32 my $bdep = 0;
4654 32 my $prev = ''; # previous scanned char (for word-boundary test)
4655 32 my $i = 0;
4656 32 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 695 25 720
4657 695 my $ch = $c[$i];
4658 0 ▶ if ($in_sq) { $in_sq = 0 if $ch eq "'"; $prev = $ch; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($in_sq) 0 695 695
if $ch eq "'" 0 0 0
4659 0 ▶ 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 695 695
Condition33%2/6
$ch eq "'" and not $in_dq and not $in_bt
!ll&&!rl&&r
69500
$ch eq "'" and not $in_dq
!ll&&!rl&&r
69500
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
4660 695 ▶ if ($ch eq '"' && !$in_bt) { $in_dq = !$in_dq; $prev = $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" and not $in_bt) 26 669 695
Condition66%2/3
$ch eq "\"" and not $in_bt
!ll&&!rl&&r
669026
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
4661 669 ▶ if ($ch eq '\\') { $prev = 'x'; $i += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 1 668 669
4662 0 ▶ if ($ch eq '`') { $in_bt = !$in_bt; $prev = $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "`") 0 668 668
4663 668 ▶ if (!$in_dq && !$in_bt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_dq or $in_bt) 428 240 668
Condition66%2/3
$in_dq or $in_bt
l!l&&r!l&&!r
2400428
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
4664 428 ▶ 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 409 428
Condition83%5/6
$ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "{"
!ll&&!rl&&r
3991019
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
399029
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
4665 409 ▶ if ($bdep > 0 && $ch eq '}') { $bdep--; $prev = $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($bdep > 0 and $ch eq "}") 19 390 409
Condition100%3/3
$bdep > 0 and $ch eq "}"
!ll&&!rl&&r
28910119
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
4666 0 ▶ if ($ch eq '(') { $pdep++; $prev = $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "(") 0 390 390
4667 0 ▶ if ($ch eq ')' && $pdep > 0) { $pdep--; $prev = $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ")" and $pdep > 0) 0 390 390
Condition33%1/3
$ch eq ")" and $pdep > 0
!ll&&!rl&&r
39000
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
4668 }
4669 630 ▶ 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) 12 618 630
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
6041412
$ch eq "#" and not $in_sq and not $in_dq and not $in_bt and $pdep == 0
!ll&&!rl&&r
604026
$ch eq "#" and not $in_sq and not $in_dq and not $in_bt
!ll&&!rl&&r
604026
$ch eq "#" and not $in_sq and not $in_dq
!ll&&!rl&&r
597726
$ch eq "#" and not $in_sq
!ll&&!rl&&r
597033
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
4670 12 ▶ 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 "(") 7 5 12
Condition73%11/15
$prev eq "" or $prev =~ /\s/ or $prev eq ";" or $prev eq "&" or $prev eq "|" or $prev eq "("
l!l&&r!l&&!r
705
$prev eq "" or $prev =~ /\s/ or $prev eq ";" or $prev eq "&" or $prev eq "|"
l!l&&r!l&&!r
705
$prev eq "" or $prev =~ /\s/ or $prev eq ";" or $prev eq "&"
l!l&&r!l&&!r
705
$prev eq "" or $prev =~ /\s/ or $prev eq ";"
l!l&&r!l&&!r
705
$prev eq "" or $prev =~ /\s/
l!l&&r!l&&!r
615
MC/DC33%2/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 table42%3/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
4671 || $prev eq ';' || $prev eq '&' || $prev eq '|' || $prev eq '(') {
4672 7 ▶ my $out = ($i > 0) ? join('', @c[0 .. $i-1]) : '';
Branch: 1 branch
BranchTrueFalseTotal
$i > 0 ? : 1 6 7
4673 7 $out =~ s/\s+\z//;
4674 7 return $out;
4675 }
4676 }
4677 623 $prev = $ch; $i++;
4678 }
4679 25 return $line;
4680 }
4681  
4682 # _if_depth_delta: net change in if/fi nesting contributed by one
4683 # physical line, counting only command-position 'if' openers (+1) and
4684 # 'fi' closers (-1). A fully-inline "if ...; then ...; fi" nets to 0.
4685 # Used by _parse_if's body collector so a nested if is not closed by the
4686 # outer 'fi'. Other block types (for/while/case) use different
4687 # terminators (done/esac) and so never affect the if/fi balance.
4688 69 ▶sub _if_depth_delta {
Subroutine _if_depth_delta: called CC=5 SCAR=16.1
4689 69 my ($line) = @_;
4690 69 my $d = 0;
4691 69 for my $seg (_split_top_semi($line)) {
4692 71 my $s = $seg;
4693 71 $s =~ s/\A\s+//;
4694 71 $s =~ s/\A(?:then|do|else)\b\s*//i; # peel a leading block keyword
4695 71 my ($w) = ($s =~ /\A(\S+)/);
4696 71 ▶ $w = defined($w) ? lc($w) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $w ? : 71 0 71
4697 71 ▶ $d++ if $w eq 'if';
Branch: 1 branch
BranchTrueFalseTotal
if $w eq "if" 1 70 71
4698 71 ▶ $d-- if $w eq 'fi';
Branch: 1 branch
BranchTrueFalseTotal
if $w eq "fi" 1 70 71
4699 }
4700 69 return $d;
4701 }
4702  
4703 # _find_control_split: when a physical line does NOT begin with a
4704 # control-structure keyword but a control opener (if/for/while/until/
4705 # case/select) appears later in COMMAND position immediately after a
4706 # top-level ';' separator (e.g. x=""; if [ -z "$x" ]; then ...; fi),
4707 # return the byte offset at which that opener begins so the caller can
4708 # run the prefix and then re-dispatch the control structure through the
4709 # normal block parser. Only a ';' separator is honoured (sequential,
4710 # so splitting is semantically safe); a control opener after && / || is
4711 # left alone. Returns undef when there is nothing to split.
4712 1513 ▶sub _find_control_split {
Subroutine _find_control_split: called CC=39 SCAR=36.7
4713 1513 my ($line) = @_;
4714 1513 ▶ return undef unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 1513 1513
4715 1513 my @c = split //, $line;
4716 1513 my $n = scalar @c;
4717 1513 my $in_sq = 0;
4718 1513 my $in_dq = 0;
4719 1513 my $in_bt = 0;
4720 1513 my $pdep = 0;
4721 1513 my $bdep = 0;
4722 1513 my $i = 0;
4723 1513 my $cmd_pos = 1; # are we at the start of a command word?
4724  
4725 1513 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 26767 1508 28275
4726 26767 my $ch = $c[$i];
4727  
4728 26767 ▶ if ($in_sq) { $in_sq = 0 if $ch eq "'"; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($in_sq) 624 26143 26767
if $ch eq "'" 64 560 624
4729 26143 ▶ 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) 64 26079 26143
Condition83%5/6
$ch eq "'" and not $in_dq and not $in_bt
!ll&&!rl&&r
26079064
$ch eq "'" and not $in_dq
!ll&&!rl&&r
26072764
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
4730 26079 ▶ 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) 1090 24989 26079
Condition66%2/3
$ch eq "\"" and not $in_bt
!ll&&!rl&&r
2498901090
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
4731 24989 ▶ if ($ch eq '\\') { $i += 2; $cmd_pos = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 20 24969 24989
4732 24969 ▶ if ($ch eq '`') { $in_bt = !$in_bt; $cmd_pos = 0; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "`") 2 24967 24969
4733  
4734 24967 ▶ if (!$in_dq && !$in_bt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_dq or $in_bt) 16901 8066 24967
Condition100%3/3
$in_dq or $in_bt
l!l&&r!l&&!r
80561016901
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
4735 16901 ▶ 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 16863 16901
Condition83%5/6
$ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "{"
!ll&&!rl&&r
1645241138
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
164520449
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
4736 16863 ▶ if ($bdep > 0 && $ch eq '}') { $bdep--; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($bdep > 0 and $ch eq "}") 38 16825 16863
Condition100%3/3
$bdep > 0 and $ch eq "}"
!ll&&!rl&&r
1657325238
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
4737 16825 ▶ if ($ch eq '(') { $pdep++; $i++; $cmd_pos = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "(") 313 16512 16825
4738 16512 ▶ if ($ch eq ')' && $pdep > 0) { $pdep--; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ")" and $pdep > 0) 313 16199 16512
Condition66%2/3
$ch eq ")" and $pdep > 0
!ll&&!rl&&r
161990313
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
4739 }
4740  
4741 24265 ▶ 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) 14482 9783 24265
Condition100%9/9
not $in_dq || $in_bt and $pdep == 0 and $bdep == 0
!ll&&!rl&&r
953125214482
not $in_dq || $in_bt and $pdep == 0
!ll&&!rl&&r
8066146514734
$in_dq || $in_bt
l!l&&r!l&&!r
80561016199
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
4742 14482 ▶ if ($ch eq ';') { $cmd_pos = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ";") 64 14418 14482
4743 14418 ▶ 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 "&") 12 14406 14418
Condition100%6/6
$ch eq "&" and $i + 1 < $n and $c[$i + 1] eq "&"
!ll&&!rl&&r
14400612
$ch eq "&" and $i + 1 < $n
!ll&&!rl&&r
14397318
MC/DC100%3/3
$ch eq "&" and $i + 1 < $n and $c[$i + 1] eq "&"
$ch eq "&" $i + 1 < $n $c[$i + 1] eq "&"
Truth table100%4/4
A and B and C
A= $ch eq "&"B= $i + 1 < $nC= $c[$i + 1] eq "&"
ABCresult
0XX0
10X0
1100
1111
4744 14406 ▶ 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 14400 14406
Condition83%5/6
$ch eq "|" and $i + 1 < $n and $c[$i + 1] eq "|"
!ll&&!rl&&r
14372286
$ch eq "|" and $i + 1 < $n
!ll&&!rl&&r
14372034
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
4745 14400 ▶ if ($ch eq '|' || $ch eq '&') { $cmd_pos = 0; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "|" or $ch eq "&") 37 14363 14400
Condition100%3/3
$ch eq "|" or $ch eq "&"
l!l&&r!l&&!r
28914363
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
4746 14363 ▶ if ($ch =~ /\s/) { $i++; next } # whitespace keeps command position
Branch: 1 branch
BranchTrueFalseTotal
if ($ch =~ /\s/) 2904 11459 14363
4747  
4748 # A non-space, non-separator byte: if we are at command
4749 # position and NOT at the very start (offset 0), test for a
4750 # control opener keyword here.
4751 11459 ▶ if ($cmd_pos && $i > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd_pos and $i > 0) 317 11142 11459
Condition100%3/3
$cmd_pos and $i > 0
!ll&&!rl&&r
98831259317
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
4752 317 my $tail = join('', @c[$i .. $n-1]);
4753 317 ▶ 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 312 317
4754 5 return $i;
4755 }
4756 }
4757 11454 $cmd_pos = 0; $i++; next;
4758 }
4759  
4760 9783 $i++;
4761 }
4762 1508 return undef;
4763 }
4764  
4765 # _run_inline_control_redir: run a one-line control structure with the
4766 # output redirections that were written after its terminator. STDOUT and
4767 # STDERR are repointed at the Perl level for the duration of the structure
4768 # and restored afterwards, including when the body dies.
4769 1 ▶sub _run_inline_control_redir {
Subroutine _run_inline_control_redir: called CC=22 SCAR=45.7
4770 1 my ($class, $head, $redirs_ref, $opts_ref) = @_;
4771  
4772 1 my ($out_file, $out_app, $err_file, $err_app);
4773 1 my $err_to_stdout = 0;
4774 1 for my $r (@{$redirs_ref}) {
4775 1 my ($fd, $append, $file) = @{$r};
4776 1 ▶ $file = BATsh::MB::dec($file) unless $file =~ /\A&[12]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /\A&[12]\z/ 1 0 1
4777 1 ▶ if ($fd == 1) { $out_file = $file; $out_app = $append }
Branch: 2 branches
BranchTrueFalseTotal
if ($fd == 1) { } 1 0 1
elsif ($fd == 2) { } 0 0 0
4778 elsif ($fd == 2) {
4779 0 ▶ if ($file eq '&1') { $err_to_stdout = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq '&1') { } 0 0 0
4780 0 else { $err_file = $file; $err_app = $append }
4781 }
4782 }
4783  
4784 1 my ($sv_out, $sv_err) = (0, 0);
4785 1 ▶ if (defined $out_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out_file) 1 0 1
4786 1 ▶ $sv_out = 1 if open(_CTLRD_SAVE_OUT, '>&STDOUT');
Branch: 1 branch
BranchTrueFalseTotal
if open _CTLRD_SAVE_OUT, ">&STDOUT" 1 0 1
4787 1 ▶ my $mode = $out_app ? ">>$out_file" : ">$out_file";
Branch: 1 branch
BranchTrueFalseTotal
$out_app ? : 0 1 1
4788 1 ▶ if (!open(STDOUT, $mode)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open STDOUT, $mode) 0 1 1
4789 0 ▶ if ($sv_out) { open(STDOUT, '>&_CTLRD_SAVE_OUT');
Branch: 1 branch
BranchTrueFalseTotal
if ($sv_out) 0 0 0
4790 0 close(_CTLRD_SAVE_OUT) }
4791 0 warn "sh: $out_file: cannot open\n";
4792 0 $LAST_STATUS = 1;
4793 0 return 1;
4794 }
4795 }
4796 1 ▶ if (defined $err_file || $err_to_stdout) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err_file or $err_to_stdout) 0 1 1
Condition33%1/3
defined $err_file or $err_to_stdout
l!l&&r!l&&!r
001
MC/DC0%0/2
defined $err_file or $err_to_stdout
defined $err_file $err_to_stdout
Truth table33%1/3
A or B
A= defined $err_fileB= $err_to_stdout
ABresult
1X1
011
000
4797 0 ▶ $sv_err = 1 if open(_CTLRD_SAVE_ERR, '>&STDERR');
Branch: 1 branch
BranchTrueFalseTotal
if open _CTLRD_SAVE_ERR, ">&STDERR" 0 0 0
4798 0 my $ok;
4799 0 ▶ if ($err_to_stdout) { $ok = open(STDERR, '>&STDOUT') }
Branch: 1 branch
BranchTrueFalseTotal
if ($err_to_stdout) { } 0 0 0
4800 0 ▶ else { $ok = open(STDERR, ($err_app ? ">>$err_file" : ">$err_file")) }
Branch: 1 branch
BranchTrueFalseTotal
$err_app ? : 0 0 0
4801 0 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
4802 0 ▶ if ($sv_err) { open(STDERR, '>&_CTLRD_SAVE_ERR');
Branch: 1 branch
BranchTrueFalseTotal
if ($sv_err) 0 0 0
4803 0 close(_CTLRD_SAVE_ERR) }
4804 0 $sv_err = 0;
4805 }
4806 }
4807  
4808 1 my $st = 0;
4809 1 eval { $st = _run_lines($class, [$head], $opts_ref); 1 };
4810 1 my $err = $@;
4811  
4812 1 ▶ if (defined $out_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out_file) 1 0 1
4813 1 close(STDOUT);
4814 1 ▶ if ($sv_out) { open(STDOUT, '>&_CTLRD_SAVE_OUT');
Branch: 1 branch
BranchTrueFalseTotal
if ($sv_out) 1 0 1
4815 1 close(_CTLRD_SAVE_OUT) }
4816 1 select(STDOUT); $| = 1;
4817 }
4818 1 ▶ if ($sv_err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sv_err) 0 1 1
4819 0 close(STDERR);
4820 0 open(STDERR, '>&_CTLRD_SAVE_ERR');
4821 0 close(_CTLRD_SAVE_ERR);
4822 }
4823 1 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 1 1
4824  
4825 1 $LAST_STATUS = $st;
4826 1 return $st;
4827 }
4828  
4829 # _run_inline_control_pipe: run "CONTROL-STRUCTURE | REST" -- for example
4830 # "for i in 1 2; do echo $i; done | sort". This interpreter never forks, so
4831 # the two halves are joined through a temp file, the same way an ordinary
4832 # pipeline is: the structure runs with STDOUT pointed at the file, then REST
4833 # runs with STDIN taken from it. Returns REST's exit status, as a pipeline
4834 # does. STDOUT is restored even when the structure dies.
4835 2 ▶sub _run_inline_control_pipe {
Subroutine _run_inline_control_pipe: called CC=7 SCAR=22.0
4836 2 my ($class, $head, $tail, $opts_ref) = @_;
4837  
4838 2 my $tmp = _hd_tempfile('');
4839 0 ▶ if (!defined $tmp) { $LAST_STATUS = 2; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tmp) 0 2 2
4840  
4841 2 my $saved = 0;
4842 2 ▶ if (open(_CTLPIPE_SAVE, '>&STDOUT')) { $saved = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if (open _CTLPIPE_SAVE, ">&STDOUT") 2 0 2
4843 2 ▶ if (!open(STDOUT, ">$tmp")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open STDOUT, ">$tmp") 0 2 2
4844 0 ▶ close(_CTLPIPE_SAVE) if $saved;
Branch: 1 branch
BranchTrueFalseTotal
if $saved 0 0 0
4845 0 unlink $tmp;
4846 0 @_HD_TMPFILES = grep { $_ ne $tmp } @_HD_TMPFILES;
4847 0 warn "sh: cannot create pipe temporary file\n";
4848 0 $LAST_STATUS = 2;
4849 0 return 2;
4850 }
4851  
4852 2 eval { _run_lines($class, [$head], $opts_ref); 1 };
4853 2 my $err = $@;
4854  
4855 2 close(STDOUT);
4856 2 ▶ if ($saved) { open(STDOUT, '>&_CTLPIPE_SAVE'); close(_CTLPIPE_SAVE) }
Branch: 1 branch
BranchTrueFalseTotal
if ($saved) 2 0 2
4857 2 select(STDOUT); $| = 1;
4858 2 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 2 2
4859  
4860 2 my @redir = ( [0, 0, $tmp] ); # fd=0 (stdin), append=0, source=tmp
4861 2 my $rc = _sh_exec_with_redirs($class, $tail, \@redir, $opts_ref);
4862  
4863 2 unlink $tmp;
4864 2 @_HD_TMPFILES = grep { $_ ne $tmp } @_HD_TMPFILES;
4865  
4866 2 $LAST_STATUS = $rc;
4867 2 return $rc;
4868 }
4869  
4870 # _split_inline_control_tail: for a physical line whose first word opens a
4871 # control structure, return (HEAD, OP, TAIL) when the structure is written
4872 # entirely on this line and is followed by more commands -- e.g.
4873 #
4874 # if true; then echo A; fi; echo B -> ("if ... fi", ";", "echo B")
4875 # while true; do break; done && echo x -> ("while ... done", "&&", "echo x")
4876 #
4877 # and the empty list when there is no such tail. _split_sh_compound() does
4878 # the actual scanning: it tracks control-structure depth, so a ';' inside an
4879 # unfinished compound is not a separator and the line is only split once the
4880 # structure has closed. Before 0.11 nothing peeled this tail off, and every
4881 # command written after the closing 'fi'/'done'/'esac' on the same line was
4882 # silently discarded (the block collector then went looking for a terminator
4883 # on the FOLLOWING lines and swallowed them too).
4884 205 ▶sub _split_inline_control_tail {
Subroutine _split_inline_control_tail: called CC=9 SCAR=22.2
4885 205 my ($line) = @_;
4886 205 my @parts = _split_sh_compound($line, 1);
4887 205 ▶ return () if scalar(@parts) <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @parts <= 1 191 14 205
4888  
4889 14 my $head = '';
4890 14 my $k = 0;
4891 14 ▶ while ($k <= $#parts && $parts[$k]->{op} eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $#parts and $parts[$k]{"op"} eq "") 14 14 28
Condition66%2/3
$k <= $#parts and $parts[$k]{"op"} eq ""
!ll&&!rl&&r
01414
MC/DC50%1/2
$k <= $#parts and $parts[$k]{"op"} eq ""
$k <= $#parts $parts[$k]{"op"} eq ""
Truth table66%2/3
A and B
A= $k <= $#partsB= $parts[$k]{"op"} eq ""
ABresult
0X0
100
111
4892 14 $head .= $parts[$k]->{cmd};
4893 14 $k++;
4894 }
4895 14 ▶ return () if $k > $#parts;
Branch: 1 branch
BranchTrueFalseTotal
if $k > $#parts 0 14 14
4896  
4897 14 my $op = $parts[$k]->{op};
4898 14 $k++;
4899  
4900 14 my $tail = '';
4901 14 for my $j ($k .. $#parts) {
4902 16 ▶ $tail .= ' ' . $parts[$j]->{op} . ' ' if $parts[$j]->{op} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $parts[$j]{"op"} ne "" 1 15 16
4903 16 $tail .= $parts[$j]->{cmd};
4904 }
4905  
4906 14 $head =~ s/\A\s+//; $head =~ s/\s+\z//;
4907 14 $tail =~ s/\A\s+//; $tail =~ s/\s+\z//;
4908 14 ▶ return () if $head eq '' || $tail eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $head eq "" or $tail eq "" 0 14 14
Condition33%1/3
$head eq "" or $tail eq ""
l!l&&r!l&&!r
0014
MC/DC0%0/2
$head eq "" or $tail eq ""
$head eq "" $tail eq ""
Truth table33%1/3
A or B
A= $head eq ""B= $tail eq ""
ABresult
1X1
011
000
4909 14 return ($head, $op, $tail);
4910 }
4911  
4912 # _split_inline_brace_tail: for a physical line that opens a brace group
4913 # (a one-line function body) or a parenthesised subshell group, return
4914 # (HEAD, OP, TAIL) when the group closes on this line and is followed by
4915 # ';', '&&' or '||' and more commands:
4916 #
4917 # f(){ echo F; }; echo G -> ("f(){ echo F; }", ";", "echo G")
4918 # ( echo A ) && echo B -> ("( echo A )", "&&", "echo B")
4919 #
4920 # Returns the empty list otherwise -- in particular when the group is
4921 # followed by a redirection or a pipe, which belong to the group itself.
4922 # _split_sh_compound() cannot be used here because it does not track brace
4923 # or parenthesis nesting, so it would split on a ';' inside the body.
4924 40 ▶sub _split_inline_brace_tail {
Subroutine _split_inline_brace_tail: called CC=19 SCAR=31.3
4925 40 my ($line, $open, $close) = @_;
4926 40 ▶ return () unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 40 40
4927  
4928 40 my @c = split //, $line;
4929 40 my $n = scalar @c;
4930 40 my $sq = 0;
4931 40 my $dq = 0;
4932 40 my $esc = 0;
4933 40 my $dep = 0;
4934 40 my $seen = 0; # the group has been opened at least once
4935 40 my $end; # offset just past the matching closer
4936  
4937 40 ▶ for (my $p = 0; $p < $n; $p++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p < $n) 812 13 825
4938 812 my $ch = $c[$p];
4939 0 ▶ if ($esc) { $esc = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($esc) 0 812 812
4940 0 ▶ if ($ch eq '\\' && !$sq) { $esc = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\" and not $sq) 0 812 812
Condition33%1/3
$ch eq "\\" and not $sq
!ll&&!rl&&r
81200
MC/DC0%0/2
$ch eq "\\" and not $sq
$ch eq "\\" not $sq
Truth table33%1/3
A and not(B)
A= $ch eq "\\"B= not $sq
ABresult
0X0
100
111
4941 0 ▶ if ($sq) { $sq = 0 if $ch eq "'"; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($sq) 0 812 812
if $ch eq "'" 0 0 0
4942 812 ▶ if ($dq) { $dq = 0 if $ch eq '"'; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($dq) 149 663 812
if $ch eq "\"" 10 139 149
4943 0 ▶ if ($ch eq "'") { $sq = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'") 0 663 663
4944 663 ▶ if ($ch eq '"') { $dq = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"") 10 653 663
4945 653 ▶ if ($ch eq $open) { $dep++; $seen = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq $open) 41 612 653
4946 612 ▶ if ($ch eq $close) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq $close) 28 584 612
4947 28 $dep--;
4948 28 ▶ if ($seen && $dep <= 0) { $end = $p + 1; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($seen and $dep <= 0) 27 1 28
Condition66%2/3
$seen and $dep <= 0
!ll&&!rl&&r
0127
MC/DC50%1/2
$seen and $dep <= 0
$seen $dep <= 0
Truth table66%2/3
A and B
A= $seenB= $dep <= 0
ABresult
0X0
100
111
4949 1 next;
4950 }
4951 }
4952 40 ▶ return () unless defined $end;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $end 13 27 40
4953  
4954 27 my $head = substr($line, 0, $end);
4955 27 my $rest = substr($line, $end);
4956 27 ▶ return () unless $rest =~ /\A\s*(;|&&|\|\|)\s*(\S.*)\z/s;
Branch: 1 branch
BranchTrueFalseTotal
unless $rest =~ /\A\s*(;|&&|\|\|)\s*(\S.*)\z/s 22 5 27
4957 5 my ($op, $tail) = ($1, $2);
4958 5 $tail =~ s/\s+\z//;
4959 5 ▶ return () if $tail eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $tail eq "" 0 5 5
4960 5 return ($head, $op, $tail);
4961 }
4962  
4963 # ----------------------------------------------------------------
4964 1942 ▶sub _split_sh_compound {
Subroutine _split_sh_compound: called CC=67 SCAR=42.1
4965 # $want_pipe is opt-in: when true, a single '|' that appears AFTER a
4966 # control structure has closed is reported as an operator, so that
4967 # _run_lines() can run "for ...; done | sort" as a pipeline instead of
4968 # hunting for a 'done' on the following lines. Every other caller
4969 # leaves it false and sees the previous behaviour exactly.
4970 1942 my ($line, $want_pipe) = @_;
4971 1942 my @parts;
4972 1942 my $cur = '';
4973 1942 my $in_sq = 0;
4974 1942 my $in_dq = 0;
4975 1942 my $depth = 0; # $( / <( / >( nesting
4976 1942 my $ctl = 0; # control-structure nesting: if/for/while/until/case/
4977 # select ... fi/done/esac. While >0, a ';' / && / ||
4978 # belongs to the compound command and is NOT a
4979 # top-level separator (so cmd | while x; do y; done
4980 # and a && for i in ..; do ..; done stay intact).
4981 1942 my $cmdpos = 1; # true when the next word is in command position
4982 1942 my $ctl_closed = 0; # a control structure has opened and closed again
4983 1942 my @chars = split //, $line;
4984 1942 my $n = scalar @chars;
4985 1942 my $i = 0;
4986  
4987 1942 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 28994 1942 30936
4988 28994 my $ch = $chars[$i];
4989  
4990 # Single-quote region
4991 28994 ▶ if ($in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq) 658 28336 28994
4992 658 ▶ if ($ch eq "'") { $in_sq = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'") 72 586 658
4993 658 $cur .= $ch; $i++; next;
4994 }
4995 28336 ▶ 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) 72 28264 28336
Condition100%3/3
$ch eq "'" and not $in_dq
!ll&&!rl&&r
28257772
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
4996  
4997 # Double-quote toggle
4998 28264 ▶ 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) 1447 26817 28264
Condition66%2/3
$ch eq "\"" and not $in_sq
!ll&&!rl&&r
2681701447
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
4999  
5000 # $( nesting inside double-quotes
5001 26817 ▶ if ($in_dq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_dq) 9271 17546 26817
5002 9271 ▶ 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 '(') { } 9 9262 9271
elsif ($ch eq ')' and $depth > 0) { } 10 18461 18471
Condition83%5/6
$ch eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "("
!ll&&!rl&&r
88234399
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
88230448
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
5003 10 ▶ elsif ($ch eq ')' && $depth > 0) { $depth-- }
Condition100%3/3
$ch eq ")" and $depth > 0
!ll&&!rl&&r
92094310
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
5004 9271 $cur .= $ch; $i++; next;
5005 }
5006  
5007 # Track $( nesting outside quotes
5008 17546 ▶ 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 "(") 174 17372 17546
Condition83%5/6
$ch eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "("
!ll&&!rl&&r
16935437174
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
169350611
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
5009 174 $depth++; $cur .= $ch; $i++; $cmdpos = 0; next;
5010 }
5011  
5012 # Track <( / >( process-substitution nesting outside quotes
5013 17372 ▶ 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 17370 17372
Condition88%8/9
$ch eq "<" || $ch eq ">" and $i + 1 < $n and $chars[$i + 1] eq "("
!ll&&!rl&&r
17322482
$ch eq "<" || $ch eq ">" and $i + 1 < $n
!ll&&!rl&&r
17322050
$ch eq "<" or $ch eq ">"
l!l&&r!l&&!r
222817322
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
5014 2 $depth++; $cur .= $ch; $i++; $cmdpos = 0; next;
5015 }
5016 17370 ▶ if ($ch eq ')' && $depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ")" and $depth > 0) 175 17195 17370
Condition100%3/3
$ch eq ")" and $depth > 0
!ll&&!rl&&r
17019176175
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
5017 175 $depth--; $cur .= $ch; $i++; $cmdpos = 0; next;
5018 }
5019  
5020 # Inside $(...) don't split on operators or track control words
5021 17195 ▶ if ($depth > 0) { $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($depth > 0) 1686 15509 17195
5022  
5023 # Backslash escape
5024 15509 ▶ if ($ch eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 4 15505 15509
5025 4 $cur .= $ch; $i++;
5026 4 ▶ $cur .= $chars[$i] if $i < $n; $i++; $cmdpos = 0; next;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $n 4 0 4
5027 }
5028  
5029 # && operator
5030 15505 ▶ 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 "&") 14 15491 15505
Condition83%5/6
$ch eq "&" and $i + 1 < $n and $chars[$i + 1] eq "&"
!ll&&!rl&&r
15485614
$ch eq "&" and $i + 1 < $n
!ll&&!rl&&r
15485020
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
5031 14 ▶ if ($ctl == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctl == 0) { } 14 0 14
5032 14 push @parts, { op => '', cmd => $cur };
5033 14 push @parts, { op => '&&', cmd => '' };
5034 14 $cur = '';
5035 }
5036 0 else { $cur .= '&&' }
5037 14 $i += 2; $cmdpos = 1; next;
5038 }
5039  
5040 # || operator
5041 15491 ▶ 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 "|") 7 15484 15491
Condition83%5/6
$ch eq "|" and $i + 1 < $n and $chars[$i + 1] eq "|"
!ll&&!rl&&r
15451337
$ch eq "|" and $i + 1 < $n
!ll&&!rl&&r
15451040
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
5042 7 ▶ if ($ctl == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctl == 0) { } 7 0 7
5043 7 push @parts, { op => '', cmd => $cur };
5044 7 push @parts, { op => '||', cmd => '' };
5045 7 $cur = '';
5046 }
5047 0 else { $cur .= '||' }
5048 7 $i += 2; $cmdpos = 1; next;
5049 }
5050  
5051 # ; separator (not inside any quote or subst)
5052 15484 ▶ if ($ch eq ';') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ";") 360 15124 15484
5053 360 ▶ if ($ctl == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctl == 0) { } 60 300 360
5054 60 push @parts, { op => '', cmd => $cur };
5055 60 push @parts, { op => ';', cmd => '' };
5056 60 $cur = '';
5057 }
5058 300 else { $cur .= ';' }
5059 360 $i++; $cmdpos = 1; next;
5060 }
5061  
5062 # Single | (pipe): not a compound separator here, but the word that
5063 # follows it is in command position (so x | while ... is tracked).
5064 15124 ▶ if ($ch eq '|') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "|") 33 15091 15124
5065 33 ▶ if ($want_pipe && $ctl == 0 && $ctl_closed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_pipe and $ctl == 0 and $ctl_closed) 2 31 33
Condition66%4/6
$want_pipe and $ctl == 0 and $ctl_closed
!ll&&!rl&&r
3102
$want_pipe and $ctl == 0
!ll&&!rl&&r
3102
MC/DC33%1/3
$want_pipe and $ctl == 0 and $ctl_closed
$want_pipe $ctl == 0 $ctl_closed
Truth table50%2/4
A and B and C
A= $want_pipeB= $ctl == 0C= $ctl_closed
ABCresult
0XX0
10X0
1100
1111
5066 2 push @parts, { op => '', cmd => $cur };
5067 2 push @parts, { op => '|', cmd => '' };
5068 2 $cur = ''; $i++; $cmdpos = 1; next;
5069 }
5070 31 $cur .= $ch; $i++; $cmdpos = 1; next;
5071 }
5072  
5073 # '(' opens a group; the next word is in command position.
5074 15091 ▶ if ($ch eq '(') { $cur .= $ch; $i++; $cmdpos = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "(") 32 15059 15091
5075  
5076 # Whitespace: preserved; does not itself change command position.
5077 15059 ▶ if ($ch =~ /\s/) { $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch =~ /\s/) 3912 11147 15059
5078  
5079 # A word starting in command position may be a control keyword.
5080 11147 ▶ if ($cmdpos && $ch =~ /[A-Za-z]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmdpos and $ch =~ /[A-Za-z]/) 2399 8748 11147
Condition100%3/3
$cmdpos and $ch =~ /[A-Za-z]/
!ll&&!rl&&r
8657912399
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
5081 2399 my $j = $i;
5082 2399 my $w = '';
5083 2399 ▶ 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]/) 8975 2399 11374
Condition100%3/3
$j < $n and $chars[$j] =~ /[A-Za-z]/
!ll&&!rl&&r
23621638975
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
5084 2399 ▶ my $after = ($j < $n) ? $chars[$j] : '';
Branch: 1 branch
BranchTrueFalseTotal
$j < $n ? : 2163 236 2399
5085 # A full word only if the next char terminates it.
5086 2399 ▶ if ($after eq '' || $after =~ /[\s;&|)]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($after eq "" or $after =~ /[\s;&|)]/) 2040 359 2399
Condition100%3/3
$after eq "" or $after =~ /[\s;&|)]/
l!l&&r!l&&!r
2361804359
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
5087 2040 my $lw = lc $w;
5088 2040 ▶ 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') { } 219 1821 2040
elsif ($lw eq 'fi' or $lw eq 'done' or $lw eq 'esac') { } 115 1706 1821
Condition100%15/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
21631821
$lw eq "if" or $lw eq "for" or $lw eq "while" or $lw eq "until" or $lw eq "case"
l!l&&r!l&&!r
162541824
$lw eq "if" or $lw eq "for" or $lw eq "while" or $lw eq "until"
l!l&&r!l&&!r
15931878
$lw eq "if" or $lw eq "for" or $lw eq "while"
l!l&&r!l&&!r
109501881
$lw eq "if" or $lw eq "for"
l!l&&r!l&&!r
61481931
MC/DC100%6/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 table100%7/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
5089 || $lw eq 'until'|| $lw eq 'case' || $lw eq 'select') {
5090 219 $ctl++;
5091 }
5092 ▶ elsif ($lw eq 'fi' || $lw eq 'done' || $lw eq 'esac') {
Condition100%6/6
$lw eq "fi" or $lw eq "done" or $lw eq "esac"
l!l&&r!l&&!r
11141706
$lw eq "fi" or $lw eq "done"
l!l&&r!l&&!r
39721710
MC/DC100%3/3
$lw eq "fi" or $lw eq "done" or $lw eq "esac"
$lw eq "fi" $lw eq "done" $lw eq "esac"
Truth table100%4/4
A or B or C
A= $lw eq "fi"B= $lw eq "done"C= $lw eq "esac"
ABCresult
1XX1
01X1
0011
0000
5093 115 ▶ if ($ctl > 0) { $ctl--; $ctl_closed = 1 if $ctl == 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($ctl > 0) 115 0 115
if $ctl == 0 113 2 115
5094 }
5095 2040 $cur .= $w;
5096 2040 $i = $j;
5097 # do/then/else/elif introduce a fresh command position.
5098 2040 ▶ $cmdpos = ($lw eq 'do' || $lw eq 'then'
Branch: 1 branch
BranchTrueFalseTotal
$lw eq 'do' || $lw eq 'then' || $lw eq 'else' || $lw eq 'elif' ? : 167 1873 2040
Condition88%8/9
$lw eq "do" or $lw eq "then" or $lw eq "else" or $lw eq "elif"
l!l&&r!l&&!r
16701873
$lw eq "do" or $lw eq "then" or $lw eq "else"
l!l&&r!l&&!r
16431873
$lw eq "do" or $lw eq "then"
l!l&&r!l&&!r
104601876
MC/DC75%3/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 table80%4/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
5099 || $lw eq 'else' || $lw eq 'elif') ? 1 : 0;
5100 2040 next;
5101 }
5102 }
5103  
5104 # Ordinary character (part of a word / argument).
5105 9107 $cur .= $ch; $i++; $cmdpos = 0;
5106 }
5107 1942 push @parts, { op => '', cmd => $cur };
5108  
5109 # If only one cmd part with no operators, return single element
5110 1942 my $has_op = 0;
5111 1942 ▶ for my $p (@parts) { $has_op = 1 if $p->{op} ne '' }
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"op"} ne "" 83 2025 2108
5112 1942 ▶ return @parts if $has_op;
Branch: 1 branch
BranchTrueFalseTotal
if $has_op 75 1867 1942
5113 1867 return ({ op => '', cmd => $line });
5114 }
5115  
5116 # ----------------------------------------------------------------
5117 # _exec_sh_compound: execute && / || / ; compound SH commands
5118 # ----------------------------------------------------------------
5119 61 ▶sub _exec_sh_compound {
Subroutine _exec_sh_compound: called CC=24 SCAR=31.9
5120 61 my ($class, $parts, $opts_ref) = @_;
5121 61 my $pending_op = '';
5122 61 my $rc = 0;
5123  
5124 # set -e semantics for lists: a command is EXEMPT when a && or ||
5125 # operator appears anywhere AFTER it in the list (bash: every command
5126 # of a && / || list except the one following the final && or ||).
5127 # Commands separated only by ; are ordinary statements and DO trigger.
5128 # Adjudication happens here, per member, so _run_lines must not check
5129 # the list's overall status again: signal that via $_ERREXIT_DONE.
5130 61 my $ncmd = 0;
5131 61 ▶ for my $part (@{$parts}) { $ncmd++ if $part->{op} eq '' }
Branch: 1 branch
BranchTrueFalseTotal
if $part->{"op"} eq "" 129 68 197
5132 61 ▶ $_ERREXIT_DONE = 1 if $ncmd > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $ncmd > 1 61 0 61
5133  
5134 61 my $seen_cmds = 0;
5135 61 for my $k (0 .. $#{$parts}) {
5136 196 my $part = $parts->[$k];
5137 196 my $op = $part->{op};
5138 196 my $cmd = $part->{cmd};
5139 196 $cmd =~ s/\A\s+//; $cmd =~ s/\s+\z//;
5140  
5141 # A prior member executed exit / errexit / break / return: stop.
5142 196 ▶ last if defined $_EXIT_CODE || $_BREAK || $_RETURN;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_EXIT_CODE or $_BREAK or $_RETURN 1 195 196
Condition66%4/6
defined $_EXIT_CODE or $_BREAK or $_RETURN
l!l&&r!l&&!r
10195
defined $_EXIT_CODE or $_BREAK
l!l&&r!l&&!r
10195
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
5143  
5144 195 ▶ if ($op eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq '') { } 128 67 195
5145 128 $seen_cmds++;
5146 # Exempt from set -e when a && or || follows later in the list
5147 128 my $exempt = 0;
5148 128 for my $j ($k + 1 .. $#{$parts}) {
5149 130 my $later = $parts->[$j]{op};
5150 130 ▶ if ($later eq '&&' || $later eq '||') { $exempt = 1; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($later eq "&&" or $later eq "||") 18 112 130
Condition100%3/3
$later eq "&&" or $later eq "||"
l!l&&r!l&&!r
126112
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
5151 }
5152 128 my $ran = 0;
5153 # Execute according to pending operator
5154 128 ▶ if ($pending_op eq '') {
Branch: 4 branches
BranchTrueFalseTotal
if ($pending_op eq '') { } 61 67 128
elsif ($pending_op eq '&&') { } 12 55 67
elsif ($pending_op eq '||') { } 6 49 55
elsif ($pending_op eq ';') { } 49 0 49
5155 61 ▶ if ($cmd =~ /\S/) { $ran = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /\S/) 61 0 61
5156 }
5157 elsif ($pending_op eq '&&') {
5158 12 ▶ if ($LAST_STATUS == 0 && $cmd =~ /\S/) { $ran = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($LAST_STATUS == 0 and $cmd =~ /\S/) 8 4 12
Condition66%2/3
$LAST_STATUS == 0 and $cmd =~ /\S/
!ll&&!rl&&r
408
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
5159 }
5160 elsif ($pending_op eq '||') {
5161 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
5162 }
5163 elsif ($pending_op eq ';') {
5164 49 ▶ if ($cmd =~ /\S/) { $ran = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /\S/) 49 0 49
5165 }
5166 128 ▶ if ($ran) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ran) 121 7 128
5167 121 ▶ if ($exempt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exempt) { } 17 104 121
5168 17 $_ERREXIT_HOLD++;
5169 17 $rc = _exec_line($class, $cmd, $opts_ref);
5170 17 $_ERREXIT_HOLD--;
5171 }
5172 else {
5173 104 $rc = _exec_line($class, $cmd, $opts_ref);
5174 104 _errexit_check($rc);
5175 }
5176 }
5177 128 $pending_op = '';
5178 }
5179 else {
5180 67 $pending_op = $op;
5181 }
5182 }
5183 61 return $rc;
5184 }
5185  
5186 # ----------------------------------------------------------------
5187 1676 ▶sub _split_sh_pipe {
Subroutine _split_sh_pipe: called CC=32 SCAR=34.8
5188 1676 my ($line) = @_;
5189 1676 my @segs;
5190 1676 my $cur = '';
5191 1676 my $in_sq = 0; # inside single quotes
5192 1676 my $in_dq = 0; # inside double quotes
5193 1676 my $depth = 0; # $( nesting depth
5194 1676 my @chars = split //, $line;
5195 1676 my $n = scalar @chars;
5196 1676 my $i = 0;
5197  
5198 1676 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 27122 1676 28798
5199 27122 my $ch = $chars[$i];
5200  
5201 # Single-quote region: nothing special until closing '
5202 27122 ▶ if ($in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq) 628 26494 27122
5203 628 ▶ if ($ch eq "'") { $in_sq = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'") 65 563 628
5204 628 $cur .= $ch; $i++; next;
5205 }
5206  
5207 # Toggle double-quote
5208 26494 ▶ if ($ch eq '"' && !$in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" and not $in_sq) 1164 25330 26494
Condition66%2/3
$ch eq "\"" and not $in_sq
!ll&&!rl&&r
2533001164
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
5209 1164 $in_dq = !$in_dq;
5210 1164 $cur .= $ch; $i++; next;
5211 }
5212  
5213 # Inside double-quotes only $( nesting matters
5214 25330 ▶ if ($in_dq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_dq) 8331 16999 25330
5215 8331 ▶ 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 "(") 9 8322 8331
Condition83%5/6
$ch eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "("
!ll&&!rl&&r
79813419
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
79810350
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
5216 9 $depth++; $cur .= $ch; $i++; next;
5217 }
5218 8322 ▶ if ($ch eq ')' && $depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ")" and $depth > 0) 10 8312 8322
Condition100%3/3
$ch eq ")" and $depth > 0
!ll&&!rl&&r
82714110
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
5219 10 $depth--; $cur .= $ch; $i++; next;
5220 }
5221 # backslash escape inside "
5222 8312 ▶ if ($ch eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 23 8289 8312
5223 23 $cur .= $ch; $i++;
5224 23 ▶ $cur .= $chars[$i] if $i < $n; $i++; next;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $n 23 0 23
5225 }
5226 8289 $cur .= $ch; $i++; next;
5227 }
5228  
5229 # Enter single-quote
5230 16999 ▶ if ($ch eq "'") { $in_sq = 1; $cur .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'") 65 16934 16999
5231  
5232 # $( command substitution: consume BOTH characters and bump the
5233 # nesting depth exactly ONCE here, so the standalone-'(' handler
5234 # below does not double-count the '(' of a $( and leave depth
5235 # stuck at 1 after the matching ')'. (That stale depth made a
5236 # bare '|' following a nested $(...) fail to split as a pipe.)
5237 16934 ▶ 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 "(") 158 16776 16934
Condition83%5/6
$ch eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "("
!ll&&!rl&&r
16477299158
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
164770457
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
5238 158 $depth++; $cur .= '$('; $i += 2; next;
5239 }
5240  
5241 # <( / >( process substitution (v0.07): same "consume both
5242 # characters, bump depth exactly once" treatment as $( above, so
5243 # a '|' inside its body is not mistaken for the outer pipeline's
5244 # separator.
5245 16776 ▶ 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 16774 16776
Condition88%8/9
$ch eq "<" || $ch eq ">" and $i + 1 < $n and $chars[$i + 1] eq "("
!ll&&!rl&&r
16728462
$ch eq "<" || $ch eq ">" and $i + 1 < $n
!ll&&!rl&&r
16728048
$ch eq "<" or $ch eq ">"
l!l&&r!l&&!r
212716728
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
5246 2 $depth++; $cur .= $ch . '('; $i += 2; next;
5247 }
5248 16774 ▶ if ($ch eq '(' ) { $depth++ if $depth > 0; $cur .= $ch; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($ch eq "(") 156 16618 16774
if $depth > 0 125 31 156
5249 16618 ▶ if ($ch eq ')' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ")") 316 16302 16618
5250 316 ▶ if ($depth > 0) { $depth-- }
Branch: 1 branch
BranchTrueFalseTotal
if ($depth > 0) 284 32 316
5251 316 $cur .= $ch; $i++; next;
5252 }
5253  
5254 # Bare | outside any quote/subst => pipeline separator
5255 16302 ▶ if ($ch eq '|' && $depth == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "|" and $depth == 0) 28 16274 16302
Condition100%3/3
$ch eq "|" and $depth == 0
!ll&&!rl&&r
162561828
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
5256 # Peek: || is logical-or, not a pipe
5257 28 ▶ if ($i+1 < $n && $chars[$i+1] eq '|') {
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 1 < $n and $chars[$i + 1] eq "|") 0 28 28
Condition33%1/3
$i + 1 < $n and $chars[$i + 1] eq "|"
!ll&&!rl&&r
0280
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
5258 0 $cur .= '||'; $i += 2; next;
5259 }
5260 28 push @segs, $cur;
5261 28 $cur = '';
5262 28 $i++; next;
5263 }
5264  
5265 # Backslash escape (outside quotes)
5266 16274 ▶ if ($ch eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 3 16271 16274
5267 3 $cur .= $ch; $i++;
5268 3 ▶ $cur .= $chars[$i] if $i < $n; $i++; next;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $n 3 0 3
5269 }
5270  
5271 16271 $cur .= $ch; $i++;
5272 }
5273 1676 push @segs, $cur;
5274 1676 return @segs;
5275 }
5276  
5277 # ----------------------------------------------------------------
5278 # _seg_is_control: true when a pipeline segment is a compound/control
5279 # construct that must be run through _run_lines (the block dispatcher)
5280 # rather than _exec_line (which would try to exec "while"/"for"/... as an
5281 # external command). Recognises the command-position control keywords and
5282 # a "( subshell )" group. Enables the common cmd | while read ... ; done
5283 # and cmd | for x in ...; done idioms. Pure Perl 5.005_03.
5284 1650 ▶sub _seg_is_control {
Subroutine _seg_is_control: called CC=11 SCAR=25.7
5285 1650 my ($seg) = @_;
5286 1650 ▶ my $s = defined($seg) ? $seg : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $seg ? : 1650 0 1650
5287 1650 $s =~ s/\A\s+//;
5288 1650 my ($w) = ($s =~ /\A(\S+)/);
5289 1650 ▶ return 0 unless defined $w;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $w 0 1650 1650
5290 1650 $w = lc $w;
5291 1650 ▶ 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" 5 1645 1650
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
501645
$w eq "while" or $w eq "until" or $w eq "for" or $w eq "if" or $w eq "case"
l!l&&r!l&&!r
501645
$w eq "while" or $w eq "until" or $w eq "for" or $w eq "if"
l!l&&r!l&&!r
501645
$w eq "while" or $w eq "until" or $w eq "for"
l!l&&r!l&&!r
321645
$w eq "while" or $w eq "until"
l!l&&r!l&&!r
301647
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
5292 || $w eq 'if' || $w eq 'case' || $w eq 'select';
5293 1645 ▶ return 1 if $s =~ /\A\(/ && $s !~ /\A\(\(/; # ( subshell ) group
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /\A\(/ and not $s =~ /\A\(\(/ 0 1645 1645
Condition33%1/3
$s =~ /\A\(/ and not $s =~ /\A\(\(/
!ll&&!rl&&r
164500
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
5294 1645 return 0;
5295 }
5296  
5297 # _exec_sh_pipe: run a SH pipeline via temporary files.
5298 # Each segment's stdout feeds the next segment's stdin.
5299 # Perl 5.005_03 compatible: bareword FHs, 2-arg open.
5300 # ----------------------------------------------------------------
5301 26 ▶sub _exec_sh_pipe {
Subroutine _exec_sh_pipe: called CC=20 SCAR=38.6
5302 26 my ($class, $segs_ref, $opts_ref) = @_;
5303 26 my @segs = @{$segs_ref};
5304 26 my $n_segs = scalar @segs;
5305 # Tag the stage files with the active pipeline-nesting depth so a nested
5306 # pipeline (reached when a segment contains a $(...) that is itself a
5307 # pipeline) gets distinct stage files and does not clobber this one.
5308 26 local $_PIPE_DEPTH = $_PIPE_DEPTH + 1;
5309 26 my $base = File::Spec->catfile(File::Spec->tmpdir(),
5310 'batsh_shp_' . $$ . '_' . $_PIPE_DEPTH);
5311 # Localize the dup/stage filehandle globs so that a nested pipeline
5312 # (a segment whose $(...) body is itself a pipeline) does not overwrite
5313 # this pipeline's saved STDOUT/STDIN handles. Perl 5.005_03 compatible.
5314 26 local (*_SH_PIPE_RFH, *_SH_PIPE_WFH, *_SH_PIPE_SAVIN, *_SH_PIPE_SAVOUT);
5315 26 my $rc = 0;
5316 26 my $input_f = undef; # tmpfile that feeds this segment's STDIN
5317  
5318 26 for my $idx (0 .. $n_segs - 1) {
5319 54 my $seg = $segs[$idx];
5320 54 $seg =~ s/\A\s+//; $seg =~ s/\s+\z//;
5321 54 ▶ next unless $seg =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $seg =~ /\S/ 0 54 54
5322  
5323 54 ▶ my $is_last = ($idx == $n_segs - 1) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$idx == $n_segs - 1 ? : 26 28 54
5324 54 my $output_f = undef;
5325  
5326 # --- redirect STDIN from previous segment's output ---
5327 54 my $saved_in = 0;
5328 54 ▶ if (defined $input_f && -f $input_f) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input_f and -f $input_f) 28 26 54
Condition66%2/3
defined $input_f and -f $input_f
!ll&&!rl&&r
26028
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
5329 open(_SH_PIPE_RFH, $input_f)
5330 0 ▶ 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 } 28 0 28
5331 open(_SH_PIPE_SAVIN, '<&STDIN')
5332 0 ▶ or do { close(_SH_PIPE_RFH); last };
Branch: 1 branch
BranchTrueFalseTotal
open _SH_PIPE_SAVIN, "<&STDIN" or do { close _SH_PIPE_RFH; last } 28 0 28
5333 open(STDIN, '<&_SH_PIPE_RFH')
5334 28 ▶ 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 } 28 0 28
5335 0 close(_SH_PIPE_RFH);
5336 0 open(STDIN, '<&_SH_PIPE_SAVIN'); close(_SH_PIPE_SAVIN);
5337 0 last;
5338 };
5339 28 close(_SH_PIPE_RFH);
5340 28 $saved_in = 1;
5341 }
5342  
5343 # --- redirect STDOUT to next segment's input file ---
5344 54 my $saved_out = 0;
5345 54 ▶ unless ($is_last) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_last) 28 26 54
5346 28 $output_f = _shp_tempfile("${base}_${idx}");
5347 28 ▶ if (!defined $output_f) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $output_f) 0 28 28
5348 0 ▶ if ($saved_in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_in) 0 0 0
5349 0 open(STDIN, '<&_SH_PIPE_SAVIN'); close(_SH_PIPE_SAVIN);
5350 }
5351 0 warn "SH pipe: cannot create stage temp file\n";
5352 0 last;
5353 }
5354 open(_SH_PIPE_SAVOUT, '>&STDOUT')
5355 28 ▶ 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 } 28 0 28
5356 0 close(_SH_PIPE_WFH);
5357 0 ▶ if ($saved_in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_in) 0 0 0
5358 0 open(STDIN, '<&_SH_PIPE_SAVIN'); close(_SH_PIPE_SAVIN);
5359 }
5360 0 last;
5361 };
5362 open(STDOUT, '>&_SH_PIPE_WFH')
5363 28 ▶ 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 } 28 0 28
5364 0 close(_SH_PIPE_WFH);
5365 0 open(STDOUT, '>&_SH_PIPE_SAVOUT'); close(_SH_PIPE_SAVOUT);
5366 0 ▶ if ($saved_in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_in) 0 0 0
5367 0 open(STDIN, '<&_SH_PIPE_SAVIN'); close(_SH_PIPE_SAVIN);
5368 }
5369 0 last;
5370 };
5371 28 close(_SH_PIPE_WFH);
5372 28 $saved_out = 1;
5373 }
5374  
5375 # --- execute the segment as a SH line ---
5376 # (_exec_line_impl routes a control/compound segment such as
5377 # "while read ...; do ...; done" through the block runner, so the
5378 # common cmd | while read ... idiom works.)
5379 54 $rc = _exec_line($class, $seg, $opts_ref);
5380  
5381 # --- restore STDOUT ---
5382 54 ▶ if ($saved_out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_out) 28 26 54
5383 28 open(STDOUT, '>&_SH_PIPE_SAVOUT');
5384 28 close(_SH_PIPE_SAVOUT);
5385 }
5386  
5387 # --- restore STDIN and remove input tmpfile ---
5388 54 ▶ if ($saved_in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_in) 28 26 54
5389 28 open(STDIN, '<&_SH_PIPE_SAVIN');
5390 28 close(_SH_PIPE_SAVIN);
5391 28 unlink $input_f;
5392 28 @_SHP_TMPFILES = grep { $_ ne $input_f } @_SHP_TMPFILES;
5393 }
5394  
5395 54 $input_f = $output_f;
5396 }
5397  
5398 26 ▶ if (defined $input_f && -f $input_f) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input_f and -f $input_f) 0 26 26
Condition33%1/3
defined $input_f and -f $input_f
!ll&&!rl&&r
2600
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
5399 0 unlink $input_f;
5400 0 @_SHP_TMPFILES = grep { $_ ne $input_f } @_SHP_TMPFILES;
5401 }
5402 26 return $rc;
5403 }
5404  
5405 # _shp_tempfile: create a unique, empty temp file for one pipeline stage's
5406 # stdout->stdin bridge, using sysopen(...O_CREAT|O_EXCL...) to avoid symlink
5407 # races (mirrors _bg_tempfile / _hd_tempfile / _subst_tempfile). Opens the
5408 # package bareword filehandle _SH_PIPE_WFH and returns the final path, or
5409 # undef on failure. $stub is the depth/index-tagged path prefix so stage
5410 # files stay distinguishable for debugging; a sequence number is appended
5411 # to make the final name unique and unpredictable.
5412 29 ▶sub _shp_tempfile {
Subroutine _shp_tempfile: called CC=3 SCAR=11.1
5413 29 my ($stub) = @_;
5414 29 my $attempt = 0;
5415 29 ▶ while ($attempt < 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attempt < 1000) 30 0 30
5416 30 $_PIPE_SEQ++;
5417 30 $attempt++;
5418 30 my $path = $stub . '_' . $_PIPE_SEQ . '.tmp';
5419 30 ▶ 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) 29 1 30
5420 29 push @_SHP_TMPFILES, $path;
5421 29 return $path;
5422 }
5423 # EEXIST or transient error: retry with next sequence number
5424 }
5425 0 return undef;
5426 }
5427  
5428 # ----------------------------------------------------------------
5429 # Pattern helpers for ${var%pat}, ${var#pat}, ${var/pat/rep}
5430 # Converts glob-style pattern to Perl regex (*, ?, [abc]).
5431 # ----------------------------------------------------------------
5432 # ----------------------------------------------------------------
5433 # _glob_expand: expand a single word that contains unquoted glob
5434 # metacharacters (* ? [...]) into a sorted list of matching pathnames.
5435 # Returns the original word unchanged if no matches are found (POSIX
5436 # "nullglob off" behaviour, which is the shell default).
5437 # Only words that were NOT wrapped in quotes are eligible; the caller
5438 # is responsible for passing only unquoted words.
5439 # ----------------------------------------------------------------
5440 46 ▶sub _glob_expand {
Subroutine _glob_expand: called CC=3 SCAR=11.0
5441 46 my ($word) = @_;
5442 # Fast path: no metacharacters
5443 46 ▶ return ($word) unless $word =~ /[*?\[]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $word =~ /[*?\[]/ 0 46 46
5444 # The matcher has to see the real pathname, so a backslash still held
5445 # behind its expansion-result sentinel (a Windows path that came out
5446 # of a variable) is restored first. What comes back is data again and
5447 # is re-protected, so the quote-removal stage downstream cannot eat
5448 # the separators of the very names it just matched.
5449 46 my $pat = _unprotect_lit($word);
5450 46 my @matches = map { _protect_lit(BATsh::MB::enc($_)) }
5451 _glob_paths(BATsh::MB::dec($pat));
5452 46 ▶ return @matches ? @matches : ($word);
Branch: 1 branch
BranchTrueFalseTotal
@matches ? : 14 32 46
5453 }
5454  
5455 # ----------------------------------------------------------------
5456 # _glob_paths: pathname expansion in pure Perl (v0.09)
5457 # ----------------------------------------------------------------
5458 # Perl's built-in glob() cannot serve a shell that has to run on Windows:
5459 # it reads a backslash as an ESCAPE character, so the perfectly ordinary
5460 # pattern C:\dir\*.txt is understood as C:dir*.txt and matches
5461 # nothing, and when a pattern matches nothing glob() hands it back with
5462 # those backslashes already deleted. File::Glob's GLOB_NOESCAPE would
5463 # avoid that, but File::Glob arrived in Perl 5.6 and BATsh supports
5464 # 5.005_03. glob() also offers no extglob support and no say over
5465 # dot-files. The pattern is therefore matched here, one path segment at
5466 # a time, with this module's own _glob_to_re() translator:
5467 #
5468 # * the pathname separator is "/" everywhere, and "\" as well on
5469 # Windows, where that is simply how a path is written. On Unix a
5470 # backslash is an ordinary character in a filename and stays one, so
5471 # a directory really named "a\b" keeps working;
5472 # * a segment holding no metacharacter is appended literally;
5473 # * a leading "." is matched only by a pattern that starts with "." --
5474 # the shell rule, which glob() follows too;
5475 # * every segment but the last has to name a directory;
5476 # * the separators the caller wrote come back unchanged, so a Windows
5477 # pattern yields Windows pathnames;
5478 # * a negated class "[!abc]" negates, and only names that exist are
5479 # returned, sorted, with case ignored on Windows.
5480 #
5481 # Returns the (possibly empty) list of matching pathnames. The caller
5482 # decides what an empty list means; POSIX leaves the word untouched.
5483 # Perl 5.005_03 safe: bareword directory handle, no File::Glob, no
5484 # File::Spec, and one directory level is read and closed at a time so the
5485 # single global handle is never nested.
5486 # ----------------------------------------------------------------
5487 48 ▶sub _glob_paths {
Subroutine _glob_paths: called CC=47 SCAR=43.1
5488 48 my ($pat) = @_;
5489 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
5490 48 ▶ my $win = ($^O =~ /MSWin32|dos|os2|cygwin/i) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /MSWin32|dos|os2|cygwin/i ? : 0 48 48
5491 48 ▶ my $sepclass = $win ? '[\\\\/]' : '[/]';
Branch: 1 branch
BranchTrueFalseTotal
$win ? : 0 48 48
5492 48 ▶ my $nonsep = $win ? '[^\\\\/]' : '[^/]';
Branch: 1 branch
BranchTrueFalseTotal
$win ? : 0 48 48
5493  
5494 # The leading root -- a Windows drive letter and/or separators -- is
5495 # never a pattern and is copied through as written.
5496 48 my $rest = $pat;
5497 48 my $prefix = '';
5498 0 ▶ 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
5499 48 ▶ if ($rest =~ s/\A($sepclass+)//) { $prefix .= $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/\A($sepclass+)//) 2 46 48
5500  
5501 # Split the remainder into [segment, following separators] pairs.
5502 48 my @parts = ();
5503 48 ▶ while ($rest ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest ne "") 77 48 125
5504 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
5505 47 elsif ($rest =~ s/\A($nonsep+)\z//) { push @parts, [$1, ''] }
5506 0 elsif ($rest =~ s/\A($sepclass+)//) { next }
5507 0 else { last }
5508 }
5509 48 ▶ return () unless @parts;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts 0 48 48
5510  
5511 48 my @cur = ($prefix);
5512 48 my $done = 0;
5513 48 my $total = scalar(@parts);
5514 48 for my $p (@parts) {
5515 77 my ($seg, $sep) = @{$p};
5516 77 $done++;
5517 77 ▶ my $last = ($done == $total) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$done == $total ? : 48 29 77
5518  
5519 # Is this segment a pattern at all? An unbalanced "[" is not:
5520 # treating it as a character class would let a stray bracket in
5521 # "echo [$dir]" match a one-character filename.
5522 77 my $is_pat = 0;
5523 77 ▶ $is_pat = 1 if $seg =~ /[*?]/;
Branch: 1 branch
BranchTrueFalseTotal
if $seg =~ /[*?]/ 20 57 77
5524 77 ▶ $is_pat = 1 if $seg =~ /\[[^\]]+\]/;
Branch: 1 branch
BranchTrueFalseTotal
if $seg =~ /\[[^\]]+\]/ 23 54 77
5525 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
5526  
5527 77 ▶ if (!$is_pat) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_pat) 35 42 77
5528 35 my @next = ();
5529 35 for my $c (@cur) { push @next, $c . $seg . $sep }
5530 35 @cur = @next;
5531 35 next;
5532 }
5533  
5534 42 my $re = _glob_to_re($seg, 1);
5535 42 ▶ $re = $win ? "(?i)\\A(?:$re)\\z" : "\\A(?:$re)\\z";
Branch: 1 branch
BranchTrueFalseTotal
$win ? : 0 42 42
5536 # A pattern that cannot be compiled (an empty or reversed
5537 # character class, say) is taken literally, as the shell does.
5538 # The warning handler keeps such a pattern from printing a Perl
5539 # diagnostic on the script's STDERR.
5540 42 my $re_bad = 0;
5541 {
5542 0 ▶ local $SIG{'__WARN__'} = sub { 1 };
Subroutine __ANON__: not called CC=1 SCAR=0.4
5543 42 eval { my $probe = 'x'; $probe =~ /$re/ };
5544 42 ▶ $re_bad = 1 if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 42 42
5545 }
5546 42 ▶ if ($re_bad) {
Branch: 1 branch
BranchTrueFalseTotal
if ($re_bad) 0 42 42
5547 0 my @lit = ();
5548 0 for my $c (@cur) { push @lit, $c . $seg . $sep }
5549 0 @cur = @lit;
5550 0 next;
5551 }
5552 42 ▶ my $want_dot = (index($seg, '.') == 0) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
index($seg, '.') == 0 ? : 1 41 42
5553 42 my @next = ();
5554 42 for my $c (@cur) {
5555 # opendir() gets the directory without its trailing separator:
5556 # Win32 turns "dir\\" into a "dir\\\\*" search pattern.
5557 45 my $dir = $c;
5558 45 ▶ if ($dir eq '') { $dir = '.' }
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq '') { } 27 18 45
5559 else {
5560 18 my $trim = $dir;
5561 18 $trim =~ s/$sepclass+\z//;
5562 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 table25%1/4
A or B and C
A= $trim eq ""B= $winC= $trim =~ /\A[A-Za-z]:\z/
ABCresult
1XX1
0111
00X0
0100
5563 || ($win && $trim =~ /\A[A-Za-z]:\z/);
5564 }
5565 45 my @names = ();
5566 45 ▶ if (opendir(_GLOB_DH, $dir)) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir _GLOB_DH, $dir) 43 2 45
5567 43 @names = readdir(_GLOB_DH);
5568 43 closedir(_GLOB_DH);
5569 }
5570 0 ▶ local $SIG{'__WARN__'} = sub { 1 };
Subroutine __ANON__: not called CC=1 SCAR=1.2
5571 45 for my $nm (sort @names) {
5572 800 ▶ next if !$want_dot && index($nm, '.') == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $want_dot and index($nm, ".") == 0 90 710 800
Condition100%3/3
not $want_dot and index($nm, ".") == 0
!ll&&!rl&&r
570590
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
5573 710 my $hit = 0;
5574 710 ▶ eval { $hit = ($nm =~ /$re/) ? 1 : 0 };
Branch: 1 branch
BranchTrueFalseTotal
$nm =~ /$re/ ? : 171 539 710
5575 710 ▶ next unless $hit;
Branch: 1 branch
BranchTrueFalseTotal
unless $hit 539 171 710
5576 171 my $full = $c . $nm;
5577 171 ▶ next if !$last && !-d $full;
Branch: 1 branch
BranchTrueFalseTotal
unless $last or -d $full 0 171 171
Condition66%2/3
$last or -d $full
l!l&&r!l&&!r
16380
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
5578 171 push @next, $full . $sep;
5579 }
5580 }
5581 42 @cur = @next;
5582 42 ▶ return () unless @cur;
Branch: 1 branch
BranchTrueFalseTotal
unless @cur 25 17 42
5583 }
5584  
5585 # Only existing names are matches. A trailing separator is dropped
5586 # for the probe, since Win32 dislikes "C:\dir\" in a file test.
5587 23 my @out = ();
5588 23 for my $c (@cur) {
5589 174 my $probe = $c;
5590 174 $probe =~ s/$sepclass+\z//;
5591 174 ▶ $probe = $c if $probe eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $probe eq "" 0 174 174
5592 174 ▶ $probe = $c if $win && $probe =~ /\A[A-Za-z]:\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $win and $probe =~ /\A[A-Za-z]:\z/ 0 174 174
Condition33%1/3
$win and $probe =~ /\A[A-Za-z]:\z/
!ll&&!rl&&r
17400
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
5593 174 ▶ next unless -e $probe || -l $probe;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $probe or -l $probe 10 164 174
Condition66%2/3
-e $probe or -l $probe
l!l&&r!l&&!r
164010
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
5594 164 push @out, $c;
5595 }
5596 23 return sort @out;
5597 }
5598  
5599  
5600 # ----------------------------------------------------------------
5601 # _raw_has_glob: true when the RAW (pre-expansion) argument text contains
5602 # a genuine, eligible filename-glob metacharacter (*, ?, [) -- one that
5603 # was actually written in the script and would trigger pathname
5604 # expansion. Used by the "echo" builtin to decide whether to run its
5605 # arguments through glob expansion, WITHOUT being fooled by:
5606 # * a metacharacter inside single or double quotes (echo "*.txt" is
5607 # literal in bash),
5608 # * the '*' of the $* / "$*" special parameter or the '?' of $? (these
5609 # are parameter references, not globs), or
5610 # * a metacharacter inside a ${...} parameter expansion (${arr[*]},
5611 # ${VAR?}), which is likewise not a glob.
5612 # The previous test -- a plain /[*?\[]/ match on the raw text -- fired on
5613 # all of these, so "echo \" x: $*\"" was needlessly re-parsed and its
5614 # leading whitespace and internal spacing collapsed.
5615 # Perl 5.005_03 compatible: single character scan, no regex features.
5616 # ----------------------------------------------------------------
5617 700 ▶sub _raw_has_glob {
Subroutine _raw_has_glob: called CC=20 SCAR=30.0
5618 700 my ($raw) = @_;
5619 700 ▶ return 0 unless defined $raw && $raw =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw and $raw =~ /\S/ 0 700 700
Condition33%1/3
defined $raw and $raw =~ /\S/
!ll&&!rl&&r
00700
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
5620 700 my @c = split //, $raw;
5621 700 my $n = scalar @c;
5622 700 my $in_sq = 0; my $in_dq = 0;
5623 700 my $bdep = 0; # depth inside ${ ... }
5624 700 my $i = 0;
5625 700 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 8159 665 8824
5626 8159 my $ch = $c[$i];
5627 8159 ▶ if ($in_sq) { $in_sq = 0 if $ch eq "'"; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($in_sq) 16 8143 8159
if $ch eq "'" 4 12 16
5628 8143 ▶ if ($ch eq "'" && !$in_dq) { $in_sq = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $in_dq) 4 8139 8143
Condition100%3/3
$ch eq "'" and not $in_dq
!ll&&!rl&&r
813724
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
5629 8139 ▶ if ($ch eq '"') { $in_dq = !$in_dq; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"") 668 7471 8139
5630 7471 ▶ if ($ch eq '\\') { $i += 2; next } # backslash escape
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 4 7467 7471
5631 # Parameter expansion: $* $? $@ ... and ${ ... }
5632 7467 ▶ if ($ch eq '$' && $i + 1 < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\$" and $i + 1 < $n) 485 6982 7467
Condition66%2/3
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
69820485
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
5633 485 ▶ if ($c[$i+1] eq '{') { $bdep++; $i += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c[$i + 1] eq "{") 74 411 485
5634 # $ followed by a single special char (including * and ?) is a
5635 # parameter reference, not a glob -- skip both characters.
5636 411 $i += 2; next;
5637 }
5638 6982 ▶ if ($bdep > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bdep > 0) 667 6315 6982
5639 667 ▶ $bdep-- if $ch eq '}';
Branch: 1 branch
BranchTrueFalseTotal
if $ch eq "}" 74 593 667
5640 667 $i++; next;
5641 }
5642 # An unquoted, non-parameter glob metacharacter counts.
5643 6315 ▶ 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 "[") 35 6280 6315
Condition91%11/12
not $in_sq || $in_dq and $ch eq "*" || $ch eq "?" || $ch eq "["
!ll&&!rl&&r
4685159535
$in_sq || $in_dq
l!l&&r!l&&!r
046851630
$ch eq "*" or $ch eq "?" or $ch eq "["
l!l&&r!l&&!r
19161595
$ch eq "*" or $ch eq "?"
l!l&&r!l&&!r
1721611
MC/DC80%4/5
not $in_sq || $in_dq and $ch eq "*" || $ch eq "?" || $ch eq "["
$in_sq $in_dq $ch eq "*" $ch eq "?" $ch eq "["
Truth table83%5/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
5644 35 return 1;
5645 }
5646 6280 $i++;
5647 }
5648 665 return 0;
5649 }
5650  
5651 # ----------------------------------------------------------------
5652 # _glob_expand_args: apply filename globbing to each unquoted word in
5653 # an already-split argument list. Words that were originally quoted
5654 # (single or double) must already have their quotes stripped by the
5655 # caller; we cannot distinguish them at this stage, so we re-check
5656 # for glob metacharacters and call _glob_expand only when present.
5657 # ----------------------------------------------------------------
5658 0 ▶sub _glob_expand_args {
Subroutine _glob_expand_args: not called CC=2 SCAR=17.9
5659 0 my (@words) = @_;
5660 0 my @result;
5661 0 for my $w (@words) {
5662 0 push @result, _glob_expand($w);
5663 }
5664 0 return @result;
5665 }
5666  
5667 48 ▶sub _glob_to_re {
Subroutine _glob_to_re: called CC=19 SCAR=33.7
5668 48 my ($pat, $greedy) = @_;
5669 48 my $re = '';
5670 48 my @chars = split //, $pat;
5671 48 my $n = scalar @chars;
5672 48 my $i = 0;
5673 48 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 139 48 187
5674 139 my $c = $chars[$i];
5675 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
5676 0 ▶ my @eg = _extglob_scan(\@chars, $i, sub { _glob_to_re($_[0], $greedy) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
5677 0 ▶ if (@eg) { $re .= $eg[1]; $i = $eg[0]; next }
Branch: 1 branch
BranchTrueFalseTotal
if (@eg) 0 0 0
5678 }
5679 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
5680 24 ▶ $re .= $greedy ? '.*' : '.*?';
Branch: 1 branch
BranchTrueFalseTotal
$greedy ? : 23 1 24
5681 }
5682 2 elsif ($c eq '?') { $re .= '.' }
5683 elsif ($c eq '[') {
5684 # POSIX character class. A leading ! (or ^) negates it -- the
5685 # shell spells the negation "[!abc]", which Perl would read as
5686 # the class of "!", "a", "b" and "c" -- and a leading ] and an
5687 # embedded backslash are literal members of the class.
5688 23 my $cls = '[';
5689 23 $i++;
5690 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
Condition66%4/6
$i < $n and $chars[$i] eq "!" || $chars[$i] eq "^"
!ll&&!rl&&r
0221
$chars[$i] eq "!" or $chars[$i] eq "^"
l!l&&r!l&&!r
1022
MC/DC33%1/3
$i < $n and $chars[$i] eq "!" || $chars[$i] eq "^"
$i < $n $chars[$i] eq "!" $chars[$i] eq "^"
Truth table50%2/4
A and B or C
A= $i < $nB= $chars[$i] eq "!"C= $chars[$i] eq "^"
ABCresult
0XX0
1000
11X1
1011
5691 1 $cls .= '^';
5692 1 $i++;
5693 }
5694 0 ▶ 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
5695 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
5696 32 ▶ $cls .= ($chars[$i] eq '\\') ? '\\\\' : $chars[$i];
Branch: 1 branch
BranchTrueFalseTotal
$chars[$i] eq '\\' ? : 3 29 32
5697 32 $i++;
5698 }
5699 23 $cls .= ']';
5700 23 $re .= $cls;
5701 }
5702 90 else { $re .= quotemeta($c) }
5703 139 $i++;
5704 }
5705 48 return $re;
5706 }
5707  
5708 2 ▶sub _sh_remove_suffix {
Subroutine _sh_remove_suffix: called CC=4 SCAR=14.1
5709 2 my ($val, $pat, $greedy) = @_;
5710 # % (greedy=0, shortest suffix): keep longest prefix
5711 # => /\A(.*) PATTERN \z/s with greedy prefix => $1
5712 # %% (greedy=1, longest suffix): keep shortest prefix
5713 # => /\A(.*?)PATTERN \z/s with lazy prefix => $1
5714 2 my $re = _glob_to_re($pat, 1); # pattern itself is always greedy for suffix
5715 2 ▶ if ($greedy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($greedy) { } 1 1 2
5716 # longest suffix removed: lazy prefix
5717 1 ▶ return ($val =~ /\A(.*?)$re\z/s) ? $1 : $val;
Branch: 1 branch
BranchTrueFalseTotal
$val =~ /\A(.*?)$re\z/s ? : 1 0 1
5718 }
5719 else {
5720 # shortest suffix removed: greedy prefix
5721 1 ▶ return ($val =~ /\A(.*)$re\z/s) ? $1 : $val;
Branch: 1 branch
BranchTrueFalseTotal
$val =~ /\A(.*)$re\z/s ? : 1 0 1
5722 }
5723 }
5724  
5725 2 ▶sub _sh_remove_prefix {
Subroutine _sh_remove_prefix: called CC=2 SCAR=7.1
5726 2 my ($val, $pat, $greedy) = @_;
5727 # # (greedy=0, shortest prefix): keep longest suffix
5728 # => /\A PATTERN(.*) \z/s with lazy pattern => $1
5729 # ## (greedy=1, longest prefix): keep shortest suffix
5730 # => /\A PATTERN(.*) \z/s with greedy pattern => $1
5731 2 my $re = _glob_to_re($pat, $greedy);
5732 2 ▶ return ($val =~ /\A$re(.*)\z/s) ? $1 : $val;
Branch: 1 branch
BranchTrueFalseTotal
$val =~ /\A$re(.*)\z/s ? : 2 0 2
5733 }
5734  
5735 2 ▶sub _sh_replace {
Subroutine _sh_replace: called CC=2 SCAR=6.9
5736 2 my ($val, $pat, $rep, $global) = @_;
5737 2 my $re = _glob_to_re($pat, 1);
5738 2 ▶ if ($global) { $val =~ s/$re/$rep/g }
Branch: 1 branch
BranchTrueFalseTotal
if ($global) { } 1 1 2
5739 1 else { $val =~ s/$re/$rep/ }
5740 2 return $val;
5741 }
5742  
5743 # ----------------------------------------------------------------
5744 # Shell function registry { name => \@body_lines }
5745 # ----------------------------------------------------------------
5746  
5747 # ----------------------------------------------------------------
5748 # _inline_body_has_control: true when a single-line function body
5749 # (the text between the braces of "name() { ... }") contains a shell
5750 # control-structure keyword in command position -- if/for/while/until/
5751 # case/select as the first word, or after a ';', '&&', '||' or '|'.
5752 # Such a body must not be torn apart on ';' (that would split
5753 # "while C; do B; done" into unusable fragments); the caller keeps it
5754 # as one line so _run_lines()'s inline-control handling parses it.
5755 # Quotes, $(...), `...` and ${...} are skipped so a keyword appearing
5756 # only inside them (echo "done", VAR=$(case ...)) does not count.
5757 # Perl 5.005_03 compatible: character scan, no regex features beyond
5758 # \A and \b.
5759 # ----------------------------------------------------------------
5760 12 ▶sub _inline_body_has_control {
Subroutine _inline_body_has_control: called CC=42 SCAR=48.4
5761 12 my ($body) = @_;
5762 12 ▶ return 0 unless defined $body && $body =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body and $body =~ /\S/ 0 12 12
Condition33%1/3
defined $body and $body =~ /\S/
!ll&&!rl&&r
0012
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
5763 12 my @c = split //, $body;
5764 12 my $n = scalar @c;
5765 12 my $in_sq = 0; my $in_dq = 0; my $in_bt = 0;
5766 12 my $pdep = 0; my $bdep = 0;
5767 12 my $i = 0;
5768 12 my $cmd_pos = 1;
5769 12 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 151 10 161
5770 151 my $ch = $c[$i];
5771 0 ▶ if ($in_sq) { $in_sq = 0 if $ch eq "'"; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($in_sq) 0 151 151
if $ch eq "'" 0 0 0
5772 0 ▶ 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 151 151
Condition33%2/6
$ch eq "'" and not $in_dq and not $in_bt
!ll&&!rl&&r
15100
$ch eq "'" and not $in_dq
!ll&&!rl&&r
15100
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
5773 151 ▶ 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) 8 143 151
Condition66%2/3
$ch eq "\"" and not $in_bt
!ll&&!rl&&r
14308
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
5774 0 ▶ if ($ch eq '\\') { $i += 2; $cmd_pos = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 0 143 143
5775 0 ▶ if ($ch eq '`') { $in_bt = !$in_bt; $cmd_pos = 0; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "`") 0 143 143
5776 143 ▶ if (!$in_dq && !$in_bt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_dq or $in_bt) 92 51 143
Condition66%2/3
$in_dq or $in_bt
l!l&&r!l&&!r
51092
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
5777 0 ▶ 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 92 92
Condition66%4/6
$ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "{"
!ll&&!rl&&r
9020
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
9002
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
5778 0 ▶ if ($bdep > 0 && $ch eq '}') { $bdep--; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($bdep > 0 and $ch eq "}") 0 92 92
Condition33%1/3
$bdep > 0 and $ch eq "}"
!ll&&!rl&&r
9200
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
5779 92 ▶ 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 91 92
Condition83%5/6
$ch eq "\$" and $i + 1 < $n and $c[$i + 1] eq "("
!ll&&!rl&&r
9011
$ch eq "\$" and $i + 1 < $n
!ll&&!rl&&r
9002
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
5780 91 ▶ if ($ch eq '(') { $pdep++; $i++; $cmd_pos = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "(") 1 90 91
5781 90 ▶ if ($ch eq ')' && $pdep > 0) { $pdep--; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ")" and $pdep > 0) 2 88 90
Condition66%2/3
$ch eq ")" and $pdep > 0
!ll&&!rl&&r
8802
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
5782 }
5783 139 ▶ 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) 80 59 139
Condition77%7/9
not $in_dq || $in_bt and $pdep == 0 and $bdep == 0
!ll&&!rl&&r
59080
not $in_dq || $in_bt and $pdep == 0
!ll&&!rl&&r
51880
$in_dq || $in_bt
l!l&&r!l&&!r
51088
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
5784 80 ▶ if ($ch eq ';') { $cmd_pos = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ";") 2 78 80
5785 0 ▶ 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 78 78
Condition33%2/6
$ch eq "&" and $i + 1 < $n and $c[$i + 1] eq "&"
!ll&&!rl&&r
7800
$ch eq "&" and $i + 1 < $n
!ll&&!rl&&r
7800
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
5786 0 ▶ 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 78 78
Condition33%2/6
$ch eq "|" and $i + 1 < $n and $c[$i + 1] eq "|"
!ll&&!rl&&r
7800
$ch eq "|" and $i + 1 < $n
!ll&&!rl&&r
7800
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
5787 0 ▶ if ($ch eq '|') { $cmd_pos = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "|") 0 78 78
5788 0 ▶ if ($ch eq '&') { $cmd_pos = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "&") 0 78 78
5789 78 ▶ if ($ch =~ /\s/) { $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch =~ /\s/) 13 65 78
5790 65 ▶ if ($cmd_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd_pos) 14 51 65
5791 14 my $tail = join('', @c[$i .. $n-1]);
5792 14 ▶ 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 12 14
5793 2 return 1;
5794 }
5795 }
5796 63 $cmd_pos = 0; $i++; next;
5797 }
5798 59 $i++;
5799 }
5800 10 return 0;
5801 }
5802  
5803 # ----------------------------------------------------------------
5804 # _parse_function: parse "name() {" or "function name {" blocks
5805 # Returns ($status, $new_i).
5806 # ----------------------------------------------------------------
5807 25 ▶sub _parse_function {
Subroutine _parse_function: called CC=16 SCAR=31.2
5808 25 my ($class, $lines_ref, $start, $opts_ref) = @_;
5809 25 my @lines = @{$lines_ref};
5810 25 my $line = $lines[$start];
5811 25 $line =~ s/\r?\n\z//;
5812 25 $line =~ s/\A\s+//;
5813  
5814 25 my $name = '';
5815 25 ▶ 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/) { } 25 0 25
elsif ($line =~ /\Afunction\s+([A-Za-z_][A-Za-z0-9_]*)\s*(?:\(\s*\))?\s*(?:\{.*)?\z/i) { } 0 0 0
5816 25 $name = $1;
5817 }
5818 elsif ($line =~ /\Afunction\s+([A-Za-z_][A-Za-z0-9_]*)\s*(?:\(\s*\))?\s*(?:\{.*)?\z/i) {
5819 0 $name = $1;
5820 }
5821 else {
5822 0 return (0, $start + 1);
5823 }
5824  
5825 25 my @body;
5826 25 ▶ my $depth = ($line =~ /\{/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /\{/ ? : 25 0 25
5827 25 my $i = $start + 1;
5828  
5829 # Check if the function body is on the same line as the definition
5830 # e.g. "name() { cmd1; cmd2; }"
5831 25 ▶ if ($depth >= 1 && $line =~ /\{(.*)\}\s*\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth >= 1 and $line =~ /\{(.*)\}\s*\z/s) 12 13 25
Condition66%2/3
$depth >= 1 and $line =~ /\{(.*)\}\s*\z/s
!ll&&!rl&&r
01312
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
5832 12 my $inline = $1;
5833 12 $inline =~ s/\A\s+//; $inline =~ s/\s+\z//;
5834 # A trailing ';' just before the closing brace (name() { ...; })
5835 # is an empty separator; drop it so an inline control structure's
5836 # "; done"/"; fi"/"; esac" terminator sits at end-of-string where
5837 # the block parsers expect it.
5838 12 $inline =~ s/;\s*\z//;
5839 12 $inline =~ s/\s+\z//;
5840 # A naive split on ';' is correct for a body of simple commands
5841 # (name() { cmd1; cmd2; }) but WRONG for an inline control
5842 # structure: "while C; do B; done" would be torn into the four
5843 # pieces "while C" / "do B" / ... / "done", and the reassembled
5844 # multi-line form ("do" glued to a command on one line) is not a
5845 # shape the block parsers accept, so the loop silently ran
5846 # nothing. When the inline body contains a control-structure
5847 # keyword in command position, keep it as a SINGLE body line and
5848 # let _run_lines() apply the same inline-control handling it uses
5849 # for a control structure typed directly on one physical line
5850 # (this also drives the "prefix; control" split via
5851 # _find_control_split, so "cmd; while ...; do ...; done" works).
5852 12 ▶ if (_inline_body_has_control($inline)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_inline_body_has_control($inline)) { } 2 10 12
5853 2 ▶ push @body, $inline if $inline =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $inline =~ /\S/ 2 0 2
5854 }
5855 else {
5856 # Split on ; to get individual commands
5857 10 for my $part (split /;/, $inline) {
5858 11 $part =~ s/\A\s+//; $part =~ s/\s+\z//;
5859 11 ▶ push @body, $part if $part =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $part =~ /\S/ 11 0 11
5860 }
5861 }
5862 12 $_SH_FUNCTIONS{$name} = [ @body ];
5863 12 return (0, $i);
5864 }
5865  
5866 13 ▶ if ($depth == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 0) 0 13 13
5867 0 ▶ while ($i <= $#lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#lines) 0 0 0
5868 0 my $l = $lines[$i]; $l =~ s/\r?\n\z//; $l =~ s/\A\s+//;
5869 0 $i++;
5870 0 ▶ if ($l =~ /\{/) { $depth = 1; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /\{/) 0 0 0
5871 }
5872 }
5873  
5874 13 ▶ while ($i <= $#lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#lines) 54 0 54
5875 54 my $l = $lines[$i]; $l =~ s/\r?\n\z//;
5876 54 $i++;
5877 54 my $opens = () = ($l =~ /\{/g);
5878 54 my $closes = () = ($l =~ /\}/g);
5879 54 $depth += $opens - $closes;
5880 54 ▶ if ($depth <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth <= 0) 13 41 54
5881 13 my $before = $l;
5882 13 $before =~ s/\}\s*\z//;
5883 13 ▶ push @body, $before if $before =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $before =~ /\S/ 0 13 13
5884 13 last;
5885 }
5886 41 push @body, $l;
5887 }
5888  
5889 13 $_SH_FUNCTIONS{$name} = [ @body ];
5890 13 return (0, $i);
5891 }
5892  
5893 # ----------------------------------------------------------------
5894 # _call_sh_function: execute a registered SH function
5895 # ----------------------------------------------------------------
5896 29 ▶sub _call_sh_function {
Subroutine _call_sh_function: called CC=14 SCAR=26.5
5897 29 my ($class, $name, $args_str, $opts_ref) = @_;
5898 29 ▶ return 1 unless exists $_SH_FUNCTIONS{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BATsh::SH::_SH_FUNCTIONS{$name} 0 29 29
5899  
5900 29 my @args = _parse_args($args_str);
5901  
5902 29 my @saved_arg;
5903 29 for my $n (1 .. 9) {
5904 261 push @saved_arg, BATsh::Env->get("BATSH_ARG$n");
5905 261 ▶ BATsh::Env->set("BATSH_ARG$n",
Branch: 1 branch
BranchTrueFalseTotal
defined $args[$n - 1] ? : 50 211 261
5906 defined($args[$n-1]) ? $args[$n-1] : '');
5907 }
5908 29 my @saved_pct;
5909 29 for my $n (1 .. 9) {
5910 261 push @saved_pct, BATsh::Env->get("%$n");
5911 261 ▶ BATsh::Env->set("%$n", defined($args[$n-1]) ? $args[$n-1] : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $args[$n - 1] ? : 50 211 261
5912 }
5913 29 my $saved_star = BATsh::Env->get('%*');
5914 29 BATsh::Env->set('%*', join(' ', @args));
5915  
5916 29 push @FUNCTION_STACK, {};
5917 29 my $saved_ret = $_RETURN;
5918 29 $_RETURN = 0;
5919  
5920 29 my $rc = _run_lines($class, $_SH_FUNCTIONS{$name}, $opts_ref);
5921  
5922 29 $_RETURN = $saved_ret;
5923  
5924 # Restore local variables saved in this function's scope
5925 29 ▶ if (@FUNCTION_STACK) {
Branch: 1 branch
BranchTrueFalseTotal
if (@FUNCTION_STACK) 29 0 29
5926 29 my $frame = $FUNCTION_STACK[-1];
5927 29 for my $var (keys %{$frame}) {
5928 3 my $old = $frame->{$var};
5929 3 ▶ if (defined $old) { BATsh::Env->set($var, $old) }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old) { } 3 0 3
5930 0 else { BATsh::Env->unset($var) }
5931 }
5932 }
5933 29 pop @FUNCTION_STACK;
5934  
5935 29 for my $n (1 .. 9) {
5936 261 my $v = $saved_arg[$n-1];
5937 261 ▶ BATsh::Env->set("BATSH_ARG$n", defined $v ? $v : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 117 144 261
5938 }
5939 29 for my $n (1 .. 9) {
5940 261 my $v = $saved_pct[$n-1];
5941 261 ▶ BATsh::Env->set("%$n", defined $v ? $v : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 145 116 261
5942 }
5943 29 ▶ BATsh::Env->set('%*', defined $saved_star ? $saved_star : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $saved_star ? : 16 13 29
5944  
5945 29 $LAST_STATUS = $rc;
5946 29 return $rc;
5947 }
5948  
5949 # ----------------------------------------------------------------
5950 # _parse_args: split a string into arguments respecting quotes
5951 # ----------------------------------------------------------------
5952 136 ▶sub _parse_args {
Subroutine _parse_args: called CC=23 SCAR=36.0
5953 # $protect_quoted is set by a caller that joins the words back into a
5954 # string and hands that string to a builtin which dequotes it again.
5955 # The quotes are gone by then, so a backslash that was INSIDE them
5956 # would be read as an escape by that second pass and disappear:
5957 # echo "C:\dir"/*.txt printed C:dir/*.txt. Protecting it marks it as
5958 # data, exactly as an expansion result is marked, while a backslash
5959 # written outside quotes stays an escape, as in bash.
5960 136 my ($str, $protect_quoted) = @_;
5961 136 ▶ $str = '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 136 136
5962 136 $str =~ s/\A\s+//; $str =~ s/\s+\z//;
5963 136 ▶ return () unless $str =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /\S/ 5 131 136
5964 131 my @args;
5965 my @quoted; # parallel array: 1 if word was quoted, 0 if bare
5966 131 my $cur = '';
5967 131 my $word_quoted = 0;
5968 131 my $in_sq = 0;
5969 131 my $in_dq = 0;
5970 131 for my $ch (split //, $str) {
5971 1304 ▶ if ($in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq) 0 1304 1304
5972 0 ▶ if ($ch eq "'") { $in_sq = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq q[']) { } 0 0 0
5973 else {
5974 0 ▶ $cur .= $protect_quoted ? _protect_lit($ch) : $ch;
Branch: 1 branch
BranchTrueFalseTotal
$protect_quoted ? : 0 0 0
5975 0 $word_quoted = 1;
5976 }
5977 0 next;
5978 }
5979 0 ▶ if ($ch eq "'" && !$in_dq) { $in_sq = 1; $word_quoted = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $in_dq) 0 1304 1304
Condition33%1/3
$ch eq "'" and not $in_dq
!ll&&!rl&&r
130400
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
5980 1304 ▶ if ($ch eq '"' && !$in_sq) { $in_dq = !$in_dq; $word_quoted = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" and not $in_sq) 134 1170 1304
Condition66%2/3
$ch eq "\"" and not $in_sq
!ll&&!rl&&r
11700134
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
5981 1170 ▶ if ($ch =~ /\s/ && !$in_sq && !$in_dq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch =~ /\s/ and not $in_sq and not $in_dq) 181 989 1170
Condition66%4/6
$ch =~ /\s/ and not $in_sq and not $in_dq
!ll&&!rl&&r
9890181
$ch =~ /\s/ and not $in_sq
!ll&&!rl&&r
9890181
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
5982 181 push @args, $cur;
5983 181 push @quoted, $word_quoted;
5984 181 $cur = ''; $word_quoted = 0;
5985 181 next;
5986 }
5987 989 ▶ $cur .= ($protect_quoted && $in_dq) ? _protect_lit($ch) : $ch;
Branch: 1 branch
BranchTrueFalseTotal
$protect_quoted && $in_dq ? : 3 986 989
Condition100%3/3
$protect_quoted and $in_dq
!ll&&!rl&&r
5913953
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
5988 }
5989 131 ▶ push @args, $cur if $cur ne '' || @args;
Branch: 1 branch
BranchTrueFalseTotal
if $cur ne "" or @args 131 0 131
Condition33%1/3
$cur ne "" or @args
l!l&&r!l&&!r
13100
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
5990 131 ▶ push @quoted, $word_quoted if $cur ne '' || @quoted;
Branch: 1 branch
BranchTrueFalseTotal
if $cur ne "" or @quoted 131 0 131
Condition33%1/3
$cur ne "" or @quoted
l!l&&r!l&&!r
13100
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
5991 # Apply filename globbing to unquoted words containing metacharacters
5992 131 my @result;
5993 131 for my $i (0 .. $#args) {
5994 312 ▶ if (!$quoted[$i] && $args[$i] =~ /[*?\[]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $quoted[$i] and $args[$i] =~ /[*?\[]/) { } 43 269 312
Condition100%3/3
not $quoted[$i] and $args[$i] =~ /[*?\[]/
!ll&&!rl&&r
6720243
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
5995 43 push @result, _glob_expand($args[$i]);
5996 }
5997 else {
5998 269 push @result, $args[$i];
5999 }
6000 }
6001 131 return @result;
6002 }
6003  
6004 # ----------------------------------------------------------------
6005 # ----------------------------------------------------------------
6006 36 ▶sub _cmd_external {
Subroutine _cmd_external: called CC=6 SCAR=18.3
6007 36 my ($cmd, $rest) = @_;
6008 36 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 36 36
6009 36 $rest =~ s/\A\s+//;
6010 36 ▶ my $full = $rest ne '' ? "$cmd $rest" : $cmd;
Branch: 1 branch
BranchTrueFalseTotal
$rest ne '' ? : 32 4 36
6011 # The command line goes to the OS verbatim, so a protected backslash
6012 # has to be a real backslash again here.
6013 36 $full = _unprotect_lit($full);
6014 36 $full = BATsh::MB::dec($full);
6015 36 BATsh::Env->sync_to_env();
6016 36 my $rc = system($full);
6017 36 ▶ if ($rc == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc == -1) { } 2 34 36
6018 # system() could not launch the command (not found / exec failed).
6019 # bash reports 127 for "command not found"; without this guard the
6020 # old ($rc >> 8) arithmetic on -1 produced a huge garbage status.
6021 2 $LAST_STATUS = 127;
6022 }
6023 else {
6024 34 ▶ $LAST_STATUS = ($rc == 0) ? 0 : (($rc >> 8) || 1);
Branch: 1 branch
BranchTrueFalseTotal
$rc == 0 ? : 34 0 34
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
6025 }
6026 36 return $LAST_STATUS;
6027 }
6028  
6029 # ----------------------------------------------------------------
6030 # let / type / command (v0.08)
6031 # ----------------------------------------------------------------
6032 # Prior to v0.08 these three POSIX/bash builtins fell through to the
6033 # external-command path and were handed to whatever real shell (if any)
6034 # happened to exist -- which failed on systems without that shell, and
6035 # defeated BATsh's "no external shell required" design. They are now
6036 # evaluated internally in pure Perl.
6037  
6038 # let EXPR [EXPR ...]
6039 # Each argument is evaluated as a shell-arithmetic expression (reusing
6040 # _eval_arith, so assignments and ++/-- write back to the variable store).
6041 # The exit status is 0 when the LAST expression is non-zero, else 1 --
6042 # the inverse-of-truth convention bash's "let" and "(( ))" both use.
6043 # Arguments are quote-stripped but NOT filename-globbed, so `let "x=1*2"`
6044 # and `let x=1+2` both behave as arithmetic rather than pathname patterns.
6045 8 ▶sub _cmd_let {
Subroutine _cmd_let: called CC=5 SCAR=16.1
6046 8 my ($rest) = @_;
6047 8 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 8 8
6048 8 my @exprs = map { _arr_dequote($_) } _arr_split_words($rest);
6049 8 ▶ if (!@exprs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@exprs) 1 7 8
6050 1 print STDERR "sh: let: expression expected\n";
6051 1 $LAST_STATUS = 1;
6052 1 return 1;
6053 }
6054 7 my $last = 0;
6055 7 for my $e (@exprs) {
6056 8 $last = _eval_arith($e);
6057 }
6058 7 ▶ $LAST_STATUS = ($last != 0) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$last != 0 ? : 5 2 7
6059 7 return $LAST_STATUS;
6060 }
6061  
6062 # _sh_name_kind(NAME): classify NAME the way "type" / "command -v" do,
6063 # in bash's precedence order (alias, keyword, function, builtin, file).
6064 # Returns (KIND, DETAIL) where KIND is one of
6065 # 'alias' (DETAIL = alias body), 'keyword', 'function', 'builtin',
6066 # 'file' (DETAIL = resolved path), or '' (not found; DETAIL undef).
6067 71 ▶sub _sh_name_kind {
Subroutine _sh_name_kind: called CC=11 SCAR=25.0
6068 71 my ($name) = @_;
6069 71 ▶ return ('', undef) unless defined $name && $name ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and $name ne "" 0 71 71
Condition33%1/3
defined $name and $name ne ""
!ll&&!rl&&r
0071
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
6070  
6071 71 ▶ return ('alias', $_SH_ALIAS{$name}) if exists $_SH_ALIAS{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $BATsh::SH::_SH_ALIAS{$name} 2 69 71
6072  
6073 69 my $lc = lc($name);
6074 # "time" is deliberately absent: bash has it as a reserved word, but this
6075 # interpreter does not implement it, so reporting it as a keyword made
6076 # "type -t time" say "keyword" while "time echo hi" failed with
6077 # "Can't exec time". Leaving it out lets it resolve as an external
6078 # program (or as "not found"), which is what actually happens.
6079 69 my %kw = map { ($_ => 1) } qw(
6080 if then else elif fi for while until do done
6081 case esac function in select
6082 );
6083 69 ▶ return ('keyword', undef) if $kw{$lc};
Branch: 1 branch
BranchTrueFalseTotal
if $kw{$lc} 17 52 69
6084  
6085 52 ▶ return ('function', undef) if exists $_SH_FUNCTIONS{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $BATsh::SH::_SH_FUNCTIONS{$name} 1 51 52
6086  
6087 51 ▶ return ('builtin', undef) if $name eq '[' || $name eq ':' || $name eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "[" or $name eq ":" or $name eq "." 0 51 51
Condition33%2/6
$name eq "[" or $name eq ":" or $name eq "."
l!l&&r!l&&!r
0051
$name eq "[" or $name eq ":"
l!l&&r!l&&!r
0051
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
6088 51 my %bi = map { ($_ => 1) } qw(
6089 alias break cd command continue declare echo eval exec exit export
6090 false getopts hash let local mapfile printf pwd read readarray
6091 readonly return set shift shopt source test true trap type typeset
6092 umask unalias unset
6093 );
6094 51 ▶ return ('builtin', undef) if $bi{$lc};
Branch: 1 branch
BranchTrueFalseTotal
if $bi{$lc} 43 8 51
6095  
6096 8 my $path = _sh_find_on_path($name);
6097 8 ▶ return ('file', $path) if defined $path;
Branch: 1 branch
BranchTrueFalseTotal
if defined $path 3 5 8
6098  
6099 5 return ('', undef);
6100 }
6101  
6102 # _sh_find_on_path(NAME): locate an executable NAME the way a shell would.
6103 # A NAME containing a directory separator is tested directly; otherwise each
6104 # PATH element is searched (';'-separated and PATHEXT-aware on Windows-like
6105 # systems, ':'-separated and -x-tested elsewhere). Returns the full path,
6106 # or undef when not found. Pure-Perl and Perl 5.005_03 safe.
6107 10 ▶sub _sh_find_on_path {
Subroutine _sh_find_on_path: called CC=25 SCAR=46.1
6108 10 my ($name) = @_;
6109 10 ▶ return undef unless defined $name && $name ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and $name ne "" 0 10 10
Condition33%1/3
defined $name and $name ne ""
!ll&&!rl&&r
0010
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
6110 10 ▶ my $is_win = ($^O =~ /MSWin32|dos|os2|cygwin/i) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /MSWin32|dos|os2|cygwin/i ? : 0 10 10
6111  
6112 10 ▶ if ($name =~ m{[/\\]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ m([/\\])) 1 9 10
6113 1 ▶ return $name if -f $name && ($is_win || -x $name);
Branch: 1 branch
BranchTrueFalseTotal
if -f $name and $is_win || -x $name 0 1 1
Condition16%1/6
-f $name and $is_win || -x $name
!ll&&!rl&&r
100
$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 table25%1/4
A and B or C
A= -f $nameB= $is_winC= -x $name
ABCresult
0XX0
1000
11X1
1011
6114 1 return undef;
6115 }
6116  
6117 9 my $path = BATsh::Env->get('PATH');
6118 9 ▶ $path = $ENV{'PATH'} unless defined $path && $path ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and $path ne "" 0 9 9
Condition33%1/3
defined $path and $path ne ""
!ll&&!rl&&r
009
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
6119 9 ▶ return undef unless defined $path && $path ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and $path ne "" 0 9 9
Condition33%1/3
defined $path and $path ne ""
!ll&&!rl&&r
009
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
6120  
6121 9 ▶ my $sep = $is_win ? ';' : ':';
Branch: 1 branch
BranchTrueFalseTotal
$is_win ? : 0 9 9
6122 9 my @exts = ('');
6123 9 ▶ if ($is_win) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_win) 0 9 9
6124 0 my $pe = BATsh::Env->get('PATHEXT');
6125 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
6126 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
6127 0 push @exts, split(/;/, $pe);
6128 }
6129  
6130 9 my @dirs = split(/\Q$sep\E/, $path);
6131 9 for my $dir (@dirs) {
6132 67 ▶ $dir = '.' if $dir eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq "" 0 67 67
6133 67 for my $ext (@exts) {
6134 67 my $cand = File::Spec->catfile($dir, $name . $ext);
6135 67 ▶ if ($is_win) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_win) { } 0 67 67
6136 0 ▶ return $cand if -f $cand;
Branch: 1 branch
BranchTrueFalseTotal
if -f $cand 0 0 0
6137 }
6138 else {
6139 67 ▶ return $cand if -f $cand && -x $cand;
Branch: 1 branch
BranchTrueFalseTotal
if -f $cand and -x $cand 4 63 67
Condition66%2/3
-f $cand and -x $cand
!ll&&!rl&&r
6304
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
6140 }
6141 }
6142 }
6143 5 return undef;
6144 }
6145  
6146 # _print_type_verbose(NAME, KIND, DETAIL): emit the "type NAME" / "command -V
6147 # NAME" description line. Returns 1 when found, 0 (with a STDERR diagnostic)
6148 # when NAME is unknown.
6149 3 ▶sub _print_type_verbose {
Subroutine _print_type_verbose: called CC=7 SCAR=24.5
6150 3 my ($name, $kind, $detail) = @_;
6151 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
6152 0 ▶ print "$name is aliased to `" . (defined $detail ? $detail : '') . "'\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $detail ? : 0 0 0
6153 }
6154 0 elsif ($kind eq 'keyword') { print "$name is a shell keyword\n" }
6155 0 elsif ($kind eq 'function') { print "$name is a function\n" }
6156 2 elsif ($kind eq 'builtin') { print "$name is a shell builtin\n" }
6157 0 elsif ($kind eq 'file') { print "$name is $detail\n" }
6158 else {
6159 1 print STDERR "sh: type: $name: not found\n";
6160 1 return 0;
6161 }
6162 2 return 1;
6163 }
6164  
6165 # type [-t|-p|-a] NAME [NAME ...]
6166 # Default: describe how each NAME would be interpreted.
6167 # -t: print only the one-word kind (alias/keyword/function/builtin/file).
6168 # -p: print only the path when NAME is an external file (else nothing).
6169 # -a: describe all matches (this interpreter reports the single primary
6170 # match, so -a behaves like the default form).
6171 # Exit status is non-zero when any NAME is not found.
6172 58 ▶sub _cmd_type {
Subroutine _cmd_type: called CC=15 SCAR=28.0
6173 58 my ($rest) = @_;
6174 58 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 58 58
6175 58 my @w = map { _arr_dequote($_) } _arr_split_words($rest);
6176  
6177 58 my $mode = ''; # '', 't', 'p', 'a'
6178 58 ▶ while (@w && $w[0] =~ /\A-[atpP]+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@w and $w[0] =~ /\A-[atpP]+\z/) 56 58 114
Condition66%2/3
@w and $w[0] =~ /\A-[atpP]+\z/
!ll&&!rl&&r
05856
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
6179 56 my $opt = shift @w;
6180 56 ▶ $mode = 'a' if $opt =~ /a/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt =~ /a/ 0 56 56
6181 56 ▶ $mode = 't' if $opt =~ /t/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt =~ /t/ 55 1 56
6182 56 ▶ $mode = 'p' if $opt =~ /[pP]/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt =~ /[pP]/ 1 55 56
6183 }
6184 0 ▶ if (!@w) { $LAST_STATUS = 0; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (@w) 0 58 58
6185  
6186 58 my $status = 0;
6187 58 for my $name (@w) {
6188 58 my ($kind, $detail) = _sh_name_kind($name);
6189 58 ▶ if ($mode eq 't') {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 't') { } 55 3 58
elsif ($mode eq 'p') { } 1 2 3
6190 55 ▶ if ($kind eq '') { $status = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq '') { } 1 54 55
6191 54 else { print $kind, "\n" }
6192 }
6193 elsif ($mode eq 'p') {
6194 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
6195 0 elsif ($kind eq '') { $status = 1 }
6196 # builtin/function/keyword/alias: print nothing (bash -p)
6197 }
6198 else {
6199 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
6200 }
6201 }
6202 58 $LAST_STATUS = $status;
6203 58 return $status;
6204 }
6205  
6206 # command [-p] [-v|-V] NAME [ARG ...]
6207 # -v: print how NAME would be invoked (name for a builtin/function/keyword,
6208 # "alias NAME='...'" for an alias, full path for an external file);
6209 # exit non-zero if NAME is not found -- the ubiquitous feature-detection
6210 # idiom `if command -v foo >/dev/null; then ...`.
6211 # -V: verbose description, like "type NAME".
6212 # plain: run NAME bypassing any shell function of that name.
6213 # -p is accepted and ignored (PATH is always searched).
6214 7 ▶sub _cmd_command {
Subroutine _cmd_command: called CC=22 SCAR=33.6
6215 7 my ($class, $rest, $raw_pre, $opts_ref) = @_;
6216 7 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 7 7
6217 7 my @w = map { _arr_dequote($_) } _arr_split_words($rest);
6218  
6219 7 my $mode = ''; # '', 'v', 'V'
6220 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
6221 6 my $opt = shift @w;
6222 6 ▶ $mode = 'v' if $opt =~ /v/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt =~ /v/ 5 1 6
6223 6 ▶ $mode = 'V' if $opt =~ /V/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt =~ /V/ 1 5 6
6224 # -p: accepted, no effect
6225 }
6226 # A bare "--" ends option parsing.
6227 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
6228  
6229 0 ▶ 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
6230  
6231 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
6232 6 my $status = 0;
6233 6 for my $name (@w) {
6234 6 my ($kind, $detail) = _sh_name_kind($name);
6235 6 ▶ if ($mode eq 'V') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq 'V') { } 1 5 6
6236 1 ▶ _print_type_verbose($name, $kind, $detail) or $status = 1;
Branch: 1 branch
BranchTrueFalseTotal
_print_type_verbose($name, $kind, $detail) or $status = 1 1 0 1
6237 }
6238 else { # -v
6239 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
6240 1 elsif ($kind eq 'file') { print $detail, "\n" }
6241 1 elsif ($kind eq '') { $status = 1 }
6242 2 else { print $name, "\n" } # keyword/function/builtin
6243 }
6244 }
6245 6 $LAST_STATUS = $status;
6246 6 return $status;
6247 }
6248  
6249 # Plain run form: re-dispatch the raw remainder with function lookup
6250 # suppressed. Recover the text after the leading "command" word (and any
6251 # -p / -- options) from the pre-expansion source so _expand() runs once.
6252 1 ▶ my $raw = defined $raw_pre ? $raw_pre : $rest;
Branch: 1 branch
BranchTrueFalseTotal
defined $raw_pre ? : 1 0 1
6253 1 $raw =~ s/\A\s*//;
6254 1 $raw =~ s/\A[Cc][Oo][Mm][Mm][Aa][Nn][Dd]\b//;
6255 1 $raw =~ s/\A\s+//;
6256 1 ▶ while ($raw =~ /\A(-[pvV]+|--)\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw =~ /\A(-[pvV]+|--)\s/) 0 1 1
6257 0 my $tok = $1;
6258 0 $raw =~ s/\A(?:-[pvV]+|--)\s+//;
6259 0 ▶ last if $tok eq '--';
Branch: 1 branch
BranchTrueFalseTotal
if $tok eq "--" 0 0 0
6260 }
6261 1 local $_CMD_NO_FUNC = 1;
6262 1 return _exec_line($class, $raw, $opts_ref);
6263 }
6264  
6265 # ----------------------------------------------------------------
6266 # Background execution helpers (v1)
6267 # ----------------------------------------------------------------
6268 # ----------------------------------------------------------------
6269 # umask / hash / readonly / mapfile (v0.08)
6270 # ----------------------------------------------------------------
6271 # _sh_is_readonly(NAME): true when NAME carries the readonly attribute.
6272 9 ▶sub _sh_is_readonly {
Subroutine _sh_is_readonly: called CC=3 SCAR=11.7
6273 9 my ($name) = @_;
6274 9 ▶ return 0 unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 9 9
6275 9 ▶ return $_SH_READONLY{ uc($name) } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$BATsh::SH::_SH_READONLY{uc $name} ? : 0 9 9
6276 }
6277  
6278 # _sh_store_scalar(NAME, VALUE): the single choke point for a plain scalar
6279 # assignment. Honours the readonly attribute (refused, status source
6280 # returns 0) and the integer attribute (VALUE evaluated as arithmetic).
6281 # Returns 1 when the value was stored, 0 when a readonly variable blocked
6282 # it. All the ordinary assignment paths (VAR=val, prefix VAR=val cmd,
6283 # export VAR=val) funnel through here so the attributes are enforced
6284 # uniformly.
6285 313 ▶sub _sh_store_scalar {
Subroutine _sh_store_scalar: called CC=5 SCAR=16.2
6286 313 my ($name, $val) = @_;
6287 313 ▶ $val = _unprotect_lit($val) if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 313 0 313
6288 313 my $ik = uc($name);
6289 313 ▶ if ($_SH_READONLY{$ik}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BATsh::SH::_SH_READONLY{$ik}) 2 311 313
6290 2 print STDERR "sh: $name: readonly variable\n";
6291 2 return 0;
6292 }
6293 311 ▶ if ($_SH_INTATTR{$ik}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BATsh::SH::_SH_INTATTR{$ik}) 1 310 311
6294 1 ▶ $val = _eval_arith(defined $val ? $val : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 1 0 1
6295 }
6296 311 BATsh::Env->set($name, $val);
6297 311 return 1;
6298 }
6299  
6300 # umask [-S] [MODE]
6301 # With no MODE, print the current file-creation mask (octal, or symbolic
6302 # with -S). A numeric (octal) MODE sets the mask. Uses Perl's umask, so
6303 # it is a genuine process mask on Unix-like systems (a no-op reflecting
6304 # whatever the C library reports on Win32).
6305 12 ▶sub _cmd_umask {
Subroutine _cmd_umask: called CC=9 SCAR=22.2
6306 12 my ($rest) = @_;
6307 12 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 12 12
6308 12 $rest =~ s/\A\s+//; $rest =~ s/\s+\z//;
6309  
6310 # Seed the shell mask from the real process umask on first use.
6311 12 ▶ if (!$_SH_UMASK_INIT) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($BATsh::SH::_SH_UMASK_INIT) 1 11 12
6312 1 my $u = umask();
6313 1 ▶ $_SH_UMASK = defined $u ? $u : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $u ? : 1 0 1
6314 1 $_SH_UMASK_INIT = 1;
6315 }
6316  
6317 12 my $symbolic = 0;
6318 12 ▶ if ($rest =~ s/\A-S\b\s*//) { $symbolic = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/\A-S\b\s*//) 1 11 12
6319  
6320 12 ▶ if ($rest eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest eq "") 5 7 12
6321 5 ▶ if ($symbolic) { print _umask_symbolic($_SH_UMASK), "\n" }
Branch: 1 branch
BranchTrueFalseTotal
if ($symbolic) { } 1 4 5
6322 4 else { printf "%04o\n", $_SH_UMASK }
6323 5 $LAST_STATUS = 0;
6324 5 return 0;
6325 }
6326 7 ▶ if ($rest =~ /\A[0-7]+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /\A[0-7]+\z/) 5 2 7
6327 5 $_SH_UMASK = oct($rest) & 0777;
6328 5 umask($_SH_UMASK); # honoured on Unix, harmless where it is not
6329 5 $LAST_STATUS = 0;
6330 5 return 0;
6331 }
6332 # Symbolic mode: "u=rwx,g=rx,o=rx" (and +/- ops). The operand names the
6333 # permissions to LEAVE enabled; the mask is their complement.
6334 2 my $sym = _umask_apply_symbolic($_SH_UMASK, $rest);
6335 2 ▶ if (defined $sym) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sym) 2 0 2
6336 2 $_SH_UMASK = $sym;
6337 2 umask($_SH_UMASK);
6338 2 $LAST_STATUS = 0;
6339 2 return 0;
6340 }
6341 0 print STDERR "sh: umask: $rest: invalid mask\n";
6342 0 $LAST_STATUS = 1;
6343 0 return 1;
6344 }
6345  
6346 # _umask_apply_symbolic(MASK, SPEC): apply a chmod-style symbolic SPEC
6347 # ("[ugoa]*[-+=][rwx]*", comma-separated) to the numeric umask MASK, in
6348 # terms of the permissions the mask leaves ENABLED. Returns the new
6349 # numeric mask, or undef when SPEC is not valid symbolic syntax.
6350 2 ▶sub _umask_apply_symbolic {
Subroutine _umask_apply_symbolic: called CC=14 SCAR=27.6
6351 2 my ($mask, $spec) = @_;
6352 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
6353 2 my @allow = (
6354 (~ (($mask >> 6) & 7)) & 7,
6355 (~ (($mask >> 3) & 7)) & 7,
6356 (~ ($mask & 7)) & 7,
6357 );
6358 2 my %idx = ('u' => 0, 'g' => 1, 'o' => 2);
6359 2 for my $clause (split /,/, $spec) {
6360 4 ▶ return undef unless $clause =~ /\A([ugoa]*)([-+=])([rwx]*)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $clause =~ /\A([ugoa]*)([-+=])([rwx]*)\z/ 0 4 4
6361 4 my ($who, $op, $pstr) = ($1, $2, $3);
6362 4 my $perm = 0;
6363 4 ▶ $perm |= 4 if $pstr =~ /r/;
Branch: 1 branch
BranchTrueFalseTotal
if $pstr =~ /r/ 2 2 4
6364 4 ▶ $perm |= 2 if $pstr =~ /w/;
Branch: 1 branch
BranchTrueFalseTotal
if $pstr =~ /w/ 1 3 4
6365 4 ▶ $perm |= 1 if $pstr =~ /x/;
Branch: 1 branch
BranchTrueFalseTotal
if $pstr =~ /x/ 1 3 4
6366 4 my @classes;
6367 0 ▶ 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
6368 else {
6369 4 for my $w (split //, $who) { push @classes, $idx{$w} }
6370 }
6371 4 for my $ci (@classes) {
6372 4 ▶ if ($op eq '=') { $allow[$ci] = $perm }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq '=') { } 3 1 4
elsif ($op eq '+') { } 0 1 1
6373 0 elsif ($op eq '+') { $allow[$ci] |= $perm }
6374 1 else { $allow[$ci] &= (~ $perm) & 7 }
6375 }
6376 }
6377 2 my $newmask = 0;
6378 2 $newmask |= ((~ $allow[0]) & 7) << 6;
6379 2 $newmask |= ((~ $allow[1]) & 7) << 3;
6380 2 $newmask |= (~ $allow[2]) & 7;
6381 2 return $newmask & 0777;
6382 }
6383  
6384 # _umask_symbolic(MASK): render a numeric umask as the bash-style symbolic
6385 # form "u=rwx,g=rx,o=rx" (the permissions the mask LEAVES enabled).
6386 1 ▶sub _umask_symbolic {
Subroutine _umask_symbolic: called CC=5 SCAR=16.2
6387 1 my ($mask) = @_;
6388 1 my @cls = ('u', 'g', 'o');
6389 1 my @sh = (($mask >> 6) & 7, ($mask >> 3) & 7, $mask & 7);
6390 1 my @out;
6391 1 for my $i (0 .. 2) {
6392 3 my $allow = (~ $sh[$i]) & 7;
6393 3 my $s = '';
6394 3 ▶ $s .= 'r' if $allow & 4;
Branch: 1 branch
BranchTrueFalseTotal
if $allow & 4 3 0 3
6395 3 ▶ $s .= 'w' if $allow & 2;
Branch: 1 branch
BranchTrueFalseTotal
if $allow & 2 1 2 3
6396 3 ▶ $s .= 'x' if $allow & 1;
Branch: 1 branch
BranchTrueFalseTotal
if $allow & 1 3 0 3
6397 3 push @out, $cls[$i] . '=' . $s;
6398 }
6399 1 return join(',', @out);
6400 }
6401  
6402 # hash [-r] [-l] [-t] [-d name] [-p path name] [NAME ...]
6403 # BATsh resolves commands through PATH on every call and keeps no
6404 # location cache, so the maintenance forms (-r clear, -l/-t list, -d/-p)
6405 # are accepted as successful no-ops. "hash NAME ..." verifies each NAME
6406 # is found on PATH (status 1 if any is missing), matching the observable
6407 # result of bash caching a lookup.
6408 3 ▶sub _cmd_hash {
Subroutine _cmd_hash: called CC=8 SCAR=21.0
6409 3 my ($rest) = @_;
6410 3 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 3 3
6411 3 $rest =~ s/\A\s+//; $rest =~ s/\s+\z//;
6412 3 ▶ return _sh_set_status(0) if $rest eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $rest eq "" 0 3 3
6413 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
6414 2 ▶ return _sh_set_status(0) if $rest =~ /\A-[dp]\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $rest =~ /\A-[dp]\b/ 0 2 2
6415  
6416 2 my @tok = map { _arr_dequote($_) } _arr_split_words($rest);
6417 2 my $status = 0;
6418 2 for my $name (@tok) {
6419 2 ▶ next if $name =~ /\A-/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\A-/ 0 2 2
6420 2 ▶ if (!defined _sh_find_on_path($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined _sh_find_on_path($name)) 1 1 2
6421 1 print STDERR "sh: hash: $name: not found\n";
6422 1 $status = 1;
6423 }
6424 }
6425 2 return _sh_set_status($status);
6426 }
6427  
6428 13 ▶sub _sh_set_status {
Subroutine _sh_set_status: called CC=1 SCAR=0.0
6429 13 my ($s) = @_;
6430 13 $LAST_STATUS = $s;
6431 13 return $s;
6432 }
6433  
6434 # readonly [-p] [NAME[=VALUE] ...]
6435 # Mark each NAME read-only (optionally assigning VALUE first). With no
6436 # NAME, or with -p, list the current readonly variables in a form that
6437 # can be re-read.
6438 5 ▶sub _cmd_readonly {
Subroutine _cmd_readonly: called CC=12 SCAR=34.0
6439 5 my ($class, $rest) = @_;
6440 5 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 5 5
6441 5 $rest =~ s/\A\s+//; $rest =~ s/\s+\z//;
6442  
6443 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
6444 0 for my $ik (sort keys %_SH_READONLY) {
6445 0 my $v = BATsh::Env->get($ik);
6446 0 ▶ $v = '' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
6447 0 $v =~ s/'/'\\''/g;
6448 0 print "readonly $ik='$v'\n";
6449 }
6450 0 return _sh_set_status(0);
6451 }
6452  
6453 5 my @tok = map { _arr_dequote($_) } _arr_split_words($rest);
6454 5 my $status = 0;
6455 5 for my $t (@tok) {
6456 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
6457 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
6458 5 my ($name, $val) = ($1, $2);
6459 5 ▶ if (_sh_is_readonly($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_sh_is_readonly($name)) 0 5 5
6460 0 print STDERR "sh: $name: readonly variable\n";
6461 0 $status = 1;
6462 0 next;
6463 }
6464 5 BATsh::Env->set($name, $val);
6465 5 $_SH_READONLY{ uc($name) } = 1;
6466 }
6467 elsif ($t =~ /\A([A-Za-z_][A-Za-z0-9_]*)\z/) {
6468 0 $_SH_READONLY{ uc($1) } = 1;
6469 }
6470 }
6471 5 return _sh_set_status($status);
6472 }
6473  
6474 # mapfile / readarray [-t] [-d DELIM] [-n COUNT] [-O ORIGIN] [-s SKIP] [ARRAY]
6475 # Read lines from standard input into the indexed array ARRAY (default
6476 # MAPFILE). -t strips the trailing delimiter from each line; -d sets the
6477 # line delimiter (default newline); -n COUNT copies at most COUNT lines
6478 # (0 = all); -s SKIP discards the first SKIP lines; -O ORIGIN stores the
6479 # first line at index ORIGIN. STDIN is whatever the surrounding
6480 # redirection supplied (e.g. "mapfile arr < file").
6481 5 ▶sub _cmd_mapfile {
Subroutine _cmd_mapfile: called CC=31 SCAR=40.4
6482 5 my ($class, $rest) = @_;
6483 5 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 5 5
6484 5 my @tok = map { _arr_dequote($_) } _arr_split_words($rest);
6485  
6486 5 my $strip = 0;
6487 5 my $delim = "\n";
6488 5 my $count = 0; # 0 = unlimited
6489 5 my $skip = 0;
6490 5 my $origin = 0;
6491 5 ▶ while (@tok) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tok) 13 0 13
6492 13 my $t = $tok[0];
6493 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
6494 1 ▶ elsif ($t eq '-n') { shift @tok; $count = @tok ? int(_arr_num(shift @tok)) : 0 }
Branch: 1 branch
BranchTrueFalseTotal
@tok ? : 1 0 1
6495 1 ▶ elsif ($t eq '-s') { shift @tok; $skip = @tok ? int(_arr_num(shift @tok)) : 0 }
Branch: 1 branch
BranchTrueFalseTotal
@tok ? : 1 0 1
6496 1 ▶ elsif ($t eq '-O') { shift @tok; $origin = @tok ? int(_arr_num(shift @tok)) : 0 }
Branch: 1 branch
BranchTrueFalseTotal
@tok ? : 1 0 1
6497 0 ▶ elsif ($t eq '-d') { shift @tok; $delim = @tok ? shift @tok : "\n" }
Branch: 1 branch
BranchTrueFalseTotal
@tok ? : 0 0 0
6498 0 elsif ($t =~ /\A-d(.+)\z/s) { shift @tok; $delim = $1 }
6499 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
6500 0 elsif ($t =~ /\A-/) { shift @tok } # unknown flag: ignore
6501 5 else { last }
6502 }
6503 5 ▶ my $name = @tok ? shift @tok : 'MAPFILE';
Branch: 1 branch
BranchTrueFalseTotal
@tok ? : 5 0 5
6504 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
6505  
6506 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
6507 5 my @lines;
6508 {
6509 5 local $/ = $sep;
6510 5 ▶ while (defined(my $l = <STDIN>)) { push @lines, $l }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $l = readline STDIN)) 15 5 20
6511 }
6512  
6513 5 ▶ if ($skip > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip > 0) 1 4 5
6514 0 ▶ if ($skip >= @lines) { @lines = () }
Branch: 1 branch
BranchTrueFalseTotal
if ($skip >= @lines) { } 0 1 1
6515 1 else { splice(@lines, 0, $skip) }
6516 }
6517 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
6518 5 ▶ if ($strip) { for my $l (@lines) { $l =~ s/\Q$sep\E\z// } }
Branch: 1 branch
BranchTrueFalseTotal
if ($strip) 5 0 5
6519 5 @lines = map { BATsh::MB::enc($_) } @lines;
6520  
6521 5 my $k = _arr_name($name);
6522 5 $_SH_ARRAY{$k} = {};
6523 5 $_SH_ARRAY_TYPE{$k} = 'indexed';
6524 5 BATsh::Env->unset($name);
6525 5 my $ix = $origin;
6526 5 for my $l (@lines) { $_SH_ARRAY{$k}{$ix} = $l; $ix++ }
6527  
6528 5 return _sh_set_status(0);
6529 }
6530  
6531 # _arr_num(S): integer value of a leading numeric prefix (0 otherwise).
6532 3 ▶sub _arr_num {
Subroutine _arr_num: called CC=3 SCAR=11.7
6533 3 my ($s) = @_;
6534 3 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 3 3
6535 3 ▶ return ($s =~ /\A\s*([-+]?\d+)/) ? int($1) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /\A\s*([-+]?\d+)/ ? : 3 0 3
6536 }
6537  
6538  
6539 # Rules:
6540 # * only the last non-space character may be the background &
6541 # * it must not be part of && (i.e. preceding char must not be &)
6542 # * it must not be an fd-duplication >& (preceding char must not be >)
6543 # * it must be outside single/double quotes
6544 # * the remaining command must be non-empty
6545 1751 ▶sub _split_trailing_bg {
Subroutine _split_trailing_bg: called CC=22 SCAR=34.7
6546 1751 my ($line) = @_;
6547 1751 ▶ return (0, $line) unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 1751 1751
6548  
6549 # Find the index of the last character, ignoring trailing whitespace.
6550 1751 my $rtrim = $line;
6551 1751 $rtrim =~ s/\s+\z//;
6552 1751 ▶ return (0, $line) if $rtrim eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $rtrim eq "" 0 1751 1751
6553  
6554 1751 my @chars = split //, $rtrim;
6555 1751 my $last = $#chars;
6556 1751 ▶ return (0, $line) unless $chars[$last] eq '&';
Branch: 1 branch
BranchTrueFalseTotal
unless $chars[$last] eq "&" 1739 12 1751
6557  
6558 # && is a compound operator, not background.
6559 12 ▶ return (0, $line) if $last >= 1 && $chars[$last-1] eq '&';
Branch: 1 branch
BranchTrueFalseTotal
if $last >= 1 and $chars[$last - 1] eq "&" 0 12 12
Condition66%2/3
$last >= 1 and $chars[$last - 1] eq "&"
!ll&&!rl&&r
1110
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
6560 # >& is fd duplication, not background.
6561 12 ▶ return (0, $line) if $last >= 1 && $chars[$last-1] eq '>';
Branch: 1 branch
BranchTrueFalseTotal
if $last >= 1 and $chars[$last - 1] eq ">" 0 12 12
Condition66%2/3
$last >= 1 and $chars[$last - 1] eq ">"
!ll&&!rl&&r
1110
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
6562  
6563 # Verify the trailing & is outside quotes and not backslash-escaped by
6564 # scanning up to it. $esc_last becomes true if the char at $last is
6565 # escaped by an immediately preceding (unquoted) backslash.
6566 12 my $in_sq = 0;
6567 12 my $in_dq = 0;
6568 12 my $esc_last = 0;
6569 12 my $i = 0;
6570 12 ▶ while ($i < $last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $last) 144 12 156
6571 144 my $ch = $chars[$i];
6572 144 ▶ if ($in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq) 0 144 144
6573 0 ▶ $in_sq = 0 if $ch eq "'";
Branch: 1 branch
BranchTrueFalseTotal
if $ch eq "'" 0 0 0
6574 0 $i++; next;
6575 }
6576 144 ▶ if ($ch eq '\\' && !$in_sq) { # backslash escapes next char
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\" and not $in_sq) 1 143 144
Condition66%2/3
$ch eq "\\" and not $in_sq
!ll&&!rl&&r
14301
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
6577 1 ▶ $esc_last = 1 if $i + 1 == $last;
Branch: 1 branch
BranchTrueFalseTotal
if $i + 1 == $last 1 0 1
6578 1 $i += 2; next;
6579 }
6580 0 ▶ if ($ch eq "'" && !$in_dq) { $in_sq = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $in_dq) 0 143 143
Condition33%1/3
$ch eq "'" and not $in_dq
!ll&&!rl&&r
14300
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
6581 143 ▶ if ($ch eq '"' && !$in_sq) { $in_dq = !$in_dq; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" and not $in_sq) 2 141 143
Condition66%2/3
$ch eq "\"" and not $in_sq
!ll&&!rl&&r
14102
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
6582 141 $i++;
6583 }
6584 12 ▶ return (0, $line) if $in_sq || $in_dq; # & is inside a quote
Branch: 1 branch
BranchTrueFalseTotal
if $in_sq or $in_dq 0 12 12
Condition33%1/3
$in_sq or $in_dq
l!l&&r!l&&!r
0012
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
6585 12 ▶ return (0, $line) if $esc_last; # & is backslash-escaped
Branch: 1 branch
BranchTrueFalseTotal
if $esc_last 1 11 12
6586  
6587 # Strip the trailing & (and surrounding whitespace before it).
6588 11 my $stripped = join('', @chars[0 .. $last-1]);
6589 11 $stripped =~ s/\s+\z//;
6590 11 ▶ return (0, $line) if $stripped eq ''; # nothing to run
Branch: 1 branch
BranchTrueFalseTotal
if $stripped eq "" 1 10 11
6591  
6592 10 return (1, $stripped);
6593 }
6594  
6595 # _sh_word_is_foreground: true when the first word of a backgrounded line
6596 # is a BATsh builtin, defined SH function, control keyword, or a variable
6597 # assignment. Such commands run in the foreground and the trailing &
6598 # is ignored (documented limitation: only external commands background).
6599 10 ▶sub _sh_word_is_foreground {
Subroutine _sh_word_is_foreground: called CC=12 SCAR=29.9
6600 10 my ($w) = @_;
6601 10 ▶ return 0 unless defined $w && $w ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $w and $w ne "" 0 10 10
Condition33%1/3
defined $w and $w ne ""
!ll&&!rl&&r
0010
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
6602  
6603 # VAR=value assignment
6604 10 ▶ 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_]*=/ 3 7 10
6605  
6606 # test bracket and no-op
6607 7 ▶ return 1 if $w eq '[' || $w eq ':' || $w eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $w eq "[" or $w eq ":" or $w eq "." 0 7 7
Condition33%2/6
$w eq "[" or $w eq ":" or $w eq "."
l!l&&r!l&&!r
007
$w eq "[" or $w eq ":"
l!l&&r!l&&!r
007
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
6608  
6609 # Everything else is classified by _sh_name_kind(), which is the single
6610 # authoritative table of aliases, keywords, functions and builtins in
6611 # this module (it is also what "type" and "command -v" report). This
6612 # used to be a second, hand-maintained copy of that table, and the copy
6613 # had fallen behind: "declare", "shopt", "alias", "unalias", "exec",
6614 # "trap", "getopts", "eval" and "typeset" were missing from it, so
6615 # "declare -i n=1 &" was treated as an external command and handed to a
6616 # real /bin/sh -- which fails outright on a machine that has no external
6617 # shell, the very situation this interpreter exists to cover. Deriving
6618 # the answer from one table instead of two removes that failure mode.
6619 7 my ($kind) = _sh_name_kind($w);
6620 7 ▶ return 1 if $kind eq 'alias'
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "alias" or $kind eq "keyword" or $kind eq "function" or $kind eq "builtin" 5 2 7
Condition44%4/9
$kind eq "alias" or $kind eq "keyword" or $kind eq "function" or $kind eq "builtin"
l!l&&r!l&&!r
052
$kind eq "alias" or $kind eq "keyword" or $kind eq "function"
l!l&&r!l&&!r
007
$kind eq "alias" or $kind eq "keyword"
l!l&&r!l&&!r
007
MC/DC25%1/4
$kind eq "alias" or $kind eq "keyword" or $kind eq "function" or $kind eq "builtin"
$kind eq "alias" $kind eq "keyword" $kind eq "function" $kind eq "builtin"
Truth table40%2/5
A or B or C or D
A= $kind eq "alias"B= $kind eq "keyword"C= $kind eq "function"D= $kind eq "builtin"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
6621 || $kind eq 'keyword'
6622 || $kind eq 'function'
6623 || $kind eq 'builtin';
6624  
6625 # A few block-structure words are not names and so are not reported by
6626 # _sh_name_kind(); they are listed here for defensiveness only (they are
6627 # normally consumed by the block parsers in _run_lines).
6628 2 my %kw = (
6629 then => 1, 'else' => 1, elif => 1, fi => 1,
6630 'do' => 1, done => 1, esac => 1,
6631 );
6632 2 ▶ return 1 if $kw{lc($w)};
Branch: 1 branch
BranchTrueFalseTotal
if $kw{lc $w} 0 2 2
6633  
6634 2 return 0;
6635 }
6636  
6637 # _bg_tempfile: create a unique, empty temp file (O_CREAT|O_EXCL to avoid
6638 # symlink races) for capturing a background job's PID on Unix-like systems.
6639 # Returns the path, or undef on failure.
6640 1 ▶sub _bg_tempfile {
Subroutine _bg_tempfile: called CC=11 SCAR=29.3
6641 1 ▶ my $dir = $ENV{'TMPDIR'} || $ENV{'TEMP'} || $ENV{'TMP'} || '';
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
6642 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
6643 1 ▶ $dir = '.' if $dir eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq "" 0 1 1
6644 1 $dir =~ s{[\\/]+\z}{};
6645 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
6646  
6647 1 my $attempt = 0;
6648 1 ▶ while ($attempt < 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attempt < 1000) 1 0 1
6649 1 $_BG_SEQ++;
6650 1 $attempt++;
6651 1 my $path = $dir . '/' . 'batsh_bg_' . $$ . '_' . $_BG_SEQ;
6652 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
6653 1 close(_BG_TMP);
6654 1 push @_BG_TMPFILES, $path;
6655 1 return $path;
6656 }
6657 # EEXIST or transient error: retry with next sequence number
6658 }
6659 0 warn "sh: cannot create background pidfile in $dir: $!\n";
6660 0 return undef;
6661 }
6662  
6663 # _bg_launch: start $cmdline asynchronously.
6664 # Win32 : system(1, STRING) spawns via the command shell (P_NOWAIT)
6665 # and returns the PID directly.
6666 # Unix-like : delegate to /bin/sh so the job is backgrounded without a
6667 # Perl fork; the shell's $! (the job PID) is written to a
6668 # temp file and read back into BATsh's own $!.
6669 # On a successful launch $? (LAST_STATUS) is 0; the exit code of the
6670 # background job itself is not awaited (sh semantics).
6671 # _bg_launch: un-guard the command line, then hand it to the platform-
6672 # specific spawner below (system(1,...) on Win32, "&" on POSIX).
6673 1 ▶sub _bg_launch {
Subroutine _bg_launch: called CC=1 SCAR=0.0
6674 1 my ($class, $cmdline) = @_;
6675 1 return _bg_launch_decoded($class, BATsh::MB::dec($cmdline));
6676 }
6677  
6678 1 ▶sub _bg_launch_decoded {
Subroutine _bg_launch_decoded: called CC=13 SCAR=33.1
6679 1 my ($class, $cmdline) = @_;
6680 1 ▶ $cmdline = '' unless defined $cmdline;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cmdline 0 1 1
6681 1 ▶ return 0 if $cmdline =~ /\A\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $cmdline =~ /\A\s*\z/ 0 1 1
6682 1 BATsh::Env->sync_to_env();
6683  
6684 1 ▶ if ($^O =~ /MSWin32/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /MSWin32/i) 0 1 1
6685 0 my $pid = system(1, $cmdline);
6686 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
6687 0 $_LAST_BG_PID = $pid;
6688 0 $LAST_STATUS = 0;
6689 }
6690 else {
6691 0 warn "sh: failed to start background process\n";
6692 0 $LAST_STATUS = 1;
6693 }
6694 0 return $LAST_STATUS;
6695 }
6696  
6697 # Unix-like
6698 1 my $pidfile = _bg_tempfile();
6699 1 my $rc;
6700 1 ▶ if (defined $pidfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pidfile) { } 1 0 1
6701 # Group the command so that the whole list (pipelines, &&, ...) is
6702 # backgrounded as a unit, then echo the job PID ($!) to the file.
6703 1 $rc = system("{ $cmdline ; } & echo \$! > '$pidfile'");
6704 1 ▶ if (open(_BG_PIDFH, "< $pidfile")) {
Branch: 1 branch
BranchTrueFalseTotal
if (open _BG_PIDFH, "< $pidfile") 1 0 1
6705 1 local $/;
6706 1 my $buf = <_BG_PIDFH>;
6707 1 close(_BG_PIDFH);
6708 1 ▶ $buf = '' unless defined $buf;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $buf 0 1 1
6709 1 my $pid = '';
6710 1 ($pid) = ($buf =~ /(\d+)/);
6711 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
6712 }
6713 1 unlink $pidfile;
6714 1 @_BG_TMPFILES = grep { $_ ne $pidfile } @_BG_TMPFILES;
6715 }
6716 else {
6717 0 $rc = system("{ $cmdline ; } &");
6718 }
6719 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
6720 1 return $LAST_STATUS;
6721 }
6722  
6723  
6724 # ----------------------------------------------------------------
6725 # Split "cmd rest" honouring quoted strings
6726 # ----------------------------------------------------------------
6727 2236 ▶sub _split_sh {
Subroutine _split_sh: called CC=2 SCAR=7.6
6728 2236 my ($line) = @_;
6729 2236 ▶ if ($line =~ /\A(\S+)\s*(.*)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A(\S+)\s*(.*)\z/s) 2236 0 2236
6730 2236 return ($1, $2);
6731 }
6732 0 return ($line, '');
6733 }
6734  
6735 # ----------------------------------------------------------------
6736 # _sh_assign_prefix: detect POSIX assignment prefixes on a RAW (un-expanded)
6737 # command line, e.g. `IFS= read -r LINE` or `LC_ALL=C sort file`.
6738 #
6739 # Parses one or more leading VAR=VALUE words, where VALUE is read with
6740 # quote / $(...) / backtick awareness so that an assignment whose value
6741 # merely *contains* spaces (e.g. UPPER=$(echo "a b")) is NOT mistaken for
6742 # a prefix followed by a command.
6743 #
6744 # Returns (\@pairs, $remainder):
6745 # - @pairs is a list of [VAR, RAW_VALUE] (value still un-expanded)
6746 # - $remainder is the rest of the line (the command to run), '' if none
6747 # Returns () when the line does not begin with an assignment.
6748 # ----------------------------------------------------------------
6749 1570 ▶sub _sh_assign_prefix {
Subroutine _sh_assign_prefix: called CC=50 SCAR=39.5
6750 1570 my ($line) = @_;
6751 1570 my @chars = split //, $line;
6752 1570 my $n = scalar @chars;
6753 1570 my $i = 0;
6754 1570 my @pairs = ();
6755  
6756 1570 while (1) {
6757 # Skip leading spaces between successive assignments.
6758 1570 ▶ $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 1570 1570
Condition33%2/6
$i < $n and $chars[$i] eq " " || $chars[$i] eq "\t"
!ll&&!rl&&r
015700
$chars[$i] eq " " or $chars[$i] eq "\t"
l!l&&r!l&&!r
001570
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
6759 1570 ▶ last if $i >= $n;
Branch: 1 branch
BranchTrueFalseTotal
if $i >= $n 0 1570 1570
6760  
6761 # Match a variable name followed by '='.
6762 1570 my $j = $i;
6763 1570 my $name = '';
6764 1570 ▶ if ($chars[$j] =~ /[A-Za-z_]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chars[$j] =~ /[A-Za-z_]/) 1489 81 1570
6765 1489 $name .= $chars[$j]; $j++;
6766 1489 ▶ 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_]/) 4424 1489 5913
Condition100%3/3
$j < $n and $chars[$j] =~ /[A-Za-z0-9_]/
!ll&&!rl&&r
8014094424
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
6767 }
6768 1570 ▶ last unless length($name) && $j < $n && $chars[$j] eq '=';
Branch: 1 branch
BranchTrueFalseTotal
unless length $name and $j < $n and $chars[$j] eq "=" 1300 270 1570
Condition100%6/6
length $name and $j < $n and $chars[$j] eq "="
!ll&&!rl&&r
1611139270
length $name and $j < $n
!ll&&!rl&&r
81801409
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
6769 270 $j++; # consume '='
6770  
6771 # Read the value with quote / $() / backtick awareness.
6772 270 my $val = '';
6773 270 my $in_sq = 0;
6774 270 my $in_dq = 0;
6775 270 my $depth = 0; # $( ) nesting
6776 270 my $in_bt = 0; # backticks
6777 270 ▶ while ($j < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 2109 260 2369
6778 2109 my $c = $chars[$j];
6779 2109 ▶ if ($in_sq) { $val .= $c; $in_sq = 0 if $c eq "'"; $j++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($in_sq) 17 2092 2109
if $c eq "'" 5 12 17
6780 # Backslash escape (outside single quotes): the backslash and
6781 # the char it protects are both kept verbatim -- so "\ " does
6782 # not end the value word. _arr_dequote() resolves them later.
6783 2092 ▶ if ($c eq "\\" && !$in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\\" and not $in_sq) 7 2085 2092
Condition66%2/3
$c eq "\\" and not $in_sq
!ll&&!rl&&r
208507
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
6784 7 $val .= $c; $j++;
6785 7 ▶ $val .= $chars[$j] if $j < $n;
Branch: 1 branch
BranchTrueFalseTotal
if $j < $n 7 0 7
6786 7 $j++; next;
6787 }
6788 2085 ▶ 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 2080 2085
Condition83%5/6
$c eq "'" and not $in_dq and not $in_bt
!ll&&!rl&&r
208005
$c eq "'" and not $in_dq
!ll&&!rl&&r
207825
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
6789 2080 ▶ if ($c eq '"' && !$in_bt) { $in_dq = !$in_dq; $val .= $c; $j++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\"" and not $in_bt) 118 1962 2080
Condition66%2/3
$c eq "\"" and not $in_bt
!ll&&!rl&&r
19620118
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
6790 1962 ▶ 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 1960 1962
Condition100%3/3
not $in_dq and $c eq "`"
!ll&&!rl&&r
55414062
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
6791 1960 ▶ 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 "(") 95 1865 1960
Condition91%11/12
not $in_dq || $in_bt and $c eq "\$" and $j + 1 < $n and $chars[$j + 1] eq "("
!ll&&!rl&&r
1859695
not $in_dq || $in_bt and $c eq "\$" and $j + 1 < $n
!ll&&!rl&&r
18590101
not $in_dq || $in_bt and $c eq "\$"
!ll&&!rl&&r
5641295101
$in_dq || $in_bt
l!l&&r!l&&!r
554101396
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
6792 95 $depth++; $val .= '$('; $j += 2; next;
6793 }
6794 1865 ▶ 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 ")") 95 1770 1865
Condition100%9/9
not $in_dq || $in_bt and $depth > 0 and $c eq ")"
!ll&&!rl&&r
99277895
not $in_dq || $in_bt and $depth > 0
!ll&&!rl&&r
564428873
$in_dq || $in_bt
l!l&&r!l&&!r
554101301
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
6795 95 $depth--; $val .= ')'; $j++; next;
6796 }
6797 1770 ▶ 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") 10 1760 1770
Condition86%13/15
not $in_sq || $in_dq || $in_bt and $depth == 0 and $c eq " " || $c eq "\t"
!ll&&!rl&&r
134241810
not $in_sq || $in_dq || $in_bt and $depth == 0
!ll&&!rl&&r
564778428
$in_sq || $in_dq || $in_bt
l!l&&r!l&&!r
554101206
$in_sq or $in_dq
l!l&&r!l&&!r
05541216
$c eq " " or $c eq "\t"
l!l&&r!l&&!r
100418
MC/DC66%4/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 table71%5/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
6798 && ($c eq ' ' || $c eq "\t")) {
6799 10 last; # end of this value word
6800 }
6801 1760 $val .= $c; $j++;
6802 }
6803  
6804 270 push @pairs, [$name, $val];
6805 270 $i = $j;
6806  
6807 # Peek: is there a following non-space token?
6808 270 my $k = $i;
6809 270 ▶ $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 } 10 270 280
Condition83%5/6
$k < $n and $chars[$k] eq " " || $chars[$k] eq "\t"
!ll&&!rl&&r
265510
$chars[$k] eq " " or $chars[$k] eq "\t"
l!l&&r!l&&!r
1005
MC/DC66%2/3
$k < $n and $chars[$k] eq " " || $chars[$k] eq "\t"
$k < $n $chars[$k] eq " " $chars[$k] eq "\t"
Truth table75%3/4
A and B or C
A= $k < $nB= $chars[$k] eq " "C= $chars[$k] eq "\t"
ABCresult
0XX0
1000
11X1
1011
6810 270 ▶ last if $k >= $n; # nothing follows: trailing pure assignment(s)
Branch: 1 branch
BranchTrueFalseTotal
if $k >= $n 265 5 270
6811  
6812 # Is the next token another assignment? If so, loop; otherwise the
6813 # remainder is the command.
6814 5 my $m = $k;
6815 5 my $nm2 = '';
6816 5 ▶ if ($chars[$m] =~ /[A-Za-z_]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chars[$m] =~ /[A-Za-z_]/) 5 0 5
6817 5 $nm2 .= $chars[$m]; $m++;
6818 5 ▶ 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_]/) 15 5 20
Condition100%3/3
$m < $n and $chars[$m] =~ /[A-Za-z0-9_]/
!ll&&!rl&&r
1415
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
6819 }
6820 5 ▶ if (length($nm2) && $m < $n && $chars[$m] eq '=') {
Branch: 1 branch
BranchTrueFalseTotal
if (length $nm2 and $m < $n and $chars[$m] eq "=") 0 5 5
Condition66%4/6
length $nm2 and $m < $n and $chars[$m] eq "="
!ll&&!rl&&r
140
length $nm2 and $m < $n
!ll&&!rl&&r
014
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
6821 0 $i = $k; # next assignment; continue the loop
6822 0 next;
6823 }
6824  
6825 # Remainder is a command.
6826 5 my $remainder = join('', @chars[$k .. $n-1]);
6827 5 return (\@pairs, $remainder);
6828 }
6829  
6830 # No command remainder: either not an assignment at all, or pure
6831 # assignment(s) which the normal post-expansion path handles.
6832 1565 ▶ return () unless @pairs;
Branch: 1 branch
BranchTrueFalseTotal
unless @pairs 1300 265 1565
6833 265 return (\@pairs, '');
6834 }
6835  
6836 # ----------------------------------------------------------------
6837 # Here-document support (Perl 5.005_03 compatible)
6838 # ----------------------------------------------------------------
6839 # _hd_detect: scan a command line for an *unquoted* << operator.
6840 # Returns () if none found, otherwise:
6841 # ($cmd_part, $dash, $delim, $quoted)
6842 # where $cmd_part is the command with the "<< DELIM" token removed
6843 # (text after the delimiter, e.g. trailing redirections, is preserved),
6844 # $dash is 1 for <<- (strip leading tabs), $delim is the delimiter word,
6845 # and $quoted is 1 when the delimiter was quoted (suppresses expansion).
6846 2750 ▶sub _hd_detect {
Subroutine _hd_detect: called CC=43 SCAR=38.0
6847 2750 my ($line) = @_;
6848 2750 my @chars = split //, $line;
6849 2750 my $n = scalar @chars;
6850 2750 my $in_sq = 0;
6851 2750 my $in_dq = 0;
6852 2750 my $arith = 0; # inside $(( ... )): << there is a shift, not a heredoc
6853 2750 my $i = 0;
6854  
6855 2750 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 52010 2724 54734
6856 52010 my $ch = $chars[$i];
6857  
6858 52010 ▶ if ($in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq) 1248 50762 52010
6859 1248 ▶ $in_sq = 0 if $ch eq "'";
Branch: 1 branch
BranchTrueFalseTotal
if $ch eq "'" 128 1120 1248
6860 1248 $i++; next;
6861 }
6862 50762 ▶ if ($ch eq "'" && !$in_dq) { $in_sq = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'" and not $in_dq) 128 50634 50762
Condition100%3/3
$ch eq "'" and not $in_dq
!ll&&!rl&&r
5062212128
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
6863 50634 ▶ if ($ch eq '"' && !$in_sq) { $in_dq = !$in_dq; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" and not $in_sq) 1956 48678 50634
Condition66%2/3
$ch eq "\"" and not $in_sq
!ll&&!rl&&r
4867801956
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
6864 48678 ▶ if ($ch eq '\\') { $i += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 43 48635 48678
6865  
6866 # Arithmetic expansion $(( ... )): skip its body entirely
6867 48635 ▶ 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 "(") 194 48441 48635
Condition100%9/9
$ch eq "\$" and $i + 2 < $n and $chars[$i + 1] eq "(" and $chars[$i + 2] eq "("
!ll&&!rl&&r
4838655194
$ch eq "\$" and $i + 2 < $n and $chars[$i + 1] eq "("
!ll&&!rl&&r
47542844249
$ch eq "\$" and $i + 2 < $n
!ll&&!rl&&r
473591831093
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
6868 194 $arith++; $i += 3; next;
6869 }
6870 48441 ▶ 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 ")") 194 48247 48441
Condition88%8/9
$arith and $ch eq ")" and $i + 1 < $n and $chars[$i + 1] eq ")"
!ll&&!rl&&r
482452194
$arith and $ch eq ")" and $i + 1 < $n
!ll&&!rl&&r
482450196
$arith and $ch eq ")"
!ll&&!rl&&r
469761269196
MC/DC75%3/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 table80%4/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
6871 194 $arith--; $i += 2; next;
6872 }
6873 48247 ▶ if ($arith) { $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($arith) 1271 46976 48247
6874  
6875 # Unquoted << starts a here-document; unquoted <<< is a
6876 # here-string (v0.07: handled later, after expansion, by
6877 # _sh_strip_herestring -- skip all three characters here so the
6878 # scan does not re-match starting at the second '<').
6879 46976 ▶ 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 46972 46976
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
46946264
not $in_dq and $ch eq "<" and $i + 1 < $n and $chars[$i + 1] eq "<" and $i + 2 < $n
!ll&&!rl&&r
46946030
not $in_dq and $ch eq "<" and $i + 1 < $n and $chars[$i + 1] eq "<"
!ll&&!rl&&r
469242230
not $in_dq and $ch eq "<" and $i + 1 < $n
!ll&&!rl&&r
46924052
not $in_dq and $ch eq "<"
!ll&&!rl&&r
152583166652
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
6880 && $i+2 < $n && $chars[$i+2] eq '<') {
6881 4 $i += 3; next;
6882 }
6883 46972 ▶ 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 46946 46972
Condition88%8/9
not $in_dq and $ch eq "<" and $i + 1 < $n and $chars[$i + 1] eq "<"
!ll&&!rl&&r
469242226
not $in_dq and $ch eq "<" and $i + 1 < $n
!ll&&!rl&&r
46924048
not $in_dq and $ch eq "<"
!ll&&!rl&&r
152583166648
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
6884 26 my $cmd_part = join('', @chars[0 .. $i-1]);
6885 26 my $j = $i + 2;
6886 26 my $dash = 0;
6887 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
6888 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
6889  
6890 26 my $quoted = 0;
6891 26 my $q = '';
6892 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
Condition66%4/6
$j < $n and $chars[$j] eq "'" || $chars[$j] eq "\""
!ll&&!rl&&r
0242
$chars[$j] eq "'" or $chars[$j] eq "\""
l!l&&r!l&&!r
2024
MC/DC33%1/3
$j < $n and $chars[$j] eq "'" || $chars[$j] eq "\""
$j < $n $chars[$j] eq "'" $chars[$j] eq "\""
Truth table50%2/4
A and B or C
A= $j < $nB= $chars[$j] eq "'"C= $chars[$j] eq "\""
ABCresult
0XX0
1000
11X1
1011
6893 2 $quoted = 1; $q = $chars[$j]; $j++;
6894 }
6895 26 my $delim = '';
6896 26 ▶ if ($quoted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quoted) { } 2 24 26
6897 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
6898 2 ▶ $j++ if $j < $n; # skip closing quote
Branch: 1 branch
BranchTrueFalseTotal
if $j < $n 2 0 2
6899 }
6900 else {
6901 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
6902 }
6903 26 ▶ return () if $delim eq ''; # malformed; treat as ordinary line
Branch: 1 branch
BranchTrueFalseTotal
if $delim eq "" 0 26 26
6904  
6905 26 my $rest = join('', @chars[$j .. $n-1]);
6906 26 $cmd_part .= $rest; # preserve trailing tokens
6907 26 return ($cmd_part, $dash, $delim, $quoted);
6908 }
6909  
6910 46946 $i++;
6911 }
6912 2724 return ();
6913 }
6914  
6915 # _hd_tempfile: write $body to a uniquely-named temp file using
6916 # sysopen(... O_CREAT|O_EXCL ...) to avoid symlink races.
6917 # Returns the path, or undef on failure.
6918 16 ▶sub _hd_tempfile {
Subroutine _hd_tempfile: called CC=11 SCAR=28.5
6919 16 my ($body) = @_;
6920  
6921 16 ▶ my $dir = $ENV{'TMPDIR'} || $ENV{'TEMP'} || $ENV{'TMP'} || '';
Condition37%3/8
$ENV{'TMPDIR'} || $ENV{'TEMP'} || $ENV{'TMP'} || ''
l!l
016
$ENV{"TMPDIR"} or $ENV{"TEMP"} or $ENV{"TMP"}
l!l&&r!l&&!r
0016
$ENV{"TMPDIR"} or $ENV{"TEMP"}
l!l&&r!l&&!r
0016
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
6922 16 ▶ $dir = '/tmp' if $dir eq '' && -d '/tmp';
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq "" and -d "/tmp" 16 0 16
Condition33%1/3
$dir eq "" and -d "/tmp"
!ll&&!rl&&r
0016
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
6923 16 ▶ $dir = '.' if $dir eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq "" 0 16 16
6924 16 $dir =~ s{[\\/]+\z}{};
6925 16 ▶ $dir = '.' if !(-d $dir && -w $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir and -w $dir 0 16 16
Condition33%1/3
-d $dir and -w $dir
!ll&&!rl&&r
0016
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
6926  
6927 16 my $attempt = 0;
6928 16 ▶ while ($attempt < 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attempt < 1000) 16 0 16
6929 16 $_HD_SEQ++;
6930 16 $attempt++;
6931 16 my $path = $dir . '/' . 'batsh_hd_' . $$ . '_' . $_HD_SEQ;
6932 16 ▶ if (sysopen(_HD_TMP, $path, O_WRONLY | O_CREAT | O_EXCL, 0600)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysopen _HD_TMP, $path, 193, 384) 16 0 16
6933 16 binmode(_HD_TMP);
6934 16 print _HD_TMP $body;
6935 16 close(_HD_TMP);
6936 16 push @_HD_TMPFILES, $path;
6937 16 return $path;
6938 }
6939 # EEXIST or transient error: retry with next sequence number
6940 }
6941 0 warn "sh: cannot create here-document temp file in $dir: $!\n";
6942 0 return undef;
6943 }
6944  
6945 # _hd_run: materialise the here-document body and run the command with
6946 # its STDIN connected to the body, reusing the existing redirect path.
6947 12 ▶sub _hd_run {
Subroutine _hd_run: called CC=5 SCAR=16.2
6948 12 my ($class, $cmd_part, $body_ref, $quoted, $opts_ref) = @_;
6949  
6950 12 my @body = @{$body_ref};
6951 12 ▶ if (!$quoted) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($quoted) 11 1 12
6952 11 for my $b (@body) { $b = _unprotect_lit(_expand($class, $b)) }
6953 }
6954 12 my $text = '';
6955 12 for my $b (@body) { $text .= BATsh::MB::dec($b) . "\n" }
6956  
6957 12 my $tmp = _hd_tempfile($text);
6958 0 ▶ if (!defined $tmp) { $LAST_STATUS = 2; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tmp) 0 12 12
6959  
6960 12 my @redir = ( [0, 0, $tmp] ); # fd=0 (stdin), append=0, source=tmp
6961 12 my $rc = _sh_exec_with_redirs($class, $cmd_part, \@redir, $opts_ref);
6962  
6963 12 unlink $tmp;
6964 12 @_HD_TMPFILES = grep { $_ ne $tmp } @_HD_TMPFILES;
6965 12 return $rc;
6966 }
6967  
6968 # Failsafe: remove any here-document temp files left behind on abnormal exit.
6969 0 ▶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
6970 64 ▶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
6971 64 ▶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
6972 0 ▶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
6973  
6974 # Failsafe: remove any background-job pidfiles left behind on abnormal exit.
6975 0 ▶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
6976  
6977  
6978 # ----------------------------------------------------------------
6979 # Array / associative-array support (v0.06)
6980 # ----------------------------------------------------------------
6981 # Array names are case-insensitive, matching the scalar store: the key
6982 # stored in %_SH_ARRAY is always the uppercased name.
6983 697 ▶sub _arr_name { return uc($_[0]) }
Subroutine _arr_name: called CC=1 SCAR=0.0
6984  
6985 520 ▶sub _arr_exists {
Subroutine _arr_exists: called CC=2 SCAR=6.9
6986 520 my ($name) = @_;
6987 520 ▶ return exists $_SH_ARRAY{ _arr_name($name) } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $BATsh::SH::_SH_ARRAY{_arr_name($name)} ? : 7 513 520
6988 }
6989  
6990 31 ▶sub _arr_is_assoc {
Subroutine _arr_is_assoc: called CC=3 SCAR=11.0
6991 31 my ($name) = @_;
6992 31 my $k = _arr_name($name);
6993 31 ▶ 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' ? : 5 26 31
Condition100%3/3
exists $BATsh::SH::_SH_ARRAY_TYPE{$k} and $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
!ll&&!rl&&r
2245
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
6994 }
6995  
6996 # Evaluate an indexed-array subscript as an integer. Plain integers are
6997 # taken verbatim; anything else is run through the arithmetic evaluator so
6998 # that subscripts such as "1+1" or a bare variable name work like bash.
6999 24 ▶sub _arr_index {
Subroutine _arr_index: called CC=5 SCAR=16.4
7000 24 my ($s) = @_;
7001 24 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 24 24
7002 24 $s =~ s/\A\s+//; $s =~ s/\s+\z//;
7003 24 ▶ return 0 if $s eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "" 0 24 24
7004 24 ▶ return int($s) if $s =~ /\A-?\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /\A-?\d+\z/ 23 1 24
7005 1 my $v = _eval_arith($s);
7006 1 ▶ return ($v =~ /\A-?\d+\z/) ? int($v) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$v =~ /\A-?\d+\z/ ? : 1 0 1
7007 }
7008  
7009 # Resolve $VAR / ${VAR} inside a subscript (no command substitution, no
7010 # arithmetic -- those are applied later for indexed subscripts).
7011 41 ▶sub _arr_expand_sub {
Subroutine _arr_expand_sub: called CC=4 SCAR=16.3
7012 41 my ($class, $s) = @_;
7013 41 ▶ return '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 41 41
7014 41 $s =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/
7015 0 ▶ do { my $v = BATsh::Env->get($1); defined $v ? $v : '' }
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 0 0 0
7016 /ge;
7017 41 $s =~ s/\$([A-Za-z_][A-Za-z0-9_]*)/
7018 6 ▶ do { my $v = BATsh::Env->get($1); defined $v ? $v : '' }
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 6 0 6
7019 /ge;
7020 41 return $s;
7021 }
7022  
7023 # Element keys in display order: ascending numeric (indexed) or sorted
7024 # string (assoc).
7025 24 ▶sub _arr_ordered_keys {
Subroutine _arr_ordered_keys: called CC=4 SCAR=13.9
7026 24 my ($name) = @_;
7027 24 my $k = _arr_name($name);
7028 24 ▶ return () unless exists $_SH_ARRAY{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BATsh::SH::_SH_ARRAY{$k} 0 24 24
7029 24 my $h = $_SH_ARRAY{$k};
7030 24 ▶ 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") 4 20 24
Condition66%2/3
defined $BATsh::SH::_SH_ARRAY_TYPE{$k} and $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
!ll&&!rl&&r
0204
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
7031 4 return sort keys %{$h};
7032 }
7033 20 return sort { $a <=> $b } keys %{$h};
7034 }
7035  
7036 13 ▶sub _arr_values {
Subroutine _arr_values: called CC=2 SCAR=7.0
7037 13 my ($name) = @_;
7038 13 my $k = _arr_name($name);
7039 13 ▶ return () unless exists $_SH_ARRAY{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BATsh::SH::_SH_ARRAY{$k} 0 13 13
7040 13 my $h = $_SH_ARRAY{$k};
7041 13 return map { $h->{$_} } _arr_ordered_keys($name);
7042 }
7043  
7044 14 ▶sub _arr_count {
Subroutine _arr_count: called CC=2 SCAR=6.9
7045 14 my ($name) = @_;
7046 14 my $k = _arr_name($name);
7047 14 ▶ return 0 unless exists $_SH_ARRAY{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BATsh::SH::_SH_ARRAY{$k} 2 12 14
7048 12 return scalar keys %{$_SH_ARRAY{$k}};
7049 }
7050  
7051 # Fetch a single element. $sub is already $VAR-expanded by the caller.
7052 # Returns undef when the element is unset.
7053 32 ▶sub _arr_get_element {
Subroutine _arr_get_element: called CC=9 SCAR=22.7
7054 32 my ($name, $sub) = @_;
7055 32 my $k = _arr_name($name);
7056 32 ▶ return undef unless exists $_SH_ARRAY{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BATsh::SH::_SH_ARRAY{$k} 0 32 32
7057 32 my $h = $_SH_ARRAY{$k};
7058 32 ▶ 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") 15 17 32
Condition66%2/3
defined $BATsh::SH::_SH_ARRAY_TYPE{$k} and $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
!ll&&!rl&&r
01715
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
7059 15 ▶ return exists $h->{$sub} ? $h->{$sub} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $h->{$sub} ? : 15 0 15
7060 }
7061 17 my $idx = _arr_index($sub);
7062 17 ▶ if ($idx < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < 0) 2 15 17
7063 # Negative subscript: count back over the ordered set of set indices.
7064 2 my @keys = _arr_ordered_keys($name);
7065 2 ▶ return undef unless @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys 0 2 2
7066 2 my $kk = $keys[$idx];
7067 2 ▶ return defined $kk ? $h->{$kk} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $kk ? : 2 0 2
7068 }
7069 15 ▶ return exists $h->{$idx} ? $h->{$idx} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $h->{$idx} ? : 15 0 15
7070 }
7071  
7072 # Remove shell quoting from a value: drop unescaped quote characters while
7073 # honouring single- vs double-quote regions. Handles whole-token quotes
7074 # ("a b") and partial quotes (foo"a b"bar) alike.
7075 1729 ▶sub _arr_dequote {
Subroutine _arr_dequote: called CC=17 SCAR=28.6
7076 1729 my ($v) = @_;
7077 1729 ▶ return '' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 1729 1729
7078 1729 my $out = '';
7079 1729 my $in_sq = 0;
7080 1729 my $in_dq = 0;
7081 1729 my @c = split //, $v;
7082 1729 my $n = scalar @c;
7083 1729 my $i = 0;
7084 1729 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 14551 1729 16280
7085 14551 my $c = $c[$i];
7086 # Single quotes: everything literal, no escapes, until the closer.
7087 14551 ▶ if ($in_sq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq) 401 14150 14551
7088 401 ▶ if ($c eq "'") { $in_sq = 0 } else { $out .= $c }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq q[']) { } 46 355 401
7089 401 $i++; next;
7090 }
7091 14150 ▶ if ($c eq "'" && !$in_dq) { $in_sq = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "'" and not $in_dq) 46 14104 14150
Condition100%3/3
$c eq "'" and not $in_dq
!ll&&!rl&&r
14101346
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
7092 # Backslash: escape processing (POSIX).
7093 14104 ▶ if ($c eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\\") 18 14086 14104
7094 18 ▶ my $nx = ($i+1 < $n) ? $c[$i+1] : '';
Branch: 1 branch
BranchTrueFalseTotal
$i + 1 < $n ? : 18 0 18
7095 18 ▶ if ($in_dq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_dq) 16 2 18
7096 # Inside "..." backslash is literal EXCEPT before $ ` " \.
7097 16 ▶ 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 "\$") 2 14 16
Condition66%6/9
$nx eq "\"" or $nx eq "\\" or $nx eq "`" or $nx eq "\$"
l!l&&r!l&&!r
2014
$nx eq "\"" or $nx eq "\\" or $nx eq "`"
l!l&&r!l&&!r
2014
$nx eq "\"" or $nx eq "\\"
l!l&&r!l&&!r
2014
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
7098 2 $out .= $nx; $i += 2; next;
7099 }
7100 14 $out .= '\\'; $i++; next;
7101 }
7102 # Unquoted: backslash quotes the following char (so \" is a
7103 # literal quote and does NOT open a quoted region).
7104 2 ▶ if ($nx ne '') { $out .= $nx; $i += 2; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($nx ne "") 2 0 2
7105 0 $out .= '\\'; $i++; next;
7106 }
7107 14086 ▶ if ($c eq '"' && !$in_sq) { $in_dq = !$in_dq; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\"" and not $in_sq) 880 13206 14086
Condition66%2/3
$c eq "\"" and not $in_sq
!ll&&!rl&&r
132060880
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
7108 13206 $out .= $c; $i++;
7109 }
7110 # A backslash that came out of an expansion is literal data, never a
7111 # shell escape: restore it now that quote removal has finished.
7112 1729 return _unprotect_lit($out);
7113 }
7114  
7115 # Split a string on unquoted whitespace, KEEPING the quote characters in
7116 # each returned token (so the caller can tell whether a token was quoted).
7117 268 ▶sub _arr_split_words {
Subroutine _arr_split_words: called CC=30 SCAR=36.2
7118 268 my ($s) = @_;
7119 268 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 268 268
7120 268 my @words;
7121 268 my $cur = '';
7122 268 my $have = 0;
7123 268 my $in_sq = 0;
7124 268 my $in_dq = 0;
7125 268 my $in_bt = 0; # backtick `...`
7126 268 my $pdep = 0; # $( ... and <( >( and plain ( ) depth
7127 268 my $bdep = 0; # ${ ... } depth
7128 268 my @chars = split //, $s;
7129 268 my $n = scalar @chars;
7130 268 my $i = 0;
7131 268 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 2415 268 2683
7132 2415 my $c = $chars[$i];
7133 2415 ▶ if ($in_sq) { $cur .= $c; $in_sq = 0 if $c eq "'"; $have = 1; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($in_sq) 197 2218 2415
if $c eq "'" 34 163 197
7134 2218 ▶ 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 2184 2218
Condition83%5/6
$c eq "'" and not $in_dq and not $in_bt
!ll&&!rl&&r
2184034
$c eq "'" and not $in_dq
!ll&&!rl&&r
2183134
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
7135 2184 ▶ 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) 40 2144 2184
Condition66%2/3
$c eq "\"" and not $in_bt
!ll&&!rl&&r
2144040
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
7136 # Backslash escape keeps the next char attached to this word.
7137 2144 ▶ if ($c eq '\\') { $cur .= $c; $have = 1; $i++; $cur .= $chars[$i] if $i < $n; $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq "\\") 8 2136 2144
if $i < $n 8 0 8
7138 0 ▶ if ($c eq '`') { $in_bt = !$in_bt; $cur .= $c; $have = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "`") 0 2136 2136
7139 # Command / parameter substitution regions are opaque so their
7140 # internal whitespace does not word-split (e.g. $(echo a b c)).
7141 2136 ▶ if (!$in_dq) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_dq) 1986 150 2136
7142 0 ▶ 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 1986 1986
Condition66%4/6
$c eq "\$" and $i + 1 < $n and $chars[$i + 1] eq "{"
!ll&&!rl&&r
198240
$c eq "\$" and $i + 1 < $n
!ll&&!rl&&r
198204
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
7143 0 ▶ if ($bdep > 0 && $c eq '}') { $bdep--; $cur .= $c; $have = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($bdep > 0 and $c eq "}") 0 1986 1986
Condition33%1/3
$bdep > 0 and $c eq "}"
!ll&&!rl&&r
198600
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
7144 1986 ▶ if ($c eq '(') { $pdep++; $cur .= $c; $have = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "(") 2 1984 1986
7145 1984 ▶ if ($c eq ')' && $pdep > 0) { $pdep--; $cur .= $c; $have = 1; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq ")" and $pdep > 0) 2 1982 1984
Condition66%2/3
$c eq ")" and $pdep > 0
!ll&&!rl&&r
198202
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
7146 }
7147 2132 ▶ 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/) 245 1887 2132
Condition80%12/15
not $in_sq || $in_dq || $in_bt and $pdep == 0 and $bdep == 0 and $c =~ /\s/
!ll&&!rl&&r
1681719245
not $in_sq || $in_dq || $in_bt and $pdep == 0 and $bdep == 0
!ll&&!rl&&r
16801964
not $in_sq || $in_dq || $in_bt and $pdep == 0
!ll&&!rl&&r
150181964
$in_sq || $in_dq || $in_bt
l!l&&r!l&&!r
15001982
$in_sq or $in_dq
l!l&&r!l&&!r
01501982
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
7148 245 ▶ if ($have) { push @words, $cur; $cur = ''; $have = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($have) 245 0 245
7149 245 $i++; next;
7150 }
7151 1887 $cur .= $c; $have = 1; $i++;
7152 }
7153 268 ▶ push @words, $cur if $have;
Branch: 1 branch
BranchTrueFalseTotal
if $have 267 1 268
7154 268 return @words;
7155 }
7156  
7157 # Parse the body of a (...) array literal into a list of [subscript, value]
7158 # pairs. $subscript is undef for positional elements and a string for
7159 # explicit [sub]=value elements.
7160 31 ▶sub _arr_parse_elements {
Subroutine _arr_parse_elements: called CC=9 SCAR=23.0
7161 31 my ($class, $body) = @_;
7162 31 my @raw = _arr_split_words($body);
7163 31 my @out;
7164 31 for my $tok (@raw) {
7165 82 my ($sub, $vpart);
7166 82 ▶ if ($tok =~ /\A\[(.*?)\]=(.*)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tok =~ /\A\[(.*?)\]=(.*)\z/s) { } 14 68 82
7167 14 ($sub, $vpart) = ($1, $2);
7168 }
7169 else {
7170 68 ($sub, $vpart) = (undef, $tok);
7171 }
7172 82 ▶ my $raw_has_quote = ($vpart =~ /['"]/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$vpart =~ /['"]/ ? : 7 75 82
7173 82 my $exp = _expand($class, $vpart);
7174 82 ▶ if (defined $sub) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $sub) { } 14 68 82
elsif (not $raw_has_quote and $exp =~ /\s/) { } 0 68 68
7175 14 $sub = _arr_dequote(_expand($class, $sub));
7176 14 push @out, [$sub, _arr_dequote($exp)];
7177 }
7178 ▶ elsif (!$raw_has_quote && $exp =~ /\s/) {
Condition66%2/3
not $raw_has_quote and $exp =~ /\s/
!ll&&!rl&&r
7610
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
7179 # Unquoted expansion is subject to word splitting.
7180 0 for my $w (split /\s+/, $exp) {
7181 0 ▶ next if $w eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $w eq "" 0 0 0
7182 0 push @out, [undef, $w];
7183 }
7184 }
7185 else {
7186 68 push @out, [undef, _arr_dequote($exp)];
7187 }
7188 }
7189 31 return @out;
7190 }
7191  
7192 # arr=( ... ) / arr+=( ... ) whole-array assignment or append.
7193 31 ▶sub _arr_assign_literal {
Subroutine _arr_assign_literal: called CC=15 SCAR=27.4
7194 31 my ($class, $name, $body, $append) = @_;
7195 31 my $k = _arr_name($name);
7196 31 my $assoc = _arr_is_assoc($name); # honour a prior 'declare -A'
7197  
7198 31 ▶ if (!$append) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $append) { } 28 3 31
7199 28 $_SH_ARRAY{$k} = {};
7200 28 ▶ $_SH_ARRAY_TYPE{$k} = $assoc ? 'assoc' : 'indexed';
Branch: 1 branch
BranchTrueFalseTotal
$assoc ? : 5 23 28
7201 }
7202 else {
7203 3 ▶ $_SH_ARRAY{$k} = {} unless exists $_SH_ARRAY{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BATsh::SH::_SH_ARRAY{$k} 0 3 3
7204 $_SH_ARRAY_TYPE{$k} = ($assoc ? 'assoc' : 'indexed')
7205 3 ▶ unless exists $_SH_ARRAY_TYPE{$k};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $BATsh::SH::_SH_ARRAY_TYPE{$k} 0 3 3
$assoc ? : 0 0 0
7206 }
7207 31 BATsh::Env->unset($name); # a name is array OR scalar, not both
7208  
7209 31 my $is_assoc = ($_SH_ARRAY_TYPE{$k} eq 'assoc');
7210 31 my @elems = _arr_parse_elements($class, $body);
7211  
7212 31 ▶ if ($is_assoc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_assoc) { } 5 26 31
7213 5 for my $e (@elems) {
7214 12 my ($sub, $val) = @{$e};
7215 12 ▶ $sub = '' unless defined $sub;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sub 0 12 12
7216 12 $_SH_ARRAY{$k}{$sub} = $val;
7217 }
7218 }
7219 else {
7220 26 my $next = 0;
7221 26 for my $ix (keys %{$_SH_ARRAY{$k}}) {
7222 7 ▶ $next = $ix + 1 if $ix =~ /\A-?\d+\z/ && $ix + 1 > $next;
Branch: 1 branch
BranchTrueFalseTotal
if $ix =~ /\A-?\d+\z/ and $ix + 1 > $next 4 3 7
Condition66%2/3
$ix =~ /\A-?\d+\z/ and $ix + 1 > $next
!ll&&!rl&&r
034
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
7223 }
7224 26 for my $e (@elems) {
7225 70 my ($sub, $val) = @{$e};
7226 70 ▶ if (defined $sub && $sub ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sub and $sub ne '') { } 2 68 70
Condition66%2/3
defined $sub and $sub ne ""
!ll&&!rl&&r
6802
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
7227 2 my $ix = _arr_index($sub);
7228 2 $_SH_ARRAY{$k}{$ix} = $val;
7229 2 $next = $ix + 1;
7230 }
7231 else {
7232 68 $_SH_ARRAY{$k}{$next} = $val;
7233 68 $next++;
7234 }
7235 }
7236 }
7237 31 $LAST_STATUS = 0;
7238 31 return 0;
7239 }
7240  
7241 # arr[sub]=value / arr[sub]+=value single-element assignment or append.
7242 11 ▶sub _arr_assign_element {
Subroutine _arr_assign_element: called CC=6 SCAR=18.3
7243 11 my ($class, $name, $sub, $rawval, $append) = @_;
7244 11 my $val = _expand($class, $rawval);
7245 11 $val =~ s/\A"(.*)"\z/$1/s;
7246 11 $val =~ s/\A'(.*)'\z/$1/s;
7247 11 my $k = _arr_name($name);
7248 11 $sub = _arr_expand_sub($class, $sub);
7249 11 ▶ if (!exists $_SH_ARRAY{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $BATsh::SH::_SH_ARRAY{$k}) 0 11 11
7250 0 $_SH_ARRAY{$k} = {};
7251 0 $_SH_ARRAY_TYPE{$k} = 'indexed';
7252 }
7253 11 BATsh::Env->unset($name);
7254 11 ▶ 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' ? : 8 3 11
Condition66%2/3
defined $BATsh::SH::_SH_ARRAY_TYPE{$k} and $BATsh::SH::_SH_ARRAY_TYPE{$k} eq "assoc"
!ll&&!rl&&r
038
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
7255 ? $sub : _arr_index($sub);
7256 11 ▶ if ($append) {
Branch: 1 branch
BranchTrueFalseTotal
if ($append) { } 1 10 11
7257 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
7258 1 $_SH_ARRAY{$k}{$key} = $old . $val;
7259 }
7260 else {
7261 10 $_SH_ARRAY{$k}{$key} = $val;
7262 }
7263 11 $LAST_STATUS = 0;
7264 11 return 0;
7265 }
7266  
7267 # declare / typeset [-aA] NAME[=(...)] ... array (and scalar) declaration.
7268 14 ▶sub _cmd_declare {
Subroutine _cmd_declare: called CC=27 SCAR=36.7
7269 14 my ($class, $rest) = @_;
7270 14 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 14 14
7271 14 $rest =~ s/\A\s+//;
7272  
7273 14 my $type; # 'assoc' | 'indexed' | undef
7274 14 my $intattr = 0;
7275 14 my $roattr = 0;
7276 14 ▶ while ($rest =~ s/\A(-[A-Za-z]+)\s+//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/\A(-[A-Za-z]+)\s+//) 14 14 28
7277 14 my $flag = $1;
7278 14 ▶ if ($flag =~ /A/) { $type = 'assoc' }
Branch: 2 branches
BranchTrueFalseTotal
if ($flag =~ /A/) { } 9 5 14
elsif ($flag =~ /a/) { } 0 5 5
7279 0 ▶ elsif ($flag =~ /a/) { $type = 'indexed' unless defined $type }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 0 0
7280 14 ▶ $intattr = 1 if $flag =~ /i/;
Branch: 1 branch
BranchTrueFalseTotal
if $flag =~ /i/ 5 9 14
7281 14 ▶ $roattr = 1 if $flag =~ /r/;
Branch: 1 branch
BranchTrueFalseTotal
if $flag =~ /r/ 0 14 14
7282 }
7283  
7284 14 ▶ while ($rest ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest ne "") 14 14 28
7285 14 $rest =~ s/\A\s+//;
7286 14 ▶ last if $rest eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $rest eq "" 0 14 14
7287 14 ▶ 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) { } 3 11 14
elsif ($rest =~ /\A([A-Za-z_][A-Za-z0-9_]*)=((?:"[^"]*"|'[^']*'|\S)*)\s*(.*)\z/s) { } 4 7 11
elsif ($rest =~ /\A([A-Za-z_][A-Za-z0-9_]*)\s*(.*)\z/s) { } 7 0 7
7288 3 my ($name, $body, $tail) = ($1, $2, $3);
7289 3 my $k = _arr_name($name);
7290 3 ▶ $_SH_ARRAY_TYPE{$k} = $type if defined $type;
Branch: 1 branch
BranchTrueFalseTotal
if defined $type 3 0 3
7291 3 _arr_assign_literal($class, $name, $body, 0);
7292 3 $rest = $tail;
7293 }
7294 elsif ($rest =~ /\A([A-Za-z_][A-Za-z0-9_]*)=((?:"[^"]*"|'[^']*'|\S)*)\s*(.*)\z/s) {
7295 4 my ($name, $val, $tail) = ($1, $2, $3);
7296 4 ▶ if (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) { } 0 4 4
7297 # Typed array declared with a scalar initialiser: seed [0].
7298 0 my $k = _arr_name($name);
7299 0 $_SH_ARRAY_TYPE{$k} = $type;
7300 0 _arr_assign_element($class, $name, '0', $val, 0);
7301 }
7302 else {
7303 4 $val = _expand($class, $val);
7304 4 $val =~ s/\A"(.*)"\z/$1/s;
7305 4 $val =~ s/\A'(.*)'\z/$1/s;
7306 4 my $ik = uc($name);
7307 4 ▶ $_SH_INTATTR{$ik} = 1 if $intattr;
Branch: 1 branch
BranchTrueFalseTotal
if $intattr 4 0 4
7308 # "declare -i x=EXPR" evaluates the right hand side as
7309 # arithmetic; _sh_store_scalar re-applies this for any
7310 # later plain "x=..." assignment because the attribute
7311 # sticks in %_SH_INTATTR.
7312 4 ▶ $val = _eval_arith($val) if $_SH_INTATTR{$ik};
Branch: 1 branch
BranchTrueFalseTotal
if $BATsh::SH::_SH_INTATTR{$ik} 4 0 4
7313 4 ▶ if (_sh_is_readonly($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_sh_is_readonly($name)) { } 0 4 4
7314 0 print STDERR "sh: $name: readonly variable\n";
7315 0 $LAST_STATUS = 1;
7316 }
7317 else {
7318 4 BATsh::Env->set($name, $val);
7319 }
7320 4 ▶ $_SH_READONLY{$ik} = 1 if $roattr;
Branch: 1 branch
BranchTrueFalseTotal
if $roattr 0 4 4
7321 }
7322 4 $rest = $tail;
7323 }
7324 elsif ($rest =~ /\A([A-Za-z_][A-Za-z0-9_]*)\s*(.*)\z/s) {
7325 7 my ($name, $tail) = ($1, $2);
7326 7 ▶ if (($intattr || $roattr) && !defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($intattr || $roattr and not defined $type) { } 1 6 7
Condition66%4/6
$intattr || $roattr and not defined $type
!ll&&!rl&&r
601
$intattr or $roattr
l!l&&r!l&&!r
106
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
7327 # Attribute-only declaration: mark the variable, do NOT
7328 # turn it into an (empty) array.
7329 1 my $ik = uc($name);
7330 1 ▶ $_SH_INTATTR{$ik} = 1 if $intattr;
Branch: 1 branch
BranchTrueFalseTotal
if $intattr 1 0 1
7331 1 ▶ $_SH_READONLY{$ik} = 1 if $roattr;
Branch: 1 branch
BranchTrueFalseTotal
if $roattr 0 1 1
7332 }
7333 else {
7334 6 my $k = _arr_name($name);
7335 6 ▶ $_SH_ARRAY{$k} = {} unless exists $_SH_ARRAY{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BATsh::SH::_SH_ARRAY{$k} 6 0 6
7336 6 ▶ $_SH_ARRAY_TYPE{$k} = (defined $type ? $type : 'indexed');
Branch: 1 branch
BranchTrueFalseTotal
defined $type ? : 6 0 6
7337 }
7338 7 $rest = $tail;
7339 }
7340 else {
7341 0 last;
7342 }
7343 }
7344 14 ▶ $LAST_STATUS = 0 unless $LAST_STATUS;
Branch: 1 branch
BranchTrueFalseTotal
unless $LAST_STATUS 14 0 14
7345 14 return $LAST_STATUS;
7346 }
7347  
7348 # _sh_try_array_op: detect and perform an array operation on the RAW line.
7349 # Returns (1, $status) when it handled the line, or () otherwise.
7350 1645 ▶sub _sh_try_array_op {
Subroutine _sh_try_array_op: called CC=11 SCAR=24.0
7351 1645 my ($class, $line, $opts_ref) = @_;
7352 1645 my $s = $line;
7353 1645 $s =~ s/\A\s+//; $s =~ s/\s+\z//;
7354 1645 $s =~ s/\s*;\s*\z//; # tolerate one trailing ';'
7355  
7356 # declare / typeset / local with array semantics
7357 1645 ▶ if ($s =~ /\A(declare|typeset|local)\b\s*(.*)\z/is) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A(declare|typeset|local)\b\s*(.*)\z/is) 17 1628 1645
7358 17 my ($kw, $args) = ($1, $2);
7359 17 my $is_local = (lc($kw) eq 'local');
7360 17 ▶ my $has_arr_flag = ($args =~ /\A-[A-Za-z]*[aA]/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args =~ /\A-[A-Za-z]*[aA]/ ? : 9 8 17
7361 17 ▶ my $has_arr_init = ($args =~ /=\(/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args =~ /=\(/ ? : 3 14 17
7362 17 ▶ 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) 14 3 17
Condition66%4/6
not $is_local or $has_arr_flag or $has_arr_init
l!l&&r!l&&!r
1403
not $is_local or $has_arr_flag
l!l&&r!l&&!r
1403
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
7363 14 return (1, _cmd_declare($class, $args));
7364 }
7365 3 return (); # plain 'local x=1' -> handled by _cmd_local
7366 }
7367  
7368 # NAME=( ... ) or NAME+=( ... ) -- whole-line array literal
7369 1628 ▶ 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) 28 1600 1628
7370 28 my ($name, $plus, $body) = ($1, $2, $3);
7371 28 ▶ return (1, _arr_assign_literal($class, $name, $body,
Branch: 1 branch
BranchTrueFalseTotal
$plus eq '+' ? : 3 25 28
7372 ($plus eq '+') ? 1 : 0));
7373 }
7374  
7375 # NAME[SUB]=VALUE or NAME[SUB]+=VALUE -- single-element assignment
7376 1600 ▶ 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) 11 1589 1600
7377 11 my ($name, $sub, $plus, $val) = ($1, $2, $3, $4);
7378 11 ▶ return (1, _arr_assign_element($class, $name, $sub, $val,
Branch: 1 branch
BranchTrueFalseTotal
$plus eq '+' ? : 1 10 11
7379 ($plus eq '+') ? 1 : 0));
7380 }
7381  
7382 1589 return ();
7383 }
7384  
7385 # _expand_word_list: turn a for-loop list string into a list of items.
7386 # Resolves variables / command substitution, applies filename globbing to
7387 # unquoted glob words, and expands a whole-word ${arr[@]} / ${arr[*]}
7388 # reference (quoted or not) to one item per array element.
7389 43 ▶sub _expand_word_list {
Subroutine _expand_word_list: called CC=13 SCAR=25.7
7390 43 my ($class, $list_str) = @_;
7391 43 ▶ $list_str = _brace_expand_line($list_str) if defined $list_str && $list_str =~ /\{/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $list_str and $list_str =~ /\{/ 5 38 43
Condition66%2/3
defined $list_str and $list_str =~ /\{/
!ll&&!rl&&r
0385
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
7392 43 my @raw = _arr_split_words($list_str);
7393 43 my @items;
7394 43 for my $tok (@raw) {
7395 # Whole-word ${arr[@]} / ${arr[*]} -> one item per element value.
7396 102 ▶ 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)) 3 99 102
Condition66%2/3
$tok =~ /\A"?\$\{([A-Za-z_][A-Za-z0-9_]*)\[[\@*]\]\}"?\z/ and _arr_exists($1)
!ll&&!rl&&r
9903
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
7397 && _arr_exists($1)) {
7398 3 push @items, _arr_values($1);
7399 3 next;
7400 }
7401 # Whole-word ${!arr[@]} / ${!arr[*]} -> one item per index / key.
7402 99 ▶ 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 97 99
Condition66%2/3
$tok =~ /\A"?\$\{!([A-Za-z_][A-Za-z0-9_]*)\[[\@*]\]\}"?\z/ and _arr_exists($1)
!ll&&!rl&&r
9702
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
7403 && _arr_exists($1)) {
7404 2 push @items, _arr_ordered_keys($1);
7405 2 next;
7406 }
7407 97 ▶ my $raw_has_quote = ($tok =~ /['"]/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$tok =~ /['"]/ ? : 1 96 97
7408 97 my $exp = _arr_dequote(_expand($class, $tok));
7409 97 ▶ if (!$raw_has_quote) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $raw_has_quote) { } 96 1 97
7410 96 ▶ if ($exp =~ /[*?\[]/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($exp =~ /[*?\[]/) { } 3 93 96
elsif ($exp =~ /\s/) { } 2 91 93
elsif ($exp ne '') { } 91 0 91
7411 3 push @items, _glob_expand($exp);
7412 }
7413 elsif ($exp =~ /\s/) {
7414 2 push @items, grep { $_ ne '' } split /\s+/, $exp;
7415 }
7416 elsif ($exp ne '') {
7417 91 push @items, $exp;
7418 }
7419 # an empty unquoted word expands to nothing
7420 }
7421 else {
7422 1 push @items, $exp;
7423 }
7424 }
7425 43 return @items;
7426 }
7427  
7428 # ----------------------------------------------------------------
7429 # Accessors
7430 # ----------------------------------------------------------------
7431 2 ▶sub last_status { return $LAST_STATUS }
Subroutine last_status: called CC=1 SCAR=0.0
Pod undocumented
7432 0 ▶sub set_last_status { $LAST_STATUS = $_[1] }
Subroutine set_last_status: not called CC=1 SCAR=6.9
Pod undocumented
7433  
7434 # Need Cwd
7435 64 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.6
7436 64 eval { require Cwd };
7437 64 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 64 64
7438 0 eval 'sub Cwd::cwd { return $ENV{PWD} || "." }';
7439 }
7440 }
7441  
7442 64 1;
7443  
7444 __END__
↑ Summary lib/BATsh/CMD.pm »