3 # Copyright (C) 1999, 2000, 2001, 2003, 2007 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.
49 ($flag, $value) = ($flag =~ /^([^=]+)(?:=(.+))?/);
50 die "no flag specified for -D\n"
52 die "flags may only contain letters, digits, hyphens, dashes and underscores\n"
53 unless $flag =~ /^[a-zA-Z0-9_-]+$/;
54 $defs{$flag} = $value;
58 $in = $_, next unless defined $in;
59 $out = $_, next unless defined $out;
66 open($inf, "<$in") or die "opening \"$in\": $!\n";
67 $ibase = $1 if $in =~ m|^(.+)/[^/]+$|;
73 open(STDOUT, ">$out") or die "opening \"$out\": $!\n";
78 # Certain commands are discarded without further processing.
80 [a-z]+index # @*index: useful only in complete manual
81 |need # @need: useful only in printed manual
82 |(?:end\s+)?group # @group .. @end group: ditto
84 |node # @node: useful only in .info file
85 |(?:end\s+)?ifnottex # @ifnottex .. @end ifnottex: use contents
90 # Look for filename and title markers.
91 /^\@setfilename\s+([^.]+)/ and $fn = $1, next;
92 /^\@settitle\s+([^.]+)/ and $tl = postprocess($1), next;
94 # Identify a man title but keep only the one we are interested in.
95 /^\@c\s+man\s+title\s+([A-Za-z0-9-]+)\s+(.+)/ and do {
96 if (exists $defs{$1}) {
98 $tl = postprocess($2);
103 # Look for blocks surrounded by @c man begin SECTION ... @c man end.
104 # This really oughta be @ifman ... @end ifman and the like, but such
105 # would require rev'ing all other Texinfo translators.
106 /^\@c\s+man\s+begin\s+([A-Z]+)\s+([A-Za-z0-9-]+)/ and do {
107 $output = 1 if exists $defs{$2};
111 /^\@c\s+man\s+begin\s+([A-Z]+)/ and $sect = $1, $output = 1, next;
112 /^\@c\s+man\s+end/ and do {
113 $sects{$sect} = "" unless exists $sects{$sect};
114 $sects{$sect} .= postprocess($section);
121 /^\@set\s+([a-zA-Z0-9_-]+)\s*(.*)$/ and do {
125 /^\@clear\s+([a-zA-Z0-9_-]+)/ and do {
132 # Discard comments. (Can't do it above, because then we'd never see
136 # End-block handler goes up here because it needs to operate even
137 # if we are skipping.
138 /^\@end\s+([a-z]+)/ and do {
139 # Ignore @end foo, where foo is not an operation which may
140 # cause us to skip, if we are presently skipping.
142 next if $skipping && $ended !~ /^(?:ifset|ifclear|ignore|menu|iftex|copying)$/;
144 die "\@end $ended without \@$ended at line $.\n" unless defined $endw;
145 die "\@$endw ended by \@end $ended at line $.\n" unless $ended eq $endw;
147 $endw = pop @endwstack;
149 if ($ended =~ /^(?:ifset|ifclear|ignore|menu|iftex)$/) {
150 $skipping = pop @skstack;
152 } elsif ($ended =~ /^(?:example|smallexample|display)$/) {
154 $_ = ""; # need a paragraph break
155 } elsif ($ended =~ /^(?:itemize|enumerate|[fv]?table)$/) {
159 die "unknown command \@end $ended at line $.\n";
163 # We must handle commands which can cause skipping even while we
164 # are skipping, otherwise we will not process nested conditionals
166 /^\@ifset\s+([a-zA-Z0-9_-]+)/ and do {
167 push @endwstack, $endw;
168 push @skstack, $skipping;
170 $skipping = 1 unless exists $defs{$1};
174 /^\@ifclear\s+([a-zA-Z0-9_-]+)/ and do {
175 push @endwstack, $endw;
176 push @skstack, $skipping;
178 $skipping = 1 if exists $defs{$1};
182 /^\@(ignore|menu|iftex|copying)\b/ and do {
183 push @endwstack, $endw;
184 push @skstack, $skipping;
192 # Character entities. First the ones that can be replaced by raw text
193 # or discarded outright:
194 s/\@copyright\{\}/(c)/g;
196 s/\@enddots\{\}/..../g;
199 s/\@bullet(?:\{\})?/*/g;
202 s/\@minus(?:\{\})?/-/g;
205 # Now the ones that have to be replaced by special escapes
206 # (which will be turned back into text by unmunge())
212 # Inside a verbatim block, handle @var specially.
214 s/\@var\{([^\}]*)\}/<$1>/g;
217 # POD doesn't interpret E<> inside a verbatim block.
226 # Single line command handlers.
228 /^\@include\s+(.+)$/ and do {
231 $file = postprocess($1);
233 # Try cwd and $ibase.
234 open($inf, "<" . $file)
235 or open($inf, "<" . $ibase . "/" . $file)
236 or die "cannot open $file or $ibase/$file: $!\n";
240 /^\@(?:section|unnumbered|unnumberedsec|center)\s+(.+)$/
241 and $_ = "\n=head2 $1\n";
242 /^\@subsection\s+(.+)$/
243 and $_ = "\n=head3 $1\n";
245 # Block command handlers:
246 /^\@itemize(?:\s+(\@[a-z]+|\*|-))?/ and do {
247 push @endwstack, $endw;
258 /^\@enumerate(?:\s+([a-zA-Z0-9]+))?/ and do {
259 push @endwstack, $endw;
270 /^\@([fv]?table)\s+(\@[a-z]+)/ and do {
271 push @endwstack, $endw;
275 $ic =~ s/\@(?:samp|strong|key|gcctabopt|env)/B/;
276 $ic =~ s/\@(?:code|kbd)/C/;
277 $ic =~ s/\@(?:dfn|var|emph|cite|i)/I/;
278 $ic =~ s/\@(?:file)/F/;
282 /^\@((?:small)?example|display)/ and do {
283 push @endwstack, $endw;
286 $_ = ""; # need a paragraph break
289 /^\@itemx?\s*(.+)?$/ and do {
292 if ($ic =~ /\@asis/) {
293 $_ = "\n=item $thing\n";
295 # Entity escapes prevent munging by the <> processing below.
296 $_ = "\n=item $ic\<$thing\>\n";
299 $_ = "\n=item $ic\n";
300 $ic =~ y/A-Ya-y/B-Zb-z/;
301 $ic =~ s/(\d+)/$1 + 1/eg;
305 $section .= $shift.$_."\n";
307 # End of current file.
312 die "No filename or title\n" unless defined $fn && defined $tl;
314 $sects{NAME} = "$fn \- $tl\n";
315 $sects{FOOTNOTES} .= "=back\n" if exists $sects{FOOTNOTES};
317 for $sect (qw(NAME SYNOPSIS DESCRIPTION OPTIONS ENVIRONMENT EXITSTATUS
318 FILES BUGS NOTES FOOTNOTES SEEALSO AUTHOR COPYRIGHT)) {
319 if(exists $sects{$sect}) {
321 $head =~ s/SEEALSO/SEE ALSO/;
322 $head =~ s/EXITSTATUS/EXIT STATUS/;
323 print "=head1 $head\n\n";
324 print scalar unmunge ($sects{$sect});
331 die "usage: $0 [-D toggle...] [infile [outfile]]\n";
338 # @value{foo} is replaced by whatever 'foo' is defined as.
339 while (m/(\@value\{([a-zA-Z0-9_-]+)\})/g) {
340 if (! exists $defs{$2}) {
341 print STDERR "Option $2 not defined\n";
349 # Formatting commands.
350 # Temporary escape for @r.
351 s/\@r\{([^\}]*)\}/R<$1>/g;
352 s/\@(?:dfn|var|emph|cite|i)\{([^\}]*)\}/I<$1>/g;
353 s/\@(?:code|kbd)\{([^\}]*)\}/C<$1>/g;
354 s/\@(?:gccoptlist|samp|strong|key|option|env|command|b)\{([^\}]*)\}/B<$1>/g;
355 s/\@sc\{([^\}]*)\}/\U$1/g;
356 s/\@file\{([^\}]*)\}/F<$1>/g;
357 s/\@w\{([^\}]*)\}/S<$1>/g;
358 s/\@(?:dmn|math)\{([^\}]*)\}/$1/g;
360 # keep references of the form @ref{...}, print them bold
361 s/\@(?:ref)\{([^\}]*)\}/B<$1>/g;
363 # Change double single quotes to double quotes.
367 # Cross references are thrown away, as are @noindent and @refill.
368 # (@noindent is impossible in .pod, and @refill is unnecessary.)
369 # @* is also impossible in .pod; we discard it and any newline that
370 # follows it. Similarly, our macro @gol must be discarded.
372 s/\(?\@xref\{(?:[^\}]*)\}(?:[^.<]|(?:<[^<>]*>))*\.\)?//g;
373 s/\s+\(\@pxref\{(?:[^\}]*)\}\)//g;
374 s/;\s+\@pxref\{(?:[^\}]*)\}//g;
380 # @uref can take one, two, or three arguments, with different
381 # semantics each time. @url and @email are just like @uref with
382 # one argument, for our purposes.
383 s/\@(?:uref|url|email)\{([^\},]*)\}/<B<$1>>/g;
384 s/\@uref\{([^\},]*),([^\},]*)\}/$2 (C<$1>)/g;
385 s/\@uref\{([^\},]*),([^\},]*),([^\},]*)\}/$3/g;
387 # Un-escape <> at this point.
391 # Now un-nest all B<>, I<>, R<>. Theoretically we could have
392 # indefinitely deep nesting; in practice, one level suffices.
393 1 while s/([BIR])<([^<>]*)([BIR])<([^<>]*)>/$1<$2>$3<$4>$1</g;
395 # Replace R<...> with bare ...; eliminate empty markup, B<>;
396 # shift white space at the ends of [BI]<...> expressions outside
400 s/([BI])<(\s+)([^>]+)>/$2$1<$3>/g;
401 s/([BI])<([^>]+?)(\s+)>/$1<$2>$3/g;
403 # Extract footnotes. This has to be done after all other
404 # processing because otherwise the regexp will choke on formatting
406 while (/\@footnote/g) {
407 s/\@footnote\{([^\}]+)\}/[$fnno]/;
408 add_footnote($1, $fnno);
417 # Replace escaped symbols with their equivalents.
431 unless (exists $sects{FOOTNOTES}) {
432 $sects{FOOTNOTES} = "\n=over 4\n\n";
435 $sects{FOOTNOTES} .= "=item $fnno.\n\n"; $fnno++;
436 $sects{FOOTNOTES} .= $_[0];
437 $sects{FOOTNOTES} .= "\n\n";
440 # stolen from Symbol.pm
445 my $name = "GEN" . $genseq++;