| 1 |
#! /usr/bin/perl |
| 2 |
|
| 3 |
# sedition <keyword> <replacement> [<key2> <replace2>]... <file> |
| 4 |
# |
| 5 |
# A wrapper for "sed", allowing editing a file in place |
| 6 |
# instead of in a stream. |
| 7 |
# |
| 8 |
# Replace all instances of @keyword@ with <replacement>, |
| 9 |
# @key2@ with <replace2>, etc. |
| 10 |
# |
| 11 |
# Note that all keywords to be replaced are enclosed by @@ in |
| 12 |
# the text where they are to be replaced, but the @@ delimiters |
| 13 |
# are not in the sedition command line. This allows you to run |
| 14 |
# sedition on scripts and Makefiles that themselves contain sedition |
| 15 |
# command lines, without inadvertently clobbering those command lines. |
| 16 |
# |
| 17 |
# Replacement strings should generally be one-liners. |
| 18 |
# See sedition-pp for a paragraph-wise replacement script. |
| 19 |
# |
| 20 |
# Note the restricted command line use: sedition, unlike |
| 21 |
# sed, must be run on a single specified file. |
| 22 |
# |
| 23 |
# Example: |
| 24 |
# sedition FOO "foo replacement" BAR "bar replacement" myfile |
| 25 |
# converts all instances of |
| 26 |
# @FOO@ |
| 27 |
# to |
| 28 |
# foo replacement |
| 29 |
# |
| 30 |
# Moved to ssdk, SRE, Mon Mar 31 19:24:19 2003 |
| 31 |
# SVN $Id: sedition 1531 2005-12-13 20:53:46Z eddy $ |
| 32 |
|
| 33 |
sub choke { |
| 34 |
my ($mesg) = @_; |
| 35 |
|
| 36 |
if (-e "$filename.sed.tmp") { system "mv $filename.sed.tmp $filename"; } |
| 37 |
die ($mesg, ", stopped"); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
$filename = pop; |
| 42 |
if (! -e $filename) { die "$filename doesn't exist"; } |
| 43 |
if ( -e "$filename.sed.tmp") { die "already have a tmp file $filename.sed.tmp"; } |
| 44 |
|
| 45 |
$sedargs = ""; |
| 46 |
while (@ARGV) { |
| 47 |
$key = shift; |
| 48 |
$replace = shift; |
| 49 |
$sedargs .= " -e 's!\@$key\@!$replace!g'"; |
| 50 |
} |
| 51 |
|
| 52 |
if (-w $filename) { $writable = 1; } |
| 53 |
|
| 54 |
system("cp $filename $filename.sed.tmp"); |
| 55 |
if ($? != 0) {die "failed to make tmp copy"; } |
| 56 |
if (! $writable) { |
| 57 |
system("chmod +w $filename"); |
| 58 |
if ($? != 0) { choke "failed to make $filename temporarily writable"; } |
| 59 |
} |
| 60 |
system("sed $sedargs $filename.sed.tmp > $filename"); |
| 61 |
if ($? != 0) {choke "sed failed"; } |
| 62 |
if (! $writable) { |
| 63 |
system("chmod -w $filename"); |
| 64 |
if ($? != 0) { choke "failed to make $filename temporarily writable"; } |
| 65 |
} |
| 66 |
unlink "$filename.sed.tmp"; |
| 67 |
|