X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=Porting%2Fexpand-macro.pl;h=472b789a32becf161a0196e4703c04143bc7b232;hb=f0cb210418521d8c2f9524d01cb9e27e0a1fd67f;hp=624e7dcdb4ff8b4fe1fdde594f6c7b00bc6b1127;hpb=d1f3479f8c62266471eafb582091197ae2cf4cad;p=p5sagit%2Fp5-mst-13.2.git diff --git a/Porting/expand-macro.pl b/Porting/expand-macro.pl index 624e7dc..472b789 100644 --- a/Porting/expand-macro.pl +++ b/Porting/expand-macro.pl @@ -1,16 +1,37 @@ #!perl -w use strict; +use Getopt::Std; + use vars qw($trysource $tryout $sentinel); $trysource = "try.c"; $tryout = "try.i"; +getopts('fF:ekvI:', \my %opt) or usage(); + +sub usage { + die< [headers] +options: + -f use 'indent' to format output + -F use to format output (instead of -f) + -e erase try.[ic] instead of failing when theyre present (errdetect) + -k keep them after generating (for handy inspection) + -v verbose + -I passed into indent +EO_HELP +} + my $macro = shift; -die "$0 macro [headers]" unless defined $macro; +usage "missing " unless defined $macro; $sentinel = "$macro expands to"; +usage "-f and -F are exclusive\n" if $opt{f} and $opt{F}; + foreach($trysource, $tryout) { + unlink $_ if $opt{e}; die "You already have a $_" if -e $_; } @@ -23,16 +44,20 @@ if (!@ARGV) { my $args = ''; +my $found_macro; while (<>) { - next unless /^#\s*define\s+$macro/; + next unless /^#\s*define\s+$macro\b/; my ($def_args) = /^#\s*define\s+$macro\(([^)]*)\)/; if (defined $def_args) { my @args = split ',', $def_args; + print "# macro: $macro args: @args in $_\n" if $opt{v}; my $argname = "A0"; $args = '(' . join (', ', map {$argname++} 1..@args) . ')'; } + $found_macro++; last; } +die "$macro not found\n" unless $found_macro; open my $out, '>', $trysource or die "Can't open $trysource: $!"; @@ -45,14 +70,38 @@ EOF close $out or die "Can't close $trysource: $!"; +print "doing: make $tryout\n" if $opt{v}; system "make $tryout" and die; +# if user wants 'indent' formatting .. +my $out_fh; + +if ($opt{f} || $opt{F}) { + # a: indent is a well behaved filter when given 0 arguments, reading from + # stdin and writing to stdout + # b: all our braces should be balanced, indented back to column 0, in the + # headers, hence everything before our #line directive can be ignored + # + # We can take advantage of this to reduce the work to indent. + + my $indent_command = $opt{f} ? 'indent' : $opt{F}; + + if (defined $opt{I}) { + $indent_command .= " $opt{I}"; + } + open $out_fh, '|-', $indent_command or die $?; +} else { + $out_fh = \*STDOUT; +} + open my $fh, '<', $tryout or die "Can't open $tryout: $!"; while (<$fh>) { - print if /$sentinel/o .. 1; + print $out_fh $_ if /$sentinel/o .. 1; } -foreach($trysource, $tryout) { - die "Can't unlink $_" unless unlink $_; +unless ($opt{k}) { + foreach($trysource, $tryout) { + die "Can't unlink $_" unless unlink $_; + } }