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