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