]> git.ipfire.org Git - thirdparty/gcc.git/blame - contrib/texi2pod.pl
Reorganise machmode.h headers
[thirdparty/gcc.git] / contrib / texi2pod.pl
CommitLineData
6251188c
ZW
1#! /usr/bin/perl -w
2
386c4027 3# Copyright (C) 1999-2014 Free Software Foundation, Inc.
6e26f4aa 4
487a9427 5# This file is part of GCC.
6e26f4aa 6
487a9427 7# GCC is free software; you can redistribute it and/or modify
6e26f4aa 8# it under the terms of the GNU General Public License as published by
3dfb41c5 9# the Free Software Foundation; either version 3, or (at your option)
6e26f4aa
JM
10# any later version.
11
487a9427 12# GCC is distributed in the hope that it will be useful,
6e26f4aa
JM
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.
16
17# You should have received a copy of the GNU General Public License
487a9427 18# along with GCC; see the file COPYING. If not, write to
89ee9c70
KC
19# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20# Boston MA 02110-1301, USA.
6e26f4aa 21
6251188c
ZW
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.
25
6251188c 26$output = 0;
3d6f4d76 27$skipping = 0;
6251188c
ZW
28%sects = ();
29$section = "";
30@icstack = ();
31@endwstack = ();
3d6f4d76 32@skstack = ();
049b03f4 33@instack = ();
6251188c 34$shift = "";
3d6f4d76 35%defs = ();
b75d998f 36$fnno = 1;
049b03f4
ZW
37$inf = "";
38$ibase = "";
84309a32 39@ipath = ();
6251188c 40
b75d998f 41while ($_ = shift) {
3d6f4d76
ZW
42 if (/^-D(.*)$/) {
43 if ($1 ne "") {
44 $flag = $1;
45 } else {
46 $flag = shift;
47 }
3da33af3
MK
48 $value = "";
49 ($flag, $value) = ($flag =~ /^([^=]+)(?:=(.+))?/);
3d6f4d76
ZW
50 die "no flag specified for -D\n"
51 unless $flag ne "";
3da33af3 52 die "flags may only contain letters, digits, hyphens, dashes and underscores\n"
3d6f4d76 53 unless $flag =~ /^[a-zA-Z0-9_-]+$/;
3da33af3 54 $defs{$flag} = $value;
84309a32
DJ
55 } elsif (/^-I(.*)$/) {
56 if ($1 ne "") {
57 $flag = $1;
58 } else {
59 $flag = shift;
60 }
61 push (@ipath, $flag);
3d6f4d76
ZW
62 } elsif (/^-/) {
63 usage();
64 } else {
65 $in = $_, next unless defined $in;
66 $out = $_, next unless defined $out;
67 usage();
68 }
69}
70
71if (defined $in) {
049b03f4
ZW
72 $inf = gensym();
73 open($inf, "<$in") or die "opening \"$in\": $!\n";
74 $ibase = $1 if $in =~ m|^(.+)/[^/]+$|;
75} else {
76 $inf = \*STDIN;
3d6f4d76 77}
049b03f4 78
3d6f4d76
ZW
79if (defined $out) {
80 open(STDOUT, ">$out") or die "opening \"$out\": $!\n";
81}
82
049b03f4
ZW
83while(defined $inf) {
84while(<$inf>) {
b75d998f
ZW
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
77bd67cb 92 |(?:end\s+)?ifnottex # @ifnottex .. @end ifnottex: use contents
b75d998f 93 )\b/x and next;
3da33af3 94
6251188c 95 chomp;
b75d998f
ZW
96
97 # Look for filename and title markers.
98 /^\@setfilename\s+([^.]+)/ and $fn = $1, next;
3da33af3
MK
99 /^\@settitle\s+([^.]+)/ and $tl = postprocess($1), next;
100
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);
106 }
107 next;
108 };
b75d998f
ZW
109
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.
3da33af3
MK
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;
117 };
118 /^\@c\s+man\s+begin\s+([A-Z]+)/ and $sect = $1, $output = 1, next;
119 /^\@c\s+man\s+end/ and do {
f4e8dec6
ZW
120 $sects{$sect} = "" unless exists $sects{$sect};
121 $sects{$sect} .= postprocess($section);
6251188c
ZW
122 $section = "";
123 $output = 0;
124 next;
125 };
3da33af3
MK
126
127 # handle variables
049b03f4
ZW
128 /^\@set\s+([a-zA-Z0-9_-]+)\s*(.*)$/ and do {
129 $defs{$1} = $2;
130 next;
131 };
132 /^\@clear\s+([a-zA-Z0-9_-]+)/ and do {
133 delete $defs{$1};
134 next;
135 };
3da33af3 136
b75d998f 137 next unless $output;
3d6f4d76 138
b75d998f
ZW
139 # Discard comments. (Can't do it above, because then we'd never see
140 # @c man lines.)
141 /^\@c\b/ and next;
6251188c 142
b75d998f
ZW
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 {
f4e8dec6
ZW
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;
d6bef02d 149 next if $skipping && $ended !~ /^(?:ifset|ifclear|ignore|menu|iftex|copying)$/;
f4e8dec6
ZW
150
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;
6251188c 153
b75d998f 154 $endw = pop @endwstack;
6251188c 155
77bd67cb 156 if ($ended =~ /^(?:ifset|ifclear|ignore|menu|iftex)$/) {
b75d998f
ZW
157 $skipping = pop @skstack;
158 next;
77bd67cb 159 } elsif ($ended =~ /^(?:example|smallexample|display)$/) {
f4e8dec6
ZW
160 $shift = "";
161 $_ = ""; # need a paragraph break
28852cc6 162 } elsif ($ended =~ /^(?:itemize|enumerate|[fv]?table)$/) {
b75d998f
ZW
163 $_ = "\n=back\n";
164 $ic = pop @icstack;
427e84f7
RS
165 } elsif ($ended eq "multitable") {
166 $_ = "\n=back\n";
f4e8dec6
ZW
167 } else {
168 die "unknown command \@end $ended at line $.\n";
3d6f4d76
ZW
169 }
170 };
f4e8dec6
ZW
171
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;
181 };
182
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;
189 };
190
d6bef02d 191 /^\@(ignore|menu|iftex|copying)\b/ and do {
f4e8dec6
ZW
192 push @endwstack, $endw;
193 push @skstack, $skipping;
194 $endw = $1;
195 $skipping = 1;
196 next;
197 };
198
3d6f4d76
ZW
199 next if $skipping;
200
b75d998f
ZW
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;
445c435a 212 s/\\,/,/g;
6251188c 213
b75d998f
ZW
214 # Now the ones that have to be replaced by special escapes
215 # (which will be turned back into text by unmunge())
ab940b73 216 # Replace @@ before @{ and @} in order to parse @samp{@@} correctly.
b75d998f 217 s/&/&amp;/g;
ab940b73 218 s/\@\@/&at;/g;
b75d998f
ZW
219 s/\@\{/&lbrace;/g;
220 s/\@\}/&rbrace;/g;
ab940b73 221 s/\@`\{(.)\}/&$1grave;/g;
77bd67cb 222
7f9766e4 223 # Inside a verbatim block, handle @var, @samp and @url specially.
77bd67cb
JM
224 if ($shift ne "") {
225 s/\@var\{([^\}]*)\}/<$1>/g;
7f9766e4
JM
226 s/\@samp\{([^\}]*)\}/"$1"/g;
227 s/\@url\{([^\}]*)\}/<$1>/g;
77bd67cb
JM
228 }
229
b75d998f
ZW
230 # POD doesn't interpret E<> inside a verbatim block.
231 if ($shift eq "") {
232 s/</&lt;/g;
233 s/>/&gt;/g;
234 } else {
235 s/</&LT;/g;
236 s/>/&GT;/g;
237 }
238
239 # Single line command handlers.
3d6f4d76 240
049b03f4
ZW
241 /^\@include\s+(.+)$/ and do {
242 push @instack, $inf;
243 $inf = gensym();
901097db 244 $file = postprocess($1);
049b03f4 245
84309a32
DJ
246 # Try cwd and $ibase, then explicit -I paths.
247 $done = 0;
5dd59f65
DJ
248 foreach $path ("", $ibase, @ipath) {
249 $mypath = $file;
250 $mypath = $path . "/" . $mypath if ($path ne "");
251 open($inf, "<" . $mypath) and ($done = 1, last);
84309a32
DJ
252 }
253 die "cannot find $file" if !$done;
049b03f4
ZW
254 next;
255 };
256
7f9766e4 257 /^\@(?:section|unnumbered|unnumberedsec|center|heading)\s+(.+)$/
049b03f4
ZW
258 and $_ = "\n=head2 $1\n";
259 /^\@subsection\s+(.+)$/
260 and $_ = "\n=head3 $1\n";
d0fc3f06
MK
261 /^\@subsubsection\s+(.+)$/
262 and $_ = "\n=head4 $1\n";
b75d998f
ZW
263
264 # Block command handlers:
9fa09038 265 /^\@itemize(?:\s+(\@[a-z]+|\*|-))?/ and do {
6251188c
ZW
266 push @endwstack, $endw;
267 push @icstack, $ic;
9fa09038
MK
268 if (defined $1) {
269 $ic = $1;
270 } else {
3caecafa 271 $ic = '*';
9fa09038 272 }
6251188c
ZW
273 $_ = "\n=over 4\n";
274 $endw = "itemize";
275 };
276
77bd67cb 277 /^\@enumerate(?:\s+([a-zA-Z0-9]+))?/ and do {
6251188c
ZW
278 push @endwstack, $endw;
279 push @icstack, $ic;
b75d998f
ZW
280 if (defined $1) {
281 $ic = $1 . ".";
282 } else {
283 $ic = "1.";
284 }
6251188c
ZW
285 $_ = "\n=over 4\n";
286 $endw = "enumerate";
287 };
288
427e84f7
RS
289 /^\@multitable\s.*/ and do {
290 push @endwstack, $endw;
291 $endw = "multitable";
292 $_ = "\n=over 4\n";
293 };
294
049b03f4 295 /^\@([fv]?table)\s+(\@[a-z]+)/ and do {
6251188c
ZW
296 push @endwstack, $endw;
297 push @icstack, $ic;
049b03f4
ZW
298 $endw = $1;
299 $ic = $2;
2642624b 300 $ic =~ s/\@(?:samp|strong|key|gcctabopt|env)/B/;
6251188c 301 $ic =~ s/\@(?:code|kbd)/C/;
b75d998f 302 $ic =~ s/\@(?:dfn|var|emph|cite|i)/I/;
6251188c 303 $ic =~ s/\@(?:file)/F/;
7b6cb843 304 $ic =~ s/\@(?:asis)//;
6251188c 305 $_ = "\n=over 4\n";
6251188c
ZW
306 };
307
77bd67cb 308 /^\@((?:small)?example|display)/ and do {
6251188c
ZW
309 push @endwstack, $endw;
310 $endw = $1;
311 $shift = "\t";
f4e8dec6 312 $_ = ""; # need a paragraph break
6251188c
ZW
313 };
314
427e84f7
RS
315 /^\@item\s+(.*\S)\s*$/ and $endw eq "multitable" and do {
316 @columns = ();
317 for $column (split (/\s*\@tab\s*/, $1)) {
318 # @strong{...} is used a @headitem work-alike
4b5ed6cf 319 $column =~ s/^\@strong\{(.*)\}$/$1/;
427e84f7
RS
320 push @columns, $column;
321 }
322 $_ = "\n=item ".join (" : ", @columns)."\n";
323 };
324
6251188c 325 /^\@itemx?\s*(.+)?$/ and do {
b75d998f 326 if (defined $1) {
7b6cb843 327 if ($ic) {
7f9766e4
JM
328 if ($endw eq "enumerate") {
329 $_ = "\n=item $ic $1\n";
330 $ic =~ s/(\d+)/$1 + 1/eg;
331 } else {
332 # Entity escapes prevent munging by the <>
333 # processing below.
334 $_ = "\n=item $ic\&LT;$1\&GT;\n";
335 }
7b6cb843
MLI
336 } else {
337 $_ = "\n=item $1\n";
338 }
b75d998f 339 } else {
386c4027 340 $_ = "\n=item Z\&LT;\&GT;$ic\n";
77bd67cb
JM
341 $ic =~ y/A-Ya-y/B-Zb-z/;
342 $ic =~ s/(\d+)/$1 + 1/eg;
6251188c
ZW
343 }
344 };
3d6f4d76 345
6251188c
ZW
346 $section .= $shift.$_."\n";
347}
049b03f4
ZW
348# End of current file.
349close($inf);
350$inf = pop @instack;
351}
6251188c 352
b75d998f
ZW
353die "No filename or title\n" unless defined $fn && defined $tl;
354
6251188c 355$sects{NAME} = "$fn \- $tl\n";
b75d998f 356$sects{FOOTNOTES} .= "=back\n" if exists $sects{FOOTNOTES};
6251188c
ZW
357
358for $sect (qw(NAME SYNOPSIS DESCRIPTION OPTIONS ENVIRONMENT FILES
b75d998f
ZW
359 BUGS NOTES FOOTNOTES SEEALSO AUTHOR COPYRIGHT)) {
360 if(exists $sects{$sect}) {
6251188c
ZW
361 $head = $sect;
362 $head =~ s/SEEALSO/SEE ALSO/;
363 print "=head1 $head\n\n";
b75d998f 364 print scalar unmunge ($sects{$sect});
6251188c
ZW
365 print "\n";
366 }
367}
3d6f4d76
ZW
368
369sub usage
370{
371 die "usage: $0 [-D toggle...] [infile [outfile]]\n";
372}
b75d998f
ZW
373
374sub postprocess
375{
376 local $_ = $_[0];
377
378 # @value{foo} is replaced by whatever 'foo' is defined as.
3da33af3
MK
379 while (m/(\@value\{([a-zA-Z0-9_-]+)\})/g) {
380 if (! exists $defs{$2}) {
381 print STDERR "Option $2 not defined\n";
382 s/\Q$1\E//;
383 } else {
384 $value = $defs{$2};
385 s/\Q$1\E/$value/;
386 }
387 }
b75d998f
ZW
388
389 # Formatting commands.
4bc1997b
JM
390 # Temporary escape for @r.
391 s/\@r\{([^\}]*)\}/R<$1>/g;
6f853fd4 392 s/\@sc\{([^\}]*)\}/\U$1/g;
b75d998f
ZW
393 s/\@(?:dfn|var|emph|cite|i)\{([^\}]*)\}/I<$1>/g;
394 s/\@(?:code|kbd)\{([^\}]*)\}/C<$1>/g;
7b6cb843 395 s/\@(?:samp|strong|key|option|env|command|b)\{([^\}]*)\}/B<$1>/g;
ab940b73 396 s/\@acronym\{([^\}]*)\}/\U$1/g;
b75d998f
ZW
397 s/\@file\{([^\}]*)\}/F<$1>/g;
398 s/\@w\{([^\}]*)\}/S<$1>/g;
399 s/\@(?:dmn|math)\{([^\}]*)\}/$1/g;
ab940b73 400 s/\@\///g;
6f853fd4 401 s/\@t\{([^\}]*)\}/$1/g;
b75d998f 402
df6e87bf
MK
403 # keep references of the form @ref{...}, print them bold
404 s/\@(?:ref)\{([^\}]*)\}/B<$1>/g;
405
0928f405
BW
406 # Change double single quotes to double quotes.
407 s/''/"/g;
408 s/``/"/g;
409
b75d998f
ZW
410 # Cross references are thrown away, as are @noindent and @refill.
411 # (@noindent is impossible in .pod, and @refill is unnecessary.)
412 # @* is also impossible in .pod; we discard it and any newline that
4bc1997b 413 # follows it. Similarly, our macro @gol must be discarded.
b75d998f 414
4bc1997b 415 s/\(?\@xref\{(?:[^\}]*)\}(?:[^.<]|(?:<[^<>]*>))*\.\)?//g;
b75d998f
ZW
416 s/\s+\(\@pxref\{(?:[^\}]*)\}\)//g;
417 s/;\s+\@pxref\{(?:[^\}]*)\}//g;
418 s/\@noindent\s*//g;
419 s/\@refill//g;
4bc1997b 420 s/\@gol//g;
b75d998f
ZW
421 s/\@\*\s*\n?//g;
422
d0fc3f06
MK
423 # Anchors are thrown away
424 s/\@anchor\{(?:[^\}]*)\}//g;
425
b75d998f
ZW
426 # @uref can take one, two, or three arguments, with different
427 # semantics each time. @url and @email are just like @uref with
428 # one argument, for our purposes.
2642624b 429 s/\@(?:uref|url|email)\{([^\},]*)\}/&lt;B<$1>&gt;/g;
b75d998f
ZW
430 s/\@uref\{([^\},]*),([^\},]*)\}/$2 (C<$1>)/g;
431 s/\@uref\{([^\},]*),([^\},]*),([^\},]*)\}/$3/g;
432
7b6cb843
MLI
433 # Handle gccoptlist here, so it can contain the above formatting
434 # commands.
435 s/\@gccoptlist\{([^\}]*)\}/B<$1>/g;
436
dd6773cd 437 # Un-escape <> at this point.
b75d998f
ZW
438 s/&LT;/</g;
439 s/&GT;/>/g;
dd6773cd
ZW
440
441 # Now un-nest all B<>, I<>, R<>. Theoretically we could have
442 # indefinitely deep nesting; in practice, one level suffices.
443 1 while s/([BIR])<([^<>]*)([BIR])<([^<>]*)>/$1<$2>$3<$4>$1</g;
444
445 # Replace R<...> with bare ...; eliminate empty markup, B<>;
446 # shift white space at the ends of [BI]<...> expressions outside
447 # the expression.
448 s/R<([^<>]*)>/$1/g;
b75d998f
ZW
449 s/[BI]<>//g;
450 s/([BI])<(\s+)([^>]+)>/$2$1<$3>/g;
451 s/([BI])<([^>]+?)(\s+)>/$1<$2>$3/g;
452
453 # Extract footnotes. This has to be done after all other
454 # processing because otherwise the regexp will choke on formatting
455 # inside @footnote.
456 while (/\@footnote/g) {
457 s/\@footnote\{([^\}]+)\}/[$fnno]/;
458 add_footnote($1, $fnno);
459 $fnno++;
460 }
461
462 return $_;
463}
464
465sub unmunge
466{
467 # Replace escaped symbols with their equivalents.
468 local $_ = $_[0];
469
ab940b73 470 s/&(.)grave;/E<$1grave>/g;
b75d998f
ZW
471 s/&lt;/E<lt>/g;
472 s/&gt;/E<gt>/g;
473 s/&lbrace;/\{/g;
474 s/&rbrace;/\}/g;
475 s/&at;/\@/g;
476 s/&amp;/&/g;
477 return $_;
478}
479
480sub add_footnote
481{
482 unless (exists $sects{FOOTNOTES}) {
483 $sects{FOOTNOTES} = "\n=over 4\n\n";
484 }
485
486 $sects{FOOTNOTES} .= "=item $fnno.\n\n"; $fnno++;
487 $sects{FOOTNOTES} .= $_[0];
488 $sects{FOOTNOTES} .= "\n\n";
489}
049b03f4
ZW
490
491# stolen from Symbol.pm
492{
493 my $genseq = 0;
494 sub gensym
495 {
496 my $name = "GEN" . $genseq++;
497 my $ref = \*{$name};
498 delete $::{$name};
499 return $ref;
500 }
501}