3 # Copyright (C) 1999, 2000, 2001, 2003, 2007, 2009, 2010 Free Software
6 # This file is part of GCC.
8 # GCC is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3, or (at your option)
13 # GCC is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with GCC. If not, see <http://www.gnu.org/licenses/>.
21 # This does trivial (and I mean _trivial_) conversion of Texinfo
22 # markup to Perl POD format. It's intended to be used to extract
23 # something suitable for a manpage from a Texinfo document.
26 BEGIN { eval { require warnings; } and warnings->import; }
50 ($flag, $value) = ($flag =~ /^([^=]+)(?:=(.+))?/);
51 die "no flag specified for -D\n"
53 die "flags may only contain letters, digits, hyphens, dashes and underscores\n"
54 unless $flag =~ /^[a-zA-Z0-9_-]+$/;
55 $defs{$flag} = $value;
59 $in = $_, next unless defined $in;
60 $out = $_, next unless defined $out;
67 open($inf, "<$in") or die "opening \"$in\": $!\n";
68 $ibase = $1 if $in =~ m|^(.+)/[^/]+$|;
74 open(STDOUT, ">$out") or die "opening \"$out\": $!\n";
79 # Certain commands are discarded without further processing.
81 [a-z]+index # @*index: useful only in complete manual
82 |need # @need: useful only in printed manual
83 |(?:end\s+)?group # @group .. @end group: ditto
85 |node # @node: useful only in .info file
86 |(?:end\s+)?ifnottex # @ifnottex .. @end ifnottex: use contents
91 # Look for filename and title markers.
92 /^\@setfilename\s+([^.]+)/ and $fn = $1, next;
93 /^\@settitle\s+([^.]+)/ and $tl = postprocess($1), next;
95 # Identify a man title but keep only the one we are interested in.
96 /^\@c\s+man\s+title\s+([A-Za-z0-9-]+)\s+(.+)/ and do {
97 if (exists $defs{$1}) {
99 $tl = postprocess($2);
104 # Look for blocks surrounded by @c man begin SECTION ... @c man end.
105 # This really oughta be @ifman ... @end ifman and the like, but such
106 # would require rev'ing all other Texinfo translators.
107 /^\@c\s+man\s+begin\s+([A-Z]+)\s+([A-Za-z0-9-]+)/ and do {
108 $output = 1 if exists $defs{$2};
112 /^\@c\s+man\s+begin\s+([A-Z]+)/ and $sect = $1, $output = 1, next;
113 /^\@c\s+man\s+end/ and do {
114 $sects{$sect} = "" unless exists $sects{$sect};
115 $sects{$sect} .= postprocess($section);
122 /^\@set\s+([a-zA-Z0-9_-]+)\s*(.*)$/ and do {
126 /^\@clear\s+([a-zA-Z0-9_-]+)/ and do {
133 # Discard comments. (Can't do it above, because then we'd never see
137 # End-block handler goes up here because it needs to operate even
138 # if we are skipping.
139 /^\@end\s+([a-z]+)/ and do {
140 # Ignore @end foo, where foo is not an operation which may
141 # cause us to skip, if we are presently skipping.
143 next if $skipping && $ended !~ /^(?:ifset|ifclear|ignore|menu|iftex|copying)$/;
145 die "\@end $ended without \@$ended at line $.\n" unless defined $endw;
146 die "\@$endw ended by \@end $ended at line $.\n" unless $ended eq $endw;
148 $endw = pop @endwstack;
150 if ($ended =~ /^(?:ifset|ifclear|ignore|menu|iftex)$/) {
151 $skipping = pop @skstack;
153 } elsif ($ended =~ /^(?:example|smallexample|display)$/) {
155 $_ = ""; # need a paragraph break
156 } elsif ($ended =~ /^(?:itemize|enumerate|[fv]?table)$/) {
160 die "unknown command \@end $ended at line $.\n";
164 # We must handle commands which can cause skipping even while we
165 # are skipping, otherwise we will not process nested conditionals
167 /^\@ifset\s+([a-zA-Z0-9_-]+)/ and do {
168 push @endwstack, $endw;
169 push @skstack, $skipping;
171 $skipping = 1 unless exists $defs{$1};
175 /^\@ifclear\s+([a-zA-Z0-9_-]+)/ and do {
176 push @endwstack, $endw;
177 push @skstack, $skipping;
179 $skipping = 1 if exists $defs{$1};
183 /^\@(ignore|menu|iftex|copying)\b/ and do {
184 push @endwstack, $endw;
185 push @skstack, $skipping;
193 # Character entities. First the ones that can be replaced by raw text
194 # or discarded outright:
195 s/\@copyright\{\}/(c)/g;
197 s/\@enddots\{\}/..../g;
200 s/\@bullet(?:\{\})?/*/g;
203 s/\@minus(?:\{\})?/-/g;
206 # Now the ones that have to be replaced by special escapes
207 # (which will be turned back into text by unmunge())
213 # Inside a verbatim block, handle @var specially.
215 s/\@var\{([^\}]*)\}/<$1>/g;
218 # POD doesn't interpret E<> inside a verbatim block.
227 # Single line command handlers.
229 /^\@include\s+(.+)$/ and do {
232 $file = postprocess($1);
234 # Try cwd and $ibase.
235 open($inf, "<" . $file)
236 or open($inf, "<" . $ibase . "/" . $file)
237 or die "cannot open $file or $ibase/$file: $!\n";
241 /^\@(?:section|unnumbered|unnumberedsec|center)\s+(.+)$/
242 and $_ = "\n=head2 $1\n";
243 /^\@subsection\s+(.+)$/
244 and $_ = "\n=head3 $1\n";
246 # Block command handlers:
247 /^\@itemize(?:\s+(\@[a-z]+|\*|-))?/ and do {
248 push @endwstack, $endw;
259 /^\@enumerate(?:\s+([a-zA-Z0-9]+))?/ and do {
260 push @endwstack, $endw;
271 /^\@([fv]?table)\s+(\@[a-z]+)/ and do {
272 push @endwstack, $endw;
276 $ic =~ s/\@(?:samp|strong|key|gcctabopt|env)/B/;
277 $ic =~ s/\@(?:code|kbd)/C/;
278 $ic =~ s/\@(?:dfn|var|emph|cite|i)/I/;
279 $ic =~ s/\@(?:file)/F/;
283 /^\@((?:small)?example|display)/ and do {
284 push @endwstack, $endw;
287 $_ = ""; # need a paragraph break
290 /^\@itemx?\s*(.+)?$/ and do {
293 if ($ic =~ /\@asis/) {
294 $_ = "\n=item $thing\n";
296 # Entity escapes prevent munging by the <> processing below.
297 $_ = "\n=item $ic\<$thing\>\n";
300 $_ = "\n=item $ic\n";
301 $ic =~ y/A-Ya-y/B-Zb-z/;
302 $ic =~ s/(\d+)/$1 + 1/eg;
306 $section .= $shift.$_."\n";
308 # End of current file.
313 die "No filename or title\n" unless defined $fn && defined $tl;
315 $sects{NAME} = "$fn \- $tl\n";
316 $sects{FOOTNOTES} .= "=back\n" if exists $sects{FOOTNOTES};
318 for $sect (qw(NAME SYNOPSIS DESCRIPTION OPTIONS ENVIRONMENT EXITSTATUS
319 FILES BUGS NOTES FOOTNOTES SEEALSO AUTHOR COPYRIGHT)) {
320 if(exists $sects{$sect}) {
322 $head =~ s/SEEALSO/SEE ALSO/;
323 $head =~ s/EXITSTATUS/EXIT STATUS/;
324 print "=head1 $head\n\n";
325 print scalar unmunge ($sects{$sect});
332 die "usage: $0 [-D toggle...] [infile [outfile]]\n";
339 # @value{foo} is replaced by whatever 'foo' is defined as.
340 while (m/(\@value\{([a-zA-Z0-9_-]+)\})/g) {
341 if (! exists $defs{$2}) {
342 print STDERR "Option $2 not defined\n";
350 # Formatting commands.
351 # Temporary escape for @r.
352 s/\@r\{([^\}]*)\}/R<$1>/g;
353 s/\@(?:dfn|var|emph|cite|i)\{([^\}]*)\}/I<$1>/g;
354 s/\@(?:code|kbd)\{([^\}]*)\}/C<$1>/g;
355 s/\@(?:gccoptlist|samp|strong|key|option|env|command|b)\{([^\}]*)\}/B<$1>/g;
356 s/\@sc\{([^\}]*)\}/\U$1/g;
357 s/\@file\{([^\}]*)\}/F<$1>/g;
358 s/\@w\{([^\}]*)\}/S<$1>/g;
359 s/\@(?:dmn|math)\{([^\}]*)\}/$1/g;
361 # keep references of the form @ref{...}, print them bold
362 s/\@(?:ref)\{([^\}]*)\}/B<$1>/g;
364 # Change double single quotes to double quotes.
368 # Cross references are thrown away, as are @noindent and @refill.
369 # (@noindent is impossible in .pod, and @refill is unnecessary.)
370 # @* is also impossible in .pod; we discard it and any newline that
371 # follows it. Similarly, our macro @gol must be discarded.
373 s/\(?\@xref\{(?:[^\}]*)\}(?:[^.<]|(?:<[^<>]*>))*\.\)?//g;
374 s/\s+\(\@pxref\{(?:[^\}]*)\}\)//g;
375 s/;\s+\@pxref\{(?:[^\}]*)\}//g;
381 # @uref can take one, two, or three arguments, with different
382 # semantics each time. @url and @email are just like @uref with
383 # one argument, for our purposes.
384 s/\@(?:uref|url|email)\{([^\},]*)\}/<B<$1>>/g;
385 s/\@uref\{([^\},]*),([^\},]*)\}/$2 (C<$1>)/g;
386 s/\@uref\{([^\},]*),([^\},]*),([^\},]*)\}/$3/g;
388 # Un-escape <> at this point.
392 # Now un-nest all B<>, I<>, R<>. Theoretically we could have
393 # indefinitely deep nesting; in practice, one level suffices.
394 1 while s/([BIR])<([^<>]*)([BIR])<([^<>]*)>/$1<$2>$3<$4>$1</g;
396 # Replace R<...> with bare ...; eliminate empty markup, B<>;
397 # shift white space at the ends of [BI]<...> expressions outside
401 s/([BI])<(\s+)([^>]+)>/$2$1<$3>/g;
402 s/([BI])<([^>]+?)(\s+)>/$1<$2>$3/g;
404 # Extract footnotes. This has to be done after all other
405 # processing because otherwise the regexp will choke on formatting
407 while (/\@footnote/g) {
408 s/\@footnote\{([^\}]+)\}/[$fnno]/;
409 add_footnote($1, $fnno);
418 # Replace escaped symbols with their equivalents.
432 unless (exists $sects{FOOTNOTES}) {
433 $sects{FOOTNOTES} = "\n=over 4\n\n";
436 $sects{FOOTNOTES} .= "=item $fnno.\n\n"; $fnno++;
437 $sects{FOOTNOTES} .= $_[0];
438 $sects{FOOTNOTES} .= "\n\n";
441 # stolen from Symbol.pm
446 my $name = "GEN" . $genseq++;