Daily bump.
[official-gcc.git] / contrib / texi2pod.pl
blobfcdc14550bae2b963821276f603269902b5c6379
1 #! /usr/bin/perl -w
3 # Copyright (C) 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
5 # This file is part of GCC.
7 # GCC is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3, or (at your option)
10 # any later version.
12 # GCC is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with GCC; see the file COPYING. If not, write to
19 # the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 # Boston MA 02110-1301, USA.
22 # This does trivial (and I mean _trivial_) conversion of Texinfo
23 # markup to Perl POD format. It's intended to be used to extract
24 # something suitable for a manpage from a Texinfo document.
26 $output = 0;
27 $skipping = 0;
28 %sects = ();
29 $section = "";
30 @icstack = ();
31 @endwstack = ();
32 @skstack = ();
33 @instack = ();
34 $shift = "";
35 %defs = ();
36 $fnno = 1;
37 $inf = "";
38 $ibase = "";
39 @ipath = ();
41 while ($_ = shift) {
42 if (/^-D(.*)$/) {
43 if ($1 ne "") {
44 $flag = $1;
45 } else {
46 $flag = shift;
48 $value = "";
49 ($flag, $value) = ($flag =~ /^([^=]+)(?:=(.+))?/);
50 die "no flag specified for -D\n"
51 unless $flag ne "";
52 die "flags may only contain letters, digits, hyphens, dashes and underscores\n"
53 unless $flag =~ /^[a-zA-Z0-9_-]+$/;
54 $defs{$flag} = $value;
55 } elsif (/^-I(.*)$/) {
56 if ($1 ne "") {
57 $flag = $1;
58 } else {
59 $flag = shift;
61 push (@ipath, $flag);
62 } elsif (/^-/) {
63 usage();
64 } else {
65 $in = $_, next unless defined $in;
66 $out = $_, next unless defined $out;
67 usage();
71 if (defined $in) {
72 $inf = gensym();
73 open($inf, "<$in") or die "opening \"$in\": $!\n";
74 $ibase = $1 if $in =~ m|^(.+)/[^/]+$|;
75 } else {
76 $inf = \*STDIN;
79 if (defined $out) {
80 open(STDOUT, ">$out") or die "opening \"$out\": $!\n";
83 while(defined $inf) {
84 while(<$inf>) {
85 # Certain commands are discarded without further processing.
86 /^\@(?:
87 [a-z]+index # @*index: useful only in complete manual
88 |need # @need: useful only in printed manual
89 |(?:end\s+)?group # @group .. @end group: ditto
90 |page # @page: ditto
91 |node # @node: useful only in .info file
92 |(?:end\s+)?ifnottex # @ifnottex .. @end ifnottex: use contents
93 )\b/x and next;
95 chomp;
97 # Look for filename and title markers.
98 /^\@setfilename\s+([^.]+)/ and $fn = $1, next;
99 /^\@settitle\s+([^.]+)/ and $tl = postprocess($1), next;
101 # Identify a man title but keep only the one we are interested in.
102 /^\@c\s+man\s+title\s+([A-Za-z0-9-]+)\s+(.+)/ and do {
103 if (exists $defs{$1}) {
104 $fn = $1;
105 $tl = postprocess($2);
107 next;
110 # Look for blocks surrounded by @c man begin SECTION ... @c man end.
111 # This really oughta be @ifman ... @end ifman and the like, but such
112 # would require rev'ing all other Texinfo translators.
113 /^\@c\s+man\s+begin\s+([A-Z]+)\s+([A-Za-z0-9-]+)/ and do {
114 $output = 1 if exists $defs{$2};
115 $sect = $1;
116 next;
118 /^\@c\s+man\s+begin\s+([A-Z]+)/ and $sect = $1, $output = 1, next;
119 /^\@c\s+man\s+end/ and do {
120 $sects{$sect} = "" unless exists $sects{$sect};
121 $sects{$sect} .= postprocess($section);
122 $section = "";
123 $output = 0;
124 next;
127 # handle variables
128 /^\@set\s+([a-zA-Z0-9_-]+)\s*(.*)$/ and do {
129 $defs{$1} = $2;
130 next;
132 /^\@clear\s+([a-zA-Z0-9_-]+)/ and do {
133 delete $defs{$1};
134 next;
137 next unless $output;
139 # Discard comments. (Can't do it above, because then we'd never see
140 # @c man lines.)
141 /^\@c\b/ and next;
143 # End-block handler goes up here because it needs to operate even
144 # if we are skipping.
145 /^\@end\s+([a-z]+)/ and do {
146 # Ignore @end foo, where foo is not an operation which may
147 # cause us to skip, if we are presently skipping.
148 my $ended = $1;
149 next if $skipping && $ended !~ /^(?:ifset|ifclear|ignore|menu|iftex|copying)$/;
151 die "\@end $ended without \@$ended at line $.\n" unless defined $endw;
152 die "\@$endw ended by \@end $ended at line $.\n" unless $ended eq $endw;
154 $endw = pop @endwstack;
156 if ($ended =~ /^(?:ifset|ifclear|ignore|menu|iftex)$/) {
157 $skipping = pop @skstack;
158 next;
159 } elsif ($ended =~ /^(?:example|smallexample|display)$/) {
160 $shift = "";
161 $_ = ""; # need a paragraph break
162 } elsif ($ended =~ /^(?:itemize|enumerate|[fv]?table)$/) {
163 $_ = "\n=back\n";
164 $ic = pop @icstack;
165 } elsif ($ended eq "multitable") {
166 $_ = "\n=back\n";
167 } else {
168 die "unknown command \@end $ended at line $.\n";
172 # We must handle commands which can cause skipping even while we
173 # are skipping, otherwise we will not process nested conditionals
174 # correctly.
175 /^\@ifset\s+([a-zA-Z0-9_-]+)/ and do {
176 push @endwstack, $endw;
177 push @skstack, $skipping;
178 $endw = "ifset";
179 $skipping = 1 unless exists $defs{$1};
180 next;
183 /^\@ifclear\s+([a-zA-Z0-9_-]+)/ and do {
184 push @endwstack, $endw;
185 push @skstack, $skipping;
186 $endw = "ifclear";
187 $skipping = 1 if exists $defs{$1};
188 next;
191 /^\@(ignore|menu|iftex|copying)\b/ and do {
192 push @endwstack, $endw;
193 push @skstack, $skipping;
194 $endw = $1;
195 $skipping = 1;
196 next;
199 next if $skipping;
201 # Character entities. First the ones that can be replaced by raw text
202 # or discarded outright:
203 s/\@copyright\{\}/(c)/g;
204 s/\@dots\{\}/.../g;
205 s/\@enddots\{\}/..../g;
206 s/\@([.!? ])/$1/g;
207 s/\@[:-]//g;
208 s/\@bullet(?:\{\})?/*/g;
209 s/\@TeX\{\}/TeX/g;
210 s/\@pounds\{\}/\#/g;
211 s/\@minus(?:\{\})?/-/g;
212 s/\\,/,/g;
214 # Now the ones that have to be replaced by special escapes
215 # (which will be turned back into text by unmunge())
216 s/&/&amp;/g;
217 s/\@\{/&lbrace;/g;
218 s/\@\}/&rbrace;/g;
219 s/\@\@/&at;/g;
221 # Inside a verbatim block, handle @var, @samp and @url specially.
222 if ($shift ne "") {
223 s/\@var\{([^\}]*)\}/<$1>/g;
224 s/\@samp\{([^\}]*)\}/"$1"/g;
225 s/\@url\{([^\}]*)\}/<$1>/g;
228 # POD doesn't interpret E<> inside a verbatim block.
229 if ($shift eq "") {
230 s/</&lt;/g;
231 s/>/&gt;/g;
232 } else {
233 s/</&LT;/g;
234 s/>/&GT;/g;
237 # Single line command handlers.
239 /^\@include\s+(.+)$/ and do {
240 push @instack, $inf;
241 $inf = gensym();
242 $file = postprocess($1);
244 # Try cwd and $ibase, then explicit -I paths.
245 $done = 0;
246 foreach $path ("", $ibase, @ipath) {
247 $mypath = $file;
248 $mypath = $path . "/" . $mypath if ($path ne "");
249 open($inf, "<" . $mypath) and ($done = 1, last);
251 die "cannot find $file" if !$done;
252 next;
255 /^\@(?:section|unnumbered|unnumberedsec|center|heading)\s+(.+)$/
256 and $_ = "\n=head2 $1\n";
257 /^\@subsection\s+(.+)$/
258 and $_ = "\n=head3 $1\n";
259 /^\@subsubsection\s+(.+)$/
260 and $_ = "\n=head4 $1\n";
262 # Block command handlers:
263 /^\@itemize(?:\s+(\@[a-z]+|\*|-))?/ and do {
264 push @endwstack, $endw;
265 push @icstack, $ic;
266 if (defined $1) {
267 $ic = $1;
268 } else {
269 $ic = '*';
271 $_ = "\n=over 4\n";
272 $endw = "itemize";
275 /^\@enumerate(?:\s+([a-zA-Z0-9]+))?/ and do {
276 push @endwstack, $endw;
277 push @icstack, $ic;
278 if (defined $1) {
279 $ic = $1 . ".";
280 } else {
281 $ic = "1.";
283 $_ = "\n=over 4\n";
284 $endw = "enumerate";
287 /^\@multitable\s.*/ and do {
288 push @endwstack, $endw;
289 $endw = "multitable";
290 $_ = "\n=over 4\n";
293 /^\@([fv]?table)\s+(\@[a-z]+)/ and do {
294 push @endwstack, $endw;
295 push @icstack, $ic;
296 $endw = $1;
297 $ic = $2;
298 $ic =~ s/\@(?:samp|strong|key|gcctabopt|env)/B/;
299 $ic =~ s/\@(?:code|kbd)/C/;
300 $ic =~ s/\@(?:dfn|var|emph|cite|i)/I/;
301 $ic =~ s/\@(?:file)/F/;
302 $ic =~ s/\@(?:asis)//;
303 $_ = "\n=over 4\n";
306 /^\@((?:small)?example|display)/ and do {
307 push @endwstack, $endw;
308 $endw = $1;
309 $shift = "\t";
310 $_ = ""; # need a paragraph break
313 /^\@item\s+(.*\S)\s*$/ and $endw eq "multitable" and do {
314 @columns = ();
315 for $column (split (/\s*\@tab\s*/, $1)) {
316 # @strong{...} is used a @headitem work-alike
317 $column =~ s/^\@strong{(.*)}$/$1/;
318 push @columns, $column;
320 $_ = "\n=item ".join (" : ", @columns)."\n";
323 /^\@itemx?\s*(.+)?$/ and do {
324 if (defined $1) {
325 if ($ic) {
326 if ($endw eq "enumerate") {
327 $_ = "\n=item $ic $1\n";
328 $ic =~ s/(\d+)/$1 + 1/eg;
329 } else {
330 # Entity escapes prevent munging by the <>
331 # processing below.
332 $_ = "\n=item $ic\&LT;$1\&GT;\n";
334 } else {
335 $_ = "\n=item $1\n";
337 } else {
338 $_ = "\n=item $ic\n";
339 $ic =~ y/A-Ya-y/B-Zb-z/;
340 $ic =~ s/(\d+)/$1 + 1/eg;
344 $section .= $shift.$_."\n";
346 # End of current file.
347 close($inf);
348 $inf = pop @instack;
351 die "No filename or title\n" unless defined $fn && defined $tl;
353 $sects{NAME} = "$fn \- $tl\n";
354 $sects{FOOTNOTES} .= "=back\n" if exists $sects{FOOTNOTES};
356 for $sect (qw(NAME SYNOPSIS DESCRIPTION OPTIONS ENVIRONMENT FILES
357 BUGS NOTES FOOTNOTES SEEALSO AUTHOR COPYRIGHT)) {
358 if(exists $sects{$sect}) {
359 $head = $sect;
360 $head =~ s/SEEALSO/SEE ALSO/;
361 print "=head1 $head\n\n";
362 print scalar unmunge ($sects{$sect});
363 print "\n";
367 sub usage
369 die "usage: $0 [-D toggle...] [infile [outfile]]\n";
372 sub postprocess
374 local $_ = $_[0];
376 # @value{foo} is replaced by whatever 'foo' is defined as.
377 while (m/(\@value\{([a-zA-Z0-9_-]+)\})/g) {
378 if (! exists $defs{$2}) {
379 print STDERR "Option $2 not defined\n";
380 s/\Q$1\E//;
381 } else {
382 $value = $defs{$2};
383 s/\Q$1\E/$value/;
387 # Formatting commands.
388 # Temporary escape for @r.
389 s/\@r\{([^\}]*)\}/R<$1>/g;
390 s/\@(?:dfn|var|emph|cite|i)\{([^\}]*)\}/I<$1>/g;
391 s/\@(?:code|kbd)\{([^\}]*)\}/C<$1>/g;
392 s/\@(?:samp|strong|key|option|env|command|b)\{([^\}]*)\}/B<$1>/g;
393 s/\@sc\{([^\}]*)\}/\U$1/g;
394 s/\@file\{([^\}]*)\}/F<$1>/g;
395 s/\@w\{([^\}]*)\}/S<$1>/g;
396 s/\@(?:dmn|math)\{([^\}]*)\}/$1/g;
398 # keep references of the form @ref{...}, print them bold
399 s/\@(?:ref)\{([^\}]*)\}/B<$1>/g;
401 # Change double single quotes to double quotes.
402 s/''/"/g;
403 s/``/"/g;
405 # Cross references are thrown away, as are @noindent and @refill.
406 # (@noindent is impossible in .pod, and @refill is unnecessary.)
407 # @* is also impossible in .pod; we discard it and any newline that
408 # follows it. Similarly, our macro @gol must be discarded.
410 s/\(?\@xref\{(?:[^\}]*)\}(?:[^.<]|(?:<[^<>]*>))*\.\)?//g;
411 s/\s+\(\@pxref\{(?:[^\}]*)\}\)//g;
412 s/;\s+\@pxref\{(?:[^\}]*)\}//g;
413 s/\@noindent\s*//g;
414 s/\@refill//g;
415 s/\@gol//g;
416 s/\@\*\s*\n?//g;
418 # Anchors are thrown away
419 s/\@anchor\{(?:[^\}]*)\}//g;
421 # @uref can take one, two, or three arguments, with different
422 # semantics each time. @url and @email are just like @uref with
423 # one argument, for our purposes.
424 s/\@(?:uref|url|email)\{([^\},]*)\}/&lt;B<$1>&gt;/g;
425 s/\@uref\{([^\},]*),([^\},]*)\}/$2 (C<$1>)/g;
426 s/\@uref\{([^\},]*),([^\},]*),([^\},]*)\}/$3/g;
428 # Handle gccoptlist here, so it can contain the above formatting
429 # commands.
430 s/\@gccoptlist\{([^\}]*)\}/B<$1>/g;
432 # Un-escape <> at this point.
433 s/&LT;/</g;
434 s/&GT;/>/g;
436 # Now un-nest all B<>, I<>, R<>. Theoretically we could have
437 # indefinitely deep nesting; in practice, one level suffices.
438 1 while s/([BIR])<([^<>]*)([BIR])<([^<>]*)>/$1<$2>$3<$4>$1</g;
440 # Replace R<...> with bare ...; eliminate empty markup, B<>;
441 # shift white space at the ends of [BI]<...> expressions outside
442 # the expression.
443 s/R<([^<>]*)>/$1/g;
444 s/[BI]<>//g;
445 s/([BI])<(\s+)([^>]+)>/$2$1<$3>/g;
446 s/([BI])<([^>]+?)(\s+)>/$1<$2>$3/g;
448 # Extract footnotes. This has to be done after all other
449 # processing because otherwise the regexp will choke on formatting
450 # inside @footnote.
451 while (/\@footnote/g) {
452 s/\@footnote\{([^\}]+)\}/[$fnno]/;
453 add_footnote($1, $fnno);
454 $fnno++;
457 return $_;
460 sub unmunge
462 # Replace escaped symbols with their equivalents.
463 local $_ = $_[0];
465 s/&lt;/E<lt>/g;
466 s/&gt;/E<gt>/g;
467 s/&lbrace;/\{/g;
468 s/&rbrace;/\}/g;
469 s/&at;/\@/g;
470 s/&amp;/&/g;
471 return $_;
474 sub add_footnote
476 unless (exists $sects{FOOTNOTES}) {
477 $sects{FOOTNOTES} = "\n=over 4\n\n";
480 $sects{FOOTNOTES} .= "=item $fnno.\n\n"; $fnno++;
481 $sects{FOOTNOTES} .= $_[0];
482 $sects{FOOTNOTES} .= "\n\n";
485 # stolen from Symbol.pm
487 my $genseq = 0;
488 sub gensym
490 my $name = "GEN" . $genseq++;
491 my $ref = \*{$name};
492 delete $::{$name};
493 return $ref;