OSDN Git Service

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