3 # Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
5 # This file is part of GNU CC.
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)
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.
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
19 # the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 # Boston, MA 02110-1301 USA
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.
49 ($flag, $value) = ($flag =~ /^([^=]+)(?:=(.+))?/);
50 die "no flag specified for -D\n"
52 die "flags may only contain letters, digits, hyphens, dashes and underscores\n"
53 unless $flag =~ /^[a-zA-Z0-9_-]+$/;
54 $defs{$flag} = $value;
55 } elsif (/^-I(.*)$/) {
56 push @ibase, $1 ne "" ? $1 : shift;
60 $in = $_, next unless defined $in;
61 $out = $_, next unless defined $out;
70 open($inf, "<$in") or die "opening \"$in\": $!\n";
71 push @ibase, $1 if $in =~ m|^(.+)/[^/]+$|;
77 open(STDOUT, ">$out") or die "opening \"$out\": $!\n";
82 # Certain commands are discarded without further processing.
84 [a-z]+index # @*index: useful only in complete manual
85 |need # @need: useful only in printed manual
86 |(?:end\s+)?group # @group .. @end group: ditto
88 |node # @node: useful only in .info file
89 |(?:end\s+)?ifnottex # @ifnottex .. @end ifnottex: use contents
94 # Look for filename and title markers.
95 /^\@setfilename\s+([^.]+)/ and $fn = $1, next;
96 /^\@settitle\s+([^.]+)/ and $tl = postprocess($1), next;
98 # Identify a man title but keep only the one we are interested in.
99 /^\@c\s+man\s+title\s+([A-Za-z0-9-]+)\s+(.+)/ and do {
100 if (exists $defs{$1}) {
102 $tl = postprocess($2);
107 /^\@include\s+(.+)$/ and do {
112 open($inf, "<" . $_ . "/" . $1) and next INF;
114 die "cannot open $1: $!\n";
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-Za-z ]+)/ and $sect = $1, push (@sects_sequence, $sect), $output = 1, next;
121 /^\@c\s+man\s+end/ and do {
122 $sects{$sect} = "" unless exists $sects{$sect};
123 $sects{$sect} .= postprocess($section);
130 /^\@set\s+([a-zA-Z0-9_-]+)\s*(.*)$/ and do {
134 /^\@clear\s+([a-zA-Z0-9_-]+)/ and do {
141 # Discard comments. (Can't do it above, because then we'd never see
145 # End-block handler goes up here because it needs to operate even
146 # if we are skipping.
147 /^\@end\s+([a-z]+)/ and do {
148 # Ignore @end foo, where foo is not an operation which may
149 # cause us to skip, if we are presently skipping.
151 next if $skipping && $ended !~ /^(?:ifset|ifclear|ignore|menu|iftex)$/;
153 die "\@end $ended without \@$ended at line $.\n" unless defined $endw;
154 die "\@$endw ended by \@end $ended at line $.\n" unless $ended eq $endw;
156 $endw = pop @endwstack;
158 if ($ended =~ /^(?:ifset|ifclear|ignore|menu|iftex)$/) {
159 $skipping = pop @skstack;
161 } elsif ($ended =~ /^(?:example|smallexample|display)$/) {
163 $_ = ""; # need a paragraph break
164 } elsif ($ended =~ /^(?:itemize|enumerate|[fv]?table)$/) {
168 die "unknown command \@end $ended at line $.\n";
172 # We must handle commands which can cause skipping even while we
173 # are skipping, otherwise we will not process nested conditionals
175 /^\@ifset\s+([a-zA-Z0-9_-]+)/ and do {
176 push @endwstack, $endw;
177 push @skstack, $skipping;
179 $skipping = 1 unless exists $defs{$1};
183 /^\@ifclear\s+([a-zA-Z0-9_-]+)/ and do {
184 push @endwstack, $endw;
185 push @skstack, $skipping;
187 $skipping = 1 if exists $defs{$1};
191 /^\@(ignore|menu|iftex)\b/ and do {
192 push @endwstack, $endw;
193 push @skstack, $skipping;
201 # Character entities. First the ones that can be replaced by raw text
202 # or discarded outright:
203 s/\@copyright\{\}/(c)/g;
205 s/\@enddots\{\}/..../g;
208 s/\@bullet(?:\{\})?/*/g;
211 s/\@minus(?:\{\})?/-/g;
214 # Now the ones that have to be replaced by special escapes
215 # (which will be turned back into text by unmunge())
221 # Inside a verbatim block, handle @var specially.
223 s/\@var\{([^\}]*)\}/<$1>/g;
226 # POD doesn't interpret E<> inside a verbatim block.
235 # Single line command handlers.
237 /^\@(?:section|unnumbered|unnumberedsec|center|heading)\s+(.+)$/
238 and $_ = "\n=head2 $1\n";
239 /^\@(?:subsection|subheading)\s+(.+)$/
240 and $_ = "\n=head3 $1\n";
241 /^\@(?:subsubsection|subsubheading)\s+(.+)$/
242 and $_ = "\n=head4 $1\n";
244 # Block command handlers:
245 /^\@itemize\s*(\@[a-z]+|\*|-)?/ and do {
246 push @endwstack, $endw;
253 /^\@enumerate(?:\s+([a-zA-Z0-9]+))?/ and do {
254 push @endwstack, $endw;
265 /^\@([fv]?table)\s+(\@[a-z]+)/ and do {
266 push @endwstack, $endw;
270 $ic =~ s/\@(?:samp|strong|key|gcctabopt|option|env)/B/;
271 $ic =~ s/\@(?:code|kbd)/C/;
272 $ic =~ s/\@(?:dfn|var|emph|cite|i)/I/;
273 $ic =~ s/\@(?:file)/F/;
277 /^\@((?:small)?example|display)/ and do {
278 push @endwstack, $endw;
281 $_ = ""; # need a paragraph break
284 /^\@itemx?\s*(.+)?$/ and do {
286 # Entity escapes prevent munging by the <> processing below.
287 $_ = "\n=item $ic\<$1\>\n";
289 $_ = "\n=item $ic\n";
290 $ic =~ y/A-Ya-y/B-Zb-z/;
291 $ic =~ s/(\d+)/$1 + 1/eg;
295 $section .= $shift.$_."\n";
297 # End of current file.
302 die "No filename or title\n" unless defined $fn && defined $tl;
304 $sects{NAME} = "$fn \- $tl\n";
305 $sects{FOOTNOTES} .= "=back\n" if exists $sects{FOOTNOTES};
307 unshift @sects_sequence, "NAME";
308 for $sect (@sects_sequence) {
309 if(exists $sects{$sect}) {
311 $head =~ s/SEEALSO/SEE ALSO/;
312 print "=head1 $head\n\n";
313 print scalar unmunge ($sects{$sect});
320 die "usage: $0 [-D toggle...] [infile [outfile]]\n";
327 # @value{foo} is replaced by whatever 'foo' is defined as.
328 while (m/(\@value\{([a-zA-Z0-9_-]+)\})/g) {
329 if (! exists $defs{$2}) {
330 print STDERR "Option $2 not defined\n";
338 # Formatting commands.
339 # Temporary escape for @r.
340 s/\@r\{([^\}]*)\}/R<$1>/g;
341 s/\@(?:dfn|var|emph|cite|i)\{([^\}]*)\}/I<$1>/g;
342 s/\@(?:code|kbd)\{([^\}]*)\}/C<$1>/g;
343 s/\@(?:gccoptlist|samp|strong|key|option|env|command|b)\{([^\}]*)\}/B<$1>/g;
344 s/\@sc\{([^\}]*)\}/\U$1/g;
345 s/\@file\{([^\}]*)\}/F<$1>/g;
346 s/\@w\{([^\}]*)\}/S<$1>/g;
347 s/\@(?:dmn|math)\{([^\}]*)\}/$1/g;
349 # Cross references are thrown away, as are @noindent and @refill.
350 # (@noindent is impossible in .pod, and @refill is unnecessary.)
351 # @* is also impossible in .pod; we discard it and any newline that
352 # follows it. Similarly, our macro @gol must be discarded.
354 s/\@anchor{(?:[^\}]*)\}//g;
355 s/\(?\@xref\{(?:[^\}]*)\}(?:[^.<]|(?:<[^<>]*>))*\.\)?//g;
356 s/\s+\(\@pxref\{(?:[^\}]*)\}\)//g;
357 s/;\s+\@pxref\{(?:[^\}]*)\}//g;
358 s/\@ref\{([^\}]*)\}/$1/g;
364 # @uref can take one, two, or three arguments, with different
365 # semantics each time. @url and @email are just like @uref with
366 # one argument, for our purposes.
367 s/\@(?:uref|url|email)\{([^\},]*)\}/<B<$1>>/g;
368 s/\@uref\{([^\},]*),([^\},]*)\}/$2 (C<$1>)/g;
369 s/\@uref\{([^\},]*),([^\},]*),([^\},]*)\}/$3/g;
371 # Turn B<blah I<blah> blah> into B<blah> I<blah> B<blah> to
372 # match Texinfo semantics of @emph inside @samp. Also handle @r
376 1 while s/B<((?:[^<>]|I<[^<>]*>)*)R<([^>]*)>/B<$1>${2}B</g;
377 1 while (s/B<([^<>]*)I<([^>]+)>/B<$1>I<$2>B</g);
378 1 while (s/I<([^<>]*)B<([^>]+)>/I<$1>B<$2>I</g);
380 s/([BI])<(\s+)([^>]+)>/$2$1<$3>/g;
381 s/([BI])<([^>]+?)(\s+)>/$1<$2>$3/g;
383 # Extract footnotes. This has to be done after all other
384 # processing because otherwise the regexp will choke on formatting
386 while (/\@footnote/g) {
387 s/\@footnote\{([^\}]+)\}/[$fnno]/;
388 add_footnote($1, $fnno);
397 # Replace escaped symbols with their equivalents.
411 unless (exists $sects{FOOTNOTES}) {
412 $sects{FOOTNOTES} = "\n=over 4\n\n";
415 $sects{FOOTNOTES} .= "=item $fnno.\n\n"; $fnno++;
416 $sects{FOOTNOTES} .= $_[0];
417 $sects{FOOTNOTES} .= "\n\n";
420 # stolen from Symbol.pm
425 my $name = "GEN" . $genseq++;