texi2pod.pl 12 KB

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