home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / makeinfo / src / makeinfo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-27  |  130.3 KB  |  5,601 lines

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