home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / tinfo210.lzh / TINFO210 / C / INFO.C < prev    next >
Text File  |  1991-06-11  |  100KB  |  4,326 lines

  1. /* info -- a stand-alone Info program
  2.  
  3.    Copyright (C) 1987, 1991 Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU Info.
  6.  
  7.    GNU Info 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.    GNU Info, 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. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <stdio.h>
  25. #include <signal.h>
  26. #include <pwd.h>
  27.  
  28. #include "getopt.h"
  29.  
  30. #if defined (USG)
  31. struct passwd *getpwnam ();
  32. #include <fcntl.h>
  33. #include <termio.h>
  34. #include <string.h>
  35. #define bcopy(source, dest, count) memcpy(dest, source, count)
  36. char *index(s,c) char *s; { char *strchr(); return strchr(s,c); }
  37. char *rindex(s,c) char *s; { char *strrchr(); return strrchr(s,c); }
  38. #else
  39. #include <sys/file.h>
  40. #include <sgtty.h>
  41. #include <strings.h>
  42. #endif
  43.  
  44. #ifndef DEFAULT_INFOPATH
  45. #define DEFAULT_INFOPATH ".:/usr/gnu/info:/usr/local/emacs/info:/usr/local/lib/emacs/info"
  46. #endif
  47.  
  48. typedef int boolean;
  49. #define true 1
  50. #define false 0
  51. #define UNIX
  52.  
  53. typedef struct nodeinfo {
  54.   char *filename;
  55.   char *nodename;
  56.   int pagetop;
  57.   int nodetop;
  58.   struct nodeinfo *next;
  59. } NODEINFO;
  60.  
  61. typedef struct indirectinfo {
  62.   char *filename;
  63.   int first_byte;
  64. } INDIRECT_INFO;
  65.  
  66. typedef int Function ();
  67.  
  68. #define PROJECT_NAME "GNU Info"
  69.  
  70. #define barf(msg) fprintf(stderr, "%s\n", msg)
  71.  
  72. /* Some character stuff. */
  73. #define control_character_threshold 0x020 /* smaller than this is control */
  74. #define meta_character_threshold 0x07f    /* larger than this is Meta. */
  75. #define control_character_bit 0x40    /* 0x000000, must be off. */
  76. #define meta_character_bit 0x080/* x0000000, must be on. */
  77.  
  78. #define info_separator_char '\037'
  79. #define start_of_node_string "\037"
  80.  
  81. #ifdef CTRL
  82. #undef CTRL
  83. #endif
  84.  
  85. #define CTRL(c) ((c) & (~control_character_bit))
  86. #define META(c) ((c) | meta_character_bit)
  87.  
  88. #define UNMETA(c) ((c) & (~meta_character_bit))
  89. #define UNCTRL(c) to_upper(((c)|control_character_bit))
  90.  
  91. #ifndef to_upper
  92. #define to_upper(c) (((c) < 'a' || (c) > 'z') ? c : c-32)
  93. #define to_lower(c) (((c) < 'A' || (c) > 'Z') ? c : c+32)
  94. #endif
  95.  
  96. #define CTRL_P(c) ((c) < control_character_threshold)
  97. #define META_P(c) ((c) > meta_character_threshold)
  98.  
  99. #define NEWLINE '\n'
  100. #define RETURN CTRL('M')
  101. #define DELETE 0x07f
  102. #define TAB '\t'
  103. #define ABORT_CHAR CTRL('G')
  104. #define PAGE CTRL('L')
  105. #define SPACE 0x020
  106. #define ESC CTRL('[')
  107. #define control_display_prefix '^'
  108.  
  109. #define TAG_TABLE_END_STRING "\037\nEND TAG TABLE"
  110. #define TAG_TABLE_BEG_STRING "\nTAG TABLE:\n"
  111. #define NODE_ID "Node:"
  112. #define NNODENAME 4        /* Default amount to grow nodename list by. */
  113. #define FILENAME_LEN 256
  114. #define NODENAME_LEN 256
  115. #define STRING_SIZE 256
  116. #define nodeend_sequence "\n\037"
  117.  
  118. /* All right, some windows stuff. */
  119.  
  120. typedef struct {
  121.   /* Absolute x and y coordinates for usable portion of this window. */
  122.   int left, top, right, bottom;
  123.   /* Absolute cursor position in this window. */
  124.   int ch, cv;
  125. } WINDOW;
  126.  
  127. typedef struct _wind_list {
  128.   int left, top, right, bottom;
  129.   int ch, cv;
  130.   struct _wind_list *next_window;
  131. } WINDOW_LIST;
  132.  
  133. WINDOW the_window = {0, 0, 80, 24, 0, 0};
  134. WINDOW_LIST *window_stack = (WINDOW_LIST *)NULL;
  135. WINDOW terminal_window = {0, 0, 80, 24, 0, 0};
  136.  
  137. /* Not really extern, but defined later in this file. */
  138. extern WINDOW echo_area;
  139. char *xmalloc (), *xrealloc (), *getenv ();
  140. char *next_info_file (), *opsys_filename ();
  141. boolean build_menu (), find_menu_node ();
  142. void swap_filestack (), pop_filestack ();
  143.  
  144. /* A crock, this should be done in a different way. */
  145. #define MAX_INDIRECT_FILES 100 
  146.  
  147. /* The info history list. */
  148. NODEINFO *Info_History = NULL;
  149.  
  150. /* ?Can't have more than xx files in the indirect list? */
  151. INDIRECT_INFO indirect_list[MAX_INDIRECT_FILES];
  152.  
  153. /* The filename of the currently loaded info file. */
  154. char current_info_file[FILENAME_LEN];
  155.  
  156. /* The nodename of the node the user is looking at. */
  157. char current_info_node[NODENAME_LEN];
  158.  
  159. /* The last file actually loaded.  Not the same as current info file. */
  160. char last_loaded_info_file[FILENAME_LEN];
  161.  
  162. /* Offsets in info_file of top and bottom of current_info_node. */
  163. int nodetop, nodebot;
  164.  
  165. /* Number of lines in this node. */
  166. int nodelines;
  167.  
  168. /* Buffer for the info file. */
  169. char *info_file = NULL;
  170.  
  171. /* Length of the above buffer. */
  172. int info_buffer_len;
  173.  
  174. /* Pointer to the start of a tag table, or NULL to show none. */
  175. char *tag_table = NULL;
  176.  
  177. /* Length of the above buffer. */
  178. int tag_buffer_len;        
  179.  
  180. /* If true, the tag table is indirect. */
  181. boolean indirect = false;
  182. int indirect_top;
  183.  
  184. /* Offset in the buffer of the current pagetop. */
  185. int pagetop;
  186.  
  187. /* If non-NULL, this is a colon separated list of directories to search
  188.    for a specific info file.  The user places this variable into his or
  189.    her environment. */
  190. char *infopath = NULL;
  191.  
  192. /* If filled, the name of a file to write to. */
  193. char dumpfile[FILENAME_LEN];
  194.  
  195. /* This is the command to print a node. A default value is compiled in,
  196.    or it can be found from the environment as $INFO_PRINT_COMMAND. */
  197. char *print_command;
  198.  
  199. /* **************************************************************** */
  200. /*                                    */
  201. /*            Getting Started.                */
  202. /*                                    */
  203. /* **************************************************************** */
  204.  
  205. /* Begin the Info session. */
  206.  
  207. /* Global is on until we are out of trouble. */
  208. int totally_inhibit_errors = 1;
  209.  
  210. struct option long_options[] = {
  211.   {"directory", 1, 0, 'd'},
  212.   {"node", 1, 0, 'n'},
  213.   {"file", 1, 0, 'f'},
  214.   {"output", 1, 0, 'o'},
  215.   {NULL, 0, NULL, 0}
  216. };
  217.  
  218. #define savestring(x) strcpy (xmalloc (1 + strlen (x)), (x))
  219.  
  220. main (argc, argv)
  221.      int argc;
  222.      char **argv;
  223. {
  224.   int c, ind;
  225.   char filename[FILENAME_LEN];
  226.   char *nodename;
  227.   char **nodenames;
  228.   int nodenames_size, nodenames_index;
  229.   char *ptr, *env_infopath, *env_print_command;
  230.  
  231.   nodenames_index = 0;
  232.   nodenames = (char **)xmalloc ((nodenames_size = 1) * sizeof (char *));
  233.   nodenames[0] = (char *)NULL;
  234.  
  235.   env_infopath = getenv ("INFOPATH");
  236.   env_print_command = getenv ("INFO_PRINT_COMMAND");
  237.  
  238.   filename[0] = '\0';
  239.  
  240.   if (env_infopath && *env_infopath)
  241.     infopath = savestring (env_infopath);
  242.   else
  243.     infopath = savestring (DEFAULT_INFOPATH);
  244.  
  245.   if (env_print_command && *env_print_command)
  246.     print_command = savestring (env_print_command);
  247.   else
  248.     print_command = savestring (INFO_PRINT_COMMAND);
  249.  
  250.   while ((c = getopt_long (argc, argv, "d:n:f:o:", long_options, &ind)) != EOF)
  251.     {
  252.       if (c == 0 && long_options[ind].flag == 0)
  253.     c = long_options[ind].val;
  254.       switch (c)
  255.     {
  256.     case 0:
  257.       break;
  258.       
  259.     case 'd':
  260.       free (infopath);
  261.       infopath = savestring (optarg);
  262.       break;
  263.       
  264.     case 'n':
  265.  
  266.       if (nodenames_index + 2 > nodenames_size)
  267.         nodenames = (char **)
  268.           xrealloc (nodenames, (nodenames_size += 10) * sizeof (char *));
  269.  
  270.       nodenames[nodenames_index++] = optarg;
  271.       nodenames[nodenames_index] = (char *)NULL;
  272.       break;
  273.       
  274.     case 'f':
  275.       strncpy (filename, optarg, FILENAME_LEN);
  276.       break;
  277.       
  278.     case 'o':
  279.       strncpy (dumpfile, optarg, FILENAME_LEN);
  280.       break;
  281.       
  282.     default:
  283.       usage ();
  284.     }
  285.     }
  286.  
  287.   /* Okay, flags are parsed.  Get possible Info menuname. */
  288.  
  289.   if (*filename && (ptr = rindex (filename,'/')) != NULL )
  290.     {
  291.       /* Add filename's directory to path. */
  292.       char *temp;
  293.  
  294.       temp = (char *)xmalloc (2 + strlen (filename) + strlen (infopath));
  295.       strncpy (temp, filename, ptr - filename);
  296.       sprintf (temp + (ptr - filename), ":%s", infopath);
  297.       free (infopath);
  298.       infopath = temp;
  299.     }
  300.  
  301.   /* Start with DIR or whatever was specified. */
  302.   if (!get_node (filename, (nodenames[0] == NULL) ? "" : nodenames[0], false)
  303.       && !get_node ((char *)NULL, (char *)NULL, true))
  304.       {
  305.     if (!*filename)
  306.       strcpy (filename, "dir");
  307.  
  308.     fprintf (stderr, "%s: Cannot find \"%s\", anywhere along the\n",
  309.          argv[0], filename);
  310.     fprintf (stderr, "search path of \"%s\".\n", infopath);
  311.  
  312.     exit (1);
  313.       }
  314.  
  315.   totally_inhibit_errors = 0;
  316.  
  317.   for (ind = 1 ; ind < nodenames_index ; ind++)
  318.     get_node (filename, nodenames[ind], false);
  319.  
  320.   nodename = nodenames[nodenames_index > 0 ? nodenames_index - 1 : 0];
  321.   if (!nodename)
  322.     {
  323.       nodename = (char *)xmalloc (NODENAME_LEN);
  324.       *nodename = '\0';
  325.     }
  326.   
  327.   if (optind != argc)
  328.     {
  329.       putchar ('\n');
  330.  
  331.       while (optind != argc)
  332.     {
  333.       if (!build_menu ())
  334.           {
  335.           display_error ("There is no menu in node \"%s\"",
  336.                  current_info_node );
  337.           break;
  338.         }
  339.       else if (!find_menu_node (argv[optind], nodename))
  340.         {
  341.           display_error
  342.         ("There is no menu entry for \"%s\" in node \"%s\"",
  343.          argv[optind], nodename );
  344.           break;
  345.         }
  346.       else if (!get_node ((char *)NULL, nodename, false))
  347.         {
  348.           break;
  349.         }
  350.       else
  351.          {
  352. #if defined (NOTDEF)
  353.           /* RMS says not to type this stuff out because he expects
  354.          programs to call Info instead of interactive users. */
  355.           printf ("%s.. ",argv[optind]);
  356.           fflush (stdout);
  357. #endif
  358.           optind++;
  359.         }
  360.     }
  361.     }
  362.   begin_info_session ();
  363.   exit (0);
  364. }
  365.  
  366. usage ()
  367. {
  368.   fprintf (stderr,"%s\n%s\n%s\n%s\n",
  369. "Usage: info [-d dir-path] [-f info-file] [-n node-name -n node-name ...]",
  370. "            [-o output-file] [+directory dir-path] [+file info-file]",
  371. "            [+node node-name +node node-name ...]",
  372. "            [+output output-file] [menu-selection...]");
  373.   exit (1);
  374. }
  375.  
  376. #if defined (SIGTSTP)
  377. Function *old_tstp;
  378. Function *old_ttou, *old_ttin;
  379. #endif /* SIGTSTP */
  380.  
  381. #if defined (SIGWINCH)
  382. Function *old_winch;
  383. #endif /* SIGWINCH */
  384.  
  385. /* Start using Info. */
  386. begin_info_session ()
  387. {
  388.   int info_signal_handler ();
  389.  
  390.   /* If the user just wants to dump the node, then do that. */
  391.   if (dumpfile[0])
  392.     {
  393.       dump_current_node (dumpfile);
  394.       exit (0);
  395.     }
  396.  
  397.   init_terminal_io ();
  398.  
  399.   /* Install handlers for restoring/breaking the screen. */
  400.  
  401.   install_signals ();
  402.   new_echo_area ();
  403.  
  404.   print_string ("Welcome to Info!  Type \"?\" for help. ");
  405.   close_echo_area ();
  406.   toploop ();
  407.   goto_xy (the_window.left, the_window.bottom + 1);
  408.   restore_io ();
  409. }
  410.  
  411. /* What to do before processing a stop signal. */
  412. before_stop_signal ()
  413. {
  414.   restore_io ();
  415. }
  416.  
  417. /* What to do after processing a stop signal. */
  418. after_stop_signal ()
  419. {
  420.   clear_screen ();
  421.   display_page ();
  422.   goto_xy (the_window.ch, the_window.cv);
  423.   opsys_init_terminal ();
  424. }
  425.  
  426. /* Do the right thing with this signal. */
  427. info_signal_handler (sig)
  428.      int sig;
  429. {
  430.   switch (sig)
  431.     {
  432. #if defined (SIGTSTP)
  433.     case SIGTSTP:
  434.     case SIGTTOU:
  435.     case SIGTTIN:
  436.       before_stop_signal ();
  437.       signal (sig, SIG_DFL);
  438.       kill (getpid (), sig);
  439.       after_stop_signal ();
  440.       signal (sig, info_signal_handler);
  441.       break;
  442. #endif /* SIGTSTP */
  443.  
  444. #if defined (SIGWINCH)
  445.     case SIGWINCH:
  446.       /* Window has changed.  Get the size of the new window, and rebuild our
  447.          window chain. */
  448.       {
  449.     int display_page ();
  450.     extern char *widest_line;
  451.     extern WINDOW terminal_window;
  452.     extern WINDOW_LIST *window_stack;
  453.     extern int terminal_rows, terminal_columns;
  454.     int delta_width, delta_height, right, bottom;
  455.  
  456.     right = get_terminal_columns ();
  457.     bottom = get_terminal_rows ();
  458.  
  459.     delta_width = right - terminal_columns;
  460.     delta_height = bottom - terminal_rows;
  461.  
  462.     terminal_columns = right;
  463.     terminal_rows = bottom;
  464.  
  465.     /* Save current window, whatever it is. */
  466.     push_window ();
  467.  
  468.     /* Change the value of the widest_line. */
  469.     free (widest_line);
  470.     widest_line = (char *) xmalloc (right);
  471.  
  472.     /* Make the new window.  Map over all windows in window list. */
  473.     {
  474.       WINDOW_LIST *wind = window_stack;
  475.       extern WINDOW modeline_window;
  476.  
  477.       while (wind != (WINDOW_LIST *)NULL)
  478.         {
  479.           adjust_wind ((WINDOW *)wind, delta_width, delta_height);
  480.           wind = wind->next_window;
  481.         }
  482.  
  483.       /* Adjust the other windows that we know about. */
  484.       adjust_wind (&terminal_window, delta_width, delta_height);
  485.       adjust_wind (&echo_area, delta_width, delta_height);
  486.       adjust_wind (&modeline_window, delta_width, delta_height);
  487.     }
  488.  
  489.     /* Clear and redisplay the entire terminal window. */
  490.     set_window (&terminal_window);
  491.     clear_screen ();
  492.  
  493.     /* Redisplay the contents of the screen. */
  494.     with_output_to_window (&terminal_window, display_page);
  495.  
  496.     /* Get back the current window. */
  497.     pop_window ();
  498.       }
  499.       break;
  500. #endif /* SIGWINCH */
  501.  
  502.     case SIGINT:
  503.       restore_io ();
  504.       exit (1);
  505.       break;
  506.     }
  507. }
  508.  
  509. install_signals ()
  510. {
  511. #if defined (SIGTSTP)
  512.   old_tstp = (Function *) signal (SIGTSTP, info_signal_handler);
  513.   old_ttou = (Function *) signal (SIGTTOU, info_signal_handler);
  514.   old_ttin = (Function *) signal (SIGTTIN, info_signal_handler);
  515. #endif /* SIGTSTP */
  516.  
  517. #if defined (SIGWINCH)
  518.   old_winch = (Function *) signal (SIGWINCH, info_signal_handler);
  519. #endif /* SIGWINCH */
  520.  
  521.   signal (SIGINT, info_signal_handler);
  522. }
  523.  
  524. adjust_wind (wind, delta_width, delta_height)
  525.      WINDOW *wind;
  526.      int delta_width, delta_height;
  527. {
  528.   wind->right += delta_width;
  529.   wind->bottom += delta_height;
  530.   wind->ch += delta_width;
  531.   wind->cv += delta_height;
  532.  
  533.   /* Ugly hack to fix busted windows code.  If the window we are adjusting
  534.      already has a TOP offset, then adjust that also. */
  535.   if (wind->top)
  536.     wind->top += delta_height;
  537. }
  538.  
  539. /* **************************************************************** */
  540. /*                                    */
  541. /*            Completing Things                */
  542. /*                                    */
  543. /* **************************************************************** */
  544.  
  545. typedef struct completion_entry
  546. {
  547.   char *identifier;
  548.   char *data;
  549.   struct completion_entry *next;
  550. }                COMP_ENTRY;
  551.  
  552. /* The linked list of COMP_ENTRY structures that you create. */
  553. COMP_ENTRY *completion_list = (COMP_ENTRY *) NULL;
  554.  
  555. /* The vector of COMP_ENTRY pointers that COMPLETE returns. */
  556. COMP_ENTRY **completions = NULL;
  557.  
  558. /* The number of elements in the above vector. */
  559. int completion_count;
  560.  
  561. /* Initial size of COMPLETIONS array. */
  562. #define INITIAL_COMPLETIONS_CORE_SIZE 200
  563.  
  564. /* Current size of the completion array in core. */
  565. int completions_core_size = 0;
  566.  
  567. /* Ease the typing task.  Another name for the I'th
  568.    IDENTIFIER of COMPLETIONS. */
  569. #define completion_id(i) ((completions[(i)])->identifier)
  570.  
  571. /* The number of completions that can be present before the help
  572.    function starts asking you about whether it should print them
  573.    all or not. */
  574. int completion_query_threshold = 100;
  575.  
  576. free_completion_list ()
  577. {
  578.   COMP_ENTRY *temp;
  579.   while (completion_list)
  580.     {
  581.       temp = completion_list;
  582.  
  583.       if (completion_list->identifier)
  584.     free (completion_list->identifier);
  585.  
  586.       if (completion_list->data)
  587.     free (completion_list->data);
  588.  
  589.       completion_list = completion_list->next;
  590.       free (temp);
  591.     }
  592. }
  593.  
  594. /* Add a single completion to COMPLETION_LIST.
  595.    IDENTIFIER is the string that the user should type.
  596.    DATA should just be a pointer to some random data that you wish to
  597.    have associated with the identifier, but I'm too stupid for that, so
  598.    it must be a string as well.  This allocates the space for the strings
  599.    so you don't necessarily have to. */
  600. add_completion (identifier, data)
  601.      char *identifier, *data;
  602. {
  603.   COMP_ENTRY *temp = (COMP_ENTRY *) xmalloc (sizeof (COMP_ENTRY));
  604.  
  605.   temp->identifier = (char *) xmalloc (strlen (identifier) + 1);
  606.   strcpy (temp->identifier, identifier);
  607.  
  608.   temp->data = (char *) xmalloc (strlen (data) + 1);
  609.   strcpy (temp->data, data);
  610.  
  611.   temp->next = completion_list;
  612.   completion_list = temp;
  613. }
  614.  
  615. /* Function for reading a line.  Supports completion on COMPLETION_LIST
  616.    if you pass COMPLETING as true.  Prompt is either a prompt or NULL,
  617.    LINE is the place to store the characters that are read.  LINE may start
  618.    out already containing some characters; if so, they are printed.  MAXCHARS
  619.    tells how many characters can fit in the buffer at LINE.  READLINE returns
  620.    FALSE if the user types the abort character.  LINE is returned with a '\0'
  621.    at the end, not a '\n'.  */
  622. boolean
  623. readline (prompt, line, maxchars, completing)
  624.      char *prompt, *line;
  625.      int maxchars;
  626.      boolean completing;
  627. {
  628.   int character;
  629.   int readline_ch, readline_cv;
  630.   int current_len = strlen (line);
  631.   int just_completed = 0;        /* Have we just done a completion? */
  632.   int meta_flag = 0;
  633.  
  634.   new_echo_area ();
  635.  
  636.   if (prompt)
  637.     print_string (prompt);
  638.  
  639.   readline_ch = the_window.ch;
  640.   readline_cv = the_window.cv;
  641.  
  642.   print_string (line);
  643.  
  644.   while (true)
  645.     {
  646.       line[current_len] = '\0';
  647.       goto_xy (readline_ch, readline_cv);
  648.       print_string (line);
  649.       clear_eol ();
  650.  
  651.       if (just_completed)
  652.     just_completed--;
  653.  
  654.       character = blink_cursor ();
  655.       if (meta_flag)
  656.     {
  657.       character = META (character);
  658.       meta_flag = 0;
  659.     }
  660.  
  661.       if (META_P (character))
  662.     character = META (to_upper (UNMETA (character)));
  663.  
  664.       switch (character)
  665.     {
  666.     case EOF:
  667.       character = '\n';
  668.  
  669.     case ESC:
  670.       meta_flag++;
  671.       break;
  672.  
  673.     case META (DELETE):
  674.     case CTRL ('W'):
  675.       while (current_len && line[current_len] == SPACE)
  676.         current_len--;
  677.  
  678.       if (!current_len)
  679.         break;
  680.  
  681.       while (current_len && line[current_len] != SPACE)
  682.         current_len--;
  683.  
  684.       break;
  685.  
  686.     case CTRL ('U'):
  687.       current_len = 0;
  688.       break;
  689.  
  690.     case '\b':
  691.     case 0x07f:
  692.       if (current_len)
  693.         current_len--;
  694.       else
  695.         ding ();
  696.       break;
  697.  
  698.     case '\n':
  699.     case '\r':
  700.       if (completing)
  701.         {
  702.           extern int completion_count;
  703.  
  704.           try_complete (line);
  705.  
  706.           if (completion_count >= 1)
  707.         {
  708.           close_echo_area ();
  709.           return (true);
  710.         }
  711.           else
  712.         {
  713.           current_len = strlen (line);
  714.           break;
  715.         }
  716.         }
  717.       else
  718.         {
  719.           close_echo_area ();
  720.           return (true);
  721.         }
  722.  
  723.     case ABORT_CHAR:
  724.       ding ();
  725.  
  726.       if (current_len)
  727.         {
  728.           current_len = 0;
  729.         }
  730.       else
  731.         {
  732.           close_echo_area ();
  733.           clear_echo_area ();
  734.           return (false);
  735.         }
  736.       break;
  737.  
  738.     case ' ':
  739.     case '\t':
  740.     case '?':
  741.       if (completing)
  742.         {
  743.           extern int completion_count;
  744.  
  745.           if (character == '?' || just_completed)
  746.         {
  747.           help_possible_completions (line);
  748.           break;
  749.         }
  750.           else
  751.         {
  752.           char temp_line[NODENAME_LEN];
  753.           strcpy (temp_line, line);
  754.           try_complete (line); just_completed = 2;
  755.           if (completion_count != 1 && character == SPACE)
  756.             {
  757.               if (strcmp (temp_line, line) == 0)
  758.             {
  759.               line[current_len] = SPACE;
  760.               line[current_len + 1] = '\0';
  761.               strcpy (temp_line, line);
  762.               try_complete (line);
  763.               if (completion_count == 0)
  764.                 {
  765.                   line[current_len] = '\0';
  766.                   ding ();
  767.                 }
  768.             }
  769.             }
  770.           current_len = strlen (line);
  771.           if (completion_count == 0)
  772.             ding ();
  773.           break;
  774.         }
  775.         }
  776.       /* Do *NOT* put anything in-between the completing cases and
  777.          the default: case.  No.  Because the SPC, TAB and `?' get
  778.          treated as normal characters by falling through the
  779.          "if (completing)" test above. */
  780.     default:
  781.       if (!CTRL_P (character) &&
  782.           !META_P (character) &&
  783.           current_len < maxchars)
  784.         line[current_len++] = character;
  785.       else
  786.         ding ();
  787.     }
  788.     }
  789. }
  790.  
  791. /* Initialize whatever the completer is using. */
  792. init_completer ()
  793. {
  794.   if (completions_core_size != INITIAL_COMPLETIONS_CORE_SIZE)
  795.     {
  796.       if (completions)
  797.     free (completions);
  798.  
  799.       completions = (COMP_ENTRY **)
  800.     xmalloc ((sizeof (COMP_ENTRY *))
  801.          * (completions_core_size = INITIAL_COMPLETIONS_CORE_SIZE));
  802.     }
  803.   completion_count = 0;
  804. }
  805.  
  806. /* Reverse the completion list passed in LIST, and
  807.    return a pointer to the new head. */
  808. COMP_ENTRY *
  809. reverse_list (list)
  810.      COMP_ENTRY *list;
  811. {
  812.   COMP_ENTRY *next;
  813.   COMP_ENTRY *prev = (COMP_ENTRY *) NULL;
  814.  
  815.   while (list)
  816.     {
  817.       next = list->next;
  818.       list->next = prev;
  819.       prev = list;
  820.       list = next;
  821.     }
  822.   return (prev);
  823. }
  824.  
  825. /* Remember the possible completion passed in POINTER on the
  826.    completions list. */
  827. remember_completion (pointer)
  828.      COMP_ENTRY *pointer;
  829. {
  830.   if (completion_count == completions_core_size)
  831.     {
  832.       COMP_ENTRY **temp = (COMP_ENTRY **)
  833.     realloc (completions, ((sizeof (COMP_ENTRY *))
  834.                    * (completions_core_size +=
  835.                   INITIAL_COMPLETIONS_CORE_SIZE)));
  836.       if (!temp)
  837.     {
  838.       display_error ("Too many completions (~d)!  Out of core!",
  839.              completion_count);
  840.       return;
  841.     }
  842.       else
  843.     completions = temp;
  844.     }
  845.   completions[completion_count++] = pointer;
  846. }
  847.  
  848. /* Complete TEXT from identifiers in LIST.  Place the resultant
  849.    completions in COMPLETIONS, and the number of completions in
  850.    COMPLETION_COUNT. Modify TEXT to contain the least common
  851.    denominator of all the completions found. */
  852. boolean
  853. complete (text, list)
  854.      char *text;
  855.      COMP_ENTRY *list;
  856. {
  857.   int low_match, i, idx;
  858.   int string_length = strlen (text);
  859.  
  860.   init_completer ();
  861.   low_match = 100000;        /* Some large number. */
  862.  
  863.   while (list)
  864.     {
  865.       if (strnicmp (text, list->identifier, string_length) == 0)
  866.     remember_completion (list);
  867.       list = list->next;
  868.     }
  869.  
  870.   if (completion_count == 0)
  871.     return (false);
  872.  
  873.   if (completion_count == 1)
  874.     {                /* One completion */
  875.       strcpy (text, completion_id (0));
  876.       return (true);
  877.     }
  878.  
  879.   /* Else find the least common denominator */
  880.  
  881.   idx = 1;
  882.  
  883.   while (idx < completion_count)
  884.     {
  885.       int c1, c2;
  886.       for (i = 0;
  887.        (c1 = to_lower (completion_id (idx - 1)[i])) &&
  888.        (c2 = to_lower (completion_id (idx)[i]));
  889.        i++)
  890.     if (c1 != c2)
  891.       break;
  892.  
  893.       if (low_match > i)
  894.     low_match = i;
  895.       idx++;
  896.     }
  897.  
  898.   strncpy (text, completion_id (0), low_match);
  899.   text[low_match] = '\0';
  900.   return (true);
  901. }
  902.  
  903. /* Complete TEXT from the completion structures in COMPLETION_LIST. */
  904. boolean
  905. try_complete (text)
  906.      char *text;
  907. {
  908.   return (complete (text, completion_list));
  909. }
  910.  
  911. /* The function that prints out the possible completions. */
  912. help_possible_completions (text)
  913.      char *text;
  914. {
  915.   char temp_string[2000];
  916.  
  917.   goto_xy (the_window.left, the_window.top);
  918.   strcpy (temp_string, text);
  919.   try_complete (temp_string);
  920.  
  921.   open_typeout ();
  922.  
  923.   if (completion_count == 0)
  924.     {
  925.       print_string ("There are no possible completions.\n");
  926.       goto print_done;
  927.     }
  928.  
  929.   if (completion_count == 1)
  930.     {
  931.       print_string
  932.     ("The only possible completion of what you have typed is:\n\n");
  933.       print_string (completion_id(0));
  934.       goto print_done;
  935.     }
  936.  
  937.   if (completion_count >= completion_query_threshold)
  938.     {
  939.       print_string
  940.     ("\nThere are %d completions.  Do you really want to see them all",
  941.      completion_count);
  942.  
  943.       if (!get_y_or_n_p ())
  944.     return;
  945.     }
  946.  
  947.   print_string ("\nThe %d completions of what you have typed are:\n\n",
  948.         completion_count);
  949.  
  950.   {
  951.     int idx = 0;
  952.     int counter = 0;
  953.     int columns = (the_window.right - the_window.left) / 30;
  954.  
  955.     while (idx < completion_count)
  956.       {
  957.     if (counter == columns)
  958.       {
  959.         charout ('\n');
  960.         counter = 0;
  961.       }
  962.     indent_to (counter * 30);
  963.     print_string (completion_id (idx));
  964.     counter++;
  965.     idx++;
  966.       }
  967.   }
  968.  
  969. print_done:
  970.   print_string ("\n\n-----------------\n");
  971.   close_typeout ();
  972. }
  973.  
  974. /* Return the next file that should be searched, or NULL if we are at the end
  975.    of the info file. If the FILE argument is provided, begin the search there,
  976.    if REWIND is true start the search at the beginning of the list.
  977.  
  978.    The list is the one built by an indirect tag table, on the supposition
  979.    that those files form a logical set to search if we are in one of them.
  980.    If no such list is current (either it doesn't exist, or FILE isn't on
  981.    it) the search list is set to be last_loaded_info_file */
  982. char *
  983. next_info_file (file, rewind)
  984.      char *file;        /* file to set `next' to. May be NULL*/
  985.      boolean rewind;        /* should I rewind the file list? */
  986. {
  987.   static int index = -1;
  988.  
  989.   if (file != NULL)
  990.     {
  991.       if (rewind)
  992.     {
  993.       fprintf (stderr,"I can't both rewind and set to a file\n");
  994.       brians_error ();
  995.     }
  996.       else
  997.     {
  998.       char *ptr = rindex (file,'/');
  999.  
  1000.       if (ptr != NULL)
  1001.         file = ptr + 1;
  1002.  
  1003.       for (index = 0;
  1004.            index < MAX_INDIRECT_FILES &&
  1005.            indirect_list[index].filename != (char *)NULL;
  1006.            index++)
  1007.         {
  1008.           if (strcmp (file, indirect_list[index].filename) == 0)
  1009.         {
  1010.           return (file);
  1011.         }
  1012.         }
  1013.       /* OK, we are not on the current indirect_list. This means that
  1014.          we have switched to another node that has no indirect list,
  1015.          so forget the old one. */
  1016.       for (index = 0;
  1017.            index < MAX_INDIRECT_FILES &&
  1018.            indirect_list[index].filename!= (char *)NULL;
  1019.            index++)
  1020.         {
  1021.           free (indirect_list[index].filename);
  1022.           indirect_list[index].filename = (char *)NULL;
  1023.         }
  1024.  
  1025.       return (indirect_list[0].filename = savestring (file));
  1026.       }
  1027.     }
  1028.   else if (rewind)
  1029.     {
  1030.       index = 0;
  1031.  
  1032.       if (indirect_list[0].filename == (char *)NULL)
  1033.     indirect_list[0].filename = savestring (last_loaded_info_file);
  1034.     }
  1035.   else
  1036.     index++;
  1037.   
  1038.   if (index < MAX_INDIRECT_FILES &&
  1039.       indirect_list[index].filename != (char *)NULL)
  1040.     {
  1041.       return (indirect_list[index].filename);
  1042.     }
  1043.   else
  1044.     {
  1045.       index = -1;
  1046.       return (NULL);
  1047.     }
  1048. }
  1049.  
  1050. /* **************************************************************** */
  1051. /*                                    */
  1052. /*            Getting Nodes                    */
  1053. /*                                    */
  1054. /* **************************************************************** */
  1055.  
  1056. /* A node name looks like:
  1057.    Node: nodename with spaces but not a comma,
  1058. or Node: (filename-containing-node)node-within-file
  1059. or Node: (filename)
  1060.  
  1061.    The latter case implies a nodename of "Top".  All files are
  1062.    supposed to have one.
  1063.  
  1064.    Lastly, the nodename specified could be "*", which specifies the
  1065.    entire file. */
  1066.  
  1067. /* Load FILENAME.  If REMEMBER_NAME is true, then remember the
  1068.    loaded filename in CURRENT_INFO_FILE.  In either case, remember
  1069.    the name of this file in LAST_LOADED_INFO_FILE. */
  1070. boolean
  1071. get_info_file (filename, remember_name)
  1072.      char *filename;
  1073.      boolean remember_name;
  1074. {
  1075.   FILE *input_stream;
  1076.   struct stat file_info;
  1077.   int pointer, result;
  1078.   char tempname[FILENAME_LEN];
  1079.  
  1080.   /* Get real filename. */
  1081.   strcpy (tempname, opsys_filename (filename));
  1082.  
  1083.   /* See if the file exists. */
  1084.   if ((result = stat (tempname, &file_info)) != 0)
  1085.     {
  1086.       /* Try again, this time with the name in lower-case. */
  1087.       char temp_tempname[FILENAME_LEN];
  1088.       int i;
  1089.  
  1090.       for (i = 0; temp_tempname[i] = to_lower (tempname[i]); i++);
  1091.       /* Get real filename again. AMS */
  1092.       strcpy (temp_tempname, opsys_filename (temp_tempname));
  1093.  
  1094.       result = stat (temp_tempname, &file_info);
  1095.       if (!result)
  1096.     strcpy (tempname, temp_tempname);
  1097.     }
  1098.  
  1099.   /* See if this file is the last loaded one. */
  1100.   if (!result && (strcmp (last_loaded_info_file, tempname) == 0))
  1101.     {
  1102.       return (true);        /* Okay, the file is loaded. */
  1103.     }
  1104.  
  1105.   /* Now try to open the file. */
  1106.   if (result || (input_stream = fopen (tempname, "r")) == NULL)
  1107.     {
  1108.       file_error (tempname);
  1109.       return (1);
  1110.     }
  1111.  
  1112.   /* If we already have a file loaded, then free it first. */
  1113.   if (info_file)
  1114.     {
  1115.       free (info_file);
  1116.  
  1117.       if (!indirect)
  1118.     {
  1119.       /* Then the tag table is also no longer valid. */
  1120.       tag_table = (char *) NULL;
  1121.     }
  1122.     }
  1123.  
  1124.   /* Read the contents of the file into a new buffer. */
  1125.  
  1126.   info_file = (char *) xmalloc (info_buffer_len = file_info.st_size);
  1127.   fread (info_file, 1, info_buffer_len, input_stream);
  1128.   fclose (input_stream);
  1129.   strcpy (last_loaded_info_file, tempname);
  1130.   if (remember_name)
  1131.     {
  1132.       strcpy (current_info_file, tempname);
  1133.       if (indirect)
  1134.     {
  1135.       int idx;
  1136.       indirect = false;
  1137.       free (tag_table);
  1138.     }
  1139.     }
  1140.   else
  1141.     return (true);
  1142.  
  1143.   /* The file has been read, and we don't know anything about it.
  1144.      Find out if it contains a tag table. */
  1145.  
  1146.   tag_table = NULL;        /* assume none. */
  1147.   indirect = false;
  1148.   tag_buffer_len = 0;
  1149.  
  1150.   set_search_constraints (info_file, info_buffer_len);
  1151.  
  1152.   /* Go to the last few lines in the file. */
  1153.   pointer = back_lines (8, info_buffer_len);
  1154.   pointer = search_forward (TAG_TABLE_END_STRING, pointer);
  1155.  
  1156.   if (pointer > -1)
  1157.     {
  1158.       /* Then there is a tag table.  Find the start of it,
  1159.      and remember that. */
  1160.       pointer = search_backward (TAG_TABLE_BEG_STRING, pointer);
  1161.  
  1162.       /* Handle error for malformed info file. */
  1163.       if (pointer < 0)
  1164.     display_error ("Start of tag table not found!");
  1165.       else
  1166.     {
  1167.       /* No problem.  If this file is an indirect file, then the contents
  1168.          of the tag table must remain in RAM the entire time.  Otherwise,
  1169.          we can flush the tag table with the file when the file is flushed.
  1170.          So, if indirect, remember that, and copy the table to another
  1171.          place.*/
  1172.  
  1173.       int indirect_check = forward_lines (2, pointer);
  1174.  
  1175.       tag_table = info_file + pointer;
  1176.       tag_buffer_len = info_buffer_len - pointer;
  1177.  
  1178.       /* Shorten the search constraints. */
  1179.       info_buffer_len = pointer;
  1180.  
  1181.       if (looking_at ("(Indirect)\n", indirect_check))
  1182.         {
  1183.           /* We have to find the start of the indirect file's
  1184.          information. */
  1185.           tag_table = (char *) xmalloc (tag_buffer_len);
  1186.  
  1187.           bcopy (&info_file[indirect_check], tag_table, tag_buffer_len);
  1188.  
  1189.           /* Find the list of filenames. */
  1190.           indirect_top = search_backward ("Indirect:\n", indirect_check);
  1191.           if (indirect_top < 0)
  1192.         {
  1193.           free (tag_table);
  1194.           tag_table = (char *) NULL;
  1195.           display_error ("Start of INDIRECT tag table not found!");
  1196.           return (false);
  1197.         }
  1198.  
  1199.           /* Remember the filenames, and their byte offsets. */
  1200.           {
  1201.         /* Index into the filename/offsets array. */
  1202.         int idx;
  1203.         char temp_filename[FILENAME_LEN];
  1204.         int temp_first_byte;
  1205.  
  1206.         info_buffer_len = indirect_top;
  1207.  
  1208.         /* For each line, scan the info into globals.  Then save
  1209.                the information in the INDIRECT_INFO structure. */
  1210.  
  1211.         for (idx = 0; idx < MAX_INDIRECT_FILES &&
  1212.              indirect_list[idx].filename != (char *) NULL;
  1213.              idx++)
  1214.             {
  1215.              free (indirect_list[idx].filename);
  1216.              indirect_list[idx].filename = (char *) NULL;
  1217.           }
  1218.         
  1219.         for ( idx = 0;info_file[indirect_top] != info_separator_char &&
  1220.                idx < MAX_INDIRECT_FILES;)
  1221.           {
  1222.             indirect_top = forward_lines (1, indirect_top);
  1223.             if (info_file[indirect_top] == info_separator_char)
  1224.               break;
  1225.  
  1226.             /* Ignore blank lines. */
  1227.             if (info_file[indirect_top] == '\n')
  1228.               continue;
  1229.  
  1230.             sscanf (&info_file[indirect_top], "%s%d",
  1231.                 temp_filename, &temp_first_byte);
  1232.  
  1233.             if (strlen (temp_filename))
  1234.               {
  1235.             temp_filename[strlen (temp_filename) - 1] = '\0';
  1236.             indirect_list[idx].filename =
  1237.               (char *) xmalloc (strlen (temp_filename) + 1);
  1238.             strcpy (indirect_list[idx].filename, temp_filename);
  1239.             indirect_list[idx].first_byte = temp_first_byte;
  1240.             idx++;
  1241.               }
  1242.           }
  1243.         /* Terminate the table. */
  1244.         if (idx == MAX_INDIRECT_FILES)
  1245.           {
  1246.             display_error
  1247.               ("Sorry, the INDIRECT file array isn't large enough.");
  1248.             idx--;
  1249.           }
  1250.         indirect_list[idx].filename = (char *) NULL;
  1251.           }
  1252.           indirect = true;
  1253.        } else {
  1254.           ;
  1255.        }
  1256.     }
  1257.     }
  1258.   return (true);
  1259. }
  1260.  
  1261. /* Make current_info_node be NODENAME.  This could involve loading
  1262.    a file, etc.  POPPING is true if we got here because we are popping
  1263.    one level. */
  1264. boolean
  1265. get_node (filename, nodename, popping)
  1266.      char *nodename, *filename;
  1267.      boolean popping;
  1268. {
  1269.   int pointer;
  1270.   char internal_filename[FILENAME_LEN];
  1271.   char internal_nodename[NODENAME_LEN];
  1272.  
  1273.   if (nodename && *nodename)
  1274.     {
  1275.       /* Maybe nodename looks like: (filename)nodename, or worse: (filename).
  1276.          If so, extract the stuff out. */
  1277.       if (*nodename == '(')
  1278.     {
  1279.       int temp = 1, temp1 = 0;
  1280.       char character;
  1281.  
  1282.       filename = internal_filename;
  1283.  
  1284.       while ((character = nodename[temp]) && character != ')')
  1285.         {
  1286.           filename[temp - 1] = character;
  1287.           temp++;
  1288.         }
  1289.       filename[temp - 1] = '\0';
  1290.       temp++;            /* skip the closing ')' */
  1291.  
  1292.       /* We have the filename now.  The nodename follows. */
  1293.       internal_nodename[0] = '\0';
  1294.       while (nodename[temp] == ' ' || nodename[temp] == '\t' ||
  1295.          nodename[temp] == '\n') temp++;
  1296.       if (nodename[temp])
  1297.         {
  1298.           while (internal_nodename[temp1++] = nodename[temp++]);
  1299.         } else {
  1300.            if (*filename != '\0') {
  1301.           strcpy(internal_nodename,"Top");
  1302.            }
  1303.         }
  1304.       nodename = internal_nodename;
  1305.     }
  1306.     }
  1307.  
  1308.   if (!popping)
  1309.     push_node (current_info_file, current_info_node, pagetop, nodetop);
  1310.  
  1311.   if (!nodename || !*nodename)
  1312.     {
  1313.       nodename = internal_nodename;
  1314.       strcpy (nodename, "Top");
  1315.     }
  1316.  
  1317.   if (!filename || !*filename)
  1318.     {
  1319.       filename = internal_filename;
  1320.       strcpy (filename, current_info_file);
  1321.     }
  1322.  
  1323.   if (!*filename)
  1324.     strcpy (filename, "dir");
  1325.  
  1326.   if (!get_info_file (filename, true))
  1327.     goto node_not_found;
  1328.  
  1329.   if (strcmp (nodename, "*") == 0)
  1330.     {
  1331.       /* The "node" that we want is the entire file. */
  1332.       pointer = 0;
  1333.       goto found_node;
  1334.     }
  1335.   
  1336.   /* If we are using a tag table, see if we can find the nodename in it. */
  1337.   if (tag_table)
  1338.     {
  1339.       pointer = find_node_in_tag_table (nodename, 0);
  1340.       if (pointer < 1)
  1341.     {
  1342.       /* The search through the tag table failed.  Maybe we
  1343.          should try searching the buffer?  Nahh, just barf. */
  1344.       boolean pop_node ();
  1345.  
  1346.     node_not_found:
  1347.       if (popping)
  1348.         return (false);    /* Second time through. */
  1349.  
  1350.       {
  1351.          int save_inhibit = totally_inhibit_errors;
  1352.          totally_inhibit_errors = 0; /* we always want to see this one */
  1353.          display_error
  1354.            ("Sorry, unable to find the node \"%s\" in the file \"%s\".",
  1355.         nodename, filename);
  1356.          totally_inhibit_errors = save_inhibit;
  1357.       }
  1358.  
  1359.       current_info_file[0] = '\0';
  1360.       current_info_node[0] = '\0';
  1361.       last_loaded_info_file[0] = '\0';
  1362.       pop_node (internal_filename, internal_nodename, &nodetop, &pagetop);
  1363.       get_node (internal_filename, internal_nodename, true);
  1364.       return (false);
  1365.     }
  1366.  
  1367.       /* Otherwise, the desired location is right here.
  1368.          Scarf the position byte. */
  1369.       while (tag_table[pointer] != '\177')
  1370.     pointer++;
  1371.  
  1372.       sscanf (&tag_table[pointer + 1], "%d", &pointer);
  1373.  
  1374.       /* Okay, we have a position pointer.  If this is an indirect file,
  1375.          then we should look through the indirect_list for the first
  1376.          element.first_byte which is larger than this.  Then we can load
  1377.          the specified file, and win. */
  1378.       if (indirect)
  1379.     {
  1380.       /* Find the filename for this node. */
  1381.       int idx;
  1382.       for (idx = 0; idx < MAX_INDIRECT_FILES &&
  1383.            indirect_list[idx].filename != (char *) NULL; idx++)
  1384.         {
  1385.           if (indirect_list[idx].first_byte > pointer)
  1386.         {
  1387.           /* We found it. */
  1388.           break;
  1389.         }
  1390.         }
  1391.       if (!get_info_file (indirect_list[idx - 1].filename, true))
  1392.         goto node_not_found;
  1393.       pointer -= indirect_list[idx - 1].first_byte;
  1394.  
  1395.       /* Here is code to compensate for the header of an indirect file. */
  1396.       {
  1397.         int tt = find_node_start (0);
  1398.         if (tt > -1)
  1399.           pointer += tt;
  1400.       }
  1401.     }
  1402.       else
  1403.     {
  1404.       /* This tag table is *not* indirect.  The filename of the file
  1405.          containing this node is the same as the current file.  The
  1406.          line probably looks like:
  1407.          File: info,  Node: Checking25796 */
  1408.     }
  1409.     }
  1410.   else
  1411.     {
  1412.       /* We don't have a tag table.  The node can only be found by
  1413.          searching this file in its entirety.  */
  1414.       get_info_file (filename, true);
  1415.       pointer = 0;
  1416.     }
  1417.  
  1418.   /* Search this file, using pointer as a good guess where to start. */
  1419.   /* This is the same number that RMS used.  It might be right or wrong. */
  1420.   pointer -= 1000;
  1421.   if (pointer < 0)
  1422.     pointer = 0;
  1423.  
  1424.   pointer = find_node_in_file (nodename, pointer);
  1425.   if (pointer < 0)
  1426.     goto node_not_found;
  1427.  
  1428.   /* We found the node in it's file.  Remember exciting information. */
  1429.  
  1430. found_node:
  1431.   back_lines (0, pointer);
  1432.   nodetop = pagetop = pointer;
  1433.   strcpy (current_info_node, nodename);
  1434.   strcpy (current_info_file, filename);
  1435.   get_node_extent ();
  1436.   return (true);
  1437. }
  1438.  
  1439. /* Get the bounds for this node.  NODETOP points to the start of the
  1440.    node. Scan forward looking for info_separator_char, and remember
  1441.    that in NODEBOT. */
  1442. get_node_extent ()
  1443. {
  1444.   int idx = nodetop;
  1445.   int character;
  1446.   boolean do_it_till_end = (strcmp (current_info_node, "*") == 0);
  1447.  
  1448.   nodelines = 0;
  1449.  
  1450. again:
  1451.   while ((idx < info_buffer_len) &&
  1452.      ((character = info_file[idx]) != info_separator_char))
  1453.     {
  1454.       if (character == '\n')
  1455.     nodelines++;
  1456.       idx++;
  1457.     }
  1458.   if (do_it_till_end && idx != info_buffer_len)
  1459.     {
  1460.       idx++;
  1461.       goto again;
  1462.     }
  1463.   nodebot = idx;
  1464. }
  1465.  
  1466. /* Locate the start of a node in the current search_buffer.  Return
  1467.    the offset to the node start, or minus one.  START is the place in
  1468.    the file at where to begin the search. */
  1469. find_node_start (start)
  1470.      int start;
  1471. {
  1472.   return (search_forward (start_of_node_string, start));
  1473. }
  1474.  
  1475. /* Find NODENAME in TAG_TABLE. */
  1476. find_node_in_tag_table (nodename, offset)
  1477.      char *nodename;
  1478.      int offset;
  1479. {
  1480.   int temp;
  1481.  
  1482.   set_search_constraints (tag_table, tag_buffer_len);
  1483.  
  1484.   temp = offset;
  1485.   while (true)
  1486.     {
  1487.       offset = search_forward (NODE_ID, temp);
  1488.  
  1489.       if (offset < 0)
  1490.     return (offset);
  1491.  
  1492.       temp = skip_whitespace (offset + strlen (NODE_ID));
  1493.  
  1494.       if (strnicmp (tag_table + temp, nodename, strlen (nodename)) == 0)
  1495.     if (*(tag_table + temp + strlen (nodename)) == '\177')
  1496.       return (temp);
  1497.     }
  1498. }
  1499.  
  1500. /* Find NODENAME in INFO_FILE. */
  1501. find_node_in_file (nodename, offset)
  1502.      char *nodename;
  1503.      int offset;
  1504. {
  1505.   int temp, last_offset = -1;
  1506.  
  1507.   set_search_constraints (info_file, info_buffer_len);
  1508.  
  1509.   while (true)
  1510.     {
  1511.       offset = find_node_start (offset);
  1512.  
  1513.       if (offset == last_offset)
  1514.     offset = -1;
  1515.       else
  1516.     last_offset = offset;
  1517.  
  1518.       if (offset < 0)
  1519.     return (offset);
  1520.       else
  1521.     temp = forward_lines (1, offset);
  1522.  
  1523.       if (temp == offset)
  1524.     return (-1);        /* At last line now, just a node start. */
  1525.       else
  1526.     offset = temp;
  1527.  
  1528.       temp = string_in_line (NODE_ID, offset);
  1529.  
  1530.       if (temp > -1)
  1531.     {
  1532.       temp = skip_whitespace (temp + strlen (NODE_ID));
  1533.       if (strnicmp (info_file + temp, nodename, strlen (nodename)) == 0)
  1534.         {
  1535.           int check_exact = *(info_file + temp + strlen (nodename));
  1536.  
  1537.           if (check_exact == '\t' ||
  1538.           check_exact == ',' ||
  1539.           check_exact == '.' ||
  1540.           check_exact == '\n')
  1541.         return (offset);
  1542.         }
  1543.     }
  1544.     }
  1545. }
  1546.  
  1547.  
  1548. /* **************************************************************** */
  1549. /*                                    */
  1550. /*            Dumping and Printing Nodes                */
  1551. /*                                    */
  1552. /* **************************************************************** */
  1553.  
  1554. /* Make a temporary filename based on STARTER and the PID of this Info. */
  1555. char *
  1556. make_temp_filename (starter)
  1557.      char *starter;
  1558. {
  1559.   char *temp = (char *) xmalloc (FILENAME_LEN);
  1560.   sprintf (temp, "%s-%d", starter, getpid ());
  1561.   return (temp);
  1562. }
  1563.  
  1564. /* Delete a file.  Print errors if necessary. */
  1565. deletefile (filename)
  1566.      char *filename;
  1567. {
  1568.   if (unlink (filename) != 0)
  1569.     {
  1570.       file_error (filename);
  1571.       return (1);
  1572.     }
  1573.   return (0);
  1574. }
  1575.  
  1576. printfile (filename)
  1577.      char *filename;
  1578. {
  1579.   int length = strlen (print_command) + strlen (filename) + strlen ("\n") + 1;
  1580.   char *command = (char *) xmalloc (length);
  1581.   int error;
  1582.  
  1583.   display_error ("Printing file `%s'...\n", filename);
  1584.   sprintf (command, "%s %s", print_command, filename);
  1585.   error = system (command);
  1586.   if (error)
  1587.     display_error ("Can't invoke `%s'", command);
  1588.   free (command);
  1589.   return (error);
  1590. }
  1591.  
  1592. /* Dump the current node into a file named FILENAME.
  1593.    Return 0 if the dump was successful, otherwise,
  1594.    print error and exit. */
  1595. dump_current_node (filename)
  1596.      char *filename;
  1597. {
  1598.   int i = nodetop;
  1599.   int c;
  1600.   FILE *output_stream = fopen (filename, "w");
  1601.   if (output_stream == (FILE *) NULL)
  1602.     {
  1603.       file_error (filename);
  1604.       return (1);
  1605.     }
  1606.  
  1607.   while (i < nodebot && i < info_buffer_len)
  1608.     {
  1609.       c = info_file[i];
  1610.       if (c < 0x020 && !(index ("\n\t\f", c)))
  1611.     {
  1612.       putc ('^', output_stream);
  1613.     }
  1614.       if (putc (info_file[i++], output_stream) == EOF)
  1615.     {
  1616.       fclose (output_stream);
  1617.       file_error (filename);
  1618.       return (1);
  1619.     }
  1620.     }
  1621.   fclose (output_stream);
  1622.   return (0);
  1623. }
  1624.  
  1625. /* **************************************************************** */
  1626. /*                                    */
  1627. /*             Toplevel eval loop.                 */
  1628. /*                                    */
  1629. /* **************************************************************** */
  1630.  
  1631. #define MENU_HEADER "\n* Menu:"
  1632. #define MENU_ID "\n* "
  1633. #define FOOTNOTE_HEADER "*Note"
  1634.  
  1635. /* Number of items that the current menu has. */
  1636. int the_menu_size = 0;
  1637.  
  1638. /* The node that last made a menus completion list. */
  1639. char *menus_nodename[NODENAME_LEN];
  1640. char *menus_filename[NODENAME_LEN];
  1641.  
  1642. boolean window_bashed = false;
  1643.  
  1644. /* The default prompt string for the Follow Reference command. */
  1645. char *visible_footnote = (char *)NULL;
  1646. toploop ()
  1647. {
  1648.   boolean done = false;
  1649.   boolean inhibit_display = false;
  1650.   int command = '\0', last_command;
  1651.   int last_pointer, count, new_ypos, last_pagetop;
  1652.   char nodename[NODENAME_LEN];
  1653.  
  1654.   new_ypos = last_pagetop = -1;
  1655.  
  1656.   while (!done)
  1657.     {
  1658.       if ((!inhibit_display || window_bashed) && pagetop != last_pagetop)
  1659.     display_page ();
  1660.  
  1661.       inhibit_display = window_bashed = false;
  1662.       last_pagetop = pagetop;
  1663.  
  1664.       nodename[0] = '\0';    /* Don't display old text in input line. */
  1665.  
  1666.       goto_xy (echo_area.left, echo_area.top);
  1667.       last_command = command;
  1668.       command = blink_cursor ();
  1669.       clear_echo_area ();
  1670.  
  1671.       if (command == EOF)
  1672.     {
  1673.       done = true;
  1674.       continue;
  1675.     }
  1676.       command = to_upper (command);
  1677.  
  1678.       switch (command)
  1679.     {
  1680.  
  1681.     case 'D':
  1682.       get_node ((char *) NULL, "(dir)Top", false);
  1683.       break;
  1684.  
  1685.     case 'H':
  1686.       if ((the_window.bottom - the_window.top) < 24)
  1687.         get_node ((char *) NULL, "(info)Help-Small-Screen", false);
  1688.       else
  1689.         get_node ((char *) NULL, "(info)Help", false);
  1690.       break;
  1691.  
  1692.     case 'N':
  1693.       if (!next_node ())
  1694.         {
  1695.           display_error ("No NEXT for this node!");
  1696.           inhibit_display = true;
  1697.         }
  1698.       break;
  1699.  
  1700.     case 'P':
  1701.       if (!prev_node ())
  1702.         {
  1703.           display_error ("No PREV for this node!");
  1704.           inhibit_display = true;
  1705.         }
  1706.       break;
  1707.  
  1708.     case 'U':
  1709.       {
  1710.         int savetop = pagetop;
  1711.  
  1712.         if (!up_node ())
  1713.           {
  1714.         display_error ("No UP for this node!");
  1715.         inhibit_display = true;
  1716.         pagetop = savetop;
  1717.           }
  1718.         break;
  1719.       }
  1720.  
  1721.     case 'M':
  1722.       if (!build_menu ())
  1723.         {
  1724.           display_error ("No menu in this node!");
  1725.           inhibit_display = true;
  1726.           break;
  1727.         }
  1728.  
  1729.       if (!readline ("Menu item: ", nodename, NODENAME_LEN, true))
  1730.         {
  1731.           clear_echo_area ();
  1732.           inhibit_display = true;
  1733.           break;
  1734.         }
  1735.  
  1736.       I_goto_xy (echo_area.left, echo_area.top);
  1737.       if (!find_menu_node (nodename, nodename))
  1738.         {
  1739.           display_error ("\"%s\" is not a menu item!", nodename);
  1740.           inhibit_display = true;
  1741.           break;
  1742.         }
  1743.  
  1744.       if (get_node ((char *) NULL, nodename, false))
  1745.         clear_echo_area ();
  1746.       break;
  1747.  
  1748.  
  1749.     case 'F':
  1750.       {
  1751.         char footnote[NODENAME_LEN];
  1752.         if (!build_notes ())
  1753.           {
  1754.         display_error ("No cross-references in this node!");
  1755.         inhibit_display = true;
  1756.         break;
  1757.           }
  1758.         strcpy (footnote, visible_footnote);
  1759.         if (!readline ("Follow reference: ", footnote, NODENAME_LEN, true))
  1760.           {
  1761.         inhibit_display = true;
  1762.         break;
  1763.           }
  1764.  
  1765.         I_goto_xy (echo_area.left, echo_area.top);
  1766.         if (!find_note_node (footnote, nodename))
  1767.           {
  1768.         display_error ("\"%s\" is not a cross-reference in this node!",
  1769.                    footnote);
  1770.         inhibit_display = true;
  1771.         break;
  1772.           }
  1773.  
  1774.         if (get_node ((char *)NULL, nodename, false))
  1775.           clear_echo_area ();
  1776.         break;
  1777.       }
  1778.  
  1779.     case 'L':
  1780.       {
  1781.         char filename[FILENAME_LEN], nodename[NODENAME_LEN];
  1782.         int ptop, ntop;
  1783.         if (pop_node (filename, nodename, &ntop, &ptop) &&
  1784.         get_node (filename, nodename, true))
  1785.           {
  1786.         pagetop = ptop;
  1787.           }
  1788.         else
  1789.           inhibit_display = true;
  1790.         break;
  1791.       }
  1792.  
  1793.     case SPACE:
  1794.       if (!next_page ())
  1795.         {
  1796.           display_error ("At last page of this node now!");
  1797.           inhibit_display = true;
  1798.         }
  1799.       break;
  1800.  
  1801.     case DELETE:
  1802.       if (!prev_page ())
  1803.         {
  1804.           display_error ("At first page of this node now!");
  1805.           inhibit_display = true;
  1806.         }
  1807.       break;
  1808.  
  1809.     case 'B':
  1810.       if (pagetop == nodetop)
  1811.         {
  1812.           display_error ("Already at beginning of this node!");
  1813.           inhibit_display = true;
  1814.         }
  1815.       else
  1816.         pagetop = nodetop;
  1817.       break;
  1818.  
  1819.       /* I don't want to do this this way, but the documentation
  1820.          clearly states that '6' doesn't work.  It states this for a
  1821.          reason, and ours is not to wonder why... */
  1822.     case '1':
  1823.     case '2':
  1824.     case '3':
  1825.     case '4':
  1826.     case '5':
  1827.       {
  1828.         int item = command - '0';
  1829.  
  1830.         if (!build_menu ())
  1831.           {
  1832.         display_error ("No menu in this node!");
  1833.         inhibit_display = true;
  1834.         break;
  1835.           }
  1836.  
  1837.         if (item > the_menu_size)
  1838.           {
  1839.         display_error ("There are only %d items in the menu!",
  1840.                    the_menu_size);
  1841.         inhibit_display = true;
  1842.         break;
  1843.           }
  1844.  
  1845.         if (!get_menu (item))
  1846.           inhibit_display = true;
  1847.  
  1848.       }
  1849.       break;
  1850.  
  1851.     case 'G':
  1852.       if (!readline ("Goto node: ", nodename, NODENAME_LEN, false))
  1853.         {
  1854.           inhibit_display = true;
  1855.           break;
  1856.         }
  1857.       if (get_node ((char *) NULL, nodename, false))
  1858.         clear_echo_area ();
  1859.  
  1860.       break;
  1861.  
  1862.     case 'S':
  1863.       {
  1864.         /* Search from the starting position forward for a string.
  1865.            Select the node containing the desired string.  Put the
  1866.            top of the page screen_lines / 2 lines behind it, but not
  1867.            before nodetop.
  1868.  
  1869.             We deal with searching sets of indirect file by searching
  1870.             through them all (as remembered in the indirect_list),
  1871.             wrapping when we reach the end. The initial file and position
  1872.             are needed in order to know when we have searched the whole file
  1873.             and we accordingly save them in starting_filename and
  1874.             starting_position */
  1875.  
  1876.         extern int info_buffer_len;
  1877.         int pointer, temp;
  1878.          char prompt[21 + NODENAME_LEN + 1];
  1879.          static char *starting_filename = NULL;
  1880.          static int starting_position;
  1881.          static int search_start;    /* where the search has got to */
  1882.          static char search_string[NODENAME_LEN] = "";
  1883.         static boolean wrap_search = false; /* should search wrap round? */
  1884.   
  1885.          sprintf (prompt, "Search for string [%s]: ", search_string);
  1886.  
  1887.          if (!readline (prompt , nodename, NODENAME_LEN, false))
  1888.           {
  1889.         inhibit_display = true;
  1890.         break;
  1891.           }
  1892.  
  1893.         if (last_command != 'S' ||
  1894.         (*nodename != '\0' && strcmp(nodename,search_string) != 0))
  1895.           {
  1896.         if (starting_filename != NULL)
  1897.           free (starting_filename);
  1898.  
  1899.         starting_filename = savestring (last_loaded_info_file);
  1900.  
  1901.         if (next_info_file (starting_filename, false) == NULL)
  1902.           {
  1903.             inhibit_display = true;
  1904.             break;
  1905.           }
  1906.  
  1907.         starting_position = search_start = pagetop;
  1908.         wrap_search = false;
  1909.         if (*nodename != '\0')
  1910.           strcpy (search_string, nodename);
  1911.           }
  1912.  
  1913.         I_goto_xy (echo_area.left, echo_area.top);
  1914.  
  1915.         if (indirect)
  1916.           {
  1917.         /* Put the indirect search right here. */
  1918.         display_error
  1919.           ("This is an indirect file, and I can't search these yet!");
  1920.         break;
  1921.           }
  1922.         else
  1923.           {
  1924.          static int pushed = 0; /* how many files are pushed? */
  1925.          boolean found_string = false; /* did we find our string? */
  1926.  
  1927.          if (wrap_search)
  1928.           {
  1929.             push_filestack (next_info_file ((char *)NULL, true),
  1930.                     false);
  1931.             pushed++;
  1932.             search_start = 0;
  1933.           }
  1934.  
  1935.          for (;;)
  1936.           {
  1937.             set_search_constraints (info_file, info_buffer_len);
  1938.             pointer = search_forward (search_string, search_start);
  1939.  
  1940.             if (pointer != -1)
  1941.               {
  1942.             found_string = true;
  1943.             break;
  1944.               }
  1945.             else
  1946.               {
  1947.             char *next_file = next_info_file ((char *)NULL, false);
  1948.  
  1949.             if (next_file != NULL)
  1950.               {
  1951.                 if (pushed)
  1952.                   {
  1953.                 pop_filestack ();
  1954.                 pushed--;
  1955.                   }
  1956.  
  1957.                 push_filestack (next_file, false);
  1958.                 pushed++;
  1959.                 search_start = 0;
  1960.                 continue;
  1961.               }
  1962.  
  1963.             if (wrap_search)
  1964.               {
  1965.                 display_error
  1966.                   ("\"%s\" not found! Try another file.",
  1967.                    search_string);
  1968.  
  1969.                 inhibit_display = true;
  1970.                 wrap_search = false;
  1971.  
  1972.                 if (pushed)
  1973.                   {
  1974.                 pop_filestack ();
  1975.                 pushed--;
  1976.                   }
  1977.                 break;
  1978.               }
  1979.             else
  1980.               {
  1981.                 display_error ("end of file");
  1982.                 inhibit_display = true;
  1983.                 wrap_search = true;
  1984.                 if (pushed)
  1985.                   {
  1986.                 pop_filestack ();
  1987.                 pushed--;
  1988.                   }
  1989.                 break;
  1990.               }
  1991.               }
  1992.           }
  1993.  
  1994.          if (pushed)
  1995.           {
  1996.             swap_filestack ();
  1997.             pop_filestack ();
  1998.             pushed--;
  1999.           }
  2000.  
  2001.          if (!found_string)
  2002.           break;
  2003.          
  2004.          wrap_search = false;
  2005.          temp = search_backward (start_of_node_string, pointer);
  2006.  
  2007.          if (temp == -1)
  2008.            temp = search_forward (start_of_node_string, pointer);
  2009.  
  2010.          if (temp == -1)
  2011.              {
  2012.              brians_error ();
  2013.                break;
  2014.              }
  2015.          
  2016.          search_start = forward_lines (1, pointer + 1);
  2017.          pointer = forward_lines (1, temp);
  2018.          if (!extract_field ("Node:", nodename, pointer))
  2019.             {
  2020.              display_error
  2021.               ("There doesn't appear to be a nodename for this node.");
  2022.              get_node ((char *)NULL, "*", false);
  2023.              pagetop = pointer;
  2024.               break;
  2025.             }
  2026.          
  2027.          if (get_node ((char *) NULL, nodename, false))
  2028.            clear_echo_area ();
  2029.          
  2030.          /* Make the string that the user wanted be visible, and
  2031.             centered in the screen. */
  2032.          {
  2033.            pointer = back_lines
  2034.             ((the_window.bottom - the_window.top) / 2, search_start);
  2035.  
  2036.            if (pointer < nodetop)
  2037.              pointer = nodetop;
  2038.            
  2039.            pagetop = pointer;
  2040.          }
  2041.            break;
  2042.           }
  2043.          }
  2044.  
  2045.     case CTRL ('H'):
  2046.     case '?':
  2047.       help_use_info ();
  2048.       last_pagetop = -1;
  2049.       break;
  2050.  
  2051.     case 'Q':
  2052.       done = true;
  2053.       break;
  2054.  
  2055.     case CTRL ('L'):    /* Control-l is redisplay */
  2056.       break;
  2057.  
  2058.     case '(':    /* You *must* be trying to type a complete nodename. */
  2059.       strcpy (nodename, "(");
  2060.       if (!readline ("Goto node: ", nodename, NODENAME_LEN, false))
  2061.         {
  2062.           inhibit_display = true;
  2063.           clear_echo_area ();
  2064.           break;
  2065.         }
  2066.       I_goto_xy (echo_area.left, echo_area.top);
  2067.       if (get_node ((char *) NULL, nodename, false))
  2068.         clear_echo_area ();
  2069.       break;
  2070.  
  2071.     case CTRL ('P'):
  2072.       /* Print the contents of this node on the default printer.  We
  2073.          would like to let the user specify the printer, but we don't
  2074.          want to ask her each time which printer to use.  Besides, he
  2075.          might not know, which is why it (the user) is in the need of
  2076.          Info. */
  2077.       {
  2078.         char *tempname = make_temp_filename (current_info_node);
  2079.         if (dump_current_node (tempname) == 0 &&
  2080.         printfile (tempname) == 0 &&
  2081.         deletefile (tempname) == 0)
  2082.           {
  2083.         display_error ("Printed node.  Go pick up your output.\n");
  2084.           }
  2085.         inhibit_display = true;
  2086.         free (tempname);
  2087.       }
  2088.       break;
  2089.  
  2090.     default:
  2091.       inhibit_display = true;
  2092.       display_error ("Unknown command! Press '?' for help.");
  2093.  
  2094.     }
  2095.     }
  2096. }
  2097.  
  2098. /* Return the screen column width that the line from START to END
  2099.    requires to display. */
  2100. line_length (start, end)
  2101.      int start, end;
  2102. {
  2103.   int count = 0;
  2104.  
  2105.   while (start < end)
  2106.     {
  2107.       if (info_file[start] == '\t')
  2108.     count += 7 - (count % 8);
  2109.       else if (CTRL_P (info_file[start]))
  2110.     count += 2;
  2111.       else
  2112.     count++;
  2113.  
  2114.       start++;
  2115.     }
  2116.  
  2117.   return (count);
  2118. }
  2119.  
  2120. /* Tell this person how to use Info. */
  2121. help_use_info ()
  2122. {
  2123.   open_typeout ();
  2124.   clear_screen ();
  2125.   print_string ("\n\
  2126.           Commands in Info\n\
  2127. \n\
  2128. h    Invoke the Info tutorial.\n\
  2129. \n\
  2130. Selecting other nodes:\n\
  2131. n    Move to the \"next\" node of this node.\n\
  2132. p    Move to the \"previous\" node of this node.\n\
  2133. u    Move \"up\" from this node.\n\
  2134. m    Pick menu item specified by name.\n\
  2135.     Picking a menu item causes another node to be selected.\n\
  2136. f    Follow a cross reference.  Reads name of reference.\n\
  2137. l    Move to the last node you were at.\n\
  2138. \n\
  2139. Moving within a node:\n\
  2140. Space    Scroll forward a page.\n\
  2141. DEL    Scroll backward a page.\n\
  2142. b    Go to the beginning of this node.\n\
  2143. \n\
  2144. Advanced commands:\n\
  2145. q    Quit Info.\n\
  2146. 1    Pick first item in node's menu.\n\
  2147. 2 - 5   Pick second ... fifth item in node's menu.\n\
  2148. g    Move to node specified by name.\n\
  2149.     You may include a filename as well, as (FILENAME)NODENAME.\n\
  2150. s    Search through this Info file for a specified string,\n\
  2151.     and select the node in which the next occurrence is found.\n\
  2152. Ctl-p   Print the contents of this node using `%s'.\n\
  2153. \n\
  2154. Done.\n\n",print_command);
  2155.   close_typeout ();
  2156. }
  2157.  
  2158. /* Move to the node specified in the NEXT field. */
  2159. boolean
  2160. next_node ()
  2161. {
  2162.   char nodename[NODENAME_LEN];
  2163.  
  2164.   if (!extract_field ("Next:", nodename, nodetop))
  2165.     return (false);
  2166.   return (get_node ((char *) NULL, nodename, false));
  2167. }
  2168.  
  2169. /* Move to the node specified in the PREVIOUS field. */
  2170. boolean
  2171. prev_node ()
  2172. {
  2173.   char nodename[NODENAME_LEN];
  2174.  
  2175.   if (!extract_field ("Previous:", nodename, nodetop)
  2176.       && !extract_field ("Prev:", nodename, nodetop))
  2177.     return (false);
  2178.   return (get_node ((char *) NULL, nodename, false));
  2179. }
  2180.  
  2181. /* Move to the node specified in the UP field. */
  2182. boolean
  2183. up_node ()
  2184. {
  2185.   char nodename[NODENAME_LEN];
  2186.  
  2187.   if (!extract_field ("Up:", nodename, nodetop))
  2188.     return (false);
  2189.   return (get_node ((char *) NULL, nodename, false));
  2190. }
  2191.  
  2192. /* Build a completion list of menuname/nodename for each
  2193.    line in this node that is a menu item. */
  2194. boolean
  2195. build_menu ()
  2196. {
  2197.   int pointer = nodetop;
  2198.   char menuname[NODENAME_LEN];
  2199.   char nodename[NODENAME_LEN];
  2200.  
  2201.   if (strcmp (menus_nodename, current_info_node) == 0 &&
  2202.       strcmp (menus_filename, current_info_file) == 0)
  2203.     return (the_menu_size != 0);
  2204.  
  2205.   strcpy (menus_nodename, current_info_node);
  2206.   strcpy (menus_filename, current_info_file);
  2207.   free_completion_list ();
  2208.  
  2209.   set_search_constraints (info_file, nodebot);
  2210.   if ((pointer = search_forward (MENU_HEADER, nodetop)) < 0)
  2211.     return (false);
  2212.  
  2213.   /* There is a menu here.  Look for members of it. */
  2214.   pointer += strlen (MENU_HEADER);
  2215.  
  2216.   while (true)
  2217.     {
  2218.       int idx;
  2219.  
  2220.       pointer = search_forward (MENU_ID, pointer);
  2221.       if (pointer < 0)
  2222.     break;            /* no more menus in this node. */
  2223.       pointer = (skip_whitespace (pointer + strlen (MENU_ID)));
  2224.       idx = 0;
  2225.       while ((menuname[idx] = info_file[pointer]) && menuname[idx] != ':')
  2226.     {
  2227.       idx++, pointer++;
  2228.     }
  2229.       menuname[idx] = '\0';
  2230.       pointer++;
  2231.       if (info_file[pointer] == ':')
  2232.     {
  2233.       strcpy (nodename, menuname);
  2234.     }
  2235.       else
  2236.     {
  2237.       pointer = skip_whitespace (pointer);
  2238.       idx = 0;
  2239.       while ((nodename[idx] = info_file[pointer]) &&
  2240.          nodename[idx] != '\t' &&
  2241.          nodename[idx] != '.' &&
  2242.          nodename[idx] != ',')
  2243.         {
  2244.           idx++, pointer++;
  2245.         }
  2246.       nodename[idx] = '\0';
  2247.     }
  2248.       add_completion (menuname, nodename);
  2249.       the_menu_size++;
  2250.     }
  2251.   if (the_menu_size)
  2252.     completion_list = reverse_list (completion_list);
  2253.   return (the_menu_size != 0);
  2254. }
  2255.  
  2256. /* Select ITEMth item from the list built by build_menu. */
  2257. boolean
  2258. get_menu (item)
  2259.      int item;
  2260. {
  2261.   if (!build_menu ())
  2262.     return (false);
  2263.   if (item > the_menu_size)
  2264.     return (false);
  2265.   else
  2266.     {
  2267.       COMP_ENTRY *temp = completion_list;
  2268.       while (--item && temp)
  2269.     temp = temp->next;
  2270.       return (get_node ((char *) NULL, temp->data, false));
  2271.     }
  2272. }
  2273.  
  2274. /* Scan through the ?already? built menu list looking
  2275.    for STRING.  If you find it, put the corresponding nodes
  2276.    name in NODENAME. */
  2277. boolean
  2278. find_menu_node (string, nodename)
  2279.      char *string, *nodename;
  2280. {
  2281.   return (scan_list (string, nodename));
  2282. }
  2283.  
  2284. /* The work part of find_menu_node and find_note_node. */
  2285. boolean
  2286. scan_list (string, nodename)
  2287.      char *string, *nodename;
  2288. {
  2289.   COMP_ENTRY *temp = completion_list;
  2290.  
  2291.   while (temp)
  2292.     {
  2293.       if (strnicmp (string, temp->identifier, strlen (string)) == 0)
  2294.     {
  2295.       strcpy (nodename, temp->data);
  2296.       return (true);
  2297.     }
  2298.       temp = temp->next;
  2299.     }
  2300.   return (false);
  2301. }
  2302.  
  2303. /* Remove <CR> and whitespace from string, replacing them with
  2304.    only one space.  Exception:  <CR> at end of string disappears. */
  2305. clean_up (string)
  2306.      char *string;
  2307. {
  2308.   char *to = string;
  2309.  
  2310.   while (*to = *string++)
  2311.     {
  2312.       if (*to == '\n' || *to == ' ')
  2313.     {
  2314.       *to = ' ';
  2315.  
  2316.       while (*string == ' ' || *string == '\t')
  2317.         string++;
  2318.     }
  2319.     to++;
  2320.     }
  2321. }
  2322.  
  2323. /* Find a reference to "*Note".  Return the offset of the start
  2324.    of that reference, or -1. */
  2325. find_footnote_ref (from)
  2326.      int from;
  2327. {
  2328.   while (true)
  2329.     {
  2330.       from = search_forward (FOOTNOTE_HEADER, from);
  2331.       if (from < 0)
  2332.     return (from);
  2333.       else
  2334.     from += strlen (FOOTNOTE_HEADER);
  2335.       if (info_file[from] == ' ' ||
  2336.       info_file[from] == '\n' ||
  2337.       info_file[from] == '\t')
  2338.     return (from);
  2339.     }
  2340. }
  2341.  
  2342. /* Build an array of (footnote.nodename) for each footnote in this node. */
  2343. boolean
  2344. build_notes ()
  2345. {
  2346.   int pointer;
  2347.   char notename[NODENAME_LEN];
  2348.   char nodename[NODENAME_LEN];
  2349.  
  2350.   set_search_constraints (info_file, nodebot);
  2351.  
  2352.   if ((find_footnote_ref (nodetop)) < 0)
  2353.     return (false);
  2354.   pointer = nodetop;
  2355.  
  2356.   menus_filename[0] = menus_nodename[0] = '\0';
  2357.   visible_footnote = "";
  2358.   free_completion_list ();
  2359.  
  2360.   while (true)
  2361.     {
  2362.       int idx;
  2363.  
  2364.       pointer = find_footnote_ref (pointer);
  2365.       if (pointer < 0)
  2366.     break;            /* no more footnotes in this node. */
  2367.  
  2368.       pointer = skip_whitespace_and_cr (pointer);
  2369.       idx = 0;
  2370.  
  2371.       while ((notename[idx] = info_file[pointer]) && notename[idx] != ':')
  2372.     {
  2373.       idx++, pointer++;
  2374.     }
  2375.  
  2376.       notename[idx] = '\0';
  2377.       clean_up (notename);
  2378.       pointer++;
  2379.       if (info_file[pointer] == ':')
  2380.     {
  2381.       strcpy (nodename, notename);
  2382.     }
  2383.       else
  2384.     {
  2385.       int in_parens = 0;
  2386.  
  2387.       pointer = skip_whitespace (pointer);
  2388.       idx = 0;
  2389.  
  2390.       while ((nodename[idx] = info_file[pointer]) &&
  2391.          (in_parens ||
  2392.           (nodename[idx] != '\t' &&
  2393.            nodename[idx] != '.' &&
  2394.            nodename[idx] != ',')))
  2395.         {
  2396.           if (nodename[idx] == '(')
  2397.         in_parens++;
  2398.           else if (nodename[idx] == ')')
  2399.         in_parens--;
  2400.  
  2401.           idx++, pointer++;
  2402.         }
  2403.       nodename[idx] = '\0';
  2404.       clean_up (nodename);
  2405.     }
  2406.       /* Add the notename/nodename to the list. */
  2407.       add_completion (notename, nodename);
  2408.       the_menu_size++;
  2409.  
  2410.       /* Remember this identifier as the default if it is the first one in the
  2411.          page. */
  2412.       if (!(*visible_footnote) &&
  2413.       pointer > pagetop &&
  2414.       pointer < forward_lines (the_window.bottom - the_window.top, pointer))
  2415.     visible_footnote = completion_list->identifier;
  2416.     }
  2417.   if (the_menu_size)
  2418.     completion_list = reverse_list (completion_list);
  2419.   return (the_menu_size != 0);
  2420. }
  2421.  
  2422. /* Scan through the ?already? built footnote list looking
  2423.    for STRING.  If found, place the corresponding node name
  2424.    in NODENAME. */
  2425. boolean
  2426. find_note_node (string, nodename)
  2427.      char *string, *nodename;
  2428. {
  2429.   return (scan_list (string, nodename));
  2430. }
  2431.  
  2432. /* **************************************************************** */
  2433. /*                                    */
  2434. /*            Page Display                     */
  2435. /*                                    */
  2436. /* **************************************************************** */
  2437.  
  2438.  
  2439. /* The display functions for GNU Info. */
  2440.  
  2441. int display_ch, display_cv;
  2442. int display_point;
  2443.  
  2444. /* Display the current page from pagetop down to the bottom of the
  2445.    page or the bottom of the node, whichever comes first. */
  2446. display_page ()
  2447. {
  2448.   display_point = pagetop;
  2449.   display_ch = the_window.left;
  2450.   display_cv = the_window.top;
  2451.   generic_page_display ();
  2452. }
  2453.  
  2454. /* Print the page from display_point to bottom of node, or window,
  2455.    whichever comes first.  Start printing at display_ch, display_cv. */
  2456. generic_page_display ()
  2457. {
  2458.   int done_with_display = 0;
  2459.   int character;
  2460.  
  2461.   goto_xy (display_ch, display_cv);
  2462.  
  2463.   while (!done_with_display)
  2464.     {
  2465.       if (display_point == nodebot)
  2466.     {
  2467.       clear_eop ();
  2468.       goto display_finish;
  2469.     }
  2470.  
  2471.       character = info_file[display_point];
  2472.  
  2473.       if ((display_width (character, the_window.ch) + the_window.ch)
  2474.       >= the_window.right)
  2475.     display_carefully (character);
  2476.       else
  2477.     charout (character);
  2478.  
  2479.       if ((the_window.cv >= the_window.bottom)
  2480.       || (the_window.cv == the_window.top
  2481.           && the_window.ch == the_window.left))
  2482.     {
  2483.     display_finish:
  2484.       make_modeline ();
  2485.       done_with_display++;
  2486.       continue;
  2487.     }
  2488.       else
  2489.     display_point++;
  2490.     }
  2491.   fflush (stdout);
  2492. }
  2493.  
  2494. /* Display character carefully, ensuring that no scrolling takes
  2495.    place, even in the case of funky control characters. */
  2496. display_carefully (character)
  2497.      int character;
  2498. {
  2499.   if (CTRL_P (character))
  2500.     {
  2501.       switch (character)
  2502.     {
  2503.     case RETURN:
  2504.     case NEWLINE:
  2505.     case TAB:
  2506.       clear_eol ();
  2507.       advance (the_window.right - the_window.ch);
  2508.       break;
  2509.     default:
  2510.       charout ('^');
  2511.       if (the_window.cv == the_window.bottom)
  2512.         break;
  2513.       else
  2514.         charout (UNCTRL (character));
  2515.     }
  2516.     }
  2517.   else
  2518.     charout (character);
  2519. }
  2520.  
  2521. /* Move to the next page in this node.  Return FALSE if
  2522.    we can't get to the next page. */
  2523. boolean
  2524. next_page ()
  2525. {
  2526.   int pointer;
  2527.  
  2528.   pointer =
  2529.     forward_lines ((the_window.bottom - the_window.top) - 2, pagetop);
  2530.  
  2531.   if (pointer >= nodebot)
  2532.     return (false);
  2533.  
  2534.   /* Hack for screens smaller than displayed line width. */
  2535.   if (pointer > display_point)
  2536.     {
  2537.       pointer = display_point;
  2538.       back_lines (1);
  2539.     }
  2540.   pagetop = pointer;
  2541.   return (true);
  2542. }
  2543.  
  2544. /* Move to the previous page in this node.  Return FALSE if
  2545.    there is no previous page. */
  2546. boolean
  2547. prev_page ()
  2548. {
  2549.   int pointer =
  2550.   back_lines ((the_window.bottom - the_window.top) - 2, pagetop);
  2551.  
  2552.   if (pagetop == nodetop)
  2553.     return (false);
  2554.  
  2555.   if (pointer < nodetop)
  2556.     pointer = nodetop;
  2557.  
  2558.   pagetop = pointer;
  2559.   return (true);
  2560. }
  2561.  
  2562.  
  2563. /* **************************************************************** */
  2564. /*                                    */
  2565. /*            Utility Functions                */
  2566. /*                                    */
  2567. /* **************************************************************** */
  2568.  
  2569. char *search_buffer;        /* area in ram to scan through. */
  2570. int buffer_bottom;        /* Length of this area. */
  2571.  
  2572. /* Set the global variables that all of these routines use. */
  2573. set_search_constraints (buffer, extent)
  2574.      char *buffer;
  2575.      int extent;
  2576. {
  2577.   search_buffer = buffer;
  2578.   buffer_bottom = extent;
  2579. }
  2580.  
  2581. /* Move back to the start of this line. */
  2582. to_beg_line (from)
  2583.      int from;
  2584. {
  2585.   while (from && search_buffer[from - 1] != '\n')
  2586.     from--;
  2587.   return (from);
  2588. }
  2589.  
  2590. /* Move forward to the end of this line. */
  2591. to_end_line (from)
  2592. {
  2593.   while (from < buffer_bottom && search_buffer[from] != '\n')
  2594.     from++;
  2595.   return (from);
  2596. }
  2597.  
  2598. /* Move back count lines in search_buffer starting at starting_pos.
  2599.    Returns the start of that line. */
  2600. back_lines (count, starting_pos)
  2601.      int count, starting_pos;
  2602. {
  2603.   starting_pos = to_beg_line (starting_pos);
  2604.   while (starting_pos && count)
  2605.     {
  2606.       starting_pos = to_beg_line (starting_pos - 1);
  2607.       count--;
  2608.     }
  2609.   return (starting_pos);
  2610. }
  2611.  
  2612. /* Move forward count lines starting at starting_pos.
  2613.    Returns the start of that line. */
  2614. forward_lines (count, starting_pos)
  2615.      int count, starting_pos;
  2616. {
  2617.   starting_pos = to_end_line (starting_pos);
  2618.   while (starting_pos < buffer_bottom && count)
  2619.     {
  2620.       starting_pos = to_end_line (starting_pos + 1);
  2621.       count--;
  2622.     }
  2623.   return (to_beg_line (starting_pos));
  2624. }
  2625.  
  2626. /* Search for STRING in SEARCH_BUFFER starting at STARTING_POS.
  2627.    Return the location of the string, or -1 if not found. */
  2628. search_forward (string, starting_pos)
  2629.      char *string;
  2630.      int starting_pos;
  2631. {
  2632.   register int c, i, len;
  2633.   register char *buff, *end;
  2634.   char *alternate;
  2635.  
  2636.  
  2637.   /* We match characters in SEARCH_BUFFER against STRING and ALTERNATE.
  2638.      ALTERNATE is a case reversed version of STRING; this is cheaper than
  2639.      case folding each character before comparison. */
  2640.  
  2641.   /* Build the alternate string. */
  2642.   alternate = savestring (string);
  2643.   len = strlen (string);
  2644.  
  2645.   for (i = 0; i < len; i++)
  2646.     {
  2647.       c = alternate[i];
  2648.  
  2649.       if (c >= 'a' && c <= 'z')
  2650.     alternate[i] = c - 32;
  2651.       else if (c >= 'A' && c <= 'Z')
  2652.     alternate[i] = c + 32;
  2653.     }
  2654.  
  2655.   buff = search_buffer + starting_pos;
  2656.   end = search_buffer + buffer_bottom + 1;
  2657.  
  2658.   while (buff < end)
  2659.     {
  2660.       for (i = 0; i < len; i++)
  2661.     {
  2662.       c = buff[i];
  2663.  
  2664.       if (c != string[i] && c != alternate[i])
  2665.         break;
  2666.     }
  2667.  
  2668.       if (!string[i])
  2669.     {
  2670.       free (alternate);
  2671.       return (buff - search_buffer);
  2672.     }
  2673.  
  2674.       buff++;
  2675.     }
  2676.  
  2677.   free (alternate);
  2678.   return (-1);
  2679. }
  2680.  
  2681. /* Search for STRING in SEARCH_BUFFER starting at STARTING_POS.
  2682.    Return the location of the string, or -1 if not found. */
  2683. search_backward (string, starting_pos)
  2684.      char *string;
  2685.      int starting_pos;
  2686. {
  2687.   int len = strlen (string);
  2688.   while (starting_pos - len > -1)
  2689.     {
  2690.       if (strnicmp (search_buffer + (starting_pos - len), string, len) == 0)
  2691.     return (starting_pos - len);
  2692.       else
  2693.     starting_pos--;
  2694.     }
  2695.   return (-1);
  2696. }
  2697.  
  2698. /* Only search for STRING from POINTER to end of line.  Return offset
  2699.    of string, or -1 if not found. */
  2700. string_in_line (string, pointer)
  2701.      char *string;
  2702.      int pointer;
  2703. {
  2704.   int old_buffer_bottom = buffer_bottom;
  2705.  
  2706.   set_search_constraints (search_buffer, to_end_line (pointer));
  2707.   pointer = search_forward (string, pointer);
  2708.   buffer_bottom = old_buffer_bottom;
  2709.   return (pointer);
  2710. }
  2711.  
  2712. /* Skip whitespace characters at OFFSET in SEARCH_BUFFER.
  2713.    Return the next non-whitespace character or -1 if BUFFER_BOTTOM
  2714.    is reached. */
  2715. skip_whitespace (offset)
  2716.      int offset;
  2717. {
  2718.   int character;
  2719.  
  2720.   while (offset < buffer_bottom)
  2721.     {
  2722.       character = search_buffer[offset];
  2723.       if (character == ' ' || character == '\t')
  2724.     offset++;
  2725.       else
  2726.     return (offset);
  2727.     }
  2728.   return (-1);
  2729. }
  2730.  
  2731. /* Skip whitespace characters including <CR> at OFFSET in
  2732.    SEARCH_BUFFER.  Return the position of the next non-whitespace
  2733.    character, or -1 if BUFFER_BOTTOM is reached. */
  2734. skip_whitespace_and_cr (offset)
  2735.      int offset;
  2736. {
  2737.   while (true)
  2738.     {
  2739.       offset = skip_whitespace (offset);
  2740.       if (offset > 0 && search_buffer[offset] != '\n')
  2741.     return (offset);
  2742.       else
  2743.     offset++;
  2744.     }
  2745. }
  2746.  
  2747. /* Extract the node name part of the of the text after the FIELD.
  2748.    Place the node name into NODENAME.  Assume the line starts at
  2749.    OFFSET in SEARCH_BUFFER. */
  2750. boolean
  2751. extract_field (field_name, nodename, offset)
  2752.      char *field_name, *nodename;
  2753.      int offset;
  2754. {
  2755.   int temp, character;
  2756.  
  2757.   temp = string_in_line (field_name, offset);
  2758.   if (temp < 0)
  2759.     return (false);
  2760.  
  2761.   temp += strlen (field_name);
  2762.   temp = skip_whitespace (temp);
  2763.  
  2764.   /* Okay, place the following text into NODENAME. */
  2765.  
  2766.   while ((character = search_buffer[temp]) != ','
  2767.      && character != '\n'
  2768.      && character != '\t')
  2769.     {
  2770.       *nodename = character;
  2771.       nodename++;
  2772.       temp++;
  2773.     }
  2774.   *nodename = '\0';
  2775.   return (true);
  2776. }
  2777.  
  2778. /* Return true if pointer is exactly at string, else false. */
  2779. boolean
  2780. looking_at (string, pointer)
  2781.      char *string;
  2782.      int pointer;
  2783. {
  2784.   if (strnicmp (search_buffer + pointer, string, strlen (string)) == 0)
  2785.     return (true);
  2786.   else
  2787.     return (false);
  2788. }
  2789.  
  2790. /* File stack stuff. This is currently only used to push one file while
  2791.    searching indirect files, but we may as well write it in full
  2792.    generality. */
  2793. typedef struct filestack
  2794. {
  2795.   struct filestack *next;
  2796.   char filename[FILENAME_LEN];
  2797.   char current_filename[FILENAME_LEN];
  2798.   char *tag_table;
  2799.   char *info_file;
  2800.   int info_buffer_len;
  2801. } FILESTACK;
  2802.  
  2803. FILESTACK *filestack = NULL;
  2804.  
  2805. boolean
  2806. push_filestack (filename, remember_name)
  2807.      char *filename;
  2808.      int remember_name;
  2809. {
  2810.   FILESTACK *element = (FILESTACK *)xmalloc (sizeof (FILESTACK));
  2811.  
  2812.   element->next = filestack;
  2813.   filestack = element;
  2814.  
  2815.   strcpy (filestack->filename, last_loaded_info_file);
  2816.   strcpy (filestack->current_filename, current_info_file);
  2817.   filestack->tag_table = tag_table;
  2818.   filestack->info_file = info_file;
  2819.   filestack->info_buffer_len = info_buffer_len;
  2820.   
  2821.   *last_loaded_info_file = '\0';    /* force the file to be read */
  2822.   if (get_info_file (filename, remember_name))
  2823.     {
  2824.       return (true);
  2825.     }
  2826.   else
  2827.     {
  2828.       pop_filestack ();
  2829.       return (false);
  2830.     }
  2831. }
  2832.  
  2833. void
  2834. pop_filestack ()
  2835. {
  2836.   FILESTACK *temp;
  2837.    
  2838.   if (filestack == NULL)
  2839.     {
  2840.        fprintf (stderr , "File stack is empty and can't be popped\n");
  2841.        brians_error ();
  2842.        return;
  2843.     }
  2844.  
  2845.   free (info_file);
  2846.  
  2847.   strcpy (last_loaded_info_file, filestack->filename);
  2848.   strcpy (current_info_file, filestack->current_filename);
  2849.   tag_table = filestack->tag_table;
  2850.   info_file = filestack->info_file;
  2851.   info_buffer_len = filestack->info_buffer_len;
  2852.  
  2853.   temp = filestack;
  2854.   filestack = filestack->next;
  2855.   free ((char *)temp);
  2856. }
  2857.  
  2858. /* Swap the current info file with the bottom of the filestack */
  2859.  
  2860. void
  2861. swap_filestack ()
  2862. {
  2863.   char t_last_loaded_info_file[FILENAME_LEN];
  2864.   char t_current_info_file[FILENAME_LEN];
  2865.   char *t_tag_table;
  2866.   char *t_info_file;
  2867.   int t_info_buffer_len;
  2868.  
  2869.   if (filestack == NULL)
  2870.     {
  2871.        fprintf (stderr , "File stack is empty and can't be swapped\n");
  2872.        brians_error ();
  2873.        return;
  2874.     }
  2875.  
  2876.   strcpy (t_last_loaded_info_file, filestack->filename);
  2877.   strcpy (t_current_info_file, filestack->current_filename);
  2878.   t_tag_table = filestack->tag_table;
  2879.   t_info_file = info_file;
  2880.   t_info_buffer_len = info_buffer_len;
  2881.  
  2882.   strcpy (filestack->filename, last_loaded_info_file);
  2883.   strcpy (filestack->current_filename, current_info_file);
  2884.   filestack->tag_table = tag_table;
  2885.   filestack->info_file = info_file;
  2886.   filestack->info_buffer_len = info_buffer_len;
  2887.  
  2888.   strcpy (last_loaded_info_file, t_last_loaded_info_file);
  2889.   strcpy (current_info_file, t_current_info_file);
  2890.   tag_table = t_tag_table;
  2891.   info_file = t_info_file;
  2892.   info_buffer_len = t_info_buffer_len;
  2893. }
  2894.  
  2895. /* Now the node history stack */
  2896.  
  2897. extern NODEINFO *Info_History;
  2898.  
  2899. /* Save the current filename, nodename, and position on the history list.
  2900.    We prepend. */
  2901. boolean
  2902. push_node (filename, nodename, page_position, node_position)
  2903.      char *filename, *nodename;
  2904.      int page_position, node_position;
  2905. {
  2906.   NODEINFO *newnode = (NODEINFO *) xmalloc (sizeof (NODEINFO));
  2907.  
  2908.   newnode->next = Info_History;
  2909.  
  2910.   newnode->filename = (char *) xmalloc (strlen (filename) + 1);
  2911.   strcpy (newnode->filename, filename);
  2912.  
  2913.   newnode->nodename = (char *) xmalloc (strlen (nodename) + 1);
  2914.   strcpy (newnode->nodename, nodename);
  2915.  
  2916.   newnode->pagetop = page_position;
  2917.   newnode->nodetop = node_position;
  2918.  
  2919.   Info_History = newnode;
  2920.   return (true);
  2921. }
  2922.  
  2923. /* Pop one node from the node list, leaving the values in
  2924.    passed variables. */
  2925. boolean
  2926. pop_node (filename, nodename, nodetop, pagetop)
  2927.      char *filename, *nodename;
  2928.      int *nodetop, *pagetop;
  2929. {
  2930.   if (Info_History->next == (NODEINFO *) NULL)
  2931.     {
  2932.       display_error ("At beginning of history now!");
  2933.       return (false);
  2934.     }
  2935.   else
  2936.     {
  2937.       NODEINFO *releaser = Info_History;
  2938.  
  2939.     /* If the popped file is not the current file, then force
  2940.        the popped file to be loaded. */
  2941.       if (strcmp (Info_History->filename, last_loaded_info_file) != 0)
  2942.     last_loaded_info_file[0] = '\0';
  2943.  
  2944.       strcpy (filename, Info_History->filename);
  2945.       strcpy (nodename, Info_History->nodename);
  2946.       *pagetop = Info_History->pagetop;
  2947.       *nodetop = Info_History->nodetop;
  2948.       free (Info_History->nodename);
  2949.       free (Info_History->filename);
  2950.       Info_History = Info_History->next;
  2951.       free (releaser);
  2952.       return (true);
  2953.     }
  2954. }
  2955.  
  2956. /* Whoops, Unix doesn't have strnicmp. */
  2957.  
  2958. /* Compare at most COUNT characters from string1 to string2.  Case
  2959.    doesn't matter. */
  2960. strnicmp (string1, string2, count)
  2961.      char *string1, *string2;
  2962. {
  2963.   char ch1, ch2;
  2964.  
  2965.   while (count)
  2966.     {
  2967.       ch1 = *string1++;
  2968.       ch2 = *string2++;
  2969.       if (to_upper (ch1) == to_upper (ch2))
  2970.     count--;
  2971.       else
  2972.     break;
  2973.     }
  2974.   return (count);
  2975. }
  2976.  
  2977. /* Make the user type "Y" or "N". */
  2978. boolean 
  2979. get_y_or_n_p ()
  2980. {
  2981.   int character;
  2982.   print_string (" (Y or N)?");
  2983.   clear_eol ();
  2984.  
  2985. until_we_like_it:
  2986.  
  2987.   character = blink_cursor ();
  2988.   if (character == EOF)
  2989.     return (false);
  2990.   if (to_upper (character) == 'Y')
  2991.     {
  2992.       charout (character);
  2993.       return (true);
  2994.     }
  2995.  
  2996.   if (to_upper (character) == 'N')
  2997.     {
  2998.       charout (character);
  2999.       return (false);
  3000.     }
  3001.  
  3002.   if (character == ABORT_CHAR)
  3003.     {
  3004.       ding ();
  3005.       return (false);
  3006.     }
  3007.  
  3008.   goto until_we_like_it;
  3009. }
  3010.  
  3011. /* Move the cursor to the desired column in the window. */
  3012. indent_to (screen_column)
  3013.      int screen_column;
  3014. {
  3015.   int counter = screen_column - the_window.ch;
  3016.   if (counter > 0)
  3017.     {
  3018.       while (counter--)
  3019.     charout (' ');
  3020.     }
  3021.   else if (screen_column != 0)
  3022.     charout (' ');
  3023. }
  3024.  
  3025.  
  3026. /* **************************************************************** */
  3027. /*                                    */
  3028. /*            Error output/handling.                */
  3029. /*                                    */
  3030. /* **************************************************************** */
  3031.  
  3032. /* Display specific error from known file error table. */
  3033. file_error (file)
  3034.      char *file;
  3035. {
  3036.   extern int errno;
  3037.   extern int sys_nerr;
  3038.   extern char *sys_errlist[];
  3039.  
  3040.   if (errno < sys_nerr)
  3041.     display_error ("%s: %s", file, sys_errlist[errno]);
  3042.   else
  3043.     display_error ("%s: Unknown error %d", file, errno);
  3044. }
  3045.  
  3046. /* Display the error in the echo-area using format_string and args.
  3047.    This is a specialized interface to printf. */
  3048. display_error (format_string, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
  3049.      char *format_string;
  3050. {
  3051.   extern boolean terminal_inited_p;
  3052.   char output_buffer[1024];
  3053.  
  3054.   if (totally_inhibit_errors)
  3055.     return;
  3056.  
  3057.   sprintf (output_buffer, format_string, arg1, arg2, arg3, arg4,
  3058.        arg5, arg6, arg7, arg8);
  3059.   if (terminal_inited_p)
  3060.     {
  3061.       new_echo_area ();
  3062.       ding ();
  3063.       print_string (output_buffer);
  3064.       close_echo_area ();
  3065.     }
  3066.   else
  3067.     {
  3068.       fprintf (stderr, "%s\n", output_buffer);
  3069.       sleep(1);
  3070.     }
  3071. }
  3072.  
  3073.  
  3074. /* Tell everybody what a loser I am.  If you see this error,
  3075.    send me a bug report. */
  3076. brians_error ()
  3077. {
  3078.   display_error ("You are never supposed to see this error.\n");
  3079.   display_error ("Tell bfox@ai.mit.edu to fix this someday.\n");
  3080.   return (-1);
  3081. }
  3082.  
  3083. /* **************************************************************** */
  3084. /*                                    */
  3085. /*            Terminal IO, and Driver                */
  3086. /*                                    */
  3087. /* **************************************************************** */
  3088.  
  3089. /* The Unix termcap interface code. */
  3090.  
  3091. #define NO_ERROR 0
  3092. #define GENERIC_ERROR 1
  3093. #define NO_TERMINAL_DESCRIPTOR 2
  3094. #define OUT_OF_MEMORY 3
  3095. #define BAD_TERMINAL 4
  3096.  
  3097. #define FERROR(msg)    fprintf (stderr, msg); exit (GENERIC_ERROR)
  3098.  
  3099. extern int tgetnum (), tgetflag ();
  3100. extern char *tgetstr ();
  3101. extern char *tgoto ();
  3102.  
  3103. #define Certainly_enough_space 2048    /* page 3, Section 1.1, para 4 */
  3104.  
  3105. #ifdef UNIX
  3106. char termcap_buffer[Certainly_enough_space];
  3107. #else
  3108. #define termcap_buffer NULL
  3109. #endif
  3110.  
  3111. /* You CANNOT remove these next four vars.  TERMCAP needs them to operate. */
  3112. char PC;
  3113. char *BC;
  3114. char *UP;
  3115.  
  3116. /* A huge array of stuff to get from termcap initialization. */
  3117.  
  3118. #define tc_int 0
  3119. #define tc_char tc_int+1
  3120. #define tc_flag tc_char+1
  3121. #define tc_last tc_flag+1
  3122.  
  3123. typedef int flag;
  3124.  
  3125. /* First, the variables which this array refers to */
  3126.  
  3127. /* Capabilities */
  3128.  
  3129. int terminal_columns;        /* {tc_int, "co" */
  3130. int terminal_rows;        /* {tc_int, "li" */
  3131. flag terminal_is_generic;    /* {tc_flag,"gn" */
  3132.  
  3133.  /* Cursor Motion */
  3134.  
  3135. char *terminal_goto;        /* {tc_char,"cm" */
  3136. char *terminal_home;        /* {tc_char,"ho" */
  3137.  
  3138. char *terminal_cursor_left;    /* {tc_char,"le" */
  3139. char *terminal_cursor_right;    /* {tc_char,"nd" */
  3140. char *terminal_cursor_up;    /* {tc_char,"up" */
  3141. char *terminal_cursor_down;    /* {tc_char,"do" */
  3142.  
  3143. /* Screen Clearing */
  3144.  
  3145. char *terminal_clearpage;    /* {tc_char,"cl" */
  3146. char *terminal_clearEOP;    /* {tc_char,"cd" */
  3147. char *terminal_clearEOL;    /* {tc_char,"ce" */
  3148.  
  3149. /* "Standout" */
  3150. char *terminal_standout_begin;    /* {tc_char,"so" */
  3151. char *terminal_standout_end;    /* {tc_char,"se" */
  3152.  
  3153. /* Reverse Video */
  3154. char *terminal_inverse_begin;    /* {tc_char,"mr" */
  3155. char *terminal_end_attributes;    /* {tc_char,"me" */
  3156.  
  3157. /* Ding! */
  3158.  
  3159. char *terminal_ear_bell;    /* {tc_char,"bl" */
  3160.  
  3161. /* Terminal Initialization */
  3162.  
  3163. char *terminal_use_begin;    /* {tc_char,"ti" */
  3164. char *terminal_use_end;        /* {tc_char,"te" */
  3165.  
  3166. /* Padding Stuff */
  3167.  
  3168. char *terminal_padding;        /* {tc_char,"pc" */
  3169.  
  3170. /* Now the whopping big array */
  3171.  
  3172. typedef struct {
  3173.   char type;
  3174.   char *name;
  3175.   char *value;
  3176. } termcap_capability_struct;
  3177.  
  3178. termcap_capability_struct capabilities[] = {
  3179.  
  3180. /* Capabilities */
  3181.   
  3182.   {tc_int, "co", (char *) &terminal_columns},
  3183.   {tc_int, "li", (char *) &terminal_rows},
  3184.   {tc_flag, "gn", (char *) &terminal_is_generic},
  3185.  
  3186. /* Cursor Motion */
  3187.  
  3188.   {tc_char, "cm", (char *) &terminal_goto},
  3189.   {tc_char, "ho", (char *) &terminal_home},
  3190.  
  3191.   {tc_char, "le", (char *) &terminal_cursor_left},
  3192.   {tc_char, "nd", (char *) &terminal_cursor_right},
  3193.   {tc_char, "up", (char *) &terminal_cursor_up},
  3194.   {tc_char, "do", (char *) &terminal_cursor_down},
  3195.  
  3196. /* Screen Clearing */
  3197.   
  3198.   {tc_char, "cl", (char *) &terminal_clearpage},
  3199.   {tc_char, "cd", (char *) &terminal_clearEOP},
  3200.   {tc_char, "ce", (char *) &terminal_clearEOL},
  3201.   
  3202. /* "Standout" */
  3203.   {tc_char, "so", (char *) &terminal_standout_begin},
  3204.   {tc_char, "se", (char *) &terminal_standout_end},
  3205.  
  3206. /* Reverse Video */
  3207.   {tc_char, "mr", (char *) &terminal_inverse_begin},
  3208.   {tc_char, "me", (char *) &terminal_end_attributes},
  3209.  
  3210. /* Ding! */
  3211.  
  3212.   {tc_char, "bl", (char *) &terminal_ear_bell},
  3213.   
  3214. /* Terminal Initialization */
  3215.  
  3216.   {tc_char, "ti", (char *) &terminal_use_begin},
  3217.   {tc_char, "te", (char *) &terminal_use_end},
  3218.  
  3219. /* Padding Stuff */
  3220.   
  3221.   {tc_char, "pc", (char *) &terminal_padding},
  3222.  
  3223. /* Terminate this array with a var of type tc_last */
  3224.   {tc_last, NULL, NULL}
  3225.  
  3226. };
  3227.  
  3228. int terminal_opened_p = 0;
  3229.  
  3230. open_terminal_io ()
  3231. {
  3232.   int error;
  3233.  
  3234.   if (terminal_opened_p)
  3235.     return (NO_ERROR);
  3236.  
  3237.   if ((error = get_terminal_info ()) != NO_ERROR)
  3238.     return (error);
  3239.  
  3240.   if ((error = get_terminal_vars (capabilities)) != NO_ERROR)
  3241.     return (error);
  3242.  
  3243.   /* Now, make sure we have the capabilites that we need. */
  3244.   if (terminal_is_generic)
  3245.     return (BAD_TERMINAL);
  3246.  
  3247.   terminal_opened_p++;
  3248.   return (NO_ERROR);
  3249. }
  3250.  
  3251. get_terminal_info ()
  3252. {
  3253.   char temp_string_buffer[256];
  3254.   int result;
  3255.  
  3256.   char *terminal_name = getenv ("TERM");
  3257.  
  3258.   if (terminal_name == NULL || *terminal_name == 0
  3259.       || (strcmp (terminal_name, "dialup") == 0))
  3260.     {
  3261.       terminal_name = temp_string_buffer;
  3262.       printf ("\nTerminal Type:");
  3263.       fflush (stdout);
  3264.       fgets (terminal_name, 256, stdin);
  3265.       if (!(*terminal_name))
  3266.     return (NO_TERMINAL_DESCRIPTOR);
  3267.     }
  3268.  
  3269. /* #define VERBOSE_GET_TERMINAL 1 */
  3270. #ifdef VERBOSE_GET_TERMINAL
  3271.  
  3272. #define buffer_limit 256
  3273. #define opsys_termcap_filename "/etc/termcap"
  3274.  
  3275.   /* We hack question mark if that is what the user typed.  All this means
  3276.      is we read /etc/termcap, and prettily print out the names of terminals
  3277.      that we find. */
  3278.  
  3279.   if (terminal_name[0] == '?' && !terminal_name[1])
  3280.     {
  3281.       FILE *termcap_file;
  3282.       if ((termcap_file = fopen (opsys_termcap_filename, "r")) != NULL)
  3283.     {
  3284.       int result;
  3285.       char line_buffer[buffer_limit];
  3286.       int terminal_count = 0;
  3287.  
  3288.       while ((readline_termcap (termcap_file, line_buffer)) != EOF)
  3289.         {
  3290.           char first_char = *line_buffer;
  3291.           if (first_char == '#' || first_char == ' '
  3292.           || first_char == '\t' || first_char == '\n')
  3293.         ;
  3294.           else
  3295.         {
  3296.           /* Print the names the pretty way. */
  3297.           printf ("\n%s", line_buffer);    /* liar */
  3298.           terminal_count++;
  3299.         }
  3300.         }
  3301.       fclose (termcap_file);
  3302.  
  3303.       if (terminal_count)
  3304.         printf ("\n%d terminals listed.\n", terminal_count);
  3305.       else
  3306.         printf ("\nNo terminals were listed.  Brian's mistake.\n");
  3307.     }
  3308.       else
  3309.     {
  3310.       fprintf (stderr,
  3311.            "\nNo such system file as %s!\nWe lose badly.\n",
  3312.            opsys_termcap_filename);
  3313.       return (NO_TERMINAL_DESCRIPTOR);
  3314.     }
  3315.       return (get_terminal_info ());
  3316.     }
  3317. #endif /* VERBOSE_GET_TERMINAL */
  3318.  
  3319.   result = tgetent (termcap_buffer, terminal_name);
  3320.  
  3321.   if (!result)
  3322.     return (NO_TERMINAL_DESCRIPTOR);
  3323.   else
  3324.     return (NO_ERROR);
  3325. }
  3326.  
  3327. #ifdef VERBOSE_GET_TERMINAL
  3328. readline_termcap (stream, buffer)
  3329.      FILE *stream;
  3330.      char *buffer;
  3331. {
  3332.   int c;
  3333.   int buffer_index = 0;
  3334.  
  3335.   while ((c = getc (stream)) != EOF && c != '\n')
  3336.     {
  3337.       if (buffer_index != buffer_limit - 1)
  3338.     buffer[buffer_index++] = c;
  3339.     }
  3340.  
  3341.   buffer[buffer_index] = 0;
  3342.  
  3343.   if (c == EOF)
  3344.     return ((buffer_index) ? 0 : EOF);
  3345.   else
  3346.     return (0);
  3347. }
  3348. #endif /* VERBOSE_GET_TERMINAL */
  3349.  
  3350. /* For each element of "from_array", read the corresponding variable's
  3351.    value into the right place. */
  3352. get_terminal_vars (from_array)
  3353.      termcap_capability_struct from_array[];
  3354. {
  3355.   int i;
  3356.   register termcap_capability_struct *item;
  3357.   char *buffer;
  3358.  
  3359. #if !defined (GNU_TERMCAP)
  3360.   buffer = (char *) xmalloc (sizeof (termcap_buffer) + 1);
  3361. # define buffer_space &buffer
  3362. #else
  3363. # define buffer_space 0
  3364. #endif
  3365.  
  3366.   for (i = 0; (item = &from_array[i]) && (item->type != tc_last); i++)
  3367.     {
  3368.       switch (item->type)
  3369.     {
  3370.     case tc_int:
  3371.       *((int *) (item->value)) = tgetnum (item->name);
  3372.       break;
  3373.  
  3374.     case tc_flag:
  3375.       *((int *) item->value) = tgetflag (item->name);
  3376.       break;
  3377.  
  3378.     case tc_char:
  3379.       *((char **) item->value) = tgetstr (item->name, buffer_space);
  3380.       break;
  3381.  
  3382.     default:
  3383.       FERROR ("Bad entry scanned in tc_struct[].\n \
  3384.            Ask bfox@ai.mit.edu to fix this someday.\n");
  3385.     }
  3386.     }
  3387.  
  3388.   PC = terminal_padding ? terminal_padding[0] : 0;
  3389.   BC = terminal_cursor_left;
  3390.   UP = terminal_cursor_up;
  3391.   return (NO_ERROR);
  3392. }
  3393.  
  3394. /* Return the number of rows this terminal has. */
  3395. int
  3396. get_terminal_rows ()
  3397. {
  3398.   int rows = 0;
  3399.  
  3400. #if defined (TIOCGWINSZ)
  3401.   {
  3402.     int tty;
  3403.     struct winsize size;
  3404.  
  3405.     tty = fileno (stdin);
  3406.  
  3407.     if (ioctl (tty, TIOCGWINSZ, &size) != -1)
  3408.       rows = size.ws_row;
  3409.   }
  3410. #endif /* TIOCGWINSZ */
  3411.  
  3412.   if (!rows)
  3413.     rows = tgetnum ("li");
  3414.  
  3415.   if (rows <= 0)
  3416.     rows = 24;
  3417.  
  3418.   return (rows);
  3419. }
  3420.  
  3421. /* Return the number of columns this terminal has. */
  3422. get_terminal_columns ()
  3423. {
  3424.   int columns = 0;
  3425.  
  3426. #if defined (TIOCGWINSZ)
  3427.   {
  3428.     int tty;
  3429.     struct winsize size;
  3430.  
  3431.     tty = fileno (stdin);
  3432.  
  3433.     if (ioctl (tty, TIOCGWINSZ, &size) != -1)
  3434.       columns = size.ws_col;
  3435.   }
  3436. #endif /* TIOCGWINSZ */
  3437.  
  3438.   if (!columns)
  3439.     columns = tgetnum ("co");
  3440.  
  3441.   if (columns <= 0)
  3442.     columns = 80;
  3443.  
  3444.   return (columns);
  3445. }
  3446.  
  3447. /* #define TERMINAL_INFO_PRINTING */
  3448. #ifdef TERMINAL_INFO_PRINTING
  3449.  
  3450. /* Scan this (already "get_terminal_vars"ed) array, printing out the
  3451.    capability name, and value for each entry.  Pretty print the value
  3452.    so that the terminal doesn't actually do anything, shitbrain. */
  3453. show_terminal_info (from_array)
  3454.      termcap_capability_struct from_array[];
  3455. {
  3456.   register int i;
  3457.   register termcap_capability_struct *item;
  3458.  
  3459.   for (i = 0; ((item = &from_array[i]) && ((item->type) != tc_last)); i++)
  3460.     {
  3461.  
  3462.       char *type_name;
  3463.       switch (item->type)
  3464.     {
  3465.     case tc_int:
  3466.       type_name = "int ";
  3467.       break;
  3468.     case tc_flag:
  3469.       type_name = "flag";
  3470.       break;
  3471.     case tc_char:
  3472.       type_name = "char";
  3473.       break;
  3474.     default:
  3475.       type_name = "Broken";
  3476.     }
  3477.  
  3478.       printf ("\t%s\t%s = ", type_name, item->name);
  3479.  
  3480.       switch (item->type)
  3481.     {
  3482.     case tc_int:
  3483.     case tc_flag:
  3484.       printf ("%d", *((int *) item->value));
  3485.       break;
  3486.     case tc_char:
  3487.       tc_pretty_print (*((char **) item->value));
  3488.       break;
  3489.     }
  3490.       printf ("\n");
  3491.     }
  3492. }
  3493.  
  3494. /* Print the contents of string without sending anything that isn't
  3495.    a normal printing ASCII character. */
  3496. tc_pretty_print (string)
  3497.      register char *string;
  3498. {
  3499.   register char c;
  3500.  
  3501.   while (c = *string++)
  3502.     {
  3503.       if (CTRLP (c))
  3504.     {
  3505.       putchar ('^');
  3506.       c += 64;
  3507.     }
  3508.       putchar (c);
  3509.     }
  3510. }
  3511. #endif TERMINAL_INFO_PRINTING
  3512.  
  3513.  
  3514. /* **************************************************************** */
  3515. /*                                    */
  3516. /*            Character IO, and driver            */
  3517. /*                                    */
  3518. /* **************************************************************** */
  3519.  
  3520. char *widest_line;
  3521. boolean terminal_inited_p = false;
  3522.  
  3523. /* Start up the character io stuff. */
  3524. init_terminal_io ()
  3525. {
  3526.   if (!terminal_inited_p)
  3527.     {
  3528.       opsys_init_terminal ();
  3529.       terminal_rows = get_terminal_rows ();
  3530.       terminal_columns = get_terminal_columns ();
  3531.  
  3532.       widest_line = (char *) xmalloc (terminal_columns);
  3533.  
  3534.       terminal_inited_p = true;
  3535.     }
  3536.  
  3537.   terminal_window.left = 0;
  3538.   terminal_window.top = 0;
  3539.   terminal_window.right = terminal_columns;
  3540.   terminal_window.bottom = terminal_rows;
  3541.  
  3542.   set_window (&terminal_window);
  3543.  
  3544.   terminal_window.bottom -= 2;
  3545.  
  3546.   set_window (&terminal_window);
  3547.  
  3548.   init_echo_area (the_window.left, the_window.bottom + 1,
  3549.           the_window.right, terminal_rows);
  3550.  
  3551.   /* Here is a list of things that the terminal has to be able to do. Do
  3552.      you think that this is too harsh? */
  3553.   if (!terminal_goto ||        /* We can't move the cursor. */
  3554.       !terminal_rows)        /* We don't how many lines it has. */
  3555.     {
  3556.       fprintf (stderr,
  3557.            "Your terminal is not clever enough to run info. Sorry.\n");
  3558.        exit (1);
  3559.     }
  3560. }
  3561.  
  3562. /* Ring the terminal bell. */
  3563. ding ()
  3564. {
  3565.   extern char *terminal_ear_bell;
  3566.  
  3567.   if (terminal_ear_bell)
  3568.     do_term (terminal_ear_bell);
  3569.   else
  3570.     putchar (CTRL ('G'));
  3571.  
  3572.   fflush (stdout);
  3573. }
  3574.  
  3575. int untyi_char = 0;
  3576. boolean inhibit_output = false;
  3577.  
  3578. /* Return a character from stdin, or the last unread character
  3579.    if there is one available. */
  3580. blink_cursor ()
  3581. {
  3582.   int character;
  3583.  
  3584.   fflush (stdout);
  3585.   if (untyi_char)
  3586.     {
  3587.       character = untyi_char;
  3588.       untyi_char = 0;
  3589.     }
  3590.   else
  3591.     character = getc (stdin);
  3592.  
  3593.   return (character);
  3594. }
  3595.  
  3596. /* Display single character on the terminal screen.  If the
  3597.    character would run off the right hand edge of the screen,
  3598.    advance the cursor to the next line. */
  3599. charout (character)
  3600.      int character;
  3601. {
  3602.   if (inhibit_output)
  3603.     return;
  3604.  
  3605.   /* This character may need special treatment if it is
  3606.      a control character. */
  3607.   if (CTRL_P (character))
  3608.     {
  3609.       switch (character)
  3610.     {
  3611.     case NEWLINE:
  3612.     case RETURN:
  3613.       print_cr ();
  3614.       break;
  3615.  
  3616.     case TAB:
  3617.       print_tab ();
  3618.       break;
  3619.  
  3620.     default:
  3621.       charout ('^');
  3622.       charout (UNCTRL (character));
  3623.     }
  3624.     }
  3625.   else
  3626.     {
  3627.       putchar (character);
  3628.       advance (1);
  3629.     }
  3630. }
  3631.  
  3632. /* Move the cursor AMOUNT character positions. */
  3633. advance (amount)
  3634.      int amount;
  3635. {
  3636.   int old_window_cv = the_window.cv;
  3637.  
  3638.   while (amount-- > 0)
  3639.     {
  3640.       the_window.ch++;
  3641.       if (the_window.ch >= the_window.right)
  3642.     {
  3643.       the_window.ch = (the_window.ch - the_window.right) + the_window.left;
  3644.       the_window.cv++;
  3645.  
  3646.       if (the_window.cv >= the_window.bottom)
  3647.         the_window.cv = the_window.top;
  3648.     }
  3649.     }
  3650.  
  3651.   if (the_window.cv != old_window_cv)
  3652.     goto_xy (the_window.ch, the_window.cv);
  3653. }
  3654.  
  3655. /* Print STRING and args using charout */
  3656. print_string (string, a1, a2, a3, a4, a5)
  3657.      char *string;
  3658. {
  3659.   int character;
  3660.   char buffer[2048];
  3661.   int idx = 0;
  3662.  
  3663.   sprintf (buffer, string, a1, a2, a3, a4, a5);
  3664.  
  3665.   while (character = buffer[idx++])
  3666.     charout (character);
  3667.  
  3668.   fflush (stdout);
  3669. }
  3670.  
  3671. /* Display a carriage return.
  3672.    Clears to the end of the line first. */
  3673. print_cr ()
  3674. {
  3675.   extern boolean typing_out;
  3676.   clear_eol ();
  3677.  
  3678.   if (typing_out)
  3679.     {                /* Do the "MORE" stuff. */
  3680.       int response;
  3681.  
  3682.       if (the_window.cv + 2 == the_window.bottom)
  3683.     {
  3684.       goto_xy (the_window.left, the_window.cv + 1);
  3685.       clear_eol ();
  3686.       print_string ("[More]");
  3687.       response = blink_cursor ();
  3688.       if (response != SPACE)
  3689.         {
  3690.           untyi_char = response;
  3691.           inhibit_output = true;
  3692.           return;
  3693.         }
  3694.       else
  3695.         {
  3696.           goto_xy (the_window.left, the_window.cv);
  3697.           clear_eol ();
  3698.           goto_xy (the_window.left, the_window.top);
  3699.           return;
  3700.         }
  3701.     }
  3702.     }
  3703.   advance (the_window.right - the_window.ch);
  3704. }
  3705.  
  3706. /* Move the cursor to the next tab stop, blanking the intervening
  3707.    spaces along the way. */
  3708. print_tab ()
  3709. {
  3710.   int destination =
  3711.   (((the_window.ch - the_window.left) +8) & 0x0f8) + the_window.left;
  3712.  
  3713.   if (destination >= the_window.right)
  3714.     destination -= the_window.right;
  3715.  
  3716.   while (the_window.ch !=destination)
  3717.     charout (SPACE);
  3718. }
  3719.  
  3720. display_width (character, hpos)
  3721.      int character, hpos;
  3722. {
  3723.   int width = 1;
  3724.  
  3725.   if (CTRL_P (character))
  3726.     {
  3727.       switch (character)
  3728.     {
  3729.     case RETURN:
  3730.     case NEWLINE:
  3731.       width = the_window.right - hpos;
  3732.       break;
  3733.     case TAB:
  3734.       width = ((hpos + 8) & 0xf7) - hpos;
  3735.       break;
  3736.     default:
  3737.       width = 2;
  3738.     }
  3739.     }
  3740.   return (width);
  3741. }
  3742.  
  3743. /* Like GOTO_XY, but do it right away. */
  3744. I_goto_xy (xpos, ypos)
  3745.      int xpos, ypos;
  3746. {
  3747.   goto_xy (xpos, ypos);
  3748.   fflush (stdout);
  3749. }
  3750.  
  3751. /* Move the cursor, (and cursor variables) to xpos, ypos. */
  3752. goto_xy (xpos, ypos)
  3753.      int xpos, ypos;
  3754. {
  3755.   the_window.ch = xpos;
  3756.   the_window.cv = ypos;
  3757.   opsys_goto_pos (xpos, ypos);
  3758. }
  3759.  
  3760. /* Clear the screen, leaving ch and cv at the top of the window. */
  3761. clear_screen ()
  3762. {
  3763.   goto_xy (the_window.left, the_window.top);
  3764.   clear_eop_slowly ();
  3765. }
  3766.  
  3767. clear_eop_slowly ()
  3768. {
  3769.   int temp_ch = the_window.ch;
  3770.   int temp_cv = the_window.cv;
  3771.  
  3772.   clear_eol ();
  3773.  
  3774.   while (++the_window.cv < the_window.bottom)
  3775.     {
  3776.       goto_xy (the_window.left, the_window.cv);
  3777.       clear_eol ();
  3778.     }
  3779.   goto_xy (temp_ch, temp_cv);
  3780. }
  3781.  
  3782. /* Clear from current cursor position to end of page. */
  3783. clear_eop ()
  3784. {
  3785.   if (terminal_clearEOP)
  3786.     do_term (terminal_clearEOP);
  3787.   else
  3788.     clear_eop_slowly ();
  3789. }
  3790.  
  3791. /* Clear from current cursor position to end of screen line */
  3792. clear_eol ()
  3793. {
  3794.   int temp_ch = the_window.ch;
  3795.  
  3796.   if (terminal_clearEOL)
  3797.     do_term (terminal_clearEOL);
  3798.   else
  3799.     {
  3800.       char *line = widest_line;
  3801.       int i;
  3802.  
  3803.       for (i = 0; i < the_window.right - the_window.ch; i++)
  3804.     line[i] = ' ';
  3805.       line[i] = '\0';
  3806.  
  3807.       printf ("%s", line);
  3808.     }
  3809.   goto_xy (temp_ch, the_window.cv);
  3810. }
  3811.  
  3812. /* Call FUNCTION with WINDOW active.  You can pass upto 5 args to the
  3813.    function.  This returns whatever FUNCTION returns. */
  3814. int
  3815. with_output_to_window (window, function, arg1, arg2, arg3, arg4, arg5)
  3816.      WINDOW *window;
  3817.      Function *function;
  3818. {
  3819.   int result;
  3820.  
  3821.   push_window ();
  3822.   set_window (window);
  3823.   result = (*function) (arg1, arg2, arg3, arg4, arg5);
  3824.   pop_window ();
  3825.   return (result);
  3826. }
  3827.  
  3828. /* Given a pointer to a window data structure, make that
  3829.    the current window. */
  3830. set_window (window)
  3831.      WINDOW *window;
  3832. {
  3833.   bcopy (window, &the_window, sizeof (WINDOW));
  3834. }
  3835.  
  3836. /* Save the current window on the window stack. */
  3837. push_window ()
  3838. {
  3839.   WINDOW_LIST *new_window = (WINDOW_LIST *) xmalloc (sizeof (WINDOW_LIST));
  3840.  
  3841.   new_window->next_window = window_stack;
  3842.   window_stack = new_window;
  3843.   new_window->ch = the_window.ch;
  3844.   new_window->cv = the_window.cv;
  3845.   new_window->top = the_window.top;
  3846.   new_window->bottom = the_window.bottom;
  3847.   new_window->left = the_window.left;
  3848.   new_window->right = the_window.right;
  3849. }
  3850.  
  3851. /* Pop the top of the window_stack into the_window. */
  3852. pop_window ()
  3853. {
  3854.   set_window ((WINDOW *)window_stack);
  3855.  
  3856.   if (window_stack->next_window)
  3857.     {
  3858.       WINDOW_LIST *thing_to_free = window_stack;
  3859.       window_stack = window_stack->next_window;
  3860.       free (thing_to_free);
  3861.     }
  3862.  
  3863.   goto_xy (the_window.ch, the_window.cv);
  3864. }
  3865.  
  3866. /* **************************************************************** */
  3867. /*                                    */
  3868. /*            "Opsys" functions.                */
  3869. /*                                    */
  3870. /* **************************************************************** */
  3871.  
  3872. /* The lowlevel terminal/file interface.  Nothing ever really gets
  3873.    low level when you're writing in C, though.
  3874.  
  3875.    This file contains all of the "opsys" labels.  You have to make
  3876.    a different one if you want GNU Info to run on machines that don't
  3877.    have Unix.  */
  3878.  
  3879. extern char *terminal_use_begin, *terminal_use_end, *terminal_goto;
  3880.  
  3881. #if defined (TIOCGETC)
  3882. struct tchars original_tchars;
  3883. #endif
  3884.  
  3885. #if defined (TIOCGLTC)
  3886. struct ltchars original_ltchars;
  3887. #endif
  3888.  
  3889. #if defined (USG)
  3890. struct termio original_termio, ttybuff;
  3891. #else
  3892. int original_tty_flags = 0;
  3893. int original_lmode;
  3894. struct sgttyb ttybuff;
  3895. #endif /* !USG */
  3896.  
  3897. /* Yes, that's right, do things that the machine needs to get
  3898.    the terminal into a usable mode. */
  3899. opsys_init_terminal ()
  3900. {
  3901.   int tty = fileno (stdin);
  3902.  
  3903. #if defined (USG)
  3904.   ioctl (tty, TCGETA, &original_termio);
  3905.   ioctl (tty, TCGETA, &ttybuff);
  3906.   ttybuff.c_iflag &= (~ISTRIP & ~INLCR & ~IGNCR & ~ICRNL &~IXON);
  3907.   ttybuff.c_oflag &= (~ONLCR & ~OCRNL);
  3908.   ttybuff.c_lflag &= (~ICANON & ~ECHO);
  3909.  
  3910.   ttybuff.c_cc[VMIN] = 1;
  3911.   ttybuff.c_cc[VTIME] = 0;
  3912.  
  3913.   if (ttybuff.c_cc[VINTR] = DELETE)
  3914.     ttybuff.c_cc[VINTR] = -1;
  3915.  
  3916.   if (ttybuff.c_cc[VQUIT] = DELETE)
  3917.     ttybuff.c_cc[VQUIT] = -1;
  3918.  
  3919.   ioctl (tty, TCSETA, &ttybuff);
  3920. #else /* !USG */
  3921.  
  3922.   ioctl (tty, TIOCGETP, &ttybuff);
  3923.  
  3924.   if (!original_tty_flags)
  3925.     original_tty_flags = ttybuff.sg_flags;
  3926.  
  3927.   /* Make this terminal pass 8 bits around while we are using it. */
  3928. #ifdef PASS8
  3929.   ttybuff.sg_flags |= PASS8;
  3930. #endif
  3931.  
  3932. #if defined (TIOCLGET) && defined (LPASS8)
  3933.   {
  3934.     int flags;
  3935.     ioctl (tty, TIOCLGET, &flags);
  3936.     original_lmode = flags;
  3937.     flags |= LPASS8;
  3938.     ioctl (tty, TIOCLSET, &flags);
  3939.   }
  3940. #endif
  3941.  
  3942. #ifdef TIOCGETC
  3943.   {
  3944.     struct tchars temp;
  3945.  
  3946.     ioctl (tty, TIOCGETC, &original_tchars);
  3947.     bcopy (&original_tchars, &temp, sizeof (struct tchars));
  3948.  
  3949.     temp.t_startc = temp.t_stopc = -1;
  3950.  
  3951.     /* If the quit character conflicts with one of our commands, then
  3952.        make it go away. */
  3953.     if (temp.t_intrc == DELETE)
  3954.       temp.t_intrc == -1;
  3955.  
  3956.     if (temp.t_quitc == DELETE)
  3957.       temp.t_quitc == -1;
  3958.  
  3959.     ioctl (tty, TIOCSETC, &temp);
  3960.   }
  3961. #endif /* TIOCGETC */
  3962.  
  3963. #ifdef TIOCGLTC
  3964.   {
  3965.     struct ltchars temp;
  3966.  
  3967.     ioctl (tty, TIOCGLTC, &original_ltchars);
  3968.     bcopy (&original_ltchars, &temp, sizeof (struct ltchars));
  3969.  
  3970.     /* Make the interrupt keys go away.  Just enough to make people happy. */
  3971.     temp.t_lnextc = -1;        /* C-v */
  3972.  
  3973.     ioctl (tty, TIOCSLTC, &temp);
  3974.   }
  3975. #endif /* TIOCGLTC */
  3976.  
  3977.   ttybuff.sg_flags &= ~ECHO;
  3978.   ttybuff.sg_flags |= CBREAK;
  3979.   ioctl (tty, TIOCSETN, &ttybuff);
  3980. #endif /* !USG */
  3981.  
  3982.   open_terminal_io ();
  3983.   do_term (terminal_use_begin);
  3984. }
  3985.  
  3986. /* Fix the terminal that I broke. */
  3987. restore_io ()
  3988. {
  3989.   int tty = fileno (stdin);
  3990.  
  3991. #if defined (USG)
  3992.   ioctl (tty, TCSETA, &original_termio);
  3993. #else
  3994.   ioctl (tty, TIOCGETP, &ttybuff);
  3995.   ttybuff.sg_flags = original_tty_flags;
  3996.   ioctl (tty, TIOCSETN, &ttybuff);
  3997.  
  3998. #ifdef TIOCGETC
  3999.   ioctl (tty, TIOCSETC, &original_tchars);
  4000. #endif /* TIOCGETC */
  4001.  
  4002. #ifdef TIOCGLTC
  4003.   ioctl (tty, TIOCSLTC, &original_ltchars);
  4004. #endif /* TIOCGLTC */
  4005.  
  4006. #if defined (TIOCLGET) && defined (LPASS8)
  4007.   ioctl (tty, TIOCLSET, &original_lmode);
  4008. #endif
  4009.  
  4010. #endif /* !USG */
  4011.   do_term (terminal_use_end);
  4012. }
  4013.  
  4014. opsys_goto_pos (xpos, ypos)
  4015.      int xpos, ypos;
  4016. {
  4017.   do_term (tgoto (terminal_goto, xpos, ypos));
  4018. }
  4019.  
  4020. character_output_function (character)
  4021.      char character;
  4022. {
  4023.   putchar (character);
  4024. }
  4025.  
  4026. /* Generic interface to tputs. */
  4027. do_term (command)
  4028.      char *command;
  4029. {
  4030.   /* Send command to the terminal, with appropriate padding. */
  4031.   tputs (command, 1, character_output_function);
  4032. }
  4033.  
  4034. /* Filename manipulators, and the like. */
  4035. char local_temp_filename[FILENAME_LEN];
  4036.  
  4037. /* Expand the filename in partial to make a real name for
  4038.    this operating system.  This looks in INFO_PATHS in order to
  4039.    find the correct file.  If it can't find the file, it just
  4040.    returns the path as you gave it. */
  4041. char *
  4042. opsys_filename (partial)
  4043.      char *partial;
  4044. {
  4045.   int initial_character;
  4046.  
  4047.   if (partial && (initial_character = *partial))
  4048.     {
  4049.  
  4050.       if (initial_character == '/')
  4051.     return (partial);
  4052.  
  4053.       if (initial_character == '~')
  4054.     {
  4055.       if (partial[1] == '/')
  4056.         {
  4057.           /* Return the concatenation of HOME and the rest
  4058.          of the string. */
  4059.           strcpy (local_temp_filename, getenv ("HOME"));
  4060.           strcat (local_temp_filename, &partial[2]);
  4061.           return (local_temp_filename);
  4062.         }
  4063.       else
  4064.         {
  4065.           struct passwd *user_entry;
  4066.           int i, c;
  4067.           char username[257];
  4068.  
  4069.           for (i = 1; c = partial[i]; i++)
  4070.         {
  4071.           if (c == '/')
  4072.             break;
  4073.           else
  4074.             username[i - 1] = c;
  4075.         }
  4076.           username[i - 1] = '\0';
  4077.  
  4078.           if (!(user_entry = getpwnam (username)))
  4079.         {
  4080.           display_error ("Not a registered user!");
  4081.           return (partial);
  4082.         }
  4083.           strcpy (local_temp_filename, user_entry->pw_dir);
  4084.           strcat (local_temp_filename, &partial[i]);
  4085.           return (local_temp_filename);
  4086.         }
  4087.     }
  4088.  
  4089.       if (initial_character == '.')
  4090.     {
  4091. #if defined (USG)
  4092.       if (!getcwd (local_temp_filename, FILENAME_LEN))
  4093. #else
  4094.       if (!getwd (local_temp_filename))
  4095. #endif
  4096.         {
  4097.           display_error (local_temp_filename);
  4098.           return (partial);
  4099.         }
  4100.  
  4101.       strcat (local_temp_filename, &partial[1]);
  4102.       return (local_temp_filename);
  4103.     }
  4104.  
  4105.       /* Scan the list of directories in INFOPATH. */
  4106.       {
  4107.     struct stat finfo;
  4108.     char *temp_dirname, *extract_colon_unit ();
  4109.     int statable, dirname_index = 0;
  4110.  
  4111.     while (temp_dirname = extract_colon_unit (infopath, &dirname_index))
  4112.       {
  4113.         strcpy (local_temp_filename, temp_dirname);
  4114.  
  4115.         if (temp_dirname[(strlen (temp_dirname)) - 1] != '/')
  4116.           strcat (local_temp_filename, "/");
  4117.  
  4118.         strcat (local_temp_filename, partial);
  4119.  
  4120.         free (temp_dirname);
  4121.  
  4122.  
  4123.         statable = (stat (local_temp_filename, &finfo) == 0);
  4124.  
  4125.         if (statable && (finfo.st_mode & S_IFMT) == S_IFREG)
  4126.           return (local_temp_filename);
  4127.       }
  4128.       }
  4129.     }
  4130.   return (partial);
  4131. }
  4132.  
  4133. /* Given a string containing units of information separated by colons,
  4134.    return the next one pointed to by IDX, or NULL if there are no more.
  4135.    Advance IDX to the character after the colon. */
  4136. char *
  4137. extract_colon_unit (string, idx)
  4138.      char *string;
  4139.      int *idx;
  4140. {
  4141.   register int i, start;
  4142.  
  4143.   i = start = *idx;
  4144.   if ((i >= strlen (string)) || !string)
  4145.     return ((char *) NULL);
  4146.  
  4147.   while (string[i] && string[i] != ':')
  4148.     i++;
  4149.   if (i == start)
  4150.     {
  4151.       return ((char *) NULL);
  4152.     }
  4153.   else
  4154.     {
  4155.       char *value = xmalloc (1 + (i - start));
  4156.       strncpy (value, &string[start], (i - start));
  4157.       value[i - start] = '\0';
  4158.       if (string[i])
  4159.     ++i;
  4160.       *idx = i;
  4161.       return (value);
  4162.     }
  4163. }
  4164.  
  4165. /* **************************************************************** */
  4166. /*                                    */
  4167. /*            The echo area.                    */
  4168. /*                                    */
  4169. /* **************************************************************** */
  4170.  
  4171. /* echoarea.c -- some functions to aid in user interaction. */
  4172.  
  4173. WINDOW echo_area = {0, 0, 0, 0, 0, 0};
  4174. boolean echo_area_open_p = false;
  4175. char modeline[256];
  4176. WINDOW modeline_window = {0, 0, 0, 0, 0, 0};
  4177.  
  4178. /* Define the location of the echo area. Also inits the
  4179.    modeline as well. */
  4180. init_echo_area (left, top, right, bottom)
  4181.      int left, top, right, bottom;
  4182. {
  4183.   echo_area.left = modeline_window.left = left;
  4184.   echo_area.top = top;
  4185.   modeline_window.top = top - 1;
  4186.   echo_area.right = modeline_window.right = right;
  4187.   echo_area.bottom = bottom;
  4188.   modeline_window.bottom = modeline_window.top;
  4189. }
  4190.  
  4191. /* Make the echo_area_window be the current window, and only allow
  4192.    output in there.  Clear the window to start. */
  4193. new_echo_area ()
  4194. {
  4195.   if (!echo_area_open_p)
  4196.     {
  4197.       push_window ();
  4198.       set_window (&echo_area);
  4199.       echo_area_open_p = true;
  4200.     }
  4201.   goto_xy (the_window.left, the_window.top);
  4202.   clear_eop ();
  4203. }
  4204.  
  4205. /* Return output to the previous window. */
  4206. close_echo_area ()
  4207. {
  4208.   if (!echo_area_open_p)
  4209.     return;
  4210.  
  4211.   pop_window ();
  4212.   echo_area_open_p = false;
  4213. }
  4214.  
  4215. /* Clear the contents of the echo area. */
  4216. clear_echo_area ()
  4217. {
  4218.   new_echo_area ();
  4219.   close_echo_area ();
  4220. }
  4221.  
  4222. /* Create and display the modeline. */
  4223. make_modeline ()
  4224. {
  4225.   extern int info_buffer_len;
  4226.   int width = modeline_window.right - modeline_window.left;
  4227.  
  4228.   sprintf (modeline, "Info: (%s)%s, %d lines",
  4229.        current_info_file, current_info_node, nodelines);
  4230.   if (strnicmp
  4231.       (opsys_filename (current_info_file), last_loaded_info_file,
  4232.        strlen (last_loaded_info_file)) != 0)
  4233.     {
  4234.       sprintf (&modeline[strlen (modeline)], ", Subfile: %s",
  4235.            last_loaded_info_file);
  4236.     }
  4237.  
  4238.   if (strlen (modeline) < width)
  4239.     {
  4240.       int idx = strlen (modeline);
  4241.       while (idx != width)
  4242.     modeline[idx++] = '-';
  4243.       modeline[idx] = '\0';
  4244.     }
  4245.  
  4246.   if (strlen (modeline) > width)
  4247.     modeline[width] = '\0';
  4248.   push_window ();
  4249.   set_window (&modeline_window);
  4250.   goto_xy (the_window.left, the_window.top);
  4251.  
  4252.   if (terminal_inverse_begin)
  4253.     do_term (terminal_inverse_begin);
  4254.   print_string (modeline);
  4255.   if (terminal_inverse_begin)
  4256.     do_term (terminal_end_attributes);
  4257.  
  4258.   pop_window ();
  4259. }
  4260.  
  4261. boolean typing_out = false;
  4262.  
  4263. /* Prepare to do output to the typeout window.  If the
  4264.    typeout window is already open, ignore this clown. */
  4265. open_typeout ()
  4266. {
  4267.   if (typing_out)
  4268.     return;
  4269.  
  4270.   push_window ();
  4271.   set_window (&terminal_window);
  4272.   goto_xy (the_window.ch, the_window.cv);
  4273.   typing_out = window_bashed = true;
  4274. }
  4275.  
  4276. /* Close the currently open typeout window. */
  4277. close_typeout ()
  4278. {
  4279.   if (inhibit_output)
  4280.     inhibit_output = false;
  4281.   else
  4282.     {
  4283.       untyi_char = getc (stdin);
  4284.       if (untyi_char == SPACE)
  4285.     untyi_char = 0;
  4286.     }
  4287.   pop_window ();
  4288.   typing_out = false;
  4289. }
  4290.  
  4291. char *
  4292. xrealloc (pointer, bytes)
  4293.      char *pointer;
  4294.      int bytes;
  4295. {
  4296.   char *temp;
  4297.  
  4298.   if (!pointer)
  4299.     temp = (char *)xmalloc (bytes);
  4300.   else
  4301.     temp = (char *)realloc (pointer, bytes);
  4302.  
  4303.   if (!temp)
  4304.     {
  4305.       fprintf (stderr, "Virtual memory exhausted\n");
  4306.       restore_io ();
  4307.       exit (2);
  4308.     }
  4309.   return (temp);
  4310. }
  4311.  
  4312. char *
  4313. xmalloc (bytes)
  4314.      int bytes;
  4315. {
  4316.   char *temp = (char *) malloc (bytes);
  4317.  
  4318.   if (!temp)
  4319.     {
  4320.       fprintf (stderr, "Virtual memory exhausted\n");
  4321.       restore_io ();
  4322.       exit (2);
  4323.     }
  4324.   return (temp);
  4325. }
  4326.