#!/usr/local/bin/perl ######################################################################## # Perl implementation of Quanterra's macromania program. # Conforms to Quanterra MSHEAR macromania v23. # # Doug Neuhauser, UC Berkeley Seismological Laboratory # doug@seismo.berkeley.edu # # 1.0.1 1998.323 v23 First release. # 1.0.2 1999.160 v23 Bug fixes to $MEND processing, variable substr. # (thanks to Luis Rivera) # 1.0.3 1999.278 v23 Bug fix in keyword replacement and expr check. # 1.005 2000.131 v23 Corrected key lookup in environment. # ######################################################################## require 5.002; use strict; use Getopt::Std; use FileHandle; use vars qw ( $opt_d $opt_h); ######################################################################## # Global initialization. ######################################################################## my($VERSION, $QVERSION); my($DEBUG_KEYDEF, $DEBUG_MACRO, $DEBUG_TEMPLATE, $DEBUG_TMACRO); my($DEBUG_KEYREPLACE, $DEBUG_ARITH, $DEBUG_KEY); my($DEFAULT_ESC, $DEFAULT_KEYMACROS); my($ESC, $AP); my($keymacro, @macrofiles, %keyval, %keymacro, $debug); # Variables to be reset for each template macro file processed. my(@ifstack, @body, $macro, %tmacro, $in_macro, $use); $VERSION = "1.0.3 (1999.278)"; $QVERSION = "Quanterra v23 compatible"; $DEBUG_KEYDEF = 1; $DEBUG_MACRO = 2; $DEBUG_TEMPLATE = 4; $DEBUG_TMACRO = 8; $DEBUG_KEYREPLACE = 16; $DEBUG_ARITH = 32; $DEBUG_KEY = 64; $DEFAULT_ESC = "%"; $AP = '(?:"[^"]*"|[^\s";]*)'; # Pattern for macro argument. $DEFAULT_KEYMACROS = "keymacros"; STDERR->autoflush(1); STDOUT->autoflush(1); ######################################################################## # print_syntax - print syntax and exit. ######################################################################## sub print_syntax { my($cmdname) = @_; $cmdname = $1 if ($cmdname =~ m|^.*/([^/]+)$|); printf "$cmdname version $VERSION ($QVERSION) $cmdname - Create output files from template and key files. Syntax: $cmdname [-d N] filelist keyfile where: -d N specifies debug option (may be added together). 1 = DEBUG_KEYDEF 2 = DEBUG_KEYMACRO 4 = DEBUG_TEMPLATE 8 = DEBUG_TEMPLATE_MACRO 16 = DEBUG_KEYREPLACE 32 = DEBUG_ARITH 64 = DEBUG_KEY (final symboltable) filelist is the filename containing a list of files to be created. keyfile is the name of the primary key file to be processed. "; exit(0); } ######################################################################## # Main program. ######################################################################## { my($keyfile, $filelist, $f, $file, @files, $macrofile, @lines, $grp); require "getopts.pl"; &Getopts ('hf:d:T:C:c:'); &print_syntax($0) if ($opt_h); $debug = $opt_d; print ("Error: Missing filenames.\n"), print_syntax($0) if (@ARGV != 2); $filelist = shift(@ARGV); $keyfile = shift(@ARGV); # 1. Read the keys, caching key definitions. open (KEYFILE, "<$keyfile") || die "Unable to open keyfile"; @lines = ; close (KEYFILE); print ("===> Start keyfile $keyfile: Pass 1\n") if ($debug & $DEBUG_KEYDEF); &process_key(1,@lines); print ("===> End keyfile $keyfile: Pass 1\n") if ($debug & $DEBUG_KEYDEF); # For debugging, dump keys. &dump_keyval() if ($debug & $DEBUG_KEYDEF); # 2. Read all of the keymacro files, caching all keymacros. # Process any user-defined keymacro files before default keymacro file. foreach $macrofile (@macrofiles, $DEFAULT_KEYMACROS) { open (KEYMACROS, "<$macrofile") || die "Unable to open keymacro file $macrofile"; @lines = ; close (KEYMACROS); &define_keymacro(@lines); } # For debugging, dump macros. &dump_keymacro() if ($debug & $DEBUG_MACRO); # 3. Reprocess key file, expanding keymacros calls. open (KEYFILE, "<$keyfile") || die "Unable to open keyfile"; @lines = ; close (KEYFILE); print ("===> Start keyfile $keyfile: Pass 2\n") if ($debug & $DEBUG_KEYDEF); &process_key(2,@lines); print ("===> End keyfile $keyfile: Pass 2\n") if ($debug & $DEBUG_KEYDEF); # For debugging, dump keys. &dump_keyval() if ($debug & $DEBUG_KEYDEF || $debug & $DEBUG_KEY); # Expand template file(s). $grp = $keyval{"GRP"}; open (FILELIST, "$filelist") || die "Unable to open filelist: $filelist"; chomp(@files = ); close (FILELIST); foreach $f (@files) { chomp ($f); $file = "$f" . "_" . $grp; print "processing template file $file\n"; open (TEMPLATE, "<$file") || die "Unable to open template file $file"; @lines =