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
CommitLineData
d1f3479f
NC
1#!perl -w
2use strict;
3
d5f33267 4use Pod::Usage;
a93e78e3 5use Getopt::Std;
d5f33267 6$Getopt::Std::STANDARD_HELP_VERSION = 1;
a93e78e3 7
d5f33267
MHM
8my $trysource = "try.c";
9my $tryout = "try.i";
d1f3479f 10
8553b68c 11getopts('fF:ekvI:X', \my %opt) or pod2usage();
a93e78e3 12
d5f33267 13my($expr, @headers) = @ARGV ? splice @ARGV : "-";
d1f3479f 14
d5f33267 15pod2usage "-f and -F <tool> are exclusive\n" if $opt{f} and $opt{F};
a93e78e3 16
d1f3479f 17foreach($trysource, $tryout) {
a93e78e3 18 unlink $_ if $opt{e};
d1f3479f
NC
19 die "You already have a $_" if -e $_;
20}
21
d5f33267
MHM
22if ($expr eq '-') {
23 warn "reading from stdin...\n";
24 $expr = do { local $/; <> };
25}
26
27my($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
30if (!(@ARGV = @headers)) {
d1f3479f
NC
31 open my $fh, '<', 'MANIFEST' or die "Can't open MANIFEST: $!";
32 while (<$fh>) {
33 push @ARGV, $1 if m!^([^/]+\.h)\t!;
34 }
ed690650 35 push @ARGV, 'config.h' if -f 'config.h';
d1f3479f
NC
36}
37
be4f373d 38my $header;
d1f3479f 39while (<>) {
e25a7dc2 40 next unless /^#\s*define\s+$macro\b/;
d1f3479f 41 my ($def_args) = /^#\s*define\s+$macro\(([^)]*)\)/;
d5f33267 42 if (defined $def_args && !$args) {
d1f3479f 43 my @args = split ',', $def_args;
a93e78e3 44 print "# macro: $macro args: @args in $_\n" if $opt{v};
d1f3479f
NC
45 my $argname = "A0";
46 $args = '(' . join (', ', map {$argname++} 1..@args) . ')';
47 }
be4f373d 48 $header = $ARGV;
d1f3479f
NC
49 last;
50}
be4f373d 51die "$macro not found\n" unless defined $header;
d1f3479f
NC
52
53open my $out, '>', $trysource or die "Can't open $trysource: $!";
54
d5f33267
MHM
55my $sentinel = "$macro expands to";
56
8553b68c
NC
57my %done_header;
58
59sub do_header {
60 my $header = shift;
61 return if $done_header{$header}++;
62 print $out qq{#include "$header"\n};
63}
64
65print $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
4784c5e8
NC
69EOF
70
8553b68c
NC
71do_header('EXTERN.h');
72do_header('perl.h');
73do_header($header);
74do_header('XSUB.h') if $opt{X};
4784c5e8
NC
75
76print $out <<"EOF";
be4f373d 77#line 4 "$sentinel"
d1f3479f
NC
78$macro$args
79EOF
80
81close $out or die "Can't close $trysource: $!";
82
a93e78e3 83print "doing: make $tryout\n" if $opt{v};
d1f3479f
NC
84system "make $tryout" and die;
85
a93e78e3 86# if user wants 'indent' formatting ..
27c6397c
NC
87my $out_fh;
88
89if ($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}
a93e78e3 106
d1f3479f
NC
107open my $fh, '<', $tryout or die "Can't open $tryout: $!";
108
109while (<$fh>) {
27c6397c 110 print $out_fh $_ if /$sentinel/o .. 1;
d1f3479f
NC
111}
112
a93e78e3
JC
113unless ($opt{k}) {
114 foreach($trysource, $tryout) {
115 die "Can't unlink $_" unless unlink $_;
116 }
d1f3479f 117}
d5f33267
MHM
118
119__END__
120
121=head1 NAME
122
123expand-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
8553b68c 136 -X include "XSUB.h" (and undefine PERL_CORE)
d5f33267
MHM
137
138=cut