Upgrade to podlators 2.1.2
[p5sagit/p5-mst-13.2.git] / Porting / expand-macro.pl
CommitLineData
d1f3479f 1#!perl -w
2use strict;
3
a93e78e3 4use Getopt::Std;
5
d1f3479f 6use vars qw($trysource $tryout $sentinel);
7$trysource = "try.c";
8$tryout = "try.i";
9
a93e78e3 10getopts('fF:ekvI:', \my %opt) or usage();
11
12sub usage {
13 die<<EO_HELP;
14@_;
15usage: $0 [options] <macro-name> [headers]
16options:
17 -f use 'indent' to format output
18 -F <tool> use <tool> to format output (instead of -f)
19 -e erase try.[ic] instead of failing when theyre present (errdetect)
20 -k keep them after generating (for handy inspection)
21 -v verbose
22 -I <indent-opts> passed into indent
23EO_HELP
24}
25
d1f3479f 26my $macro = shift;
a93e78e3 27usage "missing <macro-name>" unless defined $macro;
d1f3479f 28
29$sentinel = "$macro expands to";
30
a93e78e3 31usage "-f and -F <tool> are exclusive\n" if $opt{f} and $opt{F};
32
d1f3479f 33foreach($trysource, $tryout) {
a93e78e3 34 unlink $_ if $opt{e};
d1f3479f 35 die "You already have a $_" if -e $_;
36}
37
38if (!@ARGV) {
39 open my $fh, '<', 'MANIFEST' or die "Can't open MANIFEST: $!";
40 while (<$fh>) {
41 push @ARGV, $1 if m!^([^/]+\.h)\t!;
42 }
43}
44
45my $args = '';
46
a93e78e3 47my $found_macro;
d1f3479f 48while (<>) {
e25a7dc2 49 next unless /^#\s*define\s+$macro\b/;
d1f3479f 50 my ($def_args) = /^#\s*define\s+$macro\(([^)]*)\)/;
51 if (defined $def_args) {
52 my @args = split ',', $def_args;
a93e78e3 53 print "# macro: $macro args: @args in $_\n" if $opt{v};
d1f3479f 54 my $argname = "A0";
55 $args = '(' . join (', ', map {$argname++} 1..@args) . ')';
56 }
a93e78e3 57 $found_macro++;
d1f3479f 58 last;
59}
a93e78e3 60die "$macro not found\n" unless $found_macro;
d1f3479f 61
62open my $out, '>', $trysource or die "Can't open $trysource: $!";
63
64print $out <<"EOF";
65#include "EXTERN.h"
66#include "perl.h"
67#line 3 "$sentinel"
68$macro$args
69EOF
70
71close $out or die "Can't close $trysource: $!";
72
a93e78e3 73print "doing: make $tryout\n" if $opt{v};
d1f3479f 74system "make $tryout" and die;
75
a93e78e3 76# if user wants 'indent' formatting ..
27c6397c 77my $out_fh;
78
79if ($opt{f} || $opt{F}) {
80 # a: indent is a well behaved filter when given 0 arguments, reading from
81 # stdin and writing to stdout
82 # b: all our braces should be balanced, indented back to column 0, in the
83 # headers, hence everything before our #line directive can be ignored
84 #
85 # We can take advantage of this to reduce the work to indent.
86
87 my $indent_command = $opt{f} ? 'indent' : $opt{F};
88
89 if (defined $opt{I}) {
90 $indent_command .= " $opt{I}";
91 }
92 open $out_fh, '|-', $indent_command or die $?;
93} else {
94 $out_fh = \*STDOUT;
95}
a93e78e3 96
d1f3479f 97open my $fh, '<', $tryout or die "Can't open $tryout: $!";
98
99while (<$fh>) {
27c6397c 100 print $out_fh $_ if /$sentinel/o .. 1;
d1f3479f 101}
102
a93e78e3 103unless ($opt{k}) {
104 foreach($trysource, $tryout) {
105 die "Can't unlink $_" unless unlink $_;
106 }
d1f3479f 107}