texi2pod.pl 12 KB

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