1568d7f4 |
#!/usr/bin/perl
|
0039dd40 |
# Simple macro processor.
# Macros are defined in a control file that follows |
1568d7f4 |
# a simple definition-based grammar as documented in the
# trans script. Stdin is then copied to stdout, and any
# occurrence of @@MACRO@@ is substituted. Macros can also
# be specified on the command line. |
f9ad66c7 |
|
1568d7f4 |
die "usage: macro [-O<openquote>] [-C<closequote>] [-Dname=var ...] [control-file ...] " if (@ARGV < 1); |
f9ad66c7 |
%Parms = (); |
1568d7f4 |
$open_quote = "@@";
$close_quote = "@@"; |
f9ad66c7 |
|
1568d7f4 |
while ($arg=shift(@ARGV)) { |
657ecf14 |
if ($arg =~ /^-/) {
if ($arg =~ /^-D(\w+)(?:=(.*))?$/) {
$Parms{$1} = $2
} elsif ($arg =~ /-O(.*)$/) {
$open_quote = $1;
} elsif ($arg =~ /-C(.*)$/) {
$close_quote = $1; |
1568d7f4 |
} else { |
657ecf14 |
die "unrecognized option: $arg"; |
1568d7f4 |
} |
657ecf14 |
} else {
open(CONTROL, "< $arg") or die "cannot open $arg";
while (<CONTROL>) {
if (/^!define\s+(\w+)(?:\s+['"]?(.*?)['"]?)?\s*$/) {
$Parms{$1} = $2;
}
}
}
}
sub print_symbol_table {
foreach my $k (sort (keys(%Parms))) {
my $v = $Parms{$k};
print "[$k] -> \"$v\"\n";
} |
f9ad66c7 |
}
|
657ecf14 |
#print_symbol_table ();
#exit 0;
|
f9ad66c7 |
while (<STDIN>) {
s{ |
1568d7f4 |
\Q$open_quote\E |
f9ad66c7 |
\s*
( |
657ecf14 |
\w+
) |
f9ad66c7 |
\s* |
1568d7f4 |
\Q$close_quote\E |
f9ad66c7 |
}{
$Parms{$1} |
657ecf14 |
}xge; |
f9ad66c7 |
print;
} |