X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=Porting%2Fexpand-macro.pl;h=2cdaa79727edf3f2e75af5b191056370af2cc79c;hb=12fc04c96e084fc3d1f066acd95f1b212e9bc1e3;hp=caa4b472e19367f74112cb546f874b7827c921c8;hpb=e25a7dc23f32d0dd4f1c4c8645877a39bdb9d4b7;p=p5sagit%2Fp5-mst-13.2.git diff --git a/Porting/expand-macro.pl b/Porting/expand-macro.pl index caa4b47..2cdaa79 100644 --- a/Porting/expand-macro.pl +++ b/Porting/expand-macro.pl @@ -1,58 +1,120 @@ #!perl -w use strict; -use vars qw($trysource $tryout $sentinel); -$trysource = "try.c"; -$tryout = "try.i"; +use Pod::Usage; +use Getopt::Std; +$Getopt::Std::STANDARD_HELP_VERSION = 1; -my $macro = shift; -die "$0 macro [headers]" unless defined $macro; +my $trysource = "try.c"; +my $tryout = "try.i"; -$sentinel = "$macro expands to"; +getopts('fF:ekvI:', \my %opt) or pod2usage(); + +my($expr, @headers) = @ARGV ? splice @ARGV : "-"; + +pod2usage "-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 $_; } -if (!@ARGV) { +if ($expr eq '-') { + warn "reading from stdin...\n"; + $expr = do { local $/; <> }; +} + +my($macro, $args) = $expr =~ /^\s*(\w+)((?:\s*\(.*\))?)\s*;?\s*$/s + or pod2usage "$expr doesn't look like a macro-name or macro-expression to me"; + +if (!(@ARGV = @headers)) { open my $fh, '<', 'MANIFEST' or die "Can't open MANIFEST: $!"; while (<$fh>) { push @ARGV, $1 if m!^([^/]+\.h)\t!; } } -my $args = ''; - +my $header; while (<>) { next unless /^#\s*define\s+$macro\b/; my ($def_args) = /^#\s*define\s+$macro\(([^)]*)\)/; - if (defined $def_args) { + if (defined $def_args && !$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) . ')'; } + $header = $ARGV; last; } +die "$macro not found\n" unless defined $header; open my $out, '>', $trysource or die "Can't open $trysource: $!"; +my $sentinel = "$macro expands to"; + print $out <<"EOF"; #include "EXTERN.h" #include "perl.h" -#line 3 "$sentinel" +#include "$header" +#line 4 "$sentinel" $macro$args 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 $_; + } } + +__END__ + +=head1 NAME + +expand-macro.pl - expand C macros using the C preprocessor + +=head1 SYNOPSIS + + expand-macro.pl [options] [ < macro-name | macro-expression | - > [headers] ] + + options: + -f use 'indent' to format output + -F use to format output (instead of -f) + -e erase try.[ic] instead of failing when they're present (errdetect) + -k keep them after generating (for handy inspection) + -v verbose + -I passed into indent + +=cut