| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
$opt{"v"} or $ends
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
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | my $squeeze_empty = $opt{'s'}; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | ▶ | my $number_lines = $opt{'n'} && !$opt{'b'}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$opt{'n'} && !$opt{'b'}
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'}
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | my $number_non_blanks = $opt{'b'}; | |||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | ▶ | my $cook = grep {$_ || ()} @opt{'b', 'e', 'n', 's', 't', 'v'}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$_ || ()
MC/DC50%1/2
$_ || ()
$_
()
Truth table66%2/3
A || B
A= $_B= ()
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | ▶ | my $cat = $cook ? \&cook_file : \&raw_file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | # Unbuffer output for -u. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | ▶ | $| = 1 if $opt{'u'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 1 | my $was_empty = 0; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | my $count = 0; | |||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | ▶ | if (scalar(@ARGV) == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | $fh = *STDIN; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | ▶ | if (-d $name) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | warn "$Program: $name: is a directory\n"; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | ▶ | if (!open($fh, '<', $name)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$name ne "-" and not close $fh
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | ▶ | if ($squeeze_empty) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | my $is_empty = /^$/; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if ($is_empty && $was_empty) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$is_empty and $was_empty
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
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | $was_empty = $is_empty; | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 1 | ▶ | s/$/\$/ if $ends; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 1 | ▶ | if ($nonprinting) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | s/\x09/^I/g; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 1 | ▶ | if ($number_lines || ($number_non_blanks && /\S/)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
$number_lines or $number_non_blanks and /\S/
$number_non_blanks and /\S/
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/
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 1 | printf "%6d\t", ++$count; | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 1 | print; | |||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | __END__ | ||||||||||||||||||||||||||||||||||||||||||