home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / perl-5.003-bin.lha / bin / pod2man < prev    next >
Text File  |  1996-10-12  |  26KB  |  1,047 lines

  1. #!/ade/bin/perl
  2. eval 'exec perl -S $0 "$@"'
  3.     if 0;
  4.  
  5. =head1 NAME
  6.  
  7. pod2man - translate embedded Perl pod directives into man pages
  8.  
  9. =head1 SYNOPSIS
  10.  
  11. B<pod2man>
  12. [ B<--section=>I<manext> ]
  13. [ B<--release=>I<relpatch> ]
  14. [ B<--center=>I<string> ]
  15. [ B<--date=>I<string> ]
  16. [ B<--fixed=>I<font> ]
  17. [ B<--official> ]
  18. I<inputfile>
  19.  
  20. =head1 DESCRIPTION
  21.  
  22. B<pod2man> converts its input file containing embedded pod directives (see
  23. L<perlpod>) into nroff source suitable for viewing with nroff(1) or
  24. troff(1) using the man(7) macro set.
  25.  
  26. Besides the obvious pod conversions, B<pod2man> also takes care of
  27. func(), func(n), and simple variable references like $foo or @bar so
  28. you don't have to use code escapes for them; complex expressions like
  29. C<$fred{'stuff'}> will still need to be escaped, though.  Other nagging
  30. little roffish things that it catches include translating the minus in
  31. something like foo-bar, making a long dash--like this--into a real em
  32. dash, fixing up "paired quotes", putting a little space after the
  33. parens in something like func(), making C++ and PI look right, making
  34. double underbars have a little tiny space between them, making ALLCAPS
  35. a teeny bit smaller in troff(1), and escaping backslashes so you don't
  36. have to.
  37.  
  38. =head1 OPTIONS
  39.  
  40. =over 8
  41.  
  42. =item center
  43.  
  44. Set the centered header to a specific string.  The default is
  45. "User Contributed Perl Documentation", unless the C<--official> flag is
  46. given, in which case the default is "Perl Programmers Reference Guide".
  47.  
  48. =item date
  49.  
  50. Set the left-hand footer string to this value.  By default,
  51. the modification date of the input file will be used.
  52.  
  53. =item fixed
  54.  
  55. The fixed font to use for code refs.  Defaults to CW.
  56.  
  57. =item official
  58.  
  59. Set the default header to indicate that this page is of
  60. the standard release in case C<--center> is not given.
  61.  
  62. =item release
  63.  
  64. Set the centered footer.  By default, this is the current
  65. perl release.
  66.  
  67. =item section
  68.  
  69. Set the section for the C<.TH> macro.  The standard conventions on
  70. sections are to use 1 for user commands,  2 for system calls, 3 for
  71. functions, 4 for devices, 5 for file formats, 6 for games, 7 for
  72. miscellaneous information, and 8 for administrator commands.  This works
  73. best if you put your Perl man pages in a separate tree, like
  74. F</usr/local/perl/man/>.  By default, section 1 will be used
  75. unless the file ends in F<.pm> in which case section 3 will be selected.
  76.  
  77. =back
  78.  
  79. =head1 Anatomy of a Proper Man Page
  80.  
  81. For those not sure of the proper layout of a man page, here's
  82. an example of the skeleton of a proper man page.  Head of the
  83. major headers should be setout as a C<=head1> directive, and
  84. are historically written in the rather startling ALL UPPER CASE
  85. format, although this is not mandatory.
  86. Minor headers may be included using C<=head2>, and are
  87. typically in mixed case.
  88.  
  89. =over 10
  90.  
  91. =item NAME
  92.  
  93. Mandatory section; should be a comma-separated list of programs or
  94. functions documented by this podpage, such as:
  95.  
  96.     foo, bar - programs to do something
  97.  
  98. =item SYNOPSIS
  99.  
  100. A short usage summary for programs and functions, which
  101. may someday be deemed mandatory.
  102.  
  103. =item DESCRIPTION
  104.  
  105. Long drawn out discussion of the program.  It's a good idea to break this
  106. up into subsections using the C<=head2> directives, like
  107.  
  108.     =head2 A Sample Subection
  109.  
  110.     =head2 Yet Another Sample Subection
  111.  
  112. =item OPTIONS
  113.  
  114. Some people make this separate from the description.
  115.  
  116. =item RETURN VALUE
  117.  
  118. What the program or function returns if successful.
  119.  
  120. =item ERRORS
  121.  
  122. Exceptions, return codes, exit stati, and errno settings.
  123.  
  124. =item EXAMPLES
  125.  
  126. Give some example uses of the program.
  127.  
  128. =item ENVIRONMENT
  129.  
  130. Envariables this program might care about.
  131.  
  132. =item FILES
  133.  
  134. All files used by the program.  You should probably use the FE<lt>E<gt>
  135. for these.
  136.  
  137. =item SEE ALSO
  138.  
  139. Other man pages to check out, like man(1), man(7), makewhatis(8), or catman(8).
  140.  
  141. =item NOTES
  142.  
  143. Miscellaneous commentary.
  144.  
  145. =item CAVEATS
  146.  
  147. Things to take special care with; sometimes called WARNINGS.
  148.  
  149. =item DIAGNOSTICS
  150.  
  151. All possible messages the program can print out--and
  152. what they mean.
  153.  
  154. =item BUGS
  155.  
  156. Things that are broken or just don't work quite right.
  157.  
  158. =item RESTRICTIONS
  159.  
  160. Bugs you don't plan to fix :-)
  161.  
  162. =item AUTHOR
  163.  
  164. Who wrote it (or AUTHORS if multiple).
  165.  
  166. =item HISTORY
  167.  
  168. Programs derived from other sources sometimes have this, or
  169. you might keep a modification long here.
  170.  
  171. =back
  172.  
  173. =head1 EXAMPLES
  174.  
  175.     pod2man program > program.1
  176.     pod2man some_module.pm > /usr/perl/man/man3/some_module.3
  177.     pod2man --section=7 note.pod > note.7
  178.  
  179. =head1 DIAGNOSTICS
  180.  
  181. The following diagnostics are generated by B<pod2man>.  Items
  182. marked "(W)" are non-fatal, whereas the "(F)" errors will cause
  183. B<pod2man> to immediately exit with a non-zero status.
  184.  
  185. =over 4
  186.  
  187. =item bad option in paragraph %d of %s: ``%s'' should be [%s]<%s>
  188.  
  189. (W) If you start include an option, you should set it off
  190. as bold, italic, or code.
  191.  
  192. =item can't open %s: %s
  193.  
  194. (F) The input file wasn't available for the given reason.
  195.  
  196. =item high bit char in input stream
  197.  
  198. (W) You can't use high-bit characters in the input stream,
  199. because the translator uses them for its own nefarious purposes.
  200. Use an HTML entity in angle brackets instead.
  201.  
  202. =item Improper man page - no dash in NAME header in paragraph %d of %s
  203.  
  204. (W) The NAME header did not have an isolated dash in it.  This is
  205. considered important.
  206.  
  207. =item Invalid man page - no NAME line in %s
  208.  
  209. (F) You did not include a NAME header, which is essential.
  210.  
  211. =item roff font should be 1 or 2 chars, not `%s'  (F)
  212.  
  213. (F) The font specified with the C<--fixed> option was not
  214. a one- or two-digit roff font.
  215.  
  216. =item %s is missing required section: %s
  217.  
  218. (W) Required sections include NAME, DESCRIPTION, and if you're
  219. using a section starting with a 3, also a SYNOPSIS.  Actually,
  220. not having a NAME is a fatal.
  221.  
  222. =item Unknown escape: %s in %s
  223.  
  224. (W) An unknown HTML entity (probably for an 8-bit character) was given via
  225. a C<E<lt>E<gt>> directive.  Besides amp, lt, gt, and quot, recognized
  226. entities are Aacute, aacute, Acirc, acirc, AElig, aelig, Agrave, agrave,
  227. Aring, aring, Atilde, atilde, Auml, auml, Ccedil, ccedil, Eacute, eacute,
  228. Ecirc, ecirc, Egrave, egrave, ETH, eth, Euml, euml, Iacute, iacute, Icirc,
  229. icirc, Igrave, igrave, Iuml, iuml, Ntilde, ntilde, Oacute, oacute, Ocirc,
  230. ocirc, Ograve, ograve, Oslash, oslash, Otilde, otilde, Ouml, ouml, szlig,
  231. THORN, thorn, Uacute, uacute, Ucirc, ucirc, Ugrave, ugrave, Uuml, uuml,
  232. Yacute, yacute, and yuml.
  233.  
  234. =item Unmatched =back
  235.  
  236. (W) You have a C<=back> without a corresponding C<=over>.
  237.  
  238. =item Unrecognized pod directive: %s
  239.  
  240. (W) You specified a pod directive that isn't in the known list of
  241. C<=head1>, C<=head2>, C<=item>, C<=over>, C<=back>, or C<=cut>.
  242.  
  243.  
  244. =back
  245.  
  246. =head1 NOTES
  247.  
  248. If you would like to print out a lot of man page continuously, you
  249. probably want to set the C and D registers to set contiguous page
  250. numbering and even/odd paging, at least one some versions of man(7).
  251. Settting the F register will get you some additional experimental
  252. indexing:
  253.  
  254.     troff -man -rC1 -rD1 -rF1 perl.1 perldata.1 perlsyn.1 ...
  255.  
  256. The indexing merely outputs messages via C<.tm> for each
  257. major page, section, subsection, item, and any C<XE<lt>E<gt>>
  258. directives.
  259.  
  260.  
  261. =head1 RESTRICTIONS
  262.  
  263. You shouldn't use 8-bit characters in the input stream, as these
  264. will be used by the translator.
  265.  
  266. =head1 BUGS
  267.  
  268. The =over and =back directives don't really work right.  They
  269. take absolute positions instead of offsets, don't nest well, and
  270. making people count is suboptimal in any event.
  271.  
  272. =head1 AUTHORS
  273.  
  274. Original prototype by Larry Wall, but so massively hacked over by
  275. Tom Christiansen such that Larry probably doesn't recognize it anymore.
  276.  
  277. =cut
  278.  
  279. $/ = "";
  280. $cutting = 1;
  281.  
  282. ($version,$patch) = `\PATH=.:..:\$PATH; perl -v` =~ /version (\d\.\d{3}(?: +)(?:\S+)?)(?:.*patchlevel (\d\S*))?/s;
  283. $DEF_RELEASE  = "perl $version";
  284. $DEF_RELEASE .= ", patch $patch" if $patch;
  285.  
  286.  
  287. sub makedate {
  288.     my $secs = shift;
  289.     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($secs);
  290.     my $mname = (qw{Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec})[$mon];
  291.     return "$mday/$mname/$year";
  292. }
  293.  
  294. use Getopt::Long;
  295.  
  296. $DEF_SECTION = 1;
  297. $DEF_CENTER = "User Contributed Perl Documentation";
  298. $STD_CENTER = "Perl Programmers Reference Guide";
  299. $DEF_FIXED = 'CW';
  300.  
  301. sub usage {
  302.     warn "$0: @_\n" if @_;
  303.     die <<EOF;
  304. usage: $0 [options] podpage
  305. Options are:
  306.     --section=manext      (default "$DEF_SECTION")
  307.     --release=relpatch    (default "$DEF_RELEASE")
  308.     --center=string       (default "$DEF_CENTER")
  309.     --date=string         (default "$DEF_DATE")
  310.     --fixed=font          (default "$DEF_FIXED")
  311.     --official          (default NOT)
  312. EOF
  313. }
  314.  
  315. $uok = GetOptions( qw(
  316.     section=s
  317.     release=s
  318.     center=s
  319.     date=s
  320.     fixed=s
  321.     official
  322.     help));
  323.  
  324. $DEF_DATE = makedate((stat($ARGV[0]))[9] || time());
  325.  
  326. usage("Usage error!") unless $uok;
  327. usage() if $opt_help;
  328. usage("Need one and only one podpage argument") unless @ARGV == 1;
  329.  
  330. $section = $opt_section || ($ARGV[0] =~ /\.pm$/ ? 3 : $DEF_SECTION);
  331. $RP = $opt_release || $DEF_RELEASE;
  332. $center = $opt_center || ($opt_official ? $STD_CENTER : $DEF_CENTER);
  333.  
  334. $CFont = $opt_fixed || $DEF_FIXED;
  335.  
  336. if (length($CFont) == 2) {
  337.     $CFont_embed = "\\f($CFont";
  338. }
  339. elsif (length($CFont) == 1) {
  340.     $CFont_embed = "\\f$CFont";
  341. }
  342. else {
  343.     die "roff font should be 1 or 2 chars, not `$CFont_embed'";
  344. }
  345.  
  346. $section = $opt_section || $DEF_SECTION;
  347. $date = $opt_date || $DEF_DATE;
  348.  
  349. for (qw{NAME DESCRIPTION}) {
  350. # for (qw{NAME DESCRIPTION AUTHOR}) {
  351.     $wanna_see{$_}++;
  352. }
  353. $wanna_see{SYNOPSIS}++ if $section =~ /^3/;
  354.  
  355.  
  356. $name = @ARGV ? $ARGV[0] : "<STDIN>";
  357. $Filename = $name;
  358. $name = uc($name) if $section =~ /^1/;
  359. $name =~ s/\.[^.]*$//;
  360.  
  361. if ($name ne 'something') {
  362.     FCHECK: {
  363.     open(F, "< $ARGV[0]") || die "can't open $ARGV[0]: $!";
  364.     while (<F>) {
  365.         next unless /^=\b/;
  366.         if (/^=head1\s+NAME\s*$/) {  # an /m would forgive mistakes
  367.         $_ = <F>;
  368.         unless (/\s*-+\s+/) {
  369.             $oops++;
  370.             warn "$0: Improper man page - no dash in NAME header in paragraph $. of $ARGV[0]\n"
  371.         }
  372.         %namedesc = split /\s+-\s+/;
  373.         last FCHECK;
  374.         }
  375.         next if /^=cut\b/;    # DB_File and Net::Ping have =cut before NAME
  376.         die "$0: Invalid man page - 1st pod line is not NAME in $ARGV[0]\n";
  377.     }
  378.     die "$0: Invalid man page - no documentation in $ARGV[0]\n";
  379.     }
  380.     close F;
  381. }
  382.  
  383. print <<"END";
  384. .rn '' }`
  385. ''' \$RCSfile\$\$Revision\$\$Date\$
  386. '''
  387. ''' \$Log\$
  388. '''
  389. .de Sh
  390. .br
  391. .if t .Sp
  392. .ne 5
  393. .PP
  394. \\fB\\\\\$1\\fR
  395. .PP
  396. ..
  397. .de Sp
  398. .if t .sp .5v
  399. .if n .sp
  400. ..
  401. .de Ip
  402. .br
  403. .ie \\\\n(.\$>=3 .ne \\\\\$3
  404. .el .ne 3
  405. .IP "\\\\\$1" \\\\\$2
  406. ..
  407. .de Vb
  408. .ft $CFont
  409. .nf
  410. .ne \\\\\$1
  411. ..
  412. .de Ve
  413. .ft R
  414.  
  415. .fi
  416. ..
  417. '''
  418. '''
  419. '''     Set up \\*(-- to give an unbreakable dash;
  420. '''     string Tr holds user defined translation string.
  421. '''     Bell System Logo is used as a dummy character.
  422. '''
  423. .tr \\(*W-|\\(bv\\*(Tr
  424. .ie n \\{\\
  425. .ds -- \\(*W-
  426. .ds PI pi
  427. .if (\\n(.H=4u)&(1m=24u) .ds -- \\(*W\\h'-12u'\\(*W\\h'-12u'-\\" diablo 10 pitch
  428. .if (\\n(.H=4u)&(1m=20u) .ds -- \\(*W\\h'-12u'\\(*W\\h'-8u'-\\" diablo 12 pitch
  429. .ds L" ""
  430. .ds R" ""
  431. .ds L' '
  432. .ds R' '
  433. 'br\\}
  434. .el\\{\\
  435. .ds -- \\(em\\|
  436. .tr \\*(Tr
  437. .ds L" ``
  438. .ds R" ''
  439. .ds L' `
  440. .ds R' '
  441. .ds PI \\(*p
  442. 'br\\}
  443. END
  444.  
  445. print <<'END';
  446. .\"    If the F register is turned on, we'll generate
  447. .\"    index entries out stderr for the following things:
  448. .\"        TH    Title 
  449. .\"        SH    Header
  450. .\"        Sh    Subsection 
  451. .\"        Ip    Item
  452. .\"        X<>    Xref  (embedded
  453. .\"    Of course, you have to process the output yourself
  454. .\"    in some meaninful fashion.
  455. .if \nF \{
  456. .de IX
  457. .tm Index:\\$1\t\\n%\t"\\$2"
  458. ..
  459. .nr % 0
  460. .rr F
  461. .\}
  462. END
  463.  
  464. print <<"END";
  465. .TH $name $section "$RP" "$date" "$center"
  466. .IX Title "$name $section"
  467. .UC
  468. END
  469.  
  470. while (($name, $desc) = each %namedesc) {
  471.     for ($name, $desc) { s/^\s+//; s/\s+$//; }
  472.     print qq(.IX Name "$name - $desc"\n);
  473. }
  474.  
  475. print <<'END';
  476. .if n .hy 0
  477. .if n .na
  478. .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
  479. .de CQ          \" put $1 in typewriter font
  480. END
  481. print ".ft $CFont\n";
  482. print <<'END';
  483. 'if n "\c
  484. 'if t \\&\\$1\c
  485. 'if n \\&\\$1\c
  486. 'if n \&"
  487. \\&\\$2 \\$3 \\$4 \\$5 \\$6 \\$7
  488. '.ft R
  489. ..
  490. .\" @(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2
  491. .    \" AM - accent mark definitions
  492. .bd B 3
  493. .    \" fudge factors for nroff and troff
  494. .if n \{\
  495. .    ds #H 0
  496. .    ds #V .8m
  497. .    ds #F .3m
  498. .    ds #[ \f1
  499. .    ds #] \fP
  500. .\}
  501. .if t \{\
  502. .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
  503. .    ds #V .6m
  504. .    ds #F 0
  505. .    ds #[ \&
  506. .    ds #] \&
  507. .\}
  508. .    \" simple accents for nroff and troff
  509. .if n \{\
  510. .    ds ' \&
  511. .    ds ` \&
  512. .    ds ^ \&
  513. .    ds , \&
  514. .    ds ~ ~
  515. .    ds ? ?
  516. .    ds ! !
  517. .    ds /
  518. .    ds q
  519. .\}
  520. .if t \{\
  521. .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
  522. .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
  523. .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
  524. .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
  525. .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
  526. .    ds ? \s-2c\h'-\w'c'u*7/10'\u\h'\*(#H'\zi\d\s+2\h'\w'c'u*8/10'
  527. .    ds ! \s-2\(or\s+2\h'-\w'\(or'u'\v'-.8m'.\v'.8m'
  528. .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
  529. .    ds q o\h'-\w'o'u*8/10'\s-4\v'.4m'\z\(*i\v'-.4m'\s+4\h'\w'o'u*8/10'
  530. .\}
  531. .    \" troff and (daisy-wheel) nroff accents
  532. .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
  533. .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
  534. .ds v \\k:\h'-(\\n(.wu*9/10-\*(#H)'\v'-\*(#V'\*(#[\s-4v\s0\v'\*(#V'\h'|\\n:u'\*(#]
  535. .ds _ \\k:\h'-(\\n(.wu*9/10-\*(#H+(\*(#F*2/3))'\v'-.4m'\z\(hy\v'.4m'\h'|\\n:u'
  536. .ds . \\k:\h'-(\\n(.wu*8/10)'\v'\*(#V*4/10'\z.\v'-\*(#V*4/10'\h'|\\n:u'
  537. .ds 3 \*(#[\v'.2m'\s-2\&3\s0\v'-.2m'\*(#]
  538. .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
  539. .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
  540. .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
  541. .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
  542. .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
  543. .ds ae a\h'-(\w'a'u*4/10)'e
  544. .ds Ae A\h'-(\w'A'u*4/10)'E
  545. .ds oe o\h'-(\w'o'u*4/10)'e
  546. .ds Oe O\h'-(\w'O'u*4/10)'E
  547. .    \" corrections for vroff
  548. .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
  549. .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
  550. .    \" for low resolution devices (crt and lpr)
  551. .if \n(.H>23 .if \n(.V>19 \
  552. \{\
  553. .    ds : e
  554. .    ds 8 ss
  555. .    ds v \h'-1'\o'\(aa\(ga'
  556. .    ds _ \h'-1'^
  557. .    ds . \h'-1'.
  558. .    ds 3 3
  559. .    ds o a
  560. .    ds d- d\h'-1'\(ga
  561. .    ds D- D\h'-1'\(hy
  562. .    ds th \o'bp'
  563. .    ds Th \o'LP'
  564. .    ds ae ae
  565. .    ds Ae AE
  566. .    ds oe oe
  567. .    ds Oe OE
  568. .\}
  569. .rm #[ #] #H #V #F C
  570. END
  571.  
  572. $indent = 0;
  573.  
  574. while (<>) {
  575.     if ($cutting) {
  576.     next unless /^=/;
  577.     $cutting = 0;
  578.     }
  579.     chomp;
  580.  
  581.     # Translate verbatim paragraph
  582.  
  583.     if (/^\s/) {
  584.     @lines = split(/\n/);
  585.     for (@lines) {
  586.         1 while s
  587.         {^( [^\t]* ) \t ( \t* ) }
  588.         { $1 . ' ' x (8 - (length($1)%8) + 8 * (length($2))) }ex;
  589.         s/\\/\\e/g;
  590.         s/\A/\\&/s;
  591.     }
  592.     $lines = @lines;
  593.     makespace() unless $verbatim++;
  594.     print ".Vb $lines\n";
  595.     print join("\n", @lines), "\n";
  596.     print ".Ve\n";
  597.     $needspace = 0;
  598.     next;
  599.     }
  600.  
  601.     $verbatim = 0;
  602.  
  603.     # check for things that'll hosed our noremap scheme; affects $_
  604.     init_noremap();
  605.  
  606.     if (!/^=item/) {
  607.  
  608.     # trofficate backslashes; must do it before what happens below
  609.     s/\\/noremap('\\e')/ge;
  610.  
  611.     # first hide the escapes in case we need to
  612.     # intuit something and get it wrong due to fmting
  613.  
  614.     s/([A-Z]<[^<>]*>)/noremap($1)/ge;
  615.  
  616.     # func() is a reference to a perl function
  617.     s{
  618.         \b
  619.         (
  620.         [:\w]+ \(\)
  621.         )
  622.     } {I<$1>}gx;
  623.  
  624.     # func(n) is a reference to a man page
  625.     s{
  626.         (\w+)
  627.         (
  628.         \(
  629.             [^\s,\051]+
  630.         \)
  631.         )
  632.     } {I<$1>\\|$2}gx;
  633.  
  634.     # convert simple variable references
  635.     s/(\s+)([\$\@%][\w:]+)/${1}C<$2>/g;
  636.  
  637.     if (m{ (
  638.             [\-\w]+
  639.             \(
  640.             [^\051]*?
  641.             [\@\$,]
  642.             [^\051]*?
  643.             \)
  644.         )
  645.         }x && $` !~ /([LCI]<[^<>]*|-)$/ && !/^=\w/)
  646.     {
  647.         warn "$0: bad option in paragraph $. of $ARGV: ``$1'' should be [LCI]<$1>\n";
  648.         $oops++;
  649.     }
  650.  
  651.     while (/(-[a-zA-Z])\b/g && $` !~ /[\w\-]$/) {
  652.         warn "$0: bad option in paragraph $. of $ARGV: ``$1'' should be [CB]<$1>\n";
  653.         $oops++;
  654.     }
  655.  
  656.     # put it back so we get the <> processed again;
  657.     clear_noremap(0); # 0 means leave the E's
  658.  
  659.     } else {
  660.     # trofficate backslashes
  661.     s/\\/noremap('\\e')/ge;
  662.  
  663.     }
  664.  
  665.     # need to hide E<> first; they're processed in clear_noremap
  666.     s/(E<[^<>]+>)/noremap($1)/ge;
  667.  
  668.  
  669.     $maxnest = 10;
  670.     while ($maxnest-- && /[A-Z]</) {
  671.  
  672.     # can't do C font here
  673.     s/([BI])<([^<>]*)>/font($1) . $2 . font('R')/eg;
  674.  
  675.     # files and filelike refs in italics
  676.     s/F<([^<>]*)>/I<$1>/g;
  677.  
  678.     # no break -- usually we want C<> for this
  679.     s/S<([^<>]*)>/nobreak($1)/eg;
  680.  
  681.     # LREF: a manpage(3f)
  682.     s:L<([a-zA-Z][^\s\/]+)(\([^\)]+\))?>:the I<$1>$2 manpage:g;
  683.  
  684.     # LREF: an =item on another manpage
  685.     s{
  686.         L<
  687.         ([^/]+)
  688.         /
  689.         (
  690.             [:\w]+
  691.             (\(\))?
  692.         )
  693.         >
  694.     } {the C<$2> entry in the I<$1> manpage}gx;
  695.  
  696.     # LREF: an =item on this manpage
  697.     s{
  698.        ((?:
  699.         L<
  700.         /
  701.         (
  702.             [:\w]+
  703.             (\(\))?
  704.         )
  705.         >
  706.         (,?\s+(and\s+)?)?
  707.       )+)
  708.     } { internal_lrefs($1) }gex;
  709.  
  710.     # LREF: a =head2 (head1?), maybe on a manpage, maybe right here
  711.     # the "func" can disambiguate
  712.     s{
  713.         L<
  714.         (?:
  715.             ([a-zA-Z]\S+?) /
  716.         )?
  717.         "?(.*?)"?
  718.         >
  719.     }{
  720.         do {
  721.         $1     # if no $1, assume it means on this page.
  722.             ?  "the section on I<$2> in the I<$1> manpage"
  723.             :  "the section on I<$2>"
  724.         }
  725.     }gex;
  726.  
  727.     s/Z<>/\\&/g;
  728.  
  729.     # comes last because not subject to reprocessing
  730.     s/C<([^<>]*)>/noremap("${CFont_embed}${1}\\fR")/eg;
  731.     }
  732.  
  733.     if (s/^=//) {
  734.     $needspace = 0;        # Assume this.
  735.  
  736.     s/\n/ /g;
  737.  
  738.     ($Cmd, $_) = split(' ', $_, 2);
  739.  
  740.     if (defined $_) {
  741.         &escapes;
  742.         s/"/""/g;
  743.     }
  744.  
  745.     clear_noremap(1);
  746.  
  747.     if ($Cmd eq 'cut') {
  748.         $cutting = 1;
  749.     }
  750.     elsif ($Cmd eq 'head1') {
  751.         s/\s+$//;
  752.         delete $wanna_see{$_} if exists $wanna_see{$_};
  753.         print qq{.SH "$_"\n};
  754.         print qq{.IX Header "$_"\n};
  755.     }
  756.     elsif ($Cmd eq 'head2') {
  757.         print qq{.Sh "$_"\n};
  758.         print qq{.IX Subsection "$_"\n};
  759.     }
  760.     elsif ($Cmd eq 'over') {
  761.         push(@indent,$indent);
  762.         $indent += ($_ + 0) || 5;
  763.     }
  764.     elsif ($Cmd eq 'back') {
  765.         $indent = pop(@indent);
  766.         warn "$0: Unmatched =back in paragraph $. of $ARGV\n" unless defined $indent;
  767.         $needspace = 1;
  768.     }
  769.     elsif ($Cmd eq 'item') {
  770.         s/^\*( |$)/\\(bu$1/g;
  771.         print STDOUT qq{.Ip "$_" $indent\n};
  772.         print qq{.IX Item "$_"\n};
  773.     }
  774.     elsif ($Cmd eq 'pod') {
  775.         # this is just a comment
  776.     } 
  777.     else {
  778.         warn "$0: Unrecognized pod directive in paragraph $. of $ARGV: $Cmd\n";
  779.     }
  780.     }
  781.     else {
  782.     if ($needspace) {
  783.         &makespace;
  784.     }
  785.     &escapes;
  786.     clear_noremap(1);
  787.     print $_, "\n";
  788.     $needspace = 1;
  789.     }
  790. }
  791.  
  792. print <<"END";
  793.  
  794. .rn }` ''
  795. END
  796.  
  797. if (%wanna_see) {
  798.     @missing = keys %wanna_see;
  799.     warn "$0: $Filename is missing required section"
  800.     .  (@missing > 1 && "s")
  801.     .  ": @missing\n";
  802.     $oops++;
  803. }
  804.  
  805. exit;
  806. #exit ($oops != 0);
  807.  
  808. #########################################################################
  809.  
  810. sub nobreak {
  811.     my $string = shift;
  812.     $string =~ s/ /\\ /g;
  813.     $string;
  814. }
  815.  
  816. sub escapes {
  817.  
  818.     s/X<(.*?)>/mkindex($1)/ge;
  819.  
  820.     # translate the minus in foo-bar into foo\-bar for roff
  821.     s/([^0-9a-z-])-([^-])/$1\\-$2/g;
  822.  
  823.     # make -- into the string version \*(-- (defined above)
  824.     s/\b--\b/\\*(--/g;
  825.     s/"--([^"])/"\\*(--$1/g;  # should be a better way
  826.     s/([^"])--"/$1\\*(--"/g;
  827.  
  828.     # fix up quotes; this is somewhat tricky
  829.     if (!/""/) {
  830.     s/(^|\s)(['"])/noremap("$1\\*(L$2")/ge;
  831.     s/(['"])($|[\-\s,;\\!?.])/noremap("\\*(R$1$2")/ge;
  832.     }
  833.  
  834.     #s/(?!")(?:.)--(?!")(?:.)/\\*(--/g;
  835.     #s/(?:(?!")(?:.)--(?:"))|(?:(?:")--(?!")(?:.))/\\*(--/g;
  836.  
  837.  
  838.     # make sure that func() keeps a bit a space tween the parens
  839.     ### s/\b\(\)/\\|()/g;
  840.     ### s/\b\(\)/(\\|)/g;
  841.  
  842.     # make C++ into \*C+, which is a squinched version (defined above)
  843.     s/\bC\+\+/\\*(C+/g;
  844.  
  845.     # make double underbars have a little tiny space between them
  846.     s/__/_\\|_/g;
  847.  
  848.     # PI goes to \*(PI (defined above)
  849.     s/\bPI\b/noremap('\\*(PI')/ge;
  850.  
  851.     # make all caps a teeny bit smaller, but don't muck with embedded code literals
  852.     my $hidCFont = font('C');
  853.     if ($Cmd !~ /^head1/) { # SH already makes smaller
  854.     # /g isn't enough; 1 while or we'll be off
  855.  
  856. #    1 while s{
  857. #        (?!$hidCFont)(..|^.|^)
  858. #        \b
  859. #        (
  860. #        [A-Z][\/A-Z+:\-\d_$.]+
  861. #        )
  862. #        (s?)         
  863. #        \b
  864. #    } {$1\\s-1$2\\s0}gmox;
  865.  
  866.     1 while s{
  867.         (?!$hidCFont)(..|^.|^)
  868.         (
  869.         \b[A-Z]{2,}[\/A-Z+:\-\d_\$]*\b
  870.         )
  871.     } {
  872.         $1 . noremap( '\\s-1' .  $2 . '\\s0' )
  873.     }egmox;
  874.  
  875.     }
  876. }
  877.  
  878. # make troff just be normal, but make small nroff get quoted
  879. # decided to just put the quotes in the text; sigh;
  880. sub ccvt {
  881.      local($_,$prev) = @_;
  882.      if ( /^\W+$/ && !/^\$./ ) {
  883.      ($prev && "\n") . noremap(qq{.CQ $_ \n\\&});
  884.      # what about $" ?
  885.      } else {
  886.      noremap(qq{${CFont_embed}$_\\fR});
  887.      }
  888.     noremap(qq{.CQ "$_" \n\\&});
  889. }
  890.  
  891. sub makespace {
  892.     if ($indent) {
  893.     print ".Sp\n";
  894.     }
  895.     else {
  896.     print ".PP\n";
  897.     }
  898. }
  899.  
  900. sub mkindex {
  901.     my ($entry) = @_;
  902.     my @entries = split m:\s*/\s*:, $entry;
  903.     print ".IX Xref ";
  904.     for $entry (@entries) {
  905.     print qq("$entry" );
  906.     }
  907.     print "\n";
  908.     return '';
  909. }
  910.  
  911. sub font {
  912.     local($font) = shift;
  913.     return '\\f' . noremap($font);
  914. }
  915.  
  916. sub noremap {
  917.     local($thing_to_hide) = shift;
  918.     $thing_to_hide =~ tr/\000-\177/\200-\377/;
  919.     return $thing_to_hide;
  920. }
  921.  
  922. sub init_noremap {
  923.     if ( /[\200-\377]/ ) {
  924.     warn "$0: high bit char in input stream in paragraph $. of $ARGV\n";
  925.     }
  926. }
  927.  
  928. sub clear_noremap {
  929.     my $ready_to_print = $_[0];
  930.  
  931.     tr/\200-\377/\000-\177/;
  932.  
  933.     # trofficate backslashes
  934.     # s/(?!\\e)(?:..|^.|^)\\/\\e/g;
  935.  
  936.     # now for the E<>s, which have been hidden until now
  937.     # otherwise the interative \w<> processing would have
  938.     # been hosed by the E<gt>
  939.     s {
  940.         E<    
  941.         ( [A-Za-z]+ )    
  942.         >    
  943.     } {
  944.      do {    
  945.          exists $HTML_Escapes{$1}
  946.         ? do { $HTML_Escapes{$1} }
  947.         : do {
  948.             warn "$0: Unknown escape in paragraph $. of $ARGV: ``$&''\n";
  949.             "E<$1>";
  950.         }
  951.      }
  952.     }egx if $ready_to_print;
  953. }
  954.  
  955. sub internal_lrefs {
  956.     local($_) = shift;
  957.  
  958.     s{L</([^>]+)>}{$1}g;
  959.     my(@items) = split( /(?:,?\s+(?:and\s+)?)/ );
  960.     my $retstr = "the ";
  961.     my $i;
  962.     for ($i = 0; $i <= $#items; $i++) {
  963.     $retstr .= "C<$items[$i]>";
  964.     $retstr .= ", " if @items > 2 && $i != $#items;
  965.     $retstr .= " and " if $i+2 == @items;
  966.     }
  967.  
  968.     $retstr .= " entr" . ( @items > 1  ? "ies" : "y" )
  969.         .  " elsewhere in this document";
  970.  
  971.     return $retstr;
  972.  
  973. }
  974.  
  975. BEGIN {
  976. %HTML_Escapes = (
  977.     'amp'    =>    '&',    #   ampersand
  978.     'lt'    =>    '<',    #   left chevron, less-than
  979.     'gt'    =>    '>',    #   right chevron, greater-than
  980.     'quot'    =>    '"',    #   double quote
  981.  
  982.     "Aacute"    =>    "A\\*'",    #   capital A, acute accent
  983.     "aacute"    =>    "a\\*'",    #   small a, acute accent
  984.     "Acirc"    =>    "A\\*^",    #   capital A, circumflex accent
  985.     "acirc"    =>    "a\\*^",    #   small a, circumflex accent
  986.     "AElig"    =>    '\*(AE',    #   capital AE diphthong (ligature)
  987.     "aelig"    =>    '\*(ae',    #   small ae diphthong (ligature)
  988.     "Agrave"    =>    "A\\*`",    #   capital A, grave accent
  989.     "agrave"    =>    "A\\*`",    #   small a, grave accent
  990.     "Aring"    =>    'A\\*o',    #   capital A, ring
  991.     "aring"    =>    'a\\*o',    #   small a, ring
  992.     "Atilde"    =>    'A\\*~',    #   capital A, tilde
  993.     "atilde"    =>    'a\\*~',    #   small a, tilde
  994.     "Auml"    =>    'A\\*:',    #   capital A, dieresis or umlaut mark
  995.     "auml"    =>    'a\\*:',    #   small a, dieresis or umlaut mark
  996.     "Ccedil"    =>    'C\\*,',    #   capital C, cedilla
  997.     "ccedil"    =>    'c\\*,',    #   small c, cedilla
  998.     "Eacute"    =>    "E\\*'",    #   capital E, acute accent
  999.     "eacute"    =>    "e\\*'",    #   small e, acute accent
  1000.     "Ecirc"    =>    "E\\*^",    #   capital E, circumflex accent
  1001.     "ecirc"    =>    "e\\*^",    #   small e, circumflex accent
  1002.     "Egrave"    =>    "E\\*`",    #   capital E, grave accent
  1003.     "egrave"    =>    "e\\*`",    #   small e, grave accent
  1004.     "ETH"    =>    '\\*(D-',    #   capital Eth, Icelandic
  1005.     "eth"    =>    '\\*(d-',    #   small eth, Icelandic
  1006.     "Euml"    =>    "E\\*:",    #   capital E, dieresis or umlaut mark
  1007.     "euml"    =>    "e\\*:",    #   small e, dieresis or umlaut mark
  1008.     "Iacute"    =>    "I\\*'",    #   capital I, acute accent
  1009.     "iacute"    =>    "i\\*'",    #   small i, acute accent
  1010.     "Icirc"    =>    "I\\*^",    #   capital I, circumflex accent
  1011.     "icirc"    =>    "i\\*^",    #   small i, circumflex accent
  1012.     "Igrave"    =>    "I\\*`",    #   capital I, grave accent
  1013.     "igrave"    =>    "i\\*`",    #   small i, grave accent
  1014.     "Iuml"    =>    "I\\*:",    #   capital I, dieresis or umlaut mark
  1015.     "iuml"    =>    "i\\*:",    #   small i, dieresis or umlaut mark
  1016.     "Ntilde"    =>    'N\*~',        #   capital N, tilde
  1017.     "ntilde"    =>    'n\*~',        #   small n, tilde
  1018.     "Oacute"    =>    "O\\*'",    #   capital O, acute accent
  1019.     "oacute"    =>    "o\\*'",    #   small o, acute accent
  1020.     "Ocirc"    =>    "O\\*^",    #   capital O, circumflex accent
  1021.     "ocirc"    =>    "o\\*^",    #   small o, circumflex accent
  1022.     "Ograve"    =>    "O\\*`",    #   capital O, grave accent
  1023.     "ograve"    =>    "o\\*`",    #   small o, grave accent
  1024.     "Oslash"    =>    "O\\*/",    #   capital O, slash
  1025.     "oslash"    =>    "o\\*/",    #   small o, slash
  1026.     "Otilde"    =>    "O\\*~",    #   capital O, tilde
  1027.     "otilde"    =>    "o\\*~",    #   small o, tilde
  1028.     "Ouml"    =>    "O\\*:",    #   capital O, dieresis or umlaut mark
  1029.     "ouml"    =>    "o\\*:",    #   small o, dieresis or umlaut mark
  1030.     "szlig"    =>    '\*8',        #   small sharp s, German (sz ligature)
  1031.     "THORN"    =>    '\\*(Th',    #   capital THORN, Icelandic
  1032.     "thorn"    =>    '\\*(th',,    #   small thorn, Icelandic
  1033.     "Uacute"    =>    "U\\*'",    #   capital U, acute accent
  1034.     "uacute"    =>    "u\\*'",    #   small u, acute accent
  1035.     "Ucirc"    =>    "U\\*^",    #   capital U, circumflex accent
  1036.     "ucirc"    =>    "u\\*^",    #   small u, circumflex accent
  1037.     "Ugrave"    =>    "U\\*`",    #   capital U, grave accent
  1038.     "ugrave"    =>    "u\\*`",    #   small u, grave accent
  1039.     "Uuml"    =>    "U\\*:",    #   capital U, dieresis or umlaut mark
  1040.     "uuml"    =>    "u\\*:",    #   small u, dieresis or umlaut mark
  1041.     "Yacute"    =>    "Y\\*'",    #   capital Y, acute accent
  1042.     "yacute"    =>    "y\\*'",    #   small y, acute accent
  1043.     "yuml"    =>    "y\\*:",    #   small y, dieresis or umlaut mark
  1044. );
  1045. }
  1046.  
  1047.