]>
git.proxmox.com Git - mirror_qemu.git/blob - scripts/texi2pod.pl
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 2, 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,
19 # 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.
52 ($flag, $value) = ($flag =~ /^([^=]+)(?:=(.+))?/);
53 die "no flag specified for -D\n"
55 die "flags may only contain letters, digits, hyphens, dashes and underscores\n"
56 unless $flag =~ /^[a-zA-Z0-9_-]+$/;
57 $defs{$flag} = $value;
58 } elsif (/^-I(.*)$/) {
68 $in = $_, next unless defined $in;
69 $out = $_, next unless defined $out;
76 open($inf, "<$in") or die "opening \"$in\": $!\n";
77 $ibase = $1 if $in =~ m|^(.+)/[^/]+$|;
83 open(STDOUT, ">$out") or die "opening
\"$out\": $!\n";
88 # Certain commands are discarded without further processing.
90 [a-z
]+index # @*index: useful only in complete manual
91 |need
# @need: useful only in printed manual
92 |(?
:end\s
+)?group
# @group .. @end group: ditto
94 |node
# @node: useful only in .info file
95 |(?
:end\s
+)?ifnottex
# @ifnottex .. @end ifnottex: use contents
100 # Look for filename and title markers.
101 /^\@setfilename\s+([^.]+)/ and $fn = $1, next;
102 /^\@settitle\s+([^.]+)/ and $tl = postprocess
($1), next;
104 # Look for document encoding
105 /^\@documentencoding\s+([^.]+)/ and do {
106 $encoding = $1 unless defined $encoding;
110 # Identify a man title but keep only the one we are interested in.
111 /^\@c\s+man\s+title\s+([A-Za-z0-9-]+)\s+(.+)/ and do {
112 if (exists $defs{$1}) {
114 $tl = postprocess
($2);
119 # Look for blocks surrounded by @c man begin SECTION ... @c man end.
120 # This really oughta be @ifman ... @end ifman and the like, but such
121 # would require rev'ing all other Texinfo translators.
122 /^\@c\s+man\s+begin\s+([A-Z]+)\s+([A-Za-z0-9-]+)/ and do {
123 $output = 1 if exists $defs{$2};
127 /^\@c\s+man\s+begin\s+([A-Z]+)/ and $sect = $1, $output = 1, next;
128 /^\@c\s+man\s+end/ and do {
129 $sects{$sect} = "" unless exists $sects{$sect};
130 $sects{$sect} .= postprocess
($section);
137 /^\@set\s+([a-zA-Z0-9_-]+)\s*(.*)$/ and do {
141 /^\@clear\s+([a-zA-Z0-9_-]+)/ and do {
148 # Discard comments. (Can't do it above, because then we'd never see
152 # End-block handler goes up here because it needs to operate even
153 # if we are skipping.
154 /^\@end\s+([a-z]+)/ and do {
155 # Ignore @end foo, where foo is not an operation which may
156 # cause us to skip, if we are presently skipping.
158 next if $skipping && $ended !~ /^(?:ifset|ifclear|ignore|menu|iftex|copying)$/;
160 die "\@end $ended without \@$ended at line $.\n" unless defined $endw;
161 die "\@$endw ended by \@end $ended at line $.\n" unless $ended eq $endw;
163 $endw = pop @endwstack;
165 if ($ended =~ /^(?:ifset|ifclear|ignore|menu|iftex)$/) {
166 $skipping = pop @skstack;
168 } elsif ($ended =~ /^(?
:example
|smallexample
|display
169 |quotation
|deftp
|deftypefn
)$/x) {
171 $_ = ""; # need a paragraph break
172 } elsif ($ended =~ /^(?:itemize|enumerate|[fv]?table)$/) {
175 } elsif ($ended eq "multitable") {
178 die "unknown command \@end $ended at line $.\n";
182 # We must handle commands which can cause skipping even while we
183 # are skipping, otherwise we will not process nested conditionals
185 /^\@ifset\s+([a-zA-Z0-9_-]+)/ and do {
186 push @endwstack, $endw;
187 push @skstack, $skipping;
189 $skipping = 1 unless exists $defs{$1};
193 /^\@ifclear\s+([a-zA-Z0-9_-]+)/ and do {
194 push @endwstack, $endw;
195 push @skstack, $skipping;
197 $skipping = 1 if exists $defs{$1};
201 /^\@(ignore|menu|iftex|copying)\b/ and do {
202 push @endwstack, $endw;
203 push @skstack, $skipping;
211 # Character entities. First the ones that can be replaced by raw text
212 # or discarded outright:
213 s/\@copyright\{\}/(c)/g;
215 s/\@enddots\{\}/..../g;
218 s/\@bullet(?:\{\})?/*/g;
221 s/\@minus(?:\{\})?/-/g;
224 # Now the ones that have to be replaced by special escapes
225 # (which will be turned back into text by unmunge())
231 # Inside a verbatim block, handle @var specially.
233 s/\@var\{([^\}]*)\}/<$1>/g;
236 # POD doesn't interpret E<> inside a verbatim block.
245 # Single line command handlers.
247 /^\@include\s+(.+)$/ and do {
250 $file = postprocess
($1);
252 # Try cwd and $ibase, then explicit -I paths.
254 foreach $path ("", $ibase, @ipath) {
256 $mypath = $path . "/" . $mypath if ($path ne "");
257 open($inf, "<" . $mypath) and ($done = 1, last);
259 die "cannot find $file" if !$done;
263 /^\@(?:section|unnumbered|unnumberedsec|center)\s+(.+)$/
264 and $_ = "\n=head2 $1\n";
265 /^\@subsection\s+(.+)$/
266 and $_ = "\n=head3 $1\n";
267 /^\@subsubsection\s+(.+)$/
268 and $_ = "\n=head4 $1\n";
270 # Block command handlers:
271 /^\@itemize(?:\s+(\@[a-z]+|\*|-))?/ and do {
272 push @endwstack, $endw;
283 /^\@enumerate(?:\s+([a-zA-Z0-9]+))?/ and do {
284 push @endwstack, $endw;
295 /^\@multitable\s.*/ and do {
296 push @endwstack, $endw;
297 $endw = "multitable";
301 /^\@([fv]?table)\s+(\@[a-z]+)/ and do {
302 push @endwstack, $endw;
306 $ic =~ s/\@(?:samp|strong|key|gcctabopt|option|env)/B/;
307 $ic =~ s/\@(?:code|kbd)/C/;
308 $ic =~ s/\@(?:dfn|var|emph|cite|i)/I/;
309 $ic =~ s/\@(?:file)/F/;
310 $ic =~ s/\@(?:asis)//;
314 /^\@((?:small)?example|display)/ and do {
315 push @endwstack, $endw;
318 $_ = ""; # need a paragraph break
321 /^\@item\s+(.*\S)\s*$/ and $endw eq "multitable" and do {
323 for $column (split (/\s*\@tab\s*/, $1)) {
324 # @strong{...} is used a @headitem work-alike
325 $column =~ s/^\@strong\{(.*)\}$/$1/;
326 push @columns, $column;
328 $_ = "\n=item ".join (" : ", @columns)."\n";
331 /^\@(quotation)\s*(.+)?$/ and do {
332 push @endwstack, $endw;
337 /^{(.*)}$|^(.*)$/ and $#args > 0 and do {
339 $arguments = $1 // "";
340 if ($endw eq "deftypefn") {
342 $fname = "B<$args[2]>";
343 $_ = $ret ?
"$ret " : "";
344 $_ .= "$fname $arguments ($kind)";
346 $_ = "B<$args[1]> ($kind)\n\n$arguments";
351 /^\@(deftp)\s*(.+)?$/ and do {
352 push @endwstack, $endw;
355 $arg =~ s/{([^}]*)}/$1/g;
357 @args = split (/ /, $arg);
361 /^\@(deftypefn)\s*(.+)?$/ and do {
362 push @endwstack, $endw;
365 $arg =~ s/{([^}]*)}/$1/g;
367 @args = split (/ /, $arg);
371 /^\@itemx?\s*(.+)?$/ and do {
376 # Entity escapes prevent munging by the <> processing below.
377 $_ = "\n=item $ic\<$1\>\n";
380 $_ = "\n=item $ic\n";
381 $ic =~ y/A-Ya-y/B-Zb-z/;
382 $ic =~ s/(\d+)/$1 + 1/eg;
386 $section .= $shift.$_."\n";
388 # End of current file.
393 die "No filename or title\n" unless defined $fn && defined $tl;
395 print "=encoding $encoding\n\n" if defined $encoding;
397 $sects{NAME
} = "$fn \- $tl\n";
398 $sects{FOOTNOTES
} .= "=back\n" if exists $sects{FOOTNOTES
};
400 for $sect (qw(NAME SYNOPSIS DESCRIPTION OPTIONS ENVIRONMENT FILES
401 BUGS NOTES FOOTNOTES SEEALSO AUTHOR COPYRIGHT)) {
402 if(exists $sects{$sect}) {
404 $head =~ s/SEEALSO/SEE ALSO/;
405 print "=head1 $head\n\n";
406 print scalar unmunge
($sects{$sect});
413 die "usage: $0 [-D toggle...] [infile [outfile]]\n";
420 # @value{foo} is replaced by whatever 'foo' is defined as.
421 while (m/(\@value\{([a-zA-Z0-9_-]+)\})/g) {
422 if (! exists $defs{$2}) {
423 print STDERR
"Option $2 not defined\n";
431 # Formatting commands.
432 # Temporary escape for @r.
433 s/\@r\{([^\}]*)\}/R<$1>/g;
434 s/\@(?:dfn|var|emph|cite|i)\{([^\}]*)\}/I<$1>/g;
435 s/\@(?:code|kbd)\{([^\}]*)\}/C<$1>/g;
436 s/\@(?:gccoptlist|samp|strong|key|option|env|command|b)\{([^\}]*)\}/B<$1>/g;
437 s/\@sc\{([^\}]*)\}/\U$1/g;
438 s/\@file\{([^\}]*)\}/F<$1>/g;
439 s/\@w\{([^\}]*)\}/S<$1>/g;
440 s/\@t\{([^\}]*)\}/$1/g;
441 s/\@(?:dmn|math)\{([^\}]*)\}/$1/g;
443 # keep references of the form @ref{...}, print them bold
444 s/\@(?:ref)\{([^\}]*)\}/B<$1>/g;
446 # Change double single quotes to double quotes.
450 # Cross references are thrown away, as are @noindent and @refill.
451 # (@noindent is impossible in .pod, and @refill is unnecessary.)
452 # @* is also impossible in .pod; we discard it and any newline that
453 # follows it. Similarly, our macro @gol must be discarded.
455 s/\(?\@xref\{(?:[^\}]*)\}(?:[^.<]|(?:<[^<>]*>))*\.\)?//g;
456 s/\s+\(\@pxref\{(?:[^\}]*)\}\)//g;
457 s/;\s+\@pxref\{(?:[^\}]*)\}//g;
463 # Anchors are thrown away
464 s/\@anchor\{(?:[^\}]*)\}//g;
466 # @uref can take one, two, or three arguments, with different
467 # semantics each time. @url and @email are just like @uref with
468 # one argument, for our purposes.
469 s/\@(?:uref|url|email)\{([^\},]*)\}/<B<$1>>/g;
470 s/\@uref\{([^\},]*),([^\},]*)\}/$2 (C<$1>)/g;
471 s/\@uref\{([^\},]*),([^\},]*),([^\},]*)\}/$3/g;
473 # Un-escape <> at this point.
477 # Now un-nest all B<>, I<>, R<>. Theoretically we could have
478 # indefinitely deep nesting; in practice, one level suffices.
479 1 while s/([BIR])<([^<>]*)([BIR])<([^<>]*)>/$1<$2>$3<$4>$1</g;
481 # Replace R<...> with bare ...; eliminate empty markup, B<>;
482 # shift white space at the ends of [BI]<...> expressions outside
486 s/([BI])<(\s+)([^>]+)>/$2$1<$3>/g;
487 s/([BI])<([^>]+?)(\s+)>/$1<$2>$3/g;
489 # Extract footnotes. This has to be done after all other
490 # processing because otherwise the regexp will choke on formatting
492 while (/\@footnote/g) {
493 s/\@footnote\{([^\}]+)\}/[$fnno]/;
494 add_footnote
($1, $fnno);
503 # Replace escaped symbols with their equivalents.
517 unless (exists $sects{FOOTNOTES
}) {
518 $sects{FOOTNOTES
} = "\n=over 4\n\n";
521 $sects{FOOTNOTES
} .= "=item $fnno.\n\n"; $fnno++;
522 $sects{FOOTNOTES
} .= $_[0];
523 $sects{FOOTNOTES
} .= "\n\n";
526 # stolen from Symbol.pm
531 my $name = "GEN" . $genseq++;