This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regcomp.sym: Clarify comment
[perl5.git] / Porting / expand-macro.pl
1 #!perl -w
2 use strict;
3
4 use Pod::Usage;
5 use Getopt::Std;
6 $Getopt::Std::STANDARD_HELP_VERSION = 1;
7
8 my $trysource = "try.c";
9 my $tryout = "try.i";
10
11 getopts('fF:ekvI:X', \my %opt) or pod2usage();
12
13 my($expr, @headers) = @ARGV ? splice @ARGV : "-";
14
15 pod2usage "-f and -F <tool> are exclusive\n" if $opt{f} and $opt{F};
16
17 foreach($trysource, $tryout) {
18     unlink $_ if $opt{e};
19     die "You already have a $_" if -e $_;
20 }
21
22 if ($expr eq '-') {
23     warn "reading from stdin...\n";
24     $expr = do { local $/; <> };
25 }
26
27 my($macro, $args) = $expr =~ /^\s*(\w+)((?:\s*\(.*\))?)\s*;?\s*$/s
28     or pod2usage "$expr doesn't look like a macro-name or macro-expression to me";
29
30 if (!(@ARGV = @headers)) {
31     open my $fh, '<', 'MANIFEST' or die "Can't open MANIFEST: $!";
32     while (<$fh>) {
33         push @ARGV, $1 if m!^([^/]+\.h)\t!;
34     }
35     push @ARGV, 'config.h' if -f 'config.h';
36 }
37
38 my $header;
39 while (<>) {
40     next unless /^#\s*define\s+$macro\b/;
41     my ($def_args) = /^#\s*define\s+$macro\(([^)]*)\)/;
42     if (defined $def_args && !$args) {
43         my @args = split ',', $def_args;
44         print "# macro: $macro args: @args in $_\n" if $opt{v};
45         my $argname = "A0";
46         $args = '(' . join (', ', map {$argname++} 1..@args) . ')';
47     }
48     $header = $ARGV;
49     last;
50 }
51 die "$macro not found\n" unless defined $header;
52
53 open my $out, '>', $trysource or die "Can't open $trysource: $!";
54
55 my $sentinel = "$macro expands to";
56
57 my %done_header;
58
59 sub do_header {
60     my $header = shift;
61     return if $done_header{$header}++;
62     print $out qq{#include "$header"\n};
63 }
64
65 print $out <<'EOF' if $opt{X};
66 /* Need to do this like this, as cflags.sh sets it for us come what may.  */
67 #undef PERL_CORE
68
69 EOF
70
71 do_header('EXTERN.h');
72 do_header('perl.h');
73 do_header($header);
74 do_header('XSUB.h') if $opt{X};
75
76 print $out <<"EOF";
77 #line 4 "$sentinel"
78 $macro$args
79 EOF
80
81 close $out or die "Can't close $trysource: $!";
82
83 print "doing: make $tryout\n" if $opt{v};
84 system "make $tryout" and die;
85
86 # if user wants 'indent' formatting ..
87 my $out_fh;
88
89 if ($opt{f} || $opt{F}) {
90     # a: indent is a well behaved filter when given 0 arguments, reading from
91     #    stdin and writing to stdout
92     # b: all our braces should be balanced, indented back to column 0, in the
93     #    headers, hence everything before our #line directive can be ignored
94     #
95     # We can take advantage of this to reduce the work to indent.
96
97     my $indent_command = $opt{f} ? 'indent' : $opt{F};
98
99     if (defined $opt{I}) {
100         $indent_command .= " $opt{I}";
101     }
102     open $out_fh, '|-', $indent_command or die $?;
103 } else {
104     $out_fh = \*STDOUT;
105 }
106
107 open my $fh, '<', $tryout or die "Can't open $tryout: $!";
108
109 while (<$fh>) {
110     print $out_fh $_ if /$sentinel/o .. 1;
111 }
112
113 unless ($opt{k}) {
114     foreach($trysource, $tryout) {
115         die "Can't unlink $_" unless unlink $_;
116     }
117 }
118
119 __END__
120
121 =head1 NAME
122
123 expand-macro.pl - expand C macros using the C preprocessor
124
125 =head1 SYNOPSIS
126
127   expand-macro.pl [options] [ < macro-name | macro-expression | - > [headers] ]
128
129   options:
130     -f          use 'indent' to format output
131     -F  <tool>  use <tool> to format output  (instead of -f)
132     -e          erase try.[ic] instead of failing when they're present (errdetect)
133     -k          keep them after generating (for handy inspection)
134     -v          verbose
135     -I <indent-opts>    passed into indent
136     -X          include "XSUB.h" (and undefine PERL_CORE)
137
138 =cut