home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / top.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-10  |  91.2 KB  |  3,534 lines

  1. /* Top level stuff for GDB, the GNU debugger.
  2.    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995
  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 "value.h"
  32. #include "language.h"
  33. #include "terminal.h" /* For job_control.  */
  34. #include "annotate.h"
  35. #include <setjmp.h>
  36. #include "top.h"
  37.  
  38. /* readline include files */
  39. #include "readline.h"
  40. #include "history.h"
  41.  
  42. /* readline defines this.  */
  43. #undef savestring
  44.  
  45. #include <sys/types.h>
  46. #ifdef USG
  47. /* What is this for?  X_OK?  */
  48. #include <unistd.h>
  49. #endif
  50.  
  51. #include <string.h>
  52. #ifndef    NO_SYS_FILE
  53. #include <sys/file.h>
  54. #endif
  55. #include <sys/param.h>
  56. #include <sys/stat.h>
  57. #include <ctype.h>
  58.  
  59. extern void initialize_targets PARAMS ((void));
  60.  
  61. extern void initialize_utils PARAMS ((void));
  62.  
  63. /* Prototypes for local functions */
  64.  
  65. static char * line_completion_function PARAMS ((char *, int, char *, int));
  66.  
  67. static char * readline_line_completion_function PARAMS ((char *, int));
  68.  
  69. static void command_loop_marker PARAMS ((int));
  70.  
  71. static void while_command PARAMS ((char *, int));
  72.  
  73. static void if_command PARAMS ((char *, int));
  74.  
  75. static struct command_line *
  76. build_command_line PARAMS ((enum command_control_type, char *));
  77.  
  78. static struct command_line *
  79. get_command_line PARAMS ((enum command_control_type, char *));
  80.  
  81. static void realloc_body_list PARAMS ((struct command_line *, int));
  82.  
  83. static enum misc_command_type read_next_line PARAMS ((struct command_line **));
  84.  
  85. static enum command_control_type
  86. recurse_read_control_structure PARAMS ((struct command_line *));
  87.  
  88. static struct cleanup * setup_user_args PARAMS ((char *));
  89.  
  90. static char * locate_arg PARAMS ((char *));
  91.  
  92. static char * insert_args PARAMS ((char *));
  93.  
  94. static void arg_cleanup PARAMS ((void));
  95.  
  96. static void init_main PARAMS ((void));
  97.  
  98. static void init_cmd_lists PARAMS ((void));
  99.  
  100. static void float_handler PARAMS ((int));
  101.  
  102. static void init_signals PARAMS ((void));
  103.  
  104. static void set_verbose PARAMS ((char *, int, struct cmd_list_element *));
  105.  
  106. #ifdef TARGET_BYTE_ORDER_SELECTABLE
  107.  
  108. static void set_endian PARAMS ((char *, int));
  109.  
  110. static void set_endian_big PARAMS ((char *, int));
  111.  
  112. static void set_endian_little PARAMS ((char *, int));
  113.  
  114. static void set_endian_auto PARAMS ((char *, int));
  115.  
  116. static void show_endian PARAMS ((char *, int));
  117.  
  118. #endif
  119.  
  120. static void show_history PARAMS ((char *, int));
  121.  
  122. static void set_history PARAMS ((char *, int));
  123.  
  124. static void set_history_size_command PARAMS ((char *, int,
  125.                           struct cmd_list_element *));
  126.  
  127. static void show_commands PARAMS ((char *, int));
  128.  
  129. static void echo_command PARAMS ((char *, int));
  130.  
  131. static void pwd_command PARAMS ((char *, int));
  132.  
  133. static void show_version PARAMS ((char *, int));
  134.  
  135. static void document_command PARAMS ((char *, int));
  136.  
  137. static void define_command PARAMS ((char *, int));
  138.  
  139. static void validate_comname PARAMS ((char *));
  140.  
  141. static void help_command PARAMS ((char *, int));
  142.  
  143. static void show_command PARAMS ((char *, int));
  144.  
  145. static void info_command PARAMS ((char *, int));
  146.  
  147. static void complete_command PARAMS ((char *, int));
  148.  
  149. static void do_nothing PARAMS ((int));
  150.  
  151. static int quit_cover PARAMS ((char *));
  152.  
  153. static void disconnect PARAMS ((int));
  154.  
  155. static void source_cleanup PARAMS ((FILE *));
  156.  
  157. /* If this definition isn't overridden by the header files, assume
  158.    that isatty and fileno exist on this system.  */
  159. #ifndef ISATTY
  160. #define ISATTY(FP)    (isatty (fileno (FP)))
  161. #endif
  162.  
  163. /* Initialization file name for gdb.  This is overridden in some configs.  */
  164.  
  165. #ifndef    GDBINIT_FILENAME
  166. #define    GDBINIT_FILENAME    ".gdbinit"
  167. #endif
  168. char gdbinit[] = GDBINIT_FILENAME;
  169.  
  170. int inhibit_gdbinit = 0;
  171.  
  172. /* If nonzero, and GDB has been configured to be able to use windows,
  173.    attempt to open them upon startup.  */
  174.  
  175. int use_windows = 1;
  176.  
  177. /* Version number of GDB, as a string.  */
  178.  
  179. extern char *version;
  180.  
  181. /* Canonical host name as a string. */
  182.  
  183. extern char *host_name;
  184.  
  185. /* Canonical target name as a string. */
  186.  
  187. extern char *target_name;
  188.  
  189. extern char lang_frame_mismatch_warn[];        /* language.c */
  190.  
  191. /* Flag for whether we want all the "from_tty" gubbish printed.  */
  192.  
  193. int caution = 1;            /* Default is yes, sigh. */
  194.  
  195. /* Define all cmd_list_elements.  */
  196.  
  197. /* Chain containing all defined commands.  */
  198.  
  199. struct cmd_list_element *cmdlist;
  200.  
  201. /* Chain containing all defined info subcommands.  */
  202.  
  203. struct cmd_list_element *infolist;
  204.  
  205. /* Chain containing all defined enable subcommands. */
  206.  
  207. struct cmd_list_element *enablelist;
  208.  
  209. /* Chain containing all defined disable subcommands. */
  210.  
  211. struct cmd_list_element *disablelist;
  212.  
  213. /* Chain containing all defined delete subcommands. */
  214.  
  215. struct cmd_list_element *deletelist;
  216.  
  217. /* Chain containing all defined "enable breakpoint" subcommands. */
  218.  
  219. struct cmd_list_element *enablebreaklist;
  220.  
  221. /* Chain containing all defined set subcommands */
  222.  
  223. struct cmd_list_element *setlist;
  224.  
  225. /* Chain containing all defined unset subcommands */
  226.  
  227. struct cmd_list_element *unsetlist;
  228.  
  229. /* Chain containing all defined show subcommands.  */
  230.  
  231. struct cmd_list_element *showlist;
  232.  
  233. #ifdef TARGET_BYTE_ORDER_SELECTABLE
  234. /* Chain containing the \"set endian\" commands.  */
  235.  
  236. struct cmd_list_element *endianlist;
  237. #endif
  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.  Set to stdin normally.
  278.    Set by source_command to the file we are sourcing.  Set to NULL if we are
  279.    executing a user-defined command.  */
  280.  
  281. FILE *instream;
  282.  
  283. /* Current working directory.  */
  284.  
  285. char *current_directory;
  286.  
  287. /* The directory name is actually stored here (usually).  */
  288. char gdb_dirbuf[1024];
  289.  
  290. /* Function to call before reading a command, if nonzero.
  291.    The function receives two args: an input stream,
  292.    and a prompt string.  */
  293.  
  294. void (*window_hook) PARAMS ((FILE *, char *));
  295.  
  296. int epoch_interface;
  297. int xgdb_verbose;
  298.  
  299. /* gdb prints this when reading a command interactively */
  300. static char *prompt;
  301.  
  302. /* Buffer used for reading command lines, and the size
  303.    allocated for it so far.  */
  304.  
  305. char *line;
  306. int linesize = 100;
  307.  
  308. /* Nonzero if the current command is modified by "server ".  This
  309.    affects things like recording into the command history, comamnds
  310.    repeating on RETURN, etc.  This is so a user interface (emacs, GUI,
  311.    whatever) can issue its own commands and also send along commands
  312.    from the user, and have the user not notice that the user interface
  313.    is issuing commands too.  */
  314. int server_command;
  315.  
  316. /* Baud rate specified for talking to serial target systems.  Default
  317.    is left as -1, so targets can choose their own defaults.  */
  318. /* FIXME: This means that "show remotebaud" and gr_files_info can print -1
  319.    or (unsigned int)-1.  This is a Bad User Interface.  */
  320.  
  321. int baud_rate = -1;
  322.  
  323. /* Non-zero tells remote* modules to output debugging info.  */
  324.  
  325. int remote_debug = 0;
  326.  
  327. /* Level of control structure.  */
  328. static int control_level;
  329.  
  330. /* Structure for arguments to user defined functions.  */
  331. #define MAXUSERARGS 10
  332. struct user_args
  333. {
  334.   struct user_args *next;
  335.   struct
  336.     {
  337.       char *arg;
  338.       int len;
  339.     } a[MAXUSERARGS];
  340.   int count;
  341. } *user_args;
  342.  
  343. /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT.  */
  344.  
  345. #ifndef STOP_SIGNAL
  346. #ifdef SIGTSTP
  347. #define STOP_SIGNAL SIGTSTP
  348. static void stop_sig PARAMS ((int));
  349. #endif
  350. #endif
  351.  
  352. /* Some System V have job control but not sigsetmask(). */
  353. #if !defined (HAVE_SIGSETMASK)
  354. #if !defined (USG)
  355. #define HAVE_SIGSETMASK 1
  356. #else
  357. #define HAVE_SIGSETMASK 0
  358. #endif
  359. #endif
  360.  
  361. #if 0 == (HAVE_SIGSETMASK)
  362. #define sigsetmask(n)
  363. #endif
  364.  
  365. /* Hooks for alternate command interfaces.  */
  366.  
  367. /* Called after most modules have been initialized, but before taking users
  368.    command file.  */
  369.  
  370. void (*init_ui_hook) PARAMS ((void));
  371.  
  372. /* Called instead of command_loop at top level.  Can be invoked via
  373.    return_to_top_level.  */
  374.  
  375. void (*command_loop_hook) PARAMS ((void));
  376.  
  377. /* Called instead of fputs for all output.  */
  378.  
  379. void (*fputs_unfiltered_hook) PARAMS ((const char *linebuffer, FILE *stream));
  380.  
  381. /* Called from print_frame_info to list the line we stopped in.  */
  382.  
  383. void (*print_frame_info_listing_hook) PARAMS ((struct symtab *s, int line,
  384.                            int stopline, int noerror));
  385. /* Replaces most of query.  */
  386.  
  387. int (*query_hook) PARAMS (());
  388.  
  389. /* Called from gdb_flush to flush output.  */
  390.  
  391. void (*flush_hook) PARAMS ((FILE *stream));
  392.  
  393. /* Called as appropriate to notify the interface of the specified breakpoint
  394.    conditions.  */
  395.  
  396. void (*create_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
  397. void (*delete_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
  398. void (*enable_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
  399. void (*disable_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
  400.  
  401. /* Called during long calculations to allow GUI to repair window damage, and to
  402.    check for stop buttons, etc... */
  403.  
  404. void (*interactive_hook) PARAMS ((void));
  405.  
  406. /* Called when the registers have changed, as a hint to a GUI
  407.    to minimize window update. */
  408.  
  409. void (*registers_changed_hook) PARAMS ((void));
  410.  
  411. /* Called when going to wait for the target.  Usually allows the GUI to run
  412.    while waiting for target events.  */
  413.  
  414. int (*target_wait_hook) PARAMS ((int pid, struct target_waitstatus *status));
  415.  
  416. /* Used by UI as a wrapper around command execution.  May do various things
  417.    like enabling/disabling buttons, etc...  */
  418.  
  419. void (*call_command_hook) PARAMS ((struct cmd_list_element *c, char *cmd,
  420.                    int from_tty));
  421.  
  422. /* An alternate way to read memory for disassembly.  This is used to provide a
  423.    switch that allows disassembly to come from an exec file rather than a
  424.    remote target.  This is a speed hack.  */
  425.  
  426. int (*dis_asm_read_memory_hook) PARAMS ((bfd_vma memaddr, bfd_byte *myaddr,
  427.                      int len, disassemble_info *info));
  428. /* Takes control from error ().  Typically used to prevent longjmps out of the
  429.    middle of the GUI.  Usually used in conjunction with a catch routine.  */
  430.  
  431. NORETURN void (*error_hook) PARAMS (());
  432.  
  433.  
  434. /* Where to go for return_to_top_level (RETURN_ERROR).  */
  435. jmp_buf error_return;
  436. /* Where to go for return_to_top_level (RETURN_QUIT).  */
  437. jmp_buf quit_return;
  438.  
  439. /* Return for reason REASON.  This generally gets back to the command
  440.    loop, but can be caught via catch_errors.  */
  441.  
  442. NORETURN void
  443. return_to_top_level (reason)
  444.      enum return_reason reason;
  445. {
  446.   quit_flag = 0;
  447.   immediate_quit = 0;
  448.  
  449.   /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
  450.      I can think of a reason why that is vital, though).  */
  451.   bpstat_clear_actions(stop_bpstat);    /* Clear queued breakpoint commands */
  452.  
  453.   disable_current_display ();
  454.   do_cleanups (ALL_CLEANUPS);
  455.  
  456.   if (annotation_level > 1)
  457.     switch (reason)
  458.       {
  459.       case RETURN_QUIT:
  460.     annotate_quit ();
  461.     break;
  462.       case RETURN_ERROR:
  463.     annotate_error ();
  464.     break;
  465.       }
  466.  
  467.   (NORETURN void) longjmp
  468.     (reason == RETURN_ERROR ? error_return : quit_return, 1);
  469. }
  470.  
  471. /* Call FUNC with arg ARGS, catching any errors.  If there is no
  472.    error, return the value returned by FUNC.  If there is an error,
  473.    print ERRSTRING, print the specific error message, then return
  474.    zero.
  475.  
  476.    Must not be called with immediate_quit in effect (bad things might
  477.    happen, say we got a signal in the middle of a memcpy to quit_return).
  478.    This is an OK restriction; with very few exceptions immediate_quit can
  479.    be replaced by judicious use of QUIT.
  480.  
  481.    MASK specifies what to catch; it is normally set to
  482.    RETURN_MASK_ALL, if for no other reason than that the code which
  483.    calls catch_errors might not be set up to deal with a quit which
  484.    isn't caught.  But if the code can deal with it, it generally
  485.    should be RETURN_MASK_ERROR, unless for some reason it is more
  486.    useful to abort only the portion of the operation inside the
  487.    catch_errors.  Note that quit should return to the command line
  488.    fairly quickly, even if some further processing is being done.  */
  489.  
  490. int
  491. catch_errors (func, args, errstring, mask)
  492.      int (*func) PARAMS ((char *));
  493.      PTR args;
  494.      char *errstring;
  495.      return_mask mask;
  496. {
  497.   jmp_buf saved_error;
  498.   jmp_buf saved_quit;
  499.   jmp_buf tmp_jmp;
  500.   int val;
  501.   struct cleanup *saved_cleanup_chain;
  502.   char *saved_error_pre_print;
  503.  
  504.   saved_cleanup_chain = save_cleanups ();
  505.   saved_error_pre_print = error_pre_print;
  506.  
  507.   if (mask & RETURN_MASK_ERROR)
  508.     memcpy ((char *)saved_error, (char *)error_return, sizeof (jmp_buf));
  509.   if (mask & RETURN_MASK_QUIT)
  510.     memcpy (saved_quit, quit_return, sizeof (jmp_buf));
  511.   error_pre_print = errstring;
  512.  
  513.   if (setjmp (tmp_jmp) == 0)
  514.     {
  515.       if (mask & RETURN_MASK_ERROR)
  516.     memcpy (error_return, tmp_jmp, sizeof (jmp_buf));
  517.       if (mask & RETURN_MASK_QUIT)
  518.     memcpy (quit_return, tmp_jmp, sizeof (jmp_buf));
  519.       val = (*func) (args);
  520.     }
  521.   else
  522.     val = 0;
  523.  
  524.   restore_cleanups (saved_cleanup_chain);
  525.  
  526.   error_pre_print = saved_error_pre_print;
  527.   if (mask & RETURN_MASK_ERROR)
  528.     memcpy (error_return, saved_error, sizeof (jmp_buf));
  529.   if (mask & RETURN_MASK_QUIT)
  530.     memcpy (quit_return, saved_quit, sizeof (jmp_buf));
  531.   return val;
  532. }
  533.  
  534. /* Handler for SIGHUP.  */
  535.  
  536. static void
  537. disconnect (signo)
  538. int signo;
  539. {
  540.   catch_errors (quit_cover, NULL,
  541.         "Could not kill the program being debugged", RETURN_MASK_ALL);
  542.   signal (SIGHUP, SIG_DFL);
  543.   kill (getpid (), SIGHUP);
  544. }
  545.  
  546. /* Just a little helper function for disconnect().  */
  547.  
  548. static int
  549. quit_cover (s)
  550. char *s;
  551. {
  552.   caution = 0;        /* Throw caution to the wind -- we're exiting.
  553.                This prevents asking the user dumb questions.  */
  554.   quit_command((char *)0, 0);
  555.   return 0;
  556. }
  557.  
  558. /* Line number we are currently in in a file which is being sourced.  */
  559. static int source_line_number;
  560.  
  561. /* Name of the file we are sourcing.  */
  562. static char *source_file_name;
  563.  
  564. /* Buffer containing the error_pre_print used by the source stuff.
  565.    Malloc'd.  */
  566. static char *source_error;
  567. static int source_error_allocated;
  568.  
  569. /* Something to glom on to the start of error_pre_print if source_file_name
  570.    is set.  */
  571. static char *source_pre_error;
  572.  
  573. /* Clean up on error during a "source" command (or execution of a
  574.    user-defined command).  */
  575.  
  576. static void
  577. source_cleanup (stream)
  578.      FILE *stream;
  579. {
  580.   /* Restore the previous input stream.  */
  581.   instream = stream;
  582. }
  583.  
  584. /* Read commands from STREAM.  */
  585. void
  586. read_command_file (stream)
  587.      FILE *stream;
  588. {
  589.   struct cleanup *cleanups;
  590.  
  591.   cleanups = make_cleanup (source_cleanup, instream);
  592.   instream = stream;
  593.   command_loop ();
  594.   do_cleanups (cleanups);
  595. }
  596.  
  597. extern void init_proc ();
  598.  
  599. void (*pre_init_ui_hook) PARAMS ((void));
  600.  
  601. void
  602. gdb_init ()
  603. {
  604.   if (pre_init_ui_hook)
  605.     pre_init_ui_hook ();
  606.  
  607.   /* Run the init function of each source file */
  608.  
  609.   getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
  610.   current_directory = gdb_dirbuf;
  611.  
  612.   init_cmd_lists ();    /* This needs to be done first */
  613.   initialize_targets (); /* Setup target_terminal macros for utils.c */
  614.   initialize_utils ();    /* Make errors and warnings possible */
  615.   initialize_all_files ();
  616.   init_main ();        /* But that omits this file!  Do it now */
  617.   init_signals ();
  618.  
  619.   init_proc ();
  620.  
  621.   /* We need a default language for parsing expressions, so simple things like
  622.      "set width 0" won't fail if no language is explicitly set in a config file
  623.      or implicitly set by reading an executable during startup. */
  624.   set_language (language_c);
  625.   expected_language = current_language;    /* don't warn about the change.  */
  626.  
  627.   if (init_ui_hook)
  628.     init_ui_hook ();
  629. }
  630.  
  631. /* Allocate, initialize a new command line structure for one of the
  632.    control commands (if/while).  */
  633.  
  634. static struct command_line *
  635. build_command_line (type, args)
  636.      enum command_control_type type;
  637.      char *args;
  638. {
  639.   struct command_line *cmd;
  640.  
  641.   cmd = (struct command_line *)xmalloc (sizeof (struct command_line));
  642.   cmd->next = NULL;
  643.   cmd->control_type = type;
  644.  
  645.   cmd->body_count = 1;
  646.   cmd->body_list
  647.     = (struct command_line **)xmalloc (sizeof (struct command_line *)
  648.                        * cmd->body_count);
  649.   memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
  650.   cmd->line = savestring (args, strlen (args));
  651.   return cmd;
  652. }
  653.  
  654. /* Build and return a new command structure for the control commands
  655.    such as "if" and "while".  */
  656.  
  657. static struct command_line *
  658. get_command_line (type, arg)
  659.      enum command_control_type type;
  660.      char *arg;
  661. {
  662.   struct command_line *cmd;
  663.   struct cleanup *old_chain = NULL;
  664.  
  665.   /* Allocate and build a new command line structure.  */
  666.   cmd = build_command_line (type, arg);
  667.  
  668.   old_chain = make_cleanup (free_command_lines, &cmd);
  669.  
  670.   /* Read in the body of this command.  */
  671.   if (recurse_read_control_structure (cmd) == invalid_control)
  672.     {
  673.       warning ("error reading in control structure\n");
  674.       do_cleanups (old_chain);
  675.       return NULL;
  676.     }
  677.  
  678.   discard_cleanups (old_chain);
  679.   return cmd;
  680. }
  681.  
  682. /* Recursively print a command (including full control structures).  */
  683. void
  684. print_command_line (cmd, depth)
  685.      struct command_line *cmd;
  686.      unsigned int depth;
  687. {
  688.   unsigned int i;
  689.  
  690.   if (depth)
  691.     {
  692.       for (i = 0; i < depth; i++)
  693.     fputs_filtered ("  ", gdb_stdout);
  694.     }
  695.  
  696.   /* A simple command, print it and return.  */
  697.   if (cmd->control_type == simple_control)
  698.     {
  699.       fputs_filtered (cmd->line, gdb_stdout);
  700.       fputs_filtered ("\n", gdb_stdout);
  701.       return;
  702.     }
  703.  
  704.   /* loop_continue to jump to the start of a while loop, print it
  705.      and return. */
  706.   if (cmd->control_type == continue_control)
  707.     {
  708.       fputs_filtered ("loop_continue\n", gdb_stdout);
  709.       return;
  710.     }
  711.  
  712.   /* loop_break to break out of a while loop, print it and return.  */
  713.   if (cmd->control_type == break_control)
  714.     {
  715.       fputs_filtered ("loop_break\n", gdb_stdout);
  716.       return;
  717.     }
  718.  
  719.   /* A while command.  Recursively print its subcommands before returning.  */
  720.   if (cmd->control_type == while_control)
  721.     {
  722.       struct command_line *list;
  723.       fputs_filtered ("while ", gdb_stdout);
  724.       fputs_filtered (cmd->line, gdb_stdout);
  725.       fputs_filtered ("\n", gdb_stdout);
  726.       list = *cmd->body_list;
  727.       while (list)
  728.     {
  729.       print_command_line (list, depth + 1);
  730.       list = list->next;
  731.     }
  732.     }
  733.  
  734.   /* An if command.  Recursively print both arms before returning.  */
  735.   if (cmd->control_type == if_control)
  736.     {
  737.       fputs_filtered ("if ", gdb_stdout);
  738.       fputs_filtered (cmd->line, gdb_stdout);
  739.       fputs_filtered ("\n", gdb_stdout);
  740.       /* The true arm. */
  741.       print_command_line (cmd->body_list[0], depth + 1);
  742.  
  743.       /* Show the false arm if it exists.  */
  744.       if (cmd->body_count == 2)
  745.       {
  746.         if (depth)
  747.           {
  748.         for (i = 0; i < depth; i++)
  749.           fputs_filtered ("  ", gdb_stdout);
  750.           }
  751.         fputs_filtered ("else\n", gdb_stdout);
  752.         print_command_line (cmd->body_list[1], depth + 1);
  753.       }
  754.       if (depth)
  755.     {
  756.       for (i = 0; i < depth; i++)
  757.         fputs_filtered ("  ", gdb_stdout);
  758.     }
  759.       fputs_filtered ("end\n", gdb_stdout);
  760.     }
  761. }
  762.  
  763. /* Execute the command in CMD.  */
  764.  
  765. enum command_control_type
  766. execute_control_command (cmd)
  767.      struct command_line *cmd;
  768. {
  769.   struct expression *expr;
  770.   struct command_line *current;
  771.   struct cleanup *old_chain = 0;
  772.   value_ptr val;
  773.   int loop;
  774.   enum command_control_type ret;
  775.   char *new_line;
  776.  
  777.   switch (cmd->control_type)
  778.     {
  779.     case simple_control:
  780.       /* A simple command, execute it and return.  */
  781.       new_line = insert_args (cmd->line);
  782.       if (!new_line)
  783.     return invalid_control;
  784.       old_chain = make_cleanup (free_current_contents, &new_line);
  785.       execute_command (new_line, 0);
  786.       ret = cmd->control_type;
  787.       break;
  788.  
  789.     case continue_control:
  790.     case break_control:
  791.       /* Return for "continue", and "break" so we can either
  792.      continue the loop at the top, or break out.  */
  793.       ret = cmd->control_type;
  794.       break;
  795.  
  796.     case while_control:
  797.       {
  798.     /* Parse the loop control expression for the while statement.  */
  799.     new_line = insert_args (cmd->line);
  800.     if (!new_line)
  801.       return invalid_control;
  802.     old_chain = make_cleanup (free_current_contents, &new_line);
  803.     expr = parse_expression (new_line);
  804.     make_cleanup (free_current_contents, &expr);
  805.     
  806.     ret = simple_control;
  807.     loop = true;
  808.  
  809.     /* Keep iterating so long as the expression is true.  */
  810.     while (loop == true)
  811.       {
  812.         /* Evaluate the expression.  */
  813.         val = evaluate_expression (expr);
  814.  
  815.         /* If the value is false, then break out of the loop.  */
  816.         if (!value_true (val))
  817.           break;
  818.  
  819.         /* Execute the body of the while statement.  */
  820.         current = *cmd->body_list;
  821.         while (current)
  822.           {
  823.         ret = execute_control_command (current);
  824.  
  825.         /* If we got an error, or a "break" command, then stop
  826.            looping.  */
  827.         if (ret == invalid_control || ret == break_control)
  828.           {
  829.             loop = false;
  830.             break;
  831.           }
  832.  
  833.         /* If we got a "continue" command, then restart the loop
  834.            at this point.  */
  835.         if (ret == continue_control)
  836.           break;
  837.         
  838.         /* Get the next statement.  */
  839.         current = current->next; 
  840.           }
  841.       }
  842.  
  843.     /* Reset RET so that we don't recurse the break all the way down.  */
  844.     if (ret == break_control)
  845.       ret = simple_control;
  846.  
  847.     break;
  848.       }
  849.  
  850.     case if_control:
  851.       {
  852.     new_line = insert_args (cmd->line);
  853.     if (!new_line)
  854.       return invalid_control;
  855.     old_chain = make_cleanup (free_current_contents, &new_line);
  856.     /* Parse the conditional for the if statement.  */
  857.     expr = parse_expression (new_line);
  858.     make_cleanup (free_current_contents, &expr);
  859.  
  860.     current = NULL;
  861.     ret = simple_control;
  862.  
  863.     /* Evaluate the conditional.  */
  864.     val = evaluate_expression (expr);
  865.  
  866.     /* Choose which arm to take commands from based on the value of the
  867.        conditional expression.  */
  868.     if (value_true (val))
  869.       current = *cmd->body_list;
  870.     else if (cmd->body_count == 2)
  871.       current = *(cmd->body_list + 1);
  872.  
  873.     /* Execute commands in the given arm.  */
  874.     while (current)
  875.       {
  876.         ret = execute_control_command (current);
  877.  
  878.         /* If we got an error, get out.  */
  879.         if (ret != simple_control)
  880.           break;
  881.  
  882.         /* Get the next statement in the body.  */
  883.         current = current->next;
  884.       }
  885.  
  886.     break;
  887.       }
  888.  
  889.     default:
  890.       warning ("Invalid control type in command structure.");
  891.       return invalid_control;
  892.     }
  893.  
  894.   if (old_chain)
  895.     do_cleanups (old_chain);
  896.  
  897.   return ret;
  898. }
  899.  
  900. /* "while" command support.  Executes a body of statements while the
  901.    loop condition is nonzero.  */
  902.  
  903. static void
  904. while_command (arg, from_tty)
  905.      char *arg;
  906.      int from_tty;
  907. {
  908.   struct command_line *command = NULL;
  909.  
  910.   control_level = 1;
  911.   command = get_command_line (while_control, arg);
  912.  
  913.   if (command == NULL)
  914.     return;
  915.  
  916.   execute_control_command (command);
  917.   free_command_lines (&command);
  918. }
  919.  
  920. /* "if" command support.  Execute either the true or false arm depending
  921.    on the value of the if conditional.  */
  922.  
  923. static void
  924. if_command (arg, from_tty)
  925.      char *arg;
  926.      int from_tty;
  927. {
  928.   struct command_line *command = NULL;
  929.  
  930.   control_level = 1;
  931.   command = get_command_line (if_control, arg);
  932.  
  933.   if (command == NULL)
  934.     return;
  935.  
  936.   execute_control_command (command);
  937.   free_command_lines (&command);
  938. }
  939.  
  940. /* Cleanup */
  941. static void
  942. arg_cleanup ()
  943. {
  944.   struct user_args *oargs = user_args;
  945.   if (!user_args)
  946.     fatal ("Internal error, arg_cleanup called with no user args.\n");
  947.  
  948.   user_args = user_args->next;
  949.   free (oargs);
  950. }
  951.  
  952. /* Bind the incomming arguments for a user defined command to
  953.    $arg0, $arg1 ... $argMAXUSERARGS.  */
  954.  
  955. static struct cleanup *
  956. setup_user_args (p)
  957.      char *p;
  958. {
  959.   struct user_args *args;
  960.   struct cleanup *old_chain;
  961.   unsigned int arg_count = 0;
  962.  
  963.   args = (struct user_args *)xmalloc (sizeof (struct user_args));
  964.   memset (args, 0, sizeof (struct user_args));
  965.  
  966.   args->next = user_args;
  967.   user_args = args;
  968.  
  969.   old_chain = make_cleanup (arg_cleanup, 0);
  970.  
  971.   if (p == NULL)
  972.     return old_chain;
  973.  
  974.   while (*p)
  975.     {
  976.       char *start_arg;
  977.  
  978.       if (arg_count >= MAXUSERARGS)
  979.     {
  980.       error ("user defined function may only have %d arguments.\n",
  981.          MAXUSERARGS);
  982.       return old_chain;
  983.     }
  984.  
  985.       /* Strip whitespace.  */
  986.       while (*p == ' ' || *p == '\t')
  987.     p++;
  988.  
  989.       /* P now points to an argument.  */
  990.       start_arg = p;
  991.       user_args->a[arg_count].arg = p;
  992.  
  993.       /* Get to the end of this argument.  */
  994.       while (*p && *p != ' ' && *p != '\t')
  995.     p++;
  996.  
  997.       user_args->a[arg_count].len = p - start_arg;
  998.       arg_count++;
  999.       user_args->count++;
  1000.     }
  1001.   return old_chain;
  1002. }
  1003.  
  1004. /* Given character string P, return a point to the first argument ($arg),
  1005.    or NULL if P contains no arguments.  */
  1006.  
  1007. static char *
  1008. locate_arg (p)
  1009.      char *p;
  1010. {
  1011.   while ((p = strchr (p, '$')))
  1012.     {
  1013.       if (strncmp (p, "$arg", 4) == 0 && isdigit (p[4]))
  1014.     return p;
  1015.       p++;
  1016.     }
  1017.   return NULL;
  1018. }
  1019.  
  1020. /* Insert the user defined arguments stored in user_arg into the $arg
  1021.    arguments found in line, with the updated copy being placed into nline.  */
  1022.  
  1023. static char *
  1024. insert_args (line)
  1025.      char *line;
  1026. {
  1027.   char *p, *save_line, *new_line;
  1028.   unsigned len, i;
  1029.  
  1030.   /* First we need to know how much memory to allocate for the new line.  */
  1031.   save_line = line;
  1032.   len = 0;
  1033.   while ((p = locate_arg (line)))
  1034.     {
  1035.       len += p - line;
  1036.       i = p[4] - '0';
  1037.       
  1038.       if (i >= user_args->count)
  1039.     {
  1040.       error ("Missing argument %d in user function.\n", i);
  1041.       return NULL;
  1042.     }
  1043.       len += user_args->a[i].len;
  1044.       line = p + 5;
  1045.     }
  1046.  
  1047.   /* Don't forget the tail.  */
  1048.   len += strlen (line);
  1049.  
  1050.   /* Allocate space for the new line and fill it in.  */
  1051.   new_line = (char *)xmalloc (len + 1);
  1052.   if (new_line == NULL)
  1053.     return NULL;
  1054.  
  1055.   /* Restore pointer to beginning of old line.  */
  1056.   line = save_line;
  1057.  
  1058.   /* Save pointer to beginning of new line.  */
  1059.   save_line = new_line;
  1060.  
  1061.   while ((p = locate_arg (line)))
  1062.     {
  1063.       int i, len;
  1064.  
  1065.       memcpy (new_line, line, p - line);
  1066.       new_line += p - line;
  1067.       i = p[4] - '0';
  1068.  
  1069.       len = user_args->a[i].len;
  1070.       if (len)
  1071.     {
  1072.       memcpy (new_line, user_args->a[i].arg, len);
  1073.       new_line += len;
  1074.     }
  1075.       line = p + 5;
  1076.     }
  1077.   /* Don't forget the tail.  */
  1078.   strcpy (new_line, line);
  1079.  
  1080.   /* Return a pointer to the beginning of the new line.  */
  1081.   return save_line;
  1082. }
  1083.  
  1084. void
  1085. execute_user_command (c, args)
  1086.      struct cmd_list_element *c;
  1087.      char *args;
  1088. {
  1089.   register struct command_line *cmdlines;
  1090.   struct cleanup *old_chain;
  1091.   enum command_control_type ret;
  1092.  
  1093.   old_chain = setup_user_args (args);
  1094.  
  1095.   cmdlines = c->user_commands;
  1096.   if (cmdlines == 0)
  1097.     /* Null command */
  1098.     return;
  1099.  
  1100.   /* Set the instream to 0, indicating execution of a
  1101.      user-defined function.  */
  1102.   old_chain = make_cleanup (source_cleanup, instream);
  1103.   instream = (FILE *) 0;
  1104.   while (cmdlines)
  1105.     {
  1106.       ret = execute_control_command (cmdlines);
  1107.       if (ret != simple_control && ret != break_control)
  1108.     {
  1109.       warning ("Error in control structure.\n");
  1110.       break;
  1111.     }
  1112.       cmdlines = cmdlines->next;
  1113.     }
  1114.   do_cleanups (old_chain);
  1115. }
  1116.  
  1117. /* Execute the line P as a command.
  1118.    Pass FROM_TTY as second argument to the defining function.  */
  1119.  
  1120. void
  1121. execute_command (p, from_tty)
  1122.      char *p;
  1123.      int from_tty;
  1124. {
  1125.   register struct cmd_list_element *c;
  1126.   register enum language flang;
  1127.   static int warned = 0;
  1128.  
  1129.   free_all_values ();
  1130.  
  1131.   /* This can happen when command_line_input hits end of file.  */
  1132.   if (p == NULL)
  1133.       return;
  1134.  
  1135.   while (*p == ' ' || *p == '\t') p++;
  1136.   if (*p)
  1137.     {
  1138.       char *arg;
  1139.  
  1140.       c = lookup_cmd (&p, cmdlist, "", 0, 1);
  1141.       /* Pass null arg rather than an empty one.  */
  1142.       arg = *p ? p : 0;
  1143.  
  1144.       /* If this command has been hooked, run the hook first. */
  1145.       if (c->hook)
  1146.     execute_user_command (c->hook, (char *)0);
  1147.  
  1148.       if (c->class == class_user)
  1149.     execute_user_command (c, arg);
  1150.       else if (c->type == set_cmd || c->type == show_cmd)
  1151.     do_setshow_command (arg, from_tty & caution, c);
  1152.       else if (c->function.cfunc == NO_FUNCTION)
  1153.     error ("That is not a command, just a help topic.");
  1154.       else if (call_command_hook)
  1155.     call_command_hook (c, arg, from_tty & caution);
  1156.       else
  1157.     (*c->function.cfunc) (arg, from_tty & caution);
  1158.    }
  1159.  
  1160.   /* Tell the user if the language has changed (except first time).  */
  1161.   if (current_language != expected_language)
  1162.   {
  1163.     if (language_mode == language_mode_auto) {
  1164.       language_info (1);    /* Print what changed.  */
  1165.     }
  1166.     warned = 0;
  1167.   }
  1168.  
  1169.   /* Warn the user if the working language does not match the
  1170.      language of the current frame.  Only warn the user if we are
  1171.      actually running the program, i.e. there is a stack. */
  1172.   /* FIXME:  This should be cacheing the frame and only running when
  1173.      the frame changes.  */
  1174.  
  1175.   if (target_has_stack)
  1176.     {
  1177.       flang = get_frame_language ();
  1178.       if (!warned
  1179.       && flang != language_unknown
  1180.       && flang != current_language->la_language)
  1181.     {
  1182.       printf_filtered ("%s\n", lang_frame_mismatch_warn);
  1183.       warned = 1;
  1184.     }
  1185.     }
  1186. }
  1187.  
  1188. /* ARGSUSED */
  1189. static void
  1190. command_loop_marker (foo)
  1191.      int foo;
  1192. {
  1193. }
  1194.  
  1195. /* Read commands from `instream' and execute them
  1196.    until end of file or error reading instream.  */
  1197.  
  1198. void
  1199. command_loop ()
  1200. {
  1201.   struct cleanup *old_chain;
  1202.   char *command;
  1203.   int stdin_is_tty = ISATTY (stdin);
  1204.   long time_at_cmd_start;
  1205. #if HAVE_SBRK
  1206.   long space_at_cmd_start;
  1207. #endif
  1208.   extern int display_time;
  1209.   extern int display_space;
  1210.  
  1211.   while (!feof (instream))
  1212.     {
  1213.       if (window_hook && instream == stdin)
  1214.     (*window_hook) (instream, prompt);
  1215.  
  1216.       quit_flag = 0;
  1217.       if (instream == stdin && stdin_is_tty)
  1218.     reinitialize_more_filter ();
  1219.       old_chain = make_cleanup (command_loop_marker, 0);
  1220.       command = command_line_input (instream == stdin ? prompt : (char *) NULL,
  1221.                     instream == stdin, "prompt");
  1222.       if (command == 0)
  1223.     return;
  1224.  
  1225.       time_at_cmd_start = get_run_time ();
  1226.  
  1227.       if (display_space)
  1228.     {
  1229. #if HAVE_SBRK
  1230.       extern char **environ;
  1231.       char *lim = (char *) sbrk (0);
  1232.  
  1233.       space_at_cmd_start = (long) (lim - (char *) &environ);
  1234. #endif
  1235.     }
  1236.  
  1237.       execute_command (command, instream == stdin);
  1238.       /* Do any commands attached to breakpoint we stopped at.  */
  1239.       bpstat_do_actions (&stop_bpstat);
  1240.       do_cleanups (old_chain);
  1241.  
  1242.       if (display_time)
  1243.     {
  1244.       long cmd_time = get_run_time () - time_at_cmd_start;
  1245.  
  1246.       printf_unfiltered ("Command execution time: %ld.%06ld\n",
  1247.                  cmd_time / 1000000, cmd_time % 1000000);
  1248.     }
  1249.  
  1250.       if (display_space)
  1251.     {
  1252. #if HAVE_SBRK
  1253.       extern char **environ;
  1254.       char *lim = (char *) sbrk (0);
  1255.       long space_now = lim - (char *) &environ;
  1256.       long space_diff = space_now - space_at_cmd_start;
  1257.  
  1258.       printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
  1259.                  space_now,
  1260.                  (space_diff >= 0 ? '+' : '-'),
  1261.                  space_diff);
  1262. #endif
  1263.     }
  1264.     }
  1265. }
  1266.  
  1267. /* Commands call this if they do not want to be repeated by null lines.  */
  1268.  
  1269. void
  1270. dont_repeat ()
  1271. {
  1272.   if (server_command)
  1273.     return;
  1274.  
  1275.   /* If we aren't reading from standard input, we are saving the last
  1276.      thing read from stdin in line and don't want to delete it.  Null lines
  1277.      won't repeat here in any case.  */
  1278.   if (instream == stdin)
  1279.     *line = 0;
  1280. }
  1281.  
  1282. /* Read a line from the stream "instream" without command line editing.
  1283.  
  1284.    It prints PRROMPT once at the start.
  1285.    Action is compatible with "readline", e.g. space for the result is
  1286.    malloc'd and should be freed by the caller.
  1287.  
  1288.    A NULL return means end of file.  */
  1289. char *
  1290. gdb_readline (prrompt)
  1291.      char *prrompt;
  1292. {
  1293.   int c;
  1294.   char *result;
  1295.   int input_index = 0;
  1296.   int result_size = 80;
  1297.  
  1298.   if (prrompt)
  1299.     {
  1300.       /* Don't use a _filtered function here.  It causes the assumed
  1301.      character position to be off, since the newline we read from
  1302.      the user is not accounted for.  */
  1303.       fputs_unfiltered (prrompt, gdb_stdout);
  1304.       gdb_flush (gdb_stdout);
  1305.     }
  1306.  
  1307.   result = (char *) xmalloc (result_size);
  1308.  
  1309.   while (1)
  1310.     {
  1311.       /* Read from stdin if we are executing a user defined command.
  1312.      This is the right thing for prompt_for_continue, at least.  */
  1313.       c = fgetc (instream ? instream : stdin);
  1314.  
  1315.       if (c == EOF)
  1316.     {
  1317.       if (input_index > 0)
  1318.         /* The last line does not end with a newline.  Return it, and
  1319.            if we are called again fgetc will still return EOF and
  1320.            we'll return NULL then.  */
  1321.         break;
  1322.       free (result);
  1323.       return NULL;
  1324.     }
  1325.  
  1326.       if (c == '\n')
  1327.     break;
  1328.  
  1329.       result[input_index++] = c;
  1330.       while (input_index >= result_size)
  1331.     {
  1332.       result_size *= 2;
  1333.       result = (char *) xrealloc (result, result_size);
  1334.     }
  1335.     }
  1336.  
  1337.   result[input_index++] = '\0';
  1338.   return result;
  1339. }
  1340.  
  1341. /* Variables which control command line editing and history
  1342.    substitution.  These variables are given default values at the end
  1343.    of this file.  */
  1344. static int command_editing_p;
  1345. static int history_expansion_p;
  1346. static int write_history_p;
  1347. static int history_size;
  1348. static char *history_filename;
  1349.  
  1350. /* readline uses the word breaks for two things:
  1351.    (1) In figuring out where to point the TEXT parameter to the
  1352.    rl_completion_entry_function.  Since we don't use TEXT for much,
  1353.    it doesn't matter a lot what the word breaks are for this purpose, but
  1354.    it does affect how much stuff M-? lists.
  1355.    (2) If one of the matches contains a word break character, readline
  1356.    will quote it.  That's why we switch between
  1357.    gdb_completer_word_break_characters and
  1358.    gdb_completer_command_word_break_characters.  I'm not sure when
  1359.    we need this behavior (perhaps for funky characters in C++ symbols?).  */
  1360.  
  1361. /* Variables which are necessary for fancy command line editing.  */
  1362. char *gdb_completer_word_break_characters =
  1363.   " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
  1364.  
  1365. /* When completing on command names, we remove '-' from the list of
  1366.    word break characters, since we use it in command names.  If the
  1367.    readline library sees one in any of the current completion strings,
  1368.    it thinks that the string needs to be quoted and automatically supplies
  1369.    a leading quote. */
  1370. char *gdb_completer_command_word_break_characters =
  1371.   " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
  1372.  
  1373. /* Characters that can be used to quote completion strings.  Note that we
  1374.    can't include '"' because the gdb C parser treats such quoted sequences
  1375.    as strings. */
  1376. char *gdb_completer_quote_characters =
  1377.   "'";
  1378.  
  1379. /* Functions that are used as part of the fancy command line editing.  */
  1380.  
  1381. /* This can be used for functions which don't want to complete on symbols
  1382.    but don't want to complete on anything else either.  */
  1383. /* ARGSUSED */
  1384. char **
  1385. noop_completer (text, prefix)
  1386.      char *text;
  1387.      char *prefix;
  1388. {
  1389.   return NULL;
  1390. }
  1391.  
  1392. /* Complete on filenames.  */
  1393. char **
  1394. filename_completer (text, word)
  1395.      char *text;
  1396.      char *word;
  1397. {
  1398.   /* From readline.  */
  1399.   extern char *filename_completion_function ();
  1400.   int subsequent_name;
  1401.   char **return_val;
  1402.   int return_val_used;
  1403.   int return_val_alloced;
  1404.  
  1405.   return_val_used = 0;
  1406.   /* Small for testing.  */
  1407.   return_val_alloced = 1;
  1408.   return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
  1409.  
  1410.   subsequent_name = 0;
  1411.   while (1)
  1412.     {
  1413.       char *p;
  1414.       p = filename_completion_function (text, subsequent_name);
  1415.       if (return_val_used >= return_val_alloced)
  1416.     {
  1417.       return_val_alloced *= 2;
  1418.       return_val =
  1419.         (char **) xrealloc (return_val,
  1420.                 return_val_alloced * sizeof (char *));
  1421.     }
  1422.       if (p == NULL)
  1423.     {
  1424.       return_val[return_val_used++] = p;
  1425.       break;
  1426.     }
  1427.       /* Like emacs, don't complete on old versions.  Especially useful
  1428.      in the "source" command.  */
  1429.       if (p[strlen (p) - 1] == '~')
  1430.     continue;
  1431.  
  1432.       {
  1433.     char *q;
  1434.     if (word == text)
  1435.       /* Return exactly p.  */
  1436.       return_val[return_val_used++] = p;
  1437.     else if (word > text)
  1438.       {
  1439.         /* Return some portion of p.  */
  1440.         q = xmalloc (strlen (p) + 5);
  1441.         strcpy (q, p + (word - text));
  1442.         return_val[return_val_used++] = q;
  1443.         free (p);
  1444.       }
  1445.     else
  1446.       {
  1447.         /* Return some of TEXT plus p.  */
  1448.         q = xmalloc (strlen (p) + (text - word) + 5);
  1449.         strncpy (q, word, text - word);
  1450.         q[text - word] = '\0';
  1451.         strcat (q, p);
  1452.         return_val[return_val_used++] = q;
  1453.         free (p);
  1454.       }
  1455.       }
  1456.       subsequent_name = 1;
  1457.     }
  1458. #if 0
  1459.   /* There is no way to do this just long enough to affect quote inserting
  1460.      without also affecting the next completion.  This should be fixed in
  1461.      readline.  FIXME.  */
  1462.   /* Insure that readline does the right thing
  1463.      with respect to inserting quotes.  */
  1464.   rl_completer_word_break_characters = "";
  1465. #endif
  1466.   return return_val;
  1467. }
  1468.  
  1469. /* Here are some useful test cases for completion.  FIXME: These should
  1470.    be put in the test suite.  They should be tested with both M-? and TAB.
  1471.  
  1472.    "show output-" "radix"
  1473.    "show output" "-radix"
  1474.    "p" ambiguous (commands starting with p--path, print, printf, etc.)
  1475.    "p "  ambiguous (all symbols)
  1476.    "info t foo" no completions
  1477.    "info t " no completions
  1478.    "info t" ambiguous ("info target", "info terminal", etc.)
  1479.    "info ajksdlfk" no completions
  1480.    "info ajksdlfk " no completions
  1481.    "info" " "
  1482.    "info " ambiguous (all info commands)
  1483.    "p \"a" no completions (string constant)
  1484.    "p 'a" ambiguous (all symbols starting with a)
  1485.    "p b-a" ambiguous (all symbols starting with a)
  1486.    "p b-" ambiguous (all symbols)
  1487.    "file Make" "file" (word break hard to screw up here)
  1488.    "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
  1489.    */
  1490.  
  1491. /* Generate completions one by one for the completer.  Each time we are
  1492.    called return another potential completion to the caller.
  1493.    line_completion just completes on commands or passes the buck to the
  1494.    command's completer function, the stuff specific to symbol completion
  1495.    is in make_symbol_completion_list.
  1496.  
  1497.    TEXT is the caller's idea of the "word" we are looking at.
  1498.  
  1499.    MATCHES is the number of matches that have currently been collected from
  1500.    calling this completion function.  When zero, then we need to initialize,
  1501.    otherwise the initialization has already taken place and we can just
  1502.    return the next potential completion string.
  1503.  
  1504.    LINE_BUFFER is available to be looked at; it contains the entire text
  1505.    of the line.  POINT is the offset in that line of the cursor.  You
  1506.    should pretend that the line ends at POINT.
  1507.  
  1508.    Returns NULL if there are no more completions, else a pointer to a string
  1509.    which is a possible completion, it is the caller's responsibility to
  1510.    free the string.  */
  1511.  
  1512. static char *
  1513. line_completion_function (text, matches, line_buffer, point)
  1514.      char *text;
  1515.      int matches;
  1516.      char *line_buffer;
  1517.      int point;
  1518. {
  1519.   static char **list = (char **)NULL;        /* Cache of completions */
  1520.   static int index;                /* Next cached completion */
  1521.   char *output = NULL;
  1522.   char *tmp_command, *p;
  1523.   /* Pointer within tmp_command which corresponds to text.  */
  1524.   char *word;
  1525.   struct cmd_list_element *c, *result_list;
  1526.  
  1527.   if (matches == 0)
  1528.     {
  1529.       /* The caller is beginning to accumulate a new set of completions, so
  1530.      we need to find all of them now, and cache them for returning one at
  1531.      a time on future calls. */
  1532.  
  1533.       if (list)
  1534.     {
  1535.       /* Free the storage used by LIST, but not by the strings inside.
  1536.          This is because rl_complete_internal () frees the strings. */
  1537.       free ((PTR)list);
  1538.     }
  1539.       list = 0;
  1540.       index = 0;
  1541.  
  1542.       /* Choose the default set of word break characters to break completions.
  1543.      If we later find out that we are doing completions on command strings
  1544.      (as opposed to strings supplied by the individual command completer
  1545.      functions, which can be any string) then we will switch to the
  1546.      special word break set for command strings, which leaves out the
  1547.      '-' character used in some commands.  */
  1548.  
  1549.       rl_completer_word_break_characters =
  1550.       gdb_completer_word_break_characters;
  1551.  
  1552.       /* Decide whether to complete on a list of gdb commands or on symbols. */
  1553.       tmp_command = (char *) alloca (point + 1);
  1554.       p = tmp_command;
  1555.  
  1556.       strncpy (tmp_command, line_buffer, point);
  1557.       tmp_command[point] = '\0';
  1558.       /* Since text always contains some number of characters leading up
  1559.      to point, we can find the equivalent position in tmp_command
  1560.      by subtracting that many characters from the end of tmp_command.  */
  1561.       word = tmp_command + point - strlen (text);
  1562.  
  1563.       if (point == 0)
  1564.     {
  1565.       /* An empty line we want to consider ambiguous; that is, it
  1566.          could be any command.  */
  1567.       c = (struct cmd_list_element *) -1;
  1568.       result_list = 0;
  1569.     }
  1570.       else
  1571.     {
  1572.       c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
  1573.     }
  1574.  
  1575.       /* Move p up to the next interesting thing.  */
  1576.       while (*p == ' ' || *p == '\t')
  1577.     {
  1578.       p++;
  1579.     }
  1580.  
  1581.       if (!c)
  1582.     {
  1583.       /* It is an unrecognized command.  So there are no
  1584.          possible completions.  */
  1585.       list = NULL;
  1586.     }
  1587.       else if (c == (struct cmd_list_element *) -1)
  1588.     {
  1589.       char *q;
  1590.  
  1591.       /* lookup_cmd_1 advances p up to the first ambiguous thing, but
  1592.          doesn't advance over that thing itself.  Do so now.  */
  1593.       q = p;
  1594.       while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
  1595.         ++q;
  1596.       if (q != tmp_command + point)
  1597.         {
  1598.           /* There is something beyond the ambiguous
  1599.          command, so there are no possible completions.  For
  1600.          example, "info t " or "info t foo" does not complete
  1601.          to anything, because "info t" can be "info target" or
  1602.          "info terminal".  */
  1603.           list = NULL;
  1604.         }
  1605.       else
  1606.         {
  1607.           /* We're trying to complete on the command which was ambiguous.
  1608.          This we can deal with.  */
  1609.           if (result_list)
  1610.         {
  1611.           list = complete_on_cmdlist (*result_list->prefixlist, p,
  1612.                           word);
  1613.         }
  1614.           else
  1615.         {
  1616.           list = complete_on_cmdlist (cmdlist, p, word);
  1617.         }
  1618.           /* Insure that readline does the right thing with respect to
  1619.          inserting quotes.  */
  1620.           rl_completer_word_break_characters =
  1621.         gdb_completer_command_word_break_characters;
  1622.         }
  1623.     }
  1624.       else
  1625.     {
  1626.       /* We've recognized a full command.  */
  1627.  
  1628.       if (p == tmp_command + point)
  1629.         {
  1630.           /* There is no non-whitespace in the line beyond the command.  */
  1631.  
  1632.           if (p[-1] == ' ' || p[-1] == '\t')
  1633.         {
  1634.           /* The command is followed by whitespace; we need to complete
  1635.              on whatever comes after command.  */
  1636.           if (c->prefixlist)
  1637.             {
  1638.               /* It is a prefix command; what comes after it is
  1639.              a subcommand (e.g. "info ").  */
  1640.               list = complete_on_cmdlist (*c->prefixlist, p, word);
  1641.  
  1642.               /* Insure that readline does the right thing
  1643.              with respect to inserting quotes.  */
  1644.               rl_completer_word_break_characters =
  1645.             gdb_completer_command_word_break_characters;
  1646.             }
  1647.           else
  1648.             {
  1649.               /* It is a normal command; what comes after it is
  1650.              completed by the command's completer function.  */
  1651.               list = (*c->completer) (p, word);
  1652.             }
  1653.         }
  1654.           else
  1655.         {
  1656.           /* The command is not followed by whitespace; we need to
  1657.              complete on the command itself.  e.g. "p" which is a
  1658.              command itself but also can complete to "print", "ptype"
  1659.              etc.  */
  1660.           char *q;
  1661.  
  1662.           /* Find the command we are completing on.  */
  1663.           q = p;
  1664.           while (q > tmp_command)
  1665.             {
  1666.               if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
  1667.             --q;
  1668.               else
  1669.             break;
  1670.             }
  1671.  
  1672.           list = complete_on_cmdlist (result_list, q, word);
  1673.  
  1674.           /* Insure that readline does the right thing
  1675.              with respect to inserting quotes.  */
  1676.           rl_completer_word_break_characters =
  1677.             gdb_completer_command_word_break_characters;
  1678.         }
  1679.         }
  1680.       else
  1681.         {
  1682.           /* There is non-whitespace beyond the command.  */
  1683.  
  1684.           if (c->prefixlist && !c->allow_unknown)
  1685.         {
  1686.           /* It is an unrecognized subcommand of a prefix command,
  1687.              e.g. "info adsfkdj".  */
  1688.           list = NULL;
  1689.         }
  1690.           else
  1691.         {
  1692.           /* It is a normal command.  */
  1693.           list = (*c->completer) (p, word);
  1694.         }
  1695.         }
  1696.     }
  1697.     }
  1698.  
  1699.   /* If we found a list of potential completions during initialization then
  1700.      dole them out one at a time.  The vector of completions is NULL
  1701.      terminated, so after returning the last one, return NULL (and continue
  1702.      to do so) each time we are called after that, until a new list is
  1703.      available. */
  1704.  
  1705.   if (list)
  1706.     {
  1707.       output = list[index];
  1708.       if (output)
  1709.     {
  1710.       index++;
  1711.     }
  1712.     }
  1713.  
  1714. #if 0
  1715.   /* Can't do this because readline hasn't yet checked the word breaks
  1716.      for figuring out whether to insert a quote.  */
  1717.   if (output == NULL)
  1718.     /* Make sure the word break characters are set back to normal for the
  1719.        next time that readline tries to complete something.  */
  1720.     rl_completer_word_break_characters =
  1721.       gdb_completer_word_break_characters;
  1722. #endif
  1723.  
  1724.   return (output);
  1725. }
  1726.  
  1727. /* Line completion interface function for readline.  */
  1728.  
  1729. static char *
  1730. readline_line_completion_function (text, matches)
  1731.      char *text;
  1732.      int matches;
  1733. {
  1734.   return line_completion_function (text, matches, rl_line_buffer, rl_point);
  1735. }
  1736.  
  1737. /* Skip over a possibly quoted word (as defined by the quote characters
  1738.    and word break characters the completer uses).  Returns pointer to the
  1739.    location after the "word". */
  1740.  
  1741. char *
  1742. skip_quoted (str)
  1743.      char *str;
  1744. {
  1745.   char quote_char = '\0';
  1746.   char *scan;
  1747.  
  1748.   for (scan = str; *scan != '\0'; scan++)
  1749.     {
  1750.       if (quote_char != '\0')
  1751.     {
  1752.       /* Ignore everything until the matching close quote char */
  1753.       if (*scan == quote_char)
  1754.         {
  1755.           /* Found matching close quote. */
  1756.           scan++;
  1757.           break;
  1758.         }
  1759.     }
  1760.       else if (strchr (gdb_completer_quote_characters, *scan))
  1761.     {
  1762.       /* Found start of a quoted string. */
  1763.       quote_char = *scan;
  1764.     }
  1765.       else if (strchr (gdb_completer_word_break_characters, *scan))
  1766.     {
  1767.       break;
  1768.     }
  1769.     }
  1770.   return (scan);
  1771. }
  1772.  
  1773.  
  1774. #ifdef STOP_SIGNAL
  1775. static void
  1776. stop_sig (signo)
  1777. int signo;
  1778. {
  1779. #if STOP_SIGNAL == SIGTSTP
  1780.   signal (SIGTSTP, SIG_DFL);
  1781.   sigsetmask (0);
  1782.   kill (getpid (), SIGTSTP);
  1783.   signal (SIGTSTP, stop_sig);
  1784. #else
  1785.   signal (STOP_SIGNAL, stop_sig);
  1786. #endif
  1787.   printf_unfiltered ("%s", prompt);
  1788.   gdb_flush (gdb_stdout);
  1789.  
  1790.   /* Forget about any previous command -- null line now will do nothing.  */
  1791.   dont_repeat ();
  1792. }
  1793. #endif /* STOP_SIGNAL */
  1794.  
  1795. /* Initialize signal handlers. */
  1796. static void
  1797. do_nothing (signo)
  1798. int signo;
  1799. {
  1800. }
  1801.  
  1802. static void
  1803. init_signals ()
  1804. {
  1805.   signal (SIGINT, request_quit);
  1806.  
  1807.   /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
  1808.      passed to the inferior, which we don't want.  It would be
  1809.      possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
  1810.      on BSD4.3 systems using vfork, that can affect the
  1811.      GDB process as well as the inferior (the signal handling tables
  1812.      might be in memory, shared between the two).  Since we establish
  1813.      a handler for SIGQUIT, when we call exec it will set the signal
  1814.      to SIG_DFL for us.  */
  1815.   signal (SIGQUIT, do_nothing);
  1816.   if (signal (SIGHUP, do_nothing) != SIG_IGN)
  1817.     signal (SIGHUP, disconnect);
  1818.   signal (SIGFPE, float_handler);
  1819.  
  1820. #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
  1821.   signal (SIGWINCH, SIGWINCH_HANDLER);
  1822. #endif
  1823. }
  1824.  
  1825. /* Read one line from the command input stream `instream'
  1826.    into the local static buffer `linebuffer' (whose current length
  1827.    is `linelength').
  1828.    The buffer is made bigger as necessary.
  1829.    Returns the address of the start of the line.
  1830.  
  1831.    NULL is returned for end of file.
  1832.  
  1833.    *If* the instream == stdin & stdin is a terminal, the line read
  1834.    is copied into the file line saver (global var char *line,
  1835.    length linesize) so that it can be duplicated.
  1836.  
  1837.    This routine either uses fancy command line editing or
  1838.    simple input as the user has requested.  */
  1839.  
  1840. char *
  1841. command_line_input (prrompt, repeat, annotation_suffix)
  1842.      char *prrompt;
  1843.      int repeat;
  1844.      char *annotation_suffix;
  1845. {
  1846.   static char *linebuffer = 0;
  1847.   static unsigned linelength = 0;
  1848.   register char *p;
  1849.   char *p1;
  1850.   char *rl;
  1851.   char *local_prompt = prrompt;
  1852.   register int c;
  1853.   char *nline;
  1854.   char got_eof = 0;
  1855.  
  1856.   /* The annotation suffix must be non-NULL.  */
  1857.   if (annotation_suffix == NULL)
  1858.     annotation_suffix = "";
  1859.  
  1860.   if (annotation_level > 1 && instream == stdin)
  1861.     {
  1862.       local_prompt = alloca ((prrompt == NULL ? 0 : strlen (prrompt))
  1863.                  + strlen (annotation_suffix) + 40);
  1864.       if (prrompt == NULL)
  1865.     local_prompt[0] = '\0';
  1866.       else
  1867.     strcpy (local_prompt, prrompt);
  1868.       strcat (local_prompt, "\n\032\032");
  1869.       strcat (local_prompt, annotation_suffix);
  1870.       strcat (local_prompt, "\n");
  1871.     }
  1872.  
  1873.   if (linebuffer == 0)
  1874.     {
  1875.       linelength = 80;
  1876.       linebuffer = (char *) xmalloc (linelength);
  1877.     }
  1878.  
  1879.   p = linebuffer;
  1880.  
  1881.   /* Control-C quits instantly if typed while in this loop
  1882.      since it should not wait until the user types a newline.  */
  1883.   immediate_quit++;
  1884. #ifdef STOP_SIGNAL
  1885.   if (job_control)
  1886.     signal (STOP_SIGNAL, stop_sig);
  1887. #endif
  1888.  
  1889.   while (1)
  1890.     {
  1891.       /* Make sure that all output has been output.  Some machines may let
  1892.      you get away with leaving out some of the gdb_flush, but not all.  */
  1893.       wrap_here ("");
  1894.       gdb_flush (gdb_stdout);
  1895.       gdb_flush (gdb_stderr);
  1896.  
  1897.       if (source_file_name != NULL)
  1898.     {
  1899.       ++source_line_number;
  1900.       sprintf (source_error,
  1901.            "%s%s:%d: Error in sourced command file:\n",
  1902.            source_pre_error,
  1903.            source_file_name,
  1904.            source_line_number);
  1905.       error_pre_print = source_error;
  1906.     }
  1907.  
  1908.       if (annotation_level > 1 && instream == stdin)
  1909.     {
  1910.       printf_unfiltered ("\n\032\032pre-");
  1911.       printf_unfiltered (annotation_suffix);
  1912.       printf_unfiltered ("\n");
  1913.     }
  1914.  
  1915.       /* Don't use fancy stuff if not talking to stdin.  */
  1916.       if (command_editing_p && instream == stdin
  1917.       && ISATTY (instream))
  1918.     rl = readline (local_prompt);
  1919.       else
  1920.     rl = gdb_readline (local_prompt);
  1921.  
  1922.       if (annotation_level > 1 && instream == stdin)
  1923.     {
  1924.       printf_unfiltered ("\n\032\032post-");
  1925.       printf_unfiltered (annotation_suffix);
  1926.       printf_unfiltered ("\n");
  1927.     }
  1928.  
  1929.       if (!rl || rl == (char *) EOF)
  1930.     {
  1931.       got_eof = 1;
  1932.       break;
  1933.     }
  1934.       if (strlen(rl) + 1 + (p - linebuffer) > linelength)
  1935.     {
  1936.       linelength = strlen(rl) + 1 + (p - linebuffer);
  1937.       nline = (char *) xrealloc (linebuffer, linelength);
  1938.       p += nline - linebuffer;
  1939.       linebuffer = nline;
  1940.     }
  1941.       p1 = rl;
  1942.       /* Copy line.  Don't copy null at end.  (Leaves line alone
  1943.          if this was just a newline)  */
  1944.       while (*p1)
  1945.     *p++ = *p1++;
  1946.  
  1947.       free (rl);            /* Allocated in readline.  */
  1948.  
  1949.       if (p == linebuffer || *(p - 1) != '\\')
  1950.     break;
  1951.  
  1952.       p--;            /* Put on top of '\'.  */
  1953.       local_prompt = (char *) 0;
  1954.   }
  1955.  
  1956. #ifdef STOP_SIGNAL
  1957.   if (job_control)
  1958.     signal (STOP_SIGNAL, SIG_DFL);
  1959. #endif
  1960.   immediate_quit--;
  1961.  
  1962.   if (got_eof)
  1963.     return NULL;
  1964.  
  1965. #define SERVER_COMMAND_LENGTH 7
  1966.   server_command =
  1967.     (p - linebuffer > SERVER_COMMAND_LENGTH)
  1968.       && STREQN (linebuffer, "server ", SERVER_COMMAND_LENGTH);
  1969.   if (server_command)
  1970.     {
  1971.       /* Note that we don't set `line'.  Between this and the check in
  1972.      dont_repeat, this insures that repeating will still do the
  1973.      right thing.  */
  1974.       *p = '\0';
  1975.       return linebuffer + SERVER_COMMAND_LENGTH;
  1976.     }
  1977.  
  1978.   /* Do history expansion if that is wished.  */
  1979.   if (history_expansion_p && instream == stdin
  1980.       && ISATTY (instream))
  1981.     {
  1982.       char *history_value;
  1983.       int expanded;
  1984.  
  1985.       *p = '\0';        /* Insert null now.  */
  1986.       expanded = history_expand (linebuffer, &history_value);
  1987.       if (expanded)
  1988.     {
  1989.       /* Print the changes.  */
  1990.       printf_unfiltered ("%s\n", history_value);
  1991.  
  1992.       /* If there was an error, call this function again.  */
  1993.       if (expanded < 0)
  1994.         {
  1995.           free (history_value);
  1996.           return command_line_input (prrompt, repeat, annotation_suffix);
  1997.         }
  1998.       if (strlen (history_value) > linelength)
  1999.         {
  2000.           linelength = strlen (history_value) + 1;
  2001.           linebuffer = (char *) xrealloc (linebuffer, linelength);
  2002.         }
  2003.       strcpy (linebuffer, history_value);
  2004.       p = linebuffer + strlen(linebuffer);
  2005.       free (history_value);
  2006.     }
  2007.     }
  2008.  
  2009.   /* If we just got an empty line, and that is supposed
  2010.      to repeat the previous command, return the value in the
  2011.      global buffer.  */
  2012.   if (repeat)
  2013.     {
  2014.       if (p == linebuffer)
  2015.     return line;
  2016.       p1 = linebuffer;
  2017.       while (*p1 == ' ' || *p1 == '\t')
  2018.     p1++;
  2019.       if (!*p1)
  2020.     return line;
  2021.     }
  2022.  
  2023.   *p = 0;
  2024.  
  2025.   /* Add line to history if appropriate.  */
  2026.   if (instream == stdin
  2027.       && ISATTY (stdin) && *linebuffer)
  2028.     add_history (linebuffer);
  2029.  
  2030.   /* Note: lines consisting solely of comments are added to the command
  2031.      history.  This is useful when you type a command, and then
  2032.      realize you don't want to execute it quite yet.  You can comment
  2033.      out the command and then later fetch it from the value history
  2034.      and remove the '#'.  The kill ring is probably better, but some
  2035.      people are in the habit of commenting things out.  */
  2036.   p1 = linebuffer;
  2037.   while ((c = *p1++) != '\0')
  2038.     {
  2039.       if (c == '"')
  2040.     while ((c = *p1++) != '"')
  2041.       {
  2042.         /* Make sure an escaped '"' doesn't make us think the string
  2043.            is ended.  */
  2044.         if (c == '\\')
  2045.           parse_escape (&p1);
  2046.         if (c == '\0')
  2047.           break;
  2048.       }
  2049.       else if (c == '\'')
  2050.     while ((c = *p1++) != '\'')
  2051.       {
  2052.         /* Make sure an escaped '\'' doesn't make us think the string
  2053.            is ended.  */
  2054.         if (c == '\\')
  2055.           parse_escape (&p1);
  2056.         if (c == '\0')
  2057.           break;
  2058.       }
  2059.       else if (c == '#')
  2060.     {
  2061.       /* Found a comment.  */
  2062.       p1[-1] = '\0';
  2063.       break;
  2064.     }
  2065.     }
  2066.  
  2067.   /* Save into global buffer if appropriate.  */
  2068.   if (repeat)
  2069.     {
  2070.       if (linelength > linesize)
  2071.     {
  2072.       line = xrealloc (line, linelength);
  2073.       linesize = linelength;
  2074.     }
  2075.       strcpy (line, linebuffer);
  2076.       return line;
  2077.     }
  2078.  
  2079.   return linebuffer;
  2080. }
  2081.  
  2082.  
  2083. /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
  2084.    code bodies.  This is typically used when we encounter an "else"
  2085.    clause for an "if" command.  */
  2086.  
  2087. static void
  2088. realloc_body_list (command, new_length)
  2089.      struct command_line *command;
  2090.      int new_length;
  2091. {
  2092.   int n;
  2093.   struct command_line **body_list;
  2094.  
  2095.   n = command->body_count;
  2096.  
  2097.   /* Nothing to do?  */
  2098.   if (new_length <= n)
  2099.     return;
  2100.  
  2101.   body_list = (struct command_line **)
  2102.     xmalloc (sizeof (struct command_line *) * new_length);
  2103.  
  2104.   memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
  2105.  
  2106.   free (command->body_list);
  2107.   command->body_list = body_list;
  2108.   command->body_count = new_length;
  2109. }
  2110.  
  2111. /* Read one line from the input stream.  If the command is an "else" or
  2112.    "end", return such an indication to the caller.  */
  2113.  
  2114. static enum misc_command_type
  2115. read_next_line (command)
  2116.      struct command_line **command;
  2117. {
  2118.   char *p, *p1, *prompt_ptr, control_prompt[256];
  2119.   int i = 0;
  2120.  
  2121.   if (control_level >= 254)
  2122.     error ("Control nesting too deep!\n");
  2123.  
  2124.   /* Set a prompt based on the nesting of the control commands.  */
  2125.   if (instream == stdin)
  2126.     {
  2127.       for (i = 0; i < control_level; i++)
  2128.     control_prompt[i] = ' ';
  2129.       control_prompt[i] = '>';
  2130.       control_prompt[i+1] = '\0';
  2131.       prompt_ptr = (char *)&control_prompt[0];
  2132.     }
  2133.   else
  2134.     prompt_ptr = NULL;
  2135.  
  2136.   p = command_line_input (prompt_ptr, instream == stdin, "commands");
  2137.  
  2138.   /* Not sure what to do here.  */
  2139.   if (p == NULL)
  2140.     return end_command;
  2141.  
  2142.   /* Strip leading and trailing whitespace.  */
  2143.   while (*p == ' ' || *p == '\t')
  2144.     p++;
  2145.  
  2146.   p1 = p + strlen (p);
  2147.   while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
  2148.     p1--;
  2149.  
  2150.   /* Blanks and comments don't really do anything, but we need to
  2151.      distinguish them from else, end and other commands which can be
  2152.      executed.  */
  2153.   if (p1 == p || p[0] == '#')
  2154.     return nop_command;
  2155.       
  2156.   /* Is this the end of a simple, while, or if control structure?  */
  2157.   if (p1 - p == 3 && !strncmp (p, "end", 3))
  2158.     return end_command;
  2159.  
  2160.   /* Is the else clause of an if control structure?  */
  2161.   if (p1 - p == 4 && !strncmp (p, "else", 4))
  2162.     return else_command;
  2163.  
  2164.   /* Check for while, if, break, continue, etc and build a new command
  2165.      line structure for them.  */
  2166.   if (p1 - p > 5 && !strncmp (p, "while", 5))
  2167.     *command = build_command_line (while_control, p + 6);
  2168.   else if (p1 - p > 2 && !strncmp (p, "if", 2))
  2169.     *command = build_command_line (if_control, p + 3);
  2170.   else if (p1 - p == 5 && !strncmp (p, "loop_break", 5))
  2171.     {
  2172.       *command = (struct command_line *)
  2173.     xmalloc (sizeof (struct command_line));
  2174.       (*command)->next = NULL;
  2175.       (*command)->line = NULL;
  2176.       (*command)->control_type = break_control;
  2177.       (*command)->body_count = 0;
  2178.       (*command)->body_list = NULL;
  2179.     }
  2180.   else if (p1 - p == 8 && !strncmp (p, "loop_continue", 8))
  2181.     {
  2182.       *command = (struct command_line *)
  2183.     xmalloc (sizeof (struct command_line));
  2184.       (*command)->next = NULL;
  2185.       (*command)->line = NULL;
  2186.       (*command)->control_type = continue_control;
  2187.       (*command)->body_count = 0;
  2188.       (*command)->body_list = NULL;
  2189.     }
  2190.   else
  2191.     {
  2192.       /* A normal command.  */
  2193.       *command = (struct command_line *)
  2194.     xmalloc (sizeof (struct command_line));
  2195.       (*command)->next = NULL;
  2196.       (*command)->line = savestring (p, p1 - p);
  2197.       (*command)->control_type = simple_control;
  2198.       (*command)->body_count = 0;
  2199.       (*command)->body_list = NULL;
  2200.   }
  2201.  
  2202.   /* Nothing special.  */
  2203.   return ok_command;
  2204. }
  2205.  
  2206. /* Recursively read in the control structures and create a command_line 
  2207.    tructure from them.
  2208.  
  2209.    The parent_control parameter is the control structure in which the
  2210.    following commands are nested.  */
  2211.  
  2212. static enum command_control_type
  2213. recurse_read_control_structure (current_cmd)
  2214.      struct command_line *current_cmd;
  2215. {
  2216.   int current_body, i;
  2217.   enum misc_command_type val;
  2218.   enum command_control_type ret;
  2219.   struct command_line **body_ptr, *child_tail, *next;
  2220.   struct cleanup *old_chains, *tmp_chains;
  2221.  
  2222.   old_chains = NULL;
  2223.   child_tail = NULL;
  2224.   current_body = 1;
  2225.  
  2226.   /* Sanity checks.  */
  2227.   if (current_cmd->control_type == simple_control)
  2228.     {
  2229.       error ("Recursed on a simple control type\n");
  2230.       return invalid_control;
  2231.     }
  2232.  
  2233.   if (current_body > current_cmd->body_count)
  2234.     {
  2235.       error ("Allocated body is smaller than this command type needs\n");
  2236.       return invalid_control;
  2237.     }
  2238.  
  2239.   /* Read lines from the input stream and build control structures.  */
  2240.   while (1)
  2241.     {
  2242.       dont_repeat ();
  2243.  
  2244.       next = NULL;
  2245.       val = read_next_line (&next);
  2246.  
  2247.       /* Just skip blanks and comments.  */
  2248.       if (val == nop_command)
  2249.     continue;
  2250.  
  2251.       if (val == end_command)
  2252.     {
  2253.       if (current_cmd->control_type == while_control
  2254.           || current_cmd->control_type == if_control)
  2255.         {
  2256.           /* Success reading an entire control structure.  */
  2257.           ret = simple_control;
  2258.           break;
  2259.         }
  2260.       else
  2261.         {
  2262.           ret = invalid_control;
  2263.           break;
  2264.         }
  2265.     }
  2266.       
  2267.       /* Not the end of a control structure.  */
  2268.       if (val == else_command)
  2269.     {
  2270.       if (current_cmd->control_type == if_control
  2271.           && current_body == 1)
  2272.         {
  2273.           realloc_body_list (current_cmd, 2);
  2274.           current_body = 2;
  2275.           child_tail = NULL;
  2276.           continue;
  2277.         }
  2278.       else
  2279.         {
  2280.           ret = invalid_control;
  2281.           break;
  2282.         }
  2283.     }
  2284.  
  2285.       if (child_tail)
  2286.     {
  2287.       child_tail->next = next;
  2288.     }
  2289.       else
  2290.     {
  2291.       /* We have just read the first line of the child's control
  2292.          structure.  From now on, arrange to throw away the line
  2293.          we have if we quit or get an error.  */
  2294.       body_ptr = current_cmd->body_list;
  2295.       for (i = 1; i < current_body; i++)
  2296.         body_ptr++;
  2297.  
  2298.       *body_ptr = next;
  2299.  
  2300.       tmp_chains = make_cleanup (free_command_lines, body_ptr);
  2301.  
  2302.       if (!old_chains)
  2303.         old_chains = tmp_chains;
  2304.     }
  2305.  
  2306.       child_tail = next;
  2307.  
  2308.       /* If the latest line is another control structure, then recurse
  2309.      on it.  */
  2310.       if (next->control_type == while_control
  2311.       || next->control_type == if_control)
  2312.     {
  2313.       control_level++;
  2314.       ret = recurse_read_control_structure (next);
  2315.       control_level--;
  2316.  
  2317.       if (ret != simple_control)
  2318.         break;
  2319.     }
  2320.     }
  2321.  
  2322.   dont_repeat ();
  2323.   if (ret == invalid_control && old_chains)
  2324.     do_cleanups (old_chains);
  2325.   else if (old_chains)
  2326.     discard_cleanups (old_chains);
  2327.  
  2328.   return ret;
  2329. }
  2330.  
  2331.  
  2332. /* Read lines from the input stream
  2333.    and accumulate them in a chain of struct command_line's
  2334.    which is then returned.  */
  2335.  
  2336. struct command_line *
  2337. read_command_lines ()
  2338. {
  2339.   struct command_line *head, *tail, *next;
  2340.   struct cleanup *old_chain;
  2341.   enum command_control_type ret;
  2342.   enum misc_command_type val;
  2343.  
  2344.   head = tail = NULL;
  2345.   old_chain = NULL;
  2346.  
  2347.   while (1)
  2348.     {
  2349.       val = read_next_line (&next);
  2350.  
  2351.       /* Ignore blank lines or comments.  */
  2352.       if (val == nop_command)
  2353.     continue;
  2354.  
  2355.       if (val == end_command)
  2356.     {
  2357.       ret = simple_control;
  2358.       break;
  2359.     }
  2360.  
  2361.       if (val != ok_command)
  2362.     {
  2363.       ret = invalid_control;
  2364.       break;
  2365.     }
  2366.  
  2367.       if (next->control_type == while_control
  2368.       || next->control_type == if_control)
  2369.     {
  2370.       control_level++;
  2371.       ret = recurse_read_control_structure (next);
  2372.       control_level--;
  2373.  
  2374.       if (ret == invalid_control)
  2375.         break;
  2376.     }
  2377.       
  2378.       if (tail)
  2379.     {
  2380.       tail->next = next;
  2381.     }
  2382.       else
  2383.     {
  2384.       head = next;
  2385.       old_chain = make_cleanup (free_command_lines, &head);
  2386.     }
  2387.       tail = next;
  2388.     }
  2389.  
  2390.   dont_repeat ();
  2391.  
  2392.   if (head)
  2393.     {
  2394.       if (ret != invalid_control)
  2395.     {
  2396.       discard_cleanups (old_chain);
  2397.       return head;
  2398.     }
  2399.       else
  2400.     do_cleanups (old_chain);
  2401.     }
  2402.  
  2403.   return NULL;
  2404. }
  2405.  
  2406. /* Free a chain of struct command_line's.  */
  2407.  
  2408. void
  2409. free_command_lines (lptr)
  2410.       struct command_line **lptr;
  2411. {
  2412.   register struct command_line *l = *lptr;
  2413.   register struct command_line *next;
  2414.   struct command_line **blist;
  2415.   int i;
  2416.  
  2417.   while (l)
  2418.     {
  2419.       if (l->body_count > 0)
  2420.     {
  2421.       blist = l->body_list;
  2422.       for (i = 0; i < l->body_count; i++, blist++)
  2423.         free_command_lines (blist);
  2424.     }
  2425.       next = l->next;
  2426.       free (l->line);
  2427.       free ((PTR)l);
  2428.       l = next;
  2429.     }
  2430. }
  2431.  
  2432. /* Add an element to the list of info subcommands.  */
  2433.  
  2434. void
  2435. add_info (name, fun, doc)
  2436.      char *name;
  2437.      void (*fun) PARAMS ((char *, int));
  2438.      char *doc;
  2439. {
  2440.   add_cmd (name, no_class, fun, doc, &infolist);
  2441. }
  2442.  
  2443. /* Add an alias to the list of info subcommands.  */
  2444.  
  2445. void
  2446. add_info_alias (name, oldname, abbrev_flag)
  2447.      char *name;
  2448.      char *oldname;
  2449.      int abbrev_flag;
  2450. {
  2451.   add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
  2452. }
  2453.  
  2454. /* The "info" command is defined as a prefix, with allow_unknown = 0.
  2455.    Therefore, its own definition is called only for "info" with no args.  */
  2456.  
  2457. /* ARGSUSED */
  2458. static void
  2459. info_command (arg, from_tty)
  2460.      char *arg;
  2461.      int from_tty;
  2462. {
  2463.   printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
  2464.   help_list (infolist, "info ", -1, gdb_stdout);
  2465. }
  2466.  
  2467. /* The "complete" command is used by Emacs to implement completion.  */
  2468.  
  2469. /* ARGSUSED */
  2470. static void
  2471. complete_command (arg, from_tty)
  2472.      char *arg;
  2473.      int from_tty;
  2474. {
  2475.   int i;
  2476.   int argpoint;
  2477.   char *completion;
  2478.  
  2479.   dont_repeat ();
  2480.  
  2481.   if (arg == NULL)
  2482.     arg = "";
  2483.   argpoint = strlen (arg);
  2484.  
  2485.   for (completion = line_completion_function (arg, i = 0, arg, argpoint);
  2486.        completion;
  2487.        completion = line_completion_function (arg, ++i, arg, argpoint))
  2488.     {
  2489.       printf_unfiltered ("%s\n", completion);
  2490.       free (completion);
  2491.     }
  2492. }
  2493.  
  2494. /* The "show" command with no arguments shows all the settings.  */
  2495.  
  2496. /* ARGSUSED */
  2497. static void
  2498. show_command (arg, from_tty)
  2499.      char *arg;
  2500.      int from_tty;
  2501. {
  2502.   cmd_show_list (showlist, from_tty, "");
  2503. }
  2504.  
  2505. /* Add an element to the list of commands.  */
  2506.  
  2507. void
  2508. add_com (name, class, fun, doc)
  2509.      char *name;
  2510.      enum command_class class;
  2511.      void (*fun) PARAMS ((char *, int));
  2512.      char *doc;
  2513. {
  2514.   add_cmd (name, class, fun, doc, &cmdlist);
  2515. }
  2516.  
  2517. /* Add an alias or abbreviation command to the list of commands.  */
  2518.  
  2519. void
  2520. add_com_alias (name, oldname, class, abbrev_flag)
  2521.      char *name;
  2522.      char *oldname;
  2523.      enum command_class class;
  2524.      int abbrev_flag;
  2525. {
  2526.   add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
  2527. }
  2528.  
  2529. void
  2530. error_no_arg (why)
  2531.      char *why;
  2532. {
  2533.   error ("Argument required (%s).", why);
  2534. }
  2535.  
  2536. /* ARGSUSED */
  2537. static void
  2538. help_command (command, from_tty)
  2539.      char *command;
  2540.      int from_tty; /* Ignored */
  2541. {
  2542.   help_cmd (command, gdb_stdout);
  2543. }
  2544.  
  2545. static void
  2546. validate_comname (comname)
  2547.      char *comname;
  2548. {
  2549.   register char *p;
  2550.  
  2551.   if (comname == 0)
  2552.     error_no_arg ("name of command to define");
  2553.  
  2554.   p = comname;
  2555.   while (*p)
  2556.     {
  2557.       if (!isalnum(*p) && *p != '-')
  2558.     error ("Junk in argument list: \"%s\"", p);
  2559.       p++;
  2560.     }
  2561. }
  2562.  
  2563. /* This is just a placeholder in the command data structures.  */
  2564. static void
  2565. user_defined_command (ignore, from_tty)
  2566.      char *ignore;
  2567.      int from_tty;
  2568. {
  2569. }
  2570.  
  2571. static void
  2572. define_command (comname, from_tty)
  2573.      char *comname;
  2574.      int from_tty;
  2575. {
  2576.   register struct command_line *cmds;
  2577.   register struct cmd_list_element *c, *newc, *hookc = 0;
  2578.   char *tem = comname;
  2579. #define    HOOK_STRING    "hook-"
  2580. #define    HOOK_LEN 5
  2581.  
  2582.   validate_comname (comname);
  2583.  
  2584.   /* Look it up, and verify that we got an exact match.  */
  2585.   c = lookup_cmd (&tem, cmdlist, "", -1, 1);
  2586.   if (c && !STREQ (comname, c->name))
  2587.     c = 0;
  2588.  
  2589.   if (c)
  2590.     {
  2591.       if (c->class == class_user || c->class == class_alias)
  2592.     tem = "Redefine command \"%s\"? ";
  2593.       else
  2594.     tem = "Really redefine built-in command \"%s\"? ";
  2595.       if (!query (tem, c->name))
  2596.     error ("Command \"%s\" not redefined.", c->name);
  2597.     }
  2598.  
  2599.   /* If this new command is a hook, then mark the command which it
  2600.      is hooking.  Note that we allow hooking `help' commands, so that
  2601.      we can hook the `stop' pseudo-command.  */
  2602.  
  2603.   if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
  2604.     {
  2605.       /* Look up cmd it hooks, and verify that we got an exact match.  */
  2606.       tem = comname+HOOK_LEN;
  2607.       hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
  2608.       if (hookc && !STREQ (comname+HOOK_LEN, hookc->name))
  2609.     hookc = 0;
  2610.       if (!hookc)
  2611.     {
  2612.       warning ("Your new `%s' command does not hook any existing command.",
  2613.            comname);
  2614.       if (!query ("Proceed? ", (char *)0))
  2615.         error ("Not confirmed.");
  2616.     }
  2617.     }
  2618.  
  2619.   comname = savestring (comname, strlen (comname));
  2620.  
  2621.   /* If the rest of the commands will be case insensitive, this one
  2622.      should behave in the same manner. */
  2623.   for (tem = comname; *tem; tem++)
  2624.     if (isupper(*tem)) *tem = tolower(*tem);
  2625.  
  2626.   if (from_tty)
  2627.     {
  2628.       printf_unfiltered ("Type commands for definition of \"%s\".\n\
  2629. End with a line saying just \"end\".\n", comname);
  2630.       gdb_flush (gdb_stdout);
  2631.     }
  2632.  
  2633.   control_level = 0;
  2634.   cmds = read_command_lines ();
  2635.  
  2636.   if (c && c->class == class_user)
  2637.     free_command_lines (&c->user_commands);
  2638.  
  2639.   newc = add_cmd (comname, class_user, user_defined_command,
  2640.        (c && c->class == class_user)
  2641.        ? c->doc : savestring ("User-defined.", 13), &cmdlist);
  2642.   newc->user_commands = cmds;
  2643.  
  2644.   /* If this new command is a hook, then mark both commands as being
  2645.      tied.  */
  2646.   if (hookc)
  2647.     {
  2648.       hookc->hook = newc;    /* Target gets hooked.  */
  2649.       newc->hookee = hookc;    /* We are marked as hooking target cmd.  */
  2650.     }
  2651. }
  2652.  
  2653. static void
  2654. document_command (comname, from_tty)
  2655.      char *comname;
  2656.      int from_tty;
  2657. {
  2658.   struct command_line *doclines;
  2659.   register struct cmd_list_element *c;
  2660.   char *tem = comname;
  2661.  
  2662.   validate_comname (comname);
  2663.  
  2664.   c = lookup_cmd (&tem, cmdlist, "", 0, 1);
  2665.  
  2666.   if (c->class != class_user)
  2667.     error ("Command \"%s\" is built-in.", comname);
  2668.  
  2669.   if (from_tty)
  2670.     printf_unfiltered ("Type documentation for \"%s\".\n\
  2671. End with a line saying just \"end\".\n", comname);
  2672.  
  2673.   doclines = read_command_lines ();
  2674.  
  2675.   if (c->doc) free (c->doc);
  2676.  
  2677.   {
  2678.     register struct command_line *cl1;
  2679.     register int len = 0;
  2680.  
  2681.     for (cl1 = doclines; cl1; cl1 = cl1->next)
  2682.       len += strlen (cl1->line) + 1;
  2683.  
  2684.     c->doc = (char *) xmalloc (len + 1);
  2685.     *c->doc = 0;
  2686.  
  2687.     for (cl1 = doclines; cl1; cl1 = cl1->next)
  2688.       {
  2689.     strcat (c->doc, cl1->line);
  2690.     if (cl1->next)
  2691.       strcat (c->doc, "\n");
  2692.       }
  2693.   }
  2694.  
  2695.   free_command_lines (&doclines);
  2696. }
  2697.  
  2698. void
  2699. print_gnu_advertisement ()
  2700. {
  2701.     printf_unfiltered ("\
  2702. GDB is free software and you are welcome to distribute copies of it\n\
  2703.  under certain conditions; type \"show copying\" to see the conditions.\n\
  2704. There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\
  2705. ");
  2706. }
  2707.  
  2708. void
  2709. print_gdb_version (stream)
  2710.   GDB_FILE *stream;
  2711. {
  2712.   fprintf_filtered (stream, "\
  2713. GDB %s (%s", version, host_name);
  2714.  
  2715.   if (!STREQ (host_name, target_name))
  2716.     fprintf_filtered (stream, " --target %s", target_name);
  2717.  
  2718.   fprintf_filtered (stream, "), ");
  2719.   wrap_here("");
  2720.   fprintf_filtered (stream, "Copyright 1995 Free Software Foundation, Inc.");
  2721. }
  2722.  
  2723. /* ARGSUSED */
  2724. static void
  2725. show_version (args, from_tty)
  2726.      char *args;
  2727.      int from_tty;
  2728. {
  2729.   immediate_quit++;
  2730.   print_gnu_advertisement ();
  2731.   print_gdb_version (gdb_stdout);
  2732.   printf_filtered ("\n");
  2733.   immediate_quit--;
  2734. }
  2735.  
  2736. /* xgdb calls this to reprint the usual GDB prompt.  Obsolete now that xgdb
  2737.    is obsolete.  */
  2738.  
  2739. void
  2740. print_prompt ()
  2741. {
  2742.   printf_unfiltered ("%s", prompt);
  2743.   gdb_flush (gdb_stdout);
  2744. }
  2745.  
  2746. void
  2747. quit_command (args, from_tty)
  2748.      char *args;
  2749.      int from_tty;
  2750. {
  2751.   if (inferior_pid != 0 && target_has_execution)
  2752.     {
  2753.       if (attach_flag)
  2754.     {
  2755.       if (query ("The program is running.  Quit anyway (and detach it)? "))
  2756.         target_detach (args, from_tty);
  2757.       else
  2758.         error ("Not confirmed.");
  2759.     }
  2760.       else
  2761.     {
  2762.       if (query ("The program is running.  Quit anyway (and kill it)? "))
  2763.         target_kill ();
  2764.       else
  2765.         error ("Not confirmed.");
  2766.     }
  2767.     }
  2768.   /* UDI wants this, to kill the TIP.  */
  2769.   target_close (1);
  2770.  
  2771.   /* Save the history information if it is appropriate to do so.  */
  2772.   if (write_history_p && history_filename)
  2773.     write_history (history_filename);
  2774.  
  2775.   exit (0);
  2776. }
  2777.  
  2778. /* Returns whether GDB is running on a terminal and whether the user
  2779.    desires that questions be asked of them on that terminal.  */
  2780.  
  2781. int
  2782. input_from_terminal_p ()
  2783. {
  2784.   return gdb_has_a_terminal () && (instream == stdin) & caution;
  2785. }
  2786.  
  2787. /* ARGSUSED */
  2788. static void
  2789. pwd_command (args, from_tty)
  2790.      char *args;
  2791.      int from_tty;
  2792. {
  2793.   if (args) error ("The \"pwd\" command does not take an argument: %s", args);
  2794.   getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
  2795.  
  2796.   if (!STREQ (gdb_dirbuf, current_directory))
  2797.     printf_unfiltered ("Working directory %s\n (canonically %s).\n",
  2798.         current_directory, gdb_dirbuf);
  2799.   else
  2800.     printf_unfiltered ("Working directory %s.\n", current_directory);
  2801. }
  2802.  
  2803. void
  2804. cd_command (dir, from_tty)
  2805.      char *dir;
  2806.      int from_tty;
  2807. {
  2808.   int len;
  2809.   /* Found something other than leading repetitions of "/..".  */
  2810.   int found_real_path;
  2811.   char *p;
  2812.  
  2813.   /* If the new directory is absolute, repeat is a no-op; if relative,
  2814.      repeat might be useful but is more likely to be a mistake.  */
  2815.   dont_repeat ();
  2816.  
  2817.   if (dir == 0)
  2818.     error_no_arg ("new working directory");
  2819.  
  2820.   dir = tilde_expand (dir);
  2821.   make_cleanup (free, dir);
  2822.  
  2823.   if (chdir (dir) < 0)
  2824.     perror_with_name (dir);
  2825.  
  2826.   len = strlen (dir);
  2827.   dir = savestring (dir, len - (len > 1 && dir[len-1] == '/'));
  2828.   if (dir[0] == '/')
  2829.     current_directory = dir;
  2830.   else
  2831.     {
  2832.       if (current_directory[0] == '/' && current_directory[1] == '\0')
  2833.     current_directory = concat (current_directory, dir, NULL);
  2834.       else
  2835.     current_directory = concat (current_directory, "/", dir, NULL);
  2836.       free (dir);
  2837.     }
  2838.  
  2839.   /* Now simplify any occurrences of `.' and `..' in the pathname.  */
  2840.  
  2841.   found_real_path = 0;
  2842.   for (p = current_directory; *p;)
  2843.     {
  2844.       if (p[0] == '/' && p[1] == '.' && (p[2] == 0 || p[2] == '/'))
  2845.     strcpy (p, p + 2);
  2846.       else if (p[0] == '/' && p[1] == '.' && p[2] == '.'
  2847.            && (p[3] == 0 || p[3] == '/'))
  2848.     {
  2849.       if (found_real_path)
  2850.         {
  2851.           /* Search backwards for the directory just before the "/.."
  2852.          and obliterate it and the "/..".  */
  2853.           char *q = p;
  2854.           while (q != current_directory && q[-1] != '/')
  2855.         --q;
  2856.  
  2857.           if (q == current_directory)
  2858.         /* current_directory is
  2859.            a relative pathname ("can't happen"--leave it alone).  */
  2860.         ++p;
  2861.           else
  2862.         {
  2863.           strcpy (q - 1, p + 3);
  2864.           p = q - 1;
  2865.         }
  2866.         }
  2867.       else
  2868.         /* We are dealing with leading repetitions of "/..", for example
  2869.            "/../..", which is the Mach super-root.  */
  2870.         p += 3;
  2871.     }
  2872.       else
  2873.     {
  2874.       found_real_path = 1;
  2875.       ++p;
  2876.     }
  2877.     }
  2878.  
  2879.   forget_cached_source_info ();
  2880.  
  2881.   if (from_tty)
  2882.     pwd_command ((char *) 0, 1);
  2883. }
  2884.  
  2885. struct source_cleanup_lines_args {
  2886.   int old_line;
  2887.   char *old_file;
  2888.   char *old_pre_error;
  2889.   char *old_error_pre_print;
  2890. };
  2891.  
  2892. static void
  2893. source_cleanup_lines (args)
  2894.      PTR args;
  2895. {
  2896.   struct source_cleanup_lines_args *p =
  2897.     (struct source_cleanup_lines_args *)args;
  2898.   source_line_number = p->old_line;
  2899.   source_file_name = p->old_file;
  2900.   source_pre_error = p->old_pre_error;
  2901.   error_pre_print = p->old_error_pre_print;
  2902. }
  2903.  
  2904. /* ARGSUSED */
  2905. void
  2906. source_command (args, from_tty)
  2907.      char *args;
  2908.      int from_tty;
  2909. {
  2910.   FILE *stream;
  2911.   struct cleanup *old_cleanups;
  2912.   char *file = args;
  2913.   struct source_cleanup_lines_args old_lines;
  2914.   int needed_length;
  2915.  
  2916.   if (file == NULL)
  2917.     {
  2918.       error ("source command requires pathname of file to source.");
  2919.     }
  2920.  
  2921.   file = tilde_expand (file);
  2922.   old_cleanups = make_cleanup (free, file);
  2923.  
  2924.   stream = fopen (file, FOPEN_RT);
  2925.   if (stream == 0)
  2926.     perror_with_name (file);
  2927.  
  2928.   make_cleanup (fclose, stream);
  2929.  
  2930.   old_lines.old_line = source_line_number;
  2931.   old_lines.old_file = source_file_name;
  2932.   old_lines.old_pre_error = source_pre_error;
  2933.   old_lines.old_error_pre_print = error_pre_print;
  2934.   make_cleanup (source_cleanup_lines, &old_lines);
  2935.   source_line_number = 0;
  2936.   source_file_name = file;
  2937.   source_pre_error = error_pre_print == NULL ? "" : error_pre_print;
  2938.   source_pre_error = savestring (source_pre_error, strlen (source_pre_error));
  2939.   make_cleanup (free, source_pre_error);
  2940.   /* This will get set every time we read a line.  So it won't stay "" for
  2941.      long.  */
  2942.   error_pre_print = "";
  2943.  
  2944.   needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80;
  2945.   if (source_error_allocated < needed_length)
  2946.     {
  2947.       source_error_allocated *= 2;
  2948.       if (source_error_allocated < needed_length)
  2949.     source_error_allocated = needed_length;
  2950.       if (source_error == NULL)
  2951.     source_error = xmalloc (source_error_allocated);
  2952.       else
  2953.     source_error = xrealloc (source_error, source_error_allocated);
  2954.     }
  2955.  
  2956.   read_command_file (stream);
  2957.  
  2958.   do_cleanups (old_cleanups);
  2959. }
  2960.  
  2961. /* ARGSUSED */
  2962. static void
  2963. echo_command (text, from_tty)
  2964.      char *text;
  2965.      int from_tty;
  2966. {
  2967.   char *p = text;
  2968.   register int c;
  2969.  
  2970.   if (text)
  2971.     while ((c = *p++) != '\0')
  2972.       {
  2973.     if (c == '\\')
  2974.       {
  2975.         /* \ at end of argument is used after spaces
  2976.            so they won't be lost.  */
  2977.         if (*p == 0)
  2978.           return;
  2979.  
  2980.         c = parse_escape (&p);
  2981.         if (c >= 0)
  2982.           printf_filtered ("%c", c);
  2983.       }
  2984.     else
  2985.       printf_filtered ("%c", c);
  2986.       }
  2987.  
  2988.   /* Force this output to appear now.  */
  2989.   wrap_here ("");
  2990.   gdb_flush (gdb_stdout);
  2991. }
  2992.  
  2993.  
  2994. #ifdef TARGET_BYTE_ORDER_SELECTABLE
  2995.  
  2996. /* Functions to manipulate the endianness of the target.  */
  2997.  
  2998. #ifndef TARGET_BYTE_ORDER_DEFAULT
  2999. #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN
  3000. #endif
  3001.  
  3002. int target_byte_order = TARGET_BYTE_ORDER_DEFAULT;
  3003.  
  3004. static int target_byte_order_auto = 1;
  3005.  
  3006. /* Called if the user enters ``set endian'' without an argument.  */
  3007. static void
  3008. set_endian (args, from_tty)
  3009.      char *args;
  3010.      int from_tty;
  3011. {
  3012.   printf_unfiltered ("\"set endian\" must be followed by \"auto\", \"big\" or \"little\".\n");
  3013.   show_endian (args, from_tty);
  3014. }
  3015.  
  3016. /* Called by ``set endian big''.  */
  3017. static void
  3018. set_endian_big (args, from_tty)
  3019.      char *args;
  3020.      int from_tty;
  3021. {
  3022.   target_byte_order = BIG_ENDIAN;
  3023.   target_byte_order_auto = 0;
  3024. }
  3025.  
  3026. /* Called by ``set endian little''.  */
  3027. static void
  3028. set_endian_little (args, from_tty)
  3029.      char *args;
  3030.      int from_tty;
  3031. {
  3032.   target_byte_order = LITTLE_ENDIAN;
  3033.   target_byte_order_auto = 0;
  3034. }
  3035.  
  3036. /* Called by ``set endian auto''.  */
  3037. static void
  3038. set_endian_auto (args, from_tty)
  3039.      char *args;
  3040.      int from_tty;
  3041. {
  3042.   target_byte_order_auto = 1;
  3043. }
  3044.  
  3045. /* Called by ``show endian''.  */
  3046. static void
  3047. show_endian (args, from_tty)
  3048.      char *args;
  3049.      int from_tty;
  3050. {
  3051.   const char *msg =
  3052.     (target_byte_order_auto
  3053.      ? "The target endianness is set automatically (currently %s endian)\n"
  3054.      : "The target is assumed to be %s endian\n");
  3055.   printf_unfiltered (msg, TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little");
  3056. }
  3057.  
  3058. #endif /* defined (TARGET_BYTE_ORDER_SELECTABLE) */
  3059.  
  3060. /* Set the endianness from a BFD.  */
  3061. void
  3062. set_endian_from_file (abfd)
  3063.      bfd *abfd;
  3064. {
  3065. #ifdef TARGET_BYTE_ORDER_SELECTABLE
  3066.   int want;
  3067.  
  3068.   if (abfd->xvec->byteorder_big_p)
  3069.     want = BIG_ENDIAN;
  3070.   else
  3071.     want = LITTLE_ENDIAN;
  3072.   if (target_byte_order_auto)
  3073.     target_byte_order = want;
  3074.   else if (target_byte_order != want)
  3075.     warning ("%s endian file does not match %s endian target.",
  3076.          want == BIG_ENDIAN ? "big" : "little",
  3077.          TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little");
  3078.  
  3079. #else /* ! defined (TARGET_BYTE_ORDER_SELECTABLE) */
  3080.  
  3081.   if (abfd->xvec->byteorder_big_p
  3082.       ? TARGET_BYTE_ORDER != BIG_ENDIAN
  3083.       : TARGET_BYTE_ORDER == BIG_ENDIAN)
  3084.     warning ("%s endian file does not match %s endian target.",
  3085.          abfd->xvec->byteorder_big_p ? "big" : "little",
  3086.          TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little");
  3087.  
  3088. #endif /* ! defined (TARGET_BYTE_ORDER_SELECTABLE) */
  3089. }
  3090.  
  3091. /* Functions to manipulate command line editing control variables.  */
  3092.  
  3093. /* Number of commands to print in each call to show_commands.  */
  3094. #define Hist_print 10
  3095. static void
  3096. show_commands (args, from_tty)
  3097.      char *args;
  3098.      int from_tty;
  3099. {
  3100.   /* Index for history commands.  Relative to history_base.  */
  3101.   int offset;
  3102.  
  3103.   /* Number of the history entry which we are planning to display next.
  3104.      Relative to history_base.  */
  3105.   static int num = 0;
  3106.  
  3107.   /* The first command in the history which doesn't exist (i.e. one more
  3108.      than the number of the last command).  Relative to history_base.  */
  3109.   int hist_len;
  3110.  
  3111.   extern HIST_ENTRY *history_get PARAMS ((int));
  3112.  
  3113.   /* Print out some of the commands from the command history.  */
  3114.   /* First determine the length of the history list.  */
  3115.   hist_len = history_size;
  3116.   for (offset = 0; offset < history_size; offset++)
  3117.     {
  3118.       if (!history_get (history_base + offset))
  3119.     {
  3120.       hist_len = offset;
  3121.       break;
  3122.     }
  3123.     }
  3124.  
  3125.   if (args)
  3126.     {
  3127.       if (args[0] == '+' && args[1] == '\0')
  3128.     /* "info editing +" should print from the stored position.  */
  3129.     ;
  3130.       else
  3131.     /* "info editing <exp>" should print around command number <exp>.  */
  3132.     num = (parse_and_eval_address (args) - history_base) - Hist_print / 2;
  3133.     }
  3134.   /* "show commands" means print the last Hist_print commands.  */
  3135.   else
  3136.     {
  3137.       num = hist_len - Hist_print;
  3138.     }
  3139.  
  3140.   if (num < 0)
  3141.     num = 0;
  3142.  
  3143.   /* If there are at least Hist_print commands, we want to display the last
  3144.      Hist_print rather than, say, the last 6.  */
  3145.   if (hist_len - num < Hist_print)
  3146.     {
  3147.       num = hist_len - Hist_print;
  3148.       if (num < 0)
  3149.     num = 0;
  3150.     }
  3151.  
  3152.   for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
  3153.     {
  3154.       printf_filtered ("%5d  %s\n", history_base + offset,
  3155.           (history_get (history_base + offset))->line);
  3156.     }
  3157.  
  3158.   /* The next command we want to display is the next one that we haven't
  3159.      displayed yet.  */
  3160.   num += Hist_print;
  3161.  
  3162.   /* If the user repeats this command with return, it should do what
  3163.      "show commands +" does.  This is unnecessary if arg is null,
  3164.      because "show commands +" is not useful after "show commands".  */
  3165.   if (from_tty && args)
  3166.     {
  3167.       args[0] = '+';
  3168.       args[1] = '\0';
  3169.     }
  3170. }
  3171.  
  3172. /* Called by do_setshow_command.  */
  3173. /* ARGSUSED */
  3174. static void
  3175. set_history_size_command (args, from_tty, c)
  3176.      char *args;
  3177.      int from_tty;
  3178.      struct cmd_list_element *c;
  3179. {
  3180.   if (history_size == INT_MAX)
  3181.     unstifle_history ();
  3182.   else if (history_size >= 0)
  3183.     stifle_history (history_size);
  3184.   else
  3185.     {
  3186.       history_size = INT_MAX;
  3187.       error ("History size must be non-negative");
  3188.     }
  3189. }
  3190.  
  3191. /* ARGSUSED */
  3192. static void
  3193. set_history (args, from_tty)
  3194.      char *args;
  3195.      int from_tty;
  3196. {
  3197.   printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
  3198.   help_list (sethistlist, "set history ", -1, gdb_stdout);
  3199. }
  3200.  
  3201. /* ARGSUSED */
  3202. static void
  3203. show_history (args, from_tty)
  3204.      char *args;
  3205.      int from_tty;
  3206. {
  3207.   cmd_show_list (showhistlist, from_tty, "");
  3208. }
  3209.  
  3210. int info_verbose = 0;        /* Default verbose msgs off */
  3211.  
  3212. /* Called by do_setshow_command.  An elaborate joke.  */
  3213. /* ARGSUSED */
  3214. static void
  3215. set_verbose (args, from_tty, c)
  3216.      char *args;
  3217.      int from_tty;
  3218.      struct cmd_list_element *c;
  3219. {
  3220.   char *cmdname = "verbose";
  3221.   struct cmd_list_element *showcmd;
  3222.  
  3223.   showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
  3224.  
  3225.   if (info_verbose)
  3226.     {
  3227.       c->doc = "Set verbose printing of informational messages.";
  3228.       showcmd->doc = "Show verbose printing of informational messages.";
  3229.     }
  3230.   else
  3231.     {
  3232.       c->doc = "Set verbosity.";
  3233.       showcmd->doc = "Show verbosity.";
  3234.     }
  3235. }
  3236.  
  3237. static void
  3238. float_handler (signo)
  3239. int signo;
  3240. {
  3241.   /* This message is based on ANSI C, section 4.7.  Note that integer
  3242.      divide by zero causes this, so "float" is a misnomer.  */
  3243.   signal (SIGFPE, float_handler);
  3244.   error ("Erroneous arithmetic operation.");
  3245. }
  3246.  
  3247.  
  3248. static void
  3249. init_cmd_lists ()
  3250. {
  3251.   cmdlist = NULL;
  3252.   infolist = NULL;
  3253.   enablelist = NULL;
  3254.   disablelist = NULL;
  3255.   deletelist = NULL;
  3256.   enablebreaklist = NULL;
  3257.   setlist = NULL;
  3258.   unsetlist = NULL;
  3259.   showlist = NULL;
  3260. #ifdef TARGET_BYTE_ORDER_SELECTABLE
  3261.   endianlist = NULL;
  3262. #endif
  3263.   sethistlist = NULL;
  3264.   showhistlist = NULL;
  3265.   unsethistlist = NULL;
  3266. #if MAINTENANCE_CMDS
  3267.   maintenancelist = NULL;
  3268.   maintenanceinfolist = NULL;
  3269.   maintenanceprintlist = NULL;
  3270. #endif
  3271.   setprintlist = NULL;
  3272.   showprintlist = NULL;
  3273.   setchecklist = NULL;
  3274.   showchecklist = NULL;
  3275. }
  3276.  
  3277. /* Init the history buffer.  Note that we are called after the init file(s)
  3278.  * have been read so that the user can change the history file via his
  3279.  * .gdbinit file (for instance).  The GDBHISTFILE environment variable
  3280.  * overrides all of this.
  3281.  */
  3282.  
  3283. void
  3284. init_history()
  3285. {
  3286.   char *tmpenv;
  3287.  
  3288.   tmpenv = getenv ("HISTSIZE");
  3289.   if (tmpenv)
  3290.     history_size = atoi (tmpenv);
  3291.   else if (!history_size)
  3292.     history_size = 256;
  3293.  
  3294.   stifle_history (history_size);
  3295.  
  3296.   tmpenv = getenv ("GDBHISTFILE");
  3297.   if (tmpenv)
  3298.     history_filename = savestring (tmpenv, strlen(tmpenv));
  3299.   else if (!history_filename) {
  3300.     /* We include the current directory so that if the user changes
  3301.        directories the file written will be the same as the one
  3302.        that was read.  */
  3303.     history_filename = concat (current_directory, "/.gdb_history", NULL);
  3304.   }
  3305.   read_history (history_filename);
  3306. }
  3307.  
  3308. static void
  3309. init_main ()
  3310. {
  3311.   struct cmd_list_element *c;
  3312.  
  3313. #ifdef TARGET_BYTE_ORDER_SELECTABLE
  3314.  
  3315.   add_prefix_cmd ("endian", class_support, set_endian,
  3316.           "Set endianness of target.",
  3317.           &endianlist, "set endian ", 0, &setlist);
  3318.   add_cmd ("big", class_support, set_endian_big,
  3319.        "Set target as being big endian.", &endianlist);
  3320.   add_cmd ("little", class_support, set_endian_little,
  3321.        "Set target as being little endian.", &endianlist);
  3322.   add_cmd ("auto", class_support, set_endian_auto,
  3323.        "Select target endianness automatically.", &endianlist);
  3324.   add_cmd ("endian", class_support, show_endian,
  3325.        "Show endianness of target.", &showlist);
  3326.  
  3327. #endif /* defined (TARGET_BYTE_ORDER_SELECTABLE) */
  3328.  
  3329. #ifdef DEFAULT_PROMPT
  3330.   prompt = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT));
  3331. #else
  3332.   prompt = savestring ("(gdb) ", 6);
  3333. #endif
  3334.  
  3335.   /* Set the important stuff up for command editing.  */
  3336.   command_editing_p = 1;
  3337.   history_expansion_p = 0;
  3338.   write_history_p = 0;
  3339.  
  3340.   /* Setup important stuff for command line editing.  */
  3341.   rl_completion_entry_function = (int (*)()) readline_line_completion_function;
  3342.   rl_completer_word_break_characters = gdb_completer_word_break_characters;
  3343.   rl_completer_quote_characters = gdb_completer_quote_characters;
  3344.   rl_readline_name = "gdb";
  3345.  
  3346.   /* Define the classes of commands.
  3347.      They will appear in the help list in the reverse of this order.  */
  3348.  
  3349.   add_cmd ("internals", class_maintenance, NO_FUNCTION,
  3350.        "Maintenance commands.\n\
  3351. Some gdb commands are provided just for use by gdb maintainers.\n\
  3352. These commands are subject to frequent change, and may not be as\n\
  3353. well documented as user commands.",
  3354.        &cmdlist);
  3355.   add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist);
  3356.   add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist);
  3357.   add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\
  3358. The commands in this class are those defined by the user.\n\
  3359. Use the \"define\" command to define a command.", &cmdlist);
  3360.   add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist);
  3361.   add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist);
  3362.   add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist);
  3363.   add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist);
  3364.   add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist);
  3365.   add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\
  3366. The stack is made up of stack frames.  Gdb assigns numbers to stack frames\n\
  3367. counting from zero for the innermost (currently executing) frame.\n\n\
  3368. At any time gdb identifies one frame as the \"selected\" frame.\n\
  3369. Variable lookups are done with respect to the selected frame.\n\
  3370. When the program being debugged stops, gdb selects the innermost frame.\n\
  3371. The commands below can be used to select other frames by number or address.",
  3372.        &cmdlist);
  3373.   add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist);
  3374.  
  3375.   add_com ("pwd", class_files, pwd_command,
  3376.        "Print working directory.  This is used for your program as well.");
  3377.   c = add_cmd ("cd", class_files, cd_command,
  3378.        "Set working directory to DIR for debugger and program being debugged.\n\
  3379. The change does not take effect for the program being debugged\n\
  3380. until the next time it is started.", &cmdlist);
  3381.   c->completer = filename_completer;
  3382.  
  3383.   add_show_from_set
  3384.     (add_set_cmd ("prompt", class_support, var_string, (char *)&prompt,
  3385.        "Set gdb's prompt",
  3386.        &setlist),
  3387.      &showlist);
  3388.  
  3389.   add_com ("echo", class_support, echo_command,
  3390.        "Print a constant string.  Give string as argument.\n\
  3391. C escape sequences may be used in the argument.\n\
  3392. No newline is added at the end of the argument;\n\
  3393. use \"\\n\" if you want a newline to be printed.\n\
  3394. Since leading and trailing whitespace are ignored in command arguments,\n\
  3395. if you want to print some you must use \"\\\" before leading whitespace\n\
  3396. to be printed or after trailing whitespace.");
  3397.   add_com ("document", class_support, document_command,
  3398.        "Document a user-defined command.\n\
  3399. Give command name as argument.  Give documentation on following lines.\n\
  3400. End with a line of just \"end\".");
  3401.   add_com ("define", class_support, define_command,
  3402.        "Define a new command name.  Command name is argument.\n\
  3403. Definition appears on following lines, one command per line.\n\
  3404. End with a line of just \"end\".\n\
  3405. Use the \"document\" command to give documentation for the new command.\n\
  3406. Commands defined in this way may have up to ten arguments.");
  3407.  
  3408. #ifdef __STDC__
  3409.   c = add_cmd ("source", class_support, source_command,
  3410.        "Read commands from a file named FILE.\n\
  3411. Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\
  3412. when gdb is started.", &cmdlist);
  3413. #else
  3414.   /* Punt file name, we can't help it easily.  */
  3415.   c = add_cmd ("source", class_support, source_command,
  3416.        "Read commands from a file named FILE.\n\
  3417. Note that the file \".gdbinit\" is read automatically in this way\n\
  3418. when gdb is started.", &cmdlist);
  3419. #endif
  3420.   c->completer = filename_completer;
  3421.  
  3422.   add_com ("quit", class_support, quit_command, "Exit gdb.");
  3423.   add_com ("help", class_support, help_command, "Print list of commands.");
  3424.   add_com_alias ("q", "quit", class_support, 1);
  3425.   add_com_alias ("h", "help", class_support, 1);
  3426.  
  3427.  
  3428.   c = add_set_cmd ("verbose", class_support, var_boolean, (char *)&info_verbose,
  3429.            "Set ",
  3430.            &setlist),
  3431.   add_show_from_set (c, &showlist);
  3432.   c->function.sfunc = set_verbose;
  3433.   set_verbose (NULL, 0, c);
  3434.  
  3435.   add_show_from_set
  3436.     (add_set_cmd ("editing", class_support, var_boolean, (char *)&command_editing_p,
  3437.        "Set editing of command lines as they are typed.\n\
  3438. Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
  3439. Without an argument, command line editing is enabled.  To edit, use\n\
  3440. EMACS-like or VI-like commands like control-P or ESC.", &setlist),
  3441.      &showlist);
  3442.  
  3443.   add_prefix_cmd ("history", class_support, set_history,
  3444.           "Generic command for setting command history parameters.",
  3445.           &sethistlist, "set history ", 0, &setlist);
  3446.   add_prefix_cmd ("history", class_support, show_history,
  3447.           "Generic command for showing command history parameters.",
  3448.           &showhistlist, "show history ", 0, &showlist);
  3449.  
  3450.   add_show_from_set
  3451.     (add_set_cmd ("expansion", no_class, var_boolean, (char *)&history_expansion_p,
  3452.        "Set history expansion on command input.\n\
  3453. Without an argument, history expansion is enabled.", &sethistlist),
  3454.      &showhistlist);
  3455.  
  3456.   add_show_from_set
  3457.     (add_set_cmd ("save", no_class, var_boolean, (char *)&write_history_p,
  3458.        "Set saving of the history record on exit.\n\
  3459. Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
  3460. Without an argument, saving is enabled.", &sethistlist),
  3461.      &showhistlist);
  3462.  
  3463.   c = add_set_cmd ("size", no_class, var_integer, (char *)&history_size,
  3464.            "Set the size of the command history, \n\
  3465. ie. the number of previous commands to keep a record of.", &sethistlist);
  3466.   add_show_from_set (c, &showhistlist);
  3467.   c->function.sfunc = set_history_size_command;
  3468.  
  3469.   add_show_from_set
  3470.     (add_set_cmd ("filename", no_class, var_filename, (char *)&history_filename,
  3471.        "Set the filename in which to record the command history\n\
  3472.  (the list of previous commands of which a record is kept).", &sethistlist),
  3473.      &showhistlist);
  3474.  
  3475.   add_show_from_set
  3476.     (add_set_cmd ("confirm", class_support, var_boolean,
  3477.           (char *)&caution,
  3478.           "Set whether to confirm potentially dangerous operations.",
  3479.           &setlist),
  3480.      &showlist);
  3481.  
  3482.   add_prefix_cmd ("info", class_info, info_command,
  3483.         "Generic command for showing things about the program being debugged.",
  3484.           &infolist, "info ", 0, &cmdlist);
  3485.   add_com_alias ("i", "info", class_info, 1);
  3486.  
  3487.   add_com ("complete", class_obscure, complete_command,
  3488.        "List the completions for the rest of the line as a command.");
  3489.  
  3490.   add_prefix_cmd ("show", class_info, show_command,
  3491.           "Generic command for showing things about the debugger.",
  3492.           &showlist, "show ", 0, &cmdlist);
  3493.   /* Another way to get at the same thing.  */
  3494.   add_info ("set", show_command, "Show all GDB settings.");
  3495.  
  3496.   add_cmd ("commands", no_class, show_commands,
  3497.        "Show the the history of commands you typed.\n\
  3498. You can supply a command number to start with, or a `+' to start after\n\
  3499. the previous command number shown.",
  3500.        &showlist);
  3501.  
  3502.   add_cmd ("version", no_class, show_version,
  3503.        "Show what version of GDB this is.", &showlist);
  3504.  
  3505.   add_com ("while", class_support, while_command,
  3506. "Execute nested commands WHILE the conditional expression is non zero.\n\
  3507. The conditional expression must follow the word `while' and must in turn be\
  3508. followed by a new line.  The nested commands must be entered one per line,\
  3509. and should be terminated by the word `end'.");
  3510.  
  3511.   add_com ("if", class_support, if_command,
  3512. "Execute nested commands once IF the conditional expression is non zero.\n\
  3513. The conditional expression must follow the word `if' and must in turn be\
  3514. followed by a new line.  The nested commands must be entered one per line,\
  3515. and should be terminated by the word 'else' or `end'.  If an else clause\
  3516. is used, the same rules apply to its nested commands as to the first ones.");
  3517.  
  3518.   /* If target is open when baud changes, it doesn't take effect until the
  3519.      next open (I think, not sure).  */
  3520.   add_show_from_set (add_set_cmd ("remotebaud", no_class,
  3521.                   var_zinteger, (char *)&baud_rate,
  3522.                   "Set baud rate for remote serial I/O.\n\
  3523. This value is used to set the speed of the serial port when debugging\n\
  3524. using remote targets.", &setlist),
  3525.              &showlist);
  3526.  
  3527.   add_show_from_set (
  3528.     add_set_cmd ("remotedebug", no_class, var_zinteger, (char *)&remote_debug,
  3529.            "Set debugging of remote protocol.\n\
  3530. When enabled, each packet sent or received with the remote target\n\
  3531. is displayed.", &setlist),
  3532.              &showlist);
  3533. }
  3534.