blib/lib/IO/ReadPreProcess.pm

filter: Statementstmt 90.6% 609 / 672 Branchbran 71.2% 349 / 490 Conditioncond 65.3% 98 / 150 MC/DCmcdc 40.0% 36 / 90 Subroutinesub 82.3% 28 / 34 Podpod 40.0% 10 / 25 totaltotal 77.3% 1130 / 1461 CC 300 SCAR 50.8
CC
300
Cov
80%
_getline
313.3
ReadBlock
33.0
clearerr
20.0
CRAP 160.3
↑ Summary
1 package IO::ReadPreProcess;
2  
3 # The idea is to provide an 'intelligent' bottom end read function for scripts.
4 # Read lines, process .if/.else/.fi, do .include .let .print - and more.
5 # It provides IO::Handle-ish functions to slot in easily to most scripts.
6  
7 # Author: Alain D D Williams <addw@phcomp.co.uk> March 2015, 2016, 2017 Copyright (C) the author.
8 # SCCS: @(#)ReadPreProcess.pm 1.14 08/15/17 23:19:49
9  
10 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 #use Data::Dumper;
14 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use IO::Pipe;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 our $errstr; # Error string
18  
19 1 ▶use Math::Expression;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 our $VERSION = 0.85;
22  
23 # Control directive recognised by getline():
24 1 my %ctlDirectives = map { $_ => 1 } qw/ break case close continue do done echo else elseif elsif endswitch error eval exit
25 fi for if include last let local next noop out print read return set sub switch test unless until while /;
26  
27 # Directives that can be used in a condition:
28 1 my %condDirectives = map { $_ => 1 } qw/ include read test /;
29  
30 # Need to test for this ... all except first line
31 1 my %forbidden = map { $_ => 1 } qw/ function sub /;
32  
33 # Block pairs: start & ends
34 1 my %blkPairs = qw/ sub done while done until done for done if fi unless fi /;
35 1 my %endings = map { $_ => 1 } qw/ done fi /;
36 1 my %loops = map { $_ => 1 } qw/ while until for /;
37 1 my %makeExpr = map { $_ => 1 } qw/ let if unless elsif elseif while until for /;
38 1 my %options = map { $_ => 1 } qw/ trace /;
39  
40 # Math variables (others see POD below):
41 # _FileNames - array of open file names
42 # _LineNumbers - array of open file names
43 # _IncludeDepth - how many files open
44 # _FileName _LineNumber - current ones
45 # The arrays are to allow the generation of a traceback.
46  
47 # Properties not described in new
48 # Information about the current file is kept as references so that it can be pushed down a stack (think: .include) and popped when it is closed.
49 #
50 # Lines contains refs else we would need to update before pushing
51 sub new
52 61 ▶{
Subroutine new: called CC=12 SCAR=25.3
Pod documented
53 61 my $class = shift;
54 61 my $self = bless {
55 FrameStk => [], # Frames
56 Frame => undef, # Reference to current frame (last in FrameStk)
57  
58 subs => {}, # Keys are known sub
59 Streams => {}, # Input streams
60  
61 out => "", # Name of current output stream, empty string is return line to program
62  
63 # Public properties:
64 MaxLoopCount => 50, # Max times that a loop can go round
65 Raw => 0, # Return input as it is seen
66 PipeOK => 0, # True if allowed to open a pipe
67 Trim => 1, # Trim input lines
68 OnError => 'warn', # Can set to: warn, die, ''
69 OutStreams => {}, # Key: name, value: Stream
70  
71 Place => '??', # Last read location: current file/line#
72  
73 DirStart => '.', # Directive start sequence
74 TopIsFd => 0, # First file pre-opened, ie Fd passed to open()
75 Error => 0, # Set true on error, functions then just return undef
76  
77 trace => 0, # 1 trace directives, 2 trace generated input
78 @_
79 }, $class;
80  
81 # These output streams are given for free:
82 61 ▶ $self->{OutStreams}->{STDOUT} = *STDOUT{IO} unless( defined($self->{OutStreams}->{STDOUT}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"OutStreams"}{"STDOUT"} 61 0 61
83 61 ▶ $self->{OutStreams}->{STDERR} = *STDERR{IO} unless( defined($self->{OutStreams}->{STDERR}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"OutStreams"}{"STDERR"} 61 0 61
84  
85 # Produce an escaped version of the directive start string. All the RE special prefix with a backslash.
86 # This will be used at the start of an RE but we want it taken literally.
87 61 ▶ unless(defined($self->{DirStartRE})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"DirStartRE"}) 61 0 61
88 61 $self->{DirStartRE} = $self->{DirStart};
89 61 $self->{DirStartRE} =~ s/([\$.()\[\]*+?\\^|])/\\$1/g;
90 }
91 # This is not worth it:
92 # $self->{dirLineRE} = qr/^($self->{DirStartRE})(\w*)\s*(.*)/;
93 # $self->{commentRE} = qr/^$self->{DirStartRE}#/;
94  
95 $self->{Math} = new Math::Expression(PermitLoops => 1, EnablePrintf => 1)
96 61 ▶ unless(defined $self->{Math});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"Math"} 61 0 61
97  
98 61 ▶ unless($self->{Math}->{VarHash}->{_Initialised}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"Math"}{"VarHash"}{"_Initialised"}) 61 0 61
99 61 $self->{Math}->ParseToScalar('_FileNames := EmptyList; _LineNumbers := EmptyList; _IncludeDepth := 0; _Initialised := 1');
100 61 $self->{Math}->ParseToScalar('_ARGS := (); _ := _EOF := 0');
101 61 $self->{Math}->ParseToScalar('_CountGen := _CountSkip := _CountDirect := _CountFrames := _CountOpen := 0');
102 }
103  
104 # We do some things a lot - compile them, that is the expensive part:
105 61 my %math = (
106 SetLineMath => '_LineNumbers[-1] := _LineNumber',
107 m_openFrame => 'push(_FileNames, _FileName); push(_LineNumbers, _LineNumber); ++_IncludeDepth',
108 m_closeFrame => 'pop(_FileNames); pop(_LineNumbers); --_IncludeDepth; _FileName := ""; _LineNumber := 0; if(count(_FileNames)){_FileName := _FileNames[-1]; _LineNumber := _LineNumbers[-1]}',
109 );
110  
111 61 ▶ while (my($p, $e) = each %math) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %math) 183 61 244
112 183 $self->{$p} = $self->{Math}->Parse($e);
113 }
114  
115 # Take some out - cause problems if there since we try to set them again
116 61 my %opts;
117 61 for (qw/ Fd File /) {
118 122 ▶ if(defined($self->{$_})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$_}) 63 59 122
119 63 $opts{$_} = $self->{$_};
120 63 delete $self->{$_};
121 }
122 }
123  
124 # Attempt to open the file if passed:
125 61 ▶ if(defined($opts{File})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"File"}) 61 0 61
126 61 ▶ if(defined($opts{Fd})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'Fd'}) { } 2 59 61
127 # Already open, note name, push to include stack:
128 2 $self->openFrame(binmode => '', %opts, Name => $opts{File}, LineNumber => 0, Generate => 1, ReturnFrom => 1);
129 } else {
130 59 ▶ return undef unless($self->open(binmode => '', %opts));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->open("binmode", "", %opts) 0 59 59
131 }
132 }
133  
134 $self
135 61 }
136  
137 # Open a file. Args:
138 # * File - a name - mandatory
139 # * Fd - a file handle that it is already open on - optional
140 # Return $self on OK, undef on error
141 # Pushes the current file on a stack that allows restore by close()
142 sub open
143 59 ▶{
Subroutine open: called CC=4 SCAR=14.8
Pod documented
144 59 my $self = shift;
145 59 my %args = @_;
146  
147 return $self->SetError('open() must be given File argument', 1)
148 59 ▶ if( !defined $args{File});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"File"} 0 59 59
149  
150 # But elsewhere File is called Name - which could be a sub name
151 59 $args{Name} = $args{File};
152 59 delete $args{File};
153  
154 # Get it open on $self->{Fd}
155 59 my $Fd;
156 59 ▶ if( !defined $args{Fd}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $args{'Fd'}) { } 59 0 59
157 59 ▶ return undef unless($Fd = $self->openFile($args{Name}));
Branch: 1 branch
BranchTrueFalseTotal
unless $Fd = $self->openFile($args{"Name"}) 0 59 59
158 } else {
159 # Use already opened Fd
160 0 $Fd = $args{Fd};
161 0 $self->{TopIsFd} = 1;
162 }
163  
164 59 $self->openFrame(%args, Fd => $Fd, LineNumber => 0, Generate => 1, ReturnFrom => 1);
165 }
166  
167 # Open a file, apply any binmode, return FD
168 sub openFile
169 89 ▶{
Subroutine openFile: called CC=3 SCAR=11.2
Pod undocumented
170 89 my ($self, $name) = @_;
171 89 my $Fd;
172  
173 89 ▶ return $self->SetError("Open of file '$name' failed: $! at $self->{Place}")
Branch: 1 branch
BranchTrueFalseTotal
unless $Fd = "IO::File"->new($name, "r") 0 89 89
174 unless($Fd = IO::File->new($name, 'r'));
175  
176 89 $self->{Math}->{VarHash}->{_CountOpen}->[0]++;
177  
178 89 ▶ $Fd->binmode($self->{Frame}->{binmode}) if($self->{Frame}->{binmode});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Frame"}{"binmode"} 0 89 89
179  
180 89 $Fd
181 }
182  
183 # Internal routine.
184 # Assign the new values and push onto their own stack so that, after a later open()
185 # it can be popped by a close() of that later file.
186 sub openFrame
187 465 ▶{
Subroutine openFrame: called CC=7 SCAR=19.5
Pod undocumented
188 465 my $self = shift;
189 465 my %args = @_;
190 465 my $vh = $self->{Math}->{VarHash};
191  
192 # Create the new frame:
193 465 my %f = ( PushedInput => [], @_ );
194  
195 # Create var _ARGS if argument Args:
196 465 ▶ if(defined($args{Args})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"Args"}) 200 265 465
197 200 $f{LocalVars} = {_ARGS => $vh->{_ARGS}};
198 200 delete $vh->{_ARGS};
199 200 $vh->{_ARGS} = $args{Args};
200 200 delete $f{Args};
201 }
202  
203 # One of Code or Fd must be passed
204 # Must have the following, if not set copy from enclosing frame:
205 465 for my $p (qw/ Code CodeLine Name LineNumber Fd Generate binmode /) {
206  
207 3255 ▶ $f{$p} = $args{$p} if(defined($args{$p}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{$p} 1175 2080 3255
208  
209 3255 ▶ if(defined($self->{Frame}) && defined($self->{Frame}->{$p})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Frame"} and defined $self->{"Frame"}{$p}) 2306 949 3255
Condition100%3/3
defined $self->{"Frame"} and defined $self->{"Frame"}{$p}
!ll&&!rl&&r
149352306
MC/DC100%2/2
defined $self->{"Frame"} and defined $self->{"Frame"}{$p}
defined $self->{"Frame"} defined $self->{"Frame"}{$p}
Truth table100%3/3
A and B
A= defined $self->{"Frame"}B= defined $self->{"Frame"}{$p}
ABresult
0X0
100
111
210 2306 ▶ $f{$p} = $self->{Frame}->{$p} unless(defined($f{$p}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $f{$p} 1556 750 2306
211 }
212 }
213  
214 465 $f{FrameStart} = "$f{Name}:$f{LineNumber}";
215  
216 465 push(@{$self->{FrameStk}}, \%f);
217 465 $self->{Frame} = \%f;
218  
219 465 $vh->{_CountFrames}->[0]++;
220 465 $vh->{_LineNumber}->[0] = $f{LineNumber};
221 465 $vh->{_FileName}->[0] = $f{Name};
222 465 $self->{Math}->EvalToScalar($self->{m_openFrame});
223  
224  
225 465 $self; # success
226 }
227  
228 # Close a file - this might mean closeing more than 1 frame
229 # An important block is a (.include) file.
230 # Check $self->{Fd} for there being an open file.
231 # Return false on error
232 sub close
233 130 ▶{
Subroutine close: called CC=3 SCAR=11.0
Pod documented
234 130 my $self = shift;
235  
236 # Unwind until we find a ReturnFrom:
237 130 my $rf;
238 130 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$rf or do { do { $rf = $self->{"Frame"}{"ReturnFrom"}; return undef unless $self->closeFrame } } 130 4 134
239 134 $rf = $self->{Frame}->{ReturnFrom};
240 return undef
241 134 ▶ unless($self->closeFrame);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->closeFrame 0 134 134
242 } until($rf);
243  
244 130 $self
245 }
246  
247 # Closes the current frame, pops back the previous one - if there was one
248 sub closeFrame
249 465 ▶{
Subroutine closeFrame: called CC=9 SCAR=22.0
Pod undocumented
250 465 my $self = shift;
251  
252 return $self->SetError("Cannot close when there is not a frame open", 1)
253 465 ▶ unless(defined $self->{Frame}->{Code} or defined $self->{Frame}->{Fd});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"Frame"}{"Code"} or defined $self->{"Frame"}{"Fd"} 0 465 465
Condition66%2/3
defined $self->{"Frame"}{"Code"} or defined $self->{"Frame"}{"Fd"}
l!l&&r!l&&!r
3241410
MC/DC0%0/2
defined $self->{"Frame"}{"Code"} or defined $self->{"Frame"}{"Fd"}
defined $self->{"Frame"}{"Code"} defined $self->{"Frame"}{"Fd"}
Truth table66%2/3
A or B
A= defined $self->{"Frame"}{"Code"}B= defined $self->{"Frame"}{"Fd"}
ABresult
1X1
011
000
254  
255 # If error: this will be an OS level error
256 465 ▶ return $self->SetError("IO::File I/O error: $!")
Branch: 1 branch
BranchTrueFalseTotal
if $self->error 0 465 465
257 if($self->error);
258  
259 # Don't close - we don't want to close STDIN, could cause problems
260 # Rely on the IO::File object for files that we have opened ourselves being unreferenced and thus closed.
261 # $self->{Fd}->close;
262  
263 # Pop back the previous state/file - if there was one
264 # Pop the description/state for the file just closed and assign
265 # state for the file just revealed - what is now at the top of the stack:
266  
267 # Pop any local vars:
268 465 ▶ if($self->{Frame}->{LocalVars}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Frame"}{"LocalVars"}) 202 263 465
269 202 my $alist = $self->{Frame}->{LocalVars};
270 202 my $vh = $self->{Math}->{VarHash};
271 202 ▶ while (my ($k, undef) = each %$alist) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($k, undef) = each %$alist) 454 202 656
272 454 delete($vh->{$k});
273 454 ▶ $vh->{$k} = $alist->{$k} if(defined($alist->{$k}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $alist->{$k} 228 226 454
274 }
275 }
276  
277  
278 465 my $old = pop(@{$self->{FrameStk}});
279 465 $self->{Frame} = $self->{FrameStk}->[-1];
280  
281 465 ▶ $self->{Frame}->{CodeLine} = $old->{CodeLine} if($self->{Frame}->{Code} && $old->{CpMove});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Frame"}{"Code"} and $old->{"CpMove"} 32 433 465
Condition100%3/3
$self->{"Frame"}{"Code"} and $old->{"CpMove"}
!ll&&!rl&&r
19923432
MC/DC100%2/2
$self->{"Frame"}{"Code"} and $old->{"CpMove"}
$self->{"Frame"}{"Code"} $old->{"CpMove"}
Truth table100%3/3
A and B
A= $self->{"Frame"}{"Code"}B= $old->{"CpMove"}
ABresult
0X0
100
111
282  
283 # Get arith variables in sync
284 465 $self->{Math}->EvalToScalar($self->{m_closeFrame});
285  
286 465 $self
287 }
288  
289 # This package is intended to read text files - so straight binmode is prob not wanted.
290 # But binmode is also used to allow different encoding - eg :utf8
291 # Return true on success, on error undef with error in $!
292 # Record the mode in the frame, inherited by child frames
293 sub binmode
294 0 ▶{
Subroutine binmode: not called CC=3 SCAR=24.8
Pod documented
295 0 my ($self, $mode) = @_;
296  
297 0 ▶ return $self->SetError("binmode: a file has not been opened", 1) unless $self->{Frame}->{Fd};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Frame"}{"Fd"} 0 0 0
298  
299 0 $self->{Frame}->{binmode} = $mode;
300  
301 $self->{Frame}->{Fd}->binmode($mode) # Pass the call straight down
302 0 ▶ unless(ref $self->{Frame}->{Fd} eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"Frame"}{"Fd"} eq "ARRAY" 0 0 0
303 }
304  
305 # Return 1 if the next read will return EOF or the file is not open:
306 sub eof
307 0 ▶{
Subroutine eof: not called CC=3 SCAR=24.8
Pod documented
308 0 my $self = shift;
309 0 ▶ return 1 unless($self->{Fd});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Fd"} 0 0 0
310 0 ▶ return !@{$self->{Fd}} if(ref $self->{Fd} eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"Fd"} eq "ARRAY" 0 0 0
311 0 $self->{Fd}->eof;
312 }
313  
314 # Get the name of the file to open
315 # Args:
316 # * name
317 # * just return undef if cannot open, don't print error - optional
318 # First process escapes
319 # If it starts '$', the next word is a variable, use the value(s) like $PATH - search
320 # Else resolve
321 sub ResolveFilename
322 32 ▶{
Subroutine ResolveFilename: called CC=9 SCAR=24.6
Pod undocumented
323 32 my ($self, $name, $noerr) = @_;
324  
325 # If it starts '$'
326 32 ▶ if(substr($name, 0, 1) eq '$') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($name, 0, 1) eq "\$") 5 27 32
327 5 ▶ return $self->SetError("Bad syntax include file name: '$name' at $self->{Place}", 1)
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ m[^\$(\w+)(/.+)$]i 0 5 5
328 unless($name =~ m:^\$(\w+)(/.+)$:i);
329 5 my ($var, $rest) = ($1, $2);
330  
331 5 my ($pt, @val);
332 return $self->SetError("Bad expression in include: '$name' at $self->{Place}", 1)
333 5 ▶ unless(($pt = $self->{Math}->Parse($var)) && (@val = $self->{Math}->Eval($pt)));
Branch: 1 branch
BranchTrueFalseTotal
unless $pt = $self->{"Math"}->Parse($var) and @val = $self->{"Math"}->Eval($pt) 0 5 5
Condition33%1/3
$pt = $self->{"Math"}->Parse($var) and @val = $self->{"Math"}->Eval($pt)
!ll&&!rl&&r
005
MC/DC0%0/2
$pt = $self->{"Math"}->Parse($var) and @val = $self->{"Math"}->Eval($pt)
$pt = $self->{"Math"}->Parse($var) @val = $self->{"Math"}->Eval($pt)
Truth table33%1/3
A and B
A= $pt = $self->{"Math"}->Parse($var)B= @val = $self->{"Math"}->Eval($pt)
ABresult
0X0
100
111
334  
335 # Search down path:
336 5 for my $pref (@val) {
337 12 my $fp = $self->GetPath("$pref$rest");
338 12 ▶ return undef unless $fp;
Branch: 1 branch
BranchTrueFalseTotal
unless $fp 0 12 12
339 12 ▶ return $fp # Grab it if it exists
Branch: 1 branch
BranchTrueFalseTotal
if -e $fp 5 7 12
340 if(-e $fp);
341 }
342  
343 0 ▶ return undef if($noerr);
Branch: 1 branch
BranchTrueFalseTotal
if $noerr 0 0 0
344  
345 0 return $self->SetError("Cannot find a file in search '$name'. $var='@val' at $self->{Place}", 1)
346 }
347  
348 # Plain file name:
349 27 return $self->GetPath($name);
350 }
351  
352 # If it is absolute (starts '/'): accept
353 # If it starts '#' it is relative to the process's CWD, remove '#' & accept
354 # The rest are relative to the current file name: prepend any directory name
355 # Don't try to canonicalise a/b/../c to a/c - think symlinks.
356 sub GetPath
357 39 ▶{
Subroutine GetPath: called CC=7 SCAR=20.0
Pod undocumented
358 39 my ($self, $name) = @_;
359  
360 # Absolute path:
361 39 ▶ return $name if index($name, '/') == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($name, "/") == 0 4 35 39
362  
363 # Relative to our CWD:
364 35 ▶ if(substr($name, 0, 1) eq '#') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($name, 0, 1) eq "#") 9 26 35
365 9 $name = substr($name, 1); # Remove #
366 9 ▶ $name = substr($name, 1) # Remove / after #
Branch: 1 branch
BranchTrueFalseTotal
substr($name, 0, 1) eq "/" and do { $name = substr($name, 1) } 1 9 10
367 while(substr($name, 0, 1) eq '/');
368 9 return $name;
369 }
370  
371 # Everything else is relative to the current file
372  
373 # Cannot have a relative name if the current file was passed as Fd
374 return $self->SetError("Cannot include file relative to file descriptor. '$name' at $self->{Place}", 1)
375 0 ▶ if($self->{TopIsFd} && @{$self->{FrameStk}} == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"TopIsFd"} and @{$self->{"FrameStk"};} == 1 0 26 26
Condition33%1/3
$self->{"TopIsFd"} and @{$self->{"FrameStk"};} == 1
!ll&&!rl&&r
2600
MC/DC0%0/2
$self->{"TopIsFd"} and @{$self->{"FrameStk"};} == 1
$self->{"TopIsFd"} @{$self->{"FrameStk"};} == 1
Truth table33%1/3
A and B
A= $self->{"TopIsFd"}B= @{$self->{"FrameStk"};} == 1
ABresult
0X0
100
111
376 # **** This refers to self->file - on stack & called Name
377  
378 # Find the last opened file name
379 26 my $last;
380 return undef
381 26 ▶ unless($last = $self->GetLastFileName);
Branch: 1 branch
BranchTrueFalseTotal
unless $last = $self->GetLastFileName 0 26 26
382  
383 # Note RE ensures that $currDir is '' if $last does not contain '/':
384 26 my ($currDir) = $last =~ m:^(.*?/?)[^/]+$:;
385  
386 26 $currDir . $name
387 }
388  
389 # Get the name of the last file opened, dig down the stack
390 sub GetLastFileName
391 26 ▶{
Subroutine GetLastFileName: called CC=3 SCAR=11.2
Pod undocumented
392 26 my ($self) = @_;
393  
394 26 my $frames = @{$self->{FrameStk}};
395  
396 26 ▶ while(--$frames >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$frames >= 0) 48 0 48
397 48 ▶ return $self->{FrameStk}->[$frames]->{Name} if(exists($self->{FrameStk}->[$frames]->{Fd}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"FrameStk"}[$frames]{"Fd"} 26 22 48
398 }
399  
400 0 return $self->SetError("Cannot find previous file name at $self->{Place}", 1);
401 }
402  
403 # Line parsed for escapes: \0 \e \v{varname}. varname is: /\w+/i
404 # Arg is a string that is processed & returned
405 sub ProcEscapes
406 2866 ▶{
Subroutine ProcEscapes: called CC=8 SCAR=21.6
Pod undocumented
407 2866 my ($self, $arg) = @_;
408  
409 2866 my $ret = '';
410  
411 2866 ▶ while($arg =~ s/^([^\\]*)\\(.)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ s/^([^\\]*)\\(.)//) 840 2866 3706
412 840 $ret .= $1;
413 840 ▶ if($2 eq '0') {
Branch: 3 branches
BranchTrueFalseTotal
if ($2 eq '0') { } 0 840 840
elsif ($2 eq 'e') { } 1 839 840
elsif ($2 eq 'v') { } 839 0 839
414 ; # nothing
415 } elsif($2 eq 'e') {
416 1 $ret .= '\\';
417 } elsif($2 eq 'v') {
418 839 ▶ return $self->SetError("Invalid escape \\v$arg at $self->{Place}", 1)
Branch: 1 branch
BranchTrueFalseTotal
unless $arg =~ s/^{(\w+|\w+\[\w+\])}//i 0 839 839
419 unless($arg =~ s/^{(\w+|\w+\[\w+\])}//i);
420 839 my $vn = $1;
421 839 my $vv = $self->{Math}->ParseToScalar($1);
422 839 ▶ return $self->SetError("Invalid variable in \\v{$1} at $self->{Place}", 1)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $vv 0 839 839
423 unless(defined($vv));
424 839 $ret .= $vv;
425 } else {
426 0 return $self->SetError("Invalid escape \\$2 at $self->{Place}", 1);
427 }
428 }
429 2866 ▶ return $self->SetError("Trailing \\ on line at $self->{Place}", 1)
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /\\/ 0 2866 2866
430 if($arg =~ /\\/);
431  
432 2866 $ret . $arg;
433 }
434  
435 # Split the argument string on spaces into an array of strings.
436 # If a portion starts with a quote, it may contain a space
437 # If $doEsc each result is processed by ProcEscapes()
438 # Return the result or false
439 sub SplitArgs
440 598 ▶{
Subroutine SplitArgs: called CC=6 SCAR=17.9
Pod undocumented
441 598 my ($self, $arg, $doEsc) = @_;
442 598 my @args = ();
443  
444 598 $arg =~ s/^\s*//;
445 598 ▶ while($arg ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg ne "") 2474 598 3072
446 2474 my $c1 = substr($arg, 0, 1);
447 2474 ▶ if($c1 eq '"' or $c1 eq "'") {
Branch: 1 branch
BranchTrueFalseTotal
if ($c1 eq '"' or $c1 eq q[']) { } 10 2464 2474
Condition100%3/3
$c1 eq "\"" or $c1 eq "'"
l!l&&r!l&&!r
192464
MC/DC100%2/2
$c1 eq "\"" or $c1 eq "'"
$c1 eq "\"" $c1 eq "'"
Truth table100%3/3
A or B
A= $c1 eq "\""B= $c1 eq "'"
ABresult
1X1
011
000
448 # Extract the string delimited by quotes
449 10 ▶ return $self->SetError("Bad quoted string at $self->{Place}", 1)
Branch: 1 branch
BranchTrueFalseTotal
unless $arg =~ s/^(["'])((\\{2})*|(.*?[^\\](\\{2})*))\1\s*// 0 10 10
450 unless($arg =~ s/^(["'])((\\{2})*|(.*?[^\\](\\{2})*))\1\s*//);
451 10 my $m = $2;
452 10 $m =~ s/\\([\\'"])/$1/g; # Remove embedded escapes, eg: \" => "
453 10 push(@args, $m);
454 } else {
455 2464 $arg =~ s/^(\S+)\s*//;
456 2464 push(@args, $1);
457 }
458  
459 }
460  
461 598 ▶ @args = map { $self->ProcEscapes($_) } @args if($doEsc);
Branch: 1 branch
BranchTrueFalseTotal
if $doEsc 560 38 598
462  
463 @args
464 598 }
465  
466 # Read & store a sub or function to hash in $self->{subs}->{Name}
467 # Don't start a frame since we are just reading this in
468 # Return true if OK
469 sub readSub
470 38 ▶{
Subroutine readSub: called CC=6 SCAR=18.1
Pod undocumented
471 38 my ($self, $direc, $InLine, $arg) = @_;
472  
473 # Check that $self->{Frame}->{Fd} is an open file
474  
475 38 my $code = { };
476  
477 38 my @args;
478  
479 38 ▶ return $self->SetError("Missing $direc name at $self->{Place}", 1) unless($arg ne '');
Branch: 1 branch
BranchTrueFalseTotal
unless $arg ne "" 0 38 38
480  
481 # Also need to check that name & args are IDs
482 38 ▶ return undef unless(@args = $self->SplitArgs($arg, 0));
Branch: 1 branch
BranchTrueFalseTotal
unless @args = $self->SplitArgs($arg, 0) 0 38 38
483  
484 # First is the name:
485 38 $code->{Name} = shift @args;
486 return $self->SetError("Error: bad sub name '$code->{Name}' at $self->{Place}")
487 38 ▶ unless($code->{Name} =~ /^\w+$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $code->{"Name"} =~ /^\w+$/ 0 38 38
488  
489 return $self->SetError("Error: Redefinition of sub '$code->{Name}' at $self->{Place}")
490 38 ▶ if(exists($self->{subs}->{$code->{Name}}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"subs"}{$code->{"Name"}} 0 38 38
491  
492 38 $self->{subs}->{$code->{Name}} = $code;
493 38 $code->{ArgNames} = @args;
494  
495 # sub args can have names:
496 38 ▶ $code->{ArgNames} = \@args if(@args);
Branch: 1 branch
BranchTrueFalseTotal
if @args 9 29 38
497  
498 38 $code->{Block} = $direc; # Info only
499  
500 38 $self->ReadBlock($InLine, $code);
501  
502 38 $code->{LastLine}--; # Remove .done
503 38 $code->{FirstLine}++; # Remove .sub
504  
505 38 1
506 }
507  
508 # $direct is while/until/for
509 # $arg is loop condition/rest-of-line
510 # May start: -innn to specify max iterations
511 # **** THINKS ****
512 # Loops are found in 2 ways:
513 # (1) Reading from a {Fd} - ie in getline()
514 # (2) When in a sub or an enclosing loop
515 # We always buffer a loop, so the only difference is where/how it is found
516 # The purpose of this sub is for case (1), need to initiate a buffer creation
517 # If (1) read into a buffer/code and return a ref to the code
518 # If (2) set up $code and return that
519 sub ReadLoop
520 17 ▶{
Subroutine ReadLoop: called CC=1 SCAR=0.0
Pod undocumented
521 17 my ($self, $direc, $InLine, $arg) = @_;
522  
523 17 my $frame = $self->{Frame};
524  
525 17 my $code = { Block => $direc };
526  
527 17 $self->ReadBlock($InLine, $code);
528  
529 17 $code
530 }
531  
532 # Read a block (sub or loop) to arg \%code
533 # If this finds a loop - note it as within what we read -- works for sub & nested loops
534 # $InLine is the line just read
535 sub ReadBlock
536 55 ▶{
Subroutine ReadBlock: called CC=30 SCAR=35.0
Pod undocumented
537 55 my ($self, $InLine, $code) = @_;
538  
539 # Record where this was found:
540 55 my $h={ FileName => $self->{Frame}->{Name}, FileLine => $self->{Frame}->{LineNumber}};
541 55 ▶ while (my($k,$v)= (each %$h)){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$h) 110 55 165
542 110 $code->{$k} = $v;
543 }
544  
545 55 $code->{start} = "$code->{FileName}:$code->{FileLine}";
546  
547 55 my $started = "started $code->{start}";
548 55 my @blocks;
549  
550 55 my $frame = $self->{Frame};
551 55 my $lineNo; # when reading existing array
552  
553 55 $code->{FirstLine} = 0;
554 55 $code->{Lines} = [];
555  
556 55 my $lineCnt = 0;
557  
558 55 while(1) {
559  
560 566 my $line = { Txt => $InLine, '#' => $. };
561  
562 # Quick return if it cannot be a directive line - or one that we recognise
563 # If not generating - skip to next
564 566 ▶ unless($InLine =~ /^($self->{DirStartRE})(\w+)\s*(.*)/ and
Branch: 1 branch
BranchTrueFalseTotal
if ($InLine =~ /^($self->{'DirStartRE'})(\w+)\s*(.*)/ and defined $ctlDirectives{$2} || defined $self->{'subs'}{$2}) { } 347 219 566
Condition66%4/6
$InLine =~ /^($self->{"DirStartRE"})(\w+)\s*(.*)/ and defined $ctlDirectives{$2} || defined $self->{"subs"}{$2}
!ll&&!rl&&r
2190347
defined $ctlDirectives{$2} or defined $self->{"subs"}{$2}
l!l&&r!l&&!r
34160
MC/DC33%1/3
$InLine =~ /^($self->{"DirStartRE"})(\w+)\s*(.*)/ and defined $ctlDirectives{$2} || defined $self->{"subs"}{$2}
$InLine =~ /^($self->{"DirStartRE"})(\w+)\s*(.*)/ defined $ctlDirectives{$2} defined $self->{"subs"}{$2}
Truth table75%3/4
A and B or C
A= $InLine =~ /^($self->{"DirStartRE"})(\w+)\s*(.*)/B= defined $ctlDirectives{$2}C= defined $self->{"subs"}{$2}
ABCresult
0XX0
1000
11X1
1011
565 (defined($ctlDirectives{$2}) or defined($self->{subs}->{$2}))) {
566 219 ▶ push @{$code->{Lines}}, $line unless(defined $frame->{Code});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $frame->{"Code"} 219 0 219
567 219 $lineCnt++;
568 } else {
569  
570 347 my $leadin = $1; # String that identified the directive
571 347 my $dir = $2; # Directive
572 347 my $arg = $3; # Its argument
573  
574 347 ▶ if(exists $loops{$dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $loops{$dir}) 41 306 347
575 # Loop buster:
576 41 my $max = $self->{MaxLoopCount};
577 41 ▶ $max = $1 if($arg =~ s/^-i\s*(\d+)\s*//);
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ s/^-i\s*(\d+)\s*// 3 38 41
578 41 $line->{LoopMax} = $max;
579  
580 # Get loop condition:
581 41 ▶ return $self->SetError("Missing $dir condition at $self->{Place}", 1) unless($arg ne '');
Branch: 1 branch
BranchTrueFalseTotal
unless $arg ne "" 0 41 41
582 41 my $cond = $arg;
583 41 $line->{Not} = $dir eq 'until';
584  
585 41 ▶ if($dir eq 'for') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "for") 14 27 41
586 # Break out, 3 expressions, preserve trailing ones
587 14 my @e = split /;;/, $arg, 4;
588 14 ▶ return $self->SetError("Bad for loop, expecting: 'init ;; condition ;; expression' at $self->{Place}", 1)
Branch: 1 branch
BranchTrueFalseTotal
unless @e == 3 0 14 14
589 unless(@e == 3);
590  
591 14 ▶ $line->{Init} = $e[0] if($e[0] =~ /\S/);
Branch: 1 branch
BranchTrueFalseTotal
if $e[0] =~ /\S/ 11 3 14
592  
593 14 ▶ $e[1] = '1' unless($e[1] =~ /\S/); # Set default condition - true
Branch: 1 branch
BranchTrueFalseTotal
unless $e[1] =~ /\S/ 0 14 14
594 14 $cond = $e[1];
595  
596 14 ▶ if($e[2] =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e[2] =~ /\S/) 11 3 14
597 return $self->SetError("$dir for expression '$e[2]' fails to compile at $self->{Place}", 1)
598 11 ▶ unless($line->{For3} = $self->{Math}->Parse($e[2]));
Branch: 1 branch
BranchTrueFalseTotal
unless $line->{"For3"} = $self->{"Math"}->Parse($e[2]) 0 11 11
599 }
600 }
601  
602 # Compile the condition below:
603 41 $cond =~ s/^\s*//;
604 41 $arg = $cond;
605 }
606 347 ▶ if(exists $makeExpr{$dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $makeExpr{$dir}) 89 258 347
607 # Precompile expression unless it is a .sub (starts '.'):
608 89 ▶ if(substr($arg, 0, length($self->{DirStart})) eq $self->{DirStart}) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($arg, 0, length $self->{'DirStart'}) eq $self->{'DirStart'}) { } 43 46 89
609 43 $line->{SubCond} = $arg;
610 } else {
611 return $self->SetError("$dir condition/expression fails to compile '$arg' at $self->{Place}", 1)
612 46 ▶ unless($arg =~ /\S/ and ($line->{Expr} = $self->{Math}->Parse($arg)));
Branch: 1 branch
BranchTrueFalseTotal
unless $arg =~ /\S/ and $line->{"Expr"} = $self->{"Math"}->Parse($arg) 0 46 46
Condition33%1/3
$arg =~ /\S/ and $line->{"Expr"} = $self->{"Math"}->Parse($arg)
!ll&&!rl&&r
0046
MC/DC0%0/2
$arg =~ /\S/ and $line->{"Expr"} = $self->{"Math"}->Parse($arg)
$arg =~ /\S/ $line->{"Expr"} = $self->{"Math"}->Parse($arg)
Truth table33%1/3
A and B
A= $arg =~ /\S/B= $line->{"Expr"} = $self->{"Math"}->Parse($arg)
ABresult
0X0
100
111
613 }
614 }
615  
616 347 ▶ if(defined($blkPairs{$dir})) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $blkPairs{$dir}) { } 100 247 347
elsif (defined $blkPairs{$blocks[-1]{'type'}} and $blkPairs{$blocks[-1]{'type'}} eq $dir) { } 100 147 247
elsif (defined $endings{$dir}) { } 0 147 147
617 # Start of block
618 100 push @blocks, {type => $dir, LoopStart => @{$code->{Lines}}+0 };
619 ▶ } elsif(defined($blkPairs{$blocks[-1]->{type}}) and $blkPairs{$blocks[-1]->{type}} eq $dir) {
Condition66%2/3
defined $blkPairs{$blocks[-1]{"type"}} and $blkPairs{$blocks[-1]{"type"}} eq $dir
!ll&&!rl&&r
0147100
MC/DC50%1/2
defined $blkPairs{$blocks[-1]{"type"}} and $blkPairs{$blocks[-1]{"type"}} eq $dir
defined $blkPairs{$blocks[-1]{"type"}} $blkPairs{$blocks[-1]{"type"}} eq $dir
Truth table66%2/3
A and B
A= defined $blkPairs{$blocks[-1]{"type"}}B= $blkPairs{$blocks[-1]{"type"}} eq $dir
ABresult
0X0
100
111
620 # End of block
621  
622 100 my $blk = pop @blocks;
623  
624 # Consistency check
625 return $self->SetError("$leadin$dir followed by '$1' but match is '$blk->{type}' at $self->{Place}", 1)
626 100 ▶ if($arg =~ /(\S+)/ and $blk->{type} ne $1);
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /(\S+)/ and $blk->{"type"} ne $1 0 100 100
Condition66%2/3
$arg =~ /(\S+)/ and $blk->{"type"} ne $1
!ll&&!rl&&r
9820
MC/DC0%0/2
$arg =~ /(\S+)/ and $blk->{"type"} ne $1
$arg =~ /(\S+)/ $blk->{"type"} ne $1
Truth table66%2/3
A and B
A= $arg =~ /(\S+)/B= $blk->{"type"} ne $1
ABresult
0X0
100
111
627  
628 # If loop add LoopStart/LoopEnd
629 100 ▶ if(exists $loops{$blk->{type}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $loops{$blk->{"type"}}) 41 59 100
630 41 my $start = $blk->{LoopStart};
631 41 my $topl = $code->{Lines}->[$start];
632 41 $topl->{LoopStart} = $start;
633 41 $topl->{LoopEnd} = @{$code->{Lines}}+1;
634 }
635  
636 } elsif(defined($endings{$dir})) {
637 0 return $self->SetError("Unexpected $leadin$dir at $self->{Place} in $code->{Block} $started", 1)
638 }
639  
640 # Buffer in array
641 347 push @{$code->{Lines}}, $line;
642 347 $lineCnt++;
643  
644 347 ▶ last if($dir eq 'done' and @blocks == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq "done" and @blocks == 0 55 292 347
Condition100%3/3
$dir eq "done" and @blocks == 0
!ll&&!rl&&r
2682455
MC/DC100%2/2
$dir eq "done" and @blocks == 0
$dir eq "done" @blocks == 0
Truth table100%3/3
A and B
A= $dir eq "done"B= @blocks == 0
ABresult
0X0
100
111
645 }
646  
647 # Next line
648 do {
649 0 return $self->SetError("Unexpected EOF at $self->{Place} while reading $code->{Block} $started", 1)
650 0 ▶ } unless($InLine = (ref $self->{Frame}->{Fd} eq 'ARRAY') ? shift @{$self->{Frame}->{Fd}} : $self->{Frame}->{Fd}->getline);
Branch: 2 branches
BranchTrueFalseTotal
unless ($InLine = ref $self->{"Frame"}{"Fd"} eq "ARRAY" ? shift @{$$self{"Frame"}{"Fd"};} : $self->{"Frame"}{"Fd"}->getline) 0 511 511
ref $self->{'Frame'}{'Fd'} eq 'ARRAY' ? : 0 511 511
651  
652 511 $self->{Place} = "line $. of $self->{Frame}->{Name}";
653 }
654  
655 55 $code->{LastLine} = $code->{FirstLine} + $lineCnt - 1;
656 }
657  
658 # Run a sub: open a frame, process arguments
659 sub RunSub
660 165 ▶{
Subroutine RunSub: called CC=5 SCAR=16.1
Pod undocumented
661 165 my ($self, $dir, $arg) = @_;
662 165 my @args = $self->SplitArgs($arg, 1);
663 165 my $code = $self->{subs}->{$dir}; # Code read earlier
664  
665 # New frame to run the sub
666 $self->openFrame(Code => $code, Block => $dir, Args => [@args],
667 LineNumber => $code->{FileLine}, Name => $code->{FileName},
668 165 CodeLine => $code->{FirstLine}, ReturnFrom => 1);
669 165 my $frame = $self->{Frame};
670 165 delete $frame->{Fd};
671  
672 # If argument names are supplied, set as local vars:
673 165 ▶ if($code->{ArgNames} && @{$code->{ArgNames}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code->{"ArgNames"} and @{$code->{"ArgNames"};}) 112 53 165
Condition66%2/3
$code->{"ArgNames"} and @{$code->{"ArgNames"};}
!ll&&!rl&&r
530112
MC/DC50%1/2
$code->{"ArgNames"} and @{$code->{"ArgNames"};}
$code->{"ArgNames"} @{$code->{"ArgNames"};}
Truth table66%2/3
A and B
A= $code->{"ArgNames"}B= @{$code->{"ArgNames"};}
ABresult
0X0
100
111
674 112 my $vh = $self->{Math}->{VarHash};
675 112 foreach my $vname (@{$code->{ArgNames}}) {
676 228 my $vval = $vh->{$vname};
677 228 $frame->{LocalVars}->{$vname} = $vval;
678 228 delete($vh->{$vname});
679  
680 228 ▶ $vh->{$vname} = [shift @args] if(@args);
Branch: 1 branch
BranchTrueFalseTotal
if @args 223 5 228
681 }
682 }
683 }
684  
685  
686 # Evaluate the condition, return true/false, or undef on error
687 # This could be a Math expression or a .sub returned value
688 # BEWARE: This could open a new frame to set up a sub, return to run it and frame.CondReReun
689 # will make the .if/... return here to see what the .return was.
690 sub EvalCond
691 479 ▶{
Subroutine EvalCond: called CC=17 SCAR=28.4
Pod undocumented
692 479 my ($self, $replay, $dir, $place, $arg) = @_;
693 479 my ($iftree, $true, $esc);
694  
695 # Is the condition a sub-call/directive ?
696 479 ▶ if(($esc = exists($replay->{SubCond})) or
Branch: 1 branch
BranchTrueFalseTotal
if ($esc = exists $replay->{'SubCond'} or substr($arg, 0, length $self->{'DirStart'}) eq $self->{'DirStart'} and $arg =~ /^$self->{'DirStartRE'}(\w+)\s*(.*)/) { } 286 193 479
Condition83%5/6
$esc = exists $replay->{"SubCond"} or substr($arg, 0, length $self->{"DirStart"}) eq $self->{"DirStart"} and $arg =~ /^$self->{"DirStartRE"}(\w+)\s*(.*)/
l!l&&r!l&&!r
24442193
substr($arg, 0, length $self->{"DirStart"}) eq $self->{"DirStart"} and $arg =~ /^$self->{"DirStartRE"}(\w+)\s*(.*)/
!ll&&!rl&&r
193042
MC/DC66%2/3
$esc = exists $replay->{"SubCond"} or substr($arg, 0, length $self->{"DirStart"}) eq $self->{"DirStart"} and $arg =~ /^$self->{"DirStartRE"}(\w+)\s*(.*)/
$esc = exists $replay->{"SubCond"} substr($arg, 0, length $self->{"DirStart"}) eq $self->{"DirStart"} $arg =~ /^$self->{"DirStartRE"}(\w+)\s*(.*)/
Truth table75%3/4
A or B and C
A= $esc = exists $replay->{"SubCond"}B= substr($arg, 0, length $self->{"DirStart"}) eq $self->{"DirStart"}C= $arg =~ /^$self->{"DirStartRE"}(\w+)\s*(.*)/
ABCresult
1XX1
0111
00X0
0100
697 (substr($arg, 0, length($self->{DirStart})) eq $self->{DirStart} and $arg =~ /^$self->{DirStartRE}(\w+)\s*(.*)/)) {
698  
699 # If buffered code (loop/sub) get the arg string and break to subroutine and its arguments:
700 286 ▶ ($arg = $replay->{SubCond}) =~ /^$self->{DirStartRE}(\w+)\s*(.*)/ if($esc);
Branch: 1 branch
BranchTrueFalseTotal
if $esc 244 42 286
701  
702 286 my ($sub, $args) = ($1, $2);
703  
704 286 my $intDir = 0; # If true: $sub is allowed internal directive
705 286 ▶ unless( exists $self->{subs}->{$sub}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"subs"}{$sub}) 70 216 286
706 return $self->SetError("Unknown sub '$sub' in $dir at $place", 1)
707 70 ▶ unless exists($condDirectives{$sub});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $condDirectives{$sub} 0 70 70
708 70 $intDir = 1;
709 }
710  
711 286 ▶ unless($self->{Frame}->{CondReRun}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'Frame'}{'CondReRun'}) { } 143 143 286
712 # First time through:
713 # Set up the sub, return to main loop to run it
714 143 $self->{Frame}->{CondReRun} = 10;
715  
716 # Cause the .if/.while/... to be run again.
717 # If buffered back up a line, else push back to input for this frame
718 143 ▶ if($esc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($esc) { } 122 21 143
719 122 $self->{Frame}->{CodeLine}--;
720 } else {
721 21 push @{$self->{Frame}->{PushedInput}}, "$self->{DirStart}$dir $arg";
722 }
723  
724 143 ▶ if($intDir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($intDir) { } 35 108 143
725 # Create a frame with just 1 line to run internal command
726 35 $self->openFrame(CodeLine => 0, Code => {Lines => [{ Txt => $arg, '#' => 1 }], LastLine => 0 } );
727 } else {
728 # Run the sub
729 108 $self->RunSub($sub, $args);
730 }
731  
732 143 $self->{Frame}->{CondReRun} = 1; # Cause return here
733 143 $self->{Frame}->{intDir} = $intDir; # Directive or sub ?
734 143 $self->{Frame}->{Generate} = 1; # Cause sub/directive to run
735  
736 143 return 0;
737  
738 } else {
739 # 2nd time:
740 # Get the command 'exit' code & tidy up:
741  
742 143 ▶ delete $self->{Frame}->{CondReRun} unless($esc);
Branch: 1 branch
BranchTrueFalseTotal
unless $esc 21 122 143
743 143 $true = $self->{Math}->{VarHash}->{_}->[-1];
744 $self->closeFrame # Close internal command frame
745 143 ▶ if($self->{Frame}->{intDir});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Frame"}{"intDir"} 0 143 143
746  
747 143 ▶ delete $self->{Frame}->{CondReRun} if($esc);
Branch: 1 branch
BranchTrueFalseTotal
if $esc 122 21 143
748 }
749  
750 } else {
751 # It is a conventional expression
752 193 ▶ if($replay and exists $replay->{Expr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replay and exists $replay->{'Expr'}) { } 158 35 193
Condition66%2/3
$replay and exists $replay->{"Expr"}
!ll&&!rl&&r
035158
MC/DC50%1/2
$replay and exists $replay->{"Expr"}
$replay exists $replay->{"Expr"}
Truth table66%2/3
A and B
A= $replayB= exists $replay->{"Expr"}
ABresult
0X0
100
111
753 158 $iftree = $replay->{Expr};
754 } else {
755 return $self->SetError("Bad $self->{DirStart}$dir expression $place in $self->{Frame}->{Name} '$arg'", 1)
756 35 ▶ unless($iftree = $self->{Math}->Parse($arg));
Branch: 1 branch
BranchTrueFalseTotal
unless $iftree = $self->{"Math"}->Parse($arg) 0 35 35
757 }
758  
759 193 $true = $self->{Math}->EvalToScalar($iftree);
760 }
761  
762 336 ▶ $true = ! $true if($replay->{Not});
Branch: 1 branch
BranchTrueFalseTotal
if $replay->{"Not"} 35 301 336
763  
764 336 $true
765 }
766  
767  
768  
769 # Return true on error
770 sub error
771 465 ▶{
Subroutine error: called CC=5 SCAR=17.5
Pod documented
772 465 my $self = shift;
773  
774 $self->{Error} or (defined($self->{Frame}) and $self->{Frame}->{Fd} and
775 (ref $self->{Frame}->{Fd} ne 'ARRAY') and $self->{Frame}->{Fd}->error)
776 465 ▶}
Branch: 1 branch
BranchTrueFalseTotal
$self->{"Error"} or defined $self->{"Frame"} and $self->{"Frame"}{"Fd"} and ref $self->{"Frame"}{"Fd"} ne "ARRAY" and $self->{"Frame"}{"Fd"}->error 0 465 465
Condition66%8/12
$self->{'Error'} || defined $self->{'Frame'} && $self->{'Frame'}{'Fd'} && ref $self->{'Frame'}{'Fd'} ne 'ARRAY' && $self->{'Frame'}{'Fd'}->error
l!l&&r!l&&!r
00465
defined $self->{"Frame"} and $self->{"Frame"}{"Fd"} and ref $self->{"Frame"}{"Fd"} ne "ARRAY" and $self->{"Frame"}{"Fd"}->error
!ll&&!rl&&r
3141510
defined $self->{"Frame"} and $self->{"Frame"}{"Fd"} and ref $self->{"Frame"}{"Fd"} ne "ARRAY"
!ll&&!rl&&r
3131151
defined $self->{"Frame"} and $self->{"Frame"}{"Fd"}
!ll&&!rl&&r
0313152
MC/DC0%0/5
$self->{'Error'} || defined $self->{'Frame'} && $self->{'Frame'}{'Fd'} && ref $self->{'Frame'}{'Fd'} ne 'ARRAY' && $self->{'Frame'}{'Fd'}->error
$self->{'Error'} defined $self->{"Frame"} $self->{"Frame"}{"Fd"} ref $self->{"Frame"}{"Fd"} ne "ARRAY" $self->{"Frame"}{"Fd"}->error
Truth table50%3/6
A || B and C and D and E
A= $self->{'Error'}B= defined $self->{"Frame"}C= $self->{"Frame"}{"Fd"}D= ref $self->{"Frame"}{"Fd"} ne "ARRAY"E= $self->{"Frame"}{"Fd"}->error
ABCDEresult
1XXXX1
011111
00XXX0
010XX0
0110X0
011100
777  
778 # As IO::Handle, clear recent error
779 sub clearerr
780 0 ▶{
Subroutine clearerr: not called CC=4 SCAR=30.0
Pod documented
781 0 my $self = shift;
782  
783 0 $self->{Error} = 0;
784 0 ▶ return 0 if(ref $self->{Frame}->{Fd} eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"Frame"}{"Fd"} eq "ARRAY" 0 0 0
785 0 ▶ return -1 unless $self->{Fd} && $self->{Fd}->opened;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Fd"} and $self->{"Fd"}->opened 0 0 0
Condition0%0/3
$self->{"Fd"} and $self->{"Fd"}->opened
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"Fd"} and $self->{"Fd"}->opened
$self->{"Fd"} $self->{"Fd"}->opened
Truth table0%0/3
A and B
A= $self->{"Fd"}B= $self->{"Fd"}->opened
ABresult
0X0
100
111
786  
787 $self->{Fd}->clearerr
788 0 }
789  
790 # Record the error at $errstr and maybe $!, note that there has been an error, return undef
791 # Arg is description of the error
792 # Optional extra arg. If true set $! to EINVAL - use this eg where file format error
793 sub SetError
794 0 ▶{
Subroutine SetError: not called CC=4 SCAR=25.6
Pod undocumented
795 0 my ($self, $errm, $einval) = @_;
796  
797 0 $self->{Error} = 1;
798 0 $errstr = $errm;
799  
800 0 ▶ die "$errm\n" if($self->{OnError} eq 'die' );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"OnError"} eq "die" 0 0 0
801 0 ▶ warn "$errm\n" if($self->{OnError} eq 'warn');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"OnError"} eq "warn" 0 0 0
802  
803 0 ▶ if($einval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($einval) 0 0 0
804 1 ▶ use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
805 0 $! = &Errno::EINVAL;
806 }
807  
808 return undef
809 0 }
810  
811 # Put line(s) to be read as input
812 0 ▶sub putline {
Subroutine putline: not called CC=1 SCAR=6.9
Pod documented
813 0 my $self = shift;
814  
815 0 push @{$self->{Frame}->{PushedInput}}, @_
816 }
817  
818 # Wrapper for _getline()
819 # The point is that we might be outputting to a stream rather than returning
820 # a line of input to the program.
821 1025 ▶sub getline {
Subroutine getline: called CC=2 SCAR=6.9
Pod documented
822 1025 my $self = shift;
823  
824 1025 while(1) {
825 1029 my $l = $self->_getline;
826  
827 1029 ▶ return $l if($self->{out} eq ""); # No stream set, return to caller
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"out"} eq "" 1025 4 1029
828  
829 4 $self->writeToStream($self->{out}, $l);
830 }
831 }
832  
833 # Write the argument line $l to the output $stream
834 # It can be an IO::FILE, array or subroutine
835 9 ▶sub writeToStream {
Subroutine writeToStream: called CC=6 SCAR=18.6
Pod undocumented
836 9 my ($self, $stream, $l) = @_;
837  
838 9 my $strm = $self->{OutStreams}->{$stream};
839  
840 9 ▶ if( !defined($strm)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $strm) 0 9 9
841 # This could result in many messages
842 0 $self->SetError("Output stream unknown: '$stream'");
843 0 return;
844 }
845 9 ▶ if(ref $strm eq 'IO::File' or ref $strm eq 'IO::Handle') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $strm eq "IO::File" or ref $strm eq "IO::Handle") 4 5 9
Condition66%2/3
ref $strm eq "IO::File" or ref $strm eq "IO::Handle"
l!l&&r!l&&!r
405
MC/DC50%1/2
ref $strm eq "IO::File" or ref $strm eq "IO::Handle"
ref $strm eq "IO::File" ref $strm eq "IO::Handle"
Truth table66%2/3
A or B
A= ref $strm eq "IO::File"B= ref $strm eq "IO::Handle"
ABresult
1X1
011
000
846 4 print $strm $l;
847 4 return;
848 }
849 5 ▶ if(ref $strm eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $strm eq "CODE") 1 4 5
850 1 &$strm($l);
851 1 return;
852 }
853 4 ▶ if(ref $strm eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $strm eq "ARRAY") 4 0 4
854 4 push @$strm, $l;
855 4 return;
856 }
857 # Should not get here
858 0 $self->SetError("At $self->{Place}: Output stream '$stream' is not an IO::File or IO::Handle or array or subroutine, but is: " . ref $strm);
859 }
860  
861 # Called when every line is read
862 # One problem with this is that it cannot store anything in a local variable
863 # as it returns once it finds a line that it cannot process itself.
864 # Store in the object.
865 # Can't store 'static' since there may be different files open for different purposes.
866 # getline() getlines() close() are deliberately compatible with IO::Handle. new() is not, it is more complicated.
867 1029 ▶sub _getline {
Subroutine _getline: called CC=163 SCAR=57.5
868 1029 my $self = shift;
869  
870 return $self->SetError("A file has not been opened at $self->{Place}", 1)
871 1029 ▶ unless defined $self->{Frame}->{Code} or defined $self->{Frame}->{Fd};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"Frame"}{"Code"} or defined $self->{"Frame"}{"Fd"} 0 1029 1029
Condition66%2/3
defined $self->{"Frame"}{"Code"} or defined $self->{"Frame"}{"Fd"}
l!l&&r!l&&!r
6034260
MC/DC0%0/2
defined $self->{"Frame"}{"Code"} or defined $self->{"Frame"}{"Fd"}
defined $self->{"Frame"}{"Code"} defined $self->{"Frame"}{"Fd"}
Truth table66%2/3
A or B
A= defined $self->{"Frame"}{"Code"}B= defined $self->{"Frame"}{"Fd"}
ABresult
1X1
011
000
872  
873 1029 my $doneDone = 0; # Last directive was .done
874 1029 my $vh = $self->{Math}->{VarHash};
875  
876 1029 while(1) {
877  
878 return undef
879 3088 ▶ if $self->{Error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Error"} 0 3088 3088
880  
881 return undef
882 3088 ▶ unless(@{$self->{FrameStk}});
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"FrameStk"};} 61 3027 3088
883  
884 3027 my $lineno;
885 3027 my $frame = $self->{Frame};
886 3027 my $replay;
887  
888 3027 ▶ if(defined ($_ = pop @{$frame->{PushedInput}})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined($_ = pop @{$$frame{'PushedInput'};})) { } 21 3006 3027
elsif (exists $frame->{'Code'}) { } 1717 1289 3006
889 # Line pushed back to input
890 21 $lineno = $frame->{LineNumber};
891 } elsif(exists $frame->{Code}) {
892 # Loop or sub
893 # End of code ?
894 1717 ▶ if($frame->{CodeLine} > $frame->{Code}->{LastLine}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->{"CodeLine"} > $frame->{"Code"}{"LastLine"}) 85 1632 1717
895 85 $self->closeFrame;
896 85 next;
897 }
898  
899 1632 $replay = $frame->{Code}{Lines}->[$frame->{CodeLine}++];
900 1632 $_ = $replay->{Txt};
901 1632 $lineno = $replay->{'#'};
902 } else {
903 # From file or in-memory array
904 1289 ▶ $_ = (ref $frame->{Fd} ne 'ARRAY') ? $frame->{Fd}->getline : shift @{$frame->{Fd}};
Branch: 1 branch
BranchTrueFalseTotal
ref $frame->{'Fd'} ne 'ARRAY' ? : 1286 3 1289
905  
906 # EOF:
907 1289 ▶ unless($_) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_) 81 1208 1289
908  
909 # EOF. Close the file. This may pop another one if there are multiple open files (.include)
910 return undef
911 81 ▶ unless($self->closeFrame);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->closeFrame 0 81 81
912  
913 next # There is still a frame to look at
914 81 ▶ if($self->{Frame});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Frame"} 81 0 81
915  
916 # EOF. Return undef
917 0 return undef;
918 }
919  
920 1208 ▶ if($self->{Raw}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Raw"}) 0 1208 1208
921 0 $vh->{_CountGen}->[0]++;
922 0 return $_;
923 }
924  
925 1208 $lineno = $.;
926 1208 chomp;
927 }
928  
929 # Store the line number in a silly number of places:
930 2861 $frame->{LineNumber} = $lineno;
931 2861 $vh->{'_LineNumber'} = [$lineno]; # do directly for speed
932 2861 $self->{Math}->Eval($self->{SetLineMath});
933  
934 # Something that knows where the current line is:
935 2861 my $place = "line $lineno of $frame->{Name}";
936 2861 $self->{Place} = $place;
937  
938 EVAL_RESTART: # Restart parsing here after a .eval
939  
940 # Ignore comments
941 2905 ▶ if(/^$self->{DirStartRE}#/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^$self->{"DirStartRE"}#/) 539 2366 2905
942 539 ▶ warn "$place: $_\n" if($self->{trace});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"trace"} 0 539 539
943 539 next;
944 }
945  
946 2366 ▶ s/\s*$// if($self->{Trim});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Trim"} 2366 0 2366
947  
948 # Quick return if it cannot be a directive line - or one that we recognise
949 # If not generating - skip to next
950 2366 ▶ unless(/^($self->{DirStartRE})(\w+)\s*(.*)/ and
Branch: 1 branch
BranchTrueFalseTotal
unless (/^($self->{"DirStartRE"})(\w+)\s*(.*)/ and defined $ctlDirectives{$2} || defined $self->{"subs"}{$2}) 628 1738 2366
Condition100%6/6
/^($self->{"DirStartRE"})(\w+)\s*(.*)/ and defined $ctlDirectives{$2} || defined $self->{"subs"}{$2}
!ll&&!rl&&r
62711738
defined $ctlDirectives{$2} or defined $self->{"subs"}{$2}
l!l&&r!l&&!r
1676621
MC/DC100%3/3
/^($self->{"DirStartRE"})(\w+)\s*(.*)/ and defined $ctlDirectives{$2} || defined $self->{"subs"}{$2}
/^($self->{"DirStartRE"})(\w+)\s*(.*)/ defined $ctlDirectives{$2} defined $self->{"subs"}{$2}
Truth table100%4/4
A and B or C
A= /^($self->{"DirStartRE"})(\w+)\s*(.*)/B= defined $ctlDirectives{$2}C= defined $self->{"subs"}{$2}
ABCresult
0XX0
1000
11X1
1011
951 (defined($ctlDirectives{$2}) or defined($self->{subs}->{$2}))) {
952 628 ▶ unless($frame->{Generate}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($frame->{"Generate"}) 61 567 628
953 61 $vh->{_CountSkip}->[0]++;
954 61 next;
955 }
956  
957 567 ▶ warn "$place: $_\n" if($self->{trace} > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"trace"} > 1 0 567 567
958  
959 567 $vh->{_CountGen}->[0]++;
960 567 return $_ . $/; # Put the line terminator back on
961 }
962  
963 # Must be a directive:
964 1738 my $leadin = $1; # String that identified the directive
965 1738 my $dir = $2; # Directive
966 1738 my $arg = $3; # Its argument
967  
968 1738 ▶ warn "$place: $_\n" if($self->{trace} and $frame->{Generate});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"trace"} and $frame->{"Generate"} 0 1738 1738
Condition33%1/3
$self->{"trace"} and $frame->{"Generate"}
!ll&&!rl&&r
173800
MC/DC0%0/2
$self->{"trace"} and $frame->{"Generate"}
$self->{"trace"} $frame->{"Generate"}
Truth table33%1/3
A and B
A= $self->{"trace"}B= $frame->{"Generate"}
ABresult
0X0
100
111
969  
970 1738 $vh->{_CountDirect}->[0]++;
971  
972 # Process .if/.else/.fi .unless
973 # Because we can have nested if/... we need a stack of how the conditions evaluated
974 1738 ▶ if($dir eq 'if' or $dir eq 'unless') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "if" or $dir eq "unless") 104 1634 1738
Condition100%3/3
$dir eq "if" or $dir eq "unless"
l!l&&r!l&&!r
86181634
MC/DC100%2/2
$dir eq "if" or $dir eq "unless"
$dir eq "if" $dir eq "unless"
Truth table100%3/3
A or B
A= $dir eq "if"B= $dir eq "unless"
ABresult
1X1
011
000
975 # start a new frame with .if
976 # Unless we are here a 2nd time as evaluating: .if .subroutine; in which case the frame is already open
977 104 ▶ $self->openFrame( Type => $dir, Else => 0, CpMove => 1) unless($frame->{CondReRun});
Branch: 1 branch
BranchTrueFalseTotal
unless $frame->{"CondReRun"} 79 25 104
978 104 $frame = $self->{Frame};
979  
980 104 $frame->{ParentGenerate} = $frame->{DidGenerate} = $frame->{Generate};
981  
982 # Don't evaluate the .if if we are not generating, the expression could have side effects
983 # Don't compile it either - faster; but means that we only see errors if we try
984 104 ▶ if($frame->{Generate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->{"Generate"}) 104 0 104
985 104 $replay->{Not} = $dir eq 'unless';
986  
987 104 my $gen = $self->EvalCond($replay, $dir, $place, $arg);
988 104 ▶ return $gen unless defined $gen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gen 0 104 104
989 104 $frame = $self->{Frame};
990 104 ▶ next if($frame->{CondReRun});
Branch: 1 branch
BranchTrueFalseTotal
if $frame->{"CondReRun"} 25 79 104
991 79 $frame->{DidGenerate} = $frame->{Generate} = $gen;
992 }
993  
994 79 next;
995 }
996 1634 ▶ if($dir eq 'elseif' or $dir eq 'elsif') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "elseif" or $dir eq "elsif") 29 1605 1634
Condition100%3/3
$dir eq "elseif" or $dir eq "elsif"
l!l&&r!l&&!r
2631605
MC/DC100%2/2
$dir eq "elseif" or $dir eq "elsif"
$dir eq "elseif" $dir eq "elsif"
Truth table100%3/3
A or B
A= $dir eq "elseif"B= $dir eq "elsif"
ABresult
1X1
011
000
997 return $self->SetError("${leadin}$dir but an ${leadin}if/${leadin}unless has not been seen, at $place", 1)
998 29 ▶ unless($frame->{Type} eq 'if' or $frame->{Type} eq 'unless');
Branch: 1 branch
BranchTrueFalseTotal
unless $frame->{"Type"} eq "if" or $frame->{"Type"} eq "unless" 0 29 29
Condition66%2/3
$frame->{"Type"} eq "if" or $frame->{"Type"} eq "unless"
l!l&&r!l&&!r
2810
MC/DC0%0/2
$frame->{"Type"} eq "if" or $frame->{"Type"} eq "unless"
$frame->{"Type"} eq "if" $frame->{"Type"} eq "unless"
Truth table66%2/3
A or B
A= $frame->{"Type"} eq "if"B= $frame->{"Type"} eq "unless"
ABresult
1X1
011
000
999 return $self->SetError("Cannot have ${leadin}$dir at $place to ${leadin}if after ${leadin}else at line $frame->{Else}", 1)
1000 29 ▶ if($frame->{Else});
Branch: 1 branch
BranchTrueFalseTotal
if $frame->{"Else"} 0 29 29
1001  
1002 # Don't record that we have seen it, related errors always refer to the .if
1003  
1004 # We do the test only if the .if was false - exactly the same as .else below
1005 # Do a test if the .if was false and all parents (enclosing .ifs) are true, set the truth of Generate property.
1006  
1007 29 ▶ if($frame->{ParentGenerate} and !$frame->{DidGenerate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->{'ParentGenerate'} and not $frame->{'DidGenerate'}) { } 27 2 29
Condition66%2/3
$frame->{"ParentGenerate"} and not $frame->{"DidGenerate"}
!ll&&!rl&&r
0227
MC/DC50%1/2
$frame->{"ParentGenerate"} and not $frame->{"DidGenerate"}
$frame->{"ParentGenerate"} not $frame->{"DidGenerate"}
Truth table66%2/3
A and not(B)
A= $frame->{"ParentGenerate"}B= not $frame->{"DidGenerate"}
ABresult
0X0
100
111
1008 27 my $gen = $self->EvalCond($replay, $dir, $place, $arg);
1009 27 $frame = $self->{Frame};
1010 27 ▶ return $gen unless defined $gen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gen 0 27 27
1011 27 $frame = $self->{Frame};
1012  
1013 27 ▶ next if($frame->{CondReRun});
Branch: 1 branch
BranchTrueFalseTotal
if $frame->{"CondReRun"} 12 15 27
1014 15 $frame->{DidGenerate} = $frame->{Generate} = $gen;
1015 } else {
1016 2 $frame->{Generate} = 0; # Which it might already be
1017 }
1018  
1019 17 next;
1020 }
1021 1605 ▶ if($dir eq 'else') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "else") 43 1562 1605
1022  
1023 return $self->SetError("${leadin}else but an ${leadin}if has not been seen, at $place", 1)
1024 43 ▶ unless($frame->{Type} eq 'if' or $frame->{Type} eq 'unless');
Branch: 1 branch
BranchTrueFalseTotal
unless $frame->{"Type"} eq "if" or $frame->{"Type"} eq "unless" 0 43 43
Condition66%2/3
$frame->{"Type"} eq "if" or $frame->{"Type"} eq "unless"
l!l&&r!l&&!r
33100
MC/DC0%0/2
$frame->{"Type"} eq "if" or $frame->{"Type"} eq "unless"
$frame->{"Type"} eq "if" $frame->{"Type"} eq "unless"
Truth table66%2/3
A or B
A= $frame->{"Type"} eq "if"B= $frame->{"Type"} eq "unless"
ABresult
1X1
011
000
1025 return $self->SetError("Another ${leadin}else at $place to ${leadin}if starting line $frame->{FrameStart}, first .else at line $frame->{Else}", 1)
1026 43 ▶ if($frame->{Else});
Branch: 1 branch
BranchTrueFalseTotal
if $frame->{"Else"} 0 43 43
1027  
1028 43 $frame->{Else} = $lineno; # Note where the .else was
1029  
1030 43 ▶ if($frame->{DidGenerate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->{'DidGenerate'}) { } 27 16 43
1031 27 $frame->{Generate} = 0;
1032 } else {
1033 16 $frame->{Generate} = $frame->{ParentGenerate};
1034 }
1035  
1036 43 next;
1037 }
1038 1562 ▶ if($dir eq 'fi') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "fi") 75 1487 1562
1039 return $self->SetError("${leadin}fi but an ${leadin}if has not been seen, $place", 1)
1040 75 ▶ unless($frame->{Type} eq 'if' or $frame->{Type} eq 'unless');
Branch: 1 branch
BranchTrueFalseTotal
unless $frame->{"Type"} eq "if" or $frame->{"Type"} eq "unless" 0 75 75
Condition66%2/3
$frame->{"Type"} eq "if" or $frame->{"Type"} eq "unless"
l!l&&r!l&&!r
65100
MC/DC0%0/2
$frame->{"Type"} eq "if" or $frame->{"Type"} eq "unless"
$frame->{"Type"} eq "if" $frame->{"Type"} eq "unless"
Truth table66%2/3
A or B
A= $frame->{"Type"} eq "if"B= $frame->{"Type"} eq "unless"
ABresult
1X1
011
000
1041  
1042 75 $self->closeFrame;
1043 75 next;
1044 }
1045  
1046 # None of the rest unless generating:
1047 1487 ▶ next unless $frame->{Generate};
Branch: 1 branch
BranchTrueFalseTotal
unless $frame->{"Generate"} 26 1461 1487
1048  
1049 1461 ▶ if($dir eq 'let') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "let") 151 1310 1461
1050 151 my $iftree;
1051 151 ▶ if($replay and exists $replay->{Expr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replay and exists $replay->{'Expr'}) { } 87 64 151
Condition66%2/3
$replay and exists $replay->{"Expr"}
!ll&&!rl&&r
64087
MC/DC50%1/2
$replay and exists $replay->{"Expr"}
$replay exists $replay->{"Expr"}
Truth table66%2/3
A and B
A= $replayB= exists $replay->{"Expr"}
ABresult
0X0
100
111
1052 87 $iftree = $replay->{Expr};
1053 } else {
1054 return $self->SetError("Bad ${leadin}let expression $place '$arg'", 1)
1055 64 ▶ unless($iftree = $self->{Math}->Parse($arg));
Branch: 1 branch
BranchTrueFalseTotal
unless $iftree = $self->{"Math"}->Parse($arg) 0 64 64
1056 }
1057 151 $self->{Math}->EvalToScalar($iftree);
1058 # Don't care what the result is
1059  
1060 151 next;
1061 }
1062  
1063 # Return a line parsed for escapes
1064 1310 ▶ if($dir eq 'echo') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "echo") 401 909 1310
1065 401 $vh->{_CountGen}->[0]++;
1066 401 return $self->ProcEscapes($arg) . $/;
1067 }
1068  
1069 # Start of loop
1070 909 ▶ if(exists $loops{$dir}) { # 'while' 'until' 'for'
Branch: 1 branch
BranchTrueFalseTotal
if (exists $loops{$dir}) 348 561 909
1071 # Create a new frame with an indicator that this is a loop frame
1072 # With 'for' execute the initialisation expression and record the loop expression
1073 # For/while/until all look the same (until has a truth invert flag)
1074 # On 'EOF' of the recorded array, detect that it is a loop frame:
1075 # - execute any loop expression
1076 # - evaluate the loop condition; closeFrame on false; reset CodeLine on true
1077  
1078 348 my $code;
1079 348 my @args = $self->SplitArgs($arg, 1);
1080 348 my $oframe = $frame;
1081  
1082 # First time:
1083 348 ▶ unless($doneDone or $frame->{CondReRun}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($doneDone or $frame->{"CondReRun"}) 90 258 348
Condition100%3/3
$doneDone or $frame->{"CondReRun"}
l!l&&r!l&&!r
15210690
MC/DC100%2/2
$doneDone or $frame->{"CondReRun"}
$doneDone $frame->{"CondReRun"}
Truth table100%3/3
A or B
A= $doneDoneB= $frame->{"CondReRun"}
ABresult
1X1
011
000
1084 90 $self->openFrame(Block => $dir);
1085 90 $frame = $self->{Frame};
1086 }
1087  
1088 # If reading from a stream grab the loop to an array:
1089 348 ▶ unless(exists $frame->{Code}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $frame->{"Code"}) 17 331 348
1090 17 ▶ return $code unless($code = $self->ReadLoop($dir, $_, $arg));
Branch: 1 branch
BranchTrueFalseTotal
unless $code = $self->ReadLoop($dir, $_, $arg) 0 17 17
1091 17 $frame->{Code} = $code;
1092 17 $frame->{CodeLine} = $code->{FirstLine} + 1;
1093 17 delete $frame->{Fd};
1094 }
1095  
1096 # New loop, initialise it:
1097 348 ▶ unless($doneDone or $frame->{CondReRun}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($doneDone or $frame->{"CondReRun"}) 90 258 348
Condition100%3/3
$doneDone or $frame->{"CondReRun"}
l!l&&r!l&&!r
15210690
MC/DC100%2/2
$doneDone or $frame->{"CondReRun"}
$doneDone $frame->{"CondReRun"}
Truth table100%3/3
A or B
A= $doneDoneB= $frame->{"CondReRun"}
ABresult
1X1
011
000
1098 90 $replay = $frame->{Code}{Lines}->[$frame->{CodeLine} - 1];
1099  
1100 90 $frame->{LoopMax} = $replay->{LoopMax};
1101 90 $frame->{LoopCnt} = 0;
1102 90 $frame->{LoopStart} = $replay->{LoopStart};
1103 90 $frame->{LoopEnd} = $replay->{LoopEnd};
1104  
1105 # Set CodeLine to Line after end - in parent frame (which might be from stream and ignore it)
1106 90 $oframe->{CodeLine} = $frame->{LoopEnd};
1107  
1108 # Evaluate any loop initialisation
1109 90 ▶ $self->{Math}->ParseToScalar($replay->{Init}) if(exists $replay->{Init});
Branch: 1 branch
BranchTrueFalseTotal
if exists $replay->{"Init"} 15 75 90
1110 }
1111 348 $doneDone = 0;
1112  
1113 # Beware: might be here twice
1114 348 ▶ unless($frame->{CondReRun}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($frame->{"CondReRun"}) 242 106 348
1115 # Trap run away loops:
1116 return $self->SetError("Maximum iterations ($frame->{LoopMax}) exceeded at $frame->{FrameStart}", 1)
1117 242 ▶ if($frame->{LoopMax} && ++$frame->{LoopCnt} > $frame->{LoopMax});
Branch: 1 branch
BranchTrueFalseTotal
if $frame->{"LoopMax"} and ++$frame->{"LoopCnt"} > $frame->{"LoopMax"} 0 242 242
Condition66%2/3
$frame->{"LoopMax"} and ++$frame->{"LoopCnt"} > $frame->{"LoopMax"}
!ll&&!rl&&r
102320
MC/DC0%0/2
$frame->{"LoopMax"} and ++$frame->{"LoopCnt"} > $frame->{"LoopMax"}
$frame->{"LoopMax"} ++$frame->{"LoopCnt"} > $frame->{"LoopMax"}
Truth table66%2/3
A and B
A= $frame->{"LoopMax"}B= ++$frame->{"LoopCnt"} > $frame->{"LoopMax"}
ABresult
0X0
100
111
1118  
1119 # evaluation loop expression (not on first time)
1120 242 ▶ $self->{Math}->EvalToScalar($replay->{For3}) if(exists $replay->{For3} and $frame->{LoopCnt} != 1);
Branch: 1 branch
BranchTrueFalseTotal
if exists $replay->{"For3"} and $frame->{"LoopCnt"} != 1 86 156 242
Condition100%3/3
exists $replay->{"For3"} and $frame->{"LoopCnt"} != 1
!ll&&!rl&&r
1421486
MC/DC100%2/2
exists $replay->{"For3"} and $frame->{"LoopCnt"} != 1
exists $replay->{"For3"} $frame->{"LoopCnt"} != 1
Truth table100%3/3
A and B
A= exists $replay->{"For3"}B= $frame->{"LoopCnt"} != 1
ABresult
0X0
100
111
1121 }
1122  
1123 # Evaluate the loop condition - if true keep looping
1124 348 my $bool = $self->EvalCond($replay, $dir, $place, $arg);
1125 348 ▶ next if($frame->{CondReRun});
Branch: 1 branch
BranchTrueFalseTotal
if $frame->{"CondReRun"} 106 242 348
1126 242 ▶ $self->closeFrame if( !$bool);
Branch: 1 branch
BranchTrueFalseTotal
unless $bool 52 190 242
1127  
1128 242 next;
1129 }
1130  
1131 # Should only be seen at end of loop - which is buffered
1132 561 ▶ if($dir eq 'done') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "done") 122 439 561
1133 return $self->SetError("Unexpected '$leadin$dir' at $place", 1)
1134 122 ▶ unless(exists $frame->{LoopMax});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $frame->{"LoopMax"} 0 122 122
1135  
1136 # Next to run is loop start:
1137 122 $frame->{CodeLine} = $frame->{LoopStart};
1138 122 $doneDone = 1;
1139 122 next;
1140 }
1141  
1142 439 ▶ if($dir eq 'break' or $dir eq 'last') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "break" or $dir eq "last") 27 412 439
Condition66%2/3
$dir eq "break" or $dir eq "last"
l!l&&r!l&&!r
270412
MC/DC50%1/2
$dir eq "break" or $dir eq "last"
$dir eq "break" $dir eq "last"
Truth table66%2/3
A or B
A= $dir eq "break"B= $dir eq "last"
ABresult
1X1
011
000
1143 27 my $loops = 1;
1144 27 ▶ $loops = $1 if($arg =~ /\s*(\d+)/);
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /\s*(\d+)/ 1 26 27
1145  
1146 # Unwind until we find a LoopEnd, then close that frame
1147 27 my $le;
1148 27 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$le and --$loops <= 0 or do { do { return $self->SetError("'$leadin${dir}' too many loops at $place", 1) if exists $self->{"Frame"}{"ReturnFrom"}; $le = exists $self->{"Frame"}{"LoopEnd"}; return undef unless $self->closeFrame } } 27 1 28
Condition66%2/3
$le and --$loops <= 0
!ll&&!rl&&r
0127
MC/DC50%1/2
$le and --$loops <= 0
$le --$loops <= 0
Truth table66%2/3
A and B
A= $leB= --$loops <= 0
ABresult
0X0
100
111
1149 # Can't break out of sub:
1150 return $self->SetError("'$leadin$dir' too many loops at $place", 1)
1151 28 ▶ if(exists $self->{Frame}->{ReturnFrom});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"Frame"}{"ReturnFrom"} 0 28 28
1152  
1153 28 $le = exists $self->{Frame}->{LoopEnd};
1154 return undef
1155 28 ▶ unless($self->closeFrame);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->closeFrame 0 28 28
1156 } until($le and --$loops <= 0);
1157 27 next;
1158 }
1159  
1160 412 ▶ if($dir eq 'continue' or $dir eq 'next') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "continue" or $dir eq "next") 30 382 412
Condition66%2/3
$dir eq "continue" or $dir eq "next"
l!l&&r!l&&!r
300382
MC/DC50%1/2
$dir eq "continue" or $dir eq "next"
$dir eq "continue" $dir eq "next"
Truth table66%2/3
A or B
A= $dir eq "continue"B= $dir eq "next"
ABresult
1X1
011
000
1161 30 my $loops = 1;
1162 30 ▶ $loops = $1 if($arg =~ /\s*(\d+)/);
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /\s*(\d+)/ 10 20 30
1163  
1164 # Unwind until we find LoopStart, reset to that
1165 30 my $ls;
1166 30 while(1) {
1167 return $self->SetError("'$leadin$dir' too many loops at $place", 1)
1168 40 ▶ if(exists $self->{Frame}->{ReturnFrom});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"Frame"}{"ReturnFrom"} 0 40 40
1169  
1170 40 ▶ if(($ls = exists $self->{Frame}->{LoopStart}) && --$loops <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ls = exists $self->{"Frame"}{"LoopStart"} and --$loops <= 0) 30 10 40
Condition66%2/3
$ls = exists $self->{"Frame"}{"LoopStart"} and --$loops <= 0
!ll&&!rl&&r
01030
MC/DC50%1/2
$ls = exists $self->{"Frame"}{"LoopStart"} and --$loops <= 0
$ls = exists $self->{"Frame"}{"LoopStart"} --$loops <= 0
Truth table66%2/3
A and B
A= $ls = exists $self->{"Frame"}{"LoopStart"}B= --$loops <= 0
ABresult
0X0
100
111
1171 30 $self->{Frame}->{CodeLine} = $self->{Frame}->{LoopStart};
1172 30 last;
1173 }
1174  
1175 return undef
1176 10 ▶ unless($self->closeFrame);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->closeFrame 0 10 10
1177 };
1178  
1179 30 $doneDone = 1; # This is like .done
1180 30 next;
1181 }
1182  
1183 # Local variable
1184 382 ▶ if($dir eq 'local') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "local") 16 366 382
1185 # Push to previous var hash for this stack frame
1186 # This will be undone by closeFrame()
1187 16 foreach my $vname (split ' ', $arg) {
1188 28 my $vval = $vh->{$vname};
1189 28 $frame->{LocalVars}->{$vname} = $vval;
1190 28 delete($vh->{$vname});
1191 }
1192 16 next;
1193 }
1194  
1195 # Include another file
1196 366 ▶ if($dir eq 'include') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "include") 43 323 366
1197 43 my (@push, @args, $stream, $fd);
1198 43 my $level = 0;
1199  
1200 43 ▶ if($arg =~ s/^-s\s*(\w+)\s*//i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ s/^-s\s*(\w+)\s*//i) 8 35 43
1201 8 $stream = $1;
1202 return $self->SetError("Stream '$stream' already open at $place")
1203 8 ▶ if(exists($self->{Streams}->{$stream}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"Streams"}{$stream} 0 8 8
1204 }
1205  
1206 43 ▶ return undef unless(@args = $self->SplitArgs($arg, 1));
Branch: 1 branch
BranchTrueFalseTotal
unless @args = $self->SplitArgs($arg, 1) 0 43 43
1207 43 ▶ return $self->SetError("Missing include file at $place") unless(@args);
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 43 43
1208 43 my $fn = shift @args;
1209  
1210 # Push the include ?
1211 43 ▶ if(!defined($stream) and $fn =~ /^-p(\d*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $stream and $fn =~ /^-p(\d*)$/) 2 41 43
Condition100%3/3
not defined $stream and $fn =~ /^-p(\d*)$/
!ll&&!rl&&r
8332
MC/DC100%2/2
not defined $stream and $fn =~ /^-p(\d*)$/
not defined $stream $fn =~ /^-p(\d*)$/
Truth table100%3/3
not(A) and B
A= not defined $streamB= $fn =~ /^-p(\d*)$/
ABresult
0X0
100
111
1212 2 ▶ $level = $1 eq '' ? 1 : $1; # Default 1
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '' ? : 1 1 2
1213  
1214 0 return $self->SetError("Attempt to push too far (" . (scalar @{$self->{FrameStk}}) . " available) at $place")
1215 2 ▶ if($level > @{$self->{FrameStk}});
Branch: 1 branch
BranchTrueFalseTotal
if $level > @{$self->{"FrameStk"};} 0 2 2
1216 2 ▶ return $self->SetError("Missing include file at $place") unless(@args);
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 2 2
1217 2 $fn = shift @args;
1218 }
1219  
1220 # Opening a pipe to read from ?
1221 43 ▶ if(substr($fn, 0, 1) eq '|') {
Branch: 2 branches
BranchTrueFalseTotal
if (substr($fn, 0, 1) eq '|') { } 12 31 43
elsif ($fn =~ /^(\@\w+)$/i) { } 1 30 31
1222 return $self->SetError("Not allowed to open pipe at $place")
1223 12 ▶ unless($self->{PipeOK});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"PipeOK"} 0 12 12
1224  
1225 # Replace the command if written '|cmd'
1226 12 ▶ $fn = $fn eq '|' ? shift(@args) : substr($fn, 1);
Branch: 1 branch
BranchTrueFalseTotal
$fn eq '|' ? : 6 6 12
1227  
1228 12 $fd = IO::Pipe->new;
1229 12 ▶ return $self->SetError("Open of pipe '$fn' failed: $! at $place")
Branch: 1 branch
BranchTrueFalseTotal
unless $fd 0 12 12
1230 unless($fd);
1231  
1232 12 $fd->reader($fn, @args);
1233  
1234 12 $fn = "| $fn"; # For messages, etc, only
1235  
1236 12 $vh->{_CountOpen}->[0]++;
1237 } elsif($fn =~ /^(@\w+)$/i) {
1238 return $self->SetError("Unknown in-memory stream '$1' at $place")
1239 1 ▶ unless(defined($self->{OutStreams}->{$1}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"OutStreams"}{$1} 0 1 1
1240 1 $fd = $self->{OutStreams}->{$1};
1241 } else {
1242 30 ▶ return undef unless(defined($fn = $self->ResolveFilename($fn)));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($fn = $self->ResolveFilename($fn)) 0 30 30
1243 30 ▶ return $self->SetError("Cannot open file '$arg' at $place as $!")
Branch: 1 branch
BranchTrueFalseTotal
unless $fd = $self->openFile($fn) 0 30 30
1244 unless($fd = $self->openFile($fn));
1245 }
1246  
1247 # Either store on a named stream or push to a frame
1248 43 ▶ if(defined($stream)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stream) { } 8 35 43
1249 8 $self->{Streams}->{$stream} = $fd;
1250 } else {
1251 35 $self->openFrame(Name => $fn, Fd => $fd, Args => [@args], Generate => 1, LineNumber => 0, ReturnFrom => 1);
1252 35 ▶ delete $self->{Frame}->{Code} if( !$level); # Back to input from file unless pushed elsewhere
Branch: 1 branch
BranchTrueFalseTotal
unless $level 33 2 35
1253 35 ▶ if($level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level) 2 33 35
1254 # Insert opened stream/frame down in the stackframes:
1255 2 my $str = pop @{$self->{FrameStk}};
1256 2 splice @{$self->{FrameStk}}, -$level, 0, $str;
1257 }
1258 }
1259  
1260 43 next;
1261 }
1262  
1263 # Kill the script with optional exit code
1264 323 ▶ if($dir eq 'exit') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "exit") 0 323 323
1265 0 my $code = 2;
1266 0 ▶ if($arg ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg ne "") 0 0 0
1267 0 $code = $self->{Math}->ParseToScalar($arg);
1268 0 ▶ unless($code =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code =~ /^\d+$/) 0 0 0
1269 0 print "Exit expression at $place was not numeric: $code\n";
1270 0 $code = 2;
1271 }
1272 }
1273 0 exit $code;
1274 }
1275  
1276 # Print a line, -e print to stderr, -o stream write to named stream
1277 # Line parsed for escapes
1278 323 ▶ if($dir eq 'print') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "print") 5 318 323
1279 5 my $stream = 'STDOUT';
1280 5 ▶ $stream = 'STDERR' if($arg =~ s/^-e\b\s*//);
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ s/^-e\b\s*// 0 5 5
1281 5 ▶ $stream = $1 if($arg =~ s/^-o\s*(@?\w+)\b\s*//i);
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ s/^-o\s*(\@?\w+)\b\s*//i 5 0 5
1282  
1283 5 ▶ return undef unless(defined($arg = $self->ProcEscapes($arg)));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($arg = $self->ProcEscapes($arg)) 0 5 5
1284 5 $self->writeToStream($stream, "$arg\n");
1285 5 next;
1286 }
1287  
1288 # Set the current output stream
1289 318 ▶ if($dir eq 'out') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "out") 7 311 318
1290 7 ▶ if($arg eq "") {
Branch: 3 branches
BranchTrueFalseTotal
if ($arg eq '') { } 3 4 7
elsif ($arg =~ s/^(-c)?\s*(\@\w+)\s*$//i) { } 2 2 4
elsif ($arg =~ s/^(\w+)\s*$//i) { } 2 0 2
1291 3 $self->{out} = "";
1292 } elsif($arg =~ s/^(-c)?\s*(@\w+)\s*$//i) {
1293 # Create the memory stream if -c option
1294 2 ▶ $self->{OutStreams}->{$2} = [] if(defined $1);
Branch: 1 branch
BranchTrueFalseTotal
if defined $1 2 0 2
1295  
1296 $self->SetError("Output in-memory stream unknown: '$2' at $place")
1297 2 ▶ unless(defined $self->{OutStreams}->{$2});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"OutStreams"}{$2} 0 2 2
1298 2 $self->{out} = $2;
1299 } elsif($arg =~ s/^(\w+)\s*$//i) {
1300 $self->SetError("Unknown output stream '$1' at $place")
1301 2 ▶ unless(defined($self->{OutStreams}->{$1}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"OutStreams"}{$1} 0 2 2
1302 2 $self->{out} = $1;
1303 } else {
1304 0 return $self->SetError("Bad or missing stream name '$arg' at $place");
1305 }
1306 7 next;
1307 }
1308  
1309 # Close this file, return to the one that .included it - if any
1310 # This may result in EOF. Check at loop top
1311 311 ▶ if($dir eq 'return') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "return") 130 181 311
1312 # Evaluate expression after .return - in context of the .sub
1313 130 my $ret = undef;
1314 130 ▶ $ret = $self->{Math}->ParseToScalar($arg) if($arg =~ /\S/);
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /\S/ 127 3 130
1315 130 $vh->{_} = [$ret];
1316 return undef
1317 130 ▶ unless($self->close);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->close 0 130 130
1318  
1319 130 next;
1320 }
1321  
1322 # Eval: rewrite the line and try again
1323 181 ▶ if($dir eq 'eval') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "eval") 44 137 181
1324 44 ▶ return undef unless($_ = $self->ProcEscapes($arg));
Branch: 1 branch
BranchTrueFalseTotal
unless $_ = $self->ProcEscapes($arg) 0 44 44
1325 44 ▶ next if(/^$self->{DirStartRE}#/);
Branch: 1 branch
BranchTrueFalseTotal
if /^$self->{"DirStartRE"}#/ 0 44 44
1326 44 $place = "Evaled: $place";
1327 44 goto EVAL_RESTART;
1328 }
1329  
1330 # Close a named stream
1331 137 ▶ if($dir eq 'close') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "close") 8 129 137
1332 8 ▶ return $self->SetError("Missing option '-n stream' to ${leadin}close at $place", 1)
Branch: 1 branch
BranchTrueFalseTotal
unless $arg =~ s/^-s\s*(\w+)\s*//i 0 8 8
1333 unless($arg =~ s/^-s\s*(\w+)\s*//i);
1334  
1335 8 my $stream = $1;
1336  
1337 return $self->SetError("Unknown input stream '$stream' in ${leadin}read at $place", 1)
1338 8 ▶ unless(exists($self->{Streams}->{$stream}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"Streams"}{$stream} 0 8 8
1339  
1340 8 delete($self->{Streams}->{$stream}); # Close it
1341  
1342 8 next;
1343 }
1344  
1345 # Read next line into var
1346 129 ▶ if($dir eq 'read') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "read") 29 100 129
1347 29 my ($stream, $fd, $mem);
1348  
1349 29 ▶ $stream = $1 if($arg =~ s/^-s\s*(\w+)\s+//i);
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ s/^-s\s*(\w+)\s+//i 23 6 29
1350  
1351 # In-memory stream:
1352 29 ▶ if($arg =~ s/^(@\w+)\s+//i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ s/^(\@\w+)\s+//i) 1 28 29
1353 return $self->SetError("Unknown in-memory stream $1 at $place")
1354 1 ▶ unless(defined $self->{OutStreams}->{$1});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"OutStreams"}{$1} 0 1 1
1355 1 $mem = $1;
1356 }
1357  
1358 29 my ($vname) = $arg =~ /^(\w+)/i;
1359 29 ▶ return $self->SetError("Missing argument to ${leadin}read at $place", 1) unless($vname);
Branch: 1 branch
BranchTrueFalseTotal
unless $vname 0 29 29
1360  
1361 # Find stream or Fd on stack:
1362 29 ▶ if(defined($stream)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stream) { } 23 6 29
1363 return $self->SetError("Unknown input stream '$stream' in ${leadin}read at $place", 1)
1364 23 ▶ unless($fd = $self->{Streams}->{$stream});
Branch: 1 branch
BranchTrueFalseTotal
unless $fd = $self->{"Streams"}{$stream} 0 23 23
1365 } else {
1366 # Find an open file
1367 6 my $f = $frame;
1368 6 my $i = @{$self->{FrameStk}} - 1;
1369 6 ▶ until(exists($f->{Fd})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $f->{"Fd"}) 3 6 9
1370 3 $f = $self->{FrameStk}->[--$i];
1371 }
1372 6 $fd = $f->{Fd};
1373 }
1374  
1375 29 my $eof = 1;
1376 29 ▶ if($_ = (defined $mem) ? shift @{$self->{OutStreams}->{$mem}} : $fd->getline) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ = defined $mem ? shift @{$$self{'OutStreams'}{$mem};} : $fd->getline) { } 22 7 29
defined $mem ? : 1 28 29
1377 22 chomp;
1378 22 $eof = 0;
1379 22 ▶ s/\s*$// if($self->{Trim});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Trim"} 22 0 22
1380 } else {
1381 7 $_ = '';
1382 }
1383  
1384 29 $vh->{$vname} = [$_];
1385 29 $vh->{'_EOF'} = [0 + $eof];
1386 29 $vh->{'_'} = [1 - $eof];
1387 29 next;
1388 }
1389  
1390 # No operation
1391 100 ▶ next if($dir eq 'noop');
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq "noop" 1 99 100
1392  
1393 # Subroutine definition
1394 99 ▶ if($dir eq 'sub') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "sub") 38 61 99
1395 return undef
1396 38 ▶ unless($self->readSub($dir, $_, $arg));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->readSub($dir, $_, $arg) 0 38 38
1397 38 next;
1398 }
1399  
1400 61 ▶ if($dir eq 'test') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "test") 4 57 61
1401 4 my %an = ('-f' => 2, '-m' => 2);
1402 4 my @args = $self->SplitArgs($arg, 1);
1403 return $self->SetError("'$leadin$dir' bad or missing argument '$arg' at $place", 1)
1404 4 ▶ unless(@args and exists($an{$args[0]}) and @args == $an{$args[0]});
Branch: 1 branch
BranchTrueFalseTotal
unless @args and exists $an{$args[0]} and @args == $an{$args[0]} 0 4 4
Condition33%2/6
@args and exists $an{$args[0]} and @args == $an{$args[0]}
!ll&&!rl&&r
004
@args and exists $an{$args[0]}
!ll&&!rl&&r
004
MC/DC0%0/3
@args and exists $an{$args[0]} and @args == $an{$args[0]}
@args exists $an{$args[0]} @args == $an{$args[0]}
Truth table25%1/4
A and B and C
A= @argsB= exists $an{$args[0]}C= @args == $an{$args[0]}
ABCresult
0XX0
10X0
1100
1111
1405  
1406 4 ▶ if($args[0] eq '-f') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0] eq "-f") 2 2 4
1407 2 my ($fn, @stat);
1408 2 $vh->{_} = [0]; # assume error
1409 2 ▶ if(($fn = $self->ResolveFilename($args[1], 1)) and (@stat = stat $fn)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fn = $self->ResolveFilename($args[1], 1) and @stat = stat $fn) 2 0 2
Condition33%1/3
$fn = $self->ResolveFilename($args[1], 1) and @stat = stat $fn
!ll&&!rl&&r
002
MC/DC0%0/2
$fn = $self->ResolveFilename($args[1], 1) and @stat = stat $fn
$fn = $self->ResolveFilename($args[1], 1) @stat = stat $fn
Truth table33%1/3
A and B
A= $fn = $self->ResolveFilename($args[1], 1)B= @stat = stat $fn
ABresult
0X0
100
111
1410 2 $vh->{_} = [1]; # OK
1411 2 $vh->{_STAT} = [@stat];
1412 2 $vh->{_TestFile} = [$fn];
1413 }
1414 2 next;
1415 }
1416  
1417 2 ▶ if($args[0] eq '-m') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0] eq "-m") 2 0 2
1418 2 $vh->{_} = [0]; # assume error
1419 2 ▶ if(defined($self->{OutStreams}->{$args[1]})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"OutStreams"}{$args[1]}) 1 1 2
1420 1 $vh->{_} = [1]; # OK
1421 1 $vh->{_COUNT} = [scalar @{$self->{OutStreams}->{$args[1]}}];
1422 }
1423 2 next;
1424 }
1425 }
1426  
1427 57 ▶ if($dir eq 'error') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "error") 0 57 57
1428 0 ▶ $arg = "Error at $place" if($arg eq '');
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "" 0 0 0
1429 0 return $self->SetError($arg);
1430 }
1431  
1432 57 ▶ if($dir eq 'set') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "set") 0 57 57
1433 return $self->SetError("'$leadin$dir' bad argument '$arg' at $place")
1434 0 ▶ unless(($arg =~ /^(\w+)=(\d+)/i) and $options{$1});
Branch: 1 branch
BranchTrueFalseTotal
unless $arg =~ /^(\w+)=(\d+)/i and $options{$1} 0 0 0
Condition0%0/3
$arg =~ /^(\w+)=(\d+)/i and $options{$1}
!ll&&!rl&&r
000
MC/DC0%0/2
$arg =~ /^(\w+)=(\d+)/i and $options{$1}
$arg =~ /^(\w+)=(\d+)/i $options{$1}
Truth table0%0/3
A and B
A= $arg =~ /^(\w+)=(\d+)/iB= $options{$1}
ABresult
0X0
100
111
1435 0 $self->{$1} = $2;
1436 0 next;
1437 }
1438  
1439 57 ▶ return $self->SetError("Use of reserved directive '$leadin$dir' at $place", 1)
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq "function" or $dir eq "do" or $dir eq "case" or $dir eq "switch" or $dir eq "endswitch" 0 57 57
Condition33%4/12
$dir eq "function" or $dir eq "do" or $dir eq "case" or $dir eq "switch" or $dir eq "endswitch"
l!l&&r!l&&!r
0057
$dir eq "function" or $dir eq "do" or $dir eq "case" or $dir eq "switch"
l!l&&r!l&&!r
0057
$dir eq "function" or $dir eq "do" or $dir eq "case"
l!l&&r!l&&!r
0057
$dir eq "function" or $dir eq "do"
l!l&&r!l&&!r
0057
MC/DC0%0/5
$dir eq "function" or $dir eq "do" or $dir eq "case" or $dir eq "switch" or $dir eq "endswitch"
$dir eq "function" $dir eq "do" $dir eq "case" $dir eq "switch" $dir eq "endswitch"
Truth table16%1/6
A or B or C or D or E
A= $dir eq "function"B= $dir eq "do"C= $dir eq "case"D= $dir eq "switch"E= $dir eq "endswitch"
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
1440 if($dir eq 'function' or $dir eq 'do' or $dir eq 'case' or $dir eq 'switch' or $dir eq 'endswitch');
1441  
1442 # User defined sub.
1443 # At the bottom so cannot redefine an inbuilt directive
1444 57 ▶ if(exists($self->{subs}->{$dir})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"subs"}{$dir}) 57 0 57
1445 57 $self->RunSub($dir, $arg);
1446  
1447 57 next;
1448 }
1449  
1450 # Should not happen
1451 0 return $self->SetError("Unknown directive '$leadin$dir' at $place", 1);
1452 }
1453 }
1454  
1455 # Return the rest of input as an array
1456 sub getlines
1457 0 ▶{
Subroutine getlines: not called CC=4 SCAR=30.0
Pod documented
1458 0 my $self = shift;
1459 0 my @lines = ();
1460  
1461 return $self->SetError("A file has not been opened", 1)
1462 0 ▶ unless $self->{Fd};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Fd"} 0 0 0
1463  
1464 0 ▶ return $self->SetError("getlines called in a scalar context", 1)
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
1465 unless(wantarray);
1466  
1467 0 ▶ while(my $line = $self->getline) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->getline) 0 0 0
1468 0 push @lines, $line;
1469 }
1470  
1471 @lines
1472 0 }
1473  
1474 # Enable the object to be used in the diamond operator:
1475 1 ▶use overload '<>' => \&getline, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
1476  
1477 1 1;
1478  
1479 __END__
↑ Summary