Fix for PR gdb/209, PR gdb/156:
[deliverable/binutils-gdb.git] / gdb / top.c
CommitLineData
c906108c 1/* Top level stuff for GDB, the GNU debugger.
b6ba6518
KB
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b
JM
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
c906108c
SS
21
22#include "defs.h"
23#include "gdbcmd.h"
24#include "call-cmds.h"
210661e7
EZ
25#include "cli/cli-cmds.h"
26#include "cli/cli-script.h"
27#include "cli/cli-setshow.h"
c906108c
SS
28#include "symtab.h"
29#include "inferior.h"
042be3a9 30#include <signal.h>
c906108c
SS
31#include "target.h"
32#include "breakpoint.h"
33#include "gdbtypes.h"
34#include "expression.h"
35#include "value.h"
36#include "language.h"
c5aa993b 37#include "terminal.h" /* For job_control. */
c906108c 38#include "annotate.h"
c5f0f3d0 39#include "completer.h"
c906108c 40#include "top.h"
d4f3574e 41#include "version.h"
210661e7 42#include "serial.h"
d16aafd8 43#include "doublest.h"
f9c696d2 44#include "gdb_assert.h"
c906108c
SS
45
46/* readline include files */
47#include <readline/readline.h>
48#include <readline/history.h>
49
50/* readline defines this. */
51#undef savestring
52
53#include <sys/types.h>
c906108c 54
c4093a6a 55#include <setjmp.h>
2acceee2 56
c2c6d25f 57#include "event-top.h"
c906108c
SS
58#include "gdb_string.h"
59#include "gdb_stat.h"
60#include <ctype.h>
8b93c638
JM
61#ifdef UI_OUT
62#include "ui-out.h"
63#include "cli-out.h"
64#endif
c906108c 65
104c1213
JM
66/* Default command line prompt. This is overriden in some configs. */
67
68#ifndef DEFAULT_PROMPT
69#define DEFAULT_PROMPT "(gdb) "
c906108c
SS
70#endif
71
72/* Initialization file name for gdb. This is overridden in some configs. */
73
74#ifndef GDBINIT_FILENAME
75#define GDBINIT_FILENAME ".gdbinit"
76#endif
77char gdbinit[] = GDBINIT_FILENAME;
78
79int inhibit_gdbinit = 0;
80
81/* If nonzero, and GDB has been configured to be able to use windows,
82 attempt to open them upon startup. */
83
84int use_windows = 1;
85
c906108c
SS
86extern char lang_frame_mismatch_warn[]; /* language.c */
87
88/* Flag for whether we want all the "from_tty" gubbish printed. */
89
c5aa993b 90int caution = 1; /* Default is yes, sigh. */
c906108c 91
c906108c
SS
92/* stdio stream that command input is being read from. Set to stdin normally.
93 Set by source_command to the file we are sourcing. Set to NULL if we are
94 executing a user-defined command or interacting via a GUI. */
95
96FILE *instream;
97
98/* Current working directory. */
99
100char *current_directory;
101
102/* The directory name is actually stored here (usually). */
103char gdb_dirbuf[1024];
104
105/* Function to call before reading a command, if nonzero.
106 The function receives two args: an input stream,
107 and a prompt string. */
108
507f3c78 109void (*window_hook) (FILE *, char *);
c906108c
SS
110
111int epoch_interface;
112int xgdb_verbose;
113
114/* gdb prints this when reading a command interactively */
c5aa993b 115static char *gdb_prompt_string; /* the global prompt string */
c906108c
SS
116
117/* Buffer used for reading command lines, and the size
118 allocated for it so far. */
119
120char *line;
121int linesize = 100;
122
123/* Nonzero if the current command is modified by "server ". This
c2d11a7d 124 affects things like recording into the command history, commands
c906108c
SS
125 repeating on RETURN, etc. This is so a user interface (emacs, GUI,
126 whatever) can issue its own commands and also send along commands
127 from the user, and have the user not notice that the user interface
128 is issuing commands too. */
129int server_command;
130
131/* Baud rate specified for talking to serial target systems. Default
132 is left as -1, so targets can choose their own defaults. */
133/* FIXME: This means that "show remotebaud" and gr_files_info can print -1
134 or (unsigned int)-1. This is a Bad User Interface. */
135
136int baud_rate = -1;
137
138/* Timeout limit for response from target. */
139
ce808e91
AC
140/* The default value has been changed many times over the years. It
141 was originally 5 seconds. But that was thought to be a long time
142 to sit and wait, so it was changed to 2 seconds. That was thought
143 to be plenty unless the connection was going through some terminal
144 server or multiplexer or other form of hairy serial connection.
145
146 In mid-1996, remote_timeout was moved from remote.c to top.c and
147 it began being used in other remote-* targets. It appears that the
148 default was changed to 20 seconds at that time, perhaps because the
149 Hitachi E7000 ICE didn't always respond in a timely manner.
150
151 But if 5 seconds is a long time to sit and wait for retransmissions,
152 20 seconds is far worse. This demonstrates the difficulty of using
153 a single variable for all protocol timeouts.
154
155 As remote.c is used much more than remote-e7000.c, it was changed
156 back to 2 seconds in 1999. */
157
158int remote_timeout = 2;
c906108c
SS
159
160/* Non-zero tells remote* modules to output debugging info. */
161
162int remote_debug = 0;
163
43ff13b4
JM
164/* Non-zero means the target is running. Note: this is different from
165 saying that there is an active target and we are stopped at a
166 breakpoint, for instance. This is a real indicator whether the
167 target is off and running, which gdb is doing something else. */
168int target_executing = 0;
169
c906108c
SS
170/* Level of control structure. */
171static int control_level;
172
c906108c
SS
173/* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
174
175#ifndef STOP_SIGNAL
176#ifdef SIGTSTP
177#define STOP_SIGNAL SIGTSTP
a14ed312 178static void stop_sig (int);
c906108c
SS
179#endif
180#endif
181
c906108c
SS
182/* Hooks for alternate command interfaces. */
183
184/* Called after most modules have been initialized, but before taking users
1ad24239
KS
185 command file.
186
187 If the UI fails to initialize and it wants GDB to continue
188 using the default UI, then it should clear this hook before returning. */
c906108c 189
507f3c78 190void (*init_ui_hook) (char *argv0);
7a292a7a
SS
191
192/* This hook is called from within gdb's many mini-event loops which could
193 steal control from a real user interface's event loop. It returns
194 non-zero if the user is requesting a detach, zero otherwise. */
195
507f3c78 196int (*ui_loop_hook) (int);
c906108c
SS
197
198/* Called instead of command_loop at top level. Can be invoked via
199 return_to_top_level. */
200
507f3c78 201void (*command_loop_hook) (void);
c906108c
SS
202
203
c906108c
SS
204/* Called from print_frame_info to list the line we stopped in. */
205
507f3c78
KB
206void (*print_frame_info_listing_hook) (struct symtab * s, int line,
207 int stopline, int noerror);
c906108c
SS
208/* Replaces most of query. */
209
507f3c78 210int (*query_hook) (const char *, va_list);
c906108c
SS
211
212/* Replaces most of warning. */
213
507f3c78 214void (*warning_hook) (const char *, va_list);
c906108c 215
c906108c
SS
216/* These three functions support getting lines of text from the user. They
217 are used in sequence. First readline_begin_hook is called with a text
218 string that might be (for example) a message for the user to type in a
219 sequence of commands to be executed at a breakpoint. If this function
220 calls back to a GUI, it might take this opportunity to pop up a text
221 interaction window with this message. Next, readline_hook is called
222 with a prompt that is emitted prior to collecting the user input.
223 It can be called multiple times. Finally, readline_end_hook is called
224 to notify the GUI that we are done with the interaction window and it
225 can close it. */
226
507f3c78
KB
227void (*readline_begin_hook) (char *, ...);
228char *(*readline_hook) (char *);
229void (*readline_end_hook) (void);
c906108c
SS
230
231/* Called as appropriate to notify the interface of the specified breakpoint
232 conditions. */
233
507f3c78
KB
234void (*create_breakpoint_hook) (struct breakpoint * bpt);
235void (*delete_breakpoint_hook) (struct breakpoint * bpt);
236void (*modify_breakpoint_hook) (struct breakpoint * bpt);
c906108c 237
6426a772
JM
238/* Called as appropriate to notify the interface that we have attached
239 to or detached from an already running process. */
240
507f3c78
KB
241void (*attach_hook) (void);
242void (*detach_hook) (void);
6426a772 243
c906108c
SS
244/* Called during long calculations to allow GUI to repair window damage, and to
245 check for stop buttons, etc... */
246
507f3c78 247void (*interactive_hook) (void);
c906108c
SS
248
249/* Called when the registers have changed, as a hint to a GUI
250 to minimize window update. */
251
507f3c78 252void (*registers_changed_hook) (void);
c906108c
SS
253
254/* Tell the GUI someone changed the register REGNO. -1 means
255 that the caller does not know which register changed or
c5aa993b 256 that several registers have changed (see value_assign). */
507f3c78 257void (*register_changed_hook) (int regno);
c906108c
SS
258
259/* Tell the GUI someone changed LEN bytes of memory at ADDR */
507f3c78 260void (*memory_changed_hook) (CORE_ADDR addr, int len);
c906108c
SS
261
262/* Called when going to wait for the target. Usually allows the GUI to run
263 while waiting for target events. */
264
39f77062
KB
265ptid_t (*target_wait_hook) (ptid_t ptid,
266 struct target_waitstatus * status);
c906108c
SS
267
268/* Used by UI as a wrapper around command execution. May do various things
269 like enabling/disabling buttons, etc... */
270
507f3c78
KB
271void (*call_command_hook) (struct cmd_list_element * c, char *cmd,
272 int from_tty);
c906108c 273
96baa820
JM
274/* Called after a `set' command has finished. Is only run if the
275 `set' command succeeded. */
276
eb2f494a 277void (*set_hook) (struct cmd_list_element * c);
96baa820 278
c906108c
SS
279/* Called when the current thread changes. Argument is thread id. */
280
507f3c78 281void (*context_hook) (int id);
c906108c
SS
282
283/* Takes control from error (). Typically used to prevent longjmps out of the
284 middle of the GUI. Usually used in conjunction with a catch routine. */
285
eb2f494a 286NORETURN void (*error_hook) (void) ATTR_NORETURN;
c906108c 287\f
c5aa993b 288
99eeeb0f
ND
289/* One should use catch_errors rather than manipulating these
290 directly. */
c4093a6a
JM
291#if defined(HAVE_SIGSETJMP)
292#define SIGJMP_BUF sigjmp_buf
ae9d9b96
CF
293#define SIGSETJMP(buf) sigsetjmp((buf), 1)
294#define SIGLONGJMP(buf,val) siglongjmp((buf), (val))
c4093a6a
JM
295#else
296#define SIGJMP_BUF jmp_buf
297#define SIGSETJMP(buf) setjmp(buf)
ae9d9b96 298#define SIGLONGJMP(buf,val) longjmp((buf), (val))
c4093a6a
JM
299#endif
300
99eeeb0f
ND
301/* Where to go for return_to_top_level. */
302static SIGJMP_BUF *catch_return;
c906108c 303
99eeeb0f 304/* Return for reason REASON to the nearest containing catch_errors(). */
c906108c 305
c2d11a7d 306NORETURN void
fba45db2 307return_to_top_level (enum return_reason reason)
c906108c
SS
308{
309 quit_flag = 0;
310 immediate_quit = 0;
311
312 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
313 I can think of a reason why that is vital, though). */
c5aa993b 314 bpstat_clear_actions (stop_bpstat); /* Clear queued breakpoint commands */
c906108c
SS
315
316 disable_current_display ();
317 do_cleanups (ALL_CLEANUPS);
c4093a6a 318 if (event_loop_p && target_can_async_p () && !target_executing)
43ff13b4 319 do_exec_cleanups (ALL_CLEANUPS);
6426a772
JM
320 if (event_loop_p && sync_execution)
321 do_exec_error_cleanups (ALL_CLEANUPS);
c906108c
SS
322
323 if (annotation_level > 1)
324 switch (reason)
325 {
326 case RETURN_QUIT:
327 annotate_quit ();
328 break;
329 case RETURN_ERROR:
330 annotate_error ();
331 break;
332 }
333
99eeeb0f
ND
334 /* Jump to the containing catch_errors() call, communicating REASON
335 to that call via setjmp's return value. Note that REASON can't
336 be zero, by definition in defs.h. */
337
eb2f494a 338 (NORETURN void) SIGLONGJMP (*catch_return, (int) reason);
c906108c
SS
339}
340
f9c696d2
AC
341/* Call FUNC() with args FUNC_UIOUT and FUNC_ARGS, catching any
342 errors. Set FUNC_CAUGHT to an ``enum return_reason'' if the
343 function is aborted (using return_to_top_level() or zero if the
344 function returns normally. Set FUNC_VAL to the value returned by
345 the function or 0 if the function was aborted.
c906108c
SS
346
347 Must not be called with immediate_quit in effect (bad things might
348 happen, say we got a signal in the middle of a memcpy to quit_return).
349 This is an OK restriction; with very few exceptions immediate_quit can
350 be replaced by judicious use of QUIT.
351
352 MASK specifies what to catch; it is normally set to
353 RETURN_MASK_ALL, if for no other reason than that the code which
354 calls catch_errors might not be set up to deal with a quit which
355 isn't caught. But if the code can deal with it, it generally
356 should be RETURN_MASK_ERROR, unless for some reason it is more
357 useful to abort only the portion of the operation inside the
358 catch_errors. Note that quit should return to the command line
359 fairly quickly, even if some further processing is being done. */
360
11cf8741
JM
361/* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
362 error() et.al. could maintain a set of flags that indicate the the
363 current state of each of the longjmp buffers. This would give the
364 longjmp code the chance to detect a longjmp botch (before it gets
365 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
366 code also randomly used a SET_TOP_LEVEL macro that directly
367 initialize the longjmp buffers. */
368
e26cc349 369/* MAYBE: cagney/1999-11-05: Should the catch_errors and cleanups code
11cf8741
JM
370 be consolidated into a single file instead of being distributed
371 between utils.c and top.c? */
372
f9c696d2
AC
373static void
374catcher (catch_exceptions_ftype *func,
375 struct ui_out *func_uiout,
376 void *func_args,
377 int *func_val,
378 enum return_reason *func_caught,
379 char *errstring,
380 return_mask mask)
c906108c 381{
99eeeb0f
ND
382 SIGJMP_BUF *saved_catch;
383 SIGJMP_BUF catch;
c906108c
SS
384 struct cleanup *saved_cleanup_chain;
385 char *saved_error_pre_print;
386 char *saved_quit_pre_print;
f9c696d2 387 struct ui_out *saved_uiout;
c906108c 388
99eeeb0f
ND
389 /* Return value from SIGSETJMP(): enum return_reason if error or
390 quit caught, 0 otherwise. */
391 int caught;
392
f9c696d2
AC
393 /* Return value from FUNC(): Hopefully non-zero. Explicitly set to
394 zero if an error quit was caught. */
395 int val;
396
99eeeb0f
ND
397 /* Override error/quit messages during FUNC. */
398
c906108c
SS
399 saved_error_pre_print = error_pre_print;
400 saved_quit_pre_print = quit_pre_print;
401
402 if (mask & RETURN_MASK_ERROR)
99eeeb0f 403 error_pre_print = errstring;
c906108c 404 if (mask & RETURN_MASK_QUIT)
99eeeb0f
ND
405 quit_pre_print = errstring;
406
f9c696d2
AC
407 /* Override the global ``struct ui_out'' builder. */
408
409 saved_uiout = uiout;
410 uiout = func_uiout;
411
99eeeb0f
ND
412 /* Prevent error/quit during FUNC from calling cleanups established
413 prior to here. */
414
415 saved_cleanup_chain = save_cleanups ();
416
417 /* Call FUNC, catching error/quit events. */
418
419 saved_catch = catch_return;
420 catch_return = &catch;
421 caught = SIGSETJMP (catch);
422 if (!caught)
f9c696d2 423 val = (*func) (func_uiout, func_args);
7f7e9482
AC
424 else
425 val = 0;
99eeeb0f
ND
426 catch_return = saved_catch;
427
e26cc349 428 /* FIXME: cagney/1999-11-05: A correct FUNC implementation will
99eeeb0f 429 clean things up (restoring the cleanup chain) to the state they
e26cc349 430 were just prior to the call. Unfortunately, many FUNC's are not
99eeeb0f
ND
431 that well behaved. This could be fixed by adding either a
432 do_cleanups call (to cover the problem) or an assertion check to
433 detect bad FUNCs code. */
434
f9c696d2
AC
435 /* Restore the cleanup chain, the error/quit messages, and the uiout
436 builder, to their original states. */
c906108c
SS
437
438 restore_cleanups (saved_cleanup_chain);
439
f9c696d2
AC
440 uiout = saved_uiout;
441
c906108c 442 if (mask & RETURN_MASK_QUIT)
99eeeb0f
ND
443 quit_pre_print = saved_quit_pre_print;
444 if (mask & RETURN_MASK_ERROR)
445 error_pre_print = saved_error_pre_print;
446
f9c696d2
AC
447 /* Return normally if no error/quit event occurred or this catcher
448 can handle this exception. The caller analyses the func return
449 values. */
99eeeb0f 450
f9c696d2
AC
451 if (!caught || (mask & RETURN_MASK (caught)))
452 {
453 *func_val = val;
454 *func_caught = caught;
455 return;
456 }
99eeeb0f 457
f9c696d2 458 /* The caller didn't request that the event be caught, relay the
99eeeb0f
ND
459 event to the next containing catch_errors(). */
460
f9c696d2
AC
461 return_to_top_level (caught);
462}
99eeeb0f 463
f9c696d2
AC
464int
465catch_exceptions (struct ui_out *uiout,
466 catch_exceptions_ftype *func,
467 void *func_args,
468 char *errstring,
469 return_mask mask)
470{
471 int val;
472 enum return_reason caught;
473 catcher (func, uiout, func_args, &val, &caught, errstring, mask);
474 gdb_assert (val >= 0);
475 gdb_assert (caught <= 0);
476 if (caught < 0)
477 return caught;
478 return val;
479}
99eeeb0f 480
f9c696d2
AC
481struct catch_errors_args
482{
483 catch_errors_ftype *func;
484 void *func_args;
485};
99eeeb0f 486
f9c696d2
AC
487int
488do_catch_errors (struct ui_out *uiout, void *data)
489{
490 struct catch_errors_args *args = data;
491 return args->func (args->func_args);
492}
99eeeb0f 493
f9c696d2
AC
494int
495catch_errors (catch_errors_ftype *func, void *func_args, char *errstring,
496 return_mask mask)
497{
498 int val;
499 enum return_reason caught;
500 struct catch_errors_args args;
501 args.func = func;
502 args.func_args = func_args;
503 catcher (do_catch_errors, uiout, &args, &val, &caught, errstring, mask);
504 if (caught != 0)
505 return 0;
506 return val;
c906108c
SS
507}
508
11cf8741
JM
509struct captured_command_args
510 {
511 catch_command_errors_ftype *command;
512 char *arg;
513 int from_tty;
514 };
515
516static int
517do_captured_command (void *data)
518{
519 struct captured_command_args *context = data;
520 context->command (context->arg, context->from_tty);
521 /* FIXME: cagney/1999-11-07: Technically this do_cleanups() call
522 isn't needed. Instead an assertion check could be made that
523 simply confirmed that the called function correctly cleaned up
e26cc349 524 after itself. Unfortunately, old code (prior to 1999-11-04) in
11cf8741
JM
525 main.c was calling SET_TOP_LEVEL(), calling the command function,
526 and then *always* calling do_cleanups(). For the moment we
527 remain ``bug compatible'' with that old code.. */
528 do_cleanups (ALL_CLEANUPS);
529 return 1;
530}
531
532int
eb2f494a 533catch_command_errors (catch_command_errors_ftype * command,
11cf8741
JM
534 char *arg, int from_tty, return_mask mask)
535{
536 struct captured_command_args args;
537 args.command = command;
538 args.arg = arg;
539 args.from_tty = from_tty;
540 return catch_errors (do_captured_command, &args, "", mask);
541}
542
543
c906108c
SS
544/* Handler for SIGHUP. */
545
546#ifdef SIGHUP
c906108c
SS
547/* Just a little helper function for disconnect(). */
548
392a587b 549/* NOTE 1999-04-29: This function will be static again, once we modify
cd0fc7c3
SS
550 gdb to use the event loop as the default command loop and we merge
551 event-top.c into this file, top.c */
552/* static */ int
d0c8cdfb 553quit_cover (void *s)
c906108c 554{
c5aa993b
JM
555 caution = 0; /* Throw caution to the wind -- we're exiting.
556 This prevents asking the user dumb questions. */
557 quit_command ((char *) 0, 0);
c906108c
SS
558 return 0;
559}
64cdedad
EZ
560
561static void
562disconnect (int signo)
563{
564 catch_errors (quit_cover, NULL,
565 "Could not kill the program being debugged", RETURN_MASK_ALL);
566 signal (SIGHUP, SIG_DFL);
567 kill (getpid (), SIGHUP);
568}
c906108c
SS
569#endif /* defined SIGHUP */
570\f
571/* Line number we are currently in in a file which is being sourced. */
392a587b 572/* NOTE 1999-04-29: This variable will be static again, once we modify
cd0fc7c3
SS
573 gdb to use the event loop as the default command loop and we merge
574 event-top.c into this file, top.c */
575/* static */ int source_line_number;
c906108c
SS
576
577/* Name of the file we are sourcing. */
392a587b 578/* NOTE 1999-04-29: This variable will be static again, once we modify
cd0fc7c3
SS
579 gdb to use the event loop as the default command loop and we merge
580 event-top.c into this file, top.c */
581/* static */ char *source_file_name;
c906108c
SS
582
583/* Buffer containing the error_pre_print used by the source stuff.
584 Malloc'd. */
392a587b 585/* NOTE 1999-04-29: This variable will be static again, once we modify
cd0fc7c3
SS
586 gdb to use the event loop as the default command loop and we merge
587 event-top.c into this file, top.c */
588/* static */ char *source_error;
c906108c
SS
589static int source_error_allocated;
590
591/* Something to glom on to the start of error_pre_print if source_file_name
592 is set. */
392a587b 593/* NOTE 1999-04-29: This variable will be static again, once we modify
cd0fc7c3
SS
594 gdb to use the event loop as the default command loop and we merge
595 event-top.c into this file, top.c */
596/* static */ char *source_pre_error;
c906108c
SS
597
598/* Clean up on error during a "source" command (or execution of a
599 user-defined command). */
600
d318976c 601void
e41a3b1a 602do_restore_instream_cleanup (void *stream)
c906108c
SS
603{
604 /* Restore the previous input stream. */
605 instream = stream;
606}
607
608/* Read commands from STREAM. */
609void
fba45db2 610read_command_file (FILE *stream)
c906108c
SS
611{
612 struct cleanup *cleanups;
613
e41a3b1a 614 cleanups = make_cleanup (do_restore_instream_cleanup, instream);
c906108c 615 instream = stream;
c5aa993b 616 command_loop ();
c906108c
SS
617 do_cleanups (cleanups);
618}
619\f
507f3c78 620void (*pre_init_ui_hook) (void);
c906108c 621
e41a3b1a
AC
622#ifdef __MSDOS__
623void
624do_chdir_cleanup (void *old_dir)
625{
626 chdir (old_dir);
b8c9b27d 627 xfree (old_dir);
e41a3b1a
AC
628}
629#endif
630
d318976c
FN
631/* Execute the line P as a command.
632 Pass FROM_TTY as second argument to the defining function. */
c906108c 633
d318976c
FN
634void
635execute_command (char *p, int from_tty)
c906108c 636{
d318976c
FN
637 register struct cmd_list_element *c;
638 register enum language flang;
639 static int warned = 0;
640 char *line;
67e1e03a 641
d318976c 642 free_all_values ();
c906108c 643
d318976c
FN
644 /* Force cleanup of any alloca areas if using C alloca instead of
645 a builtin alloca. */
646 alloca (0);
c906108c 647
d318976c
FN
648 /* This can happen when command_line_input hits end of file. */
649 if (p == NULL)
650 return;
c906108c 651
d318976c 652 serial_log_command (p);
8b93c638 653
d318976c
FN
654 while (*p == ' ' || *p == '\t')
655 p++;
656 if (*p)
8b93c638 657 {
d318976c
FN
658 char *arg;
659 line = p;
8b93c638 660
d318976c 661 c = lookup_cmd (&p, cmdlist, "", 0, 1);
8b93c638 662
d318976c
FN
663 /* If the target is running, we allow only a limited set of
664 commands. */
665 if (event_loop_p && target_can_async_p () && target_executing)
666 if (!strcmp (c->name, "help")
667 && !strcmp (c->name, "pwd")
668 && !strcmp (c->name, "show")
669 && !strcmp (c->name, "stop"))
670 error ("Cannot execute this command while the target is running.");
8b93c638 671
d318976c
FN
672 /* Pass null arg rather than an empty one. */
673 arg = *p ? p : 0;
8b93c638 674
d318976c
FN
675 /* Clear off trailing whitespace, except for set and complete command. */
676 if (arg
677 && c->type != set_cmd
678 && !is_complete_command (c->function.cfunc))
8b93c638 679 {
d318976c
FN
680 p = arg + strlen (arg) - 1;
681 while (p >= arg && (*p == ' ' || *p == '\t'))
682 p--;
683 *(p + 1) = '\0';
8b93c638
JM
684 }
685
d318976c
FN
686 /* If this command has been pre-hooked, run the hook first. */
687 if ((c->hook_pre) && (!c->hook_in))
688 {
689 c->hook_in = 1; /* Prevent recursive hooking */
690 execute_user_command (c->hook_pre, (char *) 0);
691 c->hook_in = 0; /* Allow hook to work again once it is complete */
692 }
c906108c 693
d318976c
FN
694 if (c->flags & DEPRECATED_WARN_USER)
695 deprecated_cmd_warning (&line);
c906108c 696
d318976c
FN
697 if (c->class == class_user)
698 execute_user_command (c, arg);
699 else if (c->type == set_cmd || c->type == show_cmd)
700 do_setshow_command (arg, from_tty & caution, c);
701 else if (c->function.cfunc == NO_FUNCTION)
702 error ("That is not a command, just a help topic.");
703 else if (call_command_hook)
704 call_command_hook (c, arg, from_tty & caution);
705 else
706 (*c->function.cfunc) (arg, from_tty & caution);
707
708 /* If this command has been post-hooked, run the hook last. */
709 if ((c->hook_post) && (!c->hook_in))
710 {
711 c->hook_in = 1; /* Prevent recursive hooking */
712 execute_user_command (c->hook_post, (char *) 0);
713 c->hook_in = 0; /* allow hook to work again once it is complete */
714 }
c906108c 715
c906108c
SS
716 }
717
d318976c
FN
718 /* Tell the user if the language has changed (except first time). */
719 if (current_language != expected_language)
c906108c 720 {
d318976c 721 if (language_mode == language_mode_auto)
c906108c 722 {
d318976c 723 language_info (1); /* Print what changed. */
c906108c 724 }
d318976c 725 warned = 0;
c906108c
SS
726 }
727
d318976c
FN
728 /* Warn the user if the working language does not match the
729 language of the current frame. Only warn the user if we are
730 actually running the program, i.e. there is a stack. */
731 /* FIXME: This should be cacheing the frame and only running when
732 the frame changes. */
733
734 if (target_has_stack)
c906108c 735 {
d318976c
FN
736 flang = get_frame_language ();
737 if (!warned
738 && flang != language_unknown
739 && flang != current_language->la_language)
c906108c 740 {
d318976c
FN
741 printf_filtered ("%s\n", lang_frame_mismatch_warn);
742 warned = 1;
c906108c 743 }
c906108c
SS
744 }
745}
746
d318976c
FN
747/* Read commands from `instream' and execute them
748 until end of file or error reading instream. */
c906108c 749
d318976c
FN
750void
751command_loop (void)
c906108c 752{
d318976c
FN
753 struct cleanup *old_chain;
754 char *command;
755 int stdin_is_tty = ISATTY (stdin);
756 long time_at_cmd_start;
757#ifdef HAVE_SBRK
758 long space_at_cmd_start = 0;
759#endif
760 extern int display_time;
761 extern int display_space;
c5aa993b 762
d318976c
FN
763 while (instream && !feof (instream))
764 {
d318976c
FN
765 if (window_hook && instream == stdin)
766 (*window_hook) (instream, get_prompt ());
c906108c 767
d318976c
FN
768 quit_flag = 0;
769 if (instream == stdin && stdin_is_tty)
770 reinitialize_more_filter ();
771 old_chain = make_cleanup (null_cleanup, 0);
c906108c 772
d318976c
FN
773 /* Get a command-line. This calls the readline package. */
774 command = command_line_input (instream == stdin ?
775 get_prompt () : (char *) NULL,
776 instream == stdin, "prompt");
d318976c
FN
777 if (command == 0)
778 return;
c906108c 779
d318976c 780 time_at_cmd_start = get_run_time ();
c906108c 781
d318976c 782 if (display_space)
9e0b60a8 783 {
d318976c
FN
784#ifdef HAVE_SBRK
785 extern char **environ;
786 char *lim = (char *) sbrk (0);
9e0b60a8 787
d318976c 788 space_at_cmd_start = (long) (lim - (char *) &environ);
9e0b60a8 789#endif
d318976c 790 }
9e0b60a8 791
d318976c
FN
792 execute_command (command, instream == stdin);
793 /* Do any commands attached to breakpoint we stopped at. */
794 bpstat_do_actions (&stop_bpstat);
795 do_cleanups (old_chain);
9e0b60a8 796
d318976c 797 if (display_time)
9e0b60a8 798 {
d318976c 799 long cmd_time = get_run_time () - time_at_cmd_start;
9e0b60a8 800
d318976c
FN
801 printf_unfiltered ("Command execution time: %ld.%06ld\n",
802 cmd_time / 1000000, cmd_time % 1000000);
9e0b60a8 803 }
9e0b60a8 804
d318976c 805 if (display_space)
9e0b60a8 806 {
d318976c
FN
807#ifdef HAVE_SBRK
808 extern char **environ;
809 char *lim = (char *) sbrk (0);
810 long space_now = lim - (char *) &environ;
811 long space_diff = space_now - space_at_cmd_start;
812
813 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
814 space_now,
815 (space_diff >= 0 ? '+' : '-'),
816 space_diff);
817#endif
9e0b60a8 818 }
9e0b60a8 819 }
9e0b60a8 820}
9e0b60a8 821
d318976c
FN
822/* Read commands from `instream' and execute them until end of file or
823 error reading instream. This command loop doesnt care about any
824 such things as displaying time and space usage. If the user asks
825 for those, they won't work. */
826void
827simplified_command_loop (char *(*read_input_func) (char *),
828 void (*execute_command_func) (char *, int))
9e0b60a8 829{
d318976c
FN
830 struct cleanup *old_chain;
831 char *command;
832 int stdin_is_tty = ISATTY (stdin);
9e0b60a8 833
d318976c 834 while (instream && !feof (instream))
9e0b60a8 835 {
d318976c
FN
836 quit_flag = 0;
837 if (instream == stdin && stdin_is_tty)
838 reinitialize_more_filter ();
839 old_chain = make_cleanup (null_cleanup, 0);
840
841 /* Get a command-line. */
842 command = (*read_input_func) (instream == stdin ?
843 get_prompt () : (char *) NULL);
9e0b60a8 844
d318976c
FN
845 if (command == 0)
846 return;
9e0b60a8 847
d318976c 848 (*execute_command_func) (command, instream == stdin);
9e0b60a8 849
d318976c
FN
850 /* Do any commands attached to breakpoint we stopped at. */
851 bpstat_do_actions (&stop_bpstat);
9e0b60a8 852
d318976c 853 do_cleanups (old_chain);
c5aa993b 854 }
9e0b60a8 855}
d318976c
FN
856\f
857/* Commands call this if they do not want to be repeated by null lines. */
9e0b60a8 858
d318976c
FN
859void
860dont_repeat (void)
9e0b60a8 861{
d318976c
FN
862 if (server_command)
863 return;
9e0b60a8 864
d318976c
FN
865 /* If we aren't reading from standard input, we are saving the last
866 thing read from stdin in line and don't want to delete it. Null lines
867 won't repeat here in any case. */
868 if (instream == stdin)
869 *line = 0;
9e0b60a8 870}
d318976c
FN
871\f
872/* Read a line from the stream "instream" without command line editing.
9e0b60a8 873
d318976c
FN
874 It prints PROMPT_ARG once at the start.
875 Action is compatible with "readline", e.g. space for the result is
876 malloc'd and should be freed by the caller.
9e0b60a8 877
d318976c
FN
878 A NULL return means end of file. */
879char *
880gdb_readline (char *prompt_arg)
9e0b60a8 881{
d318976c
FN
882 int c;
883 char *result;
884 int input_index = 0;
885 int result_size = 80;
9e0b60a8 886
d318976c 887 if (prompt_arg)
9e0b60a8 888 {
d318976c
FN
889 /* Don't use a _filtered function here. It causes the assumed
890 character position to be off, since the newline we read from
891 the user is not accounted for. */
892 fputs_unfiltered (prompt_arg, gdb_stdout);
d036b4d9
AC
893 /* OBSOLETE #ifdef MPW */
894 /* OBSOLETE Move to a new line so the entered line doesn't have a prompt */
895 /* OBSOLETE on the front of it. */
896 /* OBSOLETE fputs_unfiltered ("\n", gdb_stdout); */
897 /* OBSOLETE #endif *//* MPW */
9e0b60a8
JM
898 gdb_flush (gdb_stdout);
899 }
900
d318976c 901 result = (char *) xmalloc (result_size);
9e0b60a8
JM
902
903 while (1)
904 {
d318976c
FN
905 /* Read from stdin if we are executing a user defined command.
906 This is the right thing for prompt_for_continue, at least. */
907 c = fgetc (instream ? instream : stdin);
9e0b60a8 908
d318976c 909 if (c == EOF)
9e0b60a8 910 {
d318976c
FN
911 if (input_index > 0)
912 /* The last line does not end with a newline. Return it, and
913 if we are called again fgetc will still return EOF and
914 we'll return NULL then. */
9e0b60a8 915 break;
b8c9b27d 916 xfree (result);
d318976c 917 return NULL;
9e0b60a8 918 }
c5aa993b 919
d318976c
FN
920 if (c == '\n')
921#ifndef CRLF_SOURCE_FILES
922 break;
923#else
9e0b60a8 924 {
d318976c
FN
925 if (input_index > 0 && result[input_index - 1] == '\r')
926 input_index--;
927 break;
9e0b60a8 928 }
d318976c 929#endif
9e0b60a8 930
d318976c
FN
931 result[input_index++] = c;
932 while (input_index >= result_size)
9e0b60a8 933 {
d318976c
FN
934 result_size *= 2;
935 result = (char *) xrealloc (result, result_size);
9e0b60a8 936 }
9e0b60a8
JM
937 }
938
d318976c
FN
939 result[input_index++] = '\0';
940 return result;
9e0b60a8
JM
941}
942
d318976c
FN
943/* Variables which control command line editing and history
944 substitution. These variables are given default values at the end
945 of this file. */
946static int command_editing_p;
947/* NOTE 1999-04-29: This variable will be static again, once we modify
948 gdb to use the event loop as the default command loop and we merge
949 event-top.c into this file, top.c */
950/* static */ int history_expansion_p;
951static int write_history_p;
952static int history_size;
953static char *history_filename;
9e0b60a8 954
9e0b60a8 955\f
d318976c
FN
956#ifdef STOP_SIGNAL
957static void
958stop_sig (int signo)
9e0b60a8 959{
d318976c
FN
960#if STOP_SIGNAL == SIGTSTP
961 signal (SIGTSTP, SIG_DFL);
46711df8
MK
962#if HAVE_SIGPROCMASK
963 {
964 sigset_t zero;
965
966 sigemptyset (&zero);
967 sigprocmask (SIG_SETMASK, &zero, 0);
968 }
969#elif HAVE_SIGSETMASK
d318976c 970 sigsetmask (0);
46711df8 971#endif
d318976c
FN
972 kill (getpid (), SIGTSTP);
973 signal (SIGTSTP, stop_sig);
974#else
975 signal (STOP_SIGNAL, stop_sig);
976#endif
977 printf_unfiltered ("%s", get_prompt ());
978 gdb_flush (gdb_stdout);
9e0b60a8 979
d318976c
FN
980 /* Forget about any previous command -- null line now will do nothing. */
981 dont_repeat ();
9e0b60a8 982}
d318976c 983#endif /* STOP_SIGNAL */
9e0b60a8 984
d318976c 985/* Initialize signal handlers. */
64cdedad
EZ
986static void
987float_handler (int signo)
988{
989 /* This message is based on ANSI C, section 4.7. Note that integer
990 divide by zero causes this, so "float" is a misnomer. */
991 signal (SIGFPE, float_handler);
992 error ("Erroneous arithmetic operation.");
993}
994
9e0b60a8 995static void
d318976c 996do_nothing (int signo)
9e0b60a8 997{
d318976c
FN
998 /* Under System V the default disposition of a signal is reinstated after
999 the signal is caught and delivered to an application process. On such
1000 systems one must restore the replacement signal handler if one wishes
1001 to continue handling the signal in one's program. On BSD systems this
1002 is not needed but it is harmless, and it simplifies the code to just do
1003 it unconditionally. */
1004 signal (signo, do_nothing);
9e0b60a8
JM
1005}
1006
9e0b60a8 1007static void
d318976c 1008init_signals (void)
9e0b60a8 1009{
d318976c 1010 signal (SIGINT, request_quit);
9e0b60a8 1011
d318976c
FN
1012 /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
1013 to the inferior and breakpoints will be ignored. */
1014#ifdef SIGTRAP
1015 signal (SIGTRAP, SIG_DFL);
1016#endif
9e0b60a8 1017
d318976c
FN
1018 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1019 passed to the inferior, which we don't want. It would be
1020 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1021 on BSD4.3 systems using vfork, that can affect the
1022 GDB process as well as the inferior (the signal handling tables
1023 might be in memory, shared between the two). Since we establish
1024 a handler for SIGQUIT, when we call exec it will set the signal
1025 to SIG_DFL for us. */
1026 signal (SIGQUIT, do_nothing);
1027#ifdef SIGHUP
1028 if (signal (SIGHUP, do_nothing) != SIG_IGN)
1029 signal (SIGHUP, disconnect);
1030#endif
1031 signal (SIGFPE, float_handler);
9e0b60a8 1032
d318976c
FN
1033#if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1034 signal (SIGWINCH, SIGWINCH_HANDLER);
1035#endif
9e0b60a8
JM
1036}
1037\f
d318976c
FN
1038/* Read one line from the command input stream `instream'
1039 into the local static buffer `linebuffer' (whose current length
1040 is `linelength').
1041 The buffer is made bigger as necessary.
1042 Returns the address of the start of the line.
9e0b60a8 1043
d318976c 1044 NULL is returned for end of file.
9e0b60a8 1045
d318976c
FN
1046 *If* the instream == stdin & stdin is a terminal, the line read
1047 is copied into the file line saver (global var char *line,
1048 length linesize) so that it can be duplicated.
9e0b60a8 1049
d318976c
FN
1050 This routine either uses fancy command line editing or
1051 simple input as the user has requested. */
10689f25 1052
d318976c
FN
1053char *
1054command_line_input (char *prompt_arg, int repeat, char *annotation_suffix)
9e0b60a8 1055{
d318976c
FN
1056 static char *linebuffer = 0;
1057 static unsigned linelength = 0;
9e0b60a8 1058 register char *p;
d318976c
FN
1059 char *p1;
1060 char *rl;
1061 char *local_prompt = prompt_arg;
1062 char *nline;
1063 char got_eof = 0;
1064
1065 /* The annotation suffix must be non-NULL. */
1066 if (annotation_suffix == NULL)
1067 annotation_suffix = "";
9e0b60a8 1068
d318976c
FN
1069 if (annotation_level > 1 && instream == stdin)
1070 {
1071 local_prompt = alloca ((prompt_arg == NULL ? 0 : strlen (prompt_arg))
1072 + strlen (annotation_suffix) + 40);
1073 if (prompt_arg == NULL)
1074 local_prompt[0] = '\0';
1075 else
1076 strcpy (local_prompt, prompt_arg);
1077 strcat (local_prompt, "\n\032\032");
1078 strcat (local_prompt, annotation_suffix);
1079 strcat (local_prompt, "\n");
1080 }
9e0b60a8 1081
d318976c 1082 if (linebuffer == 0)
9e0b60a8 1083 {
d318976c
FN
1084 linelength = 80;
1085 linebuffer = (char *) xmalloc (linelength);
9e0b60a8 1086 }
9e0b60a8 1087
d318976c 1088 p = linebuffer;
9e0b60a8 1089
d318976c
FN
1090 /* Control-C quits instantly if typed while in this loop
1091 since it should not wait until the user types a newline. */
1092 immediate_quit++;
1093#ifdef STOP_SIGNAL
1094 if (job_control)
73bc900d 1095 {
d318976c
FN
1096 if (event_loop_p)
1097 signal (STOP_SIGNAL, handle_stop_sig);
1098 else
1099 signal (STOP_SIGNAL, stop_sig);
1100 }
1101#endif
1102
1103 while (1)
9e0b60a8 1104 {
d318976c
FN
1105 /* Make sure that all output has been output. Some machines may let
1106 you get away with leaving out some of the gdb_flush, but not all. */
1107 wrap_here ("");
1108 gdb_flush (gdb_stdout);
1109 gdb_flush (gdb_stderr);
1110
1111 if (source_file_name != NULL)
1112 {
1113 ++source_line_number;
1114 sprintf (source_error,
1115 "%s%s:%d: Error in sourced command file:\n",
1116 source_pre_error,
1117 source_file_name,
1118 source_line_number);
1119 error_pre_print = source_error;
1120 }
1121
1122 if (annotation_level > 1 && instream == stdin)
1123 {
1124 printf_unfiltered ("\n\032\032pre-");
1125 printf_unfiltered (annotation_suffix);
1126 printf_unfiltered ("\n");
1127 }
1128
1129 /* Don't use fancy stuff if not talking to stdin. */
1130 if (readline_hook && instream == NULL)
1131 {
1132 rl = (*readline_hook) (local_prompt);
1133 }
1134 else if (command_editing_p && instream == stdin && ISATTY (instream))
1135 {
1136 rl = readline (local_prompt);
1137 }
9e0b60a8 1138 else
d318976c
FN
1139 {
1140 rl = gdb_readline (local_prompt);
1141 }
9e0b60a8 1142
d318976c
FN
1143 if (annotation_level > 1 && instream == stdin)
1144 {
1145 printf_unfiltered ("\n\032\032post-");
1146 printf_unfiltered (annotation_suffix);
1147 printf_unfiltered ("\n");
1148 }
9e0b60a8 1149
d318976c 1150 if (!rl || rl == (char *) EOF)
9e0b60a8 1151 {
d318976c
FN
1152 got_eof = 1;
1153 break;
9e0b60a8 1154 }
d318976c
FN
1155 if (strlen (rl) + 1 + (p - linebuffer) > linelength)
1156 {
1157 linelength = strlen (rl) + 1 + (p - linebuffer);
1158 nline = (char *) xrealloc (linebuffer, linelength);
1159 p += nline - linebuffer;
1160 linebuffer = nline;
1161 }
1162 p1 = rl;
1163 /* Copy line. Don't copy null at end. (Leaves line alone
1164 if this was just a newline) */
1165 while (*p1)
1166 *p++ = *p1++;
9e0b60a8 1167
b8c9b27d 1168 xfree (rl); /* Allocated in readline. */
9e0b60a8 1169
d318976c
FN
1170 if (p == linebuffer || *(p - 1) != '\\')
1171 break;
9e0b60a8 1172
d318976c
FN
1173 p--; /* Put on top of '\'. */
1174 local_prompt = (char *) 0;
1175 }
9e0b60a8 1176
d318976c
FN
1177#ifdef STOP_SIGNAL
1178 if (job_control)
1179 signal (STOP_SIGNAL, SIG_DFL);
1180#endif
1181 immediate_quit--;
9e0b60a8 1182
d318976c
FN
1183 if (got_eof)
1184 return NULL;
9e0b60a8 1185
d318976c
FN
1186#define SERVER_COMMAND_LENGTH 7
1187 server_command =
1188 (p - linebuffer > SERVER_COMMAND_LENGTH)
1189 && STREQN (linebuffer, "server ", SERVER_COMMAND_LENGTH);
1190 if (server_command)
9e0b60a8 1191 {
d318976c
FN
1192 /* Note that we don't set `line'. Between this and the check in
1193 dont_repeat, this insures that repeating will still do the
1194 right thing. */
1195 *p = '\0';
1196 return linebuffer + SERVER_COMMAND_LENGTH;
9e0b60a8 1197 }
9e0b60a8 1198
d318976c
FN
1199 /* Do history expansion if that is wished. */
1200 if (history_expansion_p && instream == stdin
1201 && ISATTY (instream))
1202 {
1203 char *history_value;
1204 int expanded;
9e0b60a8 1205
d318976c
FN
1206 *p = '\0'; /* Insert null now. */
1207 expanded = history_expand (linebuffer, &history_value);
1208 if (expanded)
1209 {
1210 /* Print the changes. */
1211 printf_unfiltered ("%s\n", history_value);
9e0b60a8 1212
d318976c
FN
1213 /* If there was an error, call this function again. */
1214 if (expanded < 0)
1215 {
b8c9b27d 1216 xfree (history_value);
d318976c
FN
1217 return command_line_input (prompt_arg, repeat, annotation_suffix);
1218 }
1219 if (strlen (history_value) > linelength)
1220 {
1221 linelength = strlen (history_value) + 1;
1222 linebuffer = (char *) xrealloc (linebuffer, linelength);
1223 }
1224 strcpy (linebuffer, history_value);
1225 p = linebuffer + strlen (linebuffer);
b8c9b27d 1226 xfree (history_value);
d318976c
FN
1227 }
1228 }
9e0b60a8 1229
d318976c
FN
1230 /* If we just got an empty line, and that is supposed
1231 to repeat the previous command, return the value in the
1232 global buffer. */
1233 if (repeat && p == linebuffer)
1234 return line;
1235 for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++);
1236 if (repeat && !*p1)
1237 return line;
9e0b60a8 1238
d318976c 1239 *p = 0;
9e0b60a8 1240
d318976c
FN
1241 /* Add line to history if appropriate. */
1242 if (instream == stdin
1243 && ISATTY (stdin) && *linebuffer)
1244 add_history (linebuffer);
9e0b60a8 1245
d318976c
FN
1246 /* Note: lines consisting solely of comments are added to the command
1247 history. This is useful when you type a command, and then
1248 realize you don't want to execute it quite yet. You can comment
1249 out the command and then later fetch it from the value history
1250 and remove the '#'. The kill ring is probably better, but some
1251 people are in the habit of commenting things out. */
1252 if (*p1 == '#')
1253 *p1 = '\0'; /* Found a comment. */
9e0b60a8 1254
d318976c
FN
1255 /* Save into global buffer if appropriate. */
1256 if (repeat)
1257 {
1258 if (linelength > linesize)
1259 {
1260 line = xrealloc (line, linelength);
1261 linesize = linelength;
1262 }
1263 strcpy (line, linebuffer);
1264 return line;
1265 }
9e0b60a8 1266
d318976c 1267 return linebuffer;
9e0b60a8
JM
1268}
1269\f
1270/* Print the GDB banner. */
1271void
fba45db2 1272print_gdb_version (struct ui_file *stream)
9e0b60a8
JM
1273{
1274 /* From GNU coding standards, first line is meant to be easy for a
1275 program to parse, and is just canonical program name and version
1276 number, which starts after last space. */
1277
1278 fprintf_filtered (stream, "GNU gdb %s\n", version);
1279
1280 /* Second line is a copyright notice. */
1281
388e1ff2 1282 fprintf_filtered (stream, "Copyright 2001 Free Software Foundation, Inc.\n");
9e0b60a8
JM
1283
1284 /* Following the copyright is a brief statement that the program is
1285 free software, that users are free to copy and change it on
1286 certain conditions, that it is covered by the GNU GPL, and that
1287 there is no warranty. */
1288
1289 fprintf_filtered (stream, "\
1290GDB is free software, covered by the GNU General Public License, and you are\n\
1291welcome to change it and/or distribute copies of it under certain conditions.\n\
1292Type \"show copying\" to see the conditions.\n\
1293There is absolutely no warranty for GDB. Type \"show warranty\" for details.\n");
1294
1295 /* After the required info we print the configuration information. */
1296
1297 fprintf_filtered (stream, "This GDB was configured as \"");
1298 if (!STREQ (host_name, target_name))
1299 {
1300 fprintf_filtered (stream, "--host=%s --target=%s", host_name, target_name);
1301 }
1302 else
1303 {
1304 fprintf_filtered (stream, "%s", host_name);
1305 }
1306 fprintf_filtered (stream, "\".");
1307}
9e0b60a8
JM
1308\f
1309/* get_prompt: access method for the GDB prompt string. */
1310
1311#define MAX_PROMPT_SIZE 256
1312
1313/*
1314 * int get_prompt_1 (char * buf);
1315 *
1316 * Work-horse for get_prompt (called via catch_errors).
1317 * Argument is buffer to hold the formatted prompt.
1318 *
1319 * Returns: 1 for success (use formatted prompt)
1320 * 0 for failure (use gdb_prompt_string).
c5aa993b 1321 */
9e0b60a8
JM
1322
1323static int gdb_prompt_escape;
1324
1325static int
710b33bd 1326get_prompt_1 (void *data)
9e0b60a8 1327{
710b33bd 1328 char *formatted_prompt = data;
9e0b60a8
JM
1329 char *local_prompt;
1330
6426a772 1331 if (event_loop_p)
9e0b60a8
JM
1332 local_prompt = PROMPT (0);
1333 else
1334 local_prompt = gdb_prompt_string;
1335
1336
1337 if (gdb_prompt_escape == 0)
1338 {
c5aa993b 1339 return 0; /* do no formatting */
9e0b60a8 1340 }
c5aa993b
JM
1341 else
1342 /* formatted prompt */
9e0b60a8 1343 {
c5aa993b 1344 char fmt[40], *promptp, *outp, *tmp;
3d6d86c6 1345 struct value *arg_val;
c5aa993b
JM
1346 DOUBLEST doubleval;
1347 LONGEST longval;
9e0b60a8
JM
1348 CORE_ADDR addrval;
1349
1350 int i, len;
1351 struct type *arg_type, *elt_type;
1352
1353 promptp = local_prompt;
c5aa993b 1354 outp = formatted_prompt;
9e0b60a8
JM
1355
1356 while (*promptp != '\0')
1357 {
1358 int available = MAX_PROMPT_SIZE - (outp - formatted_prompt) - 1;
1359
1360 if (*promptp != gdb_prompt_escape)
1361 {
c5aa993b 1362 if (available >= 1) /* overflow protect */
9e0b60a8
JM
1363 *outp++ = *promptp++;
1364 }
1365 else
1366 {
1367 /* GDB prompt string contains escape char. Parse for arg.
c5aa993b
JM
1368 Two consecutive escape chars followed by arg followed by
1369 a comma means to insert the arg using a default format.
1370 Otherwise a printf format string may be included between
1371 the two escape chars. eg:
1372 %%foo, insert foo using default format
1373 %2.2f%foo, insert foo using "%2.2f" format
1374 A mismatch between the format string and the data type
1375 of "foo" is an error (which we don't know how to protect
1376 against). */
9e0b60a8
JM
1377
1378 fmt[0] = '\0'; /* assume null format string */
1379 if (promptp[1] == gdb_prompt_escape) /* double esc char */
1380 {
1381 promptp += 2; /* skip past two escape chars. */
1382 }
1383 else
1384 {
1385 /* extract format string from between two esc chars */
1386 i = 0;
c5aa993b
JM
1387 do
1388 {
1389 fmt[i++] = *promptp++; /* copy format string */
1390 }
1391 while (i < sizeof (fmt) - 1 &&
1392 *promptp != gdb_prompt_escape &&
1393 *promptp != '\0');
9e0b60a8
JM
1394
1395 if (*promptp != gdb_prompt_escape)
1396 error ("Syntax error at prompt position %d",
1397 promptp - local_prompt);
1398 else
1399 {
1400 promptp++; /* skip second escape char */
1401 fmt[i++] = '\0'; /* terminate the format string */
1402 }
1403 }
1404
1405 arg_val = parse_to_comma_and_eval (&promptp);
1406 if (*promptp == ',')
c5aa993b 1407 promptp++; /* skip past the comma */
9e0b60a8 1408 arg_type = check_typedef (VALUE_TYPE (arg_val));
c5aa993b 1409 switch (TYPE_CODE (arg_type))
9e0b60a8
JM
1410 {
1411 case TYPE_CODE_ARRAY:
1412 elt_type = check_typedef (TYPE_TARGET_TYPE (arg_type));
c5aa993b 1413 if (TYPE_LENGTH (arg_type) > 0 &&
9e0b60a8
JM
1414 TYPE_LENGTH (elt_type) == 1 &&
1415 TYPE_CODE (elt_type) == TYPE_CODE_INT)
1416 {
1417 int len = TYPE_LENGTH (arg_type);
1418
1419 if (VALUE_LAZY (arg_val))
1420 value_fetch_lazy (arg_val);
1421 tmp = VALUE_CONTENTS (arg_val);
1422
1423 if (len > available)
c5aa993b 1424 len = available; /* overflow protect */
9e0b60a8
JM
1425
1426 /* FIXME: how to protect GDB from crashing
c5aa993b 1427 from bad user-supplied format string? */
9e0b60a8
JM
1428 if (fmt[0] != 0)
1429 sprintf (outp, fmt, tmp);
1430 else
1431 strncpy (outp, tmp, len);
1432 outp[len] = '\0';
1433 }
1434 break;
1435 case TYPE_CODE_PTR:
1436 elt_type = check_typedef (TYPE_TARGET_TYPE (arg_type));
1aa20aa8 1437 addrval = value_as_address (arg_val);
9e0b60a8
JM
1438
1439 if (TYPE_LENGTH (elt_type) == 1 &&
c5aa993b 1440 TYPE_CODE (elt_type) == TYPE_CODE_INT &&
9e0b60a8
JM
1441 addrval != 0)
1442 {
1443 /* display it as a string */
1444 char *default_fmt = "%s";
1445 char *tmp;
1446 int err = 0;
1447
1448 /* Limiting the number of bytes that the following call
c5aa993b
JM
1449 will read protects us from sprintf overflow later. */
1450 i = target_read_string (addrval, /* src */
1451 &tmp, /* dest */
1452 available, /* len */
9e0b60a8
JM
1453 &err);
1454 if (err) /* read failed */
1455 error ("%s on target_read", safe_strerror (err));
1456
1457 tmp[i] = '\0'; /* force-terminate string */
1458 /* FIXME: how to protect GDB from crashing
c5aa993b
JM
1459 from bad user-supplied format string? */
1460 sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
9e0b60a8 1461 tmp);
b8c9b27d 1462 xfree (tmp);
9e0b60a8
JM
1463 }
1464 else
1465 {
1466 /* display it as a pointer */
1467 char *default_fmt = "0x%x";
1468
1469 /* FIXME: how to protect GDB from crashing
c5aa993b
JM
1470 from bad user-supplied format string? */
1471 if (available >= 16 /*? */ ) /* overflow protect */
9e0b60a8
JM
1472 sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
1473 (long) addrval);
1474 }
1475 break;
1476 case TYPE_CODE_FLT:
1477 {
1478 char *default_fmt = "%g";
1479
1480 doubleval = value_as_double (arg_val);
1481 /* FIXME: how to protect GDB from crashing
1482 from bad user-supplied format string? */
c5aa993b 1483 if (available >= 16 /*? */ ) /* overflow protect */
9e0b60a8
JM
1484 sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
1485 (double) doubleval);
1486 break;
1487 }
1488 case TYPE_CODE_INT:
1489 {
1490 char *default_fmt = "%d";
1491
1492 longval = value_as_long (arg_val);
1493 /* FIXME: how to protect GDB from crashing
1494 from bad user-supplied format string? */
c5aa993b 1495 if (available >= 16 /*? */ ) /* overflow protect */
9e0b60a8
JM
1496 sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
1497 (long) longval);
1498 break;
1499 }
1500 case TYPE_CODE_BOOL:
1501 {
1502 /* no default format for bool */
1503 longval = value_as_long (arg_val);
c5aa993b 1504 if (available >= 8 /*? */ ) /* overflow protect */
9e0b60a8
JM
1505 {
1506 if (longval)
1507 strcpy (outp, "<true>");
1508 else
1509 strcpy (outp, "<false>");
1510 }
1511 break;
1512 }
1513 case TYPE_CODE_ENUM:
1514 {
1515 /* no default format for enum */
1516 longval = value_as_long (arg_val);
1517 len = TYPE_NFIELDS (arg_type);
1518 /* find enum name if possible */
1519 for (i = 0; i < len; i++)
1520 if (TYPE_FIELD_BITPOS (arg_type, i) == longval)
c5aa993b 1521 break; /* match -- end loop */
9e0b60a8
JM
1522
1523 if (i < len) /* enum name found */
1524 {
1525 char *name = TYPE_FIELD_NAME (arg_type, i);
1526
1527 strncpy (outp, name, available);
1528 /* in casel available < strlen (name), */
1529 outp[available] = '\0';
1530 }
1531 else
1532 {
c5aa993b 1533 if (available >= 16 /*? */ ) /* overflow protect */
d4f3574e 1534 sprintf (outp, "%ld", (long) longval);
9e0b60a8
JM
1535 }
1536 break;
1537 }
1538 case TYPE_CODE_VOID:
1539 *outp = '\0';
1540 break; /* void type -- no output */
1541 default:
1542 error ("bad data type at prompt position %d",
1543 promptp - local_prompt);
1544 break;
1545 }
1546 outp += strlen (outp);
1547 }
1548 }
1549 *outp++ = '\0'; /* terminate prompt string */
1550 return 1;
1551 }
1552}
1553
1554char *
fba45db2 1555get_prompt (void)
9e0b60a8
JM
1556{
1557 static char buf[MAX_PROMPT_SIZE];
1558
c5aa993b 1559 if (catch_errors (get_prompt_1, buf, "bad formatted prompt: ",
9e0b60a8
JM
1560 RETURN_MASK_ALL))
1561 {
c5aa993b 1562 return &buf[0]; /* successful formatted prompt */
9e0b60a8
JM
1563 }
1564 else
1565 {
1566 /* Prompt could not be formatted. */
6426a772 1567 if (event_loop_p)
9e0b60a8
JM
1568 return PROMPT (0);
1569 else
1570 return gdb_prompt_string;
1571 }
1572}
1573
1574void
fba45db2 1575set_prompt (char *s)
9e0b60a8
JM
1576{
1577/* ??rehrauer: I don't know why this fails, since it looks as though
1578 assignments to prompt are wrapped in calls to savestring...
c5aa993b 1579 if (prompt != NULL)
b8c9b27d 1580 xfree (prompt);
c5aa993b 1581 */
6426a772 1582 if (event_loop_p)
9e0b60a8
JM
1583 PROMPT (0) = savestring (s, strlen (s));
1584 else
1585 gdb_prompt_string = savestring (s, strlen (s));
1586}
9e0b60a8 1587\f
c5aa993b 1588
9e0b60a8
JM
1589/* If necessary, make the user confirm that we should quit. Return
1590 non-zero if we should quit, zero if we shouldn't. */
1591
1592int
fba45db2 1593quit_confirm (void)
9e0b60a8 1594{
39f77062 1595 if (! ptid_equal (inferior_ptid, null_ptid) && target_has_execution)
9e0b60a8
JM
1596 {
1597 char *s;
1598
1599 /* This is something of a hack. But there's no reliable way to
c5aa993b
JM
1600 see if a GUI is running. The `use_windows' variable doesn't
1601 cut it. */
9e0b60a8
JM
1602 if (init_ui_hook)
1603 s = "A debugging session is active.\nDo you still want to close the debugger?";
1604 else if (attach_flag)
1605 s = "The program is running. Quit anyway (and detach it)? ";
1606 else
1607 s = "The program is running. Exit anyway? ";
1608
c5aa993b 1609 if (!query (s))
9e0b60a8
JM
1610 return 0;
1611 }
1612
1613 return 1;
1614}
1615
1616/* Quit without asking for confirmation. */
1617
1618void
fba45db2 1619quit_force (char *args, int from_tty)
9e0b60a8
JM
1620{
1621 int exit_code = 0;
1622
1623 /* An optional expression may be used to cause gdb to terminate with the
1624 value of that expression. */
1625 if (args)
1626 {
3d6d86c6 1627 struct value *val = parse_and_eval (args);
9e0b60a8
JM
1628
1629 exit_code = (int) value_as_long (val);
1630 }
1631
39f77062 1632 if (! ptid_equal (inferior_ptid, null_ptid) && target_has_execution)
9e0b60a8
JM
1633 {
1634 if (attach_flag)
1635 target_detach (args, from_tty);
1636 else
1637 target_kill ();
1638 }
1639
1640 /* UDI wants this, to kill the TIP. */
1641 target_close (1);
1642
1643 /* Save the history information if it is appropriate to do so. */
1644 if (write_history_p && history_filename)
1645 write_history (history_filename);
1646
c5aa993b 1647 do_final_cleanups (ALL_CLEANUPS); /* Do any final cleanups before exiting */
9e0b60a8 1648
9e0b60a8
JM
1649 exit (exit_code);
1650}
1651
9e0b60a8
JM
1652/* Returns whether GDB is running on a terminal and whether the user
1653 desires that questions be asked of them on that terminal. */
1654
1655int
fba45db2 1656input_from_terminal_p (void)
9e0b60a8
JM
1657{
1658 return gdb_has_a_terminal () && (instream == stdin) & caution;
1659}
1660\f
9e0b60a8
JM
1661/* ARGSUSED */
1662static void
fba45db2 1663dont_repeat_command (char *ignored, int from_tty)
9e0b60a8 1664{
c5aa993b
JM
1665 *line = 0; /* Can't call dont_repeat here because we're not
1666 necessarily reading from stdin. */
9e0b60a8
JM
1667}
1668\f
1669/* Functions to manipulate command line editing control variables. */
1670
1671/* Number of commands to print in each call to show_commands. */
1672#define Hist_print 10
d318976c 1673void
fba45db2 1674show_commands (char *args, int from_tty)
9e0b60a8
JM
1675{
1676 /* Index for history commands. Relative to history_base. */
1677 int offset;
1678
1679 /* Number of the history entry which we are planning to display next.
1680 Relative to history_base. */
1681 static int num = 0;
1682
1683 /* The first command in the history which doesn't exist (i.e. one more
1684 than the number of the last command). Relative to history_base. */
1685 int hist_len;
1686
9e0b60a8
JM
1687 /* Print out some of the commands from the command history. */
1688 /* First determine the length of the history list. */
1689 hist_len = history_size;
1690 for (offset = 0; offset < history_size; offset++)
1691 {
1692 if (!history_get (history_base + offset))
1693 {
1694 hist_len = offset;
1695 break;
1696 }
1697 }
1698
1699 if (args)
1700 {
1701 if (args[0] == '+' && args[1] == '\0')
1702 /* "info editing +" should print from the stored position. */
1703 ;
1704 else
1705 /* "info editing <exp>" should print around command number <exp>. */
0e828ed1 1706 num = (parse_and_eval_long (args) - history_base) - Hist_print / 2;
9e0b60a8
JM
1707 }
1708 /* "show commands" means print the last Hist_print commands. */
1709 else
1710 {
1711 num = hist_len - Hist_print;
1712 }
1713
1714 if (num < 0)
1715 num = 0;
1716
1717 /* If there are at least Hist_print commands, we want to display the last
1718 Hist_print rather than, say, the last 6. */
1719 if (hist_len - num < Hist_print)
1720 {
1721 num = hist_len - Hist_print;
1722 if (num < 0)
1723 num = 0;
1724 }
1725
1726 for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
1727 {
1728 printf_filtered ("%5d %s\n", history_base + offset,
c5aa993b 1729 (history_get (history_base + offset))->line);
9e0b60a8
JM
1730 }
1731
1732 /* The next command we want to display is the next one that we haven't
1733 displayed yet. */
1734 num += Hist_print;
1735
1736 /* If the user repeats this command with return, it should do what
1737 "show commands +" does. This is unnecessary if arg is null,
1738 because "show commands +" is not useful after "show commands". */
1739 if (from_tty && args)
1740 {
1741 args[0] = '+';
1742 args[1] = '\0';
1743 }
1744}
1745
1746/* Called by do_setshow_command. */
1747/* ARGSUSED */
1748static void
fba45db2 1749set_history_size_command (char *args, int from_tty, struct cmd_list_element *c)
9e0b60a8
JM
1750{
1751 if (history_size == INT_MAX)
1752 unstifle_history ();
1753 else if (history_size >= 0)
1754 stifle_history (history_size);
1755 else
1756 {
1757 history_size = INT_MAX;
1758 error ("History size must be non-negative");
1759 }
1760}
1761
1762/* ARGSUSED */
d318976c 1763void
fba45db2 1764set_history (char *args, int from_tty)
9e0b60a8
JM
1765{
1766 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
1767 help_list (sethistlist, "set history ", -1, gdb_stdout);
1768}
1769
1770/* ARGSUSED */
d318976c 1771void
fba45db2 1772show_history (char *args, int from_tty)
9e0b60a8
JM
1773{
1774 cmd_show_list (showhistlist, from_tty, "");
1775}
1776
1777int info_verbose = 0; /* Default verbose msgs off */
1778
1779/* Called by do_setshow_command. An elaborate joke. */
1780/* ARGSUSED */
d318976c 1781void
fba45db2 1782set_verbose (char *args, int from_tty, struct cmd_list_element *c)
9e0b60a8
JM
1783{
1784 char *cmdname = "verbose";
1785 struct cmd_list_element *showcmd;
1786
1787 showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
1788
1789 if (info_verbose)
1790 {
1791 c->doc = "Set verbose printing of informational messages.";
1792 showcmd->doc = "Show verbose printing of informational messages.";
1793 }
1794 else
1795 {
1796 c->doc = "Set verbosity.";
1797 showcmd->doc = "Show verbosity.";
1798 }
1799}
1800
9e0b60a8
JM
1801/* Init the history buffer. Note that we are called after the init file(s)
1802 * have been read so that the user can change the history file via his
1803 * .gdbinit file (for instance). The GDBHISTFILE environment variable
1804 * overrides all of this.
1805 */
1806
1807void
fba45db2 1808init_history (void)
9e0b60a8
JM
1809{
1810 char *tmpenv;
1811
1812 tmpenv = getenv ("HISTSIZE");
1813 if (tmpenv)
1814 history_size = atoi (tmpenv);
1815 else if (!history_size)
1816 history_size = 256;
1817
1818 stifle_history (history_size);
1819
1820 tmpenv = getenv ("GDBHISTFILE");
1821 if (tmpenv)
c5aa993b
JM
1822 history_filename = savestring (tmpenv, strlen (tmpenv));
1823 else if (!history_filename)
1824 {
1825 /* We include the current directory so that if the user changes
1826 directories the file written will be the same as the one
1827 that was read. */
a0b3c4fd 1828#ifdef __MSDOS__
eb2f494a
AC
1829 /* No leading dots in file names are allowed on MSDOS. */
1830 history_filename = concat (current_directory, "/_gdb_history", NULL);
a0b3c4fd 1831#else
c5aa993b 1832 history_filename = concat (current_directory, "/.gdb_history", NULL);
a0b3c4fd 1833#endif
c5aa993b 1834 }
9e0b60a8
JM
1835 read_history (history_filename);
1836}
1837
1838static void
fba45db2 1839init_main (void)
9e0b60a8
JM
1840{
1841 struct cmd_list_element *c;
1842
1843 /* If we are running the asynchronous version,
1844 we initialize the prompts differently. */
6426a772 1845 if (!event_loop_p)
9e0b60a8 1846 {
c5aa993b 1847 gdb_prompt_string = savestring (DEFAULT_PROMPT, strlen (DEFAULT_PROMPT));
9e0b60a8
JM
1848 }
1849 else
1850 {
1851 /* initialize the prompt stack to a simple "(gdb) " prompt or to
96baa820 1852 whatever the DEFAULT_PROMPT is. */
9e0b60a8 1853 the_prompts.top = 0;
c5aa993b 1854 PREFIX (0) = "";
c5aa993b 1855 PROMPT (0) = savestring (DEFAULT_PROMPT, strlen (DEFAULT_PROMPT));
c5aa993b 1856 SUFFIX (0) = "";
9e0b60a8 1857 /* Set things up for annotation_level > 1, if the user ever decides
c5aa993b 1858 to use it. */
9e0b60a8
JM
1859 async_annotation_suffix = "prompt";
1860 /* Set the variable associated with the setshow prompt command. */
1861 new_async_prompt = savestring (PROMPT (0), strlen (PROMPT (0)));
0191bed7
EZ
1862
1863 /* If gdb was started with --annotate=2, this is equivalent to
1864 the user entering the command 'set annotate 2' at the gdb
1865 prompt, so we need to do extra processing. */
1866 if (annotation_level > 1)
1867 set_async_annotation_level (NULL, 0, NULL);
9e0b60a8
JM
1868 }
1869 gdb_prompt_escape = 0; /* default to none. */
1870
1871 /* Set the important stuff up for command editing. */
1872 command_editing_p = 1;
9e0b60a8
JM
1873 history_expansion_p = 0;
1874 write_history_p = 0;
1875
1876 /* Setup important stuff for command line editing. */
1877 rl_completion_entry_function = (int (*)()) readline_line_completion_function;
d318976c
FN
1878 rl_completer_word_break_characters =
1879 get_gdb_completer_word_break_characters ();
1880 rl_completer_quote_characters = get_gdb_completer_quote_characters ();
9e0b60a8
JM
1881 rl_readline_name = "gdb";
1882
9e0b60a8
JM
1883 /* The set prompt command is different depending whether or not the
1884 async version is run. NOTE: this difference is going to
1885 disappear as we make the event loop be the default engine of
1886 gdb. */
6426a772 1887 if (!event_loop_p)
9e0b60a8
JM
1888 {
1889 add_show_from_set
c5aa993b 1890 (add_set_cmd ("prompt", class_support, var_string,
9e0b60a8
JM
1891 (char *) &gdb_prompt_string, "Set gdb's prompt",
1892 &setlist),
1893 &showlist);
1894 }
1895 else
1896 {
c5aa993b
JM
1897 c = add_set_cmd ("prompt", class_support, var_string,
1898 (char *) &new_async_prompt, "Set gdb's prompt",
9e0b60a8
JM
1899 &setlist);
1900 add_show_from_set (c, &showlist);
1901 c->function.sfunc = set_async_prompt;
1902 }
1903
1904 add_show_from_set
c5aa993b 1905 (add_set_cmd ("prompt-escape-char", class_support, var_zinteger,
9e0b60a8
JM
1906 (char *) &gdb_prompt_escape,
1907 "Set escape character for formatting of gdb's prompt",
1908 &setlist),
1909 &showlist);
1910
9e0b60a8
JM
1911 add_com ("dont-repeat", class_support, dont_repeat_command, "Don't repeat this command.\n\
1912Primarily used inside of user-defined commands that should not be repeated when\n\
1913hitting return.");
1914
9e0b60a8
JM
1915 /* The set editing command is different depending whether or not the
1916 async version is run. NOTE: this difference is going to disappear
1917 as we make the event loop be the default engine of gdb. */
6426a772 1918 if (!event_loop_p)
9e0b60a8
JM
1919 {
1920 add_show_from_set
c5aa993b 1921 (add_set_cmd ("editing", class_support, var_boolean, (char *) &command_editing_p,
9e0b60a8
JM
1922 "Set editing of command lines as they are typed.\n\
1923Use \"on\" to enable the editing, and \"off\" to disable it.\n\
1924Without an argument, command line editing is enabled. To edit, use\n\
1925EMACS-like or VI-like commands like control-P or ESC.", &setlist),
1926 &showlist);
1927 }
1928 else
1929 {
c5aa993b 1930 c = add_set_cmd ("editing", class_support, var_boolean, (char *) &async_command_editing_p,
9e0b60a8
JM
1931 "Set editing of command lines as they are typed.\n\
1932Use \"on\" to enable the editing, and \"off\" to disable it.\n\
1933Without an argument, command line editing is enabled. To edit, use\n\
1934EMACS-like or VI-like commands like control-P or ESC.", &setlist);
1935
1936 add_show_from_set (c, &showlist);
1937 c->function.sfunc = set_async_editing_command;
1938 }
1939
9e0b60a8 1940 add_show_from_set
c5aa993b
JM
1941 (add_set_cmd ("save", no_class, var_boolean, (char *) &write_history_p,
1942 "Set saving of the history record on exit.\n\
9e0b60a8
JM
1943Use \"on\" to enable the saving, and \"off\" to disable it.\n\
1944Without an argument, saving is enabled.", &sethistlist),
1945 &showhistlist);
1946
c5aa993b 1947 c = add_set_cmd ("size", no_class, var_integer, (char *) &history_size,
9e0b60a8
JM
1948 "Set the size of the command history, \n\
1949ie. the number of previous commands to keep a record of.", &sethistlist);
1950 add_show_from_set (c, &showhistlist);
1951 c->function.sfunc = set_history_size_command;
1952
7a1bd56a
EZ
1953 c = add_set_cmd ("filename", no_class, var_filename,
1954 (char *) &history_filename,
1955 "Set the filename in which to record the command history\n\
1956 (the list of previous commands of which a record is kept).", &sethistlist);
1957 c->completer = filename_completer;
1958 add_show_from_set (c, &showhistlist);
9e0b60a8
JM
1959
1960 add_show_from_set
1961 (add_set_cmd ("confirm", class_support, var_boolean,
c5aa993b 1962 (char *) &caution,
9e0b60a8
JM
1963 "Set whether to confirm potentially dangerous operations.",
1964 &setlist),
1965 &showlist);
1966
9e0b60a8
JM
1967 /* The set annotate command is different depending whether or not
1968 the async version is run. NOTE: this difference is going to
1969 disappear as we make the event loop be the default engine of
1970 gdb. */
6426a772 1971 if (!event_loop_p)
9e0b60a8 1972 {
c5aa993b
JM
1973 c = add_set_cmd ("annotate", class_obscure, var_zinteger,
1974 (char *) &annotation_level, "Set annotation_level.\n\
9e0b60a8
JM
19750 == normal; 1 == fullname (for use when running under emacs)\n\
19762 == output annotated suitably for use by programs that control GDB.",
1977 &setlist);
1978 c = add_show_from_set (c, &showlist);
1979 }
1980 else
1981 {
c5aa993b
JM
1982 c = add_set_cmd ("annotate", class_obscure, var_zinteger,
1983 (char *) &annotation_level, "Set annotation_level.\n\
9e0b60a8
JM
19840 == normal; 1 == fullname (for use when running under emacs)\n\
19852 == output annotated suitably for use by programs that control GDB.",
c5aa993b 1986 &setlist);
9e0b60a8
JM
1987 add_show_from_set (c, &showlist);
1988 c->function.sfunc = set_async_annotation_level;
1989 }
6426a772 1990 if (event_loop_p)
104c1213
JM
1991 {
1992 add_show_from_set
1993 (add_set_cmd ("exec-done-display", class_support, var_boolean, (char *) &exec_done_display_p,
1994 "Set notification of completion for asynchronous execution commands.\n\
1995Use \"on\" to enable the notification, and \"off\" to disable it.", &setlist),
1996 &showlist);
1997 }
9e0b60a8 1998}
64cdedad
EZ
1999
2000void
2001gdb_init (char *argv0)
2002{
2003 if (pre_init_ui_hook)
2004 pre_init_ui_hook ();
2005
2006 /* Run the init function of each source file */
2007
2008 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
2009 current_directory = gdb_dirbuf;
2010
2011#ifdef __MSDOS__
2012 /* Make sure we return to the original directory upon exit, come
2013 what may, since the OS doesn't do that for us. */
2014 make_final_cleanup (do_chdir_cleanup, xstrdup (current_directory));
2015#endif
2016
2017 init_cmd_lists (); /* This needs to be done first */
2018 initialize_targets (); /* Setup target_terminal macros for utils.c */
2019 initialize_utils (); /* Make errors and warnings possible */
2020 initialize_all_files ();
2021 initialize_current_architecture ();
2022 init_cli_cmds();
2023 init_main (); /* But that omits this file! Do it now */
2024
2025 /* The signal handling mechanism is different depending whether or
2026 not the async version is run. NOTE: in the future we plan to make
2027 the event loop be the default engine of gdb, and this difference
2028 will disappear. */
2029 if (event_loop_p)
2030 async_init_signals ();
2031 else
2032 init_signals ();
2033
2034 /* We need a default language for parsing expressions, so simple things like
2035 "set width 0" won't fail if no language is explicitly set in a config file
2036 or implicitly set by reading an executable during startup. */
2037 set_language (language_c);
2038 expected_language = current_language; /* don't warn about the change. */
2039
1ad24239
KS
2040 /* Allow another UI to initialize. If the UI fails to initialize, and
2041 it wants GDB to revert to the CLI, it should clear init_ui_hook. */
2042 if (init_ui_hook)
2043 init_ui_hook (argv0);
2044
64cdedad
EZ
2045#ifdef UI_OUT
2046 /* Install the default UI */
2047 if (!init_ui_hook)
2048 {
2049 uiout = cli_out_new (gdb_stdout);
2050
2051 /* All the interpreters should have had a look at things by now.
2052 Initialize the selected interpreter. */
2053 if (interpreter_p)
2054 {
2055 fprintf_unfiltered (gdb_stderr, "Interpreter `%s' unrecognized.\n",
2056 interpreter_p);
2057 exit (1);
2058 }
2059 }
2060#endif
64cdedad 2061}
This page took 0.267375 seconds and 4 git commands to generate.