avconv: convert to new refcounted AVFrame API
[FFMpeg-mirror/mplayer-patches.git] / doc / texi2pod.pl
blob96d967ba7d71d729669d81896312812ab00781b9
1 #! /usr/bin/perl -w
3 # Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
5 # This file is part of GNU CC.
7 # GNU CC 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)
10 # any later version.
12 # GNU CC 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 GNU CC; 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 @sects_sequence = ();
30 $section = "";
31 @icstack = ();
32 @endwstack = ();
33 @skstack = ();
34 @instack = ();
35 $shift = "";
36 %defs = ();
37 $fnno = 1;
38 $inf = "";
39 @ibase = ();
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 push @ibase, $1 ne "" ? $1 : shift;
57 } elsif (/^-/) {
58 usage();
59 } else {
60 $in = $_, next unless defined $in;
61 $out = $_, next unless defined $out;
62 usage();
66 push @ibase, ".";
68 if (defined $in) {
69 $inf = gensym();
70 open($inf, "<$in") or die "opening \"$in\": $!\n";
71 push @ibase, $1 if $in =~ m|^(.+)/[^/]+$|;
72 } else {
73 $inf = \*STDIN;
76 if (defined $out) {
77 open(STDOUT, ">$out") or die "opening \"$out\": $!\n";
80 while(defined $inf) {
81 INF: while(<$inf>) {
82 # Certain commands are discarded without further processing.
83 /^\@(?:
84 [a-z]+index # @*index: useful only in complete manual
85 |need # @need: useful only in printed manual
86 |(?:end\s+)?group # @group .. @end group: ditto
87 |page # @page: ditto
88 |node # @node: useful only in .info file
89 |(?:end\s+)?ifnottex # @ifnottex .. @end ifnottex: use contents
90 )\b/x and next;
92 chomp;
94 # Look for filename and title markers.
95 /^\@setfilename\s+([^.]+)/ and $fn = $1, next;
96 /^\@settitle\s+([^.]+)/ and $tl = postprocess($1), next;
98 # Identify a man title but keep only the one we are interested in.
99 /^\@c\s+man\s+title\s+([A-Za-z0-9-]+)\s+(.+)/ and do {
100 if (exists $defs{$1}) {
101 $fn = $1;
102 $tl = postprocess($2);
104 next;
107 /^\@include\s+(.+)$/ and do {
108 push @instack, $inf;
109 $inf = gensym();
111 for (@ibase) {
112 open($inf, "<" . $_ . "/" . $1) and next INF;
114 die "cannot open $1: $!\n";
117 # Look for blocks surrounded by @c man begin SECTION ... @c man end.
118 # This really oughta be @ifman ... @end ifman and the like, but such
119 # would require rev'ing all other Texinfo translators.
120 /^\@c\s+man\s+begin\s+([A-Za-z ]+)/ and $sect = $1, push (@sects_sequence, $sect), $output = 1, next;
121 /^\@c\s+man\s+end/ and do {
122 $sects{$sect} = "" unless exists $sects{$sect};
123 $sects{$sect} .= postprocess($section);
124 $section = "";
125 $output = 0;
126 next;
129 # handle variables
130 /^\@set\s+([a-zA-Z0-9_-]+)\s*(.*)$/ and do {
131 $defs{$1} = $2;
132 next;
134 /^\@clear\s+([a-zA-Z0-9_-]+)/ and do {
135 delete $defs{$1};
136 next;
139 next unless $output;
141 # Discard comments. (Can't do it above, because then we'd never see
142 # @c man lines.)
143 /^\@c\b/ and next;
145 # End-block handler goes up here because it needs to operate even
146 # if we are skipping.
147 /^\@end\s+([a-z]+)/ and do {
148 # Ignore @end foo, where foo is not an operation which may
149 # cause us to skip, if we are presently skipping.
150 my $ended = $1;
151 next if $skipping && $ended !~ /^(?:ifset|ifclear|ignore|menu|iftex)$/;
153 die "\@end $ended without \@$ended at line $.\n" unless defined $endw;
154 die "\@$endw ended by \@end $ended at line $.\n" unless $ended eq $endw;
156 $endw = pop @endwstack;
158 if ($ended =~ /^(?:ifset|ifclear|ignore|menu|iftex)$/) {
159 $skipping = pop @skstack;
160 next;
161 } elsif ($ended =~ /^(?:example|smallexample|display)$/) {
162 $shift = "";
163 $_ = ""; # need a paragraph break
164 } elsif ($ended =~ /^(?:itemize|enumerate|(?:multi|[fv])?table)$/) {
165 $_ = "\n=back\n";
166 $ic = pop @icstack;
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)\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 specially.
222 if ($shift ne "") {
223 s/\@var\{([^\}]*)\}/<$1>/g;
226 # POD doesn't interpret E<> inside a verbatim block.
227 if ($shift eq "") {
228 s/</&lt;/g;
229 s/>/&gt;/g;
230 } else {
231 s/</&LT;/g;
232 s/>/&GT;/g;
235 # Single line command handlers.
237 /^\@(?:section|unnumbered|unnumberedsec|center|heading)\s+(.+)$/
238 and $_ = "\n=head2 $1\n";
239 /^\@(?:subsection|subheading)\s+(.+)$/
240 and $_ = "\n=head3 $1\n";
241 /^\@(?:subsubsection|subsubheading)\s+(.+)$/
242 and $_ = "\n=head4 $1\n";
244 # Block command handlers:
245 /^\@itemize\s*(\@[a-z]+|\*|-)?/ and do {
246 push @endwstack, $endw;
247 push @icstack, $ic;
248 $ic = $1 ? $1 : "*";
249 $_ = "\n=over 4\n";
250 $endw = "itemize";
253 /^\@enumerate(?:\s+([a-zA-Z0-9]+))?/ and do {
254 push @endwstack, $endw;
255 push @icstack, $ic;
256 if (defined $1) {
257 $ic = $1 . ".";
258 } else {
259 $ic = "1.";
261 $_ = "\n=over 4\n";
262 $endw = "enumerate";
265 /^\@((?:multi|[fv])?table)\s+(\@[a-z]+)/ and do {
266 push @endwstack, $endw;
267 push @icstack, $ic;
268 $endw = $1;
269 $ic = $2;
270 $ic =~ s/\@(?:samp|strong|key|gcctabopt|option|env)/B/;
271 $ic =~ s/\@(?:code|kbd)/C/;
272 $ic =~ s/\@(?:dfn|var|emph|cite|i)/I/;
273 $ic =~ s/\@(?:file)/F/;
274 $ic =~ s/\@(?:columnfractions)//;
275 $_ = "\n=over 4\n";
278 /^\@((?:small)?example|display)/ and do {
279 push @endwstack, $endw;
280 $endw = $1;
281 $shift = "\t";
282 $_ = ""; # need a paragraph break
285 /^\@item\s+(.*\S)\s*$/ and $endw eq "multitable" and do {
286 my $columns = $1;
287 $columns =~ s/\@tab/ : /;
289 $_ = "\n=item B&LT;". $columns ."&GT;\n";
292 /^\@tab\s+(.*\S)\s*$/ and $endw eq "multitable" and do {
293 my $columns = $1;
294 $columns =~ s/\@tab/ : /;
296 $_ = " : ". $columns;
297 $section =~ s/\n+\s+$//;
300 /^\@itemx?\s*(.+)?$/ and do {
301 if (defined $1) {
302 # Entity escapes prevent munging by the <> processing below.
303 $_ = "\n=item $ic\&LT;$1\&GT;\n";
304 } else {
305 $_ = "\n=item $ic\n";
306 $ic =~ y/A-Ya-y/B-Zb-z/;
307 $ic =~ s/(\d+)/$1 + 1/eg;
311 $section .= $shift.$_."\n";
313 # End of current file.
314 close($inf);
315 $inf = pop @instack;
318 die "No filename or title\n" unless defined $fn && defined $tl;
320 $sects{NAME} = "$fn \- $tl\n";
321 $sects{FOOTNOTES} .= "=back\n" if exists $sects{FOOTNOTES};
323 unshift @sects_sequence, "NAME";
324 for $sect (@sects_sequence) {
325 if(exists $sects{$sect}) {
326 $head = $sect;
327 $head =~ s/SEEALSO/SEE ALSO/;
328 print "=head1 $head\n\n";
329 print scalar unmunge ($sects{$sect});
330 print "\n";
334 sub usage
336 die "usage: $0 [-D toggle...] [infile [outfile]]\n";
339 sub postprocess
341 local $_ = $_[0];
343 # @value{foo} is replaced by whatever 'foo' is defined as.
344 while (m/(\@value\{([a-zA-Z0-9_-]+)\})/g) {
345 if (! exists $defs{$2}) {
346 print STDERR "Option $2 not defined\n";
347 s/\Q$1\E//;
348 } else {
349 $value = $defs{$2};
350 s/\Q$1\E/$value/;
354 # Formatting commands.
355 # Temporary escape for @r.
356 s/\@r\{([^\}]*)\}/R<$1>/g;
357 s/\@(?:dfn|var|emph|cite|i)\{([^\}]*)\}/I<$1>/g;
358 s/\@(?:code|kbd)\{([^\}]*)\}/C<$1>/g;
359 s/\@(?:gccoptlist|samp|strong|key|option|env|command|b)\{([^\}]*)\}/B<$1>/g;
360 s/\@sc\{([^\}]*)\}/\U$1/g;
361 s/\@file\{([^\}]*)\}/F<$1>/g;
362 s/\@w\{([^\}]*)\}/S<$1>/g;
363 s/\@(?:dmn|math)\{([^\}]*)\}/$1/g;
365 # Cross references are thrown away, as are @noindent and @refill.
366 # (@noindent is impossible in .pod, and @refill is unnecessary.)
367 # @* is also impossible in .pod; we discard it and any newline that
368 # follows it. Similarly, our macro @gol must be discarded.
370 s/\@anchor{(?:[^\}]*)\}//g;
371 s/\(?\@xref\{(?:[^\}]*)\}(?:[^.<]|(?:<[^<>]*>))*\.\)?//g;
372 s/\s+\(\@pxref\{(?:[^\}]*)\}\)//g;
373 s/;\s+\@pxref\{(?:[^\}]*)\}//g;
374 s/\@ref\{([^\}]*)\}/$1/g;
375 s/\@noindent\s*//g;
376 s/\@refill//g;
377 s/\@gol//g;
378 s/\@\*\s*\n?//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)\{([^\},]*)\}/&lt;B<$1>&gt;/g;
384 s/\@uref\{([^\},]*),([^\},]*)\}/$2 (C<$1>)/g;
385 s/\@uref\{([^\},]*),([^\},]*),([^\},]*)\}/$3/g;
387 # Turn B<blah I<blah> blah> into B<blah> I<blah> B<blah> to
388 # match Texinfo semantics of @emph inside @samp. Also handle @r
389 # inside bold.
390 s/&LT;/</g;
391 s/&GT;/>/g;
392 1 while s/B<((?:[^<>]|I<[^<>]*>)*)R<([^>]*)>/B<$1>${2}B</g;
393 1 while (s/B<([^<>]*)I<([^>]+)>/B<$1>I<$2>B</g);
394 1 while (s/I<([^<>]*)B<([^>]+)>/I<$1>B<$2>I</g);
395 s/[BI]<>//g;
396 s/([BI])<(\s+)([^>]+)>/$2$1<$3>/g;
397 s/([BI])<([^>]+?)(\s+)>/$1<$2>$3/g;
399 # Extract footnotes. This has to be done after all other
400 # processing because otherwise the regexp will choke on formatting
401 # inside @footnote.
402 while (/\@footnote/g) {
403 s/\@footnote\{([^\}]+)\}/[$fnno]/;
404 add_footnote($1, $fnno);
405 $fnno++;
408 return $_;
411 sub unmunge
413 # Replace escaped symbols with their equivalents.
414 local $_ = $_[0];
416 s/&lt;/E<lt>/g;
417 s/&gt;/E<gt>/g;
418 s/&lbrace;/\{/g;
419 s/&rbrace;/\}/g;
420 s/&at;/\@/g;
421 s/&amp;/&/g;
422 return $_;
425 sub add_footnote
427 unless (exists $sects{FOOTNOTES}) {
428 $sects{FOOTNOTES} = "\n=over 4\n\n";
431 $sects{FOOTNOTES} .= "=item $fnno.\n\n"; $fnno++;
432 $sects{FOOTNOTES} .= $_[0];
433 $sects{FOOTNOTES} .= "\n\n";
436 # stolen from Symbol.pm
438 my $genseq = 0;
439 sub gensym
441 my $name = "GEN" . $genseq++;
442 my $ref = \*{$name};
443 delete $::{$name};
444 return $ref;