#! /local/bin/perl5.003 -w # (C) Copyright 1997 Rahul Dhesi, All rights reserved. # Permission for copying and creation of derivative works is granted, # provided this copyright notice is preserved, to anybody. # # EDIT THIS IN /local/undoc/subst.pl use strict; # $Source: /files/home/ftp/pub/dhesi/www/software/RCS/subst.pl,v $ # $Id: subst.pl,v 1.1 1997/08/13 11:27:14 dhesi Exp $ # Reads a file whose name is specified on the command line. In each # file does substitutions and writes it back to the same filename. # Substitutions are done using the macros specified on the command line # in the form NAME=value. Each macro in the input file will be of the # form %NAME% where the NAME is in uppercase. Each place in the input # file where a macro substitution needs to be done, the previous line # will contain the string %SUB% . Substitutions will be done on a copy # of that line, including replacing %SUB% by the empty string, and the # resulting line, minus its first character, will be used to replace the # entire line following it. E.g., if the input file contains # #%SUB%x = %VALUE%; # x = 100; # and we invoke subst.pl as 'subs.pl infile VALUE=200 # then the output file will be written as: # #%SUB%x = %VALUE%; # x = 200; $::myname = $0; $::myname =~ s|.*/||; $::RCSHEADER = '$Source: /files/home/ftp/pub/dhesi/www/software/RCS/subst.pl,v $' . "\n" . '$Id: subst.pl,v 1.1 1997/08/13 11:27:14 dhesi Exp $'; # suppresss perl warnings $::debug = $::trace = $::verbose = $::opt_h = undef; $::opt_v = $::opt_t = $::opt_x = undef; $::usage = "usage: $::myname [-vtx] file NAME=value ... (or -h for help)"; if (@ARGV && $ARGV[0] =~ "^-.+" ) { require "getopts.pl"; &Getopts("vtxh"); } $::debug = $::opt_x; $::trace = $::opt_t; $::verbose = $::debug || $::trace || $::opt_v; if ($::opt_h) { &givehelp(); exit(0); } (@ARGV < 2) && &usage_error; my $file = shift; open(INFILE, $file) || die "$::myname: can't open $file for input: $!\n"; # temp output file on same filesystem as input file my $temp = $file; $temp .= ".temp$$"; sub END { unlink $temp; } unlink $temp; open(TEMP, ">$temp") || die "$::myname: can't open $temp for output: $!\n"; my %value; # get macros for (@ARGV) { if (/(.+)\=(.*)/) { $value{$1} = $2; # save values $::debug && print "> $1 => $2\n"; } else { die "$::myname: invalid argument '$_'\n"; } } # now read and write, doing substitutions my $macro; my $junk; while () { print TEMP $_; # this line goes out intact if (/\%SUB\%/) { # if this was a %SUB% line s/\%SUB\%//g; # .. squeeze out %SUB% macro for $macro (keys %value) { # .. for each macro ## $::debug && print "sub $macro => $value{$macro}\n"; s/\%\Q$macro\E\%/$value{$macro}/g; # .. .. do substitutions } s/^.//; # .. squeeze out first char print TEMP $_; # .. print result; $junk = ; # .. skip the next line } } close(TEMP) || die "$::myname: error writing to $temp: $!\n"; rename($temp, $file) || die "$::myname: error renaming $temp to $file: $!\n"; sub usage_error { my($msg) = @_; if ($msg) { die "$msg\n"; } else { die "$::usage\n"; } } sub givehelp { ## require 'local/page.pl'; ## &page(<