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.

431 lines
12KB

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