texi2pod.pl 13 KB

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