| 1 | package App::PTP; | ||
| 2 | |||
| 3 | use 5.022; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use App::PTP::Args; | ||
| 8 | use App::PTP::Commands 'warn_or_die_if_needed'; | ||
| 9 | use App::PTP::Files; | ||
| 10 | use Data::Dumper; | ||
| 11 | use File::Find; | ||
| 12 | use Safe; | ||
| 13 | |||
| 14 | our $VERSION = '1.19'; | ||
| 15 | |||
| 16 | $Data::Dumper::Terse = 1; # Don't output variable names. | ||
| 17 | $Data::Dumper::Sortkeys = 1; # Sort the content of the hash variables. | ||
| 18 | $Data::Dumper::Useqq = 1; # Use double quote for string (better escaping). | ||
| 19 | |||
| 20 | my $safe = Safe->new(); | ||
| 21 | $safe->deny_only(':subprocess', ':ownprocess', ':others', ':dangerous'); | ||
| 22 | $safe->reval('use App::PTP::PerlEnv;'); | ||
| 23 | $safe->reval('use File::Spec::Functions qw(:ALL);'); | ||
| 24 | |||
| 25 | # maybe_expand_dirs(filepath) | ||
| 26 | # If filepath is a normal file, then returns it as, is. | ||
| 27 | # If filepath does not exist, then terminates the program with an error. | ||
| 28 | # If filepath is a directory and the $recursive option is not set then | ||
| 29 | # terminates the program with an error, otherwise returns the list of all files | ||
| 30 | # that it contains. | ||
| 31 | sub maybe_expand_dirs { | ||
| 32 | my ($f, $options) = @_; | ||
| 33 | if (ref $f) { | ||
| 34 | # This will be the $stdin_marker reference. | ||
| 35 | if ($options->{in_place}) { | ||
| 36 | die "Reading from STDIN is incompatible with the --in-place option.\n"; | ||
| 37 | } | ||
| 38 | return $f; | ||
| 39 | } elsif (not -e $f) { | ||
| 40 | die "File does not exist: ${f}\n"; | ||
| 41 | } elsif (-d _) { | ||
| 42 | if (not $options->{recursive}) { | ||
| 43 | die "Input is a directory (did you forget the -R option?): ${f}\n"; | ||
| 44 | } | ||
| 45 | my @files; | ||
| 46 | my $filter; | ||
| 47 | if (defined $options->{input_filter}) { | ||
| 48 | $filter = $safe->reval("sub { $options->{input_filter} }"); | ||
| 49 | die "FATAL: Cannot wrap code for --input_filter: ${@}" if $@; | ||
| 50 | } | ||
| 51 | find({ | ||
| 52 | # Because of the follow option, a stat has already been done on the file, | ||
| 53 | # so the '_' magic is guaranteed to work. | ||
| 54 | wanted => sub { | ||
| 55 | if (-f _) { | ||
| 56 | if (defined $filter) { | ||
| 57 | local $_ = $_; | ||
| 58 | my $r = $filter->(); | ||
| 59 | return if warn_or_die_if_needed('Perl code failed while filtering input') || !$r; | ||
| 60 | } | ||
| 61 | push @files, $_; | ||
| 62 | } | ||
| 63 | }, | ||
| 64 | follow => 1, | ||
| 65 | no_chdir => 1, | ||
| 66 | }, | ||
| 67 | $f); | ||
| 68 | return (sort @files); | ||
| 69 | } else { | ||
| 70 | # We assume that everything else is a file. | ||
| 71 | return $f; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | sub process_all { | ||
| 76 | my ($inputs, $pre_merge_pipeline, $post_merge_pipeline, $options, $stdin) = @_; | ||
| 77 | $App::PTP::Commands::I_setter->set(1); | ||
| 78 | |||
| 79 | my $missing_final_separator = 0; | ||
| 80 | my @merged_content; | ||
| 81 | |||
| 82 | for my $file_name (@{$inputs}) { | ||
| 83 | my ($content, $missing_separator) = read_input($file_name, $options, $stdin); | ||
| 84 | $missing_final_separator = $missing_separator; | ||
| 85 | # Note that process can modify the input $file_name variable. | ||
| 86 | if (@{$pre_merge_pipeline}) { | ||
| 87 | App::PTP::Commands::process($file_name, $pre_merge_pipeline, $options, $content, | ||
| 88 | $missing_final_separator); | ||
| 89 | } | ||
| 90 | |||
| 91 | if ($options->{merge}) { | ||
| 92 | push @merged_content, @{$content}; | ||
| 93 | } else { | ||
| 94 | write_output($file_name, $content, $missing_separator, $options); | ||
| 95 | $App::PTP::Commands::I_setter->inc(); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | if ($options->{merge}) { | ||
| 100 | if (@{$post_merge_pipeline}) { | ||
| 101 | App::PTP::Commands::process(\$App::PTP::Files::merged_marker, | ||
| 102 | $post_merge_pipeline, $options, \@merged_content, $missing_final_separator); | ||
| 103 | } | ||
| 104 | write_output(\$App::PTP::Files::merged_marker, | ||
| 105 | \@merged_content, $missing_final_separator, $options); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | sub Run { | ||
| 110 | my ($stdin, $stdout, $stderr, $argv) = @_; | ||
| 111 | # All debug output is sent to STDERR, this applies inside the safe too. | ||
| 112 | select($stderr); ## no critic (ProhibitOneArgSelect) | ||
| 113 | my ($inputs, $pre_merge_pipeline, $post_merge_pipeline, $options) = | ||
| 114 | App::PTP::Args::parse_command_line($argv); | ||
| 115 | |||
| 116 | if ($options->{debug_mode}) { | ||
| 117 | print 'options = '.Dumper($options)."\n"; | ||
| 118 | print 'inputs = '.Dumper($inputs)."\n"; | ||
| 119 | print 'pre merge pipeline = '.Dumper($pre_merge_pipeline)."\n"; | ||
| 120 | print 'post merge pipeline = '.Dumper($post_merge_pipeline)."\n"; | ||
| 121 | } | ||
| 122 | |||
| 123 | @{$inputs} = map { maybe_expand_dirs($_, $options) } @{$inputs}; | ||
| 124 | print 'expanded @inputs = '.Dumper($inputs)."\n" if $options->{debug_mode}; | ||
| 125 | |||
| 126 | return if $options->{abort}; | ||
| 127 | |||
| 128 | init_global_output($options, $stdout); | ||
| 129 | process_all($inputs, $pre_merge_pipeline, $post_merge_pipeline, $options, $stdin); | ||
| 130 | close_global_output($options); | ||
| 131 | } | ||
| 132 | |||
| 133 | 1; |