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