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