blib/lib/IO/ReadPreProcess.pm

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