blib/lib/Vi/QuickFix.pm

filter: Statementstmt 74.4% 134 / 180 Branchbran 56.2% 54 / 96 Conditioncond 51.2% 20 / 39 MC/DCmcdc 27.2% 6 / 22 Subroutinesub 86.4% 32 / 37 Podpod 0.0% 0 / 12 totaltotal 63.7% 246 / 386 SCAR 53.6
CC
62
Cov
66%
fork_relay
20.0
__ANON__
14.2
import
12.6
CRAP 212.7
↑ Summary
1 #!/usr/bin/perl
2 package Vi::QuickFix;
3 17 ▶use 5.008_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5 # use Carp;
6  
7 5 our $VERSION;
8 BEGIN {
9 16 ▶ $VERSION = ('$Revision: 1.134 $' =~ /(\d+.\d+)/)[ 0];
Subroutine BEGIN: called CC=1 SCAR=0.0
10 }
11  
12 5 ▶unless ( caller ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller) 0 5 5
13 # process <> if called as an executable
14 0 exec_mode(1); # signal fact ( to END processing)
15 0 require Getopt::Std;
16 0 Getopt::Std::getopts( 'q:f:v', \ my %opt);
17 0 ▶ print "$0 version $VERSION\n" and exit 0 if $opt{ v};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"v"} 0 0 0
Condition0%0/5
$opt{'v'} && (print("$0 version $Vi::QuickFix::VERSION\n") && exit 0)
!ll&&!rl&&r
000
print "$0 version $Vi::QuickFix::VERSION\n" and exit 0
!ll
00
MC/DC0%0/2
$opt{'v'} && (print("$0 version $Vi::QuickFix::VERSION\n") && exit 0)
$opt{'v'} print "$0 version $Vi::QuickFix::VERSION\n"
Truth table0%0/3
A && B
A= $opt{'v'}B= print "$0 version $Vi::QuickFix::VERSION\n"
ABresult
0X0
100
111
18 0 ▶ err_open( $opt{ q} || $opt{ f});
Condition0%0/3
$opt{'q'} || $opt{'f'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{'q'} || $opt{'f'}
$opt{'q'} $opt{'f'}
Truth table0%0/3
A || B
A= $opt{'q'}B= $opt{'f'}
ABresult
1X1
011
000
19 0 ▶ print && err_out( $_) while <>;
Branch: 2 branches
BranchTrueFalseTotal
defined($_ = readline ARGV) and do { print $_ and err_out($_) } 0 0 0
print $_ and err_out($_) 0 0 0
20 0 exit;
21 }
22  
23 ###########################################################################
24  
25 # keywords for ->import
26 11 ▶use constant KEYWORDS => qw(silent sig tie fork);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # environment variable(s)
29 11 ▶use constant VAR_SOURCEFILE => 'VI_QUICKFIX_SOURCEFILE';
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 BEGIN {{ # space for private variables
32  
33 11 my $relay = ''; # method of transfer to error file: "sig" or "tie"
34 11 my %invocation; # from where was import() called?
35  
36 sub import {
37 8 ▶ my $class = shift;
Subroutine import: called CC=12 SCAR=25.3
38 8 my %keywords;
39 8 @keywords{ KEYWORDS()} = ();
40 8 ▶ $keywords{ shift()} = 1 while @_ and exists $keywords{ $_[ 0]};
Branch: 1 branch
BranchTrueFalseTotal
@_ and exists $keywords{$_[0]} and do { $keywords{shift()} = 1 } 7 8 15
Condition100%3/3
@_ and exists $keywords{$_[0]}
!ll&&!rl&&r
6410
MC/DC100%2/2
@_ and exists $keywords{$_[0]}
@_ exists $keywords{$_[0]}
Truth table100%3/3
A and B
A= @_B= exists $keywords{$_[0]}
ABresult
0X0
100
111
41  
42 8 my $filename = shift;
43 8 ▶ make_silent() if $keywords{ silent};
Branch: 1 branch
BranchTrueFalseTotal
if $keywords{"silent"} 1 7 8
44 8 my ( $wanted_relay) = grep $keywords{ $_}, qw( sig tie fork);
45 8 ▶ $relay = $wanted_relay || default_relay();
Condition66%2/3
$wanted_relay || default_relay()
l!l&&r!l&&!r
620
MC/DC0%0/2
$wanted_relay || default_relay()
$wanted_relay default_relay()
Truth table66%2/3
A || B
A= $wanted_relayB= default_relay()
ABresult
1X1
011
000
46 8 ▶ if ( my $reason = relay_obstacle( $relay) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $reason = relay_obstacle($relay)) 1 7 8
47 1 croak( "Cannot use '$relay' method: $reason");
48 }
49 7 ▶ err_open($filename) unless $relay eq 'fork'; # happens in background
Branch: 1 branch
BranchTrueFalseTotal
unless $relay eq "fork" 7 0 7
50 6 ▶ if ( $relay eq 'tie' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($relay eq 'tie') { } 4 2 6
elsif ($relay eq 'sig') { } 2 0 2
elsif ($relay eq 'fork') { } 0 0 0
51 # if tied, it's tied to ourselves (otherwise obstacle)
52 4 ▶ tie *STDERR, 'Vi::QuickFix::Tee', '>&STDERR' unless tied *STDERR;
Branch: 1 branch
BranchTrueFalseTotal
unless tied *STDERR 3 1 4
53 } elsif ( $relay eq 'sig' ) {
54 2 $SIG{ $_} = Vi::QuickFix::SigHandler->new( $_) for
55 qw( __WARN__ __DIE__);
56 } elsif ( $relay eq 'fork' ) {
57 0 *STDERR = fork_relay($filename);
58 }
59 # save invocation for obligate message
60 6 (undef, @invocation{qw(file line)}) = caller;
61 }
62  
63 # internal variables
64 {
65 11 my $exec_mode; # set if lib file is run as a script
66 sub exec_mode {
67 16 ▶ $exec_mode = shift if @_;
Subroutine exec_mode: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ 6 10 16
68 16 $exec_mode;
69 }
70
71 11 my $silent = 0; # switch off otherwise obligatory warning
72 12 ▶ sub make_silent { $silent = 1 }
Subroutine make_silent: called CC=1 SCAR=0.0
Pod undocumented
73 18 ▶ sub is_silent { $silent }
Subroutine is_silent: called CC=1 SCAR=0.0
Pod undocumented
74  
75 11 my $errfile = 'errors.err'; # name of error file
76 11 my $errhandle; # write formatted errors here
77 # open the given file (or default), set $errfile and $errhandle
78 sub err_open {
79 12 ▶ $errfile = shift || 'errors.err';
Subroutine err_open: called CC=4 SCAR=13.9
Pod undocumented
Condition100%2/2
shift() || 'errors.err'
l!l
57
MC/DC100%1/1
shift() || 'errors.err'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
80 12 ▶ $errhandle = IO::File->new( $errfile, '>') or warn(
Branch: 1 branch
BranchTrueFalseTotal
$errhandle = "IO::File"->new($errfile, ">") or warn "Can't create error file '${errfile}': $!" 1 11 12
81 "Can't create error file '$errfile': $!"
82 );
83 11 ▶ $errhandle->autoflush if $errhandle;
Branch: 1 branch
BranchTrueFalseTotal
if $errhandle 11 0 11
84 }
85  
86 sub err_print {
87 20 ▶ print $errhandle @_ if $errhandle;
Subroutine err_print: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $errhandle 20 0 20
88 }
89  
90 sub err_clean {
91 11 ▶ my $unlink = shift;
Subroutine err_clean: called CC=5 SCAR=16.7
Pod undocumented
92 11 ▶ close $errhandle if $errhandle;
Branch: 1 branch
BranchTrueFalseTotal
if $errhandle 10 1 11
93 11 ▶ unlink $errfile if $errfile and $unlink and not -s $errfile;
Branch: 1 branch
BranchTrueFalseTotal
if $errfile and $unlink and not -s $errfile 3 8 11
Condition50%3/6
$errfile and $unlink and not -s $errfile
!ll&&!rl&&r
083
$errfile and $unlink
!ll&&!rl&&r
0011
MC/DC33%1/3
$errfile and $unlink and not -s $errfile
$errfile $unlink not -s $errfile
Truth table50%2/4
A and B and not(C)
A= $errfileB= $unlinkC= not -s $errfile
ABCresult
0XX0
10X0
1100
1111
94 }
95 }
96  
97 11 sub err_out {
98 # handle multiple, possibly multi-line messages (though usually
99 # there will be only one)
100 32 ▶ for ( map split( /\n+/), @_ ) {
Subroutine err_out: called CC=9 SCAR=25.2
Pod undocumented
101 32 my $out;
102 32 ▶ if ( /.+:\d+:/ ) { # already in QuickFix format, pass on
Branch: 1 branch
BranchTrueFalseTotal
if (/.+:\d+:/) { } 0 32 32
103 0 err_print("$_\n");
104 } else {
105 32 for ( parse_perl_msg($_) ) {
106 20 ▶ my ( $message, $file, $line, $rest) = @$_ or next;
Branch: 1 branch
BranchTrueFalseTotal
() = @$_ or next 0 20 20
107 20 ▶ $message .= $rest if $rest =~ s/^,//;
Branch: 1 branch
BranchTrueFalseTotal
if $rest =~ s/^,// 0 20 20
108 20 ▶ $file eq '-' and defined and $file = $_ for
Branch: 1 branch
BranchTrueFalseTotal
$file eq "-" and defined $_ and $file = $_ 0 20 20
Condition33%1/3
$file eq "-" and defined $_
!ll&&!rl&&r
2000
MC/DC0%0/2
$file eq "-" and defined $_
$file eq "-" defined $_
Truth table33%1/3
A and B
A= $file eq "-"B= defined $_
ABresult
0X0
100
111
109 $ENV{ VAR_SOURCEFILE()};
110 20 err_print("$file:$line:$message\n");
111 }
112 }
113 }
114 }
115  
116 # use constant PERL_MSG => qr/^(.*?) at (.*?) line (\d+)(\.?|,.*)$/;
117 sub parse_perl_msg {
118 32 ▶ my @coll;
Subroutine parse_perl_msg: called CC=6 SCAR=18.9
Pod undocumented
119 32 for ( shift ) {
120 32 ▶ while ( m/ at /g ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/ at /g) 20 32 52
121 20 my $text = substr($_, 0, $-[0]);
122 20 my $pos = pos;
123 20 ▶ while ( m/ line (\d+)(\.?|,.*)$/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/ line (\d+)(\.?|,.*)$/g) 20 20 40
124 20 my $file = substr($_, $pos, $-[0] - $pos);
125 20 my $line = $1;
126 20 my $rest = $2;
127 20 push @coll, [$text, $file, $line, $rest];
128 }
129 20 pos = $pos;
130 }
131 }
132 32 ▶ return @coll if @coll <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if @coll <= 1 32 0 32
133 0 my @existing = grep -e $_->[1], @coll;
134 0 ▶ return @existing if @existing;
Branch: 1 branch
BranchTrueFalseTotal
if @existing 0 0 0
135 0 return @coll;
136 }
137  
138 # issue warning, erase error file
139 11 my $end_entiteled = $$;
140 END {
141 # issue warning (only original process, and not in exec mode)
142 11 ▶ unless ( is_silent or exec_mode() or $$ != $end_entiteled ) {
Subroutine END: called CC=8 SCAR=20.8
Branch: 1 branch
BranchTrueFalseTotal
unless (is_silent() or exec_mode() or $$ != $end_entiteled) 10 7 17
Condition100%6/6
is_silent() or exec_mode() or $$ != $end_entiteled
l!l&&r!l&&!r
1214
is_silent() or exec_mode()
l!l&&r!l&&!r
174
MC/DC66%2/3
is_silent() or exec_mode() or $$ != $end_entiteled
is_silent() exec_mode() $$ != $end_entiteled
Truth table75%3/4
A or B or C
A= is_silent()B= exec_mode()C= $$ != $end_entiteled
ABCresult
1XX1
01X1
0011
0000
143 4 my $invocation_at = "at $invocation{file} line $invocation{line}";
144 4 warn "QuickFix ($relay) active $invocation_at\n";
145 }
146 # silently remove objects
147 11 make_silent();
148 11 ▶ if ( $relay eq 'tie' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($relay eq 'tie') { } 4 13 17
elsif ($relay eq 'sig') { } 18 11 29
elsif ($relay eq 'fork') { } 16 6 22
149 3 untie *STDERR;
150 } elsif ( $relay eq 'sig' ) {
151 2 $SIG{ $_} = 'DEFAULT' for qw( __WARN__ __DIE__);
152 } elsif ( $relay eq 'fork' ) {
153 0 close STDERR;
154 0 wait_kid();
155 }
156 # remove file if created by us and empty
157 11 err_clean($$ == $end_entiteled);
158 }
159  
160 11 ▶}}
Subroutine BEGIN: called CC=1 SCAR=0.0
161  
162 11 ▶use constant MINVERS => 5.008001; # minimum perl version for tie method
Subroutine BEGIN: called CC=1 SCAR=0.0
163 sub relay_obstacle {
164 10 ▶ my $relay = shift || '';
Subroutine relay_obstacle: called CC=6 SCAR=18.5
Pod undocumented
Condition50%1/2
shift() || ''
l!l
100
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
165 10 ▶ return '' unless $relay eq 'tie';
Branch: 1 branch
BranchTrueFalseTotal
unless $relay eq "tie" 2 8 10
166 8 ▶ if ( $] < MINVERS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.008001") 0 8 8
167 0 return "perl version is $], must be >= @{[ MINVERS]}";
168 }
169 8 ▶ if ( my $tie_ob = tied *STDERR ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tie_ob = tied *STDERR) 2 6 8
170 2 my $tieclass = ref $tie_ob;
171 2 ▶ return "STDERR already tied to '$tieclass'" unless
Branch: 1 branch
BranchTrueFalseTotal
unless $tieclass eq "Vi::QuickFix::Tee" 1 1 2
172 $tieclass eq 'Vi::QuickFix::Tee';
173 }
174 7 return '';
175 }
176  
177 2 ▶sub default_relay { relay_obstacle( 'tie') ? 'sig' : 'tie' }
Subroutine default_relay: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
relay_obstacle('tie') ? : 0 2 2
178  
179 {
180 11 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
181 5 my ($read, $write, $kid);
182 sub fork_relay {
183 0 ▶ my $filename = shift;
Subroutine fork_relay: not called CC=4 SCAR=30.0
Pod undocumented
184 0 my $parent = $$;
185 0 pipe $read, $write;
186 0 ▶ if ( $kid = fork ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kid = fork) { } 0 0 0
187 # parent
188 0 close $read;
189 0 return $write;
190 } else {
191 0 ▶ Carp::croak "Can't fork: $!" unless defined $kid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kid 0 0 0
192 # kid
193 0 close $write;
194 0 err_open($filename);
195 0 ▶ while ( <$read> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $read)) 0 0 0
196 0 print STDERR $_;
197 0 err_out($_);
198 }
199 0 err_clean(1);
200 0 exit;
201 }
202 }
203  
204 11 ▶ use POSIX ":sys_wait_h";
Subroutine BEGIN: called CC=1 SCAR=0.0
205 sub wait_kid {
206 0 ▶ my $x;
Subroutine wait_kid: not called CC=2 SCAR=17.9
Pod undocumented
207 0 ▶ do { $x = waitpid -1, WNOHANG } while $x > 0;
Branch: 1 branch
BranchTrueFalseTotal
$x > 0 and do { do { $x = waitpid(-1, 1) } } 0 0 0
208 }
209 }
210  
211 # common destructor method
212 5 package Vi::QuickFix::Destructor;
213  
214 11 ▶use Carp qw( shortmess);
Subroutine BEGIN: called CC=1 SCAR=0.0
215 11 ▶BEGIN { our @CARP_NOT = qw( Vi::QuickFix) }
Subroutine BEGIN: called CC=1 SCAR=0.0
216 sub DESTROY {
217 7 ▶ my $ob = shift;
Subroutine DESTROY: called CC=3 SCAR=16.7
218 7 ▶ return if Vi::QuickFix::is_silent or $^C; # it's a mess under -c
Branch: 1 branch
BranchTrueFalseTotal
if Vi::QuickFix::is_silent() or $^C 7 0 7
Condition33%1/3
Vi::QuickFix::is_silent() or $^C
l!l&&r!l&&!r
700
MC/DC0%0/2
Vi::QuickFix::is_silent() or $^C
Vi::QuickFix::is_silent() $^C
Truth table33%1/3
A or B
A= Vi::QuickFix::is_silent()B= $^C
ABresult
1X1
011
000
219 0 my $id = $ob->id;
220 0 my $msg = shortmess( "QuickFix $id processing interrupted");
221 # simulate intact QuickFix processing
222 0 Vi::QuickFix::err_out( $msg);
223 0 warn "$msg";
224 }
225  
226 # Class to associate a DESTROY method with sig handlers
227 package Vi::QuickFix::SigHandler;
228 11 ▶use base qw( Vi::QuickFix::Destructor);
Subroutine BEGIN: called CC=1 SCAR=0.0
229  
230 # return a chaining handler for __WARN__ or __DIE__
231 sub new {
232 4 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.5
233 4 my $sig = shift;
234 4 my $prev_handler = $SIG{ $sig};
235 my $sub = sub {
236 6 ▶ return $sig unless @_; # backdoor
Subroutine __ANON__: called CC=8 SCAR=26.5
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 6 6
237 6 ▶ Vi::QuickFix::err_out( @_) unless $sig eq '__DIE__' and _in_eval();
Branch: 1 branch
BranchTrueFalseTotal
unless $sig eq "__DIE__" and _in_eval() 6 0 6
Condition33%1/3
$sig eq "__DIE__" and _in_eval()
!ll&&!rl&&r
600
MC/DC0%0/2
$sig eq "__DIE__" and _in_eval()
$sig eq "__DIE__" _in_eval()
Truth table33%1/3
A and B
A= $sig eq "__DIE__"B= _in_eval()
ABresult
0X0
100
111
238 6 my $code;
239 # resolve string at call time
240 6 ▶ if ( $prev_handler ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_handler) 0 6 6
241 $code = ref $prev_handler ?
242 $prev_handler :
243 0 ▶ \ &{ 'main::' . $prev_handler};
Branch: 1 branch
BranchTrueFalseTotal
ref $prev_handler ? : 0 0 0
244 }
245 6 ▶ goto &$code if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 0 6 6
246 6 ▶ die @_ if $sig eq '__DIE__';
Branch: 1 branch
BranchTrueFalseTotal
if $sig eq "__DIE__" 0 6 6
247 6 warn @_;
248 4 };
249 4 bless $sub, $class; # so we can have a destructor
250 }
251  
252 sub _in_eval {
253 0 ▶ my $i = -1; # first call with 0
Subroutine _in_eval: not called CC=3 SCAR=24.8
254 0 ▶ while ( defined(my $sub = (caller ++ $i)[3]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $sub = (caller ++$i)[3])) 0 0 0
255 0 ▶ return 1 if $sub =~ /^\(eval/;
Branch: 1 branch
BranchTrueFalseTotal
if $sub =~ /^\(eval/ 0 0 0
256 }
257 0 return 0;
258 }
259  
260 sub id {
261 0 ▶ my $handler = shift;
Subroutine id: not called CC=1 SCAR=6.9
262 0 $handler->(); # call without args returns __WARN__ or __DIE__
263 }
264  
265 # tie class to tee re-formatted output to an error file
266 package Vi::QuickFix::Tee;
267  
268 11 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
269 11 ▶use Tie::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
270 11 ▶use base qw( Tie::StdHandle Vi::QuickFix::Destructor);
Subroutine BEGIN: called CC=1 SCAR=0.0
271  
272 sub WRITE {
273 10 ▶ my $fh = shift;
Subroutine WRITE: called CC=1 SCAR=0.0
274 10 my ( $scalar, $length) = @_;
275 10 Vi::QuickFix::err_out( $scalar);
276 10 $fh->Tie::StdHandle::WRITE( @_);
277 }
278  
279 0 ▶sub id { 'STDERR' }
Subroutine id: not called CC=1 SCAR=6.9
280  
281 5 1;
282  
283 __END__
↑ Summary