1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#! /usr/bin/perl
# sedition-pp <keyword> <ppfile> <source file>
#
# Replaces occurrences of @<keyword>@ in the source file
# with the text found in <ppfile>. <ppfile> may contain
# multiple lines of text. In the <source file>, the @<keyword>@
# must be on a line of its own - it may have leading and
# trailing comment delimiters, but that's all. That's because these
# delimiters will be replicated in the multiline substitution;
# see below.
#
# Note that the <keyword> is surrounded by @@ in the text
# where it's to be replaced. These delimiters do not appear in the command
# line; this allows you to run sedition-pp on scripts and Makefiles
# that themselves contain sedition-pp command lines, without clobbering
# those commands.
#
# sedition-pp preserves and replicates leading and trailing context,
# permitting language-independent substitution within comments.
# For example,
# sedition-pp FOO foofile foo.c
# finds in foo.c (C code) a line like
# * @FOO@
# and replaces it with the text of "foofile", which might
# result in:
# * An example license for foo.
# * Copyright (C) ...
#
# Whereas a (shell or Perl script) line like
# # @FOO@
# would become
# # An example license for foo.
# # Copyright (C) ...
#
# And an HTML section like
# <!--
# -- @FOO@
# -->
# is replaced with
# <!--
# -- An example license for foo.
# -- Copyright (C) ...
# -->
#
# modified from licenseadd.pl in ssdk; SRE, Mon Mar 31 19:39:50 2003
# SVN $Id: sedition-pp 1531 2005-12-13 20:53:46Z eddy $
#
$keyword = shift;
$ppfile = shift;
$sourcefile = shift;
if (! -e $sourcefile) { die "no such file $sourcefile"; }
($dev,$ino,$mode) = stat($sourcefile);
open(PP,$ppfile) || die;
$nlines = 0;
while (<PP>)
{
chomp;
$ppline[$nlines] = $_;
$nlines++;
}
close(PP);
open(TMPFILE,">/tmp/tmp.pp.sedition") || die "Fatal: can't open /tmp/tmp.pp.sedition : $!\n";
open(SOURCE,$sourcefile) || die;
while (<SOURCE>)
{
if (/^(.*)\@$keyword\@(.*)$/)
{
$start = $1;
$end = $2;
foreach $line (@ppline)
{
print TMPFILE "$start$line$end\n";
}
} else { print TMPFILE $_;}
}
close SOURCE;
close TMPFILE;
# Replace the original file with the new one, and restore the original
# file's mode.
#
unlink $sourcefile;
system("mv /tmp/tmp.pp.sedition $sourcefile");
chmod $mode, $sourcefile;
|