texi2pod.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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, 59 Temple Place - Suite 330,
  15. # Boston MA 02111-1307, 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. 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 (/^-/) {
  47. usage();
  48. } else {
  49. $in = $_, next unless defined $in;
  50. $out = $_, next unless defined $out;
  51. usage();
  52. }
  53. }
  54. if (defined $in) {
  55. $inf = gensym();
  56. open($inf, "<$in") or die "opening \"$in\": $!\n";
  57. $ibase = $1 if $in =~ m|^(.+)/[^/]+$|;
  58. } else {
  59. $inf = \*STDIN;
  60. }
  61. if (defined $out) {
  62. open(STDOUT, ">$out") or die "opening \"$out\": $!\n";
  63. }
  64. while(defined $inf) {
  65. while(<$inf>) {
  66. # Certain commands are discarded without further processing.
  67. /^\@(?:
  68. [a-z]+index # @*index: useful only in complete manual
  69. |need # @need: useful only in printed manual
  70. |(?:end\s+)?group # @group .. @end group: ditto
  71. |page # @page: ditto
  72. |node # @node: useful only in .info file
  73. |(?:end\s+)?ifnottex # @ifnottex .. @end ifnottex: use contents
  74. )\b/x and next;
  75. chomp;
  76. # Look for filename and title markers.
  77. /^\@setfilename\s+([^.]+)/ and $fn = $1, next;
  78. /^\@settitle\s+([^.]+)/ and $tl = postprocess($1), next;
  79. # Identify a man title but keep only the one we are interested in.
  80. /^\@c\s+man\s+title\s+([A-Za-z0-9-]+)\s+(.+)/ and do {
  81. if (exists $defs{$1}) {
  82. $fn = $1;
  83. $tl = postprocess($2);
  84. }
  85. next;
  86. };
  87. # Look for blocks surrounded by @c man begin SECTION ... @c man end.
  88. # This really oughta be @ifman ... @end ifman and the like, but such
  89. # would require rev'ing all other Texinfo translators.
  90. /^\@c\s+man\s+begin\s+([A-Z]+)\s+([A-Za-z0-9-]+)/ and do {
  91. $output = 1 if exists $defs{$2};
  92. $sect = $1;
  93. next;
  94. };
  95. /^\@c\s+man\s+begin\s+([A-Z]+)/ and $sect = $1, $output = 1, next;
  96. /^\@c\s+man\s+end/ and do {
  97. $sects{$sect} = "" unless exists $sects{$sect};
  98. $sects{$sect} .= postprocess($section);
  99. $section = "";
  100. $output = 0;
  101. next;
  102. };
  103. # handle variables
  104. /^\@set\s+([a-zA-Z0-9_-]+)\s*(.*)$/ and do {
  105. $defs{$1} = $2;
  106. next;
  107. };
  108. /^\@clear\s+([a-zA-Z0-9_-]+)/ and do {
  109. delete $defs{$1};
  110. next;
  111. };
  112. next unless $output;
  113. # Discard comments. (Can't do it above, because then we'd never see
  114. # @c man lines.)
  115. /^\@c\b/ and next;
  116. # End-block handler goes up here because it needs to operate even
  117. # if we are skipping.
  118. /^\@end\s+([a-z]+)/ and do {
  119. # Ignore @end foo, where foo is not an operation which may
  120. # cause us to skip, if we are presently skipping.
  121. my $ended = $1;
  122. next if $skipping && $ended !~ /^(?:ifset|ifclear|ignore|menu|iftex)$/;
  123. die "\@end $ended without \@$ended at line $.\n" unless defined $endw;
  124. die "\@$endw ended by \@end $ended at line $.\n" unless $ended eq $endw;
  125. $endw = pop @endwstack;
  126. if ($ended =~ /^(?:ifset|ifclear|ignore|menu|iftex)$/) {
  127. $skipping = pop @skstack;
  128. next;
  129. } elsif ($ended =~ /^(?:example|smallexample|display)$/) {
  130. $shift = "";
  131. $_ = ""; # need a paragraph break
  132. } elsif ($ended =~ /^(?:itemize|enumerate|[fv]?table)$/) {
  133. $_ = "\n=back\n";
  134. $ic = pop @icstack;
  135. } else {
  136. die "unknown command \@end $ended at line $.\n";
  137. }
  138. };
  139. # We must handle commands which can cause skipping even while we
  140. # are skipping, otherwise we will not process nested conditionals
  141. # correctly.
  142. /^\@ifset\s+([a-zA-Z0-9_-]+)/ and do {
  143. push @endwstack, $endw;
  144. push @skstack, $skipping;
  145. $endw = "ifset";
  146. $skipping = 1 unless exists $defs{$1};
  147. next;
  148. };
  149. /^\@ifclear\s+([a-zA-Z0-9_-]+)/ and do {
  150. push @endwstack, $endw;
  151. push @skstack, $skipping;
  152. $endw = "ifclear";
  153. $skipping = 1 if exists $defs{$1};
  154. next;
  155. };
  156. /^\@(ignore|menu|iftex)\b/ and do {
  157. push @endwstack, $endw;
  158. push @skstack, $skipping;
  159. $endw = $1;
  160. $skipping = 1;
  161. next;
  162. };
  163. next if $skipping;
  164. # Character entities. First the ones that can be replaced by raw text
  165. # or discarded outright:
  166. s/\@copyright\{\}/(c)/g;
  167. s/\@dots\{\}/.../g;
  168. s/\@enddots\{\}/..../g;
  169. s/\@([.!? ])/$1/g;
  170. s/\@[:-]//g;
  171. s/\@bullet(?:\{\})?/*/g;
  172. s/\@TeX\{\}/TeX/g;
  173. s/\@pounds\{\}/\#/g;
  174. s/\@minus(?:\{\})?/-/g;
  175. s/\\,/,/g;
  176. # Now the ones that have to be replaced by special escapes
  177. # (which will be turned back into text by unmunge())
  178. s/&/&amp;/g;
  179. s/\@\{/&lbrace;/g;
  180. s/\@\}/&rbrace;/g;
  181. s/\@\@/&at;/g;
  182. # Inside a verbatim block, handle @var specially.
  183. if ($shift ne "") {
  184. s/\@var\{([^\}]*)\}/<$1>/g;
  185. }
  186. # POD doesn't interpret E<> inside a verbatim block.
  187. if ($shift eq "") {
  188. s/</&lt;/g;
  189. s/>/&gt;/g;
  190. } else {
  191. s/</&LT;/g;
  192. s/>/&GT;/g;
  193. }
  194. # Single line command handlers.
  195. /^\@include\s+(.+)$/ and do {
  196. push @instack, $inf;
  197. $inf = gensym();
  198. # Try cwd and $ibase.
  199. open($inf, "<" . $1)
  200. or open($inf, "<" . $ibase . "/" . $1)
  201. or die "cannot open $1 or $ibase/$1: $!\n";
  202. next;
  203. };
  204. /^\@(?:section|unnumbered|unnumberedsec|center)\s+(.+)$/
  205. and $_ = "\n=head2 $1\n";
  206. /^\@subsection\s+(.+)$/
  207. and $_ = "\n=head3 $1\n";
  208. # Block command handlers:
  209. /^\@itemize\s+(\@[a-z]+|\*|-)/ and do {
  210. push @endwstack, $endw;
  211. push @icstack, $ic;
  212. $ic = $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)/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. for $sect (qw(NAME SYNOPSIS DESCRIPTION OPTIONS ENVIRONMENT FILES
  264. BUGS NOTES FOOTNOTES SEEALSO AUTHOR COPYRIGHT)) {
  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/\(?\@xref\{(?:[^\}]*)\}(?:[^.<]|(?:<[^<>]*>))*\.\)?//g;
  305. s/\s+\(\@pxref\{(?:[^\}]*)\}\)//g;
  306. s/;\s+\@pxref\{(?:[^\}]*)\}//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. }