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.
51 ($flag, $value) = ($flag =~ /^([^=]+)(?:=(.+))?/);
52 die "no flag specified for -D\n"
54 die "flags may only contain letters, digits, hyphens, dashes and underscores\n"
55 unless $flag =~ /^[a-zA-Z0-9_-]+$/;
56 $defs{$flag} = $value;
57 } elsif (/^-I(.*)$/) {
58 push @ibase, $1 ne "" ? $1 : shift;
62 $in = $_, next unless defined $in;
63 $out = $_, next unless defined $out;
72 open($inf, "<$in") or die "opening \"$in\": $!\n";
73 push @ibase, $1 if $in =~ m|^(.+)/[^/]+$|;
79 open(STDOUT, ">$out") or die "opening \"$out\": $!\n";
84 # Certain commands are discarded without further processing.
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
90 |node # @node: useful only in .info file
91 |(?:end\s+)?ifnottex # @ifnottex .. @end ifnottex: use contents
96 # Look for filename and title markers.
97 /^\@setfilename\s+([^.]+)/ and $fn = $1, next;
98 /^\@settitle\s+([^.]+)/ and $tl = postprocess($1), next;
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 {
102 if (exists $defs{$1}) {
104 $tl = postprocess($2);
109 /^\@include\s+(.+)$/ and do {
114 open($inf, "<" . $_ . "/" . $1) and next INF;
116 die "cannot open $1: $!\n";
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.
122 /^\@c\s+man\s+begin\s+([A-Za-z ]+)/ and $sect = $1, push (@sects_sequence, $sect), $output = 1, next;
123 /^\@c\s+man\s+end/ and do {
124 $sects{$sect} = "" unless exists $sects{$sect};
125 $sects{$sect} .= postprocess($section);
132 /^\@set\s+([a-zA-Z0-9_-]+)\s*(.*)$/ and do {
136 /^\@clear\s+([a-zA-Z0-9_-]+)/ and do {
143 # Discard comments. (Can't do it above, because then we'd never see
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 {
150 # Ignore @end foo, where foo is not an operation which may
151 # cause us to skip, if we are presently skipping.
153 next if $skipping && $ended !~ /^(?:ifset|ifclear|ignore|menu|iftex)$/;
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;
158 $endw = pop @endwstack;
160 if ($ended =~ /^(?:ifset|ifclear|ignore|menu|iftex)$/) {
161 $skipping = pop @skstack;
163 } elsif ($ended =~ /^(?:example|smallexample|display)$/) {
165 $_ = ""; # need a paragraph break
166 } elsif ($ended =~ /^(?:itemize|enumerate|[fv]?table)$/) {
170 die "unknown command \@end $ended at line $.\n";
174 # We must handle commands which can cause skipping even while we
175 # are skipping, otherwise we will not process nested conditionals
177 /^\@ifset\s+([a-zA-Z0-9_-]+)/ and do {
178 push @endwstack, $endw;
179 push @skstack, $skipping;
181 $skipping = 1 unless exists $defs{$1};
185 /^\@ifclear\s+([a-zA-Z0-9_-]+)/ and do {
186 push @endwstack, $endw;
187 push @skstack, $skipping;
189 $skipping = 1 if exists $defs{$1};
193 /^\@(ignore|menu|iftex)\b/ and do {
194 push @endwstack, $endw;
195 push @skstack, $skipping;
203 # Character entities. First the ones that can be replaced by raw text
204 # or discarded outright:
205 s/\@copyright\{\}/(c)/g;
207 s/\@enddots\{\}/..../g;
210 s/\@bullet(?:\{\})?/*/g;
213 s/\@minus(?:\{\})?/-/g;
216 # Now the ones that have to be replaced by special escapes
217 # (which will be turned back into text by unmunge())
223 # Inside a verbatim block, handle @var specially.
225 s/\@var\{([^\}]*)\}/<$1>/g;
228 # POD doesn't interpret E<> inside a verbatim block.
237 # Single line command handlers.
239 /^\@(?:section|unnumbered|unnumberedsec|center|heading)\s+(.+)$/
240 and $_ = "\n=head2 $1\n";
241 /^\@(?:subsection|subheading)\s+(.+)$/
242 and $_ = "\n=head3 $1\n";
243 /^\@(?:subsubsection|subsubheading)\s+(.+)$/
244 and $_ = "\n=head4 $1\n";
246 # Block command handlers:
247 /^\@itemize\s*(\@[a-z]+|\*|-)?/ and do {
248 push @endwstack, $endw;
255 /^\@enumerate(?:\s+([a-zA-Z0-9]+))?/ and do {
256 push @endwstack, $endw;
267 /^\@([fv]?table)\s+(\@[a-z]+)/ and do {
268 push @endwstack, $endw;
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/;
279 /^\@((?:small)?example|display)/ and do {
280 push @endwstack, $endw;
283 $_ = ""; # need a paragraph break
286 /^\@itemx?\s*(.+)?$/ and do {
288 # Entity escapes prevent munging by the <> processing below.
289 $_ = "\n=item $ic\<$1\>\n";
291 $_ = "\n=item $ic\n";
292 $ic =~ y/A-Ya-y/B-Zb-z/;
293 $ic =~ s/(\d+)/$1 + 1/eg;
297 $section .= $shift.$_."\n";
299 # End of current file.
304 die "No filename or title\n" unless defined $fn && defined $tl;
306 $sects{NAME} = "$fn \- $tl\n";
307 $sects{FOOTNOTES} .= "=back\n" if exists $sects{FOOTNOTES};
309 unshift @sects_sequence, "NAME";
310 for $sect (@sects_sequence) {
311 if(exists $sects{$sect}) {
313 $head =~ s/SEEALSO/SEE ALSO/;
314 print "=head1 $head\n\n";
315 print scalar unmunge ($sects{$sect});
322 die "usage: $0 [-D toggle...] [infile [outfile]]\n";
329 # @value{foo} is replaced by whatever 'foo' is defined as.
330 while (m/(\@value\{([a-zA-Z0-9_-]+)\})/g) {
331 if (! exists $defs{$2}) {
332 print STDERR "Option $2 not defined\n";
340 # Formatting commands.
341 # Temporary escape for @r.
342 s/\@r\{([^\}]*)\}/R<$1>/g;
343 s/\@(?:dfn|var|emph|cite|i)\{([^\}]*)\}/I<$1>/g;
344 s/\@(?:code|kbd)\{([^\}]*)\}/C<$1>/g;
345 s/\@(?:gccoptlist|samp|strong|key|option|env|command|b)\{([^\}]*)\}/B<$1>/g;
346 s/\@sc\{([^\}]*)\}/\U$1/g;
347 s/\@file\{([^\}]*)\}/F<$1>/g;
348 s/\@w\{([^\}]*)\}/S<$1>/g;
349 s/\@(?:dmn|math)\{([^\}]*)\}/$1/g;
351 # Cross references are thrown away, as are @noindent and @refill.
352 # (@noindent is impossible in .pod, and @refill is unnecessary.)
353 # @* is also impossible in .pod; we discard it and any newline that
354 # follows it. Similarly, our macro @gol must be discarded.
356 s/\@anchor{(?:[^\}]*)\}//g;
357 s/\(?\@xref\{(?:[^\}]*)\}(?:[^.<]|(?:<[^<>]*>))*\.\)?//g;
358 s/\s+\(\@pxref\{(?:[^\}]*)\}\)//g;
359 s/;\s+\@pxref\{(?:[^\}]*)\}//g;
360 s/\@ref\{([^\}]*)\}/$1/g;
366 # @uref can take one, two, or three arguments, with different
367 # semantics each time. @url and @email are just like @uref with
368 # one argument, for our purposes.
369 s/\@(?:uref|url|email)\{([^\},]*)\}/<B<$1>>/g;
370 s/\@uref\{([^\},]*),([^\},]*)\}/$2 (C<$1>)/g;
371 s/\@uref\{([^\},]*),([^\},]*),([^\},]*)\}/$3/g;
373 # Turn B<blah I<blah> blah> into B<blah> I<blah> B<blah> to
374 # match Texinfo semantics of @emph inside @samp. Also handle @r
378 1 while s/B<((?:[^<>]|I<[^<>]*>)*)R<([^>]*)>/B<$1>${2}B</g;
379 1 while (s/B<([^<>]*)I<([^>]+)>/B<$1>I<$2>B</g);
380 1 while (s/I<([^<>]*)B<([^>]+)>/I<$1>B<$2>I</g);
382 s/([BI])<(\s+)([^>]+)>/$2$1<$3>/g;
383 s/([BI])<([^>]+?)(\s+)>/$1<$2>$3/g;
385 # Extract footnotes. This has to be done after all other
386 # processing because otherwise the regexp will choke on formatting
388 while (/\@footnote/g) {
389 s/\@footnote\{([^\}]+)\}/[$fnno]/;
390 add_footnote($1, $fnno);
399 # Replace escaped symbols with their equivalents.
413 unless (exists $sects{FOOTNOTES}) {
414 $sects{FOOTNOTES} = "\n=over 4\n\n";
417 $sects{FOOTNOTES} .= "=item $fnno.\n\n"; $fnno++;
418 $sects{FOOTNOTES} .= $_[0];
419 $sects{FOOTNOTES} .= "\n\n";
422 # stolen from Symbol.pm
427 my $name = "GEN" . $genseq++;