home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / gdb-4.9 / gdb / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-12  |  63.3 KB  |  2,496 lines

  1. /* Top level `main' program for GDB, the GNU debugger.
  2.    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992
  3.    Free Software Foundation, Inc.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "defs.h"
  22. #include "gdbcmd.h"
  23. #include "call-cmds.h"
  24. #include "symtab.h"
  25. #include "inferior.h"
  26. #include "signals.h"
  27. #include "target.h"
  28. #include "breakpoint.h"
  29. #include "gdbtypes.h"
  30. #include "expression.h"
  31. #include "language.h"
  32.  
  33. #include "getopt.h"
  34.  
  35. /* readline include files */
  36. #include "readline.h"
  37. #include "history.h"
  38.  
  39. /* readline defines this.  */
  40. #undef savestring
  41.  
  42. #ifdef USG
  43. #include <sys/types.h>
  44. #include <unistd.h>
  45. #endif
  46.  
  47. #include <string.h>
  48. #ifndef    NO_SYS_FILE
  49. #include <sys/file.h>
  50. #endif
  51. #include <setjmp.h>
  52. #include <sys/param.h>
  53. #include <sys/stat.h>
  54. #include <ctype.h>
  55.  
  56. #ifdef SET_STACK_LIMIT_HUGE
  57. #include <sys/time.h>
  58. #include <sys/resource.h>
  59.  
  60. int original_stack_limit;
  61. #endif
  62.  
  63. /* Prototypes for local functions */
  64.  
  65. static char *
  66. symbol_completion_function PARAMS ((char *, int));
  67.  
  68. static void
  69. command_loop PARAMS ((void));
  70.  
  71. static void
  72. command_loop_marker PARAMS ((int));
  73.  
  74. static void
  75. print_gdb_version PARAMS ((FILE *));
  76.  
  77. static void
  78. quit_command PARAMS ((char *, int));
  79.  
  80. static void
  81. initialize_main PARAMS ((void));
  82.  
  83. static void
  84. initialize_history PARAMS ((void));
  85.  
  86. static void
  87. initialize_cmd_lists PARAMS ((void));
  88.  
  89. static void
  90. float_handler PARAMS ((int));
  91.  
  92. static void
  93. source_command PARAMS ((char *, int));
  94.  
  95. static void
  96. cd_command PARAMS ((char *, int));
  97.  
  98. static void
  99. print_gnu_advertisement PARAMS ((void));
  100.  
  101. static void
  102. init_signals PARAMS ((void));
  103.  
  104. static void
  105. read_command_file PARAMS ((FILE *));
  106.  
  107. static void 
  108. set_verbose PARAMS ((char *, int, struct cmd_list_element *));
  109.  
  110. static void
  111. show_history PARAMS ((char *, int));
  112.  
  113. static void
  114. set_history PARAMS ((char *, int));
  115.  
  116. static void
  117. set_history_size_command PARAMS ((char *, int, struct cmd_list_element *));
  118.  
  119. static void
  120. show_commands PARAMS ((char *, int));
  121.  
  122. static void
  123. echo_command PARAMS ((char *, int));
  124.  
  125. static void
  126. pwd_command PARAMS ((char *, int));
  127.  
  128. static void
  129. show_version PARAMS ((char *, int));
  130.  
  131. static void
  132. document_command PARAMS ((char *, int));
  133.  
  134. static void
  135. define_command PARAMS ((char *, int));
  136.  
  137. static void
  138. validate_comname PARAMS ((char *));
  139.  
  140. static void
  141. help_command PARAMS ((char *, int));
  142.  
  143. static void
  144. show_command PARAMS ((char *, int));
  145.  
  146. static void
  147. info_command PARAMS ((char *, int));
  148.  
  149. static void
  150. do_nothing PARAMS ((int));
  151.  
  152. static int
  153. quit_cover PARAMS ((char *));
  154.  
  155. static void
  156. disconnect PARAMS ((int));
  157.  
  158. static void
  159. source_cleanup PARAMS ((FILE *));
  160.  
  161. /* If this definition isn't overridden by the header files, assume
  162.    that isatty and fileno exist on this system.  */
  163. #ifndef ISATTY
  164. #define ISATTY(FP)    (isatty (fileno (FP)))
  165. #endif
  166.  
  167. /* Initialization file name for gdb.  This is overridden in some configs.  */
  168.  
  169. #ifndef    GDBINIT_FILENAME
  170. #define    GDBINIT_FILENAME    ".gdbinit"
  171. #endif
  172. static char gdbinit[] = GDBINIT_FILENAME;
  173. static int inhibit_gdbinit = 0;
  174.  
  175. #define    ALL_CLEANUPS    ((struct cleanup *)0)
  176.  
  177. /* Version number of GDB, as a string.  */
  178.  
  179. extern char *version;
  180.  
  181. /* Message to be printed before the error message, when an error occurs.  */
  182.  
  183. extern char *error_pre_print;
  184.  
  185. /* Message to be printed before the warning message, when a warning occurs.  */
  186.  
  187. extern char *warning_pre_print;
  188.  
  189. extern char lang_frame_mismatch_warn[];        /* language.c */
  190.  
  191. /* Whether GDB's stdin is on a terminal.  */
  192.  
  193. extern int gdb_has_a_terminal;            /* inflow.c */
  194.  
  195. /* Flag for whether we want all the "from_tty" gubbish printed.  */
  196.  
  197. int caution = 1;            /* Default is yes, sigh. */
  198.  
  199. /*
  200.  * Define all cmd_list_element's
  201.  */
  202.  
  203. /* Chain containing all defined commands.  */
  204.  
  205. struct cmd_list_element *cmdlist;
  206.  
  207. /* Chain containing all defined info subcommands.  */
  208.  
  209. struct cmd_list_element *infolist;
  210.  
  211. /* Chain containing all defined enable subcommands. */
  212.  
  213. struct cmd_list_element *enablelist;
  214.  
  215. /* Chain containing all defined disable subcommands. */
  216.  
  217. struct cmd_list_element *disablelist;
  218.  
  219. /* Chain containing all defined delete subcommands. */
  220.  
  221. struct cmd_list_element *deletelist;
  222.  
  223. /* Chain containing all defined "enable breakpoint" subcommands. */
  224.  
  225. struct cmd_list_element *enablebreaklist;
  226.  
  227. /* Chain containing all defined set subcommands */
  228.  
  229. struct cmd_list_element *setlist;
  230.  
  231. /* Chain containing all defined unset subcommands */
  232.  
  233. struct cmd_list_element *unsetlist;
  234.  
  235. /* Chain containing all defined show subcommands.  */
  236.  
  237. struct cmd_list_element *showlist;
  238.  
  239. /* Chain containing all defined \"set history\".  */
  240.  
  241. struct cmd_list_element *sethistlist;
  242.  
  243. /* Chain containing all defined \"show history\".  */
  244.  
  245. struct cmd_list_element *showhistlist;
  246.  
  247. /* Chain containing all defined \"unset history\".  */
  248.  
  249. struct cmd_list_element *unsethistlist;
  250.  
  251. /* Chain containing all defined maintenance subcommands. */
  252.  
  253. #if MAINTENANCE_CMDS
  254. struct cmd_list_element *maintenancelist;
  255. #endif
  256.  
  257. /* Chain containing all defined "maintenance info" subcommands. */
  258.  
  259. #if MAINTENANCE_CMDS
  260. struct cmd_list_element *maintenanceinfolist;
  261. #endif
  262.  
  263. /* Chain containing all defined "maintenance print" subcommands. */
  264.  
  265. #if MAINTENANCE_CMDS
  266. struct cmd_list_element *maintenanceprintlist;
  267. #endif
  268.  
  269. struct cmd_list_element *setprintlist;
  270.  
  271. struct cmd_list_element *showprintlist;
  272.  
  273. struct cmd_list_element *setchecklist;
  274.  
  275. struct cmd_list_element *showchecklist;
  276.  
  277. /* stdio stream that command input is being read from.  */
  278.  
  279. FILE *instream;
  280.  
  281. /* Current working directory.  */
  282.  
  283. char *current_directory;
  284.  
  285. /* The directory name is actually stored here (usually).  */
  286. static char dirbuf[1024];
  287.  
  288. /* Function to call before reading a command, if nonzero.
  289.    The function receives two args: an input stream,
  290.    and a prompt string.  */
  291.  
  292. void (*window_hook) PARAMS ((FILE *, char *));
  293.  
  294. extern int frame_file_full_name;
  295. extern int mapped_symbol_files;
  296. extern int readnow_symbol_files;
  297.  
  298. int epoch_interface;
  299. int xgdb_verbose;
  300.  
  301. /* gdb prints this when reading a command interactively */
  302. static char *prompt;
  303.  
  304. /* Buffer used for reading command lines, and the size
  305.    allocated for it so far.  */
  306.  
  307. char *line;
  308. int linesize = 100;
  309.  
  310. /* Baud rate specified for talking to serial target systems.  Default
  311.    is left as a zero pointer, so targets can choose their own defaults.  */
  312.  
  313. char *baud_rate;
  314.  
  315. /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT.  */
  316.  
  317. #ifndef STOP_SIGNAL
  318. #ifdef SIGTSTP
  319. #define STOP_SIGNAL SIGTSTP
  320. static void stop_sig PARAMS ((int));
  321. #endif
  322. #endif
  323.  
  324. /* Some System V have job control but not sigsetmask(). */
  325. #if !defined (HAVE_SIGSETMASK)
  326. #define HAVE_SIGSETMASK !defined (USG)
  327. #endif
  328.  
  329. #if 0 == (HAVE_SIGSETMASK)
  330. #define sigsetmask(n)
  331. #endif
  332.  
  333. /* This is how `error' returns to command level.  */
  334.  
  335. jmp_buf to_top_level;
  336.  
  337. NORETURN void
  338. return_to_top_level ()
  339. {
  340.   quit_flag = 0;
  341.   immediate_quit = 0;
  342.   bpstat_clear_actions(stop_bpstat);    /* Clear queued breakpoint commands */
  343.   disable_current_display ();
  344.   do_cleanups (ALL_CLEANUPS);
  345.   (NORETURN void) longjmp (to_top_level, 1);
  346. }
  347.  
  348. /* Call FUNC with arg ARGS, catching any errors.
  349.    If there is no error, return the value returned by FUNC.
  350.    If there is an error, print ERRSTRING, print the specific error message,
  351.                  then return zero.  */
  352.  
  353. int
  354. catch_errors (func, args, errstring)
  355.      int (*func) PARAMS ((char *));
  356.      char *args;
  357.      char *errstring;
  358. {
  359.   jmp_buf saved;
  360.   int val;
  361.   struct cleanup *saved_cleanup_chain;
  362.   char *saved_error_pre_print;
  363.  
  364.   saved_cleanup_chain = save_cleanups ();
  365.   saved_error_pre_print = error_pre_print;
  366.  
  367.   memcpy ((char *)saved, (char *)to_top_level, sizeof (jmp_buf));
  368.   error_pre_print = errstring;
  369.  
  370.   if (setjmp (to_top_level) == 0)
  371.     val = (*func) (args);
  372.   else
  373.     val = 0;
  374.  
  375.   restore_cleanups (saved_cleanup_chain);
  376.  
  377.   error_pre_print = saved_error_pre_print;
  378.   memcpy ((char *)to_top_level, (char *)saved, sizeof (jmp_buf));
  379.   return val;
  380. }
  381.  
  382. /* Handler for SIGHUP.  */
  383.  
  384. static void
  385. disconnect (signo)
  386. int signo;
  387. {
  388.   catch_errors (quit_cover, NULL, "Could not kill inferior process");
  389.   signal (SIGHUP, SIG_DFL);
  390.   kill (getpid (), SIGHUP);
  391. }
  392.  
  393. /* Just a little helper function for disconnect().  */
  394.  
  395. static int
  396. quit_cover (s)
  397. char *s;
  398. {
  399.   caution = 0;        /* Throw caution to the wind -- we're exiting.
  400.                This prevents asking the user dumb questions.  */
  401.   quit_command((char *)0, 0);
  402.   return 0;
  403. }
  404.  
  405. /* Clean up on error during a "source" command (or execution of a
  406.    user-defined command).  */
  407.  
  408. static void
  409. source_cleanup (stream)
  410.      FILE *stream;
  411. {
  412.   /* Restore the previous input stream.  */
  413.   instream = stream;
  414. }
  415.  
  416. /* Read commands from STREAM.  */
  417. static void
  418. read_command_file (stream)
  419.      FILE *stream;
  420. {
  421.   struct cleanup *cleanups;
  422.  
  423.   cleanups = make_cleanup (source_cleanup, instream);
  424.   instream = stream;
  425.   command_loop ();
  426.   do_cleanups (cleanups);
  427. }
  428.  
  429. int
  430. main (argc, argv)
  431.      int argc;
  432.      char **argv;
  433. {
  434.   int count;
  435.   static int quiet = 0;
  436.   static int batch = 0;
  437.  
  438.   /* Pointers to various arguments from command line.  */
  439.   char *symarg = NULL;
  440.   char *execarg = NULL;
  441.   char *corearg = NULL;
  442.   char *cdarg = NULL;
  443.   char *ttyarg = NULL;
  444.  
  445.   /* Pointers to all arguments of +command option.  */
  446.   char **cmdarg;
  447.   /* Allocated size of cmdarg.  */
  448.   int cmdsize;
  449.   /* Number of elements of cmdarg used.  */
  450.   int ncmd;
  451.  
  452.   /* Indices of all arguments of +directory option.  */
  453.   char **dirarg;
  454.   /* Allocated size.  */
  455.   int dirsize;
  456.   /* Number of elements used.  */
  457.   int ndir;
  458.   
  459.   struct stat homebuf, cwdbuf;
  460.   char *homedir, *homeinit;
  461.  
  462.   register int i;
  463.  
  464.   /* This needs to happen before the first use of malloc.  */
  465.   init_malloc ((PTR) NULL);
  466.  
  467. #if defined (ALIGN_STACK_ON_STARTUP)
  468.   i = (int) &count & 0x3;
  469.   if (i != 0)
  470.     alloca (4 - i);
  471. #endif
  472.  
  473.   /* If error() is called from initialization code, just exit */
  474.   if (setjmp (to_top_level)) {
  475.     exit(1);
  476.   }
  477.  
  478.   cmdsize = 1;
  479.   cmdarg = (char **) xmalloc (cmdsize * sizeof (*cmdarg));
  480.   ncmd = 0;
  481.   dirsize = 1;
  482.   dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
  483.   ndir = 0;
  484.  
  485.   quit_flag = 0;
  486.   line = (char *) xmalloc (linesize);
  487.   line[0] = '\0';        /* Terminate saved (now empty) cmd line */
  488.   instream = stdin;
  489.  
  490.   getcwd (dirbuf, sizeof (dirbuf));
  491.   current_directory = dirbuf;
  492.  
  493. #ifdef SET_STACK_LIMIT_HUGE
  494.   {
  495.     struct rlimit rlim;
  496.  
  497.     /* Set the stack limit huge so that alloca (particularly stringtab
  498.      * in dbxread.c) does not fail. */
  499.     getrlimit (RLIMIT_STACK, &rlim);
  500.     original_stack_limit = rlim.rlim_cur;
  501.     rlim.rlim_cur = rlim.rlim_max;
  502.     setrlimit (RLIMIT_STACK, &rlim);
  503.   }
  504. #endif /* SET_STACK_LIMIT_HUGE */
  505.  
  506.   /* Parse arguments and options.  */
  507.   {
  508.     int c;
  509.     static int print_help;
  510.     /* When var field is 0, use flag field to record the equivalent
  511.        short option (or arbitrary numbers starting at 10 for those
  512.        with no equivalent).  */
  513.     static struct option long_options[] =
  514.       {
  515.     {"readnow", no_argument, &readnow_symbol_files, 1},
  516.     {"r", no_argument, &readnow_symbol_files, 1},
  517.     {"mapped", no_argument, &mapped_symbol_files, 1},
  518.     {"m", no_argument, &mapped_symbol_files, 1},
  519.     {"quiet", no_argument, &quiet, 1},
  520.     {"q", no_argument, &quiet, 1},
  521.     {"silent", no_argument, &quiet, 1},
  522.     {"nx", no_argument, &inhibit_gdbinit, 1},
  523.     {"n", no_argument, &inhibit_gdbinit, 1},
  524.     {"batch", no_argument, &batch, 1},
  525.     {"epoch", no_argument, &epoch_interface, 1},
  526.     {"fullname", no_argument, &frame_file_full_name, 1},
  527.     {"f", no_argument, &frame_file_full_name, 1},
  528.     {"help", no_argument, &print_help, 1},
  529.     {"se", required_argument, 0, 10},
  530.     {"symbols", required_argument, 0, 's'},
  531.     {"s", required_argument, 0, 's'},
  532.     {"exec", required_argument, 0, 'e'},
  533.     {"e", required_argument, 0, 'e'},
  534.     {"core", required_argument, 0, 'c'},
  535.     {"c", required_argument, 0, 'c'},
  536.     {"command", required_argument, 0, 'x'},
  537.     {"x", required_argument, 0, 'x'},
  538.     {"directory", required_argument, 0, 'd'},
  539.     {"cd", required_argument, 0, 11},
  540.     {"tty", required_argument, 0, 't'},
  541.     {"baud", required_argument, 0, 'b'},
  542.     {"b", required_argument, 0, 'b'},
  543. /* Allow machine descriptions to add more options... */
  544. #ifdef ADDITIONAL_OPTIONS
  545.     ADDITIONAL_OPTIONS
  546. #endif
  547.     {0, no_argument, 0, 0},
  548.       };
  549.  
  550.     while (1)
  551.       {
  552.     int option_index;
  553.  
  554.     c = getopt_long_only (argc, argv, "",
  555.                   long_options, &option_index);
  556.     if (c == EOF)
  557.       break;
  558.  
  559.     /* Long option that takes an argument.  */
  560.     if (c == 0 && long_options[option_index].flag == 0)
  561.       c = long_options[option_index].val;
  562.  
  563.     switch (c)
  564.       {
  565.       case 0:
  566.         /* Long option that just sets a flag.  */
  567.         break;
  568.       case 10:
  569.         symarg = optarg;
  570.         execarg = optarg;
  571.         break;
  572.       case 11:
  573.         cdarg = optarg;
  574.         break;
  575.       case 's':
  576.         symarg = optarg;
  577.         break;
  578.       case 'e':
  579.         execarg = optarg;
  580.         break;
  581.       case 'c':
  582.         corearg = optarg;
  583.         break;
  584.       case 'x':
  585.         cmdarg[ncmd++] = optarg;
  586.         if (ncmd >= cmdsize)
  587.           {
  588.         cmdsize *= 2;
  589.         cmdarg = (char **) xrealloc ((char *)cmdarg,
  590.                          cmdsize * sizeof (*cmdarg));
  591.           }
  592.         break;
  593.       case 'd':
  594.         dirarg[ndir++] = optarg;
  595.         if (ndir >= dirsize)
  596.           {
  597.         dirsize *= 2;
  598.         dirarg = (char **) xrealloc ((char *)dirarg,
  599.                          dirsize * sizeof (*dirarg));
  600.           }
  601.         break;
  602.       case 't':
  603.         ttyarg = optarg;
  604.         break;
  605.       case 'q':
  606.         quiet = 1;
  607.         break;
  608.       case 'b':
  609.         baud_rate = optarg;
  610.         break;
  611. #ifdef ADDITIONAL_OPTION_CASES
  612.       ADDITIONAL_OPTION_CASES
  613. #endif
  614.       case '?':
  615.         fprintf (stderr,
  616.              "Use `%s --help' for a complete list of options.\n",
  617.              argv[0]);
  618.         exit (1);
  619.       }
  620.       }
  621.  
  622.     if (print_help)
  623.       {
  624.     print_gdb_version(stderr);
  625.     fputs ("\n\
  626. This is the GNU debugger.  Usage:\n\
  627.     gdb [options] [executable-file [core-file or process-id]]\n\
  628. Options:\n\
  629.   -help             Print this message.\n\
  630.   -quiet            Do not print version number on startup.\n\
  631.   -fullname         Output information used by emacs-GDB interface.\n\
  632.   -epoch            Output information used by epoch emacs-GDB interface.\n\
  633.   -batch            Exit after processing options.\n\
  634.   -nx               Do not read .gdbinit file.\n\
  635.   -tty=TTY          Use TTY for input/output by the program being debugged.\n\
  636.   -cd=DIR           Change current directory to DIR.\n\
  637.   -directory=DIR    Search for source files in DIR.\n\
  638.   -command=FILE     Execute GDB commands from FILE.\n\
  639.   -symbols=SYMFILE  Read symbols from SYMFILE.\n\
  640.   -exec=EXECFILE    Use EXECFILE as the executable.\n\
  641.   -se=FILE          Use FILE as symbol file and executable file.\n\
  642.   -core=COREFILE    Analyze the core dump COREFILE.\n\
  643.   -b BAUDRATE       Set serial port baud rate used for remote debugging.\n\
  644.   -mapped           Use mapped symbol files if supported on this system.\n\
  645.   -readnow          Fully read symbol files on first access.\n\
  646. ", stderr);
  647. #ifdef ADDITIONAL_OPTION_HELP
  648.     fputs (ADDITIONAL_OPTION_HELP, stderr);
  649. #endif
  650.     fputs ("\n\
  651. For more information, type \"help\" from within GDB, or consult the\n\
  652. GDB manual (available as on-line info or a printed manual).\n", stderr);
  653.     /* Exiting after printing this message seems like
  654.        the most useful thing to do.  */
  655.     exit (0);
  656.       }
  657.     
  658.     /* OK, that's all the options.  The other arguments are filenames.  */
  659.     count = 0;
  660.     for (; optind < argc; optind++)
  661.       switch (++count)
  662.     {
  663.     case 1:
  664.       symarg = argv[optind];
  665.       execarg = argv[optind];
  666.       break;
  667.     case 2:
  668.       corearg = argv[optind];
  669.       break;
  670.     case 3:
  671.       fprintf (stderr,
  672.            "Excess command line arguments ignored. (%s%s)\n",
  673.            argv[optind], (optind == argc - 1) ? "" : " ...");
  674.       break;
  675.     }
  676.     if (batch)
  677.       quiet = 1;
  678.   }
  679.  
  680.   /* Run the init function of each source file */
  681.  
  682.   initialize_cmd_lists ();    /* This needs to be done first */
  683.   initialize_all_files ();
  684.   initialize_main ();        /* But that omits this file!  Do it now */
  685.   init_signals ();
  686.  
  687.   if (!quiet)
  688.     {
  689.       /* Print all the junk at the top, with trailing "..." if we are about
  690.      to read a symbol file (possibly slowly).  */
  691.       print_gnu_advertisement ();
  692.       print_gdb_version (stdout);
  693.       if (symarg)
  694.     printf_filtered ("..");
  695.       wrap_here("");
  696.       fflush (stdout);        /* Force to screen during slow operations */
  697.     }
  698.  
  699.   error_pre_print = "\n\n";
  700.   /* We may get more than one warning, don't double space all of them... */
  701.   warning_pre_print = "\nwarning: ";
  702.  
  703.   /* We need a default language for parsing expressions, so simple things like
  704.      "set width 0" won't fail if no language is explicitly set in a config file
  705.      or implicitly set by reading an executable during startup. */
  706.   set_language (language_c);
  707.   expected_language = current_language;    /* don't warn about the change.  */
  708.  
  709.   /* Read and execute $HOME/.gdbinit file, if it exists.  This is done
  710.      *before* all the command line arguments are processed; it sets
  711.      global parameters, which are independent of what file you are
  712.      debugging or what directory you are in.  */
  713.   homedir = getenv ("HOME");
  714.   if (homedir)
  715.     {
  716.       homeinit = (char *) alloca (strlen (getenv ("HOME")) +
  717.                   strlen (gdbinit) + 10);
  718.       strcpy (homeinit, getenv ("HOME"));
  719.       strcat (homeinit, "/");
  720.       strcat (homeinit, gdbinit);
  721.       if (!inhibit_gdbinit && access (homeinit, R_OK) == 0)
  722.     {
  723.       if (!setjmp (to_top_level))
  724.         source_command (homeinit, 0);
  725.     }
  726.       do_cleanups (ALL_CLEANUPS);
  727.  
  728.       /* Do stats; no need to do them elsewhere since we'll only
  729.      need them if homedir is set.  Make sure that they are
  730.      zero in case one of them fails (this guarantees that they
  731.      won't match if either exists).  */
  732.       
  733.       memset (&homebuf, 0, sizeof (struct stat));
  734.       memset (&cwdbuf, 0, sizeof (struct stat));
  735.       
  736.       stat (homeinit, &homebuf);
  737.       stat (gdbinit, &cwdbuf); /* We'll only need this if
  738.                        homedir was set.  */
  739.     }
  740.   
  741.   /* Now perform all the actions indicated by the arguments.  */
  742.   if (cdarg != NULL)
  743.     {
  744.       if (!setjmp (to_top_level))
  745.     {
  746.       cd_command (cdarg, 0);
  747.       init_source_path ();
  748.     }
  749.     }
  750.   do_cleanups (ALL_CLEANUPS);
  751.  
  752.   for (i = 0; i < ndir; i++)
  753.     if (!setjmp (to_top_level))
  754.       directory_command (dirarg[i], 0);
  755.   free ((PTR)dirarg);
  756.   do_cleanups (ALL_CLEANUPS);
  757.  
  758.   if (execarg != NULL
  759.       && symarg != NULL
  760.       && STREQ (execarg, symarg))
  761.     {
  762.       /* The exec file and the symbol-file are the same.  If we can't open
  763.      it, better only print one error message.  */
  764.       if (!setjmp (to_top_level))
  765.     {
  766.       exec_file_command (execarg, !batch);
  767.       symbol_file_command (symarg, 0);
  768.     }
  769.     }
  770.   else
  771.     {
  772.       if (execarg != NULL)
  773.     if (!setjmp (to_top_level))
  774.       exec_file_command (execarg, !batch);
  775.       if (symarg != NULL)
  776.     if (!setjmp (to_top_level))
  777.       symbol_file_command (symarg, 0);
  778.     }
  779.   do_cleanups (ALL_CLEANUPS);
  780.  
  781.   /* After the symbol file has been read, print a newline to get us
  782.      beyond the copyright line...  But errors should still set off
  783.      the error message with a (single) blank line.  */
  784.   if (!quiet)
  785.     printf_filtered ("\n");
  786.   error_pre_print = "\n";
  787.   warning_pre_print = "\nwarning: ";
  788.  
  789.   if (corearg != NULL)
  790.     if (!setjmp (to_top_level))
  791.       core_file_command (corearg, !batch);
  792.     else if (isdigit (corearg[0]) && !setjmp (to_top_level))
  793.       attach_command (corearg, !batch);
  794.   do_cleanups (ALL_CLEANUPS);
  795.  
  796.   if (ttyarg != NULL)
  797.     if (!setjmp (to_top_level))
  798.       tty_command (ttyarg, !batch);
  799.   do_cleanups (ALL_CLEANUPS);
  800.  
  801. #ifdef ADDITIONAL_OPTION_HANDLER
  802.   ADDITIONAL_OPTION_HANDLER;
  803. #endif
  804.  
  805.   /* Error messages should no longer be distinguished with extra output. */
  806.   error_pre_print = 0;
  807.   warning_pre_print = "warning: ";
  808.  
  809.   /* Read the .gdbinit file in the current directory, *if* it isn't
  810.      the same as the $HOME/.gdbinit file (it should exist, also).  */
  811.   
  812.   if (!homedir
  813.       || memcmp ((char *) &homebuf, (char *) &cwdbuf, sizeof (struct stat)))
  814.     if (!inhibit_gdbinit && access (gdbinit, R_OK) == 0)
  815.       {
  816.     if (!setjmp (to_top_level))
  817.       source_command (gdbinit, 0);
  818.       }
  819.   do_cleanups (ALL_CLEANUPS);
  820.  
  821.   for (i = 0; i < ncmd; i++)
  822.     {
  823.       if (!setjmp (to_top_level))
  824.     {
  825.       if (cmdarg[i][0] == '-' && cmdarg[i][1] == '\0')
  826.         read_command_file (stdin);
  827.       else
  828.         source_command (cmdarg[i], !batch);
  829.       do_cleanups (ALL_CLEANUPS);
  830.     }
  831.     }
  832.   free ((PTR)cmdarg);
  833.  
  834.   /* Read in the old history after all the command files have been read. */
  835.   initialize_history();
  836.  
  837.   if (batch)
  838.     {
  839.       /* We have hit the end of the batch file.  */
  840.       exit (0);
  841.     }
  842.  
  843.   /* Do any host- or target-specific hacks.  This is used for i960 targets
  844.      to force the user to set a nindy target and spec its parameters.  */
  845.  
  846. #ifdef BEFORE_MAIN_LOOP_HOOK
  847.   BEFORE_MAIN_LOOP_HOOK;
  848. #endif
  849.  
  850.   /* The command loop.  */
  851.  
  852.   while (1)
  853.     {
  854.       if (!setjmp (to_top_level))
  855.     {
  856.       do_cleanups (ALL_CLEANUPS);        /* Do complete cleanup */
  857.       command_loop ();
  858.           quit_command ((char *)0, instream == stdin);
  859.     }
  860.     }
  861.   /* No exit -- exit is through quit_command.  */
  862. }
  863.  
  864. void
  865. execute_user_command (c, args)
  866.      struct cmd_list_element *c;
  867.      char *args;
  868. {
  869.   register struct command_line *cmdlines;
  870.   struct cleanup *old_chain;
  871.   
  872.   if (args)
  873.     error ("User-defined commands cannot take arguments.");
  874.  
  875.   cmdlines = c->user_commands;
  876.   if (cmdlines == 0)
  877.     /* Null command */
  878.     return;
  879.  
  880.   /* Set the instream to 0, indicating execution of a
  881.      user-defined function.  */
  882.   old_chain = make_cleanup (source_cleanup, instream);
  883.   instream = (FILE *) 0;
  884.   while (cmdlines)
  885.     {
  886.       execute_command (cmdlines->line, 0);
  887.       cmdlines = cmdlines->next;
  888.     }
  889.   do_cleanups (old_chain);
  890. }
  891.  
  892. /* Execute the line P as a command.
  893.    Pass FROM_TTY as second argument to the defining function.  */
  894.  
  895. void
  896. execute_command (p, from_tty)
  897.      char *p;
  898.      int from_tty;
  899. {
  900.   register struct cmd_list_element *c;
  901.   register enum language flang;
  902.   static int warned = 0;
  903.  
  904.   free_all_values ();
  905.  
  906.   /* This can happen when command_line_input hits end of file.  */
  907.   if (p == NULL)
  908.       return;
  909.   
  910.   while (*p == ' ' || *p == '\t') p++;
  911.   if (*p)
  912.     {
  913.       char *arg;
  914.       
  915.       c = lookup_cmd (&p, cmdlist, "", 0, 1);
  916.       /* Pass null arg rather than an empty one.  */
  917.       arg = *p ? p : 0;
  918.  
  919.       /* If this command has been hooked, run the hook first. */
  920.       if (c->hook)
  921.     execute_user_command (c->hook, (char *)0);
  922.  
  923.       if (c->class == class_user)
  924.     execute_user_command (c, arg);
  925.       else if (c->type == set_cmd || c->type == show_cmd)
  926.     do_setshow_command (arg, from_tty & caution, c);
  927.       else if (c->function.cfunc == NO_FUNCTION)
  928.     error ("That is not a command, just a help topic.");
  929.       else
  930.     (*c->function.cfunc) (arg, from_tty & caution);
  931.    }
  932.  
  933.   /* Tell the user if the language has changed (except first time).  */
  934.   if (current_language != expected_language)
  935.   {
  936.     if (language_mode == language_mode_auto) {
  937.       language_info (1);    /* Print what changed.  */
  938.     }
  939.     warned = 0;
  940.   }
  941.  
  942.   /* Warn the user if the working language does not match the
  943.      language of the current frame.  Only warn the user if we are
  944.      actually running the program, i.e. there is a stack. */
  945.   /* FIXME:  This should be cacheing the frame and only running when
  946.      the frame changes.  */
  947.   if (target_has_stack)
  948.   {
  949.     flang = get_frame_language ();
  950.     if (!warned
  951.         && flang != language_unknown
  952.     && flang != current_language->la_language)
  953.     {
  954.       printf_filtered ("%s\n", lang_frame_mismatch_warn);
  955.       warned = 1;
  956.     }
  957.   }
  958. }
  959.  
  960. /* ARGSUSED */
  961. static void
  962. command_loop_marker (foo)
  963.      int foo;
  964. {
  965. }
  966.  
  967. /* Read commands from `instream' and execute them
  968.    until end of file or error reading instream.  */
  969. static void
  970. command_loop ()
  971. {
  972.   struct cleanup *old_chain;
  973.   char *command;
  974.   int stdin_is_tty = ISATTY (stdin);
  975.  
  976.   while (!feof (instream))
  977.     {
  978.       if (window_hook && instream == stdin)
  979.     (*window_hook) (instream, prompt);
  980.  
  981.       quit_flag = 0;
  982.       if (instream == stdin && stdin_is_tty)
  983.     reinitialize_more_filter ();
  984.       old_chain = make_cleanup (command_loop_marker, 0);
  985.       command = command_line_input (instream == stdin ? prompt : (char *) NULL,
  986.                       instream == stdin);
  987.       if (command == 0)
  988.     return;
  989.       execute_command (command, instream == stdin);
  990.       /* Do any commands attached to breakpoint we stopped at.  */
  991.       bpstat_do_actions (&stop_bpstat);
  992.       do_cleanups (old_chain);
  993.     }
  994. }
  995.  
  996. /* Commands call this if they do not want to be repeated by null lines.  */
  997.  
  998. void
  999. dont_repeat ()
  1000. {
  1001.   /* If we aren't reading from standard input, we are saving the last
  1002.      thing read from stdin in line and don't want to delete it.  Null lines
  1003.      won't repeat here in any case.  */
  1004.   if (instream == stdin)
  1005.     *line = 0;
  1006. }
  1007.  
  1008. /* Read a line from the stream "instream" without command line editing.
  1009.  
  1010.    It prints PRROMPT once at the start.
  1011.    Action is compatible with "readline", e.g. space for the result is 
  1012.    malloc'd and should be freed by the caller.
  1013.  
  1014.    A NULL return means end of file.  */
  1015. char *
  1016. gdb_readline (prrompt)
  1017.      char *prrompt;
  1018. {
  1019.   int c;
  1020.   char *result;
  1021.   int input_index = 0;
  1022.   int result_size = 80;
  1023.  
  1024.   if (prrompt)
  1025.     {
  1026.       /* Don't use a _filtered function here.  It causes the assumed
  1027.      character position to be off, since the newline we read from
  1028.      the user is not accounted for.  */
  1029.       fputs (prrompt, stdout);
  1030.       fflush (stdout);
  1031.     }
  1032.   
  1033.   result = (char *) xmalloc (result_size);
  1034.  
  1035.   while (1)
  1036.     {
  1037.       /* Read from stdin if we are executing a user defined command.
  1038.      This is the right thing for prompt_for_continue, at least.  */
  1039.       c = fgetc (instream ? instream : stdin);
  1040.  
  1041.       if (c == EOF)
  1042.     {
  1043.       free (result);
  1044.       return NULL;
  1045.     }
  1046.  
  1047.       if (c == '\n')
  1048.     break;
  1049.  
  1050.       result[input_index++] = c;
  1051.       while (input_index >= result_size)
  1052.     {
  1053.       result_size *= 2;
  1054.       result = (char *) xrealloc (result, result_size);
  1055.     }
  1056.     }
  1057.  
  1058.   result[input_index++] = '\0';
  1059.   return result;
  1060. }
  1061.  
  1062. /* Variables which control command line editing and history
  1063.    substitution.  These variables are given default values at the end
  1064.    of this file.  */
  1065. static int command_editing_p;
  1066. static int history_expansion_p;
  1067. static int write_history_p;
  1068. static int history_size;
  1069. static char *history_filename;
  1070.  
  1071. /* Variables which are necessary for fancy command line editing.  */
  1072. char *gdb_completer_word_break_characters =
  1073.   " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
  1074.  
  1075. /* When completing on command names, we remove '-' from the list of
  1076.    word break characters, since we use it in command names.  If the
  1077.    readline library sees one in any of the current completion strings,
  1078.    it thinks that the string needs to be quoted and automatically supplies
  1079.    a leading quote. */
  1080. char *gdb_completer_command_word_break_characters =
  1081.   " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
  1082.  
  1083. /* Characters that can be used to quote completion strings.  Note that we
  1084.    can't include '"' because the gdb C parser treats such quoted sequences
  1085.    as strings. */
  1086. char *gdb_completer_quote_characters =
  1087.   "'";
  1088.  
  1089. /* Functions that are used as part of the fancy command line editing.  */
  1090.  
  1091. /* This can be used for functions which don't want to complete on symbols
  1092.    but don't want to complete on anything else either.  */
  1093. /* ARGSUSED */
  1094. char **
  1095. noop_completer (text)
  1096.      char *text;
  1097. {
  1098.   return NULL;
  1099. }
  1100.  
  1101. /* Generate symbol names one by one for the completer.  Each time we are
  1102.    called return another potential completion to the caller.
  1103.  
  1104.    TEXT is what we expect the symbol to start with.
  1105.  
  1106.    MATCHES is the number of matches that have currently been collected from
  1107.    calling this completion function.  When zero, then we need to initialize,
  1108.    otherwise the initialization has already taken place and we can just
  1109.    return the next potential completion string.
  1110.  
  1111.    Returns NULL if there are no more completions, else a pointer to a string
  1112.    which is a possible completion.
  1113.  
  1114.    RL_LINE_BUFFER is available to be looked at; it contains the entire text
  1115.    of the line.  RL_POINT is the offset in that line of the cursor.  You
  1116.    should pretend that the line ends at RL_POINT. */
  1117.    
  1118. static char *
  1119. symbol_completion_function (text, matches)
  1120.      char *text;
  1121.      int matches;
  1122. {
  1123.   static char **list = (char **)NULL;        /* Cache of completions */
  1124.   static int index;                /* Next cached completion */
  1125.   char *output = NULL;
  1126.   char *tmp_command, *p;
  1127.   struct cmd_list_element *c, *result_list;
  1128.   extern char *rl_line_buffer;
  1129.   extern int rl_point;
  1130.  
  1131.   if (matches == 0)
  1132.     {
  1133.       /* The caller is beginning to accumulate a new set of completions, so
  1134.      we need to find all of them now, and cache them for returning one at
  1135.      a time on future calls. */
  1136.  
  1137.       if (list)
  1138.     {
  1139.       /* Free the storage used by LIST, but not by the strings inside.
  1140.          This is because rl_complete_internal () frees the strings. */
  1141.       free ((PTR)list);
  1142.     }
  1143.       list = 0;
  1144.       index = 0;
  1145.  
  1146.       /* Choose the default set of word break characters to break completions.
  1147.      If we later find out that we are doing completions on command strings
  1148.      (as opposed to strings supplied by the individual command completer
  1149.      functions, which can be any string) then we will switch to the
  1150.      special word break set for command strings, which leaves out the
  1151.      '-' character used in some commands. */
  1152.  
  1153.       rl_completer_word_break_characters =
  1154.       gdb_completer_word_break_characters;
  1155.  
  1156.       /* Decide whether to complete on a list of gdb commands or on symbols. */
  1157.       tmp_command = (char *) alloca (rl_point + 1);
  1158.       p = tmp_command;
  1159.       
  1160.       strncpy (tmp_command, rl_line_buffer, rl_point);
  1161.       tmp_command[rl_point] = '\0';
  1162.  
  1163.       if (rl_point == 0)
  1164.     {
  1165.       /* An empty line we want to consider ambiguous; that is, it
  1166.          could be any command.  */
  1167.       c = (struct cmd_list_element *) -1;
  1168.       result_list = 0;
  1169.     }
  1170.       else
  1171.     {
  1172.       c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
  1173.     }
  1174.  
  1175.       /* Move p up to the next interesting thing.  */
  1176.       while (*p == ' ' || *p == '\t')
  1177.     {
  1178.       p++;
  1179.     }
  1180.  
  1181.       if (!c)
  1182.     {
  1183.       /* He's typed something unrecognizable.  Sigh.  */
  1184.       list = NULL;
  1185.     }
  1186.       else if (c == (struct cmd_list_element *) -1)
  1187.     {
  1188.       /* If we didn't recognize everything up to the thing that
  1189.          needs completing, and we don't know what command it is
  1190.          yet, we are in trouble. */
  1191.  
  1192.       if (p + strlen(text) != tmp_command + rl_point)
  1193.         {
  1194.           /* This really should not produce an error.  Better would
  1195.          be to pretend to hit RETURN here; this would produce a
  1196.          response like "Ambiguous command: foo, foobar, etc",
  1197.          and leave the line available for re-entry with ^P.
  1198.          Instead, this error blows away the user's typed input
  1199.          without any way to get it back.  */
  1200.           error ("  Unrecognized command.");
  1201.         }
  1202.       
  1203.       /* He's typed something ambiguous.  This is easier.  */
  1204.       if (result_list)
  1205.         {
  1206.           list = complete_on_cmdlist (*result_list->prefixlist, text);
  1207.         }
  1208.       else
  1209.         {
  1210.           list = complete_on_cmdlist (cmdlist, text);
  1211.         }
  1212.       rl_completer_word_break_characters =
  1213.           gdb_completer_command_word_break_characters;
  1214.     }
  1215.       else
  1216.     {
  1217.       /* If we've gotten this far, gdb has recognized a full
  1218.          command.  There are several possibilities:
  1219.  
  1220.          1) We need to complete on the command.
  1221.          2) We need to complete on the possibilities coming after
  1222.          the command.
  1223.          2) We need to complete the text of what comes after the
  1224.          command.   */
  1225.  
  1226.       if (!*p && *text)
  1227.         {
  1228.           /* Always (might be longer versions of thie command).  */
  1229.           list = complete_on_cmdlist (result_list, text);
  1230.           rl_completer_word_break_characters =
  1231.           gdb_completer_command_word_break_characters;
  1232.         }
  1233.       else if (!*p && !*text)
  1234.         {
  1235.           if (c->prefixlist)
  1236.         {
  1237.           list = complete_on_cmdlist (*c->prefixlist, "");
  1238.           rl_completer_word_break_characters =
  1239.               gdb_completer_command_word_break_characters;
  1240.         }
  1241.           else
  1242.         {
  1243.           list = (*c->completer) ("");
  1244.         }
  1245.         }
  1246.       else
  1247.         {
  1248.           if (c->prefixlist && !c->allow_unknown)
  1249.         {
  1250.           /* Something like "info adsfkdj".  But error() is not the
  1251.              proper response; just return no completions instead. */
  1252.           list = NULL;
  1253.         }
  1254.           else
  1255.         {
  1256.           list = (*c->completer) (text);
  1257.         }
  1258.         }
  1259.     }
  1260.     }
  1261.  
  1262.   /* If we found a list of potential completions during initialization then
  1263.      dole them out one at a time.  The vector of completions is NULL
  1264.      terminated, so after returning the last one, return NULL (and continue
  1265.      to do so) each time we are called after that, until a new list is
  1266.      available. */
  1267.  
  1268.   if (list)
  1269.     {
  1270.       output = list[index];
  1271.       if (output)
  1272.     {
  1273.       index++;
  1274.     }
  1275.     }
  1276.  
  1277.   return (output);
  1278. }
  1279.  
  1280. /* Skip over a possibly quoted word (as defined by the quote characters
  1281.    and word break characters the completer uses).  Returns pointer to the
  1282.    location after the "word". */
  1283.  
  1284. char *
  1285. skip_quoted (str)
  1286.      char *str;
  1287. {
  1288.   char quote_char = '\0';
  1289.   char *scan;
  1290.  
  1291.   for (scan = str; *scan != '\0'; scan++)
  1292.     {
  1293.       if (quote_char != '\0')
  1294.     {
  1295.       /* Ignore everything until the matching close quote char */
  1296.       if (*scan == quote_char)
  1297.         {
  1298.           /* Found matching close quote. */
  1299.           scan++;
  1300.           break;
  1301.         }
  1302.     }
  1303.       else if (strchr (gdb_completer_quote_characters, *scan))
  1304.     {
  1305.       /* Found start of a quoted string. */
  1306.       quote_char = *scan;
  1307.     }
  1308.       else if (strchr (gdb_completer_word_break_characters, *scan))
  1309.     {
  1310.       break;
  1311.     }
  1312.     }
  1313.   return (scan);
  1314. }
  1315.  
  1316.  
  1317. #ifdef STOP_SIGNAL
  1318. static void
  1319. stop_sig (signo)
  1320. int signo;
  1321. {
  1322. #if STOP_SIGNAL == SIGTSTP
  1323.   signal (SIGTSTP, SIG_DFL);
  1324.   sigsetmask (0);
  1325.   kill (getpid (), SIGTSTP);
  1326.   signal (SIGTSTP, stop_sig);
  1327. #else
  1328.   signal (STOP_SIGNAL, stop_sig);
  1329. #endif
  1330.   printf ("%s", prompt);
  1331.   fflush (stdout);
  1332.  
  1333.   /* Forget about any previous command -- null line now will do nothing.  */
  1334.   dont_repeat ();
  1335. }
  1336. #endif /* STOP_SIGNAL */
  1337.  
  1338. /* Initialize signal handlers. */
  1339. static void
  1340. do_nothing (signo)
  1341. int signo;
  1342. {
  1343. }
  1344.  
  1345. static void
  1346. init_signals ()
  1347. {
  1348.   signal (SIGINT, request_quit);
  1349.  
  1350.   /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
  1351.      passed to the inferior, which we don't want.  It would be
  1352.      possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
  1353.      on BSD4.3 systems using vfork, that can affect the
  1354.      GDB process as well as the inferior (the signal handling tables
  1355.      might be in memory, shared between the two).  Since we establish
  1356.      a handler for SIGQUIT, when we call exec it will set the signal
  1357.      to SIG_DFL for us.  */
  1358.   signal (SIGQUIT, do_nothing);
  1359.   if (signal (SIGHUP, do_nothing) != SIG_IGN)
  1360.     signal (SIGHUP, disconnect);
  1361.   signal (SIGFPE, float_handler);
  1362.  
  1363. #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
  1364.   signal (SIGWINCH, SIGWINCH_HANDLER);
  1365. #endif
  1366. }
  1367.  
  1368. /* Read one line from the command input stream `instream'
  1369.    into the local static buffer `linebuffer' (whose current length
  1370.    is `linelength').
  1371.    The buffer is made bigger as necessary.
  1372.    Returns the address of the start of the line.
  1373.  
  1374.    NULL is returned for end of file.
  1375.  
  1376.    *If* the instream == stdin & stdin is a terminal, the line read
  1377.    is copied into the file line saver (global var char *line,
  1378.    length linesize) so that it can be duplicated.
  1379.  
  1380.    This routine either uses fancy command line editing or
  1381.    simple input as the user has requested.  */
  1382.  
  1383. char *
  1384. command_line_input (prrompt, repeat)
  1385.      char *prrompt;
  1386.      int repeat;
  1387. {
  1388.   static char *linebuffer = 0;
  1389.   static unsigned linelength = 0;
  1390.   register char *p;
  1391.   char *p1;
  1392.   char *rl;
  1393.   char *local_prompt = prrompt;
  1394.   register int c;
  1395.   char *nline;
  1396.   char got_eof = 0;
  1397.  
  1398.   if (linebuffer == 0)
  1399.     {
  1400.       linelength = 80;
  1401.       linebuffer = (char *) xmalloc (linelength);
  1402.     }
  1403.  
  1404.   p = linebuffer;
  1405.  
  1406.   /* Control-C quits instantly if typed while in this loop
  1407.      since it should not wait until the user types a newline.  */
  1408.   immediate_quit++;
  1409. #ifdef STOP_SIGNAL
  1410.   signal (STOP_SIGNAL, stop_sig);
  1411. #endif
  1412.  
  1413.   while (1)
  1414.     {
  1415.       /* Reports are that some Sys V's don't flush stdout/err on reads
  1416.      from stdin, when stdin/out are sockets rather than ttys.  So we
  1417.      have to do it ourselves, to make emacs-gdb and xxgdb work.
  1418.      On other machines, doing this once per input should be a cheap nop.  */
  1419.       fflush (stdout);
  1420.       fflush (stderr);
  1421.  
  1422.       /* Don't use fancy stuff if not talking to stdin.  */
  1423.       if (command_editing_p && instream == stdin
  1424.       && ISATTY (instream))
  1425.     rl = readline (local_prompt);
  1426.       else
  1427.     rl = gdb_readline (local_prompt);
  1428.  
  1429.       if (!rl || rl == (char *) EOF)
  1430.     {
  1431.       got_eof = 1;
  1432.       break;
  1433.     }
  1434.       if (strlen(rl) + 1 + (p - linebuffer) > linelength)
  1435.     {
  1436.       linelength = strlen(rl) + 1 + (p - linebuffer);
  1437.       nline = (char *) xrealloc (linebuffer, linelength);
  1438.       p += nline - linebuffer;
  1439.       linebuffer = nline;
  1440.     }
  1441.       p1 = rl;
  1442.       /* Copy line.  Don't copy null at end.  (Leaves line alone
  1443.          if this was just a newline)  */
  1444.       while (*p1)
  1445.     *p++ = *p1++;
  1446.  
  1447.       free (rl);            /* Allocated in readline.  */
  1448.  
  1449.       if (p == linebuffer || *(p - 1) != '\\')
  1450.     break;
  1451.  
  1452.       p--;            /* Put on top of '\'.  */
  1453.       local_prompt = (char *) 0;
  1454.   }
  1455.  
  1456. #ifdef STOP_SIGNAL
  1457.   signal (SIGTSTP, SIG_DFL);
  1458. #endif
  1459.   immediate_quit--;
  1460.  
  1461.   if (got_eof)
  1462.     return NULL;
  1463.  
  1464.   /* Do history expansion if that is wished.  */
  1465.   if (history_expansion_p && instream == stdin
  1466.       && ISATTY (instream))
  1467.     {
  1468.       char *history_value;
  1469.       int expanded;
  1470.  
  1471.       *p = '\0';        /* Insert null now.  */
  1472.       expanded = history_expand (linebuffer, &history_value);
  1473.       if (expanded)
  1474.     {
  1475.       /* Print the changes.  */
  1476.       printf ("%s\n", history_value);
  1477.  
  1478.       /* If there was an error, call this function again.  */
  1479.       if (expanded < 0)
  1480.         {
  1481.           free (history_value);
  1482.           return command_line_input (prrompt, repeat);
  1483.         }
  1484.       if (strlen (history_value) > linelength)
  1485.         {
  1486.           linelength = strlen (history_value) + 1;
  1487.           linebuffer = (char *) xrealloc (linebuffer, linelength);
  1488.         }
  1489.       strcpy (linebuffer, history_value);
  1490.       p = linebuffer + strlen(linebuffer);
  1491.       free (history_value);
  1492.     }
  1493.     }
  1494.  
  1495.   /* If we just got an empty line, and that is supposed
  1496.      to repeat the previous command, return the value in the
  1497.      global buffer.  */
  1498.   if (repeat)
  1499.     {
  1500.       if (p == linebuffer)
  1501.     return line;
  1502.       p1 = linebuffer;
  1503.       while (*p1 == ' ' || *p1 == '\t')
  1504.     p1++;
  1505.       if (!*p1)
  1506.     return line;
  1507.     }
  1508.  
  1509.   *p = 0;
  1510.  
  1511.   /* Add line to history if appropriate.  */
  1512.   if (instream == stdin
  1513.       && ISATTY (stdin) && *linebuffer)
  1514.     add_history (linebuffer);
  1515.  
  1516.   /* Note: lines consisting soley of comments are added to the command
  1517.      history.  This is useful when you type a command, and then
  1518.      realize you don't want to execute it quite yet.  You can comment
  1519.      out the command and then later fetch it from the value history
  1520.      and remove the '#'.  The kill ring is probably better, but some
  1521.      people are in the habit of commenting things out.  */
  1522.   p1 = linebuffer;
  1523.   while ((c = *p1++) != '\0')
  1524.     {
  1525.       if (c == '"')
  1526.     while ((c = *p1++) != '"')
  1527.       {
  1528.         /* Make sure an escaped '"' doesn't make us think the string
  1529.            is ended.  */
  1530.         if (c == '\\')
  1531.           parse_escape (&p1);
  1532.         if (c == '\0')
  1533.           break;
  1534.       }
  1535.       else if (c == '\'')
  1536.     while ((c = *p1++) != '\'')
  1537.       {
  1538.         /* Make sure an escaped '\'' doesn't make us think the string
  1539.            is ended.  */
  1540.         if (c == '\\')
  1541.           parse_escape (&p1);
  1542.         if (c == '\0')
  1543.           break;
  1544.       }
  1545.       else if (c == '#')
  1546.     {
  1547.       /* Found a comment.  */
  1548.       p1[-1] = '\0';
  1549.       break;
  1550.     }
  1551.     }
  1552.  
  1553.   /* Save into global buffer if appropriate.  */
  1554.   if (repeat)
  1555.     {
  1556.       if (linelength > linesize)
  1557.     {
  1558.       line = xrealloc (line, linelength);
  1559.       linesize = linelength;
  1560.     }
  1561.       strcpy (line, linebuffer);
  1562.       return line;
  1563.     }
  1564.  
  1565.   return linebuffer;
  1566. }
  1567.  
  1568. /* Read lines from the input stream
  1569.    and accumulate them in a chain of struct command_line's
  1570.    which is then returned.  */
  1571.  
  1572. struct command_line *
  1573. read_command_lines ()
  1574. {
  1575.   struct command_line *first = 0;
  1576.   register struct command_line *next, *tail = 0;
  1577.   register char *p, *p1;
  1578.   struct cleanup *old_chain = 0;
  1579.  
  1580.   while (1)
  1581.     {
  1582.       dont_repeat ();
  1583.       p = command_line_input ((char *) NULL, instream == stdin);
  1584.       if (p == NULL)
  1585.     /* Treat end of file like "end".  */
  1586.     break;
  1587.       
  1588.       /* Remove leading and trailing blanks.  */
  1589.       while (*p == ' ' || *p == '\t') p++;
  1590.       p1 = p + strlen (p);
  1591.       while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t')) p1--;
  1592.  
  1593.       /* Is this "end"?  */
  1594.       if (p1 - p == 3 && !strncmp (p, "end", 3))
  1595.     break;
  1596.  
  1597.       /* No => add this line to the chain of command lines.  */
  1598.       next = (struct command_line *) xmalloc (sizeof (struct command_line));
  1599.       next->line = savestring (p, p1 - p);
  1600.       next->next = 0;
  1601.       if (tail)
  1602.     {
  1603.       tail->next = next;
  1604.     }
  1605.       else
  1606.     {
  1607.       /* We just read the first line.
  1608.          From now on, arrange to throw away the lines we have
  1609.          if we quit or get an error while inside this function.  */
  1610.       first = next;
  1611.       old_chain = make_cleanup (free_command_lines, &first);
  1612.     }
  1613.       tail = next;
  1614.     }
  1615.  
  1616.   dont_repeat ();
  1617.  
  1618.   /* Now we are about to return the chain to our caller,
  1619.      so freeing it becomes his responsibility.  */
  1620.   if (first)
  1621.     discard_cleanups (old_chain);
  1622.   return first;
  1623. }
  1624.  
  1625. /* Free a chain of struct command_line's.  */
  1626.  
  1627. void
  1628. free_command_lines (lptr)
  1629.       struct command_line **lptr;
  1630. {
  1631.   register struct command_line *l = *lptr;
  1632.   register struct command_line *next;
  1633.  
  1634.   while (l)
  1635.     {
  1636.       next = l->next;
  1637.       free (l->line);
  1638.       free ((PTR)l);
  1639.       l = next;
  1640.     }
  1641. }
  1642.  
  1643. /* Add an element to the list of info subcommands.  */
  1644.  
  1645. void
  1646. add_info (name, fun, doc)
  1647.      char *name;
  1648.      void (*fun) PARAMS ((char *, int));
  1649.      char *doc;
  1650. {
  1651.   add_cmd (name, no_class, fun, doc, &infolist);
  1652. }
  1653.  
  1654. /* Add an alias to the list of info subcommands.  */
  1655.  
  1656. void
  1657. add_info_alias (name, oldname, abbrev_flag)
  1658.      char *name;
  1659.      char *oldname;
  1660.      int abbrev_flag;
  1661. {
  1662.   add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
  1663. }
  1664.  
  1665. /* The "info" command is defined as a prefix, with allow_unknown = 0.
  1666.    Therefore, its own definition is called only for "info" with no args.  */
  1667.  
  1668. /* ARGSUSED */
  1669. static void
  1670. info_command (arg, from_tty)
  1671.      char *arg;
  1672.      int from_tty;
  1673. {
  1674.   printf ("\"info\" must be followed by the name of an info command.\n");
  1675.   help_list (infolist, "info ", -1, stdout);
  1676. }
  1677.  
  1678. /* The "show" command with no arguments shows all the settings.  */
  1679.  
  1680. /* ARGSUSED */
  1681. static void
  1682. show_command (arg, from_tty)
  1683.      char *arg;
  1684.      int from_tty;
  1685. {
  1686.   cmd_show_list (showlist, from_tty, "");
  1687. }
  1688.  
  1689. /* Add an element to the list of commands.  */
  1690.  
  1691. void
  1692. add_com (name, class, fun, doc)
  1693.      char *name;
  1694.      enum command_class class;
  1695.      void (*fun) PARAMS ((char *, int));
  1696.      char *doc;
  1697. {
  1698.   add_cmd (name, class, fun, doc, &cmdlist);
  1699. }
  1700.  
  1701. /* Add an alias or abbreviation command to the list of commands.  */
  1702.  
  1703. void
  1704. add_com_alias (name, oldname, class, abbrev_flag)
  1705.      char *name;
  1706.      char *oldname;
  1707.      enum command_class class;
  1708.      int abbrev_flag;
  1709. {
  1710.   add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
  1711. }
  1712.  
  1713. void
  1714. error_no_arg (why)
  1715.      char *why;
  1716. {
  1717.   error ("Argument required (%s).", why);
  1718. }
  1719.  
  1720. /* ARGSUSED */
  1721. static void
  1722. help_command (command, from_tty)
  1723.      char *command;
  1724.      int from_tty; /* Ignored */
  1725. {
  1726.   help_cmd (command, stdout);
  1727. }
  1728.  
  1729. static void
  1730. validate_comname (comname)
  1731.      char *comname;
  1732. {
  1733.   register char *p;
  1734.  
  1735.   if (comname == 0)
  1736.     error_no_arg ("name of command to define");
  1737.  
  1738.   p = comname;
  1739.   while (*p)
  1740.     {
  1741.       if (!isalnum(*p) && *p != '-')
  1742.     error ("Junk in argument list: \"%s\"", p);
  1743.       p++;
  1744.     }
  1745. }
  1746.  
  1747. /* This is just a placeholder in the command data structures.  */
  1748. static void
  1749. user_defined_command (ignore, from_tty)
  1750.      char *ignore;
  1751.      int from_tty;
  1752. {
  1753. }
  1754.  
  1755. static void
  1756. define_command (comname, from_tty)
  1757.      char *comname;
  1758.      int from_tty;
  1759. {
  1760.   register struct command_line *cmds;
  1761.   register struct cmd_list_element *c, *newc, *hookc = 0;
  1762.   char *tem = comname;
  1763. #define    HOOK_STRING    "hook-"
  1764. #define    HOOK_LEN 5
  1765.  
  1766.   validate_comname (comname);
  1767.  
  1768.   /* Look it up, and verify that we got an exact match.  */
  1769.   c = lookup_cmd (&tem, cmdlist, "", -1, 1);
  1770.   if (c && !STREQ (comname, c->name))
  1771.     c = 0;
  1772.     
  1773.   if (c)
  1774.     {
  1775.       if (c->class == class_user || c->class == class_alias)
  1776.     tem = "Redefine command \"%s\"? ";
  1777.       else
  1778.     tem = "Really redefine built-in command \"%s\"? ";
  1779.       if (!query (tem, c->name))
  1780.     error ("Command \"%s\" not redefined.", c->name);
  1781.     }
  1782.  
  1783.   /* If this new command is a hook, then mark the command which it
  1784.      is hooking.  Note that we allow hooking `help' commands, so that
  1785.      we can hook the `stop' pseudo-command.  */
  1786.  
  1787.   if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
  1788.     {
  1789.       /* Look up cmd it hooks, and verify that we got an exact match.  */
  1790.       tem = comname+HOOK_LEN;
  1791.       hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
  1792.       if (hookc && !STREQ (comname+HOOK_LEN, hookc->name))
  1793.     hookc = 0;
  1794.       if (!hookc)
  1795.     {
  1796.       warning ("Your new `%s' command does not hook any existing command.",
  1797.            comname);
  1798.       if (!query ("Proceed? ", (char *)0))
  1799.         error ("Not confirmed.");
  1800.     }
  1801.     }
  1802.  
  1803.   comname = savestring (comname, strlen (comname));
  1804.  
  1805.   /* If the rest of the commands will be case insensitive, this one 
  1806.      should behave in the same manner. */
  1807.   for (tem = comname; *tem; tem++)
  1808.     if (isupper(*tem)) *tem = tolower(*tem);
  1809.  
  1810.   if (from_tty)
  1811.     {
  1812.       printf ("Type commands for definition of \"%s\".\n\
  1813. End with a line saying just \"end\".\n", comname);
  1814.       fflush (stdout);
  1815.     }
  1816.  
  1817.   cmds = read_command_lines ();
  1818.  
  1819.   if (c && c->class == class_user)
  1820.     free_command_lines (&c->user_commands);
  1821.  
  1822.   newc = add_cmd (comname, class_user, user_defined_command,
  1823.        (c && c->class == class_user)
  1824.        ? c->doc : savestring ("User-defined.", 13), &cmdlist);
  1825.   newc->user_commands = cmds;
  1826.  
  1827.   /* If this new command is a hook, then mark both commands as being
  1828.      tied.  */
  1829.   if (hookc)
  1830.     {
  1831.       hookc->hook = newc;    /* Target gets hooked.  */
  1832.       newc->hookee = hookc;    /* We are marked as hooking target cmd.  */
  1833.     }
  1834. }
  1835.  
  1836. static void
  1837. document_command (comname, from_tty)
  1838.      char *comname;
  1839.      int from_tty;
  1840. {
  1841.   struct command_line *doclines;
  1842.   register struct cmd_list_element *c;
  1843.   char *tem = comname;
  1844.  
  1845.   validate_comname (comname);
  1846.  
  1847.   c = lookup_cmd (&tem, cmdlist, "", 0, 1);
  1848.  
  1849.   if (c->class != class_user)
  1850.     error ("Command \"%s\" is built-in.", comname);
  1851.  
  1852.   if (from_tty)
  1853.     printf ("Type documentation for \"%s\".\n\
  1854. End with a line saying just \"end\".\n", comname);
  1855.  
  1856.   doclines = read_command_lines ();
  1857.  
  1858.   if (c->doc) free (c->doc);
  1859.  
  1860.   {
  1861.     register struct command_line *cl1;
  1862.     register int len = 0;
  1863.  
  1864.     for (cl1 = doclines; cl1; cl1 = cl1->next)
  1865.       len += strlen (cl1->line) + 1;
  1866.  
  1867.     c->doc = (char *) xmalloc (len + 1);
  1868.     *c->doc = 0;
  1869.  
  1870.     for (cl1 = doclines; cl1; cl1 = cl1->next)
  1871.       {
  1872.     strcat (c->doc, cl1->line);
  1873.     if (cl1->next)
  1874.       strcat (c->doc, "\n");
  1875.       }
  1876.   }
  1877.  
  1878.   free_command_lines (&doclines);
  1879. }
  1880.  
  1881. static void
  1882. print_gnu_advertisement()
  1883. {
  1884.     printf ("\
  1885. GDB is free software and you are welcome to distribute copies of it\n\
  1886.  under certain conditions; type \"show copying\" to see the conditions.\n\
  1887. There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\
  1888. ");
  1889. }
  1890.  
  1891. static void
  1892. print_gdb_version (stream)
  1893.   FILE *stream;
  1894. {
  1895.   fprintf_filtered (stream, "\
  1896. GDB %s, Copyright 1993 Free Software Foundation, Inc.",
  1897.       version);
  1898. }
  1899.  
  1900. /* ARGSUSED */
  1901. static void
  1902. show_version (args, from_tty)
  1903.      char *args;
  1904.      int from_tty;
  1905. {
  1906.   immediate_quit++;
  1907.   print_gnu_advertisement ();
  1908.   print_gdb_version (stdout);
  1909.   printf_filtered ("\n");
  1910.   immediate_quit--;
  1911. }
  1912.  
  1913. /* xgdb calls this to reprint the usual GDB prompt.  */
  1914.  
  1915. void
  1916. print_prompt ()
  1917. {
  1918.   printf ("%s", prompt);
  1919.   fflush (stdout);
  1920. }
  1921.  
  1922. static void
  1923. quit_command (args, from_tty)
  1924.      char *args;
  1925.      int from_tty;
  1926. {
  1927.   if (inferior_pid != 0 && target_has_execution)
  1928.     {
  1929.       if (query ("The program is running.  Quit anyway? "))
  1930.     {
  1931.       if (attach_flag)
  1932.         target_detach (args, from_tty);
  1933.       else
  1934.         target_kill ();
  1935.     }
  1936.       else
  1937.     error ("Not confirmed.");
  1938.     }
  1939.   /* Save the history information if it is appropriate to do so.  */
  1940.   if (write_history_p && history_filename)
  1941.     write_history (history_filename);
  1942.   exit (0);
  1943. }
  1944.  
  1945. /* Returns whether GDB is running on a terminal and whether the user
  1946.    desires that questions be asked of them on that terminal.  */
  1947.  
  1948. int
  1949. input_from_terminal_p ()
  1950. {
  1951.   return gdb_has_a_terminal && (instream == stdin) & caution;
  1952. }
  1953.  
  1954. /* ARGSUSED */
  1955. static void
  1956. pwd_command (args, from_tty)
  1957.      char *args;
  1958.      int from_tty;
  1959. {
  1960.   if (args) error ("The \"pwd\" command does not take an argument: %s", args);
  1961.   getcwd (dirbuf, sizeof (dirbuf));
  1962.  
  1963.   if (!STREQ (dirbuf, current_directory))
  1964.     printf ("Working directory %s\n (canonically %s).\n",
  1965.         current_directory, dirbuf);
  1966.   else
  1967.     printf ("Working directory %s.\n", current_directory);
  1968. }
  1969.  
  1970. static void
  1971. cd_command (dir, from_tty)
  1972.      char *dir;
  1973.      int from_tty;
  1974. {
  1975.   int len;
  1976.   int change;
  1977.  
  1978.   /* If the new directory is absolute, repeat is a no-op; if relative,
  1979.      repeat might be useful but is more likely to be a mistake.  */
  1980.   dont_repeat ();
  1981.  
  1982.   if (dir == 0)
  1983.     error_no_arg ("new working directory");
  1984.  
  1985.   dir = tilde_expand (dir);
  1986.   make_cleanup (free, dir);
  1987.  
  1988.   if (chdir (dir) < 0)
  1989.     perror_with_name (dir);
  1990.  
  1991.   len = strlen (dir);
  1992.   dir = savestring (dir, len - (len > 1 && dir[len-1] == '/'));
  1993.   if (dir[0] == '/')
  1994.     current_directory = dir;
  1995.   else
  1996.     {
  1997.       current_directory = concat (current_directory, "/", dir, NULL);
  1998.       free (dir);
  1999.     }
  2000.  
  2001.   /* Now simplify any occurrences of `.' and `..' in the pathname.  */
  2002.  
  2003.   change = 1;
  2004.   while (change)
  2005.     {
  2006.       char *p;
  2007.       change = 0;
  2008.  
  2009.       for (p = current_directory; *p;)
  2010.     {
  2011.       if (!strncmp (p, "/./", 2)
  2012.           && (p[2] == 0 || p[2] == '/'))
  2013.         strcpy (p, p + 2);
  2014.       else if (!strncmp (p, "/..", 3)
  2015.            && (p[3] == 0 || p[3] == '/')
  2016.            && p != current_directory)
  2017.         {
  2018.           char *q = p;
  2019.           while (q != current_directory && q[-1] != '/') q--;
  2020.           if (q != current_directory)
  2021.         {
  2022.           strcpy (q-1, p+3);
  2023.           p = q-1;
  2024.         }
  2025.         }
  2026.       else p++;
  2027.     }
  2028.     }
  2029.  
  2030.   forget_cached_source_info ();
  2031.  
  2032.   if (from_tty)
  2033.     pwd_command ((char *) 0, 1);
  2034. }
  2035.  
  2036. /* ARGSUSED */
  2037. static void
  2038. source_command (args, from_tty)
  2039.      char *args;
  2040.      int from_tty;
  2041. {
  2042.   FILE *stream;
  2043.   struct cleanup *cleanups;
  2044.   char *file = args;
  2045.  
  2046.   if (file == NULL)
  2047.     {
  2048.       error ("source command requires pathname of file to source.");
  2049.     }
  2050.  
  2051.   file = tilde_expand (file);
  2052.   make_cleanup (free, file);
  2053.  
  2054.   stream = fopen (file, FOPEN_RT);
  2055.   if (stream == 0)
  2056.     perror_with_name (file);
  2057.  
  2058.   cleanups = make_cleanup (fclose, stream);
  2059.  
  2060.   read_command_file (stream);
  2061.  
  2062.   do_cleanups (cleanups);
  2063. }
  2064.  
  2065. /* ARGSUSED */
  2066. static void
  2067. echo_command (text, from_tty)
  2068.      char *text;
  2069.      int from_tty;
  2070. {
  2071.   char *p = text;
  2072.   register int c;
  2073.  
  2074.   if (text)
  2075.     while ((c = *p++) != '\0')
  2076.       {
  2077.     if (c == '\\')
  2078.       {
  2079.         /* \ at end of argument is used after spaces
  2080.            so they won't be lost.  */
  2081.         if (*p == 0)
  2082.           return;
  2083.  
  2084.         c = parse_escape (&p);
  2085.         if (c >= 0)
  2086.           printf_filtered ("%c", c);
  2087.       }
  2088.     else
  2089.       printf_filtered ("%c", c);
  2090.       }
  2091.  
  2092.   /* Force this output to appear now.  */
  2093.   wrap_here ("");
  2094.   fflush (stdout);
  2095. }
  2096.  
  2097.  
  2098. /* Functions to manipulate command line editing control variables.  */
  2099.  
  2100. /* Number of commands to print in each call to show_commands.  */
  2101. #define Hist_print 10
  2102. static void
  2103. show_commands (args, from_tty)
  2104.      char *args;
  2105.      int from_tty;
  2106. {
  2107.   /* Index for history commands.  Relative to history_base.  */
  2108.   int offset;
  2109.  
  2110.   /* Number of the history entry which we are planning to display next.
  2111.      Relative to history_base.  */
  2112.   static int num = 0;
  2113.  
  2114.   /* The first command in the history which doesn't exist (i.e. one more
  2115.      than the number of the last command).  Relative to history_base.  */
  2116.   int hist_len;
  2117.  
  2118.   extern struct _hist_entry *history_get PARAMS ((int));
  2119.   extern int history_base;
  2120.  
  2121.   /* Print out some of the commands from the command history.  */
  2122.   /* First determine the length of the history list.  */
  2123.   hist_len = history_size;
  2124.   for (offset = 0; offset < history_size; offset++)
  2125.     {
  2126.       if (!history_get (history_base + offset))
  2127.     {
  2128.       hist_len = offset;
  2129.       break;
  2130.     }
  2131.     }
  2132.  
  2133.   if (args)
  2134.     {
  2135.       if (args[0] == '+' && args[1] == '\0')
  2136.     /* "info editing +" should print from the stored position.  */
  2137.     ;
  2138.       else
  2139.     /* "info editing <exp>" should print around command number <exp>.  */
  2140.     num = (parse_and_eval_address (args) - history_base) - Hist_print / 2;
  2141.     }
  2142.   /* "show commands" means print the last Hist_print commands.  */
  2143.   else
  2144.     {
  2145.       num = hist_len - Hist_print;
  2146.     }
  2147.  
  2148.   if (num < 0)
  2149.     num = 0;
  2150.  
  2151.   /* If there are at least Hist_print commands, we want to display the last
  2152.      Hist_print rather than, say, the last 6.  */
  2153.   if (hist_len - num < Hist_print)
  2154.     {
  2155.       num = hist_len - Hist_print;
  2156.       if (num < 0)
  2157.     num = 0;
  2158.     }
  2159.  
  2160.   for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
  2161.     {
  2162.       printf_filtered ("%5d  %s\n", history_base + offset,
  2163.           (history_get (history_base + offset))->line);
  2164.     }
  2165.  
  2166.   /* The next command we want to display is the next one that we haven't
  2167.      displayed yet.  */
  2168.   num += Hist_print;
  2169.   
  2170.   /* If the user repeats this command with return, it should do what
  2171.      "show commands +" does.  This is unnecessary if arg is null,
  2172.      because "show commands +" is not useful after "show commands".  */
  2173.   if (from_tty && args)
  2174.     {
  2175.       args[0] = '+';
  2176.       args[1] = '\0';
  2177.     }
  2178. }
  2179.  
  2180. /* Called by do_setshow_command.  */
  2181. /* ARGSUSED */
  2182. static void
  2183. set_history_size_command (args, from_tty, c)
  2184.      char *args;
  2185.      int from_tty;
  2186.      struct cmd_list_element *c;
  2187. {
  2188.   if (history_size == INT_MAX)
  2189.     unstifle_history ();
  2190.   else if (history_size >= 0)
  2191.     stifle_history (history_size);
  2192.   else
  2193.     {
  2194.       history_size = INT_MAX;
  2195.       error ("History size must be non-negative");
  2196.     }
  2197. }
  2198.  
  2199. /* ARGSUSED */
  2200. static void
  2201. set_history (args, from_tty)
  2202.      char *args;
  2203.      int from_tty;
  2204. {
  2205.   printf ("\"set history\" must be followed by the name of a history subcommand.\n");
  2206.   help_list (sethistlist, "set history ", -1, stdout);
  2207. }
  2208.  
  2209. /* ARGSUSED */
  2210. static void
  2211. show_history (args, from_tty)
  2212.      char *args;
  2213.      int from_tty;
  2214. {
  2215.   cmd_show_list (showhistlist, from_tty, "");
  2216. }
  2217.  
  2218. int info_verbose = 0;        /* Default verbose msgs off */
  2219.  
  2220. /* Called by do_setshow_command.  An elaborate joke.  */
  2221. /* ARGSUSED */
  2222. static void 
  2223. set_verbose (args, from_tty, c)
  2224.      char *args;
  2225.      int from_tty;
  2226.      struct cmd_list_element *c;
  2227. {
  2228.   char *cmdname = "verbose";
  2229.   struct cmd_list_element *showcmd;
  2230.   
  2231.   showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
  2232.  
  2233.   if (info_verbose)
  2234.     {
  2235.       c->doc = "Set verbose printing of informational messages.";
  2236.       showcmd->doc = "Show verbose printing of informational messages.";
  2237.     }
  2238.   else
  2239.     {
  2240.       c->doc = "Set verbosity.";
  2241.       showcmd->doc = "Show verbosity.";
  2242.     }
  2243. }
  2244.  
  2245. static void
  2246. float_handler (signo)
  2247. int signo;
  2248. {
  2249.   /* This message is based on ANSI C, section 4.7.  Note that integer
  2250.      divide by zero causes this, so "float" is a misnomer.  */
  2251.   signal (SIGFPE, float_handler);
  2252.   error ("Erroneous arithmetic operation.");
  2253. }
  2254.  
  2255. /* Return whether we are running a batch file or from terminal.  */
  2256. int
  2257. batch_mode ()
  2258. {
  2259.   return !(instream == stdin && ISATTY (stdin));
  2260. }
  2261.  
  2262.  
  2263. static void
  2264. initialize_cmd_lists ()
  2265. {
  2266.   cmdlist = NULL;
  2267.   infolist = NULL;
  2268.   enablelist = NULL;
  2269.   disablelist = NULL;
  2270.   deletelist = NULL;
  2271.   enablebreaklist = NULL;
  2272.   setlist = NULL;
  2273.   unsetlist = NULL;
  2274.   showlist = NULL;
  2275.   sethistlist = NULL;
  2276.   showhistlist = NULL;
  2277.   unsethistlist = NULL;
  2278. #if MAINTENANCE_CMDS
  2279.   maintenancelist = NULL;
  2280.   maintenanceinfolist = NULL;
  2281.   maintenanceprintlist = NULL;
  2282. #endif
  2283.   setprintlist = NULL;
  2284.   showprintlist = NULL;
  2285.   setchecklist = NULL;
  2286.   showchecklist = NULL;
  2287. }
  2288.  
  2289. /* Init the history buffer.  Note that we are called after the init file(s)
  2290.  * have been read so that the user can change the history file via his
  2291.  * .gdbinit file (for instance).  The GDBHISTFILE environment variable
  2292.  * overrides all of this.
  2293.  */
  2294.  
  2295. static void
  2296. initialize_history()
  2297. {
  2298.   char *tmpenv;
  2299.  
  2300.   tmpenv = getenv ("HISTSIZE");
  2301.   if (tmpenv)
  2302.     history_size = atoi (tmpenv);
  2303.   else if (!history_size)
  2304.     history_size = 256;
  2305.  
  2306.   stifle_history (history_size);
  2307.  
  2308.   tmpenv = getenv ("GDBHISTFILE");
  2309.   if (tmpenv)
  2310.     history_filename = savestring (tmpenv, strlen(tmpenv));
  2311.   else if (!history_filename) {
  2312.     /* We include the current directory so that if the user changes
  2313.        directories the file written will be the same as the one
  2314.        that was read.  */
  2315.     history_filename = concat (current_directory, "/.gdb_history", NULL);
  2316.   }
  2317.   read_history (history_filename);
  2318. }
  2319.  
  2320. static void
  2321. initialize_main ()
  2322. {
  2323.   struct cmd_list_element *c;
  2324.   
  2325. #ifdef DEFAULT_PROMPT
  2326.   prompt = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT));
  2327. #else
  2328.   prompt = savestring ("(gdb) ", 6);
  2329. #endif
  2330.  
  2331.   /* Set the important stuff up for command editing.  */
  2332.   command_editing_p = 1;
  2333.   history_expansion_p = 0;
  2334.   write_history_p = 0;
  2335.   
  2336.   /* Setup important stuff for command line editing.  */
  2337.   rl_completion_entry_function = (int (*)()) symbol_completion_function;
  2338.   rl_completer_word_break_characters = gdb_completer_word_break_characters;
  2339.   rl_completer_quote_characters = gdb_completer_quote_characters;
  2340.   rl_readline_name = "gdb";
  2341.  
  2342.   /* Define the classes of commands.
  2343.      They will appear in the help list in the reverse of this order.  */
  2344.  
  2345.   add_cmd ("internals", class_maintenance, NO_FUNCTION,
  2346.        "Maintenance commands.\n\
  2347. Some gdb commands are provided just for use by gdb maintainers.\n\
  2348. These commands are subject to frequent change, and may not be as\n\
  2349. well documented as user commands.",
  2350.        &cmdlist);
  2351.   add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist);
  2352.   add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist);
  2353.   add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\
  2354. The commands in this class are those defined by the user.\n\
  2355. Use the \"define\" command to define a command.", &cmdlist);
  2356.   add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist);
  2357.   add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist);
  2358.   add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist);
  2359.   add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist);
  2360.   add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist);
  2361.   add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\
  2362. The stack is made up of stack frames.  Gdb assigns numbers to stack frames\n\
  2363. counting from zero for the innermost (currently executing) frame.\n\n\
  2364. At any time gdb identifies one frame as the \"selected\" frame.\n\
  2365. Variable lookups are done with respect to the selected frame.\n\
  2366. When the program being debugged stops, gdb selects the innermost frame.\n\
  2367. The commands below can be used to select other frames by number or address.",
  2368.        &cmdlist);
  2369.   add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist);
  2370.  
  2371.   add_com ("pwd", class_files, pwd_command,
  2372.        "Print working directory.  This is used for your program as well.");
  2373.   add_com ("cd", class_files, cd_command,
  2374.        "Set working directory to DIR for debugger and program being debugged.\n\
  2375. The change does not take effect for the program being debugged\n\
  2376. until the next time it is started.");
  2377.  
  2378.   add_show_from_set
  2379.     (add_set_cmd ("prompt", class_support, var_string, (char *)&prompt,
  2380.        "Set gdb's prompt",
  2381.        &setlist),
  2382.      &showlist);
  2383.   
  2384.   add_com ("echo", class_support, echo_command,
  2385.        "Print a constant string.  Give string as argument.\n\
  2386. C escape sequences may be used in the argument.\n\
  2387. No newline is added at the end of the argument;\n\
  2388. use \"\\n\" if you want a newline to be printed.\n\
  2389. Since leading and trailing whitespace are ignored in command arguments,\n\
  2390. if you want to print some you must use \"\\\" before leading whitespace\n\
  2391. to be printed or after trailing whitespace.");
  2392.   add_com ("document", class_support, document_command,
  2393.        "Document a user-defined command.\n\
  2394. Give command name as argument.  Give documentation on following lines.\n\
  2395. End with a line of just \"end\".");
  2396.   add_com ("define", class_support, define_command,
  2397.        "Define a new command name.  Command name is argument.\n\
  2398. Definition appears on following lines, one command per line.\n\
  2399. End with a line of just \"end\".\n\
  2400. Use the \"document\" command to give documentation for the new command.\n\
  2401. Commands defined in this way do not take arguments.");
  2402.  
  2403. #ifdef __STDC__
  2404.   add_com ("source", class_support, source_command,
  2405.        "Read commands from a file named FILE.\n\
  2406. Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\
  2407. when gdb is started.");
  2408. #else
  2409.   /* Punt file name, we can't help it easily.  */
  2410.   add_com ("source", class_support, source_command,
  2411.        "Read commands from a file named FILE.\n\
  2412. Note that the file \".gdbinit\" is read automatically in this way\n\
  2413. when gdb is started.");
  2414. #endif
  2415.  
  2416.   add_com ("quit", class_support, quit_command, "Exit gdb.");
  2417.   add_com ("help", class_support, help_command, "Print list of commands.");
  2418.   add_com_alias ("q", "quit", class_support, 1);
  2419.   add_com_alias ("h", "help", class_support, 1);
  2420.  
  2421.  
  2422.   c = add_set_cmd ("verbose", class_support, var_boolean, (char *)&info_verbose,
  2423.            "Set ",
  2424.            &setlist),
  2425.   add_show_from_set (c, &showlist);
  2426.   c->function.sfunc = set_verbose;
  2427.   set_verbose (NULL, 0, c);
  2428.   
  2429.   add_show_from_set
  2430.     (add_set_cmd ("editing", class_support, var_boolean, (char *)&command_editing_p,
  2431.        "Set editing of command lines as they are typed.\n\
  2432. Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
  2433. Without an argument, command line editing is enabled.  To edit, use\n\
  2434. EMACS-like or VI-like commands like control-P or ESC.", &setlist),
  2435.      &showlist);
  2436.  
  2437.   add_prefix_cmd ("history", class_support, set_history,
  2438.           "Generic command for setting command history parameters.",
  2439.           &sethistlist, "set history ", 0, &setlist);
  2440.   add_prefix_cmd ("history", class_support, show_history,
  2441.           "Generic command for showing command history parameters.",
  2442.           &showhistlist, "show history ", 0, &showlist);
  2443.  
  2444.   add_show_from_set
  2445.     (add_set_cmd ("expansion", no_class, var_boolean, (char *)&history_expansion_p,
  2446.        "Set history expansion on command input.\n\
  2447. Without an argument, history expansion is enabled.", &sethistlist),
  2448.      &showhistlist);
  2449.  
  2450.   add_show_from_set
  2451.     (add_set_cmd ("save", no_class, var_boolean, (char *)&write_history_p,
  2452.        "Set saving of the history record on exit.\n\
  2453. Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
  2454. Without an argument, saving is enabled.", &sethistlist),
  2455.      &showhistlist);
  2456.  
  2457.   c = add_set_cmd ("size", no_class, var_integer, (char *)&history_size,
  2458.            "Set the size of the command history, \n\
  2459. ie. the number of previous commands to keep a record of.", &sethistlist);
  2460.   add_show_from_set (c, &showhistlist);
  2461.   c->function.sfunc = set_history_size_command;
  2462.  
  2463.   add_show_from_set
  2464.     (add_set_cmd ("filename", no_class, var_filename, (char *)&history_filename,
  2465.        "Set the filename in which to record the command history\n\
  2466.  (the list of previous commands of which a record is kept).", &sethistlist),
  2467.      &showhistlist);
  2468.  
  2469.   add_show_from_set
  2470.     (add_set_cmd ("confirm", class_support, var_boolean,
  2471.           (char *)&caution,
  2472.           "Set whether to confirm potentially dangerous operations.",
  2473.           &setlist),
  2474.      &showlist);
  2475.  
  2476.   add_prefix_cmd ("info", class_info, info_command,
  2477.         "Generic command for showing things about the program being debugged.",
  2478.           &infolist, "info ", 0, &cmdlist);
  2479.   add_com_alias ("i", "info", class_info, 1);
  2480.  
  2481.   add_prefix_cmd ("show", class_info, show_command,
  2482.           "Generic command for showing things about the debugger.",
  2483.           &showlist, "show ", 0, &cmdlist);
  2484.   /* Another way to get at the same thing.  */
  2485.   add_info ("set", show_command, "Show all GDB settings.");
  2486.  
  2487.   add_cmd ("commands", no_class, show_commands,
  2488.        "Show the the history of commands you typed.\n\
  2489. You can supply a command number to start with, or a `+' to start after\n\
  2490. the previous command number shown.",
  2491.        &showlist);
  2492.  
  2493.   add_cmd ("version", no_class, show_version,
  2494.        "Show what version of GDB this is.", &showlist);
  2495. }
  2496.