script/greple

filter: Statementstmt 71.8% 514 / 715 Branchbran 53.5% 226 / 422 Conditioncond 48.9% 91 / 186 MC/DCmcdc 23.0% 23 / 100 Subroutinesub 73.1% 68 / 93 Podpod n/a totaltotal 60.8% 922 / 1516 CC 156 SCAR 37.6
CC
156
Cov
63%
output
106.5
grep_data
82.9
open_nextfile
77.2
CRAP 42.8
« lib/App/Greple/Util.pm ↑ Summary lib/App/Greple/Grep.pm »
1 #!/usr/bin/env perl
2 ##
3 ## greple: extensible grep with lexical expression and region handling
4 ##
5 ## Since Mar 29 1991
6 ##
7  
8 170 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 170 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 170 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 170 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 170 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 170 ▶use Text::ParseWords qw(shellwords);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 170 ▶use List::Util qw(min max first sum uniq shuffle notall pairs pairmap);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 170 ▶use Hash::Util qw(lock_keys);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 170 ▶use Cwd qw(getcwd abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 170 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 170 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 170 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 170 ▶use Encode::Guess;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 170 ▶use open IO => ':utf8', ':std';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 170 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 {
27 170 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 0 ▶ *Data::Dumper::qquote = sub { qq["${\(shift)}"] };
Subroutine __ANON__: not called CC=1 SCAR=2.6
29 170 $Data::Dumper::Useperl = 1;
30 }
31  
32 ##
33 ## Setup greple/lib to be a module directory if exists.
34 ##
35 170 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=12.0
36 170 ▶ if (my $lib = abs_path($0) =~ s{/(?:script/|bin/)?\w+$}{/lib}r) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $lib = abs_path($0) =~ s[/(?:script/|bin/)?\w+$][/lib]ru) 170 0 170
37 170 ▶ unshift @INC, $lib if -d "$lib/App/Greple";
Branch: 1 branch
BranchTrueFalseTotal
if -d "$lib/App/Greple" 170 0 170
38 }
39 }
40  
41 170 ▶use Getopt::EX::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 170 ▶use Getopt::EX::Func qw(parse_func callable);
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 170 ▶use App::Greple;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 170 ▶use App::Greple::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 170 ▶use App::Greple::Util qw(shellquote);
Subroutine BEGIN: called CC=1 SCAR=0.0
47 170 ▶use App::Greple::Grep;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 170 ▶use App::Greple::Regions;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 170 ▶use App::Greple::Pattern;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 170 ▶use App::Greple::Pattern::Holder;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 170 ▶use App::Greple::Filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 170 my $version = $App::Greple::VERSION;
54  
55 =encoding utf8
56  
57 =head1 NAME
58  
59  
60 greple - extensible grep with lexical expression and region control
61  
62  
63 =head1 VERSION
64  
65  
66 Version 10.05
67  
68  
69 =head1 SYNOPSIS
70  
71  
72 B<greple> [B<-M>I<module>] [ B<-options> ] pattern [ file... ]
73  
74 PATTERN
75 pattern 'and +must -not ?optional &function'
76 -x, --le pattern lexical expression (same as bare pattern)
77 -e, --and pattern pattern match across line boundary
78 -r, --must pattern pattern cannot be compromised
79 -t, --may pattern pattern may exist
80 -v, --not pattern pattern not to be matched
81 -E, --re pattern regular expression
82 --fe pattern fixed expression
83 -f, --file file file contains search pattern
84 --select index select indexed pattern from -f file
85 MATCH
86 -i, --ignore-case ignore case
87 -G, --capture-group match capture groups rather than the whole pattern
88 -S, --stretch stretch the matched area to the enclosing block
89 --need=[+-]n required positive match count
90 --allow=[+-]n acceptable negative match count
91 --matchcount=n[,m] required match count for each block
92 STYLE
93 -l list filename only
94 -c print count of matched block only
95 -n print line number
96 -b print block number
97 -H, -h do or do not display filenames
98 -o print only the matching part
99 --all print entire data
100 -F, --filter use as a filter (implies --all --need=0 --exit=0)
101 -m, --max=n[,m] max count of blocks to be shown
102 -A,-B,-C [n] after/before/both match context
103 --join remove newline in the matched part
104 --joinby=string replace newline in the matched text with a string
105 --nonewline do not add newline character at the end of block
106 --filestyle=style how filenames are printed (once, separate, line)
107 --linestyle=style how line numbers are printed (separate, line)
108 --blockstyle=style how block numbers are printed (separate, line)
109 --separate set filestyle, linestyle, blockstyle "separate"
110 --format LABEL=... define the format for line number and file name
111 --frame-top top frame line
112 --frame-middle middle frame line
113 --frame-bottom bottom frame line
114 FILE
115 --glob=glob glob target files
116 --chdir=dir change directory before search
117 --readlist get filenames from stdin
118 COLOR
119 --color=when use terminal colors (auto, always, never)
120 --nocolor same as --color=never
121 --colormap=color R, G, B, C, M, Y, etc.
122 --colorsub=... shortcut for --colormap="sub{...}"
123 --colorful use default multiple colors
124 --colorindex=flags color index method: Ascend/Descend/Block/Random/Unique/Group/GP
125 --random use a random color each time (--colorindex=R)
126 --uniqcolor use a different color for each unique string (--colorindex=U)
127 --uniqsub=func preprocess function to check uniqueness
128 --ansicolor=s ANSI color 16, 256 or 24bit
129 --[no]256 same as --ansicolor 256 or 16
130 --regioncolor use different color for inside and outside regions
131 --face enable or disable visual effects
132 BLOCK
133 -p, --paragraph enable paragraph mode
134 --border=pattern specify a border pattern
135 --block=pattern specify a block of records
136 --blockend=s block-end mark (Default: "--")
137 --join-blocks join consecutive blocks that are back-to-back
138 REGION
139 --inside=pattern select matches inside of pattern
140 --outside=pattern select matches outside of pattern
141 --include=pattern limit matches to the area
142 --exclude=pattern limit matches to outside of the area
143 --strict enable strict mode for --inside/outside --block
144 CHARACTER CODE
145 --icode=name input file encoding
146 --ocode=name output file encoding
147 FILTER
148 --if,--of=filter input/output filter command
149 --pf=filter post-process filter command
150 --noif disable the default input filter
151 RUNTIME FUNCTION
152 --begin=func call a function before starting the search
153 --end=func call a function after completing the search
154 --prologue=func call a function before executing the command
155 --epilogue=func call a function after executing the command
156 --postgrep=func call a function after each grep operation
157 --callback=func callback function for each matched string
158 OTHER
159 --usage[=expand] show this help message
160 --version show version
161 --exit=n set the command exit status
162 --norc skip reading startup file
163 --man display the manual page for the command or module
164 --show display the module file contents
165 --path display the path to the module file
166 --error=action action to take after a read error occurs
167 --warn=type runtime error handling type
168 --alert [name=#] set alert parameters (size/time)
169 -P, --parallel[=n] search patterns in parallel (default: cpu cores)
170 -d flags display info (f:file d:dir c:color m:misc s:stat)
171  
172 =cut
173  
174 170 my @baseclass = qw( App::Greple Getopt::EX );
175 170 my $rcloader = Getopt::EX::Loader
176 ->new(BASECLASS => \@baseclass);
177  
178 170 my @optargs;
179 my %optargs;
180  
181 170 ▶sub newopt {
Subroutine newopt: called CC=1 SCAR=0.0
182 15640 ▶ push @optargs, pairmap {
Subroutine __ANON__: called CC=3 SCAR=11.0
183 15640 local $_ = $a;
184 15640 s/\s+//g;
185 15640 s/^(?=\w+-)([-\w]+)/$1 =~ tr[-][_]r . "|$1"/e; # "a-b" -> "a_b|a-b"
186 15640 ▶ /^(\w+)/ and $optargs{$1} = $b if ref $b ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $b ne "CODE" 12920 2720 15640
Condition75%3/4
ref $b ne 'CODE' && (/^(\w+)/u && ($optargs{$1} = $b))
!ll
272012920
/^(\w+)/u and $optargs{$1} = $b
!ll
012920
MC/DC0%0/3
ref $b ne 'CODE' && (/^(\w+)/u && ($optargs{$1} = $b))
ref $b ne 'CODE' /^(\w+)/u $optargs{$1} = $b
Truth table0%0/4
A && B and C
A= ref $b ne 'CODE'B= /^(\w+)/uC= $optargs{$1} = $b
ABCresult
0XX0
10X0
1100
1111
187 15640 $_ => $b;
188 170 } @_;
189 }
190  
191 4 ▶sub opt :lvalue {
Subroutine opt: called CC=5 SCAR=23.4
192 4 my $name = shift;
193 4 ▶ my $var = $optargs{$name} or die "$name: invalid option name\n";
Branch: 1 branch
BranchTrueFalseTotal
my $var = $optargs{$name} or die "${name}: invalid option name\n" 4 0 4
194 4 ▶ if (ref $var eq 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $var eq 'SCALAR') { } 4 0 4
elsif (ref $var eq 'HASH' and @_ == 1) { } 0 0 0
195 4 return $$var;
196 ▶ } elsif (ref $var eq 'HASH' and @_ == 1) {
Condition0%0/3
ref $var eq "HASH" and @_ == 1
!ll&&!rl&&r
000
MC/DC0%0/2
ref $var eq "HASH" and @_ == 1
ref $var eq "HASH" @_ == 1
Truth table0%0/3
A and B
A= ref $var eq "HASH"B= @_ == 1
ABresult
0X0
100
111
197 0 return $var->{+shift};
198 } else {
199 0 return $var;
200 }
201 }
202  
203 170 my @opt_pattern;
204 184 ▶sub opt_pattern {
Subroutine opt_pattern: called CC=1 SCAR=0.0
205 184 push @opt_pattern, [ map "$_", @_ ];
206 184 $opt_pattern[-1];
207 }
208  
209 170 my @opt_colormap;
210 2 ▶sub opt_colormap { push @opt_colormap, $_[1] }
Subroutine opt_colormap: called CC=1 SCAR=0.0
211 1 ▶sub opt_colorsub { push @opt_colormap, "sub{ $_[1] }" }
Subroutine opt_colorsub: called CC=1 SCAR=0.0
212  
213 170 my %opt_format = (LINE => '%d:', FILE => '%s:', BLOCK => '%s:');
214 170 my %opt_alert = (size => 512 * 1024, time => 2);
215 170 my %opt_warn = (read => 0, skip => 1, retry => 0, begin => 0);
216  
217 newopt
218  
219 ##
220 ## PATTERN
221 ##
222 ' and |e =s ' => \&opt_pattern ,
223 ' must |r =s ' => \&opt_pattern ,
224 ' may |t =s ' => \&opt_pattern ,
225 ' not |v =s ' => \&opt_pattern ,
226 ' le |x =s ' => \&opt_pattern ,
227 ' re |E =s ' => \&opt_pattern ,
228 ' fe =s ' => \&opt_pattern ,
229 ' file |f =s ' => \ my @opt_f ,
230 ' select =s ' => \ my $opt_select ,
231  
232 ##
233 ## MATCH
234 ##
235 ' ignore-case |i ! ' => \ my $opt_i ,
236 ' need =s ' => \ my @opt_need ,
237 ' allow =s ' => \ my @opt_allow ,
238 ' matchcount |mc =s ' => \ my $opt_matchcount ,
239 ' capture-group |G ! ' => \ my $opt_capture_group ,
240 ' stretch |S ! ' => \ my $opt_stretch ,
241  
242 ##
243 ## STYLE
244 ##
245 ' files-with-matches |l ' => \ my $opt_l ,
246 ' count |c ' => \ my $opt_c ,
247 ' line-number |n ! ' => \ my $opt_n ,
248 ' block-number |b ! ' => \ my $opt_b ,
249 ' filename |H ' => \ my $opt_H ,
250 ' no-filename |h ' => \ my $opt_h ,
251 ' only-matching |o ! ' => \ my $opt_o ,
252 ' all ! ' => \ my $opt_all ,
253 ' filter |F ! ' => \ my $opt_filter ,
254 ' max-count |m =s ' => \ my $opt_m ,
255 ' after-context |A :2 ' => \(my $opt_A = 0) ,
256 ' before-context |B :2 ' => \(my $opt_B = 0) ,
257 ' context |C :2 ' => \(my $opt_C = 0) ,
258 ' join ! ' => \ my $opt_join ,
259 ' joinby =s ' => \(my $opt_joinby = "") ,
260 ' newline ! ' => \(my $opt_newline = 1) ,
261 ' filestyle |fs =s ' => \(my $opt_filestyle = 'line') ,
262 ' linestyle |ls =s ' => \(my $opt_linestyle = 'line') ,
263 ' blockstyle |bs =s ' => \(my $opt_blockstyle = 'line') ,
264 1 ▶ ' separate ' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
265 1 opt('filestyle') = opt('linestyle') = opt('blockstyle') = $_[0];
266 },
267 ' format =s ' => \ %opt_format ,
268 ' frame-top :s ' => \(my $opt_frame_top = '') ,
269 ' frame-middle :s ' => \(my $opt_frame_middle = '') ,
270 ' frame-bottom :s ' => \(my $opt_frame_bottom = '') ,
271  
272 ##
273 ## FILE
274 ##
275 ' glob =s ' => \ my @opt_glob ,
276 ' chdir =s ' => \ my @opt_chdir ,
277 ' readlist ! ' => \ my $opt_readlist ,
278  
279 ##
280 ## COLOR
281 ##
282 ' color =s ' => \(my $opt_color = 'auto') ,
283 ' colormap |cm =s ' => \&opt_colormap ,
284 ' colorsub |cs =s ' => \&opt_colorsub ,
285 ' colorful ! ' => \(my $opt_colorful = 1) ,
286 ' colorindex |ci =s ' => \(my $opt_colorindex = '') ,
287 ' ansicolor =s ' => \(my $opt_ansicolor = '256') ,
288 ' regioncolor |rc ! ' => \ my $opt_regioncolor ,
289 ' uniqsub |us =s ' => \ my @opt_uniqsub ,
290 ' face =s ' => \ my @opt_face ,
291 1 ▶ ' nocolor | no-color ' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
292 1 opt('color') = 'never';
293 },
294 0 ▶ ' 256! ' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
295 0 ▶ opt('ansicolor') = $_[1] ? '256' : '16';
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 0 0
296 },
297 0 ▶ ' random! ' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
298 0 ▶ if ($_[1]) { opt('colorindex') .= 'R' }
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1]) { } 0 0 0
299 0 else { opt('colorindex') =~ s/R//gi }
300 },
301 0 ▶ ' uniqcolor |uc ' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
302 0 opt('colorindex') = 'U';
303 },
304  
305 ##
306 ## BLOCK
307 ##
308 ' paragraph |p ! ' => \ my $opt_p ,
309 ' border =s ' => \ my $opt_border ,
310 ' block =s ' => \ my @opt_block ,
311 ' blockend :s ' => \(my $opt_blockend) ,
312 ' join-blocks ! ' => \(my $opt_join_blocks = 0) ,
313  
314 ##
315 ## REGION
316 ##
317 ' inside =s ' => \ my @opt_inside ,
318 ' outside =s ' => \ my @opt_outside ,
319 ' include =s ' => \ my @opt_include ,
320 ' exclude =s ' => \ my @opt_exclude ,
321 ' strict ! ' => \(my $opt_strict = 0) ,
322  
323 ##
324 ## CHARACTER CODE
325 ##
326 ' icode =s ' => \ my @opt_icode ,
327 ' ocode =s ' => \ my $opt_ocode ,
328  
329 ##
330 ## FILTER
331 ##
332 ' if =s ' => \ my @opt_if ,
333 ' of =s ' => \ my @opt_of ,
334 ' pf =s ' => \ my @opt_pf ,
335 ' noif ' => \ my $opt_noif ,
336  
337 ##
338 ## RUNTIME FUNCTION
339 ##
340 ' print =s ' => \ my @opt_print ,
341 ' continue ! ' => \ my $opt_continue ,
342 ' callback =s ' => \ my @opt_callback ,
343 ' begin =s ' => \ my @opt_begin ,
344 ' end =s ' => \ my @opt_end ,
345 ' prologue =s ' => \ my @opt_prologue ,
346 ' epilogue =s ' => \ my @opt_epilogue ,
347 ' postgrep =s ' => \ my @opt_postgrep ,
348  
349 ##
350 ## OTHERS
351 ##
352 ' usage :s ' => \ my $opt_usage ,
353 ' version ' => \ my $opt_version ,
354 ' exit =i ' => \ my $opt_exit ,
355 # norc
356 ' man | doc ' => \ my $opt_man ,
357 ' show | less ' => \ my $opt_show ,
358 ' path ' => \ my $opt_path ,
359 ' error =s ' => \(my $opt_error = 'skip') ,
360 ' alert =i ' => \ %opt_alert ,
361 ' parallel |P :i ' => \ my $opt_parallel ,
362 ' debug |d =s ' => \ my @opt_d ,
363 3 ▶ 'warn|w:1%' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
364 9 map { $opt_warn{$_} = $_[2] }
365 3 ▶ map { $_ eq 'all' ? keys %opt_warn : $_ }
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'all' ? : 2 1 3
366 $_[1] =~ /\w+/g;
367 },
368  
369 ##
370 ## MODULE
371 ##
372 0 ▶ 'M:s' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
373 0 warn "Use -M option at the beginning with module name.\n";
374 0 ▶ if (my @modules = uniq($rcloader->modules())) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@modules) = &uniq($rcloader->modules)) 0 0 0
375 0 warn "Available modules:\n";
376 0 warn "\t", join("\n\t", @modules), "\n";
377 }
378 0 exit 2;
379 },
380  
381 ##
382 ## UNDOCUMENTED
383 ##
384 170 ' clean ! ' => \ my $opt_clean ,
385  
386 ;
387  
388 0 ▶sub setopt {
Subroutine setopt: not called CC=7 SCAR=40.3
389 0 ▶ my $opt = ref $_[0] eq 'HASH' ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
390 0 my $name = shift;
391 0 ▶ if (exists $optargs{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $optargs{$name}) 0 0 0
392 0 my $ref = $optargs{$name};
393 0 ▶ if (ref $ref eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $ref eq 'ARRAY') { } 0 0 0
elsif (ref $ref eq 'CODE') { } 0 0 0
elsif (ref $ref eq 'SCALAR') { } 0 0 0
394 0 ▶ if ($opt->{append}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'append'}) { } 0 0 0
395 0 push @$ref, @_;
396 } else {
397 0 @$ref = @_;
398 }
399 }
400 elsif (ref $ref eq 'CODE') {
401 0 &$ref($name, @_);
402 }
403 elsif (ref $ref eq 'SCALAR') {
404 0 $$ref = shift;
405 }
406 else {
407 0 die "Object error.";
408 }
409 }
410 }
411  
412 ##
413 ## @ARGV stuff
414 ##
415  
416 170 require Getopt::Long;
417 170 my $parser = Getopt::Long::Parser->new(
418 config => [ qw(bundling no_getopt_compat no_ignore_case) ],
419 );
420 0 ▶sub configure_getopt { $parser->configure(@_) }
Subroutine configure_getopt: not called CC=1 SCAR=6.9
421  
422 170 ▶configure_getopt qw(debug) if $ENV{DEBUG_GETOPT};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG_GETOPT"} 0 170 170
423 170 ▶$Getopt::EX::Loader::debug = 1 if $ENV{DEBUG_GETOPTEX};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG_GETOPTEX"} 0 170 170
424  
425 ## decode
426 my @ORIG_ARGV =
427 170 ▶ @ARGV = map { utf8::is_utf8($_) ? $_ : decode('utf8', $_) } @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($_) ? : 0 831 831
428  
429 ## ~/.greplerc
430 170 ▶unless ((@ARGV and $ARGV[0] eq "--norc" and shift)
Branch: 1 branch
BranchTrueFalseTotal
unless (@ARGV and $ARGV[0] eq "--norc" and shift @ARGV or $ENV{"GREPLE_NORC"}) 170 0 170
Condition44%4/9
@ARGV and $ARGV[0] eq "--norc" and shift @ARGV or $ENV{"GREPLE_NORC"}
l!l&&r!l&&!r
00170
@ARGV and $ARGV[0] eq "--norc" and shift @ARGV
!ll&&!rl&&r
17000
@ARGV and $ARGV[0] eq "--norc"
!ll&&!rl&&r
11690
MC/DC0%0/4
@ARGV and $ARGV[0] eq "--norc" and shift @ARGV or $ENV{"GREPLE_NORC"}
@ARGV $ARGV[0] eq "--norc" shift @ARGV $ENV{"GREPLE_NORC"}
Truth table28%2/7
A and B and C or D
A= @ARGVB= $ARGV[0] eq "--norc"C= shift @ARGVD= $ENV{"GREPLE_NORC"}
ABCDresult
111X1
0XX11
10X11
11011
0XX00
10X00
11000
431 or
432 ($ENV{GREPLE_NORC}) ) {
433 170 $rcloader->load(FILE => "$ENV{HOME}/.greplerc");
434 }
435  
436 ## modules
437 170 $rcloader->deal_with(\@ARGV);
438  
439 170 push @optargs, $rcloader->builtins;
440  
441 ## ENV
442 170 ▶$ENV{'GREPLEOPTS'} and unshift @ARGV, shellwords($ENV{'GREPLEOPTS'});
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"GREPLEOPTS"} and unshift @ARGV, shellwords($ENV{"GREPLEOPTS"}) 0 170 170
443  
444  
445 ## GetOptions
446 170 my @SAVEDARGV = @ARGV;
447 170 ▶$parser->getoptions(@optargs) || usage();
Branch: 1 branch
BranchTrueFalseTotal
$parser->getoptions(@optargs) or usage() 170 0 170
448  
449 170 ▶if ($opt_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_version) 0 170 170
450 0 print "$version\n";
451 0 exit 0;
452 }
453  
454 170 our %opt_d;
455 0 @opt_d = map { split // } @opt_d;
456 170 @opt_d{@opt_d} = @opt_d;
457  
458 170 ▶if ($opt_d{o}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::opt_d{"o"}) 0 170 170
459 0 warn "\@ARGV = ", join(' ', shellquote(@SAVEDARGV)), "\n";
460 }
461  
462 ## -m option
463 170 ▶my @splicer = (not defined $opt_m) ? () : do {
Branch: 1 branch
BranchTrueFalseTotal
!defined($opt_m) ? : 169 1 170
464 1 my @param = split /,/, $opt_m, -1;
465 1 ▶ push @param, '' if @param % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @param % 2 1 0 1
466 1 ▶ if (notall { /^(-?\d+)?$/ } @param) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if (&notall(sub { /^(-?\d+)?$/u; } , @param)) 0 1 1
467 0 die "$opt_m: option format error.\n";
468 }
469 map {
470 1 my($offset, $length) = @$_;
471 1 ▶ if ($length ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($length ne '') { } 0 1 1
472 0 ▶ sub { splice @{+shift}, $offset || 0, $length }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$offset || 0
l!l
00
MC/DC0%0/1
$offset || 0
$offset
Truth table0%0/2
A
A= $offset
Aresult
11
00
473 0 } else {
474 1 ▶ sub { splice @{+shift}, $offset || 0 }
Subroutine __ANON__: called CC=2 SCAR=7.2
Condition50%1/2
$offset || 0
l!l
10
MC/DC0%0/1
$offset || 0
$offset
Truth table50%1/2
A
A= $offset
Aresult
11
00
475 1 }
476 }
477 pairs @param;
478 };
479  
480 170 my $file_code;
481 170 my $default_icode = 'utf8'; # default input encoding
482 170 my @default_icode_list = qw(euc-jp 7bit-jis);
483 170 my $output_code;
484 170 my $default_ocode = 'utf8'; # default output encoding
485  
486 170 ▶$output_code = $opt_ocode || $default_ocode;
Condition33%1/3
$opt_ocode || $default_ocode
l!l&&r!l&&!r
01700
MC/DC0%0/2
$opt_ocode || $default_ocode
$opt_ocode $default_ocode
Truth table33%1/3
A || B
A= $opt_ocodeB= $default_ocode
ABresult
1X1
011
000
487 170 binmode STDOUT, ":encoding($output_code)";
488  
489 ## show unused option characters
490 170 ▶if ($opt_d{u}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::opt_d{"u"}) 0 170 170
491 0 my $s = join('','0'..'9',"\n",'a'..'z',"\n",'A'..'Z',"\n");
492 0 ▶ map { /\|([0-9a-zA-Z])\b/ && $s =~ s/$1/./ } @optargs;
Condition0%0/3
/\|([0-9a-zA-Z])\b/u && $s =~ s/$1/./u
!ll&&!rl&&r
000
MC/DC0%0/2
/\|([0-9a-zA-Z])\b/u && $s =~ s/$1/./u
/\|([0-9a-zA-Z])\b/u $s =~ s/$1/./u
Truth table0%0/3
A && B
A= /\|([0-9a-zA-Z])\b/uB= $s =~ s/$1/./u
ABresult
0X0
100
111
493 0 die $s;
494 }
495  
496 ## show man pages
497 170 ▶if ($opt_man or $opt_show or $opt_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_man or $opt_show or $opt_path) 0 170 170
Condition33%2/6
$opt_man or $opt_show or $opt_path
l!l&&r!l&&!r
00170
$opt_man or $opt_show
l!l&&r!l&&!r
00170
MC/DC0%0/3
$opt_man or $opt_show or $opt_path
$opt_man $opt_show $opt_path
Truth table25%1/4
A or B or C
A= $opt_manB= $opt_showC= $opt_path
ABCresult
1XX1
01X1
0011
0000
498 my @module = map {
499 0 ▶ /^-M(\w+(::\w++(?![=(]))*)/ ? "App::Greple::$1" : ()
Branch: 1 branch
BranchTrueFalseTotal
/^-M(\w+(::\w++(?![=(]))*)/u ? : 0 0 0
500 } @ORIG_ARGV;
501 0 ▶ if (@module) {
Branch: 1 branch
BranchTrueFalseTotal
if (@module) 0 0 0
502 0 my $module = $module[-1];
503 0 ▶ my $jp = first { -x "$_/perldocjp" } split /:/, $ENV{PATH};
Subroutine __ANON__: not called CC=1 SCAR=6.9
504 0 ▶ my $perldoc = $jp ? "perldocjp" : "perldoc";
Branch: 1 branch
BranchTrueFalseTotal
$jp ? : 0 0 0
505 0 $ENV{PERL5LIB} = join ':', @INC;
506 0 my $file = $module =~ s[::][/]gr . '.pm';
507 0 ▶ die unless $INC{$file};
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{$file} 0 0 0
508 0 ▶ if ($opt_man) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_man) { } 0 0 0
509 0 ▶ exec "$perldoc $module" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
exec "$perldoc $module" or die $! 0 0 0
510 } else {
511 0 chomp(my $file = `$perldoc -ml $module`);
512 0 ▶ if ($opt_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_path) { } 0 0 0
513 0 say $file;
514 } else {
515 0 ▶ my $pager = $ENV{PAGER} || 'less';
Condition0%0/2
$ENV{'PAGER'} || 'less'
l!l
00
MC/DC0%0/1
$ENV{'PAGER'} || 'less'
$ENV{'PAGER'}
Truth table0%0/2
A
A= $ENV{'PAGER'}
Aresult
11
00
516 0 ▶ exec "$pager $file" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
exec "$pager $file" or die $! 0 0 0
517 }
518 }
519 0 exit;
520 }
521 0 pod2usage({-verbose => 2});
522 0 die;
523 }
524  
525 0 ▶sub default_module {
Subroutine default_module: not called CC=3 SCAR=24.8
526 0 my $mod = shift;
527 0 my $module = $mod->module;
528 0 ▶ return 1 if $module =~ /\b \.greplerc $/x;
Branch: 1 branch
BranchTrueFalseTotal
if $module =~ /\b \.greplerc $/ux 0 0 0
529 0 ▶ return 1 if $module =~ /\b default $/x;
Branch: 1 branch
BranchTrueFalseTotal
if $module =~ /\b default $/ux 0 0 0
530 0 return 0;
531 }
532  
533 ## setup file encoding
534 170 ▶if (@opt_icode) {
Branch: 1 branch
BranchTrueFalseTotal
if (@opt_icode) { } 4 166 170
535 4 @opt_icode = map { split /[,\s]+/ } @opt_icode;
536 4 ▶ if (grep { s/^\+// } @opt_icode) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {s/^\+//u;} @opt_icode) 1 3 4
537 1 unshift @opt_icode, @default_icode_list;
538 }
539 4 @opt_icode = uniq @opt_icode;
540 4 ▶ if (@opt_icode > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@opt_icode > 1) { } 1 3 4
elsif ($opt_icode[0] =~ /^(?:guess|auto)$/iu) { } 1 2 3
541 1 @opt_icode = grep { !/(?:auto|guess)$/i } @opt_icode;
542 1 Encode::Guess->set_suspects(@opt_icode);
543 1 $file_code = 'Guess';
544 }
545 elsif ($opt_icode[0] =~ /^(?:guess|auto)$/i) {
546 1 Encode::Guess->set_suspects(@default_icode_list);
547 1 $file_code = 'Guess';
548 } else {
549 2 $file_code = $opt_icode[0];
550 }
551 }
552 else {
553 166 $file_code = $default_icode;
554 }
555  
556 ##
557 ## --filter
558 ##
559 170 ▶if ($opt_filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_filter) 6 164 170
560 6 $opt_all = 1;
561 6 push @opt_need, '0';
562 6 ▶ $opt_exit //= 0;
Condition50%1/2
$opt_exit //= 0
l!l
06
MC/DC0%0/1
$opt_exit //= 0
$opt_exit
Truth table50%1/2
A
A= $opt_exit
Aresult
11
00
563 }
564  
565 ##
566 ## --parallel
567 ## Value 0 (or omitted) means the number of CPU cores.
568 ##
569 1 ▶sub ncpu {
Subroutine ncpu: called CC=5 SCAR=18.4
570 1 ▶ my $n = do {
Condition50%1/2
do { if ($^O =~ /darwin|bsd/iu) { `sysctl -n hw.ncpu 2>/dev/null`; } elsif (-r "/proc/cpuinfo") { open my $fh, "<", "/proc/cpuinfo"; scalar grep({/^processor\s*:/u;} readline $fh); } else { $ENV{"NUMBER_OF_PROCESSORS"}; } } // ""
l!l
10
MC/DC0%0/1
do { if ($^O =~ /darwin|bsd/iu) { `sysctl -n hw.ncpu 2>/dev/null`; } elsif (-r "/proc/cpuinfo") { open my $fh, "<", "/proc/cpuinfo"; scalar grep({/^processor\s*:/u;} readline $fh); } else { $ENV{"NUMBER_OF_PROCESSORS"}; } } // ""
do { if ($^O =~ /darwin|bsd/iu) { `sysctl -n hw.ncpu 2>/dev/null`; } elsif (-r "/proc/cpuinfo") { open my $fh, "<", "/proc/cpuinfo"; scalar grep({/^processor\s*:/u;} readline $fh); } else { $ENV{"NUMBER_OF_PROCESSORS"}; } }
Truth table50%1/2
A
A= do { if ($^O =~ /darwin|bsd/iu) { `sysctl -n hw.ncpu 2>/dev/null`; } elsif (-r "/proc/cpuinfo") { open my $fh, "<", "/proc/cpuinfo"; scalar grep({/^processor\s*:/u;} readline $fh); } else { $ENV{"NUMBER_OF_PROCESSORS"}; } }
Aresult
11
00
571 1 ▶ if ($^O =~ /darwin|bsd/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O =~ /darwin|bsd/iu) { } 0 1 1
elsif (-r '/proc/cpuinfo') { } 1 0 1
572 0 `sysctl -n hw.ncpu 2>/dev/null`;
573 } elsif (-r "/proc/cpuinfo") {
574 1 open my $fh, "<", "/proc/cpuinfo";
575 1 scalar grep { /^processor\s*:/ } <$fh>;
576 } else {
577 0 $ENV{NUMBER_OF_PROCESSORS};
578 }
579 } // '';
580 1 ▶ $n =~ /(\d+)/ ? $1 : 4;
Branch: 1 branch
BranchTrueFalseTotal
$n =~ /(\d+)/u ? : 1 0 1
581 }
582 170 ▶if (defined $opt_parallel and $opt_parallel == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt_parallel and $opt_parallel == 0) 1 169 170
Condition100%3/3
defined $opt_parallel and $opt_parallel == 0
!ll&&!rl&&r
154151
MC/DC100%2/2
defined $opt_parallel and $opt_parallel == 0
defined $opt_parallel $opt_parallel == 0
Truth table100%3/3
A and B
A= defined $opt_parallelB= $opt_parallel == 0
ABresult
0X0
100
111
583 1 $opt_parallel = ncpu();
584 }
585  
586 ##
587 ## Patterns
588 ##
589  
590 170 my $pat_holder = App::Greple::Pattern::Holder->new;
591  
592 170 my $FLAG_BASE = FLAG_NONE;
593 170 ▶$FLAG_BASE |= FLAG_IGNORECASE if $opt_i;
Branch: 1 branch
BranchTrueFalseTotal
if $opt_i 1 169 170
594  
595 170 ▶if (@opt_f) {
Branch: 1 branch
BranchTrueFalseTotal
if (@opt_f) { } 9 161 170
596 9 for my $opt_f (@opt_f) {
597 9 ▶ $pat_holder->append({ flag => $FLAG_BASE, type => 'file',
Branch: 1 branch
BranchTrueFalseTotal
$opt_select ? : 4 5 9
598 $opt_select ? (select => $opt_select) : (),
599 },
600 $opt_f);
601 }
602 } else {
603 161 ▶ unless ($opt_filter or grep { $_->[0] !~ /^(not|may)/ } @opt_pattern) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt_filter or grep {not $_->[0] =~ /^(not|may)/u;} @opt_pattern) 29 132 161
Condition100%3/3
$opt_filter or grep {not $_->[0] =~ /^(not|may)/u;} @opt_pattern
l!l&&r!l&&!r
612629
MC/DC100%2/2
$opt_filter or grep {not $_->[0] =~ /^(not|may)/u;} @opt_pattern
$opt_filter grep {not $_->[0] =~ /^(not|may)/u;} @opt_pattern
Truth table100%3/3
A or B
A= $opt_filterB= grep {not $_->[0] =~ /^(not|may)/u;} @opt_pattern
ABresult
1X1
011
000
604 29 ▶ unshift @opt_pattern, [ le => shift @ARGV // &usage ];
Condition66%2/3
shift @ARGV // &usage
l!l&&r!l&&!r
2801
MC/DC0%0/2
shift @ARGV // &usage
shift @ARGV &usage
Truth table33%1/3
A // B
A= shift @ARGVB= &usage
ABresult
1X1
011
000
605 }
606 }
607  
608 169 my %pat_flag = (
609 must => FLAG_REGEX | FLAG_COOK | FLAG_REQUIRED,
610 not => FLAG_REGEX | FLAG_COOK | FLAG_NEGATIVE,
611 may => FLAG_REGEX | FLAG_COOK | FLAG_OPTIONAL,
612 le => FLAG_REGEX | FLAG_COOK | FLAG_LEXICAL,
613 and => FLAG_REGEX | FLAG_COOK,
614 re => FLAG_REGEX,
615 fe => FLAG_NONE,
616 );
617 169 for (@opt_pattern) {
618 212 my($attr, @opt) = @$_;
619 212 my $flag = $FLAG_BASE | $pat_flag{$attr};
620 212 $pat_holder->append({ flag => $flag, type => 'pattern' }, @opt);
621 }
622 # $pat_holder->optimize;
623  
624 ##
625 ## if optional pattern exist, make all non-optional pattern as required
626 ##
627 {
628 167 my @patterns = $pat_holder->patterns;
629 167 my @posi = grep { $_->is_positive } @patterns;
630 167 my @opti = grep { $_->is_optional } @posi;
631 167 ▶ if (@opti > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@opti > 0) 5 162 167
632 5 for my $p (grep { !$_->is_optional } @posi) {
633 5 $p->flag($p->flag | FLAG_REQUIRED);
634 }
635 }
636 }
637  
638 ##
639 ## set $count_must, $count_need and $opt_allow
640 ##
641 167 my $count_must = 0;
642 167 my $count_need;
643 167 my $count_allow = 0;
644 {
645 167 my $must = grep({ $_->is_required } $pat_holder->patterns);
646 167 my $posi = grep({ $_->is_positive } $pat_holder->patterns) - $must;
647 167 my $nega = grep({ $_->is_negative } $pat_holder->patterns);
648  
649 167 ▶ $count_must = $must // 0;
Condition50%1/2
$must // 0
l!l
1670
MC/DC0%0/1
$must // 0
$must
Truth table50%1/2
A
A= $must
Aresult
11
00
650 167 ▶ $count_need = $must ? 0 : $posi;
Branch: 1 branch
BranchTrueFalseTotal
$must ? : 12 155 167
651 167 for (@opt_need) {
652 14 ▶ if (/^-(\d+)$/) { # --need -n
Branch: 3 branches
BranchTrueFalseTotal
if (/^-(\d+)$/u) { } 0 14 14
elsif (/^\+(\d+)$/u) { } 0 14 14
elsif (/^(\d+)$/u) { } 14 0 14
653 0 $count_need = $posi - $1;
654 }
655 elsif (/^\+(\d+)$/) { # --need +n
656 0 $count_need += $1;
657 }
658 elsif (/^(\d+)$/) { # --need n
659 14 $count_need = $1 - $must;
660 }
661 else {
662 0 die "$_ is not valid count.\n"
663 }
664 }
665  
666 167 $count_allow = 0;
667 167 for (@opt_allow) {
668 0 ▶ if (/^-(\d+)$/) { # --allow -n
Branch: 3 branches
BranchTrueFalseTotal
if (/^-(\d+)$/u) { } 0 0 0
elsif (/^\+(\d+)$/u) { } 0 0 0
elsif (/^(\d+)$/u) { } 0 0 0
669 0 $count_allow = $nega - $1;
670 }
671 elsif (/^\+(\d+)$/) { # --allow +n
672 0 $count_allow += $1;
673 }
674 elsif (/^(\d+)$/) { # --allow n
675 0 $count_allow = $1;
676 }
677 else {
678 0 die "$_ is not valid count.\n"
679 }
680 }
681 }
682  
683 ##
684 ## --matchcount
685 ##
686 167 ▶my $count_match_sub = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
687 167 ▶ local $_ = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
local $_ = shift() or return 9 158 167
688 9 ▶ /[^\d,]/ and die "$_ is not valid count.\n";
Branch: 1 branch
BranchTrueFalseTotal
/[^\d,]/u and die "$_ is not valid count.\n" 1 8 9
689 8 ▶ my @c = map { $_ eq '' ? 0 : int } split(/,/, $_, -1);
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '' ? : 3 17 20
690 8 ▶ if (@c == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@c == 1) 1 7 8
691 1 ▶ return sub { $_[0] == $c[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
692 }
693 7 ▶ push @c, -1 if @c % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @c % 2 1 6 7
694 60 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
695 60 my @n = @c;
696 60 ▶ while (my($min, $max) = splice(@n, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@n, 0, 2)) 84 10 94
697 84 ▶ return 0 if $_[0] < $min;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] < $min 33 51 84
698 51 ▶ return 1 if $max <= 0 || $_[0] <= $max;
Branch: 1 branch
BranchTrueFalseTotal
if $max <= 0 or $_[0] <= $max 17 34 51
Condition100%3/3
$max <= 0 or $_[0] <= $max
l!l&&r!l&&!r
71034
MC/DC100%2/2
$max <= 0 or $_[0] <= $max
$max <= 0 $_[0] <= $max
Truth table100%3/3
A or B
A= $max <= 0B= $_[0] <= $max
ABresult
1X1
011
000
699 }
700 10 return 0;
701 }
702 167 }->($opt_matchcount);
703  
704 ##
705 ## setup input/output filter
706 ##
707 166 my $filter_d = App::Greple::Filter->new->parse(@opt_if);
708 166 ▶unless ($opt_noif) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt_noif) 166 0 166
709 $filter_d->append(
710 167 ▶ [ sub { s/\.Z$// }, 'zcat' ],
Subroutine __ANON__: called CC=1 SCAR=0.0
711 167 ▶ [ sub { s/\.g?z$// }, 'gunzip -c' ],
Subroutine __ANON__: called CC=1 SCAR=0.0
712 167 ▶ [ sub { m/\.pdf$/i }, 'pdftotext -nopgbrk - -' ],
Subroutine __ANON__: called CC=1 SCAR=0.0
713 166 ▶ [ sub { s/\.gpg$// }, 'gpg --quiet --no-mdc-warning --decrypt' ],
Subroutine __ANON__: called CC=1 SCAR=0.0
714 );
715 }
716  
717 ##------------------------------------------------------------
718 ## miscellaneous setups
719 ##
720  
721 166 my @argv_files;
722 my $start_directory;
723 166 ▶my $need_filename = ($opt_H or $opt_l);
Condition66%2/3
$opt_H || $opt_l
l!l&&r!l&&!r
40162
MC/DC50%1/2
$opt_H || $opt_l
$opt_H $opt_l
Truth table66%2/3
A || B
A= $opt_HB= $opt_l
ABresult
1X1
011
000
724 166 my $current_file;
725  
726 166 ▶if (@opt_chdir) {
Branch: 2 branches
BranchTrueFalseTotal
if (@opt_chdir) { } 0 166 166
elsif (@opt_glob) { } 0 166 166
727 0 $start_directory = getcwd;
728 0 @opt_chdir = uniq(map { glob $_ } @opt_chdir);
729 0 push @argv_files, splice(@ARGV);
730 0 ▶ unless ($opt_h or
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt_h or @opt_chdir == 1 and @argv_files == 1 and @opt_glob == 0) 0 0 0
Condition0%0/9
$opt_h or @opt_chdir == 1 and @argv_files == 1 and @opt_glob == 0
l!l&&r!l&&!r
000
@opt_chdir == 1 and @argv_files == 1 and @opt_glob == 0
!ll&&!rl&&r
000
@opt_chdir == 1 and @argv_files == 1
!ll&&!rl&&r
000
MC/DC0%0/4
$opt_h or @opt_chdir == 1 and @argv_files == 1 and @opt_glob == 0
$opt_h @opt_chdir == 1 @argv_files == 1 @opt_glob == 0
Truth table0%0/5
A or B and C and D
A= $opt_hB= @opt_chdir == 1C= @argv_files == 1D= @opt_glob == 0
ABCDresult
1XXX1
01111
00XX0
010X0
01100
731 (@opt_chdir == 1 and @argv_files == 1 and @opt_glob == 0)) {
732 0 $need_filename++;
733 }
734 }
735 elsif (@opt_glob) {
736 0 push @ARGV, map(glob, @opt_glob);
737 }
738  
739 166 ▶push(@ARGV, '-') unless @ARGV || @argv_files || @opt_glob || $opt_readlist;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV or @argv_files or @opt_glob or $opt_readlist 1 165 166
Condition66%6/9
@ARGV or @argv_files or @opt_glob or $opt_readlist
l!l&&r!l&&!r
16501
@ARGV or @argv_files or @opt_glob
l!l&&r!l&&!r
16501
@ARGV or @argv_files
l!l&&r!l&&!r
16501
MC/DC25%1/4
@ARGV or @argv_files or @opt_glob or $opt_readlist
@ARGV @argv_files @opt_glob $opt_readlist
Truth table40%2/5
A or B or C or D
A= @ARGVB= @argv_filesC= @opt_globD= $opt_readlist
ABCDresult
1XXX1
01XX1
001X1
00011
00000
740 166 ▶if ((@ARGV > 1 or $opt_readlist) and not $opt_h) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV > 1 || $opt_readlist and not $opt_h) 5 161 166
Condition66%4/6
@ARGV > 1 || $opt_readlist and not $opt_h
!ll&&!rl&&r
16105
@ARGV > 1 or $opt_readlist
l!l&&r!l&&!r
50161
MC/DC33%1/3
@ARGV > 1 || $opt_readlist and not $opt_h
@ARGV > 1 $opt_readlist not $opt_h
Truth table40%2/5
A or B and not(C)
A= @ARGV > 1B= $opt_readlistC= not $opt_h
ABCresult
00X0
1X00
0100
1X11
0111
741 5 $need_filename++;
742 }
743  
744 166 ▶$opt_filestyle = 'none' if not $need_filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $need_filename 157 9 166
745  
746 166 ▶$opt_join = 1 if $opt_joinby ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $opt_joinby ne "" 0 166 166
747  
748 ##------------------------------------------------------------
749 ## colors
750 ##
751 166 our %colormap = (
752 FILE => "G",
753 LINE => "Y",
754 BLOCK => "B",
755 TEXT => "",
756 BLOCKEND => "/WE",
757 PROGRESS => "B",
758 TOP => "",
759 MIDDLE => "",
760 BOTTOM => "",
761 );
762  
763 166 our @colors;
764  
765 170 ▶use Getopt::EX::Colormap;
Subroutine BEGIN: called CC=1 SCAR=0.0
766 166 my $color_handler = Getopt::EX::Colormap
767 ->new(HASH => \%colormap, LIST => \@colors)
768 ->load_params(@opt_colormap);
769  
770 166 ▶my @default_color =
Branch: 1 branch
BranchTrueFalseTotal
$opt_ansicolor eq '16' ? : 0 166 166
771 $opt_ansicolor eq '16'
772 ? qw(RD GD BD CD MD YD)
773 : qw(000D/544 000D/454 000D/445
774 000D/455 000D/545 000D/554
775 000D/543 000D/453 000D/435
776 000D/534 000D/354 000D/345
777 000D/444
778 000D/433 000D/343 000D/334
779 000D/344 000D/434 000D/443
780 000D/333)
781 ;
782  
783 166 ▶if ($color_handler->list == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($color_handler->list == 0) 163 3 166
784 163 ▶ $color_handler->append
Branch: 1 branch
BranchTrueFalseTotal
$opt_colorful ? : 163 0 163
785 ($opt_colorful ? @default_color : $default_color[0]);
786 }
787  
788 166 ▶if ($opt_ansicolor eq '24bit') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_ansicolor eq "24bit") 0 166 166
789 170 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
790 0 $Getopt::EX::Colormap::RGB24 = 1;
791 }
792  
793 166 for my $opt (@opt_face) {
794 0 ▶ while ($opt =~ /(?<mk>[-+=]) (?<s>[^-+=]*) | (?<s>[^-+=]+) /xg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt =~ /(?<mk>[-+=]) (?<s>[^-+=]*) | (?<s>[^-+=]+) /gux) 0 0 0
795 0 ▶ my($mk, $s) = ($+{mk} // '', $+{s});
Condition0%0/2
$+{"mk"} // ""
l!l
00
MC/DC0%0/1
$+{"mk"} // ""
$+{"mk"}
Truth table0%0/2
A
A= $+{"mk"}
Aresult
11
00
796 0 for my $c (@colors) {
797 0 ▶ if ($mk eq '-') {
Branch: 3 branches
BranchTrueFalseTotal
if ($mk eq '-') { } 0 0 0
elsif ($mk eq '=') { } 0 0 0
elsif ($s ne '') { } 0 0 0
798 0 ▶ $c =~ s/[\Q$s\E]//g if $s ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $s ne "" 0 0 0
799 } elsif ($mk eq '=') {
800 0 $c = $s;
801 } elsif ($s ne '') {
802 0 ▶ $c .= "^" if $c ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $c ne "" 0 0 0
803 0 $c .= $s;
804 }
805 }
806 }
807 }
808  
809 166 ▶my $need_color = (($opt_color eq 'always')
Condition66%6/9
$opt_color eq 'always' || $opt_color eq 'auto' && (!$opt_o && -t STDOUT)
l!l&&r!l&&!r
10165
$opt_color eq "auto" and (not $opt_o and -t STDOUT)
!ll&&!rl&&r
121530
not $opt_o and -t STDOUT
!ll&&!rl&&r
261270
MC/DC25%1/4
$opt_color eq 'always' || $opt_color eq 'auto' && (!$opt_o && -t STDOUT)
$opt_color eq 'always' $opt_color eq "auto" not $opt_o -t STDOUT
Truth table80%4/5
A || B and not(C) and D
A= $opt_color eq 'always'B= $opt_color eq "auto"C= not $opt_oD= -t STDOUT
ABCDresult
1XXX1
01111
00XX0
010X0
01100
810 or (($opt_color eq 'auto') and (!$opt_o and -t STDOUT)));
811  
812 166 ▶if (!$need_color) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($need_color) 165 1 166
813 165 $Getopt::EX::Colormap::NO_COLOR = 1;
814 }
815  
816 166 my %_esc = ( t => "\t", n => "\n", r => "\r", f => "\f" );
817 501 ▶sub expand_escape {
Subroutine expand_escape: called CC=2 SCAR=14.0
818 0 ▶ $_[0] =~ s{\\(.)}{$_esc{$1} // $1}egr;
Condition0%0/3
$_esc{$1} // $1
l!l&&r!l&&!r
000
MC/DC0%0/2
$_esc{$1} // $1
$_esc{$1} $1
Truth table0%0/3
A // B
A= $_esc{$1}B= $1
ABresult
1X1
011
000
819 }
820  
821 166 $_ = expand_escape($_) for values %opt_format;
822  
823 166 my $blockend = "--";
824 166 ▶if (defined $opt_blockend) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt_blockend) 3 163 166
825 3 $blockend = expand_escape($opt_blockend);
826 }
827  
828 166 ▶my $_file = sub { $color_handler->color('FILE' , sprintf($opt_format{FILE}, $_[0])) };
Subroutine __ANON__: called CC=1 SCAR=0.0
829 166 ▶my $_line = sub { $color_handler->color('LINE' , sprintf($opt_format{LINE}, $_[0])) };
Subroutine __ANON__: called CC=1 SCAR=0.0
830 0 ▶my $_block = sub { $color_handler->color('BLOCK', sprintf($opt_format{BLOCK}, $_[0])) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
831 0 ▶my $_text = sub { $color_handler->color('TEXT' , $_[0]) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
832 166 my $_blockend = $color_handler->color('BLOCKEND', $blockend);
833 166 my $_top = $color_handler->color('TOP' , $opt_frame_top);
834 166 my $_middle = $color_handler->color('MIDDLE' , $opt_frame_middle);
835 166 my $_bottom = $color_handler->color('BOTTOM' , $opt_frame_bottom);
836  
837 336 ▶sub index_color {
Subroutine index_color: called CC=1 SCAR=0.0
838 336 $color_handler->index_color(@_);
839 }
840  
841 0 ▶sub color {
Subroutine color: not called CC=1 SCAR=6.9
842 0 $color_handler->color(@_);
843 }
844  
845 166 my $uniq_color = UniqIndex->new(
846 ignore_newline => 1,
847 prepare => \@opt_uniqsub,
848 );
849  
850 0 ▶sub dump_uniqcolor {
Subroutine dump_uniqcolor: not called CC=2 SCAR=17.9
851 0 my $list = $uniq_color->list;
852 0 my $count = $uniq_color->count;
853 0 for my $i (keys @$list) {
854 0 warn sprintf("%3d (%3d) %s\n",
855 $i, $count->[$i],
856 index_color($i, $list->[$i]));
857 }
858 }
859  
860 # --colorindex
861 166 my %color_index = map { uc $_ => 1 } $opt_colorindex =~ /\w/g;
862 166 my $indexer = do {
863 166 ▶ if ($color_index{S}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($color_index{"S"}) 0 166 166
864 0 @colors = shuffle @colors;
865 }
866 166 ▶ if ($color_index{A} or $color_index{D}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($color_index{'A'} or $color_index{'D'}) { } 0 166 166
elsif ($color_index{'R'}) { } 0 166 166
Condition33%1/3
$color_index{"A"} or $color_index{"D"}
l!l&&r!l&&!r
00166
MC/DC0%0/2
$color_index{"A"} or $color_index{"D"}
$color_index{"A"} $color_index{"D"}
Truth table33%1/3
A or B
A= $color_index{"A"}B= $color_index{"D"}
ABresult
1X1
011
000
867 0 my $i = 0;
868 Indexer->new(
869 0 ▶ index => sub { $i++ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
870 0 ▶ reset => sub { $i = 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
871 block => $color_index{B},
872 reverse => $color_index{D},
873 0 );
874 }
875 elsif ($color_index{R}) {
876 0 ▶ Indexer->new(index => sub { int rand @colors });
Subroutine __ANON__: not called CC=1 SCAR=6.9
877 }
878 166 else { undef }
879 };
880 166 my $opt_uniqcolor = $color_index{U};
881  
882 # -dc
883 166 ▶if ($opt_d{c}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::opt_d{"c"}) 0 166 166
884 0 ▶ my $dump = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
885 0 local $_ = Dumper shift;
886 0 s/^\s*'\K([^'\s]+)(?=')/color($1, $1)/mge;
887 0 $_;
888 0 };
889 0 warn 'colormap = ', $dump->(\%colormap);
890 0 warn 'colors = ', $dump->(\@colors);
891 }
892  
893 ##
894 ## border regex
895 ##
896 166 my $border_re = do {
897 166 ▶ if ($opt_border) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opt_border) { } 3 163 166
elsif ($opt_p) { } 6 157 163
898 3 qr/$opt_border/m; # custom
899 } elsif ($opt_p) {
900 6 qr/(?:\A|\R)\K\R+/; # paragraph
901 } else {
902 157 qr/^/m; # line
903 }
904 };
905  
906 166 ▶if ($opt_C) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_C) 4 162 166
907 4 ▶ $opt_A ||= $opt_C;
Condition33%1/3
$opt_A ||= $opt_C
l!l&&r!l&&!r
040
MC/DC0%0/2
$opt_A ||= $opt_C
$opt_A $opt_C
Truth table33%1/3
A ||= B
A= $opt_AB= $opt_C
ABresult
1X1
011
000
908 4 ▶ $opt_B ||= $opt_C;
Condition33%1/3
$opt_B ||= $opt_C
l!l&&r!l&&!r
040
MC/DC0%0/2
$opt_B ||= $opt_C
$opt_B $opt_C
Truth table33%1/3
A ||= B
A= $opt_BB= $opt_C
ABresult
1X1
011
000
909 }
910 166 my %stat = (
911 file_searched => 0,
912 file_tried => 0,
913 length => 0,
914 match_effective => 0,
915 match_positive => 0,
916 match_negative => 0,
917 match_block => 0,
918 read_retry => 0,
919 time_start => [],
920 time_end => [],
921 );
922 166 lock_keys %stat;
923  
924 ##
925 ## Setup functions
926 ##
927 166 for my $set (
928 [ "print" , \@opt_print , 0 ],
929 [ "begin" , \@opt_begin , 0 ],
930 [ "end" , \@opt_end , 0 ],
931 [ "prologue", \@opt_prologue, 0 ],
932 [ "epilogue", \@opt_epilogue, 0 ],
933 [ "callback", \@opt_callback, 0 ],
934 [ "uniqsub" , \@opt_uniqsub , 0 ],
935 [ "postgrep", \@opt_postgrep, 0 ],
936 [ "block" , \@opt_block , 1 ], # need &
937 [ "inside" , \@opt_inside , 1 ], # need &
938 [ "outside" , \@opt_outside , 1 ], # need &
939 [ "include" , \@opt_include , 1 ], # need &
940 [ "exclude" , \@opt_exclude , 1 ], # need &
941 ) {
942 2158 my($cat, $opt, $pattern) = @$set;
943 2158 for (@{$opt}) {
944 21 ▶ next if callable $_;
Branch: 1 branch
BranchTrueFalseTotal
if callable($_) 0 21 21
945 21 ▶ /^&\w+/ or next if $pattern;
Branch: 1 branch
BranchTrueFalseTotal
if $pattern 18 3 21
Condition60%3/5
$pattern && (/^&\w+/u || (next))
!ll&&!rl&&r
3180
/^&\w+/u or next
l!l
018
MC/DC0%0/2
$pattern && (/^&\w+/u || (next))
$pattern /^&\w+/u
Truth table66%2/3
A && B
A= $patternB= /^&\w+/u
ABresult
0X0
100
111
946 3 ▶ $_ = parse_func($_) or die "$cat function format error: $_\n";
Branch: 1 branch
BranchTrueFalseTotal
$_ = parse_func($_) or die "$cat function format error: $_\n" 3 0 3
947 }
948 }
949  
950 166 my $regions = App::Greple::Regions::Holder->new;
951 166 for my $set (
952 [ \@opt_inside, REGION_INSIDE | REGION_UNION ],
953 [ \@opt_outside, REGION_OUTSIDE | REGION_UNION ],
954 [ \@opt_include, REGION_INSIDE | REGION_INTERSECT ],
955 [ \@opt_exclude, REGION_OUTSIDE | REGION_INTERSECT ])
956 {
957 664 my($opt, $flag) = @$set;
958 664 for my $spec (@$opt) {
959 11 $regions->append(FLAG => $flag, SPEC => $spec);
960 }
961 }
962  
963 ##------------------------------------------------------------
964  
965 166 ▶if ($opt_d{m}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::opt_d{"m"}) 0 166 166
966 0 warn "Search pattern:\n";
967 0 my $i;
968 0 for my $pat ($pat_holder->patterns) {
969 0 ▶ my $type =
Branch: 3 branches
BranchTrueFalseTotal
$pat->is_required ? : 0 0 0
$pat->is_negative ? : 0 0 0
$pat->is_positive ? : 0 0 0
970 $pat->is_required ? 'must' :
971 $pat->is_negative ? 'not' :
972 $pat->is_positive ? 'and' : 'else';
973 0 ▶ my $target = $pat->regex // $pat->string;
Condition0%0/3
$pat->regex // $pat->string
l!l&&r!l&&!r
000
MC/DC0%0/2
$pat->regex // $pat->string
$pat->regex $pat->string
Truth table0%0/3
A // B
A= $pat->regexB= $pat->string
ABresult
1X1
011
000
974 0 ▶ warn sprintf(" %4s %1s %s\n",
Branch: 2 branches
BranchTrueFalseTotal
$pat->is_function ? : 0 0 0
@main::colors > 1 ? : 0 0 0
975 $type,
976 $pat->is_function ? '&' : '',
977 @colors > 1 ? index_color($i++, $target) : $target);
978 }
979 0 warn sprintf("must = %d, need = %d, allow = %d\n",
980 $count_must, $count_need, $count_allow);
981 }
982  
983 ## push post-process filter
984 166 ▶if (@opt_pf) {
Branch: 1 branch
BranchTrueFalseTotal
if (@opt_pf) 2 164 166
985 2 push_output_filter(\*STDOUT, @opt_pf);
986 }
987  
988 165 ▶usage() and exit if defined $opt_usage;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt_usage 0 165 165
Condition20%1/5
defined $opt_usage && (usage() && exit)
!ll&&!rl&&r
16500
usage() and exit
!ll
00
MC/DC0%0/2
defined $opt_usage && (usage() && exit)
defined $opt_usage usage()
Truth table33%1/3
A && B
A= defined $opt_usageB= usage()
ABresult
0X0
100
111
989  
990 165 ▶open SAVESTDIN, '<&', \*STDIN or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open SAVESTDIN, "<&", \*STDIN or die "open: $!" 165 0 165
991 165 ▶open SAVESTDOUT, '>&', \*STDOUT or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open SAVESTDOUT, ">&", \*STDOUT or die "open: $!" 165 0 165
992 165 ▶open SAVESTDERR, '>&', \*STDERR or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open SAVESTDERR, ">&", \*STDERR or die "open: $!" 165 0 165
993  
994 163 ▶sub recover_stdin {
Subroutine recover_stdin: called CC=2 SCAR=7.6
995 163 ▶ open STDIN, '<&', \*SAVESTDIN or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<&", \*SAVESTDIN or die "open: $!" 163 0 163
996 }
997 1 ▶sub recover_stderr {
Subroutine recover_stderr: called CC=2 SCAR=7.2
998 1 ▶ open STDERR, '>&', \*SAVESTDERR or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", \*SAVESTDERR or die "open: $!" 1 0 1
999 1 binmode STDERR, ':encoding(utf8)';
1000 }
1001 3 ▶sub recover_stdout {
Subroutine recover_stdout: called CC=2 SCAR=7.2
1002 3 close STDOUT;
1003 3 ▶ open STDOUT, '>&', \*SAVESTDOUT or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", \*SAVESTDOUT or die "open: $!" 3 0 3
1004 }
1005 160 ▶sub close_stdout {
Subroutine close_stdout: called CC=1 SCAR=0.0
1006 160 close SAVESTDOUT;
1007 160 close STDOUT;
1008 }
1009  
1010 0 ▶sub read_stdin { <SAVESTDIN> }
Subroutine read_stdin: not called CC=1 SCAR=6.9
1011  
1012 165 my $slurp = do {
1013 ##
1014 ## Setting utf8 warnings fatal makes it easy to find code conversion
1015 ## error, so you can choose appropriate file code or automatic code
1016 ## recognition, but loose a chance to find string in unrelated area.
1017 ##
1018 165 ▶ if ($opt_error =~ /^(?: fatal | skip | retry )$/x) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opt_error =~ /^(?: fatal | skip | retry )$/ux) { } 163 2 165
elsif ($opt_error eq 'ignore') { } 1 1 2
1019 163 ▶ if ($opt_warn{read}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_warn{'read'}) { } 1 162 163
1020 1 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
1021 170 ▶ use warnings FATAL => 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
1022 1 my $stdin = eval { local $/; <STDIN> };
1023 1 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 0 1
1024 1 $stdin;
1025 }
1026 1 } else {
1027 166 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1028 170 ▶ use warnings FATAL => 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
1029 166 eval { local $/; <STDIN> };
1030 }
1031 162 }
1032 } elsif ($opt_error eq 'ignore') {
1033 1 ▶ if ($opt_warn{read}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_warn{'read'}) { } 0 1 1
1034 0 ▶ sub { local $/; <STDIN> };
Subroutine __ANON__: not called CC=1 SCAR=6.9
1035 } else {
1036 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1037 1 close STDERR;
1038 1 my $stdin = do { local $/; <STDIN> };
1039 1 recover_stderr;
1040 1 $stdin;
1041 }
1042 1 }
1043 } else {
1044 1 die "$opt_error: invalid action.\n";
1045 }
1046 };
1047  
1048 170 ▶use Term::ANSIColor::Concise qw(ansi_code);
Subroutine BEGIN: called CC=1 SCAR=0.0
1049  
1050 170 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
1051 170 EL => ansi_code('{EL}'), # Erase Line
1052 ED => ansi_code('{ED}'), # Erase Display
1053 SCP => ansi_code('{SCP}'), # Save Cursor Position
1054 RCP => ansi_code('{RCP}'), # Restore Cursor Position
1055 DSC => ansi_code('{DECSC}'), # DEC Save Cursor
1056 DRC => ansi_code('{DECRC}'), # DEC Restore Cursor
1057 CR => "\r",
1058 170 };
1059  
1060 164 my($progress_show, $progress_reset) = do {
1061 164 my $n;
1062 my($s, $e) = ! $need_color ? ('', '') :
1063 164 ▶ ( ansi_code $colormap{PROGRESS}, ansi_code 'Z');
Branch: 1 branch
BranchTrueFalseTotal
!$need_color ? : 163 1 164
1064 0 ▶ my $print = sub { STDERR->printflush(DSC, $s, @_, $e, CR, DRC) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
1065 164 my $start = do {
1066 164 ▶ if ($opt_d{n} and $opt_d{f}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($main::opt_d{'n'} and $main::opt_d{'f'}) { } 0 164 164
elsif ($main::opt_d{'n'}) { } 0 164 164
elsif ($main::opt_d{'f'}) { } 0 164 164
Condition33%1/3
$main::opt_d{"n"} and $main::opt_d{"f"}
!ll&&!rl&&r
16400
MC/DC0%0/2
$main::opt_d{"n"} and $main::opt_d{"f"}
$main::opt_d{"n"} $main::opt_d{"f"}
Truth table33%1/3
A and B
A= $main::opt_d{"n"}B= $main::opt_d{"f"}
ABresult
0X0
100
111
1067 0 ▶ sub { $print->(++$n, " ", $current_file, ED) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
1068 0 }
1069 elsif ($opt_d{n}) {
1070 0 ▶ sub { $print->(++$n) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
1071 0 }
1072 elsif ($opt_d{f}) {
1073 0 ▶ sub { STDERR->printflush($current_file, ":\n") }
Subroutine __ANON__: not called CC=1 SCAR=6.9
1074 0 }
1075 else {
1076 164 undef;
1077 }
1078 };
1079 164 my $end = do {
1080 164 ▶ if ($opt_d{n}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::opt_d{'n'}) { } 0 164 164
1081 0 ▶ sub { STDERR->printflush(ED) if $n }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $n 0 0 0
1082 0 } else {
1083 164 undef;
1084 }
1085 };
1086 164 ($start, $end);
1087 };
1088  
1089 ##------------------------------------------------------------
1090 ## now ready to run.
1091 ##
1092  
1093 ## record start time
1094 164 ▶if ($opt_d{s}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::opt_d{"s"}) 0 164 164
1095 0 $stat{time_start} = [times];
1096 }
1097  
1098 0 for (@opt_prologue) { $_->call() }
1099  
1100 164 grep_files();
1101  
1102 0 for (@opt_epilogue) { $_->call() }
1103  
1104 160 ▶if ($opt_d{n}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::opt_d{"n"}) 0 160 160
1105 0 print STDERR ED;
1106 }
1107  
1108 160 ▶if ($opt_uniqcolor and $opt_d{c}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_uniqcolor and $main::opt_d{"c"}) 0 160 160
Condition33%1/3
$opt_uniqcolor and $main::opt_d{"c"}
!ll&&!rl&&r
16000
MC/DC0%0/2
$opt_uniqcolor and $main::opt_d{"c"}
$opt_uniqcolor $main::opt_d{"c"}
Truth table33%1/3
A and B
A= $opt_uniqcolorB= $main::opt_d{"c"}
ABresult
0X0
100
111
1109 0 dump_uniqcolor();
1110 }
1111  
1112 ## show statistic info
1113 160 ▶if ($opt_d{s}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::opt_d{"s"}) 0 160 160
1114  
1115 0 $stat{time_end} = [times];
1116 0 my @s = $stat{time_start}->@*;
1117 0 my @e = $stat{time_end}->@*;
1118 0 printf(STDERR "cpu %.3fu %.3fs\n", $e[0]-$s[0], $e[1]-$s[1]);
1119  
1120 0 local $" = ', ';
1121 0 for my $k (sort keys %stat) {
1122 0 my $v = $stat{$k};
1123 0 ▶ print STDERR
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 0 0 0
1124 "$k: ",
1125 ref $v eq 'ARRAY' ? "(@$v)" : $v,
1126 "\n";
1127 }
1128 }
1129  
1130 160 close_stdout;
1131  
1132 160 ▶if ($opt_d{p}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::opt_d{"p"}) 0 160 160
1133 0 open STDOUT, ">&STDERR";
1134 0 system "ps -lww -p $$";
1135 0 system "lsof -p $$";
1136 }
1137  
1138 160 ▶exit($opt_exit // ($stat{match_effective} == 0));
Condition100%2/2
$opt_exit // $stat{"match_effective"} == 0
l!l
8152
MC/DC100%1/1
$opt_exit // $stat{"match_effective"} == 0
$opt_exit
Truth table100%2/2
A
A= $opt_exit
Aresult
11
00
1139  
1140 ######################################################################
1141  
1142 164 ▶sub grep_files {
Subroutine grep_files: called CC=11 SCAR=25.0
1143 FILE:
1144 164 ▶ while (defined($current_file = open_nextfile())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($current_file = open_nextfile())) 166 160 326
1145 166 my $content = $slurp->();
1146 166 $stat{file_tried}++;
1147 166 ▶ if (not defined $content) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 7 159 166
1148 7 ▶ if ($opt_error eq 'fatal') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_error eq "fatal") 1 6 7
1149 1 die "ABORT on $current_file\n";
1150 }
1151 6 ▶ if ($opt_error ne 'retry') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_error ne "retry") 4 2 6
1152 4 ▶ warn "SKIP $current_file\n" if $opt_warn{skip};
Branch: 1 branch
BranchTrueFalseTotal
if $opt_warn{"skip"} 3 1 4
1153 4 next FILE;
1154 }
1155  
1156 # Try again
1157 2 binmode STDIN, ':raw';
1158 2 ▶ seek STDIN, 0, 0 or do {
Branch: 1 branch
BranchTrueFalseTotal
seek STDIN, 0, 0 or do { warn "SKIP $current_file (not seekable)\n" if $opt_warn{"skip"}; next FILE } 2 0 2
1159 warn "SKIP $current_file (not seekable)\n"
1160 0 ▶ if $opt_warn{skip};
Branch: 1 branch
BranchTrueFalseTotal
if $opt_warn{"skip"} 0 0 0
1161 0 next FILE;
1162 };
1163 2 $content = $slurp->();
1164 2 ▶ if (not defined $content) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 0 2 2
1165 0 ▶ warn "SKIP* $current_file\n" if $opt_warn{skip};
Branch: 1 branch
BranchTrueFalseTotal
if $opt_warn{"skip"} 0 0 0
1166 0 next FILE;
1167 }
1168 2 ▶ warn "RETRY $current_file\n" if $opt_warn{retry};
Branch: 1 branch
BranchTrueFalseTotal
if $opt_warn{"retry"} 1 1 2
1169 2 $stat{read_retry}++;
1170 2 binmode STDOUT, ':raw';
1171 }
1172  
1173 161 my $matched = grep_data(\$content);
1174  
1175 159 $stat{match_effective} += $matched;
1176 159 $stat{file_searched}++;
1177 159 $stat{length} += length $content;
1178 } continue {
1179 163 close STDIN; # wait; # wait for 4.019 or earlier?
1180 # recover STDIN for opening '-' and some weird command which needs
1181 # STDIN opened (like unzip)
1182 163 recover_stdin;
1183 163 binmode STDOUT, ":encoding($output_code)";
1184 }
1185 }
1186  
1187 1 ▶sub usage {
Subroutine usage: called CC=7 SCAR=33.9
1188 1 pod2usage(-verbose => 0, -exitval => "NOEXIT");
1189  
1190 1 my $quote = qr/[\\(){}\|\*?]/;
1191 1 for my $bucket ($rcloader->buckets) {
1192 0 my $module = $bucket->module;
1193 0 print " $module options:\n";
1194 0 for my $name ($bucket->options) {
1195 0 ▶ my $help = $opt_usage ? "" : $bucket->help($name) // "";
Branch: 1 branch
BranchTrueFalseTotal
$opt_usage ? : 0 0 0
Condition0%0/2
$bucket->help($name) // ""
l!l
00
MC/DC0%0/1
$bucket->help($name) // ""
$bucket->help($name)
Truth table0%0/2
A
A= $bucket->help($name)
Aresult
11
00
1196 0 ▶ next if $help eq 'ignore';
Branch: 1 branch
BranchTrueFalseTotal
if $help eq "ignore" 0 0 0
1197 0 my @option = $bucket->getopt($name, ALL => 1);
1198 0 ▶ printf(" %-20s %s\n", $name,
Condition0%0/3
$help || join(' ', shellquote(@option))
l!l&&r!l&&!r
000
MC/DC0%0/2
$help || join(' ', shellquote(@option))
$help join(' ', shellquote(@option))
Truth table0%0/3
A || B
A= $helpB= join(' ', shellquote(@option))
ABresult
1X1
011
000
1199 $help || join(' ', shellquote(@option)));
1200 }
1201 0 print "\n";
1202 }
1203  
1204 1 print "Version: $version\n";
1205  
1206 1 exit 2;
1207 }
1208  
1209 327 ▶sub open_nextfile {
Subroutine open_nextfile: called CC=20 SCAR=43.5
1210  
1211 ##
1212 ## --chdir
1213 ##
1214 327 ▶ while (@ARGV == 0 and @opt_chdir and (@argv_files or @opt_glob)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV == 0 and @opt_chdir and @argv_files || @opt_glob) 0 327 327
Condition33%3/9
@ARGV == 0 and @opt_chdir and @argv_files || @opt_glob
!ll&&!rl&&r
32700
@ARGV == 0 and @opt_chdir
!ll&&!rl&&r
1671600
@argv_files or @opt_glob
l!l&&r!l&&!r
000
MC/DC0%0/4
@ARGV == 0 and @opt_chdir and @argv_files || @opt_glob
@ARGV == 0 @opt_chdir @argv_files @opt_glob
Truth table40%2/5
A and B and C or D
A= @ARGV == 0B= @opt_chdirC= @argv_filesD= @opt_glob
ABCDresult
0XXX0
10XX0
11000
111X1
11011
1215 0 my $dir = shift @opt_chdir;
1216 0 ▶ warn "chdir $dir/\n" if $opt_d{d};
Branch: 1 branch
BranchTrueFalseTotal
if $main::opt_d{"d"} 0 0 0
1217 0 ▶ chdir $start_directory or die "$!: $start_directory\n";
Branch: 1 branch
BranchTrueFalseTotal
chdir $start_directory or die "$!: $start_directory\n" 0 0 0
1218 0 ▶ chdir $dir or die "$!: $dir\n";
Branch: 1 branch
BranchTrueFalseTotal
chdir $dir or die "$!: $dir\n" 0 0 0
1219 0 push @ARGV, @argv_files, map(glob, @opt_glob);
1220 }
1221  
1222 327 my $file;
1223 327 ▶ while (defined($file = shift(@ARGV)) ||
Branch: 1 branch
BranchTrueFalseTotal
if (defined($file = shift @ARGV) or defined($file = $opt_readlist && read_stdin())) 167 160 327
Condition50%3/6
defined($file = shift @ARGV) or defined($file = $opt_readlist && read_stdin())
l!l&&r!l&&!r
1670160
$opt_readlist && read_stdin()
!ll&&!rl&&r
16000
MC/DC33%1/3
defined($file = shift @ARGV) or defined($file = $opt_readlist && read_stdin())
defined($file = shift @ARGV) $opt_readlist read_stdin()
Truth table50%2/4
A or B && C
A= defined($file = shift @ARGV)B= $opt_readlistC= read_stdin()
ABCresult
1XX1
0111
00X0
0100
1224 defined($file = $opt_readlist && read_stdin)) {
1225 167 ▶ $file = decode 'utf8', $file unless utf8::is_utf8 $file;
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($file) 1 166 167
1226 167 $file =~ s/\n+$//;
1227  
1228 167 ▶ if (0) {}
Branch: 2 branches
BranchTrueFalseTotal
if (do { $file =~ m[^https?://]u }) { } 0 167 167
elsif ($file eq '-') { } 1 166 167
1229 elsif ($file =~ /^https?:\/\//) {
1230 0 ▶ open(STDIN, '-|') || exec("w3m -dump $file") || die "w3m: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "-|" or exec "w3m -dump $file" or die "w3m: $!\n" 0 0 0
Condition0%0/2
open STDIN, "-|" or exec "w3m -dump $file"
l!l
00
MC/DC0%0/1
open STDIN, "-|" or exec "w3m -dump $file"
open STDIN, "-|"
Truth table0%0/2
A
A= open STDIN, "-|"
Aresult
11
00
1231 }
1232 elsif ($file eq '-') {
1233 # nothing to do
1234 }
1235 else {
1236 166 ▶ open(STDIN, '<', $file) or do {
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<", $file or do { warn "${file}: $!\n" unless -l $file; next } 166 0 166
1237 0 ▶ warn "$file: $!\n" unless -l $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -l $file 0 0 0
1238 0 next;
1239 };
1240 }
1241  
1242 167 ▶ if (my @filters = $filter_d->get_filters($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@filters) = $filter_d->get_filters($file)) 2 165 167
1243 2 push_input_filter({ &FILELABEL => $file }, @filters);
1244 }
1245  
1246 166 ▶ if ($file_code eq 'binary') {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_code eq 'binary') { } 0 166 166
1247 0 binmode STDIN, ":raw";
1248 } else {
1249 166 binmode STDIN, ":encoding($file_code)";
1250 }
1251  
1252 166 return $file;
1253 }
1254 160 undef;
1255 }
1256  
1257 ######################################################################
1258  
1259 161 ▶sub grep_data {
Subroutine grep_data: called CC=24 SCAR=44.2
1260 161 local *_ = shift;
1261  
1262 ##
1263 ## --begin
1264 ##
1265 161 for my $f (@opt_begin) {
1266 0 eval { $f->call(&FILELABEL => $current_file) };
1267 0 ▶ if (my $msg = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $@) 0 0 0
1268 0 ▶ if ($msg =~ /^SKIP/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg =~ /^SKIP/iu) { } 0 0 0
1269 0 ▶ warn $@ if $opt_warn{begin};
Branch: 1 branch
BranchTrueFalseTotal
if $opt_warn{"begin"} 0 0 0
1270 0 return 0;
1271 } else {
1272 0 die $msg;
1273 }
1274 }
1275 }
1276  
1277 161 ▶ $progress_show->() if $progress_show;
Branch: 1 branch
BranchTrueFalseTotal
if $progress_show 0 161 161
1278  
1279 my $grep = App::Greple::Grep->new(
1280 text => \$_,
1281 filename => $current_file,
1282 pattern => $pat_holder,
1283 regions => $regions,
1284 border => $border_re,
1285 after => $opt_A,
1286 before => $opt_B,
1287 only => $opt_o,
1288 all => $opt_all,
1289 block => \@opt_block,
1290 stretch => $opt_stretch,
1291 must => $count_must,
1292 need => $count_need,
1293 countcheck => $count_match_sub,
1294 allow => $count_allow,
1295 strict => $opt_strict,
1296 161 group_index => do { local $_ = $opt_colorindex;
1297 161 ▶ $opt_capture_group ? /G/i ? /P/i ? 3 : 2 : 1 : 0 },
Branch: 3 branches
BranchTrueFalseTotal
$opt_capture_group ? : 8 153 161
/G/iu ? : 3 5 8
/P/iu ? : 1 2 3
1298 region_index => $opt_regioncolor,
1299 stat => \%stat,
1300 callback => \@opt_callback,
1301 alert_size => $opt_alert{size},
1302 alert_time => $opt_alert{time},
1303 161 join_blocks => $opt_join_blocks,
1304 parallel => $opt_parallel,
1305 )->run;
1306  
1307 ## --postgrep
1308 161 for my $f (@opt_postgrep) {
1309 0 $f->call($grep);
1310 # remove emptied results
1311 0 my $ref = $grep->result_ref;
1312 0 @$ref = grep { @{$_} > 0 } @$ref;
1313 }
1314 ## -m
1315 161 for my $splice (@splicer) {
1316 1 $splice->($grep->result_ref);
1317 }
1318  
1319 161 my $matched = $grep->matched;
1320 161 ▶ if ($opt_l) {
Branch: 3 branches
BranchTrueFalseTotal
if ($opt_l) { } 0 161 161
elsif ($opt_c) { } 2 159 161
elsif (@{$grep->result_ref}) { } 129 30 159
1321 0 ▶ if ($matched) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matched) 0 0 0
1322 0 ▶ $progress_reset->() if $progress_reset;
Branch: 1 branch
BranchTrueFalseTotal
if $progress_reset 0 0 0
1323 0 print $current_file;
1324 0 ▶ printf ":%d", scalar $grep->result if $opt_c;
Branch: 1 branch
BranchTrueFalseTotal
if $opt_c 0 0 0
1325 0 print "\n";
1326 }
1327 }
1328 elsif ($opt_c) {
1329 2 ▶ $progress_reset->() if $progress_reset;
Branch: 1 branch
BranchTrueFalseTotal
if $progress_reset 0 2 2
1330 2 ▶ print "$current_file:" if $need_filename;
Branch: 1 branch
BranchTrueFalseTotal
if $need_filename 0 2 2
1331 2 print scalar $grep->result, "\n";
1332 }
1333 elsif ($grep->result_ref->@*) {
1334 129 ▶ $progress_reset->() if $progress_reset;
Branch: 1 branch
BranchTrueFalseTotal
if $progress_reset 0 129 129
1335 # open output filter
1336 129 ▶ @opt_of && push_output_filter(
Branch: 1 branch
BranchTrueFalseTotal
@opt_of and push_output_filter({&FILELABEL, $current_file}, \*STDOUT, @opt_of) 5 124 129
1337 { &FILELABEL => $current_file },
1338 \*STDOUT, @opt_of);
1339 127 output($grep);
1340 127 ▶ @opt_of && recover_stdout;
Branch: 1 branch
BranchTrueFalseTotal
@opt_of and recover_stdout() 3 124 127
1341 }
1342  
1343 ##
1344 ## --end
1345 ##
1346 159 for my $f (@opt_end) {
1347 0 $f->call(&FILELABEL => $current_file);
1348 }
1349  
1350 159 ▶ s/./\000/gs if $opt_clean;
Branch: 1 branch
BranchTrueFalseTotal
if $opt_clean 0 159 159
1351  
1352 159 $matched;
1353 }
1354  
1355 127 ▶sub output {
Subroutine output: called CC=56 SCAR=46.7
1356 127 my $grep = shift;
1357 127 my $file = $grep->{filename};
1358  
1359 127 ▶ if ($opt_filestyle eq 'once') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_filestyle eq "once") 0 127 127
1360 0 print $_file->($file), "\n";
1361 }
1362  
1363 127 ▶ my $need_blockend =
Condition100%18/18
!$opt_all && $blockend ne '' && ($opt_blockend || $opt_p || $opt_A || $opt_B || @opt_block)
!ll&&!rl&&r
810118
not $opt_all and $blockend ne ""
!ll&&!rl&&r
62119
$opt_blockend or $opt_p or $opt_A or $opt_B or @opt_block
l!l&&r!l&&!r
162101
$opt_blockend or $opt_p or $opt_A or $opt_B
l!l&&r!l&&!r
151103
$opt_blockend or $opt_p or $opt_A
l!l&&r!l&&!r
78104
$opt_blockend or $opt_p
l!l&&r!l&&!r
16112
MC/DC100%7/7
!$opt_all && $blockend ne '' && ($opt_blockend || $opt_p || $opt_A || $opt_B || @opt_block)
not $opt_all $blockend ne "" $opt_blockend $opt_p $opt_A $opt_B @opt_block
Truth table100%8/8
not(A) and B && C or D or E or F or G
A= not $opt_allB= $blockend ne ""C= $opt_blockendD= $opt_pE= $opt_AF= $opt_BG= @opt_block
ABCDEFGresult
0XXXXXX0
10XXXXX0
11000000
111XXXX1
1101XXX1
11001XX1
110001X1
11000011
1364 !$opt_all &&
1365 $blockend ne '' &&
1366 ($opt_blockend || $opt_p || $opt_A || $opt_B || @opt_block);
1367  
1368 127 my $line = 1;
1369 127 my $lastpos = 0;
1370 127 my @results = $grep->result;
1371 127 my @pieces = $grep->slice_blocks;
1372 127 for my $rix (keys @results) {
1373 263 my $is_top = $rix == 0;
1374 263 my $is_bottom = $rix == $#results;
1375  
1376 263 my $result = $results[$rix];
1377 263 my($blk, @result) = @{$result};
1378 263 my($block_start, $block_end, $block_number) = @$blk;
1379 263 ▶ $block_number //= 0;
Condition100%2/2
$block_number //= 0
l!l
2576
MC/DC100%1/1
$block_number //= 0
$block_number
Truth table100%2/2
A
A= $block_number
Aresult
11
00
1380 263 ▶ my $block = @pieces
Branch: 1 branch
BranchTrueFalseTotal
@pieces ? : 180 83 263
1381 ? $pieces[$rix * 2 + 1]
1382 : $grep->cut($block_start, $block_end);
1383  
1384 ## --print
1385 263 ▶ if (@opt_print) {
Branch: 1 branch
BranchTrueFalseTotal
if (@opt_print) 0 263 263
1386 0 local *_ = \$block;
1387 0 for my $func (@opt_print) {
1388 0 $_ = $func->call(&FILELABEL => $file);
1389 }
1390 0 ▶ if (not $opt_continue) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt_continue) 0 0 0
1391 0 ▶ print $block if defined $block;
Branch: 1 branch
BranchTrueFalseTotal
if defined $block 0 0 0
1392 0 next;
1393 }
1394 }
1395  
1396 263 ▶ if ($opt_n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_n) 8 255 263
1397 8 ▶ my $gap = @pieces
Branch: 1 branch
BranchTrueFalseTotal
@pieces ? : 3 5 8
1398 ? $pieces[$rix * 2]
1399 : $grep->cut($lastpos, $block_start);
1400 8 $line += $gap =~ tr/\n/\n/;
1401 }
1402 263 $lastpos = $block_end;
1403  
1404 # when --filestyle and/or --linestyle is "separate"
1405 789 grep { $_ } (
1406 do {
1407 263 ▶ print $_file->($current_file)
Branch: 1 branch
BranchTrueFalseTotal
if $opt_filestyle eq "separate" 2 261 263
1408 if $opt_filestyle eq 'separate';
1409 },
1410 do {
1411 263 ▶ print $_line->($line)
Branch: 1 branch
BranchTrueFalseTotal
if $opt_n and $opt_linestyle eq "separate" 2 261 263
Condition100%3/3
$opt_n and $opt_linestyle eq "separate"
!ll&&!rl&&r
25562
MC/DC100%2/2
$opt_n and $opt_linestyle eq "separate"
$opt_n $opt_linestyle eq "separate"
Truth table100%3/3
A and B
A= $opt_nB= $opt_linestyle eq "separate"
ABresult
0X0
100
111
1412 if $opt_n and $opt_linestyle eq 'separate';
1413 },
1414 263 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
grep {$_;} do { print &$_file($current_file) if $opt_filestyle eq "separate" }, do { print &$_line($line) if $opt_n and $opt_linestyle eq "separate" }, do { print &$_block($block_number) if $opt_b and $opt_blockstyle eq "separate" } and print " " 3 260 263
1415 263 ▶ print $_block->($block_number)
Branch: 1 branch
BranchTrueFalseTotal
if $opt_b and $opt_blockstyle eq "separate" 0 263 263
Condition33%1/3
$opt_b and $opt_blockstyle eq "separate"
!ll&&!rl&&r
26300
MC/DC0%0/2
$opt_b and $opt_blockstyle eq "separate"
$opt_b $opt_blockstyle eq "separate"
Truth table33%1/3
A and B
A= $opt_bB= $opt_blockstyle eq "separate"
ABresult
0X0
100
111
1416 if $opt_b and $opt_blockstyle eq 'separate';
1417 },
1418 ) and print "\n";
1419  
1420 263 ▶ if ($indexer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indexer) 0 263 263
1421 0 ▶ $indexer->reset if $indexer->block;
Branch: 1 branch
BranchTrueFalseTotal
if $indexer->block 0 0 0
1422 0 ▶ for ($indexer->reverse ? reverse @result : @result) {
Branch: 1 branch
BranchTrueFalseTotal
$indexer->reverse ? : 0 0 0
1423 0 $_->[2] = $indexer->index;
1424 }
1425 }
1426  
1427 263 ▶ my @slice = @pieces
Branch: 1 branch
BranchTrueFalseTotal
@pieces ? : 180 83 263
1428 ? $grep->slice_result($result, $pieces[$rix * 2 + 1])
1429 : $grep->slice_result($result);
1430 263 my $mark = "\001";
1431 263 for my $i (keys @result) {
1432 336 my($start, $end, $pi, $callback) = $result[$i]->@*;
1433 336 local *b = \$slice[$i * 2 + 1];
1434  
1435 ## run callback function
1436 336 ▶ if ($callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callback) 3 333 336
1437 3 ▶ $b = do {
Condition33%1/3
do { if (ref $callback eq "CODE") { &$callback($start, $end, $pi, $b); } elsif (callable($callback)) { $callback->call(&FILELABEL, $file, "start", $start, "end", $end, "index", $pi, "match", $b); } else { die; } } // $b
l!l&&r!l&&!r
300
MC/DC0%0/2
do { if (ref $callback eq "CODE") { &$callback($start, $end, $pi, $b); } elsif (callable($callback)) { $callback->call(&FILELABEL, $file, "start", $start, "end", $end, "index", $pi, "match", $b); } else { die; } } // $b
do { if (ref $callback eq "CODE") { &$callback($start, $end, $pi, $b); } elsif (callable($callback)) { $callback->call(&FILELABEL, $file, "start", $start, "end", $end, "index", $pi, "match", $b); } else { die; } } $b
Truth table33%1/3
A // B
A= do { if (ref $callback eq "CODE") { &$callback($start, $end, $pi, $b); } elsif (callable($callback)) { $callback->call(&FILELABEL, $file, "start", $start, "end", $end, "index", $pi, "match", $b); } else { die; } }B= $b
ABresult
1X1
011
000
1438 3 ▶ if (ref $callback eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $callback eq 'CODE') { } 0 3 3
elsif (callable($callback)) { } 3 0 3
1439 0 $callback->($start, $end, $pi, $b);
1440 }
1441 elsif (callable($callback)) {
1442 3 $callback->call(
1443 &FILELABEL => $file,
1444 start => $start,
1445 end => $end,
1446 index => $pi,
1447 match => $b);
1448 }
1449 0 else { die }
1450 } // $b;
1451 }
1452  
1453 336 ▶ if ($opt_join) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_join) 1 335 336
1454 1 ▶ if ($opt_n and $opt_linestyle eq 'line') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_n and $opt_linestyle eq 'line') { } 0 1 1
Condition33%1/3
$opt_n and $opt_linestyle eq "line"
!ll&&!rl&&r
100
MC/DC0%0/2
$opt_n and $opt_linestyle eq "line"
$opt_n $opt_linestyle eq "line"
Truth table33%1/3
A and B
A= $opt_nB= $opt_linestyle eq "line"
ABresult
0X0
100
111
1455 0 $b =~ s/(?<!\A)\n(?!\z)/$mark/g;
1456 } else {
1457 1 $b =~ s/(?<!\A)\n(?!\z)/$opt_joinby/g;
1458 }
1459 }
1460  
1461 336 ▶ $pi = $uniq_color->index($b) if $opt_uniqcolor;
Branch: 1 branch
BranchTrueFalseTotal
if $opt_uniqcolor 0 336 336
1462 336 $b = index_color($pi, $b);
1463 }
1464  
1465 263 $block = join '', @slice;
1466 263 ▶ next if $block eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $block eq "" 0 263 263
1467  
1468 263 my @line;
1469 263 ▶ if ($opt_n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_n) 8 255 263
1470 8 ▶ if ($opt_linestyle eq 'line') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_linestyle eq 'line') { } 6 2 8
1471 6 ▶ my $increment = $block =~ /[\n$mark]/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$block =~ /[\n$mark]/u ? : 6 0 6
1472 6 $block =~ s{(?:(?<mark>$mark)|(?<=\n)|\A)(?=.)}{
1473 7 ▶ push @line, $_line->($line) unless $+{mark};
Branch: 1 branch
BranchTrueFalseTotal
unless $+{"mark"} 7 0 7
1474 7 $line += $increment;
1475 7 ▶ $+{mark} ? $opt_joinby : '';
Branch: 1 branch
BranchTrueFalseTotal
$+{'mark'} ? : 0 7 7
1476 }gse;
1477 } else {
1478 2 $line += $block =~ tr/\n/\n/;
1479 }
1480 }
1481  
1482 263 ▶ $block = $_text->($block) if $colormap{TEXT} ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $main::colormap{"TEXT"} ne "" 0 263 263
1483  
1484 263 ▶ if (@line) {
Branch: 1 branch
BranchTrueFalseTotal
if (@line) 6 257 263
1485 6 $block =~ s/^/shift @line/mge;
1486 }
1487  
1488 263 ▶ if ($opt_b and $opt_blockstyle eq 'line') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_b and $opt_blockstyle eq "line") 0 263 263
Condition33%1/3
$opt_b and $opt_blockstyle eq "line"
!ll&&!rl&&r
26300
MC/DC0%0/2
$opt_b and $opt_blockstyle eq "line"
$opt_b $opt_blockstyle eq "line"
Truth table33%1/3
A and B
A= $opt_bB= $opt_blockstyle eq "line"
ABresult
0X0
100
111
1489 0 my $s = $_block->($block_number);
1490 0 $block =~ s/^/$s/mg;
1491 }
1492  
1493 263 ▶ if ($opt_filestyle eq 'line') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_filestyle eq "line") 7 256 263
1494 7 my $s = $_file->($file);
1495 7 $block =~ s/^/$s/mg;
1496 }
1497  
1498 263 ▶ print "$_top\n" if $is_top && $_top ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $is_top and $_top ne "" 0 263 263
Condition66%2/3
$is_top and $_top ne ""
!ll&&!rl&&r
1361270
MC/DC0%0/2
$is_top and $_top ne ""
$is_top $_top ne ""
Truth table66%2/3
A and B
A= $is_topB= $_top ne ""
ABresult
0X0
100
111
1499 263 print $block;
1500 263 ▶ print "\n" if $opt_newline and not $block =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt_newline and not $block =~ /\n\z/u 54 209 263
Condition66%2/3
$opt_newline and not $block =~ /\n\z/u
!ll&&!rl&&r
020954
MC/DC50%1/2
$opt_newline and not $block =~ /\n\z/u
$opt_newline not $block =~ /\n\z/u
Truth table66%2/3
A and not(B)
A= $opt_newlineB= not $block =~ /\n\z/u
ABresult
0X0
100
111
1501 263 ▶ print "$_blockend\n" if $need_blockend;
Branch: 1 branch
BranchTrueFalseTotal
if $need_blockend 23 240 263
1502 263 ▶ if ($is_bottom) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_bottom) { } 127 136 263
1503 127 ▶ print "$_bottom\n" if $_bottom ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $_bottom ne "" 0 127 127
1504 } else {
1505 136 ▶ print "$_middle\n" if $_middle ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $_middle ne "" 0 136 136
1506 }
1507 }
1508 }
1509  
1510 __END__
« lib/App/Greple/Util.pm ↑ Summary lib/App/Greple/Grep.pm »