#!/usr/local/bin/perl -w # Program: template.pl # Author: Spencer Shimko # Purpose: Read 1 or more input files and create new versions with various # substitutions made # Usage: ./template.pl [-hvw] [-s ] [-a ] [-l ] filenames... # Created: 3/30/04 # Modified: 4/01/04 use Getopt::Std; ############################ # sub parse cmd line # ############################ sub getcmdline{ getopts('hvws:a:l:', \%opts); # parse %opts hash (command line args) and create better mnemonic # if -h or no args specified display usage and bail if ( ( defined $opts{'h'} ) or ( ! defined $ARGV[0] ) ){ print "usage: $0 [] \ optional flags:\ -h: this help message\ -v: turn on verbose messages\ -w: wrap HEADER and FOOTER file contents around all inputs\ -s : suffix for output files (default = out)\ -a : specify a word=expansion abbreviation file\ -l : specify a word=hyperlink link file\n"; exit; } # verbosity on if ( defined $opts{'v'} ){ $ver = "true"; print "verbose mode\n"; } # wrap output w/ header and footer if ( defined $opts{'w'} ){ $wrap = "true"; } # suffix for output file specified else default if ( defined $opts{'s'} ){ $suffix = $opts{'s'}; } else { $suffix = "out"; } # word expansion requested if ( defined $opts{'a'} ){ print "reading abbreviations from " . $opts{'a'} . "\n" if $ver; if ( ! open(EXPND, $opts{'a'} ) ){ print "cannot open abbreviations file " . $opts{'a'} . ", $!\n"; } else { while ( ){ chomp; my ($lhs, $rhs) = split ('='); push @abbrlhs, $lhs; push @abbrrhs, $rhs; } } close (EXPND); } # word->hyperlink requested if ( defined $opts{'l'} ){ print "reading links from " . $opts{'l'} . "\n" if $ver; if ( ! open(LNK, $opts{'l'} ) ){ print "cannot open link file " . $opts{'l'} . ", $!\n"; } else { while ( ){ chomp; my ($lhs, $rhs) = split ('='); $links{$lhs} = $rhs; } } close (LNK); } } ############################# # end sub parse cmd line # ############################# ############################# # start main # ############################# getcmdline(); # read/parse cmd line # iterate over file list # INPUT label used for "next" clarity INPUT: foreach (@ARGV){ print "processing $_ => $_.$suffix\n" if $ver; # output input and output file if ( ! open (INFILE, $_) ){ print " skipping input $_, cannot open, $!\n"; next INPUT; }if ( ! open (OUTFILE, ">$_.$suffix" ) ){ print " skipping output $_.$suffix, cannot open, $!\n"; next INPUT; } # add header if ( $wrap ){ print " adding header\n" if $ver; if ( ! open ( HEAD, "HEADER" ) ){ print " cannot find HEADER, skipping\n"; } else{ while ( ){ chomp; push @text, $_; } } } # parse and replace input file LINE: while ( ){ if ( /^\#include\s*\"(.*)\"/ ){ # if line is include $incfile = $1; # check to see if included file has already resulted in err if ( ! defined $errincfile{ $incfile } ){ # attempt to include file if ( ! open ( INCFILE, $incfile ) ){ # couldnt open so error $errincfile{ $incfile } = "true"; print " skipping include file $incfile, cannot open, $!\n"; next LINE; } else { # insert included file into text array while ( ) { chomp; push @text, $_; } } } else { # error already occured with this filename so skip next LINE; } } elsif ( /^\#command\s*\"(.*)\"/ ) { # if line is command $command = $1; # check to see if command resulted in previous error if ( ! defined $errcommand{ $command } ){ # attempt to open file, if fails set error if ( ! open ( CMDFILE, "$command 2>&1 |" ) ){ $errcommand{ $command } = "true"; print " skipping command $command, cannot open, $!\n"; next LINE; } else { # insert output from command while ( ){ chomp; push @text, $_; } } } else { # error already occured with this command so skip next LINE; } } else { # else not command or include so look for abbrev and links if ( defined @abbrlhs ){ for ( $x=0; $x < scalar(@abbrrhs); $x++ ){ s:$abbrlhs[$x]:$abbrrhs[$x]:g; } } if ( defined %links ){ foreach $k ( keys %links ){ s/($k)/\$1\<\/a\>/g; } } chomp; push @text, $_; } # endif include or command } # while lines left in input file # add footer if ( defined $wrap ){ print " adding footer\n" if $ver; if ( ! open ( FOOT, "FOOTER" ) ){ print " cannot find FOOTER, skipping\n"; } else{ while ( ){ chomp; push @text, $_; } } } foreach $line ( @text ){ print OUTFILE $line . "\n"; } # close file handles close (INFILE); close (OUTFILE); # clean array undef @text; }# end foreach file ############################ # end main # ############################