File Coverage

/usr/local/bin/greple
Criterion Covered Total %
statement 395 708 55.7
branch 112 400 28.0
condition 40 152 26.3
subroutine 55 91 60.4
pod n/a
total 602 1351 44.5


line stmt bran cond sub pod time code
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 21     21   111302 use v5.24;
  21         66  
9 21     21   97 use warnings;
  21         31  
  21         1161  
10              
11 21     21   10364 use File::stat;
  21         214421  
  21         1524  
12 21     21   12263 use IO::Handle;
  21         150982  
  21         1241  
13 21     21   12484 use Pod::Usage;
  21         1638336  
  21         3489  
14 21     21   12278 use Text::ParseWords qw(shellwords);
  21         40674  
  21         1924  
15 21     21   190 use List::Util qw(min max first sum uniq shuffle notall pairs pairmap);
  21         39  
  21         3840  
16 21     21   13390 use Hash::Util qw(lock_keys);
  21         85905  
  21         165  
17 21     21   2582 use Cwd qw(getcwd abs_path);
  21         41  
  21         1202  
18 21     21   108 use Carp;
  21         37  
  21         1126  
19              
20 21     21   11516 use utf8;
  21         6664  
  21         127  
21 21     21   799 use Encode;
  21         58  
  21         1929  
22 21     21   12639 use Encode::Guess;
  21         98278  
  21         92  
23 21     21   12087 use open IO => ':utf8', ':std';
  21         32238  
  21         176  
24              
25 21     21   18422 use Data::Dumper;
  21         191804  
  21         2305  
26             {
27 21     21   5026795 no warnings 'redefine';
  21         225  
  21         32  
  21         6599  
28 21     0   278 *Data::Dumper::qquote = sub { qq["${\(shift)}"] };
  0         0  
  0         0  
29 21         112 $Data::Dumper::Useperl = 1;
30             }
31              
32             ##
33             ## Setup greple/lib to be a module directory if exists.
34             ##
35             BEGIN {
36 21 50   21   1351 if (my $lib = abs_path($0) =~ s{/(?:script/|bin/)?\w+$}{/lib}r) {
37 21 50       1173 unshift @INC, $lib if -d "$lib/App/Greple";
38             }
39             }
40              
41 21     21   12640 use Getopt::EX::Loader;
  21         2065637  
  21         1742  
42 21     21   254 use Getopt::EX::Func qw(parse_func callable);
  21         39  
  21         1302  
43              
44 21     21   14273 use App::Greple;
  21         3431  
  21         1098  
45 21     21   10855 use App::Greple::Common;
  21         10999  
  21         2758  
46 21     21   11257 use App::Greple::Util qw(shellquote);
  21         36599  
  21         1780  
47 21     21   13586 use App::Greple::Grep;
  21         392627  
  21         2314  
48 21     21   229 use App::Greple::Regions;
  21         46  
  21         2026  
49 21     21   141 use App::Greple::Pattern;
  21         44  
  21         1618  
50 21     21   14553 use App::Greple::Pattern::Holder;
  21         106298  
  21         1510  
51 21     21   13937 use App::Greple::Filter;
  21         34818  
  21         157579  
52              
53 21         83 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.04
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             -d flags display info (f:file d:dir c:color m:misc s:stat)
170              
171             =cut
172              
173 21         111 my @baseclass = qw( App::Greple Getopt::EX );
174 21         350 my $rcloader = Getopt::EX::Loader
175             ->new(BASECLASS => \@baseclass);
176              
177 21         1635 my @optargs;
178             my %optargs;
179              
180             sub newopt {
181             push @optargs, pairmap {
182 1911     1911   2760 local $_ = $a;
183 1911         7674 s/\s+//g;
184 1911         4227 s/^(?=\w+-)([-\w]+)/$1 =~ tr[-][_]r . "|$1"/e; # "a-b" -> "a_b|a-b"
  294         1159  
185 1911 100 50     8013 /^(\w+)/ and $optargs{$1} = $b if ref $b ne 'CODE';
186 1911         3971 $_ => $b;
187 21     21   376 } @_;
188             }
189              
190             sub opt :lvalue {
191 4     4   9 my $name = shift;
192 4 50       22 my $var = $optargs{$name} or die "$name: invalid option name\n";
193 4 50 0     17 if (ref $var eq 'SCALAR') {
    0          
194 4         22 return $$var;
195             } elsif (ref $var eq 'HASH' and @_ == 1) {
196 0         0 return $var->{+shift};
197             } else {
198 0         0 return $var;
199             }
200             }
201              
202 21         0 my @opt_pattern;
203             sub opt_pattern {
204 21     21   271102 push @opt_pattern, [ map "$_", @_ ];
205 21         427 $opt_pattern[-1];
206             }
207              
208 21         0 my @opt_colormap;
209 21     21   21693 sub opt_colormap { push @opt_colormap, $_[1] }
210 0     0   0 sub opt_colorsub { push @opt_colormap, "sub{ $_[1] }" }
211              
212 21         164 my %opt_format = (LINE => '%d:', FILE => '%s:', BLOCK => '%s:');
213 21         101 my %opt_alert = (size => 512 * 1024, time => 2);
214 21         141 my %opt_warn = (read => 0, skip => 1, retry => 0, begin => 0);
215              
216             newopt
217              
218             ##
219             ## PATTERN
220             ##
221             ' and |e =s ' => \&opt_pattern ,
222             ' must |r =s ' => \&opt_pattern ,
223             ' may |t =s ' => \&opt_pattern ,
224             ' not |v =s ' => \&opt_pattern ,
225             ' le |x =s ' => \&opt_pattern ,
226             ' re |E =s ' => \&opt_pattern ,
227             ' fe =s ' => \&opt_pattern ,
228             ' file |f =s ' => \ my @opt_f ,
229             ' select =s ' => \ my $opt_select ,
230              
231             ##
232             ## MATCH
233             ##
234             ' ignore-case |i ! ' => \ my $opt_i ,
235             ' need =s ' => \ my @opt_need ,
236             ' allow =s ' => \ my @opt_allow ,
237             ' matchcount |mc =s ' => \ my $opt_matchcount ,
238             ' capture-group |G ! ' => \ my $opt_capture_group ,
239             ' stretch |S ! ' => \ my $opt_stretch ,
240              
241             ##
242             ## STYLE
243             ##
244             ' files-with-matches |l ' => \ my $opt_l ,
245             ' count |c ' => \ my $opt_c ,
246             ' line-number |n ! ' => \ my $opt_n ,
247             ' block-number |b ! ' => \ my $opt_b ,
248             ' filename |H ' => \ my $opt_H ,
249             ' no-filename |h ' => \ my $opt_h ,
250             ' only-matching |o ! ' => \ my $opt_o ,
251             ' all ! ' => \ my $opt_all ,
252             ' filter |F ! ' => \ my $opt_filter ,
253             ' max-count |m =s ' => \ my $opt_m ,
254             ' after-context |A :2 ' => \(my $opt_A = 0) ,
255             ' before-context |B :2 ' => \(my $opt_B = 0) ,
256             ' context |C :2 ' => \(my $opt_C = 0) ,
257             ' join ! ' => \ my $opt_join ,
258             ' joinby =s ' => \(my $opt_joinby = "") ,
259             ' newline ! ' => \(my $opt_newline = 1) ,
260             ' filestyle |fs =s ' => \(my $opt_filestyle = 'line') ,
261             ' linestyle |ls =s ' => \(my $opt_linestyle = 'line') ,
262             ' blockstyle |bs =s ' => \(my $opt_blockstyle = 'line') ,
263             ' separate ' => sub {
264 0     0   0 opt('filestyle') = opt('linestyle') = opt('blockstyle') = $_[0];
265             },
266             ' format =s ' => \ %opt_format ,
267             ' frame-top :s ' => \(my $opt_frame_top = '') ,
268             ' frame-middle :s ' => \(my $opt_frame_middle = '') ,
269             ' frame-bottom :s ' => \(my $opt_frame_bottom = '') ,
270              
271             ##
272             ## FILE
273             ##
274             ' glob =s ' => \ my @opt_glob ,
275             ' chdir =s ' => \ my @opt_chdir ,
276             ' readlist ! ' => \ my $opt_readlist ,
277              
278             ##
279             ## COLOR
280             ##
281             ' color =s ' => \(my $opt_color = 'auto') ,
282             ' colormap |cm =s ' => \&opt_colormap ,
283             ' colorsub |cs =s ' => \&opt_colorsub ,
284             ' colorful ! ' => \(my $opt_colorful = 1) ,
285             ' colorindex |ci =s ' => \(my $opt_colorindex = '') ,
286             ' ansicolor =s ' => \(my $opt_ansicolor = '256') ,
287             ' regioncolor |rc ! ' => \ my $opt_regioncolor ,
288             ' uniqsub |us =s ' => \ my @opt_uniqsub ,
289             ' face =s ' => \ my @opt_face ,
290             ' nocolor | no-color ' => sub {
291 4     4   6297 opt('color') = 'never';
292             },
293             ' 256! ' => sub {
294 0 0   0   0 opt('ansicolor') = $_[1] ? '256' : '16';
295             },
296             ' random! ' => sub {
297 0 0   0   0 if ($_[1]) { opt('colorindex') .= 'R' }
  0         0  
298 0         0 else { opt('colorindex') =~ s/R//gi }
299             },
300             ' uniqcolor |uc ' => sub {
301 0     0   0 opt('colorindex') = 'U';
302             },
303              
304             ##
305             ## BLOCK
306             ##
307             ' paragraph |p ! ' => \ my $opt_p ,
308             ' border =s ' => \ my $opt_border ,
309             ' block =s ' => \ my @opt_block ,
310             ' blockend :s ' => \(my $opt_blockend) ,
311             ' join-blocks ! ' => \(my $opt_join_blocks = 0) ,
312              
313             ##
314             ## REGION
315             ##
316             ' inside =s ' => \ my @opt_inside ,
317             ' outside =s ' => \ my @opt_outside ,
318             ' include =s ' => \ my @opt_include ,
319             ' exclude =s ' => \ my @opt_exclude ,
320             ' strict ! ' => \(my $opt_strict = 0) ,
321              
322             ##
323             ## CHARACTER CODE
324             ##
325             ' icode =s ' => \ my @opt_icode ,
326             ' ocode =s ' => \ my $opt_ocode ,
327              
328             ##
329             ## FILTER
330             ##
331             ' if =s ' => \ my @opt_if ,
332             ' of =s ' => \ my @opt_of ,
333             ' pf =s ' => \ my @opt_pf ,
334             ' noif ' => \ my $opt_noif ,
335              
336             ##
337             ## RUNTIME FUNCTION
338             ##
339             ' print =s ' => \ my @opt_print ,
340             ' continue ! ' => \ my $opt_continue ,
341             ' callback =s ' => \ my @opt_callback ,
342             ' begin =s ' => \ my @opt_begin ,
343             ' end =s ' => \ my @opt_end ,
344             ' prologue =s ' => \ my @opt_prologue ,
345             ' epilogue =s ' => \ my @opt_epilogue ,
346             ' postgrep =s ' => \ my @opt_postgrep ,
347              
348             ##
349             ## OTHERS
350             ##
351             ' usage :s ' => \ my $opt_usage ,
352             ' version ' => \ my $opt_version ,
353             ' exit =i ' => \ my $opt_exit ,
354             # norc
355             ' man | doc ' => \ my $opt_man ,
356             ' show | less ' => \ my $opt_show ,
357             ' path ' => \ my $opt_path ,
358             ' error =s ' => \(my $opt_error = 'skip') ,
359             ' alert =i ' => \ %opt_alert ,
360             ' debug |d =s ' => \ my @opt_d ,
361             'warn|w:1%' => sub {
362 0         0 map { $opt_warn{$_} = $_[2] }
363 0 0   0   0 map { $_ eq 'all' ? keys %opt_warn : $_ }
  0         0  
364             $_[1] =~ /\w+/g;
365             },
366              
367             ##
368             ## MODULE
369             ##
370             'M:s' => sub {
371 0     0   0 warn "Use -M option at the beginning with module name.\n";
372 0 0       0 if (my @modules = uniq($rcloader->modules())) {
373 0         0 warn "Available modules:\n";
374 0         0 warn "\t", join("\n\t", @modules), "\n";
375             }
376 0         0 exit 2;
377             },
378              
379             ##
380             ## UNDOCUMENTED
381             ##
382 21         1630 ' clean ! ' => \ my $opt_clean ,
383              
384             ;
385              
386             sub setopt {
387 0 0   0   0 my $opt = ref $_[0] eq 'HASH' ? shift : {};
388 0         0 my $name = shift;
389 0 0       0 if (exists $optargs{$name}) {
390 0         0 my $ref = $optargs{$name};
391 0 0       0 if (ref $ref eq 'ARRAY') {
    0          
    0          
392 0 0       0 if ($opt->{append}) {
393 0         0 push @$ref, @_;
394             } else {
395 0         0 @$ref = @_;
396             }
397             }
398             elsif (ref $ref eq 'CODE') {
399 0         0 &$ref($name, @_);
400             }
401             elsif (ref $ref eq 'SCALAR') {
402 0         0 $$ref = shift;
403             }
404             else {
405 0         0 die "Object error.";
406             }
407             }
408             }
409              
410             ##
411             ## @ARGV stuff
412             ##
413              
414 21         20363 require Getopt::Long;
415 21         305394 my $parser = Getopt::Long::Parser->new(
416             config => [ qw(bundling no_getopt_compat no_ignore_case) ],
417             );
418 0     0   0 sub configure_getopt { $parser->configure(@_) }
419              
420 21 50       29156 configure_getopt qw(debug) if $ENV{DEBUG_GETOPT};
421 21 50       119 $Getopt::EX::Loader::debug = 1 if $ENV{DEBUG_GETOPTEX};
422              
423             ## decode
424             my @ORIG_ARGV =
425 21 50       73 @ARGV = map { utf8::is_utf8($_) ? $_ : decode('utf8', $_) } @ARGV;
  114         2457  
426              
427             ## ~/.greplerc
428 21 50 33     692 unless ((@ARGV and $ARGV[0] eq "--norc" and shift)
      33        
      33        
429             or
430             ($ENV{GREPLE_NORC}) ) {
431 21         239 $rcloader->load(FILE => "$ENV{HOME}/.greplerc");
432             }
433              
434             ## modules
435 21         730 $rcloader->deal_with(\@ARGV);
436              
437 21         75116 push @optargs, $rcloader->builtins;
438              
439             ## ENV
440 21 50       1550 $ENV{'GREPLEOPTS'} and unshift @ARGV, shellwords($ENV{'GREPLEOPTS'});
441              
442              
443             ## GetOptions
444 21         234 my @SAVEDARGV = @ARGV;
445 21 50       369 $parser->getoptions(@optargs) || usage();
446              
447 21 50       27404 if ($opt_version) {
448 0         0 print "$version\n";
449 0         0 exit 0;
450             }
451              
452 21         44 our %opt_d;
453 21         70 @opt_d = map { split // } @opt_d;
  0         0  
454 21         91 @opt_d{@opt_d} = @opt_d;
455              
456 21 50       101 if ($opt_d{o}) {
457 0         0 warn "\@ARGV = ", join(' ', shellquote(@SAVEDARGV)), "\n";
458             }
459              
460             ## -m option
461 21 50       111 my @splicer = (not defined $opt_m) ? () : do {
462 0         0 my @param = split /,/, $opt_m, -1;
463 0 0       0 push @param, '' if @param % 2;
464 0 0   0   0 if (notall { /^(-?\d+)?$/ } @param) {
  0         0  
465 0         0 die "$opt_m: option format error.\n";
466             }
467             map {
468 0         0 my($offset, $length) = @$_;
  0         0  
469 0 0       0 if ($length ne '') {
470 0   0 0   0 sub { splice @{+shift}, $offset || 0, $length }
  0         0  
471 0         0 } else {
472 0   0 0   0 sub { splice @{+shift}, $offset || 0 }
  0         0  
473 0         0 }
474             }
475             pairs @param;
476             };
477              
478 21         160 my $file_code;
479 21         115 my $default_icode = 'utf8'; # default input encoding
480 21         72 my @default_icode_list = qw(euc-jp 7bit-jis);
481 21         38 my $output_code;
482 21         40 my $default_ocode = 'utf8'; # default output encoding
483              
484 21   33     136 $output_code = $opt_ocode || $default_ocode;
485 21     21   1516 binmode STDOUT, ":encoding($output_code)";
  21         20371  
  21         360  
  21         116  
486              
487             ## show unused option characters
488 21 50       22859 if ($opt_d{u}) {
489 0         0 my $s = join('','0'..'9',"\n",'a'..'z',"\n",'A'..'Z',"\n");
490 0 0       0 map { /\|([0-9a-zA-Z])\b/ && $s =~ s/$1/./ } @optargs;
  0         0  
491 0         0 die $s;
492             }
493              
494             ## show man pages
495 21 50 33     201 if ($opt_man or $opt_show or $opt_path) {
      33        
496             my @module = map {
497 0 0       0 /^-M(\w+(::\w++(?![=(]))*)/ ? "App::Greple::$1" : ()
  0         0  
498             } @ORIG_ARGV;
499 0 0       0 if (@module) {
500 0         0 my $module = $module[-1];
501 0     0   0 my $jp = first { -x "$_/perldocjp" } split /:/, $ENV{PATH};
  0         0  
502 0 0       0 my $perldoc = $jp ? "perldocjp" : "perldoc";
503 0         0 $ENV{PERL5LIB} = join ':', @INC;
504 0         0 my $file = $module =~ s[::][/]gr . '.pm';
505 0 0       0 die unless $INC{$file};
506 0 0       0 if ($opt_man) {
507 0 0       0 exec "$perldoc $module" or die $!;
508             } else {
509 0         0 chomp(my $file = `$perldoc -ml $module`);
510 0 0       0 if ($opt_path) {
511 0         0 say $file;
512             } else {
513 0   0     0 my $pager = $ENV{PAGER} || 'less';
514 0 0       0 exec "$pager $file" or die $!;
515             }
516             }
517 0         0 exit;
518             }
519 0         0 pod2usage({-verbose => 2});
520 0         0 die;
521             }
522              
523             sub default_module {
524 0     0   0 my $mod = shift;
525 0         0 my $module = $mod->module;
526 0 0       0 return 1 if $module =~ /\b \.greplerc $/x;
527 0 0       0 return 1 if $module =~ /\b default $/x;
528 0         0 return 0;
529             }
530              
531             ## setup file encoding
532 21 50       73 if (@opt_icode) {
533 0         0 @opt_icode = map { split /[,\s]+/ } @opt_icode;
  0         0  
534 0 0       0 if (grep { s/^\+// } @opt_icode) {
  0         0  
535 0         0 unshift @opt_icode, @default_icode_list;
536             }
537 0         0 @opt_icode = uniq @opt_icode;
538 0 0       0 if (@opt_icode > 1) {
    0          
539 0         0 @opt_icode = grep { !/(?:auto|guess)$/i } @opt_icode;
  0         0  
540 0         0 Encode::Guess->set_suspects(@opt_icode);
541 0         0 $file_code = 'Guess';
542             }
543             elsif ($opt_icode[0] =~ /^(?:guess|auto)$/i) {
544 0         0 Encode::Guess->set_suspects(@default_icode_list);
545 0         0 $file_code = 'Guess';
546             } else {
547 0         0 $file_code = $opt_icode[0];
548             }
549             }
550             else {
551 21         51 $file_code = $default_icode;
552             }
553              
554             ##
555             ## --filter
556             ##
557 21 50       361 if ($opt_filter) {
558 0         0 $opt_all = 1;
559 0         0 push @opt_need, '0';
560 0   0     0 $opt_exit //= 0;
561             }
562              
563             ##
564             ## Patterns
565             ##
566              
567 21         328 my $pat_holder = App::Greple::Pattern::Holder->new;
568              
569 21         250 my $FLAG_BASE = FLAG_NONE;
570 21 50       69 $FLAG_BASE |= FLAG_IGNORECASE if $opt_i;
571              
572 21 50       92 if (@opt_f) {
573 0         0 for my $opt_f (@opt_f) {
574 0 0       0 $pat_holder->append({ flag => $FLAG_BASE, type => 'file',
575             $opt_select ? (select => $opt_select) : (),
576             },
577             $opt_f);
578             }
579             } else {
580 21 50 33     105 unless ($opt_filter or grep { $_->[0] !~ /^(not|may)/ } @opt_pattern) {
  21         152  
581 0   0     0 unshift @opt_pattern, [ le => shift @ARGV // &usage ];
582             }
583             }
584              
585 21         293 my %pat_flag = (
586             must => FLAG_REGEX | FLAG_COOK | FLAG_REQUIRED,
587             not => FLAG_REGEX | FLAG_COOK | FLAG_NEGATIVE,
588             may => FLAG_REGEX | FLAG_COOK | FLAG_OPTIONAL,
589             le => FLAG_REGEX | FLAG_COOK | FLAG_LEXICAL,
590             and => FLAG_REGEX | FLAG_COOK,
591             re => FLAG_REGEX,
592             fe => FLAG_NONE,
593             );
594 21         59 for (@opt_pattern) {
595 21         86 my($attr, @opt) = @$_;
596 21         67 my $flag = $FLAG_BASE | $pat_flag{$attr};
597 21         171 $pat_holder->append({ flag => $flag, type => 'pattern' }, @opt);
598             }
599             # $pat_holder->optimize;
600              
601             ##
602             ## if optional pattern exist, make all non-optional pattern as required
603             ##
604             {
605 21         6813 my @patterns = $pat_holder->patterns;
  21         128  
606 21         197 my @posi = grep { $_->is_positive } @patterns;
  21         95  
607 21         300 my @opti = grep { $_->is_optional } @posi;
  21         85  
608 21 50       303 if (@opti > 0) {
609 0         0 for my $p (grep { !$_->is_optional } @posi) {
  0         0  
610 0         0 $p->flag($p->flag | FLAG_REQUIRED);
611             }
612             }
613             }
614              
615             ##
616             ## set $count_must, $count_need and $opt_allow
617             ##
618 21         44 my $count_must = 0;
619 21         41 my $count_need;
620 21         68 my $count_allow = 0;
621             {
622 21         75 my $must = grep({ $_->is_required } $pat_holder->patterns);
  21         72  
  21         165  
623 21         263 my $posi = grep({ $_->is_positive } $pat_holder->patterns) - $must;
  21         142  
624 21         252 my $nega = grep({ $_->is_negative } $pat_holder->patterns);
  21         144  
625              
626 21   50     239 $count_must = $must // 0;
627 21 50       92 $count_need = $must ? 0 : $posi;
628 21         58 for (@opt_need) {
629 0 0       0 if (/^-(\d+)$/) { # --need -n
    0          
    0          
630 0         0 $count_need = $posi - $1;
631             }
632             elsif (/^\+(\d+)$/) { # --need +n
633 0         0 $count_need += $1;
634             }
635             elsif (/^(\d+)$/) { # --need n
636 0         0 $count_need = $1 - $must;
637             }
638             else {
639 0         0 die "$_ is not valid count.\n"
640             }
641             }
642              
643 21         33 $count_allow = 0;
644 21         55 for (@opt_allow) {
645 0 0       0 if (/^-(\d+)$/) { # --allow -n
    0          
    0          
646 0         0 $count_allow = $nega - $1;
647             }
648             elsif (/^\+(\d+)$/) { # --allow +n
649 0         0 $count_allow += $1;
650             }
651             elsif (/^(\d+)$/) { # --allow n
652 0         0 $count_allow = $1;
653             }
654             else {
655 0         0 die "$_ is not valid count.\n"
656             }
657             }
658             }
659              
660             ##
661             ## --matchcount
662             ##
663             my $count_match_sub = sub {
664 21 50   21   153 local $_ = shift or return;
665 0 0       0 /[^\d,]/ and die "$_ is not valid count.\n";
666 0 0       0 my @c = map { $_ eq '' ? 0 : int } split(/,/, $_, -1);
  0         0  
667 0 0       0 if (@c == 1) {
668 0         0 return sub { $_[0] == $c[0] };
  0         0  
669             }
670 0 0       0 push @c, -1 if @c % 2;
671             return sub {
672 0         0 my @n = @c;
673 0         0 while (my($min, $max) = splice(@n, 0, 2)) {
674 0 0       0 return 0 if $_[0] < $min;
675 0 0 0     0 return 1 if $max <= 0 || $_[0] <= $max;
676             }
677 0         0 return 0;
678             }
679 21         195 }->($opt_matchcount);
  0         0  
680              
681             ##
682             ## setup input/output filter
683             ##
684 21         397 my $filter_d = App::Greple::Filter->new->parse(@opt_if);
685 21 50       450 unless ($opt_noif) {
686             $filter_d->append(
687 21     21   830 [ sub { s/\.Z$// }, 'zcat' ],
688 21     21   250 [ sub { s/\.g?z$// }, 'gunzip -c' ],
689 21     21   254 [ sub { m/\.pdf$/i }, 'pdftotext -nopgbrk - -' ],
690 21     21   343 [ sub { s/\.gpg$// }, 'gpg --quiet --no-mdc-warning --decrypt' ],
  21         204  
691             );
692             }
693              
694             ##------------------------------------------------------------
695             ## miscellaneous setups
696             ##
697              
698 21         175 my @argv_files;
699             my $start_directory;
700 21   33     111 my $need_filename = ($opt_H or $opt_l);
701 21         40 my $current_file;
702              
703 21 50       102 if (@opt_chdir) {
    50          
704 0         0 $start_directory = getcwd;
705 0         0 @opt_chdir = uniq(map { glob $_ } @opt_chdir);
  0         0  
706 0         0 push @argv_files, splice(@ARGV);
707 0 0 0     0 unless ($opt_h or
      0        
      0        
708             (@opt_chdir == 1 and @argv_files == 1 and @opt_glob == 0)) {
709 0         0 $need_filename++;
710             }
711             }
712             elsif (@opt_glob) {
713 0         0 push @ARGV, map(glob, @opt_glob);
714             }
715              
716 21 50 66     210 push(@ARGV, '-') unless @ARGV || @argv_files || @opt_glob || $opt_readlist;
      66        
      33        
717 21 50 33     193 if ((@ARGV > 1 or $opt_readlist) and not $opt_h) {
      33        
718 0         0 $need_filename++;
719             }
720              
721 21 50       76 $opt_filestyle = 'none' if not $need_filename;
722              
723 21 50       71 $opt_join = 1 if $opt_joinby ne "";
724              
725             ##------------------------------------------------------------
726             ## colors
727             ##
728 21         308 our %colormap = (
729             FILE => "G",
730             LINE => "Y",
731             BLOCK => "B",
732             TEXT => "",
733             BLOCKEND => "/WE",
734             PROGRESS => "B",
735             TOP => "",
736             MIDDLE => "",
737             BOTTOM => "",
738             );
739              
740 21         65 our @colors;
741              
742 21     21   250 use Getopt::EX::Colormap;
  21         47  
  21         5723  
743 21         263 my $color_handler = Getopt::EX::Colormap
744             ->new(HASH => \%colormap, LIST => \@colors)
745             ->load_params(@opt_colormap);
746              
747 21 50       56600 my @default_color =
748             $opt_ansicolor eq '16'
749             ? qw(RD GD BD CD MD YD)
750             : qw(000D/544 000D/454 000D/445
751             000D/455 000D/545 000D/554
752             000D/543 000D/453 000D/435
753             000D/534 000D/354 000D/345
754             000D/444
755             000D/433 000D/343 000D/334
756             000D/344 000D/434 000D/443
757             000D/333)
758             ;
759              
760 21 50       227 if ($color_handler->list == 0) {
761 0 0       0 $color_handler->append
762             ($opt_colorful ? @default_color : $default_color[0]);
763             }
764              
765 21 50       259 if ($opt_ansicolor eq '24bit') {
766 21     21   182 no warnings 'once';
  21         44  
  21         78656  
767 0         0 $Getopt::EX::Colormap::RGB24 = 1;
768             }
769              
770 21         62 for my $opt (@opt_face) {
771 0         0 while ($opt =~ /(?<mk>[-+=]) (?<s>[^-+=]*) | (?<s>[^-+=]+) /xg) {
772 0   0     0 my($mk, $s) = ($+{mk} // '', $+{s});
773 0         0 for my $c (@colors) {
774 0 0       0 if ($mk eq '-') {
    0          
    0          
775 0 0       0 $c =~ s/[\Q$s\E]//g if $s ne '';
776             } elsif ($mk eq '=') {
777 0         0 $c = $s;
778             } elsif ($s ne '') {
779 0 0       0 $c .= "^" if $c ne '';
780 0         0 $c .= $s;
781             }
782             }
783             }
784             }
785              
786 21   33     373 my $need_color = (($opt_color eq 'always')
787             or (($opt_color eq 'auto') and (!$opt_o and -t STDOUT)));
788              
789 21 50       95 if (!$need_color) {
790 21         68 $Getopt::EX::Colormap::NO_COLOR = 1;
791             }
792              
793 21         153 my %_esc = ( t => "\t", n => "\n", r => "\r", f => "\f" );
794             sub expand_escape {
795 63   0 63   197 $_[0] =~ s{\\(.)}{$_esc{$1} // $1}egr;
  0         0  
796             }
797              
798 21         144 $_ = expand_escape($_) for values %opt_format;
799              
800 21         47 my $blockend = "--";
801 21 50       71 if (defined $opt_blockend) {
802 0         0 $blockend = expand_escape($opt_blockend);
803             }
804              
805 21     0   117 my $_file = sub { $color_handler->color('FILE' , sprintf($opt_format{FILE}, $_[0])) };
  0         0  
806 21     0   136 my $_line = sub { $color_handler->color('LINE' , sprintf($opt_format{LINE}, $_[0])) };
  0         0  
807 21     0   75 my $_block = sub { $color_handler->color('BLOCK', sprintf($opt_format{BLOCK}, $_[0])) };
  0         0  
808 21     0   71 my $_text = sub { $color_handler->color('TEXT' , $_[0]) };
  0         0  
809 21         137 my $_blockend = $color_handler->color('BLOCKEND', $blockend);
810 21         1158 my $_top = $color_handler->color('TOP' , $opt_frame_top);
811 21         311 my $_middle = $color_handler->color('MIDDLE' , $opt_frame_middle);
812 21         212 my $_bottom = $color_handler->color('BOTTOM' , $opt_frame_bottom);
813              
814             sub index_color {
815 196     196   920 $color_handler->index_color(@_);
816             }
817              
818             sub color {
819 0     0   0 $color_handler->color(@_);
820             }
821              
822 21         376 my $uniq_color = UniqIndex->new(
823             ignore_newline => 1,
824             prepare => \@opt_uniqsub,
825             );
826              
827             sub dump_uniqcolor {
828 0     0   0 my $list = $uniq_color->list;
829 0         0 my $count = $uniq_color->count;
830 0         0 for my $i (keys @$list) {
831 0         0 warn sprintf("%3d (%3d) %s\n",
832             $i, $count->[$i],
833             index_color($i, $list->[$i]));
834             }
835             }
836              
837             # --colorindex
838 21         685 my %color_index = map { uc $_ => 1 } $opt_colorindex =~ /\w/g;
  0         0  
839 21         36 my $indexer = do {
840 21 50       83 if ($color_index{S}) {
841 0         0 @colors = shuffle @colors;
842             }
843 21 50 33     182 if ($color_index{A} or $color_index{D}) {
    50          
844 0         0 my $i = 0;
845             Indexer->new(
846 0     0   0 index => sub { $i++ },
847 0     0   0 reset => sub { $i = 0 },
848             block => $color_index{B},
849             reverse => $color_index{D},
850 0         0 );
851             }
852             elsif ($color_index{R}) {
853 0     0   0 Indexer->new(index => sub { int rand @colors });
  0         0  
854             }
855 21         47 else { undef }
856             };
857 21         49 my $opt_uniqcolor = $color_index{U};
858              
859             # -dc
860 21 50       72 if ($opt_d{c}) {
861             my $dump = sub {
862 0     0   0 local $_ = Dumper shift;
863 0         0 s/^\s*'\K([^'\s]+)(?=')/color($1, $1)/mge;
  0         0  
864 0         0 $_;
865 0         0 };
866 0         0 warn 'colormap = ', $dump->(\%colormap);
867 0         0 warn 'colors = ', $dump->(\@colors);
868             }
869              
870             ##
871             ## border regex
872             ##
873 21         35 my $border_re = do {
874 21 50       100 if ($opt_border) {
    50          
875 0         0 qr/$opt_border/m; # custom
876             } elsif ($opt_p) {
877 0         0 qr/(?:\A|\R)\K\R+/; # paragraph
878             } else {
879 21         96 qr/^/m; # line
880             }
881             };
882              
883 21 50       66 if ($opt_C) {
884 0   0     0 $opt_A ||= $opt_C;
885 0   0     0 $opt_B ||= $opt_C;
886             }
887 21         246 my %stat = (
888             file_searched => 0,
889             file_tried => 0,
890             length => 0,
891             match_effective => 0,
892             match_positive => 0,
893             match_negative => 0,
894             match_block => 0,
895             read_retry => 0,
896             time_start => [],
897             time_end => [],
898             );
899 21         145 lock_keys %stat;
900              
901             ##
902             ## Setup functions
903             ##
904 21         593 for my $set (
905             [ "print" , \@opt_print , 0 ],
906             [ "begin" , \@opt_begin , 0 ],
907             [ "end" , \@opt_end , 0 ],
908             [ "prologue", \@opt_prologue, 0 ],
909             [ "epilogue", \@opt_epilogue, 0 ],
910             [ "callback", \@opt_callback, 0 ],
911             [ "uniqsub" , \@opt_uniqsub , 0 ],
912             [ "postgrep", \@opt_postgrep, 0 ],
913             [ "block" , \@opt_block , 1 ], # need &
914             [ "inside" , \@opt_inside , 1 ], # need &
915             [ "outside" , \@opt_outside , 1 ], # need &
916             [ "include" , \@opt_include , 1 ], # need &
917             [ "exclude" , \@opt_exclude , 1 ], # need &
918             ) {
919 273         3001 my($cat, $opt, $pattern) = @$set;
920 273         263 for (@{$opt}) {
  273         415  
921 53 50       519 next if callable $_;
922 53 50 0     335 /^&\w+/ or next if $pattern;
923 53 50       121 $_ = parse_func($_) or die "$cat function format error: $_\n";
924             }
925             }
926              
927 21         301 my $regions = App::Greple::Regions::Holder->new;
928 21         205 for my $set (
929             [ \@opt_inside, REGION_INSIDE | REGION_UNION ],
930             [ \@opt_outside, REGION_OUTSIDE | REGION_UNION ],
931             [ \@opt_include, REGION_INSIDE | REGION_INTERSECT ],
932             [ \@opt_exclude, REGION_OUTSIDE | REGION_INTERSECT ])
933             {
934 84         122 my($opt, $flag) = @$set;
935 84         153 for my $spec (@$opt) {
936 0         0 $regions->append(FLAG => $flag, SPEC => $spec);
937             }
938             }
939              
940             ##------------------------------------------------------------
941              
942 21 50       129 if ($opt_d{m}) {
943 0         0 warn "Search pattern:\n";
944 0         0 my $i;
945 0         0 for my $pat ($pat_holder->patterns) {
946 0 0       0 my $type =
    0          
    0          
947             $pat->is_required ? 'must' :
948             $pat->is_negative ? 'not' :
949             $pat->is_positive ? 'and' : 'else';
950 0   0     0 my $target = $pat->regex // $pat->string;
951 0 0       0 warn sprintf(" %4s %1s %s\n",
    0          
952             $type,
953             $pat->is_function ? '&' : '',
954             @colors > 1 ? index_color($i++, $target) : $target);
955             }
956 0         0 warn sprintf("must = %d, need = %d, allow = %d\n",
957             $count_must, $count_need, $count_allow);
958             }
959              
960             ## push post-process filter
961 21 50       66 if (@opt_pf) {
962 0         0 push_output_filter(\*STDOUT, @opt_pf);
963             }
964              
965 21 50 0     67 usage() and exit if defined $opt_usage;
966              
967 21 50       658 open SAVESTDIN, '<&', \*STDIN or die "open: $!";
968 21 50       428 open SAVESTDOUT, '>&', \*STDOUT or die "open: $!";
969 21 50       1526 open SAVESTDERR, '>&', \*STDERR or die "open: $!";
970              
971             sub recover_stdin {
972 21 50   21   587 open STDIN, '<&', \*SAVESTDIN or die "open: $!";
973             }
974             sub recover_stderr {
975 0 0   0   0 open STDERR, '>&', \*SAVESTDERR or die "open: $!";
976 0         0 binmode STDERR, ':encoding(utf8)';
977             }
978             sub recover_stdout {
979 2     2   10990 close STDOUT;
980 2 50       107 open STDOUT, '>&', \*SAVESTDOUT or die "open: $!";
981             }
982             sub close_stdout {
983 21     21   203 close SAVESTDOUT;
984 21         542 close STDOUT;
985             }
986              
987 0     0   0 sub read_stdin { <SAVESTDIN> }
988              
989 21         74 my $slurp = do {
990             ##
991             ## Setting utf8 warnings fatal makes it easy to find code conversion
992             ## error, so you can choose appropriate file code or automatic code
993             ## recognition, but loose a chance to find string in unrelated area.
994             ##
995 21 50       170 if ($opt_error =~ /^(?: fatal | skip | retry )$/x) {
    0          
996 21 50       121 if ($opt_warn{read}) {
997             sub {
998 21     21   409 use warnings FATAL => 'utf8';
  21         160  
  21         4618  
999 0     0   0 my $stdin = eval { local $/; <STDIN> };
  0         0  
  0         0  
1000 0 0       0 warn $@ if $@;
1001 0         0 $stdin;
1002             }
1003 0         0 } else {
1004             sub {
1005 21     21   190 use warnings FATAL => 'utf8';
  21         59  
  21         7523  
1006 21     21   77 eval { local $/; <STDIN> };
  21         109  
  21         1089  
1007             }
1008 21         122 }
1009             } elsif ($opt_error eq 'ignore') {
1010 0 0       0 if ($opt_warn{read}) {
1011 0     0   0 sub { local $/; <STDIN> };
  0         0  
  0         0  
1012             } else {
1013             sub {
1014 0     0   0 close STDERR;
1015 0         0 my $stdin = do { local $/; <STDIN> };
  0         0  
  0         0  
1016 0         0 recover_stderr;
1017 0         0 $stdin;
1018             }
1019 0         0 }
1020             } else {
1021 0         0 die "$opt_error: invalid action.\n";
1022             }
1023             };
1024              
1025 21     21   179 use Term::ANSIColor::Concise qw(ansi_code);
  21         65  
  21         3989  
1026              
1027             use constant {
1028 21         157 EL => ansi_code('{EL}'), # Erase Line
1029             ED => ansi_code('{ED}'), # Erase Display
1030             SCP => ansi_code('{SCP}'), # Save Cursor Position
1031             RCP => ansi_code('{RCP}'), # Restore Cursor Position
1032             DSC => ansi_code('{DECSC}'), # DEC Save Cursor
1033             DRC => ansi_code('{DECRC}'), # DEC Restore Cursor
1034             CR => "\r",
1035 21     21   171 };
  21         38  
1036              
1037 21         42 my($progress_show, $progress_reset) = do {
1038 21         42 my $n;
1039             my($s, $e) = ! $need_color ? ('', '') :
1040 21 50       94 ( ansi_code $colormap{PROGRESS}, ansi_code 'Z');
1041 21     0   90 my $print = sub { STDERR->printflush(DSC, $s, @_, $e, CR, DRC) };
  0         0  
1042 21         38 my $start = do {
1043 21 50 33     152 if ($opt_d{n} and $opt_d{f}) {
    50          
    50          
1044 0     0   0 sub { $print->(++$n, " ", $current_file, ED) }
1045 0         0 }
1046             elsif ($opt_d{n}) {
1047 0     0   0 sub { $print->(++$n) }
1048 0         0 }
1049             elsif ($opt_d{f}) {
1050 0     0   0 sub { STDERR->printflush($current_file, ":\n") }
1051 0         0 }
1052             else {
1053 21         60 undef;
1054             }
1055             };
1056 21         38 my $end = do {
1057 21 50       59 if ($opt_d{n}) {
1058 0 0   0   0 sub { STDERR->printflush(ED) if $n }
1059 0         0 } else {
1060 21         42 undef;
1061             }
1062             };
1063 21         166 ($start, $end);
1064             };
1065              
1066             ##------------------------------------------------------------
1067             ## now ready to run.
1068             ##
1069              
1070             ## record start time
1071 21 50       73 if ($opt_d{s}) {
1072 0         0 $stat{time_start} = [times];
1073             }
1074              
1075 21         72 for (@opt_prologue) { $_->call() }
  24         127  
1076              
1077 21         607 grep_files();
1078              
1079 21         69 for (@opt_epilogue) { $_->call() }
  3         19  
1080              
1081 21 50       115 if ($opt_d{n}) {
1082 0         0 print STDERR ED;
1083             }
1084              
1085 21 0 33     74 if ($opt_uniqcolor and $opt_d{c}) {
1086 0         0 dump_uniqcolor();
1087             }
1088              
1089             ## show statistic info
1090 21 50       108 if ($opt_d{s}) {
1091              
1092 0         0 $stat{time_end} = [times];
1093 0         0 my @s = $stat{time_start}->@*;
1094 0         0 my @e = $stat{time_end}->@*;
1095 0         0 printf(STDERR "cpu %.3fu %.3fs\n", $e[0]-$s[0], $e[1]-$s[1]);
1096              
1097 0         0 local $" = ', ';
1098 0         0 for my $k (sort keys %stat) {
1099 0         0 my $v = $stat{$k};
1100 0 0       0 print STDERR
1101             "$k: ",
1102             ref $v eq 'ARRAY' ? "(@$v)" : $v,
1103             "\n";
1104             }
1105             }
1106              
1107 21         231 close_stdout;
1108              
1109 21 50       88 if ($opt_d{p}) {
1110 0         0 open STDOUT, ">&STDERR";
1111 0         0 system "ps -lww -p $$";
1112 0         0 system "lsof -p $$";
1113             }
1114              
1115 21   33     0 exit($opt_exit // ($stat{match_effective} == 0));
1116              
1117             ######################################################################
1118              
1119             sub grep_files {
1120             FILE:
1121 21     21   123 while (defined($current_file = open_nextfile())) {
1122 21         83 my $content = $slurp->();
1123 21         532 $stat{file_tried}++;
1124 21 50       83 if (not defined $content) {
1125 0 0       0 if ($opt_error eq 'fatal') {
1126 0         0 die "ABORT on $current_file\n";
1127             }
1128 0 0       0 if ($opt_error ne 'retry') {
1129 0 0       0 warn "SKIP $current_file\n" if $opt_warn{skip};
1130 0         0 next FILE;
1131             }
1132              
1133             # Try again
1134 0         0 binmode STDIN, ':raw';
1135 0 0       0 seek STDIN, 0, 0 or do {
1136             warn "SKIP $current_file (not seekable)\n"
1137 0 0       0 if $opt_warn{skip};
1138 0         0 next FILE;
1139             };
1140 0         0 $content = $slurp->();
1141 0 0       0 if (not defined $content) {
1142 0 0       0 warn "SKIP* $current_file\n" if $opt_warn{skip};
1143 0         0 next FILE;
1144             }
1145 0 0       0 warn "RETRY $current_file\n" if $opt_warn{retry};
1146 0         0 $stat{read_retry}++;
1147 0         0 binmode STDOUT, ':raw';
1148             }
1149              
1150 21         115 my $matched = grep_data(\$content);
1151              
1152 21         118 $stat{match_effective} += $matched;
1153 21         51 $stat{file_searched}++;
1154 21         108 $stat{length} += length $content;
1155             } continue {
1156 21         626 close STDIN; # wait; # wait for 4.019 or earlier?
1157             # recover STDIN for opening '-' and some weird command which needs
1158             # STDIN opened (like unzip)
1159 21         295 recover_stdin;
1160 21         933 binmode STDOUT, ":encoding($output_code)";
1161             }
1162             }
1163              
1164             sub usage {
1165 0     0   0 pod2usage(-verbose => 0, -exitval => "NOEXIT");
1166              
1167 0         0 my $quote = qr/[\\(){}\|\*?]/;
1168 0         0 for my $bucket ($rcloader->buckets) {
1169 0         0 my $module = $bucket->module;
1170 0         0 print " $module options:\n";
1171 0         0 for my $name ($bucket->options) {
1172 0 0 0     0 my $help = $opt_usage ? "" : $bucket->help($name) // "";
1173 0 0       0 next if $help eq 'ignore';
1174 0         0 my @option = $bucket->getopt($name, ALL => 1);
1175 0   0     0 printf(" %-20s %s\n", $name,
1176             $help || join(' ', shellquote(@option)));
1177             }
1178 0         0 print "\n";
1179             }
1180              
1181 0         0 print "Version: $version\n";
1182              
1183 0         0 exit 2;
1184             }
1185              
1186             sub open_nextfile {
1187              
1188             ##
1189             ## --chdir
1190             ##
1191 42   66 42   1447 while (@ARGV == 0 and @opt_chdir and (@argv_files or @opt_glob)) {
      0        
      33        
1192 0         0 my $dir = shift @opt_chdir;
1193 0 0       0 warn "chdir $dir/\n" if $opt_d{d};
1194 0 0       0 chdir $start_directory or die "$!: $start_directory\n";
1195 0 0       0 chdir $dir or die "$!: $dir\n";
1196 0         0 push @ARGV, @argv_files, map(glob, @opt_glob);
1197             }
1198              
1199 42         117 my $file;
1200 42   33     352 while (defined($file = shift(@ARGV)) ||
      66        
1201             defined($file = $opt_readlist && read_stdin)) {
1202 21 100       130 $file = decode 'utf8', $file unless utf8::is_utf8 $file;
1203 21         221 $file =~ s/\n+$//;
1204              
1205 21 50       183 if (0) {}
    100          
1206 0         0 elsif ($file =~ /^https?:\/\//) {
1207 0 0 0     0 open(STDIN, '-|') || exec("w3m -dump $file") || die "w3m: $!\n";
1208             }
1209             elsif ($file eq '-') {
1210             # nothing to do
1211             }
1212             else {
1213 19 50       2128 open(STDIN, '<', $file) or do {
1214 0 0       0 warn "$file: $!\n" unless -l $file;
1215 0         0 next;
1216             };
1217             }
1218              
1219 21 50       196 if (my @filters = $filter_d->get_filters($file)) {
1220 0         0 push_input_filter({ &FILELABEL => $file }, @filters);
1221             }
1222              
1223 21 50       310 if ($file_code eq 'binary') {
1224 0         0 binmode STDIN, ":raw";
1225             } else {
1226 21         305 binmode STDIN, ":encoding($file_code)";
1227             }
1228              
1229 21         1096 return $file;
1230             }
1231 21         102 undef;
1232             }
1233              
1234             ######################################################################
1235              
1236             sub grep_data {
1237 21     21   81 local *_ = shift;
1238              
1239             ##
1240             ## --begin
1241             ##
1242 21         66 for my $f (@opt_begin) {
1243 25         45 eval { $f->call(&FILELABEL => $current_file) };
  25         178  
1244 25 50       273 if (my $msg = $@) {
1245 0 0       0 if ($msg =~ /^SKIP/i) {
1246 0 0       0 warn $@ if $opt_warn{begin};
1247 0         0 return 0;
1248             } else {
1249 0         0 die $msg;
1250             }
1251             }
1252             }
1253              
1254 21 50       87 $progress_show->() if $progress_show;
1255              
1256             my $grep = App::Greple::Grep->new(
1257             text => \$_,
1258             filename => $current_file,
1259             pattern => $pat_holder,
1260             regions => $regions,
1261             border => $border_re,
1262             after => $opt_A,
1263             before => $opt_B,
1264             only => $opt_o,
1265             all => $opt_all,
1266             block => \@opt_block,
1267             stretch => $opt_stretch,
1268             must => $count_must,
1269             need => $count_need,
1270             countcheck => $count_match_sub,
1271             allow => $count_allow,
1272             strict => $opt_strict,
1273 21         44 group_index => do { local $_ = $opt_colorindex;
1274 21 0       418 $opt_capture_group ? /G/i ? /P/i ? 3 : 2 : 1 : 0 },
    0          
    50          
1275             region_index => $opt_regioncolor,
1276             stat => \%stat,
1277             callback => \@opt_callback,
1278             alert_size => $opt_alert{size},
1279             alert_time => $opt_alert{time},
1280 21         90 join_blocks => $opt_join_blocks,
1281             )->run;
1282              
1283             ## --postgrep
1284 21         25415 for my $f (@opt_postgrep) {
1285 0         0 $f->call($grep);
1286             # remove emptied results
1287 0         0 my $ref = $grep->result_ref;
1288 0         0 @$ref = grep { @{$_} > 0 } @$ref;
  0         0  
  0         0  
1289             }
1290             ## -m
1291 21         63 for my $splice (@splicer) {
1292 0         0 $splice->($grep->result_ref);
1293             }
1294              
1295 21         166 my $matched = $grep->matched;
1296 21 50       773 if ($opt_l) {
    50          
    50          
1297 0 0       0 if ($matched) {
1298 0 0       0 $progress_reset->() if $progress_reset;
1299 0         0 print $current_file;
1300 0 0       0 printf ":%d", scalar $grep->result if $opt_c;
1301 0         0 print "\n";
1302             }
1303             }
1304             elsif ($opt_c) {
1305 0 0       0 $progress_reset->() if $progress_reset;
1306 0 0       0 print "$current_file:" if $need_filename;
1307 0         0 print scalar $grep->result, "\n";
1308             }
1309             elsif ($grep->result_ref->@*) {
1310 21 50       275 $progress_reset->() if $progress_reset;
1311             # open output filter
1312 21 100       232 @opt_of && push_output_filter(
1313             { &FILELABEL => $current_file },
1314             \*STDOUT, @opt_of);
1315 21         8023 output($grep);
1316 21 100       170 @opt_of && recover_stdout;
1317             }
1318              
1319             ##
1320             ## --end
1321             ##
1322 21         398 for my $f (@opt_end) {
1323 1         14 $f->call(&FILELABEL => $current_file);
1324             }
1325              
1326 21 50       229 s/./\000/gs if $opt_clean;
1327              
1328 21         1363 $matched;
1329             }
1330              
1331             sub output {
1332 21     21   140 my $grep = shift;
1333 21         328 my $file = $grep->{filename};
1334              
1335 21 50       314 if ($opt_filestyle eq 'once') {
1336 0         0 print $_file->($file), "\n";
1337             }
1338              
1339 21   66     639 my $need_blockend =
1340             !$opt_all &&
1341             $blockend ne '' &&
1342             ($opt_blockend || $opt_p || $opt_A || $opt_B || @opt_block);
1343              
1344 21         82 my $line = 1;
1345 21         73 my $lastpos = 0;
1346 21         245 my @results = $grep->result;
1347 21         355 for my $rix (keys @results) {
1348 73         184 my $is_top = $rix == 0;
1349 73         188 my $is_bottom = $rix == $#results;
1350              
1351 73         140 my $result = $results[$rix];
1352 73         131 my($blk, @result) = @{$result};
  73         380  
1353 73         168 my($block_start, $block_end, $block_number) = @$blk;
1354 73   100     332 $block_number //= 0;
1355 73         1441 my $block = $grep->cut($block_start, $block_end);
1356              
1357             ## --print
1358 73 50       820 if (@opt_print) {
1359 0         0 local *_ = \$block;
1360 0         0 for my $func (@opt_print) {
1361 0         0 $_ = $func->call(&FILELABEL => $file);
1362             }
1363 0 0       0 if (not $opt_continue) {
1364 0 0       0 print $block if defined $block;
1365 0         0 next;
1366             }
1367             }
1368              
1369 73 50       183 if ($opt_n) {
1370 0         0 my $gap = $grep->cut($lastpos, $block_start);
1371 0         0 $line += $gap =~ tr/\n/\n/;
1372             }
1373 73         130 $lastpos = $block_end;
1374              
1375             # when --filestyle and/or --linestyle is "separate"
1376 219         466 grep { $_ } (
1377             do {
1378 73 50       235 print $_file->($current_file)
1379             if $opt_filestyle eq 'separate';
1380             },
1381             do {
1382 73 50 33     305 print $_line->($line)
1383             if $opt_n and $opt_linestyle eq 'separate';
1384             },
1385 73 50       116 do {
1386 73 50 33     326 print $_block->($block_number)
1387             if $opt_b and $opt_blockstyle eq 'separate';
1388             },
1389             ) and print "\n";
1390              
1391 73 50       217 if ($indexer) {
1392 0 0       0 $indexer->reset if $indexer->block;
1393 0 0       0 for ($indexer->reverse ? reverse @result : @result) {
1394 0         0 $_->[2] = $indexer->index;
1395             }
1396             }
1397              
1398 73         254 my @slice = $grep->slice_result($result);
1399 73         3854 my $mark = "\001";
1400 73         298 for my $i (keys @result) {
1401 166         5102 my($start, $end, $pi, $callback) = $result[$i]->@*;
1402 166         507 local *b = \$slice[$i * 2 + 1];
1403              
1404             ## run callback function
1405 166 50       397 if ($callback) {
1406 166   33     264 $b = do {
1407 166 50       377 if (ref $callback eq 'CODE') {
    0          
1408 166         503 $callback->($start, $end, $pi, $b);
1409             }
1410             elsif (callable($callback)) {
1411 0         0 $callback->call(
1412             &FILELABEL => $file,
1413             start => $start,
1414             end => $end,
1415             index => $pi,
1416             match => $b);
1417             }
1418 0         0 else { die }
1419             } // $b;
1420             }
1421              
1422 166 50       653 if ($opt_join) {
1423 0 0 0     0 if ($opt_n and $opt_linestyle eq 'line') {
1424 0         0 $b =~ s/(?<!\A)\n(?!\z)/$mark/g;
1425             } else {
1426 0         0 $b =~ s/(?<!\A)\n(?!\z)/$opt_joinby/g;
1427             }
1428             }
1429              
1430 166 50       308 $pi = $uniq_color->index($b) if $opt_uniqcolor;
1431 166         449 $b = index_color($pi, $b);
1432             }
1433              
1434 73         3411 $block = join '', @slice;
1435 73 50       205 next if $block eq "";
1436              
1437 73         244 my @line;
1438 73 50       247 if ($opt_n) {
1439 0 0       0 if ($opt_linestyle eq 'line') {
1440 0 0       0 my $increment = $block =~ /[\n$mark]/ ? 1 : 0;
1441 0         0 $block =~ s{(?:(?<mark>$mark)|(?<=\n)|\A)(?=.)}{
1442 0 0       0 push @line, $_line->($line) unless $+{mark};
1443 0         0 $line += $increment;
1444 0 0       0 $+{mark} ? $opt_joinby : '';
1445             }gse;
1446             } else {
1447 0         0 $line += $block =~ tr/\n/\n/;
1448             }
1449             }
1450              
1451 73 50       225 $block = $_text->($block) if $colormap{TEXT} ne "";
1452              
1453 73 50       355 if (@line) {
1454 0         0 $block =~ s/^/shift @line/mge;
  0         0  
1455             }
1456              
1457 73 50 33     291 if ($opt_b and $opt_blockstyle eq 'line') {
1458 0         0 my $s = $_block->($block_number);
1459 0         0 $block =~ s/^/$s/mg;
1460             }
1461              
1462 73 50       287 if ($opt_filestyle eq 'line') {
1463 0         0 my $s = $_file->($file);
1464 0         0 $block =~ s/^/$s/mg;
1465             }
1466              
1467 73 50 66     300 print "$_top\n" if $is_top && $_top ne '';
1468 73         253 print $block;
1469 73 50 66     581 print "\n" if $opt_newline and not $block =~ /\n\z/;
1470 73 50       300 print "$_blockend\n" if $need_blockend;
1471 73 100       189 if ($is_bottom) {
1472 21 50       225 print "$_bottom\n" if $_bottom ne '';
1473             } else {
1474 52 50       195 print "$_middle\n" if $_middle ne '';
1475             }
1476             }
1477             }
1478              
1479             __END__
1480              
1481              
1482             =head1 INSTALL
1483              
1484              
1485             =head2 CPANMINUS
1486              
1487             $ cpanm App::Greple
1488              
1489              
1490             =head1 SUMMARY
1491              
1492             B<greple> is a grep-like tool designed for searching structured text
1493             such as source code and documents. Key features include:
1494              
1495             =over 4
1496              
1497             =item *
1498              
1499             B<Flexible pattern matching>: Multiple keyword search with AND/OR/NOT
1500             logic, including multi-line matching and lexical expressions.
1501              
1502             =item *
1503              
1504             B<Region control>: Target specific sections with C<--inside>,
1505             C<--outside>, C<--include>, and C<--exclude> options. Useful for
1506             searching only within code blocks, comments, or other delimited
1507             regions.
1508              
1509             =item *
1510              
1511             B<Block-oriented processing>: Define and search custom text blocks
1512             such as paragraphs or function definitions.
1513              
1514             =item *
1515              
1516             B<Multi-byte support>: Native handling of Japanese and other Asian
1517             languages with proper character encoding.
1518              
1519             =item *
1520              
1521             B<Extensibility>: Module system allows custom search patterns and
1522             filters for specific document types or use cases.
1523              
1524             =back
1525              
1526             While it can be used for general text search, greple excels at
1527             searching source code, structured documents, and multi-byte text where
1528             context and precision matter.
1529              
1530              
1531             =head1 DESCRIPTION
1532              
1533              
1534             =head2 MULTIPLE KEYWORDS
1535              
1536              
1537             =head3 AND
1538              
1539             B<greple> can take multiple search patterns with the C<-e> option, but
1540             unlike the L<egrep(1)> command, it will search them in AND context.
1541             For example, the next command prints lines that contain all of
1542             C<foo> and C<bar> and C<baz>.
1543              
1544             greple -e foo -e bar -e baz ...
1545              
1546             Each word can appear in any order and any place in the string. So
1547             this command finds all of the following lines.
1548              
1549             foo bar baz
1550             baz bar foo
1551             the foo, bar and baz
1552              
1553             =head3 OR
1554              
1555             If you want to use OR syntax, use regular expression.
1556              
1557             greple -e foo -e bar -e baz -e 'yabba|dabba|doo'
1558              
1559             This command will print lines that contain all of C<foo>, C<bar> and
1560             C<baz> and one or more of C<yabba>, C<dabba> or C<doo>.
1561              
1562             Multiple patterns may be described in a file line-by-line, and
1563             specified with the B<-f> option. In that case, the blocks matching any
1564             of the patterns in that file will be displayed.
1565              
1566             greple -f patterns ...
1567              
1568             If multiple files are specified, the patterns in the files are
1569             evaluated in the OR and each file in the AND context.
1570              
1571             The two commands below work the same way.
1572              
1573             greple -f <(echo $'foo\nbar\nbaz') -f <(echo $'yabba\ndabba\ndoo')
1574              
1575             greple -e 'foo|bar|baz' -e 'yabba|dabba|doo'
1576              
1577             =head3 NOT
1578              
1579             Use option C<-v> to specify keyword which should not be found in the data
1580             record. Next example shows lines that contain both C<foo> and
1581             C<bar> but none of C<yabba>, C<dabba> or C<doo>.
1582              
1583             greple -e foo -e bar -v yabba -v dabba -v doo
1584             greple -e foo -e bar -v 'yabba|dabba|doo'
1585              
1586             =head3 MAY
1587              
1588             When you are focusing on multiple words, there may be words that are
1589             not necessary but would be of interest if they were present.
1590              
1591             Use option C<--may> or C<-t> (tentative) to specify that kind of
1592             words. They will be a subject of search, and highlighted if they exist,
1593             but are optional.
1594              
1595             Next command prints all lines including C<foo> and C<bar>, and
1596             highlights C<baz> as well.
1597              
1598             greple -e foo -e bar -t baz
1599              
1600             =head3 MUST
1601              
1602             Option C<--must> or C<-r> is another way to specify optional keyword.
1603             If required keyword exists, all other positive match keyword becomes
1604             optional. Next command is equivalent to the above example.
1605              
1606             greple -r foo -r bar -e baz
1607              
1608              
1609             =head2 LEXICAL EXPRESSION
1610              
1611              
1612             B<greple> takes the first argument as a search pattern specified by
1613             C<--le> option. In the C<--le> pattern, you can set multiple keywords
1614             in a single parameter. Each keyword is separated by spaces, and the
1615             first letter describes its type.
1616              
1617             none And pattern : --and -e
1618             + Required pattern : --must -r
1619             - Negative match pattern : --not -v
1620             ? Optional pattern : --may -t
1621              
1622             Just like internet search engines, you can simply provide C<foo bar
1623             baz> to search lines including all of them.
1624              
1625             greple 'foo bar baz'
1626              
1627             Next command shows lines which include C<foo>, but do not include
1628             C<bar>, and highlights C<baz> if it exists.
1629              
1630             greple 'foo -bar ?baz'
1631              
1632              
1633             =head2 PHRASE SEARCH
1634              
1635              
1636             B<greple> searches a given pattern across line boundaries. This is
1637             especially useful to handle Asian multi-byte text, more specifically
1638             Japanese. Japanese text can be separated by newline almost any place
1639             in the text. So the search pattern may spread out onto multiple
1640             lines.
1641              
1642             As for the ASCII word list, the space character in the pattern matches
1643             any type of space, including newlines. The next example will search
1644             for the word sequence of C<foo>, C<bar> and C<baz>, even if they are
1645             spread over lines.
1646              
1647             greple -e 'foo bar baz'
1648              
1649             Option C<-e> is necessary because space is taken as a token separator
1650             in the bare or C<--le> pattern.
1651              
1652              
1653             =head2 FLEXIBLE BLOCKS
1654              
1655              
1656             Default data block B<greple> searches and prints is a line. Using
1657             C<--paragraph> (or C<-p> in short) option, series of text separated by
1658             empty line is taken as a record block. So the next command prints
1659             whole paragraph which contains the word C<foo>, C<bar> and C<baz>.
1660              
1661             greple -p 'foo bar baz'
1662              
1663             Block can also be defined by pattern. Next command treats the data as
1664             a series of 10-line units.
1665              
1666             greple -n --border='(.*\n){1,10}'
1667              
1668             You can also define arbitrary complex blocks by writing module or
1669             script.
1670              
1671             greple -Myour_module --block '&your_function' ...
1672              
1673              
1674             =head2 MATCH AREA CONTROL
1675              
1676              
1677             Using option C<--inside> and C<--outside>, you can specify the text
1678             area to be matched. Next commands search only in mail header and body
1679             area respectively. In these cases, data block is not changed, so
1680             print lines which contain the pattern in the specified area.
1681              
1682             greple --inside '\A(.+\n)+' pattern
1683              
1684             greple --outside '\A(.+\n)+' pattern
1685              
1686             Option C<--inside>/C<--outside> can be used repeatedly to enhance the
1687             area to be matched. There are similar options
1688             C<--include>/C<--exclude>, but they are used to trim down the area.
1689              
1690             These four options also take user defined function and any complex
1691             region can be used.
1692              
1693              
1694             =head2 MODULE AND CUSTOMIZATION
1695              
1696              
1697             User can define default and original options in F<~/.greplerc>. Next
1698             example enables colored output always, and define new option using
1699             macro processing.
1700              
1701             option default --color=always
1702              
1703             define :re1 complex-regex-1
1704             define :re2 complex-regex-2
1705             define :re3 complex-regex-3
1706             option --newopt --inside :re1 --exclude :re2 --re :re3
1707              
1708             Specific set of function and option interface can be implemented as
1709             module. Modules are invoked by C<-M> option immediately after command
1710             name.
1711              
1712             For example, B<greple> does not have recursive search option, but it
1713             can be implemented by C<--readlist> option which accepts target file
1714             list from standard input. Using B<find> module, it can be written
1715             like this:
1716              
1717             greple -Mfind . -type f -- pattern
1718              
1719             Also B<dig> module implements more complex search. It can be used as
1720             simple as this:
1721              
1722             greple -Mdig pattern --dig .
1723              
1724             but this command is finally translated into following option list.
1725              
1726             greple -Mfind . ( -name .git -o -name .svn -o -name RCS ) -prune -o
1727             -type f ! -name .* ! -name *,v ! -name *~
1728             ! -iname *.jpg ! -iname *.jpeg ! -iname *.gif ! -iname *.png
1729             ! -iname *.tar ! -iname *.tbz ! -iname *.tgz ! -iname *.pdf
1730             -print -- pattern
1731              
1732              
1733             =head2 INCLUDED MODULES
1734              
1735              
1736             The distribution includes some sample modules. Read document in each
1737             module for detail. You can read the document by C<--man> option or
1738             L<perldoc> command.
1739              
1740             greple -Mdig --man
1741              
1742             perldoc App::Greple::dig
1743              
1744             When it does not work, use C<perldoc App::Greple::dig>.
1745              
1746             =over 7
1747              
1748             =item B<colors>
1749              
1750             Color variation module.
1751             See L<App::Greple::colors>.
1752              
1753             =item B<find>
1754              
1755             Module to use L<find(1)> command to help recursive search.
1756             See L<App::Greple::find>.
1757              
1758             =item B<dig>
1759              
1760             Module for recursive search using B<find> module. Defines C<--dig>,
1761             C<--git> and C<--git-r> options. See L<App::Greple::dig>.
1762              
1763             =item B<pgp>
1764              
1765             Module to search B<pgp> files.
1766             See L<App::Greple::pgp>.
1767              
1768             =item B<select>
1769              
1770             Module to select files.
1771             See L<App::Greple::select>.
1772              
1773             =item B<perl>
1774              
1775             Sample module to search from perl source files.
1776             See L<App::Greple::perl>.
1777              
1778             =back
1779              
1780             Other modules are available at CPAN, or git repository
1781             L<https://github.com/kaz-utashiro/>.
1782              
1783              
1784             =head1 OPTIONS
1785              
1786              
1787             =head2 PATTERNS
1788              
1789              
1790             If no positive pattern option is given (i.e. other than C<--not> and
1791             C<--may>), B<greple> takes the first argument as a search pattern
1792             specified by C<--le> option. All of these patterns can be specified
1793             multiple times.
1794              
1795             Command itself is written in Perl, and any kind of Perl style regular
1796             expression can be used in patterns. See L<perlre(1)> for detail.
1797              
1798             Note that multiple line modifier (C<m>) is set when executed, so put
1799             C<(?-m)> at the beginning of regex if you want to explicitly disable
1800             it.
1801              
1802             Order of capture group in the pattern is not guaranteed. Please avoid
1803             to use direct index, and use relative or named capture group instead.
1804             For example, if you want to search repeated characters, use
1805             S<< C<(\w)\g{-1}> >> or S<< C<(?E<lt>cE<gt>\w)\g{c}> >> rather than
1806             S<< C<(\w)\1> >>.
1807              
1808             Extended Bracketed Character Classes (C<(?[...])>) and Variable Length
1809             Lookbehind can be used without warnings. See
1810             L<perlrecharclass/"Extended Bracketed Character Classes"> and
1811             L<perlre/"(?<=pattern)">.
1812              
1813             =over 7
1814              
1815             =item B<-e> I<pattern>, B<--and>=I<pattern>
1816              
1817             Specify the positive match pattern. Next command prints lines containing
1818             all of C<foo>, C<bar> and C<baz>.
1819              
1820             greple -e foo -e bar -e baz
1821              
1822             =item B<-t> I<pattern>, B<--may>=I<pattern>
1823              
1824             Specify the optional (tentative) match pattern. Next command prints
1825             lines containing C<foo> and C<bar>, and highlights C<baz> if it exists.
1826              
1827             greple -e foo -e bar -t baz
1828              
1829             Since it does not affect the bare pattern argument, you can add the
1830             highlighting word to the end of the command argument as follows.
1831              
1832             greple foo file
1833             greple foo file -t bar
1834             greple foo file -t bar -t baz
1835              
1836             =item B<-r> I<pattern>, B<--must>=I<pattern>
1837              
1838             Specify the required match pattern. If one or more required pattern
1839             exist, other positive match pattern becomes optional.
1840              
1841             greple -r foo -r bar -e baz
1842              
1843             Because C<-t> promotes all other C<-e> patterns to required, the next command
1844             does the same thing. Mixing C<-r>, C<-e> and C<-t> is not recommended,
1845             though.
1846              
1847             greple -r foo -e bar -t baz
1848              
1849             =item B<-v> I<pattern>, B<--not>=I<pattern>
1850              
1851             Specify the negative match pattern. Because it does not affect the
1852             bare pattern argument, you can narrow down the search result like
1853             this.
1854              
1855             greple foo file
1856             greple foo file -v bar
1857             greple foo file -v bar -v baz
1858              
1859             =back
1860              
1861             In the above pattern options, space characters are treated specially.
1862             They are replaced by the pattern which matches any number of white
1863             spaces including newline. So the pattern can expand to multiple
1864             lines. Next commands search the series of word C<foo> C<bar> C<baz>
1865             even if they are separated by newlines.
1866              
1867             greple -e 'foo bar baz'
1868              
1869             This is done by converting pattern C<foo bar baz> to
1870             C<foo\s+bar\s+baz>, so that word separator can match one or more white
1871             spaces.
1872              
1873             As for Asian wide characters, pattern is cooked as zero or more white
1874             spaces can be allowed between any characters. So Japanese string
1875             pattern C<日本語> will be converted to C<< 日\s*本\s*語 >>.
1876              
1877             If you don't want these conversion, use C<-E> (or C<--re>) option.
1878              
1879             =over 4
1880              
1881             =item B<-x> I<pattern>, B<--le>=I<pattern>
1882              
1883             Treat the pattern string as a collection of tokens separated by
1884             spaces. Each token is interpreted by the first character. Token
1885             start with C<-> means B<negative> pattern, C<?> means B<optional>, and
1886             C<+> does B<required>.
1887              
1888             The next example prints lines containing C<foo> and C<yabba>,
1889             and none of C<bar> and C<dabba>, with highlighting C<baz> and C<doo>
1890             if they exist.
1891              
1892             greple --le='foo -bar ?baz yabba -dabba ?doo'
1893              
1894             This is the summary of start character for C<--le> option:
1895              
1896             + Required pattern
1897             - Negative match pattern
1898             ? Optional pattern
1899             & Function call (see next section)
1900              
1901             =item B<-x> [B<+?->]B<&>I<function>, B<--le>=[B<+?->]B<&>I<function>
1902              
1903             If the pattern starts with ampersand (C<&>), it is treated as a
1904             function, and the function is called instead of searching pattern.
1905             Function call interface is the same as the one for block/region options.
1906              
1907             If you have a definition of I<odd_line> function in your F<.greplerc>,
1908             which is described in this manual later, you can print odd number
1909             lines like this:
1910              
1911             greple -n '&odd_line' file
1912              
1913             Required (C<+>), optional (C<?>) and negative (C<->) mark can be used
1914             for function pattern.
1915              
1916             B<CALLBACK FUNCTION>: Region list returned by function can have two
1917             extra elements besides start/end position. Third element is index.
1918             Fourth element is a callback function pointer which will be called to
1919             produce string to be shown in command output. Callback function is
1920             called with four arguments (start position, end position, index,
1921             matched string) and expected to return replacement string. If the
1922             function returns C<undef>, the result is not changed.
1923              
1924             =item B<-E> I<pattern>, B<--re>=I<pattern>
1925              
1926             Specify regular expression. No special treatment for space and wide
1927             characters.
1928              
1929             =item B<--fe>=I<pattern>
1930              
1931             Specify the fixed string pattern, like L<fgrep(1)>.
1932              
1933             =item B<-i>, B<--ignore-case>
1934              
1935             Ignore case.
1936              
1937             =item B<-G>, B<--capture-group>
1938              
1939             Normally, B<greple> searches for strings that match the entire
1940             pattern. Even if it contains a capturing groups, they do not affect
1941             the search target. When this option is given, strings corresponding
1942             to individual capture groups are searched, not the entire pattern. If
1943             the pattern does not contain any capturing groups, it matches the
1944             entire pattern.
1945              
1946             If C<G> is specified in the B<--colorindex> option, a sequential
1947             index starting from 0 is assigned to each capture group across all
1948             patterns. Patterns without capture groups count as one group. This
1949             will cause the strings corresponding to each capture group to be
1950             displayed in a different color.
1951              
1952             =item B<-S>, B<--stretch>
1953              
1954             Forget the information about the individual match strings and act as if
1955             the block containing them matched.
1956              
1957             The following command will match an entire line containing all of
1958             C<foo>, C<bar>, and C<baz> in any order.
1959              
1960             greple --stretch 'foo bar baz'
1961              
1962             The index is set to the smallest index number of the matches contained
1963             in the block.
1964              
1965             If multiple callback functions are specified, the first match in the
1966             block will be effective.
1967              
1968             =item B<--need>=I<n>
1969              
1970             =item B<--allow>=I<n>
1971              
1972             Option to compromise matching condition. Option C<--need> specifies
1973             the required match count, and C<--allow> the number of negative
1974             condition to be overlooked.
1975              
1976             greple --need=2 --allow=1 'foo bar baz -yabba -dabba -doo'
1977              
1978             Above command prints the line which contains two or more from C<foo>,
1979             C<bar> and C<baz>, and does not include more than one of C<yabba>,
1980             C<dabba> or C<doo>.
1981              
1982             Using option C<--need=1>, B<greple> produces same result as B<grep>
1983             command.
1984              
1985             grep -e foo -e bar -e baz
1986             greple -e foo -e bar -e baz --need=1
1987              
1988             When the count I<n> is negative value, it is subtracted from default
1989             value.
1990              
1991             If the option C<--need=0> is specified and no pattern was found,
1992             entire data is printed. This is true even for required pattern.
1993              
1994             =item B<--matchcount>=I<count> B<--mc>=...
1995              
1996             =item B<--matchcount>=I<min>,I<max> B<--mc>=...
1997              
1998             When option C<--matchcount> is specified, only blocks which have given
1999             match count will be shown. Minimum and maximum number can be given,
2000             connecting by comma, and they can be omitted. Next commands print
2001             lines including semicolons; 3 or more, exactly 3, and 3 or less,
2002             respectively.
2003              
2004             greple --matchcount=3, ';' file
2005              
2006             greple --matchcount=3 ';' file
2007              
2008             greple --matchcount=,3 ';' file
2009              
2010             In fact, I<min> and I<max> can repeat to represent multiple ranges.
2011             Missing, negative or zero I<max> means infinite. Next command finds
2012             match count 0 to 10, 20 to 30, and 40-or-greater.
2013              
2014             greple --matchcount=,10,20,30,40
2015              
2016             =item B<-f> I<file>[@I<index>], B<--file>=I<file>[@I<index>]
2017              
2018             Specifies the file containing the search pattern. If there are
2019             multiple lines in the file, each pattern is combined by an OR context.
2020             So the file:
2021              
2022             A
2023             B
2024             C
2025              
2026             makes the pattern as C<A|B|C>, or more precisely
2027             C<(?^m:A)|(?^m:B)|(?^m:C)>.
2028              
2029             Each of these patterns are evaluated independently only with C<m>
2030             modifier. So if you enable some flags in a pattern, they are only
2031             valid within itself.
2032              
2033             Blank line and the line starting with sharp (#) character is ignored.
2034             Two slashes (//) and following string are taken as a comment and
2035             removed with preceding spaces. If the character at the end of the
2036             line is a backslash, the backslash is removed and concatenated with
2037             the next line.
2038              
2039             Complex pattern can be written on multiple lines as follows.
2040              
2041             (?xxn) \
2042             ( (?<b>\[) | \@ ) # start with "[" or @ \
2043             (?<n> [ \d : , ]+) # sequence of digit, ":", or "," \
2044             (?(<b>) \] | ) # closing "]" if start with "[" \
2045             $ # EOL
2046              
2047             DEFINE patterns are supported for defining reusable subpatterns.
2048             Lines starting with C<(?(DEFINE)> are used for declarations only and
2049             not included as search patterns. Other patterns can reference these
2050             definitions using C<(?&name)> syntax.
2051              
2052             (?(DEFINE)(?<digit>\d+))
2053             (?&digit)+
2054             (?&digit){4}
2055              
2056             When writing DEFINE and pattern on the same line, place the pattern
2057             first and DEFINE at the end (recommended in L<perlre/"(DEFINE)">):
2058              
2059             (?&digit)+(?(DEFINE)(?<digit>\d+))
2060              
2061             Or prefix with C<(?:)> (empty non-capturing group that does nothing)
2062             to place DEFINE at the beginning:
2063              
2064             (?:)(?(DEFINE)(?<digit>\d+))(?&digit)+
2065              
2066             If multiple files are specified, a separate group pattern is generated
2067             for each file.
2068              
2069             If the file name is followed by C<@index> string, it is treated as
2070             specified by C<--select> option. Next commands are all equivalent.
2071              
2072             greple -f pattern_file@2,7:9
2073              
2074             greple -f pattern_file --select 2,7:9
2075              
2076             See L<App::Greple::subst> module.
2077              
2078             =item B<--select>=I<index>
2079              
2080             When you want to choose specific line in the pattern file provided by
2081             C<-f> option, use C<--select> option. I<index> is number list
2082             separated by comma (,) character and each number is interpreted by
2083             L<Getopt::EX::Numbers> module. Take a look at the module document for
2084             detail.
2085              
2086             Next command uses 2nd and 7,8,9th lines in the pattern file.
2087              
2088             greple -f pattern_file --select 2,7:9
2089              
2090             =back
2091              
2092             B<Related options:>
2093             B<--inside>/B<--outside>/B<--include>/B<--exclude> (L</REGIONS>),
2094             B<--block> (L</BLOCKS>)
2095              
2096              
2097             =head2 STYLES
2098              
2099              
2100             =over 7
2101              
2102             =item B<-l>
2103              
2104             List filename only.
2105              
2106             =item B<-c>, B<--count>
2107              
2108             Print count of matched block.
2109              
2110             =item B<-n>, B<--line-number>
2111              
2112             Show line number.
2113              
2114             =item B<-b>, B<--block-number>
2115              
2116             Show block number.
2117              
2118             =item B<-h>, B<--no-filename>
2119              
2120             Do not display filename.
2121              
2122             =item B<-H>
2123              
2124             Display filename always.
2125              
2126             =item B<-o>, B<--only-matching>
2127              
2128             Print matched string only. Newline character is printed after matched
2129             string if it does not end with newline. Use C<--no-newline> option if
2130             you don't need extra newline.
2131              
2132             =item B<--all>
2133              
2134             Print entire file. This option does not affect search behavior or
2135             block treatment. Just print all contents. Can be negated by the
2136             B<--no-all> option.
2137              
2138             =item B<-F>, B<--filter>
2139              
2140             Use B<greple> as a filter. This option implicitly sets B<--all>,
2141             B<--need>=C<0> and B<--exit>=C<0>, so the entire input is printed
2142             regardless of whether or not any pattern is matched.
2143              
2144             With this option, a search pattern is not required. The first
2145             argument is treated as a filename, not a pattern. To specify a
2146             pattern, use an explicit option such as B<-E>. When a
2147             pattern is given, matched parts are highlighted but no lines are
2148             excluded from the output.
2149              
2150             Can be negated by the B<--no-filter> option.
2151              
2152             =item B<-m> I<n>[,I<m>], B<--max-count>=I<n>[,I<m>]
2153              
2154             Set the maximum count of blocks to be shown to I<n>.
2155              
2156             Actually I<n> and I<m> are simply passed to perl L<splice> function as
2157             I<offset> and I<length>. Works like this:
2158              
2159             greple -m 10 # get first 10 blocks
2160             greple -m 0,-10 # get last 10 blocks
2161             greple -m 0,10 # remove first 10 blocks
2162             greple -m -10 # remove last 10 blocks
2163             greple -m 10,10 # remove 10 blocks from 10th (10-19)
2164              
2165             This option does not affect search performance or command exit
2166             status.
2167              
2168             Note that B<grep> command also has the same option, but its behavior is
2169             different when invoked with multiple files. B<greple> produces given
2170             number of output for each file, while B<grep> takes it as a total
2171             number of output.
2172              
2173             =item B<-m> I<*>, B<--max-count>=I<*>
2174              
2175             In fact, I<n> and I<m> can repeat as many as possible. Next example
2176             removes first 10 blocks (by C<0,10>), then get first 10 blocks from
2177             the result (by C<10>). Consequently, get 10 blocks from 10th (10-19).
2178              
2179             greple -m 0,10,10
2180              
2181             Next command gets first 20 (by C<20,>) and gets last 10 (by C<,-10>),
2182             producing same result. Empty string behaves like absence for
2183             I<length> and zero for I<offset>.
2184              
2185             greple -m 20,,,-10
2186              
2187             =item B<-A>[I<n>], B<--after-context>[=I<n>]
2188              
2189             =item B<-B>[I<n>], B<--before-context>[=I<n>]
2190              
2191             =item B<-C>[I<n>], B<--context>[=I<n>]
2192              
2193             Print I<n>-blocks before/after matched string. The value I<n> can be
2194             omitted and the default is 2. When used with C<--paragraph> or
2195             C<--block> option, I<n> means number of paragraph or block.
2196              
2197             Actually, these options expand the area of logical operation. It
2198             means
2199              
2200             greple -C1 'foo bar baz'
2201              
2202             matches following text.
2203              
2204             foo
2205             bar
2206             baz
2207              
2208             Moreover
2209              
2210             greple -C1 'foo baz'
2211              
2212             also matches this text, because matching blocks around C<foo> and
2213             C<bar> overlaps each other and makes single block.
2214              
2215             =item B<--join>
2216              
2217             =item B<--joinby>=I<string>
2218              
2219             Convert newline character found in matched string to empty or specified
2220             I<string>. Using C<--join> with C<-o> (only-matching) option, you can
2221             collect searching sentence list in one per line form. This is
2222             sometimes useful for Japanese text processing. For example, next
2223             command prints the list of KATAKANA words, including those spread
2224             across multiple lines.
2225              
2226             greple -ho --join '\p{InKatakana}+(\n\p{InKatakana}+)*'
2227              
2228             Space separated word sequence can be processed with C<--joinby>
2229             option. Next example prints all C<for *something*> pattern in pod
2230             documents within Perl script.
2231              
2232             greple -Mperl --pod -ioe '\bfor \w+' --joinby ' '
2233              
2234             =item B<--[no]newline>
2235              
2236             Since B<greple> can handle arbitrary blocks other than normal text
2237             lines, they sometimes do not end with newline character. Option C<-o>
2238             makes similar situation. In that case, extra newline is appended at
2239             the end of block to be shown. Option C<--no-newline> disables this
2240             behavior.
2241              
2242             =item B<--filestyle>=[C<line>,C<once>,C<separate>], B<--fs>
2243              
2244             Default style is I<line>, and B<greple> prints filename at the
2245             beginning of each line. Style I<once> prints the filename only once
2246             at the first time. Style I<separate> prints filename in the separate
2247             line before each line or block.
2248              
2249             =item B<--linestyle>=[C<line>,C<separate>], B<--ls>
2250              
2251             Default style is I<line>, and B<greple> prints line numbers at the
2252             beginning of each line. Style I<separate> prints line number in the
2253             separate line before each line or block.
2254              
2255             =item B<--blockstyle>=[C<line>,C<separate>], B<--bs>
2256              
2257             Default style is I<line>, and B<greple> prints block numbers at the
2258             beginning of each line. Style I<separate> prints block number in the
2259             separate line before each line or block.
2260              
2261             =item B<--separate>
2262              
2263             Shortcut for C<--filestyle=separate> C<--linestyle=separate>
2264             C<--blockstyle=separate>. This is convenient to use block mode search
2265             and visiting each location from supporting tool, such as Emacs.
2266              
2267             =item B<--format> B<LABEL>=I<format>
2268              
2269             Define the format string of line number (LINE), file name (FILE) and
2270             block number (BLOCK) to be displayed. Default is:
2271              
2272             --format LINE='%d:'
2273              
2274             --format FILE='%s:'
2275              
2276             --format BLOCK='%s:'
2277              
2278             Format string is passed to C<sprintf> function. Escape sequences
2279             C<\t>, C<\n>, C<\r>, and C<\f> are recognized.
2280              
2281             Next example will show line numbers in five digits with tab space:
2282              
2283             --format LINE='%05d\t'
2284              
2285             =item B<--frame-top>=I<string>
2286              
2287             =item B<--frame-middle>=I<string>
2288              
2289             =item B<--frame-bottom>=I<string>
2290              
2291             Print surrounding frames before and after each block. C<top> frame is
2292             printed at the beginning, C<bottom> frame at the end, C<middle> frame
2293             between blocks.
2294              
2295             =back
2296              
2297             B<Related options:>
2298             B<--block>/B<-p> (L</BLOCKS>),
2299             B<--color>/B<--colormap> (L</COLORS>)
2300              
2301              
2302             =head2 FILES
2303              
2304              
2305             =over 7
2306              
2307             =item B<--glob>=I<pattern>
2308              
2309             Get files matches to specified pattern and use them as a target files.
2310             Using C<--chdir> and C<--glob> makes easy to use B<greple> for fixed
2311             common job.
2312              
2313             =item B<--chdir>=I<directory>
2314              
2315             Change directory before processing files. When multiple directories
2316             are specified in C<--chdir> option, by using wildcard form or
2317             repeating option, C<--glob> file expansion will be done for every
2318             directories.
2319              
2320             greple --chdir '/usr/share/man/man?' --glob '*.[0-9]' ...
2321              
2322             =item B<--readlist>
2323              
2324             Get filenames from standard input. Read standard input and use each
2325             line as a filename for searching. You can feed the output from other
2326             command like L<find(1)> for B<greple> with this option. Next example
2327             searches string from files modified within 7 days:
2328              
2329             find . -mtime -7 -print | greple --readlist pattern
2330              
2331             Using B<find> module, this can be done like:
2332              
2333             greple -Mfind . -mtime -7 -- pattern
2334              
2335             =back
2336              
2337              
2338             =head2 COLORS
2339              
2340              
2341             =over 7
2342              
2343             =item B<--color>=[C<auto>,C<always>,C<never>], B<--nocolor>
2344              
2345             Use terminal color capability to emphasize the matched text. Default
2346             is C<auto>: effective when STDOUT is a terminal and option C<-o> is
2347             not given, not otherwise. Option value C<always> and C<never> will
2348             work as expected.
2349              
2350             Option B<--nocolor> is alias for B<--color>=I<never>.
2351              
2352             When color output is disabled, ANSI terminal sequence is not produced,
2353             but functional colormap, such as C<--cm sub{...}>, still works.
2354              
2355             =item B<--colormap>=I<spec>, B<--cm>=...
2356              
2357             Specify color map. Because this option is mostly implemented by
2358             L<Getopt::EX::Colormap> module, consult its document for detail and
2359             up-to-date specification.
2360              
2361             Color specification is combination of single uppercase character
2362             representing basic colors, and (usually brighter) alternative colors in
2363             lowercase:
2364              
2365             R r Red
2366             G g Green
2367             B b Blue
2368             C c Cyan
2369             M m Magenta
2370             Y y Yellow
2371             K k Black
2372             W w White
2373              
2374             or RGB value and 24 grey levels if using ANSI 256 color terminal:
2375              
2376             (255,255,255) : 24bit decimal RGB colors
2377             #000000 .. #FFFFFF : 24bit hex RGB colors
2378             #000 .. #FFF : 12bit hex RGB 4096 colors
2379             000 .. 555 : 6x6x6 RGB 216 colors
2380             L00 .. L25 : Black (L00), 24 grey levels, White (L25)
2381              
2382             =over 4
2383              
2384             Beginning # can be omitted in 24bit RGB notation.
2385              
2386             When values are all same in 24bit or 12bit RGB, it is converted to 24
2387             grey level, otherwise 6x6x6 216 color.
2388              
2389             =back
2390              
2391             or color names enclosed by angle bracket:
2392              
2393             <red> <blue> <green> <cyan> <magenta> <yellow>
2394             <aliceblue> <honeydue> <hotpink> <mooccasin>
2395             <medium_aqua_marine>
2396              
2397             with other special effects:
2398              
2399             N None
2400             Z 0 Zero (reset)
2401             D 1 Double strike (boldface)
2402             P 2 Pale (dark)
2403             I 3 Italic
2404             U 4 Underline
2405             F 5 Flash (blink: slow)
2406             Q 6 Quick (blink: rapid)
2407             S 7 Stand out (reverse video)
2408             H 8 Hide (concealed)
2409             X 9 Cross out
2410             E Erase Line
2411              
2412             ; No effect
2413             / Toggle foreground/background
2414             ^ Reset to foreground
2415             @ Reset index list
2416              
2417             If the spec includes C</>, left side is considered as foreground color
2418             and right side as background. If multiple colors are given in same
2419             spec, all indicators are produced in the order of their presence. As
2420             a result, the last one takes effect.
2421              
2422             Effect characters are case insensitive, and can be found anywhere and
2423             in any order in color spec string. Character C<;> does nothing and
2424             can be used just for readability, like C<SD;K/544>.
2425              
2426             If the special reset symbol C<@> is encountered, the index list is
2427             reset to empty at that point. The reset symbol must be used alone and
2428             may not be combined with other characters.
2429              
2430             Example:
2431              
2432             RGB 6x6x6 12bit 24bit color name
2433             === ======= ========= ============= ==================
2434             B 005 #00F (0,0,255) <blue>
2435             /M /505 /#F0F /(255,0,255) /<magenta>
2436             K/W 000/555 #000/#FFF 000000/FFFFFF <black>/<white>
2437             R/G 500/050 #F00/#0F0 FF0000/00FF00 <red>/<green>
2438             W/w L03/L20 #333/#ccc 303030/c6c6c6 <dimgrey>/<lightgrey>
2439              
2440             Multiple colors can be specified separating by white space or comma,
2441             or by repeating options. Those colors will be applied for each
2442             pattern keywords. Next command will show word C<foo> in red, C<bar>
2443             in green and C<baz> in blue.
2444              
2445             greple --colormap='R G B' 'foo bar baz'
2446              
2447             greple --cm R -e foo --cm G -e bar --cm B -e baz
2448              
2449             Coloring capability is implemented in L<Getopt::EX::Colormap> module.
2450              
2451             =item B<--colormap>=I<field>=I<spec>,...
2452              
2453             Another form of colormap option to specify the color for fields:
2454              
2455             FILE File name
2456             LINE Line number
2457             TEXT Unmatched normal text
2458             BLOCKEND Block end mark
2459             PROGRESS Progress status with -dnf option
2460              
2461             The C<BLOCKEND> mark is colored with C<E> effect provided by
2462             L<Getopt::EX> module, which allows to fill up the line with background
2463             color. This effect uses irregular escape
2464             sequence, and you may need to define C<LESSANSIENDCHARS> environment
2465             as "mK" to see the result with L<less> command.
2466              
2467             =item B<--colormap>=C<&func>
2468              
2469             =item B<--colormap>=C<sub{...}>
2470              
2471             You can also set the name of perl subroutine name or definition to be
2472             called handling matched words. Target word is passed as variable
2473             C<$_>, and the return value of the subroutine will be displayed.
2474              
2475             Next command convert all words in C comment to upper case.
2476              
2477             greple --all '/\*(?s:.*?)\*/' --cm 'sub{uc}'
2478              
2479             You can quote matched string instead of coloring (this emulates
2480             deprecated option C<--quote>):
2481              
2482             greple --cm 'sub{"<".$_.">"}' ...
2483              
2484             It is possible to use this definition with field names. Next example
2485             print line numbers in seven digits.
2486              
2487             greple -n --cm 'LINE=sub{s/(\d+)/sprintf("%07d",$1)/e;$_}'
2488              
2489             Experimentally, function can be combined with other normal color
2490             specifications. Also the form C<&func;> can be repeated.
2491              
2492             greple --cm 'BF/544;sub{uc}'
2493              
2494             greple --cm 'R;&func1;&func2;&func3'
2495              
2496             When color for 'TEXT' field is specified, whole text including matched
2497             part is passed to the function, exceptionally. It is not recommended
2498             to use user defined function for 'TEXT' field.
2499              
2500             =item B<--colorsub>=C<...>, B<--cs>=C<...>
2501              
2502             C<--colorsub> or C<--cs> is a shortcut for subroutine colormap. It
2503             simply enclose the argument by C<sub{ ... }> expression. So
2504              
2505             greple --cm 'sub{uc}'
2506              
2507             can be written as simple as this.
2508              
2509             greple --cs uc
2510              
2511             You can not use this option for labeled color.
2512              
2513             =item B<--[no]colorful>
2514              
2515             Shortcut for C<--colormap>='C<RD GD BD CD MD YD>' in ANSI 16 colors
2516             mode, and C<--colormap>='C<D/544 D/454 D/445 D/455 D/454 D/554>' and
2517             other combination of 3, 4, 5 for 256 colors mode. Enabled by default.
2518              
2519             When single pattern is specified, first color in colormap is used for
2520             the pattern. If multiple patterns and multiple colors are specified,
2521             each pattern is colored with corresponding color cyclically.
2522              
2523             Option C<--regioncolor> and C<--colorindex> change this behavior.
2524              
2525             =item B<--colorindex>=I<spec>, B<--ci>=I<spec>
2526              
2527             Specify the color index method by combination of spec characters.
2528             B<A> (ascend) and B<D> (descend) can be mixed with B<B> (block) and/or
2529             B<S> (shuffle) like C<--ci=ABS>. B<R> (random) can be too but it does
2530             not make sense. When B<S> is used alone, colormap is shuffled with
2531             normal behavior. B<U> (unique) assigns unique color for each
2532             identical string.
2533              
2534             =over 4
2535              
2536             =item A (Ascending)
2537              
2538             Apply different color sequentially according to the order of
2539             appearance.
2540              
2541             =item D (Descending)
2542              
2543             Apply different color sequentially according to the reverse order of
2544             appearance.
2545              
2546             =item B (Block)
2547              
2548             Reset sequential index on every block.
2549              
2550             =item S (Shuffle)
2551              
2552             Shuffle indexed color.
2553              
2554             =item R (Random)
2555              
2556             Use random color index every time.
2557              
2558             =item G (Group)
2559              
2560             Valid only with B<-G> option. Assigns a sequential index to each
2561             capture group across all patterns, starting from 0. For example, if
2562             the first pattern has 2 groups and the second has 3, indices are
2563             assigned as 0, 1, 2, 3, 4. Patterns without capture groups count as
2564             one group. Use C<--cm N> to skip color for specific indices.
2565              
2566             =item GP (Group per Pattern)
2567              
2568             Same as B<G>, but index resets to 0 for each pattern.
2569              
2570             =item U (Unique)
2571              
2572             Use different colors for different string matched.
2573              
2574             =item N (Normal)
2575              
2576             Reset to normal behavior. Because the last option overrides earlier
2577             ones, C<--ci=N> can be used to reset the behavior set by previous
2578             options.
2579              
2580             =back
2581              
2582             =item B<--random>
2583              
2584             Shortcut for C<--colorindex=R>.
2585              
2586             =item B<--uniqcolor>, B<--uc>
2587              
2588             Shortcut for C<--colorindex=U>.
2589              
2590             Use different colors for different string matched.
2591              
2592             Next example prints all words starting with C<color> and displays them
2593             all in different colors.
2594              
2595             greple --uniqcolor 'colou?r\w*'
2596              
2597             When used with option C<-i>, color is selected still in case-sensitive
2598             fashion. If you want case-insensitive color selection, use next
2599             C<--uniqsub> option.
2600              
2601             In B<greple> versions 9.20 and later, the C<--colorindex> option
2602             resets the effect of earlier C<--uniqcolor> option. If used in
2603             combination with other options, it is safer to specify like
2604             C<--ci=US>.
2605              
2606             =item B<--uniqsub>=I<function>, B<--us>=I<function>
2607              
2608             Above option C<--uniqcolor> set same color for same literal string.
2609             Option C<--uniqsub> specify the preprocessor code applied before
2610             comparison. I<function> get matched string by C<$_> and returns the
2611             result. For example, next command will choose unique colors for each
2612             word by their length.
2613              
2614             greple --uniqcolor --uniqsub 'sub{length}' '\w+' file
2615              
2616             If you want case-insensitive color selection, do like this.
2617              
2618             greple -i pattern --uc --uniqsub 'sub{lc}'
2619              
2620             Next command read the output from C<git blame> command and set unique
2621             color for each entire line by their commit ids.
2622              
2623             git blame ... | greple .+ --uc --us='sub{s/\s.*//r}' --face=E-D
2624              
2625             =item B<--ansicolor>=[C<16>,C<256>,C<24bit>]
2626              
2627             If set as C<16>, use ANSI 16 colors as a default color set, otherwise
2628             ANSI 256 colors. When set as C<24bit>, 6 hex digits notation produces
2629             24bit color sequence. Default is C<256>.
2630              
2631             =item B<--[no]256>
2632              
2633             Shortcut for C<--ansicolor>=C<256> or C<16>.
2634              
2635             =item B<--[no]regioncolor>, B<--[no]rc>
2636              
2637             Use different colors for each C<--inside> and C<--outside> region.
2638              
2639             Disabled by default, but automatically enabled when only single search
2640             pattern is specified. Use C<--no-regioncolor> to cancel automatic
2641             action.
2642              
2643             =item B<--face>=[+-=]I<effect>
2644              
2645             Append, remove or set specified I<effect> for all indexed color specs.
2646             Use C<+> (optional) to append, C<-> to remove, and C<=> to set.
2647             Effect is a single character expressing C<S> (Stand-out), C<U>
2648             (Underline), C<D> (Double-struck), C<F> (Flash) and such.
2649              
2650             Next example removes D (double-struck) effect.
2651              
2652             greple --face -D
2653              
2654             Multiple effects can be added/removed at once.
2655              
2656             greple --face SF-D
2657              
2658             Next example clears all existing color specs.
2659              
2660             greple --face =
2661              
2662             =back
2663              
2664             B<Related options:>
2665             B<-o> (L</STYLES>),
2666             B<--inside>/B<--outside>/B<--include>/B<--exclude> (L</REGIONS>)
2667              
2668              
2669             =head2 BLOCKS
2670              
2671              
2672             =over 7
2673              
2674             =item B<-p>, B<--paragraph>
2675              
2676             Print a paragraph which contains the pattern. Each paragraph is
2677             delimited by two or more successive newlines by default. Be aware
2678             that an empty line is not a paragraph delimiter if it contains
2679             space characters. Example:
2680              
2681             greple -np -C4 -e 'find myself' script/greple
2682              
2683             It changes the unit of context specified by C<-A>, C<-B>, C<-C>
2684             options. Space gap between paragraphs are also treated as a block
2685             unit. Thus, option C<-pC2> will print target paragraph along with
2686             previous and next paragraph. Option C<-pC1> causes consecutive
2687             paragraphs to be output as the same block in an easy-to-read format.
2688              
2689             You can create original paragraph pattern by C<--border> option.
2690              
2691             =item B<--border>=I<pattern>
2692              
2693             Specify record block border pattern. Pattern match is done in the
2694             context of multiple line mode.
2695              
2696             Default block is a single line and use C</^/m> as a pattern.
2697             Paragraph mode uses C</(?:\A|\R)\K\R+/>, which means continuous
2698             newlines at the beginning of text or following another newline (C<\R>
2699             means more general linebreaks including C<\r\n>; consult
2700             L<perlrebackslash> for detail).
2701              
2702             Next command treat the data as a series of 10-line unit.
2703              
2704             greple -n --border='(.*\n){1,10}'
2705              
2706             Contrary to the next C<--block> option, C<--border> never produce
2707             disjoint records.
2708              
2709             If you want to treat entire file as a single block, setting border to
2710             start or end of whole data is efficient way. Next commands works
2711             same.
2712              
2713             greple --border '\A' # beginning of file
2714             greple --border '\z' # end of file
2715              
2716             =item B<--block>=I<pattern>
2717              
2718             =item B<--block>=I<&sub>
2719              
2720             Specify the record block to display. Default block is a single line.
2721              
2722             Empty blocks are ignored. When blocks are not continuous, the match
2723             occurred outside blocks are ignored.
2724              
2725             If multiple block options are given, overlapping blocks are merged
2726             into a single block.
2727              
2728             Please be aware that this option is sometimes quite time consuming,
2729             because it finds all blocks before processing.
2730              
2731             =item B<--blockend>=I<string>
2732              
2733             Change the end mark displayed after C<-pABC> or C<--block> options.
2734             Default value is "--". Escape sequences C<\t>, C<\n>, C<\r>, and
2735             C<\f> are recognized.
2736              
2737             =item B<--join-blocks>
2738              
2739             Join consecutive blocks together. Logical operation is done for each
2740             individual blocks, but if the results are back-to-back connected, make
2741             them single block for final output.
2742              
2743             =back
2744              
2745             B<Related options:>
2746             B<-b>/B<--block-number> (L</STYLES>),
2747             B<-A>/B<-B>/B<-C> (L</STYLES>),
2748             B<--inside>/B<--outside>/B<--include>/B<--exclude> (L</REGIONS>)
2749              
2750              
2751             =head2 REGIONS
2752              
2753              
2754             =over 7
2755              
2756             =item B<--inside>=I<pattern>
2757              
2758             =item B<--outside>=I<pattern>
2759              
2760             Option C<--inside> and C<--outside> limit the text area to be matched.
2761             For simple example, if you want to find string C<and> not in the word
2762             C<command>, it can be done like this.
2763              
2764             greple --outside=command and
2765              
2766             The block can be larger and expand to multiple lines. Next command
2767             searches from C source, excluding comment part.
2768              
2769             greple --outside '(?s)/\*.*?\*/'
2770              
2771             Next command searches only from POD part of the perl script.
2772              
2773             greple --inside='(?s)^=.*?(^=cut|\Z)'
2774              
2775             When multiple B<inside> and B<outside> regions are specified, those
2776             regions are mixed up in union way.
2777              
2778             In multiple color environment, and if single keyword is specified,
2779             matches in each C<--inside>/C<--outside> region is printed in different
2780             color. Forcing this operation with multiple keywords, use
2781             C<--regioncolor> option.
2782              
2783             =item B<--inside>=I<&function>
2784              
2785             =item B<--outside>=I<&function>
2786              
2787             If the pattern name begins by ampersand (&) character, it is treated
2788             as a name of subroutine which returns a list of blocks. Using this
2789             option, user can use arbitrary function to determine from what part of
2790             the text they want to search. User defined function can be defined in
2791             F<.greplerc> file or by module option.
2792              
2793             =item B<--include>=I<pattern>
2794              
2795             =item B<--exclude>=I<pattern>
2796              
2797             =item B<--include>=I<&function>
2798              
2799             =item B<--exclude>=I<&function>
2800              
2801             C<--include>/C<--exclude> option behave exactly same as
2802             C<--inside>/C<--outside> when used alone.
2803              
2804             When used in combination, C<--include>/C<--exclude> are mixed in AND
2805             manner, while C<--inside>/C<--outside> are in OR.
2806              
2807             Thus, in the next example, first line prints all matches, and second
2808             does none.
2809              
2810             greple --inside PATTERN --outside PATTERN
2811              
2812             greple --include PATTERN --exclude PATTERN
2813              
2814             You can make up desired matches using C<--inside>/C<--outside> option,
2815             then remove unnecessary part by C<--include>/C<--exclude>
2816              
2817             =item B<--strict>
2818              
2819             Limit the match area strictly.
2820              
2821             By default, C<--block>, C<--inside>/C<outside>,
2822             C<--include>/C<--exclude> option allows partial match within the
2823             specified area. For instance,
2824              
2825             greple --inside and command
2826              
2827             matches pattern C<command> because the part of matched string is
2828             included in specified inside-area. Partial match fails when option
2829             C<--strict> provided, and longer string never matches within shorter
2830             area.
2831              
2832             Interestingly enough, above example
2833              
2834             greple --include PATTERN --exclude PATTERN
2835              
2836             produces output, as a matter of fact. Think of the situation
2837             searching, say, C<' PATTERN '> with this condition. Matched area
2838             includes surrounding spaces, and satisfies both conditions partially.
2839             This match does not occur when option C<--strict> is given, either.
2840              
2841             =back
2842              
2843             B<Related options:>
2844             B<--block> (L</BLOCKS>),
2845             B<--regioncolor> (L</COLORS>),
2846             B<-e>/B<-v> (L</PATTERNS>)
2847              
2848              
2849             =head2 CHARACTER CODE
2850              
2851              
2852             =over 7
2853              
2854             =item B<--icode>=I<code>
2855              
2856             Target file is assumed to be encoded in utf8 by default. Use this
2857             option to set specific encoding. When handling Japanese text, you may
2858             choose from 7bit-jis (jis), euc-jp or shiftjis (sjis). Multiple code
2859             can be supplied using multiple option or combined code names with
2860             space or comma, then file encoding is guessed from those code sets.
2861             Use encoding name C<guess> for automatic recognition from default code
2862             list which is euc-jp and 7bit-jis. Following commands are all
2863             equivalent.
2864              
2865             greple --icode=guess ...
2866             greple --icode=euc-jp,7bit-jis ...
2867             greple --icode=euc-jp --icode=7bit-jis ...
2868              
2869             Default code set are always included suspect code list. If you have
2870             just one code adding to suspect list, put + mark before the code name.
2871             Next example does automatic code detection from euc-kr, ascii, utf8
2872             and UTF-16/32.
2873              
2874             greple --icode=+euc-kr ...
2875              
2876             If the string "B<binary>" is given as encoding name, no character
2877             encoding is expected and all files are processed as binary data.
2878              
2879             =item B<--ocode>=I<code>
2880              
2881             Specify output code. Default is utf8.
2882              
2883             =back
2884              
2885              
2886             =head2 FILTER
2887              
2888              
2889             =over 7
2890              
2891             =item B<--if>=I<filter>, B<--if>=I<EXP>:I<filter>
2892              
2893             You can specify filter command which is applied to each file before
2894             search. If only one filter command is specified, it is applied to all
2895             files. If filter information include colon, first field will be perl
2896             expression to check the filename saved in variable $_. If it
2897             successes, next filter command is pushed.
2898              
2899             greple --if=rev perg
2900             greple --if='/\.tar$/:tar tvf -'
2901              
2902             If the command doesn't accept standard input as processing data, you
2903             may be able to use special device:
2904              
2905             greple --if='nm /dev/stdin' crypt /usr/lib/lib*
2906              
2907             Filters for compressed and gzipped file is set by default unless
2908             C<--noif> option is given. Default action is like this:
2909              
2910             greple --if='s/\.Z$//:zcat' --if='s/\.g?z$//:gunzip -c'
2911              
2912             File with C<.gpg> suffix is filtered by B<gpg> command. In that case,
2913             pass-phrase is asked for each file. If you want to input pass-phrase
2914             only once to find from multiple files, use C<-Mpgp> module.
2915              
2916             If the filter starts with C<&>, perl subroutine is called instead of
2917             external command. You can define the subroutine in F<.greplerc> or
2918             modules. B<Greple> simply call the subroutine, so it should be
2919             responsible for process control. It may have to use C<POSIX::_exit()>
2920             to avoid executing an C<END> block on exit or calling destructor on
2921             the object.
2922              
2923             =item B<--noif>
2924              
2925             Disable default input filter. Which means compressed files will not
2926             be decompressed automatically.
2927              
2928             =item B<--of>=I<filter>
2929              
2930             =item B<--of>=I<&func>
2931              
2932             Specify output filter which process the output of B<greple> command.
2933             Filter command can be specified in multiple times, and they are
2934             invoked for each file to be processed. So next command reset the line
2935             number for each file.
2936              
2937             greple --of 'cat -n' string file1 file2 ...
2938              
2939             If the filter starts with C<&>, perl subroutine is called instead of
2940             external command. You can define the subroutine in F<.greplerc> or
2941             modules.
2942              
2943             Output filter command is executed only when matched string exists to
2944             avoid invoking many unnecessary processes. No effect for option
2945             C<-l> and C<-c>.
2946              
2947             =item B<--pf>=I<filter>
2948              
2949             =item B<--pf>=I<&func>
2950              
2951             Similar to C<--of> filter but invoked just once and takes care of
2952             entire output from B<greple> command.
2953              
2954             =back
2955              
2956              
2957             =head2 RUNTIME FUNCTIONS
2958              
2959              
2960             =over 7
2961              
2962             =item B<--begin>=I<function>(I<...>)
2963              
2964             =item B<--begin>=I<function>=I<...>
2965              
2966             Option C<--begin> specify the function executed at the beginning of
2967             each file processing. This I<function> have to be called from B<main>
2968             package. So if you define the function in the module package, use the
2969             full package name or export properly.
2970              
2971             If the function dies with a message starting with a word "SKIP"
2972             (C</^SKIP/i>), that file is simply skipped. So you can control if the
2973             file is to be processed using the file name or content. To see the
2974             message, use C<--warn begin=1> option.
2975              
2976             For example, using next function, only perl related files will be
2977             processed.
2978              
2979             sub is_perl {
2980             my %arg = @_;
2981             my $name = delete $arg{&FILELABEL} or die;
2982             $name =~ /\.(?:pm|pl|PL|pod)$/ or /\A#!.*\bperl/
2983             or die "skip $name\n";
2984             }
2985              
2986             1;
2987              
2988             __DATA__
2989              
2990             option default --filestyle=once --format FILE='\n%s:\n'
2991              
2992             autoload -Mdig --dig
2993             option --perl $<move> --begin &__PACKAGE__::is_perl --dig .
2994              
2995             =item B<--end>=I<function>(I<...>)
2996              
2997             =item B<--end>=I<function>=I<...>
2998              
2999             Option C<--end> is almost same as C<--begin>, except that the function
3000             is called after the file processing.
3001              
3002             =item B<--prologue>=I<function>(I<...>)
3003              
3004             =item B<--prologue>=I<function>=I<...>
3005              
3006             =item B<--epilogue>=I<function>(I<...>)
3007              
3008             =item B<--epilogue>=I<function>=I<...>
3009              
3010             Option C<--prologue> and C<--epilogue> specify functions called before
3011             and after processing. During the execution, file is not opened and
3012             therefore, file name is not given to those functions.
3013              
3014             =item B<--postgrep>=I<function>(I<...>)
3015              
3016             =item B<--postgrep>=I<function>=I<...>
3017              
3018             Specify the function called after each search operation. Function is
3019             called with L<App::Greple::Grep> object which contains all information
3020             about the search.
3021              
3022             The search results are held as a list of L<App::Greple::Grep::Result>
3023             objects. Each result contains a block and matched regions. By
3024             emptying the contents of a result element, the matches for that block
3025             can be canceled.
3026              
3027             See L<App::Greple::Grep> for details about the object structure and
3028             callback mechanism.
3029              
3030             =item B<--callback>=I<function>(I<...>)
3031              
3032             Callback function is called before printing every matched pattern with
3033             four labeled parameters: B<start>, B<end>, B<index> and B<match>,
3034             which corresponds to start and end position in the text, pattern
3035             index, and the matched string. Matched string in the text is replaced
3036             by returned string from the function. If the function returns
3037             C<undef>, the result is not changed.
3038              
3039             Multiple functions can be specified, and if there are multiple search
3040             patterns, they are applied in order and cyclically.
3041              
3042             =item B<-M>I<module>::I<function(...)>
3043              
3044             =item B<-M>I<module>::I<function=...>
3045              
3046             Function can be given with module option, following module name. In
3047             this form, the function will be called with module package name. So
3048             you don't have to export it. Because it is called only once at the
3049             beginning of command execution, before starting file processing,
3050             C<FILELABEL> parameter is not given exceptionally.
3051              
3052             =item B<--print>=I<function>
3053              
3054             =item B<--print>=I<sub{...}>
3055              
3056             Specify user defined function executed before data print. Text to be
3057             printed is replaced by the result of the function. Arbitrary function
3058             can be defined in F<.greplerc> file or module. Matched data is placed
3059             in variable C<$_>. Filename is passed by C<&FILELABEL> key, as
3060             described later.
3061              
3062             It is possible to use multiple C<--print> options. In that case,
3063             second function will get the result of the first function. The
3064             command will print the final result of the last function.
3065              
3066             This option and next B<--continue> are no more recommended to use
3067             because B<--colormap> and B<--callback> functions are more simple and
3068             powerful.
3069              
3070             =item B<--continue>
3071              
3072             When C<--print> option is given, B<greple> will immediately print the
3073             result returned from print function and finish the cycle. Option
3074             C<--continue> forces to continue normal printing process after print
3075             function called. So please be sure that all data being consistent.
3076              
3077             =back
3078              
3079             For these run-time functions, optional argument list can be set in the
3080             form of C<key> or C<key=value>, connected by comma. These arguments
3081             will be passed to the function in key => value list. Sole key will
3082             have the value one. Also processing file name is passed with the key
3083             of C<FILELABEL> constant. As a result, the option in the next form:
3084              
3085             --begin function(key1,key2=val2)
3086             --begin function=key1,key2=val2
3087              
3088             will be transformed into following function call:
3089              
3090             function(&FILELABEL => "filename", key1 => 1, key2 => "val2")
3091              
3092             As described earlier, C<FILELABEL> parameter is not given to the
3093             function specified with module option. So
3094              
3095             -Mmodule::function(key1,key2=val2)
3096             -Mmodule::function=key1,key2=val2
3097              
3098             simply becomes:
3099              
3100             function(key1 => 1, key2 => "val2")
3101              
3102             The function can be defined in F<.greplerc> or modules. Assign the
3103             arguments into hash, then you can access argument list as member of
3104             the hash. It's safe to delete FILELABEL key if you expect random
3105             parameter is given. Content of the target file can be accessed by
3106             C<$_>. Ampersand (C<&>) is required to avoid the hash key is
3107             interpreted as a bare word.
3108              
3109             sub function {
3110             my %arg = @_;
3111             my $filename = delete $arg{&FILELABEL};
3112             $arg{key1}; # 1
3113             $arg{key2}; # "val2"
3114             $_; # contents
3115             }
3116              
3117              
3118             =head2 OTHERS
3119              
3120              
3121             =over 7
3122              
3123             =item B<--usage>[=I<expand>]
3124              
3125             B<Greple> print usage and exit with option C<--usage>, or no valid
3126             parameter is not specified. In this case, module option is displayed
3127             with help information if available. If you want to see how they are
3128             expanded, supply something not empty to C<--usage> option, like:
3129              
3130             greple -Mmodule --usage=expand
3131              
3132             =item B<--version>
3133              
3134             Show version.
3135              
3136             =item B<--exit>=I<number>
3137              
3138             When B<greple> executed normally, it exit with status 0 or 1 depending
3139             on something matched or not. Sometimes we want to get status 0 even
3140             if nothing matched. This option set the status code for normal
3141             execution. It still exits with non-zero status when error occurred.
3142              
3143             =item B<--man>, B<--doc>
3144              
3145             Show manual page.
3146             Display module's manual page when used with C<-M> option.
3147              
3148             =item B<--show>, B<--less>
3149              
3150             Show module file contents. Use with C<-M> option.
3151              
3152             =item B<--path>
3153              
3154             Show module file path. Use with C<-M> option.
3155              
3156             =item B<--norc>
3157              
3158             Do not read startup file: F<~/.greplerc>. This option has to be
3159             placed before any other options including C<-M> module options.
3160             Setting C<GREPLE_NORC> environment has the same effect.
3161              
3162             =begin comment
3163              
3164             =item B<-d> I<flags>
3165              
3166             Display information. Various kinds of debug, diagnostic, monitor
3167             information can be displayed by giving appropriate flag to -d option.
3168              
3169             c: color information
3170             d: directory information
3171             e: eval string
3172             f: processing file name
3173             m: misc debug information
3174             n: number of processing files
3175             o: option related information
3176             p: run `ps' command before termination (on Unix)
3177             s: statistic information
3178             u: unused options
3179             v: internal match information
3180              
3181             =end comment
3182              
3183             =item B<--error>=I<action>
3184              
3185             As B<greple> tries to read data as a character string, sometimes fails
3186             to convert them into internal representation, and the file is skipped
3187             without processing by default. This works fine to skip binary
3188             data. (B<skip>)
3189              
3190             Also sometimes encounters code mapping error due to character
3191             encoding. In this case, reading the file as a binary data helps to
3192             produce meaningful output. (B<retry>)
3193              
3194             This option specifies the action when data read error occurred.
3195              
3196             =over 4
3197              
3198             =item B<skip>
3199              
3200             Skip the file. Default.
3201              
3202             =item B<retry>
3203              
3204             Retry reading the file as a binary data.
3205              
3206             =item B<fatal>
3207              
3208             Abort the operation.
3209              
3210             =item B<ignore>
3211              
3212             Ignore error and continue to read anyway.
3213              
3214             =back
3215              
3216             You may occasionally want to find text in binary data. Next command
3217             will work like L<strings(1)> command.
3218              
3219             greple -o --re '(?a)\w{4,}' --error=retry --uc /bin/*
3220              
3221             If you want read all files as binary data, use C<--icode=binary>
3222             instead.
3223              
3224             =item B<-w>, B<--warn> I<type>=[C<0>,C<1>]
3225              
3226             Control runtime message mainly about file operation related to
3227             C<--error> option. Repeatable. Value is optional and 1 is assumed
3228             when omitted. So C<-wall> option is the same as C<-wall=1> and enables
3229             all messages, and C<-wall=0> disables all.
3230              
3231             Types are:
3232              
3233             =over 4
3234              
3235             =item B<read>
3236              
3237             (Default 0) Errors occurred during file read. Mainly unicode related
3238             errors when reading binary or ambiguous text file.
3239              
3240             =item B<skip>
3241              
3242             (Default 1) File skip message.
3243              
3244             =item B<retry>
3245              
3246             (Default 0) File retry message.
3247              
3248             =item B<begin>
3249              
3250             (Default 0) When C<--begin> function died with C</^SKIP/i> message,
3251             the file is skipped without any notice. Enables this to see the dying
3252             message.
3253              
3254             =item B<all>
3255              
3256             Set same value for all types.
3257              
3258             =back
3259              
3260             =item B<--alert> [ C<size>=#, C<time>=# ]
3261              
3262             Set alert parameter for large file. B<Greple> scans whole file
3263             content to know line borders, and it takes several seconds or more if
3264             it contains large number of lines.
3265              
3266             By default, if the target file contains more than B<512 * 1024
3267             characters> (I<size>), B<2 seconds> timer will start (I<time>). Alert
3268             message is shown when the timer expired.
3269              
3270             To disable this alert, set the size as zero:
3271              
3272             --alert size=0
3273              
3274             =item B<-Mdebug>, B<-d>I<x>
3275              
3276             Debug option is described in L<App::Greple::debug> module.
3277              
3278             =back
3279              
3280              
3281             =head1 ENVIRONMENT and STARTUP FILE
3282              
3283              
3284             =over 7
3285              
3286             =item B<GREPLEOPTS>
3287              
3288             Environment variable GREPLEOPTS is used as a default options. They
3289             are inserted before command line options.
3290              
3291             =item B<GREPLE_NORC>
3292              
3293             If set non-empty string, startup file F<~/.greplerc> is not processed.
3294              
3295             =item B<DEBUG_GETOPT>
3296              
3297             Enable L<Getopt::Long> debug option.
3298              
3299             =item B<DEBUG_GETOPTEX>
3300              
3301             Enable L<Getopt::EX> debug option.
3302              
3303             =item B<NO_COLOR>
3304              
3305             If true, all coloring capability with ANSI terminal sequence is
3306             disabled. See L<https://no-color.org/>.
3307              
3308             =back
3309              
3310             Before starting execution, B<greple> reads the file named F<.greplerc>
3311             on user's home directory. Following directives can be used.
3312              
3313             =over 7
3314              
3315             =item B<option> I<name> string
3316              
3317             Argument I<name> of B<option> directive is user defined option name.
3318             The rest are processed by C<shellwords> routine defined in
3319             Text::ParseWords module. Be sure that this module sometimes requires
3320             escape backslashes.
3321              
3322             Any kind of string can be used for option name but it is not combined
3323             with other options.
3324              
3325             option --fromcode --outside='(?s)\/\*.*?\*\/'
3326             option --fromcomment --inside='(?s)\/\*.*?\*\/'
3327              
3328             If the option named B<default> is defined, it will be used as a
3329             default option.
3330              
3331             For the purpose to include following arguments within replaced
3332             strings, two special notations can be used in option definition.
3333             String C<$E<lt>nE<gt>> is replaced by the I<n>th argument after the
3334             substituted option, where I<n> is number start from one. String
3335             C<$E<lt>shiftE<gt>> is replaced by following command line argument and
3336             the argument is removed from option list.
3337              
3338             For example, when
3339              
3340             option --line --le &line=$<shift>
3341              
3342             is defined, command
3343              
3344             greple --line 10,20-30,40
3345              
3346             will be evaluated as this:
3347              
3348             greple --le &line=10,20-30,40
3349              
3350             =item B<expand> I<name> I<string>
3351              
3352             Define local option I<name>. Command B<expand> is almost same as
3353             command B<option> in terms of its function. However, option defined
3354             by this command is expanded in, and only in, the process of
3355             definition, while option definition is expanded when command arguments
3356             are processed.
3357              
3358             This is similar to string macro defined by following B<define>
3359             command. But macro expansion is done by simple string replacement, so
3360             you have to use B<expand> to define option composed by multiple
3361             arguments.
3362              
3363             =item B<define> I<name> string
3364              
3365             Define macro. This is similar to B<option>, but argument is not
3366             processed by I<shellwords> and treated just a simple text, so
3367             meta-characters can be included without escape. Macro expansion is
3368             done for option definition and other macro definition. Macro is not
3369             evaluated in command line option. Use option directive if you want to
3370             use in command line,
3371              
3372             define (#kana) \p{InKatakana}
3373             option --kanalist --nocolor -o --join --re '(#kana)+(\n(#kana)+)*'
3374             help --kanalist List up Katakana string
3375              
3376             =item B<help> I<name>
3377              
3378             If B<help> directive is used for same option name, it will be printed
3379             in usage message. If the help message is C<ignore>, corresponding
3380             line won't show up in the usage.
3381              
3382             =item B<builtin> I<spec> I<variable>
3383              
3384             Define built-in option which should be processed by option parser.
3385             Arguments are assumed to be L<Getopt::Long> style spec, and
3386             I<variable> is string start with C<$>, C<@> or C<%>. They will be
3387             replaced by a reference to the object which the string represent.
3388              
3389             See B<pgp> module for example.
3390              
3391             =item B<autoload> I<module> I<options> ...
3392              
3393             Define module which should be loaded automatically when specified
3394             option is found in the command arguments.
3395              
3396             For example,
3397              
3398             autoload -Mdig --dig --git
3399              
3400             replaces option "C<--dig>" to "C<-Mdig --dig>", so that B<dig> module
3401             is loaded before processing C<--dig> option.
3402              
3403             =back
3404              
3405             Environment variable substitution is done for string specified by
3406             C<option> and C<define> directives. Use Perl syntax B<$ENV{NAME}> for
3407             this purpose. You can use this to make a portable module.
3408              
3409             When B<greple> found C<__PERL__> line in F<.greplerc> file, the rest
3410             of the file is evaluated as a Perl program. You can define your own
3411             subroutines which can be used by C<--inside>/C<--outside>,
3412             C<--include>/C<--exclude>, C<--block> options.
3413              
3414             For those subroutines, file content will be provided by global
3415             variable C<$_>. Expected response from the subroutine is the list of
3416             array references, which is made up by start and end offset pairs.
3417              
3418             For example, suppose that the following function is defined in your
3419             F<.greplerc> file. Start and end offset for each pattern match can be
3420             taken as array element C<$-[0]> and C<$+[0]>.
3421              
3422             __PERL__
3423             sub odd_line {
3424             my @list;
3425             my $i;
3426             while (/.*\n/g) {
3427             push(@list, [ $-[0], $+[0] ]) if ++$i % 2;
3428             }
3429             @list;
3430             }
3431              
3432             You can use next command to search pattern included in odd number
3433             lines.
3434              
3435             % greple --inside '&odd_line' pattern files...
3436              
3437              
3438             =head1 MODULE
3439              
3440             You can expand the B<greple> command using module. Module files are
3441             placed at F<App/Greple/> directory in Perl library, and therefor has
3442             B<App::Greple::module> package name.
3443              
3444             In the command line, module have to be specified preceding any other
3445             options in the form of B<-M>I<module>. However, it also can be
3446             specified at the beginning of option expansion.
3447              
3448             If the package name is declared properly, C<__DATA__> section in the
3449             module file will be interpreted same as F<.greplerc> file content. So
3450             you can declare the module specific options there. Functions declared
3451             in the module can be used from those options, it makes highly
3452             expandable option/programming interaction possible.
3453              
3454             Using C<-M> without module argument will print available module list.
3455             Option C<--man> will display module document when used with C<-M>
3456             option. Use C<--show> option to see the module itself. Option
3457             C<--path> will print the path of module file.
3458              
3459             See this sample module code. This sample defines options to search
3460             from pod, comment and other segment in Perl script. Those capability
3461             can be implemented both in function and macro.
3462              
3463             package App::Greple::perl;
3464              
3465             use Exporter 'import';
3466             our @EXPORT = qw(pod comment podcomment);
3467             our %EXPORT_TAGS = ( );
3468             our @EXPORT_OK = qw();
3469            
3470             use App::Greple::Common;
3471             use App::Greple::Regions;
3472            
3473             my $pod_re = qr{^=\w+(?s:.*?)(?:\Z|^=cut\s*\n)}m;
3474             my $comment_re = qr{^(?:\h*#.*\n)+}m;
3475            
3476             sub pod {
3477             match_regions(pattern => $pod_re);
3478             }
3479             sub comment {
3480             match_regions(pattern => $comment_re);
3481             }
3482             sub podcomment {
3483             match_regions(pattern => qr/$pod_re|$comment_re/);
3484             }
3485            
3486             1;
3487            
3488             __DATA__
3489            
3490             define :comment: ^(\s*#.*\n)+
3491             define :pod: ^=(?s:.*?)(?:\Z|^=cut\s*\n)
3492            
3493             #option --pod --inside :pod:
3494             #option --comment --inside :comment:
3495             #option --code --outside :pod:|:comment:
3496            
3497             option --pod --inside '&pod'
3498             option --comment --inside '&comment'
3499             option --code --outside '&podcomment'
3500              
3501             You can use the module like this:
3502              
3503             greple -Mperl --pod default greple
3504              
3505             greple -Mperl --colorful --code --comment --pod default greple
3506              
3507             If special subroutine C<initialize()> and C<finalize()> are defined in
3508             the module, they are called at the beginning with
3509             L<Getopt::EX::Module> object as a first argument. Second argument is
3510             the reference to C<@ARGV>, and you can modify actual C<@ARGV> using
3511             it. See L<App::Greple::find> module as an example.
3512              
3513             Calling sequence is like this. See L<Getopt::EX::Module> for detail.
3514              
3515             1) Call initialize()
3516             2) Call function given in -Mmod::func() style
3517             3) Call finalize()
3518              
3519             =head1 HISTORY
3520              
3521             Most capability of B<greple> is derived from B<mg> command, which has
3522             been developing from early 1990's by the same author. Because modern
3523             standard B<grep> family command becomes to have similar capabilities,
3524             it is a time to clean up entire functionalities, totally remodel the
3525             option interfaces, and change the command name. (2013.11)
3526              
3527              
3528             =head1 SEE ALSO
3529              
3530             L<grep(1)>, L<perl(1)>
3531              
3532             L<App::Greple>, L<App::Greple::Grep>
3533              
3534             L<https://github.com/kaz-utashiro/greple>
3535              
3536             L<Getopt::EX>, L<https://github.com/kaz-utashiro/Getopt-EX>
3537              
3538              
3539             =head1 AUTHOR
3540              
3541             Kazumasa Utashiro
3542              
3543              
3544             =head1 LICENSE
3545              
3546             Copyright 1991-2026 Kazumasa Utashiro
3547              
3548             This library is free software; you can redistribute it and/or modify
3549             it under the same terms as Perl itself.
3550              
3551             =cut
3552              
3553             # LocalWords: greple egrep foo baz yabba dabba doo ascii greplerc
3554             # LocalWords: regex readlist iname jpg jpeg gif png tbz tgz pdf RGB
3555             # LocalWords: perlre fgrep grep perl joinby KATAKANA InKatakana utf
3556             # LocalWords: nonewline filestyle linestyle chdir mtime nocolor jis
3557             # LocalWords: STDOUT colormap Cyan BLOCKEND LESSANSIENDCHARS setuid
3558             # LocalWords: sprintf regioncolor uniqcolor ansicolor nocolorful jp
3559             # LocalWords: struct sockaddr blockend icode euc shiftjis sjis zcat
3560             # LocalWords: ocode gunzip gpg FILELABEL substr eval misc unicode
3561             # LocalWords: GREPLEOPTS shellwords Katakana builtin pgp autoload
3562             # LocalWords: ENV App ARGV mg Kazumasa Utashiro github colorindex
3563             # LocalWords: matchcount gzipped stdin func CPANMINUS cpanm kana