Debian Codegraph @

Documentation/Utilities/efed.pl

raw
1 #!/usr/bin/perl -w
2 #==============================================================================
3 #
4 # Eric's File Editor (tm)
5 # Copyright (C) 2000,2001 by Eric Sunshine <[email protected]>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #
21 #==============================================================================
22 #------------------------------------------------------------------------------
23 # efed.pl
24 #
25 # A tool for applying pattern-based modifications to the content and/or
26 # name of a file or batch of files.
27 #
28 #------------------------------------------------------------------------------
29 use strict;
30 use File::Basename;
31 use Getopt::Long;
32 $Getopt::Long::ignorecase = 0;
33
34 my $PROG_NAME = 'efed.pl';
35 my $PROG_VERSION = 5;
36 my $AUTHOR_NAME = 'Eric Sunshine';
37 my $AUTHOR_EMAIL = '[email protected]';
38 my $COPYRIGHT = "Copyright (C) 2000,2001 by $AUTHOR_NAME <$AUTHOR_EMAIL>";
39
40 @main::opt_edit = ();
41 @main::opt_rename = ();
42 @main::opt_accept = ();
43 @main::opt_ignore = ();
44 $main::opt_descend = 0;
45 $main::opt_verbose = 0;
46 $main::opt_v = 0; # Alias for 'verbose'.
47 $main::opt_quiet = 0;
48 $main::opt_test = 0;
49 $main::opt_version = 0;
50 $main::opt_V = 0; # Alias for 'version'.
51 $main::opt_help = 0;
52
53 my @script_options = (
54 'edit=s@',
55 'rename=s@',
56 'accept=s@',
57 'ignore=s@',
58 'descend!',
59 'verbose!',
60 'v!', # Alias for 'verbose'.
61 'quiet!',
62 'test!',
63 'version',
64 'V', # Alias for 'version'.
65 'help'
66 );
67
68 $main::verbosity = 0;
69 @main::paths = ();
70 $main::total_files = 0;
71 $main::total_files_ignored = 0;
72 $main::total_files_edited = 0;
73 $main::total_files_renamed = 0;
74 $main::total_edits = 0;
75
76 #------------------------------------------------------------------------------
77 # Emit an error message and terminate abnormally.
78 #------------------------------------------------------------------------------
79 sub fatal {
80 my $msg = shift;
81 die "ERROR: $msg\n";
82 }
83
84 #------------------------------------------------------------------------------
85 # Emit a warning message.
86 #------------------------------------------------------------------------------
87 sub warning {
88 my $msg = shift;
89 warn "WARNING: $msg\n";
90 }
91
92 #------------------------------------------------------------------------------
93 # Print an underlined title.
94 #------------------------------------------------------------------------------
95 sub print_title {
96 my $msg = shift;
97 print "$msg\n", '-' x length($msg), "\n";
98 }
99
100 #------------------------------------------------------------------------------
101 # Should script be verbose?
102 #------------------------------------------------------------------------------
103 sub verbose {
104 return $main::verbosity > 0;
105 }
106
107 #------------------------------------------------------------------------------
108 # Should script be quiet?
109 #------------------------------------------------------------------------------
110 sub quiet {
111 return $main::verbosity < 0;
112 }
113
114 #------------------------------------------------------------------------------
115 # Synthesize a subroutine which applies a list of patterns to a string.
116 # Synthesized subroutines may be configured to return upon the first
117 # successful match or to apply all patterns in succession (which is useful
118 # when a series of s/// or tr/// style patterns must be applied). Synthesized
119 # subroutines which return after the first successful match return a boolean
120 # result, whereas subroutines which apply all patterns return the number of
121 # edits made by the patterns (which can also be viewed as a boolean result).
122 # Such subroutines also modify the input string in-place on the assumption
123 # that the caller will want access to the edited string after the patterns
124 # have been applied. If the pattern list is empty then the synthesized
125 # subroutine will return the input value $empty_result.
126 #------------------------------------------------------------------------------
127 sub synthesize {
128 my ($name, $patterns, $apply_all, $empty_result) = @_;
129 my $s = "sub $name " . '{ local $_ = $_[0]; my $n = scalar(';
130 if (!@{$patterns}) {
131 $s .= $empty_result;
132 }
133 else {
134 my ($pattern, $tests, $op) = ('', '', $apply_all ? '+' : '||');
135 foreach $pattern (@{$patterns}) {
136 $tests .= " $op " if $tests;
137 $tests .= "$pattern";
138 }
139 $s .= $tests;
140 }
141 $s .= ');';
142 $s .= '$_[0] = $_;' if $apply_all; # Return edited string for s///, etc.
143 $s .= ' return $n; }';
144 eval $s;
145 fatal $@ if $@;
146 }
147
148 #------------------------------------------------------------------------------
149 # Compile all patterns and edit operations for fast matching.
150 #------------------------------------------------------------------------------
151 sub compile {
152 synthesize('apply_edits', \@main::opt_edit, 1, 0);
153 synthesize('apply_rename', \@main::opt_rename, 1, 0);
154 synthesize('match_accept', \@main::opt_accept, 0, 1);
155 synthesize('match_ignore', \@main::opt_ignore, 0, 0);
156 }
157
158 #------------------------------------------------------------------------------
159 # Normalize a pattern. If it is of the form // then conver it to m//. If the
160 # pattern is a raw string, then insert it into m// (under the assumption that
161 # '/' does not already appear in the pattern).
162 #------------------------------------------------------------------------------
163 sub normalize_pattern {
164 local $_ = $_[0];
165 return if /^m(\W).*\1$/; # Already in m// form.
166 $_ = "/$_/" unless /^(\W).*\1$/; # Wrap in // if not already in // form.
167 $_[0] = "m$_"; # Convert // to m//.
168 }
169
170 #------------------------------------------------------------------------------
171 # Normalize an edit operation. Ensure correctness of one of the regular-
172 # expression operators, s///, tr///, or y///. If the incoming pattern does
173 # not begin with one of the operators, 's', 'tr', or 'y', then assume s///.
174 # If an s/// operation does not end with any modifiers and if $global is set,
175 # then assume s///g.
176 #------------------------------------------------------------------------------
177 sub normalize_edit {
178 local $_ = $_[0];
179 my $global = $_[1];
180 fatal "Invalid pattern matching operation: $_" unless
181 /^\W/ || /^s\W/ || /^tr\W/ || /^y\W/;
182 s/^(\W)/s$1/; # Assume s/// for ///.
183 s/(\W)$/$1g/ if $global && /^s\W/; # Assume s///g for s///.
184 $_[0] = $_;
185 }
186
187 #------------------------------------------------------------------------------
188 # Normalize all edit operations and patterns.
189 #------------------------------------------------------------------------------
190 sub normalize {
191 my $s;
192 foreach $s (@main::opt_rename) {
193 normalize_edit($s, 0);
194 }
195 foreach $s (@main::opt_edit) {
196 normalize_edit($s, 1);
197 }
198 foreach $s (@main::opt_accept, @main::opt_ignore) {
199 normalize_pattern($s);
200 }
201 }
202
203 #------------------------------------------------------------------------------
204 # Recognize and replace special alias convenience patterns:
205 # `old=new' --> `s/\.old$/\.new/' (filename suffix substitution)
206 # `old:new' --> `s/\.old$/\.new/' (ditto)
207 #------------------------------------------------------------------------------
208 sub process_aliases {
209 my $s;
210 foreach $s (@main::opt_rename) {
211 $s = "s/\\.$1\$/\\.$2/" if $s =~ /^(\w+)[=:](\w+)$/;
212 }
213 }
214
215 #------------------------------------------------------------------------------
216 # Scan a directory for filenames.
217 #------------------------------------------------------------------------------
218 sub scandir {
219 my $dir = shift;
220 opendir(DIR, $dir) or fatal("Directory scan failed ($dir): $!");
221 my @files = grep(!/^\.\.?$/, readdir(DIR));
222 closedir(DIR);
223 return sort { lc($main::a) cmp lc($main::b) } map("$dir/$_", @files);
224 }
225
226 #------------------------------------------------------------------------------
227 # As a convenience, always descend one level deep into directories explicitly
228 # mentioned on the command-line even if recursion was not requested. This
229 # pre-processing step is skipped if recursion is enabled since the directory
230 # contents will be examined later on anyhow; otherwise the contents would be
231 # examined twice.
232 #------------------------------------------------------------------------------
233 sub prescan {
234 if (!$main::opt_descend) {
235 my ($path, @paths) = ('', ());
236 foreach $path (@main::paths) {
237 push(@paths, scandir($path)) if -d $path;
238 }
239 push(@main::paths, @paths);
240 }
241 }
242
243 #------------------------------------------------------------------------------
244 # Read file contents.
245 #------------------------------------------------------------------------------
246 sub load_file {
247 my $path = shift;
248 my $content = '';
249 my $size = -s $path;
250 if ($size) {
251 open(FILE, "<$path") or fatal("Unable to open $path: $!");
252 binmode(FILE);
253 read(FILE, $content, $size) == $size
254 or fatal("Failed to read all $size bytes of $path: $!");
255 close(FILE);
256 }
257 return $content;
258 }
259
260 #------------------------------------------------------------------------------
261 # Save file contents.
262 #------------------------------------------------------------------------------
263 sub save_file {
264 my ($path, $content) = @_;
265 open (FILE, ">$path") or fatal("Unable to create $path: $!");
266 binmode(FILE);
267 print FILE $content if length($content);
268 close(FILE);
269 }
270
271 #------------------------------------------------------------------------------
272 # Rename a file.
273 #------------------------------------------------------------------------------
274 sub rename_file {
275 my ($oldpath, $newpath) = @_;
276 unlink($newpath) if -e $newpath;
277 rename($oldpath, $newpath)
278 or fatal("Failed to rename $oldpath to $newpath: $!");
279 }
280
281 #------------------------------------------------------------------------------
282 # Should file be ignored?
283 #------------------------------------------------------------------------------
284 sub should_ignore {
285 my $path = shift;
286 my $name = basename($path);
287 my $ignore = !match_accept($name) || match_ignore($name);
288 if ($ignore) {
289 $main::total_files_ignored++;
290 print "Ignored: $path\n" if verbose();
291 }
292 return $ignore;
293 }
294
295 #------------------------------------------------------------------------------
296 # Process a path rename operation.
297 #------------------------------------------------------------------------------
298 sub process_rename {
299 my ($oldpath, $newpath) = @_;
300 rename_file($oldpath, $newpath) unless $main::opt_test;
301 print "Renamed: $newpath (was ", basename($oldpath), ")\n" if verbose();
302 }
303
304 #------------------------------------------------------------------------------
305 # Process a single path.
306 #------------------------------------------------------------------------------
307 sub process_path {
308 my $path = shift;
309 $path =~ s:^\./::; # Strip leading "./".
310 $main::total_files++;
311 return if should_ignore($path);
312 my ($name, $dir) = fileparse($path);
313 $dir = '' if $dir =~ m:\./?$:;
314 my $newname = $name;
315 my $renamed = apply_rename($newname);
316 my $newpath = "$dir$newname";
317 $main::total_files_renamed++ if $renamed;
318
319 if (-d $path) {
320 process_rename($path, $newpath) if $renamed;
321 }
322 else {
323 my ($content, $edits) = (undef, 0);
324 if (@main::opt_edit) {
325 $content = load_file($path);
326 $edits = apply_edits($content);
327 }
328
329 if ($edits) {
330 $main::total_files_edited++;
331 $main::total_edits += $edits;
332 unless ($main::opt_test) {
333 my $backuppath = "$path.save";
334 rename_file($path, $backuppath) unless $renamed;
335 save_file($newpath, $content);
336 unlink($renamed ? $path : $backuppath);
337 }
338 print "Changed: $newpath [$edits edits]",
339 ($renamed ? " (was $name)" : ''), "\n" if verbose();
340 }
341 elsif ($renamed) {
342 process_rename($path, $newpath);
343 }
344 else {
345 print "Studied: $newpath (no changes)\n" if verbose();
346 }
347 }
348 }
349
350 #------------------------------------------------------------------------------
351 # Process all input paths.
352 #------------------------------------------------------------------------------
353 sub process {
354 prescan();
355 print_title("Processing") if verbose();
356 while (@main::paths) {
357 my $path = shift @main::paths;
358 process_path($path);
359 push(@main::paths, scandir($path)) if $main::opt_descend and -d $path;
360 }
361 print "\n" if verbose();
362 }
363
364 #------------------------------------------------------------------------------
365 # Display a list of patterns.
366 #------------------------------------------------------------------------------
367 sub print_patterns {
368 my ($title, $patterns) = @_;
369 return unless @{$patterns};
370 my $pattern;
371 print "$title:";
372 foreach $pattern (@{$patterns}) {
373 print "\t$pattern\n";
374 }
375 }
376
377 #------------------------------------------------------------------------------
378 # Display summary of requested edits.
379 #------------------------------------------------------------------------------
380 sub summarize_options {
381 print_title("Option Summary");
382 print_patterns("Rename", \@main::opt_rename);
383 print_patterns("Edit", \@main::opt_edit );
384 print_patterns("Accept", \@main::opt_accept);
385 print_patterns("Ignore", \@main::opt_ignore);
386 print "\n";
387 }
388
389 #------------------------------------------------------------------------------
390 # Display edit summary.
391 #------------------------------------------------------------------------------
392 sub summarize_edits {
393 print_title("Edit Summary");
394 print <<"EOT";
395 Files Examined: $main::total_files
396 Files Ignored: $main::total_files_ignored
397 Files Renamed: $main::total_files_renamed
398 Files Edited: $main::total_files_edited
399 Edits Applied: $main::total_edits
400 EOT
401 }
402
403 #------------------------------------------------------------------------------
404 # Display an opening banner.
405 #------------------------------------------------------------------------------
406 sub banner {
407 my $stream = shift;
408 $stream = \*STDOUT unless $stream;
409 print $stream "\n$PROG_NAME version $PROG_VERSION\n$COPYRIGHT\n\n";
410 }
411
412 #------------------------------------------------------------------------------
413 # Display usage statement.
414 #------------------------------------------------------------------------------
415 sub print_usage {
416 my $stream = shift;
417 $stream = \*STDOUT unless $stream;
418 banner($stream);
419 print $stream <<"EOT";
420 Applies pattern-based modifications to the content and/or name of a file
421 or batch of files. Uses standard Perl regular expression directives for
422 controlling content modification and rename operations.
423
424 Usage: $PROG_NAME [options] <path> ...
425
426 <path> Path of the file to be edited. If path refers to a directory, then
427 all files within the directory are considered, subject to the
428 constraints of --accept, --ignore, and --descend, as explained below.
429 Any number of paths may be specified.
430
431 Options:
432 -e <pattern>
433 --edit=<pattern>
434 Specifies a Perl regular-expression editing operation to
435 apply to each file's content. May be specified any number of
436 times. Each pattern will be applied to the file's content in
437 succession. Pattern should be one of the Perl editing
438 commands s///, tr///, or y///. The pattern can also be a
439 bare substitution of the form ///, in which case s/// is
440 assumed. The standard Perl pattern modifiers can be
441 specified following the trailing slash. If no modifiers are
442 present on the s/// command, then s///g is assumed.
443 -r <pattern>
444 --rename=<pattern>
445 Specifies a Perl regular-expression used to rename the input
446 file or files. May be repeated any number of times, though
447 typically one pattern is sufficient. As with --edit, ///,
448 s///, tr///, or y/// may be used. The standard pattern
449 modifiers can be specified. Unlike edit patterns, the 'g'
450 modifier is not automatically assumed for s/// operations.
451 As a convencience, the patterns `old=new' and `old:new' are
452 automatically converted to `s/\\.old\$/\\.new/' to simplify
453 the task of changing a filename's suffix since this is such
454 a common procedure.
455 -a <pattern>
456 --accept=<pattern>
457 Specifies a Perl regular-expression used as a filename
458 filter. Filenames which match the pattern will be processed.
459 This is especially useful when processing the contents of
460 entire directories. The pattern may be specified with //,
461 m//, or as a raw string, in which case it is wrapped in m//.
462 -i <pattern>
463 --ignore=<pattern>
464 Specifies a Perl regular-expression used as a filename
465 filter. Filenames which match the pattern will be excluded
466 from processing. This is especially useful when processing
467 the contents of entire directories. As with --match, the
468 pattern may be specified with //, m//, or as a raw string.
469 Ignore-patterns take precedence over accept-patterns, thus
470 a filename which is both accepted and ignored will not be
471 processed.
472 -d --descend When processing entire directories, this flag will cause the
473 search to recurse into subdirectories as well. Can be
474 negated with --nodescend. Default is --nodescend.
475 -v --verbose Emit informational messages about the processing. Can be
476 negated with --noverbose. Deafult is --noverbose.
477 -q --quiet Suppress all output except for error messages. Can be
478 negated with --noquiet. Default is --noquiet.
479 -t --test Perform all edits but do not actually modify any files. Can
480 be negated with --notest. Default is --notest.
481 -V --version Display the version number of $PROG_NAME.
482 -h --help Display this usage message.
483 EOT
484 }
485
486 #------------------------------------------------------------------------------
487 # Process command-line options.
488 #------------------------------------------------------------------------------
489 sub process_options {
490 GetOptions(@script_options) or usage_error('');
491 print_help() if $main::opt_help;
492 print_version() if $main::opt_version || $main::opt_V;
493 usage_error("Must specify at least one input file.") if @ARGV == 0;
494 usage_error("Must specify at least one edit or rename pattern.")
495 unless (@main::opt_rename or @main::opt_edit);
496 $main::verbosity = 1 if $main::opt_verbose or $main::opt_v;
497 $main::verbosity = -1 if $main::opt_quiet;
498 @main::paths = @ARGV;
499 if (!quiet()) {
500 banner();
501 print "Non-destructive testing mode enabled.\n\n" if $main::opt_test;
502 }
503 }
504
505 sub print_version {
506 banner(\*STDOUT);
507 exit(0);
508 }
509
510 sub print_help {
511 print_usage(\*STDOUT);
512 exit(0);
513 }
514
515 sub usage_error {
516 my $msg = shift;
517 print STDERR "ERROR: $msg\n" if $msg;
518 print_usage(\*STDERR);
519 exit(1);
520 }
521
522 #------------------------------------------------------------------------------
523 # Perform the complete repair process.
524 #------------------------------------------------------------------------------
525 process_options();
526 process_aliases();
527 normalize();
528 summarize_options() unless quiet();
529 compile();
530 process();
531 summarize_edits() unless quiet();