| 1 | package BATsh::Env; | ||||||||||||||||||||||||||||
| 2 | # Copyright (c) 2026 INABA Hitoshi <ina.cpan@gmail.com> | ||||||||||||||||||||||||||||
| 3 | ###################################################################### | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # BATsh::Env - Shared environment variable store | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # v0.02 changes: | ||||||||||||||||||||||||||||
| 8 | # - Variable names are case-insensitive (cmd.exe compatible). | ||||||||||||||||||||||||||||
| 9 | # Stored internally in uppercase. | ||||||||||||||||||||||||||||
| 10 | # - SETLOCAL ENABLEDELAYEDEXPANSION flag tracked per scope. | ||||||||||||||||||||||||||||
| 11 | # - expand_cmd expands !VAR! when delayed expansion is active. | ||||||||||||||||||||||||||||
| 12 | # - expand_cmd expands %0..%9 and %* positional parameters. | ||||||||||||||||||||||||||||
| 13 | # - _expand_tilde_param: %~[fdpnx]*N batch-parameter tilde modifiers. | ||||||||||||||||||||||||||||
| 14 | # Modifiers f(full path), d(drive), p(dir), n(basename), x(ext). | ||||||||||||||||||||||||||||
| 15 | # Uses File::Spec and Cwd for cross-platform absolute path resolution. | ||||||||||||||||||||||||||||
| 16 | # - init() guards undef %ENV values (Windows compatibility). | ||||||||||||||||||||||||||||
| 17 | # | ||||||||||||||||||||||||||||
| 18 | ###################################################################### | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 64 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | BEGIN { if ($] < 5.006 && !defined(&warnings::import)) { $INC{'warnings.pm'} = 'stub'; eval 'package warnings; sub import {}' } } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=16.5
Branch: 1 branch
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
| |||||||||||||||||||||||||||||
| 22 | 64 | ▶ | use warnings; local $^W = 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 64 | ▶ | BEGIN { pop @INC if $INC[-1] eq '.' } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 64 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 64 | $VERSION = '0.11'; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 64 | require BATsh::MB; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 64 | ▶ | use File::Spec (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 64 | ▶ | BEGIN { eval { require Cwd } } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 64 | ▶ | BEGIN { eval { require POSIX } } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 64 | $VERSION = $VERSION; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Keys stored in UPPERCASE for case-insensitive lookup | ||||||||||||||||||||||||||||
| 36 | 64 | ▶ | use vars qw(%STORE); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # Delayed expansion flag | ||||||||||||||||||||||||||||
| 39 | 64 | ▶ | use vars qw($DELAYED_EXPANSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 64 | $DELAYED_EXPANSION = 0; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # SETLOCAL scope stack: each entry = { store => \%snap, delayed => $flag } | ||||||||||||||||||||||||||||
| 43 | 64 | ▶ | use vars qw(@SETLOCAL_STACK); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 64 | @SETLOCAL_STACK = (); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 490 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 490 | %STORE = (); | |||||||||||||||||||||||||||
| 48 | 490 | for my $k (keys %ENV) { | |||||||||||||||||||||||||||
| 49 | 15680 | ▶ | $STORE{uc($k)} = defined $ENV{$k} ? $ENV{$k} : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 490 | $DELAYED_EXPANSION = 0; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 5922 | ▶ | sub _key { return uc($_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
_key: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 2179 | ▶ | sub get { my ($c, $n) = @_; return $STORE{_key($n)} } | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 3688 | ▶ | sub set { my ($c, $n, $v) = @_; $STORE{_key($n)} = defined $v ? $v : '' } | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=2 SCAR=7.2
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 52 | ▶ | sub unset { my ($c, $n) = @_; delete $STORE{_key($n)} } | ||||||||||||||||||||||||||
|
Subroutine
unset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 3 | ▶ | sub exists_var { my ($c, $n) = @_; return exists $STORE{_key($n)} ? 1 : 0 } | ||||||||||||||||||||||||||
|
Subroutine
exists_var: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 40 | ▶ | sub sync_to_env { | ||||||||||||||||||||||||||
|
Subroutine
sync_to_env: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 40 | %ENV = (); | |||||||||||||||||||||||||||
| 62 | 40 | for my $k (keys %STORE) { | |||||||||||||||||||||||||||
| 63 | 1505 | ▶ | next if index($k, '%') >= 0; # %0..%9, %*, %%V: not env names | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 1415 | $ENV{$k} = BATsh::MB::dec($STORE{$k}); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 9 | ▶ | sub snapshot { my %s = %STORE; return { %s } } | ||||||||||||||||||||||||||
|
Subroutine
snapshot: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | 9 | ▶ | sub restore { my ($c, $s) = @_; %STORE = %{$s} } | ||||||||||||||||||||||||||
|
Subroutine
restore: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 69 | 115 | ▶ | sub delayed_expansion { return $DELAYED_EXPANSION } | ||||||||||||||||||||||||||
|
Subroutine
delayed_expansion: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 12 | ▶ | sub setlocal { | ||||||||||||||||||||||||||
|
Subroutine
setlocal: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 12 | my ($opts) = @_; | |||||||||||||||||||||||||||
| 73 | 12 | ▶ | $opts = '' unless defined $opts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 12 | my %snap = %STORE; | |||||||||||||||||||||||||||
| 75 | 12 | push @SETLOCAL_STACK, { store => { %snap }, delayed => $DELAYED_EXPANSION }; | |||||||||||||||||||||||||||
| 76 | 12 | ▶ | if ($opts =~ /ENABLEDELAYEDEXPANSION/i) { $DELAYED_EXPANSION = 1 } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 77 | 0 | elsif ($opts =~ /DISABLEDELAYEDEXPANSION/i) { $DELAYED_EXPANSION = 0 } | |||||||||||||||||||||||||||
| 78 | # ENABLEEXTENSIONS / DISABLEEXTENSIONS: accepted, not modelled | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 12 | ▶ | sub endlocal { | ||||||||||||||||||||||||||
|
Subroutine
endlocal: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | 12 | ▶ | unless (@SETLOCAL_STACK) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | warn "[BATsh] Warning: ENDLOCAL without matching SETLOCAL\n"; | |||||||||||||||||||||||||||
| 84 | 0 | return; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 12 | my $f = pop @SETLOCAL_STACK; | |||||||||||||||||||||||||||
| 87 | 12 | %STORE = %{$f->{store}}; | |||||||||||||||||||||||||||
| 88 | 12 | $DELAYED_EXPANSION = $f->{delayed}; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 92 | # expand_cmd: %VAR% expansion, then optional !VAR! delayed expansion | ||||||||||||||||||||||||||||
| 93 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 94 | 629 | ▶ | sub expand_cmd { | ||||||||||||||||||||||||||
|
Subroutine
expand_cmd: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 629 | my ($class, $str) = @_; | |||||||||||||||||||||||||||
| 96 | 629 | ▶ | return '' unless defined $str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # %~[modifiers][0-9]: batch parameter modifiers (e.g. %~dp0, %~nx1) | ||||||||||||||||||||||||||||
| 99 | # Must be processed BEFORE %VAR% to avoid being mis-parsed. | ||||||||||||||||||||||||||||
| 100 | 629 | $str =~ s/%~([fdpnxs]*)([0-9])/_expand_tilde_param($1, $2)/ge; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # Batch positional parameters: %0..%9 and %* (single % prefix, no closing %) | ||||||||||||||||||||||||||||
| 103 | # Must expand BEFORE %VAR% so that "%0 foo=%1" is not mis-parsed by | ||||||||||||||||||||||||||||
| 104 | # the greedy %([^%]+)% pattern as a single named variable. | ||||||||||||||||||||||||||||
| 105 | 629 | $str =~ s/%([0-9*])/ | |||||||||||||||||||||||||||
| 106 | 84 | ▶ | do { my $k = "%$1"; exists($STORE{$k}) ? $STORE{$k} : '' } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | /ge; | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # %VAR:~n,m% substring and %VAR:str1=str2% substitution | ||||||||||||||||||||||||||||
| 110 | # Must be processed BEFORE plain %VAR% expansion. | ||||||||||||||||||||||||||||
| 111 | 629 | $str =~ s/%([A-Za-z_][A-Za-z0-9_]*):([^% | |||||||||||||||||||||||||||
| 112 | 15 | ]+)%/_expand_var_modifier($1,$2)/ge; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # %VAR% substitution: dynamic pseudo-variables first, then STORE lookup | ||||||||||||||||||||||||||||
| 115 | 629 | $str =~ s/%([^%\r\n]+)%/_expand_named_var($1)/ge; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # %% -> literal % | ||||||||||||||||||||||||||||
| 118 | 629 | $str =~ s/%%/%/g; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # !VAR! delayed expansion (only when enabled) | ||||||||||||||||||||||||||||
| 121 | 629 | ▶ | if ($DELAYED_EXPANSION) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 19 | $str =~ s/!([A-Za-z_][A-Za-z0-9_]*)!/ | |||||||||||||||||||||||||||
| 123 | 5 | ▶ | do { my $k=uc($1); exists($STORE{$k}) ? $STORE{$k} : '' } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | /ge; | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 629 | return $str; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 131 | # _expand_tilde_param: resolve %~[fdpnx]*N batch-parameter modifiers | ||||||||||||||||||||||||||||
| 132 | # | ||||||||||||||||||||||||||||
| 133 | # Modifier letters (combinable, same as cmd.exe): | ||||||||||||||||||||||||||||
| 134 | # (none) strip surrounding double-quotes only | ||||||||||||||||||||||||||||
| 135 | # f fully qualified path (absolute) | ||||||||||||||||||||||||||||
| 136 | # d drive letter only (e.g. "C:" on Windows, "" on Unix) | ||||||||||||||||||||||||||||
| 137 | # p path component only (directory, with trailing separator) | ||||||||||||||||||||||||||||
| 138 | # n filename without extension | ||||||||||||||||||||||||||||
| 139 | # x extension only (including leading dot, e.g. ".bat") | ||||||||||||||||||||||||||||
| 140 | # | ||||||||||||||||||||||||||||
| 141 | # The value is taken from %N in the Env store (%0..%9). | ||||||||||||||||||||||||||||
| 142 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 143 | # _expand_named_var: resolve %VARNAME% with dynamic pseudo-variable support | ||||||||||||||||||||||||||||
| 144 | # | ||||||||||||||||||||||||||||
| 145 | # Dynamic pseudo-variables (read-only, computed at expansion time): | ||||||||||||||||||||||||||||
| 146 | # %RANDOM% pseudo-random integer 0-32767 (cmd.exe range) | ||||||||||||||||||||||||||||
| 147 | # %DATE% current date YYYY-MM-DD | ||||||||||||||||||||||||||||
| 148 | # %TIME% current time HH:MM:SS.cc | ||||||||||||||||||||||||||||
| 149 | # %CD% current working directory | ||||||||||||||||||||||||||||
| 150 | # %CMDCMDLINE% empty string (not meaningful in a pure-Perl interpreter) | ||||||||||||||||||||||||||||
| 151 | # %ERRORLEVEL% current ERRORLEVEL from BATsh::CMD (via hook function) | ||||||||||||||||||||||||||||
| 152 | # | ||||||||||||||||||||||||||||
| 153 | # All other names: looked up in %STORE (case-insensitive, as cmd.exe). | ||||||||||||||||||||||||||||
| 154 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 155 | 146 | ▶ | sub _expand_named_var { | ||||||||||||||||||||||||||
|
Subroutine
_expand_named_var: called CC=11 SCAR=24.4
| |||||||||||||||||||||||||||||
| 156 | 146 | my ($name) = @_; | |||||||||||||||||||||||||||
| 157 | 146 | my $upper = uc($name); | |||||||||||||||||||||||||||
| 158 | 146 | ▶ | if ($upper eq 'RANDOM') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 1 | return int(rand(32768)); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | 145 | ▶ | if ($upper eq 'DATE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 1 | my @t = localtime(time()); | |||||||||||||||||||||||||||
| 163 | 1 | return sprintf('%04d-%02d-%02d', $t[5]+1900, $t[4]+1, $t[3]); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 144 | ▶ | if ($upper eq 'TIME') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 1 | my @t = localtime(time()); | |||||||||||||||||||||||||||
| 167 | 1 | return sprintf('%02d:%02d:%02d.%02d', $t[2], $t[1], $t[0], 0); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | 143 | ▶ | if ($upper eq 'CD') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 1 | ▶ | return BATsh::MB::enc(defined(&Cwd::cwd) ? Cwd::cwd() : '.'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 142 | ▶ | if ($upper eq 'CMDCMDLINE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 0 | return ''; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | 142 | ▶ | if ($upper eq 'ERRORLEVEL') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | return defined(&BATsh::CMD::_get_errorlevel) | ||||||||||||||||||||||||||||
| 177 | ? BATsh::CMD::_get_errorlevel() | ||||||||||||||||||||||||||||
| 178 | 3 | ▶ | : (exists($STORE{ERRORLEVEL}) ? $STORE{ERRORLEVEL} : '0'); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | 139 | ▶ | return exists($STORE{$upper}) ? $STORE{$upper} : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 184 | # _expand_var_modifier: %VAR:~n,m% substring / %VAR:str1=str2% substitution | ||||||||||||||||||||||||||||
| 185 | # | ||||||||||||||||||||||||||||
| 186 | # Substring form (cmd.exe compatible): | ||||||||||||||||||||||||||||
| 187 | # %VAR:~n% characters from offset n to end | ||||||||||||||||||||||||||||
| 188 | # %VAR:~n,m% m characters starting at offset n (negative = from end) | ||||||||||||||||||||||||||||
| 189 | # | ||||||||||||||||||||||||||||
| 190 | # Substitution form: | ||||||||||||||||||||||||||||
| 191 | # %VAR:str1=str2% replace first occurrence of str1 with str2 | ||||||||||||||||||||||||||||
| 192 | # %VAR:*str1=str2% replace from start up-to-and-including first str1 | ||||||||||||||||||||||||||||
| 193 | # with str2 (cmd.exe *-prefix behaviour) | ||||||||||||||||||||||||||||
| 194 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 195 | 15 | ▶ | sub _expand_var_modifier { | ||||||||||||||||||||||||||
|
Subroutine
_expand_var_modifier: called CC=14 SCAR=26.6
| |||||||||||||||||||||||||||||
| 196 | 15 | my ($varname, $modifier) = @_; | |||||||||||||||||||||||||||
| 197 | 15 | my $val = do { | |||||||||||||||||||||||||||
| 198 | 15 | my $k = uc($varname); | |||||||||||||||||||||||||||
| 199 | 15 | ▶ | exists($STORE{$k}) ? $STORE{$k} : '' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | }; | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | # Substring: ~n or ~n,m | ||||||||||||||||||||||||||||
| 203 | 15 | ▶ | if ($modifier =~ /\A~(-?\d+)(?:,(-?\d+))?\z/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | 9 | my ($n, $m) = ($1, $2); | |||||||||||||||||||||||||||
| 205 | 9 | my $len = BATsh::MB::mb_length($val); | |||||||||||||||||||||||||||
| 206 | 9 | ▶ | my $start = ($n < 0) ? $len + $n : $n; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 9 | ▶ | $start = 0 if $start < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | 9 | ▶ | return '' if $start >= $len; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 8 | ▶ | if (!defined $m) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 2 | return BATsh::MB::mb_substr($val, $start); | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | 6 | my $end; | |||||||||||||||||||||||||||
| 213 | 6 | ▶ | if ($m < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | 1 | $end = $len + $m; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | else { | ||||||||||||||||||||||||||||
| 217 | 5 | $end = $start + $m; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | 6 | ▶ | $end = $len if $end > $len; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 6 | ▶ | return '' if $end <= $start; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | 5 | return BATsh::MB::mb_substr($val, $start, $end - $start); | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | # Substitution: str1=str2 or *str1=str2 | ||||||||||||||||||||||||||||
| 225 | 6 | ▶ | if ($modifier =~ /\A(\*?)([^=]*)=(.*)\z/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 6 | my ($star, $str1, $str2) = ($1, $2, $3); | |||||||||||||||||||||||||||
| 227 | 6 | ▶ | if ($star eq '*') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | 1 | my $pos = index(lc($val), lc($str1)); | |||||||||||||||||||||||||||
| 229 | 1 | ▶ | if ($pos >= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 1 | return $str2 . substr($val, $pos + length($str1)); | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | 0 | return $val; | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | else { | ||||||||||||||||||||||||||||
| 235 | 5 | my $lval = lc($val); | |||||||||||||||||||||||||||
| 236 | 5 | my $lstr1 = lc($str1); | |||||||||||||||||||||||||||
| 237 | 5 | my $pos = index($lval, $lstr1); | |||||||||||||||||||||||||||
| 238 | 5 | ▶ | if ($pos >= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | 4 | return substr($val, 0, $pos) . $str2 | |||||||||||||||||||||||||||
| 240 | . substr($val, $pos + length($str1)); | ||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | 1 | return $val; | |||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | # Unrecognised modifier: return as-is | ||||||||||||||||||||||||||||
| 247 | 0 | return '%' . $varname . ':' . $modifier . '%'; | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | # Uses File::Spec (platform-aware) and a hand-rolled path splitter so | ||||||||||||||||||||||||||||
| 251 | # that Windows-style paths work correctly on Windows and Unix-style | ||||||||||||||||||||||||||||
| 252 | # paths work on Unix without requiring Win32-specific modules. | ||||||||||||||||||||||||||||
| 253 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 254 | 30 | ▶ | sub _expand_tilde_param { | ||||||||||||||||||||||||||
|
Subroutine
_expand_tilde_param: called CC=22 SCAR=31.1
| |||||||||||||||||||||||||||||
| 255 | 30 | my ($mods, $n) = @_; | |||||||||||||||||||||||||||
| 256 | 30 | my $key = "%$n"; | |||||||||||||||||||||||||||
| 257 | 30 | ▶ | my $val = exists($STORE{$key}) ? $STORE{$key} : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | # Always strip surrounding double-quotes first | ||||||||||||||||||||||||||||
| 260 | 30 | $val =~ s/\A"//; | |||||||||||||||||||||||||||
| 261 | 30 | $val =~ s/"\z//; | |||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | # With no recognised modifiers, just return the dequoted value | ||||||||||||||||||||||||||||
| 264 | 30 | ▶ | return $val unless $mods =~ /[fdpnx]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | # --- Normalise: extract drive letter first, then convert \ to / --- | ||||||||||||||||||||||||||||
| 267 | # Extracting the drive before splitting avoids "C:" being treated as | ||||||||||||||||||||||||||||
| 268 | # a path component and re-attached incorrectly. | ||||||||||||||||||||||||||||
| 269 | 27 | my $drv = ''; # e.g. "C:" on Windows, "" on Unix | |||||||||||||||||||||||||||
| 270 | 27 | my $path = $val; | |||||||||||||||||||||||||||
| 271 | 27 | $path =~ s{\\}{/}g; # normalise separators | |||||||||||||||||||||||||||
| 272 | 27 | ▶ | if ($path =~ s{\A([A-Za-z]:)}{}) { $drv = $1 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | # --- resolve to absolute path when f/d/p requested --- | ||||||||||||||||||||||||||||
| 275 | 27 | ▶ | if ($mods =~ /[fdp]/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | 11 | ▶ | unless ($path =~ m{\A/} || $drv ne '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$path =~ m[\A/] or $drv ne ""
MC/DC50%1/2
$path =~ m[\A/] or $drv ne ""
$path =~ m[\A/]
$drv ne ""
Truth table66%2/3
A or B
A= $path =~ m[\A/]B= $drv ne ""
| |||||||||||||||||||||||||||||
| 277 | # relative Unix path: prepend cwd | ||||||||||||||||||||||||||||
| 278 | 2 | ▶ | my $cwd = BATsh::MB::enc(defined(&Cwd::cwd) ? Cwd::cwd() : '.'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 279 | 2 | $cwd =~ s{\\}{/}g; | |||||||||||||||||||||||||||
| 280 | 2 | $cwd =~ s{/+\z}{}; | |||||||||||||||||||||||||||
| 281 | 2 | $path = "$cwd/$path"; | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | # Ensure exactly one leading slash | ||||||||||||||||||||||||||||
| 284 | 11 | ▶ | $path = "/$path" unless $path =~ m{\A/}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | # Collapse . and .. | ||||||||||||||||||||||||||||
| 286 | 11 | my @segs; | |||||||||||||||||||||||||||
| 287 | 11 | for my $p (split m{/+}, $path) { | |||||||||||||||||||||||||||
| 288 | 75 | ▶ | next if $p eq '' || $p eq '.'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$p eq "" or $p eq "."
MC/DC50%1/2
$p eq "" or $p eq "."
$p eq ""
$p eq "."
Truth table66%2/3
A or B
A= $p eq ""B= $p eq "."
| |||||||||||||||||||||||||||||
| 289 | 64 | ▶ | if ($p eq '..') { pop @segs if @segs } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 290 | 60 | else { push @segs, $p } | |||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||
| 292 | 11 | $path = '/' . join('/', @segs); | |||||||||||||||||||||||||||
| 293 | 11 | ▶ | $path = '/' if $path eq '/'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | # --- split path into directory and filename --- | ||||||||||||||||||||||||||||
| 297 | 27 | my ($dirs, $file) = ('', ''); | |||||||||||||||||||||||||||
| 298 | 27 | ▶ | if ($path =~ m{\A(.*/)([^/]*)\z}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 299 | 23 | ($dirs, $file) = ($1, $2); | |||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | else { | ||||||||||||||||||||||||||||
| 302 | 4 | $file = $path; | |||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | # --- split filename into base and extension --- | ||||||||||||||||||||||||||||
| 306 | 27 | my ($base, $ext) = ('', ''); | |||||||||||||||||||||||||||
| 307 | 27 | ▶ | if ($file =~ m{\A(.+)(\.[^.]+)\z}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | 27 | ($base, $ext) = ($1, $2); | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | else { | ||||||||||||||||||||||||||||
| 311 | 0 | $base = $file; | |||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | # --- build result --- | ||||||||||||||||||||||||||||
| 315 | 27 | ▶ | if ($mods =~ /f/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | # Full absolute path: drive + dirs + file | ||||||||||||||||||||||||||||
| 317 | # dirs already ends with / when non-root | ||||||||||||||||||||||||||||
| 318 | 4 | return $drv . $dirs . $file; | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 23 | my $result = ''; | |||||||||||||||||||||||||||
| 322 | 23 | ▶ | $result .= $drv if $mods =~ /d/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | 23 | ▶ | $result .= $dirs if $mods =~ /p/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 23 | ▶ | $result .= $base if $mods =~ /n/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | 23 | ▶ | $result .= $ext if $mods =~ /x/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | 23 | return $result; | |||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 330 | # expand_sh: $VAR and ${VAR} (SH mode) | ||||||||||||||||||||||||||||
| 331 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 332 | 0 | ▶ | sub expand_sh { | ||||||||||||||||||||||||||
|
Subroutine
expand_sh: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 333 | 0 | my ($class, $str) = @_; | |||||||||||||||||||||||||||
| 334 | 0 | ▶ | return '' unless defined $str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 335 | 0 | $str =~ s/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/ | |||||||||||||||||||||||||||
| 336 | 0 | ▶ | do { my $k=$1; defined($STORE{$k}) ? $STORE{$k} : defined($STORE{uc($k)}) ? $STORE{uc($k)} : '' } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 337 | /ge; | ||||||||||||||||||||||||||||
| 338 | 0 | $str =~ s/\$([A-Za-z_][A-Za-z0-9_]*)/ | |||||||||||||||||||||||||||
| 339 | 0 | ▶ | do { my $k=$1; defined($STORE{$k}) ? $STORE{$k} : defined($STORE{uc($k)}) ? $STORE{uc($k)} : '' } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 340 | /ge; | ||||||||||||||||||||||||||||
| 341 | 0 | return $str; | |||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | 64 | 1; | |||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | __END__ | ||||||||||||||||||||||||||||