blib/script/psed

filter: Statementstmt 73.8% 506 / 685 Branchbran 68.1% 285 / 418 Conditioncond 56.3% 49 / 87 MC/DCmcdc 20.3% 11 / 54 Subroutinesub 94.1% 32 / 34 Podpod n/a totaltotal 69.0% 883 / 1278 CC 208 SCAR 51.6
CC
208
Cov
71%
Parse
391.1
bre2p
203.7
sub2p
12.5
CRAP 173.6
↑ Summary
1 #!/usr/local/bin/perl
2  
3 # (p)sed - a stream editor
4 # History: Aug 12 2000: Original version.
5 # Mar 25 2002: Rearrange generated Perl program.
6 # Jul 23 2007: Fix bug in regex stripping (M.Thorland)
7  
8 package App::s2p;
9 66 $App::s2p::VERSION = '1.004';
10 66 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 66 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 66 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 66 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 66 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 66 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 66 $0 =~ s/^.*?(\w+)[\.\w]*$/$1/;
18  
19 =head1 NAME
20  
21 psed - a stream editor
22  
23 =head1 SYNOPSIS
24  
25 psed [-an] script [file ...]
26 psed [-an] [-e script] [-f script-file] [file ...]
27  
28 s2p [-an] [-e script] [-f script-file]
29  
30 =head1 DESCRIPTION
31  
32 A stream editor reads the input stream consisting of the specified files
33 (or standard input, if none are given), processes is line by line by
34 applying a script consisting of edit commands, and writes resulting lines
35 to standard output. The filename 'C<->' may be used to read standard input.
36  
37 The edit script is composed from arguments of B<-e> options and
38 script-files, in the given order. A single script argument may be specified
39 as the first parameter.
40  
41 If this program is invoked with the name F<s2p>, it will act as a
42 sed-to-Perl translator. See L<"SED SCRIPT TRANSLATION">.
43  
44 B<sed> returns an exit code of 0 on success or >0 if an error occurred.
45  
46 =head1 OPTIONS
47  
48 =over 4
49  
50 =item B<-a>
51  
52 A file specified as argument to the B<w> edit command is by default
53 opened before input processing starts. Using B<-a>, opening of such
54 files is delayed until the first line is actually written to the file.
55  
56 =item B<-e> I<script>
57  
58 The editing commands defined by I<script> are appended to the script.
59 Multiple commands must be separated by newlines or semicolons.
60  
61 =item B<-f> I<script-file>
62  
63 Editing commands from the specified I<script-file> are read and appended
64 to the script.
65  
66 =item B<-n>
67  
68 By default, a line is written to standard output after the editing script
69 has been applied to it. The B<-n> option suppresses automatic printing.
70  
71 =back
72  
73 =head1 COMMANDS
74  
75 B<sed> command syntax is defined as
76  
77 Z<> Z<> Z<> Z<>[I<address>[B<,>I<address>]][B<!>]I<function>[I<argument>]
78  
79 with whitespace being permitted before or after addresses, and between
80 the function character and the argument. The I<address>es and the
81 address inverter (C<!>) are used to restrict the application of a
82 command to the selected line(s) of input.
83  
84 Each command must be on a line of its own, except where noted in
85 the synopses below.
86  
87 The edit cycle performed on each input line consist of reading the line
88 (without its trailing newline character) into the I<pattern space>,
89 applying the applicable commands of the edit script, writing the final
90 contents of the pattern space and a newline to the standard output.
91 A I<hold space> is provided for saving the contents of the
92 pattern space for later use.
93  
94 =head2 Addresses
95  
96 A sed address is either a line number or a pattern, which may be combined
97 arbitrarily to construct ranges. Lines are numbered across all input files.
98  
99 Any address may be followed by an exclamation mark ('C<!>'), selecting
100 all lines not matching that address.
101  
102 =over 4
103  
104 =item I<number>
105  
106 The line with the given number is selected.
107  
108 =item B<$>
109  
110 A dollar sign (C<$>) is the line number of the last line of the input stream.
111  
112 =item B</>I<regular expression>B</>
113  
114 A pattern address is a basic regular expression (see
115 L<"BASIC REGULAR EXPRESSIONS">), between the delimiting character C</>.
116 Any other character except C<\> or newline may be used to delimit a
117 pattern address when the initial delimiter is prefixed with a
118 backslash ('C<\>').
119  
120 =back
121  
122 If no address is given, the command selects every line.
123  
124 If one address is given, it selects the line (or lines) matching the
125 address.
126  
127 Two addresses select a range that begins whenever the first address
128 matches, and ends (including that line) when the second address matches.
129 If the first (second) address is a matching pattern, the second
130 address is not applied to the very same line to determine the end of
131 the range. Likewise, if the second address is a matching pattern, the
132 first address is not applied to the very same line to determine the
133 begin of another range. If both addresses are line numbers,
134 and the second line number is less than the first line number, then
135 only the first line is selected.
136  
137  
138 =head2 Functions
139  
140 The maximum permitted number of addresses is indicated with each
141 function synopsis below.
142  
143 The argument I<text> consists of one or more lines following the command.
144 Embedded newlines in I<text> must be preceded with a backslash. Other
145 backslashes in I<text> are deleted and the following character is taken
146 literally.
147  
148 =over 4
149  
150 =cut
151  
152 66 my %ComTab;
153 my %GenKey;
154 #--------------------------------------------------------------------------
155 66 $ComTab{'a'}=[ 1, 'txt', \&Emit, '{ push( @Q, <<'."'TheEnd' ) }\n" ]; #ok
156  
157 =item [1addr]B<a\> I<text>
158  
159 Write I<text> (which must start on the line following the command)
160 to standard output immediately before reading the next line
161 of input, either by executing the B<N> function or by beginning a new cycle.
162  
163 =cut
164  
165 #--------------------------------------------------------------------------
166 66 $ComTab{'b'}=[ 2, 'str', \&Branch, '{ goto XXX; }' ]; #ok
167  
168 =item [2addr]B<b> [I<label>]
169  
170 Branch to the B<:> function with the specified I<label>. If no label
171 is given, branch to the end of the script.
172  
173 =cut
174  
175 #--------------------------------------------------------------------------
176 66 $ComTab{'c'}=[ 2, 'txt', \&Change, <<'-X-' ]; #ok
177 { print <<'TheEnd'; } $doPrint = 0; goto EOS;
178 -X-
179 ### continue OK => next CYCLE;
180  
181 =item [2addr]B<c\> I<text>
182  
183 The line, or range of lines, selected by the address is deleted.
184 The I<text> (which must start on the line following the command)
185 is written to standard output. With an address range, this occurs at
186 the end of the range.
187  
188 =cut
189  
190 #--------------------------------------------------------------------------
191 66 $ComTab{'d'}=[ 2, '', \&Emit, <<'-X-' ]; #ok
192 { $doPrint = 0;
193 goto EOS;
194 }
195 -X-
196 ### continue OK => next CYCLE;
197  
198 =item [2addr]B<d>
199  
200 Deletes the pattern space and starts the next cycle.
201  
202 =cut
203  
204 #--------------------------------------------------------------------------
205 66 $ComTab{'D'}=[ 2, '', \&Emit, <<'-X-' ]; #ok
206 { s/^.*\n?//;
207 if(length($_)){ goto BOS } else { goto EOS }
208 }
209 -X-
210 ### continue OK => next CYCLE;
211  
212 =item [2addr]B<D>
213  
214 Deletes the pattern space through the first embedded newline or to the end.
215 If the pattern space becomes empty, a new cycle is started, otherwise
216 execution of the script is restarted.
217  
218 =cut
219  
220 #--------------------------------------------------------------------------
221 66 $ComTab{'g'}=[ 2, '', \&Emit, '{ $_ = $Hold };' ]; #ok
222  
223 =item [2addr]B<g>
224  
225 Replace the contents of the pattern space with the hold space.
226  
227 =cut
228  
229 #--------------------------------------------------------------------------
230 66 $ComTab{'G'}=[ 2, '', \&Emit, '{ $_ .= "\n"; $_ .= $Hold };' ]; #ok
231  
232 =item [2addr]B<G>
233  
234 Append a newline and the contents of the hold space to the pattern space.
235  
236 =cut
237  
238 #--------------------------------------------------------------------------
239 66 $ComTab{'h'}=[ 2, '', \&Emit, '{ $Hold = $_ }' ]; #ok
240  
241 =item [2addr]B<h>
242  
243 Replace the contents of the hold space with the pattern space.
244  
245 =cut
246  
247 #--------------------------------------------------------------------------
248 66 $ComTab{'H'}=[ 2, '', \&Emit, '{ $Hold .= "\n"; $Hold .= $_; }' ]; #ok
249  
250 =item [2addr]B<H>
251  
252 Append a newline and the contents of the pattern space to the hold space.
253  
254 =cut
255  
256 #--------------------------------------------------------------------------
257 66 $ComTab{'i'}=[ 1, 'txt', \&Emit, '{ print <<'."'TheEnd' }\n" ]; #ok
258  
259 =item [1addr]B<i\> I<text>
260  
261 Write the I<text> (which must start on the line following the command)
262 to standard output.
263  
264 =cut
265  
266 #--------------------------------------------------------------------------
267 66 $ComTab{'l'}=[ 2, '', \&Emit, '{ _l() }' ]; #okUTF8
268  
269 =item [2addr]B<l>
270  
271 Print the contents of the pattern space: non-printable characters are
272 shown in C-style escaped form; long lines are split and have a trailing
273 ^'C<\>' at the point of the split; the true end of a line is marked with
274 a 'C<$>'. Escapes are: '\a', '\t', '\n', '\f', '\r', '\e' for
275 BEL, HT, LF, FF, CR, ESC, respectively, and '\' followed by a three-digit
276 octal number for all other non-printable characters.
277  
278 =cut
279  
280 #--------------------------------------------------------------------------
281 66 $ComTab{'n'}=[ 2, '', \&Emit, <<'-X-' ]; #ok
282 { print $_, "\n" if $doPrint;
283 printQ() if @Q;
284 $CondReg = 0;
285 last CYCLE unless getsARGV();
286 chomp();
287 }
288 -X-
289  
290 =item [2addr]B<n>
291  
292 If automatic printing is enabled, write the pattern space to the standard
293 output. Replace the pattern space with the next line of input. If
294 there is no more input, processing is terminated.
295  
296 =cut
297  
298 #--------------------------------------------------------------------------
299 66 $ComTab{'N'}=[ 2, '', \&Emit, <<'-X-' ]; #ok
300 { printQ() if @Q;
301 $CondReg = 0;
302 last CYCLE unless getsARGV( $h );
303 chomp( $h );
304 $_ .= "\n$h";
305 }
306 -X-
307  
308 =item [2addr]B<N>
309  
310 Append a newline and the next line of input to the pattern space. If
311 there is no more input, processing is terminated.
312  
313 =cut
314  
315 #--------------------------------------------------------------------------
316 66 $ComTab{'p'}=[ 2, '', \&Emit, '{ print $_, "\n"; }' ]; #ok
317  
318 =item [2addr]B<p>
319  
320 Print the pattern space to the standard output. (Use the B<-n> option
321 to suppress automatic printing at the end of a cycle if you want to
322 avoid double printing of lines.)
323  
324 =cut
325  
326 #--------------------------------------------------------------------------
327 66 $ComTab{'P'}=[ 2, '', \&Emit, <<'-X-' ]; #ok
328 { if( /^(.*)/ ){ print $1, "\n"; } }
329 -X-
330  
331 =item [2addr]B<P>
332  
333 Prints the pattern space through the first embedded newline or to the end.
334  
335 =cut
336  
337 #--------------------------------------------------------------------------
338 66 $ComTab{'q'}=[ 1, '', \&Emit, <<'-X-' ]; #ok
339 { print $_, "\n" if $doPrint;
340 last CYCLE;
341 }
342 -X-
343  
344 =item [1addr]B<q>
345  
346 Branch to the end of the script and quit without starting a new cycle.
347  
348 =cut
349  
350 #--------------------------------------------------------------------------
351 66 $ComTab{'r'}=[ 1, 'str', \&Emit, "{ _r( '-X-' ) }" ]; #ok
352  
353 =item [1addr]B<r> I<file>
354  
355 Copy the contents of the I<file> to standard output immediately before
356 the next attempt to read a line of input. Any error encountered while
357 reading I<file> is silently ignored.
358  
359 =cut
360  
361 #--------------------------------------------------------------------------
362 66 $ComTab{'s'}=[ 2, 'sub', \&Emit, '' ]; #ok
363  
364 =item [2addr]B<s/>I<regular expression>B</>I<replacement>B</>I<flags>
365  
366 Substitute the I<replacement> string for the first substring in
367 the pattern space that matches the I<regular expression>.
368 Any character other than backslash or newline can be used instead of a
369 slash to delimit the regular expression and the replacement.
370 To use the delimiter as a literal character within the regular expression
371 and the replacement, precede the character by a backslash ('C<\>').
372  
373 Literal newlines may be embedded in the replacement string by
374 preceding a newline with a backslash.
375  
376 Within the replacement, an ampersand ('C<&>') is replaced by the string
377 matching the regular expression. The strings 'C<\1>' through 'C<\9>' are
378 replaced by the corresponding subpattern (see L<"BASIC REGULAR EXPRESSIONS">).
379 To get a literal 'C<&>' or 'C<\>' in the replacement text, precede it
380 by a backslash.
381  
382 The following I<flags> modify the behaviour of the B<s> command:
383  
384 =over 8
385  
386 =item B<g>
387  
388 The replacement is performed for all matching, non-overlapping substrings
389 of the pattern space.
390  
391 =item B<1>..B<9>
392  
393 Replace only the n-th matching substring of the pattern space.
394  
395 =item B<p>
396  
397 If the substitution was made, print the new value of the pattern space.
398  
399 =item B<w> I<file>
400  
401 If the substitution was made, write the new value of the pattern space
402 to the specified file.
403  
404 =back
405  
406 =cut
407  
408 #--------------------------------------------------------------------------
409 66 $ComTab{'t'}=[ 2, 'str', \&Branch, '{ goto XXX if _t() }' ]; #ok
410  
411 =item [2addr]B<t> [I<label>]
412  
413 Branch to the B<:> function with the specified I<label> if any B<s>
414 substitutions have been made since the most recent reading of an input line
415 or execution of a B<t> function. If no label is given, branch to the end of
416 the script.
417  
418  
419 =cut
420  
421 #--------------------------------------------------------------------------
422 66 $ComTab{'w'}=[ 2, 'str', \&Write, "{ _w( '-X-' ) }" ]; #ok
423  
424 =item [2addr]B<w> I<file>
425  
426 The contents of the pattern space are written to the I<file>.
427  
428 =cut
429  
430 #--------------------------------------------------------------------------
431 66 $ComTab{'x'}=[ 2, '', \&Emit, '{ ($Hold, $_) = ($_, $Hold) }' ]; #ok
432  
433 =item [2addr]B<x>
434  
435 Swap the contents of the pattern space and the hold space.
436  
437 =cut
438  
439 #--------------------------------------------------------------------------
440 66 $ComTab{'y'}=[ 2, 'tra', \&Emit, '' ]; #ok
441  
442 =item [2addr]B<y>B</>I<string1>B</>I<string2>B</>
443  
444 In the pattern space, replace all characters occurring in I<string1> by the
445 character at the corresponding position in I<string2>. It is possible
446 to use any character (other than a backslash or newline) instead of a
447 slash to delimit the strings. Within I<string1> and I<string2>, a
448 backslash followed by any character other than a newline is that literal
449 character, and a backslash followed by an 'n' is replaced by a newline
450 character.
451  
452 =cut
453  
454 #--------------------------------------------------------------------------
455 66 $ComTab{'='}=[ 1, '', \&Emit, '{ print "$.\n" }' ]; #ok
456  
457 =item [1addr]B<=>
458  
459 Prints the current line number on the standard output.
460  
461 =cut
462  
463 #--------------------------------------------------------------------------
464 66 $ComTab{':'}=[ 0, 'str', \&Label, '' ]; #ok
465  
466 =item [0addr]B<:> [I<label>]
467  
468 The command specifies the position of the I<label>. It has no other effect.
469  
470 =cut
471  
472 #--------------------------------------------------------------------------
473 66 $ComTab{'{'}=[ 2, '', \&BeginBlock, '{' ]; #ok
474 66 $ComTab{'}'}=[ 0, '', \&EndBlock, ';}' ]; #ok
475 # ';' to avoid warning on empty {}-block
476  
477 =item [2addr]B<{> [I<command>]
478  
479 =item [0addr]B<}>
480  
481 These two commands begin and end a command list. The first command may
482 be given on the same line as the opening B<{> command. The commands
483 within the list are jointly selected by the address(es) given on the
484 B<{> command (but may still have individual addresses).
485  
486 =cut
487  
488 #--------------------------------------------------------------------------
489 66 $ComTab{'#'}=[ 0, 'str', \&Comment, '' ]; #ok
490  
491 =item [0addr]B<#> [I<comment>]
492  
493 The entire line is ignored (treated as a comment). If, however, the first
494 two characters in the script are 'C<#n>', automatic printing of output is
495 suppressed, as if the B<-n> option were given on the command line.
496  
497 =back
498  
499 =cut
500  
501 66 ▶use vars qw{ $isEOF $Hold %wFiles @Q $CondReg $doPrint };
Subroutine BEGIN: called CC=1 SCAR=0.0
502  
503 66 my $useDEBUG = exists( $ENV{PSEDDEBUG} );
504 66 ▶my $useEXTBRE = $ENV{PSEDEXTBRE} || '';
Condition50%1/2
$ENV{'PSEDEXTBRE'} || ''
l!l
660
MC/DC0%0/1
$ENV{'PSEDEXTBRE'} || ''
$ENV{'PSEDEXTBRE'}
Truth table50%1/2
A
A= $ENV{'PSEDEXTBRE'}
Aresult
11
00
505 66 $useEXTBRE =~ s/[^<>wWyB]//g; # gawk RE's handle these
506  
507 66 my $doAutoPrint = 1; # automatic printing of pattern space (-n => 0)
508 66 my $doOpenWrite = 1; # open w command output files at start (-a => 0)
509 66 my $svOpenWrite = 0; # save $doOpenWrite
510  
511 # lower case $0 below as a VMSism. The VMS build procedure creates the
512 # s2p file traditionally in upper case on the disk. When VMS is in a
513 # case preserved or case sensitive mode, $0 will be returned in the exact
514 # case which will be on the disk, and that is not predictable at this time.
515  
516 66 my $doGenerate = lc($0) eq 's2p';
517  
518 # Collected and compiled script
519 #
520 66 my( @Commands, %Defined, @BlockStack, %Label, $labNum, $Code, $Func );
521 66 $Code = '';
522  
523 ##################
524 # Compile Time
525 #
526 # Labels
527 #
528 # Error handling
529 #
530 0 ▶sub Warn($;$){
Subroutine Warn: not called CC=3 SCAR=24.8
531 0 my( $msg, $loc ) = @_;
532 0 ▶ $loc ||= '';
Condition0%0/2
$loc ||= ""
l!l
00
MC/DC0%0/1
$loc ||= ""
$loc
Truth table0%0/2
A
A= $loc
Aresult
11
00
533 0 ▶ $loc .= ': ' if length( $loc );
Branch: 1 branch
BranchTrueFalseTotal
if length $loc 0 0 0
534 0 warn( "$0: $loc$msg\n" );
535 }
536  
537 66 $labNum = 0;
538 12 ▶sub newLabel(){
Subroutine newLabel: called CC=1 SCAR=0.0
539 12 return 'L_'.++$labNum;
540 }
541  
542 # safeHere: create safe here delimiter and modify opcode and argument
543 #
544 50 ▶sub safeHere($$){
Subroutine safeHere: called CC=2 SCAR=7.1
545 50 my( $codref, $argref ) = @_;
546 50 my $eod = 'EOD000';
547 50 ▶ while( $$argref =~ /^$eod$/m ){
Branch: 1 branch
BranchTrueFalseTotal
if ($$argref =~ /^$eod$/m) 0 50 50
548 0 $eod++;
549 }
550 50 $$codref =~ s/TheEnd/$eod/e;
551 50 $$argref .= "$eod\n";
552 }
553  
554 # Emit: create address logic and emit command
555 #
556 280 ▶sub Emit($$$$$$){
Subroutine Emit: called CC=9 SCAR=22.0
557 280 my( $addr1, $addr2, $negated, $opcode, $arg, $fl ) = @_;
558 280 my $cond = '';
559 280 ▶ if( defined( $addr1 ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $addr1) 112 168 280
560 112 ▶ if( defined( $addr2 ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $addr2) { } 16 96 112
561 16 ▶ $addr1 .= $addr2 =~ /^\d+$/ ? "..$addr2" : "...$addr2";
Branch: 1 branch
BranchTrueFalseTotal
$addr2 =~ /^\d+$/ ? : 8 8 16
562 } else {
563 96 ▶ $addr1 .= ' == $.' if $addr1 =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $addr1 =~ /^\d+$/ 74 22 96
564 }
565 112 ▶ $cond = $negated ? "unless( $addr1 )\n" : "if( $addr1 )\n";
Branch: 1 branch
BranchTrueFalseTotal
$negated ? : 2 110 112
566 }
567  
568 280 ▶ if( $opcode eq '' ){
Branch: 3 branches
BranchTrueFalseTotal
if ($opcode eq '') { } 96 184 280
elsif ($opcode =~ s/-X-/$arg;/e) { } 6 178 184
elsif ($opcode =~ /TheEnd/) { } 32 146 178
569 96 $Code .= "$cond$arg\n";
570  
571 6 } elsif( $opcode =~ s/-X-/$arg/e ){
572 6 $Code .= "$cond$opcode\n";
573  
574 } elsif( $opcode =~ /TheEnd/ ){
575 32 safeHere( \$opcode, \$arg );
576 32 $Code .= "$cond$opcode$arg";
577  
578 } else {
579 146 $Code .= "$cond$opcode\n";
580 }
581 280 0;
582 }
583  
584 # Write (w command, w flag): store pathname
585 #
586 2 ▶sub Write($$$$$$){
Subroutine Write: called CC=1 SCAR=0.0
587 2 my( $addr1, $addr2, $negated, $opcode, $path, $fl ) = @_;
588 2 $wFiles{$path} = '';
589 2 Emit( $addr1, $addr2, $negated, $opcode, $path, $fl );
590 }
591  
592  
593 # Label (: command): label definition
594 #
595 12 ▶sub Label($$$$$$){
Subroutine Label: called CC=4 SCAR=14.4
596 12 my( $addr1, $addr2, $negated, $opcode, $lab, $fl ) = @_;
597 12 my $rc = 0;
598 12 $lab =~ s/\s+//;
599 12 ▶ if( length( $lab ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (length $lab) 12 0 12
600 12 my $h;
601 12 ▶ if( ! exists( $Label{$lab} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $Label{$lab}) { } 4 8 12
602 4 $h = $Label{$lab}{name} = newLabel();
603 } else {
604 8 $h = $Label{$lab}{name};
605 8 ▶ if( exists( $Label{$lab}{defined} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Label{$lab}{"defined"}) 0 8 8
606 0 my $dl = $Label{$lab}{defined};
607 0 Warn( "duplicate label $lab (first defined at $dl)", $fl );
608 0 $rc = 1;
609 }
610 }
611 12 $Label{$lab}{defined} = $fl;
612 12 $Code .= "$h:;\n";
613 }
614 12 $rc;
615 }
616  
617 # BeginBlock ({ command): push block start
618 #
619 6 ▶sub BeginBlock($$$$$$){
Subroutine BeginBlock: called CC=1 SCAR=0.0
620 6 my( $addr1, $addr2, $negated, $opcode, $arg, $fl ) = @_;
621 6 push( @BlockStack, [ $fl, $addr1, $addr2, $negated ] );
622 6 Emit( $addr1, $addr2, $negated, $opcode, $arg, $fl );
623 }
624  
625 # EndBlock (} command): check proper nesting
626 #
627 6 ▶sub EndBlock($$$$$$){
Subroutine EndBlock: called CC=2 SCAR=7.5
628 6 my( $addr1, $addr2, $negated, $opcode, $arg, $fl ) = @_;
629 6 my $rc;
630 6 my $jcom = pop( @BlockStack );
631 6 ▶ if( defined( $jcom ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $jcom) { } 6 0 6
632 6 $rc = Emit( $addr1, $addr2, $negated, $opcode, $arg, $fl );
633 } else {
634 0 Warn( "unexpected '}'", $fl );
635 0 $rc = 1;
636 }
637 6 $rc;
638 }
639  
640 # Branch (t, b commands): check or create label, substitute default
641 #
642 18 ▶sub Branch($$$$$$){
Subroutine Branch: called CC=3 SCAR=11.0
643 18 my( $addr1, $addr2, $negated, $opcode, $lab, $fl ) = @_;
644 18 $lab =~ s/\s+//; # no spaces at end
645 18 my $h;
646 18 ▶ if( length( $lab ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (length $lab) { } 16 2 18
647 16 ▶ if( ! exists( $Label{$lab} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $Label{$lab}) { } 8 8 16
648 8 $h = $Label{$lab}{name} = newLabel();
649 } else {
650 8 $h = $Label{$lab}{name};
651 }
652 16 push( @{$Label{$lab}{used}}, $fl );
653 } else {
654 2 $h = 'EOS';
655 }
656 18 $opcode =~ s/XXX/$h/e;
657 18 Emit( $addr1, $addr2, $negated, $opcode, '', $fl );
658 }
659  
660 # Change (c command): is special due to range end watching
661 #
662 18 ▶sub Change($$$$$$){
Subroutine Change: called CC=6 SCAR=17.9
663 18 my( $addr1, $addr2, $negated, $opcode, $arg, $fl ) = @_;
664 18 ▶ my $kwd = $negated ? 'unless' : 'if';
Branch: 1 branch
BranchTrueFalseTotal
$negated ? : 6 12 18
665 18 ▶ if( defined( $addr2 ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $addr2) { } 14 4 18
666 14 ▶ $addr1 .= $addr2 =~ /^\d+$/ ? "..$addr2" : "...$addr2";
Branch: 1 branch
BranchTrueFalseTotal
$addr2 =~ /^\d+$/ ? : 10 4 14
667 14 ▶ if( ! $negated ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($negated) 10 4 14
668 10 $addr1 = '$icnt = ('.$addr1.')';
669 10 $opcode = 'if( $icnt =~ /E0$/ )' . $opcode;
670 }
671 } else {
672 4 ▶ $addr1 .= ' == $.' if $addr1 =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $addr1 =~ /^\d+$/ 4 0 4
673 }
674 18 safeHere( \$opcode, \$arg );
675 18 $Code .= "$kwd( $addr1 ){\n $opcode$arg}\n";
676 18 0;
677 }
678  
679  
680 # Comment (# command): A no-op. Who would've thought that!
681 #
682 50 ▶sub Comment($$$$$$){
Subroutine Comment: called CC=1 SCAR=0.0
683 50 my( $addr1, $addr2, $negated, $opcode, $arg, $fl ) = @_;
684 ### $Code .= "# $arg\n";
685 50 0;
686 }
687  
688 # stripRegex from the current command. If we're in the first
689 # part of s///, trailing spaces have to be kept as the initial
690 # part of the replacement string.
691 #
692 216 ▶sub stripRegex($$;$){
Subroutine stripRegex: called CC=6 SCAR=20.2
693 216 my( $del, $sref, $sub ) = @_;
694 216 my $regex = $del;
695 216 ▶ print "stripRegex:$del:$$sref:\n" if $useDEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $useDEBUG 0 216 216
696 216 ▶ while( $$sref =~ s{^(.*?)(\\*)\Q$del\E(\s*)}{}s ){
Branch: 1 branch
BranchTrueFalseTotal
if ($$sref =~ s/^(.*?)(\\*)\Q$del\E(\s*)//s) 216 0 216
697 216 my $sl = $2;
698 216 $regex .= $1.$sl.$del;
699 216 ▶ if( length( $sl ) % 2 == 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if (length($sl) % 2 == 0) 216 0 216
700 216 ▶ if( $sub && (length( $3 ) > 0) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($sub and length $3 > 0) 0 216 216
Condition66%2/3
$sub and length $3 > 0
!ll&&!rl&&r
124920
MC/DC0%0/2
$sub and length $3 > 0
$sub length $3 > 0
Truth table66%2/3
A and B
A= $subB= length $3 > 0
ABresult
0X0
100
111
701 0 $$sref = $3 . $$sref;
702 }
703 216 return $regex;
704 }
705 0 $regex .= $3;
706 }
707 0 undef();
708 }
709  
710 # stripTrans: take a <del> terminated string from y command
711 # honoring and cleaning up of \-escaped <del>'s
712 #
713 8 ▶sub stripTrans($$){
Subroutine stripTrans: called CC=4 SCAR=15.0
714 8 my( $del, $sref ) = @_;
715 8 my $t = '';
716 8 ▶ print "stripTrans:$del:$$sref:\n" if $useDEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $useDEBUG 0 8 8
717 8 ▶ while( $$sref =~ s{^(.*?)(\\*)\Q$del\E}{}s ){
Branch: 1 branch
BranchTrueFalseTotal
if ($$sref =~ s/^(.*?)(\\*)\Q$del\E//s) 8 0 8
718 8 my $sl = $2;
719 8 $t .= $1;
720 8 ▶ if( length( $sl ) % 2 == 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if (length($sl) % 2 == 0) 8 0 8
721 8 $t .= $sl;
722 8 $t =~ s/\\\\/\\/g;
723 8 return $t;
724 }
725 0 chop( $sl );
726 0 $t .= $sl.$del.$3;
727 }
728 0 undef();
729 }
730  
731 # makey - construct Perl y/// from sed y///
732 #
733 4 ▶sub makey($$$){
Subroutine makey: called CC=7 SCAR=20.7
734 4 my( $fr, $to, $fl ) = @_;
735 4 my $error = 0;
736  
737 # Ensure that any '-' is up front.
738 # Diagnose duplicate contradicting mappings
739 4 my %tr;
740 4 ▶ for( my $i = 0; $i < length($fr); $i++ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $fr) 54 4 58
741 54 my $fc = substr($fr,$i,1);
742 54 my $tc = substr($to,$i,1);
743 54 ▶ if( exists( $tr{$fc} ) && $tr{$fc} ne $tc ){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tr{$fc} and $tr{$fc} ne $tc) 0 54 54
Condition33%1/3
exists $tr{$fc} and $tr{$fc} ne $tc
!ll&&!rl&&r
5400
MC/DC0%0/2
exists $tr{$fc} and $tr{$fc} ne $tc
exists $tr{$fc} $tr{$fc} ne $tc
Truth table33%1/3
A and B
A= exists $tr{$fc}B= $tr{$fc} ne $tc
ABresult
0X0
100
111
744 0 Warn( "ambiguous translation for character '$fc' in 'y' command",
745 $fl );
746 0 $error++;
747 }
748 54 $tr{$fc} = $tc;
749 }
750 4 $fr = $to = '';
751 4 ▶ if( exists( $tr{'-'} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tr{'-'}) { } 0 4 4
752 0 ( $fr, $to ) = ( '-', $tr{'-'} );
753 0 delete( $tr{'-'} );
754 } else {
755 4 $fr = $to = '';
756 }
757 # might just as well sort it...
758 4 for my $fc ( sort keys( %tr ) ){
759 54 $fr .= $fc;
760 54 $to .= $tr{$fc};
761 }
762 # make embedded delimiters and newlines safe
763 4 $fr =~ s/([{}])/\$1/g;
764 4 $to =~ s/([{}])/\$1/g;
765 4 $fr =~ s/\n/\\n/g;
766 4 $to =~ s/\n/\\n/g;
767 4 ▶ return $error ? undef() : "{ y{$fr}{$to}; }";
Branch: 1 branch
BranchTrueFalseTotal
$error ? : 0 4 4
768 }
769  
770 ######
771 # makes - construct Perl s/// from sed s///
772 #
773 92 ▶sub makes($$$$$$$){
Subroutine makes: called CC=4 SCAR=14.9
774 92 my( $regex, $subst, $path, $global, $print, $nmatch, $fl ) = @_;
775  
776 # make embedded newlines safe
777 92 $regex =~ s/\n/\\n/g;
778 92 $subst =~ s/\n/\\n/g;
779  
780 92 my $code;
781 # n-th occurrence
782 #
783 92 ▶ if( length( $nmatch ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (length $nmatch) { } 0 92 92
784 0 $code = <<TheEnd;
785 { \$n = $nmatch;
786 while( --\$n && ( \$s = m ${regex}g ) ){}
787 \$s = ( substr( \$_, pos() ) =~ s ${regex}${subst}s ) if \$s;
788 \$CondReg ||= \$s;
789 TheEnd
790 } else {
791 92 $code = <<TheEnd;
792 { \$s = s ${regex}${subst}s${global};
793 \$CondReg ||= \$s;
794 TheEnd
795 }
796 92 ▶ if( $print ){
Branch: 1 branch
BranchTrueFalseTotal
if ($print) 4 88 92
797 4 $code .= ' print $_, "\n" if $s;'."\n";
798 }
799 92 ▶ if( defined( $path ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path) 0 92 92
800 0 $wFiles{$path} = '';
801 0 $code .= " _w( '$path' ) if \$s;\n";
802 0 $GenKey{'w'} = 1;
803 }
804 92 $code .= "}";
805 }
806  
807 =head1 BASIC REGULAR EXPRESSIONS
808  
809 A I<Basic Regular Expression> (BRE), as defined in POSIX 1003.2, consists
810 of I<atoms>, for matching parts of a string, and I<bounds>, specifying
811 repetitions of a preceding atom.
812  
813 =head2 Atoms
814  
815 The possible atoms of a BRE are: B<.>, matching any single character;
816 B<^> and B<$>, matching the null string at the beginning or end
817 of a string, respectively; a I<bracket expressions>, enclosed
818 in B<[> and B<]> (see below); and any single character with no
819 other significance (matching that character). A B<\> before one
820 of: B<.>, B<^>, B<$>, B<[>, B<*>, B<\>, matching the character
821 after the backslash. A sequence of atoms enclosed in B<\(> and B<\)>
822 becomes an atom and establishes the target for a I<backreference>,
823 consisting of the substring that actually matches the enclosed atoms.
824 Finally, B<\> followed by one of the digits B<0> through B<9> is a
825 backreference.
826  
827 A B<^> that is not first, or a B<$> that is not last does not have
828 a special significance and need not be preceded by a backslash to
829 become literal. The same is true for a B<]>, that does not terminate
830 a bracket expression.
831  
832 An unescaped backslash cannot be last in a BRE.
833  
834 =head2 Bounds
835  
836 The BRE bounds are: B<*>, specifying 0 or more matches of the preceding
837 atom; B<\{>I<count>B<\}>, specifying that many repetitions;
838 B<\{>I<minimum>B<,\}>, giving a lower limit; and
839 B<\{>I<minimum>B<,>I<maximum>B<\}> finally defines a lower and upper
840 bound.
841  
842 A bound appearing as the first item in a BRE is taken literally.
843  
844 =head2 Bracket Expressions
845  
846 A I<bracket expression> is a list of characters, character ranges
847 and character classes enclosed in B<[> and B<]> and matches any
848 single character from the represented set of characters.
849  
850 A character range is written as two characters separated by B<-> and
851 represents all characters (according to the character collating sequence)
852 that are not less than the first and not greater than the second.
853 (Ranges are very collating-sequence-dependent, and portable programs
854 should avoid relying on them.)
855  
856 A character class is one of the class names
857  
858 alnum digit punct
859 alpha graph space
860 blank lower upper
861 cntrl print xdigit
862  
863 enclosed in B<[:> and B<:]> and represents the set of characters
864 as defined in ctype(3).
865  
866 If the first character after B<[> is B<^>, the sense of matching is
867 inverted.
868  
869 To include a literal 'C<^>', place it anywhere else but first. To
870 include a literal 'C<]>' place it first or immediately after an
871 initial B<^>. To include a literal 'C<->' make it the first (or
872 second after B<^>) or last character, or the second endpoint of
873 a range.
874  
875 The special bracket expression constructs C<[[:E<lt>:]]> and C<[[:E<gt>:]]>
876 match the null string at the beginning and end of a word respectively.
877 (Note that neither is identical to Perl's '\b' atom.)
878  
879 =head2 Additional Atoms
880  
881 Since some sed implementations provide additional regular expression
882 atoms (not defined in POSIX 1003.2), B<psed> is capable of translating
883 the following backslash escapes:
884  
885 =over 4
886  
887 =item B<\E<lt>> This is the same as C<[[:E<gt>:]]>.
888  
889 =item B<\E<gt>> This is the same as C<[[:E<lt>:]]>.
890  
891 =item B<\w> This is an abbreviation for C<[[:alnum:]_]>.
892  
893 =item B<\W> This is an abbreviation for C<[^[:alnum:]_]>.
894  
895 =item B<\y> Match the empty string at a word boundary.
896  
897 =item B<\B> Match the empty string between any two either word or non-word characters.
898  
899 =back
900  
901 To enable this feature, the environment variable PSEDEXTBRE must be set
902 to a string containing the requested characters, e.g.:
903 C<PSEDEXTBRE='E<lt>E<gt>wW'>.
904  
905 =cut
906  
907 #####
908 # bre2p - convert BRE to Perl RE
909 #
910 124 ▶sub peek(\$$){
Subroutine peek: called CC=2 SCAR=6.9
911 124 my( $pref, $ic ) = @_;
912 124 ▶ $ic < length($$pref)-1 ? substr( $$pref, $ic+1, 1 ) : '';
Branch: 1 branch
BranchTrueFalseTotal
$ic < length($$pref) - 1 ? : 122 2 124
913 }
914  
915 124 ▶sub bre2p($$$){
Subroutine bre2p: called CC=60 SCAR=53.2
916 124 my( $del, $pat, $fl ) = @_;
917 124 my $led = $del;
918 124 $led =~ tr/{([</})]>/;
919 124 ▶ $led = '' if $led eq $del;
Branch: 1 branch
BranchTrueFalseTotal
if $led eq $del 124 0 124
920  
921 124 $pat = substr( $pat, 1, length($pat) - 2 );
922 124 my $res = '';
923 124 my $bracklev = 0;
924 124 my $backref = 0;
925 124 my $parlev = 0;
926 124 ▶ for( my $ic = 0; $ic < length( $pat ); $ic++ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($ic < length $pat) 350 124 474
927 350 my $c = substr( $pat, $ic, 1 );
928 350 ▶ if( $c eq '\\' ){
Branch: 9 branches
BranchTrueFalseTotal
if ($c eq '\\') { } 68 282 350
elsif ($c eq '.') { } 16 266 282
elsif ($c eq '*') { } 22 244 266
elsif ($c eq '[') { } 18 226 244
elsif ($c eq $led) { } 0 226 226
elsif ($c eq ']') { } 0 226 226
elsif ($c =~ /[|+?{}()]/) { } 8 218 226
elsif ($c eq '^') { } 32 186 218
elsif ($c eq '$') { } 16 170 186
929 ### backslash escapes
930 68 my $nc = peek($pat,$ic);
931 68 ▶ if( $nc eq '' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($nc eq "") 0 68 68
932 0 Warn( "'\\' cannot be last in pattern", $fl );
933 0 return undef();
934 }
935 68 $ic++;
936 68 ▶ if( $nc eq $del ){ ## \<pattern del> => \<pattern del>
Branch: 8 branches
BranchTrueFalseTotal
if ($nc eq $del) { } 0 68 68
elsif ($nc =~ /([[.*\\n])/) { } 14 54 68
elsif ($nc eq '(') { } 14 40 54
elsif ($nc eq ')') { } 14 26 40
elsif ($nc eq '{') { } 12 14 26
elsif ($nc =~ /^[1-9]$/) { } 6 8 14
elsif ($useEXTBRE and $nc =~ /[$useEXTBRE]/) { } 6 2 8
elsif ($nc eq $led) { } 0 2 2
937 0 $res .= "\\$del";
938  
939 } elsif( $nc =~ /([[.*\\n])/ ){
940 ## check for \-escaped magics and \n:
941 ## \[ \. \* \\ \n stay as they are
942 14 $res .= '\\'.$nc;
943  
944 } elsif( $nc eq '(' ){ ## \( => (
945 14 $parlev++;
946 14 $res .= '(';
947  
948 } elsif( $nc eq ')' ){ ## \) => )
949 14 $parlev--;
950 14 $backref++;
951 14 ▶ if( $parlev < 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($parlev < 0) 0 14 14
952 0 Warn( "unmatched '\\)'", $fl );
953 0 return undef();
954 }
955 14 $res .= ')';
956  
957 } elsif( $nc eq '{' ){ ## repetition factor \{<i>[,[<j>]]\}
958 12 my $endpos = index( $pat, '\\}', $ic );
959 12 ▶ if( $endpos < 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($endpos < 0) 0 12 12
960 0 Warn( "unmatched '\\{'", $fl );
961 0 return undef();
962 }
963 12 my $rep = substr( $pat, $ic+1, $endpos-($ic+1) );
964 12 $ic = $endpos + 1;
965  
966 12 ▶ if( $res =~ /^\^?$/ ){
Branch: 2 branches
BranchTrueFalseTotal
if ($res =~ /^\^?$/) { } 0 12 12
elsif ($rep =~ /^(\d+)(,?)(\d*)?$/) { } 12 0 12
967 0 $res .= "\\{$rep\}";
968 } elsif( $rep =~ /^(\d+)(,?)(\d*)?$/ ){
969 12 my $min = $1;
970 12 ▶ my $com = $2 || '';
Condition100%2/2
$2 || ''
l!l
102
MC/DC100%1/1
$2 || ''
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
971 12 my $max = $3;
972 12 ▶ if( length( $max ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (length $max) { } 0 12 12
973 0 ▶ if( $max < $min ){
Branch: 1 branch
BranchTrueFalseTotal
if ($max < $min) 0 0 0
974 0 Warn( "maximum less than minimum in '\\{$rep\\}'",
975 $fl );
976 0 return undef();
977 }
978 } else {
979 12 $max = '';
980 }
981 # simplify some
982 12 ▶ if( $min == 0 && $max eq '1' ){
Branch: 3 branches
BranchTrueFalseTotal
if ($min == 0 and $max eq '1') { } 0 12 12
elsif ($min == 1 and "$com$max" eq ',') { } 10 2 12
elsif ($min == 0 and "$com$max" eq ',') { } 0 2 2
Condition33%1/3
$min == 0 and $max eq 1
!ll&&!rl&&r
1200
MC/DC0%0/2
$min == 0 and $max eq 1
$min == 0 $max eq 1
Truth table33%1/3
A and B
A= $min == 0B= $max eq 1
ABresult
0X0
100
111
983 0 $res .= '?';
984 ▶ } elsif( $min == 1 && "$com$max" eq ',' ){
Condition66%2/3
$min == 1 and "$com$max" eq ","
!ll&&!rl&&r
2010
MC/DC50%1/2
$min == 1 and "$com$max" eq ","
$min == 1 "$com$max" eq ","
Truth table66%2/3
A and B
A= $min == 1B= "$com$max" eq ","
ABresult
0X0
100
111
985 10 $res .= '+';
986 ▶ } elsif( $min == 0 && "$com$max" eq ',' ){
Condition33%1/3
$min == 0 and "$com$max" eq ","
!ll&&!rl&&r
200
MC/DC0%0/2
$min == 0 and "$com$max" eq ","
$min == 0 "$com$max" eq ","
Truth table33%1/3
A and B
A= $min == 0B= "$com$max" eq ","
ABresult
0X0
100
111
987 0 $res .= '*';
988 } else {
989 2 $res .= "{$min$com$max}";
990 }
991 } else {
992 0 Warn( "invalid repeat clause '\\{$rep\\}'", $fl );
993 0 return undef();
994 }
995  
996 } elsif( $nc =~ /^[1-9]$/ ){
997 ## \1 .. \9 => \1 .. \9, but check for a following digit
998 6 ▶ if( $nc > $backref ){
Branch: 1 branch
BranchTrueFalseTotal
if ($nc > $backref) 0 6 6
999 0 Warn( "invalid backreference ($nc)", $fl );
1000 0 return undef();
1001 }
1002 6 $res .= "\\$nc";
1003 6 ▶ if( peek($pat,$ic) =~ /[0-9]/ ){
Branch: 1 branch
BranchTrueFalseTotal
if (&peek(\$pat, $ic) =~ /[0-9]/) 0 6 6
1004 0 $res .= '(?:)';
1005 }
1006  
1007 ▶ } elsif( $useEXTBRE && ( $nc =~ /[$useEXTBRE]/ ) ){
Condition66%2/3
$useEXTBRE and $nc =~ /[$useEXTBRE]/
!ll&&!rl&&r
026
MC/DC50%1/2
$useEXTBRE and $nc =~ /[$useEXTBRE]/
$useEXTBRE $nc =~ /[$useEXTBRE]/
Truth table66%2/3
A and B
A= $useEXTBREB= $nc =~ /[$useEXTBRE]/
ABresult
0X0
100
111
1008 ## extensions - at most <>wWyB - not in POSIX
1009 6 ▶ if( $nc eq '<' ){ ## \< => \b(?=\w), be precise
Branch: 3 branches
BranchTrueFalseTotal
if ($nc eq '<') { } 2 4 6
elsif ($nc eq '>') { } 2 2 4
elsif ($nc eq 'y') { } 0 2 2
1010 2 $res .= '\\b(?=\\w)';
1011 } elsif( $nc eq '>' ){ ## \> => \b(?<=\w), be precise
1012 2 $res .= '\\b(?<=\\w)';
1013 } elsif( $nc eq 'y' ){ ## \y => \b
1014 0 $res .= '\\b';
1015 } else { ## \B, \w, \W remain the same
1016 2 $res .= "\\$nc";
1017 }
1018 } elsif( $nc eq $led ){
1019 ## \<closing bracketing-delimiter> - keep '\'
1020 0 $res .= "\\$nc";
1021  
1022 } else { ## \ <char> => <char> ("as if '\' were not present")
1023 2 $res .= $nc;
1024 }
1025  
1026 } elsif( $c eq '.' ){ ## . => .
1027 16 $res .= $c;
1028  
1029 } elsif( $c eq '*' ){ ## * => * but \* if there's nothing preceding it
1030 22 ▶ if( $res =~ /^\^?$/ ){
Branch: 2 branches
BranchTrueFalseTotal
if ($res =~ /^\^?$/) { } 0 22 22
elsif (substr($res, -1, 1) ne '*') { } 22 0 22
1031 0 $res .= '\\*';
1032 } elsif( substr( $res, -1, 1 ) ne '*' ){
1033 22 $res .= $c;
1034 }
1035  
1036 } elsif( $c eq '[' ){
1037 ## parse []: [^...] [^]...] [-...]
1038 18 my $add = '[';
1039 18 ▶ if( peek($pat,$ic) eq '^' ){
Branch: 1 branch
BranchTrueFalseTotal
if (&peek(\$pat, $ic) eq "^") 0 18 18
1040 0 $ic++;
1041 0 $add .= '^';
1042 }
1043 18 my $nc = peek($pat,$ic);
1044 18 ▶ if( $nc eq ']' || $nc eq '-' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($nc eq "]" or $nc eq "-") 0 18 18
Condition33%1/3
$nc eq "]" or $nc eq "-"
l!l&&r!l&&!r
0018
MC/DC0%0/2
$nc eq "]" or $nc eq "-"
$nc eq "]" $nc eq "-"
Truth table33%1/3
A or B
A= $nc eq "]"B= $nc eq "-"
ABresult
1X1
011
000
1045 0 $add .= $nc;
1046 0 $ic++;
1047 }
1048 # check that [ is not trailing
1049 18 ▶ if( $ic >= length( $pat ) - 1 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($ic >= length($pat) - 1) 0 18 18
1050 0 Warn( "unmatched '['", $fl );
1051 0 return undef();
1052 }
1053 # look for [:...:] and x-y
1054 18 my $rstr = substr( $pat, $ic+1 );
1055 18 ▶ if( $rstr =~ /^((?:\[:\(\w+|[><]\):\]|[^]-](?:-[^]])?)*)/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($rstr =~ /^((?:\[:\(\w+|[><]\):\]|[^]-](?:-[^]])?)*)/) 18 0 18
1056 18 my $cnt = $1;
1057 18 $ic += length( $cnt );
1058 18 $cnt =~ s/([\\\$])/\\$1/g; # '\', '$' are magic in Perl []
1059 # try some simplifications
1060 18 my $red = $cnt;
1061 18 ▶ if( $red =~ s/0-9// ){
Branch: 1 branch
BranchTrueFalseTotal
if ($red =~ s/0-9//) 2 16 18
1062 2 $cnt = $red.'\d';
1063 2 ▶ if( $red =~ s/A-Z// && $red =~ s/a-z// && $red =~ s/_// ){
Branch: 1 branch
BranchTrueFalseTotal
if ($red =~ s/A-Z// and $red =~ s/a-z// and $red =~ s/_//) 0 2 2
Condition33%2/6
$red =~ s/A-Z// and $red =~ s/a-z// and $red =~ s/_//
!ll&&!rl&&r
200
$red =~ s/A-Z// and $red =~ s/a-z//
!ll&&!rl&&r
200
MC/DC0%0/3
$red =~ s/A-Z// and $red =~ s/a-z// and $red =~ s/_//
$red =~ s/A-Z// $red =~ s/a-z// $red =~ s/_//
Truth table25%1/4
A and B and C
A= $red =~ s/A-Z//B= $red =~ s/a-z//C= $red =~ s/_//
ABCresult
0XX0
10X0
1100
1111
1064 0 $cnt = $red.'\w';
1065 }
1066 }
1067 18 $add .= $cnt;
1068  
1069 # POSIX 1003.2 has this (optional) for begin/end word
1070 18 ▶ $add = '\\b(?=\\W)' if $add eq '[[:<:]]';
Branch: 1 branch
BranchTrueFalseTotal
if $add eq "[[:<:]]" 0 18 18
1071 18 ▶ $add = '\\b(?<=\\W)' if $add eq '[[:>:]]';
Branch: 1 branch
BranchTrueFalseTotal
if $add eq "[[:>:]]" 0 18 18
1072  
1073 }
1074  
1075 ## may have a trailing '-' before ']'
1076 18 ▶ if( $ic < length($pat) - 1 &&
Branch: 1 branch
BranchTrueFalseTotal
if ($ic < length($pat) - 1 and substr($pat, $ic + 1) =~ /^(-?])/) { } 18 0 18
Condition33%1/3
$ic < length($pat) - 1 and substr($pat, $ic + 1) =~ /^(-?])/
!ll&&!rl&&r
0018
MC/DC0%0/2
$ic < length($pat) - 1 and substr($pat, $ic + 1) =~ /^(-?])/
$ic < length($pat) - 1 substr($pat, $ic + 1) =~ /^(-?])/
Truth table33%1/3
A and B
A= $ic < length($pat) - 1B= substr($pat, $ic + 1) =~ /^(-?])/
ABresult
0X0
100
111
1077 substr( $pat, $ic+1 ) =~ /^(-?])/ ){
1078 18 $ic += length( $1 );
1079 18 $add .= $1;
1080 # another simplification
1081 18 ▶ $add =~ s/^\[(\^?)(\\[dw])]$/ $1 eq '^' ? uc($2) : $2 /e;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '^' ? : 0 2 2
1082 18 $res .= $add;
1083 } else {
1084 0 Warn( "unmatched '['", $fl );
1085 0 return undef();
1086 }
1087  
1088 } elsif( $c eq $led ){ ## unescaped <closing bracketing-delimiter>
1089 0 $res .= "\\$c";
1090  
1091 } elsif( $c eq ']' ){ ## unmatched ] is not magic
1092 0 $res .= ']';
1093  
1094 } elsif( $c =~ /[|+?{}()]/ ){ ## not magic in BRE, but in Perl: \-quote
1095 8 $res .= "\\$c";
1096  
1097 } elsif( $c eq '^' ){ ## not magic unless 1st, but in Perl: \-quote
1098 32 ▶ $res .= length( $res ) ? '\\^' : '^';
Branch: 1 branch
BranchTrueFalseTotal
length $res ? : 0 32 32
1099  
1100 } elsif( $c eq '$' ){ ## not magic unless last, but in Perl: \-quote
1101 16 ▶ $res .= $ic == length( $pat ) - 1 ? '$' : '\\$';
Branch: 1 branch
BranchTrueFalseTotal
$ic == length($pat) - 1 ? : 16 0 16
1102  
1103 } else {
1104 170 $res .= $c;
1105 }
1106 }
1107  
1108 124 ▶ if( $parlev ){
Branch: 1 branch
BranchTrueFalseTotal
if ($parlev) 0 124 124
1109 0 Warn( "unmatched '\\('", $fl );
1110 0 return undef();
1111 }
1112  
1113 # final cleanup: eliminate raw HTs
1114 124 $res =~ s/\t/\\t/g;
1115 124 ▶ return $del . $res . ( $led ? $led : $del );
Branch: 1 branch
BranchTrueFalseTotal
$led ? : 0 124 124
1116 }
1117  
1118  
1119 #####
1120 # sub2p - convert sed substitution to Perl substitution
1121 #
1122 92 ▶sub sub2p($$$){
Subroutine sub2p: called CC=11 SCAR=25.3
1123 92 my( $del, $subst, $fl ) = @_;
1124 92 my $led = $del;
1125 92 $led =~ tr/{([</})]>/;
1126 92 ▶ $led = '' if $led eq $del;
Branch: 1 branch
BranchTrueFalseTotal
if $led eq $del 92 0 92
1127  
1128 92 $subst = substr( $subst, 1, length($subst) - 2 );
1129 92 my $res = '';
1130  
1131 92 ▶ for( my $ic = 0; $ic < length( $subst ); $ic++ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($ic < length $subst) 132 92 224
1132 132 my $c = substr( $subst, $ic, 1 );
1133 132 ▶ if( $c eq '\\' ){
Branch: 3 branches
BranchTrueFalseTotal
if ($c eq '\\') { } 14 118 132
elsif ($c eq '&') { } 4 114 118
elsif ($c =~ /[\$\@$led]/) { } 8 106 114
1134 ### backslash escapes
1135 14 my $nc = peek($subst,$ic);
1136 14 ▶ if( $nc eq '' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($nc eq "") 0 14 14
1137 0 Warn( "'\\' cannot be last in substitution", $fl );
1138 0 return undef();
1139 }
1140 14 $ic++;
1141 14 ▶ if( $nc =~ /[\\$del$led]/ ){ ## \ and delimiter
Branch: 2 branches
BranchTrueFalseTotal
if ($nc =~ /[\\$del$led]/) { } 0 14 14
elsif ($nc =~ /[1-9]/) { } 14 0 14
1142 0 $res .= '\\' . $nc;
1143 } elsif( $nc =~ /[1-9]/ ){ ## \1 - \9 => ${1} - ${9}
1144 14 $res .= '${' . $nc . '}';
1145 } else { ## everything else (includes &): omit \
1146 0 $res .= $nc;
1147 }
1148 } elsif( $c eq '&' ){ ## & => $&
1149 4 $res .= '$&';
1150 } elsif( $c =~ /[\$\@$led]/ ){ ## magic in Perl's substitution string
1151 8 $res .= '\\' . $c;
1152 } else {
1153 106 $res .= $c;
1154 }
1155 }
1156  
1157 # final cleanup: eliminate raw HTs
1158 92 $res =~ s/\t/\\t/g;
1159 92 ▶ return ( $led ? $del : $led ) . $res . ( $led ? $led : $del );
Branch: 2 branches
BranchTrueFalseTotal
$led ? : 0 92 92
$led ? : 0 92 92
1160 }
1161  
1162  
1163 66 ▶sub Parse(){
Subroutine Parse: called CC=75 SCAR=59.7
1164 66 my $error = 0;
1165 66 my( $pdef, $pfil, $plin );
1166 66 ▶ for( my $icom = 0; $icom < @Commands; $icom++ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($icom < @Commands) 406 66 472
1167 406 my $cmd = $Commands[$icom];
1168 406 ▶ print "Parse:$cmd:\n" if $useDEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $useDEBUG 0 406 406
1169 406 $cmd =~ s/^\s+//;
1170 406 ▶ next unless length( $cmd );
Branch: 1 branch
BranchTrueFalseTotal
unless length $cmd 46 360 406
1171 360 my $scom = $icom;
1172 360 ▶ if( exists( $Defined{$icom} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Defined{$icom}) { } 76 284 360
1173 76 $pdef = $Defined{$icom};
1174 76 ▶ if( $pdef =~ /^ #(\d+)/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($pdef =~ /^ #(\d+)/) { } 0 76 76
1175 0 $pfil = 'expression #';
1176 0 $plin = $1;
1177 } else {
1178 76 $pfil = "$pdef l.";
1179 76 $plin = 1;
1180 }
1181 } else {
1182 284 $plin++;
1183 }
1184 360 my $fl = "$pfil$plin";
1185  
1186 # insert command as comment in generated code
1187 #
1188 360 ▶ $Code .= "# $cmd\n" if $doGenerate;
Branch: 1 branch
BranchTrueFalseTotal
if $doGenerate 180 180 360
1189  
1190 # The Address(es)
1191 #
1192 360 my( $negated, $naddr, $addr1, $addr2 );
1193 360 $naddr = 0;
1194 360 ▶ if( $cmd =~ s/^(\d+)\s*// ){
Branch: 3 branches
BranchTrueFalseTotal
if ($cmd =~ s/^(\d+)\s*//) { } 100 260 360
elsif ($cmd =~ s/^\$\s*//) { } 4 256 260
elsif ($cmd =~ s[^(/)][] or $cmd =~ s/^\\(.)//) { } 26 230 256
1195 100 $addr1 = "$1"; $naddr++;
1196 } elsif( $cmd =~ s/^\$\s*// ){
1197 4 $addr1 = 'eofARGV()'; $naddr++;
1198 ▶ } elsif( $cmd =~ s{^(/)}{} || $cmd =~ s{^\\(.)}{} ){
Condition66%2/3
$cmd =~ s[^(/)][] or $cmd =~ s/^\\(.)//
l!l&&r!l&&!r
260230
MC/DC50%1/2
$cmd =~ s[^(/)][] or $cmd =~ s/^\\(.)//
$cmd =~ s[^(/)][] $cmd =~ s/^\\(.)//
Truth table66%2/3
A or B
A= $cmd =~ s[^(/)][]B= $cmd =~ s/^\\(.)//
ABresult
1X1
011
000
1199 26 my $del = $1;
1200 26 my $regex = stripRegex( $del, \$cmd );
1201 26 ▶ if( defined( $regex ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $regex) { } 26 0 26
1202 26 $addr1 = 'm '.bre2p( $del, $regex, $fl ).'s';
1203 26 $naddr++;
1204 } else {
1205 0 Warn( "malformed regex, 1st address", $fl );
1206 0 $error++;
1207 0 next;
1208 }
1209 }
1210 360 ▶ if( defined( $addr1 ) && $cmd =~ s/,\s*// ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $addr1 and $cmd =~ s/,\s*//) 30 330 360
Condition100%3/3
defined $addr1 and $cmd =~ s/,\s*//
!ll&&!rl&&r
23010030
MC/DC100%2/2
defined $addr1 and $cmd =~ s/,\s*//
defined $addr1 $cmd =~ s/,\s*//
Truth table100%3/3
A and B
A= defined $addr1B= $cmd =~ s/,\s*//
ABresult
0X0
100
111
1211 30 ▶ if( $cmd =~ s/^(\d+)\s*// ){
Branch: 3 branches
BranchTrueFalseTotal
if ($cmd =~ s/^(\d+)\s*//) { } 18 12 30
elsif ($cmd =~ s/^\$\s*//) { } 6 6 12
elsif ($cmd =~ s[^(/)][] or $cmd =~ s/^\\(.)//) { } 6 0 6
1212 18 $addr2 = "$1"; $naddr++;
1213 } elsif( $cmd =~ s/^\$\s*// ){
1214 6 $addr2 = 'eofARGV()'; $naddr++;
1215 ▶ } elsif( $cmd =~ s{^(/)}{} || $cmd =~ s{^\\(.)}{} ){
Condition33%1/3
$cmd =~ s[^(/)][] or $cmd =~ s/^\\(.)//
l!l&&r!l&&!r
600
MC/DC0%0/2
$cmd =~ s[^(/)][] or $cmd =~ s/^\\(.)//
$cmd =~ s[^(/)][] $cmd =~ s/^\\(.)//
Truth table33%1/3
A or B
A= $cmd =~ s[^(/)][]B= $cmd =~ s/^\\(.)//
ABresult
1X1
011
000
1216 6 my $del = $1;
1217 6 my $regex = stripRegex( $del, \$cmd );
1218 6 ▶ if( defined( $regex ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $regex) { } 6 0 6
1219 6 $addr2 = 'm '. bre2p( $del, $regex, $fl ).'s';
1220 6 $naddr++;
1221 } else {
1222 0 Warn( "malformed regex, 2nd address", $fl );
1223 0 $error++;
1224 0 next;
1225 }
1226 } else {
1227 0 Warn( "invalid address after ','", $fl );
1228 0 $error++;
1229 0 next;
1230 }
1231 }
1232  
1233 # address modifier '!'
1234 #
1235 360 $negated = $cmd =~ s/^!\s*//;
1236 360 ▶ if( defined( $addr1 ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $addr1) 130 230 360
1237 130 ▶ print "Parse: addr1=$addr1" if $useDEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $useDEBUG 0 130 130
1238 130 ▶ if( defined( $addr2 ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $addr2) 30 100 130
1239 30 ▶ print ", addr2=$addr2 " if $useDEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $useDEBUG 0 30 30
1240 # both numeric and addr1 > addr2 => eliminate addr2
1241 30 ▶ undef( $addr2 ) if $addr1 =~ /^\d+$/ &&
Branch: 1 branch
BranchTrueFalseTotal
if $addr1 =~ /^\d+$/ and $addr2 =~ /^\d+$/ and $addr1 > $addr2 0 30 30
Condition83%5/6
$addr1 =~ /^\d+$/ and $addr2 =~ /^\d+$/ and $addr1 > $addr2
!ll&&!rl&&r
12180
$addr1 =~ /^\d+$/ and $addr2 =~ /^\d+$/
!ll&&!rl&&r
8418
MC/DC0%0/3
$addr1 =~ /^\d+$/ and $addr2 =~ /^\d+$/ and $addr1 > $addr2
$addr1 =~ /^\d+$/ $addr2 =~ /^\d+$/ $addr1 > $addr2
Truth table75%3/4
A and B and C
A= $addr1 =~ /^\d+$/B= $addr2 =~ /^\d+$/C= $addr1 > $addr2
ABCresult
0XX0
10X0
1100
1111
1242 $addr2 =~ /^\d+$/ && $addr1 > $addr2;
1243 }
1244 }
1245 360 ▶ print 'negated' if $useDEBUG && $negated;
Branch: 1 branch
BranchTrueFalseTotal
if $useDEBUG and $negated 0 360 360
Condition33%1/3
$useDEBUG and $negated
!ll&&!rl&&r
36000
MC/DC0%0/2
$useDEBUG and $negated
$useDEBUG $negated
Truth table33%1/3
A and B
A= $useDEBUGB= $negated
ABresult
0X0
100
111
1246 360 ▶ print " command:$cmd\n" if $useDEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $useDEBUG 0 360 360
1247  
1248 # The Command
1249 #
1250 360 ▶ if( $cmd !~ s/^([:#={}abcdDgGhHilnNpPqrstwxy])\s*// ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmd =~ s/^([:#={}abcdDgGhHilnNpPqrstwxy])\s*//) 0 360 360
1251 0 my $h = substr( $cmd, 0, 1 );
1252 0 Warn( "unknown command '$h'", $fl );
1253 0 $error++;
1254 0 next;
1255 }
1256 360 my $key = $1;
1257  
1258 360 my $tabref = $ComTab{$key};
1259 360 $GenKey{$key} = 1;
1260 360 ▶ if( $naddr > $tabref->[0] ){
Branch: 1 branch
BranchTrueFalseTotal
if ($naddr > $tabref->[0]) 0 360 360
1261 0 Warn( "excess address(es)", $fl );
1262 0 $error++;
1263 0 next;
1264 }
1265  
1266 360 my $arg = '';
1267 360 ▶ if( $tabref->[1] eq 'str' ){
Branch: 4 branches
BranchTrueFalseTotal
if ($tabref->[1] eq 'str') { } 86 274 360
elsif ($tabref->[1] eq 'txt') { } 50 224 274
elsif ($tabref->[1] eq 'sub') { } 92 132 224
elsif ($tabref->[1] eq 'tra') { } 4 128 132
1268 # take remainder - don't care if it is empty
1269 86 $arg = $cmd;
1270 86 $cmd = '';
1271  
1272 } elsif( $tabref->[1] eq 'txt' ){
1273 # multi-line text
1274 50 my $goon = $cmd =~ /(.*)\\$/;
1275 50 ▶ if( length( $1 ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (length $1) 0 50 50
1276 0 Warn( "extra characters after command ($cmd)", $fl );
1277 0 $error++;
1278 }
1279 50 ▶ while( $goon ){
Branch: 1 branch
BranchTrueFalseTotal
if ($goon) 58 50 108
1280 58 $icom++;
1281 58 ▶ if( $icom > $#Commands ){
Branch: 1 branch
BranchTrueFalseTotal
if ($icom > $#Commands) 0 58 58
1282 0 Warn( "unexpected end of script", $fl );
1283 0 $error++;
1284 0 last;
1285 }
1286 58 $cmd = $Commands[$icom];
1287 58 ▶ $Code .= "# $cmd\n" if $doGenerate;
Branch: 1 branch
BranchTrueFalseTotal
if $doGenerate 29 29 58
1288 58 $goon = $cmd =~ s/\\$//;
1289 58 $cmd =~ s/\\(.)/$1/g;
1290 58 ▶ $arg .= "\n" if length( $arg );
Branch: 1 branch
BranchTrueFalseTotal
if length $arg 8 50 58
1291 58 $arg .= $cmd;
1292 }
1293 50 ▶ $arg .= "\n" if length( $arg );
Branch: 1 branch
BranchTrueFalseTotal
if length $arg 50 0 50
1294 50 $cmd = '';
1295  
1296 } elsif( $tabref->[1] eq 'sub' ){
1297 # s///
1298 92 ▶ if( ! length( $cmd ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (length $cmd) 0 92 92
1299 0 Warn( "'s' command requires argument", $fl );
1300 0 $error++;
1301 0 next;
1302 }
1303 92 ▶ if( $cmd =~ s{^([^\\\n])}{} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ s/^([^\\\n])//) { } 92 0 92
1304 92 my $del = $1;
1305 92 my $regex = stripRegex( $del, \$cmd, "s" );
1306 92 ▶ if( ! defined( $regex ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $regex) 0 92 92
1307 0 Warn( "malformed regular expression", $fl );
1308 0 $error++;
1309 0 next;
1310 }
1311 92 $regex = bre2p( $del, $regex, $fl );
1312  
1313 # a trailing \ indicates embedded NL (in replacement string)
1314 92 ▶ while( $cmd =~ s/(?<!\\)\\$/\n/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ s/(?<!\\)\\$/\n/) 0 92 92
1315 0 $icom++;
1316 0 ▶ if( $icom > $#Commands ){
Branch: 1 branch
BranchTrueFalseTotal
if ($icom > $#Commands) 0 0 0
1317 0 Warn( "unexpected end of script", $fl );
1318 0 $error++;
1319 0 last;
1320 }
1321 0 $cmd .= $Commands[$icom];
1322 0 ▶ $Code .= "# $Commands[$icom]\n" if $doGenerate;
Branch: 1 branch
BranchTrueFalseTotal
if $doGenerate 0 0 0
1323 }
1324  
1325 92 my $subst = stripRegex( $del, \$cmd );
1326 92 ▶ if( ! defined( $regex ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $regex) 0 92 92
1327 0 Warn( "malformed substitution expression", $fl );
1328 0 $error++;
1329 0 next;
1330 }
1331 92 $subst = sub2p( $del, $subst, $fl );
1332  
1333 # parse s/// modifier: g|p|0-9|w <file>
1334 92 my( $global, $nmatch, $print, $write ) =
1335 ( '', '', 0, undef );
1336 92 ▶ while( $cmd =~ s/^([gp0-9])// ){
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ s/^([gp0-9])//) 16 92 108
1337 16 ▶ $1 eq 'g' ? ( $global = 'g' ) :
Branch: 2 branches
BranchTrueFalseTotal
$1 eq 'g' ? : 12 4 16
$1 eq 'p' ? : 4 0 4
1338 $1 eq 'p' ? ( $print = $1 ) : ( $nmatch .= $1 );
1339 }
1340 92 ▶ $write = $1 if $cmd =~ s/w\s*(.*)$//;
Branch: 1 branch
BranchTrueFalseTotal
if $cmd =~ s/w\s*(.*)$// 0 92 92
1341 ### $nmatch =~ s/^(\d)\1*$/$1/; ### may be dangerous?
1342 92 ▶ if( $global && length( $nmatch ) || length( $nmatch ) > 1 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($global and length $nmatch or length $nmatch > 1) 0 92 92
Condition50%3/6
$global and length $nmatch or length $nmatch > 1
l!l&&r!l&&!r
0092
$global and length $nmatch
!ll&&!rl&&r
80120
MC/DC0%0/3
$global and length $nmatch or length $nmatch > 1
$global length $nmatch length $nmatch > 1
Truth table40%2/5
A and B or C
A= $globalB= length $nmatchC= length $nmatch > 1
ABCresult
11X1
0X11
1011
0X00
1000
1343 0 Warn( "conflicting flags '$global$nmatch'", $fl );
1344 0 $error++;
1345 0 next;
1346 }
1347  
1348 92 $arg = makes( $regex, $subst,
1349 $write, $global, $print, $nmatch, $fl );
1350 92 ▶ if( ! defined( $arg ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $arg) 0 92 92
1351 0 $error++;
1352 0 next;
1353 }
1354  
1355 } else {
1356 0 Warn( "improper delimiter in s command", $fl );
1357 0 $error++;
1358 0 next;
1359 }
1360  
1361 } elsif( $tabref->[1] eq 'tra' ){
1362 # y///
1363 # a trailing \ indicates embedded newline
1364 4 ▶ while( $cmd =~ s/(?<!\\)\\$/\n/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ s/(?<!\\)\\$/\n/) 2 4 6
1365 2 $icom++;
1366 2 ▶ if( $icom > $#Commands ){
Branch: 1 branch
BranchTrueFalseTotal
if ($icom > $#Commands) 0 2 2
1367 0 Warn( "unexpected end of script", $fl );
1368 0 $error++;
1369 0 last;
1370 }
1371 2 $cmd .= $Commands[$icom];
1372 2 ▶ $Code .= "# $Commands[$icom]\n" if $doGenerate;
Branch: 1 branch
BranchTrueFalseTotal
if $doGenerate 1 1 2
1373 }
1374 4 ▶ if( ! length( $cmd ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (length $cmd) 0 4 4
1375 0 Warn( "'y' command requires argument", $fl );
1376 0 $error++;
1377 0 next;
1378 }
1379 4 my $d = substr( $cmd, 0, 1 ); $cmd = substr( $cmd, 1 );
1380 4 ▶ if( $d eq '\\' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($d eq "\\") 0 4 4
1381 0 Warn( "'\\' not valid as delimiter in 'y' command", $fl );
1382 0 $error++;
1383 0 next;
1384 }
1385 4 my $fr = stripTrans( $d, \$cmd );
1386 4 ▶ if( ! defined( $fr ) || ! length( $cmd ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fr and length $cmd) 0 4 4
Condition33%1/3
defined $fr and length $cmd
!ll&&!rl&&r
004
MC/DC0%0/2
defined $fr and length $cmd
defined $fr length $cmd
Truth table33%1/3
A and B
A= defined $frB= length $cmd
ABresult
0X0
100
111
1387 0 Warn( "malformed 'y' command argument", $fl );
1388 0 $error++;
1389 0 next;
1390 }
1391 4 my $to = stripTrans( $d, \$cmd );
1392 4 ▶ if( ! defined( $to ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $to) 0 4 4
1393 0 Warn( "malformed 'y' command argument", $fl );
1394 0 $error++;
1395 0 next;
1396 }
1397 4 ▶ if( length($fr) != length($to) ){
Branch: 1 branch
BranchTrueFalseTotal
if (length $fr != length $to) 0 4 4
1398 0 Warn( "string lengths in 'y' command differ", $fl );
1399 0 $error++;
1400 0 next;
1401 }
1402 4 ▶ if( ! defined( $arg = makey( $fr, $to, $fl ) ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($arg = &makey($fr, $to, $fl))) 0 4 4
1403 0 $error++;
1404 0 next;
1405 }
1406  
1407 }
1408  
1409 # $cmd must be now empty - exception is {
1410 360 ▶ if( $cmd !~ /^\s*$/ ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmd =~ /^\s*$/) 14 346 360
1411 14 ▶ if( $key eq '{' ){
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq '{') { } 0 14 14
elsif ($cmd =~ s/^\s*;\s*//) { } 14 0 14
1412 # dirty hack to process command on '{' line
1413 0 $Commands[$icom--] = $cmd;
1414  
1415 } elsif( $cmd =~ s/^\s*;\s*// ) {
1416 # Another command follows, so handle that in the next round
1417 # This is somewhat hacky, but unless we either parse the
1418 # commands twice we have no choice but to change the @Commands array
1419 # on the fly as we discover new commands
1420 # This is the same hack as above
1421 14 $Commands[$icom--] = $cmd;
1422  
1423 } else {
1424 0 Warn( "extra characters after command ($cmd)", $fl );
1425 0 $error++;
1426 0 next;
1427 }
1428 }
1429  
1430 # Make Code
1431 #
1432 360 ▶ if( &{$tabref->[2]}( $addr1, $addr2, $negated,
Branch: 1 branch
BranchTrueFalseTotal
if (&{$tabref->[2];}($addr1, $addr2, $negated, $tabref->[3], $arg, $fl)) 0 360 360
1433 $tabref->[3], $arg, $fl ) ){
1434 0 $error++;
1435 }
1436 }
1437  
1438 66 ▶ while( @BlockStack ){
Branch: 1 branch
BranchTrueFalseTotal
if (@BlockStack) 0 66 66
1439 0 my $bl = pop( @BlockStack );
1440 0 Warn( "start of unterminated '{'", $bl );
1441 0 $error++;
1442 }
1443  
1444 66 for my $lab ( keys( %Label ) ){
1445 12 ▶ if( ! exists( $Label{$lab}{defined} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Label{$lab}{"defined"}) 0 12 12
1446 0 for my $used ( @{$Label{$lab}{used}} ){
1447 0 Warn( "undefined label '$lab'", $used );
1448 0 $error++;
1449 }
1450 }
1451 }
1452  
1453 66 ▶ exit( 1 ) if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 66 66
1454 }
1455  
1456  
1457 ##############
1458 #### MAIN ####
1459 ##############
1460  
1461 0 ▶sub usage(){
Subroutine usage: not called CC=1 SCAR=6.9
1462 0 print STDERR "Usage: sed [-an] command [file...]\n";
1463 0 print STDERR " [-an] [-e command] [-f script-file] [file...]\n";
1464 }
1465  
1466 ###################
1467 # Here we go again...
1468 #
1469 66 my $expr = 0;
1470 66 ▶while( @ARGV && $ARGV[0] =~ /^-(.)(.*)$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV and $ARGV[0] =~ /^-(.)(.*)$/) 66 66 132
Condition100%3/3
@ARGV and $ARGV[0] =~ /^-(.)(.*)$/
!ll&&!rl&&r
333366
MC/DC100%2/2
@ARGV and $ARGV[0] =~ /^-(.)(.*)$/
@ARGV $ARGV[0] =~ /^-(.)(.*)$/
Truth table100%3/3
A and B
A= @ARGVB= $ARGV[0] =~ /^-(.)(.*)$/
ABresult
0X0
100
111
1471 66 my $opt = $1;
1472 66 my $arg = $2;
1473 66 shift( @ARGV );
1474 66 ▶ if( $opt eq 'e' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($opt eq "e") 0 66 66
1475 0 ▶ if( length( $arg ) ){
Branch: 2 branches
BranchTrueFalseTotal
if (length $arg) { } 0 0 0
elsif (@ARGV) { } 0 0 0
1476 0 push( @Commands, split( "\n", $arg ) );
1477 } elsif( @ARGV ){
1478 0 push( @Commands, shift( @ARGV ) );
1479 } else {
1480 0 Warn( "option -e requires an argument" );
1481 0 usage();
1482 0 exit( 1 );
1483 }
1484 0 $expr++;
1485 0 $Defined{$#Commands} = " #$expr";
1486 0 next;
1487 }
1488 66 ▶ if( $opt eq 'f' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($opt eq "f") 66 0 66
1489 66 my $path;
1490 66 ▶ if( length( $arg ) ){
Branch: 2 branches
BranchTrueFalseTotal
if (length $arg) { } 0 66 66
elsif (@ARGV) { } 66 0 66
1491 0 $path = $arg;
1492 } elsif( @ARGV ){
1493 66 $path = shift( @ARGV );
1494 } else {
1495 0 Warn( "option -f requires an argument" );
1496 0 usage();
1497 0 exit( 1 );
1498 }
1499 66 my $fst = $#Commands + 1;
1500 66 ▶ open( SCRIPT, "<$path" ) || die( "$0: $path: could not open ($!)\n" );
Branch: 1 branch
BranchTrueFalseTotal
open SCRIPT, "<$path" or die "$0: ${path}: could not open ($!)\n" 66 0 66
1501 66 my $cmd;
1502 66 ▶ while( defined( $cmd = <SCRIPT> ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($cmd = readline SCRIPT)) 452 66 518
1503 452 chomp( $cmd );
1504 452 push( @Commands, $cmd );
1505 }
1506 66 close( SCRIPT );
1507 66 ▶ if( $#Commands >= $fst ){
Branch: 1 branch
BranchTrueFalseTotal
if ($#Commands >= $fst) 66 0 66
1508 66 $Defined{$fst} = "$path";
1509 }
1510 66 next;
1511 }
1512 0 ▶ if( $opt eq '-' && $arg eq '' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($opt eq "-" and $arg eq "") 0 0 0
Condition0%0/3
$opt eq "-" and $arg eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
$opt eq "-" and $arg eq ""
$opt eq "-" $arg eq ""
Truth table0%0/3
A and B
A= $opt eq "-"B= $arg eq ""
ABresult
0X0
100
111
1513 0 last;
1514 }
1515 0 ▶ if( $opt eq 'h' || $opt eq '?' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($opt eq "h" or $opt eq "?") 0 0 0
Condition0%0/3
$opt eq "h" or $opt eq "?"
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt eq "h" or $opt eq "?"
$opt eq "h" $opt eq "?"
Truth table0%0/3
A or B
A= $opt eq "h"B= $opt eq "?"
ABresult
1X1
011
000
1516 0 usage();
1517 0 exit( 0 );
1518 }
1519 0 ▶ if( $opt eq 'n' ){
Branch: 2 branches
BranchTrueFalseTotal
if ($opt eq 'n') { } 0 0 0
elsif ($opt eq 'a') { } 0 0 0
1520 0 $doAutoPrint = 0;
1521 } elsif( $opt eq 'a' ){
1522 0 $doOpenWrite = 0;
1523 } else {
1524 0 Warn( "illegal option '$opt'" );
1525 0 usage();
1526 0 exit( 1 );
1527 }
1528 0 ▶ if( length( $arg ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (length $arg) 0 0 0
1529 0 unshift( @ARGV, "-$arg" );
1530 }
1531 }
1532  
1533 # A singleton command may be the 1st argument when there are no options.
1534 #
1535 66 ▶if( @Commands == 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if (@Commands == 0) 0 66 66
1536 0 ▶ if( @ARGV == 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV == 0) 0 0 0
1537 0 Warn( "no script command given" );
1538 0 usage();
1539 0 exit( 1 );
1540 }
1541 0 push( @Commands, split( "\n", shift( @ARGV ) ) );
1542 0 $Defined{0} = ' #1';
1543 }
1544  
1545 66 ▶print STDERR "Files: @ARGV\n" if $useDEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $useDEBUG 0 66 66
1546  
1547 # generate leading code
1548 #
1549 66 $Func = <<'[TheEnd]';
1550  
1551 # openARGV: open 1st input file
1552 #
1553 sub openARGV(){
1554 unshift( @ARGV, '-' ) unless @ARGV;
1555 my $file = shift( @ARGV );
1556 open( ARG, "<$file" )
1557 || die( "$0: can't open $file for reading ($!)\n" );
1558 $isEOF = 0;
1559 }
1560  
1561 # getsARGV: Read another input line into argument (default: $_).
1562 # Move on to next input file, and reset EOF flag $isEOF.
1563 sub getsARGV(;\$){
1564 my $argref = @_ ? shift() : \$_;
1565 while( $isEOF || ! defined( $$argref = <ARG> ) ){
1566 close( ARG );
1567 return 0 unless @ARGV;
1568 my $file = shift( @ARGV );
1569 open( ARG, "<$file" )
1570 || die( "$0: can't open $file for reading ($!)\n" );
1571 $isEOF = 0;
1572 }
1573 1;
1574 }
1575  
1576 # eofARGV: end-of-file test
1577 #
1578 sub eofARGV(){
1579 return @ARGV == 0 && ( $isEOF = eof( ARG ) );
1580 }
1581  
1582 # makeHandle: Generates another file handle for some file (given by its path)
1583 # to be written due to a w command or an s command's w flag.
1584 sub makeHandle($){
1585 my( $path ) = @_;
1586 my $handle;
1587 if( ! exists( $wFiles{$path} ) || $wFiles{$path} eq '' ){
1588 $handle = $wFiles{$path} = gensym();
1589 if( $doOpenWrite ){
1590 if( ! open( $handle, ">$path" ) ){
1591 die( "$0: can't open $path for writing: ($!)\n" );
1592 }
1593 }
1594 } else {
1595 $handle = $wFiles{$path};
1596 }
1597 return $handle;
1598 }
1599  
1600 # printQ: Print queued output which is either a string or a reference
1601 # to a pathname.
1602 sub printQ(){
1603 for my $q ( @Q ){
1604 if( ref( $q ) ){
1605 # flush open w files so that reading this file gets it all
1606 if( exists( $wFiles{$$q} ) && $wFiles{$$q} ne '' ){
1607 open( $wFiles{$$q}, ">>$$q" );
1608 }
1609 # copy file to stdout: slow, but safe
1610 if( open( RF, "<$$q" ) ){
1611 while( defined( my $line = <RF> ) ){
1612 print $line;
1613 }
1614 close( RF );
1615 }
1616 } else {
1617 print $q;
1618 }
1619 }
1620 undef( @Q );
1621 }
1622  
1623 [TheEnd]
1624  
1625 # generate the sed loop
1626 #
1627 66 $Code .= <<'[TheEnd]';
1628 sub openARGV();
1629 sub getsARGV(;\$);
1630 sub eofARGV();
1631 sub printQ();
1632  
1633 # Run: the sed loop reading input and applying the script
1634 #
1635 sub Run(){
1636 my( $h, $icnt, $s, $n );
1637 # hack (not unbreakable :-/) to avoid // matching an empty string
1638 my $z = "\000"; $z =~ /$z/;
1639 # Initialize.
1640 openARGV();
1641 $Hold = '';
1642 $CondReg = 0;
1643 $doPrint = $doAutoPrint;
1644 CYCLE:
1645 while( getsARGV() ){
1646 chomp();
1647 $CondReg = 0; # cleared on t
1648 BOS:;
1649 [TheEnd]
1650  
1651 # parse - avoid opening files when doing s2p
1652 #
1653 66 ▶ ( $svOpenWrite, $doOpenWrite ) = ( $doOpenWrite, $svOpenWrite )
Branch: 1 branch
BranchTrueFalseTotal
if $doGenerate 33 33 66
1654 if $doGenerate;
1655 66 Parse();
1656 66 ▶ ( $svOpenWrite, $doOpenWrite ) = ( $doOpenWrite, $svOpenWrite )
Branch: 1 branch
BranchTrueFalseTotal
if $doGenerate 33 33 66
1657 if $doGenerate;
1658  
1659 # append trailing code
1660 #
1661 66 $Code .= <<'[TheEnd]';
1662 EOS: if( $doPrint ){
1663 print $_, "\n";
1664 } else {
1665 $doPrint = $doAutoPrint;
1666 }
1667 printQ() if @Q;
1668 }
1669  
1670 exit( 0 );
1671 }
1672 [TheEnd]
1673  
1674  
1675 # append optional functions, prepend prototypes
1676 #
1677 66 my $Proto = "# prototypes\n";
1678 66 ▶if( $GenKey{'l'} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($GenKey{"l"}) 0 66 66
1679 0 $Proto .= "sub _l();\n";
1680 0 $Func .= <<'[TheEnd]';
1681 # _l: l command processing
1682 #
1683 sub _l(){
1684 my $h = $_;
1685 my $mcpl = 70;
1686 # transform non printing chars into escape notation
1687 $h =~ s/\\/\\\\/g;
1688 if( $h =~ /[^[:print:]]/ ){
1689 $h =~ s/\a/\\a/g;
1690 $h =~ s/\f/\\f/g;
1691 $h =~ s/\n/\\n/g;
1692 $h =~ s/\t/\\t/g;
1693 $h =~ s/\r/\\r/g;
1694 $h =~ s/\e/\\e/g;
1695 $h =~ s/([^[:print:]])/sprintf("\\%03o", ord($1))/ge;
1696 }
1697 # split into lines of length $mcpl
1698 while( length( $h ) > $mcpl ){
1699 my $l = substr( $h, 0, $mcpl-1 );
1700 $h = substr( $h, $mcpl );
1701 # remove incomplete \-escape from end of line
1702 if( $l =~ s/(?<!\\)(\\[0-7]{0,2})$// ){
1703 $h = $1 . $h;
1704 }
1705 print $l, "\\\n";
1706 }
1707 print "$h\$\n";
1708 }
1709  
1710 [TheEnd]
1711 }
1712  
1713 66 ▶if( $GenKey{'r'} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($GenKey{"r"}) 2 64 66
1714 2 $Proto .= "sub _r(\$);\n";
1715 2 $Func .= <<'[TheEnd]';
1716 # _r: r command processing: Save a reference to the pathname.
1717 #
1718 sub _r($){
1719 my $path = shift();
1720 push( @Q, \$path );
1721 }
1722  
1723 [TheEnd]
1724 }
1725  
1726 66 ▶if( $GenKey{'t'} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($GenKey{"t"}) 6 60 66
1727 6 $Proto .= "sub _t();\n";
1728 6 $Func .= <<'[TheEnd]';
1729 # _t: t command - condition register test/reset
1730 #
1731 sub _t(){
1732 my $res = $CondReg;
1733 $CondReg = 0;
1734 $res;
1735 }
1736  
1737 [TheEnd]
1738 }
1739  
1740 66 ▶if( $GenKey{'w'} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($GenKey{"w"}) 2 64 66
1741 2 $Proto .= "sub _w(\$);\n";
1742 2 $Func .= <<'[TheEnd]';
1743 # _w: w command and s command's w flag - write to file
1744 #
1745 sub _w($){
1746 my $path = shift();
1747 my $handle = $wFiles{$path};
1748 if( ! $doOpenWrite && ! defined( fileno( $handle ) ) ){
1749 open( $handle, ">$path" )
1750 || die( "$0: $path: cannot open ($!)\n" );
1751 }
1752 print $handle $_, "\n";
1753 }
1754  
1755 [TheEnd]
1756 }
1757  
1758 66 $Code = $Proto . $Code;
1759  
1760 # magic "#n" - same as -n option
1761 #
1762 66 ▶$doAutoPrint = 0 if substr( $Commands[0], 0, 2 ) eq '#n';
Branch: 1 branch
BranchTrueFalseTotal
if substr($Commands[0], 0, 2) eq "#n" 12 54 66
1763  
1764 # eval code - check for errors
1765 #
1766 66 ▶print "Code:\n$Code$Func" if $useDEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $useDEBUG 0 66 66
1767 66 ▶eval $Code . $Func;
Subroutine Run: called CC=4 SCAR=13.9
Subroutine eofARGV: called CC=2 SCAR=6.9
Subroutine getsARGV: called CC=6 SCAR=17.9
Subroutine makeHandle: called CC=5 SCAR=16.1
Subroutine openARGV: called CC=3 SCAR=11.0
Subroutine printQ: called CC=7 SCAR=19.5
Subroutine _t: called CC=1 SCAR=0.0
Branch: 20 branches
BranchTrueFalseTotal
if (&getsARGV()) 233 31 264
if (1 == $.) 115 120 235
if (&eofARGV()) 34 174 208
if ($App::s2p::doPrint) { } 50 95 145
if @App::s2p::Q 38 96 134
@_ ? : 25 322 347
if ($App::s2p::isEOF or not defined($$argref = readline ARG)) 36 267 303
unless @ARGV 36 38 74
open ARG, "<$file" or die "$0: can't open $file for reading ($!)\n" 8 45 53
if (not exists $App::s2p::wFiles{$path} or $App::s2p::wFiles{$path} eq '') { } 2 28 30
if ($doOpenWrite) 3 4 7
unless (open $handle, ">$path") 1 10 11
unless @ARGV 1 38 39
open ARG, "<$file" or die "$0: can't open $file for reading ($!)\n" 38 0 38
if (ref $q) { } 2 13 15
if (exists $App::s2p::wFiles{$$q} and $App::s2p::wFiles{$$q} ne "") 0 7 7
if (open RF, "<$$q") 2 0 2
if (defined(my $line = readline RF)) 6 2 8
unless ($doOpenWrite or defined fileno $handle) 0 8 8
open $handle, ">$path" or die "$0: ${path}: cannot open ($!)\n" 0 0 0
Condition93%14/15
@ARGV == 0 && ($App::s2p::isEOF = eof ARG)
!ll&&!rl&&r
08213
$App::s2p::isEOF or not defined($$argref = readline ARG)
l!l&&r!l&&!r
3430251
not exists $App::s2p::wFiles{$path} or $App::s2p::wFiles{$path} eq ""
l!l&&r!l&&!r
1221
exists $App::s2p::wFiles{$$q} and $App::s2p::wFiles{$$q} ne ""
!ll&&!rl&&r
1546
$doOpenWrite or defined fileno $handle
l!l&&r!l&&!r
2515
MC/DC50%1/2
@ARGV == 0 && ($App::s2p::isEOF = eof ARG)
@ARGV == 0 ($App::s2p::isEOF = eof ARG)
MC/DC100%2/2
$App::s2p::isEOF or not defined($$argref = readline ARG)
$App::s2p::isEOF not defined($$argref = readline ARG)
MC/DC0%0/2
not exists $App::s2p::wFiles{$path} or $App::s2p::wFiles{$path} eq ""
not exists $App::s2p::wFiles{$path} $App::s2p::wFiles{$path} eq ""
MC/DC0%0/2
exists $App::s2p::wFiles{$$q} and $App::s2p::wFiles{$$q} ne ""
exists $App::s2p::wFiles{$$q} $App::s2p::wFiles{$$q} ne ""
MC/DC0%0/2
$doOpenWrite or defined fileno $handle
$doOpenWrite defined fileno $handle
Truth table66%2/3
A && B
A= @ARGV == 0B= ($App::s2p::isEOF = eof ARG)
ABresult
0X0
100
111
Truth table100%3/3
A or not(B)
A= $App::s2p::isEOFB= not defined($$argref = readline ARG)
ABresult
1X1
011
000
Truth table33%1/3
not(A) or B
A= not exists $App::s2p::wFiles{$path}B= $App::s2p::wFiles{$path} eq ""
ABresult
1X1
011
000
Truth table33%1/3
A and B
A= exists $App::s2p::wFiles{$$q}B= $App::s2p::wFiles{$$q} ne ""
ABresult
0X0
100
111
Truth table33%1/3
A or B
A= $doOpenWriteB= defined fileno $handle
ABresult
1X1
011
000
1768 66 ▶if( $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 66 66
1769 0 print "Code:\n$Code$Func";
1770 0 die( "$0: internal error - generated incorrect Perl code: $@\n" );
1771 }
1772  
1773 66 ▶if( $doGenerate ){
Branch: 1 branch
BranchTrueFalseTotal
if ($doGenerate) { } 33 33 66
1774  
1775 # write full Perl program
1776 #
1777  
1778 # bang line, declarations, prototypes
1779 33 print <<TheEnd;
1780 #!$Config{perlpath} -w
1781 eval 'exec $Config{perlpath} -S \$0 \${1+"\$@"}'
1782 if 0;
1783 \$0 =~ s/^.*?(\\w+)\[\\.\\w+\]*\$/\$1/;
1784  
1785 use strict;
1786 use Symbol;
1787 use vars qw{ \$isEOF \$Hold \%wFiles \@Q \$CondReg
1788 \$doAutoPrint \$doOpenWrite \$doPrint };
1789 \$doAutoPrint = $doAutoPrint;
1790 \$doOpenWrite = $doOpenWrite;
1791 TheEnd
1792  
1793 33 my $wf = "'" . join( "', '", keys( %wFiles ) ) . "'";
1794 33 ▶ if( $wf ne "''" ){
Branch: 1 branch
BranchTrueFalseTotal
if ($wf ne "''") 1 32 33
1795 1 print <<TheEnd;
1796 sub makeHandle(\$);
1797 for my \$p ( $wf ){
1798 exit( 1 ) unless makeHandle( \$p );
1799 }
1800 TheEnd
1801 }
1802  
1803 33 print $Code;
1804 33 print "Run();\n";
1805 33 print $Func;
1806 33 exit( 0 );
1807  
1808 } else {
1809  
1810 # execute: make handles (and optionally open) all w files; run!
1811 33 for my $p ( keys( %wFiles ) ){
1812 1 ▶ exit( 1 ) unless makeHandle( $p );
Branch: 1 branch
BranchTrueFalseTotal
unless &makeHandle($p) 0 1 1
1813 }
1814 33 Run();
1815 }
1816  
1817  
1818 =head1 ENVIRONMENT
1819  
1820 The environment variable C<PSEDEXTBRE> may be set to extend BREs.
1821 See L<"Additional Atoms">.
1822  
1823 =head1 DIAGNOSTICS
1824  
1825 =over 4
1826  
1827 =item ambiguous translation for character '%s' in 'y' command
1828  
1829 The indicated character appears twice, with different translations.
1830  
1831 =item '[' cannot be last in pattern
1832  
1833 A '[' in a BRE indicates the beginning of a I<bracket expression>.
1834  
1835 =item '\' cannot be last in pattern
1836  
1837 A '\' in a BRE is used to make the subsequent character literal.
1838  
1839 =item '\' cannot be last in substitution
1840  
1841 A '\' in a substitution string is used to make the subsequent character literal.
1842  
1843 =item conflicting flags '%s'
1844  
1845 In an B<s> command, either the 'g' flag and an n-th occurrence flag, or
1846 multiple n-th occurrence flags are specified. Note that only the digits
1847 ^'1' through '9' are permitted.
1848  
1849 =item duplicate label %s (first defined at %s)
1850  
1851 =item excess address(es)
1852  
1853 The command has more than the permitted number of addresses.
1854  
1855 =item extra characters after command (%s)
1856  
1857 =item illegal option '%s'
1858  
1859 =item improper delimiter in s command
1860  
1861 The BRE and substitution may not be delimited with '\' or newline.
1862  
1863 =item invalid address after ','
1864  
1865 =item invalid backreference (%s)
1866  
1867 The specified backreference number exceeds the number of backreferences
1868 in the BRE.
1869  
1870 =item invalid repeat clause '\{%s\}'
1871  
1872 The repeat clause does not contain a valid integer value, or pair of
1873 values.
1874  
1875 =item malformed regex, 1st address
1876  
1877 =item malformed regex, 2nd address
1878  
1879 =item malformed regular expression
1880  
1881 =item malformed substitution expression
1882  
1883 =item malformed 'y' command argument
1884  
1885 The first or second string of a B<y> command is syntactically incorrect.
1886  
1887 =item maximum less than minimum in '\{%s\}'
1888  
1889 =item no script command given
1890  
1891 There must be at least one B<-e> or one B<-f> option specifying a
1892 script or script file.
1893  
1894 =item '\' not valid as delimiter in 'y' command
1895  
1896 =item option -e requires an argument
1897  
1898 =item option -f requires an argument
1899  
1900 =item 's' command requires argument
1901  
1902 =item start of unterminated '{'
1903  
1904 =item string lengths in 'y' command differ
1905  
1906 The translation table strings in a B<y> command must have equal lengths.
1907  
1908 =item undefined label '%s'
1909  
1910 =item unexpected '}'
1911  
1912 A B<}> command without a preceding B<{> command was encountered.
1913  
1914 =item unexpected end of script
1915  
1916 The end of the script was reached although a text line after a
1917 B<a>, B<c> or B<i> command indicated another line.
1918  
1919 =item unknown command '%s'
1920  
1921 =item unterminated '['
1922  
1923 A BRE contains an unterminated bracket expression.
1924  
1925 =item unterminated '\('
1926  
1927 A BRE contains an unterminated backreference.
1928  
1929 =item '\{' without closing '\}'
1930  
1931 A BRE contains an unterminated bounds specification.
1932  
1933 =item '\)' without preceding '\('
1934  
1935 =item 'y' command requires argument
1936  
1937 =back
1938  
1939 =head1 EXAMPLE
1940  
1941 The basic material for the preceding section was generated by running
1942 the sed script
1943  
1944 #no autoprint
1945 s/^.*Warn( *"\([^"]*\)".*$/\1/
1946 t process
1947 b
1948 :process
1949 s/$!/%s/g
1950 s/$[_[:alnum:]]\{1,\}/%s/g
1951 s/\\\\/\\/g
1952 s/^/=item /
1953 p
1954  
1955 on the program's own text, and piping the output into C<sort -u>.
1956  
1957  
1958 =head1 SED SCRIPT TRANSLATION
1959  
1960 If this program is invoked with the name F<s2p> it will act as a
1961 sed-to-Perl translator. After option processing (all other
1962 arguments are ignored), a Perl program is printed on standard
1963 output, which will process the input stream (as read from all
1964 arguments) in the way defined by the sed script and the option setting
1965 used for the translation.
1966  
1967 =head1 SEE ALSO
1968  
1969 perl(1), re_format(7)
1970  
1971 =head1 BUGS
1972  
1973 The B<l> command will show escape characters (ESC) as 'C<\e>', but
1974 a vertical tab (VT) in octal.
1975  
1976 Trailing spaces are truncated from labels in B<:>, B<t> and B<b> commands.
1977  
1978 The meaning of an empty regular expression ('C<//>'), as defined by B<sed>,
1979 is "the last pattern used, at run time". This deviates from the Perl
1980 interpretation, which will re-use the "last successfully executed
1981 regular expression". Since keeping track of pattern usage would create
1982 terribly cluttered code, and differences would only appear in obscure
1983 context (where other B<sed> implementations appear to deviate, too),
1984 the Perl semantics was adopted. Note that common usage of this feature,
1985 such as in C</abc/s//xyz/>, will work as expected.
1986  
1987 Collating elements (of bracket expressions in BREs) are not implemented.
1988  
1989 =head1 STANDARDS
1990  
1991 This B<sed> implementation conforms to the IEEE Std1003.2-1992 ("POSIX.2")
1992 definition of B<sed>, and is compatible with the I<OpenBSD>
1993 implementation, except where otherwise noted (see L<"BUGS">).
1994  
1995 =head1 AUTHOR
1996  
1997 This Perl implementation of I<sed> was written by Wolfgang Laun,
1998 I<Wolfgang.Laun@alcatel.at>.
1999  
2000 =head1 COPYRIGHT and LICENSE
2001  
2002 This program is free and open software. You may use, modify,
2003 distribute, and sell this program (and any modified variants) in any
2004 way you wish, provided you do not restrict others from doing the same.
2005  
2006 =cut
↑ Summary