texi2pod.pl 13 KB

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