6 $Getopt::Std::STANDARD_HELP_VERSION = 1;
8 my $trysource = "try.c";
11 getopts('fF:ekvI:', \my %opt) or pod2usage();
13 my($expr, @headers) = @ARGV ? splice @ARGV : "-";
15 pod2usage "-f and -F <tool> are exclusive\n" if $opt{f} and $opt{F};
17 foreach($trysource, $tryout) {
19 die "You already have a $_" if -e $_;
23 warn "reading from stdin...\n";
24 $expr = do { local $/; <> };
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";
30 if (!(@ARGV = @headers)) {
31 open my $fh, '<', 'MANIFEST' or die "Can't open MANIFEST: $!";
33 push @ARGV, $1 if m!^([^/]+\.h)\t!;
35 push @ARGV, 'config.h' if -f 'config.h';
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};
46 $args = '(' . join (', ', map {$argname++} 1..@args) . ')';
51 die "$macro not found\n" unless defined $header;
53 open my $out, '>', $trysource or die "Can't open $trysource: $!";
55 my $sentinel = "$macro expands to";
65 close $out or die "Can't close $trysource: $!";
67 print "doing: make $tryout\n" if $opt{v};
68 system "make $tryout" and die;
70 # if user wants 'indent' formatting ..
73 if ($opt{f} || $opt{F}) {
74 # a: indent is a well behaved filter when given 0 arguments, reading from
75 # stdin and writing to stdout
76 # b: all our braces should be balanced, indented back to column 0, in the
77 # headers, hence everything before our #line directive can be ignored
79 # We can take advantage of this to reduce the work to indent.
81 my $indent_command = $opt{f} ? 'indent' : $opt{F};
83 if (defined $opt{I}) {
84 $indent_command .= " $opt{I}";
86 open $out_fh, '|-', $indent_command or die $?;
91 open my $fh, '<', $tryout or die "Can't open $tryout: $!";
94 print $out_fh $_ if /$sentinel/o .. 1;
98 foreach($trysource, $tryout) {
99 die "Can't unlink $_" unless unlink $_;
107 expand-macro.pl - expand C macros using the C preprocessor
111 expand-macro.pl [options] [ < macro-name | macro-expression | - > [headers] ]
114 -f use 'indent' to format output
115 -F <tool> use <tool> to format output (instead of -f)
116 -e erase try.[ic] instead of failing when they're present (errdetect)
117 -k keep them after generating (for handy inspection)
119 -I <indent-opts> passed into indent