home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / gdb / top.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-13  |  65.2 KB  |  2,485 lines

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