texi2pod.pl 13 KB

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