line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# This module contains the function that are pushed in the pipelines and which |
2
|
|
|
|
|
|
|
# are operating on the input files. |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package App::PTP::Commands; |
5
|
|
|
|
|
|
|
|
6
|
16
|
|
|
16
|
|
256
|
use 5.022; |
|
16
|
|
|
|
|
51
|
|
7
|
16
|
|
|
16
|
|
94
|
use strict; |
|
16
|
|
|
|
|
33
|
|
|
16
|
|
|
|
|
332
|
|
8
|
16
|
|
|
15
|
|
163
|
use warnings; |
|
15
|
|
|
|
|
32
|
|
|
15
|
|
|
|
|
534
|
|
9
|
|
|
|
|
|
|
|
10
|
15
|
|
|
15
|
|
97
|
no warnings 'experimental::smartmatch'; |
|
15
|
|
|
|
|
39
|
|
|
15
|
|
|
|
|
669
|
|
11
|
15
|
|
|
15
|
|
113
|
use feature 'switch'; |
|
15
|
|
|
|
|
47
|
|
|
15
|
|
|
|
|
2437
|
|
12
|
|
|
|
|
|
|
|
13
|
15
|
|
|
15
|
|
6826
|
use App::PTP::Files qw(write_side_output read_side_input); |
|
15
|
|
|
|
|
42
|
|
|
15
|
|
|
|
|
952
|
|
14
|
15
|
|
|
15
|
|
6505
|
use App::PTP::PerlEnv; |
|
15
|
|
|
|
|
39
|
|
|
15
|
|
|
|
|
712
|
|
15
|
15
|
|
|
15
|
|
6343
|
use App::PTP::Util; |
|
15
|
|
|
|
|
41
|
|
|
15
|
|
|
|
|
502
|
|
16
|
15
|
|
|
15
|
|
101
|
use Cwd qw(abs_path); |
|
15
|
|
|
|
|
29
|
|
|
15
|
|
|
|
|
678
|
|
17
|
15
|
|
|
15
|
|
80
|
use Data::Dumper; |
|
15
|
|
|
|
|
29
|
|
|
15
|
|
|
|
|
589
|
|
18
|
15
|
|
|
15
|
|
81
|
use Exporter 'import'; |
|
15
|
|
|
|
|
34
|
|
|
15
|
|
|
|
|
383
|
|
19
|
15
|
|
|
15
|
|
7073
|
use File::Spec::Functions qw(rel2abs); |
|
15
|
|
|
|
|
14452
|
|
|
15
|
|
|
|
|
1088
|
|
20
|
15
|
|
|
15
|
|
129
|
use List::Util qw(min max); |
|
15
|
|
|
|
|
31
|
|
|
15
|
|
|
|
|
9752
|
|
21
|
15
|
|
|
15
|
|
14856
|
use Safe; |
|
15
|
|
|
|
|
566086
|
|
|
15
|
|
|
|
|
45660
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# Every public function is exported by default. |
24
|
|
|
|
|
|
|
my @all_cmd = |
25
|
|
|
|
|
|
|
qw(prepare_perl_env do_grep do_substitute do_perl |
26
|
|
|
|
|
|
|
do_execute do_load do_sort do_list_op do_tail do_head do_delete_marked |
27
|
|
|
|
|
|
|
do_insert_marked do_set_markers do_number_lines do_file_name do_line_count |
28
|
|
|
|
|
|
|
do_cut do_paste do_pivot do_tee); |
29
|
|
|
|
|
|
|
our @EXPORT_OK = (@all_cmd, 'warn_or_die_if_needed'); |
30
|
|
|
|
|
|
|
our %EXPORT_TAGS = (CMD => \@all_cmd); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
our @markers; # The current marker array, not shared in the safe. |
33
|
|
|
|
|
|
|
tie @App::PTP::PerlEnv::m, 'App::PTP::Util::MarkersArray'; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
my $f_setter = tie $App::PTP::PerlEnv::f, 'App::PTP::Util::ReadOnlyVar'; |
36
|
|
|
|
|
|
|
my $F_setter = tie $App::PTP::PerlEnv::F, 'App::PTP::Util::ReadOnlyVar'; |
37
|
|
|
|
|
|
|
my $n_setter = tie $App::PTP::PerlEnv::n, 'App::PTP::Util::ReadOnlyVar'; |
38
|
|
|
|
|
|
|
my $N_setter = tie $App::PTP::PerlEnv::N, 'App::PTP::Util::ReadOnlyVar'; |
39
|
|
|
|
|
|
|
my $m_setter = tie $App::PTP::PerlEnv::m, 'App::PTP::Util::AliasVar'; |
40
|
|
|
|
|
|
|
# This variable is public because it is set in the App::PTP::process_all method. |
41
|
|
|
|
|
|
|
our $I_setter = tie $App::PTP::PerlEnv::I, 'App::PTP::Util::ReadOnlyVar'; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# This 'safe' provides an isolated environment for all the regex and perl code |
44
|
|
|
|
|
|
|
# execution provided by the user. |
45
|
|
|
|
|
|
|
my $safe; # Sets to Safe->new() before each new file processed; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# Prepare the $safe variable so that it can execute code with access to our |
48
|
|
|
|
|
|
|
# PerlEnv. |
49
|
|
|
|
|
|
|
sub new_safe { |
50
|
31
|
|
|
31
|
0
|
52
|
my ($options) = @_; |
51
|
31
|
100
|
100
|
|
|
102
|
if ($safe and $options->{preserve_perl_env} and |
|
|
|
66
|
|
|
|
|
52
|
|
|
|
|
|
|
$safe->reval('$PerlEnv_LOADED')) { |
53
|
2
|
50
|
|
|
|
1066
|
print "Skipping creation of new safe.\n" if $options->{debug_mode}; |
54
|
2
|
|
|
|
|
5
|
return; |
55
|
|
|
|
|
|
|
} |
56
|
29
|
50
|
|
|
|
85
|
print "Creating a new safe.\n" if $options->{debug_mode}; |
57
|
29
|
|
|
|
|
146
|
$safe = Safe->new(); |
58
|
29
|
|
|
|
|
29950
|
$safe->share_from('App::PTP::PerlEnv', \@App::PTP::PerlEnv::EXPORT_OK); |
59
|
29
|
100
|
|
|
|
6827
|
if ($options->{use_safe} > 1) { |
60
|
14
|
|
|
|
|
47
|
$safe->permit_only(qw(:base_core :base_mem :base_loop :base_math :base_orig |
61
|
|
|
|
|
|
|
:load)); |
62
|
14
|
|
|
|
|
136
|
$safe->deny(qw(tie untie bless)); |
63
|
|
|
|
|
|
|
} else { |
64
|
15
|
|
|
|
|
43
|
$safe->deny_only(qw(:subprocess :ownprocess :others :dangerous)); |
65
|
|
|
|
|
|
|
} |
66
|
29
|
50
|
|
|
|
258
|
if ($@) { |
67
|
0
|
|
|
|
|
0
|
chomp($@); |
68
|
0
|
|
|
|
|
0
|
die "INTERNAL ERROR: cannot load the PerlEnv module: ${@}\n"; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# Prepare an App::PTP::SafeEnv package with access to the PerlEnv one and |
73
|
|
|
|
|
|
|
# nothing else. |
74
|
|
|
|
|
|
|
sub reset_safe_env { |
75
|
101
|
|
|
101
|
0
|
218
|
my ($options) = @_; |
76
|
101
|
50
|
66
|
|
|
313
|
if ($options->{preserve_perl_env} and $App::PTP::SafeEnv::PerlEnv_LOADED) { |
77
|
2
|
50
|
|
|
|
9
|
print "Skipping reset of perl environment.\n" if $options->{debug_mode}; |
78
|
2
|
|
|
|
|
5
|
return; |
79
|
|
|
|
|
|
|
} |
80
|
99
|
50
|
|
|
|
327
|
print "Reseting the perl environment.\n" if $options->{debug_mode}; |
81
|
|
|
|
|
|
|
# We can't undef the hash, otherwise the code compiled in the eval below no |
82
|
|
|
|
|
|
|
# longer refers to the same package (this would work however with a |
83
|
|
|
|
|
|
|
# string-eval instead of a block-eval as the code would be compiled and |
84
|
|
|
|
|
|
|
# create a new package hash). |
85
|
99
|
|
|
|
|
300
|
%App::PTP::SafeEnv:: = (); |
86
|
99
|
|
|
|
|
177
|
eval { |
87
|
|
|
|
|
|
|
package App::PTP::SafeEnv; |
88
|
99
|
|
|
|
|
12835
|
App::PTP::PerlEnv->import(':all'); |
89
|
99
|
|
|
|
|
329
|
our $PerlEnv_LOADED = 1; # For some reason the import does not work |
90
|
|
|
|
|
|
|
}; |
91
|
99
|
50
|
|
|
|
358
|
if ($@) { |
92
|
0
|
|
|
|
|
0
|
chomp($@); |
93
|
0
|
|
|
|
|
0
|
die "INTERNAL ERROR: cannot prepare the SafeEnv package: ${@}\n"; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# Delete the PerlEnv (both the safe and the eval based one). This method is only |
98
|
|
|
|
|
|
|
# meant to be called from tests. |
99
|
|
|
|
|
|
|
sub delete_perl_env { |
100
|
118
|
|
|
118
|
0
|
2131666
|
undef $safe; |
101
|
118
|
|
|
|
|
3955
|
%App::PTP::SafeEnv:: = (); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# Should be called for each new file so that the environment seen by the user |
105
|
|
|
|
|
|
|
# supplied Perl code is empty. |
106
|
|
|
|
|
|
|
sub prepare_perl_env { |
107
|
132
|
|
|
132
|
0
|
317
|
my ($file_name, $options) = @_; |
108
|
132
|
100
|
|
|
|
302
|
if (ref($file_name)) { |
109
|
113
|
|
|
|
|
409
|
$f_setter->set('-'); |
110
|
113
|
|
|
|
|
253
|
$F_setter->set('-'); |
111
|
|
|
|
|
|
|
} else { |
112
|
19
|
|
|
|
|
77
|
$f_setter->set($file_name); |
113
|
19
|
|
|
|
|
551
|
$F_setter->set(abs_path($file_name)); |
114
|
|
|
|
|
|
|
} |
115
|
132
|
100
|
|
|
|
361
|
if ($options->{use_safe} > 0) { |
116
|
31
|
|
|
|
|
79
|
new_safe($options); |
117
|
|
|
|
|
|
|
} else { |
118
|
101
|
|
|
|
|
244
|
reset_safe_env($options); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# process($file_name, \@pipeline, \%options, \@content, $missing_final_sep) |
123
|
|
|
|
|
|
|
# Applies all the stage of the pipeline on the given content (which is modified |
124
|
|
|
|
|
|
|
# in place). |
125
|
|
|
|
|
|
|
sub process { |
126
|
132
|
|
|
132
|
0
|
377
|
my ($file_name, $pipeline, $options, $content, $missing_final_separator) = @_; |
127
|
132
|
50
|
|
|
|
359
|
if ($options->{debug_mode}) { |
128
|
|
|
|
|
|
|
# For long files, we print only the first and last lines. |
129
|
132
|
|
|
|
|
407
|
my @debug_content = @$content; |
130
|
132
|
|
|
|
|
632
|
my $omit_msg = sprintf "... (%d lines omitted)", (@$content - 8); |
131
|
132
|
100
|
|
|
|
347
|
splice @debug_content, 4, -4, $omit_msg if @$content > 10; |
132
|
132
|
100
|
|
|
|
334
|
if (ref($file_name)) { |
133
|
113
|
|
|
|
|
457
|
print "Processing $${file_name} with content: ".Dumper(\@debug_content); |
134
|
|
|
|
|
|
|
} else { |
135
|
19
|
|
|
|
|
91
|
print "Processing '${file_name}' with content: ".Dumper(\@debug_content); |
136
|
|
|
|
|
|
|
} |
137
|
132
|
100
|
|
|
|
8201
|
print "Has final separator: ".($missing_final_separator ? 'false' : 'true')."\n"; |
138
|
|
|
|
|
|
|
} |
139
|
132
|
|
|
|
|
419
|
prepare_perl_env($file_name, $options); |
140
|
132
|
|
|
|
|
423
|
@markers = (0) x scalar(@$content); |
141
|
132
|
|
|
|
|
259
|
my $markers = \@markers; |
142
|
132
|
|
|
|
|
340
|
for my $stage (@$pipeline) { |
143
|
178
|
|
|
|
|
1351
|
my ($command, $code, $modes, @args) = @$stage; |
144
|
178
|
|
|
|
|
665
|
$N_setter->set(scalar(@$content)); |
145
|
178
|
|
|
|
|
445
|
$modes->{missing_final_separator} = $missing_final_separator; |
146
|
178
|
|
|
|
|
388
|
$modes->{file_name_ref} = \$_[0]; # this is an alias to the passed value. |
147
|
178
|
50
|
|
|
|
857
|
if ($options->{debug_mode}) { |
148
|
178
|
|
|
|
|
312
|
local $Data::Dumper::Indent = 0; |
149
|
|
|
|
|
|
|
printf "Executing command: %s(%s).\n", $command, |
150
|
178
|
|
|
|
|
381
|
join(', ', map { Dumper($_) } @args); |
|
250
|
|
|
|
|
4002
|
|
151
|
|
|
|
|
|
|
} |
152
|
178
|
|
|
|
|
7642
|
&$code($content, $markers, $modes, $options, @args); |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub base_prepare_re { |
157
|
7
|
|
|
7
|
0
|
14
|
my ($re, $modes) = @_; |
158
|
7
|
50
|
|
|
|
24
|
if ($modes->{quote_regex}) { |
159
|
0
|
|
|
|
|
0
|
$re = quotemeta($re); |
160
|
|
|
|
|
|
|
} |
161
|
7
|
50
|
|
|
|
19
|
if (not $modes->{case_sensitive}) { |
162
|
0
|
|
|
|
|
0
|
$re = '(?i)'.$re; |
163
|
|
|
|
|
|
|
} |
164
|
7
|
|
|
|
|
17
|
return $re; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
# prepare_re('re', \%options) |
168
|
|
|
|
|
|
|
# Applies the modal option on the given regex. |
169
|
|
|
|
|
|
|
# This function is not exported. |
170
|
|
|
|
|
|
|
sub prepare_re { |
171
|
7
|
|
|
7
|
0
|
17
|
my ($re, $modes) = @_; |
172
|
7
|
|
|
|
|
20
|
$re = base_prepare_re($re, $modes); |
173
|
7
|
|
|
|
|
14
|
my $r; |
174
|
7
|
50
|
|
|
|
33
|
if ($modes->{regex_engine} ne 'perl') { |
175
|
|
|
|
|
|
|
# Some play to correctly escape whetever special characters might be in the |
176
|
|
|
|
|
|
|
# regex while preserving its semantics. This relies on the fact that the |
177
|
|
|
|
|
|
|
# 'Terse' option of Data::Dumper is set in the main program. |
178
|
|
|
|
|
|
|
# The regex-engine variable has been validated in the Args module. |
179
|
0
|
|
|
|
|
0
|
my $str_re = Dumper($re); |
180
|
0
|
|
|
|
|
0
|
$r = eval "use re::engine::$modes->{regex_engine}; |
181
|
|
|
|
|
|
|
\$re = $str_re; |
182
|
|
|
|
|
|
|
qr/\$re/s "; |
183
|
0
|
0
|
|
|
|
0
|
if ($@) { |
184
|
0
|
|
|
|
|
0
|
chomp($@); |
185
|
0
|
|
|
|
|
0
|
die "FATAL: Cannot use the specified regex engine: ${@}\n"; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
} else { |
188
|
7
|
|
|
|
|
115
|
$r = qr/$re/s; |
189
|
|
|
|
|
|
|
} |
190
|
7
|
|
|
|
|
24
|
return $r; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
sub quote_for_re { |
194
|
75
|
|
|
75
|
0
|
133
|
my ($text, $modes) = @_; |
195
|
75
|
100
|
|
|
|
192
|
if ($modes->{quote_regex}) { |
196
|
7
|
|
|
|
|
24
|
return quotemeta($text); |
197
|
|
|
|
|
|
|
} else { |
198
|
|
|
|
|
|
|
# We quote just the '{' or '}' characters. |
199
|
68
|
|
|
|
|
1333
|
return $text =~ s/(\{|\})/\\$1/gr; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
sub prepare_re2 { |
204
|
56
|
|
|
56
|
0
|
113
|
my ($re, $modes) = @_; |
205
|
56
|
|
|
|
|
127
|
$re = quote_for_re($re, $modes); |
206
|
56
|
100
|
|
|
|
163
|
if (not $modes->{case_sensitive}) { |
207
|
1
|
|
|
|
|
3
|
$re = '(?i)'.$re; |
208
|
|
|
|
|
|
|
} |
209
|
56
|
|
|
|
|
89
|
my $use_statement = ''; |
210
|
56
|
50
|
|
|
|
177
|
if ($modes->{regex_engine} ne 'perl') { |
211
|
0
|
|
|
|
|
0
|
$use_statement = "use re::engine::$modes->{regex_engine};"; |
212
|
|
|
|
|
|
|
} |
213
|
56
|
|
|
|
|
205
|
return ($use_statement, "{${re}}"); |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
sub do_grep { |
217
|
37
|
|
|
37
|
0
|
107
|
my ($content, $markers, $modes, $options, $re) = @_; |
218
|
37
|
|
|
|
|
91
|
my ($use_stmt, $quoted_re) = prepare_re2($re, $modes); |
219
|
37
|
50
|
|
|
|
141
|
print "\$re = ${quoted_re}\n" if $options->{debug_mode}; |
220
|
37
|
|
|
|
|
127
|
my $wrapped = get_code_in_safe_env( |
221
|
|
|
|
|
|
|
"{; ${use_stmt} undef \$_ unless m ${quoted_re} }", $options, '--grep'); |
222
|
37
|
|
|
|
|
95
|
$. = 0; |
223
|
37
|
|
|
|
|
88
|
map { $m_setter->set(\$markers->[$.]); |
|
218
|
|
|
|
|
15643
|
|
224
|
218
|
|
|
|
|
757
|
$n_setter->set($.++); |
225
|
218
|
|
|
|
|
3144
|
$wrapped->() } @$content; |
226
|
|
|
|
|
|
|
# This code is duplicated from do_perl: |
227
|
37
|
|
|
|
|
4828
|
for my $i (0 .. $#$content) { |
228
|
218
|
100
|
|
|
|
440
|
if (not defined $content->[$i]) { |
|
|
50
|
|
|
|
|
|
229
|
141
|
|
|
|
|
214
|
undef $markers->[$i]; |
230
|
|
|
|
|
|
|
} elsif (not defined $markers->[$i]) { |
231
|
0
|
|
|
|
|
0
|
$markers->[$i] = ''; # We don't want undef here, as we will filter on it. |
232
|
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
|
} |
234
|
37
|
|
|
|
|
74
|
@$content = grep { defined } @$content; |
|
218
|
|
|
|
|
413
|
|
235
|
37
|
|
|
|
|
71
|
@$markers = grep { defined } @$markers; |
|
218
|
|
|
|
|
705
|
|
236
|
|
|
|
|
|
|
} |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
sub do_substitute { |
239
|
19
|
|
|
19
|
0
|
53
|
my ($content, $markers, $modes, $options, $re, $subst) = @_; |
240
|
19
|
50
|
|
|
|
56
|
if ($options->{debug_mode}) { |
241
|
19
|
|
|
|
|
66
|
print "Before: \$re = ${re}; \$subst = ${subst}\n"; |
242
|
|
|
|
|
|
|
} |
243
|
19
|
|
|
|
|
46
|
my ($use_stmt, $quoted_re) = prepare_re2($re, $modes); |
244
|
19
|
|
|
|
|
49
|
my $quoted_subst = quote_for_re($subst, $modes); |
245
|
19
|
100
|
|
|
|
75
|
my $g = $modes->{global_match} ? 'g' : ''; |
246
|
19
|
50
|
|
|
|
46
|
if ($options->{debug_mode}) { |
247
|
19
|
|
|
|
|
62
|
print "After: \$re = ${quoted_re}; \$subst = ${quoted_subst}\n"; |
248
|
|
|
|
|
|
|
} |
249
|
19
|
|
|
|
|
79
|
my $wrapped = get_code_in_safe_env( |
250
|
|
|
|
|
|
|
"; ${use_stmt} s ${quoted_re}{${quoted_subst}}${g}", $options, |
251
|
|
|
|
|
|
|
'--substitute'); |
252
|
19
|
|
|
|
|
69
|
$. = 0; |
253
|
19
|
|
|
|
|
49
|
map { $m_setter->set(\$markers->[$.]); |
|
73
|
|
|
|
|
4434
|
|
254
|
73
|
|
|
|
|
263
|
$n_setter->set($.++); |
255
|
73
|
|
|
|
|
1160
|
$wrapped->() } @$content; |
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
sub warn_or_die_if_needed { |
259
|
215
|
|
|
215
|
0
|
773
|
my ($text, $modes) = @_; |
260
|
215
|
100
|
|
|
|
905
|
return 0 unless $@; |
261
|
3
|
|
|
|
|
9
|
chomp($@); |
262
|
3
|
50
|
|
|
|
9
|
if ($modes->{fatal_error}) { |
263
|
0
|
|
|
|
|
0
|
die "FATAL: ${text}: ${@}\n"; |
264
|
|
|
|
|
|
|
} else { |
265
|
3
|
|
|
|
|
17
|
print "WARNING: ${text}: ${@}\n"; |
266
|
|
|
|
|
|
|
} |
267
|
3
|
|
|
|
|
8
|
return 1; |
268
|
|
|
|
|
|
|
} |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
sub eval_in_safe_env { |
271
|
143
|
|
|
143
|
0
|
282
|
my ($code, $options) = @_; |
272
|
143
|
50
|
|
|
|
362
|
if ($options->{debug_mode} > 1) { |
273
|
0
|
|
|
|
|
0
|
print "Evaluating the following code: ${code}\n"; |
274
|
|
|
|
|
|
|
} |
275
|
143
|
100
|
|
|
|
338
|
if ($options->{use_safe} > 0) { |
276
|
61
|
|
|
|
|
176
|
return $safe->reval($code); |
277
|
|
|
|
|
|
|
} else { |
278
|
82
|
|
|
7
|
|
7342
|
return eval("package App::PTP::SafeEnv; |
|
7
|
|
|
6
|
|
53
|
|
|
7
|
|
|
6
|
|
15
|
|
|
7
|
|
|
|
|
203
|
|
|
7
|
|
|
|
|
38
|
|
|
7
|
|
|
|
|
15
|
|
|
7
|
|
|
|
|
634
|
|
|
6
|
|
|
|
|
45
|
|
|
6
|
|
|
|
|
16
|
|
|
6
|
|
|
|
|
163
|
|
|
6
|
|
|
|
|
33
|
|
|
6
|
|
|
|
|
17
|
|
|
6
|
|
|
|
|
448
|
|
|
6
|
|
|
|
|
46
|
|
|
6
|
|
|
|
|
15
|
|
|
6
|
|
|
|
|
181
|
|
|
6
|
|
|
|
|
34
|
|
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
376
|
|
|
6
|
|
|
|
|
48
|
|
|
6
|
|
|
|
|
19
|
|
|
6
|
|
|
|
|
168
|
|
|
6
|
|
|
|
|
31
|
|
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
464
|
|
|
4
|
|
|
|
|
39
|
|
|
4
|
|
|
|
|
16
|
|
|
4
|
|
|
|
|
129
|
|
|
4
|
|
|
|
|
22
|
|
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
295
|
|
|
4
|
|
|
|
|
46
|
|
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
101
|
|
|
4
|
|
|
|
|
77
|
|
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
300
|
|
|
4
|
|
|
|
|
30
|
|
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
177
|
|
|
4
|
|
|
|
|
25
|
|
|
4
|
|
|
|
|
62
|
|
|
4
|
|
|
|
|
391
|
|
|
4
|
|
|
|
|
70
|
|
|
4
|
|
|
|
|
13
|
|
|
4
|
|
|
|
|
116
|
|
|
4
|
|
|
|
|
24
|
|
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
288
|
|
|
4
|
|
|
|
|
29
|
|
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
164
|
|
|
4
|
|
|
|
|
21
|
|
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
312
|
|
|
4
|
|
|
|
|
30
|
|
|
4
|
|
|
|
|
13
|
|
|
4
|
|
|
|
|
118
|
|
|
4
|
|
|
|
|
21
|
|
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
303
|
|
|
4
|
|
|
|
|
29
|
|
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
102
|
|
|
4
|
|
|
|
|
19
|
|
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
297
|
|
|
3
|
|
|
|
|
22
|
|
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
85
|
|
|
3
|
|
|
|
|
16
|
|
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
160
|
|
|
3
|
|
|
|
|
23
|
|
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
70
|
|
|
3
|
|
|
|
|
16
|
|
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
245
|
|
|
7
|
|
|
|
|
52
|
|
|
7
|
|
|
|
|
70
|
|
|
7
|
|
|
|
|
191
|
|
|
7
|
|
|
|
|
36
|
|
|
7
|
|
|
|
|
14
|
|
|
7
|
|
|
|
|
457
|
|
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
28
|
|
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
68
|
|
279
|
|
|
|
|
|
|
no strict; |
280
|
|
|
|
|
|
|
no warnings; |
281
|
|
|
|
|
|
|
${code}"); |
282
|
|
|
|
|
|
|
} |
283
|
|
|
|
|
|
|
} |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
sub get_code_in_safe_env { |
286
|
120
|
|
|
120
|
0
|
255
|
my ($code, $options, $cmd) = @_; |
287
|
120
|
|
|
|
|
333
|
my $wrapped_code = eval_in_safe_env("sub { ${code} }", $options); |
288
|
120
|
50
|
|
|
|
28920
|
die "FATAL: Cannot wrap code for ${cmd}: ${@}" if $@; |
289
|
120
|
|
|
|
|
307
|
return $wrapped_code; |
290
|
|
|
|
|
|
|
} |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
sub do_perl { |
293
|
59
|
|
|
59
|
0
|
162
|
my ($content, $markers, $modes, $options, $cmd, $code) = @_; |
294
|
59
|
|
|
|
|
137
|
$. = 0; |
295
|
59
|
|
|
|
|
299
|
my $scmd = '-'.($cmd =~ s/^(..)/-$1/r); # --perl or -n. |
296
|
59
|
|
|
|
|
164
|
my $wrapped_code = get_code_in_safe_env($code, $options, $scmd); |
297
|
|
|
|
|
|
|
my @result = map { |
298
|
59
|
|
|
|
|
131
|
$m_setter->set(\$markers->[$.]); |
|
200
|
|
|
|
|
817
|
|
299
|
200
|
|
|
|
|
671
|
$n_setter->set(++$.); |
300
|
200
|
|
|
|
|
355
|
my $input = $_; |
301
|
|
|
|
|
|
|
# Among other things, this ensures that the code is always executed in |
302
|
|
|
|
|
|
|
# a scalar context. |
303
|
200
|
|
|
|
|
291
|
my $r = eval { $wrapped_code->() }; |
|
200
|
|
|
|
|
2373
|
|
304
|
|
|
|
|
|
|
# We can't use return as we're not in a sub. |
305
|
200
|
100
|
|
|
|
28792
|
if (warn_or_die_if_needed("Perl code failed in ${scmd}", $modes)) { |
306
|
3
|
|
|
|
|
4
|
given ($cmd) { |
307
|
3
|
|
|
|
|
11
|
1 when 'filter'; |
308
|
2
|
|
|
|
|
10
|
$input when 'n'; |
309
|
1
|
|
|
|
|
3
|
$markers[$.] when 'mark-line'; |
310
|
|
|
|
|
|
|
} |
311
|
|
|
|
|
|
|
} else { |
312
|
197
|
|
|
|
|
527
|
$r; |
313
|
|
|
|
|
|
|
} |
314
|
|
|
|
|
|
|
} @$content; |
315
|
|
|
|
|
|
|
|
316
|
59
|
|
|
|
|
180
|
$n_setter->set(undef); |
317
|
59
|
|
|
|
|
158
|
$m_setter->set(\undef); |
318
|
|
|
|
|
|
|
|
319
|
59
|
100
|
|
|
|
217
|
if ($cmd eq 'perl') { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
320
|
|
|
|
|
|
|
# Do nothing with the result. |
321
|
|
|
|
|
|
|
} elsif ($cmd eq 'n') { |
322
|
28
|
|
|
|
|
103
|
@$content = @result; |
323
|
|
|
|
|
|
|
} elsif ($cmd eq 'filter') { |
324
|
6
|
|
|
|
|
25
|
for my $i (0 .. $#$content) { |
325
|
23
|
100
|
100
|
|
|
143
|
if (!$result[$i] xor $modes->{inverse_match}) { |
326
|
12
|
|
|
|
|
39
|
undef $content->[$i]; |
327
|
|
|
|
|
|
|
} |
328
|
|
|
|
|
|
|
} |
329
|
|
|
|
|
|
|
} elsif ($cmd eq 'mark-line') { |
330
|
7
|
|
|
|
|
26
|
@$markers = @result; |
331
|
|
|
|
|
|
|
} else { |
332
|
0
|
|
|
|
|
0
|
die "FATAL: Invalid command received for perl operation ($cmd).\n"; |
333
|
|
|
|
|
|
|
} |
334
|
|
|
|
|
|
|
|
335
|
59
|
|
|
|
|
182
|
for my $i (0 .. $#$content) { |
336
|
200
|
100
|
|
|
|
502
|
if (not defined $content->[$i]) { |
|
|
100
|
|
|
|
|
|
337
|
12
|
|
|
|
|
23
|
undef $markers->[$i]; |
338
|
|
|
|
|
|
|
} elsif (not defined $markers->[$i]) { |
339
|
6
|
|
|
|
|
14
|
$markers->[$i] = ''; # We don't want undef here, as we will filter on it. |
340
|
|
|
|
|
|
|
} |
341
|
|
|
|
|
|
|
} |
342
|
59
|
|
|
|
|
120
|
@$content = grep { defined } @$content; |
|
200
|
|
|
|
|
433
|
|
343
|
59
|
|
|
|
|
105
|
@$markers = grep { defined } @$markers; |
|
200
|
|
|
|
|
1026
|
|
344
|
|
|
|
|
|
|
} |
345
|
|
|
|
|
|
|
|
346
|
|
|
|
|
|
|
sub do_execute { |
347
|
19
|
|
|
19
|
0
|
75
|
my ($content, $markers, $modes, $options, $code) = @_; |
348
|
19
|
|
|
|
|
56
|
eval_in_safe_env($code, $options); |
349
|
19
|
100
|
|
|
|
23424
|
if ($@) { |
350
|
2
|
|
|
|
|
6
|
chomp($@); |
351
|
2
|
|
|
|
|
59
|
die "FATAL: Perl code failed in --execute: ${@}\n"; |
352
|
|
|
|
|
|
|
} |
353
|
|
|
|
|
|
|
} |
354
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
sub do_load { |
356
|
3
|
|
|
3
|
0
|
11
|
my ($content, $markers, $modes, $options, $file) = @_; |
357
|
|
|
|
|
|
|
# do can open relative paths, but in that case it looks them up in the @INC |
358
|
|
|
|
|
|
|
# directory, which we want to avoid. |
359
|
|
|
|
|
|
|
# We don't use abs_path here to not die (just yet) if the file does not exist. |
360
|
3
|
|
|
|
|
15
|
my $abs_path = rel2abs($file); |
361
|
3
|
50
|
|
|
|
164
|
print "Loading file: '$abs_path'\n" if $options->{debug_mode}; |
362
|
3
|
50
|
|
|
|
16
|
if (not defined eval_in_safe_env("do '${abs_path}';", $options)) { |
363
|
0
|
0
|
|
|
|
0
|
if ($@) { |
|
|
0
|
|
|
|
|
|
364
|
0
|
|
|
|
|
0
|
die "FATAL: Perl code failed in --load: ${@}\n"; |
365
|
|
|
|
|
|
|
} elsif ($!) { |
366
|
0
|
|
|
|
|
0
|
die "FATAL: Cannot load file '$file' for --load: $!\n"; |
367
|
|
|
|
|
|
|
} |
368
|
|
|
|
|
|
|
} |
369
|
|
|
|
|
|
|
} |
370
|
|
|
|
|
|
|
|
371
|
|
|
|
|
|
|
sub do_sort { |
372
|
7
|
|
|
7
|
0
|
66
|
my ($content, $markers, $modes, $options) = @_; |
373
|
7
|
50
|
|
|
|
39
|
if (ref($modes->{comparator}) eq 'CODE') { |
|
|
100
|
|
|
|
|
|
374
|
|
|
|
|
|
|
# This branch is no longer used. |
375
|
0
|
|
|
|
|
0
|
@$content = sort { $modes->{comparator}() } @$content; |
|
0
|
|
|
|
|
0
|
|
376
|
|
|
|
|
|
|
} elsif (ref($modes->{comparator}) eq 'SCALAR') { |
377
|
5
|
100
|
|
|
|
8
|
if (${$modes->{comparator}} eq 'default') { |
|
5
|
100
|
|
|
|
16
|
|
|
|
50
|
|
|
|
|
|
378
|
3
|
|
|
|
|
22
|
@$content = sort @$content; |
379
|
2
|
|
|
|
|
9
|
} elsif (${$modes->{comparator}} eq 'numeric') { |
380
|
15
|
|
|
15
|
|
151
|
no warnings "numeric"; |
|
15
|
|
|
|
|
34
|
|
|
15
|
|
|
|
|
1301
|
|
381
|
1
|
|
|
|
|
4
|
@$content = sort { $a <=> $b } @$content; |
|
5
|
|
|
|
|
18
|
|
382
|
1
|
|
|
|
|
6
|
} elsif (${$modes->{comparator}} eq 'locale') { |
383
|
15
|
|
|
15
|
|
7164
|
use locale; |
|
15
|
|
|
|
|
7811
|
|
|
15
|
|
|
|
|
98
|
|
384
|
1
|
|
|
|
|
7
|
@$content = sort { $a cmp $b } @$content; |
|
21
|
|
|
|
|
34
|
|
385
|
|
|
|
|
|
|
} else { |
386
|
|
|
|
|
|
|
die sprintf "INTERNAL ERROR: Invalid comparator (%s)\n.", |
387
|
0
|
|
|
|
|
0
|
${$modes->{comparator}}; |
|
0
|
|
|
|
|
0
|
|
388
|
|
|
|
|
|
|
} |
389
|
|
|
|
|
|
|
} else { |
390
|
|
|
|
|
|
|
die sprintf "INTERNAL ERROR: Invalid comparator type (%s)\n.", |
391
|
2
|
50
|
|
|
|
6
|
Dumper($modes->{comparator}) if ref $modes->{comparator}; |
392
|
2
|
|
|
|
|
5
|
my $cmp = $modes->{comparator}; |
393
|
2
|
|
|
|
|
9
|
my $sort = get_code_in_safe_env("sort { $cmp } \@_", $options, |
394
|
|
|
|
|
|
|
'custom comparator'); |
395
|
2
|
|
|
|
|
55
|
@$content = $sort->(@$content); |
396
|
|
|
|
|
|
|
} |
397
|
7
|
|
|
|
|
45
|
@$markers = (0) x scalar(@$content); |
398
|
|
|
|
|
|
|
} |
399
|
|
|
|
|
|
|
|
400
|
|
|
|
|
|
|
sub do_list_op { |
401
|
0
|
|
|
0
|
0
|
0
|
my ($content, $markers, $modes, $options, $sub, $apply_on_markers) = @_; |
402
|
0
|
|
|
|
|
0
|
@$content = &$sub(@$content); |
403
|
0
|
0
|
|
|
|
0
|
if ($apply_on_markers) { |
404
|
0
|
|
|
|
|
0
|
@$markers = &$sub(@$markers); |
405
|
|
|
|
|
|
|
} else { |
406
|
0
|
|
|
|
|
0
|
@$markers = (0) x scalar(@$content); |
407
|
|
|
|
|
|
|
} |
408
|
|
|
|
|
|
|
} |
409
|
|
|
|
|
|
|
|
410
|
|
|
|
|
|
|
sub do_tail { |
411
|
4
|
|
|
4
|
0
|
12
|
my ($content, $markers, $modes, $options, $len) = @_; |
412
|
4
|
100
|
|
|
|
13
|
$len = 10 unless $len; |
413
|
4
|
|
|
|
|
12
|
splice @$content, 0, -$len; |
414
|
4
|
|
|
|
|
18
|
splice @$markers, 0, -$len; |
415
|
|
|
|
|
|
|
} |
416
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
sub do_head { |
418
|
4
|
|
|
4
|
0
|
12
|
my ($content, $markers, $modes, $options, $len) = @_; |
419
|
4
|
100
|
|
|
|
12
|
$len = 10 unless $len; |
420
|
4
|
100
|
|
|
|
12
|
$len = -@$content if $len < -@$content; |
421
|
4
|
|
|
|
|
10
|
splice @$content, $len; |
422
|
4
|
|
|
|
|
18
|
splice @$markers, $len; |
423
|
|
|
|
|
|
|
} |
424
|
|
|
|
|
|
|
|
425
|
|
|
|
|
|
|
sub do_delete_marked { |
426
|
|
|
|
|
|
|
# negative offset if we're deleting a line before the marker. |
427
|
3
|
|
|
3
|
0
|
16
|
my ($content, $markers, $modes, $options, $offset) = @_; |
428
|
3
|
|
|
|
|
32
|
my $start = min(max(0, -$offset), $#$content); |
429
|
3
|
|
|
|
|
21
|
my $end = max(min($#$content, $#$content + $offset), $#$content); |
430
|
3
|
|
|
|
|
12
|
my @markers_temp = @$markers; # So that we can read it even after an undef. |
431
|
3
|
|
|
|
|
9
|
for my $i ($start .. $end) { |
432
|
12
|
100
|
|
|
|
32
|
if ($markers_temp[$i]) { |
433
|
6
|
|
|
|
|
14
|
undef $content->[$i + $offset]; |
434
|
6
|
|
|
|
|
14
|
undef $markers->[$i + $offset]; |
435
|
|
|
|
|
|
|
} |
436
|
|
|
|
|
|
|
} |
437
|
3
|
|
|
|
|
9
|
@$content = grep { defined } @$content; |
|
12
|
|
|
|
|
29
|
|
438
|
3
|
|
|
|
|
9
|
@$markers = grep { defined } @$markers; |
|
12
|
|
|
|
|
31
|
|
439
|
|
|
|
|
|
|
} |
440
|
|
|
|
|
|
|
|
441
|
|
|
|
|
|
|
sub do_insert_marked { |
442
|
|
|
|
|
|
|
# negative offset if we're inserting a line before the marker. |
443
|
3
|
|
|
3
|
0
|
14
|
my ($content, $markers, $modes, $options, $offset, $line) = @_; |
444
|
3
|
|
|
|
|
13
|
my @markers_temp = @$markers; |
445
|
3
|
|
|
|
|
10
|
my @content_temp = @$content; |
446
|
3
|
|
|
|
|
6
|
my $added = 0; |
447
|
3
|
|
|
|
|
6
|
my $wrapped; |
448
|
3
|
50
|
|
|
|
11
|
if (not $modes->{quote_regex}) { |
449
|
3
|
|
|
|
|
14
|
$wrapped = get_code_in_safe_env("\"${line}\"", $options, |
450
|
|
|
|
|
|
|
'--insert-marked'); |
451
|
|
|
|
|
|
|
} |
452
|
3
|
|
|
|
|
23
|
for my $i (0 .. $#content_temp) { |
453
|
12
|
100
|
|
|
|
76
|
next unless $markers_temp[$i]; |
454
|
6
|
|
|
|
|
9
|
my $r; |
455
|
6
|
50
|
|
|
|
20
|
if ($modes->{quote_regex}) { |
456
|
0
|
|
|
|
|
0
|
$r = $line; |
457
|
|
|
|
|
|
|
} else { |
458
|
6
|
|
|
|
|
13
|
$_ = $content_temp[$i]; |
459
|
6
|
|
|
|
|
22
|
$n_setter->set($i + 1); |
460
|
6
|
|
|
|
|
19
|
$m_setter->set($markers_temp[$i]); |
461
|
6
|
|
|
|
|
14
|
$line =~ s/(?:[^\\]|^)((:?\\\\)*")/\\$1/g; |
462
|
6
|
|
|
|
|
11
|
$r = eval { $wrapped->() }; |
|
6
|
|
|
|
|
55
|
|
463
|
6
|
50
|
|
|
|
1220
|
next if warn_or_die_if_needed( |
464
|
|
|
|
|
|
|
'String interpolation failed in --insert-marked', $modes); |
465
|
|
|
|
|
|
|
# it should not be possible for the result to be undef... |
466
|
|
|
|
|
|
|
} |
467
|
|
|
|
|
|
|
# We never insert at a negative offset or before the previously added line. |
468
|
|
|
|
|
|
|
# Offset = 0 means we're inserting after the current line. |
469
|
6
|
|
|
|
|
16
|
my $target = $i + $offset + 1 + $added; |
470
|
6
|
50
|
|
|
|
14
|
$target = $added if $target < $added; |
471
|
6
|
50
|
|
|
|
29
|
$target = @$content if $target > @$content; |
472
|
6
|
|
|
|
|
10
|
++$added; |
473
|
6
|
|
|
|
|
18
|
splice @$content, $target, 0, $r; |
474
|
6
|
|
|
|
|
17
|
splice @$markers, $target, 0, 0; |
475
|
|
|
|
|
|
|
} |
476
|
|
|
|
|
|
|
} |
477
|
|
|
|
|
|
|
|
478
|
|
|
|
|
|
|
sub do_set_markers { |
479
|
0
|
|
|
0
|
0
|
0
|
my ($content, $markers, $modes, $options, $value) = @_; |
480
|
0
|
|
|
|
|
0
|
@$markers = ($value)x scalar(@$content); |
481
|
|
|
|
|
|
|
} |
482
|
|
|
|
|
|
|
|
483
|
|
|
|
|
|
|
sub do_number_lines { |
484
|
0
|
|
|
0
|
0
|
0
|
my ($content, $markers, $modes, $options) = @_; |
485
|
0
|
|
|
|
|
0
|
my $line = 0; |
486
|
0
|
|
|
|
|
0
|
my $n = int(log(@$content) / log(10)) + 1; |
487
|
0
|
|
|
|
|
0
|
map { $_ = sprintf("%${n}d %s", ++$line, $_) } @$content; |
|
0
|
|
|
|
|
0
|
|
488
|
|
|
|
|
|
|
} |
489
|
|
|
|
|
|
|
|
490
|
|
|
|
|
|
|
sub do_file_name { |
491
|
2
|
|
|
2
|
0
|
6
|
my ($content, $markers, $modes, $options, $replace_all) = @_; |
492
|
2
|
|
|
|
|
4
|
my $name; |
493
|
2
|
50
|
0
|
|
|
7
|
if (ref($modes->{file_name_ref}) eq 'SCALAR') { |
|
|
0
|
|
|
|
|
|
494
|
2
|
|
|
|
|
4
|
$name = ${$modes->{file_name_ref}}; |
|
2
|
|
|
|
|
5
|
|
495
|
|
|
|
|
|
|
} elsif (ref($modes->{file_name_ref}) eq 'REF' and |
496
|
0
|
|
|
|
|
0
|
ref(${$modes->{file_name_ref}}) eq 'SCALAR') { |
497
|
0
|
|
|
|
|
0
|
$name = $${$modes->{file_name_ref}}; |
|
0
|
|
|
|
|
0
|
|
498
|
|
|
|
|
|
|
} else { |
499
|
|
|
|
|
|
|
die 'INTERNAL ERROR: Invalid input marker: '.Dumper($modes->{file_name_ref}) |
500
|
0
|
|
|
|
|
0
|
."\n"; |
501
|
|
|
|
|
|
|
} |
502
|
2
|
50
|
|
|
|
5
|
if ($replace_all) { |
503
|
0
|
|
|
|
|
0
|
@$content = ($name); |
504
|
0
|
|
|
|
|
0
|
@$markers = (0); |
505
|
|
|
|
|
|
|
} else { |
506
|
2
|
|
|
|
|
5
|
unshift @$content, $name; |
507
|
2
|
|
|
|
|
5
|
unshift @$markers, 0; |
508
|
|
|
|
|
|
|
} |
509
|
|
|
|
|
|
|
} |
510
|
|
|
|
|
|
|
|
511
|
|
|
|
|
|
|
sub do_line_count { |
512
|
2
|
|
|
2
|
0
|
5
|
my ($content, $markers, $modes, $options) = @_; |
513
|
2
|
|
|
|
|
5
|
@$content = (scalar(@$content)); |
514
|
2
|
|
|
|
|
8
|
@$markers = (0); |
515
|
|
|
|
|
|
|
} |
516
|
|
|
|
|
|
|
|
517
|
|
|
|
|
|
|
sub do_cut { |
518
|
5
|
|
|
5
|
0
|
18
|
my ($content, $markers, $modes, $options, $spec) = @_; |
519
|
5
|
|
|
|
|
16
|
my $re = prepare_re($modes->{input_field}, $modes); |
520
|
5
|
50
|
|
|
|
15
|
if ($options->{debug_mode}) { |
521
|
5
|
|
|
|
|
13
|
print "Examples of the --cut operation:\n"; |
522
|
5
|
|
|
|
|
54
|
my @debug = map { [split $re] } @$content[0..min(5, $#$content)]; |
|
12
|
|
|
|
|
97
|
|
523
|
5
|
100
|
|
|
|
14
|
map { for ((@$_)[@$spec]) { $_ = "-->${_}<--" if $_ } } @debug; |
|
12
|
|
|
|
|
29
|
|
|
24
|
|
|
|
|
76
|
|
524
|
5
|
|
|
|
|
13
|
local $, = $modes->{output_field}; |
525
|
5
|
|
|
|
|
19
|
local $\ = $options->{output_separator}; |
526
|
5
|
|
|
|
|
11
|
map { print @$_ } @debug; |
|
12
|
|
|
|
|
46
|
|
527
|
|
|
|
|
|
|
} |
528
|
|
|
|
|
|
|
@$content = |
529
|
|
|
|
|
|
|
map { |
530
|
5
|
100
|
|
|
|
15
|
join $modes->{output_field}, map { $_ ? $_ : '' } (split $re)[@$spec] |
|
12
|
|
|
|
|
61
|
|
|
24
|
|
|
|
|
92
|
|
531
|
|
|
|
|
|
|
} @$content; |
532
|
|
|
|
|
|
|
} |
533
|
|
|
|
|
|
|
|
534
|
|
|
|
|
|
|
sub do_paste { |
535
|
0
|
|
|
0
|
0
|
0
|
my ($content, $markers, $modes, $options, $file) = @_; |
536
|
0
|
|
|
|
|
0
|
my ($side_content, undef) = read_side_input($file, $options); |
537
|
0
|
|
|
|
|
0
|
for my $i (0 .. $#$side_content) { |
538
|
0
|
|
|
|
|
0
|
$content->[$i] .= $modes->{output_field}.$side_content->[$i]; |
539
|
|
|
|
|
|
|
} |
540
|
0
|
|
|
|
|
0
|
for my $i ($#$side_content + 1 .. $#$content) { |
541
|
0
|
|
|
|
|
0
|
$content->[$i] .= $modes->{output_field}; |
542
|
|
|
|
|
|
|
} |
543
|
0
|
0
|
|
|
|
0
|
if (@$content > @$markers) { |
544
|
0
|
|
|
|
|
0
|
splice @$markers, scalar(@$markers), 0, (0) x (@$content - @$markers); |
545
|
|
|
|
|
|
|
} |
546
|
|
|
|
|
|
|
} |
547
|
|
|
|
|
|
|
|
548
|
|
|
|
|
|
|
sub do_pivot { |
549
|
5
|
|
|
5
|
0
|
13
|
my ($content, $markers, $modes, $options, $transpose) = @_; |
550
|
5
|
|
|
|
|
8
|
my $m = 0; |
551
|
5
|
100
|
|
|
|
14
|
if ($transpose) { |
552
|
2
|
|
|
|
|
11
|
my $re = prepare_re($modes->{input_field}, $modes); |
553
|
2
|
|
|
|
|
5
|
$m = 0; |
554
|
|
|
|
|
|
|
my @lines = |
555
|
2
|
|
|
|
|
4
|
map { my $r = [split $re]; $m = max($m, scalar(@$r)); $r } @$content; |
|
6
|
|
|
|
|
34
|
|
|
6
|
|
|
|
|
27
|
|
|
6
|
|
|
|
|
11
|
|
556
|
|
|
|
|
|
|
@$content = |
557
|
|
|
|
|
|
|
map { |
558
|
2
|
|
|
|
|
9
|
my $c = $_; |
|
5
|
|
|
|
|
9
|
|
559
|
5
|
|
100
|
|
|
9
|
join $modes->{output_field}, map { $_->[$c] // '' } @lines; |
|
15
|
|
|
|
|
49
|
|
560
|
|
|
|
|
|
|
} 0..($m - 1); |
561
|
|
|
|
|
|
|
} else { |
562
|
3
|
|
|
|
|
5
|
$m = 1; |
563
|
3
|
|
|
|
|
14
|
@$content = (join $modes->{output_field}, @$content); |
564
|
|
|
|
|
|
|
} |
565
|
5
|
|
|
|
|
27
|
@$markers = (0) x $m; |
566
|
|
|
|
|
|
|
} |
567
|
|
|
|
|
|
|
|
568
|
|
|
|
|
|
|
sub do_tee { |
569
|
6
|
|
|
6
|
0
|
17
|
my ($content, $markers, $modes, $options, $file_name) = @_; |
570
|
6
|
100
|
|
|
|
21
|
if (not $modes->{quote_regex}) { |
571
|
1
|
|
|
|
|
8
|
$file_name = eval_in_safe_env("\"${file_name}\"", $options); |
572
|
1
|
50
|
|
|
|
10
|
die "FATAL: Cannot eval string for --tee: ${@}" if $@; |
573
|
|
|
|
|
|
|
} |
574
|
|
|
|
|
|
|
# This missing_final_separator is not really an option, it is added in the |
575
|
|
|
|
|
|
|
# modes struct by the 'process' method, specifically for this function. |
576
|
|
|
|
|
|
|
write_side_output($file_name, $content, $modes->{missing_final_separator}, |
577
|
6
|
|
|
|
|
20
|
$options); |
578
|
|
|
|
|
|
|
} |
579
|
|
|
|
|
|
|
|
580
|
|
|
|
|
|
|
1; |