| 1 | package BATsh; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | ###################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # BATsh - Bilingual Shell for cmd.exe and bash in one script | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # https://metacpan.org/dist/BATsh | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # Copyright (c) 2026 INABA Hitoshi <ina.cpan@gmail.com> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # This version implements both cmd.exe and sh/bash command sets | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # entirely in Perl. No external cmd.exe, bash, or sh is required. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | ###################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 39 | ▶ | use 5.00503; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 39 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 39 | ▶ | BEGIN { if ($] < 5.006 && !defined(&warnings::import)) { $INC{'warnings.pm'} = 'stub'; eval 'package warnings; sub import {}' } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=11.4
Branch: 1 branch
Condition33%1/3
$] < "5.006" and not defined &warnings::import
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 39 | ▶ | use warnings; local $^W = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 39 | ▶ | BEGIN { pop @INC if $INC[-1] eq '.' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 57 | ▶ | use File::Spec (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 39 | ▶ | BEGIN { eval { require Cwd } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 36 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 36 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 33 | $VERSION = '0.10'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 33 | $VERSION = $VERSION; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 33 | require BATsh::MB; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 33 | require BATsh::Env; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 33 | require BATsh::CMD; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 33 | require BATsh::SH; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # Architecture | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | # BATsh is a bilingual shell interpreter. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # It splits a script into CMD sections and SH sections, then executes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | # each section using its own pure-Perl interpreter: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | # BATsh::CMD -- cmd.exe command set (SET, ECHO, IF, FOR, GOTO, ...) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # BATsh::SH -- sh/bash command set (echo, export, if/fi, for/done, ...) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # BATsh::Env -- shared variable store (bridge between both modes) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | # MODE DETECTION: first non-empty, non-comment token of each section. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | # CMD: token is [A-Z 0-9 _ - \ / : . @ %]+ with at least one A-Z | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # SH: anything else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | # SECTION BOUNDARY: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # CMD: parenthesis ( ) depth returns to 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # SH: keyword depth (if/fi, for/done, ...) returns to 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | # ENV BRIDGE: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # BATsh::Env::STORE is the single variable table. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # CMD %VAR% and SH $VAR both read/write the same store. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # Global state | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 33 | my $_TMPCOUNT = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # Subroutine registry: { LABEL => \@lines } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 33 | my %_SUBROUTINES = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | # Script-level exit state: set (to the exit code) the moment an SH "exit" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | # or a CMD "EXIT" executes in any section, including inside a sourced file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | # or a CALL'd subroutine. Once set, no further section is executed, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | # run()/run_string()/run_lines() return this code. Reset at the start of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # each top-level run. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 33 | my $_SCRIPT_EXIT = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | # Constructor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | BATsh::Env::init(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | return bless { verbose => $args{verbose} || 0 }, $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{'verbose'} || 0
MC/DC0%0/1
$args{'verbose'} || 0
$args{'verbose'}
Truth table0%0/2
A
A= $args{'verbose'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | # Public run interface | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | sub run { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 23 | ▶ | my ($class_or_self, $file, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=6 SCAR=18.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 23 | ▶ | unless (-f $file) { croak "BATsh->run: file not found: $file" } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 23 | local *SRCFH; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 23 | ▶ | open(SRCFH, $file) or croak "BATsh->run: cannot open $file: $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 23 | my @lines = <SRCFH>; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 23 | close(SRCFH); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 23 | _prepare_source(\@lines, $args{encoding}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 23 | _ensure_env_init(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | # Set batch positional parameters: %0 = script path, %1..%9 = args, %* = all args | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 23 | ▶ | my @script_args = defined($args{args}) ? @{$args{args}} : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 23 | _set_batch_args($file, @script_args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 23 | $_SCRIPT_EXIT = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 23 | ▶ | BATsh::SH::reset_sh_options() if defined &BATsh::SH::reset_sh_options; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 23 | _process_lines(@lines); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 23 | my $rc = _final_status(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 23 | ▶ | BATsh::SH::fire_exit_trap("BATsh::SH") if defined &BATsh::SH::fire_exit_trap; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 23 | return $rc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | sub run_string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 432 | ▶ | my ($class_or_self, $source, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run_string: called CC=6 SCAR=18.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 432 | ▶ | croak "BATsh->run_string: source required" unless defined $source; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 432 | my @lines = map { "$_\n" } split(/\n/, $source, -1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 432 | _prepare_source(\@lines, $args{encoding}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 432 | _ensure_env_init(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | # Optional positional parameters (used by "batsh - args..." and by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | # callers that feed a script body as a string): %0 defaults to '-'. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 432 | ▶ | if (defined $args{args}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | _set_batch_args(defined $args{script_name} ? $args{script_name} : '-', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 1 | ▶ | @{$args{args}}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 432 | $_SCRIPT_EXIT = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 432 | ▶ | BATsh::SH::reset_sh_options() if defined &BATsh::SH::reset_sh_options; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 432 | _process_lines(@lines); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 432 | my $rc = _final_status(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 432 | ▶ | BATsh::SH::fire_exit_trap("BATsh::SH") if defined &BATsh::SH::fire_exit_trap; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 432 | return $rc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | sub run_lines { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | my ($class_or_self, @lines) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run_lines: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | _prepare_source(\@lines, undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | _ensure_env_init(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | $_SCRIPT_EXIT = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | BATsh::SH::reset_sh_options() if defined &BATsh::SH::reset_sh_options; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | _process_lines(@lines); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | my $rc = _final_status(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | BATsh::SH::fire_exit_trap("BATsh::SH") if defined &BATsh::SH::fire_exit_trap; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | return $rc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | # _final_status -- the exit status of the run just finished: the code of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | # an executed exit/EXIT if any, else the last command's status. After | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | # every section flush both interpreters hold the same value (see | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | # _flush_cmd/_flush_sh), so the SH side is authoritative here. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | sub _final_status { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 455 | ▶ | return $_SCRIPT_EXIT if defined $_SCRIPT_EXIT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_final_status: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 433 | return BATsh::SH::get_status(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | # last_status -- public accessor: the unified $? / %ERRORLEVEL% value. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | sub last_status { return _final_status() } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
last_status: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | sub _ensure_env_init { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | # Init only once per process | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 455 | ▶ | BATsh::Env::init() unless %BATsh::Env::STORE; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ensure_env_init: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | # set_encoding -- select the script encoding for multibyte-safe execution | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | # BATsh->set_encoding('cp932'); # also: sjis gbk uhc big5 utf8 none auto | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | # The default is 'auto': a non-UTF-8 script containing bytes >= 0x80 is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | # treated as CP932 and guarded (see BATsh::MB). The environment variable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | # BATSH_ENCODING, when set, overrides the default before the first run. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 33 | my $_ENV_ENCODING_APPLIED = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | sub set_encoding { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 3 | ▶ | my ($class_or_self, $enc) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_encoding: called CC=4 SCAR=16.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 3 | ▶ | $enc = $class_or_self | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
not defined $enc and defined $class_or_self and not $class_or_self =~ /\ABATsh\b/
not defined $enc and defined $class_or_self
MC/DC0%0/3
not defined $enc and defined $class_or_self and not $class_or_self =~ /\ABATsh\b/
not defined $enc
defined $class_or_self
not $class_or_self =~ /\ABATsh\b/
Truth table25%1/4
not(A) and B and not(C)
A= not defined $encB= defined $class_or_selfC= not $class_or_self =~ /\ABATsh\b/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | if !defined($enc) && defined($class_or_self) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | && $class_or_self !~ /\ABATsh\b/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 3 | $_ENV_ENCODING_APPLIED = 1; # explicit choice beats BATSH_ENCODING | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 3 | return BATsh::MB::set_encoding($enc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | ▶ | sub encoding { return BATsh::MB::encoding() } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encoding: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | # _prepare_source -- per-run encoding setup on the raw script lines | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | # 1. strip a UTF-8 BOM from the first line | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | # 2. apply an explicit per-run encoding, or BATSH_ENCODING (once), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | # or leave the current ('auto' by default) setting in place | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | # 3. under 'auto', detect the source encoding and activate the guard | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | # 4. guard-transform every line in place (identity when inactive) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | sub _prepare_source { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 456 | ▶ | my ($lines_ref, $encoding) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_prepare_source: called CC=9 SCAR=25.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 456 | ▶ | $lines_ref->[0] = BATsh::MB::strip_bom($lines_ref->[0]) if @{$lines_ref}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 456 | ▶ | if (defined $encoding && $encoding ne '') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
defined $encoding and $encoding ne ""
MC/DC0%0/2
defined $encoding and $encoding ne ""
defined $encoding
$encoding ne ""
Truth table33%1/3
A and B
A= defined $encodingB= $encoding ne ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | BATsh::MB::set_encoding($encoding); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | $_ENV_ENCODING_APPLIED = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | ▶ | elsif (!$_ENV_ENCODING_APPLIED | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%3/6
not $_ENV_ENCODING_APPLIED and defined $ENV{"BATSH_ENCODING"} and $ENV{"BATSH_ENCODING"} ne ""
not $_ENV_ENCODING_APPLIED and defined $ENV{"BATSH_ENCODING"}
MC/DC0%0/3
not $_ENV_ENCODING_APPLIED and defined $ENV{"BATSH_ENCODING"} and $ENV{"BATSH_ENCODING"} ne ""
not $_ENV_ENCODING_APPLIED
defined $ENV{"BATSH_ENCODING"}
$ENV{"BATSH_ENCODING"} ne ""
Truth table50%2/4
not(A) and B and C
A= not $_ENV_ENCODING_APPLIEDB= defined $ENV{"BATSH_ENCODING"}C= $ENV{"BATSH_ENCODING"} ne ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | && defined $ENV{BATSH_ENCODING} && $ENV{BATSH_ENCODING} ne '') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | BATsh::MB::set_encoding($ENV{BATSH_ENCODING}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | $_ENV_ENCODING_APPLIED = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 456 | BATsh::MB::activate_for(join('', @{$lines_ref})); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 456 | ▶ | if (BATsh::MB::active()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 17 | for my $l (@{$lines_ref}) { $l = BATsh::MB::enc($l) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 456 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | # _set_batch_args -- populate %0..%9 and %* in the Env store | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | # %0 = script path (as passed to run()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | # %1 = first argument, ..., %9 = ninth argument | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | # %* = all arguments joined by single space (does not include %0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | sub _set_batch_args { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 28 | ▶ | my ($script, @args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_batch_args: called CC=8 SCAR=21.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | # Arguments arrive as RAW bytes from outside the interpreter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | # (command line / caller); guard them before they enter the store. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 28 | ▶ | @args = map { BATsh::MB::enc(defined $_ ? $_ : '') } @args; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | # Normalise $0: resolve to absolute path using File::Spec | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 28 | ▶ | my $abs_script = defined $script ? $script : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 28 | ▶ | if ($abs_script ne '' && !File::Spec->file_name_is_absolute($abs_script)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$abs_script ne "" and not "File::Spec"->file_name_is_absolute($abs_script)
MC/DC50%1/2
$abs_script ne "" and not "File::Spec"->file_name_is_absolute($abs_script)
$abs_script ne ""
not "File::Spec"->file_name_is_absolute($abs_script)
Truth table66%2/3
A and not(B)
A= $abs_script ne ""B= not "File::Spec"->file_name_is_absolute($abs_script)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 4 | ▶ | my $cwd = defined(&Cwd::cwd) ? Cwd::cwd() : '.'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 4 | $abs_script = File::Spec->catfile($cwd, $abs_script); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 28 | BATsh::Env->set('%0', BATsh::MB::enc($abs_script)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 28 | for my $n (1 .. 9) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 252 | ▶ | BATsh::Env->set("%$n", defined($args[$n - 1]) ? $args[$n - 1] : ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 28 | BATsh::Env->set('%*', join(' ', @args)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | # classify_token | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | sub classify_token { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 972 | ▶ | my ($class_or_token, $token) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
classify_token: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 972 | ▶ | unless (defined $token) { $token = $class_or_token } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 972 | ▶ | if ($token =~ /\A[A-Z0-9_\-\\\/\.:@%]+\z/ && $token =~ /[A-Z]/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$token =~ m[\A[A-Z0-9_\-\\/\.:\@%]+\z] and $token =~ /[A-Z]/
MC/DC100%2/2
$token =~ m[\A[A-Z0-9_\-\\/\.:\@%]+\z] and $token =~ /[A-Z]/
$token =~ m[\A[A-Z0-9_\-\\/\.:\@%]+\z]
$token =~ /[A-Z]/
Truth table100%3/3
A and B
A= $token =~ m[\A[A-Z0-9_\-\\/\.:\@%]+\z]B= $token =~ /[A-Z]/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 279 | return 'CMD'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 693 | return 'SH'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | # Line parser | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | # Returns ($mode, $stripped_line, $first_token) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | sub _parse_line { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 1167 | ▶ | my ($line) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_line: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 1167 | (my $s = $line) =~ s/\r?\n\z//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 1167 | ▶ | return ('EMPTY', $s, '') if $s =~ /\A\s*\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 964 | ▶ | return ('COMMENT', $s, '') if $s =~ /\A\s*(?:::|\@?REM(?:\s|\z))/i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 959 | ▶ | return ('COMMENT', $s, '') if $s =~ /\A\s*#(?!!)/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 957 | (my $t = $s) =~ s/\A\s+//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 957 | ▶ | my $first = ($t =~ /\A(\S+)/) ? $1 : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 957 | return (classify_token($first), $s, $first); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | # CMD section depth: count unquoted ( ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | sub _cmd_paren_delta { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 274 | ▶ | my ($line) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cmd_paren_delta: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 274 | my ($delta, $in_q) = (0, 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 274 | for my $ch (split //, $line) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 4395 | ▶ | if ($ch eq '"') { $in_q = !$in_q } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | elsif (!$in_q) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 4271 | ▶ | $delta++ if $ch eq '('; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 4271 | ▶ | $delta-- if $ch eq ')'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 274 | return $delta; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | # SH section depth | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 33 | my %_SH_OPEN = map { $_ => 1 } qw(if for while until case function select); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 33 | my %_SH_CLOSE = map { $_ => 1 } qw(fi done esac); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | # Net SH block-depth change for an ENTIRE line. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | # A single physical line may both open and close a block, e.g. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | # for i in 1 2 3; do echo $i; done (net 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | # while COND; do ...; done; done (net -1, nested close) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | # Looking only at the first token (as earlier versions did) counted the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | # leading "for"/"while" but never the inline "done", so the SH section was | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | # left open and a following CMD line (e.g. ECHO ...%VAR%...) was wrongly | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | # absorbed into the SH section and run by the SH interpreter. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | # Opener keywords (if for while until case function select) and "{" add 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | # closer keywords (fi done esac) and "}" subtract 1, but only when they are in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | # command position (line start, or just after ; & | ( ) { } or do/then/else/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | # elif). Single- and double-quoted text, $( ... ) command substitutions and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | # `...` backticks are skipped so that a "done" inside a string or substitution | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | # does not affect the depth. Perl 5.005_03 compatible (no regex features | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | # beyond character classes; substr/index style scanning). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | sub _sh_depth_delta { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 691 | ▶ | my ($line) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_sh_depth_delta: called CC=42 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 691 | ▶ | return 0 unless defined $line; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | # Fast path: a bare single keyword token (the common multi-line case). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 691 | ▶ | if ($line !~ /[\s;&|(){}'"`]/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 105 | my $l = lc($line); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 105 | ▶ | return 1 if exists $_SH_OPEN{$l}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 102 | ▶ | return -1 if exists $_SH_CLOSE{$l}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 76 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 586 | my $delta = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 586 | my $cmdpos = 1; # next bareword starts a new statement? | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 586 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 586 | my $n = length($line); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 586 | ▶ | while ($i < $n) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 4024 | my $c = substr($line, $i, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 4024 | ▶ | if ($c eq "'") { # single-quoted region | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 60 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 60 | ▶ | $i++ while $i < $n && substr($line, $i, 1) ne "'"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$i < $n and substr($line, $i, 1) ne "'"
MC/DC50%1/2
$i < $n and substr($line, $i, 1) ne "'"
$i < $n
substr($line, $i, 1) ne "'"
Truth table66%2/3
A and B
A= $i < $nB= substr($line, $i, 1) ne "'"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 60 | $i++; $cmdpos = 0; next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 3964 | ▶ | if ($c eq '"') { # double-quoted region | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 159 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 159 | ▶ | while ($i < $n && substr($line, $i, 1) ne '"') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$i < $n and substr($line, $i, 1) ne "\""
MC/DC50%1/2
$i < $n and substr($line, $i, 1) ne "\""
$i < $n
substr($line, $i, 1) ne "\""
Truth table66%2/3
A and B
A= $i < $nB= substr($line, $i, 1) ne "\""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 1638 | ▶ | if (substr($line, $i, 1) eq '\\') { $i += 2; next } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 1622 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 159 | $i++; $cmdpos = 0; next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 3805 | ▶ | if ($c eq '`') { # backtick substitution | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 1 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 1 | ▶ | $i++ while $i < $n && substr($line, $i, 1) ne '`'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$i < $n and substr($line, $i, 1) ne "`"
MC/DC50%1/2
$i < $n and substr($line, $i, 1) ne "`"
$i < $n
substr($line, $i, 1) ne "`"
Truth table66%2/3
A and B
A= $i < $nB= substr($line, $i, 1) ne "`"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 1 | $i++; $cmdpos = 0; next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 3804 | ▶ | if ($c eq '$' && $i + 1 < $n && substr($line, $i + 1, 1) eq '(') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$c eq "\$" and $i + 1 < $n and substr($line, $i + 1, 1) eq "("
$c eq "\$" and $i + 1 < $n
MC/DC66%2/3
$c eq "\$" and $i + 1 < $n and substr($line, $i + 1, 1) eq "("
$c eq "\$"
$i + 1 < $n
substr($line, $i + 1, 1) eq "("
Truth table75%3/4
A and B and C
A= $c eq "\$"B= $i + 1 < $nC= substr($line, $i + 1, 1) eq "("
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 42 | $i += 2; # $( ... ) -- skip balanced parens | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 42 | my $d = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 42 | ▶ | while ($i < $n && $d > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$i < $n and $d > 0
MC/DC100%2/2
$i < $n and $d > 0
$i < $n
$d > 0
Truth table100%3/3
A and B
A= $i < $nB= $d > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 443 | my $ch = substr($line, $i, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 443 | ▶ | $d++ if $ch eq '('; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 443 | ▶ | $d-- if $ch eq ')'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 443 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 42 | $cmdpos = 0; next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 3762 | ▶ | if ($c =~ /\s/) { $i++; next } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 2195 | ▶ | if ($c eq ';' || $c eq '&' || $c eq '|') { $cmdpos = 1; $i++; next } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$c eq ";" or $c eq "&" or $c eq "|"
$c eq ";" or $c eq "&"
MC/DC100%3/3
$c eq ";" or $c eq "&" or $c eq "|"
$c eq ";"
$c eq "&"
$c eq "|"
Truth table100%4/4
A or B or C
A= $c eq ";"B= $c eq "&"C= $c eq "|"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 1958 | ▶ | if ($c eq '(') { $cmdpos = 1; $i++; next } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 1903 | ▶ | if ($c eq ')') { $cmdpos = 1; $i++; next } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 1837 | ▶ | if ($c eq '{') { $delta++ if $cmdpos; $cmdpos = 1; $i++; next } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 1800 | ▶ | if ($c eq '}') { $delta-- if $cmdpos; $cmdpos = 1; $i++; next } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | # Bareword | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 1763 | my $word = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 1763 | ▶ | while ($i < $n) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 8799 | my $wc = substr($line, $i, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 8799 | ▶ | last if $wc =~ /[\s;&|(){}'"`]/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 7428 | $word .= $wc; $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 1763 | ▶ | if ($cmdpos) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 896 | my $lw = lc($word); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 896 | ▶ | if (exists $_SH_OPEN{$lw}) { $delta++ } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 46 | elsif (exists $_SH_CLOSE{$lw}) { $delta-- } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | # do/then/else/elif keep the FOLLOWING word in command position so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | # that an opener directly after them (e.g. "do for ...") is counted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 896 | ▶ | $cmdpos = ($lw eq 'do' || $lw eq 'then' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
$lw eq "do" or $lw eq "then" or $lw eq "else" or $lw eq "elif"
$lw eq "do" or $lw eq "then" or $lw eq "else"
$lw eq "do" or $lw eq "then"
MC/DC100%4/4
$lw eq "do" or $lw eq "then" or $lw eq "else" or $lw eq "elif"
$lw eq "do"
$lw eq "then"
$lw eq "else"
$lw eq "elif"
Truth table100%5/5
A or B or C or D
A= $lw eq "do"B= $lw eq "then"C= $lw eq "else"D= $lw eq "elif"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | || $lw eq 'else' || $lw eq 'elif') ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 867 | $cmdpos = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 586 | return $delta; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | # Subroutine extraction | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | sub _extract_subroutines { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 485 | ▶ | my (@lines) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_extract_subroutines: called CC=16 SCAR=27.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 485 | my @out = (); my $in_sub = ''; my @sub_body = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | # Determine which :LABEL lines are subroutine ENTRY points (to be lifted | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | # out of the main stream and stored in %_SUBROUTINES) versus ordinary | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | # GOTO labels (which stay in the stream for the CMD interpreter). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | # Two independent signals are unioned: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | # (a) CALL targets: any label named by a "CALL :LABEL" anywhere in the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | # script is an entry point. This is the decisive signal -- a label | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | # you CALL is a subroutine -- and it lets a subroutine contain its | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | # own internal GOTO labels without being mis-split. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | # (b) The RET heuristic: a label whose block ends in RET/RETURN before | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | # the next label. Kept for backward compatibility so a subroutine | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | # invoked only indirectly is still recognised. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | # A label is opened as a subroutine ONLY at the top level (not while | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | # already inside a subroutine body); once inside a body, every :LABEL is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | # an internal label that travels with the body so that GOTO within the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | # subroutine resolves, and only RET/RETURN closes the body. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 485 | my %is_sub_label = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | # (a) CALL targets (matched anywhere on the line, e.g. "IF .. CALL :X"). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 485 | for my $line (@lines) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 1311 | (my $s = $line) =~ s/\r?\n\z//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 1311 | ▶ | while ($s =~ /\bCALL\s+:([A-Za-z_][A-Za-z0-9_]*)/ig) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | 30 | $is_sub_label{uc($1)} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | # (b) RET heuristic. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 485 | my $cur = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | 485 | for my $line (@lines) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 1311 | (my $s = $line) =~ s/\r?\n\z//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 1311 | $s =~ s/\A\s+//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | 1311 | ▶ | if ($s =~ /\A:([A-Za-z_][A-Za-z0-9_]*)\s*\z/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 63 | $cur = uc($1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | ▶ | elsif ($cur ne '' && $s =~ /\A(?:RET|RETURN)\s*\z/i) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$cur ne "" and $s =~ /\A(?:RET|RETURN)\s*\z/i
MC/DC100%2/2
$cur ne "" and $s =~ /\A(?:RET|RETURN)\s*\z/i
$cur ne ""
$s =~ /\A(?:RET|RETURN)\s*\z/i
Truth table100%3/3
A and B
A= $cur ne ""B= $s =~ /\A(?:RET|RETURN)\s*\z/i
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | 25 | $is_sub_label{$cur} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 25 | $cur = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | ▶ | elsif ($cur ne '' && $s =~ /\A:([A-Za-z_][A-Za-z0-9_]*)\s*\z/) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$cur ne "" and $s =~ /\A:([A-Za-z_][A-Za-z0-9_]*)\s*\z/
MC/DC0%0/2
$cur ne "" and $s =~ /\A:([A-Za-z_][A-Za-z0-9_]*)\s*\z/
$cur ne ""
$s =~ /\A:([A-Za-z_][A-Za-z0-9_]*)\s*\z/
Truth table66%2/3
A and B
A= $cur ne ""B= $s =~ /\A:([A-Za-z_][A-Za-z0-9_]*)\s*\z/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | # New label before RET: previous one is a GOTO label, not sub | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | 0 | $cur = uc($1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 485 | for my $line (@lines) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 1311 | (my $s = $line) =~ s/\r?\n\z//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | 1311 | $s =~ s/\A\s+//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 1311 | ▶ | if ($s =~ /\A:([A-Za-z_][A-Za-z0-9_]*)\s*\z/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 63 | my $lbl = uc($1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 63 | ▶ | if ($in_sub ne '') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | # Inside a subroutine: this is an internal label of the body | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | # (a GOTO target), not a new subroutine. Keep it with the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | # body so the CMD interpreter can resolve GOTO :internal. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 15 | push @sub_body, $line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 15 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | 48 | ▶ | if ($is_sub_label{$lbl}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | # Top-level subroutine entry: lift it out of the main stream. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 25 | $in_sub = $lbl; @sub_body = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 25 | next; # remove the entry-label line from the stream | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | # Top-level GOTO label: keep in stream for the CMD interpreter. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 23 | push @out, $line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | 23 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | 1248 | ▶ | if ($in_sub ne '') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | 86 | ▶ | if ($s =~ /\A(?:RET|RETURN)\s*\z/i) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | 25 | $_SUBROUTINES{$in_sub} = [@sub_body]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 25 | $in_sub = ''; @sub_body = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 61 | push @sub_body, $line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 86 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 1162 | push @out, $line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | 485 | ▶ | $_SUBROUTINES{$in_sub} = [@sub_body] if $in_sub ne ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | 485 | return @out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | # call_sub / source_file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | sub call_sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 29 | ▶ | my ($class_or_self, $label, @args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
call_sub: called CC=11 SCAR=24.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | 29 | $label = uc($label); $label =~ s/^://; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | croak "BATsh->call_sub: undefined subroutine :$label" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | 29 | ▶ | unless exists $_SUBROUTINES{$label}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | # --- Save the caller's positional-parameter frame ----------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | # CALL :label is a true subroutine call: the callee gets its own | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | # %0..%9 / %* (and the SH-side BATSH_ARG* mirror), and the caller's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | # parameters are restored on return. An undef snapshot means the key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | # was absent and must be removed again on restore. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | 29 | my @frame_keys = ('%0', '%1', '%2', '%3', '%4', '%5', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | '%6', '%7', '%8', '%9', '%*', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | 'BATSH_ARGC', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | 'BATSH_ARG1', 'BATSH_ARG2', 'BATSH_ARG3', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | 'BATSH_ARG4', 'BATSH_ARG5', 'BATSH_ARG6', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | 'BATSH_ARG7', 'BATSH_ARG8', 'BATSH_ARG9'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | 29 | my %saved; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | 29 | for my $k (@frame_keys) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | 609 | ▶ | $saved{$k} = exists $BATsh::Env::STORE{$k} ? $BATsh::Env::STORE{$k} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | # --- Install the subroutine's own arguments ----------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | # %0 is the label token (":LABEL"), matching cmd.exe, so that %~..0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | # modifiers and %0 inside the subroutine refer to the label, not the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | # outer script. %1..%9 are the call arguments; %* is their join. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | 29 | BATsh::Env->set('%0', ":$label"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | 29 | for my $n (1 .. 9) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | 261 | ▶ | BATsh::Env->set("%$n", defined($args[$n-1]) ? $args[$n-1] : ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | 29 | BATsh::Env->set('%*', join(' ', @args)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | # SH-side mirror so a subroutine body written in SH mode still sees | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | # $1..$9 / $@. Keys beyond the supplied argument count are removed so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | # a shorter call does not inherit the caller's stale BATSH_ARG* values. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | 29 | $BATsh::Env::STORE{'BATSH_ARGC'} = scalar @args; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | 29 | for my $n (1 .. 9) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | 261 | ▶ | if ($n <= scalar @args) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 44 | $BATsh::Env::STORE{"BATSH_ARG$n"} = $args[$n-1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | 217 | delete $BATsh::Env::STORE{"BATSH_ARG$n"}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | # --- Run the body, then always restore the caller frame ----------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | 29 | my $ok = eval { _process_lines(@{$_SUBROUTINES{$label}}); 1 }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | 29 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | 29 | for my $k (@frame_keys) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | 609 | ▶ | if (defined $saved{$k}) { $BATsh::Env::STORE{$k} = $saved{$k} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | 295 | else { delete $BATsh::Env::STORE{$k} } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | 29 | ▶ | die $err unless $ok; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | 29 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | sub source_file { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | 1 | ▶ | my ($class_or_self, $file) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
source_file: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | # The filename may arrive guard-transformed (from a CALL/source line | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | # inside a DBCS script); un-guard it before touching the filesystem. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | 1 | $file = BATsh::MB::dec($file); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | 1 | ▶ | croak "BATsh->source_file: file not found: $file" unless -f $file; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | 1 | local *SFHH; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | 1 | ▶ | open(SFHH, $file) or croak "BATsh->source_file: cannot open $file: $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | 1 | my @src = <SFHH>; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | 1 | close(SFHH); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | 1 | _prepare_source(\@src, undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | 1 | _process_lines(@src); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | # SETLOCAL / ENDLOCAL (public API) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | 0 | ▶ | sub setlocal { BATsh::Env::setlocal() } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
setlocal: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | 0 | ▶ | sub endlocal { BATsh::Env::endlocal() } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
endlocal: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | # _exec_cmd_section -- run CMD lines through BATsh::CMD | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | sub _exec_cmd_section { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | 123 | ▶ | my (@lines) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_exec_cmd_section: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | 123 | ▶ | return if defined $_SCRIPT_EXIT; # exit/EXIT already executed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | # Handle BATsh-native directives before CMD interpreter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | 123 | my @batch = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | 123 | for my $line (@lines) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | 280 | (my $s = $line) =~ s/\r?\n\z//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | 280 | $s =~ s/\A\s+//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | 280 | ▶ | if ($s =~ /\ASETLOCAL(?:\s+(.*))?\z/i) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | 4 | ▶ | my $opts = defined $1 ? $1 : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | # Pass the whole line through to CMD interpreter so it sees | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | # ENABLEDELAYEDEXPANSION etc; also update Env flags here. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | 4 | push @batch, $line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | 4 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | 276 | ▶ | if ($s =~ /\AENDLOCAL\s*\z/i) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | # Handled inside CMD.pm / _dispatch | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | 4 | push @batch, $line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | 4 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | # NOTE: CALL :label and CALL file.batsh are intentionally NOT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | # intercepted here. They are handled by the CMD interpreter's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | # _cmd_call (which delegates to call_sub / source_file). Letting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | # CALL stay inside the batch keeps the whole CMD section in one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | # exec_block, so a GOTO whose loop body contains a CALL still finds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | # its label, and a subroutine body may use its own internal labels. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | 272 | push @batch, $line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | 123 | ▶ | _flush_cmd(\@batch) if @batch; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | sub _flush_cmd { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | 123 | ▶ | my ($lines_ref) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_flush_cmd: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | 123 | ▶ | return unless @{$lines_ref}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | 123 | ▶ | return if defined $_SCRIPT_EXIT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | 123 | BATsh::CMD::exec_block('BATsh::CMD', $lines_ref, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | _batsh => __PACKAGE__, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | _pushd_stack => [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | # Status bridge: mirror the CMD-side ERRORLEVEL into the SH-side $? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | # so the next SH section sees the outcome of this CMD section. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | 123 | BATsh::SH::set_status(BATsh::CMD::_get_errorlevel()); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | # EXIT executed: terminate the whole script with ERRORLEVEL. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | 123 | ▶ | if (BATsh::CMD::_exit_requested()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | 6 | BATsh::CMD::_clear_exit_requested(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | 6 | $_SCRIPT_EXIT = BATsh::CMD::_get_errorlevel(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | # _exec_sh_section -- run SH lines through BATsh::SH | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | sub _exec_sh_section { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | 384 | ▶ | my (@lines) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_exec_sh_section: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | 384 | ▶ | return if defined $_SCRIPT_EXIT; # exit/EXIT already executed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | 383 | my @batch = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | 383 | for my $line (@lines) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | 902 | (my $s = $line) =~ s/\r?\n\z//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | 902 | $s =~ s/\A\s+//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | 902 | ▶ | if ($s =~ /\A(?:source|\.)\s+(\S+\.batsh)/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | 1 | my $bfile = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | 1 | ▶ | _flush_sh(\@batch) if @batch; @batch = (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | 1 | eval { source_file('', $bfile) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | 1 | ▶ | warn $@ if $@; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | 901 | push @batch, $line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | 383 | ▶ | _flush_sh(\@batch) if @batch; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | sub _flush_sh { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | 384 | ▶ | my ($lines_ref) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_flush_sh: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | 384 | ▶ | return unless @{$lines_ref}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | 384 | ▶ | return if defined $_SCRIPT_EXIT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | 383 | BATsh::SH::exec_block('BATsh::SH', $lines_ref, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | _batsh => __PACKAGE__, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | # Status bridge: mirror the SH-side $? into the CMD-side ERRORLEVEL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | # so the next CMD section sees the outcome of this SH section | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | # (ECHO %ERRORLEVEL%, IF ERRORLEVEL n, ...). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | 383 | BATsh::CMD::_set_errorlevel(BATsh::SH::get_status()); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | # exit executed: terminate the whole script with its code. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | 383 | my $ec = BATsh::SH::exit_code_pending(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | 383 | ▶ | $_SCRIPT_EXIT = $ec if defined $ec; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | # _process_lines -- main dispatcher | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | sub _process_lines { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | 485 | ▶ | my (@source) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_process_lines: called CC=22 SCAR=30.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | 485 | @source = _extract_subroutines(@source); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | 485 | my @pending = (); my $cur_mode = ''; my $depth = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 648 | # Here-document tracking: while a here-document body is being collected, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 649 | # body lines are appended to the current section verbatim and are NOT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 650 | # reclassified (so an uppercase-leading body line is not misrouted to CMD). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 651 | # Activation is deferred by one line so the trigger line itself is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 652 | # classified normally. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | 485 | my $hd_delim = undef; # active delimiter (collecting body) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | 485 | my $hd_dash = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | 485 | my $pending_hd_delim = undef; # set on the trigger line, activated next iter | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | 485 | my $pending_hd_dash = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | 485 | for my $raw (@source) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | 1185 | chomp $raw; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | # Promote a pending here-document to active (trigger line already pushed) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | 1185 | ▶ | if (defined $pending_hd_delim) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | 13 | $hd_delim = $pending_hd_delim; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 664 | 13 | $hd_dash = $pending_hd_dash; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 665 | 13 | $pending_hd_delim = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 666 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 668 | # Collecting a here-document body: pass through unclassified. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 669 | 1185 | ▶ | if (defined $hd_delim) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 670 | 29 | push @pending, $raw; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 671 | 29 | my $probe = $raw; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 672 | 29 | ▶ | $probe =~ s/\A\t+// if $hd_dash; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 673 | 29 | ▶ | $hd_delim = undef if $probe eq $hd_delim; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 674 | 29 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | 1156 | my ($mode, $line, $first) = _parse_line($raw); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | # Detect a here-document opener on an SH line; defer activation so the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | # trigger line is classified normally this iteration. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | 1156 | ▶ | if ($mode eq 'SH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | 684 | my @hd = BATsh::SH::_hd_detect($raw); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 683 | 684 | ▶ | if (@hd) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 684 | 13 | $pending_hd_delim = $hd[2]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 685 | 13 | $pending_hd_dash = $hd[1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 686 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 687 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 689 | 1156 | ▶ | if ($mode eq 'EMPTY' || $mode eq 'COMMENT') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$mode eq "EMPTY" or $mode eq "COMMENT"
MC/DC100%2/2
$mode eq "EMPTY" or $mode eq "COMMENT"
$mode eq "EMPTY"
$mode eq "COMMENT"
Truth table100%3/3
A or B
A= $mode eq "EMPTY"B= $mode eq "COMMENT"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 690 | # A comment carries no execution effect, so it must never be handed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 691 | # to an interpreter verbatim. A CMD-style comment (":: ..." or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 692 | # "REM ...") carried into an SH section would be dispatched to the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 693 | # external shell (SH treats only "#" as a comment), and any "( )" or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 694 | # other shell metacharacter in the comment then makes /bin/sh fail | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 695 | # with a syntax error. The reverse ("# ..." into CMD) is likewise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 696 | # not a CMD comment. Routing it as a BLANK line is skipped | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 697 | # identically by both interpreters and in every nesting depth, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 698 | # avoids the real cmd.exe quirk of "::" inside a "( )" block. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 699 | 204 | ▶ | push @pending, '' if $cur_mode ne ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 700 | 204 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 701 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 703 | 952 | ▶ | if ($cur_mode eq '') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 704 | 485 | $cur_mode = $mode; $depth = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 705 | 485 | push @pending, $line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 706 | 485 | ▶ | $depth += ($mode eq 'CMD') ? _cmd_paren_delta($line) : _sh_depth_delta($line); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 707 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 708 | elsif ($mode eq $cur_mode) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 709 | 443 | push @pending, $line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 710 | 443 | ▶ | $depth += ($mode eq 'CMD') ? _cmd_paren_delta($line) : _sh_depth_delta($line); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 711 | 443 | ▶ | $depth = 0 if $depth < 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 712 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 713 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 714 | 24 | ▶ | if ($depth > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 715 | 2 | push @pending, $line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 716 | 2 | ▶ | $depth += ($cur_mode eq 'CMD') ? _cmd_paren_delta($line) : _sh_depth_delta($line); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 717 | 2 | ▶ | $depth = 0 if $depth < 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 718 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 719 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 720 | 22 | ▶ | _flush_section($cur_mode, @pending) if @pending; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 721 | 22 | @pending = ($line); $cur_mode = $mode; $depth = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 722 | 22 | ▶ | $depth += ($mode eq 'CMD') ? _cmd_paren_delta($line) : _sh_depth_delta($line); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 723 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 724 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 725 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 726 | 485 | ▶ | _flush_section($cur_mode, @pending) if @pending; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 727 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 728 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 729 | sub _flush_section { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 730 | 507 | ▶ | my ($mode, @lines) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_flush_section: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 731 | 507 | ▶ | return unless @lines; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 732 | 507 | ▶ | if ($mode eq 'CMD') { _exec_cmd_section(@lines) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 733 | 384 | else { _exec_sh_section(@lines) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 734 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 735 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 736 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 737 | # REPL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 738 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 739 | sub repl { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 740 | 0 | ▶ | my ($class_or_self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
repl: not called CC=24 SCAR=64.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 741 | 0 | _ensure_env_init(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 742 | 0 | $_SCRIPT_EXIT = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 743 | 0 | ▶ | BATsh::SH::reset_sh_options() if defined &BATsh::SH::reset_sh_options; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 744 | 0 | print "BATsh $VERSION - Bilingual Shell\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 745 | 0 | print "Uppercase => CMD mode, lowercase => SH mode. EXIT/exit to quit.\n\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 747 | 0 | my (@buf, $depth, $cur_mode) = ((), 0, ''); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 748 | 0 | my ($hd_delim, $hd_dash, $pending_hd_delim, $pending_hd_dash) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 749 | = (undef, 0, undef, 0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 750 | 0 | while (1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 751 | 0 | ▶ | print $depth > 0 || defined($hd_delim) ? ' +> ' : 'BATsh> '; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$depth > 0 or defined $hd_delim
MC/DC0%0/2
$depth > 0 or defined $hd_delim
$depth > 0
defined $hd_delim
Truth table0%0/3
A or B
A= $depth > 0B= defined $hd_delim
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 752 | 0 | my $line = <STDIN>; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 753 | 0 | ▶ | last unless defined $line; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 754 | 0 | chomp $line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 755 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 756 | # Guard-transform DBCS input (auto-activates on first DBCS line) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 757 | 0 | BATsh::MB::activate_for($line); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 758 | 0 | $line = BATsh::MB::enc($line); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 759 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 760 | # Promote pending here-document (trigger already buffered) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 761 | 0 | ▶ | if (defined $pending_hd_delim) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 762 | 0 | $hd_delim = $pending_hd_delim; $hd_dash = $pending_hd_dash; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 763 | 0 | $pending_hd_delim = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 764 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 765 | # Collecting here-document body: buffer verbatim, do not flush/classify | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 766 | 0 | ▶ | if (defined $hd_delim) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 767 | 0 | push @buf, $line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 768 | 0 | my $probe = $line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 769 | 0 | ▶ | $probe =~ s/\A\t+// if $hd_dash; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 770 | 0 | ▶ | $hd_delim = undef if $probe eq $hd_delim; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 771 | 0 | ▶ | if (!defined($hd_delim) && $depth == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $hd_delim and $depth == 0
MC/DC0%0/2
not defined $hd_delim and $depth == 0
not defined $hd_delim
$depth == 0
Truth table0%0/3
not(A) and B
A= not defined $hd_delimB= $depth == 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 772 | 0 | _flush_section($cur_mode, @buf); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 773 | 0 | @buf = (); $cur_mode = ''; $depth = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 774 | 0 | ▶ | last if defined $_SCRIPT_EXIT; # "exit N" ends the REPL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 775 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 776 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 777 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 779 | 0 | ▶ | if ($line =~ /\A\s*(?:EXIT|exit)\s*\z/) { print "Bye.\n"; last } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 780 | 0 | ▶ | next if $depth == 0 && $line =~ /\A\s*\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$depth == 0 and $line =~ /\A\s*\z/
MC/DC0%0/2
$depth == 0 and $line =~ /\A\s*\z/
$depth == 0
$line =~ /\A\s*\z/
Truth table0%0/3
A and B
A= $depth == 0B= $line =~ /\A\s*\z/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 781 | 0 | push @buf, $line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 782 | # (undef, ...) in a my list is a Perl 5.10 spelling; the list is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 783 | # taken as a whole and indexed instead, for 5.005_03. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 784 | 0 | my @parsed = _parse_line($line); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 785 | 0 | my $first = $parsed[2]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 786 | 0 | ▶ | $cur_mode = classify_token($first) if $depth == 0 && $cur_mode eq ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$depth == 0 and $cur_mode eq ""
MC/DC0%0/2
$depth == 0 and $cur_mode eq ""
$depth == 0
$cur_mode eq ""
Truth table0%0/3
A and B
A= $depth == 0B= $cur_mode eq ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 787 | # Detect here-document opener (SH only); defer activation one line | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 788 | 0 | ▶ | if ($cur_mode eq 'SH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 789 | 0 | my @hd = BATsh::SH::_hd_detect($line); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 790 | 0 | ▶ | if (@hd) { $pending_hd_delim = $hd[2]; $pending_hd_dash = $hd[1] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 791 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 792 | 0 | ▶ | $depth += ($cur_mode eq 'CMD') ? _cmd_paren_delta($line) : _sh_depth_delta($line); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 793 | 0 | ▶ | $depth = 0 if $depth < 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 794 | 0 | ▶ | if ($depth == 0 && !defined($pending_hd_delim)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$depth == 0 and not defined $pending_hd_delim
MC/DC0%0/2
$depth == 0 and not defined $pending_hd_delim
$depth == 0
not defined $pending_hd_delim
Truth table0%0/3
A and not(B)
A= $depth == 0B= not defined $pending_hd_delim
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 795 | 0 | _flush_section($cur_mode, @buf); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 796 | 0 | @buf = (); $cur_mode = ''; $depth = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 797 | 0 | ▶ | last if defined $_SCRIPT_EXIT; # "exit N" / "EXIT N" ends the REPL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 798 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 799 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 800 | 0 | return _final_status(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 801 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 802 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 803 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 804 | # Accessors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 805 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 806 | 0 | ▶ | sub version { return $VERSION } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
version: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 807 | 2 | ▶ | sub sh_available { return 1 } # always: built-in SH interpreter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sh_available: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 808 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 809 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 810 | # main -- command-line entry point (used by bin/batsh.pl and the modulino). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 811 | # Returns the process exit code. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 812 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 813 | sub main { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 814 | 3 | ▶ | my ($class_or_self, @argv) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
main: called CC=15 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 815 | 3 | BATsh::Env::init(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 816 | # --encoding=ENC / --encoding ENC : cp932 sjis gbk uhc big5 utf8 none auto | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 817 | 3 | ▶ | while (@argv && $argv[0] =~ /\A--encoding(?:=(.*))?\z/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@argv and $argv[0] =~ /\A--encoding(?:=(.*))?\z/
MC/DC0%0/2
@argv and $argv[0] =~ /\A--encoding(?:=(.*))?\z/
@argv
$argv[0] =~ /\A--encoding(?:=(.*))?\z/
Truth table33%1/3
A and B
A= @argvB= $argv[0] =~ /\A--encoding(?:=(.*))?\z/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 818 | 0 | my $e = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 819 | 0 | shift @argv; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 820 | 0 | ▶ | $e = shift @argv if !defined $e && @argv; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $e and @argv
MC/DC0%0/2
not defined $e and @argv
not defined $e
@argv
Truth table0%0/3
not(A) and B
A= not defined $eB= @argv
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 821 | 0 | ▶ | BATsh->set_encoding($e) if defined $e; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 822 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 823 | 3 | ▶ | if (@argv && ($argv[0] eq '--version' || $argv[0] eq '-v')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
@argv and $argv[0] eq "--version" || $argv[0] eq "-v"
$argv[0] eq "--version" or $argv[0] eq "-v"
MC/DC0%0/3
@argv and $argv[0] eq "--version" || $argv[0] eq "-v"
@argv
$argv[0] eq "--version"
$argv[0] eq "-v"
Truth table25%1/4
A and B or C
A= @argvB= $argv[0] eq "--version"C= $argv[0] eq "-v"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 824 | 0 | print "BATsh $VERSION\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 825 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 826 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 827 | 3 | ▶ | if (@argv && ($argv[0] eq '--help' || $argv[0] eq '-h')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
@argv and $argv[0] eq "--help" || $argv[0] eq "-h"
$argv[0] eq "--help" or $argv[0] eq "-h"
MC/DC0%0/3
@argv and $argv[0] eq "--help" || $argv[0] eq "-h"
@argv
$argv[0] eq "--help"
$argv[0] eq "-h"
Truth table25%1/4
A and B or C
A= @argvB= $argv[0] eq "--help"C= $argv[0] eq "-h"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 828 | 0 | print <<"END_OF_USAGE"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 829 | usage: batsh [--encoding=ENC] script.batsh [args...] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 830 | batsh [--encoding=ENC] - (read the script from STDIN) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 831 | batsh [--encoding=ENC] -e 'source' (run inline source) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 832 | batsh (interactive REPL) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 833 | batsh --version | --help | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 834 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 835 | ENC: cp932 sjis gbk uhc big5 utf8 none auto (default: auto) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 836 | The process exit code is the script's exit code (exit N / EXIT [/B] N, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 837 | or the status of the last command). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 838 | END_OF_USAGE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 839 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 840 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 841 | 3 | ▶ | if (@argv == 0) { return BATsh->repl() } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 842 | 3 | ▶ | if ($argv[0] eq '-e') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 843 | 0 | shift @argv; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 844 | 0 | return BATsh->run_string(join("\n", @argv)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 845 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 846 | 3 | ▶ | if ($argv[0] eq '-') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 847 | 1 | shift @argv; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 848 | 1 | my @lines = <STDIN>; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 849 | 1 | return BATsh->run_string(join('', @lines), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 850 | script_name => '-', args => [@argv]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 851 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 852 | 2 | my $f = shift @argv; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 853 | 2 | return BATsh->run($f, args => [@argv]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 854 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 855 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 856 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 857 | # Run as script | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 858 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 859 | 33 | ▶ | unless (caller) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 860 | 0 | exit(BATsh->main(@ARGV)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 861 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 862 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 863 | 33 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 864 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 865 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||