texi2pod.pl 12 KB

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