xref: /dflybsd-src/contrib/gdb-7/gdb/top.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* Top level stuff for GDB, the GNU debugger.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 1986-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "gdbcmd.h"
225796c8dcSSimon Schubert #include "cli/cli-cmds.h"
235796c8dcSSimon Schubert #include "cli/cli-script.h"
245796c8dcSSimon Schubert #include "cli/cli-setshow.h"
255796c8dcSSimon Schubert #include "cli/cli-decode.h"
265796c8dcSSimon Schubert #include "symtab.h"
275796c8dcSSimon Schubert #include "inferior.h"
285796c8dcSSimon Schubert #include "exceptions.h"
295796c8dcSSimon Schubert #include <signal.h>
305796c8dcSSimon Schubert #include "target.h"
315796c8dcSSimon Schubert #include "breakpoint.h"
325796c8dcSSimon Schubert #include "gdbtypes.h"
335796c8dcSSimon Schubert #include "expression.h"
345796c8dcSSimon Schubert #include "value.h"
355796c8dcSSimon Schubert #include "language.h"
365796c8dcSSimon Schubert #include "terminal.h"		/* For job_control.  */
375796c8dcSSimon Schubert #include "annotate.h"
385796c8dcSSimon Schubert #include "completer.h"
395796c8dcSSimon Schubert #include "top.h"
405796c8dcSSimon Schubert #include "version.h"
415796c8dcSSimon Schubert #include "serial.h"
425796c8dcSSimon Schubert #include "doublest.h"
435796c8dcSSimon Schubert #include "gdb_assert.h"
445796c8dcSSimon Schubert #include "main.h"
455796c8dcSSimon Schubert #include "event-loop.h"
465796c8dcSSimon Schubert #include "gdbthread.h"
47c50c785cSJohn Marino #include "python/python.h"
48a45ae5f8SJohn Marino #include "interps.h"
49*ef5ccd6cSJohn Marino #include "observer.h"
505796c8dcSSimon Schubert 
51c50c785cSJohn Marino /* readline include files.  */
525796c8dcSSimon Schubert #include "readline/readline.h"
535796c8dcSSimon Schubert #include "readline/history.h"
545796c8dcSSimon Schubert 
555796c8dcSSimon Schubert /* readline defines this.  */
565796c8dcSSimon Schubert #undef savestring
575796c8dcSSimon Schubert 
585796c8dcSSimon Schubert #include <sys/types.h>
595796c8dcSSimon Schubert 
605796c8dcSSimon Schubert #include "event-top.h"
615796c8dcSSimon Schubert #include "gdb_string.h"
625796c8dcSSimon Schubert #include "gdb_stat.h"
635796c8dcSSimon Schubert #include <ctype.h>
645796c8dcSSimon Schubert #include "ui-out.h"
655796c8dcSSimon Schubert #include "cli-out.h"
665796c8dcSSimon Schubert 
67*ef5ccd6cSJohn Marino extern void initialize_all_files (void);
68*ef5ccd6cSJohn Marino 
69a45ae5f8SJohn Marino #define PROMPT(X) the_prompts.prompt_stack[the_prompts.top + X].prompt
70a45ae5f8SJohn Marino #define PREFIX(X) the_prompts.prompt_stack[the_prompts.top + X].prefix
71a45ae5f8SJohn Marino #define SUFFIX(X) the_prompts.prompt_stack[the_prompts.top + X].suffix
72a45ae5f8SJohn Marino 
735796c8dcSSimon Schubert /* Default command line prompt.  This is overriden in some configs.  */
745796c8dcSSimon Schubert 
755796c8dcSSimon Schubert #ifndef DEFAULT_PROMPT
765796c8dcSSimon Schubert #define DEFAULT_PROMPT	"(gdb) "
775796c8dcSSimon Schubert #endif
785796c8dcSSimon Schubert 
795796c8dcSSimon Schubert /* Initialization file name for gdb.  This is overridden in some configs.  */
805796c8dcSSimon Schubert 
815796c8dcSSimon Schubert #ifndef PATH_MAX
825796c8dcSSimon Schubert # ifdef FILENAME_MAX
835796c8dcSSimon Schubert #  define PATH_MAX FILENAME_MAX
845796c8dcSSimon Schubert # else
855796c8dcSSimon Schubert #  define PATH_MAX 512
865796c8dcSSimon Schubert # endif
875796c8dcSSimon Schubert #endif
885796c8dcSSimon Schubert 
895796c8dcSSimon Schubert #ifndef	GDBINIT_FILENAME
905796c8dcSSimon Schubert #define	GDBINIT_FILENAME	".gdbinit"
915796c8dcSSimon Schubert #endif
925796c8dcSSimon Schubert char gdbinit[PATH_MAX + 1] = GDBINIT_FILENAME;
935796c8dcSSimon Schubert 
945796c8dcSSimon Schubert int inhibit_gdbinit = 0;
955796c8dcSSimon Schubert 
965796c8dcSSimon Schubert /* If nonzero, and GDB has been configured to be able to use windows,
975796c8dcSSimon Schubert    attempt to open them upon startup.  */
985796c8dcSSimon Schubert 
995796c8dcSSimon Schubert int use_windows = 0;
1005796c8dcSSimon Schubert 
1015796c8dcSSimon Schubert extern char lang_frame_mismatch_warn[];		/* language.c */
1025796c8dcSSimon Schubert 
103*ef5ccd6cSJohn Marino /* Flag for whether we want to confirm potentially dangerous
104*ef5ccd6cSJohn Marino    operations.  Default is yes.  */
1055796c8dcSSimon Schubert 
106*ef5ccd6cSJohn Marino int confirm = 1;
107*ef5ccd6cSJohn Marino 
1085796c8dcSSimon Schubert static void
show_confirm(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)109*ef5ccd6cSJohn Marino show_confirm (struct ui_file *file, int from_tty,
1105796c8dcSSimon Schubert 	      struct cmd_list_element *c, const char *value)
1115796c8dcSSimon Schubert {
112c50c785cSJohn Marino   fprintf_filtered (file, _("Whether to confirm potentially "
113c50c785cSJohn Marino 			    "dangerous operations is %s.\n"),
1145796c8dcSSimon Schubert 		    value);
1155796c8dcSSimon Schubert }
1165796c8dcSSimon Schubert 
117c50c785cSJohn Marino /* stdio stream that command input is being read from.  Set to stdin
118c50c785cSJohn Marino    normally.  Set by source_command to the file we are sourcing.  Set
119c50c785cSJohn Marino    to NULL if we are executing a user-defined command or interacting
120c50c785cSJohn Marino    via a GUI.  */
1215796c8dcSSimon Schubert 
1225796c8dcSSimon Schubert FILE *instream;
1235796c8dcSSimon Schubert 
1245796c8dcSSimon Schubert /* Flag to indicate whether a user defined command is currently running.  */
1255796c8dcSSimon Schubert 
1265796c8dcSSimon Schubert int in_user_command;
1275796c8dcSSimon Schubert 
1285796c8dcSSimon Schubert /* Current working directory.  */
1295796c8dcSSimon Schubert 
1305796c8dcSSimon Schubert char *current_directory;
1315796c8dcSSimon Schubert 
1325796c8dcSSimon Schubert /* The directory name is actually stored here (usually).  */
1335796c8dcSSimon Schubert char gdb_dirbuf[1024];
1345796c8dcSSimon Schubert 
1355796c8dcSSimon Schubert /* Function to call before reading a command, if nonzero.
1365796c8dcSSimon Schubert    The function receives two args: an input stream,
1375796c8dcSSimon Schubert    and a prompt string.  */
1385796c8dcSSimon Schubert 
1395796c8dcSSimon Schubert void (*window_hook) (FILE *, char *);
1405796c8dcSSimon Schubert 
1415796c8dcSSimon Schubert /* Buffer used for reading command lines, and the size
1425796c8dcSSimon Schubert    allocated for it so far.  */
1435796c8dcSSimon Schubert 
144a45ae5f8SJohn Marino char *saved_command_line;
145a45ae5f8SJohn Marino int saved_command_line_size = 100;
1465796c8dcSSimon Schubert 
1475796c8dcSSimon Schubert /* Nonzero if the current command is modified by "server ".  This
1485796c8dcSSimon Schubert    affects things like recording into the command history, commands
1495796c8dcSSimon Schubert    repeating on RETURN, etc.  This is so a user interface (emacs, GUI,
1505796c8dcSSimon Schubert    whatever) can issue its own commands and also send along commands
1515796c8dcSSimon Schubert    from the user, and have the user not notice that the user interface
1525796c8dcSSimon Schubert    is issuing commands too.  */
1535796c8dcSSimon Schubert int server_command;
1545796c8dcSSimon Schubert 
1555796c8dcSSimon Schubert /* Baud rate specified for talking to serial target systems.  Default
1565796c8dcSSimon Schubert    is left as -1, so targets can choose their own defaults.  */
157c50c785cSJohn Marino /* FIXME: This means that "show remotebaud" and gr_files_info can
158c50c785cSJohn Marino    print -1 or (unsigned int)-1.  This is a Bad User Interface.  */
1595796c8dcSSimon Schubert 
1605796c8dcSSimon Schubert int baud_rate = -1;
1615796c8dcSSimon Schubert 
1625796c8dcSSimon Schubert /* Timeout limit for response from target.  */
1635796c8dcSSimon Schubert 
1645796c8dcSSimon Schubert /* The default value has been changed many times over the years.  It
1655796c8dcSSimon Schubert    was originally 5 seconds.  But that was thought to be a long time
1665796c8dcSSimon Schubert    to sit and wait, so it was changed to 2 seconds.  That was thought
1675796c8dcSSimon Schubert    to be plenty unless the connection was going through some terminal
1685796c8dcSSimon Schubert    server or multiplexer or other form of hairy serial connection.
1695796c8dcSSimon Schubert 
1705796c8dcSSimon Schubert    In mid-1996, remote_timeout was moved from remote.c to top.c and
1715796c8dcSSimon Schubert    it began being used in other remote-* targets.  It appears that the
1725796c8dcSSimon Schubert    default was changed to 20 seconds at that time, perhaps because the
1735796c8dcSSimon Schubert    Renesas E7000 ICE didn't always respond in a timely manner.
1745796c8dcSSimon Schubert 
1755796c8dcSSimon Schubert    But if 5 seconds is a long time to sit and wait for retransmissions,
1765796c8dcSSimon Schubert    20 seconds is far worse.  This demonstrates the difficulty of using
1775796c8dcSSimon Schubert    a single variable for all protocol timeouts.
1785796c8dcSSimon Schubert 
1795796c8dcSSimon Schubert    As remote.c is used much more than remote-e7000.c, it was changed
1805796c8dcSSimon Schubert    back to 2 seconds in 1999.  */
1815796c8dcSSimon Schubert 
1825796c8dcSSimon Schubert int remote_timeout = 2;
1835796c8dcSSimon Schubert 
1845796c8dcSSimon Schubert /* Non-zero tells remote* modules to output debugging info.  */
1855796c8dcSSimon Schubert 
1865796c8dcSSimon Schubert int remote_debug = 0;
1875796c8dcSSimon Schubert 
1885796c8dcSSimon Schubert /* Sbrk location on entry to main.  Used for statistics only.  */
1895796c8dcSSimon Schubert #ifdef HAVE_SBRK
1905796c8dcSSimon Schubert char *lim_at_start;
1915796c8dcSSimon Schubert #endif
1925796c8dcSSimon Schubert 
1935796c8dcSSimon Schubert /* Hooks for alternate command interfaces.  */
1945796c8dcSSimon Schubert 
195c50c785cSJohn Marino /* Called after most modules have been initialized, but before taking
196c50c785cSJohn Marino    users command file.
1975796c8dcSSimon Schubert 
198c50c785cSJohn Marino    If the UI fails to initialize and it wants GDB to continue using
199c50c785cSJohn Marino    the default UI, then it should clear this hook before returning.  */
2005796c8dcSSimon Schubert 
2015796c8dcSSimon Schubert void (*deprecated_init_ui_hook) (char *argv0);
2025796c8dcSSimon Schubert 
203c50c785cSJohn Marino /* This hook is called from within gdb's many mini-event loops which
204c50c785cSJohn Marino    could steal control from a real user interface's event loop.  It
205c50c785cSJohn Marino    returns non-zero if the user is requesting a detach, zero
206c50c785cSJohn Marino    otherwise.  */
2075796c8dcSSimon Schubert 
2085796c8dcSSimon Schubert int (*deprecated_ui_loop_hook) (int);
2095796c8dcSSimon Schubert 
2105796c8dcSSimon Schubert /* Called instead of command_loop at top level.  Can be invoked via
2115796c8dcSSimon Schubert    throw_exception().  */
2125796c8dcSSimon Schubert 
2135796c8dcSSimon Schubert void (*deprecated_command_loop_hook) (void);
2145796c8dcSSimon Schubert 
2155796c8dcSSimon Schubert 
2165796c8dcSSimon Schubert /* Called from print_frame_info to list the line we stopped in.  */
2175796c8dcSSimon Schubert 
218c50c785cSJohn Marino void (*deprecated_print_frame_info_listing_hook) (struct symtab * s,
219c50c785cSJohn Marino 						  int line,
220c50c785cSJohn Marino 						  int stopline,
221c50c785cSJohn Marino 						  int noerror);
2225796c8dcSSimon Schubert /* Replaces most of query.  */
2235796c8dcSSimon Schubert 
2245796c8dcSSimon Schubert int (*deprecated_query_hook) (const char *, va_list);
2255796c8dcSSimon Schubert 
2265796c8dcSSimon Schubert /* Replaces most of warning.  */
2275796c8dcSSimon Schubert 
2285796c8dcSSimon Schubert void (*deprecated_warning_hook) (const char *, va_list);
2295796c8dcSSimon Schubert 
2305796c8dcSSimon Schubert /* These three functions support getting lines of text from the user.
2315796c8dcSSimon Schubert    They are used in sequence.  First deprecated_readline_begin_hook is
2325796c8dcSSimon Schubert    called with a text string that might be (for example) a message for
2335796c8dcSSimon Schubert    the user to type in a sequence of commands to be executed at a
2345796c8dcSSimon Schubert    breakpoint.  If this function calls back to a GUI, it might take
2355796c8dcSSimon Schubert    this opportunity to pop up a text interaction window with this
2365796c8dcSSimon Schubert    message.  Next, deprecated_readline_hook is called with a prompt
2375796c8dcSSimon Schubert    that is emitted prior to collecting the user input.  It can be
2385796c8dcSSimon Schubert    called multiple times.  Finally, deprecated_readline_end_hook is
2395796c8dcSSimon Schubert    called to notify the GUI that we are done with the interaction
2405796c8dcSSimon Schubert    window and it can close it.  */
2415796c8dcSSimon Schubert 
2425796c8dcSSimon Schubert void (*deprecated_readline_begin_hook) (char *, ...);
2435796c8dcSSimon Schubert char *(*deprecated_readline_hook) (char *);
2445796c8dcSSimon Schubert void (*deprecated_readline_end_hook) (void);
2455796c8dcSSimon Schubert 
2465796c8dcSSimon Schubert /* Called as appropriate to notify the interface that we have attached
2475796c8dcSSimon Schubert    to or detached from an already running process.  */
2485796c8dcSSimon Schubert 
2495796c8dcSSimon Schubert void (*deprecated_attach_hook) (void);
2505796c8dcSSimon Schubert void (*deprecated_detach_hook) (void);
2515796c8dcSSimon Schubert 
252c50c785cSJohn Marino /* Called during long calculations to allow GUI to repair window
253c50c785cSJohn Marino    damage, and to check for stop buttons, etc...  */
2545796c8dcSSimon Schubert 
2555796c8dcSSimon Schubert void (*deprecated_interactive_hook) (void);
2565796c8dcSSimon Schubert 
2575796c8dcSSimon Schubert /* Tell the GUI someone changed the register REGNO.  -1 means
2585796c8dcSSimon Schubert    that the caller does not know which register changed or
2595796c8dcSSimon Schubert    that several registers have changed (see value_assign).  */
2605796c8dcSSimon Schubert void (*deprecated_register_changed_hook) (int regno);
2615796c8dcSSimon Schubert 
262c50c785cSJohn Marino /* Called when going to wait for the target.  Usually allows the GUI
263c50c785cSJohn Marino    to run while waiting for target events.  */
2645796c8dcSSimon Schubert 
2655796c8dcSSimon Schubert ptid_t (*deprecated_target_wait_hook) (ptid_t ptid,
2665796c8dcSSimon Schubert 				       struct target_waitstatus *status,
2675796c8dcSSimon Schubert 				       int options);
2685796c8dcSSimon Schubert 
269c50c785cSJohn Marino /* Used by UI as a wrapper around command execution.  May do various
270c50c785cSJohn Marino    things like enabling/disabling buttons, etc...  */
2715796c8dcSSimon Schubert 
272c50c785cSJohn Marino void (*deprecated_call_command_hook) (struct cmd_list_element * c,
273c50c785cSJohn Marino 				      char *cmd, int from_tty);
2745796c8dcSSimon Schubert 
2755796c8dcSSimon Schubert /* Called after a `set' command has finished.  Is only run if the
2765796c8dcSSimon Schubert    `set' command succeeded.  */
2775796c8dcSSimon Schubert 
2785796c8dcSSimon Schubert void (*deprecated_set_hook) (struct cmd_list_element * c);
2795796c8dcSSimon Schubert 
2805796c8dcSSimon Schubert /* Called when the current thread changes.  Argument is thread id.  */
2815796c8dcSSimon Schubert 
2825796c8dcSSimon Schubert void (*deprecated_context_hook) (int id);
2835796c8dcSSimon Schubert 
2845796c8dcSSimon Schubert /* Handler for SIGHUP.  */
2855796c8dcSSimon Schubert 
2865796c8dcSSimon Schubert #ifdef SIGHUP
2875796c8dcSSimon Schubert /* NOTE 1999-04-29: This function will be static again, once we modify
2885796c8dcSSimon Schubert    gdb to use the event loop as the default command loop and we merge
289c50c785cSJohn Marino    event-top.c into this file, top.c.  */
290a45ae5f8SJohn Marino /* static */ void
quit_cover(void)291a45ae5f8SJohn Marino quit_cover (void)
2925796c8dcSSimon Schubert {
293*ef5ccd6cSJohn Marino   /* Stop asking user for confirmation --- we're exiting.  This
294*ef5ccd6cSJohn Marino      prevents asking the user dumb questions.  */
295*ef5ccd6cSJohn Marino   confirm = 0;
2965796c8dcSSimon Schubert   quit_command ((char *) 0, 0);
2975796c8dcSSimon Schubert }
2985796c8dcSSimon Schubert #endif /* defined SIGHUP */
2995796c8dcSSimon Schubert 
300a45ae5f8SJohn Marino /* Line number we are currently in, in a file which is being sourced.  */
3015796c8dcSSimon Schubert /* NOTE 1999-04-29: This variable will be static again, once we modify
3025796c8dcSSimon Schubert    gdb to use the event loop as the default command loop and we merge
303c50c785cSJohn Marino    event-top.c into this file, top.c.  */
3045796c8dcSSimon Schubert /* static */ int source_line_number;
3055796c8dcSSimon Schubert 
3065796c8dcSSimon Schubert /* Name of the file we are sourcing.  */
3075796c8dcSSimon Schubert /* NOTE 1999-04-29: This variable will be static again, once we modify
3085796c8dcSSimon Schubert    gdb to use the event loop as the default command loop and we merge
309c50c785cSJohn Marino    event-top.c into this file, top.c.  */
310cf7f2e2dSJohn Marino /* static */ const char *source_file_name;
3115796c8dcSSimon Schubert 
3125796c8dcSSimon Schubert /* Clean up on error during a "source" command (or execution of a
3135796c8dcSSimon Schubert    user-defined command).  */
3145796c8dcSSimon Schubert 
3155796c8dcSSimon Schubert void
do_restore_instream_cleanup(void * stream)3165796c8dcSSimon Schubert do_restore_instream_cleanup (void *stream)
3175796c8dcSSimon Schubert {
3185796c8dcSSimon Schubert   /* Restore the previous input stream.  */
3195796c8dcSSimon Schubert   instream = stream;
3205796c8dcSSimon Schubert }
3215796c8dcSSimon Schubert 
3225796c8dcSSimon Schubert /* Read commands from STREAM.  */
3235796c8dcSSimon Schubert void
read_command_file(FILE * stream)3245796c8dcSSimon Schubert read_command_file (FILE *stream)
3255796c8dcSSimon Schubert {
3265796c8dcSSimon Schubert   struct cleanup *cleanups;
3275796c8dcSSimon Schubert 
3285796c8dcSSimon Schubert   cleanups = make_cleanup (do_restore_instream_cleanup, instream);
3295796c8dcSSimon Schubert   instream = stream;
3305796c8dcSSimon Schubert   command_loop ();
3315796c8dcSSimon Schubert   do_cleanups (cleanups);
3325796c8dcSSimon Schubert }
3335796c8dcSSimon Schubert 
3345796c8dcSSimon Schubert void (*pre_init_ui_hook) (void);
3355796c8dcSSimon Schubert 
3365796c8dcSSimon Schubert #ifdef __MSDOS__
337*ef5ccd6cSJohn Marino static void
do_chdir_cleanup(void * old_dir)3385796c8dcSSimon Schubert do_chdir_cleanup (void *old_dir)
3395796c8dcSSimon Schubert {
3405796c8dcSSimon Schubert   chdir (old_dir);
3415796c8dcSSimon Schubert   xfree (old_dir);
3425796c8dcSSimon Schubert }
3435796c8dcSSimon Schubert #endif
3445796c8dcSSimon Schubert 
345a45ae5f8SJohn Marino struct cleanup *
prepare_execute_command(void)3465796c8dcSSimon Schubert prepare_execute_command (void)
3475796c8dcSSimon Schubert {
348a45ae5f8SJohn Marino   struct value *mark;
349a45ae5f8SJohn Marino   struct cleanup *cleanup;
350a45ae5f8SJohn Marino 
351a45ae5f8SJohn Marino   mark = value_mark ();
352a45ae5f8SJohn Marino   cleanup = make_cleanup_value_free_to_mark (mark);
3535796c8dcSSimon Schubert 
354c50c785cSJohn Marino   /* With multiple threads running while the one we're examining is
355c50c785cSJohn Marino      stopped, the dcache can get stale without us being able to detect
356c50c785cSJohn Marino      it.  For the duration of the command, though, use the dcache to
357c50c785cSJohn Marino      help things like backtrace.  */
3585796c8dcSSimon Schubert   if (non_stop)
3595796c8dcSSimon Schubert     target_dcache_invalidate ();
360a45ae5f8SJohn Marino 
361a45ae5f8SJohn Marino   return cleanup;
3625796c8dcSSimon Schubert }
3635796c8dcSSimon Schubert 
364*ef5ccd6cSJohn Marino /* Tell the user if the language has changed (except first time) after
365*ef5ccd6cSJohn Marino    executing a command.  */
366*ef5ccd6cSJohn Marino 
367*ef5ccd6cSJohn Marino void
check_frame_language_change(void)368*ef5ccd6cSJohn Marino check_frame_language_change (void)
369*ef5ccd6cSJohn Marino {
370*ef5ccd6cSJohn Marino   static int warned = 0;
371*ef5ccd6cSJohn Marino 
372*ef5ccd6cSJohn Marino   /* First make sure that a new frame has been selected, in case the
373*ef5ccd6cSJohn Marino      command or the hooks changed the program state.  */
374*ef5ccd6cSJohn Marino   deprecated_safe_get_selected_frame ();
375*ef5ccd6cSJohn Marino   if (current_language != expected_language)
376*ef5ccd6cSJohn Marino     {
377*ef5ccd6cSJohn Marino       if (language_mode == language_mode_auto && info_verbose)
378*ef5ccd6cSJohn Marino 	{
379*ef5ccd6cSJohn Marino 	  language_info (1);	/* Print what changed.  */
380*ef5ccd6cSJohn Marino 	}
381*ef5ccd6cSJohn Marino       warned = 0;
382*ef5ccd6cSJohn Marino     }
383*ef5ccd6cSJohn Marino 
384*ef5ccd6cSJohn Marino   /* Warn the user if the working language does not match the language
385*ef5ccd6cSJohn Marino      of the current frame.  Only warn the user if we are actually
386*ef5ccd6cSJohn Marino      running the program, i.e. there is a stack.  */
387*ef5ccd6cSJohn Marino   /* FIXME: This should be cacheing the frame and only running when
388*ef5ccd6cSJohn Marino      the frame changes.  */
389*ef5ccd6cSJohn Marino 
390*ef5ccd6cSJohn Marino   if (has_stack_frames ())
391*ef5ccd6cSJohn Marino     {
392*ef5ccd6cSJohn Marino       enum language flang;
393*ef5ccd6cSJohn Marino 
394*ef5ccd6cSJohn Marino       flang = get_frame_language ();
395*ef5ccd6cSJohn Marino       if (!warned
396*ef5ccd6cSJohn Marino 	  && flang != language_unknown
397*ef5ccd6cSJohn Marino 	  && flang != current_language->la_language)
398*ef5ccd6cSJohn Marino 	{
399*ef5ccd6cSJohn Marino 	  printf_filtered ("%s\n", lang_frame_mismatch_warn);
400*ef5ccd6cSJohn Marino 	  warned = 1;
401*ef5ccd6cSJohn Marino 	}
402*ef5ccd6cSJohn Marino     }
403*ef5ccd6cSJohn Marino }
404*ef5ccd6cSJohn Marino 
4055796c8dcSSimon Schubert /* Execute the line P as a command, in the current user context.
4065796c8dcSSimon Schubert    Pass FROM_TTY as second argument to the defining function.  */
4075796c8dcSSimon Schubert 
4085796c8dcSSimon Schubert void
execute_command(char * p,int from_tty)4095796c8dcSSimon Schubert execute_command (char *p, int from_tty)
4105796c8dcSSimon Schubert {
411a45ae5f8SJohn Marino   struct cleanup *cleanup_if_error, *cleanup;
4125796c8dcSSimon Schubert   struct cmd_list_element *c;
4135796c8dcSSimon Schubert   char *line;
4145796c8dcSSimon Schubert 
415a45ae5f8SJohn Marino   cleanup_if_error = make_bpstat_clear_actions_cleanup ();
416a45ae5f8SJohn Marino   cleanup = prepare_execute_command ();
4175796c8dcSSimon Schubert 
4185796c8dcSSimon Schubert   /* Force cleanup of any alloca areas if using C alloca instead of
4195796c8dcSSimon Schubert      a builtin alloca.  */
4205796c8dcSSimon Schubert   alloca (0);
4215796c8dcSSimon Schubert 
4225796c8dcSSimon Schubert   /* This can happen when command_line_input hits end of file.  */
4235796c8dcSSimon Schubert   if (p == NULL)
424a45ae5f8SJohn Marino     {
425a45ae5f8SJohn Marino       do_cleanups (cleanup);
4265796c8dcSSimon Schubert       return;
427a45ae5f8SJohn Marino     }
4285796c8dcSSimon Schubert 
4295796c8dcSSimon Schubert   target_log_command (p);
4305796c8dcSSimon Schubert 
4315796c8dcSSimon Schubert   while (*p == ' ' || *p == '\t')
4325796c8dcSSimon Schubert     p++;
4335796c8dcSSimon Schubert   if (*p)
4345796c8dcSSimon Schubert     {
4355796c8dcSSimon Schubert       char *arg;
4365796c8dcSSimon Schubert       line = p;
4375796c8dcSSimon Schubert 
4385796c8dcSSimon Schubert       /* If trace-commands is set then this will print this command.  */
4395796c8dcSSimon Schubert       print_command_trace (p);
4405796c8dcSSimon Schubert 
4415796c8dcSSimon Schubert       c = lookup_cmd (&p, cmdlist, "", 0, 1);
4425796c8dcSSimon Schubert 
4435796c8dcSSimon Schubert       /* Pass null arg rather than an empty one.  */
4445796c8dcSSimon Schubert       arg = *p ? p : 0;
4455796c8dcSSimon Schubert 
4465796c8dcSSimon Schubert       /* FIXME: cagney/2002-02-02: The c->type test is pretty dodgy
4475796c8dcSSimon Schubert          while the is_complete_command(cfunc) test is just plain
4485796c8dcSSimon Schubert          bogus.  They should both be replaced by a test of the form
4495796c8dcSSimon Schubert          c->strip_trailing_white_space_p.  */
4505796c8dcSSimon Schubert       /* NOTE: cagney/2002-02-02: The function.cfunc in the below
4515796c8dcSSimon Schubert          can't be replaced with func.  This is because it is the
4525796c8dcSSimon Schubert          cfunc, and not the func, that has the value that the
4535796c8dcSSimon Schubert          is_complete_command hack is testing for.  */
4545796c8dcSSimon Schubert       /* Clear off trailing whitespace, except for set and complete
4555796c8dcSSimon Schubert          command.  */
4565796c8dcSSimon Schubert       if (arg
4575796c8dcSSimon Schubert 	  && c->type != set_cmd
4585796c8dcSSimon Schubert 	  && !is_complete_command (c))
4595796c8dcSSimon Schubert 	{
4605796c8dcSSimon Schubert 	  p = arg + strlen (arg) - 1;
4615796c8dcSSimon Schubert 	  while (p >= arg && (*p == ' ' || *p == '\t'))
4625796c8dcSSimon Schubert 	    p--;
4635796c8dcSSimon Schubert 	  *(p + 1) = '\0';
4645796c8dcSSimon Schubert 	}
4655796c8dcSSimon Schubert 
4665796c8dcSSimon Schubert       /* If this command has been pre-hooked, run the hook first.  */
4675796c8dcSSimon Schubert       execute_cmd_pre_hook (c);
4685796c8dcSSimon Schubert 
4695796c8dcSSimon Schubert       if (c->flags & DEPRECATED_WARN_USER)
4705796c8dcSSimon Schubert 	deprecated_cmd_warning (&line);
4715796c8dcSSimon Schubert 
472*ef5ccd6cSJohn Marino       /* c->user_commands would be NULL in the case of a python command.  */
473*ef5ccd6cSJohn Marino       if (c->class == class_user && c->user_commands)
4745796c8dcSSimon Schubert 	execute_user_command (c, arg);
475*ef5ccd6cSJohn Marino       else if (c->type == set_cmd)
476*ef5ccd6cSJohn Marino 	do_set_command (arg, from_tty, c);
477*ef5ccd6cSJohn Marino       else if (c->type == show_cmd)
478*ef5ccd6cSJohn Marino 	do_show_command (arg, from_tty, c);
4795796c8dcSSimon Schubert       else if (!cmd_func_p (c))
4805796c8dcSSimon Schubert 	error (_("That is not a command, just a help topic."));
4815796c8dcSSimon Schubert       else if (deprecated_call_command_hook)
482*ef5ccd6cSJohn Marino 	deprecated_call_command_hook (c, arg, from_tty);
4835796c8dcSSimon Schubert       else
484*ef5ccd6cSJohn Marino 	cmd_func (c, arg, from_tty);
4855796c8dcSSimon Schubert 
486a45ae5f8SJohn Marino       /* If the interpreter is in sync mode (we're running a user
487a45ae5f8SJohn Marino 	 command's list, running command hooks or similars), and we
488a45ae5f8SJohn Marino 	 just ran a synchronous command that started the target, wait
489a45ae5f8SJohn Marino 	 for that command to end.  */
490a45ae5f8SJohn Marino       if (!interpreter_async && sync_execution)
491a45ae5f8SJohn Marino 	{
492a45ae5f8SJohn Marino 	  while (gdb_do_one_event () >= 0)
493a45ae5f8SJohn Marino 	    if (!sync_execution)
494a45ae5f8SJohn Marino 	      break;
495a45ae5f8SJohn Marino 	}
496a45ae5f8SJohn Marino 
4975796c8dcSSimon Schubert       /* If this command has been post-hooked, run the hook last.  */
4985796c8dcSSimon Schubert       execute_cmd_post_hook (c);
4995796c8dcSSimon Schubert 
5005796c8dcSSimon Schubert     }
5015796c8dcSSimon Schubert 
502*ef5ccd6cSJohn Marino   check_frame_language_change ();
503a45ae5f8SJohn Marino 
504a45ae5f8SJohn Marino   do_cleanups (cleanup);
505a45ae5f8SJohn Marino   discard_cleanups (cleanup_if_error);
5065796c8dcSSimon Schubert }
5075796c8dcSSimon Schubert 
508cf7f2e2dSJohn Marino /* Run execute_command for P and FROM_TTY.  Capture its output into the
509cf7f2e2dSJohn Marino    returned string, do not display it to the screen.  BATCH_FLAG will be
510cf7f2e2dSJohn Marino    temporarily set to true.  */
511cf7f2e2dSJohn Marino 
512cf7f2e2dSJohn Marino char *
execute_command_to_string(char * p,int from_tty)513cf7f2e2dSJohn Marino execute_command_to_string (char *p, int from_tty)
514cf7f2e2dSJohn Marino {
515cf7f2e2dSJohn Marino   struct ui_file *str_file;
516cf7f2e2dSJohn Marino   struct cleanup *cleanup;
517cf7f2e2dSJohn Marino   char *retval;
518cf7f2e2dSJohn Marino 
519cf7f2e2dSJohn Marino   /* GDB_STDOUT should be better already restored during these
520cf7f2e2dSJohn Marino      restoration callbacks.  */
521cf7f2e2dSJohn Marino   cleanup = set_batch_flag_and_make_cleanup_restore_page_info ();
522cf7f2e2dSJohn Marino 
523a45ae5f8SJohn Marino   make_cleanup_restore_integer (&interpreter_async);
524a45ae5f8SJohn Marino   interpreter_async = 0;
525a45ae5f8SJohn Marino 
526cf7f2e2dSJohn Marino   str_file = mem_fileopen ();
527cf7f2e2dSJohn Marino 
528c50c785cSJohn Marino   make_cleanup_ui_file_delete (str_file);
529cf7f2e2dSJohn Marino   make_cleanup_restore_ui_file (&gdb_stdout);
530cf7f2e2dSJohn Marino   make_cleanup_restore_ui_file (&gdb_stderr);
531c50c785cSJohn Marino   make_cleanup_restore_ui_file (&gdb_stdlog);
532c50c785cSJohn Marino   make_cleanup_restore_ui_file (&gdb_stdtarg);
533c50c785cSJohn Marino   make_cleanup_restore_ui_file (&gdb_stdtargerr);
534c50c785cSJohn Marino 
535a45ae5f8SJohn Marino   if (ui_out_redirect (current_uiout, str_file) < 0)
536c50c785cSJohn Marino     warning (_("Current output protocol does not support redirection"));
537c50c785cSJohn Marino   else
538a45ae5f8SJohn Marino     make_cleanup_ui_out_redirect_pop (current_uiout);
539cf7f2e2dSJohn Marino 
540cf7f2e2dSJohn Marino   gdb_stdout = str_file;
541cf7f2e2dSJohn Marino   gdb_stderr = str_file;
542c50c785cSJohn Marino   gdb_stdlog = str_file;
543c50c785cSJohn Marino   gdb_stdtarg = str_file;
544c50c785cSJohn Marino   gdb_stdtargerr = str_file;
545cf7f2e2dSJohn Marino 
546cf7f2e2dSJohn Marino   execute_command (p, from_tty);
547cf7f2e2dSJohn Marino 
548cf7f2e2dSJohn Marino   retval = ui_file_xstrdup (str_file, NULL);
549cf7f2e2dSJohn Marino 
550cf7f2e2dSJohn Marino   do_cleanups (cleanup);
551cf7f2e2dSJohn Marino 
552cf7f2e2dSJohn Marino   return retval;
553cf7f2e2dSJohn Marino }
554cf7f2e2dSJohn Marino 
5555796c8dcSSimon Schubert /* Read commands from `instream' and execute them
5565796c8dcSSimon Schubert    until end of file or error reading instream.  */
5575796c8dcSSimon Schubert 
5585796c8dcSSimon Schubert void
command_loop(void)5595796c8dcSSimon Schubert command_loop (void)
5605796c8dcSSimon Schubert {
5615796c8dcSSimon Schubert   struct cleanup *old_chain;
5625796c8dcSSimon Schubert   char *command;
5635796c8dcSSimon Schubert   int stdin_is_tty = ISATTY (stdin);
5645796c8dcSSimon Schubert 
5655796c8dcSSimon Schubert   while (instream && !feof (instream))
5665796c8dcSSimon Schubert     {
5675796c8dcSSimon Schubert       if (window_hook && instream == stdin)
5685796c8dcSSimon Schubert 	(*window_hook) (instream, get_prompt ());
5695796c8dcSSimon Schubert 
570*ef5ccd6cSJohn Marino       clear_quit_flag ();
5715796c8dcSSimon Schubert       if (instream == stdin && stdin_is_tty)
5725796c8dcSSimon Schubert 	reinitialize_more_filter ();
5735796c8dcSSimon Schubert       old_chain = make_cleanup (null_cleanup, 0);
5745796c8dcSSimon Schubert 
5755796c8dcSSimon Schubert       /* Get a command-line.  This calls the readline package.  */
5765796c8dcSSimon Schubert       command = command_line_input (instream == stdin ?
5775796c8dcSSimon Schubert 				    get_prompt () : (char *) NULL,
5785796c8dcSSimon Schubert 				    instream == stdin, "prompt");
5795796c8dcSSimon Schubert       if (command == 0)
580a45ae5f8SJohn Marino 	{
581a45ae5f8SJohn Marino 	  do_cleanups (old_chain);
5825796c8dcSSimon Schubert 	  return;
583a45ae5f8SJohn Marino 	}
5845796c8dcSSimon Schubert 
585cf7f2e2dSJohn Marino       make_command_stats_cleanup (1);
5865796c8dcSSimon Schubert 
5875796c8dcSSimon Schubert       execute_command (command, instream == stdin);
5885796c8dcSSimon Schubert 
5895796c8dcSSimon Schubert       /* Do any commands attached to breakpoint we are stopped at.  */
5905796c8dcSSimon Schubert       bpstat_do_actions ();
5915796c8dcSSimon Schubert 
5925796c8dcSSimon Schubert       do_cleanups (old_chain);
5935796c8dcSSimon Schubert     }
5945796c8dcSSimon Schubert }
5955796c8dcSSimon Schubert 
596c50c785cSJohn Marino /* When nonzero, cause dont_repeat to do nothing.  This should only be
597c50c785cSJohn Marino    set via prevent_dont_repeat.  */
598c50c785cSJohn Marino 
599c50c785cSJohn Marino static int suppress_dont_repeat = 0;
600c50c785cSJohn Marino 
6015796c8dcSSimon Schubert /* Commands call this if they do not want to be repeated by null lines.  */
6025796c8dcSSimon Schubert 
6035796c8dcSSimon Schubert void
dont_repeat(void)6045796c8dcSSimon Schubert dont_repeat (void)
6055796c8dcSSimon Schubert {
606c50c785cSJohn Marino   if (suppress_dont_repeat || server_command)
6075796c8dcSSimon Schubert     return;
6085796c8dcSSimon Schubert 
6095796c8dcSSimon Schubert   /* If we aren't reading from standard input, we are saving the last
610c50c785cSJohn Marino      thing read from stdin in line and don't want to delete it.  Null
611c50c785cSJohn Marino      lines won't repeat here in any case.  */
6125796c8dcSSimon Schubert   if (instream == stdin)
613a45ae5f8SJohn Marino     *saved_command_line = 0;
6145796c8dcSSimon Schubert }
615c50c785cSJohn Marino 
616c50c785cSJohn Marino /* Prevent dont_repeat from working, and return a cleanup that
617c50c785cSJohn Marino    restores the previous state.  */
618c50c785cSJohn Marino 
619c50c785cSJohn Marino struct cleanup *
prevent_dont_repeat(void)620c50c785cSJohn Marino prevent_dont_repeat (void)
621c50c785cSJohn Marino {
622c50c785cSJohn Marino   struct cleanup *result = make_cleanup_restore_integer (&suppress_dont_repeat);
623c50c785cSJohn Marino 
624c50c785cSJohn Marino   suppress_dont_repeat = 1;
625c50c785cSJohn Marino   return result;
626c50c785cSJohn Marino }
627c50c785cSJohn Marino 
6285796c8dcSSimon Schubert 
6295796c8dcSSimon Schubert /* Read a line from the stream "instream" without command line editing.
6305796c8dcSSimon Schubert 
6315796c8dcSSimon Schubert    It prints PROMPT_ARG once at the start.
6325796c8dcSSimon Schubert    Action is compatible with "readline", e.g. space for the result is
6335796c8dcSSimon Schubert    malloc'd and should be freed by the caller.
6345796c8dcSSimon Schubert 
6355796c8dcSSimon Schubert    A NULL return means end of file.  */
6365796c8dcSSimon Schubert char *
gdb_readline(char * prompt_arg)6375796c8dcSSimon Schubert gdb_readline (char *prompt_arg)
6385796c8dcSSimon Schubert {
6395796c8dcSSimon Schubert   int c;
6405796c8dcSSimon Schubert   char *result;
6415796c8dcSSimon Schubert   int input_index = 0;
6425796c8dcSSimon Schubert   int result_size = 80;
6435796c8dcSSimon Schubert 
6445796c8dcSSimon Schubert   if (prompt_arg)
6455796c8dcSSimon Schubert     {
6465796c8dcSSimon Schubert       /* Don't use a _filtered function here.  It causes the assumed
6475796c8dcSSimon Schubert          character position to be off, since the newline we read from
6485796c8dcSSimon Schubert          the user is not accounted for.  */
6495796c8dcSSimon Schubert       fputs_unfiltered (prompt_arg, gdb_stdout);
6505796c8dcSSimon Schubert       gdb_flush (gdb_stdout);
6515796c8dcSSimon Schubert     }
6525796c8dcSSimon Schubert 
6535796c8dcSSimon Schubert   result = (char *) xmalloc (result_size);
6545796c8dcSSimon Schubert 
6555796c8dcSSimon Schubert   while (1)
6565796c8dcSSimon Schubert     {
6575796c8dcSSimon Schubert       /* Read from stdin if we are executing a user defined command.
6585796c8dcSSimon Schubert          This is the right thing for prompt_for_continue, at least.  */
6595796c8dcSSimon Schubert       c = fgetc (instream ? instream : stdin);
6605796c8dcSSimon Schubert 
6615796c8dcSSimon Schubert       if (c == EOF)
6625796c8dcSSimon Schubert 	{
6635796c8dcSSimon Schubert 	  if (input_index > 0)
6645796c8dcSSimon Schubert 	    /* The last line does not end with a newline.  Return it, and
6655796c8dcSSimon Schubert 	       if we are called again fgetc will still return EOF and
6665796c8dcSSimon Schubert 	       we'll return NULL then.  */
6675796c8dcSSimon Schubert 	    break;
6685796c8dcSSimon Schubert 	  xfree (result);
6695796c8dcSSimon Schubert 	  return NULL;
6705796c8dcSSimon Schubert 	}
6715796c8dcSSimon Schubert 
6725796c8dcSSimon Schubert       if (c == '\n')
6735796c8dcSSimon Schubert 	{
6745796c8dcSSimon Schubert 	  if (input_index > 0 && result[input_index - 1] == '\r')
6755796c8dcSSimon Schubert 	    input_index--;
6765796c8dcSSimon Schubert 	  break;
6775796c8dcSSimon Schubert 	}
6785796c8dcSSimon Schubert 
6795796c8dcSSimon Schubert       result[input_index++] = c;
6805796c8dcSSimon Schubert       while (input_index >= result_size)
6815796c8dcSSimon Schubert 	{
6825796c8dcSSimon Schubert 	  result_size *= 2;
6835796c8dcSSimon Schubert 	  result = (char *) xrealloc (result, result_size);
6845796c8dcSSimon Schubert 	}
6855796c8dcSSimon Schubert     }
6865796c8dcSSimon Schubert 
6875796c8dcSSimon Schubert   result[input_index++] = '\0';
6885796c8dcSSimon Schubert   return result;
6895796c8dcSSimon Schubert }
6905796c8dcSSimon Schubert 
6915796c8dcSSimon Schubert /* Variables which control command line editing and history
6925796c8dcSSimon Schubert    substitution.  These variables are given default values at the end
6935796c8dcSSimon Schubert    of this file.  */
6945796c8dcSSimon Schubert static int command_editing_p;
6955796c8dcSSimon Schubert 
6965796c8dcSSimon Schubert /* NOTE 1999-04-29: This variable will be static again, once we modify
6975796c8dcSSimon Schubert    gdb to use the event loop as the default command loop and we merge
698c50c785cSJohn Marino    event-top.c into this file, top.c.  */
6995796c8dcSSimon Schubert 
7005796c8dcSSimon Schubert /* static */ int history_expansion_p;
7015796c8dcSSimon Schubert 
7025796c8dcSSimon Schubert static int write_history_p;
7035796c8dcSSimon Schubert static void
show_write_history_p(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)7045796c8dcSSimon Schubert show_write_history_p (struct ui_file *file, int from_tty,
7055796c8dcSSimon Schubert 		      struct cmd_list_element *c, const char *value)
7065796c8dcSSimon Schubert {
7075796c8dcSSimon Schubert   fprintf_filtered (file, _("Saving of the history record on exit is %s.\n"),
7085796c8dcSSimon Schubert 		    value);
7095796c8dcSSimon Schubert }
7105796c8dcSSimon Schubert 
711*ef5ccd6cSJohn Marino /* The variable associated with the "set/show history size"
712*ef5ccd6cSJohn Marino    command.  */
713*ef5ccd6cSJohn Marino static unsigned int history_size_setshow_var;
714*ef5ccd6cSJohn Marino 
7155796c8dcSSimon Schubert static void
show_history_size(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)7165796c8dcSSimon Schubert show_history_size (struct ui_file *file, int from_tty,
7175796c8dcSSimon Schubert 		   struct cmd_list_element *c, const char *value)
7185796c8dcSSimon Schubert {
7195796c8dcSSimon Schubert   fprintf_filtered (file, _("The size of the command history is %s.\n"),
7205796c8dcSSimon Schubert 		    value);
7215796c8dcSSimon Schubert }
7225796c8dcSSimon Schubert 
7235796c8dcSSimon Schubert static char *history_filename;
7245796c8dcSSimon Schubert static void
show_history_filename(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)7255796c8dcSSimon Schubert show_history_filename (struct ui_file *file, int from_tty,
7265796c8dcSSimon Schubert 		       struct cmd_list_element *c, const char *value)
7275796c8dcSSimon Schubert {
728c50c785cSJohn Marino   fprintf_filtered (file, _("The filename in which to record "
729c50c785cSJohn Marino 			    "the command history is \"%s\".\n"),
7305796c8dcSSimon Schubert 		    value);
7315796c8dcSSimon Schubert }
7325796c8dcSSimon Schubert 
7335796c8dcSSimon Schubert /* This is like readline(), but it has some gdb-specific behavior.
7345796c8dcSSimon Schubert    gdb may want readline in both the synchronous and async modes during
7355796c8dcSSimon Schubert    a single gdb invocation.  At the ordinary top-level prompt we might
7365796c8dcSSimon Schubert    be using the async readline.  That means we can't use
7375796c8dcSSimon Schubert    rl_pre_input_hook, since it doesn't work properly in async mode.
7385796c8dcSSimon Schubert    However, for a secondary prompt (" >", such as occurs during a
7395796c8dcSSimon Schubert    `define'), gdb wants a synchronous response.
7405796c8dcSSimon Schubert 
7415796c8dcSSimon Schubert    We used to call readline() directly, running it in synchronous
7425796c8dcSSimon Schubert    mode.  But mixing modes this way is not supported, and as of
7435796c8dcSSimon Schubert    readline 5.x it no longer works; the arrow keys come unbound during
7445796c8dcSSimon Schubert    the synchronous call.  So we make a nested call into the event
7455796c8dcSSimon Schubert    loop.  That's what gdb_readline_wrapper is for.  */
7465796c8dcSSimon Schubert 
7475796c8dcSSimon Schubert /* A flag set as soon as gdb_readline_wrapper_line is called; we can't
7485796c8dcSSimon Schubert    rely on gdb_readline_wrapper_result, which might still be NULL if
7495796c8dcSSimon Schubert    the user types Control-D for EOF.  */
7505796c8dcSSimon Schubert static int gdb_readline_wrapper_done;
7515796c8dcSSimon Schubert 
7525796c8dcSSimon Schubert /* The result of the current call to gdb_readline_wrapper, once a newline
7535796c8dcSSimon Schubert    is seen.  */
7545796c8dcSSimon Schubert static char *gdb_readline_wrapper_result;
7555796c8dcSSimon Schubert 
7565796c8dcSSimon Schubert /* Any intercepted hook.  Operate-and-get-next sets this, expecting it
7575796c8dcSSimon Schubert    to be called after the newline is processed (which will redisplay
7585796c8dcSSimon Schubert    the prompt).  But in gdb_readline_wrapper we will not get a new
7595796c8dcSSimon Schubert    prompt until the next call, or until we return to the event loop.
7605796c8dcSSimon Schubert    So we disable this hook around the newline and restore it before we
7615796c8dcSSimon Schubert    return.  */
7625796c8dcSSimon Schubert static void (*saved_after_char_processing_hook) (void);
7635796c8dcSSimon Schubert 
7645796c8dcSSimon Schubert /* This function is called when readline has seen a complete line of
7655796c8dcSSimon Schubert    text.  */
7665796c8dcSSimon Schubert 
7675796c8dcSSimon Schubert static void
gdb_readline_wrapper_line(char * line)7685796c8dcSSimon Schubert gdb_readline_wrapper_line (char *line)
7695796c8dcSSimon Schubert {
7705796c8dcSSimon Schubert   gdb_assert (!gdb_readline_wrapper_done);
7715796c8dcSSimon Schubert   gdb_readline_wrapper_result = line;
7725796c8dcSSimon Schubert   gdb_readline_wrapper_done = 1;
7735796c8dcSSimon Schubert 
7745796c8dcSSimon Schubert   /* Prevent operate-and-get-next from acting too early.  */
7755796c8dcSSimon Schubert   saved_after_char_processing_hook = after_char_processing_hook;
7765796c8dcSSimon Schubert   after_char_processing_hook = NULL;
7775796c8dcSSimon Schubert 
7785796c8dcSSimon Schubert   /* Prevent parts of the prompt from being redisplayed if annotations
7795796c8dcSSimon Schubert      are enabled, and readline's state getting out of sync.  */
7805796c8dcSSimon Schubert   if (async_command_editing_p)
7815796c8dcSSimon Schubert     rl_callback_handler_remove ();
7825796c8dcSSimon Schubert }
7835796c8dcSSimon Schubert 
7845796c8dcSSimon Schubert struct gdb_readline_wrapper_cleanup
7855796c8dcSSimon Schubert   {
7865796c8dcSSimon Schubert     void (*handler_orig) (char *);
7875796c8dcSSimon Schubert     int already_prompted_orig;
7885796c8dcSSimon Schubert   };
7895796c8dcSSimon Schubert 
7905796c8dcSSimon Schubert static void
gdb_readline_wrapper_cleanup(void * arg)7915796c8dcSSimon Schubert gdb_readline_wrapper_cleanup (void *arg)
7925796c8dcSSimon Schubert {
7935796c8dcSSimon Schubert   struct gdb_readline_wrapper_cleanup *cleanup = arg;
7945796c8dcSSimon Schubert 
7955796c8dcSSimon Schubert   rl_already_prompted = cleanup->already_prompted_orig;
7965796c8dcSSimon Schubert 
7975796c8dcSSimon Schubert   gdb_assert (input_handler == gdb_readline_wrapper_line);
7985796c8dcSSimon Schubert   input_handler = cleanup->handler_orig;
7995796c8dcSSimon Schubert   gdb_readline_wrapper_result = NULL;
8005796c8dcSSimon Schubert   gdb_readline_wrapper_done = 0;
8015796c8dcSSimon Schubert 
8025796c8dcSSimon Schubert   after_char_processing_hook = saved_after_char_processing_hook;
8035796c8dcSSimon Schubert   saved_after_char_processing_hook = NULL;
8045796c8dcSSimon Schubert 
8055796c8dcSSimon Schubert   xfree (cleanup);
8065796c8dcSSimon Schubert }
8075796c8dcSSimon Schubert 
8085796c8dcSSimon Schubert char *
gdb_readline_wrapper(char * prompt)8095796c8dcSSimon Schubert gdb_readline_wrapper (char *prompt)
8105796c8dcSSimon Schubert {
8115796c8dcSSimon Schubert   struct cleanup *back_to;
8125796c8dcSSimon Schubert   struct gdb_readline_wrapper_cleanup *cleanup;
8135796c8dcSSimon Schubert   char *retval;
8145796c8dcSSimon Schubert 
8155796c8dcSSimon Schubert   cleanup = xmalloc (sizeof (*cleanup));
8165796c8dcSSimon Schubert   cleanup->handler_orig = input_handler;
8175796c8dcSSimon Schubert   input_handler = gdb_readline_wrapper_line;
8185796c8dcSSimon Schubert 
8195796c8dcSSimon Schubert   cleanup->already_prompted_orig = rl_already_prompted;
8205796c8dcSSimon Schubert 
8215796c8dcSSimon Schubert   back_to = make_cleanup (gdb_readline_wrapper_cleanup, cleanup);
8225796c8dcSSimon Schubert 
8235796c8dcSSimon Schubert   /* Display our prompt and prevent double prompt display.  */
8245796c8dcSSimon Schubert   display_gdb_prompt (prompt);
8255796c8dcSSimon Schubert   rl_already_prompted = 1;
8265796c8dcSSimon Schubert 
8275796c8dcSSimon Schubert   if (after_char_processing_hook)
8285796c8dcSSimon Schubert     (*after_char_processing_hook) ();
8295796c8dcSSimon Schubert   gdb_assert (after_char_processing_hook == NULL);
8305796c8dcSSimon Schubert 
831a45ae5f8SJohn Marino   while (gdb_do_one_event () >= 0)
8325796c8dcSSimon Schubert     if (gdb_readline_wrapper_done)
8335796c8dcSSimon Schubert       break;
8345796c8dcSSimon Schubert 
8355796c8dcSSimon Schubert   retval = gdb_readline_wrapper_result;
8365796c8dcSSimon Schubert   do_cleanups (back_to);
8375796c8dcSSimon Schubert   return retval;
8385796c8dcSSimon Schubert }
8395796c8dcSSimon Schubert 
8405796c8dcSSimon Schubert 
8415796c8dcSSimon Schubert /* The current saved history number from operate-and-get-next.
8425796c8dcSSimon Schubert    This is -1 if not valid.  */
8435796c8dcSSimon Schubert static int operate_saved_history = -1;
8445796c8dcSSimon Schubert 
8455796c8dcSSimon Schubert /* This is put on the appropriate hook and helps operate-and-get-next
8465796c8dcSSimon Schubert    do its work.  */
8475796c8dcSSimon Schubert static void
gdb_rl_operate_and_get_next_completion(void)8485796c8dcSSimon Schubert gdb_rl_operate_and_get_next_completion (void)
8495796c8dcSSimon Schubert {
8505796c8dcSSimon Schubert   int delta = where_history () - operate_saved_history;
851cf7f2e2dSJohn Marino 
8525796c8dcSSimon Schubert   /* The `key' argument to rl_get_previous_history is ignored.  */
8535796c8dcSSimon Schubert   rl_get_previous_history (delta, 0);
8545796c8dcSSimon Schubert   operate_saved_history = -1;
8555796c8dcSSimon Schubert 
8565796c8dcSSimon Schubert   /* readline doesn't automatically update the display for us.  */
8575796c8dcSSimon Schubert   rl_redisplay ();
8585796c8dcSSimon Schubert 
8595796c8dcSSimon Schubert   after_char_processing_hook = NULL;
8605796c8dcSSimon Schubert   rl_pre_input_hook = NULL;
8615796c8dcSSimon Schubert }
8625796c8dcSSimon Schubert 
8635796c8dcSSimon Schubert /* This is a gdb-local readline command handler.  It accepts the
8645796c8dcSSimon Schubert    current command line (like RET does) and, if this command was taken
8655796c8dcSSimon Schubert    from the history, arranges for the next command in the history to
8665796c8dcSSimon Schubert    appear on the command line when the prompt returns.
8675796c8dcSSimon Schubert    We ignore the arguments.  */
8685796c8dcSSimon Schubert static int
gdb_rl_operate_and_get_next(int count,int key)8695796c8dcSSimon Schubert gdb_rl_operate_and_get_next (int count, int key)
8705796c8dcSSimon Schubert {
8715796c8dcSSimon Schubert   int where;
8725796c8dcSSimon Schubert 
8735796c8dcSSimon Schubert   /* Use the async hook.  */
8745796c8dcSSimon Schubert   after_char_processing_hook = gdb_rl_operate_and_get_next_completion;
8755796c8dcSSimon Schubert 
8765796c8dcSSimon Schubert   /* Find the current line, and find the next line to use.  */
8775796c8dcSSimon Schubert   where = where_history();
8785796c8dcSSimon Schubert 
8795796c8dcSSimon Schubert   /* FIXME: kettenis/20020817: max_input_history is renamed into
8805796c8dcSSimon Schubert      history_max_entries in readline-4.2.  When we do a new readline
8815796c8dcSSimon Schubert      import, we should probably change it here too, even though
8825796c8dcSSimon Schubert      readline maintains backwards compatibility for now by still
8835796c8dcSSimon Schubert      defining max_input_history.  */
8845796c8dcSSimon Schubert   if ((history_is_stifled () && (history_length >= max_input_history)) ||
8855796c8dcSSimon Schubert       (where >= history_length - 1))
8865796c8dcSSimon Schubert     operate_saved_history = where;
8875796c8dcSSimon Schubert   else
8885796c8dcSSimon Schubert     operate_saved_history = where + 1;
8895796c8dcSSimon Schubert 
8905796c8dcSSimon Schubert   return rl_newline (1, key);
8915796c8dcSSimon Schubert }
8925796c8dcSSimon Schubert 
8935796c8dcSSimon Schubert /* Read one line from the command input stream `instream'
8945796c8dcSSimon Schubert    into the local static buffer `linebuffer' (whose current length
8955796c8dcSSimon Schubert    is `linelength').
8965796c8dcSSimon Schubert    The buffer is made bigger as necessary.
8975796c8dcSSimon Schubert    Returns the address of the start of the line.
8985796c8dcSSimon Schubert 
8995796c8dcSSimon Schubert    NULL is returned for end of file.
9005796c8dcSSimon Schubert 
9015796c8dcSSimon Schubert    *If* the instream == stdin & stdin is a terminal, the line read
9025796c8dcSSimon Schubert    is copied into the file line saver (global var char *line,
9035796c8dcSSimon Schubert    length linesize) so that it can be duplicated.
9045796c8dcSSimon Schubert 
9055796c8dcSSimon Schubert    This routine either uses fancy command line editing or
9065796c8dcSSimon Schubert    simple input as the user has requested.  */
9075796c8dcSSimon Schubert 
9085796c8dcSSimon Schubert char *
command_line_input(char * prompt_arg,int repeat,char * annotation_suffix)9095796c8dcSSimon Schubert command_line_input (char *prompt_arg, int repeat, char *annotation_suffix)
9105796c8dcSSimon Schubert {
9115796c8dcSSimon Schubert   static char *linebuffer = 0;
9125796c8dcSSimon Schubert   static unsigned linelength = 0;
9135796c8dcSSimon Schubert   char *p;
9145796c8dcSSimon Schubert   char *p1;
9155796c8dcSSimon Schubert   char *rl;
9165796c8dcSSimon Schubert   char *local_prompt = prompt_arg;
9175796c8dcSSimon Schubert   char *nline;
9185796c8dcSSimon Schubert   char got_eof = 0;
9195796c8dcSSimon Schubert 
9205796c8dcSSimon Schubert   /* The annotation suffix must be non-NULL.  */
9215796c8dcSSimon Schubert   if (annotation_suffix == NULL)
9225796c8dcSSimon Schubert     annotation_suffix = "";
9235796c8dcSSimon Schubert 
9245796c8dcSSimon Schubert   if (annotation_level > 1 && instream == stdin)
9255796c8dcSSimon Schubert     {
9265796c8dcSSimon Schubert       local_prompt = alloca ((prompt_arg == NULL ? 0 : strlen (prompt_arg))
9275796c8dcSSimon Schubert 			     + strlen (annotation_suffix) + 40);
9285796c8dcSSimon Schubert       if (prompt_arg == NULL)
9295796c8dcSSimon Schubert 	local_prompt[0] = '\0';
9305796c8dcSSimon Schubert       else
9315796c8dcSSimon Schubert 	strcpy (local_prompt, prompt_arg);
9325796c8dcSSimon Schubert       strcat (local_prompt, "\n\032\032");
9335796c8dcSSimon Schubert       strcat (local_prompt, annotation_suffix);
9345796c8dcSSimon Schubert       strcat (local_prompt, "\n");
9355796c8dcSSimon Schubert     }
9365796c8dcSSimon Schubert 
9375796c8dcSSimon Schubert   if (linebuffer == 0)
9385796c8dcSSimon Schubert     {
9395796c8dcSSimon Schubert       linelength = 80;
9405796c8dcSSimon Schubert       linebuffer = (char *) xmalloc (linelength);
9415796c8dcSSimon Schubert     }
9425796c8dcSSimon Schubert 
9435796c8dcSSimon Schubert   p = linebuffer;
9445796c8dcSSimon Schubert 
9455796c8dcSSimon Schubert   /* Control-C quits instantly if typed while in this loop
9465796c8dcSSimon Schubert      since it should not wait until the user types a newline.  */
9475796c8dcSSimon Schubert   immediate_quit++;
948*ef5ccd6cSJohn Marino   QUIT;
9495796c8dcSSimon Schubert #ifdef STOP_SIGNAL
9505796c8dcSSimon Schubert   if (job_control)
9515796c8dcSSimon Schubert     signal (STOP_SIGNAL, handle_stop_sig);
9525796c8dcSSimon Schubert #endif
9535796c8dcSSimon Schubert 
9545796c8dcSSimon Schubert   while (1)
9555796c8dcSSimon Schubert     {
956c50c785cSJohn Marino       /* Make sure that all output has been output.  Some machines may
957c50c785cSJohn Marino          let you get away with leaving out some of the gdb_flush, but
958c50c785cSJohn Marino          not all.  */
9595796c8dcSSimon Schubert       wrap_here ("");
9605796c8dcSSimon Schubert       gdb_flush (gdb_stdout);
9615796c8dcSSimon Schubert       gdb_flush (gdb_stderr);
9625796c8dcSSimon Schubert 
9635796c8dcSSimon Schubert       if (source_file_name != NULL)
9645796c8dcSSimon Schubert 	++source_line_number;
9655796c8dcSSimon Schubert 
9665796c8dcSSimon Schubert       if (annotation_level > 1 && instream == stdin)
9675796c8dcSSimon Schubert 	{
9685796c8dcSSimon Schubert 	  puts_unfiltered ("\n\032\032pre-");
9695796c8dcSSimon Schubert 	  puts_unfiltered (annotation_suffix);
9705796c8dcSSimon Schubert 	  puts_unfiltered ("\n");
9715796c8dcSSimon Schubert 	}
9725796c8dcSSimon Schubert 
9735796c8dcSSimon Schubert       /* Don't use fancy stuff if not talking to stdin.  */
9745796c8dcSSimon Schubert       if (deprecated_readline_hook && input_from_terminal_p ())
9755796c8dcSSimon Schubert 	{
9765796c8dcSSimon Schubert 	  rl = (*deprecated_readline_hook) (local_prompt);
9775796c8dcSSimon Schubert 	}
9785796c8dcSSimon Schubert       else if (command_editing_p && input_from_terminal_p ())
9795796c8dcSSimon Schubert 	{
9805796c8dcSSimon Schubert 	  rl = gdb_readline_wrapper (local_prompt);
9815796c8dcSSimon Schubert 	}
9825796c8dcSSimon Schubert       else
9835796c8dcSSimon Schubert 	{
9845796c8dcSSimon Schubert 	  rl = gdb_readline (local_prompt);
9855796c8dcSSimon Schubert 	}
9865796c8dcSSimon Schubert 
9875796c8dcSSimon Schubert       if (annotation_level > 1 && instream == stdin)
9885796c8dcSSimon Schubert 	{
9895796c8dcSSimon Schubert 	  puts_unfiltered ("\n\032\032post-");
9905796c8dcSSimon Schubert 	  puts_unfiltered (annotation_suffix);
9915796c8dcSSimon Schubert 	  puts_unfiltered ("\n");
9925796c8dcSSimon Schubert 	}
9935796c8dcSSimon Schubert 
9945796c8dcSSimon Schubert       if (!rl || rl == (char *) EOF)
9955796c8dcSSimon Schubert 	{
9965796c8dcSSimon Schubert 	  got_eof = 1;
9975796c8dcSSimon Schubert 	  break;
9985796c8dcSSimon Schubert 	}
9995796c8dcSSimon Schubert       if (strlen (rl) + 1 + (p - linebuffer) > linelength)
10005796c8dcSSimon Schubert 	{
10015796c8dcSSimon Schubert 	  linelength = strlen (rl) + 1 + (p - linebuffer);
10025796c8dcSSimon Schubert 	  nline = (char *) xrealloc (linebuffer, linelength);
10035796c8dcSSimon Schubert 	  p += nline - linebuffer;
10045796c8dcSSimon Schubert 	  linebuffer = nline;
10055796c8dcSSimon Schubert 	}
10065796c8dcSSimon Schubert       p1 = rl;
10075796c8dcSSimon Schubert       /* Copy line.  Don't copy null at end.  (Leaves line alone
1008c50c785cSJohn Marino          if this was just a newline).  */
10095796c8dcSSimon Schubert       while (*p1)
10105796c8dcSSimon Schubert 	*p++ = *p1++;
10115796c8dcSSimon Schubert 
10125796c8dcSSimon Schubert       xfree (rl);		/* Allocated in readline.  */
10135796c8dcSSimon Schubert 
10145796c8dcSSimon Schubert       if (p == linebuffer || *(p - 1) != '\\')
10155796c8dcSSimon Schubert 	break;
10165796c8dcSSimon Schubert 
10175796c8dcSSimon Schubert       p--;			/* Put on top of '\'.  */
10185796c8dcSSimon Schubert       local_prompt = (char *) 0;
10195796c8dcSSimon Schubert     }
10205796c8dcSSimon Schubert 
10215796c8dcSSimon Schubert #ifdef STOP_SIGNAL
10225796c8dcSSimon Schubert   if (job_control)
10235796c8dcSSimon Schubert     signal (STOP_SIGNAL, SIG_DFL);
10245796c8dcSSimon Schubert #endif
10255796c8dcSSimon Schubert   immediate_quit--;
10265796c8dcSSimon Schubert 
10275796c8dcSSimon Schubert   if (got_eof)
10285796c8dcSSimon Schubert     return NULL;
10295796c8dcSSimon Schubert 
10305796c8dcSSimon Schubert #define SERVER_COMMAND_LENGTH 7
10315796c8dcSSimon Schubert   server_command =
10325796c8dcSSimon Schubert     (p - linebuffer > SERVER_COMMAND_LENGTH)
10335796c8dcSSimon Schubert     && strncmp (linebuffer, "server ", SERVER_COMMAND_LENGTH) == 0;
10345796c8dcSSimon Schubert   if (server_command)
10355796c8dcSSimon Schubert     {
10365796c8dcSSimon Schubert       /* Note that we don't set `line'.  Between this and the check in
10375796c8dcSSimon Schubert          dont_repeat, this insures that repeating will still do the
10385796c8dcSSimon Schubert          right thing.  */
10395796c8dcSSimon Schubert       *p = '\0';
10405796c8dcSSimon Schubert       return linebuffer + SERVER_COMMAND_LENGTH;
10415796c8dcSSimon Schubert     }
10425796c8dcSSimon Schubert 
10435796c8dcSSimon Schubert   /* Do history expansion if that is wished.  */
10445796c8dcSSimon Schubert   if (history_expansion_p && instream == stdin
10455796c8dcSSimon Schubert       && ISATTY (instream))
10465796c8dcSSimon Schubert     {
10475796c8dcSSimon Schubert       char *history_value;
10485796c8dcSSimon Schubert       int expanded;
10495796c8dcSSimon Schubert 
10505796c8dcSSimon Schubert       *p = '\0';		/* Insert null now.  */
10515796c8dcSSimon Schubert       expanded = history_expand (linebuffer, &history_value);
10525796c8dcSSimon Schubert       if (expanded)
10535796c8dcSSimon Schubert 	{
10545796c8dcSSimon Schubert 	  /* Print the changes.  */
10555796c8dcSSimon Schubert 	  printf_unfiltered ("%s\n", history_value);
10565796c8dcSSimon Schubert 
10575796c8dcSSimon Schubert 	  /* If there was an error, call this function again.  */
10585796c8dcSSimon Schubert 	  if (expanded < 0)
10595796c8dcSSimon Schubert 	    {
10605796c8dcSSimon Schubert 	      xfree (history_value);
1061c50c785cSJohn Marino 	      return command_line_input (prompt_arg, repeat,
1062c50c785cSJohn Marino 					 annotation_suffix);
10635796c8dcSSimon Schubert 	    }
10645796c8dcSSimon Schubert 	  if (strlen (history_value) > linelength)
10655796c8dcSSimon Schubert 	    {
10665796c8dcSSimon Schubert 	      linelength = strlen (history_value) + 1;
10675796c8dcSSimon Schubert 	      linebuffer = (char *) xrealloc (linebuffer, linelength);
10685796c8dcSSimon Schubert 	    }
10695796c8dcSSimon Schubert 	  strcpy (linebuffer, history_value);
10705796c8dcSSimon Schubert 	  p = linebuffer + strlen (linebuffer);
10715796c8dcSSimon Schubert 	}
10725796c8dcSSimon Schubert       xfree (history_value);
10735796c8dcSSimon Schubert     }
10745796c8dcSSimon Schubert 
1075c50c785cSJohn Marino   /* If we just got an empty line, and that is supposed to repeat the
1076c50c785cSJohn Marino      previous command, return the value in the global buffer.  */
10775796c8dcSSimon Schubert   if (repeat && p == linebuffer)
1078a45ae5f8SJohn Marino     return saved_command_line;
10795796c8dcSSimon Schubert   for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++);
10805796c8dcSSimon Schubert   if (repeat && !*p1)
1081a45ae5f8SJohn Marino     return saved_command_line;
10825796c8dcSSimon Schubert 
10835796c8dcSSimon Schubert   *p = 0;
10845796c8dcSSimon Schubert 
10855796c8dcSSimon Schubert   /* Add line to history if appropriate.  */
10865796c8dcSSimon Schubert   if (instream == stdin
10875796c8dcSSimon Schubert       && ISATTY (stdin) && *linebuffer)
10885796c8dcSSimon Schubert     add_history (linebuffer);
10895796c8dcSSimon Schubert 
10905796c8dcSSimon Schubert   /* Note: lines consisting solely of comments are added to the command
10915796c8dcSSimon Schubert      history.  This is useful when you type a command, and then
10925796c8dcSSimon Schubert      realize you don't want to execute it quite yet.  You can comment
10935796c8dcSSimon Schubert      out the command and then later fetch it from the value history
10945796c8dcSSimon Schubert      and remove the '#'.  The kill ring is probably better, but some
10955796c8dcSSimon Schubert      people are in the habit of commenting things out.  */
10965796c8dcSSimon Schubert   if (*p1 == '#')
10975796c8dcSSimon Schubert     *p1 = '\0';			/* Found a comment.  */
10985796c8dcSSimon Schubert 
10995796c8dcSSimon Schubert   /* Save into global buffer if appropriate.  */
11005796c8dcSSimon Schubert   if (repeat)
11015796c8dcSSimon Schubert     {
1102a45ae5f8SJohn Marino       if (linelength > saved_command_line_size)
11035796c8dcSSimon Schubert 	{
1104a45ae5f8SJohn Marino 	  saved_command_line = xrealloc (saved_command_line, linelength);
1105a45ae5f8SJohn Marino 	  saved_command_line_size = linelength;
11065796c8dcSSimon Schubert 	}
1107a45ae5f8SJohn Marino       strcpy (saved_command_line, linebuffer);
1108a45ae5f8SJohn Marino       return saved_command_line;
11095796c8dcSSimon Schubert     }
11105796c8dcSSimon Schubert 
11115796c8dcSSimon Schubert   return linebuffer;
11125796c8dcSSimon Schubert }
11135796c8dcSSimon Schubert 
11145796c8dcSSimon Schubert /* Print the GDB banner.  */
11155796c8dcSSimon Schubert void
print_gdb_version(struct ui_file * stream)11165796c8dcSSimon Schubert print_gdb_version (struct ui_file *stream)
11175796c8dcSSimon Schubert {
11185796c8dcSSimon Schubert   /* From GNU coding standards, first line is meant to be easy for a
11195796c8dcSSimon Schubert      program to parse, and is just canonical program name and version
11205796c8dcSSimon Schubert      number, which starts after last space.  */
11215796c8dcSSimon Schubert 
11225796c8dcSSimon Schubert   fprintf_filtered (stream, "GNU gdb %s%s\n", PKGVERSION, version);
11235796c8dcSSimon Schubert 
11245796c8dcSSimon Schubert   /* Second line is a copyright notice.  */
11255796c8dcSSimon Schubert 
1126c50c785cSJohn Marino   fprintf_filtered (stream,
1127*ef5ccd6cSJohn Marino 		    "Copyright (C) 2013 Free Software Foundation, Inc.\n");
11285796c8dcSSimon Schubert 
11295796c8dcSSimon Schubert   /* Following the copyright is a brief statement that the program is
11305796c8dcSSimon Schubert      free software, that users are free to copy and change it on
11315796c8dcSSimon Schubert      certain conditions, that it is covered by the GNU GPL, and that
11325796c8dcSSimon Schubert      there is no warranty.  */
11335796c8dcSSimon Schubert 
11345796c8dcSSimon Schubert   fprintf_filtered (stream, "\
1135c50c785cSJohn Marino License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\
1136c50c785cSJohn Marino \nThis is free software: you are free to change and redistribute it.\n\
11375796c8dcSSimon Schubert There is NO WARRANTY, to the extent permitted by law.  Type \"show copying\"\n\
11385796c8dcSSimon Schubert and \"show warranty\" for details.\n");
11395796c8dcSSimon Schubert 
11405796c8dcSSimon Schubert   /* After the required info we print the configuration information.  */
11415796c8dcSSimon Schubert 
11425796c8dcSSimon Schubert   fprintf_filtered (stream, "This GDB was configured as \"");
11435796c8dcSSimon Schubert   if (strcmp (host_name, target_name) != 0)
11445796c8dcSSimon Schubert     {
1145c50c785cSJohn Marino       fprintf_filtered (stream, "--host=%s --target=%s",
1146c50c785cSJohn Marino 			host_name, target_name);
11475796c8dcSSimon Schubert     }
11485796c8dcSSimon Schubert   else
11495796c8dcSSimon Schubert     {
11505796c8dcSSimon Schubert       fprintf_filtered (stream, "%s", host_name);
11515796c8dcSSimon Schubert     }
11525796c8dcSSimon Schubert   fprintf_filtered (stream, "\".");
11535796c8dcSSimon Schubert 
11545796c8dcSSimon Schubert   if (REPORT_BUGS_TO[0])
11555796c8dcSSimon Schubert     {
11565796c8dcSSimon Schubert       fprintf_filtered (stream,
11575796c8dcSSimon Schubert 			_("\nFor bug reporting instructions, please see:\n"));
11585796c8dcSSimon Schubert       fprintf_filtered (stream, "%s.", REPORT_BUGS_TO);
11595796c8dcSSimon Schubert     }
11605796c8dcSSimon Schubert }
11615796c8dcSSimon Schubert 
1162a45ae5f8SJohn Marino 
1163a45ae5f8SJohn Marino /* The current top level prompt, settable with "set prompt", and/or
1164a45ae5f8SJohn Marino    with the python `gdb.prompt_hook' hook.  */
1165a45ae5f8SJohn Marino static char *top_prompt;
1166a45ae5f8SJohn Marino 
1167a45ae5f8SJohn Marino /* Access method for the GDB prompt string.  */
11685796c8dcSSimon Schubert 
11695796c8dcSSimon Schubert char *
get_prompt(void)11705796c8dcSSimon Schubert get_prompt (void)
11715796c8dcSSimon Schubert {
1172a45ae5f8SJohn Marino   return top_prompt;
11735796c8dcSSimon Schubert }
11745796c8dcSSimon Schubert 
1175a45ae5f8SJohn Marino /* Set method for the GDB prompt string.  */
1176a45ae5f8SJohn Marino 
11775796c8dcSSimon Schubert void
set_prompt(const char * s)1178a45ae5f8SJohn Marino set_prompt (const char *s)
11795796c8dcSSimon Schubert {
1180a45ae5f8SJohn Marino   char *p = xstrdup (s);
1181a45ae5f8SJohn Marino 
1182a45ae5f8SJohn Marino   xfree (top_prompt);
1183a45ae5f8SJohn Marino   top_prompt = p;
11845796c8dcSSimon Schubert }
11855796c8dcSSimon Schubert 
11865796c8dcSSimon Schubert 
11875796c8dcSSimon Schubert struct qt_args
11885796c8dcSSimon Schubert {
11895796c8dcSSimon Schubert   char *args;
11905796c8dcSSimon Schubert   int from_tty;
11915796c8dcSSimon Schubert };
11925796c8dcSSimon Schubert 
11935796c8dcSSimon Schubert /* Callback for iterate_over_inferiors.  Kills or detaches the given
11945796c8dcSSimon Schubert    inferior, depending on how we originally gained control of it.  */
11955796c8dcSSimon Schubert 
11965796c8dcSSimon Schubert static int
kill_or_detach(struct inferior * inf,void * args)11975796c8dcSSimon Schubert kill_or_detach (struct inferior *inf, void *args)
11985796c8dcSSimon Schubert {
11995796c8dcSSimon Schubert   struct qt_args *qt = args;
12005796c8dcSSimon Schubert   struct thread_info *thread;
12015796c8dcSSimon Schubert 
1202cf7f2e2dSJohn Marino   if (inf->pid == 0)
1203cf7f2e2dSJohn Marino     return 0;
1204cf7f2e2dSJohn Marino 
12055796c8dcSSimon Schubert   thread = any_thread_of_process (inf->pid);
12065796c8dcSSimon Schubert   if (thread != NULL)
12075796c8dcSSimon Schubert     {
12085796c8dcSSimon Schubert       switch_to_thread (thread->ptid);
12095796c8dcSSimon Schubert 
12105796c8dcSSimon Schubert       /* Leave core files alone.  */
12115796c8dcSSimon Schubert       if (target_has_execution)
12125796c8dcSSimon Schubert 	{
12135796c8dcSSimon Schubert 	  if (inf->attach_flag)
12145796c8dcSSimon Schubert 	    target_detach (qt->args, qt->from_tty);
12155796c8dcSSimon Schubert 	  else
12165796c8dcSSimon Schubert 	    target_kill ();
12175796c8dcSSimon Schubert 	}
12185796c8dcSSimon Schubert     }
12195796c8dcSSimon Schubert 
12205796c8dcSSimon Schubert   return 0;
12215796c8dcSSimon Schubert }
12225796c8dcSSimon Schubert 
12235796c8dcSSimon Schubert /* Callback for iterate_over_inferiors.  Prints info about what GDB
12245796c8dcSSimon Schubert    will do to each inferior on a "quit".  ARG points to a struct
12255796c8dcSSimon Schubert    ui_out where output is to be collected.  */
12265796c8dcSSimon Schubert 
12275796c8dcSSimon Schubert static int
print_inferior_quit_action(struct inferior * inf,void * arg)12285796c8dcSSimon Schubert print_inferior_quit_action (struct inferior *inf, void *arg)
12295796c8dcSSimon Schubert {
12305796c8dcSSimon Schubert   struct ui_file *stb = arg;
12315796c8dcSSimon Schubert 
1232cf7f2e2dSJohn Marino   if (inf->pid == 0)
1233cf7f2e2dSJohn Marino     return 0;
1234cf7f2e2dSJohn Marino 
12355796c8dcSSimon Schubert   if (inf->attach_flag)
12365796c8dcSSimon Schubert     fprintf_filtered (stb,
12375796c8dcSSimon Schubert 		      _("\tInferior %d [%s] will be detached.\n"), inf->num,
12385796c8dcSSimon Schubert 		      target_pid_to_str (pid_to_ptid (inf->pid)));
12395796c8dcSSimon Schubert   else
12405796c8dcSSimon Schubert     fprintf_filtered (stb,
12415796c8dcSSimon Schubert 		      _("\tInferior %d [%s] will be killed.\n"), inf->num,
12425796c8dcSSimon Schubert 		      target_pid_to_str (pid_to_ptid (inf->pid)));
12435796c8dcSSimon Schubert 
12445796c8dcSSimon Schubert   return 0;
12455796c8dcSSimon Schubert }
12465796c8dcSSimon Schubert 
12475796c8dcSSimon Schubert /* If necessary, make the user confirm that we should quit.  Return
12485796c8dcSSimon Schubert    non-zero if we should quit, zero if we shouldn't.  */
12495796c8dcSSimon Schubert 
12505796c8dcSSimon Schubert int
quit_confirm(void)12515796c8dcSSimon Schubert quit_confirm (void)
12525796c8dcSSimon Schubert {
12535796c8dcSSimon Schubert   struct ui_file *stb;
12545796c8dcSSimon Schubert   struct cleanup *old_chain;
12555796c8dcSSimon Schubert   char *str;
12565796c8dcSSimon Schubert   int qr;
12575796c8dcSSimon Schubert 
12585796c8dcSSimon Schubert   /* Don't even ask if we're only debugging a core file inferior.  */
12595796c8dcSSimon Schubert   if (!have_live_inferiors ())
12605796c8dcSSimon Schubert     return 1;
12615796c8dcSSimon Schubert 
12625796c8dcSSimon Schubert   /* Build the query string as a single string.  */
12635796c8dcSSimon Schubert   stb = mem_fileopen ();
12645796c8dcSSimon Schubert   old_chain = make_cleanup_ui_file_delete (stb);
12655796c8dcSSimon Schubert 
12665796c8dcSSimon Schubert   /* This is something of a hack.  But there's no reliable way to see
12675796c8dcSSimon Schubert      if a GUI is running.  The `use_windows' variable doesn't cut
12685796c8dcSSimon Schubert      it.  */
12695796c8dcSSimon Schubert   if (deprecated_init_ui_hook)
12705796c8dcSSimon Schubert     fprintf_filtered (stb, _("A debugging session is active.\n"
12715796c8dcSSimon Schubert 			     "Do you still want to close the debugger?"));
12725796c8dcSSimon Schubert   else
12735796c8dcSSimon Schubert     {
12745796c8dcSSimon Schubert       fprintf_filtered (stb, _("A debugging session is active.\n\n"));
12755796c8dcSSimon Schubert       iterate_over_inferiors (print_inferior_quit_action, stb);
12765796c8dcSSimon Schubert       fprintf_filtered (stb, _("\nQuit anyway? "));
12775796c8dcSSimon Schubert     }
12785796c8dcSSimon Schubert 
12795796c8dcSSimon Schubert   str = ui_file_xstrdup (stb, NULL);
12805796c8dcSSimon Schubert   make_cleanup (xfree, str);
12815796c8dcSSimon Schubert 
12825796c8dcSSimon Schubert   qr = query ("%s", str);
12835796c8dcSSimon Schubert   do_cleanups (old_chain);
12845796c8dcSSimon Schubert   return qr;
12855796c8dcSSimon Schubert }
12865796c8dcSSimon Schubert 
12875796c8dcSSimon Schubert /* Helper routine for quit_force that requires error handling.  */
12885796c8dcSSimon Schubert 
12895796c8dcSSimon Schubert static int
quit_target(void * arg)12905796c8dcSSimon Schubert quit_target (void *arg)
12915796c8dcSSimon Schubert {
12925796c8dcSSimon Schubert   struct qt_args *qt = (struct qt_args *)arg;
12935796c8dcSSimon Schubert 
12945796c8dcSSimon Schubert   /* Kill or detach all inferiors.  */
12955796c8dcSSimon Schubert   iterate_over_inferiors (kill_or_detach, qt);
12965796c8dcSSimon Schubert 
12975796c8dcSSimon Schubert   /* Give all pushed targets a chance to do minimal cleanup, and pop
12985796c8dcSSimon Schubert      them all out.  */
12995796c8dcSSimon Schubert   pop_all_targets (1);
13005796c8dcSSimon Schubert 
13015796c8dcSSimon Schubert   /* Save the history information if it is appropriate to do so.  */
13025796c8dcSSimon Schubert   if (write_history_p && history_filename)
13035796c8dcSSimon Schubert     write_history (history_filename);
13045796c8dcSSimon Schubert 
1305*ef5ccd6cSJohn Marino   do_final_cleanups (all_cleanups ());    /* Do any final cleanups before
1306c50c785cSJohn Marino 					     exiting.  */
13075796c8dcSSimon Schubert   return 0;
13085796c8dcSSimon Schubert }
13095796c8dcSSimon Schubert 
13105796c8dcSSimon Schubert /* Quit without asking for confirmation.  */
13115796c8dcSSimon Schubert 
13125796c8dcSSimon Schubert void
quit_force(char * args,int from_tty)13135796c8dcSSimon Schubert quit_force (char *args, int from_tty)
13145796c8dcSSimon Schubert {
13155796c8dcSSimon Schubert   int exit_code = 0;
13165796c8dcSSimon Schubert   struct qt_args qt;
13175796c8dcSSimon Schubert 
13185796c8dcSSimon Schubert   /* An optional expression may be used to cause gdb to terminate with the
13195796c8dcSSimon Schubert      value of that expression.  */
13205796c8dcSSimon Schubert   if (args)
13215796c8dcSSimon Schubert     {
13225796c8dcSSimon Schubert       struct value *val = parse_and_eval (args);
13235796c8dcSSimon Schubert 
13245796c8dcSSimon Schubert       exit_code = (int) value_as_long (val);
13255796c8dcSSimon Schubert     }
13265796c8dcSSimon Schubert   else if (return_child_result)
13275796c8dcSSimon Schubert     exit_code = return_child_result_value;
13285796c8dcSSimon Schubert 
13295796c8dcSSimon Schubert   qt.args = args;
13305796c8dcSSimon Schubert   qt.from_tty = from_tty;
13315796c8dcSSimon Schubert 
13325796c8dcSSimon Schubert   /* We want to handle any quit errors and exit regardless.  */
13335796c8dcSSimon Schubert   catch_errors (quit_target, &qt,
13345796c8dcSSimon Schubert 	        "Quitting: ", RETURN_MASK_ALL);
13355796c8dcSSimon Schubert 
13365796c8dcSSimon Schubert   exit (exit_code);
13375796c8dcSSimon Schubert }
13385796c8dcSSimon Schubert 
13395796c8dcSSimon Schubert /* Returns whether GDB is running on a terminal and input is
13405796c8dcSSimon Schubert    currently coming from that terminal.  */
13415796c8dcSSimon Schubert 
13425796c8dcSSimon Schubert int
input_from_terminal_p(void)13435796c8dcSSimon Schubert input_from_terminal_p (void)
13445796c8dcSSimon Schubert {
1345cf7f2e2dSJohn Marino   if (batch_flag)
1346cf7f2e2dSJohn Marino     return 0;
1347cf7f2e2dSJohn Marino 
13485796c8dcSSimon Schubert   if (gdb_has_a_terminal () && instream == stdin)
13495796c8dcSSimon Schubert     return 1;
13505796c8dcSSimon Schubert 
13515796c8dcSSimon Schubert   /* If INSTREAM is unset, and we are not in a user command, we
13525796c8dcSSimon Schubert      must be in Insight.  That's like having a terminal, for our
13535796c8dcSSimon Schubert      purposes.  */
13545796c8dcSSimon Schubert   if (instream == NULL && !in_user_command)
13555796c8dcSSimon Schubert     return 1;
13565796c8dcSSimon Schubert 
13575796c8dcSSimon Schubert   return 0;
13585796c8dcSSimon Schubert }
13595796c8dcSSimon Schubert 
13605796c8dcSSimon Schubert static void
dont_repeat_command(char * ignored,int from_tty)13615796c8dcSSimon Schubert dont_repeat_command (char *ignored, int from_tty)
13625796c8dcSSimon Schubert {
1363a45ae5f8SJohn Marino   /* Can't call dont_repeat here because we're not necessarily reading
1364a45ae5f8SJohn Marino      from stdin.  */
1365a45ae5f8SJohn Marino   *saved_command_line = 0;
13665796c8dcSSimon Schubert }
13675796c8dcSSimon Schubert 
13685796c8dcSSimon Schubert /* Functions to manipulate command line editing control variables.  */
13695796c8dcSSimon Schubert 
13705796c8dcSSimon Schubert /* Number of commands to print in each call to show_commands.  */
13715796c8dcSSimon Schubert #define Hist_print 10
13725796c8dcSSimon Schubert void
show_commands(char * args,int from_tty)13735796c8dcSSimon Schubert show_commands (char *args, int from_tty)
13745796c8dcSSimon Schubert {
13755796c8dcSSimon Schubert   /* Index for history commands.  Relative to history_base.  */
13765796c8dcSSimon Schubert   int offset;
13775796c8dcSSimon Schubert 
13785796c8dcSSimon Schubert   /* Number of the history entry which we are planning to display next.
13795796c8dcSSimon Schubert      Relative to history_base.  */
13805796c8dcSSimon Schubert   static int num = 0;
13815796c8dcSSimon Schubert 
13825796c8dcSSimon Schubert   /* Print out some of the commands from the command history.  */
13835796c8dcSSimon Schubert 
13845796c8dcSSimon Schubert   if (args)
13855796c8dcSSimon Schubert     {
13865796c8dcSSimon Schubert       if (args[0] == '+' && args[1] == '\0')
13875796c8dcSSimon Schubert 	/* "info editing +" should print from the stored position.  */
13885796c8dcSSimon Schubert 	;
13895796c8dcSSimon Schubert       else
13905796c8dcSSimon Schubert 	/* "info editing <exp>" should print around command number <exp>.  */
13915796c8dcSSimon Schubert 	num = (parse_and_eval_long (args) - history_base) - Hist_print / 2;
13925796c8dcSSimon Schubert     }
13935796c8dcSSimon Schubert   /* "show commands" means print the last Hist_print commands.  */
13945796c8dcSSimon Schubert   else
13955796c8dcSSimon Schubert     {
1396*ef5ccd6cSJohn Marino       num = history_length - Hist_print;
13975796c8dcSSimon Schubert     }
13985796c8dcSSimon Schubert 
13995796c8dcSSimon Schubert   if (num < 0)
14005796c8dcSSimon Schubert     num = 0;
14015796c8dcSSimon Schubert 
14025796c8dcSSimon Schubert   /* If there are at least Hist_print commands, we want to display the last
14035796c8dcSSimon Schubert      Hist_print rather than, say, the last 6.  */
1404*ef5ccd6cSJohn Marino   if (history_length - num < Hist_print)
14055796c8dcSSimon Schubert     {
1406*ef5ccd6cSJohn Marino       num = history_length - Hist_print;
14075796c8dcSSimon Schubert       if (num < 0)
14085796c8dcSSimon Schubert 	num = 0;
14095796c8dcSSimon Schubert     }
14105796c8dcSSimon Schubert 
1411*ef5ccd6cSJohn Marino   for (offset = num;
1412*ef5ccd6cSJohn Marino        offset < num + Hist_print && offset < history_length;
1413*ef5ccd6cSJohn Marino        offset++)
14145796c8dcSSimon Schubert     {
14155796c8dcSSimon Schubert       printf_filtered ("%5d  %s\n", history_base + offset,
14165796c8dcSSimon Schubert 		       (history_get (history_base + offset))->line);
14175796c8dcSSimon Schubert     }
14185796c8dcSSimon Schubert 
14195796c8dcSSimon Schubert   /* The next command we want to display is the next one that we haven't
14205796c8dcSSimon Schubert      displayed yet.  */
14215796c8dcSSimon Schubert   num += Hist_print;
14225796c8dcSSimon Schubert 
14235796c8dcSSimon Schubert   /* If the user repeats this command with return, it should do what
14245796c8dcSSimon Schubert      "show commands +" does.  This is unnecessary if arg is null,
14255796c8dcSSimon Schubert      because "show commands +" is not useful after "show commands".  */
14265796c8dcSSimon Schubert   if (from_tty && args)
14275796c8dcSSimon Schubert     {
14285796c8dcSSimon Schubert       args[0] = '+';
14295796c8dcSSimon Schubert       args[1] = '\0';
14305796c8dcSSimon Schubert     }
14315796c8dcSSimon Schubert }
14325796c8dcSSimon Schubert 
14335796c8dcSSimon Schubert /* Called by do_setshow_command.  */
14345796c8dcSSimon Schubert static void
set_history_size_command(char * args,int from_tty,struct cmd_list_element * c)14355796c8dcSSimon Schubert set_history_size_command (char *args, int from_tty, struct cmd_list_element *c)
14365796c8dcSSimon Schubert {
1437*ef5ccd6cSJohn Marino   /* Readline's history interface works with 'int', so it can only
1438*ef5ccd6cSJohn Marino      handle history sizes up to INT_MAX.  The command itself is
1439*ef5ccd6cSJohn Marino      uinteger, so UINT_MAX means "unlimited", but we only get that if
1440*ef5ccd6cSJohn Marino      the user does "set history size 0" -- "set history size <UINT_MAX>"
1441*ef5ccd6cSJohn Marino      throws out-of-range.  */
1442*ef5ccd6cSJohn Marino   if (history_size_setshow_var > INT_MAX
1443*ef5ccd6cSJohn Marino       && history_size_setshow_var != UINT_MAX)
14445796c8dcSSimon Schubert     {
1445*ef5ccd6cSJohn Marino       unsigned int new_value = history_size_setshow_var;
1446*ef5ccd6cSJohn Marino 
1447*ef5ccd6cSJohn Marino       /* Restore previous value before throwing.  */
1448*ef5ccd6cSJohn Marino       if (history_is_stifled ())
1449*ef5ccd6cSJohn Marino 	history_size_setshow_var = history_max_entries;
1450*ef5ccd6cSJohn Marino       else
1451*ef5ccd6cSJohn Marino 	history_size_setshow_var = UINT_MAX;
1452*ef5ccd6cSJohn Marino 
1453*ef5ccd6cSJohn Marino       error (_("integer %u out of range"), new_value);
14545796c8dcSSimon Schubert     }
1455*ef5ccd6cSJohn Marino 
1456*ef5ccd6cSJohn Marino   /* Commit the new value to readline's history.  */
1457*ef5ccd6cSJohn Marino   if (history_size_setshow_var == UINT_MAX)
1458*ef5ccd6cSJohn Marino     unstifle_history ();
1459*ef5ccd6cSJohn Marino   else
1460*ef5ccd6cSJohn Marino     stifle_history (history_size_setshow_var);
14615796c8dcSSimon Schubert }
14625796c8dcSSimon Schubert 
14635796c8dcSSimon Schubert void
set_history(char * args,int from_tty)14645796c8dcSSimon Schubert set_history (char *args, int from_tty)
14655796c8dcSSimon Schubert {
1466c50c785cSJohn Marino   printf_unfiltered (_("\"set history\" must be followed "
1467c50c785cSJohn Marino 		       "by the name of a history subcommand.\n"));
14685796c8dcSSimon Schubert   help_list (sethistlist, "set history ", -1, gdb_stdout);
14695796c8dcSSimon Schubert }
14705796c8dcSSimon Schubert 
14715796c8dcSSimon Schubert void
show_history(char * args,int from_tty)14725796c8dcSSimon Schubert show_history (char *args, int from_tty)
14735796c8dcSSimon Schubert {
14745796c8dcSSimon Schubert   cmd_show_list (showhistlist, from_tty, "");
14755796c8dcSSimon Schubert }
14765796c8dcSSimon Schubert 
1477c50c785cSJohn Marino int info_verbose = 0;		/* Default verbose msgs off.  */
14785796c8dcSSimon Schubert 
14795796c8dcSSimon Schubert /* Called by do_setshow_command.  An elaborate joke.  */
14805796c8dcSSimon Schubert void
set_verbose(char * args,int from_tty,struct cmd_list_element * c)14815796c8dcSSimon Schubert set_verbose (char *args, int from_tty, struct cmd_list_element *c)
14825796c8dcSSimon Schubert {
14835796c8dcSSimon Schubert   char *cmdname = "verbose";
14845796c8dcSSimon Schubert   struct cmd_list_element *showcmd;
14855796c8dcSSimon Schubert 
14865796c8dcSSimon Schubert   showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
1487c50c785cSJohn Marino   gdb_assert (showcmd != NULL && showcmd != CMD_LIST_AMBIGUOUS);
14885796c8dcSSimon Schubert 
14895796c8dcSSimon Schubert   if (info_verbose)
14905796c8dcSSimon Schubert     {
14915796c8dcSSimon Schubert       c->doc = "Set verbose printing of informational messages.";
14925796c8dcSSimon Schubert       showcmd->doc = "Show verbose printing of informational messages.";
14935796c8dcSSimon Schubert     }
14945796c8dcSSimon Schubert   else
14955796c8dcSSimon Schubert     {
14965796c8dcSSimon Schubert       c->doc = "Set verbosity.";
14975796c8dcSSimon Schubert       showcmd->doc = "Show verbosity.";
14985796c8dcSSimon Schubert     }
14995796c8dcSSimon Schubert }
15005796c8dcSSimon Schubert 
15015796c8dcSSimon Schubert /* Init the history buffer.  Note that we are called after the init file(s)
1502c50c785cSJohn Marino    have been read so that the user can change the history file via his
1503c50c785cSJohn Marino    .gdbinit file (for instance).  The GDBHISTFILE environment variable
1504c50c785cSJohn Marino    overrides all of this.  */
15055796c8dcSSimon Schubert 
15065796c8dcSSimon Schubert void
init_history(void)15075796c8dcSSimon Schubert init_history (void)
15085796c8dcSSimon Schubert {
15095796c8dcSSimon Schubert   char *tmpenv;
15105796c8dcSSimon Schubert 
15115796c8dcSSimon Schubert   tmpenv = getenv ("HISTSIZE");
15125796c8dcSSimon Schubert   if (tmpenv)
1513*ef5ccd6cSJohn Marino     {
1514*ef5ccd6cSJohn Marino       int var;
15155796c8dcSSimon Schubert 
1516*ef5ccd6cSJohn Marino       var = atoi (tmpenv);
1517*ef5ccd6cSJohn Marino       if (var < 0)
1518*ef5ccd6cSJohn Marino 	{
1519*ef5ccd6cSJohn Marino 	  /* Prefer ending up with no history rather than overflowing
1520*ef5ccd6cSJohn Marino 	     readline's history interface, which uses signed 'int'
1521*ef5ccd6cSJohn Marino 	     everywhere.  */
1522*ef5ccd6cSJohn Marino 	  var = 0;
1523*ef5ccd6cSJohn Marino 	}
1524*ef5ccd6cSJohn Marino 
1525*ef5ccd6cSJohn Marino       history_size_setshow_var = var;
1526*ef5ccd6cSJohn Marino     }
1527*ef5ccd6cSJohn Marino   /* If the init file hasn't set a size yet, pick the default.  */
1528*ef5ccd6cSJohn Marino   else if (history_size_setshow_var == 0)
1529*ef5ccd6cSJohn Marino     history_size_setshow_var = 256;
1530*ef5ccd6cSJohn Marino 
1531*ef5ccd6cSJohn Marino   /* Note that unlike "set history size 0", "HISTSIZE=0" really sets
1532*ef5ccd6cSJohn Marino      the history size to 0...  */
1533*ef5ccd6cSJohn Marino   stifle_history (history_size_setshow_var);
15345796c8dcSSimon Schubert 
15355796c8dcSSimon Schubert   tmpenv = getenv ("GDBHISTFILE");
15365796c8dcSSimon Schubert   if (tmpenv)
15375796c8dcSSimon Schubert     history_filename = xstrdup (tmpenv);
15385796c8dcSSimon Schubert   else if (!history_filename)
15395796c8dcSSimon Schubert     {
15405796c8dcSSimon Schubert       /* We include the current directory so that if the user changes
15415796c8dcSSimon Schubert          directories the file written will be the same as the one
15425796c8dcSSimon Schubert          that was read.  */
15435796c8dcSSimon Schubert #ifdef __MSDOS__
15445796c8dcSSimon Schubert       /* No leading dots in file names are allowed on MSDOS.  */
15455796c8dcSSimon Schubert       history_filename = concat (current_directory, "/_gdb_history",
15465796c8dcSSimon Schubert 				 (char *)NULL);
15475796c8dcSSimon Schubert #else
15485796c8dcSSimon Schubert       history_filename = concat (current_directory, "/.gdb_history",
15495796c8dcSSimon Schubert 				 (char *)NULL);
15505796c8dcSSimon Schubert #endif
15515796c8dcSSimon Schubert     }
15525796c8dcSSimon Schubert   read_history (history_filename);
15535796c8dcSSimon Schubert }
15545796c8dcSSimon Schubert 
15555796c8dcSSimon Schubert static void
show_prompt(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)1556a45ae5f8SJohn Marino show_prompt (struct ui_file *file, int from_tty,
15575796c8dcSSimon Schubert 	     struct cmd_list_element *c, const char *value)
15585796c8dcSSimon Schubert {
15595796c8dcSSimon Schubert   fprintf_filtered (file, _("Gdb's prompt is \"%s\".\n"), value);
15605796c8dcSSimon Schubert }
15615796c8dcSSimon Schubert 
15625796c8dcSSimon Schubert static void
show_async_command_editing_p(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)15635796c8dcSSimon Schubert show_async_command_editing_p (struct ui_file *file, int from_tty,
15645796c8dcSSimon Schubert 			      struct cmd_list_element *c, const char *value)
15655796c8dcSSimon Schubert {
1566c50c785cSJohn Marino   fprintf_filtered (file, _("Editing of command lines as "
1567c50c785cSJohn Marino 			    "they are typed is %s.\n"),
15685796c8dcSSimon Schubert 		    value);
15695796c8dcSSimon Schubert }
15705796c8dcSSimon Schubert 
15715796c8dcSSimon Schubert static void
show_annotation_level(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)15725796c8dcSSimon Schubert show_annotation_level (struct ui_file *file, int from_tty,
15735796c8dcSSimon Schubert 		       struct cmd_list_element *c, const char *value)
15745796c8dcSSimon Schubert {
15755796c8dcSSimon Schubert   fprintf_filtered (file, _("Annotation_level is %s.\n"), value);
15765796c8dcSSimon Schubert }
15775796c8dcSSimon Schubert 
15785796c8dcSSimon Schubert static void
show_exec_done_display_p(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)15795796c8dcSSimon Schubert show_exec_done_display_p (struct ui_file *file, int from_tty,
15805796c8dcSSimon Schubert 			  struct cmd_list_element *c, const char *value)
15815796c8dcSSimon Schubert {
1582c50c785cSJohn Marino   fprintf_filtered (file, _("Notification of completion for "
1583c50c785cSJohn Marino 			    "asynchronous execution commands is %s.\n"),
15845796c8dcSSimon Schubert 		    value);
15855796c8dcSSimon Schubert }
1586*ef5ccd6cSJohn Marino 
1587*ef5ccd6cSJohn Marino /* "set" command for the gdb_datadir configuration variable.  */
1588*ef5ccd6cSJohn Marino 
1589*ef5ccd6cSJohn Marino static void
set_gdb_datadir(char * args,int from_tty,struct cmd_list_element * c)1590*ef5ccd6cSJohn Marino set_gdb_datadir (char *args, int from_tty, struct cmd_list_element *c)
1591*ef5ccd6cSJohn Marino {
1592*ef5ccd6cSJohn Marino   observer_notify_gdb_datadir_changed ();
1593*ef5ccd6cSJohn Marino }
1594*ef5ccd6cSJohn Marino 
15955796c8dcSSimon Schubert static void
init_main(void)15965796c8dcSSimon Schubert init_main (void)
15975796c8dcSSimon Schubert {
1598a45ae5f8SJohn Marino   /* Initialize the prompt to a simple "(gdb) " prompt or to whatever
1599a45ae5f8SJohn Marino      the DEFAULT_PROMPT is.  */
1600a45ae5f8SJohn Marino   set_prompt (DEFAULT_PROMPT);
1601a45ae5f8SJohn Marino 
16025796c8dcSSimon Schubert   /* Set things up for annotation_level > 1, if the user ever decides
16035796c8dcSSimon Schubert      to use it.  */
16045796c8dcSSimon Schubert   async_annotation_suffix = "prompt";
16055796c8dcSSimon Schubert 
16065796c8dcSSimon Schubert   /* Set the important stuff up for command editing.  */
16075796c8dcSSimon Schubert   command_editing_p = 1;
16085796c8dcSSimon Schubert   history_expansion_p = 0;
16095796c8dcSSimon Schubert   write_history_p = 0;
16105796c8dcSSimon Schubert 
16115796c8dcSSimon Schubert   /* Setup important stuff for command line editing.  */
16125796c8dcSSimon Schubert   rl_completion_word_break_hook = gdb_completion_word_break_characters;
16135796c8dcSSimon Schubert   rl_completion_entry_function = readline_line_completion_function;
16145796c8dcSSimon Schubert   rl_completer_word_break_characters = default_word_break_characters ();
16155796c8dcSSimon Schubert   rl_completer_quote_characters = get_gdb_completer_quote_characters ();
16165796c8dcSSimon Schubert   rl_readline_name = "gdb";
16175796c8dcSSimon Schubert   rl_terminal_name = getenv ("TERM");
16185796c8dcSSimon Schubert 
16195796c8dcSSimon Schubert   /* The name for this defun comes from Bash, where it originated.
16205796c8dcSSimon Schubert      15 is Control-o, the same binding this function has in Bash.  */
16215796c8dcSSimon Schubert   rl_add_defun ("operate-and-get-next", gdb_rl_operate_and_get_next, 15);
16225796c8dcSSimon Schubert 
16235796c8dcSSimon Schubert   add_setshow_string_cmd ("prompt", class_support,
1624a45ae5f8SJohn Marino 			  &top_prompt,
1625c50c785cSJohn Marino 			  _("Set gdb's prompt"),
1626c50c785cSJohn Marino 			  _("Show gdb's prompt"),
1627a45ae5f8SJohn Marino 			  NULL, NULL,
1628a45ae5f8SJohn Marino 			  show_prompt,
16295796c8dcSSimon Schubert 			  &setlist, &showlist);
16305796c8dcSSimon Schubert 
16315796c8dcSSimon Schubert   add_com ("dont-repeat", class_support, dont_repeat_command, _("\
1632c50c785cSJohn Marino Don't repeat this command.\nPrimarily \
1633c50c785cSJohn Marino used inside of user-defined commands that should not be repeated when\n\
16345796c8dcSSimon Schubert hitting return."));
16355796c8dcSSimon Schubert 
16365796c8dcSSimon Schubert   add_setshow_boolean_cmd ("editing", class_support,
16375796c8dcSSimon Schubert 			   &async_command_editing_p, _("\
16385796c8dcSSimon Schubert Set editing of command lines as they are typed."), _("\
16395796c8dcSSimon Schubert Show editing of command lines as they are typed."), _("\
16405796c8dcSSimon Schubert Use \"on\" to enable the editing, and \"off\" to disable it.\n\
16415796c8dcSSimon Schubert Without an argument, command line editing is enabled.  To edit, use\n\
16425796c8dcSSimon Schubert EMACS-like or VI-like commands like control-P or ESC."),
16435796c8dcSSimon Schubert 			   set_async_editing_command,
16445796c8dcSSimon Schubert 			   show_async_command_editing_p,
16455796c8dcSSimon Schubert 			   &setlist, &showlist);
16465796c8dcSSimon Schubert 
16475796c8dcSSimon Schubert   add_setshow_boolean_cmd ("save", no_class, &write_history_p, _("\
16485796c8dcSSimon Schubert Set saving of the history record on exit."), _("\
16495796c8dcSSimon Schubert Show saving of the history record on exit."), _("\
16505796c8dcSSimon Schubert Use \"on\" to enable the saving, and \"off\" to disable it.\n\
16515796c8dcSSimon Schubert Without an argument, saving is enabled."),
16525796c8dcSSimon Schubert 			   NULL,
16535796c8dcSSimon Schubert 			   show_write_history_p,
16545796c8dcSSimon Schubert 			   &sethistlist, &showhistlist);
16555796c8dcSSimon Schubert 
1656*ef5ccd6cSJohn Marino   add_setshow_uinteger_cmd ("size", no_class, &history_size_setshow_var, _("\
16575796c8dcSSimon Schubert Set the size of the command history,"), _("\
16585796c8dcSSimon Schubert Show the size of the command history,"), _("\
16595796c8dcSSimon Schubert ie. the number of previous commands to keep a record of."),
16605796c8dcSSimon Schubert 			    set_history_size_command,
16615796c8dcSSimon Schubert 			    show_history_size,
16625796c8dcSSimon Schubert 			    &sethistlist, &showhistlist);
16635796c8dcSSimon Schubert 
16645796c8dcSSimon Schubert   add_setshow_filename_cmd ("filename", no_class, &history_filename, _("\
16655796c8dcSSimon Schubert Set the filename in which to record the command history"), _("\
16665796c8dcSSimon Schubert Show the filename in which to record the command history"), _("\
16675796c8dcSSimon Schubert (the list of previous commands of which a record is kept)."),
16685796c8dcSSimon Schubert 			    NULL,
16695796c8dcSSimon Schubert 			    show_history_filename,
16705796c8dcSSimon Schubert 			    &sethistlist, &showhistlist);
16715796c8dcSSimon Schubert 
1672*ef5ccd6cSJohn Marino   add_setshow_boolean_cmd ("confirm", class_support, &confirm, _("\
16735796c8dcSSimon Schubert Set whether to confirm potentially dangerous operations."), _("\
16745796c8dcSSimon Schubert Show whether to confirm potentially dangerous operations."), NULL,
16755796c8dcSSimon Schubert 			   NULL,
1676*ef5ccd6cSJohn Marino 			   show_confirm,
16775796c8dcSSimon Schubert 			   &setlist, &showlist);
16785796c8dcSSimon Schubert 
16795796c8dcSSimon Schubert   add_setshow_zinteger_cmd ("annotate", class_obscure, &annotation_level, _("\
16805796c8dcSSimon Schubert Set annotation_level."), _("\
16815796c8dcSSimon Schubert Show annotation_level."), _("\
16825796c8dcSSimon Schubert 0 == normal;     1 == fullname (for use when running under emacs)\n\
16835796c8dcSSimon Schubert 2 == output annotated suitably for use by programs that control GDB."),
1684a45ae5f8SJohn Marino 			    NULL,
16855796c8dcSSimon Schubert 			    show_annotation_level,
16865796c8dcSSimon Schubert 			    &setlist, &showlist);
16875796c8dcSSimon Schubert 
16885796c8dcSSimon Schubert   add_setshow_boolean_cmd ("exec-done-display", class_support,
16895796c8dcSSimon Schubert 			   &exec_done_display_p, _("\
16905796c8dcSSimon Schubert Set notification of completion for asynchronous execution commands."), _("\
16915796c8dcSSimon Schubert Show notification of completion for asynchronous execution commands."), _("\
16925796c8dcSSimon Schubert Use \"on\" to enable the notification, and \"off\" to disable it."),
16935796c8dcSSimon Schubert 			   NULL,
16945796c8dcSSimon Schubert 			   show_exec_done_display_p,
16955796c8dcSSimon Schubert 			   &setlist, &showlist);
16965796c8dcSSimon Schubert 
16975796c8dcSSimon Schubert   add_setshow_filename_cmd ("data-directory", class_maintenance,
16985796c8dcSSimon Schubert                            &gdb_datadir, _("Set GDB's data directory."),
16995796c8dcSSimon Schubert                            _("Show GDB's data directory."),
17005796c8dcSSimon Schubert                            _("\
17015796c8dcSSimon Schubert When set, GDB uses the specified path to search for data files."),
1702*ef5ccd6cSJohn Marino                            set_gdb_datadir, NULL,
17035796c8dcSSimon Schubert                            &setlist,
17045796c8dcSSimon Schubert                            &showlist);
17055796c8dcSSimon Schubert }
17065796c8dcSSimon Schubert 
17075796c8dcSSimon Schubert void
gdb_init(char * argv0)17085796c8dcSSimon Schubert gdb_init (char *argv0)
17095796c8dcSSimon Schubert {
17105796c8dcSSimon Schubert   if (pre_init_ui_hook)
17115796c8dcSSimon Schubert     pre_init_ui_hook ();
17125796c8dcSSimon Schubert 
1713c50c785cSJohn Marino   /* Run the init function of each source file.  */
17145796c8dcSSimon Schubert 
17155796c8dcSSimon Schubert #ifdef __MSDOS__
17165796c8dcSSimon Schubert   /* Make sure we return to the original directory upon exit, come
17175796c8dcSSimon Schubert      what may, since the OS doesn't do that for us.  */
17185796c8dcSSimon Schubert   make_final_cleanup (do_chdir_cleanup, xstrdup (current_directory));
17195796c8dcSSimon Schubert #endif
17205796c8dcSSimon Schubert 
1721c50c785cSJohn Marino   init_cmd_lists ();	    /* This needs to be done first.  */
1722c50c785cSJohn Marino   initialize_targets ();    /* Setup target_terminal macros for utils.c.  */
1723c50c785cSJohn Marino   initialize_utils ();	    /* Make errors and warnings possible.  */
1724c50c785cSJohn Marino 
1725c50c785cSJohn Marino   /* Here is where we call all the _initialize_foo routines.  */
17265796c8dcSSimon Schubert   initialize_all_files ();
1727c50c785cSJohn Marino 
1728cf7f2e2dSJohn Marino   /* This creates the current_program_space.  Do this after all the
1729cf7f2e2dSJohn Marino      _initialize_foo routines have had a chance to install their
1730cf7f2e2dSJohn Marino      per-sspace data keys.  Also do this before
1731cf7f2e2dSJohn Marino      initialize_current_architecture is called, because it accesses
1732cf7f2e2dSJohn Marino      exec_bfd of the current program space.  */
1733cf7f2e2dSJohn Marino   initialize_progspace ();
1734cf7f2e2dSJohn Marino   initialize_inferiors ();
17355796c8dcSSimon Schubert   initialize_current_architecture ();
17365796c8dcSSimon Schubert   init_cli_cmds();
1737*ef5ccd6cSJohn Marino   initialize_event_loop ();
1738c50c785cSJohn Marino   init_main ();			/* But that omits this file!  Do it now.  */
17395796c8dcSSimon Schubert 
17405796c8dcSSimon Schubert   initialize_stdin_serial ();
17415796c8dcSSimon Schubert 
17425796c8dcSSimon Schubert   async_init_signals ();
17435796c8dcSSimon Schubert 
1744c50c785cSJohn Marino   /* We need a default language for parsing expressions, so simple
1745c50c785cSJohn Marino      things like "set width 0" won't fail if no language is explicitly
1746c50c785cSJohn Marino      set in a config file or implicitly set by reading an executable
1747c50c785cSJohn Marino      during startup.  */
17485796c8dcSSimon Schubert   set_language (language_c);
1749c50c785cSJohn Marino   expected_language = current_language;	/* Don't warn about the change.  */
17505796c8dcSSimon Schubert 
17515796c8dcSSimon Schubert   /* Allow another UI to initialize.  If the UI fails to initialize,
17525796c8dcSSimon Schubert      and it wants GDB to revert to the CLI, it should clear
17535796c8dcSSimon Schubert      deprecated_init_ui_hook.  */
17545796c8dcSSimon Schubert   if (deprecated_init_ui_hook)
17555796c8dcSSimon Schubert     deprecated_init_ui_hook (argv0);
1756c50c785cSJohn Marino 
1757c50c785cSJohn Marino #ifdef HAVE_PYTHON
1758c50c785cSJohn Marino   /* Python initialization can require various commands to be
1759c50c785cSJohn Marino      installed.  For example "info pretty-printer" needs the "info"
1760c50c785cSJohn Marino      prefix to be installed.  Keep things simple and just do final
1761c50c785cSJohn Marino      python initialization here.  */
1762c50c785cSJohn Marino   finish_python_initialization ();
1763c50c785cSJohn Marino #endif
17645796c8dcSSimon Schubert }
1765