X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=Porting%2Fexpand-macro.pl;h=ed8e188efaab0fd0cef54b947f6d11351b964059;hb=e9bdeacf40b4e2e5954ea00d091c146c150cf3ec;hp=472b789a32becf161a0196e4703c04143bc7b232;hpb=27c6397cdef882675fd24e716944ef4ce35abe7b;p=p5sagit%2Fp5-mst-13.2.git diff --git a/Porting/expand-macro.pl b/Porting/expand-macro.pl index 472b789..ed8e188 100644 --- a/Porting/expand-macro.pl +++ b/Porting/expand-macro.pl @@ -1,70 +1,69 @@ #!perl -w use strict; +use Pod::Usage; use Getopt::Std; +$Getopt::Std::STANDARD_HELP_VERSION = 1; -use vars qw($trysource $tryout $sentinel); -$trysource = "try.c"; -$tryout = "try.i"; +my $trysource = "try.c"; +my $tryout = "try.i"; -getopts('fF:ekvI:', \my %opt) or usage(); +getopts('fF:ekvI:', \my %opt) or pod2usage(); -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; -usage "missing " unless defined $macro; - -$sentinel = "$macro expands to"; +my($expr, @headers) = @ARGV ? splice @ARGV : "-"; -usage "-f and -F are exclusive\n" if $opt{f} and $opt{F}; +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!; } + push @ARGV, 'config.h' if -f 'config.h'; } -my $args = ''; - -my $found_macro; +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) . ')'; } - $found_macro++; + $header = $ARGV; last; } -die "$macro not found\n" unless $found_macro; +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" +EOF + +print qq{#include "$header"\n} + unless $header eq 'perl.h' or $header eq 'EXTERN.h'; + +print $out <<"EOF"; +#line 4 "$sentinel" $macro$args EOF @@ -105,3 +104,23 @@ unless ($opt{k}) { 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