| 1 |
#! /usr/bin/perl |
| 2 |
############################################################################ |
| 3 |
# |
| 4 |
# replace -- script for replacing standard text blocks if found in large |
| 5 |
# file sets |
| 6 |
# |
| 7 |
# sample: |
| 8 |
# find . -name "*.h" | xargs replace --orig orig.txt --new new.txt |
| 9 |
# |
| 10 |
# ************************************************************************** |
| 11 |
|
| 12 |
############################################################################ |
| 13 |
|
| 14 |
if ( @ARGV == 0 ) { |
| 15 |
&PrintUsage(); |
| 16 |
exit 0; |
| 17 |
} |
| 18 |
|
| 19 |
%OPTIONS = ( # option perl code |
| 20 |
'--help', '&PrintUsage(); exit 0;', |
| 21 |
'--usage', '&PrintUsage(); exit 0;', |
| 22 |
'-h', '&PrintUsage(); exit 0;', |
| 23 |
'-?', '&BrintUsage(); exit 0;', |
| 24 |
'--version', '&PrintVersion(); exit 0;', |
| 25 |
'-v', '&PrintVersion(); exit 0;', |
| 26 |
'--orig', '$next_is_orig = 1;', |
| 27 |
'-o', '$next_is_orig = 1;', |
| 28 |
'--new', '$next_is_new = 1;', |
| 29 |
'-n', '$next_is_new = 1;', |
| 30 |
'--debug', '$debug = 1;', |
| 31 |
'-d', '$debug = 1;', |
| 32 |
); |
| 33 |
|
| 34 |
############################################################################ |
| 35 |
# separate files from options |
| 36 |
|
| 37 |
$debug = 0; |
| 38 |
$make_backups = 1; |
| 39 |
$note_failures = 0; |
| 40 |
|
| 41 |
@files = (); |
| 42 |
@orig = (); |
| 43 |
@new = (); |
| 44 |
|
| 45 |
$next_is_orig = 0; |
| 46 |
$next_is_new = 0; |
| 47 |
|
| 48 |
foreach $arg ( @ARGV ) { |
| 49 |
if ( $next_is_orig == 1 ) { |
| 50 |
open(ORIG, $arg) || die "$0: error opening '$arg'"; |
| 51 |
@orig = <ORIG>; |
| 52 |
chomp(@orig); |
| 53 |
close(ORIG); |
| 54 |
$next_is_orig = 0; |
| 55 |
} elsif ( $next_is_new == 1 ) { |
| 56 |
open(NEW, $arg) || die "$0: error opening '$arg'"; |
| 57 |
@new = <NEW>; |
| 58 |
chomp(@new); |
| 59 |
close(NEW); |
| 60 |
$next_is_new = 0; |
| 61 |
} elsif ( defined $OPTIONS{$arg} ) { |
| 62 |
eval $OPTIONS{$arg}; |
| 63 |
} else { |
| 64 |
push( @files, $arg ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
if ( scalar(@orig) == 0 ) { |
| 69 |
print "scalar(orig) == 0\n"; |
| 70 |
&PrintUsage(); |
| 71 |
exit 1; |
| 72 |
} |
| 73 |
|
| 74 |
if ( scalar(@files) == 0 ) { |
| 75 |
print "scalar(files) == 0\n"; |
| 76 |
# stdin/stdout is not supported yet |
| 77 |
&PrintUsage(); |
| 78 |
exit 1; |
| 79 |
} |
| 80 |
|
| 81 |
foreach $file ( @files ) { |
| 82 |
next if ! -f $file; |
| 83 |
if ( ! open(CURRENT, $file) ) { |
| 84 |
print STDERR "$0: unable to open file '$file'\n"; |
| 85 |
next; |
| 86 |
} |
| 87 |
$match = 0; |
| 88 |
@current = <CURRENT>; |
| 89 |
chomp(@current); |
| 90 |
for ( $line = 0; $line < scalar(@current) - scalar(@orig) + 1; $line++ ) { |
| 91 |
if ( $current[$line] eq $orig[0] ) { |
| 92 |
$local_match = 1; |
| 93 |
for ( $off = 1; $off < scalar(@orig) && $local_match == 1; $off++ ) { |
| 94 |
$local_match = 0 if ( $current[$line + $off] ne $orig[$off] ); |
| 95 |
} |
| 96 |
if ( $local_match == 1 ) { |
| 97 |
splice(@current, $line, scalar(@orig), @new); |
| 98 |
$line += scalar(@new); |
| 99 |
$match = 1; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
if ( $match == 0 ) { |
| 104 |
printf "\r%-60s%-18s\n", $file . ":", "no change..."; |
| 105 |
} else { |
| 106 |
printf "\r%-60s%-18s\n", $file . ":", "updating..."; |
| 107 |
system( "mv $file $file.bak" ); |
| 108 |
open( FILE, "> $file" ) || die $file, ": ", $!, "\n"; |
| 109 |
foreach $line ( @current ) { |
| 110 |
print FILE $line, "\n"; |
| 111 |
} |
| 112 |
close( FILE ); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
# prints program usage |
| 117 |
sub PrintUsage { ########################################################### |
| 118 |
print <<"END_OF_USAGE"; |
| 119 |
Usage: |
| 120 |
$0 [options] [files...] |
| 121 |
Options: |
| 122 |
-o, --orig <file> : the original text |
| 123 |
-n, --new <file> : the new replacement text |
| 124 |
END_OF_USAGE |
| 125 |
} |
| 126 |
|
| 127 |
# print CVS version |
| 128 |
sub PrintVersion { ######################################################### |
| 129 |
print '$Id$', "\n"; |
| 130 |
} |
| 131 |
|
| 132 |
############################################################################ |
| 133 |
|
| 134 |
|
| 135 |
|