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