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.
31 @chapters_sequence = ();
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 /^\@chapter\s+([A-Za-z ]+)/ and do {
121 $chapters{$chapter_name} .= postprocess($chapter) if ($chapter_name);
124 $chapter_name = $1, push (@chapters_sequence, $chapter_name) unless $skipping;
125 $chapters{$chapter_name} = "" unless exists $chapters{$chapter_name};
133 $chapters{$chapter_name} .= postprocess($chapter) if ($chapter_name);
138 /^\@set\s+([a-zA-Z0-9_-]+)\s*(.*)$/ and do {
142 /^\@clear\s+([a-zA-Z0-9_-]+)/ and do {
149 # Discard comments. (Can't do it above, because then we'd never see
153 # End-block handler goes up here because it needs to operate even
154 # if we are skipping.
155 /^\@end\s+([a-z]+)/ and do {
156 # Ignore @end foo, where foo is not an operation which may
157 # cause us to skip, if we are presently skipping.
159 next if $skipping && $ended !~ /^(?:ifset|ifclear|ignore|menu|iftex|ifhtml|ifnothtml)$/;
161 die "\@end $ended without \@$ended at line $.\n" unless defined $endw;
162 die "\@$endw ended by \@end $ended at line $.\n" unless $ended eq $endw;
164 $endw = pop @endwstack;
166 if ($ended =~ /^(?:ifset|ifclear|ignore|menu|iftex|ifhtml|ifnothtml)$/) {
167 $skipping = pop @skstack;
169 } elsif ($ended =~ /^(?:example|smallexample|verbatim|display)$/) {
171 $_ = ""; # need a paragraph break
172 } elsif ($ended =~ /^(?:itemize|enumerate|(?:multi|[fv])?table)$/) {
176 die "unknown command \@end $ended at line $.\n";
180 # We must handle commands which can cause skipping even while we
181 # are skipping, otherwise we will not process nested conditionals
183 /^\@ifset\s+([a-zA-Z0-9_-]+)/ and do {
184 push @endwstack, $endw;
185 push @skstack, $skipping;
187 $skipping = 1 unless exists $defs{$1};
191 /^\@ifclear\s+([a-zA-Z0-9_-]+)/ and do {
192 push @endwstack, $endw;
193 push @skstack, $skipping;
195 $skipping = 1 if exists $defs{$1};
199 /^\@(ignore|menu|iftex|ifhtml|ifnothtml)\b/ and do {
200 push @endwstack, $endw;
201 push @skstack, $skipping;
203 $skipping = $endw !~ /ifnothtml/;
209 # Character entities. First the ones that can be replaced by raw text
210 # or discarded outright:
211 s/\@copyright\{\}/(c)/g;
213 s/\@enddots\{\}/..../g;
216 s/\@bullet(?:\{\})?/*/g;
219 s/\@minus(?:\{\})?/-/g;
221 # Now the ones that have to be replaced by special escapes
222 # (which will be turned back into text by unmunge())
228 # Inside a verbatim block, handle @var specially.
230 s/\@var\{([^\}]*)\}/<$1>/g;
233 # POD doesn't interpret E<> inside a verbatim block.
242 # Single line command handlers.
244 /^\@(?:section|unnumbered|unnumberedsec|center|heading)\s+(.+)$/
245 and $_ = "\n=head2 $1\n";
246 /^\@(?:subsection|subheading)\s+(.+)$/
247 and $_ = "\n=head3 $1\n";
248 /^\@(?:subsubsection|subsubheading)\s+(.+)$/
249 and $_ = "\n=head4 $1\n";
251 # Block command handlers:
252 /^\@itemize\s*(\@[a-z]+|\*|-)?/ and do {
253 push @endwstack, $endw;
260 /^\@enumerate(?:\s+([a-zA-Z0-9]+))?/ and do {
261 push @endwstack, $endw;
272 /^\@((?:multi|[fv])?table)\s+(\@[a-z]+)/ and do {
273 push @endwstack, $endw;
277 $ic =~ s/\@(?:samp|strong|key|gcctabopt|option|env|command)/B/;
278 $ic =~ s/\@(?:code|kbd)/C/;
279 $ic =~ s/\@(?:dfn|var|emph|cite|i)/I/;
280 $ic =~ s/\@(?:file)/F/;
281 $ic =~ s/\@(?:columnfractions)//;
285 /^\@(multitable)\s+{.*/ and do {
286 push @endwstack, $endw;
293 /^\@((?:small)?example|verbatim|display)/ and do {
294 push @endwstack, $endw;
297 $_ = ""; # need a paragraph break
300 /^\@item\s+(.*\S)\s*$/ and $endw eq "multitable" and do {
302 $columns =~ s/\@tab/ : /;
304 $_ = "\n=item B<". $columns .">\n";
307 /^\@tab\s+(.*\S)\s*$/ and $endw eq "multitable" and do {
309 $columns =~ s/\@tab//;
315 /^\@itemx?\s*(.+)?$/ and do {
317 # Entity escapes prevent munging by the <> processing below.
318 $_ = "\n=item $ic\<$1\>\n";
320 $_ = "\n=item $ic\n";
321 $ic =~ y/A-Ya-y/B-Zb-z/;
322 $ic =~ s/(\d+)/$1 + 1/eg;
326 $chapter .= $shift.$_."\n";
328 # End of current file.
333 die "No filename or title\n" unless defined $fn && defined $tl;
336 print "=encoding utf8\n\n";
338 $chapters{NAME} = "$fn \- $tl\n";
339 $chapters{FOOTNOTES} .= "=back\n" if exists $chapters{FOOTNOTES};
341 unshift @chapters_sequence, "NAME";
342 for $chapter (@chapters_sequence) {
343 if (exists $chapters{$chapter}) {
344 $head = uc($chapter);
345 print "=head1 $head\n\n";
346 print scalar unmunge ($chapters{$chapter});
353 die "usage: $0 [-D toggle...] [infile [outfile]]\n";
360 # @value{foo} is replaced by whatever 'foo' is defined as.
361 while (m/(\@value\{([a-zA-Z0-9_-]+)\})/g) {
362 if (! exists $defs{$2}) {
363 print STDERR "Option $2 not defined\n";
371 # Formatting commands.
372 # Temporary escape for @r.
373 s/\@r\{([^\}]*)\}/R<$1>/g;
374 s/\@(?:dfn|var|emph|cite|i)\{([^\}]*)\}/I<$1>/g;
375 s/\@(?:code|kbd)\{([^\}]*)\}/C<$1>/g;
376 s/\@(?:gccoptlist|samp|strong|key|option|env|command|b)\{([^\}]*)\}/B<$1>/g;
377 s/\@sc\{([^\}]*)\}/\U$1/g;
378 s/\@file\{([^\}]*)\}/F<$1>/g;
379 s/\@w\{([^\}]*)\}/S<$1>/g;
380 s/\@(?:dmn|math)\{([^\}]*)\}/$1/g;
382 # Cross references are thrown away, as are @noindent and @refill.
383 # (@noindent is impossible in .pod, and @refill is unnecessary.)
384 # @* is also impossible in .pod; we discard it and any newline that
385 # follows it. Similarly, our macro @gol must be discarded.
387 s/\@anchor\{(?:[^\}]*)\}//g;
388 s/\(?\@xref\{(?:[^\}]*)\}(?:[^.<]|(?:<[^<>]*>))*\.\)?//g;
389 s/\s+\(\@pxref\{(?:[^\}]*)\}\)//g;
390 s/;\s+\@pxref\{(?:[^\}]*)\}//g;
391 s/\@ref\{(?:[^,\}]*,)(?:[^,\}]*,)([^,\}]*).*\}/B<$1>/g;
392 s/\@ref\{([^\}]*)\}/B<$1>/g;
398 # @uref can take one, two, or three arguments, with different
399 # semantics each time. @url and @email are just like @uref with
400 # one argument, for our purposes.
401 s/\@(?:uref|url|email)\{([^\},]*),?[^\}]*\}/<B<$1>>/g;
402 s/\@uref\{([^\},]*),([^\},]*)\}/$2 (C<$1>)/g;
403 s/\@uref\{([^\},]*),([^\},]*),([^\},]*)\}/$3/g;
405 # Turn B<blah I<blah> blah> into B<blah> I<blah> B<blah> to
406 # match Texinfo semantics of @emph inside @samp. Also handle @r
410 1 while s/B<((?:[^<>]|I<[^<>]*>)*)R<([^>]*)>/B<$1>${2}B</g;
411 1 while (s/B<([^<>]*)I<([^>]+)>/B<$1>I<$2>B</g);
412 1 while (s/I<([^<>]*)B<([^>]+)>/I<$1>B<$2>I</g);
414 s/([BI])<(\s+)([^>]+)>/$2$1<$3>/g;
415 s/([BI])<([^>]+?)(\s+)>/$1<$2>$3/g;
417 # Extract footnotes. This has to be done after all other
418 # processing because otherwise the regexp will choke on formatting
420 while (/\@footnote/g) {
421 s/\@footnote\{([^\}]+)\}/[$fnno]/;
422 add_footnote($1, $fnno);
431 # Replace escaped symbols with their equivalents.
445 unless (exists $chapters{FOOTNOTES}) {
446 $chapters{FOOTNOTES} = "\n=over 4\n\n";
449 $chapters{FOOTNOTES} .= "=item $fnno.\n\n"; $fnno++;
450 $chapters{FOOTNOTES} .= $_[0];
451 $chapters{FOOTNOTES} .= "\n\n";
454 # stolen from Symbol.pm
459 my $name = "GEN" . $genseq++;