-
Notifications
You must be signed in to change notification settings - Fork 19
/
ack2
executable file
·5400 lines (3999 loc) · 139 KB
/
ack2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl
#
# This file, ack, is generated code.
# Please DO NOT EDIT or send patches for it.
#
# Please take a look at the source from
# https://github.com/petdance/ack2
# and submit patches against the individual files
# that build ack.
#
package main;
use strict;
use warnings;
our $VERSION = '2.15_01'; # Check http://beyondgrep.com/ for updates
use 5.008008;
use Getopt::Long 2.38 ();
use Carp 1.04 ();
use File::Spec ();
# XXX Don't make this so brute force
# See also: https://github.com/petdance/ack2/issues/89
our $opt_after_context;
our $opt_before_context;
our $opt_output;
our $opt_print0;
our $opt_color;
our $opt_heading;
our $opt_show_filename;
our $opt_regex;
our $opt_break;
our $opt_count;
our $opt_v;
our $opt_m;
our $opt_g;
our $opt_f;
our $opt_lines;
our $opt_L;
our $opt_l;
our $opt_passthru;
our $opt_column;
# flag if we need any context tracking
our $is_tracking_context;
# These are all our globals.
MAIN: {
$App::Ack::orig_program_name = $0;
$0 = join(' ', 'ack', $0);
if ( $App::Ack::VERSION ne $main::VERSION ) {
App::Ack::die( "Program/library version mismatch\n\t$0 is $main::VERSION\n\t$INC{'App/Ack.pm'} is $App::Ack::VERSION" );
}
# Do preliminary arg checking;
my $env_is_usable = 1;
for my $arg ( @ARGV ) {
last if ( $arg eq '--' );
# Get the --thpppt, --bar, --cathy checking out of the way.
$arg =~ /^--th[pt]+t+$/ and App::Ack::_thpppt($arg);
$arg eq '--bar' and App::Ack::_bar();
$arg eq '--cathy' and App::Ack::_cathy();
# See if we want to ignore the environment. (Don't tell Al Gore.)
$arg eq '--env' and $env_is_usable = 1;
$arg eq '--noenv' and $env_is_usable = 0;
}
if ( !$env_is_usable ) {
my @keys = ( 'ACKRC', grep { /^ACK_/ } keys %ENV );
delete @ENV{@keys};
}
load_colors();
Getopt::Long::Configure('default', 'no_auto_help', 'no_auto_version');
Getopt::Long::Configure('pass_through', 'no_auto_abbrev');
Getopt::Long::GetOptions(
'help' => sub { App::Ack::show_help(); exit; },
'version' => sub { App::Ack::print_version_statement(); exit; },
'man' => sub { App::Ack::show_man(); exit; },
);
Getopt::Long::Configure('default', 'no_auto_help', 'no_auto_version');
if ( !@ARGV ) {
App::Ack::show_help();
exit 1;
}
main();
}
sub _compile_descend_filter {
my ( $opt ) = @_;
my $idirs = 0;
my $dont_ignore_dirs = 0;
for my $filter (@{$opt->{idirs} || []}) {
if ($filter->is_inverted()) {
$dont_ignore_dirs++;
}
else {
$idirs++;
}
}
# if we have one or more --noignore-dir directives, we can't ignore
# entire subdirectory hierarchies, so we return an "accept all"
# filter and scrutinize the files more in _compile_file_filter
return if $dont_ignore_dirs;
return unless $idirs;
$idirs = $opt->{idirs};
return sub {
my $resource = App::Ack::Resource::Basic->new($File::Next::dir);
return !grep { $_->filter($resource) } @{$idirs};
};
}
sub _compile_file_filter {
my ( $opt, $start ) = @_;
my $ifiles_filters = $opt->{ifiles};
my $filters = $opt->{'filters'} || [];
my $direct_filters = App::Ack::Filter::Collection->new();
my $inverse_filters = App::Ack::Filter::Collection->new();
foreach my $filter (@{$filters}) {
if ($filter->is_inverted()) {
# We want to check if files match the uninverted filters
$inverse_filters->add($filter->invert());
}
else {
$direct_filters->add($filter);
}
}
my %is_member_of_starting_set = map { (get_file_id($_) => 1) } @{$start};
my @ignore_dir_filter = @{$opt->{idirs} || []};
my @is_inverted = map { $_->is_inverted() } @ignore_dir_filter;
# this depends on InverseFilter->invert returning the original
# filter (for optimization)
@ignore_dir_filter = map { $_->is_inverted() ? $_->invert() : $_ } @ignore_dir_filter;
my $dont_ignore_dir_filter = grep { $_ } @is_inverted;
my $previous_dir = '';
my $previous_dir_ignore_result;
return sub {
if ( $opt_g ) {
if ( $File::Next::name =~ /$opt_regex/ && $opt_v ) {
return;
}
if ( $File::Next::name !~ /$opt_regex/ && !$opt_v ) {
return;
}
}
# ack always selects files that are specified on the command
# line, regardless of filetype. If you want to ack a JPEG,
# and say "ack foo whatever.jpg" it will do it for you.
return 1 if $is_member_of_starting_set{ get_file_id($File::Next::name) };
if ( $dont_ignore_dir_filter ) {
if ( $previous_dir eq $File::Next::dir ) {
if ( $previous_dir_ignore_result ) {
return 0;
}
}
else {
my @dirs = File::Spec->splitdir($File::Next::dir);
my $is_ignoring = 0;
for ( my $i = 0; $i < @dirs; $i++) {
my $dir_rsrc = App::Ack::Resource::Basic->new(File::Spec->catfile(@dirs[0 .. $i]));
my $j = 0;
for my $filter (@ignore_dir_filter) {
if ( $filter->filter($dir_rsrc) ) {
$is_ignoring = !$is_inverted[$j];
}
$j++;
}
}
$previous_dir = $File::Next::dir;
$previous_dir_ignore_result = $is_ignoring;
if ( $is_ignoring ) {
return 0;
}
}
}
# Ignore named pipes found in directory searching. Named
# pipes created by subprocesses get specified on the command
# line, so the rule of "always select whatever is on the
# command line" wins.
return 0 if -p $File::Next::name;
# We can't handle unreadable filenames; report them.
if ( not -r _ ) {
use filetest 'access';
if ( not -R $File::Next::name ) {
if ( $App::Ack::report_bad_filenames ) {
App::Ack::warn( "${File::Next::name}: cannot open file for reading" );
}
return 0;
}
}
my $resource = App::Ack::Resource::Basic->new($File::Next::name);
if ( $ifiles_filters && $ifiles_filters->filter($resource) ) {
return 0;
}
my $match_found = $direct_filters->filter($resource);
# Don't bother invoking inverse filters unless we consider the current resource a match
if ( $match_found && $inverse_filters->filter( $resource ) ) {
$match_found = 0;
}
return $match_found;
};
}
sub show_types {
my $resource = shift;
my $ors = shift;
my @types = filetypes( $resource );
my $types = join( ',', @types );
my $arrow = @types ? ' => ' : ' =>';
App::Ack::print( $resource->name, $arrow, join( ',', @types ), $ors );
return;
}
# Set default colors, load Term::ANSIColor
sub load_colors {
eval 'use Term::ANSIColor 1.10 ()';
eval 'use Win32::Console::ANSI' if $App::Ack::is_windows;
$ENV{ACK_COLOR_MATCH} ||= 'black on_yellow';
$ENV{ACK_COLOR_FILENAME} ||= 'bold green';
$ENV{ACK_COLOR_LINENO} ||= 'bold yellow';
return;
}
sub filetypes {
my ( $resource ) = @_;
my @matches;
foreach my $k (keys %App::Ack::mappings) {
my $filters = $App::Ack::mappings{$k};
foreach my $filter (@{$filters}) {
# clone the resource
my $clone = $resource->clone;
if ( $filter->filter($clone) ) {
push @matches, $k;
last;
}
}
}
# http://search.cpan.org/dist/Perl-Critic/lib/Perl/Critic/Policy/Subroutines/ProhibitReturnSort.pm
@matches = sort @matches;
return @matches;
}
# Returns a (fairly) unique identifier for a file.
# Use this function to compare two files to see if they're
# equal (ie. the same file, but with a different path/links/etc).
sub get_file_id {
my ( $filename ) = @_;
if ( $App::Ack::is_windows ) {
return File::Next::reslash( $filename );
}
else {
# XXX is this the best method? it always hits the FS
if( my ( $dev, $inode ) = (stat($filename))[0, 1] ) {
return join(':', $dev, $inode);
}
else {
# XXX this could be better
return $filename;
}
}
}
# Returns a regex object based on a string and command-line options.
# Dies when the regex $str is undefined (i.e. not given on command line).
sub build_regex {
my $str = shift;
my $opt = shift;
defined $str or App::Ack::die( 'No regular expression found.' );
$str = quotemeta( $str ) if $opt->{Q};
if ( $opt->{w} ) {
my $pristine_str = $str;
$str = "(?:$str)";
$str = "\\b$str" if $pristine_str =~ /^\w/;
$str = "$str\\b" if $pristine_str =~ /\w$/;
}
my $regex_is_lc = $str eq lc $str;
if ( $opt->{i} || ($opt->{smart_case} && $regex_is_lc) ) {
$str = "(?i)$str";
}
my $re = eval { qr/$str/m };
if ( !$re ) {
die "Invalid regex '$str':\n $@";
}
return $re;
}
my $match_column_number;
{
# number of context lines
my $n_before_ctx_lines;
my $n_after_ctx_lines;
# array to keep track of lines that might be required for a "before" context
my @before_context_buf;
# position to insert next line in @before_context_buf
my $before_context_pos;
# number of "after" context lines still pending
my $after_context_pending;
# number of latest line that got printed
my $printed_line_no;
my $is_iterating;
my $is_first_match;
my $has_printed_something;
BEGIN {
$has_printed_something = 0;
}
# setup context tracking variables
sub setup_line_context {
$n_before_ctx_lines = $opt_output ? 0 : ($opt_before_context || 0);
$n_after_ctx_lines = $opt_output ? 0 : ($opt_after_context || 0);
@before_context_buf = (undef) x $n_before_ctx_lines;
$before_context_pos = 0;
$is_tracking_context = $n_before_ctx_lines || $n_after_ctx_lines;
$is_first_match = 1;
return;
}
# adjust context tracking variables when entering a new file
sub setup_line_context_for_file {
$printed_line_no = 0;
$after_context_pending = 0;
if ( $opt_heading && !$opt_lines ) {
$is_first_match = 1;
}
return;
}
=for Developers
This subroutine jumps through a number of optimization hoops to
try to be fast in the more common use cases of ack. For one thing,
in non-context tracking searches (not using -A, -B, or -C),
conditions that normally would be checked inside the loop happen
outside, resulting in three nearly identical loops for -v, --passthru,
and normal searching. Any changes that happen to one should propagate
to the others if they make sense. The non-context branches also inline
does_match for performance reasons; any relevant changes that happen here
must also happen there.
=cut
sub print_matches_in_resource {
my ( $resource, $opt ) = @_;
my $max_count = $opt_m || -1;
my $nmatches = 0;
my $filename = $resource->name;
my $ors = $opt_print0 ? "\0" : "\n";
my $has_printed_for_this_resource = 0;
$is_iterating = 1;
my $fh = $resource->open();
if ( !$fh ) {
if ( $App::Ack::report_bad_filenames ) {
App::Ack::warn( "$filename: $!" );
}
return 0;
}
my $display_filename = $filename;
if ( $opt_show_filename && $opt_heading && $opt_color ) {
$display_filename = Term::ANSIColor::colored($display_filename, $ENV{ACK_COLOR_FILENAME});
}
# check for context before the main loop, so we don't
# pay for it if we don't need it
if ( $is_tracking_context ) {
$after_context_pending = 0;
while ( <$fh> ) {
if ( does_match($opt, $_) && $max_count ) {
if ( !$has_printed_for_this_resource ) {
if ( $opt_break && $has_printed_something ) {
App::Ack::print_blank_line();
}
if ( $opt_show_filename && $opt_heading ) {
App::Ack::print_filename( $display_filename, $ors );
}
}
print_line_with_context($opt, $filename, $_, $.);
$has_printed_for_this_resource = 1;
$nmatches++;
$max_count--;
}
elsif ( $opt_passthru ) {
chomp; # XXX proper newline handling?
# XXX inline this call?
if ( $opt_break && !$has_printed_for_this_resource && $has_printed_something ) {
App::Ack::print_blank_line();
}
print_line_with_options($opt, $filename, $_, $., ':');
$has_printed_for_this_resource = 1;
}
else {
chomp; # XXX proper newline handling?
print_line_if_context($opt, $filename, $_, $., '-');
}
last if ($max_count == 0) && ($after_context_pending == 0);
}
}
else {
local $_;
if ( $opt_passthru ) {
while ( <$fh> ) {
$match_column_number = undef;
if ( $opt_v ? !/$opt_regex/o : /$opt_regex/o ) {
if ( !$opt_v ) {
$match_column_number = $-[0] + 1;
}
if ( !$has_printed_for_this_resource ) {
if ( $opt_break && $has_printed_something ) {
App::Ack::print_blank_line();
}
if ( $opt_show_filename && $opt_heading ) {
App::Ack::print_filename( $display_filename, $ors );
}
}
print_line_with_context($opt, $filename, $_, $.);
$has_printed_for_this_resource = 1;
$nmatches++;
$max_count--;
}
else {
chomp; # XXX proper newline handling?
if ( $opt_break && !$has_printed_for_this_resource && $has_printed_something ) {
App::Ack::print_blank_line();
}
print_line_with_options($opt, $filename, $_, $., ':');
$has_printed_for_this_resource = 1;
}
last unless $max_count != 0;
}
}
elsif ( $opt_v ) {
$match_column_number = undef;
while ( <$fh> ) {
if ( !/$opt_regex/o ) {
if ( !$has_printed_for_this_resource ) {
if ( $opt_break && $has_printed_something ) {
App::Ack::print_blank_line();
}
if ( $opt_show_filename && $opt_heading ) {
App::Ack::print_filename( $display_filename, $ors );
}
}
print_line_with_context($opt, $filename, $_, $.);
$has_printed_for_this_resource = 1;
$nmatches++;
$max_count--;
}
last unless $max_count != 0;
}
}
else {
# XXX unroll first match check ($has_printed_for_this_resource)
# XXX what if this is a *huge* file? (see also -l)
my $contents = do {
local $/;
<$fh>;
};
my $prev_match_end = 0;
my $line_no = 1;
$match_column_number = undef;
while ( $contents =~ /$opt_regex/og ) {
my $match_start = $-[0];
my $match_end = $+[0];
pos($contents) = $prev_match_end;
$prev_match_end = $match_end;
while ( $contents =~ /\n/og && $-[0] < $match_start ) {
$line_no++;
}
my $start_line = rindex($contents, "\n", $match_start);
my $end_line = index($contents, "\n", $match_end);
if ( $start_line == -1 ) {
$start_line = 0;
}
else {
$start_line++;
}
if ( $end_line == -1 ) {
$end_line = length($contents);
}
else {
$end_line--;
}
$match_column_number = $match_start - $start_line + 1;
if ( !$has_printed_for_this_resource ) {
if ( $opt_break && $has_printed_something ) {
App::Ack::print_blank_line();
}
if ( $opt_show_filename && $opt_heading ) {
App::Ack::print_filename( $display_filename, $ors );
}
}
my $line = substr($contents, $start_line, $end_line - $start_line + 1);
$line =~ s/[\r\n]+$//g;
print_line_with_options($opt, $filename, $line, $line_no, ':');
pos($contents) = $end_line + 1;
$has_printed_for_this_resource = 1;
$nmatches++;
$max_count--;
last unless $max_count != 0;
}
}
}
$is_iterating = 0; # XXX this won't happen on an exception
# then again, do we care? ack doesn't really
# handle exceptions anyway.
return $nmatches;
}
sub print_line_with_options {
my ( $opt, $filename, $line, $line_no, $separator ) = @_;
$has_printed_something = 1;
$printed_line_no = $line_no;
my $ors = $opt_print0 ? "\0" : "\n";
my @line_parts;
if( $opt_color ) {
$filename = Term::ANSIColor::colored($filename,
$ENV{ACK_COLOR_FILENAME});
$line_no = Term::ANSIColor::colored($line_no,
$ENV{ACK_COLOR_LINENO});
}
if($opt_show_filename) {
if( $opt_heading ) {
push @line_parts, $line_no;
}
else {
push @line_parts, $filename, $line_no;
}
if( $opt_column ) {
push @line_parts, get_match_column();
}
}
if( $opt_output ) {
while ( $line =~ /$opt_regex/og ) {
# XXX We need to stop using eval() for --output. See https://github.com/petdance/ack2/issues/421
my $output = eval $opt_output;
App::Ack::print( join( $separator, @line_parts, $output ), $ors );
}
}
else {
if ( $opt_color ) {
$line =~ /$opt_regex/o; # this match is redundant, but we need
# to perfom it in order to get if
# capture groups are set
if ( @+ > 1 ) { # if we have captures
while ( $line =~ /$opt_regex/og ) {
my $offset = 0; # additional offset for when we add stuff
my $previous_match_end = 0;
for ( my $i = 1; $i < @+; $i++ ) {
my ( $match_start, $match_end ) = ( $-[$i], $+[$i] );
next unless defined($match_start);
next if $match_start < $previous_match_end;
my $substring = substr( $line,
$offset + $match_start, $match_end - $match_start );
my $substitution = Term::ANSIColor::colored( $substring,
$ENV{ACK_COLOR_MATCH} );
substr( $line, $offset + $match_start,
$match_end - $match_start, $substitution );
$previous_match_end = $match_end; # offsets do not need to be applied
$offset += length( $substitution ) - length( $substring );
}
pos($line) = $+[0] + $offset;
}
}
else {
my $matched = 0; # flag; if matched, need to escape afterwards
while ( $line =~ /$opt_regex/og ) {
$matched = 1;
my ( $match_start, $match_end ) = ($-[0], $+[0]);
next unless defined($match_start);
my $substring = substr( $line, $match_start,
$match_end - $match_start );
my $substitution = Term::ANSIColor::colored( $substring,
$ENV{ACK_COLOR_MATCH} );
substr( $line, $match_start, $match_end - $match_start,
$substitution );
pos($line) = $match_end +
(length( $substitution ) - length( $substring ));
}
# XXX why do we do this?
$line .= "\033[0m\033[K" if $matched;
}
}
push @line_parts, $line;
App::Ack::print( join( $separator, @line_parts ), $ors );
}
return;
}
sub iterate {
my ( $resource, $opt, $cb ) = @_;
$is_iterating = 1;
my $fh = $resource->open();
if ( !$fh ) {
if ( $App::Ack::report_bad_filenames ) {
App::Ack::warn( $resource->name . ': ' . $! );
}
return;
}
# Check for context before the main loop, so we don't pay for it if we don't need it.
if ( $is_tracking_context ) {
$after_context_pending = 0;
while ( <$fh> ) {
last unless $cb->();
}
}
else {
local $_;
while ( <$fh> ) {
last unless $cb->();
}
}
$is_iterating = 0; # XXX this won't happen on an exception
# then again, do we care? ack doesn't really
# handle exceptions anyway.
return;
}
sub print_line_with_context {
my ( $opt, $filename, $matching_line, $line_no ) = @_;
my $ors = $opt_print0 ? "\0" : "\n";
my $is_tracking_context = $opt_after_context || $opt_before_context;
$matching_line =~ s/[\r\n]+$//g;
# check if we need to print context lines first
if( $is_tracking_context ) {
my $before_unprinted = $line_no - $printed_line_no - 1;
if ( !$is_first_match && ( !$printed_line_no || $before_unprinted > $n_before_ctx_lines ) ) {
App::Ack::print('--', $ors);
}
# We want at most $n_before_ctx_lines of context
if ( $before_unprinted > $n_before_ctx_lines ) {
$before_unprinted = $n_before_ctx_lines;
}
while ( $before_unprinted > 0 ) {
my $line = $before_context_buf[($before_context_pos - $before_unprinted + $n_before_ctx_lines) % $n_before_ctx_lines];
chomp $line;
# Disable $opt->{column} since there are no matches in the context lines
local $opt_column = 0;
print_line_with_options($opt, $filename, $line, $line_no-$before_unprinted, '-');
$before_unprinted--;
}
}
print_line_with_options($opt, $filename, $matching_line, $line_no, ':');
# We want to get the next $n_after_ctx_lines printed
$after_context_pending = $n_after_ctx_lines;
$is_first_match = 0;
return;
}
# print the line only if it's part of a context we need to display
sub print_line_if_context {
my ( $opt, $filename, $line, $line_no, $separator ) = @_;
if ( $after_context_pending ) {
# Disable $opt->{column} since there are no matches in the context lines
local $opt_column = 0;
print_line_with_options( $opt, $filename, $line, $line_no, $separator );
--$after_context_pending;
}
elsif ( $n_before_ctx_lines ) {
# save line for "before" context
$before_context_buf[$before_context_pos] = $_;
$before_context_pos = ($before_context_pos+1) % $n_before_ctx_lines;
}
return;
}
}
# does_match() MUST have an $opt_regex set.
=for Developers
This subroutine is inlined a few places in print_matches_in_resource
for performance reasons, so any changes here must be copied there as
well.
=cut
sub does_match {
my ( $opt, $line ) = @_;
$match_column_number = undef;
if ( $opt_v ) {
return ( $line !~ /$opt_regex/o );
}
else {
if ( $line =~ /$opt_regex/o ) {
# @- = @LAST_MATCH_START
# @+ = @LAST_MATCH_END
$match_column_number = $-[0] + 1;
return 1;
}
else {
return;
}
}
}
sub get_match_column {
return $match_column_number;
}
sub resource_has_match {
my ( $resource, $opt ) = @_;
my $has_match = 0;
my $fh = $resource->open();
if ( !$fh ) {
if ( $App::Ack::report_bad_filenames ) {
App::Ack::warn( $resource->name . ': ' . $! );
}
}
else {
if ( $opt_v ) {
while ( <$fh> ) {
if (!/$opt_regex/o) {
$has_match = 1;
last;
}
}
}
else {
# XXX read in chunks
# XXX only do this for certain file sizes?
my $content = do {
local $/;
<$fh>;
};
$has_match = $content =~ /$opt_regex/o;
}
close $fh;
}
return $has_match;
}
sub count_matches_in_resource {
my ( $resource, $opt ) = @_;
my $nmatches = 0;
my $fh = $resource->open();
if ( !$fh ) {
if ( $App::Ack::report_bad_filenames ) {
App::Ack::warn( $resource->name . ': ' . $! );
}
}
else {
if ( $opt_v ) {
while ( <$fh> ) {
++$nmatches if (!/$opt_regex/o);
}
}
else {
my $content = do {
local $/;
<$fh>;
};
$nmatches =()= ($content =~ /$opt_regex/og);
}
close $fh;
}
return $nmatches;
}
sub main {
my @arg_sources = App::Ack::ConfigLoader::retrieve_arg_sources();
my $opt = App::Ack::ConfigLoader::process_args( @arg_sources );
$opt_after_context = $opt->{after_context};
$opt_before_context = $opt->{before_context};
$opt_output = $opt->{output};
$opt_print0 = $opt->{print0};
$opt_color = $opt->{color};
$opt_heading = $opt->{heading};
$opt_show_filename = $opt->{show_filename};
$opt_regex = $opt->{regex};
$opt_break = $opt->{break};
$opt_count = $opt->{count};
$opt_v = $opt->{v};
$opt_m = $opt->{m};
$opt_g = $opt->{g};
$opt_f = $opt->{f};
$opt_lines = $opt->{lines};
$opt_L = $opt->{L};
$opt_l = $opt->{l};
$opt_passthru = $opt->{passthru};
$opt_column = $opt->{column};
$App::Ack::report_bad_filenames = !$opt->{dont_report_bad_filenames};
if ( $opt->{flush} ) {
$| = 1;
}
if ( !defined($opt_color) && !$opt_g ) {
my $windows_color = 1;
if ( $App::Ack::is_windows ) {
$windows_color = eval { require Win32::Console::ANSI; };
}
$opt_color = !App::Ack::output_to_pipe() && $windows_color;
}
if ( not defined $opt_heading and not defined $opt_break ) {
$opt_heading = $opt_break = $opt->{break} = !App::Ack::output_to_pipe();
}
if ( defined($opt->{H}) || defined($opt->{h}) ) {
$opt_show_filename = $opt->{show_filename} = $opt->{H} && !$opt->{h};
}
if ( my $output = $opt_output ) {
$output =~ s{\\}{\\\\}g;
$output =~ s{"}{\\"}g;
$opt_output = qq{"$output"};
}
my $resources;
if ( $App::Ack::is_filter_mode && !$opt->{files_from} ) { # probably -x
$resources = App::Ack::Resources->from_stdin( $opt );
$opt_regex = shift @ARGV if not defined $opt_regex;
$opt_regex = $opt->{regex} = build_regex( $opt_regex, $opt );
}
else {
if ( $opt_f || $opt_lines ) {
if ( $opt_regex ) {
App::Ack::warn( "regex ($opt_regex) specified with -f or --lines" );
App::Ack::exit_from_ack( 0 ); # XXX the 0 is misleading
}
}
else {
$opt_regex = shift @ARGV if not defined $opt_regex;
$opt_regex = $opt->{regex} = build_regex( $opt_regex, $opt );
}
if ( $opt_regex && $opt_regex =~ /\n/ ) {
App::Ack::exit_from_ack( 0 );
}
my @start;
if ( not defined $opt->{files_from} ) {
@start = @ARGV;
}
if ( !exists($opt->{show_filename}) ) {
unless(@start == 1 && !(-d $start[0])) {
$opt_show_filename = $opt->{show_filename} = 1;
}
}
if ( defined $opt->{files_from} ) {
$resources = App::Ack::Resources->from_file( $opt, $opt->{files_from} );
exit 1 unless $resources;
}
else {
@start = ('.') unless @start;
foreach my $target (@start) {
if ( !-e $target && $App::Ack::report_bad_filenames) {
App::Ack::warn( "$target: No such file or directory" );
}
}
$opt->{file_filter} = _compile_file_filter($opt, \@start);
$opt->{descend_filter} = _compile_descend_filter($opt);
$resources = App::Ack::Resources->from_argv( $opt, \@start );
}
}
App::Ack::set_up_pager( $opt->{pager} ) if defined $opt->{pager};
my $ors = $opt_print0 ? "\0" : "\n";
my $only_first = $opt->{1};
my $nmatches = 0;
my $total_count = 0;
setup_line_context( $opt );