119370Spst/* Top level stuff for GDB, the GNU debugger.
298948Sobrien
398948Sobrien   Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4130809Smarcel   1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
519370Spst   Free Software Foundation, Inc.
619370Spst
798948Sobrien   This file is part of GDB.
819370Spst
998948Sobrien   This program is free software; you can redistribute it and/or modify
1098948Sobrien   it under the terms of the GNU General Public License as published by
1198948Sobrien   the Free Software Foundation; either version 2 of the License, or
1298948Sobrien   (at your option) any later version.
1319370Spst
1498948Sobrien   This program is distributed in the hope that it will be useful,
1598948Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1698948Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1798948Sobrien   GNU General Public License for more details.
1819370Spst
1998948Sobrien   You should have received a copy of the GNU General Public License
2098948Sobrien   along with this program; if not, write to the Free Software
2198948Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2298948Sobrien   Boston, MA 02111-1307, USA.  */
2319370Spst
2419370Spst#include "defs.h"
2519370Spst#include "gdbcmd.h"
2619370Spst#include "call-cmds.h"
2798948Sobrien#include "cli/cli-cmds.h"
2898948Sobrien#include "cli/cli-script.h"
2998948Sobrien#include "cli/cli-setshow.h"
30130809Smarcel#include "cli/cli-decode.h"
3119370Spst#include "symtab.h"
3219370Spst#include "inferior.h"
3398948Sobrien#include <signal.h>
3419370Spst#include "target.h"
3519370Spst#include "breakpoint.h"
3619370Spst#include "gdbtypes.h"
3719370Spst#include "expression.h"
3819370Spst#include "value.h"
3919370Spst#include "language.h"
4098948Sobrien#include "terminal.h"		/* For job_control.  */
4119370Spst#include "annotate.h"
4298948Sobrien#include "completer.h"
4319370Spst#include "top.h"
4498948Sobrien#include "version.h"
4598948Sobrien#include "serial.h"
4698948Sobrien#include "doublest.h"
4798948Sobrien#include "gdb_assert.h"
4819370Spst
4919370Spst/* readline include files */
50130809Smarcel#include "readline/readline.h"
51130809Smarcel#include "readline/history.h"
5219370Spst
5319370Spst/* readline defines this.  */
5419370Spst#undef savestring
5519370Spst
5619370Spst#include <sys/types.h>
5719370Spst
5898948Sobrien#include <setjmp.h>
5998948Sobrien
6098948Sobrien#include "event-top.h"
6119370Spst#include "gdb_string.h"
6219370Spst#include "gdb_stat.h"
6319370Spst#include <ctype.h>
6498948Sobrien#include "ui-out.h"
6598948Sobrien#include "cli-out.h"
6619370Spst
6798948Sobrien/* Default command line prompt.  This is overriden in some configs. */
6819370Spst
6998948Sobrien#ifndef DEFAULT_PROMPT
7098948Sobrien#define DEFAULT_PROMPT	"(gdb) "
7146283Sdfr#endif
7246283Sdfr
7319370Spst/* Initialization file name for gdb.  This is overridden in some configs.  */
7419370Spst
7519370Spst#ifndef	GDBINIT_FILENAME
7619370Spst#define	GDBINIT_FILENAME	".gdbinit"
7719370Spst#endif
7819370Spstchar gdbinit[] = GDBINIT_FILENAME;
7919370Spst
8019370Spstint inhibit_gdbinit = 0;
8119370Spst
8219370Spst/* If nonzero, and GDB has been configured to be able to use windows,
8319370Spst   attempt to open them upon startup.  */
8419370Spst
85130809Smarcelint use_windows = 0;
8619370Spst
8719370Spstextern char lang_frame_mismatch_warn[];		/* language.c */
8819370Spst
8919370Spst/* Flag for whether we want all the "from_tty" gubbish printed.  */
9019370Spst
9198948Sobrienint caution = 1;		/* Default is yes, sigh. */
9219370Spst
9319370Spst/* stdio stream that command input is being read from.  Set to stdin normally.
9419370Spst   Set by source_command to the file we are sourcing.  Set to NULL if we are
9546283Sdfr   executing a user-defined command or interacting via a GUI.  */
9619370Spst
9719370SpstFILE *instream;
9819370Spst
9919370Spst/* Current working directory.  */
10019370Spst
10119370Spstchar *current_directory;
10219370Spst
10319370Spst/* The directory name is actually stored here (usually).  */
10419370Spstchar gdb_dirbuf[1024];
10519370Spst
10619370Spst/* Function to call before reading a command, if nonzero.
10719370Spst   The function receives two args: an input stream,
10819370Spst   and a prompt string.  */
10919370Spst
11098948Sobrienvoid (*window_hook) (FILE *, char *);
11119370Spst
11219370Spstint epoch_interface;
11319370Spstint xgdb_verbose;
11419370Spst
11519370Spst/* gdb prints this when reading a command interactively */
11698948Sobrienstatic char *gdb_prompt_string;	/* the global prompt string */
11719370Spst
11819370Spst/* Buffer used for reading command lines, and the size
11919370Spst   allocated for it so far.  */
12019370Spst
12119370Spstchar *line;
12219370Spstint linesize = 100;
12319370Spst
12419370Spst/* Nonzero if the current command is modified by "server ".  This
12598948Sobrien   affects things like recording into the command history, commands
12619370Spst   repeating on RETURN, etc.  This is so a user interface (emacs, GUI,
12719370Spst   whatever) can issue its own commands and also send along commands
12819370Spst   from the user, and have the user not notice that the user interface
12919370Spst   is issuing commands too.  */
13019370Spstint server_command;
13119370Spst
13219370Spst/* Baud rate specified for talking to serial target systems.  Default
13319370Spst   is left as -1, so targets can choose their own defaults.  */
13419370Spst/* FIXME: This means that "show remotebaud" and gr_files_info can print -1
13519370Spst   or (unsigned int)-1.  This is a Bad User Interface.  */
13619370Spst
13719370Spstint baud_rate = -1;
13819370Spst
13946283Sdfr/* Timeout limit for response from target. */
14046283Sdfr
14198948Sobrien/* The default value has been changed many times over the years.  It
14298948Sobrien   was originally 5 seconds.  But that was thought to be a long time
14398948Sobrien   to sit and wait, so it was changed to 2 seconds.  That was thought
14498948Sobrien   to be plenty unless the connection was going through some terminal
14598948Sobrien   server or multiplexer or other form of hairy serial connection.
14646283Sdfr
14798948Sobrien   In mid-1996, remote_timeout was moved from remote.c to top.c and
14898948Sobrien   it began being used in other remote-* targets.  It appears that the
14998948Sobrien   default was changed to 20 seconds at that time, perhaps because the
150130809Smarcel   Renesas E7000 ICE didn't always respond in a timely manner.
15198948Sobrien
15298948Sobrien   But if 5 seconds is a long time to sit and wait for retransmissions,
15398948Sobrien   20 seconds is far worse.  This demonstrates the difficulty of using
15498948Sobrien   a single variable for all protocol timeouts.
15598948Sobrien
15698948Sobrien   As remote.c is used much more than remote-e7000.c, it was changed
15798948Sobrien   back to 2 seconds in 1999. */
15898948Sobrien
15998948Sobrienint remote_timeout = 2;
16098948Sobrien
16119370Spst/* Non-zero tells remote* modules to output debugging info.  */
16219370Spst
16319370Spstint remote_debug = 0;
16419370Spst
16598948Sobrien/* Non-zero means the target is running. Note: this is different from
16698948Sobrien   saying that there is an active target and we are stopped at a
16798948Sobrien   breakpoint, for instance. This is a real indicator whether the
16898948Sobrien   target is off and running, which gdb is doing something else. */
16998948Sobrienint target_executing = 0;
17098948Sobrien
17119370Spst/* Level of control structure.  */
17219370Spststatic int control_level;
17319370Spst
174130809Smarcel/* Sbrk location on entry to main.  Used for statistics only.  */
175130809Smarcel#ifdef HAVE_SBRK
176130809Smarcelchar *lim_at_start;
177130809Smarcel#endif
178130809Smarcel
17919370Spst/* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT.  */
18019370Spst
18119370Spst#ifndef STOP_SIGNAL
18219370Spst#ifdef SIGTSTP
18319370Spst#define STOP_SIGNAL SIGTSTP
18498948Sobrienstatic void stop_sig (int);
18519370Spst#endif
18619370Spst#endif
18719370Spst
18819370Spst/* Hooks for alternate command interfaces.  */
18919370Spst
19019370Spst/* Called after most modules have been initialized, but before taking users
19198948Sobrien   command file.
19219370Spst
19398948Sobrien   If the UI fails to initialize and it wants GDB to continue
19498948Sobrien   using the default UI, then it should clear this hook before returning. */
19519370Spst
19698948Sobrienvoid (*init_ui_hook) (char *argv0);
19719370Spst
19898948Sobrien/* This hook is called from within gdb's many mini-event loops which could
19998948Sobrien   steal control from a real user interface's event loop. It returns
20098948Sobrien   non-zero if the user is requesting a detach, zero otherwise. */
20119370Spst
20298948Sobrienint (*ui_loop_hook) (int);
20319370Spst
20498948Sobrien/* Called instead of command_loop at top level.  Can be invoked via
20598948Sobrien   throw_exception().  */
20619370Spst
20798948Sobrienvoid (*command_loop_hook) (void);
20819370Spst
20919370Spst
21019370Spst/* Called from print_frame_info to list the line we stopped in.  */
21119370Spst
21298948Sobrienvoid (*print_frame_info_listing_hook) (struct symtab * s, int line,
21398948Sobrien				       int stopline, int noerror);
21419370Spst/* Replaces most of query.  */
21519370Spst
21698948Sobrienint (*query_hook) (const char *, va_list);
21719370Spst
21846283Sdfr/* Replaces most of warning.  */
21946283Sdfr
22098948Sobrienvoid (*warning_hook) (const char *, va_list);
22146283Sdfr
22246283Sdfr/* These three functions support getting lines of text from the user.  They
22346283Sdfr   are used in sequence.  First readline_begin_hook is called with a text
22446283Sdfr   string that might be (for example) a message for the user to type in a
22546283Sdfr   sequence of commands to be executed at a breakpoint.  If this function
22646283Sdfr   calls back to a GUI, it might take this opportunity to pop up a text
22746283Sdfr   interaction window with this message.  Next, readline_hook is called
22846283Sdfr   with a prompt that is emitted prior to collecting the user input.
22946283Sdfr   It can be called multiple times.  Finally, readline_end_hook is called
23046283Sdfr   to notify the GUI that we are done with the interaction window and it
23146283Sdfr   can close it. */
23246283Sdfr
23398948Sobrienvoid (*readline_begin_hook) (char *, ...);
23498948Sobrienchar *(*readline_hook) (char *);
23598948Sobrienvoid (*readline_end_hook) (void);
23646283Sdfr
23719370Spst/* Called as appropriate to notify the interface of the specified breakpoint
23819370Spst   conditions.  */
23919370Spst
24098948Sobrienvoid (*create_breakpoint_hook) (struct breakpoint * bpt);
24198948Sobrienvoid (*delete_breakpoint_hook) (struct breakpoint * bpt);
24298948Sobrienvoid (*modify_breakpoint_hook) (struct breakpoint * bpt);
24319370Spst
24498948Sobrien/* Called as appropriate to notify the interface that we have attached
24598948Sobrien   to or detached from an already running process. */
24698948Sobrien
24798948Sobrienvoid (*attach_hook) (void);
24898948Sobrienvoid (*detach_hook) (void);
24998948Sobrien
25019370Spst/* Called during long calculations to allow GUI to repair window damage, and to
25119370Spst   check for stop buttons, etc... */
25219370Spst
25398948Sobrienvoid (*interactive_hook) (void);
25419370Spst
25519370Spst/* Called when the registers have changed, as a hint to a GUI
25619370Spst   to minimize window update. */
25719370Spst
25898948Sobrienvoid (*registers_changed_hook) (void);
25919370Spst
26046283Sdfr/* Tell the GUI someone changed the register REGNO. -1 means
26146283Sdfr   that the caller does not know which register changed or
26298948Sobrien   that several registers have changed (see value_assign). */
26398948Sobrienvoid (*register_changed_hook) (int regno);
26446283Sdfr
26546283Sdfr/* Tell the GUI someone changed LEN bytes of memory at ADDR */
26698948Sobrienvoid (*memory_changed_hook) (CORE_ADDR addr, int len);
26746283Sdfr
26819370Spst/* Called when going to wait for the target.  Usually allows the GUI to run
26919370Spst   while waiting for target events.  */
27019370Spst
27198948Sobrienptid_t (*target_wait_hook) (ptid_t ptid,
27298948Sobrien                            struct target_waitstatus * status);
27319370Spst
27419370Spst/* Used by UI as a wrapper around command execution.  May do various things
27519370Spst   like enabling/disabling buttons, etc...  */
27619370Spst
27798948Sobrienvoid (*call_command_hook) (struct cmd_list_element * c, char *cmd,
27898948Sobrien			   int from_tty);
27919370Spst
28098948Sobrien/* Called after a `set' command has finished.  Is only run if the
28198948Sobrien   `set' command succeeded.  */
28298948Sobrien
28398948Sobrienvoid (*set_hook) (struct cmd_list_element * c);
28498948Sobrien
28546283Sdfr/* Called when the current thread changes.  Argument is thread id.  */
28619370Spst
28798948Sobrienvoid (*context_hook) (int id);
28846283Sdfr
28919370Spst/* Takes control from error ().  Typically used to prevent longjmps out of the
29019370Spst   middle of the GUI.  Usually used in conjunction with a catch routine.  */
29119370Spst
29298948SobrienNORETURN void (*error_hook) (void) ATTR_NORETURN;
29319370Spst
29419370Spst
29598948Sobrien/* One should use catch_errors rather than manipulating these
29698948Sobrien   directly.  */
29798948Sobrien#if defined(HAVE_SIGSETJMP)
29898948Sobrien#define SIGJMP_BUF		sigjmp_buf
29998948Sobrien#define SIGSETJMP(buf)		sigsetjmp((buf), 1)
30098948Sobrien#define SIGLONGJMP(buf,val)	siglongjmp((buf), (val))
30198948Sobrien#else
30298948Sobrien#define SIGJMP_BUF		jmp_buf
30398948Sobrien#define SIGSETJMP(buf)		setjmp(buf)
30498948Sobrien#define SIGLONGJMP(buf,val)	longjmp((buf), (val))
30598948Sobrien#endif
30619370Spst
30798948Sobrien/* Where to go for throw_exception().  */
30898948Sobrienstatic SIGJMP_BUF *catch_return;
30998948Sobrien
31098948Sobrien/* Return for reason REASON to the nearest containing catch_errors().  */
31198948Sobrien
31219370SpstNORETURN void
31398948Sobrienthrow_exception (enum return_reason reason)
31419370Spst{
31519370Spst  quit_flag = 0;
31619370Spst  immediate_quit = 0;
31719370Spst
31819370Spst  /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
31919370Spst     I can think of a reason why that is vital, though).  */
32098948Sobrien  bpstat_clear_actions (stop_bpstat);	/* Clear queued breakpoint commands */
32119370Spst
32219370Spst  disable_current_display ();
32319370Spst  do_cleanups (ALL_CLEANUPS);
32498948Sobrien  if (event_loop_p && target_can_async_p () && !target_executing)
32598948Sobrien    do_exec_cleanups (ALL_CLEANUPS);
32698948Sobrien  if (event_loop_p && sync_execution)
32798948Sobrien    do_exec_error_cleanups (ALL_CLEANUPS);
32819370Spst
32919370Spst  if (annotation_level > 1)
33019370Spst    switch (reason)
33119370Spst      {
33219370Spst      case RETURN_QUIT:
33319370Spst	annotate_quit ();
33419370Spst	break;
33519370Spst      case RETURN_ERROR:
33619370Spst	annotate_error ();
33719370Spst	break;
33819370Spst      }
33919370Spst
34098948Sobrien  /* Jump to the containing catch_errors() call, communicating REASON
34198948Sobrien     to that call via setjmp's return value.  Note that REASON can't
34298948Sobrien     be zero, by definition in defs.h. */
34398948Sobrien
34498948Sobrien  (NORETURN void) SIGLONGJMP (*catch_return, (int) reason);
34519370Spst}
34619370Spst
34798948Sobrien/* Call FUNC() with args FUNC_UIOUT and FUNC_ARGS, catching any
34898948Sobrien   errors.  Set FUNC_CAUGHT to an ``enum return_reason'' if the
34998948Sobrien   function is aborted (using throw_exception() or zero if the
35098948Sobrien   function returns normally.  Set FUNC_VAL to the value returned by
35198948Sobrien   the function or 0 if the function was aborted.
35219370Spst
35319370Spst   Must not be called with immediate_quit in effect (bad things might
35419370Spst   happen, say we got a signal in the middle of a memcpy to quit_return).
35519370Spst   This is an OK restriction; with very few exceptions immediate_quit can
35619370Spst   be replaced by judicious use of QUIT.
35719370Spst
35819370Spst   MASK specifies what to catch; it is normally set to
35919370Spst   RETURN_MASK_ALL, if for no other reason than that the code which
36019370Spst   calls catch_errors might not be set up to deal with a quit which
36119370Spst   isn't caught.  But if the code can deal with it, it generally
36219370Spst   should be RETURN_MASK_ERROR, unless for some reason it is more
36319370Spst   useful to abort only the portion of the operation inside the
36419370Spst   catch_errors.  Note that quit should return to the command line
36519370Spst   fairly quickly, even if some further processing is being done.  */
36619370Spst
36798948Sobrien/* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
36898948Sobrien   error() et.al. could maintain a set of flags that indicate the the
36998948Sobrien   current state of each of the longjmp buffers.  This would give the
37098948Sobrien   longjmp code the chance to detect a longjmp botch (before it gets
37198948Sobrien   to longjmperror()).  Prior to 1999-11-05 this wasn't possible as
37298948Sobrien   code also randomly used a SET_TOP_LEVEL macro that directly
37398948Sobrien   initialize the longjmp buffers. */
37498948Sobrien
37598948Sobrien/* MAYBE: cagney/1999-11-05: Should the catch_errors and cleanups code
37698948Sobrien   be consolidated into a single file instead of being distributed
37798948Sobrien   between utils.c and top.c? */
37898948Sobrien
37998948Sobrienstatic void
38098948Sobriencatcher (catch_exceptions_ftype *func,
38198948Sobrien	 struct ui_out *func_uiout,
38298948Sobrien	 void *func_args,
38398948Sobrien	 int *func_val,
38498948Sobrien	 enum return_reason *func_caught,
38598948Sobrien	 char *errstring,
386130809Smarcel	 char **gdberrmsg,
38798948Sobrien	 return_mask mask)
38819370Spst{
38998948Sobrien  SIGJMP_BUF *saved_catch;
39098948Sobrien  SIGJMP_BUF catch;
39119370Spst  struct cleanup *saved_cleanup_chain;
39219370Spst  char *saved_error_pre_print;
39319370Spst  char *saved_quit_pre_print;
39498948Sobrien  struct ui_out *saved_uiout;
39519370Spst
39698948Sobrien  /* Return value from SIGSETJMP(): enum return_reason if error or
39798948Sobrien     quit caught, 0 otherwise. */
39898948Sobrien  int caught;
39919370Spst
40098948Sobrien  /* Return value from FUNC(): Hopefully non-zero. Explicitly set to
40198948Sobrien     zero if an error quit was caught.  */
40298948Sobrien  int val;
40319370Spst
40498948Sobrien  /* Override error/quit messages during FUNC. */
40519370Spst
40698948Sobrien  saved_error_pre_print = error_pre_print;
40798948Sobrien  saved_quit_pre_print = quit_pre_print;
40819370Spst
40919370Spst  if (mask & RETURN_MASK_ERROR)
41098948Sobrien    error_pre_print = errstring;
41119370Spst  if (mask & RETURN_MASK_QUIT)
41298948Sobrien    quit_pre_print = errstring;
41319370Spst
41498948Sobrien  /* Override the global ``struct ui_out'' builder.  */
41519370Spst
41698948Sobrien  saved_uiout = uiout;
41798948Sobrien  uiout = func_uiout;
41819370Spst
41998948Sobrien  /* Prevent error/quit during FUNC from calling cleanups established
42098948Sobrien     prior to here. */
42119370Spst
42298948Sobrien  saved_cleanup_chain = save_cleanups ();
42319370Spst
42498948Sobrien  /* Call FUNC, catching error/quit events. */
42519370Spst
42698948Sobrien  saved_catch = catch_return;
42798948Sobrien  catch_return = &catch;
42898948Sobrien  caught = SIGSETJMP (catch);
42998948Sobrien  if (!caught)
43098948Sobrien    val = (*func) (func_uiout, func_args);
43198948Sobrien  else
432130809Smarcel    {
433130809Smarcel      val = 0;
434130809Smarcel      /* If caller wants a copy of the low-level error message, make one.
435130809Smarcel         This is used in the case of a silent error whereby the caller
436130809Smarcel         may optionally want to issue the message.  */
437130809Smarcel      if (gdberrmsg)
438130809Smarcel	*gdberrmsg = error_last_message ();
439130809Smarcel    }
44098948Sobrien  catch_return = saved_catch;
44119370Spst
44298948Sobrien  /* FIXME: cagney/1999-11-05: A correct FUNC implementation will
44398948Sobrien     clean things up (restoring the cleanup chain) to the state they
44498948Sobrien     were just prior to the call.  Unfortunately, many FUNC's are not
44598948Sobrien     that well behaved.  This could be fixed by adding either a
44698948Sobrien     do_cleanups call (to cover the problem) or an assertion check to
44798948Sobrien     detect bad FUNCs code. */
44819370Spst
44998948Sobrien  /* Restore the cleanup chain, the error/quit messages, and the uiout
45098948Sobrien     builder, to their original states. */
45119370Spst
45298948Sobrien  restore_cleanups (saved_cleanup_chain);
45319370Spst
45498948Sobrien  uiout = saved_uiout;
45519370Spst
45698948Sobrien  if (mask & RETURN_MASK_QUIT)
45798948Sobrien    quit_pre_print = saved_quit_pre_print;
45898948Sobrien  if (mask & RETURN_MASK_ERROR)
45998948Sobrien    error_pre_print = saved_error_pre_print;
46019370Spst
46198948Sobrien  /* Return normally if no error/quit event occurred or this catcher
46298948Sobrien     can handle this exception.  The caller analyses the func return
46398948Sobrien     values.  */
46419370Spst
46598948Sobrien  if (!caught || (mask & RETURN_MASK (caught)))
46698948Sobrien    {
46798948Sobrien      *func_val = val;
46898948Sobrien      *func_caught = caught;
46998948Sobrien      return;
47098948Sobrien    }
47119370Spst
47298948Sobrien  /* The caller didn't request that the event be caught, relay the
47398948Sobrien     event to the next containing catch_errors(). */
47419370Spst
47598948Sobrien  throw_exception (caught);
47619370Spst}
47719370Spst
47898948Sobrienint
47998948Sobriencatch_exceptions (struct ui_out *uiout,
48098948Sobrien		  catch_exceptions_ftype *func,
48198948Sobrien		  void *func_args,
48298948Sobrien		  char *errstring,
48398948Sobrien		  return_mask mask)
48419370Spst{
48598948Sobrien  int val;
48698948Sobrien  enum return_reason caught;
487130809Smarcel  catcher (func, uiout, func_args, &val, &caught, errstring, NULL, mask);
48898948Sobrien  gdb_assert (val >= 0);
48998948Sobrien  gdb_assert (caught <= 0);
49098948Sobrien  if (caught < 0)
49198948Sobrien    return caught;
49298948Sobrien  return val;
49319370Spst}
49419370Spst
495130809Smarcelint
496130809Smarcelcatch_exceptions_with_msg (struct ui_out *uiout,
497130809Smarcel		  	   catch_exceptions_ftype *func,
498130809Smarcel		  	   void *func_args,
499130809Smarcel		  	   char *errstring,
500130809Smarcel			   char **gdberrmsg,
501130809Smarcel		  	   return_mask mask)
502130809Smarcel{
503130809Smarcel  int val;
504130809Smarcel  enum return_reason caught;
505130809Smarcel  catcher (func, uiout, func_args, &val, &caught, errstring, gdberrmsg, mask);
506130809Smarcel  gdb_assert (val >= 0);
507130809Smarcel  gdb_assert (caught <= 0);
508130809Smarcel  if (caught < 0)
509130809Smarcel    return caught;
510130809Smarcel  return val;
511130809Smarcel}
512130809Smarcel
51398948Sobrienstruct catch_errors_args
51498948Sobrien{
51598948Sobrien  catch_errors_ftype *func;
51698948Sobrien  void *func_args;
51798948Sobrien};
51819370Spst
519130809Smarcelstatic int
52098948Sobriendo_catch_errors (struct ui_out *uiout, void *data)
52119370Spst{
52298948Sobrien  struct catch_errors_args *args = data;
52398948Sobrien  return args->func (args->func_args);
52419370Spst}
52519370Spst
52698948Sobrienint
52798948Sobriencatch_errors (catch_errors_ftype *func, void *func_args, char *errstring,
52898948Sobrien	      return_mask mask)
52919370Spst{
53098948Sobrien  int val;
53198948Sobrien  enum return_reason caught;
53298948Sobrien  struct catch_errors_args args;
53398948Sobrien  args.func = func;
53498948Sobrien  args.func_args = func_args;
535130809Smarcel  catcher (do_catch_errors, uiout, &args, &val, &caught, errstring,
536130809Smarcel	   NULL, mask);
53798948Sobrien  if (caught != 0)
53898948Sobrien    return 0;
53998948Sobrien  return val;
54019370Spst}
54119370Spst
54298948Sobrienstruct captured_command_args
54398948Sobrien  {
54498948Sobrien    catch_command_errors_ftype *command;
54598948Sobrien    char *arg;
54698948Sobrien    int from_tty;
54798948Sobrien  };
54819370Spst
54998948Sobrienstatic int
55098948Sobriendo_captured_command (void *data)
55119370Spst{
55298948Sobrien  struct captured_command_args *context = data;
55398948Sobrien  context->command (context->arg, context->from_tty);
55498948Sobrien  /* FIXME: cagney/1999-11-07: Technically this do_cleanups() call
55598948Sobrien     isn't needed.  Instead an assertion check could be made that
55698948Sobrien     simply confirmed that the called function correctly cleaned up
55798948Sobrien     after itself.  Unfortunately, old code (prior to 1999-11-04) in
55898948Sobrien     main.c was calling SET_TOP_LEVEL(), calling the command function,
55998948Sobrien     and then *always* calling do_cleanups().  For the moment we
56098948Sobrien     remain ``bug compatible'' with that old code..  */
56198948Sobrien  do_cleanups (ALL_CLEANUPS);
56298948Sobrien  return 1;
56319370Spst}
56419370Spst
56598948Sobrienint
56698948Sobriencatch_command_errors (catch_command_errors_ftype * command,
56798948Sobrien		      char *arg, int from_tty, return_mask mask)
56819370Spst{
56998948Sobrien  struct captured_command_args args;
57098948Sobrien  args.command = command;
57198948Sobrien  args.arg = arg;
57298948Sobrien  args.from_tty = from_tty;
57398948Sobrien  return catch_errors (do_captured_command, &args, "", mask);
57498948Sobrien}
57519370Spst
57619370Spst
57798948Sobrien/* Handler for SIGHUP.  */
57819370Spst
57998948Sobrien#ifdef SIGHUP
58098948Sobrien/* Just a little helper function for disconnect().  */
58119370Spst
58298948Sobrien/* NOTE 1999-04-29: This function will be static again, once we modify
58398948Sobrien   gdb to use the event loop as the default command loop and we merge
58498948Sobrien   event-top.c into this file, top.c */
58598948Sobrien/* static */ int
58698948Sobrienquit_cover (void *s)
58719370Spst{
58898948Sobrien  caution = 0;			/* Throw caution to the wind -- we're exiting.
58998948Sobrien				   This prevents asking the user dumb questions.  */
59098948Sobrien  quit_command ((char *) 0, 0);
59198948Sobrien  return 0;
59219370Spst}
59319370Spst
59419370Spststatic void
59598948Sobriendisconnect (int signo)
59619370Spst{
59798948Sobrien  catch_errors (quit_cover, NULL,
59898948Sobrien	      "Could not kill the program being debugged", RETURN_MASK_ALL);
59998948Sobrien  signal (SIGHUP, SIG_DFL);
60098948Sobrien  kill (getpid (), SIGHUP);
60119370Spst}
60298948Sobrien#endif /* defined SIGHUP */
60398948Sobrien
60498948Sobrien/* Line number we are currently in in a file which is being sourced.  */
60598948Sobrien/* NOTE 1999-04-29: This variable will be static again, once we modify
60698948Sobrien   gdb to use the event loop as the default command loop and we merge
60798948Sobrien   event-top.c into this file, top.c */
60898948Sobrien/* static */ int source_line_number;
60919370Spst
61098948Sobrien/* Name of the file we are sourcing.  */
61198948Sobrien/* NOTE 1999-04-29: This variable will be static again, once we modify
61298948Sobrien   gdb to use the event loop as the default command loop and we merge
61398948Sobrien   event-top.c into this file, top.c */
61498948Sobrien/* static */ char *source_file_name;
61519370Spst
61698948Sobrien/* Buffer containing the error_pre_print used by the source stuff.
61798948Sobrien   Malloc'd.  */
61898948Sobrien/* NOTE 1999-04-29: This variable will be static again, once we modify
61998948Sobrien   gdb to use the event loop as the default command loop and we merge
62098948Sobrien   event-top.c into this file, top.c */
62198948Sobrien/* static */ char *source_error;
62298948Sobrienstatic int source_error_allocated;
62319370Spst
62498948Sobrien/* Something to glom on to the start of error_pre_print if source_file_name
62598948Sobrien   is set.  */
62698948Sobrien/* NOTE 1999-04-29: This variable will be static again, once we modify
62798948Sobrien   gdb to use the event loop as the default command loop and we merge
62898948Sobrien   event-top.c into this file, top.c */
62998948Sobrien/* static */ char *source_pre_error;
63019370Spst
63198948Sobrien/* Clean up on error during a "source" command (or execution of a
63298948Sobrien   user-defined command).  */
63319370Spst
63498948Sobrienvoid
63598948Sobriendo_restore_instream_cleanup (void *stream)
63619370Spst{
63798948Sobrien  /* Restore the previous input stream.  */
63898948Sobrien  instream = stream;
63919370Spst}
64019370Spst
64198948Sobrien/* Read commands from STREAM.  */
64298948Sobrienvoid
64398948Sobrienread_command_file (FILE *stream)
64419370Spst{
64598948Sobrien  struct cleanup *cleanups;
64619370Spst
64798948Sobrien  cleanups = make_cleanup (do_restore_instream_cleanup, instream);
64898948Sobrien  instream = stream;
64998948Sobrien  command_loop ();
65098948Sobrien  do_cleanups (cleanups);
65119370Spst}
65298948Sobrien
65398948Sobrienvoid (*pre_init_ui_hook) (void);
65419370Spst
65598948Sobrien#ifdef __MSDOS__
65619370Spstvoid
65798948Sobriendo_chdir_cleanup (void *old_dir)
65819370Spst{
65998948Sobrien  chdir (old_dir);
66098948Sobrien  xfree (old_dir);
66119370Spst}
66298948Sobrien#endif
66319370Spst
66419370Spst/* Execute the line P as a command.
66519370Spst   Pass FROM_TTY as second argument to the defining function.  */
66619370Spst
66719370Spstvoid
66898948Sobrienexecute_command (char *p, int from_tty)
66919370Spst{
670130809Smarcel  struct cmd_list_element *c;
671130809Smarcel  enum language flang;
67219370Spst  static int warned = 0;
67398948Sobrien  char *line;
67498948Sobrien
67519370Spst  free_all_values ();
67619370Spst
67746283Sdfr  /* Force cleanup of any alloca areas if using C alloca instead of
67846283Sdfr     a builtin alloca.  */
67946283Sdfr  alloca (0);
68046283Sdfr
68119370Spst  /* This can happen when command_line_input hits end of file.  */
68219370Spst  if (p == NULL)
68398948Sobrien    return;
68419370Spst
68546283Sdfr  serial_log_command (p);
68619370Spst
68798948Sobrien  while (*p == ' ' || *p == '\t')
68898948Sobrien    p++;
68919370Spst  if (*p)
69019370Spst    {
69119370Spst      char *arg;
69298948Sobrien      line = p;
69319370Spst
69419370Spst      c = lookup_cmd (&p, cmdlist, "", 0, 1);
69598948Sobrien
69698948Sobrien      /* If the target is running, we allow only a limited set of
69798948Sobrien         commands. */
69898948Sobrien      if (event_loop_p && target_can_async_p () && target_executing)
699130809Smarcel	if (strcmp (c->name, "help") != 0
700130809Smarcel	    && strcmp (c->name, "pwd") != 0
701130809Smarcel	    && strcmp (c->name, "show") != 0
702130809Smarcel	    && strcmp (c->name, "stop") != 0)
70398948Sobrien	  error ("Cannot execute this command while the target is running.");
70498948Sobrien
70519370Spst      /* Pass null arg rather than an empty one.  */
70619370Spst      arg = *p ? p : 0;
70719370Spst
70898948Sobrien      /* FIXME: cagney/2002-02-02: The c->type test is pretty dodgy
70998948Sobrien         while the is_complete_command(cfunc) test is just plain
71098948Sobrien         bogus.  They should both be replaced by a test of the form
71198948Sobrien         c->strip_trailing_white_space_p.  */
71298948Sobrien      /* NOTE: cagney/2002-02-02: The function.cfunc in the below
71398948Sobrien         can't be replaced with func.  This is because it is the
71498948Sobrien         cfunc, and not the func, that has the value that the
71598948Sobrien         is_complete_command hack is testing for.  */
71698948Sobrien      /* Clear off trailing whitespace, except for set and complete
71798948Sobrien         command.  */
71898948Sobrien      if (arg
71998948Sobrien	  && c->type != set_cmd
72098948Sobrien	  && !is_complete_command (c))
72119370Spst	{
72219370Spst	  p = arg + strlen (arg) - 1;
72319370Spst	  while (p >= arg && (*p == ' ' || *p == '\t'))
72419370Spst	    p--;
72519370Spst	  *(p + 1) = '\0';
72619370Spst	}
72719370Spst
72898948Sobrien      /* If this command has been pre-hooked, run the hook first. */
729130809Smarcel      execute_cmd_pre_hook (c);
73019370Spst
73198948Sobrien      if (c->flags & DEPRECATED_WARN_USER)
73298948Sobrien	deprecated_cmd_warning (&line);
73398948Sobrien
73419370Spst      if (c->class == class_user)
73519370Spst	execute_user_command (c, arg);
73619370Spst      else if (c->type == set_cmd || c->type == show_cmd)
73719370Spst	do_setshow_command (arg, from_tty & caution, c);
738130809Smarcel      else if (!cmd_func_p (c))
73919370Spst	error ("That is not a command, just a help topic.");
74019370Spst      else if (call_command_hook)
74119370Spst	call_command_hook (c, arg, from_tty & caution);
74219370Spst      else
743130809Smarcel	cmd_func (c, arg, from_tty & caution);
74498948Sobrien
74598948Sobrien      /* If this command has been post-hooked, run the hook last. */
746130809Smarcel      execute_cmd_post_hook (c);
74719370Spst
74898948Sobrien    }
74998948Sobrien
75019370Spst  /* Tell the user if the language has changed (except first time).  */
75119370Spst  if (current_language != expected_language)
75298948Sobrien    {
75398948Sobrien      if (language_mode == language_mode_auto)
75498948Sobrien	{
75598948Sobrien	  language_info (1);	/* Print what changed.  */
75698948Sobrien	}
75798948Sobrien      warned = 0;
75819370Spst    }
75919370Spst
76019370Spst  /* Warn the user if the working language does not match the
76119370Spst     language of the current frame.  Only warn the user if we are
76219370Spst     actually running the program, i.e. there is a stack. */
76319370Spst  /* FIXME:  This should be cacheing the frame and only running when
76419370Spst     the frame changes.  */
76519370Spst
76619370Spst  if (target_has_stack)
76719370Spst    {
76819370Spst      flang = get_frame_language ();
76919370Spst      if (!warned
77019370Spst	  && flang != language_unknown
77119370Spst	  && flang != current_language->la_language)
77219370Spst	{
77319370Spst	  printf_filtered ("%s\n", lang_frame_mismatch_warn);
77419370Spst	  warned = 1;
77519370Spst	}
77619370Spst    }
77719370Spst}
77819370Spst
77919370Spst/* Read commands from `instream' and execute them
78019370Spst   until end of file or error reading instream.  */
78119370Spst
78219370Spstvoid
78398948Sobriencommand_loop (void)
78419370Spst{
78519370Spst  struct cleanup *old_chain;
78619370Spst  char *command;
78719370Spst  int stdin_is_tty = ISATTY (stdin);
78819370Spst  long time_at_cmd_start;
78919370Spst#ifdef HAVE_SBRK
79046283Sdfr  long space_at_cmd_start = 0;
79119370Spst#endif
79219370Spst  extern int display_time;
79319370Spst  extern int display_space;
79419370Spst
79546283Sdfr  while (instream && !feof (instream))
79619370Spst    {
79719370Spst      if (window_hook && instream == stdin)
79898948Sobrien	(*window_hook) (instream, get_prompt ());
79919370Spst
80019370Spst      quit_flag = 0;
80119370Spst      if (instream == stdin && stdin_is_tty)
80219370Spst	reinitialize_more_filter ();
80398948Sobrien      old_chain = make_cleanup (null_cleanup, 0);
80446283Sdfr
80546283Sdfr      /* Get a command-line. This calls the readline package. */
80698948Sobrien      command = command_line_input (instream == stdin ?
80798948Sobrien				    get_prompt () : (char *) NULL,
80819370Spst				    instream == stdin, "prompt");
80919370Spst      if (command == 0)
81019370Spst	return;
81119370Spst
81219370Spst      time_at_cmd_start = get_run_time ();
81319370Spst
81419370Spst      if (display_space)
81519370Spst	{
81619370Spst#ifdef HAVE_SBRK
81719370Spst	  char *lim = (char *) sbrk (0);
818130809Smarcel	  space_at_cmd_start = lim - lim_at_start;
81919370Spst#endif
82019370Spst	}
82119370Spst
82219370Spst      execute_command (command, instream == stdin);
82319370Spst      /* Do any commands attached to breakpoint we stopped at.  */
82419370Spst      bpstat_do_actions (&stop_bpstat);
82519370Spst      do_cleanups (old_chain);
82619370Spst
82719370Spst      if (display_time)
82819370Spst	{
82919370Spst	  long cmd_time = get_run_time () - time_at_cmd_start;
83019370Spst
83119370Spst	  printf_unfiltered ("Command execution time: %ld.%06ld\n",
83219370Spst			     cmd_time / 1000000, cmd_time % 1000000);
83319370Spst	}
83419370Spst
83519370Spst      if (display_space)
83619370Spst	{
83719370Spst#ifdef HAVE_SBRK
83819370Spst	  char *lim = (char *) sbrk (0);
839130809Smarcel	  long space_now = lim - lim_at_start;
84019370Spst	  long space_diff = space_now - space_at_cmd_start;
84119370Spst
84219370Spst	  printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
84319370Spst			     space_now,
84419370Spst			     (space_diff >= 0 ? '+' : '-'),
84519370Spst			     space_diff);
84619370Spst#endif
84719370Spst	}
84819370Spst    }
84919370Spst}
85098948Sobrien
85198948Sobrien/* Read commands from `instream' and execute them until end of file or
85298948Sobrien   error reading instream. This command loop doesnt care about any
85398948Sobrien   such things as displaying time and space usage. If the user asks
85498948Sobrien   for those, they won't work. */
85598948Sobrienvoid
85698948Sobriensimplified_command_loop (char *(*read_input_func) (char *),
85798948Sobrien			 void (*execute_command_func) (char *, int))
85898948Sobrien{
85998948Sobrien  struct cleanup *old_chain;
86098948Sobrien  char *command;
86198948Sobrien  int stdin_is_tty = ISATTY (stdin);
86298948Sobrien
86398948Sobrien  while (instream && !feof (instream))
86498948Sobrien    {
86598948Sobrien      quit_flag = 0;
86698948Sobrien      if (instream == stdin && stdin_is_tty)
86798948Sobrien	reinitialize_more_filter ();
86898948Sobrien      old_chain = make_cleanup (null_cleanup, 0);
86998948Sobrien
87098948Sobrien      /* Get a command-line. */
87198948Sobrien      command = (*read_input_func) (instream == stdin ?
87298948Sobrien				    get_prompt () : (char *) NULL);
87398948Sobrien
87498948Sobrien      if (command == 0)
87598948Sobrien	return;
87698948Sobrien
87798948Sobrien      (*execute_command_func) (command, instream == stdin);
87898948Sobrien
87998948Sobrien      /* Do any commands attached to breakpoint we stopped at.  */
88098948Sobrien      bpstat_do_actions (&stop_bpstat);
88198948Sobrien
88298948Sobrien      do_cleanups (old_chain);
88398948Sobrien    }
88498948Sobrien}
88519370Spst
88619370Spst/* Commands call this if they do not want to be repeated by null lines.  */
88719370Spst
88819370Spstvoid
88998948Sobriendont_repeat (void)
89019370Spst{
89119370Spst  if (server_command)
89219370Spst    return;
89319370Spst
89419370Spst  /* If we aren't reading from standard input, we are saving the last
89519370Spst     thing read from stdin in line and don't want to delete it.  Null lines
89619370Spst     won't repeat here in any case.  */
89719370Spst  if (instream == stdin)
89819370Spst    *line = 0;
89919370Spst}
90019370Spst
90119370Spst/* Read a line from the stream "instream" without command line editing.
90219370Spst
90398948Sobrien   It prints PROMPT_ARG once at the start.
90419370Spst   Action is compatible with "readline", e.g. space for the result is
90519370Spst   malloc'd and should be freed by the caller.
90619370Spst
90719370Spst   A NULL return means end of file.  */
90819370Spstchar *
90998948Sobriengdb_readline (char *prompt_arg)
91019370Spst{
91119370Spst  int c;
91219370Spst  char *result;
91319370Spst  int input_index = 0;
91419370Spst  int result_size = 80;
91519370Spst
91698948Sobrien  if (prompt_arg)
91719370Spst    {
91819370Spst      /* Don't use a _filtered function here.  It causes the assumed
91998948Sobrien         character position to be off, since the newline we read from
92098948Sobrien         the user is not accounted for.  */
92198948Sobrien      fputs_unfiltered (prompt_arg, gdb_stdout);
92219370Spst      gdb_flush (gdb_stdout);
92319370Spst    }
92419370Spst
92519370Spst  result = (char *) xmalloc (result_size);
92619370Spst
92719370Spst  while (1)
92819370Spst    {
92919370Spst      /* Read from stdin if we are executing a user defined command.
93098948Sobrien         This is the right thing for prompt_for_continue, at least.  */
93119370Spst      c = fgetc (instream ? instream : stdin);
93219370Spst
93319370Spst      if (c == EOF)
93419370Spst	{
93519370Spst	  if (input_index > 0)
93619370Spst	    /* The last line does not end with a newline.  Return it, and
93719370Spst	       if we are called again fgetc will still return EOF and
93819370Spst	       we'll return NULL then.  */
93919370Spst	    break;
94098948Sobrien	  xfree (result);
94119370Spst	  return NULL;
94219370Spst	}
94319370Spst
94419370Spst      if (c == '\n')
94546283Sdfr#ifndef CRLF_SOURCE_FILES
94619370Spst	break;
94746283Sdfr#else
94846283Sdfr	{
94946283Sdfr	  if (input_index > 0 && result[input_index - 1] == '\r')
95046283Sdfr	    input_index--;
95146283Sdfr	  break;
95246283Sdfr	}
95346283Sdfr#endif
95419370Spst
95519370Spst      result[input_index++] = c;
95619370Spst      while (input_index >= result_size)
95719370Spst	{
95819370Spst	  result_size *= 2;
95919370Spst	  result = (char *) xrealloc (result, result_size);
96019370Spst	}
96119370Spst    }
96219370Spst
96319370Spst  result[input_index++] = '\0';
96419370Spst  return result;
96519370Spst}
96619370Spst
96719370Spst/* Variables which control command line editing and history
96819370Spst   substitution.  These variables are given default values at the end
96919370Spst   of this file.  */
97019370Spststatic int command_editing_p;
97198948Sobrien/* NOTE 1999-04-29: This variable will be static again, once we modify
97298948Sobrien   gdb to use the event loop as the default command loop and we merge
97398948Sobrien   event-top.c into this file, top.c */
97498948Sobrien/* static */ int history_expansion_p;
97519370Spststatic int write_history_p;
97619370Spststatic int history_size;
97719370Spststatic char *history_filename;
97819370Spst
979130809Smarcel/* This is like readline(), but it has some gdb-specific behavior.
980130809Smarcel   gdb can use readline in both the synchronous and async modes during
981130809Smarcel   a single gdb invocation.  At the ordinary top-level prompt we might
982130809Smarcel   be using the async readline.  That means we can't use
983130809Smarcel   rl_pre_input_hook, since it doesn't work properly in async mode.
984130809Smarcel   However, for a secondary prompt (" >", such as occurs during a
985130809Smarcel   `define'), gdb just calls readline() directly, running it in
986130809Smarcel   synchronous mode.  So for operate-and-get-next to work in this
987130809Smarcel   situation, we have to switch the hooks around.  That is what
988130809Smarcel   gdb_readline_wrapper is for.  */
989130809Smarcelchar *
990130809Smarcelgdb_readline_wrapper (char *prompt)
991130809Smarcel{
992130809Smarcel  /* Set the hook that works in this case.  */
993130809Smarcel  if (event_loop_p && after_char_processing_hook)
994130809Smarcel    {
995130809Smarcel      rl_pre_input_hook = (Function *) after_char_processing_hook;
996130809Smarcel      after_char_processing_hook = NULL;
997130809Smarcel    }
998130809Smarcel
999130809Smarcel  return readline (prompt);
1000130809Smarcel}
1001130809Smarcel
100219370Spst
100319370Spst#ifdef STOP_SIGNAL
100419370Spststatic void
100598948Sobrienstop_sig (int signo)
100619370Spst{
100719370Spst#if STOP_SIGNAL == SIGTSTP
100819370Spst  signal (SIGTSTP, SIG_DFL);
100998948Sobrien#if HAVE_SIGPROCMASK
101098948Sobrien  {
101198948Sobrien    sigset_t zero;
101298948Sobrien
101398948Sobrien    sigemptyset (&zero);
101498948Sobrien    sigprocmask (SIG_SETMASK, &zero, 0);
101598948Sobrien  }
101698948Sobrien#elif HAVE_SIGSETMASK
101719370Spst  sigsetmask (0);
101898948Sobrien#endif
101919370Spst  kill (getpid (), SIGTSTP);
102019370Spst  signal (SIGTSTP, stop_sig);
102119370Spst#else
102219370Spst  signal (STOP_SIGNAL, stop_sig);
102319370Spst#endif
102498948Sobrien  printf_unfiltered ("%s", get_prompt ());
102519370Spst  gdb_flush (gdb_stdout);
102619370Spst
102719370Spst  /* Forget about any previous command -- null line now will do nothing.  */
102819370Spst  dont_repeat ();
102919370Spst}
103019370Spst#endif /* STOP_SIGNAL */
103119370Spst
103219370Spst/* Initialize signal handlers. */
103319370Spststatic void
103498948Sobrienfloat_handler (int signo)
103519370Spst{
103698948Sobrien  /* This message is based on ANSI C, section 4.7.  Note that integer
103798948Sobrien     divide by zero causes this, so "float" is a misnomer.  */
103898948Sobrien  signal (SIGFPE, float_handler);
103998948Sobrien  error ("Erroneous arithmetic operation.");
104098948Sobrien}
104198948Sobrien
104298948Sobrienstatic void
104398948Sobriendo_nothing (int signo)
104498948Sobrien{
104546283Sdfr  /* Under System V the default disposition of a signal is reinstated after
104646283Sdfr     the signal is caught and delivered to an application process.  On such
104746283Sdfr     systems one must restore the replacement signal handler if one wishes
104846283Sdfr     to continue handling the signal in one's program.  On BSD systems this
104946283Sdfr     is not needed but it is harmless, and it simplifies the code to just do
105046283Sdfr     it unconditionally. */
105146283Sdfr  signal (signo, do_nothing);
105219370Spst}
105319370Spst
105419370Spststatic void
105598948Sobrieninit_signals (void)
105619370Spst{
105719370Spst  signal (SIGINT, request_quit);
105819370Spst
105919370Spst  /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
106019370Spst     to the inferior and breakpoints will be ignored.  */
106119370Spst#ifdef SIGTRAP
106219370Spst  signal (SIGTRAP, SIG_DFL);
106319370Spst#endif
106419370Spst
106519370Spst  /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
106619370Spst     passed to the inferior, which we don't want.  It would be
106719370Spst     possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
106819370Spst     on BSD4.3 systems using vfork, that can affect the
106919370Spst     GDB process as well as the inferior (the signal handling tables
107019370Spst     might be in memory, shared between the two).  Since we establish
107119370Spst     a handler for SIGQUIT, when we call exec it will set the signal
107219370Spst     to SIG_DFL for us.  */
107319370Spst  signal (SIGQUIT, do_nothing);
107446283Sdfr#ifdef SIGHUP
107519370Spst  if (signal (SIGHUP, do_nothing) != SIG_IGN)
107619370Spst    signal (SIGHUP, disconnect);
107746283Sdfr#endif
107819370Spst  signal (SIGFPE, float_handler);
107919370Spst
108019370Spst#if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
108119370Spst  signal (SIGWINCH, SIGWINCH_HANDLER);
108219370Spst#endif
108319370Spst}
108419370Spst
108598948Sobrien/* The current saved history number from operate-and-get-next.
108698948Sobrien   This is -1 if not valid.  */
108798948Sobrienstatic int operate_saved_history = -1;
108898948Sobrien
108998948Sobrien/* This is put on the appropriate hook and helps operate-and-get-next
109098948Sobrien   do its work.  */
1091130809Smarcelstatic void
1092130809Smarcelgdb_rl_operate_and_get_next_completion (void)
109398948Sobrien{
109498948Sobrien  int delta = where_history () - operate_saved_history;
109598948Sobrien  /* The `key' argument to rl_get_previous_history is ignored.  */
109698948Sobrien  rl_get_previous_history (delta, 0);
109798948Sobrien  operate_saved_history = -1;
109898948Sobrien
109998948Sobrien  /* readline doesn't automatically update the display for us.  */
110098948Sobrien  rl_redisplay ();
110198948Sobrien
110298948Sobrien  after_char_processing_hook = NULL;
110398948Sobrien  rl_pre_input_hook = NULL;
110498948Sobrien}
110598948Sobrien
110698948Sobrien/* This is a gdb-local readline command handler.  It accepts the
110798948Sobrien   current command line (like RET does) and, if this command was taken
110898948Sobrien   from the history, arranges for the next command in the history to
110998948Sobrien   appear on the command line when the prompt returns.
111098948Sobrien   We ignore the arguments.  */
111198948Sobrienstatic int
111298948Sobriengdb_rl_operate_and_get_next (int count, int key)
111398948Sobrien{
1114130809Smarcel  int where;
1115130809Smarcel
111698948Sobrien  if (event_loop_p)
111798948Sobrien    {
111898948Sobrien      /* Use the async hook.  */
111998948Sobrien      after_char_processing_hook = gdb_rl_operate_and_get_next_completion;
112098948Sobrien    }
112198948Sobrien  else
112298948Sobrien    {
112398948Sobrien      /* This hook only works correctly when we are using the
112498948Sobrien	 synchronous readline.  */
112598948Sobrien      rl_pre_input_hook = (Function *) gdb_rl_operate_and_get_next_completion;
112698948Sobrien    }
112798948Sobrien
1128130809Smarcel  /* Find the current line, and find the next line to use.  */
1129130809Smarcel  where = where_history();
1130130809Smarcel
1131130809Smarcel  /* FIXME: kettenis/20020817: max_input_history is renamed into
1132130809Smarcel     history_max_entries in readline-4.2.  When we do a new readline
1133130809Smarcel     import, we should probably change it here too, even though
1134130809Smarcel     readline maintains backwards compatibility for now by still
1135130809Smarcel     defining max_input_history.  */
1136130809Smarcel  if ((history_is_stifled () && (history_length >= max_input_history)) ||
1137130809Smarcel      (where >= history_length - 1))
1138130809Smarcel    operate_saved_history = where;
1139130809Smarcel  else
1140130809Smarcel    operate_saved_history = where + 1;
1141130809Smarcel
114298948Sobrien  return rl_newline (1, key);
114398948Sobrien}
114498948Sobrien
114519370Spst/* Read one line from the command input stream `instream'
114619370Spst   into the local static buffer `linebuffer' (whose current length
114719370Spst   is `linelength').
114819370Spst   The buffer is made bigger as necessary.
114919370Spst   Returns the address of the start of the line.
115019370Spst
115119370Spst   NULL is returned for end of file.
115219370Spst
115319370Spst   *If* the instream == stdin & stdin is a terminal, the line read
115419370Spst   is copied into the file line saver (global var char *line,
115519370Spst   length linesize) so that it can be duplicated.
115619370Spst
115719370Spst   This routine either uses fancy command line editing or
115819370Spst   simple input as the user has requested.  */
115919370Spst
116019370Spstchar *
116198948Sobriencommand_line_input (char *prompt_arg, int repeat, char *annotation_suffix)
116219370Spst{
116319370Spst  static char *linebuffer = 0;
116419370Spst  static unsigned linelength = 0;
1165130809Smarcel  char *p;
116619370Spst  char *p1;
116719370Spst  char *rl;
116898948Sobrien  char *local_prompt = prompt_arg;
116919370Spst  char *nline;
117019370Spst  char got_eof = 0;
117119370Spst
117219370Spst  /* The annotation suffix must be non-NULL.  */
117319370Spst  if (annotation_suffix == NULL)
117419370Spst    annotation_suffix = "";
117519370Spst
117619370Spst  if (annotation_level > 1 && instream == stdin)
117719370Spst    {
117898948Sobrien      local_prompt = alloca ((prompt_arg == NULL ? 0 : strlen (prompt_arg))
117919370Spst			     + strlen (annotation_suffix) + 40);
118098948Sobrien      if (prompt_arg == NULL)
118119370Spst	local_prompt[0] = '\0';
118219370Spst      else
118398948Sobrien	strcpy (local_prompt, prompt_arg);
118419370Spst      strcat (local_prompt, "\n\032\032");
118519370Spst      strcat (local_prompt, annotation_suffix);
118619370Spst      strcat (local_prompt, "\n");
118719370Spst    }
118819370Spst
118919370Spst  if (linebuffer == 0)
119019370Spst    {
119119370Spst      linelength = 80;
119219370Spst      linebuffer = (char *) xmalloc (linelength);
119319370Spst    }
119419370Spst
119519370Spst  p = linebuffer;
119619370Spst
119719370Spst  /* Control-C quits instantly if typed while in this loop
119819370Spst     since it should not wait until the user types a newline.  */
119919370Spst  immediate_quit++;
120019370Spst#ifdef STOP_SIGNAL
120119370Spst  if (job_control)
120298948Sobrien    {
120398948Sobrien      if (event_loop_p)
120498948Sobrien	signal (STOP_SIGNAL, handle_stop_sig);
120598948Sobrien      else
120698948Sobrien	signal (STOP_SIGNAL, stop_sig);
120798948Sobrien    }
120819370Spst#endif
120919370Spst
121019370Spst  while (1)
121119370Spst    {
121219370Spst      /* Make sure that all output has been output.  Some machines may let
121398948Sobrien         you get away with leaving out some of the gdb_flush, but not all.  */
121419370Spst      wrap_here ("");
121519370Spst      gdb_flush (gdb_stdout);
121619370Spst      gdb_flush (gdb_stderr);
121719370Spst
121819370Spst      if (source_file_name != NULL)
121919370Spst	{
122019370Spst	  ++source_line_number;
122119370Spst	  sprintf (source_error,
122219370Spst		   "%s%s:%d: Error in sourced command file:\n",
122319370Spst		   source_pre_error,
122419370Spst		   source_file_name,
122519370Spst		   source_line_number);
122619370Spst	  error_pre_print = source_error;
122719370Spst	}
122819370Spst
122919370Spst      if (annotation_level > 1 && instream == stdin)
123019370Spst	{
1231130809Smarcel	  puts_unfiltered ("\n\032\032pre-");
1232130809Smarcel	  puts_unfiltered (annotation_suffix);
1233130809Smarcel	  puts_unfiltered ("\n");
123419370Spst	}
123519370Spst
123619370Spst      /* Don't use fancy stuff if not talking to stdin.  */
123746283Sdfr      if (readline_hook && instream == NULL)
123846283Sdfr	{
123946283Sdfr	  rl = (*readline_hook) (local_prompt);
124046283Sdfr	}
124146283Sdfr      else if (command_editing_p && instream == stdin && ISATTY (instream))
124246283Sdfr	{
1243130809Smarcel	  rl = gdb_readline_wrapper (local_prompt);
124446283Sdfr	}
124519370Spst      else
124646283Sdfr	{
124746283Sdfr	  rl = gdb_readline (local_prompt);
124846283Sdfr	}
124919370Spst
125019370Spst      if (annotation_level > 1 && instream == stdin)
125119370Spst	{
1252130809Smarcel	  puts_unfiltered ("\n\032\032post-");
1253130809Smarcel	  puts_unfiltered (annotation_suffix);
1254130809Smarcel	  puts_unfiltered ("\n");
125519370Spst	}
125619370Spst
125719370Spst      if (!rl || rl == (char *) EOF)
125819370Spst	{
125919370Spst	  got_eof = 1;
126019370Spst	  break;
126119370Spst	}
126298948Sobrien      if (strlen (rl) + 1 + (p - linebuffer) > linelength)
126319370Spst	{
126498948Sobrien	  linelength = strlen (rl) + 1 + (p - linebuffer);
126519370Spst	  nline = (char *) xrealloc (linebuffer, linelength);
126619370Spst	  p += nline - linebuffer;
126719370Spst	  linebuffer = nline;
126819370Spst	}
126919370Spst      p1 = rl;
127019370Spst      /* Copy line.  Don't copy null at end.  (Leaves line alone
127119370Spst         if this was just a newline)  */
127219370Spst      while (*p1)
127319370Spst	*p++ = *p1++;
127419370Spst
127598948Sobrien      xfree (rl);		/* Allocated in readline.  */
127619370Spst
127719370Spst      if (p == linebuffer || *(p - 1) != '\\')
127819370Spst	break;
127919370Spst
128019370Spst      p--;			/* Put on top of '\'.  */
128119370Spst      local_prompt = (char *) 0;
128298948Sobrien    }
128319370Spst
128419370Spst#ifdef STOP_SIGNAL
128519370Spst  if (job_control)
128619370Spst    signal (STOP_SIGNAL, SIG_DFL);
128719370Spst#endif
128819370Spst  immediate_quit--;
128919370Spst
129019370Spst  if (got_eof)
129119370Spst    return NULL;
129219370Spst
129319370Spst#define SERVER_COMMAND_LENGTH 7
129419370Spst  server_command =
129519370Spst    (p - linebuffer > SERVER_COMMAND_LENGTH)
1296130809Smarcel    && strncmp (linebuffer, "server ", SERVER_COMMAND_LENGTH) == 0;
129719370Spst  if (server_command)
129819370Spst    {
129919370Spst      /* Note that we don't set `line'.  Between this and the check in
130098948Sobrien         dont_repeat, this insures that repeating will still do the
130198948Sobrien         right thing.  */
130219370Spst      *p = '\0';
130319370Spst      return linebuffer + SERVER_COMMAND_LENGTH;
130419370Spst    }
130519370Spst
130619370Spst  /* Do history expansion if that is wished.  */
130719370Spst  if (history_expansion_p && instream == stdin
130819370Spst      && ISATTY (instream))
130919370Spst    {
131019370Spst      char *history_value;
131119370Spst      int expanded;
131219370Spst
131319370Spst      *p = '\0';		/* Insert null now.  */
131419370Spst      expanded = history_expand (linebuffer, &history_value);
131519370Spst      if (expanded)
131619370Spst	{
131719370Spst	  /* Print the changes.  */
131819370Spst	  printf_unfiltered ("%s\n", history_value);
131919370Spst
132019370Spst	  /* If there was an error, call this function again.  */
132119370Spst	  if (expanded < 0)
132219370Spst	    {
132398948Sobrien	      xfree (history_value);
132498948Sobrien	      return command_line_input (prompt_arg, repeat, annotation_suffix);
132519370Spst	    }
132619370Spst	  if (strlen (history_value) > linelength)
132719370Spst	    {
132819370Spst	      linelength = strlen (history_value) + 1;
132919370Spst	      linebuffer = (char *) xrealloc (linebuffer, linelength);
133019370Spst	    }
133119370Spst	  strcpy (linebuffer, history_value);
133298948Sobrien	  p = linebuffer + strlen (linebuffer);
133398948Sobrien	  xfree (history_value);
133419370Spst	}
133519370Spst    }
133619370Spst
133719370Spst  /* If we just got an empty line, and that is supposed
133819370Spst     to repeat the previous command, return the value in the
133919370Spst     global buffer.  */
134019370Spst  if (repeat && p == linebuffer)
134119370Spst    return line;
134298948Sobrien  for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++);
134319370Spst  if (repeat && !*p1)
134419370Spst    return line;
134519370Spst
134619370Spst  *p = 0;
134719370Spst
134819370Spst  /* Add line to history if appropriate.  */
134919370Spst  if (instream == stdin
135019370Spst      && ISATTY (stdin) && *linebuffer)
135119370Spst    add_history (linebuffer);
135219370Spst
135319370Spst  /* Note: lines consisting solely of comments are added to the command
135419370Spst     history.  This is useful when you type a command, and then
135519370Spst     realize you don't want to execute it quite yet.  You can comment
135619370Spst     out the command and then later fetch it from the value history
135719370Spst     and remove the '#'.  The kill ring is probably better, but some
135819370Spst     people are in the habit of commenting things out.  */
135919370Spst  if (*p1 == '#')
136098948Sobrien    *p1 = '\0';			/* Found a comment. */
136119370Spst
136219370Spst  /* Save into global buffer if appropriate.  */
136319370Spst  if (repeat)
136419370Spst    {
136519370Spst      if (linelength > linesize)
136619370Spst	{
136719370Spst	  line = xrealloc (line, linelength);
136819370Spst	  linesize = linelength;
136919370Spst	}
137019370Spst      strcpy (line, linebuffer);
137119370Spst      return line;
137219370Spst    }
137319370Spst
137419370Spst  return linebuffer;
137519370Spst}
137619370Spst
137798948Sobrien/* Print the GDB banner. */
137898948Sobrienvoid
137998948Sobrienprint_gdb_version (struct ui_file *stream)
138019370Spst{
138198948Sobrien  /* From GNU coding standards, first line is meant to be easy for a
138298948Sobrien     program to parse, and is just canonical program name and version
138398948Sobrien     number, which starts after last space. */
138419370Spst
138598948Sobrien  fprintf_filtered (stream, "GNU gdb %s\n", version);
138619370Spst
138798948Sobrien  /* Second line is a copyright notice. */
138819370Spst
1389130809Smarcel  fprintf_filtered (stream, "Copyright 2004 Free Software Foundation, Inc.\n");
139019370Spst
139198948Sobrien  /* Following the copyright is a brief statement that the program is
139298948Sobrien     free software, that users are free to copy and change it on
139398948Sobrien     certain conditions, that it is covered by the GNU GPL, and that
139498948Sobrien     there is no warranty. */
139519370Spst
139698948Sobrien  fprintf_filtered (stream, "\
139798948SobrienGDB is free software, covered by the GNU General Public License, and you are\n\
139898948Sobrienwelcome to change it and/or distribute copies of it under certain conditions.\n\
139998948SobrienType \"show copying\" to see the conditions.\n\
140098948SobrienThere is absolutely no warranty for GDB.  Type \"show warranty\" for details.\n");
140119370Spst
140298948Sobrien  /* After the required info we print the configuration information. */
140319370Spst
140498948Sobrien  fprintf_filtered (stream, "This GDB was configured as \"");
1405130809Smarcel  if (strcmp (host_name, target_name) != 0)
140619370Spst    {
140798948Sobrien      fprintf_filtered (stream, "--host=%s --target=%s", host_name, target_name);
140819370Spst    }
140919370Spst  else
141019370Spst    {
141198948Sobrien      fprintf_filtered (stream, "%s", host_name);
141219370Spst    }
141398948Sobrien  fprintf_filtered (stream, "\".");
141419370Spst}
141598948Sobrien
141698948Sobrien/* get_prompt: access method for the GDB prompt string.  */
141719370Spst
141898948Sobrienchar *
141998948Sobrienget_prompt (void)
142019370Spst{
1421130809Smarcel  if (event_loop_p)
1422130809Smarcel    return PROMPT (0);
142398948Sobrien  else
1424130809Smarcel    return gdb_prompt_string;
142519370Spst}
142619370Spst
142719370Spstvoid
142898948Sobrienset_prompt (char *s)
142919370Spst{
143098948Sobrien/* ??rehrauer: I don't know why this fails, since it looks as though
143198948Sobrien   assignments to prompt are wrapped in calls to savestring...
143298948Sobrien   if (prompt != NULL)
143398948Sobrien   xfree (prompt);
143498948Sobrien */
143598948Sobrien  if (event_loop_p)
143698948Sobrien    PROMPT (0) = savestring (s, strlen (s));
143746283Sdfr  else
143898948Sobrien    gdb_prompt_string = savestring (s, strlen (s));
143919370Spst}
144019370Spst
144119370Spst
144246283Sdfr/* If necessary, make the user confirm that we should quit.  Return
144346283Sdfr   non-zero if we should quit, zero if we shouldn't.  */
144446283Sdfr
144546283Sdfrint
144698948Sobrienquit_confirm (void)
144746283Sdfr{
144898948Sobrien  if (! ptid_equal (inferior_ptid, null_ptid) && target_has_execution)
144946283Sdfr    {
145046283Sdfr      char *s;
145146283Sdfr
145246283Sdfr      /* This is something of a hack.  But there's no reliable way to
145398948Sobrien         see if a GUI is running.  The `use_windows' variable doesn't
145498948Sobrien         cut it.  */
145546283Sdfr      if (init_ui_hook)
145646283Sdfr	s = "A debugging session is active.\nDo you still want to close the debugger?";
145746283Sdfr      else if (attach_flag)
145846283Sdfr	s = "The program is running.  Quit anyway (and detach it)? ";
145946283Sdfr      else
146046283Sdfr	s = "The program is running.  Exit anyway? ";
146146283Sdfr
1462130809Smarcel      if (!query ("%s", s))
146346283Sdfr	return 0;
146446283Sdfr    }
146546283Sdfr
146646283Sdfr  return 1;
146746283Sdfr}
146846283Sdfr
1469130809Smarcel/* Helper routine for quit_force that requires error handling.  */
1470130809Smarcel
1471130809Smarcelstruct qt_args
1472130809Smarcel{
1473130809Smarcel  char *args;
1474130809Smarcel  int from_tty;
1475130809Smarcel};
1476130809Smarcel
1477130809Smarcelstatic int
1478130809Smarcelquit_target (void *arg)
1479130809Smarcel{
1480130809Smarcel  struct qt_args *qt = (struct qt_args *)arg;
1481130809Smarcel
1482130809Smarcel  if (! ptid_equal (inferior_ptid, null_ptid) && target_has_execution)
1483130809Smarcel    {
1484130809Smarcel      if (attach_flag)
1485130809Smarcel        target_detach (qt->args, qt->from_tty);
1486130809Smarcel      else
1487130809Smarcel        target_kill ();
1488130809Smarcel    }
1489130809Smarcel
1490130809Smarcel  /* UDI wants this, to kill the TIP.  */
1491130809Smarcel  target_close (&current_target, 1);
1492130809Smarcel
1493130809Smarcel  /* Save the history information if it is appropriate to do so.  */
1494130809Smarcel  if (write_history_p && history_filename)
1495130809Smarcel    write_history (history_filename);
1496130809Smarcel
1497130809Smarcel  do_final_cleanups (ALL_CLEANUPS);	/* Do any final cleanups before exiting */
1498130809Smarcel
1499130809Smarcel  return 0;
1500130809Smarcel}
1501130809Smarcel
150246283Sdfr/* Quit without asking for confirmation.  */
150346283Sdfr
150419370Spstvoid
150598948Sobrienquit_force (char *args, int from_tty)
150619370Spst{
150719370Spst  int exit_code = 0;
1508130809Smarcel  struct qt_args qt;
150919370Spst
151019370Spst  /* An optional expression may be used to cause gdb to terminate with the
151119370Spst     value of that expression. */
151219370Spst  if (args)
151319370Spst    {
151498948Sobrien      struct value *val = parse_and_eval (args);
151519370Spst
151619370Spst      exit_code = (int) value_as_long (val);
151719370Spst    }
151819370Spst
1519130809Smarcel  qt.args = args;
1520130809Smarcel  qt.from_tty = from_tty;
152146283Sdfr
1522130809Smarcel  /* We want to handle any quit errors and exit regardless.  */
1523130809Smarcel  catch_errors (quit_target, &qt,
1524130809Smarcel	        "Quitting: ", RETURN_MASK_ALL);
152519370Spst
152619370Spst  exit (exit_code);
152719370Spst}
152819370Spst
152919370Spst/* Returns whether GDB is running on a terminal and whether the user
153019370Spst   desires that questions be asked of them on that terminal.  */
153119370Spst
153219370Spstint
153398948Sobrieninput_from_terminal_p (void)
153419370Spst{
153519370Spst  return gdb_has_a_terminal () && (instream == stdin) & caution;
153619370Spst}
153719370Spst
153819370Spststatic void
153998948Sobriendont_repeat_command (char *ignored, int from_tty)
154019370Spst{
154198948Sobrien  *line = 0;			/* Can't call dont_repeat here because we're not
154298948Sobrien				   necessarily reading from stdin.  */
154319370Spst}
154419370Spst
154519370Spst/* Functions to manipulate command line editing control variables.  */
154619370Spst
154719370Spst/* Number of commands to print in each call to show_commands.  */
154819370Spst#define Hist_print 10
154998948Sobrienvoid
155098948Sobrienshow_commands (char *args, int from_tty)
155119370Spst{
155219370Spst  /* Index for history commands.  Relative to history_base.  */
155319370Spst  int offset;
155419370Spst
155519370Spst  /* Number of the history entry which we are planning to display next.
155619370Spst     Relative to history_base.  */
155719370Spst  static int num = 0;
155819370Spst
155919370Spst  /* The first command in the history which doesn't exist (i.e. one more
156019370Spst     than the number of the last command).  Relative to history_base.  */
156119370Spst  int hist_len;
156219370Spst
156319370Spst  /* Print out some of the commands from the command history.  */
156419370Spst  /* First determine the length of the history list.  */
156519370Spst  hist_len = history_size;
156619370Spst  for (offset = 0; offset < history_size; offset++)
156719370Spst    {
156819370Spst      if (!history_get (history_base + offset))
156919370Spst	{
157019370Spst	  hist_len = offset;
157119370Spst	  break;
157219370Spst	}
157319370Spst    }
157419370Spst
157519370Spst  if (args)
157619370Spst    {
157719370Spst      if (args[0] == '+' && args[1] == '\0')
157819370Spst	/* "info editing +" should print from the stored position.  */
157919370Spst	;
158019370Spst      else
158119370Spst	/* "info editing <exp>" should print around command number <exp>.  */
158298948Sobrien	num = (parse_and_eval_long (args) - history_base) - Hist_print / 2;
158319370Spst    }
158419370Spst  /* "show commands" means print the last Hist_print commands.  */
158519370Spst  else
158619370Spst    {
158719370Spst      num = hist_len - Hist_print;
158819370Spst    }
158919370Spst
159019370Spst  if (num < 0)
159119370Spst    num = 0;
159219370Spst
159319370Spst  /* If there are at least Hist_print commands, we want to display the last
159419370Spst     Hist_print rather than, say, the last 6.  */
159519370Spst  if (hist_len - num < Hist_print)
159619370Spst    {
159719370Spst      num = hist_len - Hist_print;
159819370Spst      if (num < 0)
159919370Spst	num = 0;
160019370Spst    }
160119370Spst
160219370Spst  for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
160319370Spst    {
160419370Spst      printf_filtered ("%5d  %s\n", history_base + offset,
160598948Sobrien		       (history_get (history_base + offset))->line);
160619370Spst    }
160719370Spst
160819370Spst  /* The next command we want to display is the next one that we haven't
160919370Spst     displayed yet.  */
161019370Spst  num += Hist_print;
161119370Spst
161219370Spst  /* If the user repeats this command with return, it should do what
161319370Spst     "show commands +" does.  This is unnecessary if arg is null,
161419370Spst     because "show commands +" is not useful after "show commands".  */
161519370Spst  if (from_tty && args)
161619370Spst    {
161719370Spst      args[0] = '+';
161819370Spst      args[1] = '\0';
161919370Spst    }
162019370Spst}
162119370Spst
162219370Spst/* Called by do_setshow_command.  */
162319370Spststatic void
162498948Sobrienset_history_size_command (char *args, int from_tty, struct cmd_list_element *c)
162519370Spst{
162619370Spst  if (history_size == INT_MAX)
162719370Spst    unstifle_history ();
162819370Spst  else if (history_size >= 0)
162919370Spst    stifle_history (history_size);
163019370Spst  else
163119370Spst    {
163219370Spst      history_size = INT_MAX;
163319370Spst      error ("History size must be non-negative");
163419370Spst    }
163519370Spst}
163619370Spst
163798948Sobrienvoid
163898948Sobrienset_history (char *args, int from_tty)
163919370Spst{
164019370Spst  printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
164119370Spst  help_list (sethistlist, "set history ", -1, gdb_stdout);
164219370Spst}
164319370Spst
164498948Sobrienvoid
164598948Sobrienshow_history (char *args, int from_tty)
164619370Spst{
164719370Spst  cmd_show_list (showhistlist, from_tty, "");
164819370Spst}
164919370Spst
165019370Spstint info_verbose = 0;		/* Default verbose msgs off */
165119370Spst
165219370Spst/* Called by do_setshow_command.  An elaborate joke.  */
165398948Sobrienvoid
165498948Sobrienset_verbose (char *args, int from_tty, struct cmd_list_element *c)
165519370Spst{
165619370Spst  char *cmdname = "verbose";
165719370Spst  struct cmd_list_element *showcmd;
165819370Spst
165919370Spst  showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
166019370Spst
166119370Spst  if (info_verbose)
166219370Spst    {
166319370Spst      c->doc = "Set verbose printing of informational messages.";
166419370Spst      showcmd->doc = "Show verbose printing of informational messages.";
166519370Spst    }
166619370Spst  else
166719370Spst    {
166819370Spst      c->doc = "Set verbosity.";
166919370Spst      showcmd->doc = "Show verbosity.";
167019370Spst    }
167119370Spst}
167219370Spst
167319370Spst/* Init the history buffer.  Note that we are called after the init file(s)
167419370Spst * have been read so that the user can change the history file via his
167519370Spst * .gdbinit file (for instance).  The GDBHISTFILE environment variable
167619370Spst * overrides all of this.
167719370Spst */
167819370Spst
167919370Spstvoid
168098948Sobrieninit_history (void)
168119370Spst{
168219370Spst  char *tmpenv;
168319370Spst
168419370Spst  tmpenv = getenv ("HISTSIZE");
168519370Spst  if (tmpenv)
168619370Spst    history_size = atoi (tmpenv);
168719370Spst  else if (!history_size)
168819370Spst    history_size = 256;
168919370Spst
169019370Spst  stifle_history (history_size);
169119370Spst
169219370Spst  tmpenv = getenv ("GDBHISTFILE");
169319370Spst  if (tmpenv)
169498948Sobrien    history_filename = savestring (tmpenv, strlen (tmpenv));
169598948Sobrien  else if (!history_filename)
169698948Sobrien    {
169798948Sobrien      /* We include the current directory so that if the user changes
169898948Sobrien         directories the file written will be the same as the one
169998948Sobrien         that was read.  */
170098948Sobrien#ifdef __MSDOS__
170198948Sobrien      /* No leading dots in file names are allowed on MSDOS.  */
170298948Sobrien      history_filename = concat (current_directory, "/_gdb_history", NULL);
170398948Sobrien#else
170498948Sobrien      history_filename = concat (current_directory, "/.gdb_history", NULL);
170598948Sobrien#endif
170698948Sobrien    }
170719370Spst  read_history (history_filename);
170819370Spst}
170919370Spst
171019370Spststatic void
171198948Sobrieninit_main (void)
171219370Spst{
171319370Spst  struct cmd_list_element *c;
171419370Spst
171598948Sobrien  /* If we are running the asynchronous version,
171698948Sobrien     we initialize the prompts differently. */
171798948Sobrien  if (!event_loop_p)
171898948Sobrien    {
171998948Sobrien      gdb_prompt_string = savestring (DEFAULT_PROMPT, strlen (DEFAULT_PROMPT));
172098948Sobrien    }
172198948Sobrien  else
172298948Sobrien    {
172398948Sobrien      /* initialize the prompt stack to a simple "(gdb) " prompt or to
172498948Sobrien         whatever the DEFAULT_PROMPT is. */
172598948Sobrien      the_prompts.top = 0;
172698948Sobrien      PREFIX (0) = "";
172798948Sobrien      PROMPT (0) = savestring (DEFAULT_PROMPT, strlen (DEFAULT_PROMPT));
172898948Sobrien      SUFFIX (0) = "";
172998948Sobrien      /* Set things up for annotation_level > 1, if the user ever decides
173098948Sobrien         to use it. */
173198948Sobrien      async_annotation_suffix = "prompt";
173298948Sobrien      /* Set the variable associated with the setshow prompt command. */
173398948Sobrien      new_async_prompt = savestring (PROMPT (0), strlen (PROMPT (0)));
173419370Spst
173598948Sobrien      /* If gdb was started with --annotate=2, this is equivalent to
173698948Sobrien	 the user entering the command 'set annotate 2' at the gdb
173798948Sobrien	 prompt, so we need to do extra processing. */
173898948Sobrien      if (annotation_level > 1)
173998948Sobrien        set_async_annotation_level (NULL, 0, NULL);
174098948Sobrien    }
174198948Sobrien
174219370Spst  /* Set the important stuff up for command editing.  */
174319370Spst  command_editing_p = 1;
174419370Spst  history_expansion_p = 0;
174519370Spst  write_history_p = 0;
174619370Spst
174719370Spst  /* Setup important stuff for command line editing.  */
1748130809Smarcel  rl_completion_entry_function = readline_line_completion_function;
1749130809Smarcel  rl_completer_word_break_characters = default_word_break_characters ();
175098948Sobrien  rl_completer_quote_characters = get_gdb_completer_quote_characters ();
175119370Spst  rl_readline_name = "gdb";
1752130809Smarcel  rl_terminal_name = getenv ("TERM");
175319370Spst
175498948Sobrien  /* The name for this defun comes from Bash, where it originated.
175598948Sobrien     15 is Control-o, the same binding this function has in Bash.  */
175698948Sobrien  rl_add_defun ("operate-and-get-next", gdb_rl_operate_and_get_next, 15);
175719370Spst
175898948Sobrien  /* The set prompt command is different depending whether or not the
175998948Sobrien     async version is run. NOTE: this difference is going to
176098948Sobrien     disappear as we make the event loop be the default engine of
176198948Sobrien     gdb. */
176298948Sobrien  if (!event_loop_p)
176398948Sobrien    {
176498948Sobrien      add_show_from_set
176598948Sobrien	(add_set_cmd ("prompt", class_support, var_string,
176698948Sobrien		      (char *) &gdb_prompt_string, "Set gdb's prompt",
176798948Sobrien		      &setlist),
176898948Sobrien	 &showlist);
176998948Sobrien    }
177098948Sobrien  else
177198948Sobrien    {
177298948Sobrien      c = add_set_cmd ("prompt", class_support, var_string,
177398948Sobrien		       (char *) &new_async_prompt, "Set gdb's prompt",
177498948Sobrien		       &setlist);
177598948Sobrien      add_show_from_set (c, &showlist);
177698948Sobrien      set_cmd_sfunc (c, set_async_prompt);
177798948Sobrien    }
177819370Spst
177919370Spst  add_com ("dont-repeat", class_support, dont_repeat_command, "Don't repeat this command.\n\
178019370SpstPrimarily used inside of user-defined commands that should not be repeated when\n\
178119370Spsthitting return.");
178219370Spst
178398948Sobrien  /* The set editing command is different depending whether or not the
178498948Sobrien     async version is run. NOTE: this difference is going to disappear
178598948Sobrien     as we make the event loop be the default engine of gdb. */
178698948Sobrien  if (!event_loop_p)
178798948Sobrien    {
178898948Sobrien      add_show_from_set
178998948Sobrien	(add_set_cmd ("editing", class_support, var_boolean, (char *) &command_editing_p,
179098948Sobrien		      "Set editing of command lines as they are typed.\n\
179146283SdfrUse \"on\" to enable the editing, and \"off\" to disable it.\n\
179219370SpstWithout an argument, command line editing is enabled.  To edit, use\n\
179319370SpstEMACS-like or VI-like commands like control-P or ESC.", &setlist),
179498948Sobrien	 &showlist);
179598948Sobrien    }
179698948Sobrien  else
179798948Sobrien    {
179898948Sobrien      c = add_set_cmd ("editing", class_support, var_boolean, (char *) &async_command_editing_p,
179998948Sobrien		       "Set editing of command lines as they are typed.\n\
180098948SobrienUse \"on\" to enable the editing, and \"off\" to disable it.\n\
180198948SobrienWithout an argument, command line editing is enabled.  To edit, use\n\
180298948SobrienEMACS-like or VI-like commands like control-P or ESC.", &setlist);
180319370Spst
180498948Sobrien      add_show_from_set (c, &showlist);
180598948Sobrien      set_cmd_sfunc (c, set_async_editing_command);
180698948Sobrien    }
180719370Spst
180819370Spst  add_show_from_set
180998948Sobrien    (add_set_cmd ("save", no_class, var_boolean, (char *) &write_history_p,
181098948Sobrien		  "Set saving of the history record on exit.\n\
181146283SdfrUse \"on\" to enable the saving, and \"off\" to disable it.\n\
181219370SpstWithout an argument, saving is enabled.", &sethistlist),
181319370Spst     &showhistlist);
181419370Spst
181598948Sobrien  c = add_set_cmd ("size", no_class, var_integer, (char *) &history_size,
1816130809Smarcel		   "Set the size of the command history,\n\
181719370Spstie. the number of previous commands to keep a record of.", &sethistlist);
181819370Spst  add_show_from_set (c, &showhistlist);
181998948Sobrien  set_cmd_sfunc (c, set_history_size_command);
182019370Spst
182198948Sobrien  c = add_set_cmd ("filename", no_class, var_filename,
182298948Sobrien		   (char *) &history_filename,
182398948Sobrien		   "Set the filename in which to record the command history\n\
1824130809Smarcel(the list of previous commands of which a record is kept).", &sethistlist);
1825130809Smarcel  set_cmd_completer (c, filename_completer);
182698948Sobrien  add_show_from_set (c, &showhistlist);
182719370Spst
182819370Spst  add_show_from_set
182919370Spst    (add_set_cmd ("confirm", class_support, var_boolean,
183098948Sobrien		  (char *) &caution,
183119370Spst		  "Set whether to confirm potentially dangerous operations.",
183219370Spst		  &setlist),
183319370Spst     &showlist);
183419370Spst
183598948Sobrien  /* The set annotate command is different depending whether or not
183698948Sobrien     the async version is run. NOTE: this difference is going to
183798948Sobrien     disappear as we make the event loop be the default engine of
183898948Sobrien     gdb. */
183998948Sobrien  if (!event_loop_p)
184098948Sobrien    {
184198948Sobrien      c = add_set_cmd ("annotate", class_obscure, var_zinteger,
184298948Sobrien		       (char *) &annotation_level, "Set annotation_level.\n\
184398948Sobrien0 == normal;     1 == fullname (for use when running under emacs)\n\
184498948Sobrien2 == output annotated suitably for use by programs that control GDB.",
184598948Sobrien		       &setlist);
184698948Sobrien      c = add_show_from_set (c, &showlist);
184798948Sobrien    }
184898948Sobrien  else
184998948Sobrien    {
185098948Sobrien      c = add_set_cmd ("annotate", class_obscure, var_zinteger,
185198948Sobrien		       (char *) &annotation_level, "Set annotation_level.\n\
185298948Sobrien0 == normal;     1 == fullname (for use when running under emacs)\n\
185398948Sobrien2 == output annotated suitably for use by programs that control GDB.",
185498948Sobrien		       &setlist);
185598948Sobrien      add_show_from_set (c, &showlist);
185698948Sobrien      set_cmd_sfunc (c, set_async_annotation_level);
185798948Sobrien    }
185898948Sobrien  if (event_loop_p)
185998948Sobrien    {
186098948Sobrien      add_show_from_set
186198948Sobrien	(add_set_cmd ("exec-done-display", class_support, var_boolean, (char *) &exec_done_display_p,
186298948Sobrien		      "Set notification of completion for asynchronous execution commands.\n\
186398948SobrienUse \"on\" to enable the notification, and \"off\" to disable it.", &setlist),
186498948Sobrien	 &showlist);
186598948Sobrien    }
186698948Sobrien}
186719370Spst
186898948Sobrienvoid
186998948Sobriengdb_init (char *argv0)
187098948Sobrien{
187198948Sobrien  if (pre_init_ui_hook)
187298948Sobrien    pre_init_ui_hook ();
187319370Spst
187498948Sobrien  /* Run the init function of each source file */
187519370Spst
187698948Sobrien  getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
187798948Sobrien  current_directory = gdb_dirbuf;
187819370Spst
187998948Sobrien#ifdef __MSDOS__
188098948Sobrien  /* Make sure we return to the original directory upon exit, come
188198948Sobrien     what may, since the OS doesn't do that for us.  */
188298948Sobrien  make_final_cleanup (do_chdir_cleanup, xstrdup (current_directory));
188398948Sobrien#endif
188419370Spst
188598948Sobrien  init_cmd_lists ();		/* This needs to be done first */
188698948Sobrien  initialize_targets ();	/* Setup target_terminal macros for utils.c */
188798948Sobrien  initialize_utils ();		/* Make errors and warnings possible */
188898948Sobrien  initialize_all_files ();
188998948Sobrien  initialize_current_architecture ();
189098948Sobrien  init_cli_cmds();
189198948Sobrien  init_main ();			/* But that omits this file!  Do it now */
189219370Spst
189398948Sobrien  /* The signal handling mechanism is different depending whether or
189498948Sobrien     not the async version is run. NOTE: in the future we plan to make
189598948Sobrien     the event loop be the default engine of gdb, and this difference
189698948Sobrien     will disappear. */
189798948Sobrien  if (event_loop_p)
189898948Sobrien    async_init_signals ();
189998948Sobrien  else
190098948Sobrien    init_signals ();
190119370Spst
190298948Sobrien  /* We need a default language for parsing expressions, so simple things like
190398948Sobrien     "set width 0" won't fail if no language is explicitly set in a config file
190498948Sobrien     or implicitly set by reading an executable during startup. */
190598948Sobrien  set_language (language_c);
190698948Sobrien  expected_language = current_language;		/* don't warn about the change.  */
190719370Spst
190898948Sobrien  /* Allow another UI to initialize. If the UI fails to initialize, and
190998948Sobrien     it wants GDB to revert to the CLI, it should clear init_ui_hook. */
191098948Sobrien  if (init_ui_hook)
191198948Sobrien    init_ui_hook (argv0);
191219370Spst}
1913