texi2pod.pl 12 KB

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