bin/nl

filter: Statementstmt 77.2% 105 / 136 Branchbran 55.7% 39 / 70 Conditioncond 36.3% 4 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 91.6% 11 / 12 Podpod n/a totaltotal 67.6% 159 / 235 CC 20 SCAR 23.5
CC
20
Cov
68%
print_line
18.2
do_file
13.5
VERSION_MESSAGE
2.0
CRAP 10.5
« bin/cut ↑ Summary bin/rm »
1 #!/usr/bin/env perl
2 #
3 # nl - line numbering filter
4 #
5 # 2020.10.25 v1.00 jul : first public release
6  
7 =begin metadata
8  
9 Name: nl
10 Description: line numbering filter
11 Author: jul, kaldor@cpan.org
12 License: artistic2
13  
14 =end metadata
15  
16 =cut
17  
18 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use Getopt::Std qw(getopts);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Pod::Usage qw(pod2usage);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 ▶use constant EX_SUCCESS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use constant EX_FAILURE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 our $VERSION = '1.3';
30 1 my $program = basename($0);
31  
32 # options
33 1 $Getopt::Std::STANDARD_HELP_VERSION = 1;
34 1 my %options = ();
35 1 ▶getopts('b:d:f:h:i:n:ps:v:w:', \%options) or pod2usage(EX_FAILURE);
Branch: 1 branch
BranchTrueFalseTotal
&getopts("b:d:f:h:i:n:ps:v:w:", \%options) or pod2usage(1) 1 0 1
36 1 my $file = shift;
37 1 ▶$file = '-' unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 1 1
38 1 ▶pod2usage(EX_FAILURE) if @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV 0 1 1
39  
40 1 ▶my $type_b = $options{b} || "t";
Condition50%1/2
$options{'b'} || 't'
l!l
10
MC/DC0%0/1
$options{'b'} || 't'
$options{'b'}
Truth table50%1/2
A
A= $options{'b'}
Aresult
11
00
41 1 ▶my $delim = $options{d} || '\:';
Condition50%1/2
$options{'d'} || '\\:'
l!l
01
MC/DC0%0/1
$options{'d'} || '\\:'
$options{'d'}
Truth table50%1/2
A
A= $options{'d'}
Aresult
11
00
42 1 ▶my $type_f = $options{f} || "n";
Condition50%1/2
$options{'f'} || 'n'
l!l
01
MC/DC0%0/1
$options{'f'} || 'n'
$options{'f'}
Truth table50%1/2
A
A= $options{'f'}
Aresult
11
00
43 1 ▶my $type_h = $options{h} || "n";
Condition50%1/2
$options{'h'} || 'n'
l!l
01
MC/DC0%0/1
$options{'h'} || 'n'
$options{'h'}
Truth table50%1/2
A
A= $options{'h'}
Aresult
11
00
44 1 my $incr = $options{i};
45 1 my $format = $options{n};
46 1 my $single_page = $options{p};
47 1 ▶my $sep = exists $options{'s'} ? $options{'s'} : "\t";
Branch: 1 branch
BranchTrueFalseTotal
exists $options{'s'} ? : 1 0 1
48 1 my $startnum = $options{v};
49 1 my $width = $options{w};
50 1 ▶if (defined $format) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $format) { } 1 0 1
51 1 my %expect = (
52 'ln' => 1,
53 'rn' => 1,
54 'rz' => 1,
55 );
56 1 ▶ unless ($expect{$format}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($expect{$format}) 0 1 1
57 0 warn "$program: invalid line number format: '$format'\n";
58 0 exit EX_FAILURE;
59 }
60 }
61 else {
62 0 $format = 'rn';
63 }
64  
65 1 ▶if (defined $width) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $width) { } 0 1 1
66 0 ▶ if ($width !~ m/\A\+?[0-9]+\Z/ || $width == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $width =~ /\A\+?[0-9]+\Z/ or $width == 0) 0 0 0
Condition0%0/3
not $width =~ /\A\+?[0-9]+\Z/ or $width == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not $width =~ /\A\+?[0-9]+\Z/ or $width == 0
not $width =~ /\A\+?[0-9]+\Z/ $width == 0
Truth table0%0/3
not(A) or B
A= not $width =~ /\A\+?[0-9]+\Z/B= $width == 0
ABresult
1X1
011
000
67 0 warn "$program: invalid line number field width: '$width'\n";
68 0 exit EX_FAILURE;
69 }
70 0 $width = int $width; # strip '+'
71 }
72 else {
73 1 $width = 6;
74 }
75  
76 1 ▶if (defined $startnum) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $startnum) { } 1 0 1
77 1 ▶ if ($startnum !~ m/\A[\+\-]?[0-9]+\Z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($startnum =~ /\A[\+\-]?[0-9]+\Z/) 0 1 1
78 0 warn "$program: invalid starting line number: '$startnum'\n";
79 0 exit EX_FAILURE;
80 }
81 }
82 else {
83 0 $startnum = 1;
84 }
85  
86 1 ▶if (defined $incr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $incr) { } 0 1 1
87 0 ▶ if ($incr !~ m/\A[\+\-]?[0-9]+\Z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($incr =~ /\A[\+\-]?[0-9]+\Z/) 0 0 0
88 0 warn "$program: invalid line number increment: '$incr'\n";
89 0 exit EX_FAILURE;
90 }
91 }
92 else {
93 1 $incr = 1;
94 }
95  
96 0 ▶sub VERSION_MESSAGE {
Subroutine VERSION_MESSAGE: not called CC=1 SCAR=6.9
97 0 print "$program version $VERSION\n";
98 0 exit EX_SUCCESS;
99 }
100  
101 # options -b -f -h
102 1 my $regex_b = "";
103 1 my $regex_f = "";
104 1 my $regex_h = "";
105  
106 1 ($type_b, $regex_b) = split //, $type_b, 2;
107 1 ($type_f, $regex_f) = split //, $type_f, 2;
108 1 ($type_h, $regex_h) = split //, $type_h, 2;
109  
110 1 my @type = ($type_h, $type_b, $type_f,); # don't change order
111 1 for (@type) {
112 3 my %expect = (
113 'a' => 1,
114 't' => 1,
115 'n' => 1,
116 'p' => 1,
117 'e' => 1,
118 );
119 3 ▶ unless ($expect{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($expect{$_}) 0 3 3
120 0 warn "$program: invalid numbering style: '$_'\n";
121 0 pod2usage(EX_FAILURE);
122 }
123 }
124  
125 1 my @regex = ($regex_h, $regex_b, $regex_f); # don't change order
126  
127 # options -d
128 1 my $delim_std = '\:';
129 1 substr($delim_std, 0, length($delim), $delim);
130 1 $delim = quotemeta(substr($delim_std, 0, 2)); # max 2 chars, backslash escaped
131  
132 # options -n -w
133 1 my $format_str = '%';
134 1 ▶$format_str .= '-' if $format eq "ln";
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "ln" 0 1 1
135 1 ▶$format_str .= '0' if $format eq "rz";
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "rz" 1 0 1
136 1 $format_str .= $width;
137 1 $format_str .= 'd';
138  
139 # options -v
140 1 my $number = $startnum;
141  
142 1 my $section = 1;
143 1 my $new_section = 1;
144  
145 1 ▶exit (do_file($file) ? EX_FAILURE : EX_SUCCESS);
Branch: 1 branch
BranchTrueFalseTotal
do_file($file) ? : 0 1 1
146  
147 ###############
148 # SUBROUTINES #
149 ###############
150  
151 7 ▶sub print_number {
Subroutine print_number: called CC=2 SCAR=6.9
152  
153 7 my $match = shift;
154  
155 7 ▶ if ($match)
Branch: 1 branch
BranchTrueFalseTotal
if ($match) { } 4 3 7
156 {
157 4 printf($format_str, $number);
158 4 $number += $incr;
159 }
160 else
161 {
162 3 print ' ' x $width;
163 }
164  
165 7 print $sep;
166 }
167  
168 7 ▶sub print_line {
Subroutine print_line: called CC=9 SCAR=29.0
169  
170 7 my $line = shift;
171 7 my $type = shift;
172 7 my $regex = shift;
173  
174 7 ▶ if ($type eq 'a')
Branch: 5 branches
BranchTrueFalseTotal
if ($type eq 'a') { } 0 7 7
elsif ($type eq 't') { } 0 7 7
elsif ($type eq 'n') { } 2 5 7
elsif ($type eq 'p') { } 0 5 5
elsif ($type eq 'e') { } 5 0 5
175 {
176 0 print_number(1);
177 }
178 elsif ($type eq 't')
179 {
180 0 ▶ my $match = $line =~ /\A\Z/ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /\A\Z/ ? : 0 0 0
181 0 print_number($match);
182 }
183 elsif ($type eq 'n')
184 {
185 2 print_number(0);
186 }
187 elsif ($type eq 'p')
188 {
189 0 ▶ my $match = $line =~ /$regex/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /$regex/ ? : 0 0 0
190 0 print_number($match);
191 }
192 elsif ($type eq 'e')
193 {
194 5 ▶ my $match = $line =~ /$regex/ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /$regex/ ? : 1 4 5
195 5 print_number($match);
196 }
197 else
198 {
199 0 warn "$program: invalid type '$type'\n";
200 0 pod2usage(EX_FAILURE);
201 }
202  
203 7 print $line;
204 }
205  
206 1 ▶sub do_file {
Subroutine do_file: called CC=11 SCAR=26.0
207 1 my $name = shift;
208 1 my ($fh, $line);
209  
210 1 ▶ if ($name eq '-')
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq '-') { } 0 1 1
211 {
212 0 $fh = *STDIN;
213 }
214 else
215 {
216 1 ▶ if (-d $name)
Branch: 1 branch
BranchTrueFalseTotal
if (-d $name) 0 1 1
217 {
218 0 warn "$program: '$name': is a directory\n";
219 0 return 1;
220 }
221 1 ▶ unless (open $fh, '<', $name)
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<", $name) 0 1 1
222 {
223 0 warn "$program: '$name': $!\n";
224 0 return 1;
225 }
226 }
227  
228 1 ▶ while ($line = <$fh>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 10 1 11
229 {
230 10 ▶ if ($line =~ /^($delim)($delim)?($delim)?$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^($delim)($delim)?($delim)?$/) { } 3 7 10
231 {
232 3 ▶ if ($3) {$new_section = 0} # header
Branch: 2 branches
BranchTrueFalseTotal
if ($3) { } 1 2 3
elsif ($2) { } 1 1 2
233 1 elsif ($2) {$new_section = 1} # body
234 1 else {$new_section = 2} # footer
235  
236 # change page
237 3 ▶ if ($new_section <= $section)
Branch: 1 branch
BranchTrueFalseTotal
if ($new_section <= $section) 1 2 3
238 {
239 1 ▶ $number = $startnum unless $single_page;
Branch: 1 branch
BranchTrueFalseTotal
unless $single_page 0 1 1
240 }
241  
242 3 $section = $new_section;
243 }
244 else
245 {
246 7 print_line($line, $type[$section], $regex[$section]);
247 }
248 }
249 1 ▶ unless (close $fh)
Branch: 1 branch
BranchTrueFalseTotal
unless (close $fh) 0 1 1
250 {
251 0 warn "$program: cannot close '$name': $!\n";
252 0 return 1;
253 }
254  
255 1 return 0;
256 }
257  
258 __END__
« bin/cut ↑ Summary bin/rm »