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