#! /usr/bin/perl # sedition-pp # # Replaces occurrences of @@ in the source file # with the text found in . may contain # multiple lines of text. In the , the @@ # 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 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 # # is replaced with # # # 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 () { 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 () { 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;