Debian Codegraph @

texmf-dist/doc/latex/typog/typog-grep.pl.in

raw
1 #! /usr/bin/env perl
2
3 use autodie qw(:all);
4 use strict;
5 use warnings;
6
7 use Data::Dumper ();
8 use Encode ();
9 use English;
10 use File::Basename ();
11 use Getopt::Long;
12 use IO::File;
13 use IO::Handle;
14 use Term::ANSIColor ();
15
16 use constant COMMAND_NAME => File::Basename::basename($PROGRAM_NAME);
17
18 my $DEBUG = 0;
19 my $MATCH_COUNT = 0;
20 my $OUTPUT_IS_REDIRECTED;
21
22 sub fail_with_error {
23 print STDERR join('', COMMAND_NAME, ': ', @_, "\n");
24 exit 2;
25 }
26
27 sub issue_warning {
28 print STDERR join('', COMMAND_NAME, ': warning: ', @_, "\n");
29 }
30
31 sub debug_print {
32 return unless $DEBUG;
33 print STDERR "+ @_\n";
34 }
35
36 sub quote_filesystem {qq("$_[0]")}
37 sub quote_literal {qq(`$_[0]')}
38
39 sub limit_string_length {
40 my ($a_string, $a_maximum_length) = @_;
41
42 if (length $a_string <= $a_maximum_length) {
43 $a_string;
44 } else {
45 substr($a_string, 0, $a_maximum_length - 3) . '...';
46 }
47 }
48
49 ## We set all colors to `undef' and fill them later with the values
50 ## of the actual configuration.
51 my $highlight_patterns = {
52 PARTIAL_LINE => {
53 FONT_SPEC => [qr#
54 \\
55 (?: OMS | OMX | OT1 | T1 | TS1 | U )
56 (?: /[^/]+ ){5} / \S+ \s
57 (?: \([+-]\d+\) )?
58 #x, undef],
59 MATH => [qr#
60 \$
61 \\
62 (?: LMS | OML )
63 (?: /[^/]+ ){5} / \S+ \s
64 (?: \([+-]\d+\) )?
65 .*?
66 \$
67 #x, undef]
68 },
69 WHOLE_LINE => {
70 FILL_STATE => [qr#^(?:Under|Over)full \\hbox .*$#, undef],
71 FIRST_VBOX => [qr#^%%#, undef],
72 HORIZONTAL_BREAKPOINT => [qr#^@@\d+:.*$#, undef],
73 HORIZONTAL_BREAK_CANDIDATE => [qr#^@[\\ ].*$#, undef],
74 LINE_BREAK_PASS => [qr#^@[a-z]+?pass#, undef],
75 TIGHTNESS => [qr#^(?:Loose|Tight) \\hbox .*$#, undef],
76 VERTICAL_BREAKPOINT => [qr#^% t=\d+.*$#, undef]
77 }
78 };
79
80 sub colorize_line {
81 my ($configuration, $line) = @_;
82
83 foreach my $pattern_color_pair (values %{$highlight_patterns->{WHOLE_LINE}}) {
84 next unless $pattern_color_pair->[1];
85 return Term::ANSIColor::colored($line, $pattern_color_pair->[1])
86 if $line =~ $pattern_color_pair->[0];
87 }
88 return $line if $line =~ m#^\.#; # we do not paint box contents yet
89
90 $line =~ s#$highlight_patterns->{PARTIAL_LINE}->{MATH}->[0]
91 #Term::ANSIColor::colored($MATCH, $highlight_patterns->{PARTIAL_LINE}->{MATH}->[1])
92 #egx;
93
94 $line =~ s#$highlight_patterns->{PARTIAL_LINE}->{FONT_SPEC}->[0]
95 #Term::ANSIColor::colored($MATCH, $highlight_patterns->{PARTIAL_LINE}->{FONT_SPEC}->[1])
96 #egx;
97
98 return $line;
99 }
100
101 my $open_or_close_tag_regexp = qr#^</?typog-inspect[ >]#; # somewhat sloppy definition
102 my $close_tag_regexp = qr#^</typog-inspect>#;
103 my $open_tag_regexp =
104 qr#^
105 <typog-inspect \s+
106 id="(?<id_match> .*?)" \s+
107 job="(?<job_match> .*?)" \s+
108 line="(?<line_match> .*?)" \s+
109 page="(?<page_match> .*?)"
110 >#x;
111
112 sub find_encoder {
113 my $encoding = shift;
114
115 my $encoder;
116
117 if ($encoding) {
118 $encoder = Encode::find_encoding($encoding);
119 if (!$encoder) {
120 issue_warning("encoding @{[quote_literal($encoding)]} unknown; proceeding without decoder");
121 }
122 }
123
124 return $encoder;
125 }
126
127 sub grep_log_file {
128 my ($options, $configuration, $file, $filename, $id_regexp) = @_;
129
130 my $encoder = find_encoder($options->{ENCODING});
131 my $job_name;
132 my $line_number = 0; # line number in the log file we are inspecting, i.e., $filename
133 my $match_count = 0;
134 my $source_line_number; # line number in TeX file the log refers to, i.e., "$job_name.tex"
135 my $page_number;
136 my $regexp_modifier = $options->{IGNORE_CASE} ? 'i' : '';
137 my $id_value;
138 my @nesting_levels;
139
140 if ($options->{WORD_REGEXP}) {
141 $id_regexp = "\\b$id_regexp\\b";
142 }
143
144 while (my $line = readline $file) {
145 chomp $line;
146 $line_number++;
147
148 $line = Encode::encode_utf8($encoder->decode($line, Encode::FB_CROAK)) if $encoder;
149
150 if ($line =~ $close_tag_regexp) {
151 fail_with_error("$filename: $line_number: mismatched open/close tags") unless @nesting_levels;
152 pop @nesting_levels;
153 }
154
155 if (@nesting_levels and $nesting_levels[-1] and $line !~ $open_or_close_tag_regexp) {
156 if ($options->{LOG_LINE_NUMBER}) {
157 my $formatted_log_line_number =
158 sprintf $configuration->{LOG_LINE_NUMBER_FORMAT}, $line_number;
159 if ($options->{COLORIZE_OUTPUT}) {
160 $formatted_log_line_number =
161 Term::ANSIColor::colored($formatted_log_line_number,
162 $configuration->{COLORS}->{LOG_LINE_NUMBER});
163 }
164 print $formatted_log_line_number, ' ';
165 }
166
167 print "$job_name: " if $options->{JOB_NAME};
168
169 if ($options->{LINE_NUMBER}) {
170 my $formatted_line_number = sprintf $configuration->{LINE_NUMBER_FORMAT}, $source_line_number;
171 if ($options->{COLORIZE_OUTPUT}) {
172 $formatted_line_number =
173 Term::ANSIColor::colored($formatted_line_number,
174 $configuration->{COLORS}->{LINE_NUMBER});
175 }
176 print $formatted_line_number, ' ';
177 }
178
179 if ($options->{PAGE_NUMBER}) {
180 my $formatted_page_number = sprintf $configuration->{PAGE_NUMBER_FORMAT}, $page_number;
181 if ($options->{COLORIZE_OUTPUT}) {
182 $formatted_page_number =
183 Term::ANSIColor::colored($formatted_page_number,
184 $configuration->{COLORS}->{PAGE_NUMBER});
185 }
186 print $formatted_page_number, ' ';
187 }
188
189 if ($options->{ID} and not $configuration->{PRINT_ID_AS_HEADING}) {
190 my $formatted_id = sprintf $configuration->{ID_INLINE_FORMAT}, $id_value;
191 if ($options->{COLORIZE_OUTPUT}) {
192 $formatted_id = Term::ANSIColor::colored($formatted_id ,
193 $configuration->{COLORS}->{ID_COLOR});
194 }
195 print $formatted_id, ' ';
196 }
197
198 if ($options->{COLORIZE_OUTPUT}) {
199 print colorize_line($configuration, $line);
200 } else {
201 print $line;
202 }
203 print "\n";
204 }
205
206 if ($line =~ $open_tag_regexp) {
207 $id_value = limit_string_length($+{id_match}, $configuration->{ID_MAX_LENGTH});
208 $job_name = $+{job_match};
209 $source_line_number = $+{line_match};
210 $page_number = $+{page_match};
211
212 my $found_matching_id = ($id_value =~ m/(?$regexp_modifier)$id_regexp/) ? 1 : 0;
213 push @nesting_levels, $found_matching_id;
214 if ($found_matching_id) {
215 ++$MATCH_COUNT; # global count -- needed for return code of program
216 ++$match_count; # per file count -- needed to be able to separate the hunks
217
218 print "\n" if $match_count >= 2;
219 if ($options->{ID} and $configuration->{PRINT_ID_AS_HEADING}) {
220 my $formatted_id = sprintf $configuration->{ID_HEADING_FORMAT}, $id_value;
221 if ($options->{COLORIZE_OUTPUT}) {
222 $formatted_id =
223 Term::ANSIColor::colored($formatted_id,
224 $configuration->{COLORS}->{ID_HEADING_COLOR});
225 }
226 print $formatted_id, "\n";
227 }
228 }
229 }
230 }
231
232 return $match_count;
233 }
234
235 sub show_ids_in_file {
236 my ($options, $configuration, $file, $filename, $id_regexp) = @_;
237
238 my $encoder = find_encoder($options->{ENCODING});
239 my $line_number = 0;
240 my $match_count = 0;
241 my @nesting_levels;
242
243 while (my $line = readline $file) {
244 chomp $line;
245 $line_number++;
246
247 $line = Encode::encode_utf8($encoder->decode($line, Encode::FB_CROAK)) if $encoder;
248
249 if ($line =~ $close_tag_regexp) {
250 fail_with_error("$filename: $line_number: mismatched open/close tags") unless @nesting_levels;
251 pop @nesting_levels;
252 }
253
254 if ($line =~ $open_tag_regexp) {
255 my $id_value = limit_string_length($+{id_match}, $configuration->{ID_MAX_LENGTH});
256 my $job_name = $+{job_match};
257 my $source_line_number = $+{line_match};
258 my $page_number = $+{page_match};
259
260 ++$MATCH_COUNT;
261 ++$match_count;
262 push @nesting_levels, 1;
263
264 if ($options->{LOG_LINE_NUMBER}) {
265 my $formatted_log_line_number =
266 sprintf $configuration->{LOG_LINE_NUMBER_FORMAT}, $line_number;
267 if ($options->{COLORIZE_OUTPUT}) {
268 $formatted_log_line_number =
269 Term::ANSIColor::colored($formatted_log_line_number,
270 $configuration->{COLORS}->{LOG_LINE_NUMBER});
271 }
272 print $formatted_log_line_number, ' ';
273 }
274
275 print "$job_name: " if $options->{JOB_NAME};
276
277 if ($options->{LINE_NUMBER}) {
278 my $formatted_line_number = sprintf $configuration->{LINE_NUMBER_FORMAT}, $source_line_number;
279 if ($options->{COLORIZE_OUTPUT}) {
280 $formatted_line_number =
281 Term::ANSIColor::colored($formatted_line_number,
282 $configuration->{COLORS}->{LINE_NUMBER});
283 }
284 print $formatted_line_number, ' ';
285 }
286
287 if ($options->{PAGE_NUMBER}) {
288 my $formatted_page_number = sprintf $configuration->{PAGE_NUMBER_FORMAT}, $page_number;
289 if ($options->{COLORIZE_OUTPUT}) {
290 $formatted_page_number =
291 Term::ANSIColor::colored($formatted_page_number,
292 $configuration->{COLORS}->{PAGE_NUMBER});
293 }
294 print $formatted_page_number, ' ';
295 }
296
297 my $indent = $configuration->{ID_INDENT} * (@nesting_levels - 1);
298 print ' ' x $indent, $id_value, "\n";
299 }
300 }
301
302 return $match_count;
303 }
304
305 sub open_file_for_reading {
306 my $filename = shift;
307
308 my $file;
309
310 if ($filename eq 'stdin') {
311 $file = IO::Handle->new();
312 $file->fdopen(fileno(STDIN), 'r') or
313 fail_with_error("cannot open stdin: $OS_ERROR");
314 } else {
315 $file = IO::File->new($filename, 'r') or
316 fail_with_error("cannot open @{[quote_filesystem($filename)]}: $OS_ERROR");
317 }
318
319 return $file;
320 }
321
322 sub close_file {
323 my ($file, $filename) = shift;
324
325 $file->close or
326 issue_warning("problems while closing @{[quote_filesystem($filename)]}: $OS_ERROR");
327 }
328
329 sub grep_or_show {
330 my ($options, $configuration, $file, $filename, $id_regexp) = @_;
331
332 if ($options->{SHOW_ALL_IDS}) {
333 return show_ids_in_file($options, $configuration, $file, $filename, $id_regexp);
334 } else {
335 return grep_log_file($options, $configuration, $file, $filename, $id_regexp);
336 }
337 }
338
339 sub scan_files {
340 my ($options, $configuration, $id_regexp, $log_filenames) = @_;
341
342 my $match_count = 0;
343
344 if (@$log_filenames) {
345 foreach my $log_filename (@$log_filenames) {
346 $log_filename = 'stdin' if $log_filename eq '-';
347 if (@$log_filenames >= 2) {
348 print "\n" unless $log_filename eq $log_filenames->[0];
349 my $filename_header = "==> $log_filename <==\n";
350 $filename_header = Term::ANSIColor::colored($filename_header,
351 $configuration->{COLORS}->{FILE_HEADER})
352 if $options->{COLORIZE_OUTPUT};
353 print $filename_header;
354 }
355 my $file = open_file_for_reading($log_filename);
356 $match_count += grep_or_show($options, $configuration, $file, $log_filename, $id_regexp);
357 close_file($file, $log_filename);
358 }
359 } else {
360 my $log_filename = 'stdin';
361 my $file = open_file_for_reading($log_filename);
362 $match_count = grep_or_show($options, $configuration, $file, $log_filename, $id_regexp);
363 close_file($file, $log_filename);
364 }
365
366 return $match_count;
367 }
368
369 sub redirect_and_scan_files {
370 my ($options, $configuration, $id_regexp, $log_filenames) = @_;
371
372 my $pager;
373
374 my $pid = open($pager, '|-', $configuration->{PAGER}, $configuration->{PAGER_FLAGS});
375 fail_with_error('failed to redirect to pager ', quote_literal($configuration->{PAGER}),
376 ' with flags ', quote_literal($configuration->{PAGER_FLAGS}),
377 ": $OS_ERROR")
378 unless defined $pid;
379 my $stdout = select $pager;
380
381 $pager->autoflush;
382 my $match_count = scan_files($options, $configuration, $id_regexp, $log_filenames);
383
384 close $pager or issue_warning "error occurred while closing the pager (pid: $pid) pipe: $OS_ERROR";
385 select $stdout;
386
387 return $match_count;
388 }
389
390 ########################################################################
391
392 my $configuration_key_map = {
393 'id-format' => 'ID_INLINE_FORMAT',
394 'id-indent' => 'ID_INDENT',
395 'id-heading' => 'PRINT_ID_AS_HEADING',
396 'id-heading-format' => 'ID_HEADING_FORMAT',
397 'id-max-length' => 'ID_MAX_LENGTH',
398 'line-number-format' => 'LINE_NUMBER_FORMAT',
399 'log-line-number-format' => 'LOG_LINE_NUMBER_FORMAT',
400 'page-number-format' => 'PAGE_NUMBER_FORMAT',
401
402 'file-header-color' => 'FILE_HEADER',
403 'fill-state-color' => 'FILL_STATE',
404 'first-vbox-color' => 'FIRST_VBOX',
405 'font-spec-color' => 'FONT_SPEC',
406 'horizontal-break-candidate-color' => 'HORIZONTAL_BREAK_CANDIDATE',
407 'horizontal-breakpoint-color' => 'HORIZONTAL_BREAKPOINT',
408 'id-color' => 'ID_COLOR',
409 'id-heading-color' => 'ID_HEADING_COLOR',
410 'line-break-pass-color' => 'LINE_BREAK_PASS',
411 'line-number-color' => 'LINE_NUMBER',
412 'log-line-number-color' => 'LOG_LINE_NUMBER',
413 'math-color' => 'MATH',
414 'page-number-color' => 'PAGE_NUMBER',
415 'pager' => 'PAGER',
416 'pager-flags' => 'PAGER_FLAGS',
417 'tightness-color' => 'TIGHTNESS',
418 'vertical-breakpoint-color' => 'VERTICAL_BREAKPOINT'
419 };
420
421 my $default_configuration = {
422 COLORS => {
423 FILE_HEADER => 'bold black',
424 FILL_STATE => 'bold magenta',
425 FIRST_VBOX => 'bold red',
426 FONT_SPEC => 'grey12',
427 HORIZONTAL_BREAKPOINT => 'bold green',
428 HORIZONTAL_BREAK_CANDIDATE => 'blue',
429 ID_COLOR => 'white on_black',
430 ID_HEADING_COLOR => 'white on_black',
431 LINE_BREAK_PASS => 'bold green',
432 LINE_NUMBER => 'bold black',
433 LOG_LINE_NUMBER => 'italic black',
434 MATH => 'yellow',
435 PAGE_NUMBER => 'bold white on_red',
436 TIGHTNESS => 'bold cyan',
437 VERTICAL_BREAKPOINT => 'red'
438 },
439 ID_INLINE_FORMAT => '%s:',
440 ID_HEADING_FORMAT => '--> %s <--',
441 ID_INDENT => 8,
442 ID_MAX_LENGTH => 40,
443 LINE_NUMBER_FORMAT => '%5d',
444 LOG_LINE_NUMBER_FORMAT => '%6d',
445 PAGE_NUMBER_FORMAT => '[%3d]',
446 PAGER => 'less',
447 PAGER_FLAGS => '--quit-if-one-screen',
448 PRINT_ID_AS_HEADING => 0
449 };
450
451 sub initialize_highlighting_from_configuration {
452 my $configuration = shift;
453
454 while (my (undef, $assoc) = each %$highlight_patterns) {
455 while (my ($name, $pattern_color_pair) = each %$assoc) {
456 $pattern_color_pair->[1] = $configuration->{COLORS}->{$name};
457 }
458 }
459 }
460
461 sub modify_configuration {
462 my ($configuration, $key, $value) = @_;
463
464 fail_with_error('malformed KEY=VALUE pair -- missing key') unless $key;
465
466 if (defined $configuration_key_map->{$key}) {
467 if ($key =~ m/-color$/) {
468 $configuration->{COLORS}->{$configuration_key_map->{$key}} = $value;
469 } else {
470 $configuration->{$configuration_key_map->{$key}} = $value;
471 }
472 } else {
473 fail_with_error("@{[quote_literal($key)]} is not a valid configuration KEY");
474 }
475 }
476
477 sub setup_configuation {
478 my ($config_spec, $configuration) = @_;
479
480 foreach my $spec (split ':', $config_spec) {
481 my ($key, $value) = split '=', $spec;
482 modify_configuration($configuration, $key, $value);
483 }
484 }
485
486 my $default_options = {
487 COLORIZE_MODE => 'auto',
488 DEBUG => 0,
489 ENCODING => undef,
490 ID => 0,
491 IGNORE_CASE => 0,
492 JOB_NAME => 0,
493 LINE_NUMBER => 0,
494 LOG_LINE_NUMBER => 0,
495 PAGE_NUMBER => 0,
496 REQUEST_PAGER => 1,
497 WORD_REGEXP => 0
498 };
499
500 sub show_help {
501 print <<HELP_TEXT;
502 Usage: @{[COMMAND_NAME]} [OPTION] ID-REGEXP LOG-FILE...
503 Structured grep for typog-inspect elements that match ID-REGEXP in LOG-FILE.
504
505 Options
506 --color [WHEN],
507 --colour [WHEN] use color to highlight specific log contents
508 WHEN is 'always', 'never', or 'auto'
509 -C, --config KEY=VALUE set configuration KEY to VALUE
510 -E, --encoding ENCODING set character ENCODING of LOG-FILE
511 -i, --[no-]id print matching id with output lines
512 -y, --[no-]ignore-case ignore case distinctions in patterns and data
513 -j, --[no-]job-name print \\jobname with output lines
514 -n, --[no-]line-number print TeX-source line number with output lines
515 -N, --[no-]log-line-number print log-file line number with output lines
516 -p, --[no-]page-number print page number with output lines
517 -P, --[no-]pager redirect output to pager
518 -w, --[no-]word-regexp match only whole words
519
520 -a, --all, --any discover all IDs in LOG-FILE
521
522 --debug turn on debug output
523 -h, --help display this help and exit
524 --show-config show default configuration and exit
525 --show-encodings show all known encodings and exit
526 -V, --version show version information and exit
527
528 HELP_TEXT
529
530 exit 0;
531 }
532
533 sub show_encodings {
534 my @all_encodings = Encode->encodings(':all');
535
536 foreach my $encoding (@all_encodings) {
537 print "$encoding\n";
538 }
539
540 exit 0;
541 }
542
543 sub show_configuration {
544 my $format_string_value = sub {quote_literal($default_configuration->{$_[0]})};
545
546 print <<FIXED_CONFIGURATION_TEXT;
547 Configuration
548 Key Default Value
549 ------------------------------------ -------------
550 id-format @{[$format_string_value->('ID_INLINE_FORMAT')]}
551 id-heading $default_configuration->{PRINT_ID_AS_HEADING}
552 id-heading-format @{[$format_string_value->('ID_HEADING_FORMAT')]}
553 id-indent $default_configuration->{ID_INDENT}
554 id-max-length $default_configuration->{ID_MAX_LENGTH}
555 line-number-format @{[$format_string_value->('LINE_NUMBER_FORMAT')]}
556 log-line-number-format @{[$format_string_value->('LOG_LINE_NUMBER_FORMAT')]}
557 page-number-format @{[$format_string_value->('PAGE_NUMBER_FORMAT')]}
558 pager @{[$format_string_value->('PAGER')]}
559 pager-flags @{[$format_string_value->('PAGER_FLAGS')]}
560
561 FIXED_CONFIGURATION_TEXT
562
563 foreach my $configuration_key (sort keys %$configuration_key_map) {
564 next unless $configuration_key =~ m/-color$/;
565 printf("%-36s %s\n",
566 $configuration_key,
567 quote_literal($default_configuration->
568 {COLORS}->
569 {$configuration_key_map->{$configuration_key}}));
570 }
571
572 exit 0;
573 }
574
575 sub show_version {
576 print <<VERSION_TEXT;
577 typog-grep @RELEASE@
578
579 Copyright (C) 2025 by Ch. L. Spiel
580 License LPPL: LaTeX Project Public License version 1.3c or later
581 VERSION_TEXT
582
583 exit 0;
584 }
585
586 sub get_options {
587 my ($options, $configuration) = @_;
588
589 Getopt::Long::Configure('gnu_getopt', 'no_ignore_case');
590
591 Getopt::Long::GetOptions('a|all|any' => \$options->{SHOW_ALL_IDS},
592 'color|colour=s' => \$options->{COLORIZE_MODE},
593 'C|configuration=s' => sub{setup_configuation($_[1], $configuration)},
594 'debug+' => \$DEBUG,
595 'E|encoding=s' => \$options->{ENCODING},
596 'h|help' => \&show_help,
597 'i|id!' => \$options->{ID},
598 'y|ignore-case!' => \$options->{IGNORE_CASE},
599 'j|job-name!' => \$options->{JOB_NAME},
600 'n|line-number!' => \$options->{LINE_NUMBER},
601 'N|log-line-number!' => \$options->{LOG_LINE_NUMBER},
602 'p|page-number!' => \$options->{PAGE_NUMBER},
603 'P|pager!' => \$options->{REQUEST_PAGER},
604 'show-encodings' => \&show_encodings,
605 'show-config' => \&show_configuration,
606 'V|version' => \&show_version,
607 'w|word-regexp!' => \$options->{WORD_REGEXP}) or
608 fail_with_error('problems while parsing options');
609
610 if ($options->{COLORIZE_MODE}) {
611 fail_with_error("unknown colorize mode @{[quote_literal($options->{COLORIZE_MODE})]}")
612 unless $options->{COLORIZE_MODE} =~ m/^(?:always|auto|never)$/i;
613 } else {
614 $options->{COLORIZE_MODE} = 'auto';
615 }
616 }
617
618 sub do_colorize {
619 my $colorize_mode = shift;
620
621 if ($colorize_mode =~ m/never/i) {
622 0;
623 } elsif ($colorize_mode =~ m/always/i) {
624 1;
625 } elsif ($colorize_mode =~ m/auto/i) {
626 not $OUTPUT_IS_REDIRECTED;
627 }
628 }
629
630 ## For the comparison with the POSIX spec of grep(1) consult
631 ## https://pubs.opengroup.org/onlinepubs/9699919799/utilities/grep.html
632
633 sub main {
634 $OUTPUT_IS_REDIRECTED = -t STDOUT ? 0 : 1;
635
636 my $configuration = {%$default_configuration};
637 my $options = {%$default_options};
638 debug_print(Data::Dumper::Dumper(['Default Configuration', $configuration]));
639 debug_print(Data::Dumper::Dumper(['Default Options', $options]));
640
641 my $user_options = {};
642 my $user_configuration = {};
643 get_options($user_options, $user_configuration);
644 debug_print(Data::Dumper::Dumper(['User Configuration', $user_configuration]));
645 debug_print(Data::Dumper::Dumper(['User Options', $user_options]));
646 while (my ($key, $value) = each %$user_options) {
647 $options->{$key} = $value if $value;
648 }
649 while (my ($key, $value) = each %$user_configuration) {
650 $configuration->{$key} = $value if $value;
651 }
652 debug_print(Data::Dumper::Dumper(['Final Configuration', $configuration]));
653 debug_print(Data::Dumper::Dumper(['Final Options', $options]));
654
655 $options->{COLORIZE_OUTPUT} = do_colorize($options->{COLORIZE_MODE});
656 initialize_highlighting_from_configuration($configuration);
657
658 my $id_regexp;
659 if ($options->{SHOW_ALL_IDS}) {
660 $id_regexp = '^';
661 issue_warning("option @{[quote_literal('--id')]} ignored in @{[quote_literal('--all')]} mode")
662 if $options->{ID};
663 } else {
664 fail_with_error('missing ID-REGEXP') unless @ARGV >= 1;
665 $id_regexp = shift @ARGV;
666 }
667
668 if ($user_options->{REQUEST_PAGER} && $OUTPUT_IS_REDIRECTED) {
669 issue_warning("option @{[quote_literal('--pager')]} ignored because output is redirected");
670 }
671 my $use_pager = $options->{REQUEST_PAGER} && !$OUTPUT_IS_REDIRECTED;
672 my $match_count;
673 if ($use_pager) {
674 $match_count = redirect_and_scan_files($options, $configuration, $id_regexp, \@ARGV);
675 } else {
676 $match_count = scan_files($options, $configuration, $id_regexp, \@ARGV);
677 }
678
679 exit ($match_count == 0);
680 }
681
682 main();