| 1 | #! /usr/bin/perl -w |
| 2 | |
| 3 | # Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. |
| 4 | |
| 5 | # This file is part of GNU CC. |
| 6 | |
| 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) |
| 10 | # any later version. |
| 11 | |
| 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. |
| 16 | |
| 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, 59 Temple Place - Suite 330, |
| 20 | # Boston MA 02111-1307, 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 | $shift = ""; |
| 34 | %defs = (); |
| 35 | $fnno = 1; |
| 36 | |
| 37 | while ($_ = shift) { |
| 38 | if (/^-D(.*)$/) { |
| 39 | if ($1 ne "") { |
| 40 | $flag = $1; |
| 41 | } else { |
| 42 | $flag = shift; |
| 43 | } |
| 44 | $value=$flag; |
| 45 | $value =~ s,^[a-zA-Z0-9_]+,,; |
| 46 | $value =~ s,.*=,,; |
| 47 | $flag =~ s,=.*$,,; |
| 48 | die "no flag specified for -D\n" |
| 49 | unless $flag ne ""; |
| 50 | #print STDERR "FL = $flag, V = $value\n"; |
| 51 | $defs{$flag} = $value; |
| 52 | |
| 53 | } elsif (/^-/) { |
| 54 | usage(); |
| 55 | } else { |
| 56 | $in = $_, next unless defined $in; |
| 57 | $out = $_, next unless defined $out; |
| 58 | usage(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if (defined $in) { |
| 63 | open(STDIN, $in) or die "opening \"$in\": $!\n"; |
| 64 | } |
| 65 | if (defined $out) { |
| 66 | open(STDOUT, ">$out") or die "opening \"$out\": $!\n"; |
| 67 | } |
| 68 | |
| 69 | while(<STDIN>) |
| 70 | { |
| 71 | # Certain commands are discarded without further processing. |
| 72 | /^\@(?: |
| 73 | [a-z]+index # @*index: useful only in complete manual |
| 74 | |need # @need: useful only in printed manual |
| 75 | |(?:end\s+)?group # @group .. @end group: ditto |
| 76 | |page # @page: ditto |
| 77 | |node # @node: useful only in .info file |
| 78 | )\b/x and next; |
| 79 | |
| 80 | chomp; |
| 81 | |
| 82 | # Look for filename and title markers. |
| 83 | /^\@setfilename\s+([^.]+)/ and $fn = $1, next; |
| 84 | /^\@settitle\s+([^.]+)/ and $tl = postprocess($1), next; |
| 85 | |
| 86 | # Identify a man title but keep only the one we are interested in. |
| 87 | /^\@c man title ([A-Za-z0-9]+) (.+)/ and do { |
| 88 | if (exists $defs{$1}) { |
| 89 | $fn = $1; |
| 90 | $tl = postprocess($2); |
| 91 | } |
| 92 | next; |
| 93 | }; |
| 94 | |
| 95 | # Look for blocks surrounded by @c man begin SECTION ... @c man end. |
| 96 | # This really oughta be @ifman ... @end ifman and the like, but such |
| 97 | # would require rev'ing all other Texinfo translators. |
| 98 | /^\@c man begin ([A-Z]+) ([A-Za-z0-9]+)/ and do { |
| 99 | $output = 1 if exists $defs{$2}; |
| 100 | $sect = $1; |
| 101 | next; |
| 102 | }; |
| 103 | /^\@c man begin ([A-Z]+)/ and $sect = $1, $output = 1, next; |
| 104 | /^\@c man end/ and do { |
| 105 | $sects{$sect} = "" unless exists $sects{$sect}; |
| 106 | $sects{$sect} .= postprocess($section); |
| 107 | $section = ""; |
| 108 | $output = 0; |
| 109 | next; |
| 110 | }; |
| 111 | next unless $output; |
| 112 | |
| 113 | # Discard comments. (Can't do it above, because then we'd never see |
| 114 | # @c man lines.) |
| 115 | /^\@c\b/ and next; |
| 116 | |
| 117 | # End-block handler goes up here because it needs to operate even |
| 118 | # if we are skipping. |
| 119 | /^\@end\s+([a-z]+)/ and do { |
| 120 | # Ignore @end foo, where foo is not an operation which may |
| 121 | # cause us to skip, if we are presently skipping. |
| 122 | my $ended = $1; |
| 123 | next if $skipping && $ended !~ /^(?:ifset|ifclear|ignore|menu)$/; |
| 124 | |
| 125 | die "\@end $ended without \@$ended at line $.\n" unless defined $endw; |
| 126 | die "\@$endw ended by \@end $ended at line $.\n" unless $ended eq $endw; |
| 127 | |
| 128 | $endw = pop @endwstack; |
| 129 | |
| 130 | if ($ended =~ /^(?:ifset|ifclear|ignore|menu)$/) { |
| 131 | $skipping = pop @skstack; |
| 132 | next; |
| 133 | } elsif ($ended =~ /^(?:example|smallexample)$/) { |
| 134 | $shift = ""; |
| 135 | $_ = ""; # need a paragraph break |
| 136 | } elsif ($ended =~ /^(?:itemize|enumerate|table)$/) { |
| 137 | $_ = "\n=back\n"; |
| 138 | $ic = pop @icstack; |
| 139 | } else { |
| 140 | die "unknown command \@end $ended at line $.\n"; |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | # We must handle commands which can cause skipping even while we |
| 145 | # are skipping, otherwise we will not process nested conditionals |
| 146 | # correctly. |
| 147 | /^\@ifset\s+([a-zA-Z0-9_-]+)/ and do { |
| 148 | push @endwstack, $endw; |
| 149 | push @skstack, $skipping; |
| 150 | $endw = "ifset"; |
| 151 | $skipping = 1 unless exists $defs{$1}; |
| 152 | next; |
| 153 | }; |
| 154 | |
| 155 | /^\@ifclear\s+([a-zA-Z0-9_-]+)/ and do { |
| 156 | push @endwstack, $endw; |
| 157 | push @skstack, $skipping; |
| 158 | $endw = "ifclear"; |
| 159 | $skipping = 1 if exists $defs{$1}; |
| 160 | next; |
| 161 | }; |
| 162 | |
| 163 | /^\@(ignore|menu)\b/ and do { |
| 164 | push @endwstack, $endw; |
| 165 | push @skstack, $skipping; |
| 166 | $endw = $1; |
| 167 | $skipping = 1; |
| 168 | next; |
| 169 | }; |
| 170 | |
| 171 | next if $skipping; |
| 172 | |
| 173 | # Character entities. First the ones that can be replaced by raw text |
| 174 | # or discarded outright: |
| 175 | s/\@copyright\{\}/(c)/g; |
| 176 | s/\@dots\{\}/.../g; |
| 177 | s/\@enddots\{\}/..../g; |
| 178 | s/\@([.!? ])/$1/g; |
| 179 | s/\@[:-]//g; |
| 180 | s/\@bullet(?:\{\})?/*/g; |
| 181 | s/\@TeX\{\}/TeX/g; |
| 182 | s/\@pounds\{\}/\#/g; |
| 183 | s/\@minus(?:\{\})?/-/g; |
| 184 | s/\\,/,/g; |
| 185 | |
| 186 | # Now the ones that have to be replaced by special escapes |
| 187 | # (which will be turned back into text by unmunge()) |
| 188 | s/&/&/g; |
| 189 | s/\@\{/{/g; |
| 190 | s/\@\}/}/g; |
| 191 | s/\@\@/&at;/g; |
| 192 | # POD doesn't interpret E<> inside a verbatim block. |
| 193 | if ($shift eq "") { |
| 194 | s/</</g; |
| 195 | s/>/>/g; |
| 196 | } else { |
| 197 | s/</</g; |
| 198 | s/>/>/g; |
| 199 | } |
| 200 | |
| 201 | # Single line command handlers. |
| 202 | /^\@set\s+([a-zA-Z0-9_-]+)\s*(.*)$/ and $defs{$1} = $2, next; |
| 203 | /^\@clear\s+([a-zA-Z0-9_-]+)/ and delete $defs{$1}, next; |
| 204 | |
| 205 | /^\@section\s+(.+)$/ and $_ = "\n=head2 $1\n"; |
| 206 | /^\@subsection\s+(.+)$/ and $_ = "\n=head3 $1\n"; |
| 207 | |
| 208 | # Block command handlers: |
| 209 | /^\@itemize\s+(\@[a-z]+|\*|-)/ and do { |
| 210 | push @endwstack, $endw; |
| 211 | push @icstack, $ic; |
| 212 | $ic = $1; |
| 213 | $_ = "\n=over 4\n"; |
| 214 | $endw = "itemize"; |
| 215 | }; |
| 216 | |
| 217 | /^\@enumerate(?:\s+([A-Z0-9]+))?/ and do { |
| 218 | push @endwstack, $endw; |
| 219 | push @icstack, $ic; |
| 220 | if (defined $1) { |
| 221 | $ic = $1 . "."; |
| 222 | } else { |
| 223 | $ic = "1."; |
| 224 | } |
| 225 | $_ = "\n=over 4\n"; |
| 226 | $endw = "enumerate"; |
| 227 | }; |
| 228 | |
| 229 | /^\@table\s+(\@[a-z]+)/ and do { |
| 230 | push @endwstack, $endw; |
| 231 | push @icstack, $ic; |
| 232 | $ic = $1; |
| 233 | $ic =~ s/\@(?:samp|strong|key|gcctabopt|env)/B/; |
| 234 | $ic =~ s/\@(?:code|kbd)/C/; |
| 235 | $ic =~ s/\@(?:dfn|var|emph|cite|i)/I/; |
| 236 | $ic =~ s/\@(?:file)/F/; |
| 237 | $_ = "\n=over 4\n"; |
| 238 | $endw = "table"; |
| 239 | }; |
| 240 | |
| 241 | /^\@((?:small)?example)/ and do { |
| 242 | push @endwstack, $endw; |
| 243 | $endw = $1; |
| 244 | $shift = "\t"; |
| 245 | $_ = ""; # need a paragraph break |
| 246 | }; |
| 247 | |
| 248 | /^\@itemx?\s*(.+)?$/ and do { |
| 249 | if (defined $1) { |
| 250 | # Entity escapes prevent munging by the <> processing below. |
| 251 | $_ = "\n=item $ic\<$1\>\n"; |
| 252 | } else { |
| 253 | $_ = "\n=item $ic\n"; |
| 254 | $ic =~ y/A-Ya-y1-8/B-Zb-z2-9/; |
| 255 | } |
| 256 | }; |
| 257 | |
| 258 | $section .= $shift.$_."\n"; |
| 259 | } |
| 260 | |
| 261 | die "No filename or title\n" unless defined $fn && defined $tl; |
| 262 | |
| 263 | $sects{NAME} = "$fn \- $tl\n"; |
| 264 | $sects{FOOTNOTES} .= "=back\n" if exists $sects{FOOTNOTES}; |
| 265 | |
| 266 | for $sect (qw(NAME SYNOPSIS DESCRIPTION OPTIONS ENVIRONMENT FILES |
| 267 | BUGS NOTES FOOTNOTES SEEALSO AUTHOR COPYRIGHT)) { |
| 268 | if(exists $sects{$sect}) { |
| 269 | $head = $sect; |
| 270 | $head =~ s/SEEALSO/SEE ALSO/; |
| 271 | print "=head1 $head\n\n"; |
| 272 | print scalar unmunge ($sects{$sect}); |
| 273 | print "\n"; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | sub usage |
| 278 | { |
| 279 | die "usage: $0 [-D toggle...] [infile [outfile]]\n"; |
| 280 | } |
| 281 | |
| 282 | sub postprocess |
| 283 | { |
| 284 | local $_ = $_[0]; |
| 285 | |
| 286 | # @value{foo} is replaced by whatever 'foo' is defined as. |
| 287 | if (/\@value\{([a-zA-Z0-9_-]+)\}/) { |
| 288 | if (! exists $defs{$1}) { |
| 289 | print STDERR "Option $1 not define\n"; |
| 290 | } |
| 291 | |
| 292 | } |
| 293 | s/\@value\{([a-zA-Z0-9_-]+)\}/$defs{$1}/g; |
| 294 | |
| 295 | # Formatting commands. |
| 296 | # Temporary escape for @r. |
| 297 | s/\@r\{([^\}]*)\}/R<$1>/g; |
| 298 | s/\@(?:dfn|var|emph|cite|i)\{([^\}]*)\}/I<$1>/g; |
| 299 | s/\@(?:code|kbd)\{([^\}]*)\}/C<$1>/g; |
| 300 | s/\@(?:gccoptlist|samp|strong|key|option|env|command|b)\{([^\}]*)\}/B<$1>/g; |
| 301 | s/\@sc\{([^\}]*)\}/\U$1/g; |
| 302 | s/\@file\{([^\}]*)\}/F<$1>/g; |
| 303 | s/\@w\{([^\}]*)\}/S<$1>/g; |
| 304 | s/\@(?:dmn|math)\{([^\}]*)\}/$1/g; |
| 305 | |
| 306 | # Handle @r inside bold. |
| 307 | 1 while s/B<((?:[^<>]|I<[^<>]*>)*)R<([^>]*)>/B<$1>${2}B</g; |
| 308 | |
| 309 | # Cross references are thrown away, as are @noindent and @refill. |
| 310 | # (@noindent is impossible in .pod, and @refill is unnecessary.) |
| 311 | # @* is also impossible in .pod; we discard it and any newline that |
| 312 | # follows it. Similarly, our macro @gol must be discarded. |
| 313 | |
| 314 | s/\(?\@xref\{(?:[^\}]*)\}(?:[^.<]|(?:<[^<>]*>))*\.\)?//g; |
| 315 | s/\s+\(\@pxref\{(?:[^\}]*)\}\)//g; |
| 316 | s/;\s+\@pxref\{(?:[^\}]*)\}//g; |
| 317 | s/\@noindent\s*//g; |
| 318 | s/\@refill//g; |
| 319 | s/\@gol//g; |
| 320 | s/\@\*\s*\n?//g; |
| 321 | |
| 322 | # @uref can take one, two, or three arguments, with different |
| 323 | # semantics each time. @url and @email are just like @uref with |
| 324 | # one argument, for our purposes. |
| 325 | s/\@(?:uref|url|email)\{([^\},]*)\}/<B<$1>>/g; |
| 326 | s/\@uref\{([^\},]*),([^\},]*)\}/$2 (C<$1>)/g; |
| 327 | s/\@uref\{([^\},]*),([^\},]*),([^\},]*)\}/$3/g; |
| 328 | |
| 329 | # Turn B<blah I<blah> blah> into B<blah> I<blah> B<blah> to |
| 330 | # match Texinfo semantics of @emph inside @samp. |
| 331 | s/</</g; |
| 332 | s/>/>/g; |
| 333 | 1 while (s/B<([^<>]*)I<([^>]+)>/B<$1>I<$2>B</g); |
| 334 | 1 while (s/I<([^<>]*)B<([^>]+)>/I<$1>B<$2>I</g); |
| 335 | s/[BI]<>//g; |
| 336 | s/([BI])<(\s+)([^>]+)>/$2$1<$3>/g; |
| 337 | s/([BI])<([^>]+?)(\s+)>/$1<$2>$3/g; |
| 338 | |
| 339 | # Extract footnotes. This has to be done after all other |
| 340 | # processing because otherwise the regexp will choke on formatting |
| 341 | # inside @footnote. |
| 342 | while (/\@footnote/g) { |
| 343 | s/\@footnote\{([^\}]+)\}/[$fnno]/; |
| 344 | add_footnote($1, $fnno); |
| 345 | $fnno++; |
| 346 | } |
| 347 | |
| 348 | return $_; |
| 349 | } |
| 350 | |
| 351 | sub unmunge |
| 352 | { |
| 353 | # Replace escaped symbols with their equivalents. |
| 354 | local $_ = $_[0]; |
| 355 | |
| 356 | s/</E<lt>/g; |
| 357 | s/>/E<gt>/g; |
| 358 | s/{/\{/g; |
| 359 | s/}/\}/g; |
| 360 | s/&at;/\@/g; |
| 361 | s/&/&/g; |
| 362 | return $_; |
| 363 | } |
| 364 | |
| 365 | sub add_footnote |
| 366 | { |
| 367 | unless (exists $sects{FOOTNOTES}) { |
| 368 | $sects{FOOTNOTES} = "\n=over 4\n\n"; |
| 369 | } |
| 370 | |
| 371 | $sects{FOOTNOTES} .= "=item $fnno.\n\n"; $fnno++; |
| 372 | $sects{FOOTNOTES} .= $_[0]; |
| 373 | $sects{FOOTNOTES} .= "\n\n"; |
| 374 | } |
| 375 | |