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