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;
58 $in = $_, next unless defined $in;
59 $out = $_, next unless defined $out;
66 open($inf, "<$in") or die "opening \"$in\": $!\n";
67 $ibase = $1 if $in =~ m|^(.+)/[^/]+$|;
73 open(STDOUT, ">$out") or die "opening \"$out\": $!\n";
78 # Certain commands are discarded without further processing.
80 [a-z]+index # @*index: useful only in complete manual
81 |need # @need: useful only in printed manual
82 |(?:end\s+)?group # @group .. @end group: ditto
84 |node # @node: useful only in .info file
85 |(?:end\s+)?ifnottex # @ifnottex .. @end ifnottex: use contents
90 # Look for filename and title markers.
91 /^\@setfilename\s+([^.]+)/ and $fn = $1, next;
92 /^\@settitle\s+([^.]+)/ and $tl = postprocess($1), next;
94 # Identify a man title but keep only the one we are interested in.
95 /^\@c\s+man\s+title\s+([A-Za-z0-9-]+)\s+(.+)/ and do {
96 if (exists $defs{$1}) {
98 $tl = postprocess($2);
103 /^\@include\s+(.+)$/ and do {
107 # Try cwd and $ibase.
109 or open($inf, "<" . $ibase . "/" . $1)
110 or die "cannot open $1 or $ibase/$1: $!\n";
114 # Look for blocks surrounded by @c man begin SECTION ... @c man end.
115 # This really oughta be @ifman ... @end ifman and the like, but such
116 # would require rev'ing all other Texinfo translators.
117 /^\@c\s+man\s+begin\s+([A-Za-z ]+)/ and $sect = $1, push (@sects_sequence, $sect), $output = 1, next;
118 /^\@c\s+man\s+end/ and do {
119 $sects{$sect} = "" unless exists $sects{$sect};
120 $sects{$sect} .= postprocess($section);
127 /^\@set\s+([a-zA-Z0-9_-]+)\s*(.*)$/ and do {
131 /^\@clear\s+([a-zA-Z0-9_-]+)/ and do {
138 # Discard comments. (Can't do it above, because then we'd never see
142 # End-block handler goes up here because it needs to operate even
143 # if we are skipping.
144 /^\@end\s+([a-z]+)/ and do {
145 # Ignore @end foo, where foo is not an operation which may
146 # cause us to skip, if we are presently skipping.
148 next if $skipping && $ended !~ /^(?:ifset|ifclear|ignore|menu|iftex)$/;
150 die "\@end $ended without \@$ended at line $.\n" unless defined $endw;
151 die "\@$endw ended by \@end $ended at line $.\n" unless $ended eq $endw;
153 $endw = pop @endwstack;
155 if ($ended =~ /^(?:ifset|ifclear|ignore|menu|iftex)$/) {
156 $skipping = pop @skstack;
158 } elsif ($ended =~ /^(?:example|smallexample|display)$/) {
160 $_ = ""; # need a paragraph break
161 } elsif ($ended =~ /^(?:itemize|enumerate|[fv]?table)$/) {
165 die "unknown command \@end $ended at line $.\n";
169 # We must handle commands which can cause skipping even while we
170 # are skipping, otherwise we will not process nested conditionals
172 /^\@ifset\s+([a-zA-Z0-9_-]+)/ and do {
173 push @endwstack, $endw;
174 push @skstack, $skipping;
176 $skipping = 1 unless exists $defs{$1};
180 /^\@ifclear\s+([a-zA-Z0-9_-]+)/ and do {
181 push @endwstack, $endw;
182 push @skstack, $skipping;
184 $skipping = 1 if exists $defs{$1};
188 /^\@(ignore|menu|iftex)\b/ and do {
189 push @endwstack, $endw;
190 push @skstack, $skipping;
198 # Character entities. First the ones that can be replaced by raw text
199 # or discarded outright:
200 s/\@copyright\{\}/(c)/g;
202 s/\@enddots\{\}/..../g;
205 s/\@bullet(?:\{\})?/*/g;
208 s/\@minus(?:\{\})?/-/g;
211 # Now the ones that have to be replaced by special escapes
212 # (which will be turned back into text by unmunge())
218 # Inside a verbatim block, handle @var specially.
220 s/\@var\{([^\}]*)\}/<$1>/g;
223 # POD doesn't interpret E<> inside a verbatim block.
232 # Single line command handlers.
234 /^\@(?:section|unnumbered|unnumberedsec|center)\s+(.+)$/
235 and $_ = "\n=head2 $1\n";
236 /^\@subsection\s+(.+)$/
237 and $_ = "\n=head3 $1\n";
239 # Block command handlers:
240 /^\@itemize\s*(\@[a-z]+|\*|-)?/ and do {
241 push @endwstack, $endw;
248 /^\@enumerate(?:\s+([a-zA-Z0-9]+))?/ and do {
249 push @endwstack, $endw;
260 /^\@([fv]?table)\s+(\@[a-z]+)/ and do {
261 push @endwstack, $endw;
265 $ic =~ s/\@(?:samp|strong|key|gcctabopt|option|env)/B/;
266 $ic =~ s/\@(?:code|kbd)/C/;
267 $ic =~ s/\@(?:dfn|var|emph|cite|i)/I/;
268 $ic =~ s/\@(?:file)/F/;
272 /^\@((?:small)?example|display)/ and do {
273 push @endwstack, $endw;
276 $_ = ""; # need a paragraph break
279 /^\@itemx?\s*(.+)?$/ and do {
281 # Entity escapes prevent munging by the <> processing below.
282 $_ = "\n=item $ic\<$1\>\n";
284 $_ = "\n=item $ic\n";
285 $ic =~ y/A-Ya-y/B-Zb-z/;
286 $ic =~ s/(\d+)/$1 + 1/eg;
290 $section .= $shift.$_."\n";
292 # End of current file.
297 die "No filename or title\n" unless defined $fn && defined $tl;
299 $sects{NAME} = "$fn \- $tl\n";
300 $sects{FOOTNOTES} .= "=back\n" if exists $sects{FOOTNOTES};
302 unshift @sects_sequence, "NAME";
303 for $sect (@sects_sequence) {
304 if(exists $sects{$sect}) {
306 $head =~ s/SEEALSO/SEE ALSO/;
307 print "=head1 $head\n\n";
308 print scalar unmunge ($sects{$sect});
315 die "usage: $0 [-D toggle...] [infile [outfile]]\n";
322 # @value{foo} is replaced by whatever 'foo' is defined as.
323 while (m/(\@value\{([a-zA-Z0-9_-]+)\})/g) {
324 if (! exists $defs{$2}) {
325 print STDERR "Option $2 not defined\n";
333 # Formatting commands.
334 # Temporary escape for @r.
335 s/\@r\{([^\}]*)\}/R<$1>/g;
336 s/\@(?:dfn|var|emph|cite|i)\{([^\}]*)\}/I<$1>/g;
337 s/\@(?:code|kbd)\{([^\}]*)\}/C<$1>/g;
338 s/\@(?:gccoptlist|samp|strong|key|option|env|command|b)\{([^\}]*)\}/B<$1>/g;
339 s/\@sc\{([^\}]*)\}/\U$1/g;
340 s/\@file\{([^\}]*)\}/F<$1>/g;
341 s/\@w\{([^\}]*)\}/S<$1>/g;
342 s/\@(?:dmn|math)\{([^\}]*)\}/$1/g;
344 # Cross references are thrown away, as are @noindent and @refill.
345 # (@noindent is impossible in .pod, and @refill is unnecessary.)
346 # @* is also impossible in .pod; we discard it and any newline that
347 # follows it. Similarly, our macro @gol must be discarded.
349 s/\@anchor{(?:[^\}]*)\}//g;
350 s/\(?\@xref\{(?:[^\}]*)\}(?:[^.<]|(?:<[^<>]*>))*\.\)?//g;
351 s/\s+\(\@pxref\{(?:[^\}]*)\}\)//g;
352 s/;\s+\@pxref\{(?:[^\}]*)\}//g;
358 # @uref can take one, two, or three arguments, with different
359 # semantics each time. @url and @email are just like @uref with
360 # one argument, for our purposes.
361 s/\@(?:uref|url|email)\{([^\},]*)\}/<B<$1>>/g;
362 s/\@uref\{([^\},]*),([^\},]*)\}/$2 (C<$1>)/g;
363 s/\@uref\{([^\},]*),([^\},]*),([^\},]*)\}/$3/g;
365 # Turn B<blah I<blah> blah> into B<blah> I<blah> B<blah> to
366 # match Texinfo semantics of @emph inside @samp. Also handle @r
370 1 while s/B<((?:[^<>]|I<[^<>]*>)*)R<([^>]*)>/B<$1>${2}B</g;
371 1 while (s/B<([^<>]*)I<([^>]+)>/B<$1>I<$2>B</g);
372 1 while (s/I<([^<>]*)B<([^>]+)>/I<$1>B<$2>I</g);
374 s/([BI])<(\s+)([^>]+)>/$2$1<$3>/g;
375 s/([BI])<([^>]+?)(\s+)>/$1<$2>$3/g;
377 # Extract footnotes. This has to be done after all other
378 # processing because otherwise the regexp will choke on formatting
380 while (/\@footnote/g) {
381 s/\@footnote\{([^\}]+)\}/[$fnno]/;
382 add_footnote($1, $fnno);
391 # Replace escaped symbols with their equivalents.
405 unless (exists $sects{FOOTNOTES}) {
406 $sects{FOOTNOTES} = "\n=over 4\n\n";
409 $sects{FOOTNOTES} .= "=item $fnno.\n\n"; $fnno++;
410 $sects{FOOTNOTES} .= $_[0];
411 $sects{FOOTNOTES} .= "\n\n";
414 # stolen from Symbol.pm
419 my $name = "GEN" . $genseq++;