]> git.sesse.net Git - ffmpeg/blob - doc/texi2pod.pl
avcodec/mjpegdec: fix SOF check in EOI
[ffmpeg] / doc / texi2pod.pl
1 #!/usr/bin/env perl
2
3 #   Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
4
5 # This file is part of GNU CC.
6
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.
11
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.
16
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
21
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
26 use warnings;
27
28 $output = 0;
29 $skipping = 0;
30 %chapters = ();
31 @chapters_sequence = ();
32 $chapter = "";
33 @icstack = ();
34 @endwstack = ();
35 @skstack = ();
36 @instack = ();
37 $shift = "";
38 %defs = ();
39 $fnno = 1;
40 $inf = "";
41 @ibase = ();
42
43 while ($_ = shift) {
44     if (/^-D(.*)$/) {
45         if ($1 ne "") {
46             $flag = $1;
47         } else {
48             $flag = shift;
49         }
50         $value = "";
51         ($flag, $value) = ($flag =~ /^([^=]+)(?:=(.+))?/);
52         die "no flag specified for -D\n"
53             unless $flag ne "";
54         die "flags may only contain letters, digits, hyphens, dashes and underscores\n"
55             unless $flag =~ /^[a-zA-Z0-9_-]+$/;
56         $defs{$flag} = $value;
57     } elsif (/^-I(.*)$/) {
58         push @ibase, $1 ne "" ? $1 : shift;
59     } elsif (/^-/) {
60         usage();
61     } else {
62         $in = $_, next unless defined $in;
63         $out = $_, next unless defined $out;
64         usage();
65     }
66 }
67
68 push @ibase, ".";
69
70 if (defined $in) {
71     $inf = gensym();
72     open($inf, "<$in") or die "opening \"$in\": $!\n";
73     push @ibase, $1 if $in =~ m|^(.+)/[^/]+$|;
74 } else {
75     $inf = \*STDIN;
76 }
77
78 if (defined $out) {
79     open(STDOUT, ">$out") or die "opening \"$out\": $!\n";
80 }
81
82 while(defined $inf) {
83 INF: while(<$inf>) {
84     # Certain commands are discarded without further processing.
85     /^\@(?:
86          [a-z]+index            # @*index: useful only in complete manual
87          |need                  # @need: useful only in printed manual
88          |(?:end\s+)?group      # @group .. @end group: ditto
89          |page                  # @page: ditto
90          |node                  # @node: useful only in .info file
91          |(?:end\s+)?ifnottex   # @ifnottex .. @end ifnottex: use contents
92         )\b/x and next;
93
94     chomp;
95
96     # Look for filename and title markers.
97     /^\@setfilename\s+([^.]+)/ and $fn = $1, next;
98     /^\@settitle\s+([^.]+)/ and $tl = postprocess($1), next;
99
100     # Identify a man title but keep only the one we are interested in.
101     /^\@c\s+man\s+title\s+([A-Za-z0-9-]+)\s+(.+)/ and do {
102         if (exists $defs{$1}) {
103             $fn = $1;
104             $tl = postprocess($2);
105         }
106         next;
107     };
108
109     /^\@include\s+(.+)$/ and do {
110         push @instack, $inf;
111         $inf = gensym();
112
113         for (@ibase) {
114             open($inf, "<" . $_ . "/" . $1) and next INF;
115         }
116         die "cannot open $1: $!\n";
117     };
118
119     /^\@chapter\s+([A-Za-z ]+)/ and do {
120         # close old chapter
121         $chapters{$chapter_name} .= postprocess($chapter) if ($chapter_name);
122
123         # start new chapter
124         $chapter_name = $1, push (@chapters_sequence, $chapter_name) unless $skipping;
125         $chapters{$chapter_name} = "" unless exists $chapters{$chapter_name};
126         $chapter = "";
127         $output = 1;
128         next;
129     };
130
131     /^\@bye/ and do {
132         # close old chapter
133         $chapters{$chapter_name} .= postprocess($chapter) if ($chapter_name);
134         last INF;
135     };
136
137     # handle variables
138     /^\@set\s+([a-zA-Z0-9_-]+)\s*(.*)$/ and do {
139         $defs{$1} = $2;
140         next;
141     };
142     /^\@clear\s+([a-zA-Z0-9_-]+)/ and do {
143         delete $defs{$1};
144         next;
145     };
146
147     next unless $output;
148
149     # Discard comments.  (Can't do it above, because then we'd never see
150     # @c man lines.)
151     /^\@c\b/ and next;
152
153     # End-block handler goes up here because it needs to operate even
154     # if we are skipping.
155     /^\@end\s+([a-z]+)/ and do {
156         # Ignore @end foo, where foo is not an operation which may
157         # cause us to skip, if we are presently skipping.
158         my $ended = $1;
159         next if $skipping && $ended !~ /^(?:ifset|ifclear|ignore|menu|iftex|ifhtml|ifnothtml)$/;
160
161         die "\@end $ended without \@$ended at line $.\n" unless defined $endw;
162         die "\@$endw ended by \@end $ended at line $.\n" unless $ended eq $endw;
163
164         $endw = pop @endwstack;
165
166         if ($ended =~ /^(?:ifset|ifclear|ignore|menu|iftex|ifhtml|ifnothtml)$/) {
167             $skipping = pop @skstack;
168             next;
169         } elsif ($ended =~ /^(?:example|smallexample|verbatim|display)$/) {
170             $shift = "";
171             $_ = "";        # need a paragraph break
172         } elsif ($ended =~ /^(?:itemize|enumerate|(?:multi|[fv])?table)$/) {
173             $_ = "\n=back\n";
174             $ic = pop @icstack;
175         } elsif ($ended =~ /^float$/) {
176             $_ = "\n=back\n";
177             $ic = pop @icstack;
178         } else {
179             die "unknown command \@end $ended at line $.\n";
180         }
181     };
182
183     # We must handle commands which can cause skipping even while we
184     # are skipping, otherwise we will not process nested conditionals
185     # correctly.
186     /^\@ifset\s+([a-zA-Z0-9_-]+)/ and do {
187         push @endwstack, $endw;
188         push @skstack, $skipping;
189         $endw = "ifset";
190         $skipping = 1 unless exists $defs{$1};
191         next;
192     };
193
194     /^\@ifclear\s+([a-zA-Z0-9_-]+)/ and do {
195         push @endwstack, $endw;
196         push @skstack, $skipping;
197         $endw = "ifclear";
198         $skipping = 1 if exists $defs{$1};
199         next;
200     };
201
202     /^\@(ignore|menu|iftex|ifhtml|ifnothtml)\b/ and do {
203         push @endwstack, $endw;
204         push @skstack, $skipping;
205         $endw = $1;
206         $skipping = $endw !~ /ifnothtml/;
207         next;
208     };
209
210     next if $skipping;
211
212     # Character entities.  First the ones that can be replaced by raw text
213     # or discarded outright:
214     s/\@copyright\{\}/(c)/g;
215     s/\@dots\{\}/.../g;
216     s/\@enddots\{\}/..../g;
217     s/\@([.!? ])/$1/g;
218     s/\@[:-]//g;
219     s/\@bullet(?:\{\})?/*/g;
220     s/\@TeX\{\}/TeX/g;
221     s/\@pounds\{\}/\#/g;
222     s/\@minus(?:\{\})?/-/g;
223
224     # Now the ones that have to be replaced by special escapes
225     # (which will be turned back into text by unmunge())
226     s/&/&amp;/g;
227     s/\@\{/&lbrace;/g;
228     s/\@\}/&rbrace;/g;
229     s/\@\@/&at;/g;
230
231     # Inside a verbatim block, handle @var specially.
232     if ($shift ne "") {
233         s/\@var\{([^\}]*)\}/<$1>/g;
234     }
235
236     # POD doesn't interpret E<> inside a verbatim block.
237     if ($shift eq "") {
238         s/</&lt;/g;
239         s/>/&gt;/g;
240     } else {
241         s/</&LT;/g;
242         s/>/&GT;/g;
243     }
244
245     # Single line command handlers.
246
247     /^\@(?:section|unnumbered|unnumberedsec|center|heading)\s+(.+)$/
248         and $_ = "\n=head2 $1\n";
249     /^\@(?:subsection|subheading)\s+(.+)$/
250         and $_ = "\n=head3 $1\n";
251     /^\@(?:subsubsection|subsubheading)\s+(.+)$/
252         and $_ = "\n=head4 $1\n";
253
254     # Block command handlers:
255     /^\@itemize\s*(\@[a-z]+|\*|-)?/ and do {
256         push @endwstack, $endw;
257         push @icstack, $ic;
258         $ic = $1 ? $1 : "*";
259         $_ = "\n=over 4\n";
260         $endw = "itemize";
261     };
262
263     /^\@enumerate(?:\s+([a-zA-Z0-9]+))?/ and do {
264         push @endwstack, $endw;
265         push @icstack, $ic;
266         if (defined $1) {
267             $ic = $1 . ".";
268         } else {
269             $ic = "1.";
270         }
271         $_ = "\n=over 4\n";
272         $endw = "enumerate";
273     };
274
275     /^\@((?:multi|[fv])?table)\s+(\@[a-z]+)/ and do {
276         push @endwstack, $endw;
277         push @icstack, $ic;
278         $endw = $1;
279         $ic = $2;
280         $ic =~ s/\@(?:samp|strong|key|gcctabopt|option|env|command)/B/;
281         $ic =~ s/\@(?:code|kbd)/C/;
282         $ic =~ s/\@(?:dfn|var|emph|cite|i)/I/;
283         $ic =~ s/\@(?:file)/F/;
284         $ic =~ s/\@(?:columnfractions)//;
285         $_ = "\n=over 4\n";
286     };
287
288     /^\@(multitable)\s+{.*/ and do {
289         push @endwstack, $endw;
290         push @icstack, $ic;
291         $endw = $1;
292         $ic = "";
293         $_ = "\n=over 4\n";
294     };
295
296     /^\@((?:small)?example|verbatim|display)/ and do {
297         push @endwstack, $endw;
298         $endw = $1;
299         $shift = "\t";
300         $_ = "";        # need a paragraph break
301     };
302
303     /^\@(float)\s+\w+/ and do {
304         push @endwstack, $endw;
305         $endw = $1;
306         $_ = "\n=over 4\n";
307     };
308
309     /^\@item\s+(.*\S)\s*$/ and $endw eq "multitable" and do {
310         my $columns = $1;
311         $columns =~ s/\@tab/ : /;
312
313         $_ = "\n=item B&LT;". $columns ."&GT;\n";
314     };
315
316     /^\@tab\s+(.*\S)\s*$/ and $endw eq "multitable" and do {
317         my $columns = $1;
318         $columns =~ s/\@tab//;
319
320         $_ = $columns;
321         $chapter =~ s/$//;
322     };
323
324     /^\@itemx?\s*(.+)?$/ and do {
325         if (defined $1) {
326             # Entity escapes prevent munging by the <> processing below.
327             $_ = "\n=item $ic\&LT;$1\&GT;\n";
328         } else {
329             $_ = "\n=item $ic\n";
330             $ic =~ y/A-Ya-y/B-Zb-z/;
331             $ic =~ s/(\d+)/$1 + 1/eg;
332         }
333     };
334
335     $chapter .= $shift.$_."\n";
336 }
337 # End of current file.
338 close($inf);
339 $inf = pop @instack;
340 }
341
342 die "No filename or title\n" unless defined $fn && defined $tl;
343
344 # always use utf8
345 print "=encoding utf8\n\n";
346
347 $chapters{NAME} = "$fn \- $tl\n";
348 $chapters{FOOTNOTES} .= "=back\n" if exists $chapters{FOOTNOTES};
349
350 unshift @chapters_sequence, "NAME";
351 for $chapter (@chapters_sequence) {
352     if (exists $chapters{$chapter}) {
353         $head = uc($chapter);
354         print "=head1 $head\n\n";
355         print scalar unmunge ($chapters{$chapter});
356         print "\n";
357     }
358 }
359
360 sub usage
361 {
362     die "usage: $0 [-D toggle...] [infile [outfile]]\n";
363 }
364
365 sub postprocess
366 {
367     local $_ = $_[0];
368
369     # @value{foo} is replaced by whatever 'foo' is defined as.
370     while (m/(\@value\{([a-zA-Z0-9_-]+)\})/g) {
371         if (! exists $defs{$2}) {
372             print STDERR "Option $2 not defined\n";
373             s/\Q$1\E//;
374         } else {
375             $value = $defs{$2};
376             s/\Q$1\E/$value/;
377         }
378     }
379
380     # Formatting commands.
381     # Temporary escape for @r.
382     s/\@r\{([^\}]*)\}/R<$1>/g;
383     s/\@(?:dfn|var|emph|cite|i)\{([^\}]*)\}/I<$1>/g;
384     s/\@(?:code|kbd)\{([^\}]*)\}/C<$1>/g;
385     s/\@(?:gccoptlist|samp|strong|key|option|env|command|b)\{([^\}]*)\}/B<$1>/g;
386     s/\@sc\{([^\}]*)\}/\U$1/g;
387     s/\@file\{([^\}]*)\}/F<$1>/g;
388     s/\@w\{([^\}]*)\}/S<$1>/g;
389     s/\@(?:dmn|math)\{([^\}]*)\}/$1/g;
390
391     # Cross references are thrown away, as are @noindent and @refill.
392     # (@noindent is impossible in .pod, and @refill is unnecessary.)
393     # @* is also impossible in .pod; we discard it and any newline that
394     # follows it.  Similarly, our macro @gol must be discarded.
395
396     s/\@anchor\{(?:[^\}]*)\}//g;
397     s/\(?\@xref\{(?:[^\}]*)\}(?:[^.<]|(?:<[^<>]*>))*\.\)?//g;
398     s/\s+\(\@pxref\{(?:[^\}]*)\}\)//g;
399     s/;\s+\@pxref\{(?:[^\}]*)\}//g;
400     s/\@ref\{(?:[^,\}]*,)(?:[^,\}]*,)([^,\}]*).*\}/B<$1>/g;
401     s/\@ref\{([^\}]*)\}/B<$1>/g;
402     s/\@noindent\s*//g;
403     s/\@refill//g;
404     s/\@gol//g;
405     s/\@\*\s*\n?//g;
406
407     # @uref can take one, two, or three arguments, with different
408     # semantics each time.  @url and @email are just like @uref with
409     # one argument, for our purposes.
410     s/\@(?:uref|url|email)\{([^\},]*),?[^\}]*\}/&lt;B<$1>&gt;/g;
411     s/\@uref\{([^\},]*),([^\},]*)\}/$2 (C<$1>)/g;
412     s/\@uref\{([^\},]*),([^\},]*),([^\},]*)\}/$3/g;
413
414     # Turn B<blah I<blah> blah> into B<blah> I<blah> B<blah> to
415     # match Texinfo semantics of @emph inside @samp.  Also handle @r
416     # inside bold.
417     s/&LT;/</g;
418     s/&GT;/>/g;
419     1 while s/B<((?:[^<>]|I<[^<>]*>)*)R<([^>]*)>/B<$1>${2}B</g;
420     1 while (s/B<([^<>]*)I<([^>]+)>/B<$1>I<$2>B</g);
421     1 while (s/I<([^<>]*)B<([^>]+)>/I<$1>B<$2>I</g);
422     s/[BI]<>//g;
423     s/([BI])<(\s+)([^>]+)>/$2$1<$3>/g;
424     s/([BI])<([^>]+?)(\s+)>/$1<$2>$3/g;
425
426     # Extract footnotes.  This has to be done after all other
427     # processing because otherwise the regexp will choke on formatting
428     # inside @footnote.
429     while (/\@footnote/g) {
430         s/\@footnote\{([^\}]+)\}/[$fnno]/;
431         add_footnote($1, $fnno);
432         $fnno++;
433     }
434
435     return $_;
436 }
437
438 sub unmunge
439 {
440     # Replace escaped symbols with their equivalents.
441     local $_ = $_[0];
442
443     s/&lt;/E<lt>/g;
444     s/&gt;/E<gt>/g;
445     s/&lbrace;/\{/g;
446     s/&rbrace;/\}/g;
447     s/&at;/\@/g;
448     s/&amp;/&/g;
449     return $_;
450 }
451
452 sub add_footnote
453 {
454     unless (exists $chapters{FOOTNOTES}) {
455         $chapters{FOOTNOTES} = "\n=over 4\n\n";
456     }
457
458     $chapters{FOOTNOTES} .= "=item $fnno.\n\n"; $fnno++;
459     $chapters{FOOTNOTES} .= $_[0];
460     $chapters{FOOTNOTES} .= "\n\n";
461 }
462
463 # stolen from Symbol.pm
464 {
465     my $genseq = 0;
466     sub gensym
467     {
468         my $name = "GEN" . $genseq++;
469         my $ref = \*{$name};
470         delete $::{$name};
471         return $ref;
472     }
473 }