bin/cat

filter: Statementstmt 66.2% 53 / 80 Branchbran 47.0% 16 / 34 Conditioncond 29.1% 7 / 24 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 77.7% 7 / 9 Podpod n/a totaltotal 52.1% 84 / 161 CC 17 SCAR 29.6
CC
17
Cov
55%
cook_file
36.5
do_file
10.5
raw_file
6.0
CRAP 19.3
« bin/units ↑ Summary bin/date »
1 #!/usr/bin/perl
2  
3 =begin metadata
4  
5 Name: cat
6 Description: concatenate and print files
7 Author: Abigail, perlpowertools@abigail.be
8 License: perl
9  
10 =end metadata
11  
12 =cut
13  
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Getopt::Std qw(getopts);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use constant EX_SUCCESS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use constant EX_FAILURE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 our $VERSION = '1.3';
23 1 my $Program = basename($0);
24  
25 1 my %opt;
26 1 ▶getopts('benstuv', \%opt) or do {
Branch: 1 branch
BranchTrueFalseTotal
&getopts("benstuv", \%opt) or do { warn "usage: $Program [-benstuv] [file ...]\n"; exit 1 } 1 0 1
27 0 warn "usage: $Program [-benstuv] [file ...]\n";
28 0 exit EX_FAILURE;
29 };
30  
31 1 my $ends = $opt{'e'};
32 1 my $tabs = $opt{'t'};
33 1 ▶my $nonprinting = $opt{'v'} || $ends || $tabs;
Condition33%2/6
$opt{'v'} || $ends || $tabs
l!l&&r!l&&!r
001
$opt{"v"} or $ends
l!l&&r!l&&!r
001
MC/DC0%0/3
$opt{'v'} || $ends || $tabs
$opt{"v"} $ends $tabs
Truth table25%1/4
A or B || C
A= $opt{"v"}B= $endsC= $tabs
ABCresult
1XX1
01X1
0011
0000
34 1 my $squeeze_empty = $opt{'s'};
35 1 ▶my $number_lines = $opt{'n'} && !$opt{'b'};
Condition33%1/3
$opt{'n'} && !$opt{'b'}
!ll&&!rl&&r
001
MC/DC0%0/2
$opt{'n'} && !$opt{'b'}
$opt{'n'} !$opt{'b'}
Truth table33%1/3
A && not(B)
A= $opt{'n'}B= !$opt{'b'}
ABresult
0X0
100
111
36 1 my $number_non_blanks = $opt{'b'};
37  
38 1 ▶my $cook = grep {$_ || ()} @opt{'b', 'e', 'n', 's', 't', 'v'};
Condition66%2/3
$_ || ()
l!l&&r!l&&!r
105
MC/DC50%1/2
$_ || ()
$_ ()
Truth table66%2/3
A || B
A= $_B= ()
ABresult
1X1
011
000
39 1 ▶my $cat = $cook ? \&cook_file : \&raw_file;
Branch: 1 branch
BranchTrueFalseTotal
$cook ? : 1 0 1
40  
41 # Unbuffer output for -u.
42 1 ▶$| = 1 if $opt{'u'};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"u"} 0 1 1
43  
44 1 my $was_empty = 0;
45 1 my $count = 0;
46  
47 1 ▶if (scalar(@ARGV) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @ARGV == 0) 0 1 1
48 0 push @ARGV, '-';
49 }
50 1 my $err = 0;
51 1 foreach my $f (@ARGV) {
52 1 $err |= do_file($f);
53 }
54 1 ▶exit ($err ? EX_FAILURE : EX_SUCCESS);
Branch: 1 branch
BranchTrueFalseTotal
$err ? : 0 1 1
55  
56 0 ▶sub VERSION_MESSAGE {
Subroutine VERSION_MESSAGE: not called CC=1 SCAR=6.9
57 0 print "$Program version $VERSION\n";
58 0 exit EX_SUCCESS;
59 }
60  
61 1 ▶sub do_file {
Subroutine do_file: called CC=6 SCAR=23.5
62 1 my $name = shift;
63  
64 1 my $fh;
65 1 ▶ if ($name eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq '-') { } 0 1 1
66 0 $fh = *STDIN;
67 } else {
68 1 ▶ if (-d $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $name) 0 1 1
69 0 warn "$Program: $name: is a directory\n";
70 0 return 1;
71 }
72 1 ▶ if (!open($fh, '<', $name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<", $name) 0 1 1
73 0 warn "$Program: $name: $!\n";
74 0 return 1;
75 }
76 }
77 1 $cat->($fh);
78  
79 1 ▶ if ($name ne '-' && !close($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name ne "-" and not close $fh) 0 1 1
Condition33%1/3
$name ne "-" and not close $fh
!ll&&!rl&&r
010
MC/DC0%0/2
$name ne "-" and not close $fh
$name ne "-" not close $fh
Truth table33%1/3
A and not(B)
A= $name ne "-"B= not close $fh
ABresult
0X0
100
111
80 0 warn "$Program: failed to close '$name': $!\n";
81 0 return 1;
82 }
83 1 return 0;
84 }
85  
86 0 ▶sub raw_file {
Subroutine raw_file: not called CC=2 SCAR=17.9
87 0 my $fh = shift;
88 0 my $BUFSZ = 8192;
89 0 my $buf;
90 0 ▶ while (read $fh, $buf, $BUFSZ) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $fh, $buf, $BUFSZ) 0 0 0
91 0 print $buf;
92 }
93 }
94  
95 1 ▶sub cook_file {
Subroutine cook_file: called CC=11 SCAR=36.0
96 1 my $fh = shift;
97  
98 1 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 1 1 2
99 1 ▶ if ($squeeze_empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($squeeze_empty) 0 1 1
100 0 my $is_empty = /^$/;
101 0 ▶ if ($is_empty && $was_empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_empty and $was_empty) 0 0 0
Condition0%0/3
$is_empty and $was_empty
!ll&&!rl&&r
000
MC/DC0%0/2
$is_empty and $was_empty
$is_empty $was_empty
Truth table0%0/3
A and B
A= $is_emptyB= $was_empty
ABresult
0X0
100
111
102 0 next;
103 }
104 0 $was_empty = $is_empty;
105 }
106  
107 1 ▶ s/$/\$/ if $ends;
Branch: 1 branch
BranchTrueFalseTotal
if $ends 0 1 1
108 1 ▶ if ($nonprinting) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nonprinting) 0 1 1
109 0 s/([\x80-\xFF])/"M-" . ("\x7F" & $1)/ge;
110 0 s/([\x00-\x08\x0B-\x1F])/"^" . chr (0100 + ord $1)/ge;
111 0 s/\x7F/^?/g;
112 }
113 1 ▶ if ($tabs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tabs) 0 1 1
114 0 s/\x09/^I/g;
115 }
116  
117 1 ▶ if ($number_lines || ($number_non_blanks && /\S/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number_lines or $number_non_blanks and /\S/) 1 0 1
Condition16%1/6
$number_lines or $number_non_blanks and /\S/
l!l&&r!l&&!r
100
$number_non_blanks and /\S/
!ll&&!rl&&r
000
MC/DC0%0/3
$number_lines or $number_non_blanks and /\S/
$number_lines $number_non_blanks /\S/
Truth table25%1/4
A or B and C
A= $number_linesB= $number_non_blanksC= /\S/
ABCresult
1XX1
0111
00X0
0100
118 1 printf "%6d\t", ++$count;
119 }
120 1 print;
121 }
122 }
123  
124 __END__
« bin/units ↑ Summary bin/date »