lib/BATsh/CMD.pm

filter: Statementstmt 75.8% 858 / 1132 Branchbran 63.2% 387 / 612 Conditioncond 50.3% 84 / 167 MC/DCmcdc 29.2% 31 / 106 Subroutinesub 90.3% 56 / 62 Podpod 0.0% 0 / 4 totaltotal 67.9% 1416 / 2083 CC 386 SCAR 39.1
CC
386
Cov
70%
_dispatch_with_redirs
236.6
_cmd_for_f
146.7
_cmd_dir
110.0
CRAP 49.8
« lib/BATsh/SH.pm ↑ Summary lib/BATsh.pm »
1 package BATsh::CMD;
2 # Copyright (c) 2026 INABA Hitoshi <ina.cpan@gmail.com>
3 ######################################################################
4 #
5 # BATsh::CMD - Pure Perl cmd.exe interpreter
6 #
7 # v0.02 changes (cmd.exe compatibility fixes):
8 # 1. Environment variable case-insensitivity (via Env.pm)
9 # 2. ^ escape character: protects & | < > and line continuation
10 # 3. Redirect/pipe: > >> 2> 2>> < | parsed before dispatch
11 # 4. SETLOCAL ENABLEDELAYEDEXPANSION + !VAR! (via Env.pm)
12 # 5. IF block pre-expansion: entire IF block expanded at parse time
13 # (matching cmd.exe's "parse before execute" semantics)
14 # 6. FOR /F: tokens= delims= skip= eol= usebackq
15 # 7. IF /I must be parsed BEFORE plain == to avoid shadowing
16 # 8. ECHO no longer resets ERRORLEVEL
17 # 9. SETLOCAL passes option string to Env::setlocal()
18 # 10. IF EXIST handles quoted paths with spaces
19 # 11. Pipeline (|): _split_compound detects |, _exec_pipe chains via tmpfile
20 # 12. SET /P VAR=Prompt: reads one line from STDIN
21 # 13. SHIFT / SHIFT /N: shifts %1..%9 and rebuilds %* (dispatched builtin)
22 # 14. Batch-parameter tilde modifiers via Env::expand_cmd():
23 # %~0 %~f1 %~d0 %~p0 %~n1 %~x1 %~dp0 %~nx1 (f d p n x combinable),
24 # and on CALL :label arguments once they populate %1..%9
25 # 15. & && || compound commands (_exec_compound)
26 # 16. %0..%9 and %* positional parameter expansion in expand_cmd()
27 # 17. CALL :label arg1 arg2: installs the subroutine's own %0..%9 / %*
28 # (quote-aware, %-expanded) via BATsh->call_sub, which saves and
29 # restores the caller's positional-parameter frame
30 #
31 ######################################################################
32  
33 64 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 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
35 64 ▶use warnings; local $^W = 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 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
37  
38 64 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 64 ▶use File::Copy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 64 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
41 64 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 64 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 64 $VERSION = '0.11';
44 64 $VERSION = $VERSION;
45  
46 64 require BATsh::Env;
47 64 require BATsh::MB;
48 # For BATsh::SH::_glob_paths(): CMD-mode wildcards (FOR sets, DEL) are
49 # expanded by the same pure-Perl matcher the SH side uses, because Perl's
50 # built-in glob() reads a backslash as an escape and would destroy an
51 # ordinary Windows pattern such as C:\dir\*.txt.
52 64 require BATsh::SH;
53  
54 # ----------------------------------------------------------------
55 # Module-level state
56 # ----------------------------------------------------------------
57 64 my $ECHO_ON = 1;
58 64 my $ERRORLEVEL = 0;
59 64 my $_GOTO_LABEL = '';
60 64 my $_EXIT_REQUESTED = 0; # set by EXIT; consumed by BATsh.pm (script exit)
61  
62 # ----------------------------------------------------------------
63 # Public: execute an array of CMD lines
64 # ----------------------------------------------------------------
65 190 ▶sub exec_block {
Subroutine exec_block: called CC=9 SCAR=22.0
Pod undocumented
66 190 my ($class, $lines_ref, %opts) = @_;
67 190 my @lines = @{$lines_ref};
68  
69 # Preprocess: join ^ line-continuations
70 190 @lines = _join_continuations(@lines);
71  
72 # Build label index
73 190 my %labels = ();
74 190 for my $i (0 .. $#lines) {
75 790 my $l = $lines[$i];
76 790 $l =~ s/\r?\n\z//;
77 790 $l =~ s/\A\s+//;
78 790 ▶ if ($l =~ /\A:([A-Za-z_][A-Za-z0-9_]*)\s*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /\A:([A-Za-z_][A-Za-z0-9_]*)\s*\z/) 31 759 790
79 31 $labels{uc($1)} = $i;
80 }
81 }
82  
83 190 my $i = 0;
84 190 ▶ while ($i <= $#lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#lines) 740 161 901
85 740 my $raw = $lines[$i];
86 740 $i++;
87 740 $raw =~ s/\r?\n\z//;
88 740 my $rc = _exec_line($class, $raw, \@lines, { %labels }, \$i, { %opts });
89  
90 740 ▶ if ($_GOTO_LABEL ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_GOTO_LABEL ne "") 63 677 740
91 63 my $lbl = $_GOTO_LABEL;
92 63 $_GOTO_LABEL = '';
93 63 ▶ if (exists $labels{$lbl}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $labels{$lbl}) { } 40 23 63
elsif ($lbl eq 'EOF') { } 23 0 23
94 40 $i = $labels{$lbl} + 1;
95 }
96 elsif ($lbl eq 'EOF') {
97 23 last;
98 }
99 else {
100 0 _warn("GOTO: label :$lbl not found");
101 }
102 40 next;
103 }
104 677 ▶ if (defined $rc && $rc eq '__EXIT__') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rc and $rc eq "__EXIT__") 6 671 677
Condition66%2/3
defined $rc and $rc eq "__EXIT__"
!ll&&!rl&&r
06716
MC/DC50%1/2
defined $rc and $rc eq "__EXIT__"
defined $rc $rc eq "__EXIT__"
Truth table66%2/3
A and B
A= defined $rcB= $rc eq "__EXIT__"
ABresult
0X0
100
111
105 6 return $ERRORLEVEL;
106 }
107 }
108 184 return $ERRORLEVEL;
109 }
110  
111 # ----------------------------------------------------------------
112 # _get_errorlevel: public accessor for the current ERRORLEVEL value.
113 # Used by BATsh::Env::_expand_named_var for %ERRORLEVEL% expansion.
114 # ----------------------------------------------------------------
115 167 ▶sub _get_errorlevel { return $ERRORLEVEL }
Subroutine _get_errorlevel: called CC=1 SCAR=0.0
116  
117 # ----------------------------------------------------------------
118 # _set_errorlevel: set ERRORLEVEL from outside the CMD interpreter.
119 # Used by BATsh.pm to bridge the SH-side $? into %ERRORLEVEL% at
120 # every SH->CMD section boundary. Call as a plain function.
121 # ----------------------------------------------------------------
122 501 ▶sub _set_errorlevel {
Subroutine _set_errorlevel: called CC=2 SCAR=7.2
123 501 ▶ $ERRORLEVEL = defined $_[0] ? int($_[0]) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 501 0 501
124 501 return $ERRORLEVEL;
125 }
126  
127 # ----------------------------------------------------------------
128 # _exit_requested / _clear_exit_requested: EXIT was executed in a CMD
129 # section. BATsh.pm polls the flag after each CMD block to terminate
130 # the whole script with ERRORLEVEL as the process exit code, then
131 # clears it. Call as plain functions.
132 # ----------------------------------------------------------------
133 158 ▶sub _exit_requested { return $_EXIT_REQUESTED }
Subroutine _exit_requested: called CC=1 SCAR=0.0
134 6 ▶sub _clear_exit_requested { $_EXIT_REQUESTED = 0; return 0 }
Subroutine _clear_exit_requested: called CC=1 SCAR=0.0
135  
136 # ----------------------------------------------------------------
137 # _join_continuations: merge lines ending with bare ^ (not ^^ or "^")
138 # cmd.exe rule: ^ at end-of-line (outside quotes) joins next line,
139 # consuming the ^ and leading whitespace of the next line.
140 # ----------------------------------------------------------------
141 190 ▶sub _join_continuations {
Subroutine _join_continuations: called CC=4 SCAR=13.9
142 190 my @in = @_;
143 190 my @out;
144 190 my $i = 0;
145 190 ▶ while ($i <= $#in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#in) 790 190 980
146 790 my $line = $in[$i]; $i++;
147 790 $line =~ s/\r?\n\z//;
148 # Count unescaped ^ at end: odd count = continuation
149 790 ▶ while ($line =~ /\A((?:[^^]|\^\^)*)\^\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A((?:[^^]|\^\^)*)\^\z/) 1 790 791
150 # Strip the trailing ^, append next line (minus leading ws)
151 1 $line = $1;
152 1 ▶ if ($i <= $#in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#in) { } 1 0 1
153 1 my $next = $in[$i]; $i++;
154 1 $next =~ s/\r?\n\z//;
155 1 $next =~ s/\A\s+//;
156 1 $line .= $next;
157 }
158 0 else { last }
159 }
160 790 push @out, $line;
161 }
162 190 return @out;
163 }
164  
165 # ----------------------------------------------------------------
166 # _unescape_caret: replace ^X with X (^ is escape char in cmd.exe)
167 # Called AFTER %VAR% expansion for non-block contexts.
168 # ----------------------------------------------------------------
169 241 ▶sub _unescape_caret {
Subroutine _unescape_caret: called CC=1 SCAR=0.0
170 241 my ($str) = @_;
171 241 $str =~ s/\^(.)/$1/g;
172 241 return $str;
173 }
174  
175 # ----------------------------------------------------------------
176 # Execute one logical line
177 # $pre_expanded: if true, skip %VAR% expansion (already done by FOR)
178 # $block_expanded: if true, skip expansion entirely (IF block body)
179 # ----------------------------------------------------------------
180 855 ▶sub _exec_line {
Subroutine _exec_line: called CC=13 SCAR=25.7
181 855 my ($class, $raw, $lines_ref, $labels_ref, $i_ref, $opts_ref, $pre_expanded) = @_;
182 855 ▶ $pre_expanded = 0 unless defined $pre_expanded;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pre_expanded 740 115 855
183  
184 855 my $line = $raw;
185 855 $line =~ s/\A\s+//;
186  
187 855 my $suppress_echo = 0;
188 855 ▶ if ($line =~ s/\A\@//) { $suppress_echo = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/\A\@//) 37 818 855
189  
190 855 ▶ return 0 if $line =~ /\A\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\s*\z/ 151 704 855
191 704 ▶ return 0 if $line =~ /\A::/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A::/ 0 704 704
192 704 ▶ return 0 if $line =~ /\AREM(?:\s|\z)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\AREM(?:\s|\z)/i 0 704 704
193 704 ▶ return 0 if $line =~ /\A:[A-Za-z_]/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A:[A-Za-z_]/ 11 693 704
194 693 ▶ return 0 if $line =~ /\A\s*\)\s*(?:ELSE\s*.*)??\s*\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\s*\)\s*(?:ELSE\s*.*)??\s*\z/i 0 693 693
195 693 ▶ return 0 if $line =~ /\A#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A#/ 0 693 693
196  
197 693 ▶ if (!$pre_expanded) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $pre_expanded) { } 578 115 693
elsif (BATsh::Env::delayed_expansion()) { } 8 107 115
198 578 $line = _expand_line($line);
199 }
200 elsif (BATsh::Env::delayed_expansion()) {
201 # Even in pre_expanded blocks, !VAR! must be expanded at runtime
202 8 $line = BATsh::Env->expand_cmd($line);
203 }
204  
205 # Handle compound commands: & && || (outside quotes, after expansion)
206 # Split on & / && / || and execute left to right
207 693 ▶ if ($line =~ /[&|]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /[&|]/) 5 688 693
208 5 my @parts = _split_compound($line);
209 5 ▶ if (@parts > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts > 1) 4 1 5
210 4 return _exec_compound($class, \@parts, $lines_ref, $labels_ref, $i_ref, $opts_ref);
211 }
212 }
213  
214 # Handle redirection stripping before dispatch
215 689 my ($clean_line, $redirs) = _strip_redirects($line);
216  
217 689 return _dispatch_with_redirs($class, $clean_line, $redirs, $lines_ref, $labels_ref, $i_ref, $opts_ref);
218 }
219  
220 # ----------------------------------------------------------------
221 # _expand_line: %VAR% expansion protecting FOR loop variables
222 # ----------------------------------------------------------------
223 578 ▶sub _expand_line {
Subroutine _expand_line: called CC=3 SCAR=11.5
224 578 my ($line) = @_;
225 578 ▶ if ($line =~ /\AFOR\s/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\AFOR\s/i) { } 23 555 578
226 23 ▶ if ($line =~ /\A(FOR\s+(?:\/[A-Za-z]\s+(?:"[^"]*"\s+)?)?%%[A-Za-z]\s+(?:\/[A-Za-z]\s+)?IN\s*\([^)]*\)\s+DO\s+)(.*)\z/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[\A(FOR\s+(?:/[A-Za-z]\s+(?:"[^"]*"\s+)?)?%%[A-Za-z]\s+(?:/[A-Za-z]\s+)?IN\s*\([^)]*\)\s+DO\s+)(.*)\z]i) { } 23 0 23
227 23 my ($for_head, $do_part) = ($1, $2);
228 23 $for_head =~ s/%%([A-Za-z])/"\x00FOR_$1\x00"/ge;
229 23 $for_head = BATsh::Env->expand_cmd($for_head);
230 23 $for_head =~ s/\x00FOR_([A-Za-z])\x00/%%$1/g;
231 23 $do_part =~ s/%%([A-Za-z])/"\x00FOR_$1\x00"/ge;
232 23 $do_part =~ s/%([^%\r\n]+)%/"\x00PCT_$1\x00"/ge;
233 23 return $for_head . $do_part;
234 }
235 else {
236 0 $line =~ s/%%([A-Za-z])/"\x00FOR_$1\x00"/ge;
237 0 $line = BATsh::Env->expand_cmd($line);
238 0 $line =~ s/\x00FOR_([A-Za-z])\x00/%%$1/g;
239 0 return $line;
240 }
241 }
242 else {
243 0 $line =~ s/%%([A-Za-z])/"\x00FOR_$1\x00"/ge;
244 555 $line = BATsh::Env->expand_cmd($line);
245 555 $line =~ s/\x00FOR_([A-Za-z])\x00/%%$1/g;
246 555 return $line;
247 }
248 }
249  
250 # ----------------------------------------------------------------
251 # _split_compound: split on bare & && || (respecting quotes and ^)
252 # Returns list of { op => '&'|'&&'|'||'|'', cmd => $str }
253 # ----------------------------------------------------------------
254 7 ▶sub _split_compound {
Subroutine _split_compound: called CC=17 SCAR=29.5
255 7 my ($line) = @_;
256 7 my @parts;
257 7 my $cur = '';
258 7 my $in_q = 0;
259 7 my @chars = split //, $line;
260 7 my $n = scalar @chars;
261 7 my $j = 0;
262 7 ▶ while ($j < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 164 7 171
263 164 my $ch = $chars[$j];
264 164 ▶ if ($ch eq '^' && !$in_q) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "^" and not $in_q) 1 163 164
Condition66%2/3
$ch eq "^" and not $in_q
!ll&&!rl&&r
16301
MC/DC50%1/2
$ch eq "^" and not $in_q
$ch eq "^" not $in_q
Truth table66%2/3
A and not(B)
A= $ch eq "^"B= not $in_q
ABresult
0X0
100
111
265 # escaped: take next char literally
266 1 $j++;
267 1 ▶ if ($j < $n) { $cur .= $chars[$j]; $j++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 1 0 1
268 1 next;
269 }
270 163 ▶ if ($ch eq '"') { $in_q = !$in_q; $cur .= $ch; $j++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"") 2 161 163
271 161 ▶ if (!$in_q && $ch eq '&') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_q and $ch eq "&") 2 159 161
Condition100%3/3
not $in_q and $ch eq "&"
!ll&&!rl&&r
81512
MC/DC100%2/2
not $in_q and $ch eq "&"
not $in_q $ch eq "&"
Truth table100%3/3
not(A) and B
A= not $in_qB= $ch eq "&"
ABresult
0X0
100
111
272 2 ▶ if ($j+1 < $n && $chars[$j+1] eq '&') {
Branch: 1 branch
BranchTrueFalseTotal
if ($j + 1 < $n and $chars[$j + 1] eq '&') { } 1 1 2
Condition66%2/3
$j + 1 < $n and $chars[$j + 1] eq "&"
!ll&&!rl&&r
011
MC/DC50%1/2
$j + 1 < $n and $chars[$j + 1] eq "&"
$j + 1 < $n $chars[$j + 1] eq "&"
Truth table66%2/3
A and B
A= $j + 1 < $nB= $chars[$j + 1] eq "&"
ABresult
0X0
100
111
273 1 push @parts, { op => '', cmd => $cur }; $cur = ''; $j += 2;
274 1 push @parts, { op => '&&', cmd => '' };
275 }
276 else {
277 1 push @parts, { op => '', cmd => $cur }; $cur = ''; $j++;
278 1 push @parts, { op => '&', cmd => '' };
279 }
280 2 next;
281 }
282 159 ▶ if (!$in_q && $ch eq '|') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_q and $ch eq "|") 5 154 159
Condition100%3/3
not $in_q and $ch eq "|"
!ll&&!rl&&r
81465
MC/DC100%2/2
not $in_q and $ch eq "|"
not $in_q $ch eq "|"
Truth table100%3/3
not(A) and B
A= not $in_qB= $ch eq "|"
ABresult
0X0
100
111
283 5 ▶ if ($j+1 < $n && $chars[$j+1] eq '|') {
Branch: 2 branches
BranchTrueFalseTotal
if ($j + 1 < $n and $chars[$j + 1] eq '|') { } 0 5 5
elsif ($j + 1 < $n and $chars[$j + 1] ne '>') { } 5 0 5
Condition33%1/3
$j + 1 < $n and $chars[$j + 1] eq "|"
!ll&&!rl&&r
050
MC/DC0%0/2
$j + 1 < $n and $chars[$j + 1] eq "|"
$j + 1 < $n $chars[$j + 1] eq "|"
Truth table33%1/3
A and B
A= $j + 1 < $nB= $chars[$j + 1] eq "|"
ABresult
0X0
100
111
284 0 push @parts, { op => '', cmd => $cur }; $cur = ''; $j += 2;
285 0 push @parts, { op => '||', cmd => '' };
286 }
287 ▶ elsif ($j+1 < $n && $chars[$j+1] ne '>') {
Condition33%1/3
$j + 1 < $n and $chars[$j + 1] ne ">"
!ll&&!rl&&r
005
MC/DC0%0/2
$j + 1 < $n and $chars[$j + 1] ne ">"
$j + 1 < $n $chars[$j + 1] ne ">"
Truth table33%1/3
A and B
A= $j + 1 < $nB= $chars[$j + 1] ne ">"
ABresult
0X0
100
111
288 # pipe: record left side and mark as pipe op
289 5 push @parts, { op => '', cmd => $cur }; $cur = ''; $j++;
290 5 push @parts, { op => '|', cmd => '' };
291 }
292 else {
293 0 $cur .= $ch; $j++; next;
294 }
295 5 next;
296 }
297 154 $cur .= $ch; $j++;
298 }
299 7 ▶ push @parts, { op => '', cmd => $cur } if $cur =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $cur =~ /\S/ 7 0 7
300 7 return @parts;
301 }
302  
303 # ----------------------------------------------------------------
304 # _exec_compound: execute & / && / || compound commands
305 # ----------------------------------------------------------------
306 4 ▶sub _exec_compound {
Subroutine _exec_compound: called CC=15 SCAR=29.7
307 4 my ($class, $parts, $lines_ref, $labels_ref, $i_ref, $opts_ref) = @_;
308  
309 # If any part uses pipe operator, delegate entirely to _exec_pipe
310 # before executing any segment (so left-side stdout is captured first).
311 4 for my $part (@{$parts}) {
312 10 ▶ if ($part->{op} eq '|') {
Branch: 1 branch
BranchTrueFalseTotal
if ($part->{"op"} eq "|") 2 8 10
313 2 return _exec_pipe($class, $parts, $lines_ref, $labels_ref, $i_ref, $opts_ref);
314 }
315 }
316  
317 2 my $pending_op = '';
318 2 my $rc = 0;
319 2 for my $part (@{$parts}) {
320 6 my $op = $part->{op};
321 6 my $cmd = $part->{cmd};
322 6 $cmd =~ s/\A\s+//; $cmd =~ s/\s+\z//;
323  
324 6 ▶ if ($op eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq '') { } 4 2 6
325 # This is a command to run, pending_op tells us under what condition
326 4 ▶ if ($pending_op eq '') {
Branch: 4 branches
BranchTrueFalseTotal
if ($pending_op eq '') { } 2 2 4
elsif ($pending_op eq '&&') { } 1 1 2
elsif ($pending_op eq '||') { } 0 1 1
elsif ($pending_op eq '&') { } 1 0 1
327 2 ▶ $rc = _exec_single($class, $cmd, $lines_ref, $labels_ref, $i_ref, $opts_ref) if $cmd =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $cmd =~ /\S/ 2 0 2
328 }
329 elsif ($pending_op eq '&&') {
330 1 ▶ $rc = _exec_single($class, $cmd, $lines_ref, $labels_ref, $i_ref, $opts_ref) if $ERRORLEVEL == 0 && $cmd =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $ERRORLEVEL == 0 and $cmd =~ /\S/ 1 0 1
Condition33%1/3
$ERRORLEVEL == 0 and $cmd =~ /\S/
!ll&&!rl&&r
001
MC/DC0%0/2
$ERRORLEVEL == 0 and $cmd =~ /\S/
$ERRORLEVEL == 0 $cmd =~ /\S/
Truth table33%1/3
A and B
A= $ERRORLEVEL == 0B= $cmd =~ /\S/
ABresult
0X0
100
111
331 }
332 elsif ($pending_op eq '||') {
333 0 ▶ $rc = _exec_single($class, $cmd, $lines_ref, $labels_ref, $i_ref, $opts_ref) if $ERRORLEVEL != 0 && $cmd =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $ERRORLEVEL != 0 and $cmd =~ /\S/ 0 0 0
Condition0%0/3
$ERRORLEVEL != 0 and $cmd =~ /\S/
!ll&&!rl&&r
000
MC/DC0%0/2
$ERRORLEVEL != 0 and $cmd =~ /\S/
$ERRORLEVEL != 0 $cmd =~ /\S/
Truth table0%0/3
A and B
A= $ERRORLEVEL != 0B= $cmd =~ /\S/
ABresult
0X0
100
111
334 }
335 elsif ($pending_op eq '&') {
336 1 ▶ $rc = _exec_single($class, $cmd, $lines_ref, $labels_ref, $i_ref, $opts_ref) if $cmd =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $cmd =~ /\S/ 1 0 1
337 }
338 4 $pending_op = '';
339 }
340 else {
341 2 $pending_op = $op;
342 }
343 }
344 2 return $rc;
345 }
346  
347 8 ▶sub _exec_single {
Subroutine _exec_single: called CC=2 SCAR=7.0
348 8 my ($class, $cmd, $lines_ref, $labels_ref, $i_ref, $opts_ref) = @_;
349 8 ▶ return 0 unless $cmd =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd =~ /\S/ 0 8 8
350 8 my ($clean, $redirs) = _strip_redirects($cmd);
351 8 return _dispatch_with_redirs($class, $clean, $redirs, $lines_ref, $labels_ref, $i_ref, $opts_ref);
352 }
353  
354 # ----------------------------------------------------------------
355 # _exec_pipe: execute cmd1 | cmd2 [| cmd3 ...] via temporary files.
356 # Left side stdout -> tmpfile; right side reads tmpfile as stdin.
357 # Perl 5.005_03 compatible: bareword filehandles, 2-arg open only.
358 # ----------------------------------------------------------------
359 64 ▶use vars qw(*_PIPE_SAVOUT *_PIPE_SAVIN *_PIPE_WFH *_PIPE_RFH);
Subroutine BEGIN: called CC=1 SCAR=0.0
360  
361 2 ▶sub _exec_pipe {
Subroutine _exec_pipe: called CC=24 SCAR=39.9
362 2 my ($class, $parts, $lines_ref, $labels_ref, $i_ref, $opts_ref) = @_;
363  
364 # Collect command segments from parts list.
365 # parts layout (from _split_compound):
366 # { op=>'', cmd=>'left_cmd ' }
367 # { op=>'|', cmd=>'' }
368 # { op=>'', cmd=>' right_cmd'}
369 # Segments are op='' chunks; '|' entries are separators.
370 2 my @segments;
371 2 my $cur = '';
372 2 for my $part (@{$parts}) {
373 6 my $op = $part->{op};
374 6 my $cmd = $part->{cmd};
375 6 ▶ if ($op eq '|') {
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq '|') { } 2 4 6
elsif ($op eq '') { } 4 0 4
376 2 push @segments, $cur;
377 2 $cur = '';
378 }
379 elsif ($op eq '') {
380 4 $cur .= $cmd;
381 }
382 else {
383 # &&, ||, & after a pipe: attach to current segment
384 0 $cur .= " $op $cmd";
385 }
386 }
387 2 push @segments, $cur;
388  
389 2 my $rc = 0;
390 2 my $base = File::Spec->catfile(File::Spec->tmpdir(),
391 "batsh_pipe_$$");
392 2 my $input_f = undef; # tmpfile feeding this segment's stdin
393 2 my $n_segs = scalar @segments;
394  
395 2 for my $idx (0 .. $n_segs - 1) {
396 4 my $seg = $segments[$idx];
397 4 $seg =~ s/\A\s+//; $seg =~ s/\s+\z//;
398 4 ▶ next unless $seg =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $seg =~ /\S/ 0 4 4
399  
400 4 ▶ my $is_last = ($idx == $n_segs - 1) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$idx == $n_segs - 1 ? : 2 2 4
401 4 ▶ my $output_f = $is_last ? undef : "${base}_${idx}.tmp";
Branch: 1 branch
BranchTrueFalseTotal
$is_last ? : 2 2 4
402  
403 # --- redirect STDIN from previous segment output ---
404 4 my $saved_in = 0;
405 4 ▶ if (defined $input_f && -f $input_f) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input_f and -f $input_f) 2 2 4
Condition66%2/3
defined $input_f and -f $input_f
!ll&&!rl&&r
202
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
406 open(_PIPE_RFH, $input_f)
407 0 ▶ or do { _warn("pipe: open $input_f: $!"); last };
Branch: 1 branch
BranchTrueFalseTotal
open _PIPE_RFH, $input_f or do { _warn("pipe: open ${input_f}: $!"); last } 2 0 2
408 open(_PIPE_SAVIN, '<&STDIN')
409 0 ▶ or do { close(_PIPE_RFH); last };
Branch: 1 branch
BranchTrueFalseTotal
open _PIPE_SAVIN, "<&STDIN" or do { close _PIPE_RFH; last } 2 0 2
410 open(STDIN, '<&_PIPE_RFH')
411 0 ▶ or do { close(_PIPE_RFH); open(STDIN, '<&_PIPE_SAVIN'); close(_PIPE_SAVIN); last };
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<&_PIPE_RFH" or do { close _PIPE_RFH; open STDIN, "<&_PIPE_SAVIN"; close _PIPE_SAVIN; last } 2 0 2
412 2 close(_PIPE_RFH);
413 2 $saved_in = 1;
414 }
415  
416 # --- redirect STDOUT to next segment input file ---
417 4 my $saved_out = 0;
418 4 ▶ if (defined $output_f) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output_f) 2 2 4
419 open(_PIPE_WFH, ">$output_f")
420 2 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
open _PIPE_WFH, ">$output_f" or do { if ($saved_in) { open STDIN, "<&_PIPE_SAVIN"; close _PIPE_SAVIN; } ; _warn("pipe: open ${output_f}: $!"); last } 2 0 2
421 0 ▶ if ($saved_in) { open(STDIN, '<&_PIPE_SAVIN'); close(_PIPE_SAVIN) }
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_in) 0 0 0
422 0 _warn("pipe: open $output_f: $!");
423 0 last;
424 };
425 open(_PIPE_SAVOUT, '>&STDOUT')
426 2 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
open _PIPE_SAVOUT, ">&STDOUT" or do { close _PIPE_WFH; if ($saved_in) { open STDIN, "<&_PIPE_SAVIN"; close _PIPE_SAVIN; } ; last } 2 0 2
427 0 close(_PIPE_WFH);
428 0 ▶ if ($saved_in) { open(STDIN, '<&_PIPE_SAVIN'); close(_PIPE_SAVIN) }
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_in) 0 0 0
429 0 last;
430 };
431 open(STDOUT, '>&_PIPE_WFH')
432 2 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&_PIPE_WFH" or do { close _PIPE_WFH; open STDOUT, ">&_PIPE_SAVOUT"; close _PIPE_SAVOUT; if ($saved_in) { open STDIN, "<&_PIPE_SAVIN"; close _PIPE_SAVIN; } ; last } 2 0 2
433 0 close(_PIPE_WFH);
434 0 open(STDOUT, '>&_PIPE_SAVOUT'); close(_PIPE_SAVOUT);
435 0 ▶ if ($saved_in) { open(STDIN, '<&_PIPE_SAVIN'); close(_PIPE_SAVIN) }
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_in) 0 0 0
436 0 last;
437 };
438 2 close(_PIPE_WFH);
439 2 $saved_out = 1;
440 }
441  
442 # --- run the segment ---
443 4 $rc = _exec_single($class, $seg, $lines_ref, $labels_ref, $i_ref, $opts_ref);
444  
445 # --- restore STDOUT ---
446 4 ▶ if ($saved_out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_out) 2 2 4
447 2 open(STDOUT, '>&_PIPE_SAVOUT');
448 2 close(_PIPE_SAVOUT);
449 }
450  
451 # --- restore STDIN and clean up input tmpfile ---
452 4 ▶ if ($saved_in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_in) 2 2 4
453 2 open(STDIN, '<&_PIPE_SAVIN');
454 2 close(_PIPE_SAVIN);
455 2 unlink $input_f;
456 }
457  
458 4 $input_f = $output_f; # next segment reads what we just wrote
459 }
460  
461 # Clean up any leftover tmpfile (e.g. if last segment was skipped)
462 2 ▶ unlink $input_f if defined $input_f && -f $input_f;
Branch: 1 branch
BranchTrueFalseTotal
if defined $input_f and -f $input_f 0 2 2
Condition33%1/3
defined $input_f and -f $input_f
!ll&&!rl&&r
200
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
463  
464 2 return $rc;
465 }
466  
467 # ----------------------------------------------------------------
468 # _strip_redirects: parse > >> 2> 2>> < from end of command
469 # Returns ($clean_cmd, \@redirs) where @redirs = ([fd,mode,file], ...)
470 # ----------------------------------------------------------------
471 697 ▶sub _strip_redirects {
Subroutine _strip_redirects: called CC=23 SCAR=34.3
472 697 my ($line) = @_;
473 697 my @redirs;
474 # Parse redirects while respecting ^ escapes and quotes.
475 # A > or < preceded by ^ is NOT a redirect.
476 # Strategy: walk char-by-char to find bare (unescaped, unquoted) redirects.
477 697 my @chars = split //, $line;
478 697 my $n = scalar @chars;
479 697 my ($in_q, $i) = (0, 0);
480 697 my $clean = '';
481 697 my @found; # [pos_in_clean, fd, append, file_str]
482  
483 697 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 13560 697 14257
484 13560 my $ch = $chars[$i];
485 13560 ▶ if ($ch eq '^' && !$in_q) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "^" and not $in_q) 19 13541 13560
Condition66%2/3
$ch eq "^" and not $in_q
!ll&&!rl&&r
13541019
MC/DC50%1/2
$ch eq "^" and not $in_q
$ch eq "^" not $in_q
Truth table66%2/3
A and not(B)
A= $ch eq "^"B= not $in_q
ABresult
0X0
100
111
486 # Escape: pass through both ^ and next char as literals
487 19 $clean .= $ch;
488 19 $i++;
489 19 ▶ $clean .= $chars[$i] if $i < $n;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $n 19 0 19
490 19 $i++;
491 19 next;
492 }
493 13541 ▶ if ($ch eq '"') { $in_q = !$in_q; $clean .= $ch; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"") 182 13359 13541
494 13359 ▶ if (!$in_q && ($ch eq '>' || $ch eq '<')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_q and $ch eq ">" || $ch eq "<") 3 13356 13359
Condition83%5/6
not $in_q and $ch eq ">" || $ch eq "<"
!ll&&!rl&&r
367129893
$ch eq ">" or $ch eq "<"
l!l&&r!l&&!r
3012989
MC/DC66%2/3
not $in_q and $ch eq ">" || $ch eq "<"
not $in_q $ch eq ">" $ch eq "<"
Truth table75%3/4
not(A) and B or C
A= not $in_qB= $ch eq ">"C= $ch eq "<"
ABCresult
0XX0
1000
11X1
1011
495 3 my $fd = 1;
496 3 ▶ my $is_in = ($ch eq '<') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ch eq '<' ? : 0 3 3
497 # Check if the character immediately before (in clean, ignoring trailing space)
498 # is a bare fd digit that is not part of a word.
499 # Only '2' (stderr) and '1' (stdout explicit) are valid fd numbers in cmd.exe.
500 # We accept N> only if N is a single digit preceded by space/start-of-string.
501 3 ▶ if ($clean =~ s/(?:\A|(?<=[ \t]))([12])[ \t]*\z//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clean =~ s/(?:\A|(?<=[ \t]))([12])[ \t]*\z//) 0 3 3
502 0 $fd = int($1);
503 }
504 3 my $append = 0;
505 3 $i++;
506 3 ▶ if (!$is_in && $i < $n && $chars[$i] eq '>') { $append = 1; $i++ }
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_in and $i < $n and $chars[$i] eq ">") 1 2 3
Condition50%3/6
not $is_in and $i < $n and $chars[$i] eq ">"
!ll&&!rl&&r
021
not $is_in and $i < $n
!ll&&!rl&&r
003
MC/DC33%1/3
not $is_in and $i < $n and $chars[$i] eq ">"
not $is_in $i < $n $chars[$i] eq ">"
Truth table50%2/4
not(A) and B and C
A= not $is_inB= $i < $nC= $chars[$i] eq ">"
ABCresult
0XX0
10X0
1100
1111
507 # Skip whitespace before filename
508 3 ▶ $i++ while $i < $n && $chars[$i] =~ /[ \t]/;
Branch: 1 branch
BranchTrueFalseTotal
$i < $n and $chars[$i] =~ /[ \t]/ and do { ++$i } 3 3 6
Condition66%2/3
$i < $n and $chars[$i] =~ /[ \t]/
!ll&&!rl&&r
033
MC/DC50%1/2
$i < $n and $chars[$i] =~ /[ \t]/
$i < $n $chars[$i] =~ /[ \t]/
Truth table66%2/3
A and B
A= $i < $nB= $chars[$i] =~ /[ \t]/
ABresult
0X0
100
111
509 # Read filename (until space/tab or end)
510 3 my $file = '';
511 3 ▶ if ($i < $n && $chars[$i] eq '"') {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n and $chars[$i] eq '"') { } 0 3 3
Condition33%1/3
$i < $n and $chars[$i] eq "\""
!ll&&!rl&&r
030
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
512 0 $i++;
513 0 ▶ while ($i < $n && $chars[$i] ne '"') { $file .= $chars[$i++] }
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n and $chars[$i] ne "\"") 0 0 0
Condition0%0/3
$i < $n and $chars[$i] ne "\""
!ll&&!rl&&r
000
MC/DC0%0/2
$i < $n and $chars[$i] ne "\""
$i < $n $chars[$i] ne "\""
Truth table0%0/3
A and B
A= $i < $nB= $chars[$i] ne "\""
ABresult
0X0
100
111
514 0 $i++; # closing "
515 }
516 else {
517 3 ▶ while ($i < $n && $chars[$i] !~ /[ \t]/) { $file .= $chars[$i++] }
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n and not $chars[$i] =~ /[ \t]/) 72 3 75
Condition66%2/3
$i < $n and not $chars[$i] =~ /[ \t]/
!ll&&!rl&&r
3072
MC/DC50%1/2
$i < $n and not $chars[$i] =~ /[ \t]/
$i < $n not $chars[$i] =~ /[ \t]/
Truth table66%2/3
A and not(B)
A= $i < $nB= not $chars[$i] =~ /[ \t]/
ABresult
0X0
100
111
518 }
519 3 ▶ push @found, [$is_in ? 0 : $fd, $append, $file];
Branch: 1 branch
BranchTrueFalseTotal
$is_in ? : 0 3 3
520 3 next;
521 }
522 13356 $clean .= $ch; $i++;
523 }
524 697 $clean =~ s/\s+\z//;
525 697 return ($clean, \@found);
526 }
527  
528 # ----------------------------------------------------------------
529 # _dispatch_with_redirs: set up redirections then dispatch
530 # Perl 5.005_03 compatible: fixed bareword FHs, 2-argument open.
531 # ----------------------------------------------------------------
532  
533 # Fixed bareword filehandles used only inside _dispatch_with_redirs.
534 # Pre-declared at package level so they are valid under strict.
535 64 ▶use vars qw(*_REDIR_SRC *_REDIR_DST *_REDIR_SAVOUT *_REDIR_SAVERR *_REDIR_SAVIN);
Subroutine BEGIN: called CC=1 SCAR=0.0
536  
537 697 ▶sub _dispatch_with_redirs {
Subroutine _dispatch_with_redirs: called CC=32 SCAR=54.7
538 697 my ($class, $line, $redirs, $lines_ref, $labels_ref, $i_ref, $opts_ref) = @_;
539  
540 return _dispatch($class, $line, $lines_ref, $labels_ref, $i_ref, $opts_ref)
541 697 ▶ unless @{$redirs};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$redirs;} 694 3 697
542  
543 # Process redirections one at a time using fixed bareword FHs.
544 # We support one redirect per fd (last one wins, matching cmd.exe).
545 # Collect: stdin_file, stdout_file, stdout_append, stderr_file, stderr_append
546 3 my ($in_file, $out_file, $out_app, $err_file, $err_app);
547 3 for my $r (@{$redirs}) {
548 3 my ($fd, $append, $file) = @{$r};
549 3 $file = BATsh::MB::dec($file);
550 0 ▶ if ($fd == 0) { $in_file = $file }
Branch: 2 branches
BranchTrueFalseTotal
if ($fd == 0) { } 0 3 3
elsif ($fd == 1) { } 3 0 3
551 3 elsif ($fd == 1) { $out_file = $file; $out_app = $append }
552 0 else { $err_file = $file; $err_app = $append }
553 }
554  
555 3 my $ok = 1;
556 3 my ($saved_in, $saved_out, $saved_err) = (0, 0, 0);
557  
558 # stdin
559 3 ▶ if (defined $in_file && $ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $in_file and $ok) 0 3 3
Condition33%1/3
defined $in_file and $ok
!ll&&!rl&&r
300
MC/DC0%0/2
defined $in_file and $ok
defined $in_file $ok
Truth table33%1/3
A and B
A= defined $in_fileB= $ok
ABresult
0X0
100
111
560 0 ▶ open(_REDIR_SRC, $in_file) or do { _warn("Cannot open $in_file: $!"); $ok=0 };
Branch: 1 branch
BranchTrueFalseTotal
open _REDIR_SRC, $in_file or do { _warn("Cannot open ${in_file}: $!"); $ok = 0 } 0 0 0
561 0 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 0 0 0
562 0 ▶ open(_REDIR_SAVIN, '<&STDIN') or do { $ok=0 };
Branch: 1 branch
BranchTrueFalseTotal
open _REDIR_SAVIN, "<&STDIN" or do { $ok = 0 } 0 0 0
563 }
564 0 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 0 0 0
565 0 ▶ open(STDIN, '<&_REDIR_SRC') or do { $ok=0 };
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<&_REDIR_SRC" or do { $ok = 0 } 0 0 0
566 0 close(_REDIR_SRC);
567 0 $saved_in = 1;
568 }
569 }
570  
571 # stdout
572 3 ▶ if (defined $out_file && $ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out_file and $ok) 3 0 3
Condition33%1/3
defined $out_file and $ok
!ll&&!rl&&r
003
MC/DC0%0/2
defined $out_file and $ok
defined $out_file $ok
Truth table33%1/3
A and B
A= defined $out_fileB= $ok
ABresult
0X0
100
111
573 3 ▶ my $mode = $out_app ? '>>' : '>';
Branch: 1 branch
BranchTrueFalseTotal
$out_app ? : 1 2 3
574 0 ▶ open(_REDIR_DST, "$mode$out_file") or do { _warn("Cannot open $out_file: $!"); $ok=0 };
Branch: 1 branch
BranchTrueFalseTotal
open _REDIR_DST, "$mode$out_file" or do { _warn("Cannot open ${out_file}: $!"); $ok = 0 } 3 0 3
575 3 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 3 0 3
576 0 ▶ open(_REDIR_SAVOUT, '>&STDOUT') or do { $ok=0 };
Branch: 1 branch
BranchTrueFalseTotal
open _REDIR_SAVOUT, ">&STDOUT" or do { $ok = 0 } 3 0 3
577 }
578 3 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 3 0 3
579 0 ▶ open(STDOUT, '>&_REDIR_DST') or do { $ok=0 };
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&_REDIR_DST" or do { $ok = 0 } 3 0 3
580 3 close(_REDIR_DST);
581 3 $saved_out = 1;
582 }
583 }
584  
585 # stderr
586 3 ▶ if (defined $err_file && $ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err_file and $ok) 0 3 3
Condition33%1/3
defined $err_file and $ok
!ll&&!rl&&r
300
MC/DC0%0/2
defined $err_file and $ok
defined $err_file $ok
Truth table33%1/3
A and B
A= defined $err_fileB= $ok
ABresult
0X0
100
111
587 0 ▶ my $mode = $err_app ? '>>' : '>';
Branch: 1 branch
BranchTrueFalseTotal
$err_app ? : 0 0 0
588 0 ▶ open(_REDIR_DST, "$mode$err_file") or do { _warn("Cannot open $err_file: $!"); $ok=0 };
Branch: 1 branch
BranchTrueFalseTotal
open _REDIR_DST, "$mode$err_file" or do { _warn("Cannot open ${err_file}: $!"); $ok = 0 } 0 0 0
589 0 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 0 0 0
590 0 ▶ open(_REDIR_SAVERR, '>&STDERR') or do { $ok=0 };
Branch: 1 branch
BranchTrueFalseTotal
open _REDIR_SAVERR, ">&STDERR" or do { $ok = 0 } 0 0 0
591 }
592 0 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 0 0 0
593 0 ▶ open(STDERR, '>&_REDIR_DST') or do { $ok=0 };
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&_REDIR_DST" or do { $ok = 0 } 0 0 0
594 0 close(_REDIR_DST);
595 0 $saved_err = 1;
596 }
597 }
598  
599 3 my $rc = 0;
600 3 ▶ $rc = _dispatch($class, $line, $lines_ref, $labels_ref, $i_ref, $opts_ref) if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 3 0 3
601  
602 # Restore in reverse order
603 0 ▶ if ($saved_err) { open(STDERR, '>&_REDIR_SAVERR'); close(_REDIR_SAVERR) }
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_err) 0 3 3
604 3 ▶ if ($saved_out) { open(STDOUT, '>&_REDIR_SAVOUT'); close(_REDIR_SAVOUT) }
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_out) 3 0 3
605 0 ▶ if ($saved_in) { open(STDIN, '<&_REDIR_SAVIN'); close(_REDIR_SAVIN) }
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_in) 0 3 3
606  
607 3 return $rc;
608 }
609  
610 # ----------------------------------------------------------------
611 # Command dispatcher
612 # ----------------------------------------------------------------
613 697 ▶sub _dispatch {
Subroutine _dispatch: called CC=38 SCAR=39.2
614 697 my ($class, $line, $lines_ref, $labels_ref, $i_ref, $opts_ref) = @_;
615  
616 697 my ($cmd, $rest) = _split_cmd($line);
617 697 ▶ return 0 unless defined $cmd && $cmd ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cmd and $cmd ne "" 0 697 697
Condition33%1/3
defined $cmd and $cmd ne ""
!ll&&!rl&&r
00697
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
618  
619 697 my $CMD = uc($cmd);
620  
621 697 ▶ if ($CMD eq 'ECHO') { return _cmd_echo($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "ECHO") 276 421 697
622 0 ▶ if ($CMD eq '@ECHO') { return _cmd_echo($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "\@ECHO") 0 421 421
623 421 ▶ if ($CMD eq 'SET') { return _cmd_set($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "SET") 166 255 421
624 255 ▶ if ($CMD eq 'IF') { return _cmd_if($class, $line, $lines_ref, $labels_ref, $i_ref, $opts_ref) }
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "IF") 69 186 255
625 186 ▶ if ($CMD eq 'FOR') { return _cmd_for($class, $line, $lines_ref, $labels_ref, $i_ref, $opts_ref) }
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "FOR") 23 163 186
626 163 ▶ if ($CMD eq 'GOTO') {
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "GOTO") 63 100 163
627 63 $rest =~ s/\A\s+//; $rest =~ s/\s+\z//; $rest =~ s/\A://;
628 63 $_GOTO_LABEL = uc($rest);
629 63 return 0;
630 }
631 100 ▶ if ($CMD eq 'CALL') { return _cmd_call($class, $rest, $opts_ref) }
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "CALL") 39 61 100
632 61 ▶ if ($CMD eq 'SHIFT') { return _cmd_shift($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "SHIFT") 16 45 61
633 45 ▶ if ($CMD eq 'SETLOCAL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "SETLOCAL") 12 33 45
634 12 $rest =~ s/\A\s+//; $rest =~ s/\s+\z//;
635 12 BATsh::Env::setlocal($rest);
636 12 return 0;
637 }
638 33 ▶ if ($CMD eq 'ENDLOCAL') { BATsh::Env::endlocal(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "ENDLOCAL") 12 21 33
639 21 ▶ if ($CMD eq 'CD' || $CMD eq 'CHDIR') { return _cmd_cd($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "CD" or $CMD eq "CHDIR") 2 19 21
Condition66%2/3
$CMD eq "CD" or $CMD eq "CHDIR"
l!l&&r!l&&!r
2019
MC/DC50%1/2
$CMD eq "CD" or $CMD eq "CHDIR"
$CMD eq "CD" $CMD eq "CHDIR"
Truth table66%2/3
A or B
A= $CMD eq "CD"B= $CMD eq "CHDIR"
ABresult
1X1
011
000
640 0 ▶ if ($CMD eq 'DIR') { return _cmd_dir($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "DIR") 0 19 19
641 0 ▶ if ($CMD eq 'COPY') { return _cmd_copy($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "COPY") 0 19 19
642 19 ▶ if ($CMD eq 'DEL' || $CMD eq 'ERASE') { return _cmd_del($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "DEL" or $CMD eq "ERASE") 1 18 19
Condition66%2/3
$CMD eq "DEL" or $CMD eq "ERASE"
l!l&&r!l&&!r
1018
MC/DC50%1/2
$CMD eq "DEL" or $CMD eq "ERASE"
$CMD eq "DEL" $CMD eq "ERASE"
Truth table66%2/3
A or B
A= $CMD eq "DEL"B= $CMD eq "ERASE"
ABresult
1X1
011
000
643 0 ▶ if ($CMD eq 'MOVE') { return _cmd_move($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "MOVE") 0 18 18
644 18 ▶ if ($CMD eq 'MKDIR' || $CMD eq 'MD') { return _cmd_mkdir($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "MKDIR" or $CMD eq "MD") 1 17 18
Condition66%2/3
$CMD eq "MKDIR" or $CMD eq "MD"
l!l&&r!l&&!r
1017
MC/DC50%1/2
$CMD eq "MKDIR" or $CMD eq "MD"
$CMD eq "MKDIR" $CMD eq "MD"
Truth table66%2/3
A or B
A= $CMD eq "MKDIR"B= $CMD eq "MD"
ABresult
1X1
011
000
645 17 ▶ if ($CMD eq 'RMDIR' || $CMD eq 'RD') { return _cmd_rmdir($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "RMDIR" or $CMD eq "RD") 1 16 17
Condition66%2/3
$CMD eq "RMDIR" or $CMD eq "RD"
l!l&&r!l&&!r
1016
MC/DC50%1/2
$CMD eq "RMDIR" or $CMD eq "RD"
$CMD eq "RMDIR" $CMD eq "RD"
Truth table66%2/3
A or B
A= $CMD eq "RMDIR"B= $CMD eq "RD"
ABresult
1X1
011
000
646 0 ▶ if ($CMD eq 'REN' || $CMD eq 'RENAME') { return _cmd_rename($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "REN" or $CMD eq "RENAME") 0 16 16
Condition33%1/3
$CMD eq "REN" or $CMD eq "RENAME"
l!l&&r!l&&!r
0016
MC/DC0%0/2
$CMD eq "REN" or $CMD eq "RENAME"
$CMD eq "REN" $CMD eq "RENAME"
Truth table33%1/3
A or B
A= $CMD eq "REN"B= $CMD eq "RENAME"
ABresult
1X1
011
000
647 16 ▶ if ($CMD eq 'TYPE') { return _cmd_type($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "TYPE") 4 12 16
648 12 ▶ if ($CMD eq 'PAUSE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "PAUSE") 0 12 12
649 0 print "Press any key to continue . . . ";
650 0 my $ch = '';
651 0 eval { local $| = 1; require POSIX; POSIX::tcgetattr(0); read(STDIN, $ch, 1) };
652 0 print "\n";
653 0 return 0;
654 }
655 12 ▶ if ($CMD eq 'EXIT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "EXIT") 6 6 12
656 6 $rest =~ s/\A\s+//;
657 6 ▶ my $is_b = ($rest =~ s{/B\s*}{}i) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rest =~ s[/B\s*][]i ? : 5 1 6
658 6 $rest =~ s/\A\s+//;
659 # EXIT n / EXIT /B n set the exit code; a bare EXIT [/B] keeps the
660 # current ERRORLEVEL (cmd.exe: exit code of the batch is the last
661 # ERRORLEVEL when no code is given).
662 6 ▶ $ERRORLEVEL = int($rest) if $rest =~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $rest =~ /\A\d+\z/ 5 1 6
663 6 $_EXIT_REQUESTED = 1;
664 6 return '__EXIT__';
665 }
666 0 ▶ if ($CMD eq 'CLS') { print "\033[2J\033[H"; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "CLS") 0 6 6
667 6 ▶ if ($CMD eq 'TITLE') { print "\033]0;$rest\007"; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "TITLE") 1 5 6
668 5 ▶ if ($CMD eq 'VER') { print "BATsh [Version $BATsh::VERSION]\n"; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "VER") 1 4 5
669 4 ▶ if ($CMD eq 'PUSHD') {
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "PUSHD") 1 3 4
670 1 $rest =~ s/\A\s+//; $rest =~ s/\s+\z//;
671 1 push @{$opts_ref->{'_pushd_stack'}}, Cwd::cwd();
672 1 return _cmd_cd($rest);
673 }
674 3 ▶ if ($CMD eq 'POPD') {
Branch: 1 branch
BranchTrueFalseTotal
if ($CMD eq "POPD") 1 2 3
675 1 ▶ if (defined $opts_ref->{'_pushd_stack'} && @{$opts_ref->{'_pushd_stack'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts_ref->{"_pushd_stack"} and @{$opts_ref->{"_pushd_stack"};}) 1 0 1
Condition33%1/3
defined $opts_ref->{"_pushd_stack"} and @{$opts_ref->{"_pushd_stack"};}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $opts_ref->{"_pushd_stack"} and @{$opts_ref->{"_pushd_stack"};}
defined $opts_ref->{"_pushd_stack"} @{$opts_ref->{"_pushd_stack"};}
Truth table33%1/3
A and B
A= defined $opts_ref->{"_pushd_stack"}B= @{$opts_ref->{"_pushd_stack"};}
ABresult
0X0
100
111
676 1 chdir(pop @{$opts_ref->{'_pushd_stack'}});
677 }
678 1 return 0;
679 }
680  
681 2 return _cmd_external($cmd, $rest);
682 }
683  
684 # ----------------------------------------------------------------
685 # ECHO (does NOT reset ERRORLEVEL -- cmd.exe compatible)
686 # ----------------------------------------------------------------
687 276 ▶sub _cmd_echo {
Subroutine _cmd_echo: called CC=6 SCAR=21.3
688 276 my ($rest) = @_;
689 276 $rest =~ s/\A\s+//;
690  
691 276 ▶ if ($rest =~ /\AOFF\s*\z/i) { $ECHO_ON = 0; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /\AOFF\s*\z/i) 37 239 276
692 0 ▶ if ($rest =~ /\AON\s*\z/i) { $ECHO_ON = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /\AON\s*\z/i) 0 239 239
693 0 ▶ if ($rest =~ /\A\.\s*\z/) { print "\n"; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /\A\.\s*\z/) 0 239 239
694 0 ▶ if ($rest =~ /\A\s*\z/) { print "ECHO is " . ($ECHO_ON ? "on" : "off") . "\n"; return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if ($rest =~ /\A\s*\z/) 0 239 239
$ECHO_ON ? : 0 0 0
695  
696 # Remove ^ escapes for display (they were protection, not content)
697 239 $rest = _unescape_caret($rest);
698 239 print BATsh::MB::dec($rest), "\n";
699 # ERRORLEVEL intentionally NOT modified here
700 239 return 0;
701 }
702  
703 # ----------------------------------------------------------------
704 # SET
705 # ----------------------------------------------------------------
706 166 ▶sub _cmd_set {
Subroutine _cmd_set: called CC=12 SCAR=30.3
707 166 my ($rest) = @_;
708 166 $rest =~ s/\A\s+//;
709  
710 # SET /P VAR=PromptString (interactive prompt input)
711 166 ▶ if ($rest =~ s/\A\/P\s*//i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s[\A/P\s*][]i) 3 163 166
712 3 ▶ if ($rest =~ /\A([A-Za-z_][A-Za-z0-9_]*)\s*=(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /\A([A-Za-z_][A-Za-z0-9_]*)\s*=(.*)/) 3 0 3
713 3 my ($var, $prompt) = ($1, $2);
714 3 print BATsh::MB::dec($prompt);
715 3 my $input = <STDIN>;
716 3 ▶ $input = '' unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 3 3
717 3 chomp $input;
718 3 BATsh::Env->set($var, BATsh::MB::enc($input));
719 3 $ERRORLEVEL = 0;
720 }
721 3 return 0;
722 }
723  
724 # SET /A
725 163 ▶ if ($rest =~ s/\A\/A\s*//i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s[\A/A\s*][]i) 70 93 163
726 70 ▶ if ($rest =~ /\A\s*([A-Za-z_][A-Za-z0-9_]*)\s*=(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /\A\s*([A-Za-z_][A-Za-z0-9_]*)\s*=(.*)/) { } 70 0 70
727 70 BATsh::Env->set($1, _eval_arith($2));
728 }
729 else {
730 0 print _eval_arith($rest) . "\n";
731 }
732 70 $ERRORLEVEL = 0;
733 70 return 0;
734 }
735  
736 # SET with no args: display all
737 93 ▶ if ($rest =~ /\A\s*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /\A\s*\z/) 0 93 93
738 0 for my $k (sort keys %BATsh::Env::STORE) {
739 0 print "$k=", BATsh::MB::dec($BATsh::Env::STORE{$k}), "\n";
740 }
741 0 return 0;
742 }
743  
744 # SET VAR=value (variable name may contain spaces before =)
745 93 ▶ if ($rest =~ /\A([^=]+?)\s*=(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /\A([^=]+?)\s*=(.*)/) 93 0 93
746 93 BATsh::Env->set($1, $2);
747 93 $ERRORLEVEL = 0;
748 93 return 0;
749 }
750  
751 # SET VAR (display matching prefix)
752 0 ▶ if ($rest =~ /\A(\S+)\s*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /\A(\S+)\s*\z/) 0 0 0
753 0 my $prefix = uc($1);
754 0 for my $k (sort keys %BATsh::Env::STORE) {
755 0 ▶ if (index(uc($k), $prefix) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index(uc $k, $prefix) == 0) 0 0 0
756 0 print "$k=", BATsh::MB::dec($BATsh::Env::STORE{$k}), "\n";
757 }
758 }
759 0 return 0;
760 }
761  
762 0 return 0;
763 }
764  
765 # ----------------------------------------------------------------
766 # SET /A arithmetic evaluator
767 # Supports: + - * / % ^ & | ~ << >> () hex (0x) and variable refs
768 # ----------------------------------------------------------------
769 70 ▶sub _eval_arith {
Subroutine _eval_arith: called CC=5 SCAR=16.4
770 70 my ($expr) = @_;
771 # Expand variable names
772 70 $expr =~ s/([A-Za-z_][A-Za-z0-9_]*)/
773 5 ▶ do { my $v = BATsh::Env->get($1); defined $v && $v =~ m|^-?\d+$| ? $v : 0 }
Branch: 1 branch
BranchTrueFalseTotal
defined $v && $v =~ /^-?\d+$/ ? : 4 1 5
Condition66%2/3
defined $v and $v =~ /^-?\d+$/
!ll&&!rl&&r
104
MC/DC50%1/2
defined $v and $v =~ /^-?\d+$/
defined $v $v =~ /^-?\d+$/
Truth table66%2/3
A and B
A= defined $vB= $v =~ /^-?\d+$/
ABresult
0X0
100
111
774 /ge;
775 # Convert 0xHEX literals
776 0 $expr =~ s/0x([0-9A-Fa-f]+)/hex($1)/ge;
777 # %% -> % (modulo)
778 70 $expr =~ s/%%/%/g;
779 # Safe eval: digits, operators, hex chars already substituted
780 70 ▶ if ($expr =~ /\A[\d\s\+\-\*\/\%\(\)\^\&\|\~\<\>]+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ m[\A[\d\s\+\-\*/\%\(\)\^\&\|\~\<\>]+\z]) 70 0 70
781 # Perl ^ is XOR, same as cmd.exe SET /A
782 70 my $result = eval $expr;
783 70 ▶ return defined $result ? int($result) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $result ? : 69 1 70
784 }
785 0 return 0;
786 }
787  
788 # ----------------------------------------------------------------
789 # IF
790 #
791 # cmd.exe parse order:
792 # IF [NOT] /I "a"=="b" ... (case-insensitive string)
793 # IF [NOT] ERRORLEVEL n ...
794 # IF [NOT] EXIST path ...
795 # IF [NOT] DEFINED var ...
796 # IF [NOT] "a"=="b" ... (case-sensitive string)
797 #
798 # IMPORTANT: /I must be checked BEFORE plain == to avoid /I being
799 # consumed as part of the left-hand operand.
800 #
801 # Block expansion: the THEN/ELSE bodies of a parenthesised IF block
802 # are expanded at parse time (before any SET inside runs), matching
803 # cmd.exe's behaviour. Only !VAR! (delayed) is deferred to runtime.
804 # ----------------------------------------------------------------
805 69 ▶sub _cmd_if {
Subroutine _cmd_if: called CC=21 SCAR=30.5
806 69 my ($class, $line, $lines_ref, $labels_ref, $i_ref, $opts_ref) = @_;
807  
808 69 (my $rest = $line) =~ s/\AIF\s+//i;
809  
810 69 my $negate = 0;
811 69 ▶ if ($rest =~ s/\ANOT\s+//i) { $negate = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/\ANOT\s+//i) 4 65 69
812  
813 69 my $condition = 0;
814  
815 # /I must be tried first
816 69 ▶ if ($rest =~ s/\A\/I\s+//i) {
Branch: 5 branches
BranchTrueFalseTotal
if ($rest =~ s[\A/I\s+][]i) { } 2 67 69
elsif ($rest =~ s/\AERRORLEVEL\s+(\d+)\s*//i) { } 5 62 67
elsif ($rest =~ s/\AEXIST\s+//i) { } 5 57 62
elsif ($rest =~ s/\ADEFINED\s+(\S+)\s*//i) { } 3 54 57
elsif ($rest =~ s/\A("(?:[^"]*)"|[^\s=][^\s=]*)\s*==\s*("(?:[^"]*)"|[^\s=]*)\s*//) { } 54 0 54
817 # Case-insensitive comparison
818 2 ▶ if ($rest =~ s/\A("(?:[^"]*)"|[^\s=][^\s=]*)\s*==\s*("(?:[^"]*)"|[^\s=]*)\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/\A("(?:[^"]*)"|[^\s=][^\s=]*)\s*==\s*("(?:[^"]*)"|[^\s=]*)\s*//) 2 0 2
819 2 my ($a, $b) = ($1, $2);
820 2 $a =~ s/\A"//; $a =~ s/"\z//;
821 2 $b =~ s/\A"//; $b =~ s/"\z//;
822 2 ▶ $condition = (lc($a) eq lc($b)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
lc $a eq lc $b ? : 2 0 2
823 }
824 }
825 # ERRORLEVEL n
826 elsif ($rest =~ s/\AERRORLEVEL\s+(\d+)\s*//i) {
827 5 ▶ $condition = ($ERRORLEVEL >= int($1)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ERRORLEVEL >= int $1 ? : 4 1 5
828 }
829 # EXIST path (handles quoted paths with spaces)
830 elsif ($rest =~ s/\AEXIST\s+//i) {
831 5 my $path;
832 5 ▶ if ($rest =~ s/\A"([^"]+)"\s*//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rest =~ s/\A"([^"]+)"\s*//) { } 2 3 5
elsif ($rest =~ s/\A(\S+)\s*//) { } 3 0 3
833 2 $path = $1;
834 }
835 elsif ($rest =~ s/\A(\S+)\s*//) {
836 3 $path = $1;
837 }
838 5 ▶ $condition = (defined $path && -e BATsh::MB::dec($path)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $path && -e BATsh::MB::dec($path) ? : 4 1 5
Condition66%2/3
defined $path and -e BATsh::MB::dec($path)
!ll&&!rl&&r
014
MC/DC50%1/2
defined $path and -e BATsh::MB::dec($path)
defined $path -e BATsh::MB::dec($path)
Truth table66%2/3
A and B
A= defined $pathB= -e BATsh::MB::dec($path)
ABresult
0X0
100
111
839 }
840 # DEFINED var
841 elsif ($rest =~ s/\ADEFINED\s+(\S+)\s*//i) {
842 3 ▶ $condition = BATsh::Env->exists_var($1) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
'BATsh::Env'->exists_var($1) ? : 2 1 3
843 }
844 # "str"=="str" or str==str (case-sensitive)
845 elsif ($rest =~ s/\A("(?:[^"]*)"|[^\s=][^\s=]*)\s*==\s*("(?:[^"]*)"|[^\s=]*)\s*//) {
846 54 my ($a, $b) = ($1, $2);
847 54 $a =~ s/\A"//; $a =~ s/"\z//;
848 54 $b =~ s/\A"//; $b =~ s/"\z//;
849 54 ▶ $condition = ($a eq $b) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$a eq $b ? : 20 34 54
850 }
851  
852 69 ▶ $condition = !$condition if $negate;
Branch: 1 branch
BranchTrueFalseTotal
if $negate 4 65 69
853  
854 69 my ($then_body, $else_body) = _parse_if_bodies($rest, $lines_ref, $i_ref);
855  
856 # Block expansion: expand %VAR% in the bodies NOW (parse-time),
857 # before any commands inside the block execute.
858 # !VAR! is NOT expanded here (that is deferred to execute-time via Env).
859 69 ▶ $then_body = _block_expand($then_body) if defined $then_body;
Branch: 1 branch
BranchTrueFalseTotal
if defined $then_body 69 0 69
860 69 ▶ $else_body = _block_expand($else_body) if defined $else_body;
Branch: 1 branch
BranchTrueFalseTotal
if defined $else_body 11 58 69
861  
862 69 ▶ if ($condition) {
Branch: 2 branches
BranchTrueFalseTotal
if ($condition) { } 36 33 69
elsif (defined $else_body) { } 5 28 33
863 36 return _exec_body($class, $then_body, $lines_ref, $labels_ref, $i_ref, $opts_ref, 1);
864 }
865 elsif (defined $else_body) {
866 5 return _exec_body($class, $else_body, $lines_ref, $labels_ref, $i_ref, $opts_ref, 1);
867 }
868 28 return 0;
869 }
870  
871 # ----------------------------------------------------------------
872 # _block_expand: expand %VAR% in a block string at parse time.
873 # Protects %%V FOR loop variables and !VAR! delayed references.
874 # ----------------------------------------------------------------
875 89 ▶sub _block_expand {
Subroutine _block_expand: called CC=3 SCAR=11.1
876 89 my ($body) = @_;
877 89 ▶ return $body unless defined $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 0 89 89
878 # Protect !VAR! -- replace with placeholder to survive %% pass
879 89 $body =~ s/(!(?:[A-Za-z_][A-Za-z0-9_]*)!)/"\x00DELAY\x00$1\x00DELAY\x00"/ge;
880 # Protect %%V
881 0 $body =~ s/%%([A-Za-z])/"\x00FOR_$1\x00"/ge;
882 # Expand %VAR%
883 89 $body =~ s/%([^%\r\n]+)%/
884 8 ▶ do { my $k=uc($1); exists($BATsh::Env::STORE{$k}) ? $BATsh::Env::STORE{$k} : '' }
Branch: 1 branch
BranchTrueFalseTotal
exists $BATsh::Env::STORE{$k} ? : 8 0 8
885 /ge;
886 # %% -> %
887 89 $body =~ s/%%/%/g;
888 # Restore
889 89 $body =~ s/\x00FOR_([A-Za-z])\x00/%%$1/g;
890 89 $body =~ s/\x00DELAY\x00(!(?:[A-Za-z_][A-Za-z0-9_]*)!)\x00DELAY\x00/$1/g;
891 89 return $body;
892 }
893  
894 69 ▶sub _parse_if_bodies {
Subroutine _parse_if_bodies: called CC=8 SCAR=21.9
895 69 my ($rest, $lines_ref, $i_ref) = @_;
896 69 my ($then_body, $else_body);
897 69 $rest =~ s/\A\s+//;
898 69 ▶ if ($rest =~ s/\A\(//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/\A\(//) { } 18 51 69
899 # First try the fully-inline single-line form on THIS physical
900 # line: ( then-body ) [ ELSE ( else-body ) | ELSE else-body ]
901 # _match_paren finds the ')' that closes the just-opened '(' on
902 # the same line (quote- and nesting-aware). If the block instead
903 # spans following lines (no closing ')' here), fall back to the
904 # multi-line reader.
905 18 my ($inner, $after) = _match_paren($rest);
906 18 ▶ if (defined $inner) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $inner) { } 2 16 18
907 2 $then_body = $inner;
908 2 $after =~ s/\A\s+//;
909 2 ▶ if ($after =~ s/\AELSE\s*//i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($after =~ s/\AELSE\s*//i) 2 0 2
910 2 ▶ if ($after =~ /\A\(/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($after =~ /\A\(/) { } 2 0 2
elsif ($after =~ /\S/) { } 0 0 0
911 2 $after =~ s/\A\(//;
912 2 my ($einner) = _match_paren($after);
913 2 ▶ $else_body = defined($einner) ? $einner : $after;
Branch: 1 branch
BranchTrueFalseTotal
defined $einner ? : 2 0 2
914 }
915 elsif ($after =~ /\S/) {
916 0 $else_body = $after;
917 }
918 }
919 }
920 else {
921 16 $then_body = _read_paren_block($rest, $lines_ref, $i_ref, \$else_body);
922 }
923 }
924 else {
925 0 ▶ if ($rest =~ s/\s+ELSE\s+(.+)\z//i) { $else_body = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/\s+ELSE\s+(.+)\z//i) 0 51 51
926 51 $then_body = $rest;
927 }
928 69 return ($then_body, $else_body);
929 }
930  
931 # _match_paren: $str begins with the content right AFTER an opening '('
932 # (depth already 1). Scan for the matching ')' at depth 0, honouring
933 # double quotes. Returns (inside_content, remainder_after_close) when
934 # the paren closes within $str, or (undef, undef) when it does not
935 # (i.e. the block continues on following physical lines).
936 20 ▶sub _match_paren {
Subroutine _match_paren: called CC=8 SCAR=23.1
937 20 my ($str) = @_;
938 20 my @c = split //, $str;
939 20 my $n = scalar @c;
940 20 my $dep = 0;
941 20 my $in_q = 0;
942 20 my $i = 0;
943 20 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 31 16 47
944 31 my $ch = $c[$i];
945 0 ▶ if ($ch eq '"') { $in_q = !$in_q; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"") 0 31 31
946 31 ▶ if (!$in_q) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_q) 31 0 31
947 0 ▶ if ($ch eq '(') { $dep++; $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "(") 0 31 31
948 31 ▶ if ($ch eq ')') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ")") 4 27 31
949 4 ▶ if ($dep == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dep == 0) 4 0 4
950 4 my $inside = join('', @c[0 .. $i-1]);
951 4 ▶ my $after = ($i+1 <= $n-1) ? join('', @c[$i+1 .. $n-1]) : '';
Branch: 1 branch
BranchTrueFalseTotal
$i + 1 <= $n - 1 ? : 2 2 4
952 4 return ($inside, $after);
953 }
954 0 $dep--; $i++; next;
955 }
956 }
957 27 $i++;
958 }
959 16 return (undef, undef);
960 }
961  
962 34 ▶sub _read_paren_block {
Subroutine _read_paren_block: called CC=17 SCAR=29.6
963 34 my ($first_content, $lines_ref, $i_ref, $else_ref) = @_;
964 34 my @body = ();
965 34 ▶ push @body, $first_content if defined $first_content && $first_content =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $first_content and $first_content =~ /\S/ 0 34 34
Condition33%1/3
defined $first_content and $first_content =~ /\S/
!ll&&!rl&&r
0340
MC/DC0%0/2
defined $first_content and $first_content =~ /\S/
defined $first_content $first_content =~ /\S/
Truth table33%1/3
A and B
A= defined $first_contentB= $first_content =~ /\S/
ABresult
0X0
100
111
966 34 my $depth = 1;
967 34 ▶ while ($$i_ref <= $#{$lines_ref}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$i_ref <= $#{$lines_ref;}) 81 0 81
968 81 my $l = $lines_ref->[$$i_ref];
969 81 $$i_ref++;
970 81 $l =~ s/\r?\n\z//;
971 81 my $ls = $l; $ls =~ s/\A\s+//;
972 81 ▶ if ($depth == 1 && $ls =~ /\A\)\s*ELSE\s*\(\s*\z/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 1 and $ls =~ /\A\)\s*ELSE\s*\(\s*\z/i) 9 72 81
Condition100%3/3
$depth == 1 and $ls =~ /\A\)\s*ELSE\s*\(\s*\z/i
!ll&&!rl&&r
8649
MC/DC100%2/2
$depth == 1 and $ls =~ /\A\)\s*ELSE\s*\(\s*\z/i
$depth == 1 $ls =~ /\A\)\s*ELSE\s*\(\s*\z/i
Truth table100%3/3
A and B
A= $depth == 1B= $ls =~ /\A\)\s*ELSE\s*\(\s*\z/i
ABresult
0X0
100
111
973 9 ▶ if (defined $else_ref) { $$else_ref = _read_paren_block('', $lines_ref, $i_ref) }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $else_ref) 9 0 9
974 9 last;
975 }
976 72 ▶ if ($depth == 1 && $ls =~ /\A\)\s*ELSE\s+(.+)\z/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 1 and $ls =~ /\A\)\s*ELSE\s+(.+)\z/i) 0 72 72
Condition66%2/3
$depth == 1 and $ls =~ /\A\)\s*ELSE\s+(.+)\z/i
!ll&&!rl&&r
8640
MC/DC0%0/2
$depth == 1 and $ls =~ /\A\)\s*ELSE\s+(.+)\z/i
$depth == 1 $ls =~ /\A\)\s*ELSE\s+(.+)\z/i
Truth table66%2/3
A and B
A= $depth == 1B= $ls =~ /\A\)\s*ELSE\s+(.+)\z/i
ABresult
0X0
100
111
977 0 ▶ if (defined $else_ref) { $$else_ref = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $else_ref) 0 0 0
978 0 last;
979 }
980 72 my ($delta, $in_q) = (0, 0);
981 72 for my $ch (split //, $l) {
982 1292 ▶ if ($ch eq '"') { $in_q = !$in_q }
Branch: 2 branches
BranchTrueFalseTotal
if ($ch eq '"') { } 4 1288 1292
elsif (not $in_q) { } 1280 8 1288
983 1280 ▶ elsif (!$in_q) { $delta++ if $ch eq '('; $delta-- if $ch eq ')' }
Branch: 2 branches
BranchTrueFalseTotal
if $ch eq "(" 8 1272 1280
if $ch eq ")" 33 1247 1280
984 }
985 72 $depth += $delta;
986 72 ▶ if ($depth <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth <= 0) 25 47 72
987 25 $l =~ s/\)\s*\z//;
988 25 ▶ push @body, $l if $l =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /\S/ 0 25 25
989 25 last;
990 }
991 47 push @body, $l;
992 }
993 34 return join("\n", @body);
994 }
995  
996 75 ▶sub _exec_body {
Subroutine _exec_body: called CC=11 SCAR=25.6
997 75 my ($class, $body, $lines_ref, $labels_ref, $i_ref, $opts_ref, $pre_expanded) = @_;
998 75 ▶ return 0 unless defined $body && $body =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body and $body =~ /\S/ 0 75 75
Condition33%1/3
defined $body and $body =~ /\S/
!ll&&!rl&&r
0075
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
999 75 ▶ $pre_expanded = 0 unless defined $pre_expanded;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pre_expanded 0 75 75
1000 75 my @sub_lines = split /\n/, $body;
1001 75 my $sub_i = 0;
1002 75 my %sub_labels = ();
1003 75 for my $j (0 .. $#sub_lines) {
1004 92 my $ls = $sub_lines[$j]; $ls =~ s/\A\s+//;
1005 92 ▶ if ($ls =~ /\A:([A-Za-z_][A-Za-z0-9_]*)\s*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ls =~ /\A:([A-Za-z_][A-Za-z0-9_]*)\s*\z/) 0 92 92
1006 0 $sub_labels{uc($1)} = $j;
1007 }
1008 }
1009 75 ▶ while ($sub_i <= $#sub_lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub_i <= $#sub_lines) 84 65 149
1010 84 my $sl = $sub_lines[$sub_i];
1011 84 $sub_i++;
1012 # For pre_expanded blocks: still need to handle !VAR! at runtime
1013 84 my $rc = _exec_line($class, $sl, \@sub_lines, { %sub_labels }, \$sub_i, $opts_ref, $pre_expanded);
1014 84 ▶ return $rc if defined $rc && $rc eq '__EXIT__';
Branch: 1 branch
BranchTrueFalseTotal
if defined $rc and $rc eq "__EXIT__" 0 84 84
Condition33%1/3
defined $rc and $rc eq "__EXIT__"
!ll&&!rl&&r
0840
MC/DC0%0/2
defined $rc and $rc eq "__EXIT__"
defined $rc $rc eq "__EXIT__"
Truth table33%1/3
A and B
A= defined $rcB= $rc eq "__EXIT__"
ABresult
0X0
100
111
1015 84 ▶ if ($_GOTO_LABEL ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_GOTO_LABEL ne "") 10 74 84
1016 10 my $lbl = $_GOTO_LABEL;
1017 10 $_GOTO_LABEL = '';
1018 10 ▶ if (exists $sub_labels{$lbl}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sub_labels{$lbl}) { } 0 10 10
1019 0 $sub_i = $sub_labels{$lbl} + 1;
1020 }
1021 else {
1022 10 $_GOTO_LABEL = $lbl;
1023 10 return 0;
1024 }
1025 }
1026 }
1027 65 return 0;
1028 }
1029  
1030 # ----------------------------------------------------------------
1031 # FOR
1032 # ----------------------------------------------------------------
1033 23 ▶sub _cmd_for {
Subroutine _cmd_for: called CC=12 SCAR=25.2
1034 23 my ($class, $line, $lines_ref, $labels_ref, $i_ref, $opts_ref) = @_;
1035  
1036 # FOR /F "options" %%V IN (source) DO cmd
1037 23 ▶ if ($line =~ /\AFOR\s+\/F\s+("(?:[^"]*)"|'(?:[^']*)'|[^\s]+)\s+(?:%%|\x00FOR_)([A-Za-z])(?:\x00)?\s+IN\s*\(([^)]*)\)\s+DO\s+(.*)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[\AFOR\s+/F\s+("(?:[^"]*)"|'(?:[^']*)'|[^\s]+)\s+(?:%%|\x00FOR_)([A-Za-z])(?:\x00)?\s+IN\s*\(([^)]*)\)\s+DO\s+(.*)]i) 7 16 23
1038 7 return _cmd_for_f($class, $1, $2, $3, $4, $lines_ref, $labels_ref, $i_ref, $opts_ref);
1039 }
1040  
1041 # FOR /L %%V IN (start,step,end) DO cmd
1042 16 ▶ if ($line =~ /\AFOR\s+\/L\s+(?:%%|\x00FOR_)([A-Za-z])(?:\x00)?\s+IN\s*\(([^)]*)\)\s+DO\s+(.*)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[\AFOR\s+/L\s+(?:%%|\x00FOR_)([A-Za-z])(?:\x00)?\s+IN\s*\(([^)]*)\)\s+DO\s+(.*)]i) 8 8 16
1043 8 my ($var, $range, $do_part) = ($1, $2, $3);
1044 8 my ($start, $step, $end) = split /,/, $range;
1045 8 ▶ $start = defined $start ? int($start) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $start ? : 8 0 8
1046 8 ▶ $step = defined $step ? int($step) : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $step ? : 8 0 8
1047 8 ▶ $end = defined $end ? int($end) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $end ? : 8 0 8
1048 8 ▶ $step = 1 if $step == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $step == 0 0 8 8
1049 return _for_iterate($class, $var, $do_part, $lines_ref, $labels_ref, $i_ref, $opts_ref,
1050 8 ▶ sub { # generator: returns list of values
Subroutine __ANON__: called CC=5 SCAR=16.1
1051 8 my @vals;
1052 8 my $v = $start;
1053 8 ▶ while (($step > 0 && $v <= $end) || ($step < 0 && $v >= $end)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step > 0 and $v <= $end or $step < 0 and $v >= $end) 34 8 42
Condition100%9/9
$step > 0 and $v <= $end or $step < 0 and $v >= $end
l!l&&r!l&&!r
3048
$step > 0 and $v <= $end
!ll&&!rl&&r
5730
$step < 0 and $v >= $end
!ll&&!rl&&r
714
MC/DC75%3/4
$step > 0 and $v <= $end or $step < 0 and $v >= $end
$step > 0 $v <= $end $step < 0 $v >= $end
Truth table57%4/7
A and B or C and D
A= $step > 0B= $v <= $endC= $step < 0D= $v >= $end
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
1054 34 push @vals, $v;
1055 34 $v += $step;
1056 }
1057 8 return @vals;
1058 8 });
1059 }
1060  
1061 # FOR %%V IN (list) DO cmd
1062 8 ▶ if ($line =~ /\AFOR\s+(?:%%|\x00FOR_)([A-Za-z])(?:\x00)?\s+IN\s*\(([^)]*)\)\s+DO\s+(.*)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\AFOR\s+(?:%%|\x00FOR_)([A-Za-z])(?:\x00)?\s+IN\s*\(([^)]*)\)\s+DO\s+(.*)/i) 8 0 8
1063 8 my ($var, $list_str, $do_part) = ($1, $2, $3);
1064 8 my @items = split /[\s,]+/, $list_str;
1065 8 my @expanded = ();
1066 8 for my $item (@items) {
1067 23 $item =~ s/\A\s+//; $item =~ s/\s+\z//;
1068 23 ▶ next if $item eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $item eq "" 0 23 23
1069 23 ▶ if ($item =~ /[*?]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ /[*?]/) { } 1 22 23
1070 1 my @g = map { BATsh::MB::enc($_) }
1071 BATsh::SH::_glob_paths(BATsh::MB::dec($item));
1072 1 ▶ push @expanded, @g ? @g : ($item);
Branch: 1 branch
BranchTrueFalseTotal
@g ? : 1 0 1
1073 }
1074 22 else { push @expanded, $item }
1075 }
1076 return _for_iterate($class, $var, $do_part, $lines_ref, $labels_ref, $i_ref, $opts_ref,
1077 8 ▶ sub { return @expanded });
Subroutine __ANON__: called CC=1 SCAR=0.0
1078 }
1079  
1080 0 _warn("FOR: unsupported syntax: $line");
1081 0 return 1;
1082 }
1083  
1084 # ----------------------------------------------------------------
1085 # _for_iterate: common FOR loop body runner
1086 # ----------------------------------------------------------------
1087 16 ▶sub _for_iterate {
Subroutine _for_iterate: called CC=6 SCAR=17.9
1088 16 my ($class, $var, $do_part, $lines_ref, $labels_ref, $i_ref, $opts_ref, $gen) = @_;
1089  
1090 # Pre-read paren body if do_part is "("
1091 16 my $paren_body_template = undef;
1092 {
1093 16 my $probe = $do_part;
1094 16 $probe =~ s/\x00FOR_[A-Za-z]\x00//g;
1095 16 $probe =~ s/\x00PCT_[^\x00]+\x00//g;
1096 16 $probe =~ s/%%[A-Za-z]//g;
1097 16 ▶ if ($probe =~ /\A\s*\(\s*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($probe =~ /\A\s*\(\s*\z/) 9 7 16
1098 9 $paren_body_template = _read_paren_block('', $lines_ref, $i_ref);
1099 }
1100 }
1101  
1102 # If we have a paren block, expand %VAR% ONCE at FOR-parse time
1103 # (cmd.exe expands the whole block before any iteration runs).
1104 # PCT placeholders (%%V protected vars) are restored to %VAR% first,
1105 # then _block_expand runs the single-pass %VAR% substitution.
1106 # The result is a template with loop-var placeholders still intact.
1107 16 my $paren_expanded = undef;
1108 16 ▶ if (defined $paren_body_template) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $paren_body_template) 9 7 16
1109 9 my $tpl = $paren_body_template;
1110 # Restore PCT placeholders -> %VAR% so _block_expand can see them
1111 9 $tpl =~ s/\x00PCT_([^\x00]+)\x00/%$1%/g;
1112 # But protect the loop variable itself from _block_expand
1113 # (it will be substituted per-iteration below)
1114 9 $tpl =~ s/%%$var/"\x00LOOPVAR\x00"/ge;
1115 0 $tpl =~ s/\x00FOR_$var\x00/"\x00LOOPVAR\x00"/ge;
1116 # Single-pass %VAR% expansion at FOR-line parse time
1117 9 $paren_expanded = _block_expand($tpl);
1118 # _block_expand already restored other %%X -> %%X, leave loop placeholder
1119 }
1120  
1121 16 my @values = $gen->();
1122  
1123 16 for my $val (@values) {
1124 57 BATsh::Env->set("%%$var", $val);
1125  
1126 57 ▶ if (defined $paren_expanded) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $paren_expanded) { } 34 23 57
1127 # Substitute loop variable placeholder with current value
1128 34 my $body = $paren_expanded;
1129 34 $body =~ s/\x00LOOPVAR\x00/$val/g;
1130 # At runtime: if delayed expansion is on, expand !VAR!
1131 # _exec_body with pre_expanded=1 handles this via _exec_line
1132 34 _exec_body($class, $body, $lines_ref, $labels_ref, $i_ref, $opts_ref, 1);
1133 }
1134 else {
1135 23 my $do_line = $do_part;
1136 # Replace loop variable placeholder/shorthand with current value
1137 23 $do_line =~ s/%%$var/$val/g;
1138 23 $do_line =~ s/\x00FOR_$var\x00/$val/g;
1139 # Restore other FOR-variable placeholders to %%X form
1140 23 $do_line =~ s/\x00FOR_([A-Za-z])\x00/%%$1/g;
1141 # Restore %VAR% placeholders so expand_cmd can expand them
1142 23 $do_line =~ s/\x00PCT_([^\x00]+)\x00/%$1%/g;
1143 23 $do_line = BATsh::Env->expand_cmd($do_line);
1144 23 _exec_line($class, $do_line, $lines_ref, $labels_ref, $i_ref, $opts_ref, 1);
1145 }
1146 57 ▶ last if $_GOTO_LABEL ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $_GOTO_LABEL ne "" 0 57 57
1147 }
1148 16 return 0;
1149 }
1150  
1151 # ----------------------------------------------------------------
1152 # FOR /F
1153 #
1154 # Options string (inside quotes): tokens= delims= skip= eol= usebackq
1155 # Source:
1156 # "filename" -- iterate lines of file (or usebackq: command output)
1157 # 'command' -- command output (or usebackq: literal filename)
1158 # ("string") -- tokenize the string itself
1159 # ----------------------------------------------------------------
1160 7 ▶sub _cmd_for_f {
Subroutine _cmd_for_f: called CC=44 SCAR=49.9
1161 7 my ($class, $opts_str, $var, $source_str, $do_part, $lines_ref, $labels_ref, $i_ref, $opts_ref) = @_;
1162  
1163 # Strip outer quotes from opts_str
1164 7 $opts_str =~ s/\A"//; $opts_str =~ s/"\z//;
1165  
1166 # Parse options
1167 7 my $tokens_spec = '1'; # default: first token only
1168 7 my $delims = " \t"; # default delimiters
1169 7 my $skip = 0;
1170 7 my $eol = ';'; # default: skip lines starting with ;
1171 7 my $usebackq = 0;
1172  
1173 7 ▶ $usebackq = 1 if $opts_str =~ /usebackq/i;
Branch: 1 branch
BranchTrueFalseTotal
if $opts_str =~ /usebackq/i 0 7 7
1174 7 ▶ if ($opts_str =~ /tokens=(\S+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts_str =~ /tokens=(\S+)/i) 5 2 7
1175 5 $tokens_spec = $1;
1176 5 $tokens_spec =~ s/,\z//;
1177 }
1178 7 ▶ if ($opts_str =~ /delims=([^\s"]*)/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts_str =~ /delims=([^\s"]*)/i) { } 2 5 7
elsif ($opts_str =~ /delims=\s*\z/i) { } 0 5 5
1179 2 $delims = $1;
1180 2 ▶ $delims = ' ' if $delims eq ''; # delims= (empty) means no split? No: empty = space only
Branch: 1 branch
BranchTrueFalseTotal
if $delims eq "" 0 2 2
1181 }
1182 elsif ($opts_str =~ /delims=\s*\z/i) {
1183 0 $delims = ''; # delims= with nothing = no delimiter (whole line = one token)
1184 }
1185 7 ▶ if ($opts_str =~ /skip=(\d+)/i) { $skip = int($1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($opts_str =~ /skip=(\d+)/i) 1 6 7
1186 7 ▶ if ($opts_str =~ /eol=(.)/i) { $eol = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opts_str =~ /eol=(.)/i) 1 6 7
1187  
1188 # Parse tokens spec: e.g. "1,2,3" "1-3" "1,2*" "*"
1189 7 my @token_indices = _parse_tokens_spec($tokens_spec);
1190 7 ▶ my $want_star = ($tokens_spec =~ /\*/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$tokens_spec =~ /\*/ ? : 1 6 7
1191  
1192 # Determine source lines
1193 7 my @lines_to_process;
1194 7 $source_str =~ s/\A\s+//; $source_str =~ s/\s+\z//;
1195  
1196 7 ▶ if ($source_str =~ /\A'([^']*)'\z/ || ($usebackq && $source_str =~ /\A`([^`]*)`\z/)) {
Branch: 4 branches
BranchTrueFalseTotal
if ($source_str =~ /\A'([^']*)'\z/ or $usebackq and $source_str =~ /\A`([^`]*)`\z/) { } 0 7 7
elsif ($usebackq and $source_str =~ /\A"([^"]*)"\z/) { } 0 7 7
elsif ($source_str =~ /\A"([^"]*)"\z/ and not $usebackq) { } 3 4 7
elsif ($source_str =~ /\A(\S+)\z/) { } 4 0 4
Condition33%2/6
$source_str =~ /\A'([^']*)'\z/ or $usebackq and $source_str =~ /\A`([^`]*)`\z/
l!l&&r!l&&!r
007
$usebackq and $source_str =~ /\A`([^`]*)`\z/
!ll&&!rl&&r
700
MC/DC0%0/3
$source_str =~ /\A'([^']*)'\z/ or $usebackq and $source_str =~ /\A`([^`]*)`\z/
$source_str =~ /\A'([^']*)'\z/ $usebackq $source_str =~ /\A`([^`]*)`\z/
Truth table25%1/4
A or B and C
A= $source_str =~ /\A'([^']*)'\z/B= $usebackqC= $source_str =~ /\A`([^`]*)`\z/
ABCresult
1XX1
0111
00X0
0100
1197 # Command output
1198 0 my $cmd = BATsh::MB::dec($1);
1199 0 BATsh::Env->sync_to_env();
1200 0 local *CMDOUT;
1201 0 ▶ open(CMDOUT, "$cmd |") or return 1;
Branch: 1 branch
BranchTrueFalseTotal
open CMDOUT, "$cmd |" or return 1 0 0 0
1202 0 @lines_to_process = map { BATsh::MB::enc($_) } <CMDOUT>;
1203 0 close(CMDOUT);
1204 }
1205 ▶ elsif ($usebackq && $source_str =~ /\A"([^"]*)"\z/) {
Condition33%1/3
$usebackq and $source_str =~ /\A"([^"]*)"\z/
!ll&&!rl&&r
700
MC/DC0%0/2
$usebackq and $source_str =~ /\A"([^"]*)"\z/
$usebackq $source_str =~ /\A"([^"]*)"\z/
Truth table33%1/3
A and B
A= $usebackqB= $source_str =~ /\A"([^"]*)"\z/
ABresult
0X0
100
111
1206 # usebackq: "..." = filename
1207 0 my $file = BATsh::MB::dec($1);
1208 0 local *FFH;
1209 0 ▶ open(FFH, $file) or do { _warn("FOR /F: cannot open $file"); return 1 };
Branch: 1 branch
BranchTrueFalseTotal
open FFH, $file or do { _warn("FOR /F: cannot open $file"); return 1 } 0 0 0
1210 0 @lines_to_process = map { BATsh::MB::enc($_) } <FFH>;
1211 0 close(FFH);
1212 }
1213 ▶ elsif ($source_str =~ /\A"([^"]*)"\z/ && !$usebackq) {
Condition66%2/3
$source_str =~ /\A"([^"]*)"\z/ and not $usebackq
!ll&&!rl&&r
403
MC/DC50%1/2
$source_str =~ /\A"([^"]*)"\z/ and not $usebackq
$source_str =~ /\A"([^"]*)"\z/ not $usebackq
Truth table66%2/3
A and not(B)
A= $source_str =~ /\A"([^"]*)"\z/B= not $usebackq
ABresult
0X0
100
111
1214 # No usebackq: "string" = literal string to tokenize
1215 3 @lines_to_process = ("$1\n");
1216 }
1217 elsif ($source_str =~ /\A(\S+)\z/) {
1218 # Bare filename
1219 4 my $file = BATsh::MB::dec($1);
1220 4 local *FFH2;
1221 0 ▶ open(FFH2, $file) or do { _warn("FOR /F: cannot open $file"); return 1 };
Branch: 1 branch
BranchTrueFalseTotal
open FFH2, $file or do { _warn("FOR /F: cannot open $file"); return 1 } 4 0 4
1222 4 @lines_to_process = map { BATsh::MB::enc($_) } <FFH2>;
1223 4 close(FFH2);
1224 }
1225 else {
1226 0 _warn("FOR /F: cannot parse source: $source_str");
1227 0 return 1;
1228 }
1229  
1230 # Skip leading lines
1231 7 ▶ splice(@lines_to_process, 0, $skip) if $skip > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $skip > 0 1 6 7
1232  
1233 # Pre-read paren body if needed
1234 7 my $paren_body = undef;
1235 {
1236 7 my $probe = $do_part;
1237 7 $probe =~ s/%%[A-Za-z]//g;
1238 7 ▶ if ($probe =~ /\A\s*\(\s*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($probe =~ /\A\s*\(\s*\z/) 0 7 7
1239 0 $paren_body = _read_paren_block('', $lines_ref, $i_ref);
1240 }
1241 }
1242  
1243 # Determine variable names: %%a and following letters for extra tokens
1244 7 my @var_names;
1245 7 for my $i (0 .. $#token_indices) {
1246 10 push @var_names, chr(ord($var) + $i);
1247 }
1248 # Star token goes to the next letter after the listed ones
1249 7 my $star_var = chr(ord($var) + scalar @token_indices);
1250  
1251 7 for my $src_line (@lines_to_process) {
1252 9 $src_line =~ s/\r?\n\z//;
1253 # Skip eol lines
1254 9 ▶ next if $eol ne '' && $src_line =~ /\A\Q$eol\E/;
Branch: 1 branch
BranchTrueFalseTotal
if $eol ne "" and $src_line =~ /\A\Q$eol\E/ 1 8 9
Condition66%2/3
$eol ne "" and $src_line =~ /\A\Q$eol\E/
!ll&&!rl&&r
081
MC/DC50%1/2
$eol ne "" and $src_line =~ /\A\Q$eol\E/
$eol ne "" $src_line =~ /\A\Q$eol\E/
Truth table66%2/3
A and B
A= $eol ne ""B= $src_line =~ /\A\Q$eol\E/
ABresult
0X0
100
111
1255 8 ▶ next if $src_line =~ /\A\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $src_line =~ /\A\s*\z/ 0 8 8
1256  
1257 # Tokenize
1258 8 my @tokens;
1259 8 ▶ if ($delims eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($delims eq '') { } 0 8 8
1260 0 @tokens = ($src_line);
1261 }
1262 else {
1263 8 my $escaped_delims = quotemeta($delims);
1264 8 @tokens = split /[$escaped_delims]+/, $src_line;
1265 # cmd.exe skips leading delimiters
1266 8 ▶ if ($src_line =~ /\A[$escaped_delims]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src_line =~ /\A[$escaped_delims]/) 0 8 8
1267 0 ▶ shift @tokens if @tokens && $tokens[0] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if @tokens and $tokens[0] eq "" 0 0 0
Condition0%0/3
@tokens and $tokens[0] eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
@tokens and $tokens[0] eq ""
@tokens $tokens[0] eq ""
Truth table0%0/3
A and B
A= @tokensB= $tokens[0] eq ""
ABresult
0X0
100
111
1268 }
1269 }
1270  
1271 # Assign to variables
1272 8 for my $i (0 .. $#token_indices) {
1273 12 my $tidx = $token_indices[$i] - 1; # 0-based
1274 12 ▶ BATsh::Env->set("%%$var_names[$i]", defined $tokens[$tidx] ? $tokens[$tidx] : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $tokens[$tidx] ? : 12 0 12
1275 }
1276 8 ▶ if ($want_star && @tokens > $token_indices[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_star and @tokens > $token_indices[-1]) 1 7 8
Condition66%2/3
$want_star and @tokens > $token_indices[-1]
!ll&&!rl&&r
701
MC/DC50%1/2
$want_star and @tokens > $token_indices[-1]
$want_star @tokens > $token_indices[-1]
Truth table66%2/3
A and B
A= $want_starB= @tokens > $token_indices[-1]
ABresult
0X0
100
111
1277 # Star: remainder from token N onwards joined by first delimiter
1278 1 ▶ my $delim1 = length($delims) > 0 ? substr($delims, 0, 1) : ' ';
Branch: 1 branch
BranchTrueFalseTotal
length $delims > 0 ? : 1 0 1
1279 1 my $remainder = join($delim1, @tokens[$token_indices[-1] .. $#tokens]);
1280 1 BATsh::Env->set("%%$star_var", $remainder);
1281 }
1282  
1283 # Execute body
1284 8 ▶ if (defined $paren_body) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $paren_body) { } 0 8 8
1285 0 my $body = $paren_body;
1286 # Restore any \x00FOR_x\x00 placeholders before substituting values
1287 0 $body =~ s/\x00FOR_([A-Za-z])\x00/%%$1/g;
1288 0 ▶ for my $vn (@var_names, $want_star ? ($star_var) : ()) {
Branch: 1 branch
BranchTrueFalseTotal
$want_star ? : 0 0 0
1289 0 ▶ my $val = defined(BATsh::Env->get("%%$vn")) ? BATsh::Env->get("%%$vn") : '';
Branch: 1 branch
BranchTrueFalseTotal
defined 'BATsh::Env'->get("%%$vn") ? : 0 0 0
1290 0 $body =~ s/%%$vn/$val/g;
1291 }
1292 0 $body = _block_expand($body);
1293 0 _exec_body($class, $body, $lines_ref, $labels_ref, $i_ref, $opts_ref, 1);
1294 }
1295 else {
1296 8 my $do_line = $do_part;
1297 # Restore \x00FOR_x\x00 -> %%x, then substitute values
1298 8 $do_line =~ s/\x00FOR_([A-Za-z])\x00/%%$1/g;
1299 8 ▶ for my $vn (@var_names, $want_star ? ($star_var) : ()) {
Branch: 1 branch
BranchTrueFalseTotal
$want_star ? : 1 7 8
1300 13 ▶ my $val = defined(BATsh::Env->get("%%$vn")) ? BATsh::Env->get("%%$vn") : '';
Branch: 1 branch
BranchTrueFalseTotal
defined 'BATsh::Env'->get("%%$vn") ? : 13 0 13
1301 13 $do_line =~ s/%%$vn/$val/g;
1302 }
1303 8 $do_line = BATsh::Env->expand_cmd($do_line);
1304 8 _exec_line($class, $do_line, $lines_ref, $labels_ref, $i_ref, $opts_ref, 1);
1305 }
1306 8 ▶ last if $_GOTO_LABEL ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $_GOTO_LABEL ne "" 0 8 8
1307 }
1308 7 return 0;
1309 }
1310  
1311 # ----------------------------------------------------------------
1312 # _parse_tokens_spec: "1,2,3-5,*" -> (1,2,3,4,5)
1313 # ----------------------------------------------------------------
1314 7 ▶sub _parse_tokens_spec {
Subroutine _parse_tokens_spec: called CC=6 SCAR=18.8
1315 7 my ($spec) = @_;
1316 7 $spec =~ s/\*//g; # star handled separately
1317 7 my @indices;
1318 7 for my $part (split /,/, $spec) {
1319 10 $part =~ s/\A\s+//; $part =~ s/\s+\z//;
1320 10 ▶ next unless $part =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $part =~ /\S/ 0 10 10
1321 10 ▶ if ($part =~ /\A(\d+)-(\d+)\z/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($part =~ /\A(\d+)-(\d+)\z/) { } 0 10 10
elsif ($part =~ /\A(\d+)\z/) { } 10 0 10
1322 0 push @indices, ($1 .. $2);
1323 }
1324 elsif ($part =~ /\A(\d+)\z/) {
1325 10 push @indices, $1;
1326 }
1327 }
1328 7 ▶ @indices = (1) unless @indices;
Branch: 1 branch
BranchTrueFalseTotal
unless @indices 0 7 7
1329 7 return @indices;
1330 }
1331  
1332 # ----------------------------------------------------------------
1333 # CALL
1334 # ----------------------------------------------------------------
1335 39 ▶sub _cmd_call {
Subroutine _cmd_call: called CC=5 SCAR=22.5
1336 39 my ($class, $rest, $opts_ref) = @_;
1337 39 $rest =~ s/\A\s+//;
1338  
1339 39 ▶ if ($rest =~ /\A:([A-Za-z_][A-Za-z0-9_]*)(.*)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /\A:([A-Za-z_][A-Za-z0-9_]*)(.*)/i) 39 0 39
1340 39 my ($lbl, $argstr) = (uc($1), $2);
1341 # $rest reaches here already %-expanded by _expand_line, so split the
1342 # argument string with double-quote awareness and let call_sub install
1343 # the %0..%9 / %* frame (and restore the caller's on return).
1344 39 my @args = _split_call_args($argstr);
1345 39 ▶ if (defined $opts_ref->{'_batsh'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts_ref->{"_batsh"}) 39 0 39
1346 39 $opts_ref->{'_batsh'}->call_sub($lbl, @args);
1347 }
1348 39 return 0;
1349 }
1350  
1351 0 ▶ if ($rest =~ /(\S+\.batsh)(.*)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /(\S+\.batsh)(.*)/i) 0 0 0
1352 0 my $file = $1;
1353 0 ▶ if (defined $opts_ref->{'_batsh'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts_ref->{"_batsh"}) 0 0 0
1354 0 $opts_ref->{'_batsh'}->source_file($file);
1355 }
1356 0 return 0;
1357 }
1358  
1359 # CALL cmd args: execute with double-expansion
1360 # Re-expand the already-expanded string (second pass)
1361 0 my $reexpanded = BATsh::Env->expand_cmd($rest);
1362 0 return _cmd_external($reexpanded, '');
1363 }
1364  
1365 # ----------------------------------------------------------------
1366 # _split_call_args: split a CALL argument string into words, honouring
1367 # double quotes (cmd.exe uses double quotes only). Surrounding quotes
1368 # are removed.
1369 # ----------------------------------------------------------------
1370 39 ▶sub _split_call_args {
Subroutine _split_call_args: called CC=9 SCAR=22.0
1371 39 my ($s) = @_;
1372 39 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 39 39
1373 39 $s =~ s/\A\s+//; $s =~ s/\s+\z//;
1374 39 ▶ return () if $s eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "" 3 36 39
1375 36 my @out;
1376 36 my $cur = '';
1377 36 my $have = 0;
1378 36 my $in_q = 0;
1379 36 for my $c (split //, $s) {
1380 247 ▶ if ($c eq '"') { $in_q = !$in_q; $have = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\"") 4 243 247
1381 243 ▶ if ($c =~ /\s/ && !$in_q) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /\s/ and not $in_q) 21 222 243
Condition100%3/3
$c =~ /\s/ and not $in_q
!ll&&!rl&&r
219321
MC/DC100%2/2
$c =~ /\s/ and not $in_q
$c =~ /\s/ not $in_q
Truth table100%3/3
A and not(B)
A= $c =~ /\s/B= not $in_q
ABresult
0X0
100
111
1382 21 ▶ if ($have) { push @out, $cur; $cur = ''; $have = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($have) 21 0 21
1383 21 next;
1384 }
1385 222 $cur .= $c; $have = 1;
1386 }
1387 36 ▶ push @out, $cur if $have;
Branch: 1 branch
BranchTrueFalseTotal
if $have 36 0 36
1388 36 return @out;
1389 }
1390  
1391 # ----------------------------------------------------------------
1392 # SHIFT [/N] -- shift positional parameters %1..%9 left, rebuild %*
1393 #
1394 # Plain SHIFT moves %2 into %1, %3 into %2, ... %9 into %8, and clears
1395 # %9. SHIFT /N begins the shift at %N (parameters %1..%(N-1) are left
1396 # unchanged), matching cmd.exe. %0 is left untouched (BATsh's documented
1397 # contract shifts %1..%9 and %*; this also keeps a CALL'd label's name
1398 # stable). The SH-side BATSH_ARG* mirror is updated so an SH subroutine
1399 # body sees the same shift via $1..$9.
1400 # ----------------------------------------------------------------
1401 16 ▶sub _cmd_shift {
Subroutine _cmd_shift: called CC=12 SCAR=25.2
1402 16 my ($rest) = @_;
1403 16 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 16 16
1404 16 my $start = 1;
1405 16 ▶ if ($rest =~ /\/(\d)/) { $start = int($1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ m[/(\d)]) 1 15 16
1406 16 ▶ $start = 1 if $start < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $start < 1 0 16 16
1407 16 ▶ $start = 9 if $start > 9;
Branch: 1 branch
BranchTrueFalseTotal
if $start > 9 0 16 16
1408  
1409 16 ▶ for (my $n = $start; $n < 9; $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 9) 127 16 143
1410 127 my $next = BATsh::Env->get('%' . ($n + 1));
1411 127 ▶ BATsh::Env->set('%' . $n, defined($next) ? $next : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $next ? : 127 0 127
1412 }
1413 16 BATsh::Env->set('%9', '');
1414  
1415 # Rebuild %* from the contiguous run of non-empty positional params.
1416 16 my @remaining;
1417 16 for my $n (1 .. 9) {
1418 39 my $v = BATsh::Env->get("%$n");
1419 39 ▶ last unless defined $v && $v ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and $v ne "" 16 23 39
Condition66%2/3
defined $v and $v ne ""
!ll&&!rl&&r
01623
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
1420 23 push @remaining, $v;
1421 }
1422 16 BATsh::Env->set('%*', join(' ', @remaining));
1423  
1424 # Keep the SH-side mirror consistent with the shifted %1..%9.
1425 16 for my $n (1 .. 9) {
1426 144 my $v = BATsh::Env->get("%$n");
1427 144 ▶ $BATsh::Env::STORE{"BATSH_ARG$n"} = defined $v ? $v : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 144 0 144
1428 }
1429 16 $BATsh::Env::STORE{'BATSH_ARGC'} = scalar @remaining;
1430  
1431 16 $ERRORLEVEL = 0;
1432 16 return 0;
1433 }
1434  
1435 # ----------------------------------------------------------------
1436 # CD / CHDIR
1437 # ----------------------------------------------------------------
1438 3 ▶sub _cmd_cd {
Subroutine _cmd_cd: called CC=4 SCAR=15.8
1439 3 my ($rest) = @_;
1440 3 $rest = BATsh::MB::dec($rest);
1441 3 $rest =~ s/\A\s+//; $rest =~ s/\s+\z//;
1442 3 $rest =~ s/\A"//; $rest =~ s/"\z//;
1443 3 ▶ if ($rest eq '' || $rest =~ /\A\/D\s*\z/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest eq "" or $rest =~ m[\A/D\s*\z]i) 0 3 3
Condition33%1/3
$rest eq "" or $rest =~ m[\A/D\s*\z]i
l!l&&r!l&&!r
003
MC/DC0%0/2
$rest eq "" or $rest =~ m[\A/D\s*\z]i
$rest eq "" $rest =~ m[\A/D\s*\z]i
Truth table33%1/3
A or B
A= $rest eq ""B= $rest =~ m[\A/D\s*\z]i
ABresult
1X1
011
000
1444 0 print Cwd::cwd(), "\n";
1445 0 return 0;
1446 }
1447 3 $rest =~ s/\A\/D\s*//i;
1448 3 ▶ unless (chdir($rest)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chdir $rest) 0 3 3
1449 0 print "The system cannot find the path specified.\n";
1450 0 $ERRORLEVEL = 1;
1451 0 return 1;
1452 }
1453 3 BATsh::Env->set('CD', BATsh::MB::enc(Cwd::cwd()));
1454 3 $ERRORLEVEL = 0;
1455 3 return 0;
1456 }
1457  
1458 # ----------------------------------------------------------------
1459 # _cmd_strip_switches: remove a command's own /X switches from its
1460 # argument string. The pattern used before v0.09 -- s{\s*/[A-Za-z:]+}{}g
1461 # -- deleted every "/word" run, so it also ate the directory components
1462 # of a forward-slash pathname: DEL /tmp/log/*.tmp lost "/tmp" and "/log"
1463 # and then complained about a file called "\*.tmp". A switch is now only
1464 # removed when its letter is one this command actually has (optionally
1465 # followed by :attributes) and it stands as a word of its own.
1466 # ----------------------------------------------------------------
1467 1 ▶sub _cmd_strip_switches {
Subroutine _cmd_strip_switches: called CC=4 SCAR=13.9
1468 1 my ($str, $letters) = @_;
1469 1 ▶ return '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 1 1
1470 1 my $again = 1;
1471 1 ▶ while ($again) {
Branch: 1 branch
BranchTrueFalseTotal
if ($again) 2 1 3
1472 2 $again = 0;
1473 2 ▶ $again = 1 if $str =~ s{\A/([$letters])(?::[-A-Za-z]*)?(?=\s|\z)\s*}{}i;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ s[\A/([$letters])(?::[-A-Za-z]*)?(?=\s|\z)\s*][]i 1 1 2
1474 }
1475 1 $str =~ s{\s+/([$letters])(?::[-A-Za-z]*)?(?=\s|\z)}{}ig;
1476 1 return $str;
1477 }
1478  
1479 # ----------------------------------------------------------------
1480 # DIR
1481 # ----------------------------------------------------------------
1482 0 ▶sub _cmd_dir {
Subroutine _cmd_dir: not called CC=10 SCAR=47.0
1483  
1484 0 my ($rest) = @_;
1485 0 $rest = BATsh::MB::dec($rest);
1486 0 $rest =~ s/\A\s+//; $rest =~ s/\s+\z//;
1487 0 ▶ my $target = $rest eq '' ? '.' : $rest;
Branch: 1 branch
BranchTrueFalseTotal
$rest eq '' ? : 0 0 0
1488 0 $target = _cmd_strip_switches($target, 'ABSPWOQTLNXCR');
1489 0 $target =~ s/\s+\z//;
1490 0 ▶ $target = '.' if $target eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $target eq "" 0 0 0
1491 0 $target =~ s/\A"//; $target =~ s/"\z//;
1492 0 ▶ unless (-e $target) { print "File Not Found\n"; $ERRORLEVEL = 1; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $target) 0 0 0
1493 0 local *DH;
1494 0 ▶ if (-d $target) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $target) { } 0 0 0
1495 0 ▶ opendir(DH, $target) or do { print "Access denied.\n"; return 1 };
Branch: 1 branch
BranchTrueFalseTotal
opendir DH, $target or do { print "Access denied.\n"; return 1 } 0 0 0
1496 0 my @entries = sort readdir(DH);
1497 0 closedir(DH);
1498 0 print " Directory of $target\n\n";
1499 0 for my $e (@entries) {
1500 0 ▶ next if $e eq '.' || $e eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $e eq "." or $e eq ".." 0 0 0
Condition0%0/3
$e eq "." or $e eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$e eq "." or $e eq ".."
$e eq "." $e eq ".."
Truth table0%0/3
A or B
A= $e eq "."B= $e eq ".."
ABresult
1X1
011
000
1501 0 my $full = "$target/$e";
1502 0 ▶ if (-d $full) { printf "%-40s <DIR>\n", $e }
Branch: 1 branch
BranchTrueFalseTotal
if (-d $full) { } 0 0 0
1503 0 else { printf "%-40s %12d\n", $e, (-s $full) }
1504 }
1505 }
1506 0 else { printf "%-40s %12d\n", $target, (-s $target) }
1507 0 $ERRORLEVEL = 0;
1508 0 return 0;
1509 }
1510  
1511 # ----------------------------------------------------------------
1512 # File operations
1513 # ----------------------------------------------------------------
1514 0 ▶sub _cmd_copy {
Subroutine _cmd_copy: not called CC=4 SCAR=30.0
1515 0 my ($rest) = @_;
1516 0 $rest = BATsh::MB::dec($rest);
1517 0 $rest =~ s/\A\s+//; $rest =~ s/\s*\/[YN]\s*//gi;
1518 0 my ($src, $dst) = split /\s+/, $rest, 2;
1519 0 ▶ unless (defined $src && defined $dst) { print "The syntax of the command is incorrect.\n"; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $src and defined $dst) 0 0 0
Condition0%0/3
defined $src and defined $dst
!ll&&!rl&&r
000
MC/DC0%0/2
defined $src and defined $dst
defined $src defined $dst
Truth table0%0/3
A and B
A= defined $srcB= defined $dst
ABresult
0X0
100
111
1520 0 $src =~ s/\A"//; $src =~ s/"\z//;
1521 0 $dst =~ s/\A"//; $dst =~ s/"\z//;
1522 0 ▶ unless (File::Copy::copy($src, $dst)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (File::Copy::copy($src, $dst)) 0 0 0
1523 0 print "The system cannot find the file specified.\n"; $ERRORLEVEL = 1; return 1
1524 }
1525 0 print " 1 file(s) copied.\n"; $ERRORLEVEL = 0; return 0;
1526 }
1527  
1528 1 ▶sub _cmd_del {
Subroutine _cmd_del: called CC=4 SCAR=13.9
1529 1 my ($rest) = @_;
1530 1 $rest = BATsh::MB::dec($rest);
1531 1 $rest =~ s/\A\s+//;
1532 1 $rest = _cmd_strip_switches($rest, 'PFSQA');
1533 1 $rest =~ s/\s+\z//;
1534 1 $rest =~ s/\A"//; $rest =~ s/"\z//;
1535 1 my @files = BATsh::SH::_glob_paths($rest);
1536 1 ▶ @files = ($rest) unless @files;
Branch: 1 branch
BranchTrueFalseTotal
unless @files 0 1 1
1537 1 for my $f (@files) {
1538 2 ▶ unlink($f) or print "Could not find $f\n";
Branch: 1 branch
BranchTrueFalseTotal
unlink $f or print "Could not find $f\n" 2 0 2
1539 }
1540 1 $ERRORLEVEL = 0; return 0;
1541 }
1542  
1543 0 ▶sub _cmd_move {
Subroutine _cmd_move: not called CC=4 SCAR=30.0
1544 0 my ($rest) = @_;
1545 0 $rest = BATsh::MB::dec($rest);
1546 0 $rest =~ s/\A\s+//; $rest =~ s/\s*\/[YN]\s*//gi;
1547 0 my ($src, $dst) = split /\s+/, $rest, 2;
1548 0 ▶ unless (defined $src && defined $dst) { print "The syntax of the command is incorrect.\n"; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $src and defined $dst) 0 0 0
Condition0%0/3
defined $src and defined $dst
!ll&&!rl&&r
000
MC/DC0%0/2
defined $src and defined $dst
defined $src defined $dst
Truth table0%0/3
A and B
A= defined $srcB= defined $dst
ABresult
0X0
100
111
1549 0 $src =~ s/\A"//; $src =~ s/"\z//;
1550 0 $dst =~ s/\A"//; $dst =~ s/"\z//;
1551 0 ▶ unless (File::Copy::move($src, $dst)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (File::Copy::move($src, $dst)) 0 0 0
1552 0 print "The system cannot find the file specified.\n"; $ERRORLEVEL = 1; return 1
1553 }
1554 0 print " 1 file(s) moved.\n"; $ERRORLEVEL = 0; return 0;
1555 }
1556  
1557 1 ▶sub _cmd_mkdir {
Subroutine _cmd_mkdir: called CC=2 SCAR=7.3
1558 1 my ($rest) = @_;
1559 1 $rest = BATsh::MB::dec($rest);
1560 1 $rest =~ s/\A\s+//; $rest =~ s/\s+\z//; $rest =~ s/\A"//; $rest =~ s/"\z//;
1561 0 ▶ if (-d $rest) { print "A subdirectory or file $rest already exists.\n"; $ERRORLEVEL = 1; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if (-d $rest) 0 1 1
1562 1 File::Path::mkpath($rest); $ERRORLEVEL = 0; return 0;
1563 }
1564  
1565 1 ▶sub _cmd_rmdir {
Subroutine _cmd_rmdir: called CC=4 SCAR=15.1
1566 1 my ($rest) = @_;
1567 1 $rest = BATsh::MB::dec($rest);
1568 1 $rest =~ s/\A\s+//;
1569 1 ▶ my $recurse = ($rest =~ s/\s*\/S\s*//i) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rest =~ s[\s*/S\s*][]i ? : 0 1 1
1570 1 $rest =~ s/\s*\/Q\s*//i; $rest =~ s/\s+\z//; $rest =~ s/\A"//; $rest =~ s/"\z//;
1571 0 ▶ if ($recurse) { File::Path::rmtree($rest) }
Branch: 1 branch
BranchTrueFalseTotal
if ($recurse) { } 0 1 1
1572 else {
1573 1 ▶ unless (rmdir($rest)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (rmdir $rest) 0 1 1
1574 0 print "The directory is not empty.\n"; $ERRORLEVEL = 1; return 1
1575 }
1576 }
1577 1 $ERRORLEVEL = 0; return 0;
1578 }
1579  
1580 0 ▶sub _cmd_rename {
Subroutine _cmd_rename: not called CC=4 SCAR=30.0
1581 0 my ($rest) = @_;
1582 0 $rest = BATsh::MB::dec($rest);
1583 0 $rest =~ s/\A\s+//;
1584 0 my ($src, $dst) = split /\s+/, $rest, 2;
1585 0 ▶ unless (defined $src && defined $dst) { print "The syntax of the command is incorrect.\n"; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $src and defined $dst) 0 0 0
Condition0%0/3
defined $src and defined $dst
!ll&&!rl&&r
000
MC/DC0%0/2
defined $src and defined $dst
defined $src defined $dst
Truth table0%0/3
A and B
A= defined $srcB= defined $dst
ABresult
0X0
100
111
1586 0 ▶ unless (rename($src, $dst)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (rename $src, $dst) 0 0 0
1587 0 print "Could not rename $src to $dst: $!\n"; $ERRORLEVEL = 1; return 1
1588 }
1589 0 $ERRORLEVEL = 0; return 0;
1590 }
1591  
1592 4 ▶sub _cmd_type {
Subroutine _cmd_type: called CC=3 SCAR=11.0
1593 4 my ($rest) = @_;
1594 4 $rest = BATsh::MB::dec($rest);
1595 4 $rest =~ s/\A\s+//; $rest =~ s/\s+\z//; $rest =~ s/\A"//; $rest =~ s/"\z//;
1596 4 local *TFH;
1597 4 ▶ unless (open(TFH, $rest)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open TFH, $rest) 1 3 4
1598 1 print "The system cannot find the file specified.\n"; $ERRORLEVEL = 1; return 1
1599 }
1600 3 ▶ while (<TFH>) { print }
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline TFH)) 4 3 7
1601 3 close(TFH);
1602 3 $ERRORLEVEL = 0; return 0;
1603 }
1604  
1605 # ----------------------------------------------------------------
1606 # External command
1607 # ----------------------------------------------------------------
1608 2 ▶sub _cmd_external {
Subroutine _cmd_external: called CC=5 SCAR=17.1
1609 2 my ($cmd, $rest) = @_;
1610 2 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 2 2
1611 2 $rest =~ s/\A\s+//;
1612 2 ▶ my $full = $rest ne '' ? "$cmd $rest" : $cmd;
Branch: 1 branch
BranchTrueFalseTotal
$rest ne '' ? : 2 0 2
1613 2 $full = _unescape_caret($full);
1614 2 $full = BATsh::MB::dec($full);
1615 2 BATsh::Env->sync_to_env();
1616 2 my $rc = system($full);
1617 2 ▶ $ERRORLEVEL = ($rc == 0) ? 0 : (($rc >> 8) || 1);
Branch: 1 branch
BranchTrueFalseTotal
$rc == 0 ? : 2 0 2
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
1618 2 return $ERRORLEVEL;
1619 }
1620  
1621 # ----------------------------------------------------------------
1622 # _split_cmd: split "COMMAND rest" respecting quotes
1623 # ----------------------------------------------------------------
1624 697 ▶sub _split_cmd {
Subroutine _split_cmd: called CC=2 SCAR=7.6
1625 697 my ($line) = @_;
1626 697 ▶ if ($line =~ /\A(\S+)\s*(.*)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A(\S+)\s*(.*)\z/s) 697 0 697
1627 697 return ($1, $2);
1628 }
1629 0 return ($line, '');
1630 }
1631  
1632 0 ▶sub _warn { print STDERR "[BATsh::CMD] ", BATsh::MB::dec($_[0]), "\n" }
Subroutine _warn: not called CC=1 SCAR=6.9
1633  
1634 # ----------------------------------------------------------------
1635 # Accessors
1636 # ----------------------------------------------------------------
1637 1 ▶sub errorlevel { return $ERRORLEVEL }
Subroutine errorlevel: called CC=1 SCAR=0.0
Pod undocumented
1638 2 ▶sub set_errorlevel { $ERRORLEVEL = $_[1] }
Subroutine set_errorlevel: called CC=1 SCAR=0.0
Pod undocumented
1639 0 ▶sub echo_on { return $ECHO_ON }
Subroutine echo_on: not called CC=1 SCAR=6.9
Pod undocumented
1640  
1641 64 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.6
1642 64 eval { require Cwd };
1643 64 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 64 64
1644 0 eval 'sub Cwd::cwd { return $ENV{CD} || "." }';
1645 }
1646 }
1647  
1648 64 1;
1649  
1650 __END__
« lib/BATsh/SH.pm ↑ Summary lib/BATsh.pm »