| 1 | package IO::Factotum; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 52 | ▶ | use v5.16; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 52 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 52 | our $VERSION = "0.04"; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 52 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 52 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 52 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 52 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 52 | ▶ | use POSIX (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 52 | ▶ | use Command::Run::Tmpfile; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 52 | my @converter; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 80 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=8 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 80 | my %arg = @_; | |||||||||||||||||||||||||||
| 19 | 80 | ▶ | my $match = delete $arg{match} // croak "register: match is required"; | ||||||||||||||||||||||||||
|
Condition50%1/2
delete $arg{"match"} // croak("register: match is required")
MC/DC0%0/1
delete $arg{"match"} // croak("register: match is required")
delete $arg{"match"}
Truth table50%1/2
A
A= delete $arg{"match"}
| |||||||||||||||||||||||||||||
| 20 | 80 | ▶ | my $command = delete $arg{command} // croak "register: command is required"; | ||||||||||||||||||||||||||
|
Condition50%1/2
delete $arg{"command"} // croak("register: command is required")
MC/DC0%0/1
delete $arg{"command"} // croak("register: command is required")
delete $arg{"command"}
Truth table50%1/2
A
A= delete $arg{"command"}
| |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | croak "register: unknown parameter: @{[ sort keys %arg ]}" if %arg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 80 | ▶ | ref $match eq 'Regexp' or ref $match eq 'CODE' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $match eq "Regexp" or ref $match eq "CODE"
MC/DC0%0/2
ref $match eq "Regexp" or ref $match eq "CODE"
ref $match eq "Regexp"
ref $match eq "CODE"
Truth table66%2/3
A or B
A= ref $match eq "Regexp"B= ref $match eq "CODE"
| |||||||||||||||||||||||||||||
| 23 | or croak "register: match must be a regex or a code reference"; | ||||||||||||||||||||||||||||
| 24 | 80 | ▶ | ref $command eq 'ARRAY' && @$command | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $command eq "ARRAY" and @$command
MC/DC0%0/2
ref $command eq "ARRAY" and @$command
ref $command eq "ARRAY"
@$command
Truth table33%1/3
A and B
A= ref $command eq "ARRAY"B= @$command
| |||||||||||||||||||||||||||||
| 25 | or croak "register: command must be an array reference"; | ||||||||||||||||||||||||||||
| 26 | 80 | push @converter, { match => $match, command => [ @$command ] }; | |||||||||||||||||||||||||||
| 27 | 80 | return; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 36 | ▶ | sub clear { | ||||||||||||||||||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 36 | @converter = (); | |||||||||||||||||||||||||||
| 32 | 36 | return; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 106 | ▶ | sub converter { | ||||||||||||||||||||||||||
|
Subroutine
converter: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 36 | 106 | my $file = shift; | |||||||||||||||||||||||||||
| 37 | 106 | for my $c (@converter) { | |||||||||||||||||||||||||||
| 38 | 101 | my $match = $c->{match}; | |||||||||||||||||||||||||||
| 39 | 101 | ▶ | if (ref $match eq 'CODE' ? $match->($file) : $file =~ $match) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 40 | 92 | return $c; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 14 | undef; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 115 | ▶ | sub open_input { | ||||||||||||||||||||||||||
|
Subroutine
open_input: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 115 | my $file = shift; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # The standard input is read as it is, by STDIN itself: another | ||||||||||||||||||||||||||||
| 50 | # handle on it would not see what STDIN has read ahead | ||||||||||||||||||||||||||||
| 51 | 115 | ▶ | return *STDIN{IO} if $file eq '-'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # The file is opened first: a file which can not be read is an | ||||||||||||||||||||||||||||
| 54 | # error of the caller with $! set, whatever the converter does | ||||||||||||||||||||||||||||
| 55 | 112 | ▶ | my $fh = IO::File->new($file, '<') // return undef; | ||||||||||||||||||||||||||
|
Condition100%2/2
"IO::File"->new($file, "<") // (return undef)
MC/DC100%1/1
"IO::File"->new($file, "<") // (return undef)
"IO::File"->new($file, "<")
Truth table100%2/2
A
A= "IO::File"->new($file, "<")
| |||||||||||||||||||||||||||||
| 56 | 106 | binmode $fh, ':raw'; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 106 | ▶ | my $c = converter($file) // return $fh; | ||||||||||||||||||||||||||
|
Condition100%2/2
converter($file) // (return $fh)
MC/DC100%1/1
converter($file) // (return $fh)
converter($file)
Truth table100%2/2
A
A= converter($file)
| |||||||||||||||||||||||||||||
| 59 | # a converter which fails leaves the file as it is | ||||||||||||||||||||||||||||
| 60 | 92 | ▶ | convert($c->{command}, $file) // $fh; | ||||||||||||||||||||||||||
|
Condition66%2/3
convert($c->{"command"}, $file) // $fh
MC/DC0%0/2
convert($c->{"command"}, $file) // $fh
convert($c->{"command"}, $file)
$fh
Truth table66%2/3
A // B
A= convert($c->{"command"}, $file)B= $fh
| |||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # Run the command with the file name, in a process of its own, and | ||||||||||||||||||||||||||||
| 64 | # return the handle of a temporary file which has what it wrote to the | ||||||||||||||||||||||||||||
| 65 | # standard output, or undef when it fails. The output goes to the file | ||||||||||||||||||||||||||||
| 66 | # as it is, byte for byte. The standard error is thrown away. | ||||||||||||||||||||||||||||
| 67 | 92 | ▶ | sub convert { | ||||||||||||||||||||||||||
|
Subroutine
convert: called CC=11 SCAR=26.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | 92 | my($command, $file) = @_; | |||||||||||||||||||||||||||
| 69 | 92 | my @command = (@$command, $file); | |||||||||||||||||||||||||||
| 70 | 92 | my $tmp = Command::Run::Tmpfile->new(raw => 1); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # $? and $! of the caller are kept, and the child is waited for | ||||||||||||||||||||||||||||
| 73 | # even when the caller ignores SIGCHLD | ||||||||||||||||||||||||||||
| 74 | 92 | local($?, $!); | |||||||||||||||||||||||||||
| 75 | 92 | local $SIG{CHLD} = 'DEFAULT'; | |||||||||||||||||||||||||||
| 76 | # what is in the buffers is not to be written twice | ||||||||||||||||||||||||||||
| 77 | 92 | STDOUT->flush; | |||||||||||||||||||||||||||
| 78 | 92 | STDERR->flush; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 92 | ▶ | my $pid = fork // return undef; | ||||||||||||||||||||||||||
|
Condition50%1/2
fork // (return undef)
MC/DC0%0/1
fork // (return undef)
fork
Truth table50%1/2
A
A= fork
| |||||||||||||||||||||||||||||
| 81 | 92 | ▶ | if ($pid == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | # The child never returns to the program: it ends by _exit, so | ||||||||||||||||||||||||||||
| 83 | # that the END blocks and the destructors of the program do not | ||||||||||||||||||||||||||||
| 84 | # run here, even when open_input is called inside eval. | ||||||||||||||||||||||||||||
| 85 | 30 | ▶ | my $status = eval { | ||||||||||||||||||||||||||
|
Condition0%0/2
eval {
do {
open STDIN, "<", "File::Spec"->devnull or die;
open STDOUT, ">&", $tmp->fh or die;
open STDERR, ">", "File::Spec"->devnull or die;
binmode STDOUT, ":raw";
if (ref $command[0] eq "CODE") {
my $code = shift @command;
&$code(@command);
"STDOUT"->flush;
0;
}
else {
BEGIN {${^WARNING_BITS} = "\x55\x15\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"}
exec {$command[0];} @command or POSIX::_exit(127);
}
}
} // 1
MC/DC0%0/1
eval {
do {
open STDIN, "<", "File::Spec"->devnull or die;
open STDOUT, ">&", $tmp->fh or die;
open STDERR, ">", "File::Spec"->devnull or die;
binmode STDOUT, ":raw";
if (ref $command[0] eq "CODE") {
my $code = shift @command;
&$code(@command);
"STDOUT"->flush;
0;
}
else {
BEGIN {${^WARNING_BITS} = "\x55\x15\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"}
exec {$command[0];} @command or POSIX::_exit(127);
}
}
} // 1
eval {
do {
open STDIN, "<", "File::Spec"->devnull or die;
open STDOUT, ">&", $tmp->fh or die;
open STDERR, ">", "File::Spec"->devnull or die;
binmode STDOUT, ":raw";
if (ref $command[0] eq "CODE") {
my $code = shift @command;
&$code(@command);
"STDOUT"->flush;
0;
}
else {
BEGIN {${^WARNING_BITS} = "\x55\x15\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"}
exec {$command[0];} @command or POSIX::_exit(127);
}
}
}
Truth table0%0/2
A
A= eval {
do {
open STDIN, "<", "File::Spec"->devnull or die;
open STDOUT, ">&", $tmp->fh or die;
open STDERR, ">", "File::Spec"->devnull or die;
binmode STDOUT, ":raw";
if (ref $command[0] eq "CODE") {
my $code = shift @command;
&$code(@command);
"STDOUT"->flush;
0;
}
else {
BEGIN {${^WARNING_BITS} = "\x55\x15\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"}
exec {$command[0];} @command or POSIX::_exit(127);
}
}
}
| |||||||||||||||||||||||||||||
| 86 | 30 | ▶ | open STDIN, '<', File::Spec->devnull or die; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 30 | ▶ | open STDOUT, '>&', $tmp->fh or die; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 30 | ▶ | open STDERR, '>', File::Spec->devnull or die; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 30 | binmode STDOUT, ':raw'; | |||||||||||||||||||||||||||
| 90 | 30 | ▶ | if (ref $command[0] eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | my $code = shift @command; | |||||||||||||||||||||||||||
| 92 | 0 | $code->(@command); | |||||||||||||||||||||||||||
| 93 | 0 | STDOUT->flush; | |||||||||||||||||||||||||||
| 94 | 0 | 0; | |||||||||||||||||||||||||||
| 95 | } else { | ||||||||||||||||||||||||||||
| 96 | # "or" keeps old perls from warning about a statement | ||||||||||||||||||||||||||||
| 97 | # after exec | ||||||||||||||||||||||||||||
| 98 | 52 | ▶ | no warnings 'exec'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 99 | 30 | ▶ | exec { $command[0] } @command | ||||||||||||||||||||||||||
|
Condition0%0/3
exec({$command[0];} @command) || POSIX::_exit(127)
MC/DC0%0/2
exec({$command[0];} @command) || POSIX::_exit(127)
exec({$command[0];} @command)
POSIX::_exit(127)
Truth table0%0/3
A || B
A= exec({$command[0];} @command)B= POSIX::_exit(127)
| |||||||||||||||||||||||||||||
| 100 | or POSIX::_exit(127); | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } // 1; | ||||||||||||||||||||||||||||
| 103 | 0 | POSIX::_exit($status); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 62 | waitpid $pid, 0; | |||||||||||||||||||||||||||
| 106 | 62 | ▶ | return undef if $? != 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 55 | my $converted = $tmp->fh; | |||||||||||||||||||||||||||
| 109 | 55 | ▶ | $converted->seek(0, 0) or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 55 | $converted; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # Register the converters of IO::Factotum::NAME by their names | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | sub load { | ||||||||||||||||||||||||||
|
Subroutine
load: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | 0 | for my $name (@_) { | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | $name =~ /\A\w+(?:::\w+)*\z/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | or croak "IO::Factotum: $name: invalid converter name"; | ||||||||||||||||||||||||||||
| 118 | 0 | my $module = __PACKAGE__ . "::$name"; | |||||||||||||||||||||||||||
| 119 | 0 | ▶ | eval "require $module; 1" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | or die "IO::Factotum: $name: no such converter\n$@"; | ||||||||||||||||||||||||||||
| 121 | 0 | ▶ | my $setup = $module->can('setup') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | or die "IO::Factotum: $module has no setup function\n"; | ||||||||||||||||||||||||||||
| 123 | 0 | $setup->(); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 0 | return; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 35 | ▶ | sub tie_argv { | ||||||||||||||||||||||||||
|
Subroutine
tie_argv: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 35 | ▶ | return if tied *ARGV; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 30 | require IO::Factotum::ARGV; | |||||||||||||||||||||||||||
| 131 | 30 | tie *ARGV, 'IO::Factotum::ARGV'; | |||||||||||||||||||||||||||
| 132 | 30 | return; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 52 | 1; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | __END__ | ||||||||||||||||||||||||||||