3 # Copyright (C) 1999-2023 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)
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.
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;
55 } elsif (/^-I(.*)$/) {
65 $in = $_, next unless defined $in;
66 $out = $_, next unless defined $out;
73 open($inf, "<$in") or die "opening \"$in\": $!\n";
74 $ibase = $1 if $in =~ m
|^(.+)/[^/]+$|;
80 open(STDOUT
, ">$out") or die "opening \"$out\": $!\n";
85 # Certain commands are discarded without further processing.
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
91 |node
# @node: useful only in .info file
92 |(?
:end\s
+)?ifnottex
# @ifnottex .. @end ifnottex: use contents
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}) {
105 $tl = postprocess
($2);
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};
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);
128 /^\@set\s+([a-zA-Z0-9_-]+)\s*(.*)$/ and do {
132 /^\@clear\s+([a-zA-Z0-9_-]+)/ and do {
139 # Discard comments. (Can't do it above, because then we'd never see
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.
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;
159 } elsif ($ended =~ /^(?:example|smallexample|display)$/) {
161 $_ = ""; # need a paragraph break
162 } elsif ($ended =~ /^(?:itemize|enumerate|[fv]?table)$/) {
165 } elsif ($ended eq "multitable") {
169 die "unknown command \@end $ended at line $.\n";
173 # We must handle commands which can cause skipping even while we
174 # are skipping, otherwise we will not process nested conditionals
176 /^\@ifset\s+([a-zA-Z0-9_-]+)/ and do {
177 push @endwstack, $endw;
178 push @skstack, $skipping;
180 $skipping = 1 unless exists $defs{$1};
184 /^\@ifclear\s+([a-zA-Z0-9_-]+)/ and do {
185 push @endwstack, $endw;
186 push @skstack, $skipping;
188 $skipping = 1 if exists $defs{$1};
192 /^\@(ignore|menu|iftex|copying)\b/ and do {
193 push @endwstack, $endw;
194 push @skstack, $skipping;
202 # Character entities. First the ones that can be replaced by raw text
203 # or discarded outright:
204 s/\@copyright\{\}/(c)/g;
206 s/\@enddots\{\}/..../g;
209 s/\@bullet(?:\{\})?/*/g;
212 s/\@minus(?:\{\})?/-/g;
216 # Now the ones that have to be replaced by special escapes
217 # (which will be turned back into text by unmunge())
218 # Replace @@ before @{ and @} in order to parse @samp{@@} correctly.
223 s/\@`\{(.)\}/&$1grave;/g;
225 # Inside a verbatim block, handle @var, @samp and @url specially.
227 s/\@var\{([^\}]*)\}/<$1>/g;
228 s/\@samp\{([^\}]*)\}/"$1"/g;
229 s/\@url\{([^\}]*)\}/<$1>/g;
232 # POD doesn't interpret E<> inside a verbatim block.
241 # Single line command handlers.
243 /^\@include\s+(.+)$/ and do {
246 $file = postprocess
($1);
248 # Try cwd and $ibase, then explicit -I paths.
250 foreach $path ("", $ibase, @ipath) {
252 $mypath = $path . "/" . $mypath if ($path ne "");
253 open($inf, "<" . $mypath) and ($done = 1, last);
255 die "cannot find $file" if !$done;
259 /^\@(?:section|unnumbered|unnumberedsec|center|heading)\s+(.+)$/
260 and $_ = "\n=head2 $1\n";
261 /^\@subsection\s+(.+)$/
262 and $_ = "\n=head3 $1\n";
263 /^\@subsubsection\s+(.+)$/
264 and $_ = "\n=head4 $1\n";
266 # Block command handlers:
267 /^\@itemize(?:\s+(\@[a-z]+|\*|-))?/ and do {
268 push @endwstack, $endw;
279 /^\@enumerate(?:\s+([a-zA-Z0-9]+))?/ and do {
280 push @endwstack, $endw;
291 /^\@multitable\s.*/ and do {
292 push @endwstack, $endw;
294 $endw = "multitable";
299 /^\@([fv]?table)\s+(\@[a-z]+)/ and do {
300 push @endwstack, $endw;
304 $ic =~ s/\@(?:samp|strong|key|gcctabopt|env)/B/;
305 $ic =~ s/\@(?:code|kbd)/C/;
306 $ic =~ s/\@(?:dfn|var|emph|cite|i)/I/;
307 $ic =~ s/\@(?:file)/F/;
308 $ic =~ s/\@(?:asis)//;
312 /^\@((?:small)?example|display)/ and do {
313 push @endwstack, $endw;
316 $_ = ""; # need a paragraph break
319 /^\@(headitem|item)\s+(.*\S)\s*$/ and $endw eq "multitable" and do {
322 for $column (split (/\s*\@tab\s*/, $2)) {
323 # @strong{...} is used a @headitem work-alike
324 $column =~ s/^\@strong\{(.*)\}$/$1/;
325 $column = "I<$column>" if $item eq "headitem";
326 push @columns, $column;
328 $_ = "\n=item ".join (" : ", @columns)."\n";
331 /^\@itemx?\s*(.+)?$/ and do {
334 if ($endw eq "enumerate") {
335 $_ = "\n=item $ic $1\n";
336 $ic =~ s/(\d+)/$1 + 1/eg;
338 # Entity escapes prevent munging by the <>
340 $_ = "\n=item $ic\<$1\>\n";
346 $_ = "\n=item Z\<\>$ic\n";
347 $ic =~ y/A-Ya-y/B-Zb-z/;
348 $ic =~ s/(\d+)/$1 + 1/eg;
352 $section .= $shift.$_."\n";
354 # End of current file.
359 die "No filename or title\n" unless defined $fn && defined $tl;
361 $sects{NAME
} = "$fn \- $tl\n";
362 $sects{FOOTNOTES
} .= "=back\n" if exists $sects{FOOTNOTES
};
364 for $sect (qw(NAME SYNOPSIS DESCRIPTION OPTIONS ENVIRONMENT FILES
365 BUGS NOTES FOOTNOTES SEEALSO AUTHOR COPYRIGHT)) {
366 if(exists $sects{$sect}) {
368 $head =~ s/SEEALSO/SEE ALSO/;
369 print "=head1 $head\n\n";
370 print scalar unmunge
($sects{$sect});
377 die "usage: $0 [-D toggle...] [infile [outfile]]\n";
384 # @value{foo} is replaced by whatever 'foo' is defined as.
385 while (m/(\@value\{([a-zA-Z0-9_-]+)\})/g) {
386 if (! exists $defs{$2}) {
387 print STDERR
"Option $2 not defined\n";
395 # Formatting commands.
396 # Temporary escape for @r.
397 s/\@r\{([^\}]*)\}/R<$1>/g;
398 s/\@sc\{([^\}]*)\}/\U$1/g;
399 s/\@(?:dfn|var|emph|cite|i)\{([^\}]*)\}/I<$1>/g;
400 s/\@(?:code|kbd)\{([^\}]*)\}/C<$1>/g;
401 s/\@(?:samp|strong|key|option|env|command|b)\{([^\}]*)\}/B<$1>/g;
402 s/\@acronym\{([^\}]*)\}/\U$1/g;
403 s/\@file\{([^\}]*)\}/F<$1>/g;
404 s/\@w\{([^\}]*)\}/S<$1>/g;
405 s/\@(?:dmn|math)\{([^\}]*)\}/$1/g;
407 s/\@t\{([^\}]*)\}/$1/g;
409 # keep references of the form @ref{...}, print them bold
410 s/\@(?:ref)\{([^\}]*)\}/B<$1>/g;
412 # Change double single quotes to double quotes.
416 # Cross references are thrown away, as are @noindent and @refill.
417 # (@noindent is impossible in .pod, and @refill is unnecessary.)
418 # @* is also impossible in .pod; we discard it and any newline that
419 # follows it. Similarly, our macro @gol must be discarded.
421 s/\(?\@xref\{(?:[^\}]*)\}(?:[^.<]|(?:<[^<>]*>))*\.\)?//g;
422 s/\s+\(\@pxref\{(?:[^\}]*)\}\)//g;
423 s/;\s+\@pxref\{(?:[^\}]*)\}//g;
429 # Anchors are thrown away
430 s/\@anchor\{(?:[^\}]*)\}//g;
432 # @uref can take one, two, or three arguments, with different
433 # semantics each time. @url and @email are just like @uref with
434 # one argument, for our purposes.
435 s/\@(?:uref|url|email)\{([^\},]*)\}/<B<$1>>/g;
436 s/\@uref\{([^\},]*),([^\},]*)\}/$2 (C<$1>)/g;
437 s/\@uref\{([^\},]*),([^\},]*),([^\},]*)\}/$3/g;
439 # Handle gccoptlist here, so it can contain the above formatting
441 s/\@gccoptlist\{([^\}]*)\}/B<$1>/g;
443 # Un-escape <> at this point.
447 # Now un-nest all B<>, I<>, R<>. Theoretically we could have
448 # indefinitely deep nesting; in practice, one level suffices.
449 1 while s/([BIR])<([^<>]*)([BIR])<([^<>]*)>/$1<$2>$3<$4>$1</g;
451 # Replace R<...> with bare ...; eliminate empty markup, B<>;
452 # shift white space at the ends of [BI]<...> expressions outside
456 s/([BI])<(\s+)([^>]+)>/$2$1<$3>/g;
457 s/([BI])<([^>]+?)(\s+)>/$1<$2>$3/g;
459 # Extract footnotes. This has to be done after all other
460 # processing because otherwise the regexp will choke on formatting
462 while (/\@footnote/g) {
463 s/\@footnote\{([^\}]+)\}/[$fnno]/;
464 add_footnote
($1, $fnno);
473 # Replace escaped symbols with their equivalents.
476 s/&(.)grave;/E<$1grave>/g;
488 unless (exists $sects{FOOTNOTES
}) {
489 $sects{FOOTNOTES
} = "\n=over 4\n\n";
492 $sects{FOOTNOTES
} .= "=item $fnno.\n\n"; $fnno++;
493 $sects{FOOTNOTES
} .= $_[0];
494 $sects{FOOTNOTES
} .= "\n\n";
497 # stolen from Symbol.pm
502 my $name = "GEN" . $genseq++;