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