#! /usr/bin/perl
# (Works with both perl 4 and perl 5)
$version = 'July 8 1997';
$copyright = <<EOT;
texi2sgml - converts texinfo to Linuxdoc SGML

Copyright (C) 1908 Claus Heine

Derived from texi2www, copyright (C) 1994, 1995, 1996, 1997 Tim Singletary

This program is freely distributable under the terms of the GNU 
GENERAL PUBLIC LICENSE.  In particular, modified versions of this 
program must retain this copyright notice and must remain freely
distributable.

EOT

$usage = <<EOT;
Usage: texi2www [option ...] texinfo_file
where options are:
  -dir directory    -- Specify output directory.  Default is \`.\'. 
  -file file        -- Specify output file. Default is texinfo_file
                       without ".texi" extension with the ".sgml"
                       appended -verbose -- Verbose output.
  -info             -- specify an "info to html" gateway.

                       If the argument is ommitted, then the resulting
                       HTML scripts translate "\@inforef" references to
                       HTML url of info pages already translated with
                       THIS texi2www scritp which might not be what
                       you want. A good example -- in case you have
                       the "info2html" cgi script -- is 
                       "-info http://localhost/cgi-bin/info2html".

                       As you can deduce from this example, it does
                       not suffice to specify the "info 2 html"
                       server, but you have to specify the entire
                       url. This script appends the string
                       "?(\$infofile)\$node" to the argument of this
                       switch, which is just what the "info2html" cgi
                       script expects.

                       Default is "http://localhost/cgi-bin/info2html".

The complete user\'s guide is available at
http://sunland.gsfc.nasa.gov/info/texi2www/Top.html
EOT

######################################################################## 

#$info2htmlserver = "http://localhost/cgi-bin/info2html";

$info2htmlserver = "http://localhost/cgi-bin/info2html";

$dir = "."; 
while ($ARGV[0] =~ /^-/) { 
    $_ = shift; 
    if (/-dir/) {$_ = shift; s!/$!!; s!$!/!; $dir = $_; next;} 
    if (/-file/) {$_ = shift; $sgmlfile = $_; next;} 
    if (/-verbose/) {$verbose = 1; next;} 
    die $usage;
}

&initialize_tables();

#
# Read the texinfo input into @texinfo
#
&open_input_file($ARGV[0]);
&read_input(1,'/^\@bye/',"$texinfo_file[0] line 1");
$texinfo[$ntexinfo] = "\@bye\n";
$origin[$ntexinfo] = "$texinfo_file[0] line $.";

if (!$sgmlfile) {
  $sgmlfile = @_[split('/',$ARGV[0])-1];
  $sgmlfile =~ s/\.texi$//;
  $sgmlfile = ">$dir/$sgmlfile" . ".sgml";
} else {
  $sgmlfile = ">$sgmlfile";
}

open(SGML,$sgmlfile) || die "Couldn't open $sgmlfile -- $!\n";

#
# Parse @texinfo
#
$raise_sections = 0;
$texinfo_index = 0;
while ($texinfo_index < @texinfo) {
    &get_more_stuff_to_parse();
    if (/^\@bye/) {
	&printSGML("</article>\n");
        &terminate_node();
        print "Normal completion\n";
        exit;
    }
    &parse();
}

print "Huh? didn\'t parse the \@bye directive!\n";

########################################################################
sub canonical # (node_name)
{
    local ($n) = @_;

    $n =~ s/^\s+//; $n =~ s/\s+$//; # strip whitespace

    return "../" if ($n =~ /\(dir\)/i); # handle (dir)
    
    if ($n =~ /^\(([^\)]+)\)(.*)/) {
	$p = $1; $p =~ s/^\s+//; $p =~ s/\s+$//; $p .= "/";
	$n = $2; $n =~ s/^\s+//; $n =~ s/\s+$//;
    } else {
	$p = "";
    }


    $n =~ s/\$/\$\$/;		# `$' -> `$$'
    $n =~ s/_/\$_/g;		# `_' -> `$_'
    $n =~ s/\s+/_/g;		# whitespace -> `_'

				# anything else that's funky get
				# translated to `$xx' where `xx'
				# are hex digits.
    while ($n =~ /(.*)([^-a-zA-Z0-9\$_.])(.*)/) {
	$n = $1 . sprintf("\$%02x",ord($2)) . $3;
    }

    return "$p$n.sgml" if ($n);
    return "";
} # canonical

########################################################################
sub deduce_node_links
#
# On entry, $_ is a node line and $start_index is the index (in @texinfo)
# of the node line.
#
# &deduce_node_links() sets $next, $prev, and $up.
{
    local ($level,$i,$node,$j);

    $node = $texinfo[$start_index];
    chop $node;
    $node =~ s/\@node\s+//;
    $node =~ s/\s*,.*//;

    # First, search down from this node to the next sectioning command.
    $level = &determine_node_level($start_index+1);

    # Next, look for the `next' node (i.e., the next node at the
    # same or a higher level).
    undef($next);
    for ($i=$start_index+1; $i < $ntexinfo; ++$i) {
	$_ = $texinfo[$i];
	next unless /^\@node +([^,]+).*\n/;
	$j = &determine_node_level($i+1);
	if ($j <= $level) {
	    if ($j == $level) {$next = $1;}
	    last;
	} 
    }

    # Look for the `prev' and `up' nodes
    undef($prev);
    undef($up);
    for ($i=$start_index-1; $i > 1; --$i) {
	$_ = $texinfo[$i];
	next unless /^\@node\s+([^,]+).*\n/;
	$j = &determine_node_level($i+1);
	if ($j == $level) {
	    unless ($prev) {$prev = $1;}
	} elsif ($j < $level) {
	    $up = $1;
	    last;
	}
    }

    unless (defined($up)) {$up = $node eq "Top" ? "(dir)" : "Top";}

    $xthis = $this;
    $xthis =~ s/\n//;

} # deduce_node_links

########################################################################
sub determine_node_level
{
    local ($i) = @_;
    local ($level);

    $level = 0;
    while ($i < $ntexinfo) {
	$_ = $texinfo[$i];
	++$i;
	next if /^\s+$/;
	last if (/\@node/);
	last unless (/\@(\w+)/);
	if ($directive_section{$1}) {
	    $level = $directive_section{&raised_section_name($1)};
	    last;
	}
    }

    return $level;
} # determine_node_level


########################################################################
sub expand_xref
{
    local ($cmd,$arg) = @_;
    local ($ref,$node,$xrefname,$topic,$infofile,$manual,$url,$x,$urlprefix);


    if ($cmd eq 'url') {
        $url = $arg;
	$xrefname = $arg;
    } elsif ($cmd eq 'email') {
        ($url,$name) = split(/,/,$arg);
	$name =~ s/^\s+//;
	$name =~ s/\s+$//;
	$name =~ s/\s+/ /;
	if ($name =~ /^$/) {
	    $xrefname = $url;
	} else {
	    $xrefname = "$name ($url)";
	}
        $url = "mailto:" . $url;
	$urlprefix = "html"
    } elsif ($cmd eq 'uref') {
        ($url,$name) = split(/,/,$arg);
	$node = $url;
	$xrefname = $name;
#	$urlprefix = "html"
    } elsif ($cmd eq 'inforef') {
	($node,$xrefname,$infofile) = split(/,/,$arg);
	$topic = $manual = '';
	$node =~ s/^\s+//;
	$infofile =~ s/^\s+//;
	if ($info2htmlserver) {
	    $node =~ s/\s/\&percnt;20/g;
	    $node =~ s/\+/\&percnt;AB/g;
	    $url = $info2htmlserver . "?($infofile)$node";
	}
	$urlprefix = "html"
    } elsif ($cmd eq 'href') {
	($xrefname,$node,$infofile,$url) = split(/,/,$arg);
	$urlprefix = "html"
    } else {
	($node,$xrefname,$topic,$infofile,$manual) = split(/,/,$arg);
	$ref = $node;
    }
    $xrefname =~ s/^\s+//; $infofile =~ s/^\s+//;
    $xrefname =~ s/\s+$//; $infofile =~ s/\s+$//;
    $xrefname =~ s/\s+/ /; $infofile =~ s/\s+/ /;
    $infofile =~ s/\.texi$//;
    $infofile =~ s/\.texinfo$//;

    if ($xrefname =~ /^$/) {
	$xrefname = $node;
    }

    if ($info2htmlserver && $infofile) {
	$xrefname .= " (info file $infofile)";
    }

    if ($ref) {
      $x = "<ref id=\"$ref\" name=\"$xrefname\">";
    } else {
      $node = &canonical($node);
      unless ($url) {
	unless ($infofile =~ /^$/) {$url = "../$infofile/";}
	$url = $url . $node;
      }
      $xrefname =~ s/<[^\/>]+\/([^\/]*)\//$1/g;
      $xrefname =~ s/<[^\/>]+>([^\/]*)<\/[^\/>]+>/$1/g;
      if ($url eq $xrefname) {
	  $urlprefix = "html";
      }
      $x = "<$urlprefix" . "url url=\"$url\" name=\"$xrefname\">";
  }
} # expand_xref

########################################################################
sub get_more_stuff_to_parse
{
    $start_index = $texinfo_index;

    $_ = '';
    do {
	if ($texinfo_index >= @texinfo) {
	    print "Unclosed \@x{y} in chunk beginning at "
		. "$origin[$start_index]\n";
	    return;
	}
        s/\n$/ /;
	$more = $texinfo[$texinfo_index++];
	$more =~ s/\@\*/<newline>\n/g;
	$more =~ s/\@\./\./g;
	$more =~ s/\@\://g;
	$more =~ s/\@refill//g;
	$more =~ s/\@,\{c\}/&ccedil;/g;
	$more =~ s/\@,\{C\}/&Ccedil;/g;
        $more =~ s/\@\`([AaEeIiOoUuYy])/\&$1acute;/g;
        $more =~ s/\@\'([AaEeIiOoUu])/\&$1grave;/g;
        $more =~ s/\@\^([AaEeIiOoUu])/\&$1circ;/g;
        $more =~ s/\@\~([AaNnOo])/\&$1tilde;/g;
	$more =~ s/\@[\`\'^~=](.)/$1/g;
	$more =~ s/\@\&quot;([AaIiOoUu])/\&$1uml;/g;
	$more =~ s/\@\&quot;(.)/$1/g;
	$more =~ s/%/\&percnt;/g;
	
	if ($more =~ /^\@(\w+)/) {
	    if ($1 eq 'set') {
		if ($more =~ /^\@set\s+(\S+)\s+(.+)$/) {
		    $texinfo_variable{$1} = $2;
		} else {
		    $more =~ /^\@set\s+(\S+)/;
		    $texinfo_variable{$1} = 1;
		}
		next;
	    }
	}

	$_ .= $more; 

	# Expand all @a{b} in line 
	while (/\@(\w+)\{([^{}]*)\}/) { 
            $atcmd = $1;
	    $atarg = $2;

            if ($z = $atxy_2_zyz{$atcmd}) {
		if ($z =~ /(.+),(.+),(.+)/) {
		    $left = $1; $z = $2; $right = $3;
		} else {
		    $left = ''; $right = '';
		}
		if ($z =~ s/^\^//) {$atarg =~ tr/a-z/A-Z/;}
		$x = ($z eq "") ? $atarg :  "$left<$z>$atarg</$z>$right";
            } elsif ($atxy_2_y{$atcmd}) {
                $x = $atarg;
            } elsif ($z = $atx_2_z{$atcmd}) {
                $x = $z;
            } elsif ($z = $atxy_2_z{$atcmd . ' ' . $atarg}) {
                $x = $z;
            } elsif ($z = $atxy_2_ref{$atcmd}) {
                $x = $z . &expand_xref($atcmd,$atarg);
                $x =~ s/^X//; # works because $z must start with 'X'!
            } elsif ($atcmd eq 'value') {
                $x = $texinfo_variable{$atarg};
            } elsif ($atcmd eq 'today') {
                $x = &today();
            } elsif ($atcmd eq 'footnote') {
 		$footnote[$nfootnotes++] = $atarg;
		$x = "<footnote>\n$atarg\n</footnote>\n";
#                 $x =
# 		  " <label id=\"$this" . "etontoof$nfootnotes\">" .
# 		    "<ref id=\"$this" . "footnote$nfootnotes\" " .
# 		      "name=\"&lsqb;$nfootnotes&rsqb\">";
	   } elsif ($atcmd eq 'gif') {
		$x = "\n";
           } else {
                print "**WARNING** Don't know how to expand "
                    . "\@$atcmd\{$atarg\}\n";
		$debug = 1;
                $x = "?$atcmd\?$atarg\?";
            }
            
	    print "$origin[$start_index]: \@$atcmd\{$atarg\} => $x\n"
		                                   if $debug{expansions};

            s/\@\w+\{[^{}]*\}/$x/;	    
        }
    } while (/\@\w+\{[^}]*$/);
    print "$origin[$start_index]: $_" if $debug{chunks};
} # get_more_stuff_to_parse

########################################################################
sub parse
# On entry:
#    $_ -- the line(s) to parse.
#    $start_index -- where, in $texinfo, $_ begins.
{
    local ($x);

    if (/^\@(\w+)/) {
	if ($x=$directive_block{$1}) { # @example, @quotation, etc.
	    &parse_block($1,$x);
	} elsif ($directive_section{$1}) { # @chapter, @subsection, etc.
	    &process_section();
	} elsif ($1 eq 'bye') {
# 	    if ($nfootnotes > 0) {
# 		&printSGML("<P>\n");
# 		&printSGML("<tscreen>\n");
# 		for ($n=0; $n < $nfootnotes; ++$n) {
# 		    &printSGML("<p>\n");
# 		    &printSGML("<label id=\"$this" .
# 			       "footnote" . ($n+1) . "\">\n");
# 		    &printSGML("<ref id=\"$this" . 
# 			       "etontoof" . ($n+1) . "\" " .
# 			       "name=\"&lsqb;" . ($n+1) . "&rsqb;\">\n");
# 		    &printSGML("$footnote[$n]\n");
# 		}
# 		&printSGML("</tscreen>\n");
# 	    }
	    close (SGML);
	    return;
	} elsif ($1 eq 'center') {
	    /^\@center\s+(.*)/;
	    &printSGML("$paragraph_end") if $in_paragraph;
	    &printSGML("<p>$1\n");
	    $in_paragraph = 0;
	} elsif ($1 eq 'clear') {
	    /^\@clear\s+(\S+)/;
	    undef($texinfo_variable{$1});
	} elsif ($1 =~ /^def(code)?index/) {
	    /^\@(def|defcode)index\s+(\w+)/;
	    $index_name{$2} = $2 . "index";
	    $index_style{$2} = 'tt' if ($1 eq "defcode");
	} elsif ($1 =~ /^(def.*)/) { # @defn, @defun, ... @deftp
	    &parse_def($1);
	} elsif ($1 eq 'enumerate') {
	    &parse_enumerate();
	} elsif ($1 eq 'exdent') {
	    /^\@exdent\s+(.*)/;
	    &printSGML("$paragraph_end") if $in_paragraph;
	    # A bug -- doesn't exdent the line!
	    &printSGML("<P>$1\n");
	    $in_paragraph = 0;
	} elsif ($1 eq 'flushleft' || $1 eq 'flushright') {
	    &parse_flush();
	} elsif ($1 eq 'html') {
	    while ($texinfo_index < @texinfo) {
		&get_more_stuff_to_parse();
		last if (/^\@end\s+sgml/);
		s/\&quot;/\"/g; s/\&gt;/\>/g; s/\&lt;/\</g; s/\&amp;/\&/g;
		&printSGML("$_");
	    }
	} elsif ($1 eq 'itemize') {
	    &parse_itemize();
	} elsif ($1 eq 'menu') {
	    &parse_menu();
	} elsif ($1 eq 'node') {
	    &terminate_node();
	    &process_node();
	} elsif ($1 eq 'printindex') {
	    /^\@printindex\s+([a-z]+)/;
	    &print_index($1);
	} elsif ($1 eq 'raisesections') {
	    ++$raise_sections;
	    print "** WARNING** $origin[$start_index]: \@raisesections > 3\n"
		if ($raise_sections > 3);
	} elsif ($1 eq 'lowersections') {
	    --$raise_sections;
	    print "** WARNING** $origin[$start_index]: \@lowersections < -3\n"
		if ($raise_sections < -3);
	} elsif ($1 eq 'settitle') {
	    /^\@settitle\s+(.*)/;
	    unless ($title) {$title = $1;}
	} elsif ($1 eq 'set') {
	    if (/^\@set\s+(\S+)\s+(.+)$/) {
		$texinfo_variable{$1} = $2;
	    } else {
		/^\@set\s+(\S+)/;
		$texinfo_variable{$1} = 1;
	    }
	} elsif ($1 eq 'syncodeindex') {
	    &process_synindex(1);
	} elsif ($1 eq 'synindex') {
	    &process_synindex(0);
	} elsif ($1 =~ /^.?table/) { # @table, @vtable, @ftable
	    $tbltype = '.';
	    unless (/^\@(.?table)\s*\@(\w*)/
		           && ($2 eq 'asis' || ($tbltype=$atxy_2_zyz{$2}))) {
		print "**WARNING** $origin[$start_index]: assuming "
		    . "\@table \@asis\n";
	    }
	    &parse_table($1,$tbltype);
	} elsif ($1 eq 'titlepage') {
	    &parse_titlepage();
	} elsif ($1 eq 'multitable') {
	    &parse_multitable($_);
	} elsif ($1 =~ /..?index/) { # @cindex, @findex, .. @auindex, etc.
	    &process_index();
	} else {
	    print "**WARNING** $origin[$start_index]: ignoring $_";
	}
    } elsif ($in_document) {
	if (/^\s*$/) {
	    if ($in_paragraph) {
		&printSGML("$paragraph_end");
	    } elsif ($in_preformatted) {
		&printSGML("\n");
	    }
	    $in_paragraph = 0;
	} else {
	    unless ($in_preformatted) {
		unless ($in_paragraph) {
		    &printSGML("<P>\n");
		    $in_paragraph = 1;
		    $paragraph_end = "\n";
		}
	    }
	    &printSGML("$_");
	}
    } elsif ($in_abstract) {
	&printSGML("$_") unless /^\s*$/;
    }
} # parse

########################################################################
sub parse_block
#
# Handles @example, @display, etc.
#
#    > @example            > <PRE>
#    > a + b = c     ==>   > a + b = c
#    > @end example        > </PRE>
{
    local ($block,$pre) = @_;
    local ($started_at,$blkcmd,$first,$modifier,$verbatim);
    local ($old_pref);

    $started_at = $start_index;

    &printSGML("$paragraph_end") if $in_paragraph;
    $in_paragraph = 0;
    
    &printSGML("<p>\n");
    $pre =~ /^([+-])([>]*)(.*)$/;
    if ($2) {
	&printSGML("<tscreen>\n");
    }
    $blkcmd = $3;

    if ($1 eq "-") {
	$verbatim = 1;
    }

    $first = 1;
    $old_pref = $in_preformatted;
    $in_preformatted = $block;

    while ($texinfo_index < @texinfo) {
	&get_more_stuff_to_parse();
	if ($first) {
	    $first = 0;
	    if (/^\@group/) {
		$modifier = "group";
		&get_more_stuff_to_parse();
	    } elsif (/^\@cartouche/) {
		$modifier = "cartouche";
		$blkcmd = "code";
		&get_more_stuff_to_parse();
	    }
	    &printSGML("<$blkcmd>\n");
	    $paragraph_end = "</$blkcmd>\n<p>\n<$blkcmd>\n" unless $modifier;
	}
	s/<\//\&etago;/g if ($verbatim);
	if ($modifier && /^\@end\s+$modifier/) {
	    next;
	} elsif (/^\@end\s+$block/) {
	    &printSGML("</$blkcmd>\n");
	    if ($pre =~ /^[+-]>(.*)$/) {
		&printSGML("</tscreen>\n");
	    }
	    $in_preformatted = $old_pref;
	    return;
	} elsif (/^\s*$/) {
	    &printSGML("</$blkcmd>\n<p>\n<$blkcmd>\n") unless $modifier;
	} else {
	  &parse();
	}
    }
    print "**ERROR** reached EOF while searching for end of the \@$block "
	. "block that started on $origin[$started_at]\n";
} # parse_block

########################################################################
sub parse_def
# $_ contains a @def* command
{
    local ($def) = @_;
    local ($started_at,$in_dd);

    $started_at = $start_index;
        
    &printSGML("$paragraph_end") if $in_paragraph;
    $in_paragraph = 0;
    
    &printSGML("<DL>\n");

    &printdef();

    while ($texinfo_index < @texinfo) {
	&get_more_stuff_to_parse();
	if (/^\@end\s+$def/) {
	    &printSGML("</DL>\n");
	    $in_paragraph = 0;
	    return;
	}
	if (s/^(\@def\w+)x\s/$1 /) {&printdef();}
	else {
	    unless ($in_dd) {
		&printSGML("<DD>\n");
		++$in_dd;
		$in_paragraph = 1;
		$paragraph_end = "\n";
	    }
	    &parse();
	}
    }
    print "**ERROR** reached EOF while searching for end of the $def "
	. "definition that started on $origin[$started_at]\n";

} # parse_def
sub printdef
{

    s/\@defun(x?)\s/\@deffn Function / 
	|| s/\@defmac(x?)\s/\@deffn Macro /
	|| s/\@defspec(x?)\s/\@deffn \{Special Form\} /
        || s/\@defvar(x?)\s/\@defvr Variable /
        || s/\@defopt(x?)\s/\@defvr \{User Option\} /
        || s/\@deftypefun(x?)\s/\@deftypefn Function /
        || s/\@deftypevar(x?)\s/\@deftypefn Variable /
	|| s/\@defivar(x?)\s/\@defcv \{Instance Variable\} /
	|| s/\@defmethod(x?)\s/\@defop Method /;
    s/(\@\w+)x\s/$1 /;

    @words = split;

    $i = 1;
    $category = $words[$i++];
    while ($i < @words && $category =~ /^\{[^}]*$/) {
        $category .= ' ' . $words[$i++];
    }
    if ($i>=@words) {
	print "def error at $origin{$started_at}\n";
    }
    $category =~ s/^\{//;
    $category =~ s/\}$//;

    &printSGML("<DT>$category: ");

    if ($words[0] eq '@deftypefn' || $words[0] eq '@deftypevr'
	|| $words[0] eq '@defcv' || $words[0] eq '@defop') {
	if ($words[$i] =~ s/^\{//) {
	    &printSGML("<VAR>");
	    until ($words[$i] =~ s/\}$//) {&printSGML("$words[$i++] ");}
	    &printSGML("$words[$i++]</VAR> ");
	} else {
	    &printSGML("<VAR>$words[$i++]</VAR> ");
	}
	$words[0] =~ /.*([a-z][a-z])/;
	$_ = "\@" . $1 . "index " . $words[$i];
	&process_index;
    }
    &printSGML("<STRONG>$words[$i++]</STRONG>\n<VAR>");

    while ($i < @words) {&printSGML(" $words[$i++]");}
    &printSGML("</VAR>\n");

} # printdef

########################################################################
sub parse_enumerate
# $_ is `@enumerate'.  Note that @enumerate with an arg (`@enumerate 3',
# for example) is kinda funky due to SGML limitations.
{
    local ($count,$started_at);
    
    $started_at = $start_index;

    &printSGML("$paragraph_end") if $in_paragraph;
    $in_paragraph = 0;

    &printSGML("<p>\n<enum>\n");
    while ($texinfo_index < @texinfo) {
	&get_more_stuff_to_parse();
	if (/^\@end\s+enumerate/) {
	    &printSGML("</enum>\n");
	    return;
	}
	if (/^\@item\s+(.*)/ || /^\@item()$/) {
	    if ($count) {
		&printSGML("<item> $1\n");
		++$count;
	    } else {
		&printSGML("<item> $1\n");
	    }
	    $in_paragraph = 1;
	    $paragraph_end = "\n";
	} else {
	    &parse();
	}
    }
    print "**ERROR** reached EOF while searching for end of the \@enumerate "
	. "that started on $origin[$started_at]\n";
} # parse_enumerate

########################################################################
sub parse_flush
{
    local ($started_at,$flush);

    /^\@(\w+)\s/;
    $flush = $1;
    $started_at = $start_index;

    &printSGML("$paragraph_end") if $in_paragraph;
    $in_paragraph = 0;

    while ($texinfo_index < @texinfo) {
	&get_more_stuff_to_parse();
	if (/^\@end\s+$flush/) {
	    return;
	}
	&parse();
    }
    print "**ERROR** reached EOF while searching for end of the $flush "
	. "that started on $origin[$started_at]\n";


} # parse_flush

########################################################################
sub parse_itemize
# $_ is `@itemize'.  Due to SGML limitation, `@itemize @bullet' comes
# out the same as `@itemize @minus'.
{
    local ($started_at);

    $started_at = $start_index;

    &printSGML("$paragraph_end") if $in_paragraph;
    $in_paragraph = 0;

    &printSGML("<p>\n<itemize>\n");
    

    while ($texinfo_index < @texinfo) {
	&get_more_stuff_to_parse();
	if (/^\@end\s+itemize/) {
	    &printSGML("</itemize>\n");
	    return;
	}
	if (/^\@item\s+(.*)/ || /^\@item()$/) {
	    &printSGML("<item>$1\n");
	    $in_paragraph = 1;
	    $paragraph_end = "\n";
	} else {
	    &parse();
	}
    }
    print "**ERROR** reached EOF while searching for end of the itemize "
	. "that started on $origin[$started_at]\n";
} # parse_itemize

########################################################################
sub parse_menu
{
    local ($started_at);

    $started_at = $start_index;

    &printSGML("$paragraph_end") if $in_paragraph;
    $in_paragraph = 0;

#    &printSGML("<MENU>\n");

    while ($texinfo_index < @texinfo) {
	&get_more_stuff_to_parse();
	if (/^\@end\s+menu/) {
#	    &printSGML("</MENU>\n");
	    return;
	}

        # Like ` * menu-item:: description of item'
	if (/^\s*\*\s*([^:]*)\s*::\s*(.*)$/) {
#	    &printSGML("$paragraph_end") if $in_paragraph;
	    $in_paragraph = 0;
	    $node = &canonical($1);	
#	    &printSGML("<LI><A HREF=\"$node\">$1</A>\n");
#	    &printSGML("$2\n") if $2;
        # Like ` * menu-item: cross-reference. description of item'
	} elsif (/^\s*\*\s*([^:]*)\s*:([^.]*)\.\s*(.*)$/) {
#	    &printSGML("$paragraph_end") if $in_paragraph;
	    $in_paragraph = 0;
	    $node = &canonical($2);
#	    &printSGML("<LI><A HREF=\"$node\">$1</A>\n");
#	    &printSGML("$3\n");
	} elsif (/^\@detailmenu/ || /^\@end\s+detailmenu/) {
	    # ignore `@detailmenu' and `@end detailmenu' statements
	} elsif (/^\@/) {
	    print "**WARNING** Don\'t know how to process \`$_\' inside "
		. "a menu!\n";
	} else {
	    if (/^\s*$/ && !$in_paragraph) {
#		&printSGML("<P>");
		$in_paragraph = "1";
	        $paragraph_end = "\n";
	    }
#	    &printSGML("$_");
	}
    }
    print "**ERROR** reached EOF while searching for end of the menu "
	. "that started on $origin[$started_at]\n";
} # parse_menu

########################################################################
sub parse_table
# $_ is `@table', `@vtable', etc.
#
# For example, convert
#    @table @code
#    @item  one
#    @itemx two
#    The first two numbers
#    @item  three
#    The third number
#    @end table
#
# into
#    <TABLE>
#    <TR><TD VALIGN=TOP><CODE>one</CODE><BR><CODE>two</CODE</TD>
#        <TD VALIGN=TOP>The first two numbers</TD></TR>
#    <TR><TD VALIGN=TOP><CODE>three</CODE></TD>
#        <TD VALIGN=TOP>The third number</TD></TR>
#    </TABLE>
{
    ($table,$ttype) = @_;
    local ($started_at,$in_keys,$in_data);

    $started_at = $start_index;

    &printSGML("$paragraph_end") if $in_paragraph;
    $in_paragraph = 0;

    &printSGML("<p>\n<descrip>\n");

    $in_keys = 0;
    while ($texinfo_index < @texinfo) {
	&get_more_stuff_to_parse();
	if (/^\@end\s+$table/) {
 	    &printSGML("<p>\n") if $in_keys;
	    &printSGML("</descrip>\n");
	    return;
	}
	if (/^\@item(x?)()$/ || /^\@item(x?)\s+(.*)/) {
	    $in_data = 0;
	    $item = $2;
	    if ($1 eq 'x') {
		if (!$in_keys) {
		    print "**ERROR** $origin[$start_index]: "
			. "unexpected \@itemx\n";
		}
	    } else {
	        &printSGML("<p>\n") if $in_keys;
		$in_keys = 1;
		&printSGML("<p>\n<tag>");
	    }
	    if ($ttype && $ttype ne '.' && $ttype ne '^') {
		if ($ttype =~ /(.+),(.+),(.+)/) {
		    $left = $1; $z = $2; $right = $3;
		} else {
		    $left = ''; $right = ''; $z = $ttype;
		}
		$atarg = $item;
		if ($z =~ s/^\^//) {$atarg =~ tr/a-z/A-Z/;}
		&printSGML("$left<$z>$atarg</$z>$right</tag>\n");
	    } else {
		&printSGML("$2</tag>\n");
	    }
	    if ($item && $table =~ /([fv])table/) {
		$_ = "\@" . $1 . "index " . $item;
		&process_index;
	    }
	} else {
	    &printSGML("<p>\n") if $in_keys;
	    $in_keys = 0;
#	    &printSGML("    <TD VALIGN=TOP>") unless $in_data;
	    $in_data = 1;
	    &parse();
	}
    }
    print "**ERROR** reached EOF while searching for end of the table "
	. "that started on $origin[$started_at]\n";
} # parse_table

########################################################################
sub parse_titlepage
# $_ is `@titlepage'.
{
    local ($started_at);

    $started_at = $start_index;

    $titlepage{num_subtitles} = 0;
    $titlepage{copyright} = "<P>";
    while ($texinfo_index < @texinfo) {
	&get_more_stuff_to_parse();
	if (/^\@title\s+(.*)/) {
	    $titlepage{title} = $1;
	} elsif (/^\@subtitle\s+(.*)/) {
	    $titlepage{subtitle}[$titlepage{num_subtitles}++] = $1;
	} elsif (/^\@author\s+(.*)/) {
	    $titlepage{author} = $1;
	} elsif (/^\@end\s+titlepage/) {
	    $titlepage{copyright} .= "";
	    return;
	} elsif (/^\s*$/) {
	    $titlepage{copyright} .= "<P>\n";
	} else {
	    $titlepage{copyright} .= $_;
	}
    }
    print "**ERROR** reached EOF while searching for end of the itemize "
	. "that started on $origin[$started_at]\n";
} # parse_titlepage

sub parse_multitable { 
    my ($started_at,$row_i);
    local ($first,$cols);
    
    $started_at = $start_index;
    
    &printSGML("$paragraph_end") if $in_paragraph;
    $in_paragraph = 0;
    $cols = 0;
    
    $tbl = "";
    
    $row_i = 0;
    $first = "";
#    $cols  = "|c";
    $cols  = "c";
    while ($texinfo_index < @texinfo) {
	&get_more_stuff_to_parse();    
	if (/^\@end\s+multitable/) {
	    if ($row_i == 1) {
#		&printSGML("<table><tabular ca=\"$cols|\">\n");
		&printSGML("<table>\n<tabular ca=\"$cols\">\n");
		$_ = $first;
		s/\|/\&verbar;/g;
		&parse();
	    }
	    $n_multitables ++;
	    &printSGML("</tabular>\n<caption/\n");
	    &printSGML("<label id=\"Table $n_multitables\">\n");
	    &printSGML("<ref id=\"elbaT $n_multitables\" ".
		       "name=\"Table $n_multitables\">\n");
	    &printSGML("</table>\n");
	    &printSGML("<label id=\"elbaT $n_multitables\">");
	    &printSGML("(<ref id=\"Table $n_multitables\" ".
		       "name=\"Table $n_multitables\">)\n");
	    return;
	}
	
	if ($row_1 <= 1) {
	    while (s/\@tab/<colsep>/) {
#		$cols .= "|c";
		$cols .= "c";
	    }
	} else {
	    s|\@tab|<colsep>|g;
	}
	
	if (/^\@item\s+(.*)/ || /^\@item()$/) {
	    if (++$row_i == 1) {
#		$cols  = "|c";
		$cols  = "c";
		$first = "$1\n";
	    } elsif ($row_i == 2) {
#		&printSGML("<table><tabular ca=\"$cols|\">\n");
		&printSGML("<table><tabular ca=\"$cols\">\n");
		$_ = $first;
		s/\|/\&verbar;/g;
		&parse();
		&printSGML("<rowsep>\n");
		&printSGML("$1\n");
	    } else {
		&printSGML("<rowsep>\n");
		&printSGML("$1\n");
	    }
	    $in_paragraph = 1;
	    $paragraph_end = "\n";
	} elsif ($row_i == 1) {
	    $first .= $_;
	} else {
	    s/\|/\&verbar;/g;
	    &parse();
	}
    }
    print "**ERROR** reached EOF while searching for end of the multitable "
      . "that started on $origin[$started_at]\n";
}

########################################################################
sub print_index
{
    local ($index) = @_;
    local ($letter, $inletter);
    $index = $index_name{$index};

    eval "\@keys = keys \%$index";

    $letter = "";
    foreach $item (sort texinfo_sort @keys) {
        $item =~ /^(.)/;
	unless ("\u$1" eq $letter) {
	  $letter = "\u$1";
	  &printSGML("</descrip>\n") if ($inletter);
	  &printSGML("<sect1>$letter</>\n<P>\n");
	  &printSGML("<descrip>\n");
	  $inletter = 1;
	}
	eval "\$val = \$$index\{\$item\}";
	&printSGML("<tag>$val</tag><p>\n");
    }
#    &printSGML("<tag>The End</tag><p>\n");
    &printSGML("</descrip>\n") if ($inletter);
} # print_index

sub texinfo_sort
{
    $x = $a; $x =~ s/<[^>]*>//g; $x =~ tr/A-Z/a-z/;
    $y = $b; $y =~ s/<[^>]*>//g; $y =~ tr/A-Z/a-z/;
    $x cmp $y;
} # texinfo_sort

########################################################################
sub process_index
#
# For example, `@cindex whatever' generates an entry in %cpindex
#
{
    s/\@cindex/\@cpindex/ || s/\@findex/\@fnindex/
	|| s/\@vindex/\@vrindex/ || s/\@kindex/\@kyindex/
	|| s/\@pindex/\@pgindex/ || s/\@tindex/\@tpindex/;

    /\@(..)index\s+(.*)/;

    $entry = "<ref id=\"$this\" name=\"$2\">";
    print "*** \$$index_name{$1}\{$2\} = $entry\n" if $debug{'index'};
    eval "\$$index_name{$1}\{\$2\} = \$entry";
} # process_index

########################################################################
sub process_node
# On entry, $_ is an @node line.
{
    s/^\@node\s+//;
    ($this,$next,$prev,$up) = split(/,/);

    &deduce_node_links() unless ($next || $prev || $up);

    $label  = $this;
#    $clabel = $this;
    
    $cthis = &canonical($this);
    $cnext = &canonical($next);
    $cprev = &canonical($prev);
    $cup = &canonical($up);
    
    $nfootnotes = 0;

    if ($this eq "Top") {
      &printSGML("<!doctype linuxdoc system>\n");
      &printSGML("<article>\n");

      if ($titlepage{title}) {
	&printSGML("<title>$titlepage{title}\n");
      }
      if ($titlepage{author}) {
	&printSGML("<author>$titlepage{author}\n");
      }
      &printSGML("<abstract>\n");
      $in_abstract = 1;

    }
} # process_node

sub terminate_node
{
    if ($this eq "Top") {
	&printSGML("</abstract>\n");
	&printSGML("<toc>\n");
	
	$in_abstract = 0;
	$in_document = 1;

	if ($titlepage{copyright}) {
	    &printSGML("<p>\n<quote>\n");
	    &printSGML("$titlepage{copyright}\n");
	    &printSGML("</quote>\n");
	}
    }

#    if ($nfootnotes) {
#	&printSGML("<p>\n");
# 	&printSGML("<tscreen>\n");
# 	for ($n=0; $n < $nfootnotes; ++$n) {
# 	    &printSGML("<p>\n");
# 	    &printSGML("<label id=\"$this" .
# 		       "footnote" . ($n+1) . "\">\n");
# 	    &printSGML("<ref id=\"$this" . 
# 		       "etontoof" . ($n+1) . "\" " .
# 		       "name=\"&lsqb;" . ($n+1) . "&rsqb;\">\n");
# 	    &printSGML("$footnote[$n]\n");
# 	}
# 	&printSGML("</tscreen>\n");
#    }
}

########################################################################
sub process_section
#
# On entry:
#     $_ is the section command (I.e. `@chapter Overview')
#     $i is the index to $_ in @lines
{
    &printSGML("$paragraph_end") if $in_paragraph;
    $in_paragraph = 0;

    /^\@(\w+)\s+(.*)/;

    $section_type = &raised_section_name($1);

    $section_number = '';

    if ($section_type eq 'chapter') {
	++$chapter; $section=$subsection=$subsubsection=0;
	$section_number = "Chapter $chapter: ";
    } elsif ($section_type eq 'section') {
	++$section; $subsection=$subsubsection=0;
	$section_number = "$chapter.$section: ";
    } elsif ($section_type eq 'subsection') {
	++$subsection; $subsubsection=0;
	$section_number = "$chapter.$section.$subsection: ";
    } elsif ($section_type eq 'subsubsection') {
	++$subsubsection;
	$section_number = "$chapter.$section.$subsection.$subsubsection: ";
    } elsif ($section_type eq 'appendix') {
	++$appendix; $section=$subsection=$subsubsection=0;
	$x = ('A'..'Z')[$appendix-1];
	$section_number = "Appendix $x: ";
    } elsif ($section_type eq 'appendixsec') {
	++$section; $subsection=$subsubsection=0;
	$x = ('A'..'Z')[$appendix-1];
	$section_number = "$x.$section: ";
    } elsif ($section_type eq 'appendixsubsec') {
	++$subsection; $subsubsection=0;
	$x = ('A'..'Z')[$appendix-1];
	$section_number = "$x.$section.$subsection: ";
    } elsif ($section_type eq 'appendixsubsubsec') {
	++$subsubsection;
	$x = ('A'..'Z')[$appendix-1];
	$section_number = "$x.$section.$subsection.$subsubsection: ";
    }

    $x = $directive_section{$section_type};
    if ($x < 0) {
      $x = "";
    }
    if ($label) {
      &printSGML("<sect$x>$2<label id=\"$label\"></>\n");
      $label = 0;
    } else {
      &printSGML("<sect$x>$2</>\n");
    }
} # process_section

########################################################################
sub process_synindex
#
# There's perhaps a bug here -- this presumes the @synindex comes before
# any @?index directives; anything already in <from> doesn't get merged
# into <to>!
#
{
    local ($code) = @_;		# Either 0 or 1; 1 means @syncodeindex

    /\@syn\w*index\s+(\w+)\s+(\w+)/;

    print "*** synindex $1 $2\n" if $debug{'index'};

    $index_name{$1} = $2 . "index";
    $index_style{$1} = 'tt' if $code;
} # process_synindex

########################################################################
sub printSGML
{
    local ($line) = @_;
    $line =~ s/\$R/\}/g;
    $line =~ s/\$L/\{/g;
    $line =~ s/\$A/\@/g;
    $line =~ s/\$D/\$/g;
    if ($debug{printSGML}) {
	print $line;
    } else {
	print SGML $line;
    }
} # printSGML

sub raised_section_name { ($n) = @_;

    if ($n eq 'chapter') {
	return 'chapter' if $raise_sections >= 0;
	return 'section' if $raise_sections == -1;
	return 'subsection' if $raise_sections == -2;
	return 'subsubsection' if $raise_sections <= -3;
    } elsif ($n eq 'section') {
	return 'chapter' if $raise_sections >= 1;
	return 'section' if $raise_sections == 0;
	return 'subsection' if $raise_sections == -1;
	return 'subsubsection' if $raise_sections <= -2;
    } elsif ($n eq 'subsection') {
	return 'chapter' if $raise_sections >= 2;
	return 'section' if $raise_sections == 1;
	return 'subsection' if $raise_sections == 0;
	return 'subsubsection' if $raise_sections <= -1;
    } elsif ($n eq 'subsubsection') {
	return 'chapter' if $raise_sections >= 3;
	return 'section' if $raise_sections == 2;
	return 'subsection' if $raise_sections == 1;
	return 'subsubsection' if $raise_sections <= 0;
    } elsif ($n eq 'appendix') {
	return 'appendix' if $raise_sections >= 0;
	return 'appendixsec' if $raise_sections == -1;
	return 'appendixsubsec' if $raise_sections == -2;
	return 'appendixsubsubsec' if $raise_sections <= -3;
    } elsif ($n eq 'appendixsec') {
	return 'appendix' if $raise_sections >= 1;
	return 'appendixsec' if $raise_sections == 0;
	return 'appendixsubsec' if $raise_sections == -1;
	return 'appendixsubsubsec' if $raise_sections <= -2;
    } elsif ($n eq 'appendixsubsec') {
	return 'appendix' if $raise_sections >= 2;
	return 'appendixsec' if $raise_sections == 1;
	return 'appendixsubsec' if $raise_sections == 0;
	return 'appendixsubsubsec' if $raise_sections <= -1;
    } elsif ($n eq 'appendixsubsubsec') {
	return 'appendix' if $raise_sections >= 3;
	return 'appendixsec' if $raise_sections == 2;
	return 'appendixsubsec' if $raise_sections == 1;
	return 'appendixsubsubsec' if $raise_sections <= 0;
    } elsif ($n eq 'chapheading') {
	return 'chapheading' if $raise_sections >= 0;
	return 'heading' if $raise_sections == -1;
	return 'subheading' if $raise_sections == -2;
	return 'subsubheading' if $raise_sections <= -3;
    } elsif ($n eq 'majorheading') {
	return 'majorheading' if $raise_sections >= 0;
	return 'heading' if $raise_sections == -1;
	return 'subheading' if $raise_sections == -2;
	return 'subsubheading' if $raise_sections <= -3;
    } elsif ($n eq 'heading') {
	return 'majorheading' if $raise_sections >= 1;
	return 'heading' if $raise_sections == 0;
	return 'subheading' if $raise_sections == -1;
	return 'subsubheading' if $raise_sections <= -2;
    } elsif ($n eq 'subheading') {
	return 'majorheading' if $raise_sections >= 2;
	return 'heading' if $raise_sections == 1;
	return 'subheading' if $raise_sections == 0;
	return 'subsubheading' if $raise_sections <= -1;
    } elsif ($n eq 'subsubheading') {
	return 'majorheading' if $raise_sections >= 3;
	return 'heading' if $raise_sections == 2;
	return 'subheading' if $raise_sections == 1;
	return 'subsubheading' if $raise_sections <= 0;
    } elsif ($n eq 'unnumbered') {
	return 'unnumbered' if $raise_sections >= 0;
	return 'unnumberedsec' if $raise_sections == -1;
	return 'unnumberedsubsec' if $raise_sections == -2;
	return 'unnumberedsubsubsec' if $raise_sections <= -3;
    } elsif ($n eq 'unnumberedsec') {
	return 'unnumbered' if $raise_sections >= 1;
	return 'unnumberedsec' if $raise_sections == 0;
	return 'unnumberedsubsec' if $raise_sections == -1;
	return 'unnumberedsubsubsec' if $raise_sections <= -2;
    } elsif ($n eq 'unnumberedsubsec') {
	return 'unnumbered' if $raise_sections >= 2;
	return 'unnumberedsec' if $raise_sections == 1;
	return 'unnumberedsubsec' if $raise_sections == 0;
	return 'unnumberedsubsubsec' if $raise_sections <= -1;
    } elsif ($n eq 'unnumberedsubsubsec') {
	return 'unnumbered' if $raise_sections >= 3;
	return 'unnumberedsec' if $raise_sections == 2;
	return 'unnumberedsubsec' if $raise_sections == 1;
	return 'unnumberedsubsubsec' if $raise_sections <= 0;
    }
    print "**WARNING** $origin[$start_index] unknown section type $n\n";
    return $n;
}

########################################################################
sub read_input
#
# Read the texinfo source into @texinfo.  Don't copy comments or the
# `@ifxxx' and `@end ifxxx' surrounding [or the contents of] conditional
# blocks.  Read `@include' files.
{
    local ($echo,$terminator_re,$started_at) = @_;
    local ($doecho);

    while (&texinfo_read()) {

	next if (/^\@c$/ || /^\@c\s/ || /^\@comment/);
	next if (/^\\input texinfo/);

	if (/^\@if(not)*info/) {
	    &read_input($echo,'/^\@end\s+if(not)*info/',
			                      "$texinfo_file[0] line $.");
	    next;
	}
	if (/^\@if(not)*html/) {
	    if ($1) {
		$doecho = 0;
	    } else {
		$doecho = $echo;
	    }
	    &read_input($doecho,'/^\@end\s+if(not)*html/',
			                      "$texinfo_file[0] line $.");
	    next;
	}
	if (/^\@if(not)*tex/)  {
	    if ($1) {
		$doecho = $echo;
	    } else {
		$doecho = 0;
	    }
	    &read_input($doecho,'/^\@end\s+if(not)*tex/',
			"$texinfo_file[0] line $.");
	    next;
	}
	if (/^\@tex/)  {
	    &read_input(0,'/^\@end\s+tex/',
			                      "$texinfo_file[0] line $.");
	    next;
	}
	if (/^\@ignore/) {
	    # @ignore doesn't nest
	    $ignore_from = "$texinfo_file[0] line $.";
	    while (&texinfo_read()) {
		last if (/^\@end\s+ignore/);
	    }
	    unless (/^\@end\s+ignore/) {
		print "Unexpected EOF while searching from $ignore_from "
		    . "for \'\@end ignore\'\n";
	    }
	    next;
	}
	if (/^\@direntry/) {
	    # @direntry doesn't nest
	    $ignore_from = "$texinfo_file[0] line $.";
	    while (&texinfo_read()) {
		last if (/^\@end\s+direntry/);
	    }
	    unless (/^\@end\s+direntry/) {
		print "Unexpected EOF while searching from $ignore_from "
		    . "for \'\@end direntry\'\n";
	    }
	    next;
	}
	    
#  	if (/^\@titlepage/) {
#  	    &read_input(0,'/^\@end\s+titlepage/',"$texinfo_file[0] line $.");
#  	    next;
#  	}

	if (/^\@ifclear\s+(\S+)/) {
	    &read_input($echo&&(!defined($set{$1})),'/^\@end\s+ifclear/',
			                          "$texinfo_file[0] line $.");
	    next;
	}
	if (/^\@ifset\s+(\S+)/) {
	    &read_input($echo&&defined($set{$1}),'/^\@end\s+ifset/',
			                          "$texinfo_file[0] line $.");
	    next;
	}
	
	return if eval "$terminator_re";

	if (/^\@include\s+(\S+)/) {
	    &open_input_file($1);
	    next;
	}

	if (/^\@(set|clear)\s+(\S+)/) {
	    if ($1 eq "set") {
		$set{$2} = 1;
	    } else {
		undef($set{$2});
	    }
	}

	next unless $echo;

	if (/^\@(\w+)/) {next if $ignore_these_directives{$1};}
	
	# Hide @@, @{, and @} so later on it'll be easier to process
	# stuff like `@code{@@TeX@{@}}'.
	s/\$/\$D/g; s/\@\@/\$A/g; s/\@{/\$L/g; s/\@}/\$R/g;

	# Convert the SGML special characters
	s/\&/\&amp;/g; s/\</\&lt;/g; s/\>/\&gt;/g; s/\"/\&quot;/g;
	s/\[/\&lsqb;/g; s/\]/\&rsqb;/g;
#	s/\//\&sol;/g;
	
	$texinfo[$ntexinfo] = $_;
	$origin[$ntexinfo] =  "$texinfo_file[0] line $.";
	++$ntexinfo;
    }

    print "Unexpected EOF while searching from $started_at "
	. "for $terminator_re\n";
} # read_input

########################################################################
sub initialize_tables
{
    # Lists which `@x{y}' get expanded into `y'.
    %atxy_2_y = (
	'asis', 1,
         'r', 1,
         'w', 1,
    );

    # Describes which `@x{y}' get expanded into `<z>y</z>' and what `z'
    # is in those expansions!  (If the expansion matches
    # ``/(.*),(.*),(.*)/'' then y actually expands to ``$1<$2>y</$2>$3'';
    # if z (or $2) begins with ^ then uppercase y before doing the
    # expansion.  Finally, if z is "^" then only uppercase y and don't
    # bother with any <z> expansion).
    %atxy_2_zyz= (
        'b',         'bf',
	'cite',      'tt',
	'code',      "tt",
	'dfn',       'em',
        'dmn',       'em',
        'emph',      'em',
        'file',      "tt",
        'i',         'em',
	'kbd',       'tt',
	'key',       'tt',
	'math',      'tt',
	'samp',      "tt",
        'sc',        'tt',
	'strong',    'bf',
        't',         'tt',
	'titlefont', 'tt',
	'var',       '^',
    );

    # Describes which `@x{}' can be expanded into `z' and what `z' is in
    # those expansions!
    %atx_2_z = (
	'TeX',         '&tex;',
	'bullet',      '*',
	'copyright',   '&copy;',
	'dots',        '...',
        'enddots',     '....',
	'equiv',       '==',
	'error',       'error-->',
	'expansion',   '==>',
	'minus',       '-',
	'point',       '-!-',
	'pounds',      '&pound;',
	'print',       '-|',
	'result',      '=>',
	'exclamdown',  '&iexcl;',    # upside down expclamation point
	'questiondown','&iquest;',   # upside down question mark
	'aa',          '&aring;',    # `a' with a circle overtop
	'AA',          '&Aring;',    # `A' with a circle overtop
	'ae',          '&aelig;',    # `ae' squeezed together
	'AE',          '&AElig;',    # `AE' squeezed together
	'l',           'l',          # suppressed `l'
	'L',           'L',          # suppressed `L'
	'o',           '&oslash;',   # `o' with slash
	'O',           '&Oslash;',   # `O' with slash
	'oe',          'oe',         # `oe' squeezed together
	'OE',          'OE',         # `OE' squeezed together
	'ss',          '&szlig;',    # es-zet (German character)
	'today',       &today(),
    ); 

    # Describes which `@x{y}' can be expanded into `z' and what `z' is in
    # those expansions!
    %atxy_2_z = (
	'dotaccent o',  'o',        # `o' with a dot overtop
	'dotless i',    'i',        # undotted `i'
	'dotless j',    'j',        # undotted `j'
	'H o',          'o',        # `o' with `"' overtop
	'ringaccent o', 'o',        # `o' with a circle overtop
	'u o',          'o',        # `o' with a `u' curve overtop
	'ubaraccent o', 'o',        # `o' with a `_' underneath
	'udotaccent o', 'o',        # `o' with a dot underneath
	'v o',          'o',        # `o' with a `v' mark overtop
        'tieaccent oo', 'oo',       # `oo' joined with a half-circle overtop
    );

    # Lists the '@x{y}' cross reference commands, and describes how they get
    # expanded.  Note the 'X' beginning each expansion -- it's there so 'ref'
    # doesn't get expanded to ''!
    %atxy_2_ref = (
	'xref',     'XSee ',
	'ref',      'X',
	'pxref',    'Xsee ',
	'href',     'X',
	'inforef',  'XSee ',
        'email',    'X',
	'url',      'X',
	'uref',      'X',
    );

    %ignore_these_directives = (
	'break', 1,
	'contents', 1,
	'dircategory', 1,
	'evenfooting', 1,
	'everyfooting', 1,
	'everyheading', 1,
	'finalout', 1,
	'footnotestyle', 1,
	'headings', 1,
        'need', 1,
	'noindent', 1,
	'oddfooting', 1,
        'page', 1,
	'paragraphindent', 1,
	'setchapternewpage', 1,
	'setfilename', 1,
	'shortcontents', 1,
	'shorttitlepage', 1,
	'smallbook', 1,
        'sp', 1,
	'subtitle', 1,
	'summarycontents', 1,
        'top', 1,
        'vskip', 1,			    
    );

    # List the section directives and indicate what heading level 
    # each one gets.
    %directive_section = (
	'chapter', -1,
	'section', 1,
	'subsection', 2,
	'subsubsection',3,
	'appendix', -1,
	'appendixsec', 1,
	'appendixsubsec', 2,
	'appendixsubsubsec', 3,
	'chapheading', -1,
	'majorheading', -1,
	'heading', 1,
	'subheading', 2,
	'subsubheading', 3,
	'unnumbered', -1,
	'unnumberedsec', 1,
	'unnumberedsubsec', 2,
	'unnumberedsubsubsec', 3,
    );

    # These @ directives begin a block of preformatted text
    # (">PRE" means indented inside <PRE>...</PRE>)
    %directive_block = (
	'display',      '->verb',
	'example',      '->verb',
	'format',       '-verb',
	'group',       '-',
	'lisp',         '->verb',
	'quotation',    '+quote',
        'smallexample', '->verb',
    );

    %index_name = (
        'cp', 'cpindex',
	'fn', 'fnindex',
	'ky', 'kyindex',
	'pg', 'pgindex',
	'tp', 'tpindex',
	'vr', 'vrindex',
    );
    %index_style = (
	'fn', 'tt',
	'ky', 'tt',
	'pg', 'tt',
	'tp', 'tt',
	'vr', 'tt',
    );
} # initialize_tables

########################################################################
sub open_input_file
{
    unshift(@texinfo_file,$_[0]);
    print "opening $_[0] ...\n" if $debug{open_input_file};
    open($texinfo_file[0],$_[0]) || die "Couldn't open $_[0]: $!\n";
} # open_input_file

########################################################################
sub texinfo_read
# Reads the next line of texinfo input into $_.
{
    do {
	$fd = $texinfo_file[0];
	$_ = <$fd>;
    } while ($_ eq '' && shift @texinfo_file);
    return $_;
} # texinfo_read

########################################################################
sub today
{
    $today = `date`;
    $today =~ s/\w+ (\w+ +[0-9]+) [0-9]+:[0-9]+:[0-9]+ \w+ ([0-9]+)\n/$1 $2/;
    $today =~ s/ +/ /g;
    return $today;
} # today

