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