home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / tinfo210.lzh / TINFO210 / C / MAKEINFO.C < prev    next >
C/C++ Source or Header  |  1991-09-26  |  153KB  |  6,393 lines

  1. /* Makeinfo -- convert texinfo format files into info files
  2.  
  3.    Copyright (C) 1987, 1991 Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU Info.
  6.  
  7.    Makeinfo is distributed in the hope that it will be useful,
  8.    but WITHOUT ANY WARRANTY.  No author or distributor accepts
  9.    responsibility to anyone for the consequences of using it or for
  10.    whether it serves any particular purpose or works at all, unless he
  11.    says so in writing.  Refer to the GNU Emacs General Public License
  12.    for full details.
  13.  
  14.    Everyone is granted permission to copy, modify and redistribute
  15.    Makeinfo, but only under the conditions described in the GNU Emacs
  16.    General Public License.   A copy of this license is supposed to
  17.    have been given to you along with GNU Emacs so you can know your
  18.    rights and responsibilities.  It should be in a file named COPYING.
  19.    Among other things, the copyright notice and this notice must be
  20.    preserved on all copies.  */
  21.  
  22. /* **************************************************************** */
  23. /*                                    */
  24. /*            Include File Declarations               */
  25. /*                                    */
  26. /* **************************************************************** */
  27.  
  28. #if defined (_AIX)
  29. #  pragma alloca
  30. #endif /* _AIX */
  31.  
  32. #include <stdio.h>
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <ctype.h>
  36. #include <pwd.h>
  37. #include <errno.h>
  38. #include "getopt.h"
  39.  
  40. #if defined (VMS)
  41. #include <perror.h>
  42. #endif
  43.  
  44. #if defined (USG) || defined (VMS)
  45. #include <string.h>
  46. #include <time.h>
  47. #else
  48. #include <strings.h>
  49. #include <sys/time.h>
  50. #endif
  51.  
  52. #include <fcntl.h>
  53. #include <sys/file.h>
  54.  
  55. #if defined (USG) || defined (hpux)
  56. #define bcopy(source, dest, count) memcpy (dest, source, count)
  57. char *index(s,c) char *s; { char *strchr(); return strchr(s,c); }
  58. char *rindex(s,c) char *s; { char *strrchr(); return strrchr(s,c); }
  59. #endif /* USG || hpux */
  60.  
  61. #if defined (__GNUC__)
  62. #  define alloca __builtin_alloca
  63. #else
  64. #  if defined (sparc)
  65. #    include <alloca.h>
  66. #  else
  67. #    if !defined (_AIX)
  68. extern char *alloca ();
  69. #    endif /* _AIX */
  70. #  endif /* !sparc */
  71. #endif /* !__GNUC__ */
  72.  
  73. /* Forward declarations. */
  74. unsigned char *xmalloc (), *xrealloc ();
  75. extern int in_fixed_width_font;
  76.  
  77. /* Some systems don't declare this function in pwd.h. */
  78. struct passwd *getpwnam ();
  79.  
  80.  
  81. /* **************************************************************** */
  82. /*                                    */
  83. /*                  Global Defines                  */
  84. /*                                    */
  85. /* **************************************************************** */
  86.  
  87. /* Error levels */
  88. #define NO_ERROR 0
  89. #define SYNTAX     2
  90. #define FATAL     4
  91.  
  92. /* Boolean values. */
  93. #define true  1
  94. #define false 0
  95. typedef int boolean;
  96.  
  97. /* How to allocate permanent storage for STRING. */
  98. #define savestring(x) \
  99.   ((char *)strcpy ((char *)xmalloc (1 + ((x) ? strlen (x) : 0)), \
  100.            (x) ? (x) : ""))
  101.  
  102. /* C's standard macros don't check to make sure that the characters being
  103.    changed are within range.  So I have to check explicitly. */
  104.  
  105. /* GNU Library doesn't have toupper().  Until GNU gets this fixed, I will
  106.    have to do it. */
  107. #ifndef toupper
  108. #define toupper(c) ((c) - 32)
  109. #endif
  110.  
  111. #define coerce_to_upper(c) ((islower(c) ? toupper(c) : (c)))
  112. #define coerce_to_lower(c) ((isupper(c) ? tolower(c) : (c)))
  113.  
  114. #define control_character_bit 0x40 /* %01000000, must be off. */
  115. #define meta_character_bit 0x080/* %10000000, must be on.  */
  116. #define CTL(c) ((c) & (~control_character_bit))
  117. #define UNCTL(c) coerce_to_upper(((c)|control_character_bit))
  118. #define META(c) ((c) | (meta_character_bit))
  119. #define UNMETA(c) ((c) & (~meta_character_bit))
  120.  
  121. #define whitespace(c) (((c) == '\t') || ((c) == ' '))
  122. #define sentence_ender(c) ((c) == '.' || (c) == '?' || (c) == '!')
  123. #define cr_or_whitespace(c) (((c) == '\t') || ((c) == ' ') || ((c) == '\n'))
  124. #define member(c, s) (index (s, c) != NULL)
  125.  
  126. #define COMMAND_PREFIX '@'
  127.  
  128. /* Stuff for splitting large files. */
  129. #define SPLIT_SIZE_THRESHOLD 70000    /* What's good enough for Stallman... */
  130. #define DEFAULT_SPLIT_SIZE 50000/* Is probably good enough for me. */
  131. boolean splitting = true;    /* Always true for now. */
  132.  
  133. typedef int FUNCTION ();    /* So I can say FUNCTION *foo; */
  134.  
  135.  
  136. /* **************************************************************** */
  137. /*                                    */
  138. /*                Global Variables                */
  139. /*                                    */
  140. /* **************************************************************** */
  141.  
  142. /* Global pointer to argv[0]. */
  143. char *progname;
  144.  
  145. /* The current input file state. */
  146. char *input_filename;
  147. char *input_text;
  148. int size_of_input_text;
  149. int input_text_offset;
  150. int line_number;
  151.  
  152. #define curchar() input_text[input_text_offset]
  153.  
  154. #define command_char(c) ((!whitespace(c)) && \
  155.              ((c) != '\n') && \
  156.              ((c) != '{') && \
  157.              ((c) != '}'))
  158.  
  159. #define skip_whitespace() while (input_text_offset != size_of_input_text \
  160.                  && whitespace(curchar()))\
  161.   input_text_offset++
  162.  
  163. /* Return non-zero if STRING is the text at input_text + input_text_offset,
  164.    else zero. */
  165. #define looking_at(string) \
  166.   (strncmp (input_text + input_text_offset, string, strlen (string)) == 0)
  167.  
  168. /* And writing to the output. */
  169.  
  170. /* The output file name. */
  171. char *output_filename, *pretty_output_filename;
  172.  
  173. /* Current output stream. */
  174. FILE *output_stream;
  175.  
  176. /* Position in the output file. */
  177. int output_position;
  178.  
  179. /* Output paragraph buffer. */
  180. unsigned char *output_paragraph;
  181.  
  182. /* Offset into OUTPUT_PARAGRAPH. */
  183. int output_paragraph_offset;
  184.  
  185. /* The output paragraph "cursor" horizontal position. */
  186. int output_column = 0;
  187.  
  188. /* Non-zero means output_paragraph contains text. */
  189. boolean paragraph_is_open = false;
  190.  
  191. #define INITIAL_PARAGRAPH_SPACE 5000
  192. int paragraph_buffer_len = INITIAL_PARAGRAPH_SPACE;
  193.  
  194. /* Filling.. */
  195. /* True indicates that filling will take place on long lines. */
  196. boolean filling_enabled = true;
  197.  
  198. /* Non-zero means that words are not to be split, even in long lines.  This
  199.    gets changed for cm_w (). */
  200. int non_splitting_words = 0;
  201.  
  202. /* True indicates that filling a line also indents the new line. */
  203. boolean indented_fill = false;
  204.  
  205. /* The column at which long lines are broken. */
  206. int fill_column = 72;
  207.  
  208. /* The amount of indentation to apply at the start of each line. */
  209. int current_indent = 0;
  210.  
  211. /* The amount of indentation to add at the starts of paragraphs.
  212.    0 means don't change existing indentation at paragraph starts.
  213.    > 0 is amount to indent new paragraphs by.
  214.    < 0 means indent to column zero by removing indentation if necessary.
  215.  
  216.    This is normally zero, but some people prefer paragraph starts to be
  217.    somewhat more indented than paragraph bodies.  A pretty value for
  218.    this is 3. */
  219. int paragraph_start_indent = 3;
  220.  
  221. /* Non-zero means that the use of paragraph_start_indent is inhibited.
  222.    @example uses this to line up the left columns of the example text.
  223.    A negative value for this variable is incremented each time it is used.
  224.    @noindent uses this to inhibit indentation for a single paragraph.  */
  225. int inhibit_paragraph_indentation = 0;
  226.  
  227. /* Indentation that is pending insertion.  We have this for hacking lines
  228.    which look blank, but contain whitespace.  We want to treat those as
  229.    blank lines. */
  230. int pending_indent = 0;
  231.  
  232. /* The amount that indentation increases/decreases by. */
  233. int default_indentation_increment = 5;
  234.  
  235. /* True indicates that indentation is temporarily turned off. */
  236. boolean no_indent = true;
  237.  
  238. /* Non-zero means forciing output text to be flushright. */
  239. int force_flush_right = 0;
  240.  
  241. /* Non-zero means that we automatically number footnotes that have no
  242.    specified marker. */
  243. int number_footnotes = 1;
  244.  
  245. /* The current footnote number in this node.  Each time a new node is
  246.    started this is reset to 1. */
  247. int current_footnote_number = 1;
  248.  
  249. /* Command name in the process of being hacked. */
  250. char *command;
  251.  
  252. /* The index in our internal command table of the currently
  253.    executing command. */
  254. int command_index;
  255.  
  256. /* A stack of file information records.  If a new file is read in with
  257.    "@input", we remember the old input file state on this stack. */
  258. typedef struct fstack
  259. {
  260.   struct fstack *next;
  261.   char *filename;
  262.   char *text;
  263.   int size;
  264.   int offset;
  265.   int line_number;
  266. } FSTACK;
  267.  
  268. FSTACK *filestack = (FSTACK *) NULL;
  269.  
  270. /* Stuff for nodes. */
  271. /* The current nodes node name. */
  272. char *current_node = (char *)NULL;
  273.  
  274. /* The current nodes section level. */
  275. int current_section = 0;
  276.  
  277. /* The filename of the current input file.  This is never freed. */
  278. char *node_filename = (char *)NULL;
  279.  
  280. /* What we remember for each node. */
  281. typedef struct tentry
  282. {
  283.   struct tentry *next_ent;
  284.   char *node;        /* name of this node. */
  285.   char *prev;        /* name of "Prev:" for this node. */
  286.   char *next;        /* name of "Next:" for this node. */
  287.   char *up;        /* name of "Up:" for this node.   */
  288.   int position;        /* output file position of this node. */
  289.   int line_no;        /* defining line in source file. */
  290.   char *filename;    /* The file that this node was found in. */
  291.   int touched;        /* non-zero means this node has been referenced. */
  292.   int flags;        /* Room for growth.  Right now, contains 1 bit. */
  293. } TAG_ENTRY;
  294.  
  295. /* If node-a has a "Next" for node-b, but node-b has no "Prev" for node-a,
  296.    we turn on this flag bit in node-b's tag entry.  This means that when
  297.    it is time to validate node-b, we don't report an additional error
  298.    if there was no "Prev" field. */
  299. #define PREV_ERROR 0x1
  300. #define NEXT_ERROR 0x2
  301. #define UP_ERROR   0x4
  302. #define NO_WARN       0x8
  303. #define IS_TOP        0x10
  304.  
  305. TAG_ENTRY *tag_table = (TAG_ENTRY *) NULL;
  306.  
  307. /* Menu reference, *note reference, and validation hacking. */
  308.  
  309. /* The various references that we know about. */
  310. enum reftype
  311. {
  312.   menu_reference, followed_reference
  313. };
  314.  
  315. /* A structure to remember references with.  A reference to a node is
  316.    either an entry in a menu, or a cross-reference made with [px]ref. */
  317. typedef struct node_ref
  318. {
  319.   struct node_ref *next;
  320.   char *node;            /* Name of node referred to. */
  321.   char *containing_node;    /* Name of node containing this reference. */
  322.   int line_no;            /* Line number where the reference occurs. */
  323.   int section;            /* Section level where the reference occurs. */
  324.   char *filename;        /* Name of file where the reference occurs. */
  325.   enum reftype type;        /* Type of reference, either menu or note. */
  326. } NODE_REF;
  327.  
  328. /* The linked list of such structures. */
  329. NODE_REF *node_references = (NODE_REF *) NULL;
  330.  
  331. /* Flag which tells us whether to examine menu lines or not. */
  332. int in_menu = 0;
  333.  
  334. /* Flags controlling the operation of the program. */
  335.  
  336. /* Default is to notify users of bad choices. */
  337. boolean print_warnings = true;
  338.  
  339. /* Default is to check node references. */
  340. boolean validating = true;
  341.  
  342. /* Number of errors that we tolerate on a given fileset. */
  343. int max_error_level = 100;
  344.  
  345. /* Maximum number of references to a single node before complaining. */
  346. int reference_warning_limit = 1000;
  347.  
  348. /* Non-zero means print out information about what is going on when it
  349.    is going on. */
  350. int verbose_mode = 0;
  351.  
  352. /* The list of commands that we hack in texinfo.  Each one
  353.    has an associated function.  When the command is encountered in the
  354.    text, the associated function is called with START as the argument.
  355.    If the function expects arguments in braces, it remembers itself on
  356.    the stack.  When the corresponding close brace is encountered, the
  357.    function is called with END as the argument. */
  358.  
  359. #define START 0
  360. #define END 1
  361.  
  362. typedef struct brace_element
  363. {
  364.   struct brace_element *next;
  365.   FUNCTION *proc;
  366.   int pos, line;
  367. } BRACE_ELEMENT;
  368.  
  369. BRACE_ELEMENT *brace_stack = (BRACE_ELEMENT *) NULL;
  370.  
  371. /* Forward declarations. */
  372.  
  373. int
  374. insert_self (), cm_tex (), cm_asterisk (), cm_dots (), cm_bullet (),
  375. cm_TeX (), cm_copyright (), cm_code (), cm_samp (), cm_file (), cm_kbd (),
  376. cm_key (), cm_ctrl (), cm_var (), cm_dfn (), cm_emph (), cm_strong (),
  377. cm_cite (), cm_italic (), cm_bold (), cm_roman (), cm_title (), cm_w (),
  378. cm_refill ();
  379.  
  380. int
  381. cm_chapter (), cm_unnumbered (), cm_appendix (), cm_top (),
  382. cm_section (), cm_unnumberedsec (), cm_appendixsec (),
  383. cm_subsection (), cm_unnumberedsubsec (), cm_appendixsubsec (),
  384. cm_subsubsection (), cm_unnumberedsubsubsec (), cm_appendixsubsubsec (),
  385. cm_heading (), cm_chapheading (), cm_subheading (), cm_subsubheading (),
  386. cm_majorheading ();
  387.  
  388. /* All @defxxx commands map to cm_defun (). */
  389. int
  390. cm_defun ();
  391.  
  392. int
  393.   cm_node (), cm_menu (), cm_xref (), cm_ftable (), cm_alphaenumerate (),
  394.   cm_pxref (), cm_inforef (), cm_quotation (), cm_display (), cm_itemize (),
  395.   cm_enumerate (), cm_table (), cm_itemx (), cm_noindent (), cm_setfilename (),
  396.   cm_comment (), cm_br (), cm_sp (), cm_page (), cm_group (), cm_need (),
  397.   cm_center (), cm_include (), cm_bye (), cm_item (), cm_end (),
  398.   cm_infoinclude (), cm_ifinfo (), cm_kindex (), cm_cindex (), cm_findex (),
  399.   cm_pindex (), cm_vindex (), cm_tindex (), cm_asis (), cm_synindex (),
  400.   cm_settitle (), cm_setchapternewpage (), cm_printindex (), cm_minus (),
  401.   cm_footnote (), cm_force_abbreviated_whitespace (), cm_force_sentence_end (),
  402.   cm_example (), cm_smallexample (), cm_lisp (), cm_format (), cm_exdent (),
  403.   cm_defindex (), cm_defcodeindex (), cm_sc (), cm_result (), cm_expansion (),
  404.   cm_equiv (), cm_print (), cm_error (), cm_point (), cm_smallbook (),
  405.   cm_headings (), cm_today (), cm_flushleft (), cm_flushright (),
  406.   cm_smalllisp (), cm_finalout (), cm_math (), cm_capsenumerate (),
  407.   cm_cartouche ();
  408.  
  409. /* Conditionals. */
  410. int cm_set (), cm_clear (), cm_ifset (), cm_ifclear ();
  411.  
  412. /* Options. */
  413. int cm_paragraphindent (), cm_footnotestyle ();
  414.  
  415. /* Internals. */
  416. int do_nothing (), command_name_condition ();
  417. int misplaced_brace (), cm_obsolete ();
  418.  
  419. typedef struct
  420. {
  421.   char *name;
  422.   FUNCTION *proc;
  423.   boolean argument_in_braces;
  424. } COMMAND;
  425.  
  426. /* Stuff for defining commands on the fly. */
  427. COMMAND **user_command_array = (COMMAND **) NULL;
  428. int user_command_array_len = 0;
  429.  
  430. static COMMAND CommandTable[] = {
  431.   { "!", cm_force_sentence_end, false },
  432.   { "'", insert_self, false },
  433.   { "*", cm_asterisk, false },
  434.   { ".", cm_force_sentence_end, false },
  435.   { ":", cm_force_abbreviated_whitespace, false },
  436.   { "?", cm_force_sentence_end, false },
  437.   { "@", insert_self, false },
  438.   { " ", insert_self, false },
  439.   { "\n", insert_self, false },
  440.   { "TeX", cm_TeX, true },
  441.   { "`", insert_self, false },
  442.   { "alphaenumerate", cm_alphaenumerate, false },
  443.   { "appendix", cm_appendix, false },
  444.   { "appendixsection", cm_appendixsec, false },
  445.   { "appendixsec", cm_appendixsec, false },
  446.   { "appendixsubsec", cm_appendixsubsec, false },
  447.   { "appendixsubsubsec", cm_appendixsubsubsec, false },
  448.   { "asis", cm_asis, true },
  449.   { "b", cm_bold, true },
  450.   { "br", cm_br, false },
  451.   { "bullet", cm_bullet, true },
  452.   { "bye", cm_bye, false },
  453.   { "c", cm_comment, false },
  454.   { "capsenumerate", cm_capsenumerate, false },
  455.   { "cartouche", cm_cartouche, false },
  456.   { "center", cm_center, false },
  457.   { "chapheading", cm_chapheading, false },
  458.   { "chapter", cm_chapter, false },
  459.   { "cindex", cm_cindex, false },
  460.   { "cite", cm_cite, true },
  461.   { "clear", cm_clear, false },
  462.   { "code", cm_code, true },
  463.   { "comment", cm_comment, false },
  464.   { "contents", do_nothing, false },
  465.   { "copyright", cm_copyright, true },
  466.   { "ctrl", cm_ctrl, true },
  467.   { "defcodeindex", cm_defcodeindex, false },
  468.   { "defindex", cm_defindex, false },
  469.   { "dfn", cm_dfn, true },
  470.  
  471. /* The `def' commands. */
  472.   { "deffn", cm_defun, false },
  473.   { "deffnx", cm_defun, false },
  474.   { "defun", cm_defun, false },
  475.   { "defunx", cm_defun, false },
  476.   { "defmac", cm_defun, false },
  477.   { "defmacx", cm_defun, false },
  478.   { "defspec", cm_defun, false },
  479.   { "defspecx", cm_defun, false },
  480.   { "defvr", cm_defun, false },
  481.   { "defvrx", cm_defun, false },
  482.   { "defvar", cm_defun, false },
  483.   { "defvarx", cm_defun, false },
  484.   { "defopt", cm_defun, false },
  485.   { "defoptx", cm_defun, false },
  486.   { "deftypefn", cm_defun, false },
  487.   { "deftypefnx", cm_defun, false },
  488.   { "deftypefun", cm_defun, false },
  489.   { "deftypefunx", cm_defun, false },
  490.   { "deftypevr", cm_defun, false },
  491.   { "deftypevrx", cm_defun, false },
  492.   { "deftypevar", cm_defun, false },
  493.   { "deftypevarx", cm_defun, false },
  494.   { "defcv", cm_defun, false },
  495.   { "defcvx", cm_defun, false },
  496.   { "defivar", cm_defun, false },
  497.   { "defivarx", cm_defun, false },
  498.   { "defop", cm_defun, false },
  499.   { "defopx", cm_defun, false },
  500.   { "defmethod", cm_defun, false },
  501.   { "defmethodx", cm_defun, false },
  502.   { "deftypemethod", cm_defun, false },
  503.   { "deftypemethodx", cm_defun, false },
  504.   { "deftp", cm_defun, false },
  505.   { "deftpx", cm_defun, false },
  506. /* The end of the `def' commands. */
  507.  
  508.   { "display", cm_display, false },
  509.   { "dots", cm_dots, true },
  510.   { "dmn", do_nothing, true },
  511.   { "emph", cm_emph, true },
  512.   { "end", cm_end, false },
  513.   { "enumerate", cm_enumerate, false },
  514.   { "equiv", cm_equiv, true },
  515.   { "error", cm_error, true },
  516.   { "example", cm_example, false },
  517.   { "exdent", cm_exdent, false },
  518.   { "expansion", cm_expansion, true },
  519.   { "file", cm_file, true },
  520.   { "findex", cm_findex, false },
  521.   { "finalout", do_nothing, false },
  522.   { "flushleft", cm_flushleft, false },
  523.   { "flushright", cm_flushright, false },
  524.   { "format", cm_format, false },
  525.   { "ftable", cm_ftable, false },
  526.   { "group", cm_group, false },
  527.   { "heading", cm_heading, false },
  528.   { "headings", cm_headings, false },
  529.   { "i", cm_italic, true },
  530.   { "iappendix", cm_appendix, false },
  531.   { "iappendixsection", cm_appendixsec, false },
  532.   { "iappendixsec", cm_appendixsec, false },
  533.   { "iappendixsubsec", cm_appendixsubsec, false },
  534.   { "iappendixsubsubsec", cm_appendixsubsubsec, false },
  535.   { "ichapter", cm_chapter, false },
  536.   { "ifclear", cm_ifclear, false },
  537.   { "ifinfo", cm_ifinfo, false },
  538.   { "ifset", cm_ifset, false },
  539.   { "iftex", command_name_condition, false },
  540.   { "ignore", command_name_condition, false },
  541.   { "include", cm_include, false },
  542.   { "inforef", cm_inforef, true },
  543.   { "input", cm_include, false },
  544.   { "isection", cm_section, false },
  545.   { "isubsection", cm_subsection, false },
  546.   { "isubsubsection", cm_subsubsection, false },
  547.   { "item", cm_item, false },
  548.   { "itemize", cm_itemize, false },
  549.   { "itemx", cm_itemx, false },
  550.   { "iunnumbered", cm_unnumbered, false },
  551.   { "iunnumberedsec", cm_unnumberedsec, false },
  552.   { "iunnumberedsubsec", cm_unnumberedsubsec, false },
  553.   { "iunnumberedsubsubsec", cm_unnumberedsubsubsec, false },
  554.   { "kbd", cm_kbd, true },
  555.   { "key", cm_key, true },
  556.   { "kindex", cm_kindex, false },
  557.   { "lisp", cm_lisp, false },
  558.   { "majorheading", cm_majorheading, false },
  559.   { "math", cm_math, true },
  560.   { "menu", cm_menu },
  561.   { "minus", cm_minus, true },
  562.   { "need", cm_need, false },
  563.   { "node", cm_node, false },
  564.   { "noindent", cm_noindent, false },
  565.   { "page", do_nothing, false },
  566.   { "pindex", cm_pindex, false },
  567.   { "point", cm_point, true },
  568.   { "print", cm_print, true },
  569.   { "printindex", cm_printindex, false },
  570.   { "pxref", cm_pxref, true },
  571.   { "quotation", cm_quotation, false },
  572.   { "r", cm_roman, true },
  573.   { "ref", cm_xref, true },
  574.   { "refill", cm_refill, false },
  575.   { "result", cm_result, true },
  576.   { "samp", cm_samp, true },
  577.   { "sc", cm_sc, true },
  578.   { "section", cm_section, false },
  579.   { "set", cm_set, false },
  580.   { "setchapternewpage", cm_setchapternewpage, false },
  581.   { "setfilename", cm_setfilename, false },
  582.   { "settitle", cm_settitle, false },
  583.   { "shortcontents", do_nothing, false },
  584.   { "smallbook", cm_smallbook, false },
  585.   { "smallexample", cm_smallexample, false },
  586.   { "smalllisp", cm_smalllisp, false },
  587.   { "sp", cm_sp, false },
  588.   { "strong", cm_strong, true },
  589.   { "subheading", cm_subheading, false },
  590.   { "subsection", cm_subsection, false },
  591.   { "subsubheading", cm_subsubheading, false },
  592.   { "subsubsection", cm_subsubsection, false },
  593.   { "summarycontents", do_nothing, false },
  594.   { "syncodeindex", cm_synindex, false },
  595.   { "synindex", cm_synindex, false },
  596.   { "t", cm_title, true },
  597.   { "table", cm_table, false },
  598.   { "tex", command_name_condition, false },
  599.   { "tindex", cm_tindex, false },
  600.   { "titlepage", command_name_condition, false },
  601.   { "titlespec", command_name_condition, false },
  602.   { "today", cm_today, true },
  603.   { "top", cm_top, false  },
  604.   { "unnumbered", cm_unnumbered, false },
  605.   { "unnumberedsec", cm_unnumberedsec, false },
  606.   { "unnumberedsubsec", cm_unnumberedsubsec, false },
  607.   { "unnumberedsubsubsec", cm_unnumberedsubsubsec, false },
  608.   { "var", cm_var, true },
  609.   { "vindex", cm_vindex, false },
  610.   { "w", cm_w, true },
  611.   { "xref", cm_xref, true },
  612.   { "{", insert_self, false },
  613.   { "}", insert_self, false },
  614.  
  615.   /* Some obsoleted commands. */
  616.   { "infotop", cm_obsolete, false },
  617.   { "infounnumbered", cm_obsolete, false },
  618.   { "infounnumberedsec", cm_obsolete, false },
  619.   { "infounnumberedsubsec", cm_obsolete, false },
  620.   { "infounnumberedsubsubsec", cm_obsolete, false },
  621.   { "infoappendix", cm_obsolete, false },
  622.   { "infoappendixsec", cm_obsolete, false },
  623.   { "infoappendixsubsec", cm_obsolete, false },
  624.   { "infoappendixsubsubsec", cm_obsolete, false },
  625.   { "infochapter", cm_obsolete, false },
  626.   { "infosection", cm_obsolete, false },
  627.   { "infosubsection", cm_obsolete, false },
  628.   { "infosubsubsection", cm_obsolete, false },
  629.  
  630.   /* Now @include does what this was supposed to. */
  631.   { "infoinclude", cm_infoinclude, false },
  632.   { "footnote", cm_footnote, false}, /* self-arg eater */
  633.   { "footnotestyle", cm_footnotestyle, false },
  634.   { "paragraphindent", cm_paragraphindent, false },
  635.  
  636.   {(char *) NULL, (FUNCTION *) NULL}, false};
  637.  
  638. int major_version = 1;
  639. int minor_version = 40;
  640.  
  641. struct option long_options[] =
  642. {
  643.   { "error-limit", 1, 0, 'e' },            /* formerly -el */
  644.   { "fill-column", 1, 0, 'f' },            /* formerly -fc */
  645.   { "footnote-style", 1, 0, 's' },        /* formerly -ft */
  646.   { "no-pointer-validate", 0, &validating, false }, /* formerly -nv */
  647.   { "no-split", 0, &splitting, false },        /* formerly -ns */
  648.   { "no-validate", 0, &validating, false },    /* formerly -nv */
  649.   { "no-warn", 0, &print_warnings, false },    /* formerly -nw */
  650.   { "number-footnotes", 0, &number_footnotes, 1 },
  651.   { "no-number-footnotes", 0, &number_footnotes, 0 },
  652.   { "paragraph-indent", 1, 0, 'p' },        /* formerly -pi */
  653.   { "reference-limit", 1, 0, 'r' },        /* formerly -rl */
  654.   { "verbose", 0, &verbose_mode, 1 },        /* formerly -verbose */
  655.   { "version", 0, 0, 'V' },
  656.   {NULL, 0, NULL, 0}
  657. };
  658.  
  659. /* **************************************************************** */
  660. /*                                    */
  661. /*            Main ()  Start of code              */
  662. /*                                        */
  663. /* **************************************************************** */
  664.  
  665. /* For each file mentioned in the command line, process it, turning
  666.    texinfo commands into wonderfully formatted output text. */
  667. main (argc, argv)
  668.      int argc;
  669.      char **argv;
  670. {
  671.   extern int errors_printed;
  672.   char *filename_part ();
  673.   int c, ind;
  674.  
  675.   /* The name of this program is the last filename in argv[0]. */
  676.   progname = filename_part (argv[0]);
  677.  
  678.   /* Parse argument flags from the input line. */
  679.   while ((c = getopt_long (argc, argv, "", long_options, &ind)) != EOF)
  680.     {
  681.       if (c == 0 && long_options[ind].flag == 0)
  682.     c = long_options[ind].val;
  683.  
  684.       switch (c)
  685.     {
  686.       /* User specified fill_column? */
  687.     case 'f':
  688.       if (sscanf (optarg, "%d", &fill_column) != 1)
  689.         usage ();
  690.       break;
  691.  
  692.       /* User specified paragraph indent (paragraph_start_index)? */
  693.     case 'p':
  694.       if (sscanf (optarg, "%d", ¶graph_start_indent) != 1)
  695.         usage ();
  696.       break;
  697.  
  698.       /* User specified error level? */
  699.     case 'e':
  700.       if (sscanf (optarg, "%d", &max_error_level) != 1)
  701.         usage ();
  702.       break;
  703.  
  704.       /* User specified reference warning limit? */
  705.     case 'r':
  706.       if (sscanf (optarg, "%d", &reference_warning_limit) != 1)
  707.         usage ();
  708.       break;
  709.  
  710.       /* User specified footnote style? */
  711.     case 's':
  712.       set_footnote_style (optarg);
  713.       break;
  714.  
  715.       /* User requested version info? */
  716.     case 'V':
  717.       print_version_info ();
  718.       exit (NO_ERROR);
  719.       break;
  720.  
  721.     case '?':
  722.       usage ();
  723.     }
  724.     }
  725.  
  726.   if (optind == argc)
  727.     usage ();
  728.   else if (verbose_mode)
  729.     print_version_info ();
  730.  
  731.   /* Remaining arguments are file names of texinfo files.
  732.      Convert them, one by one. */
  733.   while (optind != argc)
  734.     convert (argv[optind++]);
  735.  
  736.   if (errors_printed)
  737.     exit (SYNTAX);
  738.   else
  739.     exit (NO_ERROR);
  740. }
  741.  
  742. /* Display the version info of this invocation of Makeinfo. */
  743. print_version_info ()
  744. {
  745.   fprintf (stderr, "This is GNU Makeinfo version %d.%d.\n",
  746.        major_version, minor_version);
  747. }
  748.  
  749. /* **************************************************************** */
  750. /*                                    */
  751. /*            Generic Utilities                */
  752. /*                                    */
  753. /* **************************************************************** */
  754.  
  755. /* Just like malloc, but kills the program in case of fatal error. */
  756. unsigned char *
  757. xmalloc (nbytes)
  758.      int nbytes;
  759. {
  760.   unsigned char *temp = (unsigned char *) malloc (nbytes);
  761.   if (temp == (unsigned char *) NULL)
  762.     {
  763.       error ("Virtual memory exhausted! Needed %d bytes.", nbytes);
  764.       exit (FATAL);
  765.     }
  766.   return (temp);
  767. }
  768.  
  769. /* Like realloc (), but barfs if there isn't enough memory. */
  770. unsigned char *
  771. xrealloc (pointer, nbytes)
  772.      unsigned char *pointer;
  773.      int nbytes;
  774. {
  775.   unsigned char *temp;
  776.  
  777.   if (!pointer)
  778.     temp = (unsigned char *)xmalloc (nbytes);
  779.   else
  780.     temp = (unsigned char *)realloc (pointer, nbytes);
  781.  
  782.   if (!temp)
  783.     {
  784.       error ("Virtual memory exhausted in xrealloc ().");
  785.       abort ();
  786.     }
  787.   return (temp);
  788. }
  789.  
  790. /* Tell the user how to use this program. */
  791. usage ()
  792. {
  793.   fprintf (stderr, "Usage: %s [options] texinfo-file...\n\
  794. \n\
  795. This program accepts as input files of texinfo commands and text\n\
  796. and outputs a file suitable for reading with GNU Info.\n\
  797. \n\
  798. The options are:\n\
  799. `+no-validate' to suppress node cross reference validation.\n\
  800. `+no-warn' to suppress warning messages (errors are still output).\n\
  801. `+no-split' to suppress the splitting of large files.\n\
  802. `+verbose' to print information about what is being done.\n\
  803. `+version' to print the version number of Makeinfo.\n\
  804. `+paragraph-indent NUM' to set the paragraph indent to NUM (default %d).\n\
  805. `+fill-column NUM' to set the filling column to NUM (default %d).\n\
  806. `+error-limit NUM' to set the error limit to NUM (default %d).\n\
  807. `+reference-limit NUM' to set the reference warning limit to NUM (default %d).\n\
  808. `+footnote-style STYLE' to set the footnote style to STYLE.  STYLE should\n\
  809.   either be `separate' to place footnotes in their own node, or\n\
  810.   `end', to place the footnotes at the end of the node in which they are\n\
  811.    defined (the default).\n\n",
  812.        progname, paragraph_start_indent,
  813.        fill_column, max_error_level, reference_warning_limit);
  814.   exit (FATAL);
  815. }
  816.  
  817. /* **************************************************************** */
  818. /*                                    */
  819. /*            Manipulating Lists                  */
  820. /*                                        */
  821. /* **************************************************************** */
  822.  
  823. typedef struct generic_list
  824. {
  825.   struct generic_list *next;
  826. }            GENERIC_LIST;
  827.  
  828. /* Reverse the chain of structures in LIST.  Output the new head
  829.    of the chain.  You should always assign the output value of this
  830.    function to something, or you will lose the chain. */
  831. GENERIC_LIST *
  832. reverse_list (list)
  833.      register GENERIC_LIST *list;
  834. {
  835.   register GENERIC_LIST *next;
  836.   register GENERIC_LIST *prev = (GENERIC_LIST *) NULL;
  837.  
  838.   while (list)
  839.     {
  840.       next = list->next;
  841.       list->next = prev;
  842.       prev = list;
  843.       list = next;
  844.     }
  845.   return (prev);
  846. }
  847.  
  848.  
  849. /* **************************************************************** */
  850. /*                                    */
  851. /*            Pushing and Popping Files               */
  852. /*                                    */
  853. /* **************************************************************** */
  854.  
  855. /* Find and load the file named FILENAME.  Return a pointer to
  856.    the loaded file, or NULL if it can't be loaded. */
  857. char *
  858. find_and_load (filename)
  859.      char *filename;
  860. {
  861.   struct stat fileinfo;
  862.   int file, n, i, count = 0;
  863.   char *result = (char *) NULL;
  864.  
  865.   if (stat (filename, &fileinfo) != 0)
  866.     goto error_exit;
  867.  
  868.   file = open (filename, O_RDONLY);
  869.   if (file < 0)
  870.     goto error_exit;
  871.  
  872.   /* Load the file. */
  873.   result = (char *)xmalloc (fileinfo.st_size);
  874.  
  875.   /* VMS stat lies about the st_size value.  The actual number of
  876.      readable bytes is always less than this value.  The arcane
  877.      mysteries of VMS/RMS are too much to probe, so this hack
  878.     suffices to make things work. */
  879. #if defined (VMS)
  880.   while ((n = read (file, result+count, fileinfo.st_size)) > 0)
  881.     count += n;
  882.   if (n == -1)
  883. #else
  884.     count = fileinfo.st_size;
  885.     if (read (file, result, fileinfo.st_size) != fileinfo.st_size)
  886. #endif
  887.   error_exit:
  888.     {
  889.       if (result)
  890.     free (result);
  891.       if (file != -1)
  892.     close (file);
  893.       return ((char *) NULL);
  894.     }
  895.   close (file);
  896.  
  897.   /* Set the globals to the new file. */
  898.   input_text = result;
  899.   size_of_input_text = fileinfo.st_size;
  900.   input_filename = savestring (filename);
  901.   node_filename = savestring (filename);
  902.   input_text_offset = 0;
  903.   line_number = 1;
  904.   return (result);
  905. }
  906.  
  907. /* Save the state of the current input file. */
  908. pushfile ()
  909. {
  910.   FSTACK *newstack = (FSTACK *) xmalloc (sizeof (FSTACK));
  911.   newstack->filename = input_filename;
  912.   newstack->text = input_text;
  913.   newstack->size = size_of_input_text;
  914.   newstack->offset = input_text_offset;
  915.   newstack->line_number = line_number;
  916.   newstack->next = filestack;
  917.  
  918.   filestack = newstack;
  919.   push_node_filename ();
  920. }
  921.  
  922. /* Make the current file globals be what is on top of the file stack. */
  923. popfile ()
  924. {
  925.   extern int executing_string;
  926.   FSTACK *temp = filestack;
  927.  
  928.   if (!filestack)
  929.     abort ();            /* My fault.  I wonder what I did? */
  930.  
  931.   /* Make sure that commands with braces have been satisfied. */
  932.   if (!executing_string)
  933.     discard_braces ();
  934.  
  935.   /* Get the top of the stack into the globals. */
  936.   input_filename = filestack->filename;
  937.   input_text = filestack->text;
  938.   size_of_input_text = filestack->size;
  939.   input_text_offset = filestack->offset;
  940.   line_number = filestack->line_number;
  941.  
  942.   /* Pop the stack. */
  943.   filestack = filestack->next;
  944.   free (temp);
  945.   pop_node_filename ();
  946. }
  947.  
  948. /* Flush all open files on the file stack. */
  949. flush_file_stack ()
  950. {
  951.   while (filestack)
  952.     {
  953.       free (input_filename);
  954.       free (input_text);
  955.       popfile ();
  956.     }
  957. }
  958.  
  959. int node_filename_stack_index = 0;
  960. int node_filename_stack_size = 0;
  961. char **node_filename_stack = (char **)NULL;
  962.  
  963. push_node_filename ()
  964. {
  965.   if (node_filename_stack_index + 1 > node_filename_stack_size)
  966.     {
  967.       if (!node_filename_stack)
  968.     node_filename_stack =
  969.       (char **)xmalloc ((node_filename_stack_size += 10)
  970.                 * sizeof (char *));
  971.       else
  972.     node_filename_stack =
  973.       (char **)xrealloc (node_filename_stack,
  974.                  (node_filename_stack_size + 10)
  975.                  * sizeof (char *));
  976.     }
  977.  
  978.   node_filename_stack[node_filename_stack_index] = node_filename;
  979.   node_filename_stack_index++;
  980. }
  981.  
  982. pop_node_filename ()
  983. {
  984.   node_filename = node_filename_stack[--node_filename_stack_index];
  985. }
  986.  
  987. /* Return just the simple part of the filename; i.e. the
  988.    filename without the path information, or extensions.
  989.    This conses up a new string. */
  990. char *
  991. filename_part (filename)
  992.      char *filename;
  993. {
  994.   register int i = strlen (filename) - 1;
  995.  
  996.   while (i && filename[i] != '/')
  997.     i--;
  998.   if (filename[i] == '/')
  999.     i++;
  1000.  
  1001. #if defined (REMOVE_OUTPUT_EXTENSIONS)
  1002.   result = savestring (&filename[i]);
  1003.  
  1004.   /* See if there is an extension to remove.  If so, remove it. */
  1005.   if (rindex (result, '.'))
  1006.     *(rindex (result, '.')) = '\0';
  1007.   return (result);
  1008. #else
  1009.   return (savestring (&filename[i]));
  1010. #endif /* REMOVE_OUTPUT_EXTENSIONS */
  1011. }
  1012.  
  1013. /* Return the pathname part of filename.  This can be NULL. */
  1014. char *
  1015. pathname_part (filename)
  1016.      char *filename;
  1017. {
  1018.   char *expand_filename ();
  1019.   char *result = (char *) NULL;
  1020.   register int i;
  1021.  
  1022.   filename = expand_filename (filename, "");
  1023.  
  1024.   i = strlen (filename) - 1;
  1025.  
  1026.   while (i && filename[i] != '/')
  1027.     i--;
  1028.   if (filename[i] == '/')
  1029.     i++;
  1030.  
  1031.   if (i)
  1032.     {
  1033.       result = (char *)xmalloc (1 + i);
  1034.       strncpy (result, filename, i);
  1035.       result[i] = '\0';
  1036.     }
  1037.   free (filename);
  1038.   return (result);
  1039. }
  1040.  
  1041. /* Return the expansion of FILENAME. */
  1042. char *
  1043. expand_filename (filename, input_name)
  1044.      char *filename, *input_name;
  1045. {
  1046.   char *full_pathname ();
  1047.   filename = full_pathname (filename);
  1048.  
  1049.   if (filename[0] == '.')
  1050.     return (filename);
  1051.  
  1052.   if (filename[0] != '/' && input_name[0] == '/')
  1053.     {
  1054.       /* Make it so that relative names work. */
  1055.       char *result;
  1056.       int i = strlen (input_name) - 1;
  1057.  
  1058.       result = (char *)xmalloc (1 + strlen (input_name) + strlen (filename));
  1059.       strcpy (result, input_name);
  1060.  
  1061.       while (result[i] != '/' && i)
  1062.     i--;
  1063.  
  1064.       if (result[i] == '/')
  1065.     i++;
  1066.  
  1067.       strcpy (&result[i], filename);
  1068.       free (filename);
  1069.       return (result);
  1070.     }
  1071.   return (filename);
  1072. }
  1073.  
  1074. /* Return the full path to FILENAME. */
  1075. char *
  1076. full_pathname (filename)
  1077.      char *filename;
  1078. {
  1079.   int initial_character;
  1080.  
  1081.   if (filename && (initial_character = *filename))
  1082.     {
  1083.       if (initial_character == '/')
  1084.     return (savestring (filename));
  1085.       if (initial_character != '~')
  1086.     {
  1087.       return (savestring (filename));
  1088.     }
  1089.       else
  1090.     {
  1091.       if (filename[1] == '/')
  1092.         {
  1093.           /* Return the concatenation of HOME and the rest of the string. */
  1094.           char *temp_home;
  1095.           char *temp_name;
  1096.  
  1097.           temp_home = (char *) getenv ("HOME");
  1098.           temp_name = (char *)xmalloc (strlen (&filename[2])
  1099.                        + 1
  1100.                        + temp_home ? strlen (temp_home)
  1101.                        : 0);
  1102.           if (temp_home)
  1103.         strcpy (temp_name, temp_home);
  1104.  
  1105.           strcat (temp_name, &filename[2]);
  1106.           return (temp_name);
  1107.         }
  1108.       else
  1109.         {
  1110.           struct passwd *user_entry;
  1111.           int i, c;
  1112.           char *username = (char *)xmalloc (257);
  1113.           char *temp_name;
  1114.  
  1115.           for (i = 1; c = filename[i]; i++)
  1116.         {
  1117.           if (c == '/')
  1118.             break;
  1119.           else
  1120.             username[i - 1] = c;
  1121.         }
  1122.           if (c)
  1123.         username[i - 1] = '\0';
  1124.  
  1125.           user_entry = getpwnam (username);
  1126.  
  1127.           if (!user_entry)
  1128.         return (savestring (filename));
  1129.  
  1130.           temp_name = (char *)xmalloc (1 + strlen (user_entry->pw_dir)
  1131.                        + strlen (&filename[i]));
  1132.           strcpy (temp_name, user_entry->pw_dir);
  1133.           strcat (temp_name, &filename[i]);
  1134.           return (temp_name);
  1135.         }
  1136.     }
  1137.     }
  1138.   else
  1139.     {
  1140.       return (savestring (filename));
  1141.     }
  1142. }
  1143.  
  1144. /* **************************************************************** */
  1145. /*                                    */
  1146. /*            Error Handling                    */
  1147. /*                                    */
  1148. /* **************************************************************** */
  1149.  
  1150. /* Number of errors encountered. */
  1151. int errors_printed = 0;
  1152.  
  1153. /* Print the last error gotten from the file system. */
  1154. fs_error (filename)
  1155.      char *filename;
  1156. {
  1157.   perror (filename);
  1158.   return ((int) false);
  1159. }
  1160.  
  1161. /* Print an error message, and return false. */
  1162. error (format, arg1, arg2, arg3, arg4, arg5)
  1163.      char *format;
  1164. {
  1165.   remember_error ();
  1166.   fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
  1167.   fprintf (stderr, "\n");
  1168.   return ((int) false);
  1169. }
  1170.  
  1171. /* Just like error (), but print the line number as well. */
  1172. line_error (format, arg1, arg2, arg3, arg4, arg5)
  1173.      char *format;
  1174. {
  1175.   remember_error ();
  1176.   fprintf (stderr, "%s:%d: ", input_filename, line_number);
  1177.   fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
  1178.   fprintf (stderr, ".\n");
  1179.   return ((int) false);
  1180. }
  1181.  
  1182. warning (format, arg1, arg2, arg3, arg4, arg5)
  1183.      char *format;
  1184. {
  1185.   if (print_warnings)
  1186.     {
  1187.       fprintf (stderr, "%s:%d: Warning: ", input_filename, line_number);
  1188.       fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
  1189.       fprintf (stderr, ".\n");
  1190.     }
  1191.   return ((int) false);
  1192. }
  1193.  
  1194. /* Remember that an error has been printed.  If this is the first
  1195.    error printed, then tell them which program is printing them.
  1196.    If more than max_error_level have been printed, then exit the
  1197.    program. */
  1198. remember_error ()
  1199. {
  1200.   errors_printed++;
  1201.   if (max_error_level && (errors_printed > max_error_level))
  1202.     {
  1203.       fprintf (stderr, "Too many errors!  Gave up.\n");
  1204.       flush_file_stack ();
  1205.       cm_bye ();
  1206.       exit (1);
  1207.     }
  1208. }
  1209.  
  1210.  
  1211. /* **************************************************************** */
  1212. /*                                    */
  1213. /*            Hacking Tokens and Strings            */
  1214. /*                                    */
  1215. /* **************************************************************** */
  1216.  
  1217. /* Return the next token as a string pointer.  We cons the
  1218.    string. */
  1219. char *
  1220. read_token ()
  1221. {
  1222.   int i, character;
  1223.   char *result;
  1224.  
  1225.   /* Hack special case.  If the first character to be read is
  1226.      self-delimiting, then that is the command itself. */
  1227.  
  1228.   character = curchar ();
  1229.   if (self_delimiting (character))
  1230.     {
  1231.       input_text_offset++;
  1232.       result = savestring (" ");
  1233.       *result = character;
  1234.       return (result);
  1235.     }
  1236.  
  1237.   for (i = 0; ((input_text_offset != size_of_input_text)
  1238.            && (character = curchar ())
  1239.            && command_char (character));
  1240.        i++, input_text_offset++);
  1241.   result = (char *)xmalloc (i + 1);
  1242.   strncpy (result, &input_text[input_text_offset - i], i);
  1243.   result[i] = '\0';
  1244.   return (result);
  1245. }
  1246.  
  1247. /* Return TRUE if CHARACTER is self-delimiting. */
  1248. boolean
  1249. self_delimiting (character)
  1250.      int character;
  1251. {
  1252.   return (member (character, "{}:.@*'`,!?; \n"));
  1253. }
  1254.  
  1255. /* Clear whitespace from the front and end of string. */
  1256. canon_white (string)
  1257.      char *string;
  1258. {
  1259.   int len = strlen (string);
  1260.   int x;
  1261.  
  1262.   if (!len)
  1263.     return;
  1264.  
  1265.   for (x = 0; x < len; x++)
  1266.     {
  1267.       if (!whitespace (string[x]))
  1268.     {
  1269.       strcpy (string, string + x);
  1270.       break;
  1271.     }
  1272.     }
  1273.   len = strlen (string);
  1274.   if (len)
  1275.     len--;
  1276.   while (len > -1 && cr_or_whitespace (string[len]))
  1277.     len--;
  1278.   string[len + 1] = '\0';
  1279. }
  1280.  
  1281. /* Bash STRING, replacing all whitespace with just one space. */
  1282. fix_whitespace (string)
  1283.      char *string;
  1284. {
  1285.   char *temp = (char *)xmalloc (strlen (string) + 1);
  1286.   int string_index = 0;
  1287.   int temp_index = 0;
  1288.   int c;
  1289.  
  1290.   canon_white (string);
  1291.  
  1292.   while (string[string_index])
  1293.     {
  1294.       c = temp[temp_index++] = string[string_index++];
  1295.  
  1296.       if (c == ' ' || c == '\n' || c == '\t')
  1297.     {
  1298.       temp[temp_index - 1] = ' ';
  1299.       while ((c = string[string_index]) && (c == ' ' ||
  1300.                         c == '\t' ||
  1301.                         c == '\n'))
  1302.         string_index++;
  1303.     }
  1304.     }
  1305.   temp[temp_index] = '\0';
  1306.   strcpy (string, temp);
  1307.   free (temp);
  1308. }
  1309.  
  1310. /* Discard text until the desired string is found.  The string is
  1311.    included in the discarded text. */
  1312. discard_until (string)
  1313.      char *string;
  1314. {
  1315.   int temp = search_forward (string, input_text_offset);
  1316.  
  1317.   int tt = (temp < 0) ? size_of_input_text : temp + strlen (string);
  1318.   int from = input_text_offset;
  1319.  
  1320.   /* Find out what line we are on. */
  1321.   while (from != tt)
  1322.     if (input_text[from++] == '\n')
  1323.       line_number++;
  1324.  
  1325.   if (temp < 0)
  1326.     {
  1327.       input_text_offset = size_of_input_text - strlen (string);
  1328.  
  1329.       if (strcmp (string, "\n") != 0)
  1330.     {
  1331.       line_error ("Expected `%s'", string);
  1332.       return;
  1333.     }
  1334.     }
  1335.   else
  1336.     input_text_offset = temp;
  1337.  
  1338.   input_text_offset += strlen (string);
  1339. }
  1340.  
  1341. /* Read characters from the file until we are at MATCH.
  1342.    Place the characters read into STRING.
  1343.    On exit input_text_offset is after the match string.
  1344.    Return the length of STRING. */
  1345. get_until (match, string)
  1346.      char *match, **string;
  1347. {
  1348.   int len, current_point, x, new_point;
  1349.  
  1350.   current_point = x = input_text_offset;
  1351.   new_point = search_forward (match, input_text_offset);
  1352.  
  1353.   if (new_point < 0)
  1354.     new_point = size_of_input_text;
  1355.   len = new_point - current_point;
  1356.  
  1357.   /* Keep track of which line number we are at. */
  1358.   while (x != new_point)
  1359.     if (input_text[x++] == '\n')
  1360.       line_number++;
  1361.  
  1362.   *string = (char *)xmalloc (len + 1);
  1363.  
  1364.   strncpy (*string, &input_text[current_point], len);
  1365.   (*string)[len] = '\0';
  1366.  
  1367.   /* Now leave input_text_offset in a consistent state. */
  1368.   input_text_offset = new_point + (strlen (match) - 1);
  1369.  
  1370.   if (input_text_offset > size_of_input_text)
  1371.     input_text_offset = size_of_input_text;
  1372. }
  1373.  
  1374. /* Read characters from the file until we are at MATCH or end of line.
  1375.    Place the characters read into STRING.  */
  1376. get_until_in_line (match, string)
  1377.      char *match, **string;
  1378. {
  1379.   int real_bottom = size_of_input_text;
  1380.   int temp = search_forward ("\n", input_text_offset);
  1381.   if (temp < 0)
  1382.     temp = size_of_input_text;
  1383.   size_of_input_text = temp;
  1384.   get_until (match, string);
  1385.   size_of_input_text = real_bottom;
  1386. }
  1387.  
  1388. get_rest_of_line (string)
  1389.      char **string;
  1390. {
  1391.   get_until ("\n", string);
  1392.   canon_white (*string);
  1393.   if (curchar () == '\n')    /* as opposed to the end of the file... */
  1394.     {
  1395.       line_number++;
  1396.       input_text_offset++;
  1397.     }
  1398. }
  1399.  
  1400. /* Backup the input pointer to the previous character, keeping track
  1401.    of the current line number. */
  1402. backup_input_pointer ()
  1403. {
  1404.   if (input_text_offset)
  1405.     {
  1406.       input_text_offset--;
  1407.       if (curchar () == '\n')
  1408.     line_number--;
  1409.     }
  1410. }
  1411.  
  1412. /* Read characters from the file until we are at MATCH or closing brace.
  1413.    Place the characters read into STRING.  */
  1414. get_until_in_braces (match, string)
  1415.      char *match, **string;
  1416. {
  1417.   int i, brace = 0;
  1418.   int match_len = strlen (match);
  1419.   char *temp;
  1420.  
  1421.   for (i = input_text_offset; i < size_of_input_text; i++)
  1422.     {
  1423.       if (input_text[i] == '{')
  1424.     brace++;
  1425.       else if (input_text[i] == '}')
  1426.     brace--;
  1427.       else if (input_text[i] == '\n')
  1428.     line_number++;
  1429.  
  1430.       if (brace < 0 ||
  1431.       (brace == 0 && strncmp (input_text + i, match, match_len) == 0))
  1432.     break;
  1433.     }
  1434.  
  1435.   match_len = i - input_text_offset;
  1436.   temp = (char *)xmalloc (2 + match_len);
  1437.   strncpy (temp, input_text + input_text_offset, match_len);
  1438.   temp[match_len] = '\0';
  1439.   input_text_offset = i;
  1440.   *string = temp;
  1441. }
  1442.  
  1443. /* **************************************************************** */
  1444. /*                                    */
  1445. /*            Converting the File                 */
  1446. /*                                    */
  1447. /* **************************************************************** */
  1448.  
  1449. /* Convert the file named by NAME.  The output is saved on the file
  1450.    named as the argument to the @setfilename command. */
  1451. static char *suffixes[] = {
  1452.   "",
  1453.   ".texinfo",
  1454.   ".texi",
  1455.   ".txinfo",
  1456.   (char *)NULL
  1457. };
  1458.  
  1459. convert (name)
  1460.      char *name;
  1461. {
  1462.   char *real_output_filename, *expand_filename (), *filename_part ();
  1463.   char *filename = (char *)xmalloc (strlen (name) + 50);
  1464.   register int i;
  1465.  
  1466.   init_tag_table ();
  1467.   init_indices ();
  1468.   init_internals ();
  1469.   init_paragraph ();
  1470.  
  1471.   /* Try to load the file specified by NAME.  If the file isn't found, and
  1472.      there is no suffix in NAME, then try NAME.texinfo, and NAME.texi. */
  1473.   for (i = 0; suffixes[i]; i++)
  1474.     {
  1475.       strcpy (filename, name);
  1476.       strcat (filename, suffixes[i]);
  1477.  
  1478.       if (find_and_load (filename))
  1479.     break;
  1480.  
  1481.       if (!suffixes[i][0] && rindex (filename, '.'))
  1482.     {
  1483.       fs_error (filename);
  1484.       free (filename);
  1485.       return;
  1486.     }
  1487.     }
  1488.  
  1489.   if (!suffixes[i])
  1490.     {
  1491.       fs_error (name);
  1492.       free (filename);
  1493.       return;
  1494.     }
  1495.  
  1496.   input_filename = filename;
  1497.  
  1498.   /* Search this file looking for the special string which starts conversion.
  1499.      Once found, we may truly begin. */
  1500.  
  1501.   input_text_offset = search_forward ("@setfilename", 0);
  1502.   if (input_text_offset < 0)
  1503.     {
  1504.       error ("No `@setfilename' found in `%s'", name);
  1505.       goto finished;
  1506.     }
  1507.   else
  1508.     input_text_offset += strlen ("@setfilename");
  1509.  
  1510.   get_until ("\n", &output_filename);
  1511.   canon_white (output_filename);
  1512.  
  1513.   printf ("Making info file `%s' from `%s'.\n", output_filename, name);
  1514.  
  1515.   if (verbose_mode)
  1516.     fprintf (stderr, "  The input file contains %d characters.\n",
  1517.          size_of_input_text);
  1518.  
  1519.   real_output_filename = expand_filename (output_filename, name);
  1520.   output_stream = fopen (real_output_filename, "w");
  1521.   if (output_stream == NULL)
  1522.     {
  1523.       fs_error (real_output_filename);
  1524.       goto finished;
  1525.     }
  1526.  
  1527.   /* Make the displayable filename from output_filename.  Only the root
  1528.      portion of the filename need be displayed. */
  1529.   pretty_output_filename = filename_part (output_filename);
  1530.  
  1531.   /* For this file only, count the number of newlines from the top of
  1532.      the file to here.  This way, we keep track of line numbers for
  1533.      error reporting.  Line_number starts at 1, since the user isn't
  1534.      zero-based. */
  1535.   {
  1536.     int temp = 0;
  1537.     line_number = 1;
  1538.     while (temp != input_text_offset)
  1539.       if (input_text[temp++] == '\n')
  1540.     line_number++;
  1541.   }
  1542.  
  1543.   add_word_args ("Info file %s, produced by Makeinfo, -*- Text -*-\n\
  1544. from input file %s.\n", output_filename, input_filename);
  1545.   close_paragraph ();
  1546.  
  1547.   reader_loop ();
  1548.  
  1549. finished:
  1550.   close_paragraph ();
  1551.   flush_file_stack ();
  1552.   if (output_stream != NULL)
  1553.     {
  1554.       output_pending_notes ();
  1555.       free_pending_notes ();
  1556.       if (tag_table != NULL)
  1557.     {
  1558.       tag_table = (TAG_ENTRY *) reverse_list (tag_table);
  1559.       write_tag_table ();
  1560.     }
  1561.  
  1562.       fclose (output_stream);
  1563.  
  1564.       /* If validating, then validate the entire file right now. */
  1565.       if (validating)
  1566.     validate_file (real_output_filename, tag_table);
  1567.  
  1568.       /* This used to test  && !errors_printed.
  1569.      But some files might have legit warnings.  So split anyway.  */
  1570.       if (splitting)
  1571.     split_file (real_output_filename, 0);
  1572.     }
  1573. }
  1574.  
  1575. free_and_clear (pointer)
  1576.      char **pointer;
  1577. {
  1578.   if ((*pointer) != (char *) NULL)
  1579.     {
  1580.       free (*pointer);
  1581.       *pointer = (char *) NULL;
  1582.     }
  1583. }
  1584.  
  1585.  /* Initialize some state. */
  1586. init_internals ()
  1587. {
  1588.   free_and_clear (¤t_node);
  1589.   free_and_clear (&output_filename);
  1590.   free_and_clear (&command);
  1591.   free_and_clear (&input_filename);
  1592.   free_node_references ();
  1593.   init_insertion_stack ();
  1594.   init_brace_stack ();
  1595.   command_index = 0;
  1596.   in_menu = 0;
  1597. }
  1598.  
  1599. init_paragraph ()
  1600. {
  1601.   free_and_clear (&output_paragraph);
  1602.   output_paragraph = xmalloc (paragraph_buffer_len);
  1603.   output_position = 0;
  1604.   output_paragraph[0] = '\0';
  1605.   output_paragraph_offset = 0;
  1606.   output_column = 0;
  1607.   paragraph_is_open = false;
  1608.   current_indent = 0;
  1609. }
  1610.  
  1611. /* Okay, we are ready to start the conversion.  Call the reader on
  1612.    some text, and fill the text as it is output.  Handle commands by
  1613.    remembering things like open braces and the current file position on a
  1614.    stack, and when the corresponding close brace is found, you can call
  1615.    the function with the proper arguments. */
  1616. reader_loop ()
  1617. {
  1618.   int character;
  1619.   boolean done = false;
  1620.   int dash_count = 0;
  1621.  
  1622.   while (!done)
  1623.     {
  1624.       if (input_text_offset >= size_of_input_text)
  1625.     {
  1626.       if (filestack)
  1627.         {
  1628.           free (input_filename);
  1629.           free (input_text);
  1630.           popfile ();
  1631.         }
  1632.       else
  1633.         break;
  1634.     }
  1635.  
  1636.       character = curchar ();
  1637.  
  1638.       if (!in_fixed_width_font &&
  1639.       (character == '\'' || character == '`') &&
  1640.       input_text[input_text_offset + 1] == character)
  1641.     {
  1642.       input_text_offset++;
  1643.       character = '"';
  1644.     }
  1645.  
  1646.       if (character == '-')
  1647.     {
  1648.       dash_count++;
  1649.       if (dash_count == 3 && !in_fixed_width_font)
  1650.         {
  1651.           input_text_offset++;
  1652.           continue;
  1653.         }
  1654.     }
  1655.       else
  1656.     {
  1657.       dash_count = 0;
  1658.     }
  1659.  
  1660.       /* If this is a whitespace character, then check to see if the line
  1661.      is blank.  If so, advance to the carriage return. */
  1662.       if (whitespace (character))
  1663.     {
  1664.       register int i = input_text_offset + 1;
  1665.  
  1666.       while (i < size_of_input_text && whitespace (input_text[i]))
  1667.         i++;
  1668.  
  1669.       if (i == size_of_input_text || input_text[i] == '\n')
  1670.         {
  1671.           if (i == size_of_input_text)
  1672.         i--;
  1673.  
  1674.           input_text_offset = i;
  1675.           character = curchar ();
  1676.         }
  1677.     }
  1678.       
  1679.       if (character == '\n')
  1680.     {
  1681.       line_number++;
  1682.       if (in_menu && input_text_offset + 1 < size_of_input_text)
  1683.         {
  1684.           char *glean_node_from_menu (), *tem;
  1685.  
  1686.           /* Note that the value of TEM is discarded, since it is
  1687.          gauranteed to be NULL when glean_node_from_menu () is
  1688.          called with a non-zero argument. */
  1689.           tem = glean_node_from_menu (1);
  1690.         }
  1691.     }
  1692.  
  1693.       switch (character)
  1694.     {
  1695.     case COMMAND_PREFIX:
  1696.       read_command ();
  1697.       if (strcmp (command, "bye") == 0)
  1698.         {
  1699.           done = true;
  1700.           continue;
  1701.         }
  1702.       break;
  1703.  
  1704.     case '{':
  1705.  
  1706.       /* Special case.  I'm not supposed to see this character by itself.
  1707.          If I do, it means there is a syntax error in the input text.
  1708.          Report the error here, but remember this brace on the stack so
  1709.          you can ignore its partner. */
  1710.  
  1711.       line_error ("Misplaced `{'");
  1712.       remember_brace (misplaced_brace);
  1713.  
  1714.       /* Don't advance input_text_offset since this happens in
  1715.          remember_brace ().
  1716.          input_text_offset++;
  1717.            */
  1718.       break;
  1719.  
  1720.     case '}':
  1721.       pop_and_call_brace ();
  1722.       input_text_offset++;
  1723.       break;
  1724.  
  1725.     default:
  1726.       add_char (character);
  1727.       input_text_offset++;
  1728.     }
  1729.     }
  1730. }
  1731.  
  1732. /* Find the command corresponding to STRING.  If the command
  1733.    is found, return a pointer to the data structure.  Otherwise
  1734.    return (-1). */
  1735. COMMAND *
  1736. get_command_entry (string)
  1737.      char *string;
  1738. {
  1739.   register int i;
  1740.  
  1741.   for (i = 0; CommandTable[i].name; i++)
  1742.     if (strcmp (CommandTable[i].name, string) == 0)
  1743.       return (&CommandTable[i]);
  1744.  
  1745.   /* This command is not in our predefined command table.  Perhaps
  1746.      it is a user defined command. */
  1747.   for (i = 0; i < user_command_array_len; i++)
  1748.     if (user_command_array[i] &&
  1749.     (strcmp (user_command_array[i]->name, string) == 0))
  1750.       return (user_command_array[i]);
  1751.  
  1752.   /* Nope, we never heard of this command. */
  1753.   return ((COMMAND *) -1);
  1754. }
  1755.  
  1756. /* input_text_offset is right at the command prefix character.
  1757.    Read the next token to determine what to do. */
  1758. read_command ()
  1759. {
  1760.   COMMAND *entry;
  1761.   input_text_offset++;
  1762.   free_and_clear (&command);
  1763.   command = read_token ();
  1764.  
  1765.   entry = get_command_entry (command);
  1766.  
  1767.   if ((int) entry == -1)
  1768.     {
  1769.       line_error ("Unknown info command `%s'", command);
  1770.       return;
  1771.     }
  1772.  
  1773.   if (entry->argument_in_braces)
  1774.     remember_brace (entry->proc);
  1775.  
  1776.   (*(entry->proc)) (START);
  1777. }
  1778.  
  1779. /* Return the string which invokes PROC; a pointer to a function. */
  1780. char *
  1781. find_proc_name (proc)
  1782.      FUNCTION *proc;
  1783. {
  1784.   register int i;
  1785.  
  1786.   for (i = 0; CommandTable[i].name; i++)
  1787.     if (proc == CommandTable[i].proc)
  1788.       return (CommandTable[i].name);
  1789.   return ("NO_NAME!");
  1790. }
  1791.  
  1792. init_brace_stack ()
  1793. {
  1794.   brace_stack = (BRACE_ELEMENT *) NULL;
  1795. }
  1796.  
  1797. remember_brace (proc)
  1798.      FUNCTION *proc;
  1799. {
  1800.   if (curchar () != '{')
  1801.     line_error ("@%s expected `{..}'", command);
  1802.   else
  1803.     input_text_offset++;
  1804.   remember_brace_1 (proc, output_paragraph_offset);
  1805. }
  1806.  
  1807. /* Remember the current output position here.  Save PROC
  1808.    along with it so you can call it later. */
  1809. remember_brace_1 (proc, position)
  1810.      FUNCTION *proc;
  1811.      int position;
  1812. {
  1813.   BRACE_ELEMENT *new = (BRACE_ELEMENT *) xmalloc (sizeof (BRACE_ELEMENT));
  1814.   new->next = brace_stack;
  1815.   new->proc = proc;
  1816.   new->pos = position;
  1817.   new->line = line_number;
  1818.   brace_stack = new;
  1819. }
  1820.  
  1821. /* Pop the top of the brace stack, and call the associated function
  1822.    with the args END and POS. */
  1823. pop_and_call_brace ()
  1824. {
  1825.   BRACE_ELEMENT *temp;
  1826.   FUNCTION *proc;
  1827.   int pos;
  1828.  
  1829.   if (brace_stack == (BRACE_ELEMENT *) NULL)
  1830.     return (line_error ("Unmatched close bracket"));
  1831.  
  1832.   pos = brace_stack->pos;
  1833.   proc = brace_stack->proc;
  1834.   temp = brace_stack->next;
  1835.   free (brace_stack);
  1836.   brace_stack = temp;
  1837.  
  1838.   return ((*proc) (END, pos, output_paragraph_offset));
  1839. }
  1840.  
  1841. /* You call discard_braces () when you shouldn't have any braces on the stack.
  1842.    I used to think that this happens for commands that don't take arguments
  1843.    in braces, but that was wrong because of things like @code{foo @@}.  So now
  1844.    I only detect it at the beginning of nodes. */
  1845. discard_braces ()
  1846. {
  1847.   int temp_line_number = line_number;
  1848.   char *proc_name;
  1849.  
  1850.   if (!brace_stack)
  1851.     return;
  1852.  
  1853.   while (brace_stack)
  1854.     {
  1855.       line_number = brace_stack->line;
  1856.       proc_name = find_proc_name (brace_stack->proc);
  1857.       line_error ("@%s missing close brace", proc_name);
  1858.       line_number = temp_line_number;
  1859.       pop_and_call_brace ();
  1860.     }
  1861. }
  1862.  
  1863. get_char_len (character)
  1864.      int character;
  1865. {
  1866.   /* Return the printed length of the character. */
  1867.   int len;
  1868.  
  1869.   switch (character)
  1870.     {
  1871.     case '\t':
  1872.       len = (output_column + 8) & 0xf7;
  1873.       if (len > fill_column)
  1874.     len = fill_column - output_column;
  1875.       else
  1876.     len = len - output_column;
  1877.       break;
  1878.  
  1879.     case '\n':
  1880.       len = fill_column - output_column;
  1881.       break;
  1882.  
  1883.     default:
  1884.       if (character < ' ')
  1885.     len = 2;
  1886.       else
  1887.     len = 1;
  1888.     }
  1889.   return (len);
  1890. }
  1891.  
  1892. add_word_args (format, arg1, arg2, arg3, arg4, arg5)
  1893.      char *format;
  1894. {
  1895.   char buffer[1000];
  1896.   sprintf (buffer, format, arg1, arg2, arg3, arg4, arg5);
  1897.   add_word (buffer);
  1898. }
  1899.  
  1900. /* Add STRING to output_paragraph. */
  1901. add_word (string)
  1902.      char *string;
  1903. {
  1904.   while (*string)
  1905.     add_char (*string++);
  1906. }
  1907.  
  1908. boolean last_char_was_newline = true;
  1909. int last_inserted_character = 0;
  1910.  
  1911. /* Add the character to the current paragraph.  If filling_enabled is
  1912.    true, then do filling as well. */
  1913. add_char (character)
  1914.      int character;
  1915. {
  1916.   extern int must_start_paragraph;
  1917.  
  1918.   /* If we are adding a character now, then we don't have to
  1919.      ignore close_paragraph () calls any more. */
  1920.   if (must_start_paragraph)
  1921.     {
  1922.       must_start_paragraph = 0;
  1923.       if (current_indent > output_column)
  1924.     {
  1925.       indent (current_indent - output_column);
  1926.       output_column = current_indent;
  1927.     }
  1928.     }
  1929.  
  1930.   if (non_splitting_words && member (character, " \t\n"))
  1931.     character = ' ' | 0x80;
  1932.  
  1933.   switch (character)
  1934.     {
  1935.     case '\n':
  1936.       if (!filling_enabled)
  1937.     {
  1938.       insert ('\n');
  1939.  
  1940.       if (force_flush_right)
  1941.         {
  1942.           close_paragraph ();
  1943.           /* Hack to force single blank lines out in this mode. */
  1944.           flush_output ();
  1945.         }
  1946.  
  1947.       output_column = 0;
  1948.  
  1949.       if (!no_indent && paragraph_is_open)
  1950.         indent (output_column = current_indent);
  1951.       break;
  1952.     }
  1953.       else
  1954.     {
  1955.       if (sentence_ender (last_inserted_character))
  1956.         {
  1957.           insert (' ');
  1958.           output_column++;
  1959.           last_inserted_character = character;
  1960.         }
  1961.     }
  1962.  
  1963.       if (last_char_was_newline)
  1964.     {
  1965.       close_paragraph ();
  1966.       pending_indent = 0;
  1967.     }
  1968.       else
  1969.     {
  1970.       last_char_was_newline = true;
  1971.       insert (' ');
  1972.       output_column++;
  1973.     }
  1974.       break;
  1975.  
  1976.     default:
  1977.       {
  1978.     int len = get_char_len (character);
  1979.  
  1980.     if ((character == ' ') && (last_char_was_newline))
  1981.       {
  1982.         if (!paragraph_is_open)
  1983.           {
  1984.         pending_indent++;
  1985.         return;
  1986.           }
  1987.       }
  1988.  
  1989.     if (!paragraph_is_open)
  1990.       {
  1991.         start_paragraph ();
  1992.  
  1993.         /* If the paragraph is supposed to be indented a certain way,
  1994.            then discard all of the pending whitespace.  Otherwise, we
  1995.            let the whitespace stay. */
  1996.         if (!paragraph_start_indent)
  1997.           indent (pending_indent);
  1998.         pending_indent = 0;
  1999.       }
  2000.  
  2001.     if ((output_column += len) >= fill_column)
  2002.       {
  2003.         if (filling_enabled)
  2004.           {
  2005.         int temp = output_paragraph_offset;
  2006.         while (--temp > 0 && output_paragraph[temp] != '\n')
  2007.           {
  2008.             if (output_paragraph[temp] == ' ')
  2009.               {
  2010.             output_paragraph[temp++] = '\n';
  2011.  
  2012.             /* We have correctly broken the line where we want
  2013.                to.  What we don't want is spaces following where
  2014.                we have decided to break the line.  We get rid of
  2015.                them. */
  2016.             {
  2017.               int t1 = temp;
  2018.               while (t1 < output_paragraph_offset
  2019.                  && whitespace (output_paragraph[t1]))
  2020.                 t1++;
  2021.  
  2022.               if (t1 != temp)
  2023.                 {
  2024.                   strncpy ((char *) &output_paragraph[temp],
  2025.                        (char *) &output_paragraph[t1],
  2026.                        (output_paragraph_offset - t1));
  2027.                   output_paragraph_offset -= (t1 - temp);
  2028.                 }
  2029.             }
  2030.  
  2031.             /* Filled, but now indent if that is right. */
  2032.             if (indented_fill && current_indent)
  2033.               {
  2034.                 int buffer_len = ((output_paragraph_offset - temp)
  2035.                           + current_indent);
  2036.                 char *temp_buffer = (char *)xmalloc (buffer_len);
  2037.                 int indentation = 0;
  2038.  
  2039.                 /* We have to shift any markers that are in
  2040.                    front of the wrap point. */
  2041.                 {
  2042.                   register BRACE_ELEMENT *stack = brace_stack;
  2043.  
  2044.                   while (stack)
  2045.                 {
  2046.                   if (stack->pos > temp)
  2047.                     stack->pos += current_indent;
  2048.                   stack = stack->next;
  2049.                 }
  2050.                 }
  2051.  
  2052.                 while (current_indent > 0 &&
  2053.                    indentation != current_indent)
  2054.                   temp_buffer[indentation++] = ' ';
  2055.  
  2056.                 strncpy ((char *) &temp_buffer[current_indent],
  2057.                      (char *) &output_paragraph[temp],
  2058.                      buffer_len - current_indent);
  2059.  
  2060.                 if (output_paragraph_offset + buffer_len
  2061.                 >= paragraph_buffer_len)
  2062.                   {
  2063.                 unsigned char *tt = xrealloc
  2064.                   (output_paragraph,
  2065.                    (paragraph_buffer_len += buffer_len));
  2066.                 output_paragraph = tt;
  2067.                   }
  2068.                 strncpy ((char *) &output_paragraph[temp],
  2069.                      temp_buffer, buffer_len);
  2070.                 output_paragraph_offset += current_indent;
  2071.                 free (temp_buffer);
  2072.               }
  2073.             output_column = 0;
  2074.             while (temp < output_paragraph_offset)
  2075.               output_column +=
  2076.                 get_char_len (output_paragraph[temp++]);
  2077.             output_column += len;
  2078.             break;
  2079.               }
  2080.           }
  2081.           }
  2082.       }
  2083.     insert (character);
  2084.     last_char_was_newline = false;
  2085.     last_inserted_character = character;
  2086.       }
  2087.     }
  2088. }
  2089.  
  2090. /* Insert CHARACTER into OUTPUT_PARAGRAPH. */
  2091. insert (character)
  2092.      int character;
  2093. {
  2094.   output_paragraph[output_paragraph_offset++] = character;
  2095.   if (output_paragraph_offset == paragraph_buffer_len)
  2096.     {
  2097.       output_paragraph =
  2098.     xrealloc (output_paragraph, (paragraph_buffer_len += 100));
  2099.     }
  2100. }
  2101.  
  2102. /* Remove upto COUNT characters of whitespace from the
  2103.    the current output line.  If COUNT is less than zero,
  2104.    then remove until none left. */
  2105. kill_self_indent (count)
  2106.      int count;
  2107. {
  2108.   /* Handle infinite case first. */
  2109.   if (count < 0)
  2110.     {
  2111.       output_column = 0;
  2112.       while (output_paragraph_offset)
  2113.     {
  2114.       if (whitespace (output_paragraph[output_paragraph_offset - 1]))
  2115.         output_paragraph_offset--;
  2116.       else
  2117.         break;
  2118.     }
  2119.     }
  2120.   else
  2121.     {
  2122.       while (output_paragraph_offset && count--)
  2123.     if (whitespace (output_paragraph[output_paragraph_offset - 1]))
  2124.       output_paragraph_offset--;
  2125.     else
  2126.       break;
  2127.     }
  2128. }
  2129.  
  2130. flush_output ()
  2131. {
  2132.   register int i;
  2133.  
  2134.   if (!output_paragraph_offset)
  2135.     return;
  2136.  
  2137.   for (i = 0; i < output_paragraph_offset; i++)
  2138.     {
  2139.       if (output_paragraph[i] == (unsigned char)(' ' | 0x80) ||
  2140.       output_paragraph[i] == (unsigned char)('\t' | 0x80) ||
  2141.       output_paragraph[i] == (unsigned char)('\n' | 0x80))
  2142.     output_paragraph[i] &= 0x7f;
  2143.     }
  2144.  
  2145.   fwrite (output_paragraph, 1, output_paragraph_offset, output_stream);
  2146.   output_position += output_paragraph_offset;
  2147.   output_paragraph_offset = 0;
  2148. }
  2149.  
  2150. /* How to close a paragraph controlling the number of lines between
  2151.    this one and the last one. */
  2152.  
  2153. /* Paragraph spacing is controlled by this variable.  It is the number of
  2154.    blank lines that you wish to appear between paragraphs.  A value of
  2155.    1 creates a single blank line between paragraphs. */
  2156. int paragraph_spacing = 1;
  2157.  
  2158.  
  2159. /* Close the current paragraph, leaving no blank lines between them. */
  2160. close_single_paragraph ()
  2161. {
  2162.   close_paragraph_with_lines (0);
  2163. }
  2164.  
  2165. close_paragraph_with_lines (lines)
  2166.      int lines;
  2167. {
  2168.   int old_spacing = paragraph_spacing;
  2169.   paragraph_spacing = lines;
  2170.   close_paragraph ();
  2171.   paragraph_spacing = old_spacing;
  2172. }
  2173.  
  2174. /* Non-zero means that start_paragraph () MUST be called before we pay
  2175.    any attention to close_paragraph () calls. */
  2176. int must_start_paragraph = 0;
  2177.  
  2178. /* Close the currently open paragraph. */
  2179. close_paragraph ()
  2180. {
  2181.   register int i;
  2182.  
  2183.   if (paragraph_is_open && !must_start_paragraph)
  2184.     {
  2185.       /* Gobble up blank lines that are extra... */
  2186.       register int tindex = output_paragraph_offset;
  2187.       register int c;
  2188.       while (tindex &&
  2189.          ((c = output_paragraph[tindex - 1]) == ' ' || c == '\n'))
  2190.     output_paragraph[--tindex] = '\n';
  2191.  
  2192.       output_paragraph_offset = tindex;
  2193.  
  2194.       insert ('\n');
  2195.  
  2196.       if (!force_flush_right)
  2197.     {
  2198.       for (i = 0; i < paragraph_spacing; i++)
  2199.         insert ('\n');
  2200.     }
  2201.  
  2202.       /* If we are doing flush right indentation, then do it now
  2203.      on the paragraph (really a single line). */
  2204.       if (force_flush_right)
  2205.     {
  2206.       char *temp;
  2207.  
  2208.       kill_self_indent (-1);
  2209.  
  2210.       if (output_paragraph[0] != '\n')
  2211.         {
  2212.           output_paragraph[output_paragraph_offset] = '\0';
  2213.  
  2214.           if (output_paragraph_offset < fill_column)
  2215.         {
  2216.           if (fill_column >= paragraph_buffer_len)
  2217.             output_paragraph =
  2218.               xrealloc (output_paragraph,
  2219.                 (paragraph_buffer_len += fill_column));
  2220.  
  2221.           temp = savestring ((char *)output_paragraph);
  2222.  
  2223.           for (i = 0; i < fill_column - output_paragraph_offset; i++)
  2224.             output_paragraph[i] = ' ';
  2225.  
  2226.           sprintf ((char *) output_paragraph + i, "%s", temp);
  2227.           free (temp);
  2228.           output_paragraph_offset = fill_column;
  2229.         }
  2230.         }
  2231.     }
  2232.  
  2233.       flush_output ();
  2234.       paragraph_is_open = false;
  2235.       no_indent = false;
  2236.       output_column = 0;
  2237.     }
  2238.   ignore_blank_line ();
  2239. }
  2240.  
  2241. /* Make the last line just read look as if it were only a newline. */
  2242. ignore_blank_line ()
  2243. {
  2244.   last_inserted_character = '\n';
  2245.   last_char_was_newline = true;
  2246. }
  2247.  
  2248. /* Begin a new paragraph. */
  2249. start_paragraph ()
  2250. {
  2251.  
  2252.   /* First close existing one. */
  2253.   if (paragraph_is_open)
  2254.     close_paragraph ();
  2255.  
  2256.   paragraph_is_open = true;
  2257.  
  2258.   if (!must_start_paragraph)
  2259.     {
  2260.       output_column = 0;
  2261.  
  2262.       /* If doing indentation, then insert the appropriate amount. */
  2263.       if (!no_indent)
  2264.     {
  2265.       if (inhibit_paragraph_indentation != 0)
  2266.         {
  2267.           output_column = current_indent;
  2268.           if (inhibit_paragraph_indentation < 0)
  2269.         inhibit_paragraph_indentation += 1;
  2270.         }
  2271.       else if (paragraph_start_indent < 0)
  2272.         output_column = current_indent;
  2273.       else
  2274.         output_column = current_indent + paragraph_start_indent;
  2275.  
  2276.       indent (output_column);
  2277.     }
  2278.     }
  2279.   else
  2280.     must_start_paragraph = 0;
  2281. }
  2282.  
  2283. /* Insert the indentation specified by AMOUNT. */
  2284. indent (amount)
  2285.      int amount;
  2286. {
  2287.   while (--amount >= 0)
  2288.     insert (' ');
  2289. }
  2290.  
  2291. /* Search forward for STRING in input_text.
  2292.    FROM says where where to start. */
  2293. search_forward (string, from)
  2294.      char *string;
  2295.      int from;
  2296. {
  2297.   int len = strlen (string);
  2298.  
  2299.   while (from < size_of_input_text)
  2300.     {
  2301.       if (strnicmp (input_text + from, string, len) == 0)
  2302.     return (from);
  2303.       from++;
  2304.     }
  2305.   return (-1);
  2306. }
  2307.  
  2308. /* Whoops, Unix doesn't have stricmp, or strnicmp. */
  2309.  
  2310. /* Case independent string compare. */
  2311. stricmp (string1, string2)
  2312.      char *string1, *string2;
  2313. {
  2314.   char ch1, ch2;
  2315.  
  2316.   for (;;)
  2317.     {
  2318.       ch1 = *string1++;
  2319.       ch2 = *string2++;
  2320.       if (!(ch1 | ch2))
  2321.     return (0);
  2322.  
  2323.       ch1 = coerce_to_upper (ch1);
  2324.       ch2 = coerce_to_upper (ch2);
  2325.  
  2326.       if (ch1 != ch2)
  2327.     return (1);
  2328.     }
  2329. }
  2330.  
  2331. /* Compare at most COUNT characters from string1 to string2.  Case
  2332.    doesn't matter. */
  2333. strnicmp (string1, string2, count)
  2334.      char *string1, *string2;
  2335. {
  2336.   char ch1, ch2;
  2337.  
  2338.   while (count)
  2339.     {
  2340.       ch1 = *string1++;
  2341.       ch2 = *string2++;
  2342.       if (coerce_to_upper (ch1) == coerce_to_upper (ch2))
  2343.     count--;
  2344.       else
  2345.     break;
  2346.     }
  2347.   return (count);
  2348. }
  2349.  
  2350. enum insertion_type
  2351. {
  2352.   menu, quotation, lisp, smalllisp, example, smallexample, display,
  2353.   itemize, format, enumerate, alphaenumerate, capsenumerate, cartouche,
  2354.   table, ftable, group, ifinfo, flushleft, flushright, ifset, ifclear,
  2355.   deffn, defun, defmac, defspec, defvr, defvar, defopt, deftypefn,
  2356.   deftypefun, deftypevr, deftypevar, defcv, defivar, defop, defmethod,
  2357.   deftypemethod, deftp, bad_type
  2358. };
  2359.  
  2360. char *insertion_type_names[] =
  2361. {
  2362.   "menu", "quotation", "lisp", "smalllisp", "example", "smallexample",
  2363.   "display", "itemize", "format", "enumerate", "alphaenumerate",
  2364.   "capsenumerate", "cartouche", "table", "ftable", "group", "ifinfo",
  2365.   "flushleft", "flushright", "ifset", "ifclear", "deffn", "defun",
  2366.   "defmac", "defspec", "defvr", "defvar", "defopt", "deftypefn",
  2367.   "deftypefun", "deftypevr", "deftypevar", "defcv", "defivar",
  2368.   "defop", "defmethod", "deftypemethod", "deftp", "bad_type"
  2369. };
  2370.  
  2371. int insertion_level = 0;
  2372. typedef struct istack_elt
  2373. {
  2374.   struct istack_elt *next;
  2375.   char *item_function;
  2376.   int line_number;
  2377.   int filling_enabled;
  2378.   int indented_fill;
  2379.   enum insertion_type insertion;
  2380.   int inhibited;
  2381. } INSERTION_ELT;
  2382.  
  2383. INSERTION_ELT *insertion_stack = (INSERTION_ELT *) NULL;
  2384.  
  2385. init_insertion_stack ()
  2386. {
  2387.   insertion_stack = (INSERTION_ELT *) NULL;
  2388. }
  2389.  
  2390. /* Return the type of the current insertion. */
  2391. enum insertion_type
  2392. current_insertion_type ()
  2393. {
  2394.   if (!insertion_level)
  2395.     return (bad_type);
  2396.   else
  2397.     return (insertion_stack->insertion);
  2398. }
  2399.  
  2400. /* Return a pointer to the string which is the function
  2401.    to wrap around items. */
  2402. char *
  2403. current_item_function ()
  2404. {
  2405.   if (!insertion_level)
  2406.     return ((char *) NULL);
  2407.   else
  2408.     return (insertion_stack->item_function);
  2409. }
  2410.  
  2411. char *
  2412. get_item_function ()
  2413. {
  2414.   char *item_function;
  2415.   get_rest_of_line (&item_function);
  2416.   backup_input_pointer ();
  2417.   canon_white (item_function);
  2418.   return (item_function);
  2419. }
  2420.  
  2421.  /* Push the state of the current insertion on the stack. */
  2422. push_insertion (type, item_function)
  2423.      enum insertion_type type;
  2424.      char *item_function;
  2425. {
  2426.   INSERTION_ELT *new = (INSERTION_ELT *) xmalloc (sizeof (INSERTION_ELT));
  2427.  
  2428.   new->item_function = item_function;
  2429.   new->filling_enabled = filling_enabled;
  2430.   new->indented_fill = indented_fill;
  2431.   new->insertion = type;
  2432.   new->line_number = line_number;
  2433.   new->inhibited = inhibit_paragraph_indentation;
  2434.   new->next = insertion_stack;
  2435.   insertion_stack = new;
  2436.   insertion_level++;
  2437. }
  2438.  
  2439.  /* Pop the value on top of the insertion stack into the
  2440.     global variables. */
  2441. pop_insertion ()
  2442. {
  2443.   INSERTION_ELT *temp = insertion_stack;
  2444.   if (temp == (INSERTION_ELT *) NULL)
  2445.     return;
  2446.   inhibit_paragraph_indentation = temp->inhibited;
  2447.   filling_enabled = insertion_stack->filling_enabled;
  2448.   indented_fill = insertion_stack->indented_fill;
  2449.   free_and_clear (&(temp->item_function));
  2450.   insertion_stack = insertion_stack->next;
  2451.   free (temp);
  2452.   insertion_level--;
  2453. }
  2454.  
  2455.  /* Return a pointer to the print name of this
  2456.     enumerated type. */
  2457. char *
  2458. insertion_type_pname (type)
  2459.      enum insertion_type type;
  2460. {
  2461.   if ((int) type < (int) bad_type)
  2462.     return (insertion_type_names[(int) type]);
  2463.   else
  2464.     return ("Broken-Type in insertion_type_pname");
  2465. }
  2466.  
  2467. /* Return the insertion_type associated with NAME.
  2468.    If the type is not one of the known ones, return BAD_TYPE. */
  2469. enum insertion_type
  2470. find_type_from_name (name)
  2471.      char *name;
  2472. {
  2473.   int index = 0;
  2474.   while (index < (int) bad_type)
  2475.     {
  2476.       if (stricmp (name, insertion_type_names[index]) == 0)
  2477.     return (enum insertion_type) index;
  2478.       index++;
  2479.     }
  2480.   return (bad_type);
  2481. }
  2482.  
  2483. do_nothing ()
  2484. {
  2485. }
  2486.  
  2487. int
  2488. defun_insertion (type)
  2489.      enum insertion_type type;
  2490. {
  2491.   return
  2492.     ((type == deffn)
  2493.      || (type == defun)
  2494.      || (type == defmac)
  2495.      || (type == defspec)
  2496.      || (type == defvr)
  2497.      || (type == defvar)
  2498.      || (type == defopt)
  2499.      || (type == deftypefn)
  2500.      || (type == deftypefun)
  2501.      || (type == deftypevr)
  2502.      || (type == deftypevar)
  2503.      || (type == defcv)
  2504.      || (type == defivar)
  2505.      || (type == defop)
  2506.      || (type == defmethod)
  2507.      || (type == deftypemethod)
  2508.      || (type == deftp));
  2509. }
  2510.  
  2511. /* Non-zero means that we are currently hacking the insides of an
  2512.    insertion which would use a fixed width font. */
  2513. int in_fixed_width_font = 0;
  2514.  
  2515. /* This is where the work for all the "insertion" style
  2516.    commands is done.  A huge switch statement handles the
  2517.    various setups, and generic code is on both sides. */
  2518. begin_insertion (type)
  2519.      enum insertion_type type;
  2520. {
  2521.   int no_discard = 0;
  2522.  
  2523.   close_paragraph ();
  2524.  
  2525.   if (defun_insertion (type))
  2526.     {
  2527.       push_insertion (type, savestring (""));
  2528.       no_discard++;
  2529.     }
  2530.   else
  2531.     push_insertion (type, get_item_function ());
  2532.  
  2533.   filling_enabled = false;    /* The general case for insertions. */
  2534.   inhibit_paragraph_indentation = 1;
  2535.   no_indent = false;
  2536.  
  2537.   switch (type)
  2538.     {
  2539.     case menu:
  2540.       add_word ("* Menu:\n");
  2541.       in_menu++;
  2542.       no_discard++;
  2543.       break;
  2544.  
  2545.       /* I think @quotation is meant to do filling.
  2546.      If you don't want filling, then use @example. */
  2547.     case quotation:
  2548.       last_char_was_newline = 0;
  2549.       indented_fill = filling_enabled = true;
  2550.       current_indent += default_indentation_increment;
  2551.       break;
  2552.  
  2553.     case display:
  2554.     case example:
  2555.     case smallexample:
  2556.     case lisp:
  2557.     case smalllisp:
  2558.       last_char_was_newline = 0;
  2559.       current_indent += default_indentation_increment;
  2560.       in_fixed_width_font++;
  2561.       break;
  2562.  
  2563.       /* Just like @example, but no indentation. */
  2564.     case format:
  2565.       last_char_was_newline = 0;
  2566.       in_fixed_width_font++;
  2567.       break;
  2568.  
  2569.     case table:
  2570.     case ftable:
  2571.     case itemize:
  2572.       current_indent += default_indentation_increment;
  2573.       filling_enabled = indented_fill = true;
  2574.  
  2575.       /* Make things work for losers who forget the itemize syntax. */
  2576.       if (type == itemize)
  2577.     {
  2578.       if (!(*insertion_stack->item_function))
  2579.         {
  2580.           free (insertion_stack->item_function);
  2581.           insertion_stack->item_function = savestring ("@bullet");
  2582.         }
  2583.     }
  2584.       break;
  2585.  
  2586.     case enumerate:
  2587.       inhibit_paragraph_indentation = 0;
  2588.       current_indent += default_indentation_increment;
  2589.       start_numbering (1);
  2590.       filling_enabled = indented_fill = true;
  2591.       break;
  2592.  
  2593.     case alphaenumerate:
  2594.     case capsenumerate:
  2595.       inhibit_paragraph_indentation = 0;
  2596.       current_indent += default_indentation_increment;
  2597.       filling_enabled = indented_fill = true;
  2598.  
  2599.       if (type == alphaenumerate)
  2600.     start_lettering ('a');
  2601.       else
  2602.     start_lettering ('A');
  2603.  
  2604.       break;
  2605.  
  2606.       /* Insertions that are no-ops in info, but do something in TeX. */
  2607.     case group:
  2608.     case ifinfo:
  2609.     case ifset:
  2610.     case ifclear:
  2611.     case cartouche:
  2612.       inhibit_paragraph_indentation = insertion_stack->inhibited;
  2613.       filling_enabled = insertion_stack->filling_enabled;
  2614.       indented_fill = insertion_stack->indented_fill;
  2615.       break;
  2616.  
  2617.     case deffn:
  2618.     case defun:
  2619.     case defmac:
  2620.     case defspec:
  2621.     case defvr:
  2622.     case defvar:
  2623.     case defopt:
  2624.     case deftypefn:
  2625.     case deftypefun:
  2626.     case deftypevr:
  2627.     case deftypevar:
  2628.     case defcv:
  2629.     case defivar:
  2630.     case defop:
  2631.     case defmethod:
  2632.     case deftypemethod:
  2633.     case deftp:
  2634.       filling_enabled = indented_fill = true;
  2635.       current_indent += default_indentation_increment;
  2636.       break;
  2637.  
  2638.     case flushleft:
  2639.       filling_enabled = indented_fill = false;
  2640.       break;
  2641.  
  2642.     case flushright:
  2643.       filling_enabled = indented_fill = false;
  2644.       force_flush_right++;
  2645.       break;
  2646.     }
  2647.  
  2648.   if (!no_discard)
  2649.     discard_until ("\n");
  2650. }
  2651.  
  2652. /* Try to end the quotation with the specified type.
  2653.    Like begin_insertion (), this uses a giant switch statement as
  2654.    well.  A big difference is that you *must* pass a valid type to
  2655.    this function, and a value of bad_type gets translated to match
  2656.    the value currently on top of the stack.  If, however, the value
  2657.    passed is a valid type, and it doesn't match the top of the
  2658.    stack, then we produce an error.  Should fix this, somewhat
  2659.    unclean. */
  2660. end_insertion (type)
  2661.      enum insertion_type type;
  2662. {
  2663.   enum insertion_type temp_type;
  2664.  
  2665.   if (!insertion_level)
  2666.     return;
  2667.  
  2668.   temp_type = current_insertion_type ();
  2669.  
  2670.   if (type == bad_type)
  2671.     type = temp_type;
  2672.  
  2673.   if (type != temp_type)
  2674.     {
  2675.       line_error
  2676.     ("Expected `%s', but saw `%s'.",
  2677.      insertion_type_pname (temp_type), insertion_type_pname (type));
  2678.       return;
  2679.     }
  2680.  
  2681.   pop_insertion ();
  2682.  
  2683.   switch (type)
  2684.     {
  2685.     case menu:
  2686.       in_menu--;        /* No longer hacking menus. */
  2687.       break;
  2688.  
  2689.     case enumerate:
  2690.       stop_numbering ();
  2691.       current_indent -= default_indentation_increment;
  2692.       break;
  2693.  
  2694.     case alphaenumerate:
  2695.     case capsenumerate:
  2696.       stop_lettering ();
  2697.       current_indent -= default_indentation_increment;
  2698.       break;
  2699.  
  2700.     case flushleft:
  2701.     case group:
  2702.     case ifinfo:
  2703.     case ifset:
  2704.     case ifclear:
  2705.     case cartouche:
  2706.       break;
  2707.  
  2708.     case format:
  2709.       in_fixed_width_font--;
  2710.       break;
  2711.  
  2712.     case display:
  2713.     case example:
  2714.     case smallexample:
  2715.     case lisp:
  2716.     case smalllisp:
  2717.       in_fixed_width_font--;
  2718.       current_indent -= default_indentation_increment;
  2719.       break;
  2720.  
  2721.     case flushright:
  2722.       force_flush_right--;
  2723.       break;
  2724.  
  2725.     default:
  2726.       current_indent -= default_indentation_increment;
  2727.       break;
  2728.     }
  2729.   close_paragraph ();
  2730.   must_start_paragraph = 1;
  2731. }
  2732.  
  2733. /* Insertions cannot cross certain boundaries, such as node beginnings.  In
  2734.    code that creates such boundaries, you should call discard_insertions ()
  2735.    before doing anything else.  It prints the errors for you, and cleans up
  2736.    the insertion stack. */
  2737. discard_insertions ()
  2738. {
  2739.   int real_line_number = line_number;
  2740.   while (insertion_stack)
  2741.     {
  2742.       if (insertion_stack->insertion == ifinfo ||
  2743.       insertion_stack->insertion == ifset ||
  2744.       insertion_stack->insertion == ifclear ||
  2745.       insertion_stack->insertion == cartouche)
  2746.     break;
  2747.       else
  2748.     {
  2749.       char *offender = (char *)
  2750.         insertion_type_pname (insertion_stack->insertion);
  2751.  
  2752.       line_number = insertion_stack->line_number;
  2753.       line_error ("This `%s' doesn't have a matching `%cend %s'", offender,
  2754.               COMMAND_PREFIX, offender);
  2755.       pop_insertion ();
  2756.     }
  2757.     }
  2758.   line_number = real_line_number;
  2759. }
  2760.  
  2761. /* MAX_NS is the maximum nesting level for enumerations.  I picked 100
  2762.    which seemed reasonable.  This doesn't control the number of items,
  2763.    just the number of nested lists. */
  2764. #define max_ns 100
  2765. int number_stack[max_ns];
  2766. int number_offset = 0;
  2767. int the_current_number = 0;
  2768.  
  2769. int letter_stack[max_ns];
  2770. int letter_offset = 0;
  2771. int the_current_letter = 0;
  2772.  
  2773. start_numbering (at_number)
  2774.      int at_number;
  2775. {
  2776.   if (number_offset + 1 == max_ns)
  2777.     {
  2778.       line_error ("Enumeration stack overflow");
  2779.       return;
  2780.     }
  2781.   number_stack[number_offset++] = the_current_number;
  2782.   the_current_number = at_number;
  2783. }
  2784.  
  2785. stop_numbering ()
  2786. {
  2787.   the_current_number = number_stack[--number_offset];
  2788.   if (number_offset < 0)
  2789.     number_offset = 0;
  2790. }
  2791.  
  2792. start_lettering (at_letter)
  2793.      int at_letter;
  2794. {
  2795.   if (letter_offset + 1 == max_ns)
  2796.     {
  2797.       line_error ("Alpha-enumeration stack overflow");
  2798.       return;
  2799.     }
  2800.   letter_stack[letter_offset++] = the_current_letter;
  2801.   the_current_letter = at_letter;
  2802. }
  2803.  
  2804. stop_lettering ()
  2805. {
  2806.   the_current_letter = letter_stack[--letter_offset];
  2807.   if (letter_offset < 0)
  2808.     letter_offset = 0;
  2809. }
  2810.  
  2811. /* Place a letter into the output stream. */
  2812. letter_item ()
  2813. {
  2814.   char temp[10];
  2815.  
  2816.   sprintf (temp, "%c) ", the_current_letter);
  2817.   indent (output_column += (current_indent - strlen (temp)));
  2818.   add_word (temp);
  2819.  
  2820.   if (the_current_letter == 'z' || the_current_letter == 'Z')
  2821.     {
  2822.       the_current_letter = (the_current_letter == 'z' ? 'a' : 'A');
  2823.       warning ("Lettering overflow, restarting at %c", the_current_letter);
  2824.     }
  2825.   else
  2826.     the_current_letter++;
  2827. }
  2828.  
  2829. /* Place a number into the output stream. */
  2830. number_item ()
  2831. {
  2832.   char temp[10];
  2833.   sprintf (temp, "%d. ", the_current_number);
  2834.   indent (output_column += (current_indent - strlen (temp)));
  2835.   add_word (temp);
  2836.   the_current_number++;
  2837. }
  2838.  
  2839. /* The actual commands themselves. */
  2840.  
  2841. /* Commands which insert themselves. */
  2842. insert_self ()
  2843. {
  2844.   add_word (command);
  2845. }
  2846.  
  2847. /* Force line break */
  2848. cm_asterisk ()
  2849. {
  2850.   /* Force a line break in the output. */
  2851.   insert ('\n');
  2852.   indent (output_column = current_indent);
  2853. }
  2854.  
  2855. /* Insert ellipsis. */
  2856. cm_dots (arg)
  2857.      int arg;
  2858. {
  2859.   if (arg == START)
  2860.     add_word ("...");
  2861. }
  2862.  
  2863. cm_bullet (arg)
  2864.      int arg;
  2865. {
  2866.   if (arg == START)
  2867.     add_char ('*');
  2868. }
  2869.  
  2870. cm_minus (arg)
  2871.      int arg;
  2872. {
  2873.   if (arg == START)
  2874.     add_char ('-');
  2875. }
  2876.  
  2877. /* Insert "TeX". */
  2878. cm_TeX (arg)
  2879.      int arg;
  2880. {
  2881.   if (arg == START)
  2882.     add_word ("TeX");
  2883. }
  2884.  
  2885. cm_copyright (arg)
  2886.      int arg;
  2887. {
  2888.   if (arg == START)
  2889.     add_word ("(C)");
  2890. }
  2891.  
  2892. cm_today (arg)
  2893.      int arg;
  2894. {
  2895.   static char * months [12] =
  2896.     { "January", "February", "March", "April", "May", "June", "July",
  2897.     "August", "September", "October", "November", "December" };
  2898.   if (arg == START)
  2899.     {
  2900.       long timer = (time (0));
  2901.       struct tm * ts = (localtime (&timer));
  2902.       add_word_args
  2903.     ("%d %s %d",
  2904.      (ts -> tm_mday),
  2905.      (months [ts -> tm_mon]),
  2906.      ((ts -> tm_year) + 1900));
  2907.     }
  2908. }
  2909.  
  2910. cm_code (arg)
  2911.      int arg;
  2912. {
  2913.   extern int printing_index;
  2914.  
  2915.   if (printing_index)
  2916.     return;
  2917.  
  2918.   if (arg == START)
  2919.     add_char ('`');
  2920.   else
  2921.     add_word ("'");
  2922. }
  2923.  
  2924. cm_samp (arg)
  2925.      int arg;
  2926. {
  2927.   cm_code (arg);
  2928. }
  2929.  
  2930. cm_file (arg)
  2931.      int arg;
  2932. {
  2933.   cm_code (arg);
  2934. }
  2935.  
  2936. cm_kbd (arg)
  2937.      int arg;
  2938. {
  2939.   cm_code (arg);
  2940. }
  2941.  
  2942. cm_key (arg)
  2943.      int arg;
  2944. {
  2945. }
  2946.  
  2947. /* Convert the character at position-1 into CTL. */
  2948. cm_ctrl (arg, position)
  2949.      int arg, position;
  2950. {
  2951.   if (arg == END)
  2952.     output_paragraph[position - 1] = CTL (output_paragraph[position - 1]);
  2953. }
  2954.  
  2955. /* Small Caps in makeinfo just does all caps. */
  2956. cm_sc (arg, start_pos, end_pos)
  2957.      int arg, start_pos, end_pos;
  2958. {
  2959.   if (arg == END)
  2960.     {
  2961.       while (start_pos < end_pos)
  2962.     {
  2963.       output_paragraph[start_pos] =
  2964.         coerce_to_upper (output_paragraph[start_pos]);
  2965.       start_pos++;
  2966.     }
  2967.     }
  2968. }
  2969.  
  2970. /* @var in makeinfo just uppercases the text. */
  2971. cm_var (arg, start_pos, end_pos)
  2972.      int arg, start_pos, end_pos;
  2973. {
  2974.   if (arg == END)
  2975.     {
  2976.       while (start_pos < end_pos)
  2977.     {
  2978.       output_paragraph[start_pos] =
  2979.         coerce_to_upper (output_paragraph[start_pos]);
  2980.       start_pos++;
  2981.     }
  2982.     }
  2983. }
  2984.  
  2985. cm_dfn (arg, position)
  2986.      int arg, position;
  2987. {
  2988.   add_char ('"');
  2989. }
  2990.  
  2991. cm_emph (arg)
  2992.      int arg;
  2993. {
  2994.   add_char ('*');
  2995. }
  2996.  
  2997. cm_strong (arg, position)
  2998.      int arg, position;
  2999. {
  3000.   cm_emph (arg);
  3001. }
  3002.  
  3003. cm_cite (arg, position)
  3004.      int arg, position;
  3005. {
  3006.   if (arg == START)
  3007.     add_word ("`");
  3008.   else
  3009.     add_word ("'");
  3010. }
  3011.  
  3012. /* Current text is italicized. */
  3013. cm_italic (arg, start, end)
  3014.      int arg, start, end;
  3015. {
  3016. }
  3017.  
  3018. /* Current text is highlighted. */
  3019. cm_bold (arg, start, end)
  3020.      int arg, start, end;
  3021. {
  3022.   cm_italic (arg);
  3023. }
  3024.  
  3025. /* Current text is in roman font. */
  3026. cm_roman (arg, start, end)
  3027.      int arg, start, end;
  3028. {
  3029. }
  3030.  
  3031. /* Italicize titles. */
  3032. cm_title (arg, start, end)
  3033.      int arg, start, end;
  3034. {
  3035.   cm_italic (arg);
  3036. }
  3037.  
  3038. /* @refill is a NOP. */
  3039. cm_refill ()
  3040. {
  3041. }
  3042.  
  3043. /* Prevent the argument from being split across two lines. */
  3044. cm_w (arg, start, end)
  3045.      int arg, start, end;
  3046. {
  3047.   if (arg == START)
  3048.     non_splitting_words++;
  3049.   else
  3050.     non_splitting_words--;
  3051. }
  3052.  
  3053.  
  3054. /* Explain that this command is obsolete, thus the user shouldn't
  3055.    do anything with it. */
  3056. cm_obsolete (arg, start, end)
  3057.      int arg, start, end;
  3058. {
  3059.   if (arg == START)
  3060.     warning ("The command `@%s' is obsolete", command);
  3061. }
  3062.  
  3063. /* Insert the text following input_text_offset up to the end of the line
  3064.    in a new, separate paragraph.  Directly underneath it, insert a
  3065.    line of WITH_CHAR, the same length of the inserted text. */
  3066. insert_and_underscore (with_char)
  3067.      int with_char;
  3068. {
  3069.   int len, i, old_no_indent;
  3070.   char *temp;
  3071.  
  3072.   close_paragraph ();
  3073.   filling_enabled =  indented_fill = false;
  3074.   old_no_indent = no_indent;
  3075.   no_indent = true;
  3076.   get_rest_of_line (&temp);
  3077.  
  3078.   len = output_position;
  3079.   execute_string ("%s\n", temp);
  3080.   free (temp);
  3081.  
  3082.   len = ((output_position + output_paragraph_offset) - 1) - len;
  3083.   for (i = 0; i < len; i++)
  3084.     add_char (with_char);
  3085.   insert ('\n');
  3086.   close_paragraph ();
  3087.   filling_enabled = true;
  3088.   no_indent = old_no_indent;
  3089. }
  3090.  
  3091. /* The remainder of the text on this line is a chapter heading. */
  3092. cm_chapter ()
  3093. {
  3094.   insert_and_underscore ('*');
  3095. }
  3096.  
  3097. /* The remainder of the text on this line is a section heading. */
  3098. cm_section ()
  3099. {
  3100.   insert_and_underscore ('=');
  3101. }
  3102.  
  3103. /* The remainder of the text on this line is a subsection heading. */
  3104. cm_subsection ()
  3105. {
  3106.   insert_and_underscore ('-');
  3107. }
  3108.  
  3109. /* The remainder of the text on this line is a subsubsection heading. */
  3110. cm_subsubsection ()
  3111. {
  3112.   insert_and_underscore ('.');
  3113. }
  3114.  
  3115. /* Here is a structure which associates sectioning commands with
  3116.    an integer, hopefully to reflect the `depth' of the current
  3117.    section. */
  3118. struct {
  3119.   char *name;
  3120.   int level;
  3121. } section_alist[] = {
  3122.   { "unnumberedsubsubsec", 5 },
  3123.   { "unnumberedsubsec", 4 },
  3124.   { "unnumberedsec", 3 },
  3125.   { "unnumbered", 2 },
  3126.   { "appendixsubsubsec", 5 },
  3127.   { "appendixsubsec", 4 },
  3128.   { "appendixsec", 3 },
  3129.   { "appendixsection", 3 },
  3130.   { "appendix", 2 },
  3131.   { "subsubsec", 5 },
  3132.   { "subsubsection", 5 },
  3133.   { "subsection", 4 },
  3134.   { "section", 3 },
  3135.   { "chapter", 2 },
  3136.   { "top", 1 },
  3137.  
  3138.   { (char *)NULL, 0 }
  3139. };
  3140.  
  3141. /* Return an integer which identifies the type section present in TEXT. */
  3142. int
  3143. what_section (text)
  3144.      char *text;
  3145. {
  3146.   int i, j;
  3147.   char *t;
  3148.  
  3149.  find_section_command:
  3150.   for (j = 0; text[j] && cr_or_whitespace (text[j]); j++);
  3151.   if (text[j] != '@')
  3152.     return (-1);
  3153.  
  3154.   text = text + j + 1;
  3155.  
  3156.   /* We skip @comment commands. */
  3157.   if (strncmp (text, "comment", strlen ("comment")) == 0)
  3158.     {
  3159.       for (j = 0; text[j] && text[j] != '\n'; j++);
  3160.       goto find_section_command;
  3161.     }
  3162.  
  3163.   /* Handle italicized sectioning commands. */
  3164.   if (*text == 'i')
  3165.     text++;
  3166.  
  3167.   for (j = 0; text[j] && !cr_or_whitespace (text[j]); j++);
  3168.  
  3169.   for (i = 0; t = section_alist[i].name; i++)
  3170.     {
  3171.       if (j == strlen (t) && strncmp (t, text, j) == 0)
  3172.     return (section_alist[i].level);
  3173.     }
  3174.   return (-1);
  3175. }
  3176.  
  3177. /* Treat this just like @unnumbered.  The only difference is
  3178.    in node defaulting. */
  3179. cm_top ()
  3180. {
  3181.   static int top_encountered = 0;
  3182.   cm_unnumbered ();
  3183.  
  3184.   /* It is an error to have more than one @top. */
  3185.   if (top_encountered)
  3186.     {
  3187.       TAG_ENTRY *tag = tag_table;
  3188.  
  3189.       line_error ("There already is a node having @top as a section");
  3190.  
  3191.       while (tag != (TAG_ENTRY *)NULL)
  3192.     {
  3193.       if ((tag->flags & IS_TOP))
  3194.         {
  3195.           int old_line_number = line_number;
  3196.           line_number = tag->line_no;
  3197.           line_error ("Here is the @top node.");
  3198.           line_number = old_line_number;
  3199.           return;
  3200.         }
  3201.       tag = tag->next_ent;
  3202.     }
  3203.     }
  3204.   else
  3205.     {
  3206.       top_encountered = 1;
  3207.  
  3208.       /* The most recently defined node is the top node. */
  3209.       if (tag_table)
  3210.     tag_table->flags |= IS_TOP;
  3211.  
  3212.       /* Now set the logical hierarchical level of the Top node. */
  3213.       {
  3214.     int orig_offset = input_text_offset;
  3215.  
  3216.     input_text_offset = search_forward ("\n@node", orig_offset);
  3217.  
  3218.     if (input_text_offset > 0)
  3219.       {
  3220.         int this_section;
  3221.  
  3222.         /* Move to the end of this line, and find out what the
  3223.            sectioning command is here. */
  3224.         while (input_text[input_text_offset] != '\n')
  3225.           input_text_offset++;
  3226.  
  3227.         if (input_text_offset < size_of_input_text)
  3228.           input_text_offset++;
  3229.  
  3230.         this_section = what_section (input_text + input_text_offset);
  3231.  
  3232.         /* If we found a sectioning command, then give the top section
  3233.            a level of this section - 1. */
  3234.         if (this_section != -1)
  3235.           {
  3236.         register int i;
  3237.  
  3238.         for (i = 0; section_alist[i].name; i++)
  3239.           if (strcmp (section_alist[i].name, "Top") == 0)
  3240.             {
  3241.               section_alist[i].level = this_section - 1;
  3242.               break;
  3243.             }
  3244.           }
  3245.       }
  3246.     input_text_offset = orig_offset;
  3247.       }
  3248.     }
  3249. }
  3250.  
  3251. /* The remainder of the text on this line is an unnumbered heading. */
  3252. cm_unnumbered ()
  3253. {
  3254.   cm_chapter ();
  3255. }
  3256.  
  3257. /* The remainder of the text on this line is an unnumbered section heading. */
  3258. cm_unnumberedsec ()
  3259. {
  3260.   cm_section ();
  3261. }
  3262.  
  3263. /* The remainder of the text on this line is an unnumbered
  3264.    subsection heading. */
  3265. cm_unnumberedsubsec ()
  3266. {
  3267.   cm_subsection ();
  3268. }
  3269.  
  3270. /* The remainder of the text on this line is an unnumbered
  3271.    subsubsection heading. */
  3272. cm_unnumberedsubsubsec ()
  3273. {
  3274.   cm_subsubsection ();
  3275. }
  3276.  
  3277. /* The remainder of the text on this line is an appendix heading. */
  3278. cm_appendix ()
  3279. {
  3280.   cm_chapter ();
  3281. }
  3282.  
  3283. /* The remainder of the text on this line is an appendix section heading. */
  3284. cm_appendixsec ()
  3285. {
  3286.   cm_section ();
  3287. }
  3288.  
  3289. /* The remainder of the text on this line is an appendix subsection heading. */
  3290. cm_appendixsubsec ()
  3291. {
  3292.   cm_subsection ();
  3293. }
  3294.  
  3295. /* The remainder of the text on this line is an appendix
  3296.    subsubsection heading. */
  3297. cm_appendixsubsubsec ()
  3298. {
  3299.   cm_subsubsection ();
  3300. }
  3301.  
  3302. /* Compatibility functions substitute for chapter, section, etc. */
  3303. cm_majorheading ()
  3304. {
  3305.   cm_chapheading ();
  3306. }
  3307.  
  3308. cm_chapheading ()
  3309. {
  3310.   cm_chapter ();
  3311. }
  3312.  
  3313. cm_heading ()
  3314. {
  3315.   cm_section ();
  3316. }
  3317.  
  3318. cm_subheading ()
  3319. {
  3320.   cm_subsection ();
  3321. }
  3322.  
  3323. cm_subsubheading ()
  3324. {
  3325.   cm_subsubsection ();
  3326. }
  3327.  
  3328.  
  3329. /* **************************************************************** */
  3330. /*                                    */
  3331. /*           Adding nodes, and making tags            */
  3332. /*                                    */
  3333. /* **************************************************************** */
  3334.  
  3335. /* Start a new tag table. */
  3336. init_tag_table ()
  3337. {
  3338.   while (tag_table != (TAG_ENTRY *) NULL)
  3339.     {
  3340.       TAG_ENTRY *temp = tag_table;
  3341.       free (temp->node);
  3342.       free (temp->prev);
  3343.       free (temp->next);
  3344.       free (temp->up);
  3345.       tag_table = tag_table->next_ent;
  3346.       free (temp);
  3347.     }
  3348. }
  3349.  
  3350. write_tag_table ()
  3351. {
  3352.   return (write_tag_table_internal (false));    /* Not indirect. */
  3353. }
  3354.  
  3355. write_tag_table_indirect ()
  3356. {
  3357.   return (write_tag_table_internal (true));
  3358. }
  3359.  
  3360. /* Write out the contents of the existing tag table.
  3361.    INDIRECT_P says how to format the output. */
  3362. write_tag_table_internal (indirect_p)
  3363.      boolean indirect_p;
  3364. {
  3365.   TAG_ENTRY *node = tag_table;
  3366.   boolean old_indent = no_indent;
  3367.  
  3368.   no_indent = true;
  3369.   filling_enabled = false;
  3370.   must_start_paragraph = 0;
  3371.   close_paragraph ();
  3372.  
  3373.   if (!indirect_p)
  3374.     {
  3375.       no_indent = true;
  3376.       insert ('\n');
  3377.     }
  3378.  
  3379.   add_word_args ("\037\nTag Table:\n%s", indirect_p ? "(Indirect)\n" : "");
  3380.  
  3381.   while (node != (TAG_ENTRY *) NULL)
  3382.     {
  3383.       add_word_args ("Node: %s\177%d\n", node->node, node->position);
  3384.       node = node->next_ent;
  3385.     }
  3386.  
  3387.   add_word ("\037\nEnd Tag Table\n");
  3388.   flush_output ();
  3389.   no_indent = old_indent;
  3390. }
  3391.  
  3392. char *
  3393. get_node_token ()
  3394. {
  3395.   char *string;
  3396.  
  3397.   get_until_in_line (",", &string);
  3398.  
  3399.   if (curchar () == ',')
  3400.     input_text_offset++;
  3401.  
  3402.   canon_white (string);
  3403.  
  3404.   /* Allow things like @@nodename. */
  3405.   normalize_node_name (string);
  3406.  
  3407.   return (string);
  3408. }
  3409.  
  3410. /* Given a node name in STRING, remove double @ signs, replacing them
  3411.    with just one. */
  3412. normalize_node_name (string)
  3413.      char *string;
  3414. {
  3415.   register int i, l = strlen (string);
  3416.  
  3417.   for (i = 0; i < l; i++)
  3418.     {
  3419.       if (string[i] == '@' && string[i + 1] == '@')
  3420.     {
  3421.       strncpy (string + i, string + i + 1, l - i);
  3422.       l--;
  3423.     }
  3424.     }
  3425. }
  3426.  
  3427. /* Look up NAME in the tag table, and return the associated
  3428.    tag_entry.  If the node is not in the table return NULL. */
  3429. TAG_ENTRY *
  3430. find_node (name)
  3431.      char *name;
  3432. {
  3433.   TAG_ENTRY *tag = tag_table;
  3434.  
  3435.   while (tag != (TAG_ENTRY *) NULL)
  3436.     {
  3437.       if (stricmp (tag->node, name) == 0)
  3438.     return (tag);
  3439.       tag = tag->next_ent;
  3440.     }
  3441.   return ((TAG_ENTRY *) NULL);
  3442. }
  3443.  
  3444. /* Remember NODE and associates. */
  3445. remember_node (node, prev, next, up, position, line_no, no_warn)
  3446.      char *node, *prev, *next, *up;
  3447.      int position, line_no, no_warn;
  3448. {
  3449.   /* Check for existence of this tag already. */
  3450.   if (validating)
  3451.     {
  3452.       register TAG_ENTRY *tag = find_node (node);
  3453.       if (tag)
  3454.     {
  3455.       line_error ("Node `%s' multiply defined (%d is first definition)",
  3456.               node, tag->line_no);
  3457.       return;
  3458.     }
  3459.     }
  3460.  
  3461.   /* First, make this the current node. */
  3462.   current_node = node;
  3463.  
  3464.   /* Now add it to the list. */
  3465.   {
  3466.     TAG_ENTRY *new = (TAG_ENTRY *) xmalloc (sizeof (TAG_ENTRY));
  3467.     new->node = node;
  3468.     new->prev = prev;
  3469.     new->next = next;
  3470.     new->up = up;
  3471.     new->position = position;
  3472.     new->line_no = line_no;
  3473.     new->filename = node_filename;
  3474.     new->touched = 0;        /* not yet referenced. */
  3475.     new->flags = 0;
  3476.     if (no_warn)
  3477.       new->flags |= NO_WARN;
  3478.     new->next_ent = tag_table;
  3479.     tag_table = new;
  3480.   }
  3481. }
  3482.  
  3483.  
  3484. /* The order is: nodename, nextnode, prevnode, upnode.
  3485.    The next, prev, and up fields can be defaulted.
  3486.    You must follow a node command which has those fields
  3487.    defaulted with a sectioning command (e.g. @chapter) giving
  3488.    the "level" of that node.  It is an error not to do so.
  3489.    The defaults come from the menu in this nodes parent. */
  3490. cm_node ()
  3491. {
  3492.   char *node, *prev, *next, *up;
  3493.   int new_node_pos, defaulting, this_section, no_warn = 0;
  3494.   extern int already_outputting_pending_notes;
  3495.  
  3496.   if (strcmp (command, "nwnode") == 0)
  3497.     no_warn = 1;
  3498.  
  3499.   /* Get rid of unmatched brace arguments from previous commands. */
  3500.   discard_braces ();
  3501.  
  3502.   /* There also might be insertions left lying around that haven't been
  3503.      ended yet.  Do that also. */
  3504.   discard_insertions ();
  3505.  
  3506.   this_section = -1;
  3507.  
  3508.   if (!already_outputting_pending_notes)
  3509.     {
  3510.       close_paragraph ();
  3511.       output_pending_notes ();
  3512.       free_pending_notes ();
  3513.     }
  3514.  
  3515.   filling_enabled = indented_fill = false;
  3516.   new_node_pos = output_position + 1;
  3517.   current_footnote_number = 1;
  3518.  
  3519.   node = get_node_token ();
  3520.   next = get_node_token ();
  3521.   prev = get_node_token ();
  3522.   up = get_node_token ();
  3523.  
  3524.   no_indent = true;
  3525.   add_word_args ("\037\nFile: %s,  Node: %s", pretty_output_filename, node);
  3526.  
  3527.   /* Check for defaulting of this node's next, prev, and up fields. */
  3528.   defaulting = ((strlen (next) == 0) &&
  3529.         (strlen (prev) == 0) &&
  3530.         (strlen (up) == 0));
  3531.  
  3532.   /* If we are defaulting, then look at the immediately following
  3533.      sectioning command (error if none) to determine the node's
  3534.      level.  Find the node that contains the menu mentioning this node
  3535.      that is one level up (error if not found).  That node is the "Up"
  3536.      of this node.  Default the "Next" and "Prev" from the menu. */
  3537.   if (defaulting)
  3538.     {
  3539.       NODE_REF *last_ref = (NODE_REF *)NULL;
  3540.       NODE_REF *ref = node_references;
  3541.  
  3542.       this_section = what_section (input_text + input_text_offset);
  3543.  
  3544.       if (this_section < 0)
  3545.     {
  3546.       char *polite_section_name = "top";
  3547.       int i;
  3548.  
  3549.       for (i = 0; section_alist[i].name; i++)
  3550.         if (section_alist[i].level == current_section + 1)
  3551.           {
  3552.         polite_section_name = section_alist[i].name;
  3553.         break;
  3554.           }
  3555.  
  3556.       line_error
  3557.         ("Node `%s' requires a sectioning command (e.g. @%s)",
  3558.          node, polite_section_name);
  3559.     }
  3560.       else
  3561.     {
  3562.       if (stricmp (node, "Top") == 0)
  3563.         {
  3564.           /* Default the NEXT pointer to be the first menu item in
  3565.          this node, if there is a menu in this node. */
  3566.           {
  3567.         int orig_offset, orig_size;
  3568.         char *glean_node_from_menu ();
  3569.  
  3570.         orig_offset = input_text_offset;
  3571.         orig_size = search_forward ("\n@node ", orig_offset);
  3572.  
  3573.         if (orig_size < 0)
  3574.           orig_size = size_of_input_text;
  3575.  
  3576.         input_text_offset = search_forward ("\n@menu", orig_offset);
  3577.         if (input_text_offset > -1)
  3578.           {
  3579.             input_text_offset =
  3580.               search_forward ("\n* ", input_text_offset);
  3581.  
  3582.             if (input_text_offset > -1)
  3583.               next = glean_node_from_menu (0);
  3584.  
  3585.             if (next)
  3586.               {
  3587.             prev = savestring ("(DIR)");
  3588.             up = savestring ("(DIR)");
  3589.               }
  3590.           }
  3591.         input_text_offset = orig_offset;
  3592.           }
  3593.         }
  3594.  
  3595.       while (ref)
  3596.         {
  3597.           if (ref->section == (this_section - 1) &&
  3598.           ref->type == menu_reference &&
  3599.           stricmp (ref->node, node) == 0)
  3600.         {
  3601.           char *containing_node = ref->containing_node;
  3602.  
  3603.           free (up);
  3604.           up = savestring (containing_node);
  3605.  
  3606.           if (last_ref &&
  3607.               strcmp
  3608.               (last_ref->containing_node, containing_node) == 0)
  3609.             {
  3610.               free (next);
  3611.               next = savestring (last_ref->node);
  3612.             }
  3613.  
  3614.           while ((ref->section == this_section - 1) &&
  3615.              (ref->next) &&
  3616.              (ref->next->type != menu_reference))
  3617.             ref = ref->next;
  3618.  
  3619.           if (ref->next &&
  3620.               strcmp
  3621.               (ref->next->containing_node, containing_node) == 0)
  3622.             {
  3623.               free (prev);
  3624.               prev = savestring (ref->next->node);
  3625.             }
  3626.           else if (!ref->next &&
  3627.                stricmp (ref->containing_node, "Top") == 0)
  3628.             {
  3629.               free (prev);
  3630.               prev = savestring (ref->containing_node);
  3631.             }
  3632.           break;
  3633.         }
  3634.           last_ref = ref;
  3635.           ref = ref->next;
  3636.         }
  3637.     }
  3638.     }
  3639.  
  3640.   if (*next)
  3641.     add_word_args (",  Next: %s", next);
  3642.  
  3643.   if (*prev)
  3644.     add_word_args (",  Prev: %s", prev);
  3645.  
  3646.   if (*up)
  3647.     add_word_args (",  Up: %s", up);
  3648.  
  3649.   insert ('\n');
  3650.   close_paragraph ();
  3651.   no_indent = false;
  3652.  
  3653.   if (!*node)
  3654.     {
  3655.       line_error ("No node name specified for `@%s' command", command);
  3656.       free (node);
  3657.       free (next);
  3658.       free (prev);
  3659.       free (up);
  3660.     }
  3661.   else
  3662.     {
  3663.       if (!*next) { free (next); next = (char *)NULL; }
  3664.       if (!*prev) { free (prev); prev = (char *)NULL; }
  3665.       if (!*up) { free (up); up = (char *)NULL; }
  3666.       remember_node (node, prev, next, up, new_node_pos, line_number, no_warn);
  3667.     }
  3668.  
  3669.   /* Change the section only if there was a sectioning command. */
  3670.   if (this_section >= 0)
  3671.     current_section = this_section;
  3672.  
  3673.   filling_enabled = true;
  3674. }
  3675.  
  3676. /* Validation of an info file.
  3677.    Scan through the list of tag entrys touching the Prev, Next, and Up
  3678.    elements of each.  It is an error not to be able to touch one of them,
  3679.    except in the case of external node references, such as "(DIR)".
  3680.  
  3681.    If the Prev is different from the Up,
  3682.    then the Prev node must have a Next pointing at this node.
  3683.  
  3684.    Every node except Top must have an Up.
  3685.    The Up node must contain some sort of reference, other than a Next,
  3686.    to this node.
  3687.  
  3688.    If the Next is different from the Next of the Up,
  3689.    then the Next node must have a Prev pointing at this node. */
  3690. validate_file (filename, tag_table)
  3691.      char *filename;
  3692.      TAG_ENTRY *tag_table;
  3693. {
  3694.   char *old_input_filename = input_filename;
  3695.   TAG_ENTRY *tags = tag_table;
  3696.  
  3697.   while (tags != (TAG_ENTRY *) NULL)
  3698.     {
  3699.       register TAG_ENTRY *temp_tag;
  3700.  
  3701.       input_filename = tags->filename;
  3702.       line_number = tags->line_no;
  3703.  
  3704.       /* If this node has a Next, then make sure that the Next exists. */
  3705.       if (tags->next)
  3706.     {
  3707.       validate (tags->next, tags->line_no, "Next");
  3708.  
  3709.       /* If the Next node exists, and there is no Up, then make
  3710.          sure that the Prev of the Next points back. */
  3711.       if (temp_tag = find_node (tags->next))
  3712.         {
  3713.           char *prev = temp_tag->prev;
  3714.           if (!prev || (stricmp (prev, tags->node) != 0))
  3715.         {
  3716.           line_error
  3717.             ("Node `%s''s Next field not pointed back to", tags->node);
  3718.           line_number = temp_tag->line_no;
  3719.           input_filename = temp_tag->filename;
  3720.           line_error
  3721.             ("This node (`%s') is the one with the bad `Prev'",
  3722.              temp_tag->node);
  3723.           input_filename = tags->filename;
  3724.           line_number = tags->line_no;
  3725.           temp_tag->flags |= PREV_ERROR;
  3726.         }
  3727.         }
  3728.     }
  3729.  
  3730.       /* Validate the Prev field if there is one, and we haven't already
  3731.      complained about it in some way.  You don't have to have a Prev
  3732.      field at this stage. */
  3733.       if (!(tags->flags & PREV_ERROR) && tags->prev)
  3734.     {
  3735.       int valid = validate (tags->prev, tags->line_no, "Prev");
  3736.  
  3737.       if (!valid)
  3738.         tags->flags |= PREV_ERROR;
  3739.       else
  3740.         {
  3741.           /* If the Prev field is not the same as the Up field,
  3742.          then the node pointed to by the Prev field must have
  3743.          a Next field which points to this node. */
  3744.           if (tags->up && (stricmp (tags->prev, tags->up) != 0))
  3745.         {
  3746.           temp_tag = find_node (tags->prev);
  3747.           if (!temp_tag->next ||
  3748.               (stricmp (temp_tag->next, tags->node) != 0))
  3749.             {
  3750.               line_error ("Node `%s''s Prev field not pointed back to",
  3751.                   tags->node);
  3752.               line_number = temp_tag->line_no;
  3753.               input_filename = temp_tag->filename;
  3754.               line_error
  3755.             ("This node (`%s') is the one with the bad `Next'",
  3756.              temp_tag->node);
  3757.               input_filename = tags->filename;
  3758.               line_number = tags->line_no;
  3759.               temp_tag->flags |= NEXT_ERROR;
  3760.             }
  3761.         }
  3762.         }
  3763.     }
  3764.  
  3765.       if (!tags->up && (stricmp (tags->node, "Top") != 0))
  3766.     line_error ("Node `%s' is missing an \"Up\" field", tags->node);
  3767.       else if (tags->up)
  3768.     {
  3769.       int valid = validate (tags->up, tags->line_no, "Up");
  3770.  
  3771.       /* If node X has Up: Y, then warn if Y fails to have a menu item
  3772.          or note pointing at X, if Y isn't of the form "(Y)". */
  3773.       if (valid && *tags->up != '(')
  3774.         {
  3775.           NODE_REF *nref, *tref, *list;
  3776.           NODE_REF *find_node_reference ();
  3777.  
  3778.           tref = (NODE_REF *) NULL;
  3779.           list = node_references;
  3780.  
  3781.           for (;;)
  3782.         {
  3783.           if (!(nref = find_node_reference (tags->node, list)))
  3784.             break;
  3785.  
  3786.           if (stricmp (nref->containing_node, tags->up) == 0)
  3787.             {
  3788.               if (nref->type != menu_reference)
  3789.             {
  3790.               tref = nref;
  3791.               list = nref->next;
  3792.             }
  3793.               else
  3794.             break;
  3795.             }
  3796.           list = nref->next;
  3797.         }
  3798.  
  3799.           if (!nref)
  3800.         {
  3801.           temp_tag = find_node (tags->up);
  3802.           line_number = temp_tag->line_no;
  3803.           filename = temp_tag->filename;
  3804.           if (!tref)
  3805.             line_error (
  3806. "`%s' has an Up field of `%s', but `%s' has no menu item for `%s'",
  3807.                 tags->node, tags->up, tags->up, tags->node);
  3808.           line_number = tags->line_no;
  3809.           filename = tags->filename;
  3810.         }
  3811.         }
  3812.     }
  3813.       tags = tags->next_ent;
  3814.     }
  3815.  
  3816.   validate_other_references (node_references);
  3817.   /* We have told the user about the references which didn't exist.
  3818.      Now tell him about the nodes which aren't referenced. */
  3819.  
  3820.   tags = tag_table;
  3821.   while (tags != (TAG_ENTRY *) NULL)
  3822.     {
  3823.       /* Special hack.  If the node in question appears to have
  3824.          been referenced more than REFERENCE_WARNING_LIMIT times,
  3825.          give a warning. */
  3826.       if (tags->touched > reference_warning_limit)
  3827.     {
  3828.       input_filename = tags->filename;
  3829.       line_number = tags->line_no;
  3830.       warning ("Node `%s' has been referenced %d times",
  3831.            tags->node, tags->touched);
  3832.     }
  3833.  
  3834.       if (tags->touched == 0)
  3835.     {
  3836.       input_filename = tags->filename;
  3837.       line_number = tags->line_no;
  3838.  
  3839.       /* Notice that the node "Top" is special, and doesn't have to
  3840.          be referenced. */
  3841.       if (stricmp (tags->node, "Top") != 0)
  3842.         warning ("Unreferenced node `%s'", tags->node);
  3843.     }
  3844.       tags = tags->next_ent;
  3845.     }
  3846.   input_filename = old_input_filename;
  3847. }
  3848.  
  3849. /* Return 1 if tag correctly validated, or 0 if not. */
  3850. validate (tag, line, label)
  3851.      char *tag;
  3852.      int line;
  3853.      char *label;
  3854. {
  3855.   TAG_ENTRY *result;
  3856.  
  3857.   /* If there isn't a tag to verify, or if the tag is in another file,
  3858.      then it must be okay. */
  3859.   if (!tag || !*tag || *tag == '(')
  3860.     return (1);
  3861.  
  3862.   /* Otherwise, the tag must exist. */
  3863.   result = find_node (tag);
  3864.  
  3865.   if (!result)
  3866.     {
  3867.       line_number = line;
  3868.       line_error (
  3869. "Validation error.  `%s' field points to node `%s', which doesn't exist",
  3870.           label, tag);
  3871.       return (0);
  3872.     }
  3873.   result->touched++;
  3874.   return (1);
  3875. }
  3876.  
  3877. /* Split large output files into a series of smaller files.  Each file
  3878.    is pointed to in the tag table, which then gets written out as the
  3879.    original file.  The new files have the same name as the original file
  3880.    with a "-num" attached.  SIZE is the largest number of bytes to allow
  3881.    in any single split file. */
  3882. split_file (filename, size)
  3883.      char *filename;
  3884.      int size;
  3885. {
  3886.   char *root_filename, *root_pathname;
  3887.   char *the_file, *filename_part ();
  3888.   struct stat fileinfo;
  3889.   char *the_header;
  3890.   int header_size;
  3891.  
  3892.   /* Can only do this to files with tag tables. */
  3893.   if (!tag_table)
  3894.     return;
  3895.  
  3896.   if (size == 0)
  3897.     size = DEFAULT_SPLIT_SIZE;
  3898.  
  3899.   if ((stat (filename, &fileinfo) != 0) ||
  3900.       (fileinfo.st_size < SPLIT_SIZE_THRESHOLD))
  3901.     return;
  3902.  
  3903.   the_file = find_and_load (filename);
  3904.   if (!the_file)
  3905.     return;
  3906.  
  3907.   root_filename = filename_part (filename);
  3908.   root_pathname = pathname_part (filename);
  3909.  
  3910.   if (!root_pathname)
  3911.     root_pathname = savestring ("");
  3912.  
  3913.   /* Start splitting the file.  Walk along the tag table
  3914.      outputting sections of the file.  When we have written
  3915.      all of the nodes in the tag table, make the top-level
  3916.      pointer file, which contains indirect pointers and
  3917.      tags for the nodes. */
  3918.   {
  3919.     int which_file = 1;
  3920.     TAG_ENTRY *tags = tag_table;
  3921.     char *indirect_info = (char *)NULL;
  3922.  
  3923.     /* Remember the `header' of this file.  The first tag in the file is
  3924.        the bottom of the header; the top of the file is the start. */
  3925.     the_header = (char *)xmalloc (1 + (header_size = (tags->position - 2)));
  3926.     bcopy (the_file, the_header, header_size);
  3927.  
  3928.     while (tags)
  3929.       {
  3930.     int file_top, file_bot, limit;
  3931.  
  3932.     /* Have to include the Control-_. */
  3933.     file_top = file_bot = tags->position - 2;
  3934.     limit = file_top + size;
  3935.  
  3936.     /* If the rest of this file is only one node, then
  3937.        that is the entire subfile. */
  3938.     if (!tags->next_ent)
  3939.       {
  3940.         int i = tags->position + 1;
  3941.         char last_char = the_file[i];
  3942.  
  3943.         while (i < fileinfo.st_size)
  3944.           {
  3945.         if ((the_file[i] == '\037') &&
  3946.             ((last_char == '\n') ||
  3947.              (last_char == '\014')))
  3948.           break;
  3949.         else
  3950.           last_char = the_file[i];
  3951.         i++;
  3952.           }
  3953.         file_bot = i;
  3954.         tags = tags->next_ent;
  3955.         goto write_region;
  3956.       }
  3957.  
  3958.     /* Otherwise, find the largest number of nodes that can fit in
  3959.        this subfile. */
  3960.     for (; tags; tags = tags->next_ent)
  3961.       {
  3962.         if (!tags->next_ent)
  3963.           {
  3964.         /* This entry is the last node.  Search forward for the end
  3965.                of this node, and that is the end of this file. */
  3966.         int i = tags->position + 1;
  3967.         char last_char = the_file[i];
  3968.  
  3969.         while (i < fileinfo.st_size)
  3970.           {
  3971.             if ((the_file[i] == '\037') &&
  3972.             ((last_char == '\n') ||
  3973.              (last_char == '\014')))
  3974.               break;
  3975.             else
  3976.               last_char = the_file[i];
  3977.             i++;
  3978.           }
  3979.         file_bot = i;
  3980.  
  3981.         if (file_bot < limit)
  3982.           {
  3983.             tags = tags->next_ent;
  3984.             goto write_region;
  3985.           }
  3986.         else
  3987.           {
  3988.             /* Here we want to write out everything before the last
  3989.                node, and then write the last node out in a file
  3990.                by itself. */
  3991.             file_bot = tags->position;
  3992.             goto write_region;
  3993.           }
  3994.           }
  3995.  
  3996.         if (tags->next_ent->position > limit)
  3997.           {
  3998.         if ((tags->position) - 2 == file_top)
  3999.           tags = tags->next_ent;
  4000.         file_bot = tags->position;
  4001.           write_region:
  4002.         {
  4003.           int fd;
  4004.           char *split_file = (char *)
  4005.             xmalloc (10
  4006.                  + strlen (root_pathname)
  4007.                  + strlen (root_filename));
  4008.           sprintf (split_file,
  4009.                "%s%s-%d", root_pathname, root_filename, which_file);
  4010.  
  4011.           if (((fd = open (split_file, O_WRONLY | O_TRUNC | O_CREAT, 0666)) < 0)
  4012.               || (write (fd, the_header, header_size) != header_size)
  4013.               || (write (fd, the_file + file_top, file_bot - file_top)
  4014.               != (file_bot - file_top))
  4015.               || ((close (fd)) < 0))
  4016.             {
  4017.               perror (split_file);
  4018.               close (fd);
  4019.               exit (FATAL);
  4020.             }
  4021.  
  4022.           if (!indirect_info)
  4023.             {
  4024.               indirect_info = the_file + file_top;
  4025.               sprintf (indirect_info, "\037\nIndirect:\n");
  4026.               indirect_info += strlen (indirect_info);
  4027.             }
  4028.  
  4029.           sprintf (indirect_info, "%s-%d: %d\n",
  4030.                root_filename, which_file, file_top);
  4031.  
  4032.           free (split_file);
  4033.           indirect_info += strlen (indirect_info);
  4034.           which_file++;
  4035.           break;
  4036.         }
  4037.           }
  4038.       }
  4039.       }
  4040.  
  4041.     /* We have sucessfully created the subfiles.  Now write out the
  4042.        original again.  We must use `output_stream', or
  4043.        write_tag_table_indirect () won't know where to place the output. */
  4044.     output_stream = fopen (filename, "w");
  4045.     if (!output_stream)
  4046.       {
  4047.     perror (filename);
  4048.     exit (FATAL);
  4049.       }
  4050.  
  4051.     {
  4052.       int distance = indirect_info - the_file;
  4053.       fwrite (the_file, 1, distance, output_stream);
  4054.  
  4055.       /* Inhibit newlines. */
  4056.       paragraph_is_open = false;
  4057.  
  4058.       write_tag_table_indirect ();
  4059.       fclose (output_stream);
  4060.       free (the_header);
  4061.       free (the_file);
  4062.       return;
  4063.     }
  4064.   }
  4065. }
  4066.  
  4067. /* Some menu hacking.  This is used to remember menu references while
  4068.    reading the input file.  After the output file has been written, if
  4069.    validation is on, then we use the contents of NODE_REFERENCES as a
  4070.    list of nodes to validate. */
  4071. char *
  4072. reftype_type_string (type)
  4073.      enum reftype type;
  4074. {
  4075.   switch (type)
  4076.     {
  4077.     case menu_reference:
  4078.       return ("Menu");
  4079.     case followed_reference:
  4080.       return ("Followed-Reference");
  4081.     default:
  4082.       return ("Internal-bad-reference-type");
  4083.     }
  4084. }
  4085.  
  4086. /* Remember this node name for later validation use. */
  4087. remember_node_reference (node, line, type)
  4088.      char *node;
  4089.      int line;
  4090.      enum reftype type;
  4091. {
  4092.   NODE_REF *temp = (NODE_REF *) xmalloc (sizeof (NODE_REF));
  4093.  
  4094.   temp->next = node_references;
  4095.   temp->node = savestring (node);
  4096.   temp->line_no = line;
  4097.   temp->section = current_section;
  4098.   temp->type = type;
  4099.   temp->containing_node = savestring (current_node);
  4100.   temp->filename = node_filename;
  4101.  
  4102.   node_references = temp;
  4103. }
  4104.  
  4105. validate_other_references (ref_list)
  4106.      register NODE_REF *ref_list;
  4107. {
  4108.   char *old_input_filename = input_filename;
  4109.  
  4110.   while (ref_list != (NODE_REF *) NULL)
  4111.     {
  4112.       input_filename = ref_list->filename;
  4113.       validate (ref_list->node, ref_list->line_no,
  4114.         reftype_type_string (ref_list->type));
  4115.       ref_list = ref_list->next;
  4116.     }
  4117.   input_filename = old_input_filename;
  4118. }
  4119.  
  4120. /* Find NODE in REF_LIST. */
  4121. NODE_REF *
  4122. find_node_reference (node, ref_list)
  4123.      char *node;
  4124.      register NODE_REF *ref_list;
  4125. {
  4126.   while (ref_list)
  4127.     {
  4128.       if (stricmp (node, ref_list->node) == 0)
  4129.     break;
  4130.       ref_list = ref_list->next;
  4131.     }
  4132.   return (ref_list);
  4133. }
  4134.  
  4135. free_node_references ()
  4136. {
  4137.   register NODE_REF *list, *temp;
  4138.  
  4139.   list = node_references;
  4140.  
  4141.   while (list)
  4142.     {
  4143.       temp = list;
  4144.       free (list->node);
  4145.       free (list->containing_node);
  4146.       list = list->next;
  4147.       free (temp);
  4148.     }
  4149.   node_references = (NODE_REF *) NULL;
  4150. }
  4151.  
  4152.   /* This function gets called at the start of every line while inside of
  4153.      a menu.  It checks to see if the line starts with "* ", and if so,
  4154.      remembers the node reference that this menu refers to.
  4155.      input_text_offset is at the \n just before the line start. */
  4156. #define menu_starter "* "
  4157. char *
  4158. glean_node_from_menu (remember_reference)
  4159.      int remember_reference;
  4160. {
  4161.   int i, orig_offset = input_text_offset;
  4162.   char *nodename;
  4163.  
  4164.   if (strncmp (&input_text[input_text_offset + 1],
  4165.            menu_starter,
  4166.            strlen (menu_starter)) != 0)
  4167.     return ((char *)NULL);
  4168.   else
  4169.     input_text_offset += strlen (menu_starter) + 1;
  4170.  
  4171.   get_until_in_line (":", &nodename);
  4172.   if (curchar () == ':')
  4173.     input_text_offset++;
  4174.   canon_white (nodename);
  4175.  
  4176.   if (curchar () == ':')
  4177.     goto save_node;
  4178.  
  4179.   free (nodename);
  4180.   get_rest_of_line (&nodename);
  4181.  
  4182.   /* Special hack: If the nodename follows the menu item name,
  4183.      then we have to read the rest of the line in order to find
  4184.      out what the nodename is.  But we still have to read the
  4185.      line later, in order to process any formatting commands that
  4186.      might be present.  So un-count the carriage return that has just
  4187.      been counted. */
  4188.   line_number--;
  4189.  
  4190.   canon_white (nodename);
  4191.   for (i = 0; i < strlen (nodename); i++)
  4192.     {
  4193.       if (nodename[i] == '\t' ||
  4194.       nodename[i] == '.' ||
  4195.       nodename[i] == ',')
  4196.     {
  4197.       nodename[i] = '\0';
  4198.       break;
  4199.     }
  4200.     }
  4201. save_node:
  4202.   input_text_offset = orig_offset;
  4203.   normalize_node_name (nodename);
  4204.   i = strlen (nodename);
  4205.   if (i && nodename[i - 1] == ':')
  4206.     nodename[i - 1] = '\0';
  4207.  
  4208.   if (remember_reference)
  4209.     {
  4210.       remember_node_reference (nodename, line_number, menu_reference);
  4211.       free (nodename);
  4212.       return ((char *)NULL);
  4213.     }
  4214.   else
  4215.     return (nodename);
  4216. }
  4217.  
  4218. cm_menu ()
  4219. {
  4220.   begin_insertion (menu);
  4221. }
  4222.  
  4223.  
  4224. /* **************************************************************** */
  4225. /*                                    */
  4226. /*            Cross Reference Hacking                */
  4227. /*                                    */
  4228. /* **************************************************************** */
  4229.  
  4230. char *
  4231. get_xref_token ()
  4232. {
  4233.   char *string;
  4234.  
  4235.   get_until_in_braces (",", &string);
  4236.   if (curchar () == ',')
  4237.     input_text_offset++;
  4238.   fix_whitespace (string);
  4239.   return (string);
  4240. }
  4241.  
  4242. int px_ref_flag = 0;        /* Controls initial output string. */
  4243.  
  4244. /* Make a cross reference. */
  4245. cm_xref (arg)
  4246. {
  4247.   if (arg == START)
  4248.     {
  4249.       char *arg1, *arg2, *arg3, *arg4, *arg5;
  4250.  
  4251.       arg1 = get_xref_token ();
  4252.       arg2 = get_xref_token ();
  4253.       arg3 = get_xref_token ();
  4254.       arg4 = get_xref_token ();
  4255.       arg5 = get_xref_token ();
  4256.  
  4257.       add_word_args ("%s", px_ref_flag ? "*note " : "*Note ");
  4258.  
  4259.       if (*arg5 || *arg4)
  4260.     {
  4261.       char *node_name;
  4262.  
  4263.       if (!*arg2)
  4264.         {
  4265.           if (*arg3)
  4266.         node_name = arg3;
  4267.           else
  4268.         node_name = arg1;
  4269.         }
  4270.       else
  4271.         node_name = arg2;
  4272.  
  4273.       execute_string ("%s: (%s)%s", node_name, arg4, arg1);
  4274.       return;
  4275.     }
  4276.       else
  4277.     remember_node_reference (arg1, line_number, followed_reference);
  4278.  
  4279.       if (*arg3)
  4280.     {
  4281.       if (!*arg2)
  4282.         execute_string ("%s: %s", arg3, arg1);
  4283.       else
  4284.         execute_string ("%s: %s", arg2, arg1);
  4285.       return;
  4286.     }
  4287.  
  4288.       if (*arg2)
  4289.     execute_string ("%s: %s", arg2, arg1);
  4290.       else
  4291.     execute_string ("%s::", arg1);
  4292.     }
  4293.   else
  4294.     {
  4295.  
  4296.       /* Check to make sure that the next non-whitespace character is either
  4297.          a period or a comma. input_text_offset is pointing at the "}" which
  4298.          ended the xref or pxref command. */
  4299.       int temp = input_text_offset + 1;
  4300.  
  4301.       if (output_paragraph[output_paragraph_offset - 2] == ':' &&
  4302.       output_paragraph[output_paragraph_offset - 1] == ':')
  4303.     return;
  4304.       while (temp < size_of_input_text)
  4305.     {
  4306.       if (cr_or_whitespace (input_text[temp]))
  4307.         temp++;
  4308.       else
  4309.         {
  4310.           if (input_text[temp] == '.' ||
  4311.           input_text[temp] == ',' ||
  4312.           input_text[temp] == '\t')
  4313.         return;
  4314.           else
  4315.         {
  4316.           line_error ("Cross-reference must be terminated with a period or a comma");
  4317.           return;
  4318.         }
  4319.         }
  4320.     }
  4321.     }
  4322. }
  4323.  
  4324. cm_pxref (arg)
  4325.      int arg;
  4326. {
  4327.   if (arg == START)
  4328.     {
  4329.       px_ref_flag++;
  4330.       cm_xref (arg);
  4331.       px_ref_flag--;
  4332.     }
  4333.   else
  4334.     add_char ('.');
  4335. }
  4336.  
  4337. cm_inforef (arg)
  4338.      int arg;
  4339. {
  4340.   if (arg == START)
  4341.     {
  4342.       char *node, *pname, *file;
  4343.  
  4344.       node = get_xref_token ();
  4345.       pname = get_xref_token ();
  4346.       file = get_xref_token ();
  4347.  
  4348.       execute_string ("*note %s: (%s)%s", pname, file, node);
  4349.     }
  4350. }
  4351.  
  4352. /* **************************************************************** */
  4353. /*                                    */
  4354. /*            Insertion Command Stubs                */
  4355. /*                                    */
  4356. /* **************************************************************** */
  4357.  
  4358. cm_quotation ()
  4359. {
  4360.   begin_insertion (quotation);
  4361. }
  4362.  
  4363. cm_example ()
  4364. {
  4365.   begin_insertion (example);
  4366. }
  4367.  
  4368. cm_smallexample ()
  4369. {
  4370.   begin_insertion (smallexample);
  4371. }
  4372.  
  4373. cm_lisp ()
  4374. {
  4375.   begin_insertion (lisp);
  4376. }
  4377.  
  4378. cm_smalllisp ()
  4379. {
  4380.   begin_insertion (smalllisp);
  4381. }
  4382.  
  4383. /* @cartouche/@end cartouche draws box with rounded corners in
  4384.    TeX output.  Right now, just a NOP insertion. */
  4385. cm_cartouche ()
  4386. {
  4387.   begin_insertion (cartouche);
  4388. }
  4389.  
  4390. cm_format ()
  4391. {
  4392.   begin_insertion (format);
  4393. }
  4394.  
  4395. cm_display ()
  4396. {
  4397.   begin_insertion (display);
  4398. }
  4399.  
  4400. cm_itemize ()
  4401. {
  4402.   begin_insertion (itemize);
  4403. }
  4404.  
  4405. cm_enumerate ()
  4406. {
  4407.   begin_insertion (enumerate);
  4408. }
  4409.  
  4410. cm_alphaenumerate ()
  4411. {
  4412.   begin_insertion (alphaenumerate);
  4413. }
  4414.  
  4415. cm_capsenumerate ()
  4416. {
  4417.   begin_insertion (capsenumerate);
  4418. }
  4419.  
  4420. cm_table ()
  4421. {
  4422.   begin_insertion (table);
  4423. }
  4424.  
  4425. cm_ftable ()
  4426. {
  4427.   begin_insertion (ftable);
  4428. }
  4429.  
  4430. cm_group ()
  4431. {
  4432.   begin_insertion (group);
  4433. }
  4434.  
  4435. cm_ifinfo ()
  4436. {
  4437.   begin_insertion (ifinfo);
  4438. }
  4439.  
  4440. /* Begin an insertion where the lines are not filled or indented. */
  4441. cm_flushleft ()
  4442. {
  4443.   begin_insertion (flushleft);
  4444. }
  4445.  
  4446. /* Begin an insertion where the lines are not filled, and each line is
  4447.    forced to the right-hand side of the page. */
  4448. cm_flushright ()
  4449. {
  4450.   begin_insertion (flushright);
  4451. }
  4452.  
  4453.  
  4454. /* **************************************************************** */
  4455. /*                                    */
  4456. /*              Conditional Handling                */
  4457. /*                                    */
  4458. /* **************************************************************** */
  4459.  
  4460. /* A structure which contains `defined' variables. */
  4461. typedef struct _defines {
  4462.   struct _defines *next;
  4463.   char *name;
  4464. } DEFINE;
  4465.  
  4466. /* The linked list of `set' defines. */
  4467. DEFINE *defines = (DEFINE *)NULL;
  4468.  
  4469. /* Add NAME to the list of `set' defines. */
  4470. set (name)
  4471.      char *name;
  4472. {
  4473.   DEFINE *temp;
  4474.  
  4475.   for (temp = defines; temp; temp = temp->next)
  4476.     if (strcmp (name, temp->name) == 0)
  4477.       return;
  4478.  
  4479.   temp = (DEFINE *)xmalloc (sizeof (DEFINE));
  4480.   temp->next = defines;
  4481.   temp->name = savestring (name);
  4482.   defines = temp;
  4483. }
  4484.  
  4485. /* Remove NAME from the list of `set' defines. */
  4486. clear (name)
  4487.      char *name;
  4488. {
  4489.   register DEFINE *temp, *last;
  4490.  
  4491.   last = (DEFINE *)NULL;
  4492.   temp = defines;
  4493.  
  4494.   while (temp)
  4495.     {
  4496.       if (strcmp (temp->name, name) == 0)
  4497.     {
  4498.       if (last)
  4499.         last->next = temp->next;
  4500.       else
  4501.         defines = temp->next;
  4502.  
  4503.       free (temp->name);
  4504.       free (temp);
  4505.       break;
  4506.     }
  4507.       last = temp;
  4508.       temp = temp->next;
  4509.     }
  4510. }
  4511.  
  4512. /* Return non-zero if NAME is present in the list of `set' defines. */
  4513. set_p (name)
  4514.      char *name;
  4515. {
  4516.   register DEFINE *temp;
  4517.  
  4518.   for (temp = defines; temp; temp = temp->next)
  4519.     if (strcmp (temp->name, name) == 0)
  4520.       return (1);
  4521.  
  4522.   return (0);
  4523. }
  4524.  
  4525. /* Conditionally parse based on the current command name. */
  4526. command_name_condition ()
  4527. {
  4528.   char discarder[128];
  4529.  
  4530.   sprintf (discarder, "\n@end %s", command);
  4531.   discard_until (discarder);
  4532.   discard_until ("\n");
  4533. }
  4534.  
  4535. #define SET    1
  4536. #define CLEAR    2
  4537. #define IFSET    3
  4538. #define IFCLEAR    4
  4539.  
  4540. /* Create a variable whose name is the rest of the line. */
  4541. cm_set ()
  4542. {
  4543.   handle_variable (SET);
  4544. }
  4545.  
  4546. /* Remove a variable whose name is the rest of the line. */
  4547. cm_clear ()
  4548. {
  4549.   handle_variable (CLEAR);
  4550. }
  4551.  
  4552. cm_ifset ()
  4553. {
  4554.   handle_variable (IFSET);
  4555. }
  4556.  
  4557. cm_ifclear ()
  4558. {
  4559.   handle_variable (IFCLEAR);
  4560. }
  4561.  
  4562. /* Set, clear, or conditionalize based on ACTION. */
  4563. handle_variable (action)
  4564.      int action;
  4565. {
  4566.   char *name;
  4567.  
  4568.   get_rest_of_line (&name);
  4569.   backup_input_pointer ();
  4570.   canon_white (name);
  4571.  
  4572.   if (!*name)
  4573.     line_error ("@%s requires a name", command);
  4574.   else
  4575.     {
  4576.       switch (action)
  4577.     {
  4578.     case SET:
  4579.       set (name);
  4580.       break;
  4581.  
  4582.     case CLEAR:
  4583.       clear (name);
  4584.       break;
  4585.  
  4586.     case IFSET:
  4587.       if (!set_p (name))
  4588.         {
  4589.           discard_until ("\n@end ifset");
  4590.           discard_until ("\n");
  4591.         }
  4592.       else
  4593.         begin_insertion (ifset);
  4594.       break;
  4595.  
  4596.     case IFCLEAR:
  4597.       if (set_p (name))
  4598.         {
  4599.           discard_until ("\n@end ifclear");
  4600.           discard_until ("\n");
  4601.         }
  4602.       else
  4603.         begin_insertion (ifclear);
  4604.       break;
  4605.     }
  4606.     }
  4607.   free (name);
  4608. }
  4609.  
  4610. /* **************************************************************** */
  4611. /*                                    */
  4612. /*            @itemx, @item                    */
  4613. /*                                    */
  4614. /* **************************************************************** */
  4615.  
  4616. /* Non-zero means a string is in execution, as opposed to a file. */
  4617. int executing_string = 0;
  4618.  
  4619. /* Execute the string produced by formatting the ARGs with FORMAT.  This
  4620.    is like submitting a new file with @include. */
  4621. execute_string (format, arg1, arg2, arg3, arg4, arg5)
  4622.      char *format;
  4623. {
  4624.   static char temp_string[4000];
  4625.   sprintf (temp_string, format, arg1, arg2, arg3, arg4, arg5);
  4626.   strcat (temp_string, "@bye\n");
  4627.   pushfile ();
  4628.   input_text_offset = 0;
  4629.   input_text = temp_string;
  4630.   input_filename = savestring (input_filename);
  4631.   size_of_input_text = strlen (temp_string);
  4632.  
  4633.   executing_string++;
  4634.   reader_loop ();
  4635.  
  4636.   popfile ();
  4637.   executing_string--;
  4638.  
  4639.   free_and_clear (&command);
  4640.   command = savestring ("not bye");
  4641. }
  4642.  
  4643. int itemx_flag = 0;
  4644.  
  4645. cm_itemx ()
  4646. {
  4647.   itemx_flag++;
  4648.   cm_item ();
  4649.   itemx_flag--;
  4650. }
  4651.  
  4652. cm_item ()
  4653. {
  4654.   char *rest_of_line, *item_func;
  4655.  
  4656.   /* Can only hack "@item" while inside of an insertion. */
  4657.   if (insertion_level)
  4658.     {
  4659.       INSERTION_ELT *stack = insertion_stack;
  4660.  
  4661.       get_rest_of_line (&rest_of_line);
  4662.       canon_white (rest_of_line);
  4663.       item_func = current_item_function ();
  4664.  
  4665.       /* Okay, do the right thing depending on which insertion function
  4666.      is active. */
  4667.  
  4668.     switch_top:
  4669.       switch (stack->insertion)
  4670.     {
  4671.     case ifinfo:
  4672.     case ifset:
  4673.     case ifclear:
  4674.     case cartouche:
  4675.       stack = stack->next;
  4676.       if (!stack)
  4677.         goto no_insertion;
  4678.       else
  4679.         goto switch_top;
  4680.       break;
  4681.  
  4682.     case menu:
  4683.     case quotation:
  4684.     case example:
  4685.     case smallexample:
  4686.     case lisp:
  4687.     case format:
  4688.     case display:
  4689.     case group:
  4690.       line_error ("The `@%s' command is meaningless within a `@%s' block",
  4691.               command,
  4692.               insertion_type_pname (current_insertion_type ()));
  4693.       break;
  4694.  
  4695.     case itemize:
  4696.     case enumerate:
  4697.     case alphaenumerate:
  4698.     case capsenumerate:
  4699.       if (itemx_flag)
  4700.         {
  4701.           line_error ("@itemx is not meaningful inside of a `%s' block",
  4702.               insertion_type_pname (current_insertion_type ()));
  4703.         }
  4704.       else
  4705.         {
  4706.           start_paragraph ();
  4707.           kill_self_indent (-1);
  4708.           filling_enabled = indented_fill = true;
  4709.  
  4710.           if (current_insertion_type () == itemize)
  4711.         {
  4712.           indent (output_column = current_indent - 2);
  4713.  
  4714.           /* I need some way to determine whether this command
  4715.              takes braces or not.  I believe the user can type
  4716.              either "@bullet" or "@bullet{}".  Of course, they
  4717.              can also type "o" or "#" or whatever else they want. */
  4718.           if (item_func && *item_func)
  4719.             {
  4720.               if (*item_func == '@')
  4721.             if (item_func[strlen (item_func) - 1] != '}')
  4722.               execute_string ("%s{}", item_func);
  4723.             else
  4724.               execute_string ("%s", item_func);
  4725.               else
  4726.             execute_string ("%s", item_func);
  4727.             }
  4728.           insert (' ');
  4729.           output_column++;
  4730.         }
  4731.           else if (current_insertion_type () == enumerate)
  4732.         number_item ();
  4733.           else
  4734.         letter_item ();
  4735.  
  4736.           /* Special hack.  This makes close paragraph ignore you until
  4737.          the start_paragraph () function has been called. */
  4738.           must_start_paragraph = 1;
  4739.         }
  4740.       break;
  4741.  
  4742.     case table:
  4743.     case ftable:
  4744.       {
  4745.         /* Get rid of extra characters. */
  4746.         kill_self_indent (-1);
  4747.  
  4748.         /* close_paragraph () almost does what we want.  The problem
  4749.            is when paragraph_is_open, and last_char_was_newline, and
  4750.            the last newline has been turned into a space, because
  4751.            filling_enabled. I handle it here. */
  4752.         if (last_char_was_newline && filling_enabled && paragraph_is_open)
  4753.           insert ('\n');
  4754.         close_paragraph ();
  4755.  
  4756.         /* Indent on a new line, but back up one indentation level. */
  4757.         /* force existing indentation. */
  4758.         add_char ('i');
  4759.         output_paragraph_offset--;
  4760.         kill_self_indent (default_indentation_increment + 1);
  4761.  
  4762.         /* Add item's argument to the line. */
  4763.         filling_enabled = false;
  4764.         if (!item_func && !(*item_func))
  4765.           execute_string ("%s", rest_of_line);
  4766.         else
  4767.           execute_string ("%s{%s}", item_func, rest_of_line);
  4768.  
  4769.         if (current_insertion_type () == ftable)
  4770.           execute_string ("@findex %s\n", rest_of_line);
  4771.  
  4772.         /* Start a new line, and let start_paragraph ()
  4773.            do the indenting of it for you. */
  4774.         close_single_paragraph ();
  4775.         indented_fill = filling_enabled = true;
  4776.       }
  4777.     }
  4778.       free (rest_of_line);
  4779.     }
  4780.   else
  4781.     {
  4782.     no_insertion:
  4783.       line_error ("@%s found outside of an insertion block", command);
  4784.     }
  4785. }
  4786.  
  4787.  
  4788. /* **************************************************************** */
  4789. /*                                    */
  4790. /*            Defun and Friends                   */
  4791. /*                                    */
  4792. /* **************************************************************** */
  4793.  
  4794. #define DEFUN_SELF_DELIMITING(c)                    \
  4795.   (((c) == '(')                                \
  4796.    || ((c) == ')')                            \
  4797.    || ((c) == '[')                            \
  4798.    || ((c) == ']'))
  4799.  
  4800. struct token_accumulator
  4801. {
  4802.   unsigned int length;
  4803.   unsigned int index;
  4804.   char ** tokens;
  4805. };
  4806.  
  4807. void
  4808. initialize_token_accumulator (accumulator)
  4809.      struct token_accumulator * accumulator;
  4810. {
  4811.   (accumulator -> length) = 0;
  4812.   (accumulator -> index) = 0;
  4813.   (accumulator -> tokens) = NULL;
  4814. }
  4815.  
  4816. void
  4817. accumulate_token (accumulator, token)
  4818.      struct token_accumulator * accumulator;
  4819.      char * token;
  4820. {
  4821.   if ((accumulator -> index) >= (accumulator -> length))
  4822.     {
  4823.       (accumulator -> length) += 10;
  4824.       (accumulator -> tokens) =
  4825.     ((char **)
  4826.      (xrealloc ((accumulator -> tokens),
  4827.             ((accumulator -> length) * (sizeof (char *))))));
  4828.     }
  4829.   ((accumulator -> tokens) [accumulator -> index]) = token;
  4830.   (accumulator -> index) += 1;
  4831. }
  4832.  
  4833. char *
  4834. copy_substring (start, end)
  4835.      char * start;
  4836.      char * end;
  4837. {
  4838.   char * result = ((char *) (xmalloc ((end - start) + 1)));
  4839.   char * scan = start;
  4840.   char * scan_result = result;
  4841.   while (scan < end)
  4842.     (*scan_result++) = (*scan++);
  4843.   (*scan_result) = '\0';
  4844.   return (result);
  4845. }
  4846.  
  4847. /* Given `string' pointing at an open brace, skip forward and return a
  4848.    pointer to just past the matching close brace. */
  4849. int
  4850. scan_group_in_string (string_pointer)
  4851.      char ** string_pointer;
  4852. {
  4853.   register int c;
  4854.   register char * scan_string = ((*string_pointer) + 1);
  4855.   register unsigned int level = 1;
  4856.  
  4857.   while (1)
  4858.     {
  4859.       if (level == 0)
  4860.     {
  4861.       (*string_pointer) = scan_string;
  4862.       return (1);
  4863.     }
  4864.       c = (*scan_string++);
  4865.       if (c == '\0')
  4866.     {
  4867.       /* Tweak line_number to compensate for fact that
  4868.          we gobbled the whole line before coming here. */
  4869.       line_number -= 1;
  4870.       line_error ("Missing `}' in @def arg");
  4871.       line_number += 1;
  4872.       (*string_pointer) = (scan_string - 1);
  4873.       return (0);
  4874.     }
  4875.       if (c == '{')
  4876.     level += 1;
  4877.       if (c == '}')
  4878.     level -= 1;
  4879.     }
  4880. }
  4881.  
  4882. /* Return a list of tokens from the contents of `string'.
  4883.    Commands and brace-delimited groups count as single tokens.
  4884.    Contiguous whitespace characters are converted to a token
  4885.    consisting of a single space. */
  4886. char **
  4887. args_from_string (string)
  4888.      char *string;
  4889. {
  4890.   struct token_accumulator accumulator;
  4891.   register char * scan_string = string;
  4892.   char * token_start;
  4893.   char * token_end;
  4894.  
  4895.   initialize_token_accumulator (&accumulator);
  4896.   while ((*scan_string) != '\0')
  4897.     {
  4898.       /* Replace arbitrary whitespace by a single space. */
  4899.       if (whitespace (*scan_string))
  4900.     {
  4901.       scan_string += 1;
  4902.       while (whitespace (*scan_string))
  4903.         scan_string += 1;
  4904.       accumulate_token ((&accumulator), (savestring (" ")));
  4905.       continue;
  4906.     }
  4907.  
  4908.       /* Commands count as single tokens. */
  4909.       if ((*scan_string) == COMMAND_PREFIX)
  4910.     {
  4911.       token_start = scan_string;
  4912.       scan_string += 1;
  4913.       if (self_delimiting (*scan_string))
  4914.         scan_string += 1;
  4915.       else
  4916.         {
  4917.           register int c;
  4918.           while (1)
  4919.         {
  4920.           c = (*scan_string++);
  4921.            if ((c == '\0') || (c == '{') || (whitespace (c)))
  4922.             {
  4923.               scan_string -= 1;
  4924.               break;
  4925.             }
  4926.         }
  4927.  
  4928.           if ((*scan_string) == '{')
  4929.         {
  4930.           char *s = scan_string;
  4931.           (void) scan_group_in_string (&s);
  4932.           scan_string = s;
  4933.         }
  4934.         }
  4935.       token_end = scan_string;
  4936.     }
  4937.  
  4938.       /* Parentheses and brackets are self-delimiting. */
  4939.       else if (DEFUN_SELF_DELIMITING (*scan_string))
  4940.     {
  4941.       token_start = scan_string;
  4942.       scan_string += 1;
  4943.       token_end = scan_string;
  4944.     }
  4945.  
  4946.       /* Open brace introduces a group that is a single token. */
  4947.       else if ((*scan_string) == '{')
  4948.     {
  4949.       char * s = scan_string;
  4950.       int balanced = (scan_group_in_string (&s));
  4951.       token_start = (scan_string + 1);
  4952.       scan_string = s;
  4953.       token_end = (balanced ? (scan_string - 1) : scan_string);
  4954.     }
  4955.  
  4956.       /* Otherwise a token is delimited by whitespace, parentheses,
  4957.      brackets, or braces. */
  4958.       else
  4959.     {
  4960.       int allow_brace_gathering = 0;
  4961.       int brace_level = 0;
  4962.  
  4963.       token_start = scan_string;
  4964.  
  4965.       while (1)
  4966.         {
  4967.           register int c = (*scan_string++);
  4968.  
  4969.           if (!c ||
  4970.           (!brace_level &&
  4971.            (whitespace (c) ||
  4972.             DEFUN_SELF_DELIMITING (c) ||
  4973.             ((!allow_brace_gathering) && c == '{') ||
  4974.             (c == '}'))))
  4975.         {
  4976.           scan_string--;
  4977.           break;
  4978.         }
  4979.  
  4980.           /* If we encounter a command imbedded within a token,
  4981.          then that command (and it's associated braces) are
  4982.          part of the token that we gathering. */
  4983.           if (c == COMMAND_PREFIX && !self_delimiting (*scan_string))
  4984.         {
  4985.           allow_brace_gathering++;
  4986.           continue;
  4987.         }
  4988.  
  4989.           if (allow_brace_gathering)
  4990.         {
  4991.           if (c == '{')
  4992.             brace_level++;
  4993.           else
  4994.             if (c == '}')
  4995.               {
  4996.             if (--brace_level == 0)
  4997.               allow_brace_gathering = 0;
  4998.               }
  4999.         }
  5000.         }
  5001.       token_end = scan_string;
  5002.     }
  5003.  
  5004.       accumulate_token
  5005.     ((&accumulator), (copy_substring (token_start, token_end)));
  5006.     }
  5007.   accumulate_token ((&accumulator), NULL);
  5008.   return (accumulator . tokens);
  5009. }
  5010.  
  5011. void
  5012. process_defun_args (defun_args, auto_var_p)
  5013.      char ** defun_args;
  5014.      int auto_var_p;
  5015. {
  5016.   int pending_space = 0;
  5017.   while (1)
  5018.     {
  5019.       char * defun_arg = (*defun_args++);
  5020.       if (defun_arg == NULL)
  5021.     break;
  5022.       if ((defun_arg[0]) == ' ')
  5023.     {
  5024.       pending_space = 1;
  5025.       continue;
  5026.     }
  5027.       if (pending_space)
  5028.     {
  5029.       add_char (' ');
  5030.       pending_space = 0;
  5031.     }
  5032.       if (DEFUN_SELF_DELIMITING (defun_arg[0]))
  5033.     add_char (defun_arg[0]);
  5034.       else if ((defun_arg[0]) == '&')
  5035.     add_word (defun_arg);
  5036.       else if ((defun_arg[0]) == COMMAND_PREFIX)
  5037.     execute_string ("%s", defun_arg);
  5038.       else if (auto_var_p)
  5039.     execute_string ("@var{%s}", defun_arg);
  5040.       else
  5041.     add_word (defun_arg);
  5042.     }
  5043. }
  5044.  
  5045. char *
  5046. next_nonwhite_defun_arg (arg_pointer)
  5047.      char ***arg_pointer;
  5048. {
  5049.   char **scan = (*arg_pointer);
  5050.   char *arg = (*scan++);
  5051.  
  5052.   if ((arg != 0) && ((*arg) == ' '))
  5053.     arg = (*scan++);
  5054.  
  5055.   if (arg == 0)
  5056.     scan -= 1;
  5057.  
  5058.   (*arg_pointer) = scan;
  5059.  
  5060.   return ((arg == 0) ? "" : arg);
  5061. }
  5062.  
  5063. /* Make the defun type insertion.
  5064.    TYPE says which insertion this is.
  5065.    X_P says not to start a new insertion if non-zero. */
  5066. void
  5067. defun_internal (type, x_p)
  5068.      enum insertion_type type;
  5069.      int x_p;
  5070. {
  5071.   enum insertion_type base_type;
  5072.   char **defun_args, **scan_args;
  5073.   char *category, *defined_name, *type_name, *type_name2;
  5074.  
  5075.   {
  5076.     char *line;
  5077.     get_rest_of_line (&line);
  5078.     defun_args = (args_from_string (line));
  5079.     free (line);
  5080.   }
  5081.  
  5082.   scan_args = defun_args;
  5083.  
  5084.   switch (type)
  5085.     {
  5086.     case defun:
  5087.       category = "Function";
  5088.       base_type = deffn;
  5089.       break;
  5090.     case defmac:
  5091.       category = "Macro";
  5092.       base_type = deffn;
  5093.       break;
  5094.     case defspec:
  5095.       category = "Special Form";
  5096.       base_type = deffn;
  5097.       break;
  5098.     case defvar:
  5099.       category = "Variable";
  5100.       base_type = defvr;
  5101.       break;
  5102.     case defopt:
  5103.       category = "User Option";
  5104.       base_type = defvr;
  5105.       break;
  5106.     case deftypefun:
  5107.       category = "Function";
  5108.       base_type = deftypefn;
  5109.       break;
  5110.     case deftypevar:
  5111.       category = "Variable";
  5112.       base_type = deftypevr;
  5113.       break;
  5114.     case defivar:
  5115.       category = "Instance Variable";
  5116.       base_type = defcv;
  5117.       break;
  5118.     case defmethod:
  5119.       category = "Method";
  5120.       base_type = defop;
  5121.       break;
  5122.     case deftypemethod:
  5123.       category = "Method";
  5124.       base_type = deftypemethod;
  5125.       break;
  5126.     default:
  5127.       category = (next_nonwhite_defun_arg (&scan_args));
  5128.       base_type = type;
  5129.       break;
  5130.     }
  5131.  
  5132.   if ((base_type == deftypefn)
  5133.       || (base_type == deftypevr)
  5134.       || (base_type == defcv)
  5135.       || (base_type == defop)
  5136.       || (base_type == deftypemethod))
  5137.     type_name = next_nonwhite_defun_arg (&scan_args);
  5138.  
  5139.   if (base_type == deftypemethod)
  5140.     type_name2 = next_nonwhite_defun_arg (&scan_args);
  5141.  
  5142.   defined_name = next_nonwhite_defun_arg (&scan_args);
  5143.  
  5144.   /* This hack exists solely for the purposes of formatting the texinfo
  5145.      manual.  I couldn't think of a better way.  The token might be
  5146.      a simple @@ followed immediately by more text.  If this is the case,
  5147.      then the next defun arg is part of this one, and we should concatenate
  5148.      them. */
  5149.   if (*scan_args && **scan_args && !whitespace (**scan_args) &&
  5150.       (strcmp (defined_name, "@@") == 0))
  5151.     {
  5152.       char *tem = (char *)xmalloc (3 + strlen (scan_args[0]));
  5153.  
  5154.       sprintf (tem, "@@%s", scan_args[0]);
  5155.  
  5156.       free (scan_args[0]);
  5157.       scan_args[0] = tem;
  5158.       scan_args++;
  5159.       defined_name = tem;
  5160.     }
  5161.  
  5162.   if (!x_p)
  5163.     begin_insertion (type);
  5164.  
  5165.   /* Write the definition header line.
  5166.      This should start at the normal indentation.  */
  5167.   current_indent -= default_indentation_increment;
  5168.   start_paragraph ();
  5169.  
  5170.   switch (base_type)
  5171.     {
  5172.     case deffn:
  5173.     case defvr:
  5174.     case deftp:
  5175.       execute_string (" * %s: %s", category, defined_name);
  5176.       break;
  5177.     case deftypefn:
  5178.     case deftypevr:
  5179.       execute_string (" * %s: %s %s", category, type_name, defined_name);
  5180.       break;
  5181.     case defcv:
  5182.       execute_string (" * %s of %s: %s", category, type_name, defined_name);
  5183.       break;
  5184.     case defop:
  5185.       execute_string (" * %s on %s: %s", category, type_name, defined_name);
  5186.       break;
  5187.     case deftypemethod:
  5188.       execute_string (" * %s on %s: %s %s", category, type_name, type_name2,
  5189.               defined_name);
  5190.       break;
  5191.     }
  5192.   current_indent += default_indentation_increment;
  5193.  
  5194.   /* Now process the function arguments, if any.
  5195.      If these carry onto the next line, they should be indented by two
  5196.      increments to distinguish them from the body of the definition,
  5197.      which is indented by one increment.  */
  5198.   current_indent += default_indentation_increment;
  5199.  
  5200.   switch (base_type)
  5201.     {
  5202.     case deffn:
  5203.     case defop:
  5204.       process_defun_args (scan_args, 1);
  5205.       break;
  5206.     case deftp:
  5207.     case deftypefn:
  5208.     case deftypemethod:
  5209.       process_defun_args (scan_args, 0);
  5210.       break;
  5211.     }
  5212.   current_indent -= default_indentation_increment;
  5213.   close_single_paragraph ();
  5214.  
  5215.   /* Make an entry in the appropriate index. */
  5216.   switch (base_type)
  5217.     {
  5218.     case deffn:
  5219.     case deftypefn:
  5220.       execute_string ("@findex %s\n", defined_name);
  5221.       break;
  5222.     case defvr:
  5223.     case deftypevr:
  5224.     case defcv:
  5225.       execute_string ("@vindex %s\n", defined_name);
  5226.       break;
  5227.     case defop:
  5228.     case deftypemethod:
  5229.       execute_string ("@findex %s on %s\n", defined_name, type_name);
  5230.       break;
  5231.     case deftp:
  5232.       execute_string ("@tindex %s\n", defined_name);
  5233.       break;
  5234.     }
  5235.  
  5236.   /* Deallocate the token list. */
  5237.   scan_args = defun_args;
  5238.   while (1)
  5239.     {
  5240.       char * arg = (*scan_args++);
  5241.       if (arg == NULL)
  5242.     break;
  5243.       free (arg);
  5244.     }
  5245.   free (defun_args);
  5246. }
  5247.  
  5248. /* Add an entry for a function, macro, special form, variable, or option.
  5249.    If the name of the calling command ends in `x', then this is an extra
  5250.    entry included in the body of an insertion of the same type. */
  5251. cm_defun ()
  5252. {
  5253.   int x_p;
  5254.   enum insertion_type type;
  5255.   char *temp = savestring (command);
  5256.  
  5257.   x_p = (command[strlen (command) - 1] == 'x');
  5258.  
  5259.   if (x_p)
  5260.     temp[strlen (temp) - 1] = '\0';
  5261.  
  5262.   type = find_type_from_name (temp);
  5263.   free (temp);
  5264.  
  5265.   /* If we are adding to an already existing insertion, then make sure
  5266.      that we are already in an insertion of type TYPE. */
  5267.   if (x_p &&
  5268.       (!insertion_level || insertion_stack->insertion != type))
  5269.     {
  5270.       line_error ("Must be in a `%s' insertion in order to use `%s'x",
  5271.           command, command);
  5272.       discard_until ("\n");
  5273.       return;
  5274.     }
  5275.  
  5276.   defun_internal (type, x_p);
  5277. }
  5278.  
  5279. /* End existing insertion block. */
  5280. cm_end ()
  5281. {
  5282.   char *temp;
  5283.   enum insertion_type type;
  5284.  
  5285.   if (!insertion_level)
  5286.     {
  5287.       line_error ("Unmatched `@%s'", command);
  5288.       return;
  5289.     }
  5290.  
  5291.   get_rest_of_line (&temp);
  5292.   canon_white (temp);
  5293.  
  5294.   if (strlen (temp) == 0)
  5295.     line_error ("`@%s' needs something after it", command);
  5296.  
  5297.   type = find_type_from_name (temp);
  5298.  
  5299.   if (type == bad_type)
  5300.     {
  5301.       line_error ("Bad argument to `%s', `%s', using `%s'",
  5302.        command, temp, insertion_type_pname (current_insertion_type ()));
  5303.     }
  5304.   end_insertion (type);
  5305.   free (temp);
  5306. }
  5307.  
  5308.  
  5309. /* **************************************************************** */
  5310. /*                                    */
  5311. /*            Other Random Commands                   */
  5312. /*                                    */
  5313. /* **************************************************************** */
  5314.  
  5315. /* This says to inhibit the indentation of the next paragraph, but
  5316.    not of following paragraphs.  */
  5317. cm_noindent ()
  5318. {
  5319.   if (!inhibit_paragraph_indentation)
  5320.     inhibit_paragraph_indentation = -1;
  5321. }
  5322.  
  5323. /* I don't know exactly what to do with this.  Should I allow
  5324.    someone to switch filenames in the middle of output?  Since the
  5325.    file could be partially written, this doesn't seem to make sense.
  5326.    Another option: ignore it, since they don't *really* want to
  5327.    switch files.  Finally, complain, or at least warn. */
  5328. cm_setfilename ()
  5329. {
  5330.   char *filename;
  5331.   get_rest_of_line (&filename);
  5332.   /* warning ("`@%s %s' encountered and ignored", command, filename); */
  5333.   free (filename);
  5334. }
  5335.  
  5336. cm_comment ()
  5337. {
  5338.   discard_until ("\n");
  5339. }
  5340.  
  5341. /* @br can be immediately followed by `{}', so we have to read those here.
  5342.    It should simply close the paragraph. */
  5343. cm_br ()
  5344. {
  5345.   if (looking_at ("{}"))
  5346.     input_text_offset += 2;
  5347.  
  5348.   if (curchar () == '\n')
  5349.     {
  5350.       input_text_offset++;
  5351.       line_number++;
  5352.     }
  5353.  
  5354.   close_paragraph ();
  5355. }
  5356.  
  5357.  /* Insert the number of blank lines passed as argument. */
  5358. cm_sp ()
  5359. {
  5360.   int lines;
  5361.   char *line;
  5362.  
  5363.   get_rest_of_line (&line);
  5364.  
  5365.   sscanf (line, "%d", &lines);
  5366.   while (lines--)
  5367.     add_char ('\n');
  5368.   free (line);
  5369. }
  5370.  
  5371. cm_settitle ()
  5372. {
  5373.   discard_until ("\n");
  5374. }
  5375.  
  5376. cm_need ()
  5377. {
  5378.   discard_until ("\n");
  5379. }
  5380.  
  5381. cm_headings ()
  5382. {
  5383.   discard_until ("\n");
  5384. }
  5385.  
  5386. /* Start a new line with just this text on it.
  5387.    Then center the line of text.
  5388.    This always ends the current paragraph. */
  5389. cm_center ()
  5390. {
  5391.   char *line;
  5392.  
  5393.   close_paragraph ();
  5394.   filling_enabled = indented_fill = false;
  5395.  
  5396.   get_rest_of_line (&line);
  5397.  
  5398.   if (strlen (line) < fill_column)
  5399.     {
  5400.       int i = (fill_column - strlen (line)) / 2;
  5401.       while (i--)
  5402.     insert (' ');
  5403.     }
  5404.   execute_string (line);
  5405.   free (line);
  5406.   insert ('\n');
  5407.   close_paragraph ();
  5408.   filling_enabled = true;
  5409. }
  5410.  
  5411. /* Show what an expression returns. */
  5412. cm_result (arg)
  5413.      int arg;
  5414. {
  5415.   if (arg == END)
  5416.     add_word ("=>");
  5417. }
  5418.  
  5419. /* What an expression expands to. */
  5420. cm_expansion (arg)
  5421.      int arg;
  5422. {
  5423.   if (arg == END)
  5424.     add_word ("==>");
  5425. }
  5426.  
  5427. /* Indicates two expressions are equivalent. */
  5428. cm_equiv (arg)
  5429.      int arg;
  5430. {
  5431.   if (arg == END)
  5432.     add_word ("==");
  5433. }
  5434.  
  5435. /* What an expression may print. */
  5436. cm_print (arg)
  5437.      int arg;
  5438. {
  5439.   if (arg == END)
  5440.     add_word ("-|");
  5441. }
  5442.  
  5443. /* An error signaled. */
  5444. cm_error (arg)
  5445.      int arg;
  5446. {
  5447.   if (arg == END)
  5448.     add_word ("error-->");
  5449. }
  5450.  
  5451. /* The location of point in an example of a buffer. */
  5452. cm_point (arg)
  5453.      int arg;
  5454. {
  5455.   if (arg == END)
  5456.     add_word ("-!-");
  5457. }
  5458.  
  5459. /* Start a new line with just this text on it.
  5460.    The text is outdented one level if possible. */
  5461. cm_exdent ()
  5462. {
  5463.   char *line;
  5464.   int i = current_indent;
  5465.  
  5466.   if (current_indent)
  5467.     current_indent -= default_indentation_increment;
  5468.  
  5469.   get_rest_of_line (&line);
  5470.   close_single_paragraph ();
  5471.   add_word_args ("%s", line);
  5472.   current_indent = i;
  5473.   free (line);
  5474.   close_single_paragraph ();
  5475. }
  5476.  
  5477. cm_include ()
  5478. {
  5479.   cm_infoinclude ();
  5480. }
  5481.  
  5482. /* Remember this file, and move onto the next. */
  5483. cm_infoinclude ()
  5484. {
  5485.   char *filename;
  5486.  
  5487.   close_paragraph ();
  5488.   get_rest_of_line (&filename);
  5489.   pushfile ();
  5490.  
  5491.   /* In verbose mode we print info about including another file. */
  5492.   if (verbose_mode)
  5493.     {
  5494.       register int i = 0;
  5495.       register FSTACK *stack = filestack;
  5496.  
  5497.       for (i = 0, stack = filestack; stack; stack = stack->next, i++);
  5498.  
  5499.       i *= 2;
  5500.  
  5501.       printf ("%*s", i, "");
  5502.       printf ("%c%s %s\n", COMMAND_PREFIX, command, filename);
  5503.       fflush (stdout);
  5504.     }
  5505.  
  5506.   if (!find_and_load (filename))
  5507.     {
  5508.       extern char *sys_errlist[];
  5509.       extern int errno, sys_nerr;
  5510.       popfile ();
  5511.  
  5512.       /* Cannot "@include foo", in line 5 of "/wh/bar". */
  5513.       line_error ("`%c%s %s': %s", COMMAND_PREFIX, command, filename,
  5514.           ((errno < sys_nerr) ?
  5515.            sys_errlist[errno] : "Unknown file system error"));
  5516.     }
  5517.   free (filename);
  5518. }
  5519.  
  5520. /* The other side of a malformed expression. */
  5521. misplaced_brace ()
  5522. {
  5523.   line_error ("Misplaced `}'");
  5524. }
  5525.  
  5526. /* Don't let the filling algorithm insert extra whitespace here. */
  5527. cm_force_abbreviated_whitespace ()
  5528. {
  5529. }
  5530.  
  5531. /* Make the output paragraph end the sentence here, even though it
  5532.    looks like it shouldn't.  This also inserts the character which
  5533.    invoked it. */
  5534. cm_force_sentence_end ()
  5535. {
  5536.   add_char (META ((*command)));
  5537. }
  5538.  
  5539. /* Signals end of processing.  Easy to make this happen. */
  5540. cm_bye ()
  5541. {
  5542.   input_text_offset = size_of_input_text;
  5543. }
  5544.  
  5545. cm_asis ()
  5546. {
  5547. }
  5548.  
  5549. cm_math ()
  5550. {
  5551. }
  5552.  
  5553. cm_setchapternewpage ()
  5554. {
  5555.   discard_until ("\n");
  5556. }
  5557.  
  5558. cm_smallbook ()
  5559. {
  5560.   discard_until ("\n");
  5561. }
  5562.  
  5563. /* **************************************************************** */
  5564. /*                                    */
  5565. /*            Indexing Stuff                    */
  5566. /*                                    */
  5567. /* **************************************************************** */
  5568.  
  5569.  
  5570. /* An index element... */
  5571. typedef struct index_elt
  5572. {
  5573.   struct index_elt *next;
  5574.   char *entry;            /* The index entry itself. */
  5575.   char *node;            /* The node from whence it came. */
  5576.   int code;            /* Non-zero means add `@code{...}' when
  5577.                    printing this element. */
  5578.   int defining_line;        /* Line number where this entry was written. */
  5579. } INDEX_ELT;
  5580.  
  5581. /* A list of short-names for each index, and the index to that index in our
  5582.    index array, the_indices.  In addition, for each index, it is remembered
  5583.    whether that index is a code index or not.  Code indices have @code{}
  5584.    inserted around the first word when they are printed with printindex. */
  5585. typedef struct
  5586. {
  5587.   char *name;
  5588.   int index;
  5589.   int code;
  5590. } INDEX_ALIST;
  5591.  
  5592. INDEX_ALIST **name_index_alist = (INDEX_ALIST **) NULL;
  5593.  
  5594. /* An array of pointers.  Each one is for a different index.  The
  5595.    "synindex" command changes which array slot is pointed to by a
  5596.    given "index". */
  5597. INDEX_ELT **the_indices = (INDEX_ELT **) NULL;
  5598.  
  5599. /* The number of defined indices. */
  5600. int defined_indices = 0;
  5601.  
  5602. /* We predefine these. */
  5603. #define program_index 0
  5604. #define function_index 1
  5605. #define concept_index 2
  5606. #define variable_index 3
  5607. #define datatype_index 4
  5608. #define key_index 5
  5609.  
  5610. init_indices ()
  5611. {
  5612.   int i;
  5613.  
  5614.   /* Create the default data structures. */
  5615.  
  5616.   /* Initialize data space. */
  5617.   if (!the_indices)
  5618.     {
  5619.       the_indices = (INDEX_ELT **) xmalloc ((1 + defined_indices) *
  5620.                         sizeof (INDEX_ELT *));
  5621.       the_indices[defined_indices] = (INDEX_ELT *) NULL;
  5622.  
  5623.       name_index_alist = (INDEX_ALIST **) xmalloc ((1 + defined_indices) *
  5624.                            sizeof (INDEX_ALIST *));
  5625.       name_index_alist[defined_indices] = (INDEX_ALIST *) NULL;
  5626.     }
  5627.  
  5628.   /* If there were existing indices, get rid of them now. */
  5629.   for (i = 0; i < defined_indices; i++)
  5630.     undefindex (name_index_alist[i]->name);
  5631.  
  5632.   /* Add the default indices. */
  5633.   defindex ("pg", 0);
  5634.   defindex ("fn", 1);        /* "fn" is a code index.  */
  5635.   defindex ("cp", 0);
  5636.   defindex ("vr", 0);
  5637.   defindex ("tp", 0);
  5638.   defindex ("ky", 0);
  5639.  
  5640. }
  5641.  
  5642. /* Find which element in the known list of indices has this name.
  5643.    Returns -1 if NAME isn't found. */
  5644. int
  5645. find_index_offset (name)
  5646.      char *name;
  5647. {
  5648.   register int i;
  5649.   for (i = 0; i < defined_indices; i++)
  5650.     if (name_index_alist[i] &&
  5651.     stricmp (name, name_index_alist[i]->name) == 0)
  5652.       return (name_index_alist[i]->index);
  5653.   return (-1);
  5654. }
  5655.  
  5656. /* Return a pointer to the entry of (name . index) for this name.
  5657.    Return NULL if the index doesn't exist. */
  5658. INDEX_ALIST *
  5659. find_index (name)
  5660.      char *name;
  5661. {
  5662.   int offset = find_index_offset (name);
  5663.   if (offset > -1)
  5664.     return (name_index_alist[offset]);
  5665.   else
  5666.     return ((INDEX_ALIST *) NULL);
  5667. }
  5668.  
  5669. /* Given an index name, return the offset in the_indices of this index,
  5670.    or -1 if there is no such index. */
  5671. translate_index (name)
  5672.      char *name;
  5673. {
  5674.   INDEX_ALIST *which = find_index (name);
  5675.  
  5676.   if (which)
  5677.     return (which->index);
  5678.   else
  5679.     return (-1);
  5680. }
  5681.  
  5682. /* Return the index list which belongs to NAME. */
  5683. INDEX_ELT *
  5684. index_list (name)
  5685.      char *name;
  5686. {
  5687.   int which = translate_index (name);
  5688.   if (which < 0)
  5689.     return ((INDEX_ELT *) - 1);
  5690.   else
  5691.     return (the_indices[which]);
  5692. }
  5693.  
  5694. /* Please release me, let me go... */
  5695. free_index (index)
  5696.      INDEX_ELT *index;
  5697. {
  5698.   INDEX_ELT *temp;
  5699.  
  5700.   while ((temp = index) != (INDEX_ELT *) NULL)
  5701.     {
  5702.       free (temp->entry);
  5703.       free (temp->node);
  5704.       index = index->next;
  5705.       free (temp);
  5706.     }
  5707. }
  5708.  
  5709. /* Flush an index by name. */
  5710. undefindex (name)
  5711.      char *name;
  5712. {
  5713.   int i;
  5714.   int which = find_index_offset (name);
  5715.  
  5716.   if (which < 0)
  5717.     return (which);
  5718.  
  5719.   i = name_index_alist[which]->index;
  5720.  
  5721.  
  5722.   free_index (the_indices[i]);
  5723.   the_indices[i] = (INDEX_ELT *) NULL;
  5724.  
  5725.   free (name_index_alist[which]->name);
  5726.   free (name_index_alist[which]);
  5727.   name_index_alist[which] = (INDEX_ALIST *) NULL;
  5728. }
  5729.  
  5730. /* Define an index known as NAME.  We assign the slot number.
  5731.    CODE if non-zero says to make this a code index. */
  5732. defindex (name, code)
  5733.      char *name;
  5734.      int code;
  5735. {
  5736.   register int i, slot;
  5737.  
  5738.   /* If it already exists, flush it. */
  5739.   undefindex (name);
  5740.  
  5741.   /* Try to find an empty slot. */
  5742.   slot = -1;
  5743.   for (i = 0; i < defined_indices; i++)
  5744.     if (!name_index_alist[i])
  5745.       {
  5746.     slot = i;
  5747.     break;
  5748.       }
  5749.  
  5750.   if (slot < 0)
  5751.     {
  5752.       /* No such luck.  Make space for another index. */
  5753.       slot = defined_indices;
  5754.       defined_indices++;
  5755.  
  5756.       name_index_alist = (INDEX_ALIST **)
  5757.     xrealloc ((char *)name_index_alist,
  5758.           (1 + defined_indices) * sizeof (INDEX_ALIST *));
  5759.       the_indices = (INDEX_ELT **)
  5760.     xrealloc ((char *)the_indices,
  5761.           (1 + defined_indices) * sizeof (INDEX_ELT *));
  5762.     }
  5763.  
  5764.   /* We have a slot.  Start assigning. */
  5765.   name_index_alist[slot] = (INDEX_ALIST *) xmalloc (sizeof (INDEX_ALIST));
  5766.   name_index_alist[slot]->name = savestring (name);
  5767.   name_index_alist[slot]->index = slot;
  5768.   name_index_alist[slot]->code = code;
  5769.  
  5770.   the_indices[slot] = (INDEX_ELT *) NULL;
  5771. }
  5772.  
  5773. /* Add the arguments to the current index command to the index NAME. */
  5774. index_add_arg (name)
  5775.      char *name;
  5776. {
  5777.   int which;
  5778.   char *index_entry;
  5779.   INDEX_ALIST *tem;
  5780.  
  5781.   tem = find_index (name);
  5782.  
  5783.   which = tem ? tem->index : -1;
  5784.  
  5785.   get_rest_of_line (&index_entry);
  5786.   ignore_blank_line ();
  5787.  
  5788.   if (which < 0)
  5789.     {
  5790.       line_error ("Unknown index reference `%s'", name);
  5791.       free (index_entry);
  5792.     }
  5793.   else
  5794.     {
  5795.       INDEX_ELT *new = (INDEX_ELT *) xmalloc (sizeof (INDEX_ELT));
  5796.       new->next = the_indices[which];
  5797.       new->entry = index_entry;
  5798.       new->node = current_node;
  5799.       new->code = tem->code;
  5800.       new->defining_line = line_number - 1;
  5801.       the_indices[which] = new;
  5802.     }
  5803. }
  5804.  
  5805. #define INDEX_COMMAND_SUFFIX "index"
  5806.  
  5807. /* The function which user defined index commands call. */
  5808. gen_index ()
  5809. {
  5810.   char *name = savestring (command);
  5811.   if (strlen (name) >= strlen ("index"))
  5812.     name[strlen (name) - strlen ("index")] = '\0';
  5813.   index_add_arg (name);
  5814.   free (name);
  5815. }
  5816.  
  5817. /* Define a new index command.  Arg is name of index. */
  5818. cm_defindex ()
  5819. {
  5820.   gen_defindex (0);
  5821. }
  5822.  
  5823. cm_defcodeindex ()
  5824. {
  5825.   gen_defindex (1);
  5826. }
  5827.  
  5828. gen_defindex (code)
  5829.      int code;
  5830. {
  5831.   char *name;
  5832.   get_rest_of_line (&name);
  5833.  
  5834.   if (find_index (name))
  5835.     {
  5836.       line_error ("Index `%s' already exists", name);
  5837.       free (name);
  5838.       return;
  5839.     }
  5840.   else
  5841.     {
  5842.       char *temp = (char *) alloca (1 + strlen (name) + strlen ("index"));
  5843.       sprintf (temp, "%sindex", name);
  5844.       define_user_command (temp, gen_index, 0);
  5845.       defindex (name, code);
  5846.       free (name);
  5847.     }
  5848. }
  5849.  
  5850. /* Append LIST2 to LIST1.  Return the head of the list. */
  5851. INDEX_ELT *
  5852. index_append (head, tail)
  5853.      INDEX_ELT *head, *tail;
  5854. {
  5855.   register INDEX_ELT *t_head = head;
  5856.  
  5857.   if (!t_head)
  5858.     return (tail);
  5859.  
  5860.   while (t_head->next)
  5861.     t_head = t_head->next;
  5862.   t_head->next = tail;
  5863.   return (head);
  5864. }
  5865.  
  5866. /* Expects 2 args, on the same line.  Both are index abbreviations.
  5867.    Make the first one be a synonym for the second one, i.e. make the
  5868.    first one have the same index as the second one. */
  5869. cm_synindex ()
  5870. {
  5871.   int redirector, redirectee;
  5872.   char *temp;
  5873.  
  5874.   skip_whitespace ();
  5875.   get_until_in_line (" ", &temp);
  5876.   redirectee = find_index_offset (temp);
  5877.   skip_whitespace ();
  5878.   free_and_clear (&temp);
  5879.   get_until_in_line (" ", &temp);
  5880.   redirector = find_index_offset (temp);
  5881.   free (temp);
  5882.   if (redirector < 0 || redirectee < 0)
  5883.     {
  5884.       line_error ("Unknown index reference");
  5885.     }
  5886.   else
  5887.     {
  5888.       /* I think that we should let the user make indices synonymous to
  5889.          each other without any lossage of info.  This means that one can
  5890.          say @synindex cp dt anywhere in the file, and things that used to
  5891.          be in cp will go into dt. */
  5892.       INDEX_ELT *i1 = the_indices[redirectee], *i2 = the_indices[redirector];
  5893.  
  5894.       if (i1 || i2)
  5895.     {
  5896.       if (i1)
  5897.         the_indices[redirectee] = index_append (i1, i2);
  5898.       else
  5899.         the_indices[redirectee] = index_append (i2, i1);
  5900.     }
  5901.  
  5902.       name_index_alist[redirectee]->index =
  5903.     name_index_alist[redirector]->index;
  5904.     }
  5905. }
  5906.  
  5907. cm_pindex ()            /* Pinhead index. */
  5908. {
  5909.   index_add_arg ("pg");
  5910. }
  5911.  
  5912. cm_vindex ()            /* Variable index. */
  5913. {
  5914.   index_add_arg ("vr");
  5915. }
  5916.  
  5917. cm_kindex ()            /* Key index. */
  5918. {
  5919.   index_add_arg ("ky");
  5920. }
  5921.  
  5922. cm_cindex ()            /* Concept index. */
  5923. {
  5924.   index_add_arg ("cp");
  5925. }
  5926.  
  5927. cm_findex ()            /* Function index. */
  5928. {
  5929.   index_add_arg ("fn");
  5930. }
  5931.  
  5932. cm_tindex ()            /* Data Type index. */
  5933. {
  5934.   index_add_arg ("tp");
  5935. }
  5936.  
  5937. /* Sorting the index. */
  5938. index_element_compare (element1, element2)
  5939.      INDEX_ELT **element1, **element2;
  5940. {
  5941.   /* This needs to ignore leading non-text characters. */
  5942.   return (strcmp ((*element1)->entry, (*element2)->entry));
  5943. }
  5944.  
  5945. /* Sort the index passed in INDEX, returning an array of
  5946.    pointers to elements.  The array is terminated with a NULL
  5947.    pointer.  We call qsort because it's supposed to be fast.
  5948.    I think this looks bad. */
  5949. INDEX_ELT **
  5950. sort_index (index)
  5951.      INDEX_ELT *index;
  5952. {
  5953.   INDEX_ELT *temp = index;
  5954.   INDEX_ELT **array;
  5955.   int count = 0;
  5956.  
  5957.   while (temp != (INDEX_ELT *) NULL)
  5958.     {
  5959.       count++;
  5960.       temp = temp->next;
  5961.     }
  5962.  
  5963.   /* We have the length.  Make an array. */
  5964.  
  5965.   array = (INDEX_ELT **) xmalloc ((count + 1) * sizeof (INDEX_ELT *));
  5966.   count = 0;
  5967.   temp = index;
  5968.  
  5969.   while (temp != (INDEX_ELT *) NULL)
  5970.     {
  5971.       array[count++] = temp;
  5972.       temp = temp->next;
  5973.     }
  5974.   array[count] = (INDEX_ELT *) NULL;    /* terminate the array. */
  5975.  
  5976.   /* Sort the array. */
  5977.   qsort (array, count, sizeof (INDEX_ELT *), index_element_compare);
  5978.  
  5979.   return (array);
  5980. }
  5981.  
  5982. /* Non-zero means that we are in the middle of printing an index. */
  5983. int printing_index = 0;
  5984.  
  5985. /* Takes one arg, a short name of an index to print.
  5986.    Outputs a menu of the sorted elements of the index. */
  5987. cm_printindex ()
  5988. {
  5989.   int item;
  5990.   INDEX_ELT *index;
  5991.   INDEX_ELT **array;
  5992.   char *index_name;
  5993.   int old_inhibitions = inhibit_paragraph_indentation;
  5994.   boolean previous_filling_enabled_value = filling_enabled;
  5995.  
  5996.   close_paragraph ();
  5997.   get_rest_of_line (&index_name);
  5998.  
  5999.   index = index_list (index_name);
  6000.   if ((int) index < 0)
  6001.     {
  6002.       line_error ("Unknown index name `%s'", index_name);
  6003.       free (index_name);
  6004.       return;
  6005.     }
  6006.   else
  6007.     free (index_name);
  6008.  
  6009.   array = sort_index (index);
  6010.  
  6011.   filling_enabled = false;
  6012.   inhibit_paragraph_indentation = 1;
  6013.   close_paragraph ();
  6014.   add_word ("* Menu:\n\n");
  6015.  
  6016.   printing_index = 1;
  6017.   for (item = 0; (index = array[item]); item++)
  6018.     {
  6019.       int real_line_number = line_number;
  6020.  
  6021.       /* Let errors generated while making the index entry point back
  6022.      at the line which contains the entry. */
  6023.       line_number = index->defining_line;
  6024.  
  6025.       /* If this particular entry should be printed as a "code" index,
  6026.      then wrap the entry with "@code{...}". */
  6027.       if (index->code)
  6028.     execute_string ("* @code{%s}: ", index->entry);
  6029.       else
  6030.     execute_string ("* %s: ", index->entry);
  6031.  
  6032.       /* Pad the front of the destination nodename so that
  6033.      the output looks nice. */
  6034.       if (fill_column > 40 && output_column < 40)
  6035.     indent (40 - output_column);
  6036.  
  6037.       execute_string ("%s.\n", index->node);
  6038.  
  6039.       line_number = real_line_number;
  6040.       flush_output ();
  6041.     }
  6042.  
  6043.   printing_index = 0;
  6044.   free (array);
  6045.   close_single_paragraph ();
  6046.   filling_enabled = previous_filling_enabled_value;
  6047.   inhibit_paragraph_indentation = old_inhibitions;
  6048. }
  6049.  
  6050.  
  6051. /* **************************************************************** */
  6052. /*                                    */
  6053. /*            Making User Defined Commands            */
  6054. /*                                    */
  6055. /* **************************************************************** */
  6056.  
  6057. define_user_command (name, proc, needs_braces_p)
  6058.      char *name;
  6059.      FUNCTION *proc;
  6060.      int needs_braces_p;
  6061. {
  6062.   int slot = user_command_array_len;
  6063.   user_command_array_len++;
  6064.  
  6065.   if (!user_command_array)
  6066.     user_command_array = (COMMAND **) xmalloc (1 * sizeof (COMMAND *));
  6067.  
  6068.   user_command_array = (COMMAND **) xrealloc (user_command_array,
  6069.                           (1 + user_command_array_len) *
  6070.                           sizeof (COMMAND *));
  6071.  
  6072.   user_command_array[slot] = (COMMAND *) xmalloc (sizeof (COMMAND));
  6073.   user_command_array[slot]->name = savestring (name);
  6074.   user_command_array[slot]->proc = proc;
  6075.   user_command_array[slot]->argument_in_braces = needs_braces_p;
  6076. }
  6077.  
  6078. /* Make ALIAS run the named FUNCTION.  Copies properties from FUNCTION. */
  6079. define_alias (alias, function)
  6080.      char *alias, *function;
  6081. {
  6082. }
  6083.  
  6084. /* Set the paragraph indentation variable to the value specified in STRING.
  6085.    Values can be:
  6086.    `asis': Don't change existing indentation.
  6087.    `none': Remove existing indentation.
  6088.       NUM: Indent NUM spaces at the starts of paragraphs.
  6089.            Note that if NUM is zero, we assume `none'.
  6090.  
  6091.    Returns 0 if successful, or non-zero if STRING isn't one of the above. */
  6092. int
  6093. set_paragraph_indent (string)
  6094.      char *string;
  6095. {
  6096.   if (strcmp (string, "asis") == 0)
  6097.     paragraph_start_indent = 0;
  6098.   else if (strcmp (string, "none") == 0)
  6099.     paragraph_start_indent = -1;
  6100.   else
  6101.     {
  6102.       if (sscanf (string, "%d", ¶graph_start_indent) != 1)
  6103.     return (-1);
  6104.       else
  6105.     {
  6106.       if (paragraph_start_indent == 0)
  6107.         paragraph_start_indent = -1;
  6108.     }
  6109.     }
  6110.   return (0);
  6111. }
  6112.  
  6113. cm_paragraphindent ()
  6114. {
  6115.   char *arg;
  6116.  
  6117.   get_rest_of_line (&arg);
  6118.   if (set_paragraph_indent (arg) != 0)
  6119.     line_error ("Bad argument to @paragraphindent");
  6120.  
  6121.   free (arg);
  6122. }
  6123.  
  6124. /* Some support for footnotes. */
  6125.  
  6126. /* Footnotes are a new construct in Info.  We don't know the best method
  6127.    of implementing them for sure, so we present two possiblities.
  6128.  
  6129.    SeparateNode:
  6130.     Make them look like followed references, with the reference
  6131.     destinations in a makeinfo manufactured node or,
  6132.  
  6133.    EndNode:
  6134.     Make them appear at the bottom of the node that they originally
  6135.     appeared in. */
  6136. #define SeparateNode 0
  6137. #define EndNode 1
  6138.  
  6139. int footnote_style = EndNode;
  6140. boolean first_footnote_this_node = true;
  6141. int footnote_count = 0;
  6142.  
  6143. /* Set the footnote style based on he style identifier in STRING. */
  6144. int
  6145. set_footnote_style (string)
  6146.      char *string;
  6147. {
  6148.   if ((stricmp (string, "separate") == 0) ||
  6149.       (stricmp (string, "MN") == 0))
  6150.     footnote_style = SeparateNode;
  6151.   else if ((stricmp (string, "end") == 0) ||
  6152.        (stricmp (string, "EN") == 0))
  6153.     footnote_style = EndNode;
  6154.   else
  6155.     return (-1);
  6156.  
  6157.  return (0);
  6158. }
  6159.  
  6160. cm_footnotestyle ()
  6161. {
  6162.   char *arg;
  6163.  
  6164.   get_rest_of_line (&arg);
  6165.  
  6166.   if (set_footnote_style (arg) != 0)
  6167.     line_error ("Bad argument to @footnotestyle");
  6168.  
  6169.   free (arg);
  6170. }
  6171.  
  6172. typedef struct fn
  6173. {
  6174.   struct fn *next;
  6175.   char *marker;
  6176.   char *note;
  6177. }  FN;
  6178.  
  6179. FN *pending_notes = (FN *) NULL;
  6180.  
  6181. /* A method for remembering footnotes.  Note that this list gets output
  6182.    at the end of the current node. */
  6183. remember_note (marker, note)
  6184.      char *marker, *note;
  6185. {
  6186.   FN *temp = (FN *) xmalloc (sizeof (FN));
  6187.  
  6188.   temp->marker = savestring (marker);
  6189.   temp->note = savestring (note);
  6190.   temp->next = pending_notes;
  6191.   pending_notes = temp;
  6192.   footnote_count++;
  6193. }
  6194.  
  6195. /* How to get rid of existing footnotes. */
  6196. free_pending_notes ()
  6197. {
  6198.   FN *temp;
  6199.  
  6200.   while ((temp = pending_notes) != (FN *) NULL)
  6201.     {
  6202.       free (temp->marker);
  6203.       free (temp->note);
  6204.       pending_notes = pending_notes->next;
  6205.       free (temp);
  6206.     }
  6207.   first_footnote_this_node = true;
  6208.   footnote_count = 0;
  6209. }
  6210.  
  6211. /* What to do when you see a @footnote construct. */
  6212.  
  6213.  /* Handle a "footnote".
  6214.     footnote *{this is a footnote}
  6215.     where "*" is the marker character for this note. */
  6216. cm_footnote ()
  6217. {
  6218.   char *marker;
  6219.   char *note;
  6220.  
  6221.   get_until ("{", &marker);
  6222.   canon_white (marker);
  6223.  
  6224.   /* Read the argument in braces. */
  6225.   if (curchar () != '{')
  6226.     {
  6227.       line_error ("`@%s' expected more than just `%s'.  It needs something in `{...}'", command, marker);
  6228.       free (marker);
  6229.       return;
  6230.     }
  6231.   else
  6232.     {
  6233.       int braces = 1;
  6234.       int temp = ++input_text_offset;
  6235.       int len;
  6236.  
  6237.       while (braces)
  6238.     {
  6239.       if (temp == size_of_input_text)
  6240.         {
  6241.           line_error ("No closing brace for footnote `%s'", marker);
  6242.           return;
  6243.         }
  6244.  
  6245.       if (input_text[temp] == '{')
  6246.         braces++;
  6247.       else if (input_text[temp] == '}')
  6248.         braces--;
  6249.       else if (input_text[temp] == '\n')
  6250.         line_number ++;
  6251.  
  6252.       temp++;
  6253.     }
  6254.  
  6255.       len = (temp - input_text_offset) - 1;
  6256.       note = (char *)xmalloc (len + 1);
  6257.       strncpy (note, &input_text[input_text_offset], len);
  6258.       note[len] = '\0';
  6259.       input_text_offset = temp;
  6260.     }
  6261.  
  6262.   if (!current_node || !*current_node)
  6263.     {
  6264.       line_error ("Footnote defined without parent node");
  6265.       free (marker);
  6266.       free (note);
  6267.       return;
  6268.     }
  6269.  
  6270.   if (!*marker)
  6271.     {
  6272.       free (marker);
  6273.  
  6274.       if (number_footnotes)
  6275.     {
  6276.       marker = (char *)xmalloc (10);
  6277.       sprintf (marker, "%d", current_footnote_number);
  6278.       current_footnote_number++;
  6279.     }
  6280.       else
  6281.     marker = savestring ("*");
  6282.     }
  6283.  
  6284.   remember_note (marker, note);
  6285.  
  6286.   /* Your method should at least insert MARKER. */
  6287.   switch (footnote_style)
  6288.     {
  6289.     case SeparateNode:
  6290.       add_word_args ("(%s)", marker);
  6291.       if (first_footnote_this_node)
  6292.     {
  6293.       char *temp_string;
  6294.  
  6295.       temp_string = (char *)
  6296.         xmalloc ((strlen (current_node)) + (strlen ("-Footnotes")) + 1);
  6297.  
  6298.       add_word_args (" (*note %s-Footnotes::)", current_node);
  6299.       strcpy (temp_string, current_node);
  6300.       strcat (temp_string, "-Footnotes");
  6301.       remember_node_reference (temp_string, line_number, followed_reference);
  6302.       free (temp_string);
  6303.       first_footnote_this_node = false;
  6304.     }
  6305.       break;
  6306.  
  6307.     case EndNode:
  6308.       add_word_args ("(%s)", marker);
  6309.       break;
  6310.  
  6311.     default:
  6312.       break;
  6313.     }
  6314.   free (marker);
  6315.   free (note);
  6316. }
  6317.  
  6318. /* Non-zero means that we are currently in the process of outputting
  6319.    footnotes. */
  6320. int already_outputting_pending_notes = 0;
  6321.  
  6322. /* Output the footnotes.  We are at the end of the current node. */
  6323. output_pending_notes ()
  6324. {
  6325.   FN *footnote = pending_notes;
  6326.  
  6327.   if (!pending_notes)
  6328.     return;
  6329.  
  6330.   switch (footnote_style)
  6331.     {
  6332.  
  6333.     case SeparateNode:
  6334.       {
  6335.     char *old_current_node = current_node;
  6336.     char *old_command = savestring (command);
  6337.  
  6338.     already_outputting_pending_notes++;
  6339.     execute_string ("@node %s-Footnotes,,,%s\n", current_node, current_node);
  6340.     already_outputting_pending_notes--;
  6341.     current_node = old_current_node;
  6342.     free (command);
  6343.     command = old_command;
  6344.       }
  6345.       break;
  6346.  
  6347.     case EndNode:
  6348.       close_paragraph ();
  6349.       in_fixed_width_font++;
  6350.       execute_string ("---------- Footnotes ----------\n\n");
  6351.       in_fixed_width_font--;
  6352.       break;
  6353.     }
  6354.  
  6355.   /* Handle the footnotes in reverse order. */
  6356.   {
  6357.     FN **array = (FN **) xmalloc ((footnote_count + 1) * sizeof (FN *));
  6358.  
  6359.     array[footnote_count] = (FN *) NULL;
  6360.  
  6361.     while (--footnote_count > -1)
  6362.       {
  6363.     array[footnote_count] = footnote;
  6364.     footnote = footnote->next;
  6365.       }
  6366.  
  6367.     filling_enabled = true;
  6368.     indented_fill = true;
  6369.  
  6370.     while (footnote = array[++footnote_count])
  6371.       {
  6372.  
  6373.     switch (footnote_style)
  6374.       {
  6375.       case SeparateNode:
  6376.       case EndNode:
  6377.         execute_string ("(%s)  %s", footnote->marker, footnote->note);
  6378.         close_paragraph ();
  6379.         break;
  6380.       }
  6381.       }
  6382.     close_paragraph ();
  6383.     free (array);
  6384.   }
  6385. }
  6386.  
  6387. /*
  6388.  * Local variables:
  6389.  * compile-command: "gcc -g -Bstatic -o makeinfo makeinfo.c getopt.c"
  6390.  * end:
  6391.  */
  6392.  
  6393.