You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

460 lines
13KB

  1. #!/usr/bin/env perl
  2. # Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
  3. # This file is part of GNU CC.
  4. # GNU CC is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2, or (at your option)
  7. # any later version.
  8. # GNU CC is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with GNU CC; see the file COPYING. If not, write to
  14. # the Free Software Foundation, 51 Franklin Street, Fifth Floor,
  15. # Boston, MA 02110-1301 USA
  16. # This does trivial (and I mean _trivial_) conversion of Texinfo
  17. # markup to Perl POD format. It's intended to be used to extract
  18. # something suitable for a manpage from a Texinfo document.
  19. use warnings;
  20. $output = 0;
  21. $skipping = 0;
  22. %sects = ();
  23. @sects_sequence = ();
  24. $section = "";
  25. @icstack = ();
  26. @endwstack = ();
  27. @skstack = ();
  28. @instack = ();
  29. $shift = "";
  30. %defs = ();
  31. $fnno = 1;
  32. $inf = "";
  33. @ibase = ();
  34. while ($_ = shift) {
  35. if (/^-D(.*)$/) {
  36. if ($1 ne "") {
  37. $flag = $1;
  38. } else {
  39. $flag = shift;
  40. }
  41. $value = "";
  42. ($flag, $value) = ($flag =~ /^([^=]+)(?:=(.+))?/);
  43. die "no flag specified for -D\n"
  44. unless $flag ne "";
  45. die "flags may only contain letters, digits, hyphens, dashes and underscores\n"
  46. unless $flag =~ /^[a-zA-Z0-9_-]+$/;
  47. $defs{$flag} = $value;
  48. } elsif (/^-I(.*)$/) {
  49. push @ibase, $1 ne "" ? $1 : shift;
  50. } elsif (/^-/) {
  51. usage();
  52. } else {
  53. $in = $_, next unless defined $in;
  54. $out = $_, next unless defined $out;
  55. usage();
  56. }
  57. }
  58. push @ibase, ".";
  59. if (defined $in) {
  60. $inf = gensym();
  61. open($inf, "<$in") or die "opening \"$in\": $!\n";
  62. push @ibase, $1 if $in =~ m|^(.+)/[^/]+$|;
  63. } else {
  64. $inf = \*STDIN;
  65. }
  66. if (defined $out) {
  67. open(STDOUT, ">$out") or die "opening \"$out\": $!\n";
  68. }
  69. while(defined $inf) {
  70. INF: while(<$inf>) {
  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. |(?:end\s+)?ifnottex # @ifnottex .. @end ifnottex: use contents
  79. )\b/x and next;
  80. chomp;
  81. # Look for filename and title markers.
  82. /^\@setfilename\s+([^.]+)/ and $fn = $1, next;
  83. /^\@settitle\s+([^.]+)/ and $tl = postprocess($1), next;
  84. # Identify a man title but keep only the one we are interested in.
  85. /^\@c\s+man\s+title\s+([A-Za-z0-9-]+)\s+(.+)/ and do {
  86. if (exists $defs{$1}) {
  87. $fn = $1;
  88. $tl = postprocess($2);
  89. }
  90. next;
  91. };
  92. /^\@include\s+(.+)$/ and do {
  93. push @instack, $inf;
  94. $inf = gensym();
  95. for (@ibase) {
  96. open($inf, "<" . $_ . "/" . $1) and next INF;
  97. }
  98. die "cannot open $1: $!\n";
  99. };
  100. # Look for blocks surrounded by @c man begin SECTION ... @c man end.
  101. # This really oughta be @ifman ... @end ifman and the like, but such
  102. # would require rev'ing all other Texinfo translators.
  103. /^\@c\s+man\s+begin\s+([A-Za-z ]+)/ and $sect = $1, push (@sects_sequence, $sect), $output = 1, next;
  104. /^\@c\s+man\s+end/ and do {
  105. $sects{$sect} = "" unless exists $sects{$sect};
  106. $sects{$sect} .= postprocess($section);
  107. $section = "";
  108. $output = 0;
  109. next;
  110. };
  111. # handle variables
  112. /^\@set\s+([a-zA-Z0-9_-]+)\s*(.*)$/ and do {
  113. $defs{$1} = $2;
  114. next;
  115. };
  116. /^\@clear\s+([a-zA-Z0-9_-]+)/ and do {
  117. delete $defs{$1};
  118. next;
  119. };
  120. next unless $output;
  121. # Discard comments. (Can't do it above, because then we'd never see
  122. # @c man lines.)
  123. /^\@c\b/ and next;
  124. # End-block handler goes up here because it needs to operate even
  125. # if we are skipping.
  126. /^\@end\s+([a-z]+)/ and do {
  127. # Ignore @end foo, where foo is not an operation which may
  128. # cause us to skip, if we are presently skipping.
  129. my $ended = $1;
  130. next if $skipping && $ended !~ /^(?:ifset|ifclear|ignore|menu|iftex)$/;
  131. die "\@end $ended without \@$ended at line $.\n" unless defined $endw;
  132. die "\@$endw ended by \@end $ended at line $.\n" unless $ended eq $endw;
  133. $endw = pop @endwstack;
  134. if ($ended =~ /^(?:ifset|ifclear|ignore|menu|iftex)$/) {
  135. $skipping = pop @skstack;
  136. next;
  137. } elsif ($ended =~ /^(?:example|smallexample|display)$/) {
  138. $shift = "";
  139. $_ = ""; # need a paragraph break
  140. } elsif ($ended =~ /^(?:itemize|enumerate|(?:multi|[fv])?table)$/) {
  141. $_ = "\n=back\n";
  142. $ic = pop @icstack;
  143. } else {
  144. die "unknown command \@end $ended at line $.\n";
  145. }
  146. };
  147. # We must handle commands which can cause skipping even while we
  148. # are skipping, otherwise we will not process nested conditionals
  149. # correctly.
  150. /^\@ifset\s+([a-zA-Z0-9_-]+)/ and do {
  151. push @endwstack, $endw;
  152. push @skstack, $skipping;
  153. $endw = "ifset";
  154. $skipping = 1 unless exists $defs{$1};
  155. next;
  156. };
  157. /^\@ifclear\s+([a-zA-Z0-9_-]+)/ and do {
  158. push @endwstack, $endw;
  159. push @skstack, $skipping;
  160. $endw = "ifclear";
  161. $skipping = 1 if exists $defs{$1};
  162. next;
  163. };
  164. /^\@(ignore|menu|iftex)\b/ and do {
  165. push @endwstack, $endw;
  166. push @skstack, $skipping;
  167. $endw = $1;
  168. $skipping = 1;
  169. next;
  170. };
  171. next if $skipping;
  172. # Character entities. First the ones that can be replaced by raw text
  173. # or discarded outright:
  174. s/\@copyright\{\}/(c)/g;
  175. s/\@dots\{\}/.../g;
  176. s/\@enddots\{\}/..../g;
  177. s/\@([.!? ])/$1/g;
  178. s/\@[:-]//g;
  179. s/\@bullet(?:\{\})?/*/g;
  180. s/\@TeX\{\}/TeX/g;
  181. s/\@pounds\{\}/\#/g;
  182. s/\@minus(?:\{\})?/-/g;
  183. s/\\,/,/g;
  184. # Now the ones that have to be replaced by special escapes
  185. # (which will be turned back into text by unmunge())
  186. s/&/&amp;/g;
  187. s/\@\{/&lbrace;/g;
  188. s/\@\}/&rbrace;/g;
  189. s/\@\@/&at;/g;
  190. # Inside a verbatim block, handle @var specially.
  191. if ($shift ne "") {
  192. s/\@var\{([^\}]*)\}/<$1>/g;
  193. }
  194. # POD doesn't interpret E<> inside a verbatim block.
  195. if ($shift eq "") {
  196. s/</&lt;/g;
  197. s/>/&gt;/g;
  198. } else {
  199. s/</&LT;/g;
  200. s/>/&GT;/g;
  201. }
  202. # Single line command handlers.
  203. /^\@(?:section|unnumbered|unnumberedsec|center|heading)\s+(.+)$/
  204. and $_ = "\n=head2 $1\n";
  205. /^\@(?:subsection|subheading)\s+(.+)$/
  206. and $_ = "\n=head3 $1\n";
  207. /^\@(?:subsubsection|subsubheading)\s+(.+)$/
  208. and $_ = "\n=head4 $1\n";
  209. # Block command handlers:
  210. /^\@itemize\s*(\@[a-z]+|\*|-)?/ and do {
  211. push @endwstack, $endw;
  212. push @icstack, $ic;
  213. $ic = $1 ? $1 : "*";
  214. $_ = "\n=over 4\n";
  215. $endw = "itemize";
  216. };
  217. /^\@enumerate(?:\s+([a-zA-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. /^\@((?:multi|[fv])?table)\s+(\@[a-z]+)/ and do {
  229. push @endwstack, $endw;
  230. push @icstack, $ic;
  231. $endw = $1;
  232. $ic = $2;
  233. $ic =~ s/\@(?:samp|strong|key|gcctabopt|option|env)/B/;
  234. $ic =~ s/\@(?:code|kbd)/C/;
  235. $ic =~ s/\@(?:dfn|var|emph|cite|i)/I/;
  236. $ic =~ s/\@(?:file)/F/;
  237. $ic =~ s/\@(?:columnfractions)//;
  238. $_ = "\n=over 4\n";
  239. };
  240. /^\@(multitable)\s+{.*/ and do {
  241. push @endwstack, $endw;
  242. push @icstack, $ic;
  243. $endw = $1;
  244. $ic = "";
  245. $_ = "\n=over 4\n";
  246. };
  247. /^\@((?:small)?example|display)/ and do {
  248. push @endwstack, $endw;
  249. $endw = $1;
  250. $shift = "\t";
  251. $_ = ""; # need a paragraph break
  252. };
  253. /^\@item\s+(.*\S)\s*$/ and $endw eq "multitable" and do {
  254. my $columns = $1;
  255. $columns =~ s/\@tab/ : /;
  256. $_ = "\n=item B&LT;". $columns ."&GT;\n";
  257. };
  258. /^\@tab\s+(.*\S)\s*$/ and $endw eq "multitable" and do {
  259. my $columns = $1;
  260. $columns =~ s/\@tab//;
  261. $_ = $columns;
  262. $section =~ s/$//;
  263. };
  264. /^\@itemx?\s*(.+)?$/ and do {
  265. if (defined $1) {
  266. # Entity escapes prevent munging by the <> processing below.
  267. $_ = "\n=item $ic\&LT;$1\&GT;\n";
  268. } else {
  269. $_ = "\n=item $ic\n";
  270. $ic =~ y/A-Ya-y/B-Zb-z/;
  271. $ic =~ s/(\d+)/$1 + 1/eg;
  272. }
  273. };
  274. $section .= $shift.$_."\n";
  275. }
  276. # End of current file.
  277. close($inf);
  278. $inf = pop @instack;
  279. }
  280. die "No filename or title\n" unless defined $fn && defined $tl;
  281. # always use utf8
  282. print "=encoding utf8\n\n";
  283. $sects{NAME} = "$fn \- $tl\n";
  284. $sects{FOOTNOTES} .= "=back\n" if exists $sects{FOOTNOTES};
  285. unshift @sects_sequence, "NAME";
  286. for $sect (@sects_sequence) {
  287. if(exists $sects{$sect}) {
  288. $head = $sect;
  289. $head =~ s/SEEALSO/SEE ALSO/;
  290. print "=head1 $head\n\n";
  291. print scalar unmunge ($sects{$sect});
  292. print "\n";
  293. }
  294. }
  295. sub usage
  296. {
  297. die "usage: $0 [-D toggle...] [infile [outfile]]\n";
  298. }
  299. sub postprocess
  300. {
  301. local $_ = $_[0];
  302. # @value{foo} is replaced by whatever 'foo' is defined as.
  303. while (m/(\@value\{([a-zA-Z0-9_-]+)\})/g) {
  304. if (! exists $defs{$2}) {
  305. print STDERR "Option $2 not defined\n";
  306. s/\Q$1\E//;
  307. } else {
  308. $value = $defs{$2};
  309. s/\Q$1\E/$value/;
  310. }
  311. }
  312. # Formatting commands.
  313. # Temporary escape for @r.
  314. s/\@r\{([^\}]*)\}/R<$1>/g;
  315. s/\@(?:dfn|var|emph|cite|i)\{([^\}]*)\}/I<$1>/g;
  316. s/\@(?:code|kbd)\{([^\}]*)\}/C<$1>/g;
  317. s/\@(?:gccoptlist|samp|strong|key|option|env|command|b)\{([^\}]*)\}/B<$1>/g;
  318. s/\@sc\{([^\}]*)\}/\U$1/g;
  319. s/\@file\{([^\}]*)\}/F<$1>/g;
  320. s/\@w\{([^\}]*)\}/S<$1>/g;
  321. s/\@(?:dmn|math)\{([^\}]*)\}/$1/g;
  322. # Cross references are thrown away, as are @noindent and @refill.
  323. # (@noindent is impossible in .pod, and @refill is unnecessary.)
  324. # @* is also impossible in .pod; we discard it and any newline that
  325. # follows it. Similarly, our macro @gol must be discarded.
  326. s/\@anchor\{(?:[^\}]*)\}//g;
  327. s/\(?\@xref\{(?:[^\}]*)\}(?:[^.<]|(?:<[^<>]*>))*\.\)?//g;
  328. s/\s+\(\@pxref\{(?:[^\}]*)\}\)//g;
  329. s/;\s+\@pxref\{(?:[^\}]*)\}//g;
  330. s/\@ref\{([^\}]*)\}/$1/g;
  331. s/\@noindent\s*//g;
  332. s/\@refill//g;
  333. s/\@gol//g;
  334. s/\@\*\s*\n?//g;
  335. # @uref can take one, two, or three arguments, with different
  336. # semantics each time. @url and @email are just like @uref with
  337. # one argument, for our purposes.
  338. s/\@(?:uref|url|email)\{([^\},]*)\}/&lt;B<$1>&gt;/g;
  339. s/\@uref\{([^\},]*),([^\},]*)\}/$2 (C<$1>)/g;
  340. s/\@uref\{([^\},]*),([^\},]*),([^\},]*)\}/$3/g;
  341. # Turn B<blah I<blah> blah> into B<blah> I<blah> B<blah> to
  342. # match Texinfo semantics of @emph inside @samp. Also handle @r
  343. # inside bold.
  344. s/&LT;/</g;
  345. s/&GT;/>/g;
  346. 1 while s/B<((?:[^<>]|I<[^<>]*>)*)R<([^>]*)>/B<$1>${2}B</g;
  347. 1 while (s/B<([^<>]*)I<([^>]+)>/B<$1>I<$2>B</g);
  348. 1 while (s/I<([^<>]*)B<([^>]+)>/I<$1>B<$2>I</g);
  349. s/[BI]<>//g;
  350. s/([BI])<(\s+)([^>]+)>/$2$1<$3>/g;
  351. s/([BI])<([^>]+?)(\s+)>/$1<$2>$3/g;
  352. # Extract footnotes. This has to be done after all other
  353. # processing because otherwise the regexp will choke on formatting
  354. # inside @footnote.
  355. while (/\@footnote/g) {
  356. s/\@footnote\{([^\}]+)\}/[$fnno]/;
  357. add_footnote($1, $fnno);
  358. $fnno++;
  359. }
  360. return $_;
  361. }
  362. sub unmunge
  363. {
  364. # Replace escaped symbols with their equivalents.
  365. local $_ = $_[0];
  366. s/&lt;/E<lt>/g;
  367. s/&gt;/E<gt>/g;
  368. s/&lbrace;/\{/g;
  369. s/&rbrace;/\}/g;
  370. s/&at;/\@/g;
  371. s/&amp;/&/g;
  372. return $_;
  373. }
  374. sub add_footnote
  375. {
  376. unless (exists $sects{FOOTNOTES}) {
  377. $sects{FOOTNOTES} = "\n=over 4\n\n";
  378. }
  379. $sects{FOOTNOTES} .= "=item $fnno.\n\n"; $fnno++;
  380. $sects{FOOTNOTES} .= $_[0];
  381. $sects{FOOTNOTES} .= "\n\n";
  382. }
  383. # stolen from Symbol.pm
  384. {
  385. my $genseq = 0;
  386. sub gensym
  387. {
  388. my $name = "GEN" . $genseq++;
  389. my $ref = \*{$name};
  390. delete $::{$name};
  391. return $ref;
  392. }
  393. }