/* Makeinfo -- convert texinfo format files into info files

   Copyright (C) 1987, 1991 Free Software Foundation, Inc.

   This file is part of GNU Info.

   Makeinfo is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY.  No author or distributor accepts
   responsibility to anyone for the consequences of using it or for
   whether it serves any particular purpose or works at all, unless he
   says so in writing.  Refer to the GNU Emacs General Public License
   for full details.

   Everyone is granted permission to copy, modify and redistribute
   Makeinfo, but only under the conditions described in the GNU Emacs
   General Public License.   A copy of this license is supposed to
   have been given to you along with GNU Emacs so you can know your
   rights and responsibilities.  It should be in a file named COPYING.
   Among other things, the copyright notice and this notice must be
   preserved on all copies.  */

/* This is Makeinfo version 1.43.  If you change the version number of
   Makeinfo, please change it here and at the lines reading:

    int major_version = 1;
    int minor_version = 43;

   in the code below. */

/* You can change some of the behaviour of Makeinfo by changing the
   following defines: */

/* Define INDENT_PARAGRAPHS_IN_TABLE if you want the paragraphs which
   appear within an @table, @ftable, or @itemize environment to have
   standard paragraph indentation.  Without this, such paragraphs have
   no starting indentation. */
/* #define INDENT_PARAGRAPHS_IN_TABLE */

/* Define DEFAULT_INDENTATION_INCREMENT as an integer which is the amount
   that @example should increase indentation by.  This increment is used
   for all insertions which indent the enclosed text. */
#define DEFAULT_INDENTATION_INCREMENT 5

/* Define PARAGRAPH_START_INDENT to be the amount of indentation that
   the first lines of paragraphs receive by default, where no other
   value has been specified.  Users can change this value on the command
   line, with the +paragraph-indent option, or within the texinfo file,
   with the @paragraphindent command. */
#define PARAGRAPH_START_INDENT 3

/* Define DEFAULT_PARAGRAPH_SPACING as the number of blank lines that you
   wish to appear between paragraphs.  A value of 1 creates a single blank
   line between paragraphs.  Paragraphs are defined by 2 or more consecutive
   newlines in the input file (i.e., one or more blank lines). */
#define DEFAULT_PARAGRAPH_SPACING 1

/* **************************************************************** */
/*								    */
/*			Include File Declarations       	    */
/*								    */
/* **************************************************************** */

#if defined (_AIX)
#  pragma alloca
#endif /* _AIX */

#include <stdio.h>
#include <sys/types.h>
#include <ctype.h>
#include <sys/stat.h>
#include <ctype.h>
#include <pwd.h>
#include <errno.h>
#include <memory.h>
#include <stdlib.h>
#if __STDC__
#include <stdarg.h>
#endif
#include "getopt.h"

#if defined (VMS)
#include <perror.h>
#endif

#if defined (USG) || defined (VMS)
#include <string.h>
#include <time.h>
#else
#include <strings.h>
#include <sys/time.h>
#endif

#include <fcntl.h>
#include <sys/file.h>

#if defined (USG) || defined (hpux)
#define bcopy(source, dest, count) memcpy (dest, source, count)
char *index(s,c) char *s; { char *strchr(); return strchr(s,c); }
char *rindex(s,c) char *s; { char *strrchr(); return strrchr(s,c); }
#endif /* USG || hpux */

#if defined (__GNUC__)
#ifndef alloca
#  define alloca __builtin_alloca
#endif
#else
#  if defined (sparc)
#    include <alloca.h>
#  else
#    if !defined (_AIX)
extern char *alloca ();
#    endif /* _AIX */
#  endif /* !sparc */
#endif /* !__GNUC__ */

#ifdef NeXT
extern int open(const char *, int, ...), close(int);
extern int read(int, void *, int), write(int, const void *, int);
#endif /* NeXT */

#ifdef atarist
/* #include <unistd.h> */ /* don't - clash with getopt.h from GNU */
extern int open (const char *, int, ...), close(int);
extern long _read (int, void *, unsigned long);
extern long _write (int, const void *, unsigned long);
#include <support.h>
/* #define DOTS	*/	/* define this if you would like to have 
			   a visual indicator that computer still works */
#define STDERR stdout	/* hack around redirection problems */
#define WMODE  "w"	/* allow for easy modifications     */
			/* we assume that library is smart enough */
			/* and write() behaves appriopriately     */
#else  /* not-atarist */
#define STDERR stderr
#define WMODE  "w"
#endif /* atarist */

/* Forward declarations. */
/* void *xmalloc (), *xrealloc (); */


#ifndef __PROTO
#if defined(__STDC__) || defined(__cplusplus)
# define __PROTO(s) s
#else
# define __PROTO(s) ()
#endif
#endif

/* Non-zero means that we are currently hacking the insides of an
   insertion which would use a fixed width font. */
static int in_fixed_width_font = 0;

/* Non-zero means that start_paragraph () MUST be called before we pay
   any attention to close_paragraph () calls. */
int must_start_paragraph = 0;

/* Some systems don't declare this function in pwd.h. */
extern struct passwd *getpwnam __PROTO((const char *));

/* **************************************************************** */
/*								    */
/*			      Global Defines  			    */
/*								    */
/* **************************************************************** */

/* Error levels */
#define NO_ERROR 0
#define SYNTAX	 2
#define FATAL	 4

/* How to allocate permanent storage for STRING. */
#define savestring(x) \
  ((char *)strcpy ((char *)xmalloc (1 + ((x) ? strlen (x) : 0)), \
		   (x) ? (x) : ""))

/* C's standard macros don't check to make sure that the characters being
   changed are within range.  So I have to check explicitly. */

/* GNU Library doesn't have toupper().  Until GNU gets this fixed, I will
   have to do it. */
#ifndef atarist
/* the above does not apply to gnulib on AtariST which is not GNU Library */
#ifndef toupper
#define toupper(c) ((c) - 32)
#endif

#define coerce_to_upper(c) ((islower(c) ? toupper(c) : (c)))
#define coerce_to_lower(c) ((isupper(c) ? tolower(c) : (c)))
#else  /* on ST toupper and tolower include checks - use _to...  instead */
#define coerce_to_upper(c) ((islower(c) ? _toupper(c) : (c)))
#define coerce_to_lower(c) ((isupper(c) ? _tolower(c) : (c)))
#endif /* atarist */

#define control_character_bit 0x40 /* %01000000, must be off. */
#define meta_character_bit 0x080/* %10000000, must be on.  */
#define CTL(c) ((c) & (~control_character_bit))
#define UNCTL(c) coerce_to_upper(((c)|control_character_bit))
#define META(c) ((c) | (meta_character_bit))
#define UNMETA(c) ((c) & (~meta_character_bit))

#define whitespace(c) (((c) == '\t') || ((c) == ' '))
#ifdef atarist
#define cr_or_whitespace(c) isspace((c))  /* small optimization */
#else
#define cr_or_whitespace(c) (((c) == '\t') || ((c) == ' ') || ((c) == '\n'))
#endif /* atarist */
#define sentence_ender(c) ((c) == '.' || (c) == '?' || (c) == '!')

#ifndef isletter
#define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
#endif

#ifndef isupper
#define isupper(c) ((c) >= 'A' && (c) <= 'Z')
#endif

#ifndef isdigit
#define isdigit(c)  ((c) >= '0' && (c) <= '9')
#endif

#ifndef digit_value
#define digit_value(c) ((c) - '0')
#endif

#define member(c, s) (index (s, c) != NULL)

#define COMMAND_PREFIX '@'

/* Stuff for splitting large files. */
#define SPLIT_SIZE_THRESHOLD 70000L    /* What's good enough for Stallman... */
#define DEFAULT_SPLIT_SIZE   50000L    /* Is probably good enough for me. */
int splitting = 1;		/* Always true for now. */

typedef int FUNCTION ();	/* So I can say FUNCTION *foo; */


/* **************************************************************** */
/*								    */
/*			    Global Variables			    */
/*								    */
/* **************************************************************** */

/* Global pointer to argv[0]. */
char *progname;

/* The current input file state. */
char *input_filename;
char *input_text;
size_t size_of_input_text;
size_t input_text_offset;
size_t line_number;

#define curchar() input_text[input_text_offset]

#ifndef atarist
#define command_char(c) ((!whitespace(c)) && \
			 ((c) != '\n') && \
			 ((c) != '{') && \
			 ((c) != '}'))
#else  /* atarist */
#define command_char(c) ((!isspace(c)) && \
			 ((c) != '{') && \
			 ((c) != '}'))
#endif /* atarist */

#define skip_whitespace() while (input_text_offset != size_of_input_text \
				 && whitespace(curchar()))\
  input_text_offset++

/* Return non-zero if STRING is the text at input_text + input_text_offset,
   else zero. */
#define looking_at(string) \
  (strncmp (input_text + input_text_offset, string, strlen (string)) == 0)

/* And writing to the output. */

/* The output file name. */
char *output_filename = (char *)NULL;
char *pretty_output_filename;

/* Name of the output file that the user elected to pass on the command line.
   Such a name overrides any name found with the @setfilename command. */
char *command_output_filename = (char *)NULL;

/* Current output stream. */
FILE *output_stream;

/* Position in the output file. */
size_t output_position;

/* Output paragraph buffer. */
unsigned char *output_paragraph;

/* Offset into OUTPUT_PARAGRAPH. */
size_t output_paragraph_offset;

/* The output paragraph "cursor" horizontal position. */
int output_column = 0;

/* Non-zero means output_paragraph contains text. */
int paragraph_is_open = 0;

#define INITIAL_PARAGRAPH_SPACE 5000
size_t paragraph_buffer_len = INITIAL_PARAGRAPH_SPACE;

/* Filling.. */
/* Non-zero indicates that filling will take place on long lines. */
int filling_enabled = 1;

/* Non-zero means that words are not to be split, even in long lines.  This
   gets changed for cm_w (). */
int non_splitting_words = 0;

/* Non-zero indicates that filling a line also indents the new line. */
int indented_fill = 0;

/* The column at which long lines are broken. */
int fill_column = 72;

/* The amount of indentation to apply at the start of each line. */
int current_indent = 0;

/* The amount of indentation to add at the starts of paragraphs.
   0 means don't change existing indentation at paragraph starts.
   > 0 is amount to indent new paragraphs by.
   < 0 means indent to column zero by removing indentation if necessary.

   This is normally zero, but some people prefer paragraph starts to be
   somewhat more indented than paragraph bodies.  A pretty value for
   this is 3. */
int paragraph_start_indent = PARAGRAPH_START_INDENT;

/* Non-zero means that the use of paragraph_start_indent is inhibited.
   @example uses this to line up the left columns of the example text.
   A negative value for this variable is incremented each time it is used.
   @noindent uses this to inhibit indentation for a single paragraph.  */
int inhibit_paragraph_indentation = 0;

/* Indentation that is pending insertion.  We have this for hacking lines
   which look blank, but contain whitespace.  We want to treat those as
   blank lines. */
int pending_indent = 0;

/* The amount that indentation increases/decreases by. */
int default_indentation_increment = DEFAULT_INDENTATION_INCREMENT;

/* Non-zero indicates that indentation is temporarily turned off. */
int no_indent = 1;

/* Non-zero means forcing output text to be flushright. */
int force_flush_right = 0;

/* Non-zero means that the footnote style for this document was set on
   the command line, which overrides any other settings. */
int footnote_style_preset = 0;

/* Non-zero means that we automatically number footnotes that have no
   specified marker. */
int number_footnotes = 1;

/* The current footnote number in this node.  Each time a new node is
   started this is reset to 1. */
int current_footnote_number = 1;

/* Command name in the process of being hacked. */
char *command;

/* The index in our internal command table of the currently
   executing command. */
int command_index;

/* A stack of file information records.  If a new file is read in with
   "@input", we remember the old input file state on this stack. */
typedef struct fstack
{
  struct fstack *next;
  char *filename;
  char *text;
  size_t size;
  size_t offset;
  size_t line_number;
} FSTACK;

FSTACK *filestack = (FSTACK *) NULL;

/* Stuff for nodes. */
/* The current nodes node name. */
char *current_node = (char *)NULL;

/* The current nodes section level. */
int current_section = 0;

/* The filename of the current input file.  This is never freed. */
char *node_filename = (char *)NULL;

/* What we remember for each node. */
typedef struct tentry
{
  struct tentry *next_ent;
  char *node;		/* name of this node. */
  char *prev;		/* name of "Prev:" for this node. */
  char *next;		/* name of "Next:" for this node. */
  char *up;		/* name of "Up:" for this node.   */
  size_t position;	/* output file position of this node. */
  size_t line_no;	/* defining line in source file. */
  char *filename;	/* The file that this node was found in. */
  int touched;		/* non-zero means this node has been referenced. */
  int flags;		/* Room for growth.  Right now, contains 1 bit. */
} TAG_ENTRY;

/* If node-a has a "Next" for node-b, but node-b has no "Prev" for node-a,
   we turn on this flag bit in node-b's tag entry.  This means that when
   it is time to validate node-b, we don't report an additional error
   if there was no "Prev" field. */
#define PREV_ERROR 0x1
#define NEXT_ERROR 0x2
#define UP_ERROR   0x4
#define NO_WARN	   0x8
#define IS_TOP 	   0x10

TAG_ENTRY *tag_table = (TAG_ENTRY *) NULL;

#define HAVE_MACROS
#if defined (HAVE_MACROS)
/* Macro definitions for user-defined commands. */
typedef struct {
  char *name;			/* Name of the macro. */
  char *definition;		/* Definition text. */
  char *filename;		/* File where this macro is defined. */
  size_t lineno;		/* Line number within FILENAME. */
} MACRO_DEF;

/**
void add_macro (), execute_macro ();
MACRO_DEF *find_macro (), *delete_macro ();
**/
#endif /* HAVE_MACROS */

/* Menu reference, *note reference, and validation hacking. */

/* The various references that we know about. */
enum reftype
{
  menu_reference, followed_reference
};

/* A structure to remember references with.  A reference to a node is
   either an entry in a menu, or a cross-reference made with [px]ref. */
typedef struct node_ref
{
  struct node_ref *next;
  char *node;			/* Name of node referred to. */
  char *containing_node;	/* Name of node containing this reference. */
  size_t line_no;		/* Line number where the reference occurs. */
  int section;			/* Section level where the reference occurs. */
  char *filename;		/* Name of file where the reference occurs. */
  enum reftype type;		/* Type of reference, either menu or note. */
} NODE_REF;

/* The linked list of such structures. */
NODE_REF *node_references = (NODE_REF *) NULL;

/* Flag which tells us whether to examine menu lines or not. */
int in_menu = 0;

/* Flags controlling the operation of the program. */

/* Default is to notify users of bad choices. */
int print_warnings = 1;

/* Default is to check node references. */
int validating = 1;

/* Non-zero means do not output "Node: Foo" for node separations. */
int no_headers = 0;

/* Number of errors that we tolerate on a given fileset. */
int max_error_level = 100;

/* Maximum number of references to a single node before complaining. */
int reference_warning_limit = 1000;

/* Non-zero means print out information about what is going on when it
   is going on. */
int verbose_mode = 0;

/* Non-zero means run only splitting part on already made info file */
int only_split = 0;

/* The list of commands that we hack in texinfo.  Each one
   has an associated function.  When the command is encountered in the
   text, the associated function is called with START as the argument.
   If the function expects arguments in braces, it remembers itself on
   the stack.  When the corresponding close brace is encountered, the
   function is called with END as the argument. */

#define START 0
#define END 1

typedef struct brace_element
{
  struct brace_element *next;
  FUNCTION *proc;
  size_t pos, line;
} BRACE_ELEMENT;

BRACE_ELEMENT *brace_stack = (BRACE_ELEMENT *) NULL;

/* we need more typedefs here for prototypes below */

typedef struct
{
  const char *name;
  FUNCTION *proc;
  int argument_in_braces;
} COMMAND;

typedef struct generic_list
{
  struct generic_list *next;
}            GENERIC_LIST;

/* An index element... */
typedef struct index_elt
{
  struct index_elt *next;
  char *entry;			/* The index entry itself. */
  char *node;			/* The node from whence it came. */
  int code;			/* Non-zero means add `@code{...}' when
				   printing this element. */
  size_t defining_line;		/* Line number where this entry was written. */
} INDEX_ELT;

typedef struct
{
  char *name;
  int index;
  int code;
} INDEX_ALIST;

/* A list of short-names for each index, and the index to that index in our
   index array, the_indices.  In addition, for each index, it is remembered
   whether that index is a code index or not.  Code indices have @code{}
   inserted around the first word when they are printed with printindex. */
INDEX_ALIST **name_index_alist = (INDEX_ALIST **) NULL;

/* An array of pointers.  Each one is for a different index.  The
   "synindex" command changes which array slot is pointed to by a
   given "index". */
INDEX_ELT **the_indices = (INDEX_ELT **) NULL;

/* The number of defined indices. */
int defined_indices = 0;

enum insertion_type { menu, quotation, lisp, smalllisp, example,
  smallexample, display, itemize, format, enumerate, cartouche, table,
  ftable, vtable, group, ifinfo, flushleft, flushright, ifset, ifclear, deffn,
  defun, defmac, defspec, defvr, defvar, defopt, deftypefn,
  deftypefun, deftypevr, deftypevar, defcv, defivar, defop, defmethod,
  deftypemethod, deftp, bad_type };

struct token_accumulator
{
  unsigned int length;
  unsigned int index;
  char **tokens;
};

/* Forward declarations (protypes). */

int main __PROTO((int argc, char **argv));
void print_version_info __PROTO((void));
void *xmalloc __PROTO((size_t nbytes));
void *xrealloc __PROTO((void *pointer, size_t nbytes));
void memory_error __PROTO((const char *callers_name, size_t bytes_wanted));
void usage __PROTO((void));
GENERIC_LIST *reverse_list __PROTO((GENERIC_LIST *list));
char *find_and_load __PROTO((char *filename));
void pushfile __PROTO((void));
void popfile __PROTO((void));
void flush_file_stack __PROTO((void));
void push_node_filename __PROTO((void));
void pop_node_filename __PROTO((void));
char *filename_part __PROTO((char *filename));
char *pathname_part __PROTO((char *filename));
char *expand_filename __PROTO((char *filename, const char *input_name));
char *full_pathname __PROTO((char *filename));
int fs_error __PROTO((char *filename));
int error __PROTO((const char *format, ...));
void line_error __PROTO((const char *format, ...));
int warning __PROTO((const char *format, ...));
void remember_error __PROTO((void));
char *read_token __PROTO((void));
int self_delimiting __PROTO((int character));
void canon_white __PROTO((char *string));
void fix_whitespace __PROTO((char *string));
void discard_until __PROTO((const char *string));
int get_until __PROTO((const char *match, char **string));
void get_until_in_line __PROTO((const char *match, char **string));
void get_rest_of_line __PROTO((char **string));
void backup_input_pointer __PROTO((void));
void get_until_in_braces __PROTO((const char *match, char **string));
void convert __PROTO((char *name));
void free_and_clear __PROTO((char **pointer));
void init_internals __PROTO((void));
void init_paragraph __PROTO((void));
void init_par_parameters __PROTO((void));
void reader_loop __PROTO((void));
COMMAND *get_command_entry __PROTO((char *string));
void read_command __PROTO((void));
const char *find_proc_name __PROTO((FUNCTION *proc));
void init_brace_stack __PROTO((void));
void remember_brace __PROTO((FUNCTION *proc));
void remember_brace_1 __PROTO((FUNCTION *proc, size_t position));
void pop_and_call_brace __PROTO((void));
void discard_braces __PROTO((void));
int get_char_len __PROTO((unsigned int character));
void add_word_args __PROTO((const char *format, ...));
void add_word __PROTO((const char *string));
void add_char __PROTO((unsigned int character));
void insert __PROTO((int character));
void kill_self_indent __PROTO((int count));
void flush_output __PROTO((void));
void close_single_paragraph __PROTO((void));
void close_insertion_paragraph __PROTO((void));
void close_paragraph_with_lines __PROTO((int lines));
void close_paragraph __PROTO((void));
void ignore_blank_line __PROTO((void));
void do_flush_right_indentation __PROTO((void));
void start_paragraph __PROTO((void));
void indent __PROTO((int amount));
size_t search_forward __PROTO((const char *string, size_t from));
int stricmp __PROTO((const char *string1, const char *string2));
int strnicmp __PROTO((const char *string1, const char *string2, size_t count));
void init_insertion_stack __PROTO((void));
enum insertion_type current_insertion_type __PROTO((void));
char *current_item_function __PROTO((void));
char *get_item_function __PROTO((void));
void push_insertion __PROTO((enum insertion_type type, char *item_function));
void pop_insertion __PROTO((void));
const char *insertion_type_pname __PROTO((enum insertion_type type));
enum insertion_type find_type_from_name __PROTO((char *name));
void do_nothing __PROTO((void));
int  defun_insertion __PROTO((enum insertion_type type));
void start_enumerating __PROTO((int at, int type));
void stop_enumerating __PROTO((void));
void enumerate_item __PROTO((void));
void begin_insertion __PROTO((enum insertion_type type));
void end_insertion __PROTO((enum insertion_type type));
void discard_insertions __PROTO((void));
void insert_self __PROTO((void));
void cm_asterisk __PROTO((void));
void cm_dots __PROTO((int arg));
void cm_bullet __PROTO((int arg));
void cm_minus __PROTO((int arg));
void cm_TeX __PROTO((int arg));
void cm_copyright __PROTO((int arg));
void cm_today __PROTO((int arg));
void cm_code __PROTO((int arg));
void cm_samp __PROTO((int arg));
void cm_file __PROTO((int arg));
void cm_kbd __PROTO((int arg));
void cm_key __PROTO((int arg));
void cm_ctrl __PROTO((int arg, size_t position));
void cm_sc __PROTO((int arg, size_t start_pos, size_t end_pos));
void cm_var __PROTO((int arg, size_t start_pos, size_t end_pos));
void cm_dfn __PROTO((int arg, size_t position));
void cm_emph __PROTO((int arg));
void cm_strong __PROTO((int arg, size_t position));
void cm_cite __PROTO((int arg, size_t position));
void cm_italic __PROTO((int arg, size_t start, size_t end));
void cm_bold __PROTO((int arg, size_t start, size_t end));
void cm_roman __PROTO((int arg, size_t start, size_t end));
void cm_titlefont __PROTO((int arg, size_t start, size_t end));
void cm_title __PROTO((int arg, size_t start, size_t end));
void cm_refill __PROTO((void));
void cm_w __PROTO((int arg, size_t start, size_t end));
void cm_obsolete __PROTO((int arg, size_t start, size_t end));
void insert_and_underscore __PROTO((int with_char));
void cm_chapter __PROTO((void));
void cm_section __PROTO((void));
void cm_subsection __PROTO((void));
void cm_subsubsection __PROTO((void));
int what_section __PROTO((char *text));
void cm_top __PROTO((void));
void cm_unnumbered __PROTO((void));
void cm_unnumberedsec __PROTO((void));
void cm_unnumberedsubsec __PROTO((void));
void cm_unnumberedsubsubsec __PROTO((void));
void cm_appendix __PROTO((void));
void cm_appendixsec __PROTO((void));
void cm_appendixsubsec __PROTO((void));
void cm_appendixsubsubsec __PROTO((void));
void cm_majorheading __PROTO((void));
void cm_chapheading __PROTO((void));
void cm_heading __PROTO((void));
void cm_subheading __PROTO((void));
void cm_subsubheading __PROTO((void));
void init_tag_table __PROTO((void));
void write_tag_table __PROTO((void));
void write_tag_table_indirect __PROTO((void));
void write_tag_table_internal __PROTO((int indirect_p));
char *get_node_token __PROTO((void));
void normalize_node_name __PROTO((char *string));
TAG_ENTRY *find_node __PROTO((char *name));
void remember_node __PROTO((char *node,
			    char *prev,
			    char *next,
			    char *up,
			    size_t position,
			    size_t line_no,
			    int no_warn));
void cm_node __PROTO((void));
void validate_file __PROTO((char *filename, TAG_ENTRY *tag_table));
int validate __PROTO((char *tag, size_t line, const char *label));
void split_file __PROTO((char *filename, size_t size));
const char *reftype_type_string __PROTO((enum reftype type));
void remember_node_reference __PROTO((char *node, size_t line,
				      enum reftype type));
void validate_other_references __PROTO((NODE_REF *ref_list));
NODE_REF *find_node_reference __PROTO((char *node, NODE_REF *ref_list));
void free_node_references __PROTO((void));
char *glean_node_from_menu __PROTO((int remember_reference));
void cm_menu __PROTO((void));
char *get_xref_token __PROTO((void));
void cm_xref __PROTO((int arg));
void cm_pxref __PROTO((int arg));
void cm_inforef __PROTO((int arg));
void cm_quotation __PROTO((void));
void cm_example __PROTO((void));
void cm_smallexample __PROTO((void));
void cm_lisp __PROTO((void));
void cm_smalllisp __PROTO((void));
void cm_cartouche __PROTO((void));
void cm_format __PROTO((void));
void cm_display __PROTO((void));
void cm_itemize __PROTO((void));
void cm_enumerate __PROTO((void));
void do_enumeration __PROTO((int type, const char *default_string));
void cm_table __PROTO((void));
void cm_ftable __PROTO((void));
void cm_vtable __PROTO((void));
void cm_group __PROTO((void));
void cm_ifinfo __PROTO((void));
void cm_flushleft __PROTO((void));
void cm_flushright __PROTO((void));
void set __PROTO((char *name));
void clear __PROTO((char *name));
int set_p __PROTO((char *name));
void command_name_condition __PROTO((void));
void cm_set __PROTO((void));
void cm_clear __PROTO((void));
void cm_ifset __PROTO((void));
void cm_ifclear __PROTO((void));
void handle_variable __PROTO((int action));
void execute_string __PROTO((const char *format, ...));
void cm_itemx __PROTO((void));
void cm_item __PROTO((void));
void initialize_token_accumulator
    __PROTO((struct token_accumulator *accumulator));
void accumulate_token
    __PROTO((struct token_accumulator *accumulator, char *token));
char *copy_substring __PROTO((char *start, char *end));
int scan_group_in_string __PROTO((char **string_pointer));
char **args_from_string __PROTO((char *string));
void process_defun_args __PROTO((char **defun_args, int auto_var_p));
char *next_nonwhite_defun_arg __PROTO((char ***arg_pointer));
void defun_internal __PROTO((enum insertion_type type, int x_p));
void cm_defun __PROTO((void));
void cm_end __PROTO((void));
void cm_noindent __PROTO((void));
void cm_setfilename __PROTO((void));
void cm_comment __PROTO((void));
void cm_br __PROTO((void));
void cm_sp __PROTO((void));
void cm_settitle __PROTO((void));
void cm_need __PROTO((void));
void cm_headings __PROTO((void));
void cm_center __PROTO((void));
void cm_result __PROTO((int arg));
void cm_expansion __PROTO((int arg));
void cm_equiv __PROTO((int arg));
void cm_print __PROTO((int arg));
void cm_error __PROTO((int arg));
void cm_point __PROTO((int arg));
void cm_exdent __PROTO((void));
void cm_include __PROTO((void));
void cm_infoinclude __PROTO((void));
void misplaced_brace __PROTO((void));
void cm_force_abbreviated_whitespace __PROTO((void));
void cm_ignore_sentence_ender __PROTO((void));
void cm_bye __PROTO((void));
void cm_asis __PROTO((void));
void cm_math __PROTO((void));
void cm_setchapternewpage __PROTO((void));
void cm_smallbook __PROTO((void));
void init_indices __PROTO((void));
int find_index_offset __PROTO((const char *name));
INDEX_ALIST *find_index __PROTO((const char *name));
int translate_index __PROTO((const char *name));
INDEX_ELT *index_list __PROTO((const char *name));
void free_index __PROTO((INDEX_ELT *index));
void undefindex __PROTO((const char *name));
void defindex __PROTO((const char *name, int code));
void index_add_arg __PROTO((const char *name));
void gen_index __PROTO((void));
void cm_defindex __PROTO((void));
void cm_defcodeindex __PROTO((void));
void gen_defindex __PROTO((int code));
INDEX_ELT *index_append __PROTO((INDEX_ELT *head, INDEX_ELT *tail));
void cm_synindex __PROTO((void));
void cm_pindex __PROTO((void));
void cm_vindex __PROTO((void));
void cm_kindex __PROTO((void));
void cm_cindex __PROTO((void));
void cm_findex __PROTO((void));
void cm_tindex __PROTO((void));
int index_element_compare __PROTO((INDEX_ELT **element1,
				   INDEX_ELT **element2));
INDEX_ELT **sort_index __PROTO((INDEX_ELT *index));
void cm_printindex __PROTO((void));
void define_user_command __PROTO((char *name,
				  FUNCTION *proc, int needs_braces_p));
void define_alias __PROTO((char *alias, char *function));
int set_paragraph_indent __PROTO((char *string));
void cm_paragraphindent __PROTO((void));
int set_footnote_style __PROTO((char *string));
void cm_footnotestyle __PROTO((void));
void remember_note __PROTO((char *marker, char *note));
void free_pending_notes __PROTO((void));
void cm_footnote __PROTO((void));
void output_pending_notes __PROTO((void));

#if defined (HAVE_MACROS)
MACRO_DEF *find_macro __PROTO((char *name));
void add_macro __PROTO((char *name,
			char *definition, char *filename, size_t lineno));
MACRO_DEF *delete_macro __PROTO((char *name));
void execute_macro __PROTO((MACRO_DEF *def));
void cm_macro __PROTO((void));
void cm_unmacro __PROTO((void));
#endif /* HAVE_MACROS */

TAG_ENTRY *restore_tag_table __PROTO((char *the_file, size_t size));

#undef __PROTO
#if 0  /* this is replaced */
int
insert_self (), cm_tex (), cm_asterisk (), cm_dots (), cm_bullet (),
cm_TeX (), cm_copyright (), cm_code (), cm_samp (), cm_file (), cm_kbd (),
cm_key (), cm_ctrl (), cm_var (), cm_dfn (), cm_emph (), cm_strong (),
cm_cite (), cm_italic (), cm_bold (), cm_roman (), cm_title (), cm_w (),
cm_refill (), cm_titlefont ();

int
cm_chapter (), cm_unnumbered (), cm_appendix (), cm_top (),
cm_section (), cm_unnumberedsec (), cm_appendixsec (),
cm_subsection (), cm_unnumberedsubsec (), cm_appendixsubsec (),
cm_subsubsection (), cm_unnumberedsubsubsec (), cm_appendixsubsubsec (),
cm_heading (), cm_chapheading (), cm_subheading (), cm_subsubheading (),
cm_majorheading ();

/* All @defxxx commands map to cm_defun (). */
int cm_defun ();

int
  cm_node (), cm_menu (), cm_xref (), cm_ftable (), cm_vtable (), cm_pxref (),
  cm_inforef (), cm_quotation (), cm_display (), cm_itemize (),
  cm_enumerate (), cm_table (), cm_itemx (), cm_noindent (),
  cm_setfilename (), cm_comment (), cm_br (), cm_sp (), cm_page (),
  cm_group (), cm_need (), cm_center (), cm_include (), cm_bye (),
  cm_item (), cm_end (), cm_infoinclude (), cm_ifinfo (), cm_kindex (),
  cm_cindex (), cm_findex (), cm_pindex (), cm_vindex (),
  cm_tindex (), cm_asis (), cm_synindex (), cm_settitle (),
  cm_setchapternewpage (), cm_printindex (), cm_minus (), cm_footnote (),
  cm_force_abbreviated_whitespace (), cm_example (), cm_smallexample (),
  cm_lisp (), cm_format (), cm_exdent (), cm_defindex (), cm_defcodeindex (),
  cm_sc (), cm_result (), cm_expansion (), cm_equiv (), cm_print (),
  cm_error (), cm_point (), cm_smallbook (), cm_headings (), cm_today (),
  cm_flushleft (),  cm_flushright (), cm_smalllisp (), cm_finalout (),
  cm_math (), cm_cartouche (), cm_ignore_sentence_ender ();

/* Conditionals. */
int cm_set (), cm_clear (), cm_ifset (), cm_ifclear ();

#if defined (HAVE_MACROS)
/* Define a user-defined command which is simple substitution. */
int cm_macro (), cm_unmacro ();
#endif /* HAVE_MACROS */

/* Options. */
int cm_paragraphindent (), cm_footnotestyle ();

/* Internals. */
int do_nothing (), command_name_condition ();
int misplaced_brace (), cm_obsolete ();
#endif  /* 0  -- this is replaced */


/* Stuff for defining commands on the fly. */
COMMAND **user_command_array = (COMMAND **) NULL;
int user_command_array_len = 0;

#define NO_BRACE_ARGS 0
#define BRACE_ARGS 1

/* casts added to make gcc happy */
static COMMAND CommandTable[] = {
  { "!", (FUNCTION *)cm_ignore_sentence_ender, NO_BRACE_ARGS },
  { "'", (FUNCTION *)insert_self, NO_BRACE_ARGS },
  { "*", (FUNCTION *)cm_asterisk, NO_BRACE_ARGS },
  { ".", (FUNCTION *)cm_ignore_sentence_ender, NO_BRACE_ARGS },
  { ":", (FUNCTION *)cm_force_abbreviated_whitespace, NO_BRACE_ARGS },
  { "?", (FUNCTION *)cm_ignore_sentence_ender, NO_BRACE_ARGS },
  { "@", (FUNCTION *)insert_self, NO_BRACE_ARGS },
  { " ", (FUNCTION *)insert_self, NO_BRACE_ARGS },
  { "\n", (FUNCTION *)insert_self, NO_BRACE_ARGS },
  { "TeX", (FUNCTION *)cm_TeX, BRACE_ARGS },
  { "`", (FUNCTION *)insert_self, NO_BRACE_ARGS },
  { "appendix", (FUNCTION *)cm_appendix, NO_BRACE_ARGS },
  { "appendixsection", (FUNCTION *)cm_appendixsec, NO_BRACE_ARGS },
  { "appendixsec", (FUNCTION *)cm_appendixsec, NO_BRACE_ARGS },
  { "appendixsubsec", (FUNCTION *)cm_appendixsubsec, NO_BRACE_ARGS },
  { "appendixsubsubsec", (FUNCTION *)cm_appendixsubsubsec, NO_BRACE_ARGS },
  { "asis", (FUNCTION *)cm_asis, BRACE_ARGS },
  { "b", (FUNCTION *)cm_bold, BRACE_ARGS },
  { "br", (FUNCTION *)cm_br, NO_BRACE_ARGS },
  { "bullet", (FUNCTION *)cm_bullet, BRACE_ARGS },
  { "bye", (FUNCTION *)cm_bye, NO_BRACE_ARGS },
  { "c", (FUNCTION *)cm_comment, NO_BRACE_ARGS },
  { "cartouche", (FUNCTION *)cm_cartouche, NO_BRACE_ARGS },
  { "center", (FUNCTION *)cm_center, NO_BRACE_ARGS },
  { "chapheading", (FUNCTION *)cm_chapheading, NO_BRACE_ARGS },
  { "chapter", (FUNCTION *)cm_chapter, NO_BRACE_ARGS },
  { "cindex", (FUNCTION *)cm_cindex, NO_BRACE_ARGS },
  { "cite", (FUNCTION *)cm_cite, BRACE_ARGS },
  { "clear", (FUNCTION *)cm_clear, NO_BRACE_ARGS },
  { "code", (FUNCTION *)cm_code, BRACE_ARGS },
  { "comment", (FUNCTION *)cm_comment, NO_BRACE_ARGS },
  { "contents", (FUNCTION *)do_nothing, NO_BRACE_ARGS },
  { "copyright", (FUNCTION *)cm_copyright, BRACE_ARGS },
  { "ctrl", (FUNCTION *)cm_ctrl, BRACE_ARGS },
  { "defcodeindex", (FUNCTION *)cm_defcodeindex, NO_BRACE_ARGS },
  { "defindex", (FUNCTION *)cm_defindex, NO_BRACE_ARGS },
  { "dfn", (FUNCTION *)cm_dfn, BRACE_ARGS },

/* The `def' commands. */
  { "deffn", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "deffnx", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "defun", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "defunx", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "defmac", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "defmacx", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "defspec", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "defspecx", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "defvr", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "defvrx", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "defvar", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "defvarx", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "defopt", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "defoptx", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "deftypefn", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "deftypefnx", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "deftypefun", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "deftypefunx", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "deftypevr", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "deftypevrx", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "deftypevar", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "deftypevarx", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "defcv", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "defcvx", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "defivar", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "defivarx", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "defop", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "defopx", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "defmethod", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "defmethodx", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "deftypemethod", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "deftypemethodx", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "deftp", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
  { "deftpx", (FUNCTION *)cm_defun, NO_BRACE_ARGS },
/* The end of the `def' commands. */

  { "display", (FUNCTION *)cm_display, NO_BRACE_ARGS },
  { "dots", (FUNCTION *)cm_dots, BRACE_ARGS },
  { "dmn", (FUNCTION *)do_nothing, BRACE_ARGS },
  { "emph", (FUNCTION *)cm_emph, BRACE_ARGS },
  { "end", (FUNCTION *)cm_end, NO_BRACE_ARGS },
  { "enumerate", (FUNCTION *)cm_enumerate, NO_BRACE_ARGS },
  { "equiv", (FUNCTION *)cm_equiv, BRACE_ARGS },
  { "error", (FUNCTION *)cm_error, BRACE_ARGS },
  { "example", (FUNCTION *)cm_example, NO_BRACE_ARGS },
  { "exdent", (FUNCTION *)cm_exdent, NO_BRACE_ARGS },
  { "expansion", (FUNCTION *)cm_expansion, BRACE_ARGS },
  { "file", (FUNCTION *)cm_file, BRACE_ARGS },
  { "findex", (FUNCTION *)cm_findex, NO_BRACE_ARGS },
  { "finalout", (FUNCTION *)do_nothing, NO_BRACE_ARGS },
  { "flushleft", (FUNCTION *)cm_flushleft, NO_BRACE_ARGS },
  { "flushright", (FUNCTION *)cm_flushright, NO_BRACE_ARGS },
  { "format", (FUNCTION *)cm_format, NO_BRACE_ARGS },
  { "ftable", (FUNCTION *)cm_ftable, NO_BRACE_ARGS },
  { "group", (FUNCTION *)cm_group, NO_BRACE_ARGS },
  { "heading", (FUNCTION *)cm_heading, NO_BRACE_ARGS },
  { "headings", (FUNCTION *)cm_headings, NO_BRACE_ARGS },
  { "i", (FUNCTION *)cm_italic, BRACE_ARGS },
  { "iappendix", (FUNCTION *)cm_appendix, NO_BRACE_ARGS },
  { "iappendixsection", (FUNCTION *)cm_appendixsec, NO_BRACE_ARGS },
  { "iappendixsec", (FUNCTION *)cm_appendixsec, NO_BRACE_ARGS },
  { "iappendixsubsec", (FUNCTION *)cm_appendixsubsec, NO_BRACE_ARGS },
  { "iappendixsubsubsec", (FUNCTION *)cm_appendixsubsubsec, NO_BRACE_ARGS },
  { "ichapter", (FUNCTION *)cm_chapter, NO_BRACE_ARGS },
  { "ifclear", (FUNCTION *)cm_ifclear, NO_BRACE_ARGS },
  { "ifinfo", (FUNCTION *)cm_ifinfo, NO_BRACE_ARGS },
  { "ifset", (FUNCTION *)cm_ifset, NO_BRACE_ARGS },
  { "iftex", (FUNCTION *)command_name_condition, NO_BRACE_ARGS },
  { "ignore", (FUNCTION *)command_name_condition, NO_BRACE_ARGS },
  { "include", (FUNCTION *)cm_include, NO_BRACE_ARGS },
  { "inforef", (FUNCTION *)cm_inforef, BRACE_ARGS },
  { "input", (FUNCTION *)cm_include, NO_BRACE_ARGS },
  { "isection", (FUNCTION *)cm_section, NO_BRACE_ARGS },
  { "isubsection", (FUNCTION *)cm_subsection, NO_BRACE_ARGS },
  { "isubsubsection", (FUNCTION *)cm_subsubsection, NO_BRACE_ARGS },
  { "item", (FUNCTION *)cm_item, NO_BRACE_ARGS },
  { "itemize", (FUNCTION *)cm_itemize, NO_BRACE_ARGS },
  { "itemx", (FUNCTION *)cm_itemx, NO_BRACE_ARGS },
  { "iunnumbered", (FUNCTION *)cm_unnumbered, NO_BRACE_ARGS },
  { "iunnumberedsec", (FUNCTION *)cm_unnumberedsec, NO_BRACE_ARGS },
  { "iunnumberedsubsec", (FUNCTION *)cm_unnumberedsubsec, NO_BRACE_ARGS },
  { "iunnumberedsubsubsec", (FUNCTION *)cm_unnumberedsubsubsec, NO_BRACE_ARGS },
  { "kbd", (FUNCTION *)cm_kbd, BRACE_ARGS },
  { "key", (FUNCTION *)cm_key, BRACE_ARGS },
  { "kindex", (FUNCTION *)cm_kindex, NO_BRACE_ARGS },
  { "lisp", (FUNCTION *)cm_lisp, NO_BRACE_ARGS },
  { "macro", (FUNCTION *)cm_macro, NO_BRACE_ARGS },
  { "majorheading", (FUNCTION *)cm_majorheading, NO_BRACE_ARGS },
  { "math", (FUNCTION *)cm_math, BRACE_ARGS },
  { "menu", (FUNCTION *)cm_menu, NO_BRACE_ARGS },
  { "minus", (FUNCTION *)cm_minus, BRACE_ARGS },
  { "need", (FUNCTION *)cm_need, NO_BRACE_ARGS },
  { "node", (FUNCTION *)cm_node, NO_BRACE_ARGS },
  { "noindent", (FUNCTION *)cm_noindent, NO_BRACE_ARGS },
  { "nwnode", (FUNCTION *)cm_node, NO_BRACE_ARGS },
  { "page", (FUNCTION *)do_nothing, NO_BRACE_ARGS },
  { "pindex", (FUNCTION *)cm_pindex, NO_BRACE_ARGS },
  { "point", (FUNCTION *)cm_point, BRACE_ARGS },
  { "print", (FUNCTION *)cm_print, BRACE_ARGS },
  { "printindex", (FUNCTION *)cm_printindex, NO_BRACE_ARGS },
  { "pxref", (FUNCTION *)cm_pxref, BRACE_ARGS },
  { "quotation", (FUNCTION *)cm_quotation, NO_BRACE_ARGS },
  { "r", (FUNCTION *)cm_roman, BRACE_ARGS },
  { "ref", (FUNCTION *)cm_xref, BRACE_ARGS },
  { "refill", (FUNCTION *)cm_refill, NO_BRACE_ARGS },
  { "result", (FUNCTION *)cm_result, BRACE_ARGS },
  { "samp", (FUNCTION *)cm_samp, BRACE_ARGS },
  { "sc", (FUNCTION *)cm_sc, BRACE_ARGS },
  { "section", (FUNCTION *)cm_section, NO_BRACE_ARGS },
  { "set", (FUNCTION *)cm_set, NO_BRACE_ARGS },
  { "setchapternewpage", (FUNCTION *)cm_setchapternewpage, NO_BRACE_ARGS },
  { "setfilename", (FUNCTION *)cm_setfilename, NO_BRACE_ARGS },
  { "settitle", (FUNCTION *)cm_settitle, NO_BRACE_ARGS },
  { "shortcontents", (FUNCTION *)do_nothing, NO_BRACE_ARGS },
  { "smallbook", (FUNCTION *)cm_smallbook, NO_BRACE_ARGS },
  { "smallexample", (FUNCTION *)cm_smallexample, NO_BRACE_ARGS },
  { "smalllisp", (FUNCTION *)cm_smalllisp, NO_BRACE_ARGS },
  { "sp", (FUNCTION *)cm_sp, NO_BRACE_ARGS },
  { "strong", (FUNCTION *)cm_strong, BRACE_ARGS },
  { "subheading", (FUNCTION *)cm_subheading, NO_BRACE_ARGS },
  { "subsection", (FUNCTION *)cm_subsection, NO_BRACE_ARGS },
  { "subsubheading", (FUNCTION *)cm_subsubheading, NO_BRACE_ARGS },
  { "subsubsection", (FUNCTION *)cm_subsubsection, NO_BRACE_ARGS },
  { "summarycontents", (FUNCTION *)do_nothing, NO_BRACE_ARGS },
  { "syncodeindex", (FUNCTION *)cm_synindex, NO_BRACE_ARGS },
  { "synindex", (FUNCTION *)cm_synindex, NO_BRACE_ARGS },
  { "t", (FUNCTION *)cm_title, BRACE_ARGS },
  { "table", (FUNCTION *)cm_table, NO_BRACE_ARGS },
  { "tex", (FUNCTION *)command_name_condition, NO_BRACE_ARGS },
  { "tindex", (FUNCTION *)cm_tindex, NO_BRACE_ARGS },
  { "titlefont", (FUNCTION *)cm_titlefont, BRACE_ARGS },
  { "titlepage", (FUNCTION *)command_name_condition, NO_BRACE_ARGS },
  { "titlespec", (FUNCTION *)command_name_condition, NO_BRACE_ARGS },
  { "today", (FUNCTION *)cm_today, BRACE_ARGS },
  { "top", (FUNCTION *)cm_top, NO_BRACE_ARGS  },
  { "unmacro", (FUNCTION *)cm_unmacro, NO_BRACE_ARGS },
  { "unnumbered", (FUNCTION *)cm_unnumbered, NO_BRACE_ARGS },
  { "unnumberedsec", (FUNCTION *)cm_unnumberedsec, NO_BRACE_ARGS },
  { "unnumberedsubsec", (FUNCTION *)cm_unnumberedsubsec, NO_BRACE_ARGS },
  { "unnumberedsubsubsec", (FUNCTION *)cm_unnumberedsubsubsec, NO_BRACE_ARGS },
  { "var", (FUNCTION *)cm_var, BRACE_ARGS },
  { "vindex", (FUNCTION *)cm_vindex, NO_BRACE_ARGS },
  { "vtable", (FUNCTION *)cm_vtable, NO_BRACE_ARGS },
  { "w", (FUNCTION *)cm_w, BRACE_ARGS },
  { "xref", (FUNCTION *)cm_xref, BRACE_ARGS },
  { "{", (FUNCTION *)insert_self, NO_BRACE_ARGS },
  { "}", (FUNCTION *)insert_self, NO_BRACE_ARGS },

  /* Some obsoleted commands. */
  { "infotop", (FUNCTION *)cm_obsolete, NO_BRACE_ARGS },
  { "infounnumbered", (FUNCTION *)cm_obsolete, NO_BRACE_ARGS },
  { "infounnumberedsec", (FUNCTION *)cm_obsolete, NO_BRACE_ARGS },
  { "infounnumberedsubsec", (FUNCTION *)cm_obsolete, NO_BRACE_ARGS },
  { "infounnumberedsubsubsec", (FUNCTION *)cm_obsolete, NO_BRACE_ARGS },
  { "infoappendix", (FUNCTION *)cm_obsolete, NO_BRACE_ARGS },
  { "infoappendixsec", (FUNCTION *)cm_obsolete, NO_BRACE_ARGS },
  { "infoappendixsubsec", (FUNCTION *)cm_obsolete, NO_BRACE_ARGS },
  { "infoappendixsubsubsec", (FUNCTION *)cm_obsolete, NO_BRACE_ARGS },
  { "infochapter", (FUNCTION *)cm_obsolete, NO_BRACE_ARGS },
  { "infosection", (FUNCTION *)cm_obsolete, NO_BRACE_ARGS },
  { "infosubsection", (FUNCTION *)cm_obsolete, NO_BRACE_ARGS },
  { "infosubsubsection", (FUNCTION *)cm_obsolete, NO_BRACE_ARGS },

  /* Now @include does what this was supposed to. */
  { "infoinclude", (FUNCTION *)cm_infoinclude, NO_BRACE_ARGS },
  { "footnote", (FUNCTION *)cm_footnote, NO_BRACE_ARGS}, /* self-arg eater */
  { "footnotestyle", (FUNCTION *)cm_footnotestyle, NO_BRACE_ARGS },
  { "paragraphindent", (FUNCTION *)cm_paragraphindent, NO_BRACE_ARGS },

  {(const char *) NULL, (FUNCTION *) NULL}, NO_BRACE_ARGS};

int major_version = 1;
int minor_version = 43;

struct option long_options[] =
{
  { "error-limit", 1, 0, 'e' },			/* formerly -el */
  { "fill-column", 1, 0, 'f' },			/* formerly -fc */
  { "footnote-style", 1, 0, 's' },		/* formerly -ft */
  { "no-headers", 0, &no_headers, 1 },		/* Do not output Node: foo */
  { "no-pointer-validate", 0, &validating, 0 }, /* formerly -nv */
  { "no-split", 0, &splitting, 0 },		/* formerly -ns */
  { "no-validate", 0, &validating, 0 },		/* formerly -nv */
  { "no-warn", 0, &print_warnings, 0 },		/* formerly -nw */
  { "number-footnotes", 0, &number_footnotes, 1 },
  { "no-number-footnotes", 0, &number_footnotes, 0 },
  { "output", 1, 0, 'o' },
  { "paragraph-indent", 1, 0, 'p' },		/* formerly -pi */
  { "reference-limit", 1, 0, 'r' },		/* formerly -rl */
  { "verbose", 0, &verbose_mode, 1 },		/* formerly -verbose */
  { "only-split", 0, 0, 'l'},
  { "version", 0, 0, 'V' },
  {NULL, 0, NULL, 0}
};

/* **************************************************************** */
/*								    */
/*			Main ()  Start of code  		    */
/*					        		    */
/* **************************************************************** */

/* For each file mentioned in the command line, process it, turning
   texinfo commands into wonderfully formatted output text. */
int
main (argc, argv)
     int argc;
     char **argv;
{
  extern int errors_printed;
  int c, ind;

  /* The name of this program is the last filename in argv[0]. */
  progname = filename_part (argv[0]);

  /* Parse argument flags from the input line. */
  while ((c = getopt_long (argc, argv, "f:o:p:e:r:s:lV", long_options, &ind))
	 != EOF)
    {
      if (c == 0 && long_options[ind].flag == 0)
	c = long_options[ind].val;

      switch (c)
	{
	  /* User specified fill_column? */
	case 'f':
	  if (sscanf (optarg, "%d", &fill_column) != 1)
	    usage ();
	  break;

	  /* User specified output file? */
	case 'o':
	  command_output_filename = savestring (optarg);
	  break;

	  /* User specified paragraph indent (paragraph_start_index)? */
	case 'p':
	  if (set_paragraph_indent (optarg) < 0)
	    usage ();
	  break;

	  /* User specified error level? */
	case 'e':
	  if (sscanf (optarg, "%d", &max_error_level) != 1)
	    usage ();
	  break;

	  /* User specified reference warning limit? */
	case 'r':
	  if (sscanf (optarg, "%d", &reference_warning_limit) != 1)
	    usage ();
	  break;

	  /* User specified footnote style? */
	case 's':
	  if (set_footnote_style (optarg) < 0)
	    usage ();
	  footnote_style_preset = 1;
	  break;

	  /* User wants only split an info file */
	case 'l':
	  only_split = 1;
	  break;

	  /* User requested version info? */
	case 'V':
	  print_version_info ();
	  exit (NO_ERROR);
	  break;

	case '?':
	  usage ();
	}
    }

  if (optind == argc)
    usage ();
  else if (verbose_mode)
    print_version_info ();

  /* Remaining arguments are file names of texinfo files.
     Convert them, one by one. */
  while (optind != argc)
    convert (argv[optind++]);

  if (errors_printed)
    return (SYNTAX);
  else
    return (NO_ERROR);
}

/* Display the version info of this invocation of Makeinfo. */
void
print_version_info ()
{
#ifndef atarist
  fprintf (stderr, "This is GNU Makeinfo version %d.%d.\n",
	   major_version, minor_version);
#else  /* atarist */
  fprintf (stderr, "This is GNU Makeinfo version %d.%d (Atari ST). \n",
	   major_version, minor_version);
#endif /* atarist */
}

/* **************************************************************** */
/*								    */
/*			Generic Utilities			    */
/*								    */
/* **************************************************************** */

/* Just like malloc, but kills the program in case of fatal error. */
void *
xmalloc (nbytes)
     size_t nbytes;
{
  void *temp = (void *) malloc (nbytes);

  if (nbytes && temp == (void *)NULL)
    memory_error ("xmalloc", nbytes);

  return (temp);
}

/* Like realloc (), but barfs if there isn't enough memory. */
void *
xrealloc (pointer, nbytes)
     void *pointer;
     size_t nbytes;
{
  void *temp;

  if (!pointer)
    temp = (void *)xmalloc (nbytes);
  else
    temp = (void *)realloc (pointer, nbytes);

  if (nbytes && !temp)
    memory_error ("xrealloc", nbytes);

  return (temp);
}

void
memory_error (callers_name, bytes_wanted)
     const char *callers_name;
     size_t bytes_wanted;
{
  char printable_string[80];

  sprintf (printable_string,
	   "Virtual memory exhausted in %s ()!  Needed %lu bytes.",
	   callers_name, bytes_wanted);

  error (printable_string);
  abort ();
}

/* Tell the user how to use this program. */
void
usage ()
{
  fprintf (stderr, "Usage: %s [options] texinfo-file...\n\
\n\
This program accepts as input files of texinfo commands and text\n\
and outputs a file suitable for reading with GNU Info.\n\
\n\
The options are:\n\
`+no-validate' to suppress node cross reference validation.\n\
`+no-warn' to suppress warning messages (errors are still output).\n\
`+no-split' to suppress the splitting of large files.\n\
`+only-split' to split already made single large info file.\n\
`+no-headers' to suppress the output of Node: Foo headers.\n\
`+verbose' to print information about what is being done.\n\
`+version' to print the version number of Makeinfo.\n\
`+paragraph-indent NUM' to set the paragraph indent to NUM (default %d).\n\
`+fill-column NUM' to set the filling column to NUM (default %d).\n\
`+error-limit NUM' to set the error limit to NUM (default %d).\n\
`+reference-limit NUM' to set the reference warning limit to NUM (default %d).\n\
`+footnote-style STYLE' to set the footnote style to STYLE.  STYLE should\n\
  either be `separate' to place footnotes in their own node, or\n\
  `end', to place the footnotes at the end of the node in which they are\n\
   defined (the default).\n\n",
	   progname, paragraph_start_indent,
	   fill_column, max_error_level, reference_warning_limit);
  exit (FATAL);
}

/* **************************************************************** */
/*								    */
/*			Manipulating Lists      		    */
/*					        		    */
/* **************************************************************** */


/* Reverse the chain of structures in LIST.  Output the new head
   of the chain.  You should always assign the output value of this
   function to something, or you will lose the chain. */
GENERIC_LIST *
reverse_list (list)
     register GENERIC_LIST *list;
{
  register GENERIC_LIST *next;
  register GENERIC_LIST *prev = (GENERIC_LIST *) NULL;

  while (list)
    {
      next = list->next;
      list->next = prev;
      prev = list;
      list = next;
    }
  return (prev);
}


/* **************************************************************** */
/*								    */
/*			Pushing and Popping Files       	    */
/*								    */
/* **************************************************************** */

/* Find and load the file named FILENAME.  Return a pointer to
   the loaded file, or NULL if it can't be loaded. */
char *
find_and_load (filename)
     char *filename;
{
  struct stat fileinfo;
  int file, count = 0;
  char *result = (char *) NULL;
#if defined (VMS)
  int n;
#endif

  file = -1; /* we do not want to close a non-existent file */
  if (stat (filename, &fileinfo) != 0)
    goto error_exit;

  file = open (filename, O_RDONLY);
  if (file < 0)
    goto error_exit;

  /* Load the file. */
  result = (char *)xmalloc (fileinfo.st_size);

  /* VMS stat lies about the st_size value.  The actual number of
     readable bytes is always less than this value.  The arcane
     mysteries of VMS/RMS are too much to probe, so this hack
    suffices to make things work. */
#if defined (VMS)
  while ((n = read (file, result+count, fileinfo.st_size)) > 0)
    count += n;
  if (n == -1)
#else
    count = fileinfo.st_size;
#ifndef atarist
    if (read (file, result, fileinfo.st_size) != fileinfo.st_size)
#else  /* atarist  - be careful about sizes!! ints are not good enough */
    if (_read (file, result, fileinfo.st_size) != fileinfo.st_size)
#endif /* atarist */
#endif
  error_exit:
    {
      if (result)
	free (result);
      if (file != -1)
	close (file);
      return ((char *) NULL);
    }
  close (file);

  /* Set the globals to the new file. */
  input_text = result;
#ifndef atarist
  size_of_input_text = fileinfo.st_size;
#else
  /*
   * We have to deal with possible CR/LF line ends.
   * We will toss all that garbage on input and restore it when
   * writing file out - see flush_output()
   */
  {
    char *text_pos = result, *text_end = result + fileinfo.st_size;
    while (text_pos < text_end) {
      if ('\r' != (*result = *text_pos++))
	  result += 1;
    }
    size_of_input_text = result - input_text;
    /* return extra memory back to the system */
    input_text = xrealloc(input_text, size_of_input_text);
  }
#endif /* atarist */
  input_filename = savestring (filename);
  node_filename = savestring (filename);
  input_text_offset = 0;
  line_number = 1;
  return (input_text);
}

/* Save the state of the current input file. */
void
pushfile ()
{
  FSTACK *newstack = (FSTACK *) xmalloc (sizeof (FSTACK));
  newstack->filename = input_filename;
  newstack->text = input_text;
  newstack->size = size_of_input_text;
  newstack->offset = input_text_offset;
  newstack->line_number = line_number;
  newstack->next = filestack;

  filestack = newstack;
  push_node_filename ();
}

/* Make the current file globals be what is on top of the file stack. */
void
popfile ()
{
  extern int executing_string;
  FSTACK *temp = filestack;

  if (!filestack)
    abort ();			/* My fault.  I wonder what I did? */

  /* Make sure that commands with braces have been satisfied. */
  if (!executing_string)
    discard_braces ();

  /* Get the top of the stack into the globals. */
  input_filename = filestack->filename;
  input_text = filestack->text;
  size_of_input_text = filestack->size;
  input_text_offset = filestack->offset;
  line_number = filestack->line_number;

  /* Pop the stack. */
  filestack = filestack->next;
  free (temp);
  pop_node_filename ();
}

/* Flush all open files on the file stack. */
void
flush_file_stack ()
{
  while (filestack)
    {
      free (input_filename);
      free (input_text);
      popfile ();
    }
}

int node_filename_stack_index = 0;
int node_filename_stack_size = 0;
char **node_filename_stack = (char **)NULL;

void
push_node_filename ()
{
  if (node_filename_stack_index + 1 > node_filename_stack_size)
    {
      if (!node_filename_stack)
	node_filename_stack =
	  (char **)xmalloc ((node_filename_stack_size += 10)
			    * sizeof (char *));
      else
	node_filename_stack =
	  (char **)xrealloc (node_filename_stack,
			     (node_filename_stack_size + 10)
			     * sizeof (char *));
    }

  node_filename_stack[node_filename_stack_index] = node_filename;
  node_filename_stack_index++;
}

void
pop_node_filename ()
{
  node_filename = node_filename_stack[--node_filename_stack_index];
}

/* Return just the simple part of the filename; i.e. the
   filename without the path information, or extensions.
   This conses up a new string. */
char *
filename_part (filename)
     char *filename;
{
#ifndef atarist
  register int i = strlen (filename) - 1;
#else  /* atarist - convert to make the code below happy */
  int i;

  (void) dos2unx(filename, filename);
  i = strlen (filename) - 1;
#endif /* atarist */


  while (i && filename[i] != '/')
    i--;
  if (filename[i] == '/')
    i++;

#if defined (REMOVE_OUTPUT_EXTENSIONS)
  result = savestring (&filename[i]);

  /* See if there is an extension to remove.  If so, remove it. */
  if (rindex (result, '.'))
    *(rindex (result, '.')) = '\0';
  return (result);
#else
  return (savestring (&filename[i]));
#endif /* REMOVE_OUTPUT_EXTENSIONS */
}

/* Return the pathname part of filename.  This can be NULL. */
char *
pathname_part (filename)
     char *filename;
{
  char *result = (char *) NULL;
  register size_t i;

  filename = expand_filename (filename, "");

  i = strlen (filename) - 1;

  while (i && filename[i] != '/')
    i--;
  if (filename[i] == '/')
    i++;

  if (i)
    {
      result = (char *)xmalloc (1 + i);
      strncpy (result, filename, i);
      result[i] = '\0';
    }
  free (filename);
  return (result);
}

/* Return the expansion of FILENAME. */
char *
expand_filename (filename, input_name)
     char *filename;
     const char *input_name;
{
#ifdef atarist
  (void) dos2unx(filename, filename);
#endif

  filename = full_pathname (filename);

  if (filename[0] == '.')
    return (filename);

  if (filename[0] != '/' && input_name[0] == '/')
    {
      /* Make it so that relative names work. */
      char *result;
      int i = strlen (input_name) - 1;

      result = (char *)xmalloc (1 + strlen (input_name) + strlen (filename));
      strcpy (result, input_name);

      while (result[i] != '/' && i)
	i--;

      if (result[i] == '/')
	i++;

      strcpy (&result[i], filename);
      free (filename);
      return (result);
    }
  return (filename);
}

/* Return the full path to FILENAME. */
char *
full_pathname (filename)
     char *filename;
{
  int initial_character;

  if (filename && (initial_character = *filename))
    {
      if (initial_character == '/')
	return (savestring (filename));
      if (initial_character != '~')
	{
	  return (savestring (filename));
	}
      else
	{
	  if (filename[1] == '/')
	    {
	      /* Return the concatenation of HOME and the rest of the string. */
	      char *temp_home;
	      char *temp_name;

	      temp_home = (char *) getenv ("HOME");
	      temp_name = (char *)xmalloc (strlen (&filename[2])
					   + 1
					   + temp_home ? strlen (temp_home)
					   : 0);
	      if (temp_home)
		strcpy (temp_name, temp_home);

	      strcat (temp_name, &filename[2]);
	      return (temp_name);
	    }
	  else
	    {
	      struct passwd *user_entry;
	      int i, c;
	      char *username = (char *)xmalloc ((size_t)257);
	      char *temp_name;

	      for (i = 1; c = filename[i]; i++)
		{
		  if (c == '/')
		    break;
		  else
		    username[i - 1] = c;
		}
	      if (c)
		username[i - 1] = '\0';

	      user_entry = getpwnam (username);

	      if (!user_entry)
		return (savestring (filename));

	      temp_name = (char *)xmalloc (1 + strlen (user_entry->pw_dir)
					   + strlen (&filename[i]));
	      strcpy (temp_name, user_entry->pw_dir);
	      strcat (temp_name, &filename[i]);
	      return (temp_name);
	    }
	}
    }
  else
    {
      return (savestring (filename));
    }
}

/* **************************************************************** */
/*								    */
/*			Error Handling				    */
/*								    */
/* **************************************************************** */

/* Number of errors encountered. */
int errors_printed = 0;

/* Print the last error gotten from the file system. */
int
fs_error (filename)
     char *filename;
{
  remember_error ();
  perror (filename);
  return (0);
}

/* Make error routines into someting suitable for a Standard compiler */
#if __STDC__

/* Print an error message, and return false. */
int
error (const char *format, ...)
{
  va_list args;

  remember_error ();
  va_start(args, format);
  vfprintf (STDERR, format, args);
  va_end(args);
  fprintf (STDERR, "\n");
  return ((int) 0);
}

/* Just like error (), but print the line number as well. */
void
line_error (const char *format, ...)
{
  va_list args;

  remember_error ();
  fprintf (STDERR, "%s:%lu: ", input_filename, line_number);
  va_start(args, format);
  vfprintf (STDERR, format, args);
  va_end(args);
  fprintf (STDERR, ".\n");
  return;
/*  return ((int) 0); - nobody is using it! */
}

int
warning (const char *format, ...)
{
  va_list args;

  if (print_warnings)
    {
      fprintf (STDERR, "%s:%lu: Warning: ", input_filename, line_number);
      va_start(args, format);
      vfprintf (STDERR, format, args);
      va_end(args);
      fprintf (STDERR, ".\n");
    }
  return ((int) 0);
}

#else /* not __STDC__ */

/* Print an error message, and return false. */
error (format, arg1, arg2, arg3, arg4, arg5)
     char *format;
{
  remember_error ();
  fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
  fprintf (stderr, "\n");
  return ((int) 0);
}

/* Just like error (), but print the line number as well. */
line_error (format, arg1, arg2, arg3, arg4, arg5)
     char *format;
{
  remember_error ();
  fprintf (stderr, "%s:%lu: ", input_filename, line_number);
  fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
  fprintf (stderr, ".\n");
  return ((int) 0);
}

warning (format, arg1, arg2, arg3, arg4, arg5)
     char *format;
{
  if (print_warnings)
    {
      fprintf (stderr, "%s:%lu: Warning: ", input_filename, line_number);
      fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
      fprintf (stderr, ".\n");
    }
  return ((int) 0);
}
#endif /* __STDC__ */

/* Remember that an error has been printed.  If this is the first
   error printed, then tell them which program is printing them.
   If more than max_error_level have been printed, then exit the
   program. */
void
remember_error ()
{
  errors_printed++;
  if (max_error_level && (errors_printed > max_error_level))
    {
      fprintf (stderr, "Too many errors!  Gave up.\n");
      flush_file_stack ();
      cm_bye ();
      exit (1);
    }
}


/* **************************************************************** */
/*								    */
/*			Hacking Tokens and Strings		    */
/*								    */
/* **************************************************************** */

/* Return the next token as a string pointer.  We cons the
   string. */
char *
read_token ()
{
  size_t i;
  int character;
  char *result;

  /* Hack special case.  If the first character to be read is
     self-delimiting, then that is the command itself. */

  character = curchar ();
  if (self_delimiting (character))
    {
      input_text_offset++;
      result = savestring (" ");
      *result = character;
      return (result);
    }

  for (i = 0; ((input_text_offset != size_of_input_text)
	       && (character = curchar ())
	       && command_char (character));
       i++, input_text_offset++);
  result = (char *)xmalloc (i + 1);
  bcopy (&input_text[input_text_offset - i], result, i);
  result[i] = '\0';
  return (result);
}

/* Return non-zero if CHARACTER is self-delimiting. */
int
self_delimiting (character)
     int character;
{
#ifndef atarist
  return (member (character, "{}:.@*'`,!?; \n"));
#else  /* we do not want to see any CR's in our names */
  return (isspace(character) || member (character, "{}:.@*'`,!?;"));
#endif /* atarist */
}

/* Clear whitespace from the front and end of string. */
void
canon_white (string)
     char *string;
{
  int len = strlen (string);
  int x;

  if (!len)
    return;

  for (x = 0; x < len; x++)
    {
      if (!whitespace (string[x]))
	{
	  if(0 != x)
	    (void)strcpy (string, string + x);
	  break;
	}
    }
  len = strlen (string);
  if (len)
    len--;
  while (len > -1 && cr_or_whitespace (string[len]))
    len--;
  string[len + 1] = '\0';
}

/* Bash STRING, replacing all whitespace with just one space. */
void
fix_whitespace (string)
     char *string;
{
  char *temp = (char *)xmalloc (strlen (string) + 1);
  int string_index = 0;
  int temp_index = 0;
  int c;

  canon_white (string);

  while (string[string_index])
    {
      c = temp[temp_index++] = string[string_index++];
      /*
       * don't expand cr_or_whitespace - it may have different
       * meaning on different operating systems
       */
      if (cr_or_whitespace(c))
	{
	  temp[temp_index - 1] = ' ';
	  while ((c = string[string_index]) && cr_or_whitespace(c))
	    string_index++;
	}
    }
  temp[temp_index] = '\0';
  strcpy (string, temp);
  free (temp);
}

/* Discard text until the desired string is found.  The string is
   included in the discarded text. */
void
discard_until (string)
     const char *string;
{
  size_t temp = search_forward (string, input_text_offset);

  size_t tt = ((long)temp < 0) ? size_of_input_text : temp + strlen (string);
  size_t from = input_text_offset;

  /* Find out what line we are on. */
  while (from != tt)
    if (input_text[from++] == '\n')
      line_number++;

  if ((long)temp < 0)
    {
      input_text_offset = size_of_input_text - strlen (string);

      if (strcmp (string, "\n") != 0)
	{
	  line_error ("Expected `%s'", string);
	  return;
	}
    }
  else
    input_text_offset = temp;

  input_text_offset += strlen (string);
}

/* Read characters from the file until we are at MATCH.
   Place the characters read into STRING.
   On exit input_text_offset is after the match string.
   Return the offset where the string starts. */
int
get_until (match, string)
     const char *match;
     char **string;
{
  size_t len, current_point, x, new_point, tem;

  current_point = x = input_text_offset;
  new_point = search_forward (match, input_text_offset);

  if ((long)new_point < 0)
    new_point = size_of_input_text;
  len = new_point - current_point;

  /* Keep track of which line number we are at. */
  tem = new_point + (strlen (match) - 1);
  while (x != tem)
    if (input_text[x++] == '\n')
      line_number++;

  *string = (char *)xmalloc (len + 1);

  bcopy (&input_text[current_point], *string, len);
  (*string)[len] = '\0';

  /* Now leave input_text_offset in a consistent state. */
  input_text_offset = tem;

  if (input_text_offset > size_of_input_text)
    input_text_offset = size_of_input_text;

  return (new_point);
}

/* Read characters from the file until we are at MATCH or end of line.
   Place the characters read into STRING.  */
void
get_until_in_line (match, string)
     const char *match;
     char **string;
{
  size_t real_bottom, temp;

  real_bottom = size_of_input_text;
  temp = search_forward ("\n", input_text_offset);

  if ((long)temp < 0)
    temp = size_of_input_text;

  size_of_input_text = temp;
  get_until (match, string);
  size_of_input_text = real_bottom;
}

void
get_rest_of_line (string)
     char **string;
{
  get_until ("\n", string);
  canon_white (*string);

  if (curchar () == '\n')	/* as opposed to the end of the file... */
    {
      line_number++;
      input_text_offset++;
    }
}

/* Backup the input pointer to the previous character, keeping track
   of the current line number. */
void
backup_input_pointer ()
{
  if (input_text_offset)
    {
      input_text_offset--;
      if (curchar () == '\n')
	line_number--;
    }
}

/* Read characters from the file until we are at MATCH or closing brace.
   Place the characters read into STRING.  */
void
get_until_in_braces (match, string)
     const char *match;
     char **string;
{
  size_t i, brace = 0;
  size_t match_len = strlen (match);
  char *temp;

  for (i = input_text_offset; i < size_of_input_text; i++)
    {
      if (input_text[i] == '{')
	brace++;
      else if (input_text[i] == '}')
	brace--;
      else if (input_text[i] == '\n')
	line_number++;

      if ((long) brace < 0 ||
	  (brace == 0 && strncmp (input_text + i, match, match_len) == 0))
	break;
    }

  match_len = i - input_text_offset;
  temp = (char *)xmalloc (2 + match_len);
  strncpy (temp, input_text + input_text_offset, match_len);
  temp[match_len] = '\0';
  input_text_offset = i;
  *string = temp;
}

/* **************************************************************** */
/*								    */
/*			Converting the File     		    */
/*								    */
/* **************************************************************** */

/* Convert the file named by NAME.  The output is saved on the file
   named as the argument to the @setfilename command. */
static const char *suffixes[] = {
  "",
  ".texinfo",
  ".texi",
  ".txinfo",
  ".txi",		/* an allowance for 8+3 type file names */
  (const char *)NULL
};

void
convert (name)
     char *name;
{
  char *real_output_filename;
  char *filename;
  register int i;

  init_tag_table ();
  init_internals ();
  if (only_split) {
    split_file (name, (size_t)0);
    return;
  }
  init_paragraph ();
  init_indices ();

  /* Try to load the file specified by NAME.  If the file isn't found, and
     there is no suffix in NAME, then try NAME.texinfo, and NAME.texi. */
  filename = (char *)xmalloc (strlen (name) + 50);
  for (i = 0; suffixes[i]; i++)
    {
      strcpy (filename, name);
      strcat (filename, suffixes[i]);

      if (find_and_load (filename))
	break;

      if (!suffixes[i][0] && rindex (filename, '.'))
	{
	  fs_error (filename);
	  free (filename);
	  return;
	}
    }

  if (!suffixes[i])
    {
      fs_error (name);
      free (filename);
      return;
    }

  input_filename = filename;

  /* Search this file looking for the special string which starts conversion.
     Once found, we may truly begin. */

  input_text_offset = search_forward ("@setfilename", (size_t)0);

  real_output_filename = (char *)NULL;

  if ((long) input_text_offset < 0)
    {
      if (!command_output_filename)
	{
	  error ("No `@setfilename' found in `%s'", name);
	  goto finished;
	}
    }
  else
/*    input_text_offset += strlen ("@setfilename"); */
      input_text_offset += sizeof("@setfilename") - 1;

  if (!command_output_filename)
    get_until ("\n", &output_filename);
  else
    {
      discard_until ("\n");
      real_output_filename = output_filename = command_output_filename;
      command_output_filename = (char *)NULL;
    }

  canon_white (output_filename);
  printf ("Making info file `%s' from `%s'.\n", output_filename, name);

  if (verbose_mode)
    fprintf (stderr, "  The input file contains %lu characters.\n",
	     size_of_input_text);

  if (real_output_filename &&
      strcmp (real_output_filename, "-") == 0)
    {
      output_stream = stdout;
    }
  else
    {
      if (!real_output_filename)
	real_output_filename = expand_filename (output_filename, name);

      output_stream = fopen (real_output_filename, WMODE);
    }

  if (output_stream == NULL)
    {
      fs_error (real_output_filename);
      goto finished;
    }

  /* Make the displayable filename from output_filename.  Only the base
     portion of the filename need be displayed. */
  pretty_output_filename = filename_part (output_filename);

  /* For this file only, count the number of newlines from the top of
     the file to here.  This way, we keep track of line numbers for
     error reporting.  Line_number starts at 1, since the user isn't
     zero-based. */
  {
    int temp = 0;
    line_number = 1;
    while (temp != input_text_offset)
      if (input_text[temp++] == '\n')
	line_number++;
  }

  add_word_args ("This is Info file %s, produced by Makeinfo-%d.%d from ",
		 output_filename, major_version, minor_version);
  add_word_args ("the input file %s.\n", input_filename);
  close_paragraph ();

  reader_loop ();

finished:
  close_paragraph ();
  flush_file_stack ();
  if (output_stream != NULL)
    {
      output_pending_notes ();
      free_pending_notes ();
      if (tag_table != NULL)
	{
	  tag_table = (TAG_ENTRY *) reverse_list ((GENERIC_LIST *)tag_table);
	  write_tag_table ();
	}

      if (output_stream != stdout)
	fclose (output_stream);

      /* If validating, then validate the entire file right now. */
      if (validating)
	validate_file (real_output_filename, tag_table);

      /* This used to test  && !errors_printed.
	 But some files might have legit warnings.  So split anyway.  */

      /* With a big file and a computer without virtual memory
	 we may have not enough of contiguous memory
	 to reload created info file back (it may grow on write out)
	 so free what it is possible to free and restart */
      init_tag_table ();
      for (i = 0; i < defined_indices; i++) {
#ifdef atarist
	/* Bug alert!! - cannot do that on NeXT since init_tag_table()
	   and free_index() clobber memory for each other.
	   I do not know why -- mj */
	free_index(the_indices[i]);
#endif /* atarist */
	free (name_index_alist[i]->name);
	free (name_index_alist[i]);
      }
      free_and_clear ((char **)&the_indices);
      free_and_clear ((char **)&name_index_alist);
      free_and_clear ((char **)&output_paragraph);
      free_and_clear ((char **)&current_node);
      free_and_clear (&command);
      free_and_clear (&input_filename);
      if (splitting)
	split_file (real_output_filename, (size_t)0);
      free_and_clear (&real_output_filename);
      free_and_clear (&output_filename);
    }
}

void
free_and_clear (pointer)
     char **pointer;
{
  if ((*pointer) != (char *) NULL)
    {
      free (*pointer);
      *pointer = (char *) NULL;
    }
}

 /* Initialize some state. */
void
init_internals ()
{
  free_and_clear (&current_node);
  free_and_clear (&output_filename);
  free_and_clear (&command);
  free_and_clear (&input_filename);
  free_node_references ();
  init_insertion_stack ();
  init_brace_stack ();
  command_index = 0;
  in_menu = 0;
}

void
init_paragraph ()
{
  free_and_clear ((char **)&output_paragraph);
  output_paragraph = (unsigned char *)xmalloc (paragraph_buffer_len);
  init_par_parameters ();
}

void
init_par_parameters ()
{
  output_position = 0;
  output_paragraph[0] = '\0';
  output_paragraph_offset = 0;
  output_column = 0;
  paragraph_is_open = 0;
  current_indent = 0;
}

/* Okay, we are ready to start the conversion.  Call the reader on
   some text, and fill the text as it is output.  Handle commands by
   remembering things like open braces and the current file position on a
   stack, and when the corresponding close brace is found, you can call
   the function with the proper arguments. */
void
reader_loop ()
{
  int character;
  int done = 0;
  int dash_count = 0;

  while (!done)
    {
      if (input_text_offset >= size_of_input_text)
	{
	  if (filestack)
	    {
	      free (input_filename);
	      free (input_text);
	      popfile ();
	    }
	  else
	    break;
	}

      character = curchar ();

      if (!in_fixed_width_font &&
	  (character == '\'' || character == '`') &&
	  input_text[input_text_offset + 1] == character)
	{
	  input_text_offset++;
	  character = '"';
	}

      if (character == '-')
	{
	  dash_count++;
	  if (dash_count == 3 && !in_fixed_width_font)
	    {
	      input_text_offset++;
	      continue;
	    }
	}
      else
	{
	  dash_count = 0;
	}

      /* If this is a whitespace character, then check to see if the line
	 is blank.  If so, advance to the carriage return. */
      if (whitespace (character))
	{
	  register size_t i = input_text_offset + 1;

	  while (i < size_of_input_text && whitespace (input_text[i]))
	    i++;

	  if (i == size_of_input_text || input_text[i] == '\n')
	    {
	      if (i == size_of_input_text)
		i--;

	      input_text_offset = i;
	      character = curchar ();
	    }
	}

      if (character == '\n')
	{
	  line_number++;
#ifdef DOTS
	  /*
	   * provide some indicator that we are still alive on slower machine
	   */
          if (0 == (line_number & 0x3fL))
              fprintf(stderr, ".");
#endif

	  if (in_menu && input_text_offset + 1 < size_of_input_text)
	    {
	      char *tem;

	      /* Note that the value of TEM is discarded, since it is
		 gauranteed to be NULL when glean_node_from_menu () is
		 called with a non-zero argument. */
	      tem = glean_node_from_menu (1);
	    }
	}

      switch (character)
	{
	case COMMAND_PREFIX:
	  read_command ();
	  if (strcmp (command, "bye") == 0)
	    {
	      done = 1;
	      continue;
	    }
	  break;

	case '{':

	  /* Special case.  I'm not supposed to see this character by itself.
	     If I do, it means there is a syntax error in the input text.
	     Report the error here, but remember this brace on the stack so
	     you can ignore its partner. */

	  line_error ("Misplaced `{'");
	  remember_brace ((FUNCTION *)misplaced_brace);

	  /* Don't advance input_text_offset since this happens in
	     remember_brace ().
	     input_text_offset++;
           */
	  break;

	case '}':
	  pop_and_call_brace ();
	  input_text_offset++;
	  break;

	default:
	  add_char (character);
	  input_text_offset++;
	}
    }
}

/* Find the command corresponding to STRING.  If the command
   is found, return a pointer to the data structure.  Otherwise
   return (-1). */
COMMAND *
get_command_entry (string)
     char *string;
{
  register int i;

  for (i = 0; CommandTable[i].name; i++)
    if (strcmp (CommandTable[i].name, string) == 0)
      return (&CommandTable[i]);

  /* This command is not in our predefined command table.  Perhaps
     it is a user defined command. */
  for (i = 0; i < user_command_array_len; i++)
    if (user_command_array[i] &&
	(strcmp (user_command_array[i]->name, string) == 0))
      return (user_command_array[i]);

  /* Nope, we never heard of this command. */
  return ((COMMAND *) -1L);
}

/* input_text_offset is right at the command prefix character.
   Read the next token to determine what to do. */
void
read_command ()
{
  COMMAND *entry;
  input_text_offset++;
  free_and_clear (&command);
  command = read_token ();

#if defined (HAVE_MACROS)
  /* Check to see if this command is a macro.  If so, execute it here. */
  {
    MACRO_DEF *def;

    def = find_macro (command);

    if (def)
      {
	execute_macro (def);
	return;
      }
  }
#endif /* HAVE_MACROS */

  entry = get_command_entry (command);

  if ((long) entry == -1L)
    {
      line_error ("Unknown info command `%s'", command);
      return;
    }

  if (entry->argument_in_braces)
    remember_brace (entry->proc);

  (*(entry->proc)) (START);
}

/* Return the string which invokes PROC; a pointer to a function. */
const char *
find_proc_name (proc)
     FUNCTION *proc;
{
  register int i;

  for (i = 0; CommandTable[i].name; i++)
    if (proc == CommandTable[i].proc)
      return (CommandTable[i].name);
  return ("NO_NAME!");
}

void
init_brace_stack ()
{
  brace_stack = (BRACE_ELEMENT *) NULL;
}

void
remember_brace (proc)
     FUNCTION *proc;
{
  if (curchar () != '{')
    line_error ("@%s expected `{..}'", command);
  else
    input_text_offset++;
  remember_brace_1 (proc, output_paragraph_offset);
}

/* Remember the current output position here.  Save PROC
   along with it so you can call it later. */
void
remember_brace_1 (proc, position)
     FUNCTION *proc;
     size_t position;
{
  BRACE_ELEMENT *new = (BRACE_ELEMENT *) xmalloc (sizeof (BRACE_ELEMENT));
  new->next = brace_stack;
  new->proc = proc;
  new->pos = position;
  new->line = line_number;
  brace_stack = new;
}

/* Pop the top of the brace stack, and call the associated function
   with the args END and POS. */

void
pop_and_call_brace ()
{
  BRACE_ELEMENT *temp;
  FUNCTION *proc;
  size_t pos;

  if (brace_stack == (BRACE_ELEMENT *) NULL)
    line_error ("Unmatched close brace");

  pos = brace_stack->pos;
  proc = brace_stack->proc;
  temp = brace_stack->next;
  free (brace_stack);
  brace_stack = temp;
  (*proc) (END, pos, output_paragraph_offset);
}

/* You call discard_braces () when you shouldn't have any braces on the stack.
   I used to think that this happens for commands that don't take arguments
   in braces, but that was wrong because of things like @code{foo @@}.  So now
   I only detect it at the beginning of nodes. */
void
discard_braces ()
{
  size_t temp_line_number = line_number;
  const char *proc_name;

  if (!brace_stack)
    return;

  while (brace_stack)
    {
      line_number = brace_stack->line;
      proc_name = find_proc_name (brace_stack->proc);
      line_error ("@%s missing close brace", proc_name);
      line_number = temp_line_number;
      pop_and_call_brace ();
    }
}

int
get_char_len (character)
     unsigned int character;
{
  /* Return the printed length of the character. */
  int len;

  switch (character)
    {
    case '\t':
      len = (output_column + 8) & 0xf7;
      if (len > fill_column)
	len = fill_column - output_column;
      else
	len = len - output_column;
      break;

    case '\n':
      len = fill_column - output_column;
      break;

    default:
      if (character < ' ')
	len = 2;
      else
	len = 1;
    }
  return (len);
}

#if __STDC__
void
add_word_args (const char *format, ...)
{
  va_list args;
  char buffer[1000];

  va_start(args, format);
  vsprintf (buffer, format, args);
  va_end(args);
  add_word (buffer);
}
#else
void
add_word_args (format, arg1, arg2, arg3, arg4, arg5)
     char *format;
{
  char buffer[1000];
  sprintf (buffer, format, arg1, arg2, arg3, arg4, arg5);
  add_word (buffer);
}
#endif /* __STDC__ */

/* Add STRING to output_paragraph. */
void
add_word (string)
     const char *string;
{
  while (*string)
    add_char (*string++);
}

int last_char_was_newline = 1;
int last_inserted_character = 0;

/* Non-zero means that a newline character has already been
   inserted, so close_paragraph () should insert one less. */
int line_already_broken = 0;

/* Add the character to the current paragraph.  If filling_enabled is
   non-zero, then do filling as well. */
void
add_char (character)
     unsigned int character;
{
  /* If we are avoiding outputting headers, and we are currently
     in a menu, then simply return. */
  if (no_headers && in_menu)
    return;

  /* If we are adding a character now, then we don't have to
     ignore close_paragraph () calls any more. */
  if (must_start_paragraph && character != '\n')
    {
      must_start_paragraph = 0;
      line_already_broken = 0;	/* The line is no longer broken. */
      if (current_indent > output_column)
	{
	  indent (current_indent - output_column);
	  output_column = current_indent;
	}
    }

  if (non_splitting_words && member (character, " \t\n"))
    character = ' ' | 0x80;

  switch (character)
    {
    case '\n':
      if (!filling_enabled)
	{
	  insert ('\n');

	  if (force_flush_right)
	    {
	      close_paragraph ();
	      /* Hack to force single blank lines out in this mode. */
	      flush_output ();
	    }

	  output_column = 0;

	  if (!no_indent && paragraph_is_open)
	    indent (output_column = current_indent);
	  break;
	}
      else
	{
	  if (sentence_ender (last_inserted_character))
	    {
	      insert (' ');
	      output_column++;
	      last_inserted_character = character;
	    }
	}

      if (last_char_was_newline)
	{
	  close_paragraph ();
	  pending_indent = 0;
	}
      else
	{
	  last_char_was_newline = 1;
	  insert (' ');
	  output_column++;
	}
      break;

    default:
      {
	int len = get_char_len (character);

	if ((character == ' ') && (last_char_was_newline))
	  {
	    if (!paragraph_is_open)
	      {
		pending_indent++;
		return;
	      }
	  }

	if (!paragraph_is_open)
	  {
	    start_paragraph ();

	    /* If the paragraph is supposed to be indented a certain way,
	       then discard all of the pending whitespace.  Otherwise, we
	       let the whitespace stay. */
	    if (!paragraph_start_indent)
	      indent (pending_indent);
	    pending_indent = 0;
	  }

	if ((output_column += len) >= fill_column)
	  {
	    if (filling_enabled)
	      {
		size_t temp = output_paragraph_offset;
		while (--temp > 0 && output_paragraph[temp] != '\n')
		  {
		    /* If we have found a space, we have the place to break
		       the line. */
		    if (output_paragraph[temp] == ' ')
		      {
			output_paragraph[temp++] = '\n';

			/* We have correctly broken the line where we want
			   to.  What we don't want is spaces following where
			   we have decided to break the line.  We get rid of
			   them. */
			{
			  size_t t1 = temp;
			  while (t1 < output_paragraph_offset
				 && whitespace (output_paragraph[t1]))
			    t1++;

			  if (t1 != temp)
			    {
			      strncpy ((char *) &output_paragraph[temp],
				       (char *) &output_paragraph[t1],
				       (output_paragraph_offset - t1));
			      output_paragraph_offset -= (t1 - temp);
			    }
			}

			/* Filled, but now indent if that is right. */
			if (indented_fill && current_indent)
			  {
			    size_t buffer_len =
				((output_paragraph_offset - temp)
				 + current_indent);
			    char *temp_buffer = (char *)xmalloc (buffer_len);
			    int indentation = 0;

			    /* We have to shift any markers that are in
			       front of the wrap point. */
			    {
			      register BRACE_ELEMENT *stack = brace_stack;

			      while (stack)
				{
				  if (stack->pos > temp)
				    stack->pos += current_indent;
				  stack = stack->next;
				}
			    }

			    while (current_indent > 0 &&
				   indentation != current_indent)
			      temp_buffer[indentation++] = ' ';

			    strncpy ((char *) &temp_buffer[current_indent],
				     (char *) &output_paragraph[temp],
				     buffer_len - current_indent);

			    if (output_paragraph_offset + buffer_len
				>= paragraph_buffer_len)
			      {
				unsigned char *tt = xrealloc
				  (output_paragraph,
				   (paragraph_buffer_len += buffer_len));
				output_paragraph = tt;
			      }
			    strncpy ((char *) &output_paragraph[temp],
				     temp_buffer, buffer_len);
			    output_paragraph_offset += current_indent;
			    free (temp_buffer);
			  }
			output_column = 0;
			while (temp < output_paragraph_offset)
			  output_column +=
			    get_char_len (output_paragraph[temp++]);
			output_column += len;
			break;
		      }
		  }
	      }
	  }
	insert (character);
	line_already_broken = 0;
	last_char_was_newline = 0;
	last_inserted_character = character;
      }
    }
}

/* Insert CHARACTER into OUTPUT_PARAGRAPH. */
void
insert (character)
     int character;
{
  output_paragraph[output_paragraph_offset++] = character;
  if (output_paragraph_offset == paragraph_buffer_len)
    {
      output_paragraph =
	xrealloc (output_paragraph, (paragraph_buffer_len += 100));
    }
}

/* Remove upto COUNT characters of whitespace from the
   the current output line.  If COUNT is less than zero,
   then remove until none left. */
void
kill_self_indent (count)
     int count;
{
  /* Handle infinite case first. */
  if (count < 0)
    {
      output_column = 0;
      while (output_paragraph_offset)
	{
	  if (whitespace (output_paragraph[output_paragraph_offset - 1]))
	    output_paragraph_offset--;
	  else
	    break;
	}
    }
  else
    {
      while (output_paragraph_offset && count--)
	if (whitespace (output_paragraph[output_paragraph_offset - 1]))
	  output_paragraph_offset--;
	else
	  break;
    }
}

void
flush_output ()
{
  register size_t i;
  register unsigned char cc;
#ifdef atarist
  register size_t j = 0;
#endif

  if (!output_paragraph_offset)
    return;
 
  i = 0;
  while (i < output_paragraph_offset)
    {
      cc = output_paragraph[i];
      if (cc == (unsigned char)(' ' | 0x80) ||
	  cc == (unsigned char)('\t' | 0x80) ||
	  cc == (unsigned char)('\n' | 0x80) ||
	  sentence_ender (UNMETA (cc)))
        {
	  cc &= 0x7f;
	  output_paragraph[i] = cc;
        }
#ifdef atarist
      if (cc == '\n')
        {
	  /* Restore CR/LF brain damage */
	  if ((i - j) != fwrite (&output_paragraph[j], (size_t)1,
				 (i - j), output_stream) ||
	      /* in 'text' mode this really writes '\r\n' */
	      EOF == fputc  ('\n', output_stream)) {
	    perror (output_filename);
	    exit (FATAL);
	  }
	  i += 1;
	  j = i;
        }
      else
	  i += 1;
#else
      i += 1;
#endif      
    }
#ifndef atarist
  if (output_paragraph_offset != fwrite (output_paragraph, 1,
					 output_paragraph_offset,
					 output_stream)) {
    perror (output_filename);
    exit (FATAL);
  }
#else
  /* flush the remainder in case when the last character in
   * output_paragraph was not '\n' */
  if ((i - j) != fwrite (&output_paragraph[j], (size_t)1,
			 i - j, output_stream)) {
    perror (output_filename);
    exit (FATAL);
  }
#endif  /* atarist */
  output_position += i; /* i is equal now to output_paragraph_offset */
  output_paragraph_offset = 0;
}

/* How to close a paragraph controlling the number of lines between
   this one and the last one. */

/* Paragraph spacing is controlled by this variable.  It is the number of
   blank lines that you wish to appear between paragraphs.  A value of
   1 creates a single blank line between paragraphs. */
int paragraph_spacing = DEFAULT_PARAGRAPH_SPACING;

/* Close the current paragraph, leaving no blank lines between them. */
void
close_single_paragraph ()
{
  close_paragraph_with_lines (0);
}

/* Close a paragraph after an insertion has ended. */
int insertion_paragraph_closed = 0;

void
close_insertion_paragraph ()
{
  if (!insertion_paragraph_closed)
    {
      /* Close the current paragraph, breaking the line. */
      close_single_paragraph ();

      /* Start a new paragraph here, inserting whatever indention is correct
	 for the now current insertion level (one above the one that we are
	 ending). */
      start_paragraph ();

      /* Tell close_paragraph () that the previous line has already been
	 broken, so it should insert one less newline. */
      line_already_broken = 1;

      /* Let functions such as add_char () know that we have already found a
	 newline. */
      ignore_blank_line ();
    }

  insertion_paragraph_closed = 1;
}

void
close_paragraph_with_lines (lines)
     int lines;
{
  int old_spacing = paragraph_spacing;
  paragraph_spacing = lines;
  close_paragraph ();
  paragraph_spacing = old_spacing;
}

/* Close the currently open paragraph. */
void
close_paragraph ()
{
  register int i;

  /* The insertion paragraph is no longer closed. */
  insertion_paragraph_closed = 0;

  if (paragraph_is_open && !must_start_paragraph)
    {
      register size_t tindex;
      register int c;

      tindex = output_paragraph_offset;

      /* Back up to last non-newline/space character, forcing all such
	 subsequent characters to be newlines.  This isn't strictly
	 necessary, but a couple of functions use the presence of a newline
	 to make decisions. */
      for (tindex = output_paragraph_offset - 1; (long) tindex >= 0; --tindex)
	{
	  c = output_paragraph[tindex];

	  if (c == ' '|| c == '\n')
	    output_paragraph[tindex] = '\n';
	  else
	    break;
	}

      /* All trailing whitespace is ignored. */
      output_paragraph_offset = ++tindex;

      /* Break the line if that is appropriate. */
      if (paragraph_spacing >= 0)
	insert ('\n');

      /* Add as many blank lines as is specified in PARAGRAPH_SPACING. */
      if (!force_flush_right)
	{
	  for (i = 0; i < (paragraph_spacing - line_already_broken); i++)
	    insert ('\n');
	}

      /* If we are doing flush right indentation, then do it now
	 on the paragraph (really a single line). */
      if (force_flush_right)
	do_flush_right_indentation ();

      flush_output ();
      paragraph_is_open = 0;
      no_indent = 0;
      output_column = 0;
    }
  ignore_blank_line ();
}

/* Make the last line just read look as if it were only a newline. */
void
ignore_blank_line ()
{
  last_inserted_character = '\n';
  last_char_was_newline = 1;
}

/* Align the end of the text in output_paragraph with fill_column. */
void
do_flush_right_indentation ()
{
  char *temp;
  size_t temp_len;

  kill_self_indent (-1);

  if (output_paragraph[0] != '\n')
    {
      output_paragraph[output_paragraph_offset] = '\0';

      if (output_paragraph_offset < (size_t)fill_column)
	{
	  register int i;

	  if ((size_t)fill_column >= paragraph_buffer_len)
	    output_paragraph =
	      xrealloc (output_paragraph,
			(paragraph_buffer_len += (size_t)fill_column));

	  temp_len = strlen (output_paragraph);
	  temp = (char *)xmalloc (temp_len + 1);
	  bcopy ((char *)output_paragraph, temp, temp_len);

	  for (i = 0; i < (size_t)fill_column - output_paragraph_offset; i++)
	    output_paragraph[i] = ' ';

	  bcopy (temp, (char *)output_paragraph + i, temp_len);
	  free (temp);
	  output_paragraph_offset = (size_t)fill_column;
	}
    }
}

/* Begin a new paragraph. */
void
start_paragraph ()
{
  /* First close existing one. */
  if (paragraph_is_open)
    close_paragraph ();

  /* In either case, the insertion paragraph is no longer closed. */
  insertion_paragraph_closed = 0;

  /* However, the paragraph is open! */
  paragraph_is_open = 1;

  /* If we MUST_START_PARAGRAPH, that simply means that start_paragraph ()
     had to be called before we would allow any other paragraph operations
     to have an effect. */
  if (!must_start_paragraph)
    {
      int amount_to_indent = 0;

      /* If doing indentation, then insert the appropriate amount. */
      if (!no_indent)
	{
	  if (inhibit_paragraph_indentation)
	    {
	      amount_to_indent = current_indent;
	      if (!filling_enabled) {
		  amount_to_indent += pending_indent;
		  pending_indent = 0;
	      }
	      if (inhibit_paragraph_indentation < 0)
		inhibit_paragraph_indentation++;
	    }
	  else if (paragraph_start_indent < 0)
	    amount_to_indent = current_indent;
	  else
	    amount_to_indent = current_indent + paragraph_start_indent;

	  if (amount_to_indent >= output_column)
	    {
	      amount_to_indent -= output_column;
	      indent (amount_to_indent);
	      output_column += amount_to_indent;
	    }
	}
    }
  else
    must_start_paragraph = 0;
}

/* Insert the indentation specified by AMOUNT. */
void
indent (amount)
     int amount;
{
  while (--amount >= 0)
    insert (' ');
}

/* Search forward for STRING in input_text.
   FROM says where where to start. */
size_t
search_forward (string, from)
     const char *string;
     size_t from;
{
  size_t len = strlen (string);

  while (from < size_of_input_text)
    {
      if (strnicmp (input_text + from, string, len) == 0)
	return (from);
      from++;
    }
  return (-1);
}

/* Whoops, Unix doesn't have stricmp, or strnicmp. */

/* Case independent string compare. */
int
stricmp (string1, string2)
     const char *string1, *string2;
{
  char ch1, ch2;

  for (;;)
    {
      ch1 = *string1++;
      ch2 = *string2++;
      if (!(ch1 | ch2))
	return (0);

      ch1 = coerce_to_upper (ch1);
      ch2 = coerce_to_upper (ch2);

      if (ch1 != ch2)
	return (1);
    }
}

/* Compare at most COUNT characters from string1 to string2.  Case
   doesn't matter. */
int
strnicmp (string1, string2, count)
     const char *string1, *string2;
     size_t count;
{
  char ch1, ch2;

  while (count)
    {
      ch1 = *string1++;
      ch2 = *string2++;
      if (coerce_to_upper (ch1) == coerce_to_upper (ch2))
	count--;
      else
	break;
    }
  return ((int)count);
}

/***
enum insertion_type { menu, quotation, lisp, smalllisp, example,
  smallexample, display, itemize, format, enumerate, cartouche, table,
  ftable, vtable, group, ifinfo, flushleft, flushright, ifset, ifclear, deffn,
  defun, defmac, defspec, defvr, defvar, defopt, deftypefn,
  deftypefun, deftypevr, deftypevar, defcv, defivar, defop, defmethod,
  deftypemethod, deftp, bad_type };
***/

const char *insertion_type_names[] = { "menu", "quotation", "lisp",
  "smalllisp", "example", "smallexample", "display", "itemize",
  "format", "enumerate", "cartouche", "table", "ftable", "vtable", "group",
  "ifinfo", "flushleft", "flushright", "ifset", "ifclear", "deffn",
  "defun", "defmac", "defspec", "defvr", "defvar", "defopt",
  "deftypefn", "deftypefun", "deftypevr", "deftypevar", "defcv",
  "defivar", "defop", "defmethod", "deftypemethod", "deftp",
  "bad_type" };

int insertion_level = 0;
typedef struct istack_elt
{
  struct istack_elt *next;
  char *item_function;
  int line_number;
  int filling_enabled;
  int indented_fill;
  enum insertion_type insertion;
  int inhibited;
} INSERTION_ELT;

INSERTION_ELT *insertion_stack = (INSERTION_ELT *) NULL;

void
init_insertion_stack ()
{
  insertion_stack = (INSERTION_ELT *) NULL;
}

/* Return the type of the current insertion. */
enum insertion_type
current_insertion_type ()
{
  if (!insertion_level)
    return (bad_type);
  else
    return (insertion_stack->insertion);
}

/* Return a pointer to the string which is the function
   to wrap around items. */
char *
current_item_function ()
{
  if (!insertion_level)
    return ((char *) NULL);
  else
    return (insertion_stack->item_function);
}

char *
get_item_function ()
{
  char *item_function;
  get_rest_of_line (&item_function);
  backup_input_pointer ();
  canon_white (item_function);
  return (item_function);
}

 /* Push the state of the current insertion on the stack. */
void
push_insertion (type, item_function)
     enum insertion_type type;
     char *item_function;
{
  INSERTION_ELT *new = (INSERTION_ELT *) xmalloc (sizeof (INSERTION_ELT));

  new->item_function = item_function;
  new->filling_enabled = filling_enabled;
  new->indented_fill = indented_fill;
  new->insertion = type;
  new->line_number = line_number;
  new->inhibited = inhibit_paragraph_indentation;
  new->next = insertion_stack;
  insertion_stack = new;
  insertion_level++;
}

 /* Pop the value on top of the insertion stack into the
    global variables. */
void
pop_insertion ()
{
  INSERTION_ELT *temp = insertion_stack;

  if (temp == (INSERTION_ELT *) NULL)
    return;

  inhibit_paragraph_indentation = temp->inhibited;
  filling_enabled = temp->filling_enabled;
  indented_fill = temp->indented_fill;
  free_and_clear (&(temp->item_function));
  insertion_stack = insertion_stack->next;
  free (temp);
  insertion_level--;
}

 /* Return a pointer to the print name of this
    enumerated type. */
const char *
insertion_type_pname (type)
     enum insertion_type type;
{
  if ((int) type < (int) bad_type)
    return (insertion_type_names[(int) type]);
  else
    return ("Broken-Type in insertion_type_pname");
}

/* Return the insertion_type associated with NAME.
   If the type is not one of the known ones, return BAD_TYPE. */
enum insertion_type
find_type_from_name (name)
     char *name;
{
  int index = 0;
  while (index < (int) bad_type)
    {
      if (stricmp (name, insertion_type_names[index]) == 0)
	return (enum insertion_type) index;
      index++;
    }
  return (bad_type);
}

void
do_nothing ()
{
}

int
defun_insertion (type)
     enum insertion_type type;
{
  return
    ((type == deffn)
     || (type == defun)
     || (type == defmac)
     || (type == defspec)
     || (type == defvr)
     || (type == defvar)
     || (type == defopt)
     || (type == deftypefn)
     || (type == deftypefun)
     || (type == deftypevr)
     || (type == deftypevar)
     || (type == defcv)
     || (type == defivar)
     || (type == defop)
     || (type == defmethod)
     || (type == deftypemethod)
     || (type == deftp));
}

/* MAX_NS is the maximum nesting level for enumerations.  I picked 100
   which seemed reasonable.  This doesn't control the number of items,
   just the number of nested lists. */
#define max_stack_depth 100
#define ENUM_DIGITS 1
#define ENUM_ALPHA  2
typedef struct {
  int enumtype;
  int enumval;
} DIGIT_ALPHA;

DIGIT_ALPHA enumstack[max_stack_depth];
int enumstack_offset = 0;
int current_enumval = 1;
int current_enumtype = ENUM_DIGITS;
char *enumeration_arg = (char *)NULL;

void
start_enumerating (at, type)
     int at, type;
{
  if ((enumstack_offset + 1) == max_stack_depth)
    {
      line_error ("Enumeration stack overflow");
      return;
    }
  enumstack[enumstack_offset].enumtype = current_enumtype;
  enumstack[enumstack_offset].enumval = current_enumval;
  enumstack_offset++;
  current_enumval = at;
  current_enumtype = type;
}

void
stop_enumerating ()
{
  --enumstack_offset;
  if (enumstack_offset < 0)
    enumstack_offset = 0;

  current_enumval = enumstack[enumstack_offset].enumval;
  current_enumtype = enumstack[enumstack_offset].enumtype;
}

/* Place a letter or digits into the output stream. */
void
enumerate_item ()
{
  char temp[10];

  if (current_enumtype == ENUM_ALPHA)
    {
      if (current_enumval == ('z' + 1) || current_enumval == ('Z' + 1))
	{
	  current_enumval = ((current_enumval - 1) == 'z' ? 'a' : 'A');
	  warning ("Lettering overflow, restarting at %c", current_enumval);
	}
      sprintf (temp, "%c. ", current_enumval);
    }
  else
    sprintf (temp, "%d. ", current_enumval);

  indent (output_column += (current_indent - strlen (temp)));
  add_word (temp);
  current_enumval++;
}

/* This is where the work for all the "insertion" style
   commands is done.  A huge switch statement handles the
   various setups, and generic code is on both sides. */
void
begin_insertion (type)
     enum insertion_type type;
{
  int no_discard = 0;

  if (defun_insertion (type))
    {
      push_insertion (type, savestring (""));
      no_discard++;
    }
  else
    push_insertion (type, get_item_function ());

  switch (type)
    {
    case menu:
      close_paragraph ();

      filling_enabled = no_indent = 0;
      inhibit_paragraph_indentation = 1;

      if (!no_headers)
	add_word ("* Menu:\n");

      in_menu++;
      no_discard++;
      break;

      /* I think @quotation is meant to do filling.
	 If you don't want filling, then use @example. */
    case quotation:
      close_single_paragraph ();
      last_char_was_newline = no_indent = 0;
      indented_fill = filling_enabled = 1;
      inhibit_paragraph_indentation = 1;
      current_indent += default_indentation_increment;
      break;

    case display:
    case example:
    case smallexample:
    case lisp:
    case smalllisp:
      /* Just like @example, but no indentation. */
    case format:

      close_single_paragraph ();
      inhibit_paragraph_indentation = 1;
      in_fixed_width_font++;
      filling_enabled = 0;
      last_char_was_newline = 0;

      if (type != format)
	current_indent += default_indentation_increment;

      break;

    case table:
    case ftable:
    case vtable:
    case itemize:
      close_single_paragraph ();
      current_indent += default_indentation_increment;
      filling_enabled = indented_fill = 1;
#if defined (INDENT_PARAGRAPHS_IN_TABLE)
      inhibit_paragraph_indentation = 0;
#else
      inhibit_paragraph_indentation = 1;
#endif /* !INDENT_PARAGRAPHS_IN_TABLE */

      /* Make things work for losers who forget the itemize syntax. */
      if (type == itemize)
	{
	  if (!(*insertion_stack->item_function))
	    {
	      free (insertion_stack->item_function);
	      insertion_stack->item_function = savestring ("@bullet");
	    }
	}
      break;

    case enumerate:
      close_single_paragraph ();
      inhibit_paragraph_indentation = no_indent = 0;
      current_indent += default_indentation_increment;
      filling_enabled = indented_fill = 1;

      if (isdigit (*enumeration_arg))
	start_enumerating (atoi (enumeration_arg), ENUM_DIGITS);
      else
	start_enumerating (*enumeration_arg, ENUM_ALPHA);
      break;

      /* Does nothing special in makeinfo. */
    case group:
      close_single_paragraph ();
      break;

      /* Insertions that are no-ops in info, but do something in TeX. */
    case ifinfo:
    case ifset:
    case ifclear:
    case cartouche:
      break;

    case deffn:
    case defun:
    case defmac:
    case defspec:
    case defvr:
    case defvar:
    case defopt:
    case deftypefn:
    case deftypefun:
    case deftypevr:
    case deftypevar:
    case defcv:
    case defivar:
    case defop:
    case defmethod:
    case deftypemethod:
    case deftp:
      inhibit_paragraph_indentation = 1;
      filling_enabled = indented_fill = 1;
      current_indent += default_indentation_increment;
      no_indent = 0;
      break;

    case flushleft:
      close_single_paragraph ();
      inhibit_paragraph_indentation = 1;
      filling_enabled = indented_fill = no_indent = 0;
      break;

    case flushright:
      close_single_paragraph ();
      filling_enabled = indented_fill = no_indent = 0;
      inhibit_paragraph_indentation = 1;
      force_flush_right++;
      break;

    default:
      break;
    }

  if (!no_discard)
    discard_until ("\n");
}

/* Try to end the insertion with the specified TYPE.
   TYPE, with a value of bad_type,  gets translated to match
   the value currently on top of the stack.
   Otherwise, if TYPE doesn't match the top of the insertion stack,
   give error. */
void
end_insertion (type)
     enum insertion_type type;
{
  enum insertion_type temp_type;

  if (!insertion_level)
    return;

  temp_type = current_insertion_type ();

  if (type == bad_type)
    type = temp_type;

  if (type != temp_type)
    {
      line_error ("Expected `%s', but saw `%s'.",
	 insertion_type_pname (temp_type), insertion_type_pname (type));
      return;
    }

  pop_insertion ();

  switch (type)
    {
      /* "Insertions which have no effect on paragraph formatting. */
    case ifinfo:
    case ifset:
    case ifclear:
      break;

    case menu:
      in_menu--;		/* No longer hacking menus. */
      close_insertion_paragraph ();
      break;

    case enumerate:
      stop_enumerating ();
      close_insertion_paragraph ();
      current_indent -= default_indentation_increment;
      break;

    case flushleft:
    case group:
    case cartouche:
      close_insertion_paragraph ();
      break;

    case format:
    case display:
    case example:
    case smallexample:
    case lisp:
    case smalllisp:
    case quotation:

      /* @quotation is the only one of the above without a fixed width
	 font. */
      if (type != quotation)
	in_fixed_width_font--;

      /* @format is the only fixed_width insertion without a change
	 in indentation. */
      if (type != format)
	current_indent -= default_indentation_increment;

      /* The ending of one of these insertions always marks the
	 start of a new paragraph. */
      close_insertion_paragraph ();
      break;

    case table:
    case ftable:
    case vtable:
    case itemize:
      current_indent -= default_indentation_increment;
      break;

    case flushright:
      force_flush_right--;
      close_insertion_paragraph ();
      break;

      /* Handle the @defun style insertions with a default clause. */
    default:
      current_indent -= default_indentation_increment;
      close_insertion_paragraph ();
      break;
    }
}

/* Insertions cannot cross certain boundaries, such as node beginnings.  In
   code that creates such boundaries, you should call discard_insertions ()
   before doing anything else.  It prints the errors for you, and cleans up
   the insertion stack. */
void
discard_insertions ()
{
  size_t real_line_number = line_number;
  while (insertion_stack)
    {
      if (insertion_stack->insertion == ifinfo ||
	  insertion_stack->insertion == ifset ||
	  insertion_stack->insertion == ifclear ||
	  insertion_stack->insertion == cartouche)
	break;
      else
	{
	  char *offender = (char *)
	    insertion_type_pname (insertion_stack->insertion);

	  line_number = insertion_stack->line_number;
	  line_error ("This `%s' doesn't have a matching `%cend %s'",
			    offender, COMMAND_PREFIX, offender);
	  pop_insertion ();
	}
    }
  line_number = real_line_number;
}

/* The actual commands themselves. */

/* Commands which insert themselves. */
void
insert_self ()
{
  add_word (command);
}

/* Force line break */
void
cm_asterisk ()
{
  /* Force a line break in the output. */
  insert ('\n');
  indent (output_column = current_indent);
}

/* Insert ellipsis. */
void
cm_dots (arg)
     int arg;
{
  if (arg == START)
    add_word ("...");
}

void
cm_bullet (arg)
     int arg;
{
  if (arg == START)
    add_char ('*');
}

void
cm_minus (arg)
     int arg;
{
  if (arg == START)
    add_char ('-');
}

/* Insert "TeX". */
void
cm_TeX (arg)
     int arg;
{
  if (arg == START)
    add_word ("TeX");
}

void
cm_copyright (arg)
     int arg;
{
  if (arg == START)
    add_word ("(C)");
}

void
cm_today (arg)
     int arg;
{
  static const char * months [12] =
    { "January", "February", "March", "April", "May", "June", "July",
	"August", "September", "October", "November", "December" };
  if (arg == START)
    {
      long timer = (time (0));
      struct tm * ts = (localtime (&timer));
      add_word_args
	("%d %s %d",
	 (ts -> tm_mday),
	 (months [ts -> tm_mon]),
	 ((ts -> tm_year) + 1900));
    }
}

void
cm_code (arg)
     int arg;
{
  extern int printing_index;

  if (printing_index)
    return;

  if (arg == START)
    {
      in_fixed_width_font++;
      add_char ('`');
    }
  else
    {
      add_word ("'");
      in_fixed_width_font--;
    }
}

void
cm_samp (arg)
     int arg;
{
  cm_code (arg);
}

void
cm_file (arg)
     int arg;
{
  cm_code (arg);
}

void
cm_kbd (arg)
     int arg;
{
  cm_code (arg);
}

void
cm_key (arg)
     int arg;
{
}

/* Convert the character at position into CTL. */
void
cm_ctrl (arg, position)
     int arg;
     size_t position;
{
  if (arg == END)
    output_paragraph[position - 1] = CTL (output_paragraph[position]);
}

/* Small Caps in makeinfo just does all caps. */
void
cm_sc (arg, start_pos, end_pos)
     int arg;
     size_t start_pos, end_pos;
{
  if (arg == END)
    {
      while (start_pos < end_pos)
	{
	  output_paragraph[start_pos] =
	    coerce_to_upper (output_paragraph[start_pos]);
	  start_pos++;
	}
    }
}

/* @var in makeinfo just uppercases the text. */
void
cm_var (arg, start_pos, end_pos)
     int arg;
     size_t start_pos, end_pos;
{
  if (arg == END)
    {
      while (start_pos < end_pos)
	{
	  output_paragraph[start_pos] =
	    coerce_to_upper (output_paragraph[start_pos]);
	  start_pos++;
	}
    }
}

void
cm_dfn (arg, position)
     int arg;
     size_t position;
{
  add_char ('"');
}

void
cm_emph (arg)
     int arg;
{
  add_char ('*');
}

void
cm_strong (arg, position)
     int arg;
     size_t position;
{
  cm_emph (arg);
}

void
cm_cite (arg, position)
     int arg;
     size_t position;
{
  if (arg == START)
    add_word ("`");
  else
    add_word ("'");
}

/* Current text is italicized. */
void
cm_italic (arg, start, end)
     int arg;
     size_t start, end;
{
}

/* Current text is highlighted. */
void
cm_bold (arg, start, end)
     int arg;
     size_t start, end;
{
  cm_italic (arg, start, end);
}

/* Current text is in roman font. */
void
cm_roman (arg, start, end)
     int arg;
     size_t start, end;
{
}

/* Current text is in roman font. */
void
cm_titlefont (arg, start, end)
     int arg;
     size_t start, end;
{
}

/* Italicize titles. */
void
cm_title (arg, start, end)
     int arg;
     size_t start, end;
{
  cm_italic (arg, start, end);
}

/* @refill is a NOP. */
void
cm_refill ()
{
}

/* Prevent the argument from being split across two lines. */
void
cm_w (arg, start, end)
     int arg;
     size_t start, end;
{
  if (arg == START)
    non_splitting_words++;
  else
    non_splitting_words--;
}


/* Explain that this command is obsolete, thus the user shouldn't
   do anything with it. */
void
cm_obsolete (arg, start, end)
     int arg;
     size_t start, end;
{
  if (arg == START)
    warning ("The command `@%s' is obsolete", command);
}

/* Insert the text following input_text_offset up to the end of the line
   in a new, separate paragraph.  Directly underneath it, insert a
   line of WITH_CHAR, the same length of the inserted text. */
void
insert_and_underscore (with_char)
     int with_char;
{
  size_t len, i;
  int old_no_indent;
  char *temp;

  close_paragraph ();
  filling_enabled =  indented_fill = 0;
  old_no_indent = no_indent;
  no_indent = 1;
  get_rest_of_line (&temp);

  len = output_position;
  execute_string ("%s\n", temp);
  free (temp);

  len = ((output_position + output_paragraph_offset) - 1) - len;
  for (i = 0; i < len; i++)
    add_char (with_char);
  insert ('\n');
  close_paragraph ();
  filling_enabled = 1;
  no_indent = old_no_indent;
}

/* The remainder of the text on this line is a chapter heading. */
void
cm_chapter ()
{
  insert_and_underscore ('*');
}

/* The remainder of the text on this line is a section heading. */
void
cm_section ()
{
  insert_and_underscore ('=');
}

/* The remainder of the text on this line is a subsection heading. */
void
cm_subsection ()
{
  insert_and_underscore ('-');
}

/* The remainder of the text on this line is a subsubsection heading. */
void
cm_subsubsection ()
{
  insert_and_underscore ('.');
}

/* Here is a structure which associates sectioning commands with
   an integer, hopefully to reflect the `depth' of the current
   section. */
struct {
  const char *name;
  int level;
} section_alist[] = {
  { "unnumberedsubsubsec", 5 },
  { "unnumberedsubsec", 4 },
  { "unnumberedsec", 3 },
  { "unnumbered", 2 },
  { "appendixsubsubsec", 5 },
  { "appendixsubsec", 4 },
  { "appendixsec", 3 },
  { "appendixsection", 3 },
  { "appendix", 2 },
  { "subsubsec", 5 },
  { "subsubsection", 5 },
  { "subsection", 4 },
  { "section", 3 },
  { "chapter", 2 },
  { "top", 1 },

  { (const char *)NULL, 0 }
};

/* Return an integer which identifies the type section present in TEXT. */
int
what_section (text)
     char *text;
{
  register int i, j;
  const char *t;

 find_section_command:
  for (j = 0; text[j] && cr_or_whitespace (text[j]); j++);
  if (text[j] != '@')
    return (-1);

  text = text + j + 1;

  /* We skip @comment commands. */
  /* if ((strncmp (text, "comment", strlen ("comment")) == 0) || */
  /*    (strncmp (text, "c ", strlen ("c ")) == 0)) */
  if ((strncmp (text, "comment", sizeof("comment") - 1) == 0) ||
      (strncmp (text, "c ", sizeof("c ") - 1) == 0))
    {
      while (*text++ != '\n');
      goto find_section_command;
    }

  /* Handle italicized sectioning commands. */
  if (*text == 'i')
    text++;

  for (j = 0; text[j] && !cr_or_whitespace (text[j]); j++);

  for (i = 0; t = section_alist[i].name; i++)
    {
      if (j == strlen (t) && strncmp (t, text, j) == 0)
	return (section_alist[i].level);
    }
  return (-1);
}

/* Treat this just like @unnumbered.  The only difference is
   in node defaulting. */
void
cm_top ()
{
  static int top_encountered = 0;
  cm_unnumbered ();

  /* It is an error to have more than one @top. */
  if (top_encountered)
    {
      TAG_ENTRY *tag = tag_table;

      line_error ("There already is a node having @top as a section");

      while (tag != (TAG_ENTRY *)NULL)
	{
	  if ((tag->flags & IS_TOP))
	    {
	      size_t old_line_number = line_number;
	      line_number = tag->line_no;
	      line_error ("Here is the @top node.");
	      line_number = old_line_number;
	      return;
	    }
	  tag = tag->next_ent;
	}
    }
  else
    {
      top_encountered = 1;

      /* The most recently defined node is the top node. */
      if (tag_table)
	tag_table->flags |= IS_TOP;

      /* Now set the logical hierarchical level of the Top node. */
      {
	size_t orig_offset = input_text_offset;

	input_text_offset = search_forward ("\n@node", orig_offset);

	if ((long)input_text_offset > 0)
	  {
	    int this_section;

	    /* Move to the end of this line, and find out what the
	       sectioning command is here. */
	    while (input_text[input_text_offset] != '\n')
	      input_text_offset++;

	    if (input_text_offset < size_of_input_text)
	      input_text_offset++;

	    this_section = what_section (input_text + input_text_offset);

	    /* If we found a sectioning command, then give the top section
	       a level of this section - 1. */
	    if (this_section != -1)
	      {
		register int i;

		for (i = 0; section_alist[i].name; i++)
		  if (strcmp (section_alist[i].name, "Top") == 0)
		    {
		      section_alist[i].level = this_section - 1;
		      break;
		    }
	      }
	  }
	input_text_offset = orig_offset;
      }
    }
}

/* The remainder of the text on this line is an unnumbered heading. */
void
cm_unnumbered ()
{
  cm_chapter ();
}

/* The remainder of the text on this line is an unnumbered section heading. */
void
cm_unnumberedsec ()
{
  cm_section ();
}

/* The remainder of the text on this line is an unnumbered
   subsection heading. */
void
cm_unnumberedsubsec ()
{
  cm_subsection ();
}

/* The remainder of the text on this line is an unnumbered
   subsubsection heading. */
void
cm_unnumberedsubsubsec ()
{
  cm_subsubsection ();
}

/* The remainder of the text on this line is an appendix heading. */
void
cm_appendix ()
{
  cm_chapter ();
}

/* The remainder of the text on this line is an appendix section heading. */
void
cm_appendixsec ()
{
  cm_section ();
}

/* The remainder of the text on this line is an appendix subsection heading. */
void
cm_appendixsubsec ()
{
  cm_subsection ();
}

/* The remainder of the text on this line is an appendix
   subsubsection heading. */
void
cm_appendixsubsubsec ()
{
  cm_subsubsection ();
}

/* Compatibility functions substitute for chapter, section, etc. */
void
cm_majorheading ()
{
  cm_chapheading ();
}

void
cm_chapheading ()
{
  cm_chapter ();
}

void
cm_heading ()
{
  cm_section ();
}

void
cm_subheading ()
{
  cm_subsection ();
}

void
cm_subsubheading ()
{
  cm_subsubsection ();
}


/* **************************************************************** */
/*								    */
/*		   Adding nodes, and making tags		    */
/*								    */
/* **************************************************************** */

/* Start a new tag table. */
void
init_tag_table ()
{
  while (tag_table != (TAG_ENTRY *) NULL)
    {
      TAG_ENTRY *temp = tag_table;
      free (temp->node);
      free (temp->prev);
      free (temp->next);
      free (temp->up);
      tag_table = tag_table->next_ent;
      free (temp);
    }
}

void
write_tag_table ()
{
  write_tag_table_internal (0);		/* Not indirect. */
}

void
write_tag_table_indirect ()
{
  write_tag_table_internal (1);
}

/* Write out the contents of the existing tag table.
   INDIRECT_P says how to format the output. */
void
write_tag_table_internal (indirect_p)
     int indirect_p;
{
  TAG_ENTRY *node = tag_table;
  int old_indent = no_indent;

  no_indent = 1;
  filling_enabled = 0;
  must_start_paragraph = 0;
  close_paragraph ();

  if (!indirect_p)
    {
      no_indent = 1;
      insert ('\n');
    }

  add_word_args ("\037\nTag Table:\n%s", indirect_p ? "(Indirect)\n" : "");

  while (node != (TAG_ENTRY *) NULL)
    {
      add_word_args ("Node: %s\177%lu\n", node->node, node->position);
      node = node->next_ent;
    }

  add_word ("\037\nEnd Tag Table\n");
  flush_output ();
  no_indent = old_indent;
}

char *
get_node_token ()
{
  char *string;

  get_until_in_line (",", &string);

  if (curchar () == ',')
    input_text_offset++;

  canon_white (string);

  /* Allow things like @@nodename. */
  normalize_node_name (string);

  return (string);
}

/* Given a node name in STRING, remove double @ signs, replacing them
   with just one. */
void
normalize_node_name (string)
     char *string;
{
  register int i, l = strlen (string);

  for (i = 0; i < l; i++)
    {
      if (string[i] == '@' && string[i + 1] == '@')
	{
	  strncpy (string + i, string + i + 1, l - i);
	  l--;
	}
    }
}

/* Look up NAME in the tag table, and return the associated
   tag_entry.  If the node is not in the table return NULL. */
TAG_ENTRY *
find_node (name)
     char *name;
{
  TAG_ENTRY *tag = tag_table;

  while (tag != (TAG_ENTRY *) NULL)
    {
      if (stricmp (tag->node, name) == 0)
	return (tag);
      tag = tag->next_ent;
    }
  return ((TAG_ENTRY *) NULL);
}

/* Remember NODE and associates. */
void
remember_node (node, prev, next, up, position, line_no, no_warn)
     char *node, *prev, *next, *up;
     size_t position, line_no;
     int no_warn;
{
  /* Check for existence of this tag already. */
  if (validating)
    {
      register TAG_ENTRY *tag = find_node (node);
      if (tag)
	{
	  line_error ("Node `%s' multiply defined (%lu is first definition)",
		      node, tag->line_no);
	  return;
	}
    }

  /* First, make this the current node. */
  current_node = node;

  /* Now add it to the list. */
  {
    TAG_ENTRY *new = (TAG_ENTRY *) xmalloc (sizeof (TAG_ENTRY));
    new->node = node;
    new->prev = prev;
    new->next = next;
    new->up = up;
    new->position = position;
    new->line_no = line_no;
    new->filename = node_filename;
    new->touched = 0;		/* not yet referenced. */
    new->flags = 0;
    if (no_warn)
      new->flags |= NO_WARN;
    new->next_ent = tag_table;
    tag_table = new;
  }
}


/* The order is: nodename, nextnode, prevnode, upnode.
   The next, prev, and up fields can be defaulted.
   You must follow a node command which has those fields
   defaulted with a sectioning command (e.g. @chapter) giving
   the "level" of that node.  It is an error not to do so.
   The defaults come from the menu in this nodes parent. */
void
cm_node ()
{
  char *node, *prev, *next, *up;
  size_t new_node_pos;
  int defaulting, this_section, no_warn = 0;
  extern int already_outputting_pending_notes;

  if (strcmp (command, "nwnode") == 0)
    no_warn = 1;

  /* Get rid of unmatched brace arguments from previous commands. */
  discard_braces ();

  /* There also might be insertions left lying around that haven't been
     ended yet.  Do that also. */
  discard_insertions ();

  if (!already_outputting_pending_notes)
    {
      close_paragraph ();
      output_pending_notes ();
      free_pending_notes ();
    }

  filling_enabled = indented_fill = 0;
  new_node_pos = output_position + 1;
  current_footnote_number = 1;

  node = get_node_token ();
  next = get_node_token ();
  prev = get_node_token ();
  up = get_node_token ();

  no_indent = 1;
  if (!no_headers)
    add_word_args ("\037\nFile: %s,  Node: %s", pretty_output_filename, node);

  /* Check for defaulting of this node's next, prev, and up fields. */
  defaulting = ((strlen (next) == 0) &&
		(strlen (prev) == 0) &&
		(strlen (up) == 0));

  this_section = what_section (input_text + input_text_offset);

  /* If we are defaulting, then look at the immediately following
     sectioning command (error if none) to determine the node's
     level.  Find the node that contains the menu mentioning this node
     that is one level up (error if not found).  That node is the "Up"
     of this node.  Default the "Next" and "Prev" from the menu. */
  if (defaulting)
    {
      NODE_REF *last_ref = (NODE_REF *)NULL;
      NODE_REF *ref = node_references;

      if (this_section < 0)
	{
	  const char *polite_section_name = "top";
	  int i;

	  for (i = 0; section_alist[i].name; i++)
	    if (section_alist[i].level == current_section + 1)
	      {
		polite_section_name = section_alist[i].name;
		break;
	      }

	  line_error ("Node `%s' requires a sectioning command (e.g. @%s)",
	     node, polite_section_name);
	}
      else
	{
	  if (stricmp (node, "Top") == 0)
	    {
	      /* Default the NEXT pointer to be the first menu item in
		 this node, if there is a menu in this node. */
	      {
		size_t orig_offset, orig_size;

		orig_offset = input_text_offset;
		orig_size = search_forward ("\n@node ", orig_offset);

		if ((long) orig_size < 0)
		  orig_size = size_of_input_text;

		input_text_offset = search_forward ("\n@menu", orig_offset);
		if ((long) input_text_offset > -1)
		  {
		    input_text_offset =
		      search_forward ("\n* ", input_text_offset);

		    if ((long) input_text_offset > -1)
		      next = glean_node_from_menu (0);

		    if (next)
		      {
			prev = savestring ("(DIR)");
			up = savestring ("(DIR)");
		      }
		  }
		input_text_offset = orig_offset;
	      }
	    }

	  while (ref)
	    {
	      if (ref->section == (this_section - 1) &&
		  ref->type == menu_reference &&
		  stricmp (ref->node, node) == 0)
		{
		  char *containing_node = ref->containing_node;

		  free (up);
		  up = savestring (containing_node);

		  if (last_ref &&
		      strcmp
		      (last_ref->containing_node, containing_node) == 0)
		    {
		      free (next);
		      next = savestring (last_ref->node);
		    }

		  while ((ref->section == this_section - 1) &&
			 (ref->next) &&
			 (ref->next->type != menu_reference))
		    ref = ref->next;

		  if (ref->next &&
		      strcmp
		      (ref->next->containing_node, containing_node) == 0)
		    {
		      free (prev);
		      prev = savestring (ref->next->node);
		    }
		  else if (!ref->next &&
			   stricmp (ref->containing_node, "Top") == 0)
		    {
		      free (prev);
		      prev = savestring (ref->containing_node);
		    }
		  break;
		}
	      last_ref = ref;
	      ref = ref->next;
	    }
	}
    }

  if (!no_headers)
    {
      if (*next)
	add_word_args (",  Next: %s", next);

      if (*prev)
	add_word_args (",  Prev: %s", prev);

      if (*up)
	add_word_args (",  Up: %s", up);
    }

  insert ('\n');
  close_paragraph ();
  no_indent = 0;

  if (!*node)
    {
      line_error ("No node name specified for `@%s' command", command);
      free (node);
      free (next);
      free (prev);
      free (up);
    }
  else
    {
      if (!*next) { free (next); next = (char *)NULL; }
      if (!*prev) { free (prev); prev = (char *)NULL; }
      if (!*up) { free (up); up = (char *)NULL; }
      remember_node (node, prev, next, up, new_node_pos, line_number, no_warn);
    }

  /* Change the section only if there was a sectioning command. */
  if (this_section >= 0)
    current_section = this_section;

  filling_enabled = 1;
}

/* Validation of an info file.
   Scan through the list of tag entrys touching the Prev, Next, and Up
   elements of each.  It is an error not to be able to touch one of them,
   except in the case of external node references, such as "(DIR)".

   If the Prev is different from the Up,
   then the Prev node must have a Next pointing at this node.

   Every node except Top must have an Up.
   The Up node must contain some sort of reference, other than a Next,
   to this node.

   If the Next is different from the Next of the Up,
   then the Next node must have a Prev pointing at this node. */
void
validate_file (filename, tag_table)
     char *filename;
     TAG_ENTRY *tag_table;
{
  char *old_input_filename = input_filename;
  TAG_ENTRY *tags = tag_table;

  while (tags != (TAG_ENTRY *) NULL)
    {
      register TAG_ENTRY *temp_tag;

      input_filename = tags->filename;
      line_number = tags->line_no;

      /* If this node has a Next, then make sure that the Next exists. */
      if (tags->next)
	{
	  validate (tags->next, tags->line_no, "Next");

	  /* If the Next node exists, and there is no Up, then make
	     sure that the Prev of the Next points back. */
	  if (temp_tag = find_node (tags->next))
	    {
	      char *prev = temp_tag->prev;
	      if (!prev || (stricmp (prev, tags->node) != 0))
		{
		  line_error
		    ("Node `%s''s Next field not pointed back to", tags->node);
		  line_number = temp_tag->line_no;
		  input_filename = temp_tag->filename;
		  line_error("This node (`%s') is the one with the bad `Prev'",
		     temp_tag->node);
		  input_filename = tags->filename;
		  line_number = tags->line_no;
		  temp_tag->flags |= PREV_ERROR;
		}
	    }
	}

      /* Validate the Prev field if there is one, and we haven't already
	 complained about it in some way.  You don't have to have a Prev
	 field at this stage. */
      if (!(tags->flags & PREV_ERROR) && tags->prev)
	{
	  int valid = validate (tags->prev, tags->line_no, "Prev");

	  if (!valid)
	    tags->flags |= PREV_ERROR;
	  else
	    {
	      /* If the Prev field is not the same as the Up field,
		 then the node pointed to by the Prev field must have
		 a Next field which points to this node. */
	      if (tags->up && (stricmp (tags->prev, tags->up) != 0))
		{
		  temp_tag = find_node (tags->prev);
		  if (!temp_tag->next ||
		      (stricmp (temp_tag->next, tags->node) != 0))
		    {
		      line_error("Node `%s''s Prev field not pointed back to",
				  tags->node);
		      line_number = temp_tag->line_no;
		      input_filename = temp_tag->filename;
		      line_error
			  ("This node (`%s') is the one with the bad `Next'",
			 temp_tag->node);
		      input_filename = tags->filename;
		      line_number = tags->line_no;
		      temp_tag->flags |= NEXT_ERROR;
		    }
		}
	    }
	}

      if (!tags->up && (stricmp (tags->node, "Top") != 0))
	line_error ("Node `%s' is missing an \"Up\" field", tags->node);
      else if (tags->up)
	{
	  int valid = validate (tags->up, tags->line_no, "Up");

	  /* If node X has Up: Y, then warn if Y fails to have a menu item
	     or note pointing at X, if Y isn't of the form "(Y)". */
	  if (valid && *tags->up != '(')
	    {
	      NODE_REF *nref, *tref, *list;

	      tref = (NODE_REF *) NULL;
	      list = node_references;

	      for (;;)
		{
		  if (!(nref = find_node_reference (tags->node, list)))
		    break;

		  if (stricmp (nref->containing_node, tags->up) == 0)
		    {
		      if (nref->type != menu_reference)
			{
			  tref = nref;
			  list = nref->next;
			}
		      else
			break;
		    }
		  list = nref->next;
		}

	      if (!nref)
		{
		  temp_tag = find_node (tags->up);
		  line_number = temp_tag->line_no;
		  filename = temp_tag->filename;
		  if (!tref)
		    line_error (
"`%s' has an Up field of `%s', but `%s' has no menu item for `%s'",
				tags->node, tags->up, tags->up, tags->node);
		  line_number = tags->line_no;
		  filename = tags->filename;
		}
	    }
	}
      tags = tags->next_ent;
    }

  validate_other_references (node_references);
  /* We have told the user about the references which didn't exist.
     Now tell him about the nodes which aren't referenced. */

  tags = tag_table;
  while (tags != (TAG_ENTRY *) NULL)
    {
      /* Special hack.  If the node in question appears to have
         been referenced more than REFERENCE_WARNING_LIMIT times,
         give a warning. */
      if (tags->touched > reference_warning_limit)
	{
	  input_filename = tags->filename;
	  line_number = tags->line_no;
	  warning ("Node `%s' has been referenced %d times",
		   tags->node, tags->touched);
	}

      if (tags->touched == 0)
	{
	  input_filename = tags->filename;
	  line_number = tags->line_no;

	  /* Notice that the node "Top" is special, and doesn't have to
	     be referenced. */
	  if (stricmp (tags->node, "Top") != 0)
	    warning ("Unreferenced node `%s'", tags->node);
	}
      tags = tags->next_ent;
    }
  input_filename = old_input_filename;
}

/* Return 1 if tag correctly validated, or 0 if not. */
int
validate (tag, line, label)
     char *tag;
     size_t line;
     const char *label;
{
  TAG_ENTRY *result;

  /* If there isn't a tag to verify, or if the tag is in another file,
     then it must be okay. */
  if (!tag || !*tag || *tag == '(')
    return (1);

  /* Otherwise, the tag must exist. */
  result = find_node (tag);

  if (!result)
    {
      line_number = line;
      line_error (
"Validation error.  `%s' field points to node `%s', which doesn't exist",
		  label, tag);
      return (0);
    }
  result->touched++;
  return (1);
}

/* Split large output files into a series of smaller files.  Each file
   is pointed to in the tag table, which then gets written out as the
   original file.  The new files have the same name as the original file
   with a "-num" attached.  SIZE is the largest number of bytes to allow
   in any single split file. */
/* For MessyDOS like file system we will usurp the whole extension for
   file number, in a form of .-num so we may have up to 99 files */
void
split_file (filename, size)
     char *filename;
     size_t size;
{
  char *root_filename, *root_pathname;
  char *the_file;
  struct stat fileinfo;
  char *the_header;
  size_t header_size;

  if (size == 0)
    size = DEFAULT_SPLIT_SIZE;

  if ((stat (filename, &fileinfo) != 0) ||
      (fileinfo.st_size < SPLIT_SIZE_THRESHOLD))
    return;

  the_file = find_and_load (filename);
  /* find_and_load() sets the_file to input_text */
  if (!the_file)
    return;
  /* Can only do this to files with tag tables. */
  if (NULL == (tag_table = restore_tag_table(the_file, size_of_input_text)))
      return;  /* no tag table */

  root_filename = filename_part (filename);
#ifdef  atarist
  /* truncate file name extension */
  {
      char *loc;
      if (NULL != (loc = rindex (root_filename, '.')))
	  *(loc + 1) = '\0';
  }
#endif /* atarist */
  root_pathname = pathname_part (filename);

  if (!root_pathname)
    root_pathname = savestring ("");

  /* Start splitting the file.  Walk along the tag table
     outputting sections of the file.  When we have written
     all of the nodes in the tag table, make the top-level
     pointer file, which contains indirect pointers and
     tags for the nodes. */
  {
    int which_file = 1;
    TAG_ENTRY *tags = tag_table;
    char *indirect_info = (char *)NULL;

    /* Remember the `header' of this file.  The first tag in the file is
       the bottom of the header; the top of the file is the start. */
    /*
    the_header = (char *)xmalloc (1 + (header_size = (tags->position - 2)));
    bcopy (the_file, the_header, header_size);
    */
    /* We are only writing this out, so we do need really a copy */
    the_header = the_file;
    header_size = (tags->position - 2);

    while (tags)
      {
	size_t file_top, file_bot, limit;
#ifdef DOTS
	fprintf(stderr, ".");		/* we are still crunching */
#endif

	/* Have to include the Control-_. */
	file_top = file_bot = tags->position - 2;
	limit = file_top + size;

	/* If the rest of this file is only one node, then
	   that is the entire subfile. */
	if (!tags->next_ent)
	  {
	    size_t i = tags->position + 1;
	    char last_char = the_file[i];

	    while (i < size_of_input_text)
	      {
		if ((the_file[i] == '\037') &&
		    ((last_char == '\n') ||
		     (last_char == '\014')))
		  break;
		else
		  last_char = the_file[i];
		i++;
	      }
	    file_bot = i;
	    tags = tags->next_ent;
	    goto write_region;
	  }

	/* Otherwise, find the largest number of nodes that can fit in
	   this subfile. */
	for (; tags; tags = tags->next_ent)
	  {
	    if (!tags->next_ent)
	      {
		/* This entry is the last node.  Search forward for the end
	           of this node, and that is the end of this file. */
		size_t i = tags->position + 1;
		char last_char = the_file[i];

		while (i < size_of_input_text)
		  {
		    if ((the_file[i] == '\037') &&
			((last_char == '\n') ||
			 (last_char == '\014')))
		      break;
		    else
		      last_char = the_file[i];
		    i++;
		  }
		file_bot = i;

		if (file_bot < limit)
		  {
		    tags = tags->next_ent;
		    goto write_region;
		  }
		else
		  {
		    /* Here we want to write out everything before the last
		       node, and then write the last node out in a file
		       by itself. */
		    file_bot = tags->position;
		    goto write_region;
		  }
	      }

	    if (tags->next_ent->position > limit)
	      {
		if ((tags->position) - 2 == file_top)
		  tags = tags->next_ent;
		file_bot = tags->position;
	      write_region:
		{
		  char *split_file = (char *)
		    xmalloc (10
			     + strlen (root_pathname)
			     + strlen (root_filename));
		  sprintf (split_file,
		       "%s%s-%d", root_pathname, root_filename, which_file);
		  output_filename = split_file;  /* needed for flush_output */

#ifndef atarist
	        {
		  int fd;
		  if (((fd =
		    open (split_file, O_WRONLY | O_TRUNC | O_CREAT, 0666)) < 0)
		      || (write (fd, the_header, header_size) != header_size)
		      || (write (fd, the_file + file_top, file_bot - file_top)
			  != (file_bot - file_top))
		      || ((close (fd)) < 0))
		    {
		      perror (split_file);
		      close (fd);
		      exit (FATAL);
		    }
	        }
#else /* atarist */
		  /*
		   * we cannot just 'write' due to differences between
		   * text and binary files - we will use flush_output()
		   * instead; it also checks for errors on write
		   */
		  if (NULL != (output_stream = fopen (split_file, WMODE))) {
		    output_paragraph = the_header;
		    output_paragraph_offset = header_size;
		    flush_output();
		    output_paragraph = the_file + file_top;
		    output_paragraph_offset = file_bot - file_top;
		    flush_output();
		    fclose (output_stream);
		  }
#endif /* endif */

		  if (!indirect_info)
		    {
		      indirect_info = the_file + file_top;
		      indirect_info +=
			  sprintf (indirect_info, "\037\nIndirect:\n");
		    }

#ifndef atarist
		  indirect_info += sprintf (indirect_info, "%s-%d: %lu\n",
			   root_filename, which_file, file_top);
#else
		  indirect_info += sprintf (indirect_info, "%sinfo-%d: %lu\n",
			   root_filename, which_file, file_top);
#endif /* atarist */

		  free (split_file);
		  which_file++;
		  break;
		}
	      }
	  }
      }

    /* We have sucessfully created the subfiles.  Now write out the
       original again.  We must use `output_stream', or
       write_tag_table_indirect () won't know where to place the output. */
    /* On Atari we do not bother with checking how this file is written
       it contains mostly a tag table anyway and is meant for reading
       with info program */
    output_stream = fopen (filename, WMODE);
    if (!output_stream)
      {
	perror (filename);
	exit (FATAL);
      }

    {
      size_t distance = indirect_info - the_file;
      fwrite (the_file, 1, distance, output_stream);
      
      /* make the_file into paragraph_output_buffer - the contents
	 of this buffer is already written out so we may reuse it */
      output_paragraph = the_file;
      paragraph_buffer_len = size_of_input_text;
      
      /* Set initial parameters and Inhibit newlines. */
      init_par_parameters ();

      write_tag_table_indirect ();
      fclose (output_stream);
/*      free (the_header);  */
      free (the_file);
      return;
    }
  }
}

/* Some menu hacking.  This is used to remember menu references while
   reading the input file.  After the output file has been written, if
   validation is on, then we use the contents of NODE_REFERENCES as a
   list of nodes to validate. */
const char *
reftype_type_string (type)
     enum reftype type;
{
  switch (type)
    {
    case menu_reference:
      return ("Menu");
    case followed_reference:
      return ("Followed-Reference");
    default:
      return ("Internal-bad-reference-type");
    }
}

/* Remember this node name for later validation use. */
void
remember_node_reference (node, line, type)
     char *node;
     size_t line;
     enum reftype type;
{
  NODE_REF *temp = (NODE_REF *) xmalloc (sizeof (NODE_REF));

  temp->next = node_references;
  temp->node = savestring (node);
  temp->line_no = line;
  temp->section = current_section;
  temp->type = type;
  temp->containing_node = savestring (current_node);
  temp->filename = node_filename;

  node_references = temp;
}

void
validate_other_references (ref_list)
     register NODE_REF *ref_list;
{
  char *old_input_filename = input_filename;

  while (ref_list != (NODE_REF *) NULL)
    {
      input_filename = ref_list->filename;
      validate (ref_list->node, ref_list->line_no,
		reftype_type_string (ref_list->type));
      ref_list = ref_list->next;
    }
  input_filename = old_input_filename;
}

/* Find NODE in REF_LIST. */
NODE_REF *
find_node_reference (node, ref_list)
     char *node;
     register NODE_REF *ref_list;
{
  while (ref_list)
    {
      if (stricmp (node, ref_list->node) == 0)
	break;
      ref_list = ref_list->next;
    }
  return (ref_list);
}

void
free_node_references ()
{
  register NODE_REF *list, *temp;

  list = node_references;

  while (list)
    {
      temp = list;
      free (list->node);
      free (list->containing_node);
      list = list->next;
      free (temp);
    }
  node_references = (NODE_REF *) NULL;
}

  /* This function gets called at the start of every line while inside of
     a menu.  It checks to see if the line starts with "* ", and if so,
     remembers the node reference that this menu refers to.
     input_text_offset is at the \n just before the line start. */
#define menu_starter "* "
char *
glean_node_from_menu (remember_reference)
     int remember_reference;
{
  size_t i, orig_offset = input_text_offset;
  char *nodename;

  if (strncmp (&input_text[input_text_offset + 1],
	       menu_starter,
	       strlen (menu_starter)) != 0)
    return ((char *)NULL);
  else
    input_text_offset += strlen (menu_starter) + 1;

  get_until_in_line (":", &nodename);
  if (curchar () == ':')
    input_text_offset++;
  canon_white (nodename);

  if (curchar () == ':')
    goto save_node;

  free (nodename);
  get_rest_of_line (&nodename);

  /* Special hack: If the nodename follows the menu item name,
     then we have to read the rest of the line in order to find
     out what the nodename is.  But we still have to read the
     line later, in order to process any formatting commands that
     might be present.  So un-count the carriage return that has just
     been counted. */
  line_number--;

  canon_white (nodename);
  for (i = 0; i < strlen (nodename); i++)
    {
      if (nodename[i] == '\t' ||
	  nodename[i] == '.' ||
	  nodename[i] == ',')
	{
	  nodename[i] = '\0';
	  break;
	}
    }
save_node:
  input_text_offset = orig_offset;
  normalize_node_name (nodename);
  i = strlen (nodename);
  if (i && nodename[i - 1] == ':')
    nodename[i - 1] = '\0';

  if (remember_reference)
    {
      remember_node_reference (nodename, line_number, menu_reference);
      free (nodename);
      return ((char *)NULL);
    }
  else
    return (nodename);
}

void
cm_menu ()
{
  begin_insertion (menu);
}


/* **************************************************************** */
/*								    */
/*			Cross Reference Hacking			    */
/*								    */
/* **************************************************************** */

char *
get_xref_token ()
{
  char *string;

  get_until_in_braces (",", &string);
  if (curchar () == ',')
    input_text_offset++;
  fix_whitespace (string);
  return (string);
}

int px_ref_flag = 0;		/* Controls initial output string. */

/* Make a cross reference. */
void
cm_xref (arg)
int arg;
{
  if (arg == START)
    {
      char *arg1, *arg2, *arg3, *arg4, *arg5;

      arg1 = get_xref_token ();
      arg2 = get_xref_token ();
      arg3 = get_xref_token ();
      arg4 = get_xref_token ();
      arg5 = get_xref_token ();

      add_word_args ("%s", px_ref_flag ? "*note " : "*Note ");

      if (*arg5 || *arg4)
	{
	  char *node_name;

	  if (!*arg2)
	    {
	      if (*arg3)
		node_name = arg3;
	      else
		node_name = arg1;
	    }
	  else
	    node_name = arg2;

	  execute_string ("%s: (%s)%s", node_name, arg4, arg1);
	  return;
	}
      else
	remember_node_reference (arg1, line_number, followed_reference);

      if (*arg3)
	{
	  if (!*arg2)
	    execute_string ("%s: %s", arg3, arg1);
	  else
	    execute_string ("%s: %s", arg2, arg1);
	  return;
	}

      if (*arg2)
	execute_string ("%s: %s", arg2, arg1);
      else
	execute_string ("%s::", arg1);
    }
  else
    {

      /* Check to make sure that the next non-whitespace character is either
         a period or a comma. input_text_offset is pointing at the "}" which
         ended the xref or pxref command. */
      size_t temp = input_text_offset + 1;

      if (output_paragraph[output_paragraph_offset - 2] == ':' &&
	  output_paragraph[output_paragraph_offset - 1] == ':')
	return;
      while (temp < size_of_input_text)
	{
	  if (cr_or_whitespace (input_text[temp]))
	    temp++;
	  else
	    {
	      if (input_text[temp] == '.' ||
		  input_text[temp] == ',' ||
		  input_text[temp] == '\t')
		return;
	      else
		{
		  line_error
	      ("Cross-reference must be terminated with a period or a comma");
		  return;
		}
	    }
	}
    }
}

void
cm_pxref (arg)
     int arg;
{
  if (arg == START)
    {
      px_ref_flag++;
      cm_xref (arg);
      px_ref_flag--;
    }
  else
    add_char ('.');
}

void
cm_inforef (arg)
     int arg;
{
  if (arg == START)
    {
      char *node, *pname, *file;

      node = get_xref_token ();
      pname = get_xref_token ();
      file = get_xref_token ();

      execute_string ("*note %s: (%s)%s", pname, file, node);
    }
}

/* **************************************************************** */
/*								    */
/*			Insertion Command Stubs			    */
/*								    */
/* **************************************************************** */

void
cm_quotation ()
{
  begin_insertion (quotation);
}

void
cm_example ()
{
  begin_insertion (example);
}

void
cm_smallexample ()
{
  begin_insertion (smallexample);
}

void
cm_lisp ()
{
  begin_insertion (lisp);
}

void
cm_smalllisp ()
{
  begin_insertion (smalllisp);
}

/* @cartouche/@end cartouche draws box with rounded corners in
   TeX output.  Right now, just a NOP insertion. */
void
cm_cartouche ()
{
  begin_insertion (cartouche);
}

void
cm_format ()
{
  begin_insertion (format);
}

void
cm_display ()
{
  begin_insertion (display);
}

void
cm_itemize ()
{
  begin_insertion (itemize);
}

void
cm_enumerate ()
{
  do_enumeration (enumerate, "1");
}

/* Start an enumeration insertion of type TYPE.  If the user supplied
   no argument on the line, then use DEFAULT_STRING as the initial string. */
void
do_enumeration (type, default_string)
     int type;
     const char *default_string;
{
  get_until_in_line (".", &enumeration_arg);
  canon_white (enumeration_arg);

  if (!*enumeration_arg)
    {
      free (enumeration_arg);
      enumeration_arg = savestring (default_string);
    }

  if (!isdigit (*enumeration_arg) && !isletter (*enumeration_arg))
    {
      warning ("%s requires a letter or a digit", insertion_type_pname (type));

      switch (type)
	{
	case enumerate:
	  default_string = "1";
	  break;
	}
      enumeration_arg = savestring (default_string);
    }
  begin_insertion (type);
}

void
cm_table ()
{
  begin_insertion (table);
}

void
cm_ftable ()
{
  begin_insertion (ftable);
}

void
cm_vtable ()
{
  begin_insertion (vtable);
}

void
cm_group ()
{
  begin_insertion (group);
}

void
cm_ifinfo ()
{
  begin_insertion (ifinfo);
}

/* Begin an insertion where the lines are not filled or indented. */
void
cm_flushleft ()
{
  begin_insertion (flushleft);
}

/* Begin an insertion where the lines are not filled, and each line is
   forced to the right-hand side of the page. */
void
cm_flushright ()
{
  begin_insertion (flushright);
}


/* **************************************************************** */
/*								    */
/*			  Conditional Handling			    */
/*								    */
/* **************************************************************** */

/* A structure which contains `defined' variables. */
typedef struct _defines {
  struct _defines *next;
  char *name;
} DEFINE;

/* The linked list of `set' defines. */
DEFINE *defines = (DEFINE *)NULL;

/* Add NAME to the list of `set' defines. */
void
set (name)
     char *name;
{
  DEFINE *temp;

  for (temp = defines; temp; temp = temp->next)
    if (strcmp (name, temp->name) == 0)
      return;

  temp = (DEFINE *)xmalloc (sizeof (DEFINE));
  temp->next = defines;
  temp->name = savestring (name);
  defines = temp;
}

/* Remove NAME from the list of `set' defines. */
void
clear (name)
     char *name;
{
  register DEFINE *temp, *last;

  last = (DEFINE *)NULL;
  temp = defines;

  while (temp)
    {
      if (strcmp (temp->name, name) == 0)
	{
	  if (last)
	    last->next = temp->next;
	  else
	    defines = temp->next;

	  free (temp->name);
	  free (temp);
	  break;
	}
      last = temp;
      temp = temp->next;
    }
}

/* Return non-zero if NAME is present in the list of `set' defines. */
int
set_p (name)
     char *name;
{
  register DEFINE *temp;

  for (temp = defines; temp; temp = temp->next)
    if (strcmp (temp->name, name) == 0)
      return (1);

  return (0);
}

/* Conditionally parse based on the current command name. */
void
command_name_condition ()
{
  char discarder[128];

  sprintf (discarder, "\n@end %s", command);
  discard_until (discarder);
  discard_until ("\n");
}

#define SET	1
#define CLEAR	2
#define IFSET	3
#define IFCLEAR	4

/* Create a variable whose name is the rest of the line. */
void
cm_set ()
{
  handle_variable (SET);
}

/* Remove a variable whose name is the rest of the line. */
void
cm_clear ()
{
  handle_variable (CLEAR);
}

void
cm_ifset ()
{
  handle_variable (IFSET);
}

void
cm_ifclear ()
{
  handle_variable (IFCLEAR);
}

/* Set, clear, or conditionalize based on ACTION. */
void
handle_variable (action)
     int action;
{
  char *name;

  get_rest_of_line (&name);
  backup_input_pointer ();
  canon_white (name);

  if (!*name)
    line_error ("@%s requires a name", command);
  else
    {
      switch (action)
	{
	case SET:
	  set (name);
	  break;

	case CLEAR:
	  clear (name);
	  break;

	case IFSET:
	  if (!set_p (name))
	    {
	      discard_until ("\n@end ifset");
	      discard_until ("\n");
	    }
	  else
	    begin_insertion (ifset);
	  break;

	case IFCLEAR:
	  if (set_p (name))
	    {
	      discard_until ("\n@end ifclear");
	      discard_until ("\n");
	    }
	  else
	    begin_insertion (ifclear);
	  break;
	}
    }
  free (name);
}

/* **************************************************************** */
/*								    */
/*			@itemx, @item				    */
/*								    */
/* **************************************************************** */

/* Non-zero means a string is in execution, as opposed to a file. */
int executing_string = 0;

/* Execute the string produced by formatting the ARGs with FORMAT.  This
   is like submitting a new file with @include. */
#if __STDC__
void
execute_string (const char *format, ...)
{

  va_list args;
  static char temp_string[4000];
  va_start(args, format);
  vsprintf (temp_string, format, args);
  va_end(args);
  strcat (temp_string, "@bye\n");
  pushfile ();
  input_text_offset = 0;
  input_text = temp_string;
  input_filename = savestring (input_filename);
  size_of_input_text = strlen (temp_string);

  executing_string++;
  reader_loop ();

  popfile ();
  executing_string--;

  free_and_clear (&command);
  command = savestring ("not bye");
}

#else /* fork-and-hope */

void
execute_string (format, arg1, arg2, arg3, arg4, arg5)
     char *format;
{
  static char temp_string[4000];
  sprintf (temp_string, format, arg1, arg2, arg3, arg4, arg5);
  strcat (temp_string, "@bye\n");
  pushfile ();
  input_text_offset = 0;
  input_text = temp_string;
  input_filename = savestring (input_filename);
  size_of_input_text = strlen (temp_string);

  executing_string++;
  reader_loop ();

  popfile ();
  executing_string--;

  free_and_clear (&command);
  command = savestring ("not bye");
}
#endif /* __STDC__ */

int itemx_flag = 0;

void
cm_itemx ()
{
  itemx_flag++;
  cm_item ();
  itemx_flag--;
}

void
cm_item ()
{
  char *rest_of_line, *item_func;

  /* Can only hack "@item" while inside of an insertion. */
  if (insertion_level)
    {
      INSERTION_ELT *stack = insertion_stack;

      get_rest_of_line (&rest_of_line);
      canon_white (rest_of_line);
      item_func = current_item_function ();

      /* Okay, do the right thing depending on which insertion function
	 is active. */

    switch_top:
      switch (stack->insertion)
	{
	case ifinfo:
	case ifset:
	case ifclear:
	case cartouche:
	  stack = stack->next;
	  if (!stack)
	    goto no_insertion;
	  else
	    goto switch_top;
	  break;

	case menu:
	case quotation:
	case example:
	case smallexample:
	case lisp:
	case format:
	case display:
	case group:
	  line_error("The `@%s' command is meaningless within a `@%s' block",
		      command,
		      insertion_type_pname (current_insertion_type ()));
	  break;

	case itemize:
	case enumerate:
	  if (itemx_flag)
	    {
	      line_error("@itemx is not meaningful inside of a `%s' block",
			  insertion_type_pname (current_insertion_type ()));
	    }
	  else
	    {
	      start_paragraph ();
	      kill_self_indent (-1);
	      filling_enabled = indented_fill = 1;

	      if (current_insertion_type () == itemize)
		{
		  indent (output_column = current_indent - 2);

		  /* I need some way to determine whether this command
		     takes braces or not.  I believe the user can type
		     either "@bullet" or "@bullet{}".  Of course, they
		     can also type "o" or "#" or whatever else they want. */
		  if (item_func && *item_func)
		    {
		      if (*item_func == '@')
			if (item_func[strlen (item_func) - 1] != '}')
			  execute_string ("%s{}", item_func);
			else
			  execute_string ("%s", item_func);
		      else
			execute_string ("%s", item_func);
		    }
		  insert (' ');
		  output_column++;
		}
	      else
		enumerate_item ();

	      /* Special hack.  This makes close paragraph ignore you until
		 the start_paragraph () function has been called. */
	      must_start_paragraph = 1;
	    }
	  break;

	case table:
	case ftable:
	case vtable:
	  {
	    /* Get rid of extra characters. */
	    kill_self_indent (-1);

	    /* close_paragraph () almost does what we want.  The problem
	       is when paragraph_is_open, and last_char_was_newline, and
	       the last newline has been turned into a space, because
	       filling_enabled. I handle it here. */
	    if (last_char_was_newline && filling_enabled && paragraph_is_open)
	      insert ('\n');
	    close_paragraph ();

#if defined (INDENT_PARAGRAPHS_IN_TABLE)
	    /* Indent on a new line, but back up one indentation level. */
	    /* force existing indentation. */
	    {
	      int t;

	      t = inhibit_paragraph_indentation;
	      inhibit_paragraph_indentation = 1;
	      add_char ('i');
	      inhibit_paragraph_indentation = t;
	    }
#else /* !INDENT_PARAGRAPHS_IN_TABLE */
	    add_char ('i');
#endif /* !INDENT_PARAGRAPHS_IN_TABLE */

	    output_paragraph_offset--;
	    kill_self_indent (default_indentation_increment + 1);

	    /* Add item's argument to the line. */
	    filling_enabled = 0;
	    if (!item_func && !(*item_func))
	      execute_string ("%s", rest_of_line);
	    else
	      execute_string ("%s{%s}", item_func, rest_of_line);

	    if (current_insertion_type () == ftable)
	      execute_string ("@findex %s\n", rest_of_line);

	    if (current_insertion_type () == vtable)
	      execute_string ("@vindex %s\n", rest_of_line);

	    /* Start a new line, and let start_paragraph ()
	       do the indenting of it for you. */
	    close_single_paragraph ();
	    indented_fill = filling_enabled = 1;
	  }
        default:
	  break;
	}
      free (rest_of_line);
    }
  else
    {
    no_insertion:
      line_error ("@%s found outside of an insertion block", command);
    }
}


/* **************************************************************** */
/*								    */
/*			Defun and Friends       		    */
/*								    */
/* **************************************************************** */

#define DEFUN_SELF_DELIMITING(c)					\
  (((c) == '(')								\
   || ((c) == ')')							\
   || ((c) == '[')							\
   || ((c) == ']'))

/***
struct token_accumulator
{
  unsigned int length;
  unsigned int index;
  char **tokens;
};
***/

void
initialize_token_accumulator (accumulator)
     struct token_accumulator *accumulator;
{
  (accumulator->length) = 0;
  (accumulator->index) = 0;
  (accumulator->tokens) = NULL;
}

void
accumulate_token (accumulator, token)
     struct token_accumulator *accumulator;
     char *token;
{
  if ((accumulator->index) >= (accumulator->length))
    {
      (accumulator->length) += 10;
      (accumulator->tokens) = (char **) xrealloc
	(accumulator->tokens, (accumulator->length * sizeof (char *)));
    }
  accumulator->tokens[accumulator->index] = token;
  accumulator->index += 1;
}

char *
copy_substring (start, end)
     char *start;
     char *end;
{
  char *result, *scan, *scan_result;

  result = (char *) xmalloc ((end - start) + 1);
  scan_result = result;
  scan = start;

  while (scan < end)
    *scan_result++ = *scan++;

  *scan_result = '\0';
  return (result);
}

/* Given `string' pointing at an open brace, skip forward and return a
   pointer to just past the matching close brace. */
int
scan_group_in_string (string_pointer)
     char **string_pointer;
{
  register int c;
  register char *scan_string;
  register unsigned int level = 1;

  scan_string = (*string_pointer) + 1;

  while (1)
    {
      if (level == 0)
	{
	  (*string_pointer) = scan_string;
	  return (1);
	}
      c = (*scan_string++);
      if (c == '\0')
	{
	  /* Tweak line_number to compensate for fact that
	     we gobbled the whole line before coming here. */
	  line_number -= 1;
	  line_error ("Missing `}' in @def arg");
	  line_number += 1;
	  (*string_pointer) = (scan_string - 1);
	  return (0);
	}
      if (c == '{')
	level += 1;
      if (c == '}')
	level -= 1;
    }
}

/* Return a list of tokens from the contents of `string'.
   Commands and brace-delimited groups count as single tokens.
   Contiguous whitespace characters are converted to a token
   consisting of a single space. */
char **
args_from_string (string)
     char *string;
{
  struct token_accumulator accumulator;
  register char *scan_string = string;
  char *token_start, *token_end;

  initialize_token_accumulator (&accumulator);

  while ((*scan_string) != '\0')
    {
      /* Replace arbitrary whitespace by a single space. */
      if (whitespace (*scan_string))
	{
	  scan_string += 1;
	  while (whitespace (*scan_string))
	    scan_string += 1;
	  accumulate_token ((&accumulator), (savestring (" ")));
	  continue;
	}

      /* Commands count as single tokens. */
      if ((*scan_string) == COMMAND_PREFIX)
	{
	  token_start = scan_string;
	  scan_string += 1;
	  if (self_delimiting (*scan_string))
	    scan_string += 1;
	  else
	    {
	      register int c;
	      while (1)
		{
		  c = *scan_string++;

 		  if ((c == '\0') || (c == '{') || (whitespace (c)))
		    {
		      scan_string -= 1;
		      break;
		    }
		}

	      if (*scan_string == '{')
		{
		  char *s = scan_string;
		  (void) scan_group_in_string (&s);
		  scan_string = s;
		}
	    }
	  token_end = scan_string;
	}

      /* Parentheses and brackets are self-delimiting. */
      else if (DEFUN_SELF_DELIMITING (*scan_string))
	{
	  token_start = scan_string;
	  scan_string += 1;
	  token_end = scan_string;
	}

      /* Open brace introduces a group that is a single token. */
      else if (*scan_string == '{')
	{
	  char *s = scan_string;
	  int balanced = scan_group_in_string (&s);

	  token_start = scan_string + 1;
	  scan_string = s;
	  token_end = balanced ? (scan_string - 1) : scan_string;
	}

      /* Otherwise a token is delimited by whitespace, parentheses,
	 brackets, or braces.  A token is also ended by a command. */
      else
	{
	  token_start = scan_string;

	  while (1)
	    {
	      register int c;

	      c = *scan_string++;

	      if (!c ||
		  (whitespace (c) || DEFUN_SELF_DELIMITING (c) ||
		   c == '{' || c == '}'))
		{
		  scan_string--;
		  break;
		}

	      /* If we encounter a command imbedded within a token,
		 then end the token. */
	      if (c == COMMAND_PREFIX)
		{
		  scan_string--;
		  break;
		}
	    }
	  token_end = scan_string;
	}

      accumulate_token
	(&accumulator, copy_substring (token_start, token_end));
    }
  accumulate_token (&accumulator, NULL);
  return (accumulator.tokens);
}

void
process_defun_args (defun_args, auto_var_p)
     char **defun_args;
     int auto_var_p;
{
  int pending_space = 0;

  while (1)
    {
      char *defun_arg = *defun_args++;

      if (defun_arg == NULL)
	break;

      if (defun_arg[0] == ' ')
	{
	  pending_space = 1;
	  continue;
	}

      if (pending_space)
	{
	  add_char (' ');
	  pending_space = 0;
	}

      if (DEFUN_SELF_DELIMITING (defun_arg[0]))
	add_char (defun_arg[0]);
      else if (defun_arg[0] == '&')
	add_word (defun_arg);
      else if (defun_arg[0] == COMMAND_PREFIX)
	execute_string ("%s", defun_arg);
      else if (auto_var_p)
	execute_string ("@var{%s}", defun_arg);
      else
	add_word (defun_arg);
    }
}

char *
next_nonwhite_defun_arg (arg_pointer)
     char ***arg_pointer;
{
  char **scan = (*arg_pointer);
  char *arg = (*scan++);

  if ((arg != 0) && (*arg == ' '))
    arg = *scan++;

  if (arg == 0)
    scan -= 1;

  *arg_pointer = scan;

  return ((arg == 0) ? "" : arg);
}

/* Make the defun type insertion.
   TYPE says which insertion this is.
   X_P says not to start a new insertion if non-zero. */
void
defun_internal (type, x_p)
     enum insertion_type type;
     int x_p;
{
  enum insertion_type base_type;
  char **defun_args, **scan_args;
  const char *category;
  char *defined_name, *type_name = 0, *type_name2 = 0;

  {
    char *line;
    get_rest_of_line (&line);
    defun_args = (args_from_string (line));
    free (line);
  }

  scan_args = defun_args;

  switch (type)
    {
    case defun:
      category = "Function";
      base_type = deffn;
      break;
    case defmac:
      category = "Macro";
      base_type = deffn;
      break;
    case defspec:
      category = "Special Form";
      base_type = deffn;
      break;
    case defvar:
      category = "Variable";
      base_type = defvr;
      break;
    case defopt:
      category = "User Option";
      base_type = defvr;
      break;
    case deftypefun:
      category = "Function";
      base_type = deftypefn;
      break;
    case deftypevar:
      category = "Variable";
      base_type = deftypevr;
      break;
    case defivar:
      category = "Instance Variable";
      base_type = defcv;
      break;
    case defmethod:
      category = "Method";
      base_type = defop;
      break;
    case deftypemethod:
      category = "Method";
      base_type = deftypemethod;
      break;
    default:
      category = next_nonwhite_defun_arg (&scan_args);
      base_type = type;
      break;
    }

  if ((base_type == deftypefn)
      || (base_type == deftypevr)
      || (base_type == defcv)
      || (base_type == defop)
      || (base_type == deftypemethod))
    type_name = next_nonwhite_defun_arg (&scan_args);

  if (base_type == deftypemethod)
    type_name2 = next_nonwhite_defun_arg (&scan_args);

  defined_name = next_nonwhite_defun_arg (&scan_args);

  /* This hack exists solely for the purposes of formatting the texinfo
     manual.  I couldn't think of a better way.  The token might be
     a simple @@ followed immediately by more text.  If this is the case,
     then the next defun arg is part of this one, and we should concatenate
     them. */
  if (*scan_args && **scan_args && !whitespace (**scan_args) &&
      (strcmp (defined_name, "@@") == 0))
    {
      char *tem = (char *)xmalloc (3 + strlen (scan_args[0]));

      sprintf (tem, "@@%s", scan_args[0]);

      free (scan_args[0]);
      scan_args[0] = tem;
      scan_args++;
      defined_name = tem;
    }

  if (!x_p)
    begin_insertion (type);

  /* Write the definition header line.
     This should start at the normal indentation.  */
  current_indent -= default_indentation_increment;
  start_paragraph ();

  switch (base_type)
    {
    case deffn:
    case defvr:
    case deftp:
      execute_string (" -- %s: %s", category, defined_name);
      break;
    case deftypefn:
    case deftypevr:
      execute_string (" -- %s: %s %s", category, type_name, defined_name);
      break;
    case defcv:
      execute_string (" -- %s of %s: %s", category, type_name, defined_name);
      break;
    case defop:
      execute_string (" -- %s on %s: %s", category, type_name, defined_name);
      break;
    case deftypemethod:
      execute_string (" -- %s on %s: %s %s", category, type_name, type_name2,
		      defined_name);
      break;
    default:
      break;
    }
  current_indent += default_indentation_increment;

  /* Now process the function arguments, if any.
     If these carry onto the next line, they should be indented by two
     increments to distinguish them from the body of the definition,
     which is indented by one increment.  */
  current_indent += default_indentation_increment;

  switch (base_type)
    {
    case deffn:
    case defop:
      process_defun_args (scan_args, 1);
      break;
    case deftp:
    case deftypefn:
    case deftypemethod:
      process_defun_args (scan_args, 0);
      break;
    default:
      break;
    }
  current_indent -= default_indentation_increment;
  close_single_paragraph ();

  /* Make an entry in the appropriate index. */
  switch (base_type)
    {
    case deffn:
    case deftypefn:
      execute_string ("@findex %s\n", defined_name);
      break;
    case defvr:
    case deftypevr:
    case defcv:
      execute_string ("@vindex %s\n", defined_name);
      break;
    case defop:
    case deftypemethod:
      execute_string ("@findex %s on %s\n", defined_name, type_name);
      break;
    case deftp:
      execute_string ("@tindex %s\n", defined_name);
      break;
    default:
      break;
    }

  /* Deallocate the token list. */
  scan_args = defun_args;
  while (1)
    {
      char * arg = (*scan_args++);
      if (arg == NULL)
	break;
      free (arg);
    }
  free (defun_args);
}

/* Add an entry for a function, macro, special form, variable, or option.
   If the name of the calling command ends in `x', then this is an extra
   entry included in the body of an insertion of the same type. */
void
cm_defun ()
{
  int x_p;
  enum insertion_type type;
  char *temp = savestring (command);

  x_p = (command[strlen (command) - 1] == 'x');

  if (x_p)
    temp[strlen (temp) - 1] = '\0';

  type = find_type_from_name (temp);
  free (temp);

  /* If we are adding to an already existing insertion, then make sure
     that we are already in an insertion of type TYPE. */
  if (x_p &&
      (!insertion_level || insertion_stack->insertion != type))
    {
      line_error ("Must be in a `%s' insertion in order to use `%s'x",
		  command, command);
      discard_until ("\n");
      return;
    }

  defun_internal (type, x_p);
}

/* End existing insertion block. */
void
cm_end ()
{
  char *temp;
  enum insertion_type type;

  if (!insertion_level)
    {
      line_error ("Unmatched `@%s'", command);
      return;
    }

  get_rest_of_line (&temp);
  canon_white (temp);

  if (strlen (temp) == 0)
    line_error ("`@%s' needs something after it", command);

  type = find_type_from_name (temp);

  if (type == bad_type)
    {
      line_error ("Bad argument to `%s', `%s', using `%s'",
	   command, temp, insertion_type_pname (current_insertion_type ()));
    }
  end_insertion (type);
  free (temp);
}


/* **************************************************************** */
/*								    */
/*			Other Random Commands		   	    */
/*								    */
/* **************************************************************** */

/* This says to inhibit the indentation of the next paragraph, but
   not of following paragraphs.  */
void
cm_noindent ()
{
  if (!inhibit_paragraph_indentation)
    inhibit_paragraph_indentation = -1;
}

/* I don't know exactly what to do with this.  Should I allow
   someone to switch filenames in the middle of output?  Since the
   file could be partially written, this doesn't seem to make sense.
   Another option: ignore it, since they don't *really* want to
   switch files.  Finally, complain, or at least warn. */
void
cm_setfilename ()
{
  char *filename;
  get_rest_of_line (&filename);
  /* warning ("`@%s %s' encountered and ignored", command, filename); */
  free (filename);
}

void
cm_comment ()
{
  discard_until ("\n");
}

/* @br can be immediately followed by `{}', so we have to read those here.
   It should simply close the paragraph. */
void
cm_br ()
{
  if (looking_at ("{}"))
    input_text_offset += 2;

  if (curchar () == '\n')
    {
      input_text_offset++;
      line_number++;
    }

  close_paragraph ();
}

 /* Insert the number of blank lines passed as argument. */
void
cm_sp ()
{
  int lines;
  char *line;

  get_rest_of_line (&line);

  sscanf (line, "%d", &lines);
  while (lines--)
    add_char ('\n');
  free (line);
}

void
cm_settitle ()
{
  discard_until ("\n");
}

void
cm_need ()
{
  discard_until ("\n");
}

void
cm_headings ()
{
  discard_until ("\n");
}

/* Start a new line with just this text on it.
   Then center the line of text.
   This always ends the current paragraph. */
void
cm_center ()
{
  char *line;
  size_t start, length;

  close_paragraph ();
  filling_enabled = indented_fill = 0;

  get_rest_of_line (&line);

  start = output_paragraph_offset;
  execute_string (line);
  length = output_paragraph_offset - start;
  free (line);

  if ((int)length < fill_column)
    {
      int i;

      line = (char *)xmalloc (1 + length);
      strncpy (line, output_paragraph + start, length);
      line[length] = '\0';

      i = ((int)fill_column - length) / 2;
      output_paragraph_offset = start;
      while (i--)
	insert (' ');

      for (i = 0; i < length; i++)
	insert (line[i]);

      free (line);
    }

  insert ('\n');
  close_paragraph ();
  filling_enabled = 1;
}

/* Show what an expression returns. */
void
cm_result (arg)
     int arg;
{
  if (arg == END)
    add_word ("=>");
}

/* What an expression expands to. */
void
cm_expansion (arg)
     int arg;
{
  if (arg == END)
    add_word ("==>");
}

/* Indicates two expressions are equivalent. */
void
cm_equiv (arg)
     int arg;
{
  if (arg == END)
    add_word ("==");
}

/* What an expression may print. */
void
cm_print (arg)
     int arg;
{
  if (arg == END)
    add_word ("-|");
}

/* An error signaled. */
void
cm_error (arg)
     int arg;
{
  if (arg == END)
    add_word ("error-->");
}

/* The location of point in an example of a buffer. */
void
cm_point (arg)
     int arg;
{
  if (arg == END)
    add_word ("-!-");
}

/* Start a new line with just this text on it.
   The text is outdented one level if possible. */
void
cm_exdent ()
{
  char *line;
  int i = current_indent;

  if (current_indent)
    current_indent -= default_indentation_increment;

  get_rest_of_line (&line);
  close_single_paragraph ();
  add_word_args ("%s", line);
  current_indent = i;
  free (line);
  close_single_paragraph ();
}

void
cm_include ()
{
  cm_infoinclude ();
}

/* Remember this file, and move onto the next. */
void
cm_infoinclude ()
{
  char *filename;

  close_paragraph ();
  get_rest_of_line (&filename);
  pushfile ();

  /* In verbose mode we print info about including another file. */
  if (verbose_mode)
    {
      register int i = 0;
      register FSTACK *stack = filestack;

      for (i = 0, stack = filestack; stack; stack = stack->next, i++);

      i *= 2;

      printf ("%*s", i, "");
      printf ("%c%s %s\n", COMMAND_PREFIX, command, filename);
      fflush (stdout);
    }

  if (!find_and_load (filename))
    {
      extern char *sys_errlist[];
      extern int errno, sys_nerr;
      popfile ();

      /* Cannot "@include foo", in line 5 of "/wh/bar". */
      line_error ("`%c%s %s': %s", COMMAND_PREFIX, command, filename,
		  ((errno < sys_nerr) ?
		   sys_errlist[errno] : "Unknown file system error"));
    }
  free (filename);
}

/* The other side of a malformed expression. */
void
misplaced_brace ()
{
  line_error ("Misplaced `}'");
}

/* Don't let the filling algorithm insert extra whitespace here. */
void
cm_force_abbreviated_whitespace ()
{
}

/* Do not let this character signify the end of a sentence, though
   if it was seen without the command prefix it normally would.  We
   do this by turning on the 8th bit of the character. */
void
cm_ignore_sentence_ender ()
{
  add_char (META ((*command)));
}

/* Signals end of processing.  Easy to make this happen. */
void
cm_bye ()
{
  input_text_offset = size_of_input_text;
}

void
cm_asis ()
{
}

void
cm_math ()
{
}

void
cm_setchapternewpage ()
{
  discard_until ("\n");
}

void
cm_smallbook ()
{
  discard_until ("\n");
}

/* **************************************************************** */
/*								    */
/*			Indexing Stuff				    */
/*								    */
/* **************************************************************** */

#if 0  /* this already declared - here for a reference */
typedef struct index_elt
{
  struct index_elt *next;
  char *entry;			/* The index entry itself. */
  char *node;			/* The node from whence it came. */
  int code;			/* Non-zero means add `@code{...}' when
				   printing this element. */
  size_t defining_line;		/* Line number where this entry was written. */
} INDEX_ELT;

typedef struct
{
  char *name;
  int index;
  int code;
} INDEX_ALIST;
#endif /* 0 -- this already declared */



/* We predefine these. */
#define program_index 0
#define function_index 1
#define concept_index 2
#define variable_index 3
#define datatype_index 4
#define key_index 5

void
init_indices ()
{
  int i;

  /* Create the default data structures. */

  /* Initialize data space. */
  if (!the_indices)
    {
      the_indices = (INDEX_ELT **) xmalloc ((1 + defined_indices) *
					    sizeof (INDEX_ELT *));
      the_indices[defined_indices] = (INDEX_ELT *) NULL;

      name_index_alist = (INDEX_ALIST **) xmalloc ((1 + defined_indices) *
						   sizeof (INDEX_ALIST *));
      name_index_alist[defined_indices] = (INDEX_ALIST *) NULL;
    }

  /* If there were existing indices, get rid of them now. */
  for (i = 0; i < defined_indices; i++)
    undefindex (name_index_alist[i]->name);

  /* Add the default indices. */
  defindex ("pg", 0);
  defindex ("fn", 1);		/* "fn" is a code index.  */
  defindex ("cp", 0);
  defindex ("vr", 0);
  defindex ("tp", 0);
  defindex ("ky", 0);

}

/* Find which element in the known list of indices has this name.
   Returns -1 if NAME isn't found. */
int
find_index_offset (name)
     const char *name;
{
  register int i;
  for (i = 0; i < defined_indices; i++)
    if (name_index_alist[i] &&
	stricmp (name, name_index_alist[i]->name) == 0)
      return (name_index_alist[i]->index);
  return (-1);
}

/* Return a pointer to the entry of (name . index) for this name.
   Return NULL if the index doesn't exist. */
INDEX_ALIST *
find_index (name)
     const char *name;
{
  int offset = find_index_offset (name);
  if (offset > -1)
    return (name_index_alist[offset]);
  else
    return ((INDEX_ALIST *) NULL);
}

/* Given an index name, return the offset in the_indices of this index,
   or -1 if there is no such index. */
int
translate_index (name)
     const char *name;
{
  INDEX_ALIST *which = find_index (name);

  if (which)
    return (which->index);
  else
    return (-1);
}

/* Return the index list which belongs to NAME. */
INDEX_ELT *
index_list (name)
     const char *name;
{
  int which = translate_index (name);
  if (which < 0)
    return ((INDEX_ELT *) -1L);
  else
    return (the_indices[which]);
}

/* Please release me, let me go... */
void
free_index (index)
     INDEX_ELT *index;
{
  INDEX_ELT *temp;

  while ((temp = index) != (INDEX_ELT *) NULL)
    {
      free (temp->entry);
      free (temp->node);
      index = index->next;
      free (temp);
    }
}

/* Flush an index by name. */
void
undefindex (name)
     const char *name;
{
  int i;
  int which = (name ? find_index_offset (name) : -1);

  if (which < 0 && name_index_alist[which])
/*    return (which); */ /* this is not used anyway */
      return;

  i = name_index_alist[which]->index;


  free_index (the_indices[i]);
  the_indices[i] = (INDEX_ELT *) NULL;

  free (name_index_alist[which]->name);
  free (name_index_alist[which]);
  name_index_alist[which] = (INDEX_ALIST *) NULL;
}

/* Define an index known as NAME.  We assign the slot number.
   CODE if non-zero says to make this a code index. */
void
defindex (name, code)
     const char *name;
     int code;
{
  register int i, slot;

  /* If it already exists, flush it. */
  undefindex (name);

  /* Try to find an empty slot. */
  slot = -1;
  for (i = 0; i < defined_indices; i++)
    if (!name_index_alist[i])
      {
	slot = i;
	break;
      }

  if (slot < 0)
    {
      /* No such luck.  Make space for another index. */
      slot = defined_indices;
      defined_indices++;

      name_index_alist = (INDEX_ALIST **)
	xrealloc ((char *)name_index_alist,
		  (1 + defined_indices) * sizeof (INDEX_ALIST *));
      the_indices = (INDEX_ELT **)
	xrealloc ((char *)the_indices,
		  (1 + defined_indices) * sizeof (INDEX_ELT *));
    }

  /* We have a slot.  Start assigning. */
  name_index_alist[slot] = (INDEX_ALIST *) xmalloc (sizeof (INDEX_ALIST));
  name_index_alist[slot]->name = savestring (name);
  name_index_alist[slot]->index = slot;
  name_index_alist[slot]->code = code;

  the_indices[slot] = (INDEX_ELT *) NULL;
}

/* Add the arguments to the current index command to the index NAME. */
void
index_add_arg (name)
     const char *name;
{
  int which;
  char *index_entry;
  INDEX_ALIST *tem;

  tem = find_index (name);

  which = tem ? tem->index : -1;

  get_rest_of_line (&index_entry);
  ignore_blank_line ();

  if (which < 0)
    {
      line_error ("Unknown index reference `%s'", name);
      free (index_entry);
    }
  else
    {
      INDEX_ELT *new = (INDEX_ELT *) xmalloc (sizeof (INDEX_ELT));
      new->next = the_indices[which];
      new->entry = index_entry;
      new->node = current_node;
      new->code = tem->code;
      new->defining_line = line_number - 1;
      the_indices[which] = new;
    }
}

#define INDEX_COMMAND_SUFFIX "index"

/* The function which user defined index commands call. */
void
gen_index ()
{
  char *name = savestring (command);
  size_t len = strlen(name);

  /* if (len  >= strlen ("index")) */
  /*  name[len - strlen ("index")] = '\0'; */
  if (len  >= sizeof("index") - 1)
    name[len - (sizeof("index") - 1)] = '\0';
  index_add_arg (name);
  free (name);
}

/* Define a new index command.  Arg is name of index. */
void
cm_defindex ()
{
  gen_defindex (0);
}

void
cm_defcodeindex ()
{
  gen_defindex (1);
}

void
gen_defindex (code)
     int code;
{
  char *name;
  get_rest_of_line (&name);

  if (find_index (name))
    {
      line_error ("Index `%s' already exists", name);
      free (name);
      return;
    }
  else
    {
/*      char *temp = (char *) alloca (1 + strlen (name) + strlen ("index")); */
      char *temp = (char *) alloca (1 + strlen (name) + sizeof("index") - 1);
      sprintf (temp, "%sindex", name);
      define_user_command (temp, (FUNCTION *)gen_index, 0);
      defindex (name, code);
      free (name);
    }
}

/* Append LIST2 to LIST1.  Return the head of the list. */
INDEX_ELT *
index_append (head, tail)
     INDEX_ELT *head, *tail;
{
  register INDEX_ELT *t_head = head;

  if (!t_head)
    return (tail);

  while (t_head->next)
    t_head = t_head->next;
  t_head->next = tail;
  return (head);
}

/* Expects 2 args, on the same line.  Both are index abbreviations.
   Make the first one be a synonym for the second one, i.e. make the
   first one have the same index as the second one. */
void
cm_synindex ()
{
  int redirector, redirectee;
  char *temp;

  skip_whitespace ();
  temp = read_token(); /* was --  get_until_in_line (" ", &temp); */
  redirectee = find_index_offset (temp);
  skip_whitespace ();
  free_and_clear (&temp);
  temp = read_token(); /* was --  get_until_in_line (" ", &temp); */
  redirector = find_index_offset (temp);
  free (temp);
  if (redirector < 0 || redirectee < 0)
    {
      line_error ("Unknown index reference");
    }
  else
    {
      /* I think that we should let the user make indices synonymous to
         each other without any lossage of info.  This means that one can
         say @synindex cp dt anywhere in the file, and things that used to
         be in cp will go into dt. */
      INDEX_ELT *i1 = the_indices[redirectee], *i2 = the_indices[redirector];

      if (i1 || i2)
	{
	  if (i1)
	    the_indices[redirectee] = index_append (i1, i2);
	  else
	    the_indices[redirectee] = index_append (i2, i1);
	}

      name_index_alist[redirectee]->index =
	name_index_alist[redirector]->index;
    }
}

void
cm_pindex ()			/* Pinhead index. */
{
  index_add_arg ("pg");
}

void
cm_vindex ()			/* Variable index. */
{
  index_add_arg ("vr");
}

void
cm_kindex ()			/* Key index. */
{
  index_add_arg ("ky");
}

void
cm_cindex ()			/* Concept index. */
{
  index_add_arg ("cp");
}

void
cm_findex ()			/* Function index. */
{
  index_add_arg ("fn");
}

void
cm_tindex ()			/* Data Type index. */
{
  index_add_arg ("tp");
}

/* Sorting the index. */
int
index_element_compare (element1, element2)
     INDEX_ELT **element1, **element2;
{
  /* This needs to ignore leading non-text characters. */
  return (strcmp ((*element1)->entry, (*element2)->entry));
}

/* Sort the index passed in INDEX, returning an array of
   pointers to elements.  The array is terminated with a NULL
   pointer.  We call qsort because it's supposed to be fast.
   I think this looks bad. */
INDEX_ELT **
sort_index (index)
     INDEX_ELT *index;
{
  INDEX_ELT *temp = index;
  INDEX_ELT **array;
  int count = 0;

  while (temp != (INDEX_ELT *) NULL)
    {
      count++;
      temp = temp->next;
    }

  /* We have the length.  Make an array. */

  array = (INDEX_ELT **) xmalloc ((count + 1) * sizeof (INDEX_ELT *));
  count = 0;
  temp = index;

  while (temp != (INDEX_ELT *) NULL)
    {
      array[count++] = temp;
      temp = temp->next;
    }
  array[count] = (INDEX_ELT *) NULL;	/* terminate the array. */

  /* Sort the array. */
  qsort (array, (size_t)count, sizeof (INDEX_ELT *), index_element_compare);

  return (array);
}

/* Non-zero means that we are in the middle of printing an index. */
int printing_index = 0;

/* Takes one arg, a short name of an index to print.
   Outputs a menu of the sorted elements of the index. */
void
cm_printindex ()
{
  int item;
  INDEX_ELT *index;
  INDEX_ELT **array;
  char *index_name;
  int old_inhibitions = inhibit_paragraph_indentation;
  int previous_filling_enabled_value = filling_enabled;

  close_paragraph ();
  get_rest_of_line (&index_name);

  index = index_list (index_name);
  if ((long) index == -1L)
    {
      line_error ("Unknown index name `%s'", index_name);
      free (index_name);
      return;
    }
  else
    free (index_name);

  array = sort_index (index);

  filling_enabled = 0;
  inhibit_paragraph_indentation = 1;
  close_paragraph ();
  add_word ("* Menu:\n\n");

  printing_index = 1;
  for (item = 0; (index = array[item]); item++)
    {
      size_t real_line_number = line_number;

      /* Let errors generated while making the index entry point back
	 at the line which contains the entry. */
      line_number = index->defining_line;

      /* If this particular entry should be printed as a "code" index,
	 then wrap the entry with "@code{...}". */
      if (index->code)
	execute_string ("* @code{%s}: ", index->entry);
      else
	execute_string ("* %s: ", index->entry);

      /* Pad the front of the destination nodename so that
	 the output looks nice. */
      if (fill_column > 40 && output_column < 40)
	indent (40 - output_column);

      execute_string ("%s.\n", index->node);

      line_number = real_line_number;
      flush_output ();
    }

  printing_index = 0;
  free (array);
  close_single_paragraph ();
  filling_enabled = previous_filling_enabled_value;
  inhibit_paragraph_indentation = old_inhibitions;
}


/* **************************************************************** */
/*								    */
/*		    Making User Defined Commands		    */
/*								    */
/* **************************************************************** */

void
define_user_command (name, proc, needs_braces_p)
     char *name;
     FUNCTION *proc;
     int needs_braces_p;
{
  int slot = user_command_array_len;
  user_command_array_len++;

  if (!user_command_array)
    user_command_array = (COMMAND **) xmalloc (1 * sizeof (COMMAND *));

  user_command_array = (COMMAND **) xrealloc (user_command_array,
					      (1 + user_command_array_len) *
					      sizeof (COMMAND *));

  user_command_array[slot] = (COMMAND *) xmalloc (sizeof (COMMAND));
  user_command_array[slot]->name = savestring (name);
  user_command_array[slot]->proc = proc;
  user_command_array[slot]->argument_in_braces = needs_braces_p;
}

/* Make ALIAS run the named FUNCTION.  Copies properties from FUNCTION. */
void
define_alias (alias, function)
     char *alias, *function;
{
}

/* Set the paragraph indentation variable to the value specified in STRING.
   Values can be:
   `asis': Don't change existing indentation.
   `none': Remove existing indentation.
      NUM: Indent NUM spaces at the starts of paragraphs.
           Note that if NUM is zero, we assume `none'.

   Returns 0 if successful, or non-zero if STRING isn't one of the above. */
int
set_paragraph_indent (string)
     char *string;
{
  if (strcmp (string, "asis") == 0)
    paragraph_start_indent = 0;
  else if (strcmp (string, "none") == 0)
    paragraph_start_indent = -1;
  else
    {
      if (sscanf (string, "%d", &paragraph_start_indent) != 1)
	return (-1);
      else
	{
	  if (paragraph_start_indent == 0)
	    paragraph_start_indent = -1;
	}
    }
  return (0);
}

void
cm_paragraphindent ()
{
  char *arg;

  get_rest_of_line (&arg);
  if (set_paragraph_indent (arg) != 0)
    line_error ("Bad argument to @paragraphindent");

  free (arg);
}

/* Some support for footnotes. */

/* Footnotes are a new construct in Info.  We don't know the best method
   of implementing them for sure, so we present two possiblities.

   SeparateNode:
	Make them look like followed references, with the reference
	destinations in a makeinfo manufactured node or,

   EndNode:
	Make them appear at the bottom of the node that they originally
	appeared in. */
#define SeparateNode 0
#define EndNode 1

int footnote_style = EndNode;
int first_footnote_this_node = 1;
int footnote_count = 0;

/* Set the footnote style based on he style identifier in STRING. */
int
set_footnote_style (string)
     char *string;
{
  if ((stricmp (string, "separate") == 0) ||
      (stricmp (string, "MN") == 0))
    footnote_style = SeparateNode;
  else if ((stricmp (string, "end") == 0) ||
	   (stricmp (string, "EN") == 0))
    footnote_style = EndNode;
  else
    return (-1);

 return (0);
}

void
cm_footnotestyle ()
{
  char *arg;

  get_rest_of_line (&arg);

  if (set_footnote_style (arg) != 0)
    line_error ("Bad argument to @footnotestyle");

  free (arg);
}

typedef struct fn
{
  struct fn *next;
  char *marker;
  char *note;
}  FN;

FN *pending_notes = (FN *) NULL;

/* A method for remembering footnotes.  Note that this list gets output
   at the end of the current node. */
void
remember_note (marker, note)
     char *marker, *note;
{
  FN *temp = (FN *) xmalloc (sizeof (FN));

  temp->marker = savestring (marker);
  temp->note = savestring (note);
  temp->next = pending_notes;
  pending_notes = temp;
  footnote_count++;
}

/* How to get rid of existing footnotes. */
void
free_pending_notes ()
{
  FN *temp;

  while ((temp = pending_notes) != (FN *) NULL)
    {
      free (temp->marker);
      free (temp->note);
      pending_notes = pending_notes->next;
      free (temp);
    }
  first_footnote_this_node = 1;
  footnote_count = 0;
}

/* What to do when you see a @footnote construct. */

 /* Handle a "footnote".
    footnote *{this is a footnote}
    where "*" is the marker character for this note. */
void
cm_footnote ()
{
  char *marker;
  char *note;

  get_until ("{", &marker);
  canon_white (marker);

  /* Read the argument in braces. */
  if (curchar () != '{')
    {
      line_error ("`@%s' expected more than just `%s'.  It needs something in `{...}'", command, marker);
      free (marker);
      return;
    }
  else
    {
      int braces = 1;
      size_t temp = ++input_text_offset;
      size_t len;

      while (braces)
	{
	  if (temp == size_of_input_text)
	    {
	      line_error ("No closing brace for footnote `%s'", marker);
	      return;
	    }

	  if (input_text[temp] == '{')
	    braces++;
	  else if (input_text[temp] == '}')
	    braces--;
	  else if (input_text[temp] == '\n')
	    line_number ++;

	  temp++;
	}

      len = (temp - input_text_offset) - 1;
      note = (char *)xmalloc (len + 1);
      strncpy (note, &input_text[input_text_offset], len);
      note[len] = '\0';
      input_text_offset = temp;
    }

  if (!current_node || !*current_node)
    {
      line_error ("Footnote defined without parent node");
      free (marker);
      free (note);
      return;
    }

  if (!*marker)
    {
      free (marker);

      if (number_footnotes)
	{
	  marker = (char *)xmalloc ((size_t)10);
	  sprintf (marker, "%d", current_footnote_number);
	  current_footnote_number++;
	}
      else
	marker = savestring ("*");
    }

  remember_note (marker, note);

  /* Your method should at least insert MARKER. */
  switch (footnote_style)
    {
    case SeparateNode:
      add_word_args ("(%s)", marker);
      if (first_footnote_this_node)
	{
	  char *temp_string;

	  temp_string = (char *)
/*	    xmalloc ((strlen (current_node)) + (strlen ("-Footnotes")) + 1); */
	    xmalloc (strlen (current_node) + sizeof("-Footnotes"));

	  add_word_args (" (*note %s-Footnotes::)", current_node);
	  strcpy (temp_string, current_node);
	  strcat (temp_string, "-Footnotes");
	  remember_node_reference (temp_string, line_number, followed_reference);
	  free (temp_string);
	  first_footnote_this_node = 0;
	}
      break;

    case EndNode:
      add_word_args ("(%s)", marker);
      break;

    default:
      break;
    }
  free (marker);
  free (note);
}

/* Non-zero means that we are currently in the process of outputting
   footnotes. */
int already_outputting_pending_notes = 0;

/* Output the footnotes.  We are at the end of the current node. */
void
output_pending_notes ()
{
  FN *footnote = pending_notes;

  if (!pending_notes)
    return;

  switch (footnote_style)
    {

    case SeparateNode:
      {
	char *old_current_node = current_node;
	char *old_command = savestring (command);

	already_outputting_pending_notes++;
	execute_string ("@node %s-Footnotes,,,%s\n", current_node, current_node);
	already_outputting_pending_notes--;
	current_node = old_current_node;
	free (command);
	command = old_command;
      }
      break;

    case EndNode:
      close_paragraph ();
      in_fixed_width_font++;
      execute_string ("---------- Footnotes ----------\n\n");
      in_fixed_width_font--;
      break;
    }

  /* Handle the footnotes in reverse order. */
  {
    FN **array = (FN **) xmalloc ((footnote_count + 1) * sizeof (FN *));

    array[footnote_count] = (FN *) NULL;

    while (--footnote_count > -1)
      {
	array[footnote_count] = footnote;
	footnote = footnote->next;
      }

    filling_enabled = 1;
    indented_fill = 1;

    while (footnote = array[++footnote_count])
      {

	switch (footnote_style)
	  {
	  case SeparateNode:
	  case EndNode:
	    execute_string ("(%s)  %s", footnote->marker, footnote->note);
	    close_paragraph ();
	    break;
	  }
      }
    close_paragraph ();
    free (array);
  }
}


/* **************************************************************** */
/*                                                                  */
/*              User definable Macros (text substitution)	    */
/*                                                                  */
/* **************************************************************** */

#if defined (HAVE_MACROS)

/* Array of macros and definitions. */
MACRO_DEF **macro_list = (MACRO_DEF **)NULL;

int macro_list_len = 0;		/* Number of elements. */
int macro_list_size = 0;	/* Number of slots in total. */

/* Return the macro definition of NAME or NULL if NAME is not defined. */
MACRO_DEF *
find_macro (name)
     char *name;
{
  register int i;
  register MACRO_DEF *def;

  def = (MACRO_DEF *)NULL;
  for (i = 0; macro_list && (def = macro_list[i]); i++)
    if (strcmp (def->name, name) == 0)
      break;

  return (def);
}

/* Add the macro NAME with DEFINITION to macro_list.  FILENAME is
   the name of the file where this definition can be found, and
   LINENO is the line number within that file.  If a macro already
   exists with NAME, then a warning is produced, and that previous
   definition is overwritten. */
void
add_macro (name, definition, filename, lineno)
     char *name, *definition;
     char *filename;
     size_t lineno;
{
  register MACRO_DEF *def;

  def = find_macro (name);

  if (!def)
    {
      if (macro_list_len + 2 >= macro_list_size)
	macro_list = (MACRO_DEF **)xrealloc
	  (macro_list, ((macro_list_size += 10) * sizeof (MACRO_DEF *)));

      macro_list[macro_list_len] = (MACRO_DEF *)xmalloc (sizeof (MACRO_DEF));
      macro_list[macro_list_len + 1] = (MACRO_DEF *)NULL;

      def = macro_list[macro_list_len];
      macro_list_len += 1;
      def->name = savestring (name);
    }
  else
    {
      char *temp_filename = input_filename;
      size_t temp_line = line_number;

      warning ("The macro `%s' is previously defined.", name);

      input_filename = def->filename;
      line_number = def->lineno;

      warning ("Here is the previous definition of `%s'.", name);

      input_filename = temp_filename;
      line_number = temp_line;

      free (def->filename);
      free (def->definition);
    }

  def->filename = savestring (filename);
  def->lineno = lineno;
  def->definition = savestring (definition);
}


/* Delete the macro with name NAME.  The macro is deleted from the list,
   but it is also returned.  If there was no macro defined, NULL is
   returned. */
MACRO_DEF *
delete_macro (name)
     char *name;
{
  register int i;
  register MACRO_DEF *def;

  def = (MACRO_DEF *)NULL;
  for (i = 0; macro_list && (def = macro_list[i]); i++)
    if (strcmp (def->name, name) == 0)
      {
	bcopy (macro_list + i + 1, macro_list + i,
	       ((macro_list_len + 1) - i) * sizeof (MACRO_DEF *));
	break;
      }
  return (def);
}

/* Execute the macro passed in DEF, a pointer to a MACRO_DEF. */
void
execute_macro (def)
     MACRO_DEF *def;
{

  if (def != (MACRO_DEF *)NULL)
    {
      char *line, *string;

      get_until ("\n", &line);

      if (curchar () == '\n')	/* as opposed to the end of the file... */
	{
	  line_number++;
	  input_text_offset++;
	}
      string = (char *)xmalloc (1 + strlen (def->definition) + strlen (line));
      strcpy (string, def->definition);
      strcat (string, line);
      free (line);

      execute_string ("%s\n", string);
      free (string);
    }
}

void
cm_macro ()
{
  register size_t i;
  char *line, *name;

  get_rest_of_line (&line);
  canon_white (line);

  for (i = 0; line[i] && !whitespace (line[i]); i++);
  name = (char *)xmalloc (i);
  strncpy (name, line, i);
  name[i] = '\0';

  while (whitespace (line[i]))
    i++;

  add_macro (name, line + i, input_filename, line_number);
  free (line);
  free (name);
}

void
cm_unmacro ()
{
  register size_t i;
  char *line, *name;
  MACRO_DEF *def;

  get_rest_of_line (&line);
  canon_white (line);

  for (i = 0; line[i] && !whitespace (line[i]); i++);
  name = (char *)xmalloc (i);
  strncpy (name, line, i);
  name[i] = '\0';

  def = delete_macro (name);

  if (def)
    {
      free (def->filename);
      free (def->name);
      free (def->definition);
      free (def);
    }

  free (line);
  free (name);
}
#endif /* HAVE_MACROS */

#define TAG_TABLE_END_STRING "\037\nEND TAG TABLE"
#define TAG_TABLE_BEG_STRING "\nTAG TABLE:\n"
#define NODE_ID "Node:"
#define nodeend_sequence "\n\037"
#define SLACK 8

/* Build a new tag_table from a freshly loaded info file.
   We need that for splitting, so the only info filled out
   is next_ent, node name and position.
   Takes buffer with a freshly loaded info file and its size.
   Returns a pointer to a newly created tag_table or NULL in
   a case of failure. */

TAG_ENTRY *
restore_tag_table (the_file, size)
     char *the_file;
     size_t size;
{
   size_t loc = size;
   char *node;

   loc -= sizeof(TAG_TABLE_END_STRING) + SLACK;  /* leave some reserve
						    for a trailing garbage */
   if ((-1L) == (loc = search_forward (TAG_TABLE_END_STRING, loc)))
     return NULL;				  /* no tag table */
   loc -= sizeof(nodeend_sequence) + sizeof(NODE_ID);
   while (loc > 0) {
     if (0 == strncmp (nodeend_sequence, &the_file[loc],
		       sizeof(nodeend_sequence) - 1))
       break;
     loc -= 1;
   }
   if (0 == loc)
     return NULL;
   loc += sizeof(nodeend_sequence) - 1;
   if ((-1L) == (loc = search_forward (TAG_TABLE_BEG_STRING, loc)))
     return NULL;	/* we are lost - get out */

   /* if we are here the we found at last where out tags start */
   loc += sizeof(TAG_TABLE_BEG_STRING) - 1;

   init_tag_table ();   /* this actually frees entries in tag_table */
   while ((-1L) != (loc = search_forward (NODE_ID, loc))) {
     loc += sizeof(NODE_ID);  /* one extra character for a trailing space */
     node = &the_file[loc];
     while (loc < size_of_input_text && '\177' != the_file[loc])
       loc += 1;
     if (loc == size_of_input_text) {
       /* we are way too far */
       init_tag_table ();
       return NULL;
     }
     the_file[loc++] = '\0'; /* terminate node name */
     /* add new TAG_ENTRY to the the list */
     {
       char *endp;  /* for strtol() */
       TAG_ENTRY *new = (TAG_ENTRY *) xmalloc (sizeof (TAG_ENTRY));
       bzero (new, sizeof (TAG_ENTRY));
       new->node = savestring(node);
       new->next_ent = tag_table;
       tag_table = new;
       /* this should be strtoul but it is broken in a quite
	  few systems - sigh... */
       new->position = strtol (&the_file[loc], &endp, 10);
       loc = endp - the_file;
     }
   }
   tag_table = (TAG_ENTRY *) reverse_list ((GENERIC_LIST *)tag_table);
   return tag_table;
}
