OSDN Git Service

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