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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
#! @PERL@
##
## Copyright (C) by Argonne National Laboratory
## See COPYRIGHT in top-level directory
##
# This file provides a way to merge a template file with a set of
# code fragments. This makes it simple to generate many related tests
# from a single test harness, while ending up with relatively simple code
# in case it is necessary to debug the code.
# The template file uses an XML-like notation to mark off places for code.
# Specifically, lines of the form
# <name/>
# are replaced from a definition file. The template is XML-like because
# angle brackets and ampersands can be freely used as long as they don't
# match the perl pattern <\w*\/> (which no valid C, C++, or Fortran code will)
#
# The file of definitions has the following form, also in an XML-like
# format (for simple parsing)
# <TESTDEFN filename="fname">
# <blockname>
# definition
# </blockname>
# ...
# </TESTDEFN>
# where "blockname" is an arbitrary name (matching the perl expression \w*)
# that matches the names in the template file.
# TESTDEFN is a required field
#
# Possible extensions:
# Common definitions for all files (allows a common template for
# multiple sets of merges)
# Allow the *template* to define some names (eg, <var>value</var>)
# that are replaced in the generated file.
#
# ----------------------------------------------------------------------------
# Global variables
$debug = 0;
$lang = "Fortran";
%knownLang = ( "Fortran" => 1, "C" => 1, "C++" => 1 );
#$lang = "C";
#$lang = "C++";
# Definitions
%Definitions = ();
# Global definitions are for all files, and can contain standard comments,
# initializations, and other data
%GlobalDefinitions = ();
# Read a definition file
# ReadDefinition( filename )
# Places the definitions into %Definitions{name} => content
sub ReadDefinition {
my $DFD = $_[0];
while (<$DFD>) {
# Check for end-of-description
if (/<\/TESTDEFN>/) { last; }
# match definition name
if (/<(\w*)>/) {
my $name = $1;
my $defn = "";
my $found = 0;
while (<$DFD>) {
if (/<\/$name>/) { $found = 1; last; }
s/\r//g; # Remove any extraneous characters
$defn .= $_;
}
# If we didn't close the definition, generate an error message
if (! $found) {
print STDERR "Read to end-of-file while looking for </$name>\n";
}
$Definitions{$name} = $defn;
}
else {
# Skip (blank space, comment, etc)
next;
}
}
}
# ReadGlobalDefinitions( filename )
sub ReadGlobalDefinitions {
my $filename = $_[0];
# Save Definitions, if any
my %saveDefinitions = %Definitions;
# Reset Definitions to hold the current global set
%Definitions = %GlobalDefinitions;
my $DFD = "DFD";
open $DFD, "<$filename" || die "Could not open global definition file $filename\n";
print "Opening $filename..\n";
&ReadDefinition( $DFD );
close $DFD;
%GlobalDefinitions = %Definitions;
%Definitions = %saveDefinitions;
# Print the new definitions if requested.
if ($debug) {
foreach my $key (keys(%GlobalDefinitions)) {
my $val = $GlobalDefinitions{$key};
print "$key => $val\n";
}
}
}
# ---------------------------------------------------------------------------
# MergeTemplate
# Read a template and merge the output
# MergeTemplate( template file, output file )
# Preserve indentation
sub MergeTemplate {
my $templateFilename = $_[0];
my $outputFilename = $_[1];
open IFD, "<$templateFilename" || die "Cannot open $templateFilename\n";
open OFD, ">$outputFilename" || die "Cannot open $outputFilename\n";
while (<IFD>) {
s/\r//;
my $loopLimit = 20;
while (/(\s*)<(\w*)\/>/) {
my $indent = $1;
my $name = $2;
$indent =~ s/\s*\n//g;
if ($loopLimit-- <= 0) {
print STDERR "Exceeded loop limit while writing $outputFilename\n";
print STDERR "Searching for $name in $_";
last;
}
if (defined($Definitions{$name})) {
my $defn = $Definitions{$name};
my $newdefn = "";
# Add indentation to definition; substitute any definitions
foreach my $line (split(/\n/,$defn)) {
print "Looking at |$line|\n" if $debug;
$newdefn .= $indent . $line . "\n";
}
chop $newdefn;
print "Replacing <$name> with |$newdefn|\n" if $debug;
s/$indent<$name\/>/$newdefn/; # Only do one at a time
}
elsif (defined($GlobalDefinitions{$name})) {
# local definitions can override any global definitions
my $defn = $GlobalDefinitions{$name};
my $newdefn = "";
# Add indentation to definition
foreach my $line (split(/\n/,$defn)) {
$newdefn .= $indent . $line . "\n";
}
print "Replacing <$name> with |$newdefn|\n" if $debug;
chop $newdefn;
s/$indent<$name\/>/$newdefn/; # Only do one at a time
}
else {
# Unknown name!
print STDERR "Unknown name $name in template file when creating $outputFilename!\n";
last;
}
}
&printLine( OFD, $_ );
}
close OFD;
close IFD;
}
# ---------------------------------------------------------------------------
# ReadAndMerge( description file, template file )
sub ReadAndMerge {
my $filename = $_[0];
my $templateFile = $_[1];
$DFD = "DFD";
open $DFD, "<$filename" || die "Can not open $filename\n";
while (<$DFD>) {
s/#.*//; # Remove comments
# Read until a TESTDEFN line
if (/<TESTDEFN\s+filename=\"(.*)\">/) {
my $outputFile = $1;
%Definitions = ();
# Read until </TESTDEFN>
&ReadDefinition( $DFD );
# Create the merged file
&MergeTemplate( $templateFile, $outputFile );
}
elsif (/<LANG>([\w\+]*)<\/LANG>/) {
# Special for language definition
$lang = $1;
if (!defined($knownLang{$lang})) {
print STDERR "Unknown language $lang\n";
}
}
elsif (/<(\w*)>/) {
my $name = $1;
my $defn = "";
# read this as a global definition
while (<$DFD>) {
if (/<\/$name>/) { last; }
s/\r//;
$defn .= $_;
}
if (eof($DFD)) { print STDERR "found EOF before end of $name\n"; }
$GlobalDefinitions{$name} = $defn;
}
}
close $DFD;
}
# --------------------------------------------------------------------------
# Debug
sub PrintDefinitions {
foreach my $name (keys(%Definitions)) {
print "<$name>\n";
my $defn = $Definitions{$name};
# Here we could consider doing replacement for embedded <name>...</name>,
# for things like arguments.
print $defn;
print "</$name>\n";
}
}
# --------------------------------------------------------------------------
# printLine handles any continuation conventions
# printLine ( FD, lines )
# Note that a very simple approach works for Fortran because blanks
# are ignored. However, we will try to make the code easier to read
sub printLine {
my $OFD = $_[0];
my $lines = $_[1];
# Make sure that we get the current conventions
if ($lang eq "Fortran") {
$maxPrintLine = 72;
$postLine = " &";
$preLine = " &";
}
elsif ($lang eq "C" || $lang eq "C++") {
$maxPrintLine = 180;
$postLine = "";
$preLine = "\t";
}
foreach my $line (split(/\n/,$lines)) {
# Compute length
my $length = length($line);
while ($length > $maxPrintLine) {
# For Fortran 90 and C/C++, lines must be
# broken at whitespace. Fortran 77 ignores whitespace,
my $subline = substr $line, 0, $maxPrintLine;
# Now, break subline at the last non-letter
if ($subline =~ /(.*)([^\w]\S*)$/) {
$subline = $1;
$line = $2 . $line;
# Add blanks to end of the subline to match what was stripped
# off
my $len = length($2);
for (my $i=0; $i < $len; $i++) {
$subline .= " ";
}
}
print $OFD $subline . $postLine . "\n";
$line = substr $line, $maxPrintLine;
$line = $preLine . $line;
$length = length($line);
}
print $OFD $line . "\n";
}
}
# --------------------------------------------------------------------------
# Process the file
# Still to do:
# Allow multiple definition files, to allow for common definitions
# for such things as headers
my $defnFile = "";
my $tmplFile = "";
my $posCount = 0;
for (@ARGV) {
if (/-defn=(.*)/) {
my $extraFile = $1;
&ReadGlobalDefinitions( $extraFile );
}
elsif (/-lang=(.*)/) {
$lang = $1;
if (!defined($knownLang{$lang})) {
print STDERR "Unknown language $lang\n";
exit 1;
}
}
elsif (/^-/) {
print STDERR "Unrecognized argument $_\n";
exit 1;
}
else {
if ($posCount == 0) {
$defnFile = $_;
}
elsif ($posCount == 1) {
$tmplFile = $_;
}
else {
print STDERR "Too many arguments ($_)\n";
exit 1;
}
$posCount ++;
}
}
if ($defnFile eq "" || $tmplFile eq "") {
print STDERR "testmerge [ -defn=name ] defintion-file template-file \n";
exit 1;
}
&ReadAndMerge( $defnFile, $tmplFile );
|