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.

433 lines
12KB

  1. #! /usr/bin/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|[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. /^\@([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. $_ = "\n=over 4\n";
  238. };
  239. /^\@((?:small)?example|display)/ and do {
  240. push @endwstack, $endw;
  241. $endw = $1;
  242. $shift = "\t";
  243. $_ = ""; # need a paragraph break
  244. };
  245. /^\@itemx?\s*(.+)?$/ and do {
  246. if (defined $1) {
  247. # Entity escapes prevent munging by the <> processing below.
  248. $_ = "\n=item $ic\&LT;$1\&GT;\n";
  249. } else {
  250. $_ = "\n=item $ic\n";
  251. $ic =~ y/A-Ya-y/B-Zb-z/;
  252. $ic =~ s/(\d+)/$1 + 1/eg;
  253. }
  254. };
  255. $section .= $shift.$_."\n";
  256. }
  257. # End of current file.
  258. close($inf);
  259. $inf = pop @instack;
  260. }
  261. die "No filename or title\n" unless defined $fn && defined $tl;
  262. $sects{NAME} = "$fn \- $tl\n";
  263. $sects{FOOTNOTES} .= "=back\n" if exists $sects{FOOTNOTES};
  264. unshift @sects_sequence, "NAME";
  265. for $sect (@sects_sequence) {
  266. if(exists $sects{$sect}) {
  267. $head = $sect;
  268. $head =~ s/SEEALSO/SEE ALSO/;
  269. print "=head1 $head\n\n";
  270. print scalar unmunge ($sects{$sect});
  271. print "\n";
  272. }
  273. }
  274. sub usage
  275. {
  276. die "usage: $0 [-D toggle...] [infile [outfile]]\n";
  277. }
  278. sub postprocess
  279. {
  280. local $_ = $_[0];
  281. # @value{foo} is replaced by whatever 'foo' is defined as.
  282. while (m/(\@value\{([a-zA-Z0-9_-]+)\})/g) {
  283. if (! exists $defs{$2}) {
  284. print STDERR "Option $2 not defined\n";
  285. s/\Q$1\E//;
  286. } else {
  287. $value = $defs{$2};
  288. s/\Q$1\E/$value/;
  289. }
  290. }
  291. # Formatting commands.
  292. # Temporary escape for @r.
  293. s/\@r\{([^\}]*)\}/R<$1>/g;
  294. s/\@(?:dfn|var|emph|cite|i)\{([^\}]*)\}/I<$1>/g;
  295. s/\@(?:code|kbd)\{([^\}]*)\}/C<$1>/g;
  296. s/\@(?:gccoptlist|samp|strong|key|option|env|command|b)\{([^\}]*)\}/B<$1>/g;
  297. s/\@sc\{([^\}]*)\}/\U$1/g;
  298. s/\@file\{([^\}]*)\}/F<$1>/g;
  299. s/\@w\{([^\}]*)\}/S<$1>/g;
  300. s/\@(?:dmn|math)\{([^\}]*)\}/$1/g;
  301. # Cross references are thrown away, as are @noindent and @refill.
  302. # (@noindent is impossible in .pod, and @refill is unnecessary.)
  303. # @* is also impossible in .pod; we discard it and any newline that
  304. # follows it. Similarly, our macro @gol must be discarded.
  305. s/\@anchor{(?:[^\}]*)\}//g;
  306. s/\(?\@xref\{(?:[^\}]*)\}(?:[^.<]|(?:<[^<>]*>))*\.\)?//g;
  307. s/\s+\(\@pxref\{(?:[^\}]*)\}\)//g;
  308. s/;\s+\@pxref\{(?:[^\}]*)\}//g;
  309. s/\@ref\{([^\}]*)\}/$1/g;
  310. s/\@noindent\s*//g;
  311. s/\@refill//g;
  312. s/\@gol//g;
  313. s/\@\*\s*\n?//g;
  314. # @uref can take one, two, or three arguments, with different
  315. # semantics each time. @url and @email are just like @uref with
  316. # one argument, for our purposes.
  317. s/\@(?:uref|url|email)\{([^\},]*)\}/&lt;B<$1>&gt;/g;
  318. s/\@uref\{([^\},]*),([^\},]*)\}/$2 (C<$1>)/g;
  319. s/\@uref\{([^\},]*),([^\},]*),([^\},]*)\}/$3/g;
  320. # Turn B<blah I<blah> blah> into B<blah> I<blah> B<blah> to
  321. # match Texinfo semantics of @emph inside @samp. Also handle @r
  322. # inside bold.
  323. s/&LT;/</g;
  324. s/&GT;/>/g;
  325. 1 while s/B<((?:[^<>]|I<[^<>]*>)*)R<([^>]*)>/B<$1>${2}B</g;
  326. 1 while (s/B<([^<>]*)I<([^>]+)>/B<$1>I<$2>B</g);
  327. 1 while (s/I<([^<>]*)B<([^>]+)>/I<$1>B<$2>I</g);
  328. s/[BI]<>//g;
  329. s/([BI])<(\s+)([^>]+)>/$2$1<$3>/g;
  330. s/([BI])<([^>]+?)(\s+)>/$1<$2>$3/g;
  331. # Extract footnotes. This has to be done after all other
  332. # processing because otherwise the regexp will choke on formatting
  333. # inside @footnote.
  334. while (/\@footnote/g) {
  335. s/\@footnote\{([^\}]+)\}/[$fnno]/;
  336. add_footnote($1, $fnno);
  337. $fnno++;
  338. }
  339. return $_;
  340. }
  341. sub unmunge
  342. {
  343. # Replace escaped symbols with their equivalents.
  344. local $_ = $_[0];
  345. s/&lt;/E<lt>/g;
  346. s/&gt;/E<gt>/g;
  347. s/&lbrace;/\{/g;
  348. s/&rbrace;/\}/g;
  349. s/&at;/\@/g;
  350. s/&amp;/&/g;
  351. return $_;
  352. }
  353. sub add_footnote
  354. {
  355. unless (exists $sects{FOOTNOTES}) {
  356. $sects{FOOTNOTES} = "\n=over 4\n\n";
  357. }
  358. $sects{FOOTNOTES} .= "=item $fnno.\n\n"; $fnno++;
  359. $sects{FOOTNOTES} .= $_[0];
  360. $sects{FOOTNOTES} .= "\n\n";
  361. }
  362. # stolen from Symbol.pm
  363. {
  364. my $genseq = 0;
  365. sub gensym
  366. {
  367. my $name = "GEN" . $genseq++;
  368. my $ref = \*{$name};
  369. delete $::{$name};
  370. return $ref;
  371. }
  372. }