* stabsread.c (read_member_functions): Remove skip code for duplicate
[deliverable/binutils-gdb.git] / gdb / top.c
CommitLineData
c906108c 1/* Top level stuff for GDB, the GNU debugger.
a752853e
AC
2
3 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
5 Free Software Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b
JM
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
c906108c
SS
23
24#include "defs.h"
25#include "gdbcmd.h"
26#include "call-cmds.h"
210661e7
EZ
27#include "cli/cli-cmds.h"
28#include "cli/cli-script.h"
29#include "cli/cli-setshow.h"
c906108c
SS
30#include "symtab.h"
31#include "inferior.h"
042be3a9 32#include <signal.h>
c906108c
SS
33#include "target.h"
34#include "breakpoint.h"
35#include "gdbtypes.h"
36#include "expression.h"
37#include "value.h"
38#include "language.h"
c5aa993b 39#include "terminal.h" /* For job_control. */
c906108c 40#include "annotate.h"
c5f0f3d0 41#include "completer.h"
c906108c 42#include "top.h"
d4f3574e 43#include "version.h"
210661e7 44#include "serial.h"
d16aafd8 45#include "doublest.h"
f9c696d2 46#include "gdb_assert.h"
c906108c
SS
47
48/* readline include files */
49#include <readline/readline.h>
50#include <readline/history.h>
51
52/* readline defines this. */
53#undef savestring
54
55#include <sys/types.h>
c906108c 56
c4093a6a 57#include <setjmp.h>
2acceee2 58
c2c6d25f 59#include "event-top.h"
c906108c
SS
60#include "gdb_string.h"
61#include "gdb_stat.h"
62#include <ctype.h>
8b93c638
JM
63#include "ui-out.h"
64#include "cli-out.h"
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
b5a2688f 199 throw_exception(). */
c906108c 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
b5a2688f 301/* Where to go for throw_exception(). */
99eeeb0f 302static SIGJMP_BUF *catch_return;
c906108c 303
99eeeb0f 304/* Return for reason REASON to the nearest containing catch_errors(). */
c906108c 305
c2d11a7d 306NORETURN void
b5a2688f 307throw_exception (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
b5a2688f 343 function is aborted (using throw_exception() or zero if the
f9c696d2
AC
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
b5a2688f 461 throw_exception (caught);
f9c696d2 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
9f60d481
AC
675 /* FIXME: cagney/2002-02-02: The c->type test is pretty dodgy
676 while the is_complete_command(cfunc) test is just plain
677 bogus. They should both be replaced by a test of the form
678 c->strip_trailing_white_space_p. */
679 /* NOTE: cagney/2002-02-02: The function.cfunc in the below
680 can't be replaced with func. This is because it is the
681 cfunc, and not the func, that has the value that the
682 is_complete_command hack is testing for. */
683 /* Clear off trailing whitespace, except for set and complete
684 command. */
d318976c
FN
685 if (arg
686 && c->type != set_cmd
bbaca940 687 && !is_complete_command (c))
8b93c638 688 {
d318976c
FN
689 p = arg + strlen (arg) - 1;
690 while (p >= arg && (*p == ' ' || *p == '\t'))
691 p--;
692 *(p + 1) = '\0';
8b93c638
JM
693 }
694
d318976c 695 /* If this command has been pre-hooked, run the hook first. */
5913bcb0 696 execute_cmd_pre_hook (c);
c906108c 697
d318976c
FN
698 if (c->flags & DEPRECATED_WARN_USER)
699 deprecated_cmd_warning (&line);
c906108c 700
d318976c
FN
701 if (c->class == class_user)
702 execute_user_command (c, arg);
703 else if (c->type == set_cmd || c->type == show_cmd)
704 do_setshow_command (arg, from_tty & caution, c);
9f60d481 705 else if (c->func == NULL)
d318976c
FN
706 error ("That is not a command, just a help topic.");
707 else if (call_command_hook)
708 call_command_hook (c, arg, from_tty & caution);
709 else
9f60d481 710 (*c->func) (c, arg, from_tty & caution);
d318976c
FN
711
712 /* If this command has been post-hooked, run the hook last. */
5913bcb0 713 execute_cmd_post_hook (c);
c906108c 714
c906108c
SS
715 }
716
d318976c
FN
717 /* Tell the user if the language has changed (except first time). */
718 if (current_language != expected_language)
c906108c 719 {
d318976c 720 if (language_mode == language_mode_auto)
c906108c 721 {
d318976c 722 language_info (1); /* Print what changed. */
c906108c 723 }
d318976c 724 warned = 0;
c906108c
SS
725 }
726
d318976c
FN
727 /* Warn the user if the working language does not match the
728 language of the current frame. Only warn the user if we are
729 actually running the program, i.e. there is a stack. */
730 /* FIXME: This should be cacheing the frame and only running when
731 the frame changes. */
732
733 if (target_has_stack)
c906108c 734 {
d318976c
FN
735 flang = get_frame_language ();
736 if (!warned
737 && flang != language_unknown
738 && flang != current_language->la_language)
c906108c 739 {
d318976c
FN
740 printf_filtered ("%s\n", lang_frame_mismatch_warn);
741 warned = 1;
c906108c 742 }
c906108c
SS
743 }
744}
745
d318976c
FN
746/* Read commands from `instream' and execute them
747 until end of file or error reading instream. */
c906108c 748
d318976c
FN
749void
750command_loop (void)
c906108c 751{
d318976c
FN
752 struct cleanup *old_chain;
753 char *command;
754 int stdin_is_tty = ISATTY (stdin);
755 long time_at_cmd_start;
756#ifdef HAVE_SBRK
757 long space_at_cmd_start = 0;
758#endif
759 extern int display_time;
760 extern int display_space;
c5aa993b 761
d318976c
FN
762 while (instream && !feof (instream))
763 {
d318976c
FN
764 if (window_hook && instream == stdin)
765 (*window_hook) (instream, get_prompt ());
c906108c 766
d318976c
FN
767 quit_flag = 0;
768 if (instream == stdin && stdin_is_tty)
769 reinitialize_more_filter ();
770 old_chain = make_cleanup (null_cleanup, 0);
c906108c 771
d318976c
FN
772 /* Get a command-line. This calls the readline package. */
773 command = command_line_input (instream == stdin ?
774 get_prompt () : (char *) NULL,
775 instream == stdin, "prompt");
d318976c
FN
776 if (command == 0)
777 return;
c906108c 778
d318976c 779 time_at_cmd_start = get_run_time ();
c906108c 780
d318976c 781 if (display_space)
9e0b60a8 782 {
d318976c
FN
783#ifdef HAVE_SBRK
784 extern char **environ;
785 char *lim = (char *) sbrk (0);
9e0b60a8 786
d318976c 787 space_at_cmd_start = (long) (lim - (char *) &environ);
9e0b60a8 788#endif
d318976c 789 }
9e0b60a8 790
d318976c
FN
791 execute_command (command, instream == stdin);
792 /* Do any commands attached to breakpoint we stopped at. */
793 bpstat_do_actions (&stop_bpstat);
794 do_cleanups (old_chain);
9e0b60a8 795
d318976c 796 if (display_time)
9e0b60a8 797 {
d318976c 798 long cmd_time = get_run_time () - time_at_cmd_start;
9e0b60a8 799
d318976c
FN
800 printf_unfiltered ("Command execution time: %ld.%06ld\n",
801 cmd_time / 1000000, cmd_time % 1000000);
9e0b60a8 802 }
9e0b60a8 803
d318976c 804 if (display_space)
9e0b60a8 805 {
d318976c
FN
806#ifdef HAVE_SBRK
807 extern char **environ;
808 char *lim = (char *) sbrk (0);
809 long space_now = lim - (char *) &environ;
810 long space_diff = space_now - space_at_cmd_start;
811
812 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
813 space_now,
814 (space_diff >= 0 ? '+' : '-'),
815 space_diff);
816#endif
9e0b60a8 817 }
9e0b60a8 818 }
9e0b60a8 819}
9e0b60a8 820
d318976c
FN
821/* Read commands from `instream' and execute them until end of file or
822 error reading instream. This command loop doesnt care about any
823 such things as displaying time and space usage. If the user asks
824 for those, they won't work. */
825void
826simplified_command_loop (char *(*read_input_func) (char *),
827 void (*execute_command_func) (char *, int))
9e0b60a8 828{
d318976c
FN
829 struct cleanup *old_chain;
830 char *command;
831 int stdin_is_tty = ISATTY (stdin);
9e0b60a8 832
d318976c 833 while (instream && !feof (instream))
9e0b60a8 834 {
d318976c
FN
835 quit_flag = 0;
836 if (instream == stdin && stdin_is_tty)
837 reinitialize_more_filter ();
838 old_chain = make_cleanup (null_cleanup, 0);
839
840 /* Get a command-line. */
841 command = (*read_input_func) (instream == stdin ?
842 get_prompt () : (char *) NULL);
9e0b60a8 843
d318976c
FN
844 if (command == 0)
845 return;
9e0b60a8 846
d318976c 847 (*execute_command_func) (command, instream == stdin);
9e0b60a8 848
d318976c
FN
849 /* Do any commands attached to breakpoint we stopped at. */
850 bpstat_do_actions (&stop_bpstat);
9e0b60a8 851
d318976c 852 do_cleanups (old_chain);
c5aa993b 853 }
9e0b60a8 854}
d318976c
FN
855\f
856/* Commands call this if they do not want to be repeated by null lines. */
9e0b60a8 857
d318976c
FN
858void
859dont_repeat (void)
9e0b60a8 860{
d318976c
FN
861 if (server_command)
862 return;
9e0b60a8 863
d318976c
FN
864 /* If we aren't reading from standard input, we are saving the last
865 thing read from stdin in line and don't want to delete it. Null lines
866 won't repeat here in any case. */
867 if (instream == stdin)
868 *line = 0;
9e0b60a8 869}
d318976c
FN
870\f
871/* Read a line from the stream "instream" without command line editing.
9e0b60a8 872
d318976c
FN
873 It prints PROMPT_ARG once at the start.
874 Action is compatible with "readline", e.g. space for the result is
875 malloc'd and should be freed by the caller.
9e0b60a8 876
d318976c
FN
877 A NULL return means end of file. */
878char *
879gdb_readline (char *prompt_arg)
9e0b60a8 880{
d318976c
FN
881 int c;
882 char *result;
883 int input_index = 0;
884 int result_size = 80;
9e0b60a8 885
d318976c 886 if (prompt_arg)
9e0b60a8 887 {
d318976c
FN
888 /* Don't use a _filtered function here. It causes the assumed
889 character position to be off, since the newline we read from
890 the user is not accounted for. */
891 fputs_unfiltered (prompt_arg, gdb_stdout);
9e0b60a8
JM
892 gdb_flush (gdb_stdout);
893 }
894
d318976c 895 result = (char *) xmalloc (result_size);
9e0b60a8
JM
896
897 while (1)
898 {
d318976c
FN
899 /* Read from stdin if we are executing a user defined command.
900 This is the right thing for prompt_for_continue, at least. */
901 c = fgetc (instream ? instream : stdin);
9e0b60a8 902
d318976c 903 if (c == EOF)
9e0b60a8 904 {
d318976c
FN
905 if (input_index > 0)
906 /* The last line does not end with a newline. Return it, and
907 if we are called again fgetc will still return EOF and
908 we'll return NULL then. */
9e0b60a8 909 break;
b8c9b27d 910 xfree (result);
d318976c 911 return NULL;
9e0b60a8 912 }
c5aa993b 913
d318976c
FN
914 if (c == '\n')
915#ifndef CRLF_SOURCE_FILES
916 break;
917#else
9e0b60a8 918 {
d318976c
FN
919 if (input_index > 0 && result[input_index - 1] == '\r')
920 input_index--;
921 break;
9e0b60a8 922 }
d318976c 923#endif
9e0b60a8 924
d318976c
FN
925 result[input_index++] = c;
926 while (input_index >= result_size)
9e0b60a8 927 {
d318976c
FN
928 result_size *= 2;
929 result = (char *) xrealloc (result, result_size);
9e0b60a8 930 }
9e0b60a8
JM
931 }
932
d318976c
FN
933 result[input_index++] = '\0';
934 return result;
9e0b60a8
JM
935}
936
d318976c
FN
937/* Variables which control command line editing and history
938 substitution. These variables are given default values at the end
939 of this file. */
940static int command_editing_p;
941/* NOTE 1999-04-29: This variable will be static again, once we modify
942 gdb to use the event loop as the default command loop and we merge
943 event-top.c into this file, top.c */
944/* static */ int history_expansion_p;
945static int write_history_p;
946static int history_size;
947static char *history_filename;
9e0b60a8 948
9e0b60a8 949\f
d318976c
FN
950#ifdef STOP_SIGNAL
951static void
952stop_sig (int signo)
9e0b60a8 953{
d318976c
FN
954#if STOP_SIGNAL == SIGTSTP
955 signal (SIGTSTP, SIG_DFL);
46711df8
MK
956#if HAVE_SIGPROCMASK
957 {
958 sigset_t zero;
959
960 sigemptyset (&zero);
961 sigprocmask (SIG_SETMASK, &zero, 0);
962 }
963#elif HAVE_SIGSETMASK
d318976c 964 sigsetmask (0);
46711df8 965#endif
d318976c
FN
966 kill (getpid (), SIGTSTP);
967 signal (SIGTSTP, stop_sig);
968#else
969 signal (STOP_SIGNAL, stop_sig);
970#endif
971 printf_unfiltered ("%s", get_prompt ());
972 gdb_flush (gdb_stdout);
9e0b60a8 973
d318976c
FN
974 /* Forget about any previous command -- null line now will do nothing. */
975 dont_repeat ();
9e0b60a8 976}
d318976c 977#endif /* STOP_SIGNAL */
9e0b60a8 978
d318976c 979/* Initialize signal handlers. */
64cdedad
EZ
980static void
981float_handler (int signo)
982{
983 /* This message is based on ANSI C, section 4.7. Note that integer
984 divide by zero causes this, so "float" is a misnomer. */
985 signal (SIGFPE, float_handler);
986 error ("Erroneous arithmetic operation.");
987}
988
9e0b60a8 989static void
d318976c 990do_nothing (int signo)
9e0b60a8 991{
d318976c
FN
992 /* Under System V the default disposition of a signal is reinstated after
993 the signal is caught and delivered to an application process. On such
994 systems one must restore the replacement signal handler if one wishes
995 to continue handling the signal in one's program. On BSD systems this
996 is not needed but it is harmless, and it simplifies the code to just do
997 it unconditionally. */
998 signal (signo, do_nothing);
9e0b60a8
JM
999}
1000
9e0b60a8 1001static void
d318976c 1002init_signals (void)
9e0b60a8 1003{
d318976c 1004 signal (SIGINT, request_quit);
9e0b60a8 1005
d318976c
FN
1006 /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
1007 to the inferior and breakpoints will be ignored. */
1008#ifdef SIGTRAP
1009 signal (SIGTRAP, SIG_DFL);
1010#endif
9e0b60a8 1011
d318976c
FN
1012 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1013 passed to the inferior, which we don't want. It would be
1014 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1015 on BSD4.3 systems using vfork, that can affect the
1016 GDB process as well as the inferior (the signal handling tables
1017 might be in memory, shared between the two). Since we establish
1018 a handler for SIGQUIT, when we call exec it will set the signal
1019 to SIG_DFL for us. */
1020 signal (SIGQUIT, do_nothing);
1021#ifdef SIGHUP
1022 if (signal (SIGHUP, do_nothing) != SIG_IGN)
1023 signal (SIGHUP, disconnect);
1024#endif
1025 signal (SIGFPE, float_handler);
9e0b60a8 1026
d318976c
FN
1027#if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1028 signal (SIGWINCH, SIGWINCH_HANDLER);
1029#endif
9e0b60a8
JM
1030}
1031\f
467d8519
TT
1032/* The current saved history number from operate-and-get-next.
1033 This is -1 if not valid. */
1034static int operate_saved_history = -1;
1035
1036/* This is put on the appropriate hook and helps operate-and-get-next
1037 do its work. */
1038void
1039gdb_rl_operate_and_get_next_completion ()
1040{
1041 int delta = where_history () - operate_saved_history;
1042 /* The `key' argument to rl_get_previous_history is ignored. */
1043 rl_get_previous_history (delta, 0);
1044 operate_saved_history = -1;
1045
1046 /* readline doesn't automatically update the display for us. */
1047 rl_redisplay ();
1048
1049 after_char_processing_hook = NULL;
1050 rl_pre_input_hook = NULL;
1051}
1052
1053/* This is a gdb-local readline command handler. It accepts the
1054 current command line (like RET does) and, if this command was taken
1055 from the history, arranges for the next command in the history to
1056 appear on the command line when the prompt returns.
1057 We ignore the arguments. */
1058static int
1059gdb_rl_operate_and_get_next (int count, int key)
1060{
1061 if (event_loop_p)
1062 {
1063 /* Use the async hook. */
1064 after_char_processing_hook = gdb_rl_operate_and_get_next_completion;
1065 }
1066 else
1067 {
1068 /* This hook only works correctly when we are using the
1069 synchronous readline. */
1070 rl_pre_input_hook = (Function *) gdb_rl_operate_and_get_next_completion;
1071 }
1072
1073 /* Add 1 because we eventually want the next line. */
1074 operate_saved_history = where_history () + 1;
1075 return rl_newline (1, key);
1076}
1077\f
d318976c
FN
1078/* Read one line from the command input stream `instream'
1079 into the local static buffer `linebuffer' (whose current length
1080 is `linelength').
1081 The buffer is made bigger as necessary.
1082 Returns the address of the start of the line.
9e0b60a8 1083
d318976c 1084 NULL is returned for end of file.
9e0b60a8 1085
d318976c
FN
1086 *If* the instream == stdin & stdin is a terminal, the line read
1087 is copied into the file line saver (global var char *line,
1088 length linesize) so that it can be duplicated.
9e0b60a8 1089
d318976c
FN
1090 This routine either uses fancy command line editing or
1091 simple input as the user has requested. */
10689f25 1092
d318976c
FN
1093char *
1094command_line_input (char *prompt_arg, int repeat, char *annotation_suffix)
9e0b60a8 1095{
d318976c
FN
1096 static char *linebuffer = 0;
1097 static unsigned linelength = 0;
9e0b60a8 1098 register char *p;
d318976c
FN
1099 char *p1;
1100 char *rl;
1101 char *local_prompt = prompt_arg;
1102 char *nline;
1103 char got_eof = 0;
1104
1105 /* The annotation suffix must be non-NULL. */
1106 if (annotation_suffix == NULL)
1107 annotation_suffix = "";
9e0b60a8 1108
d318976c
FN
1109 if (annotation_level > 1 && instream == stdin)
1110 {
1111 local_prompt = alloca ((prompt_arg == NULL ? 0 : strlen (prompt_arg))
1112 + strlen (annotation_suffix) + 40);
1113 if (prompt_arg == NULL)
1114 local_prompt[0] = '\0';
1115 else
1116 strcpy (local_prompt, prompt_arg);
1117 strcat (local_prompt, "\n\032\032");
1118 strcat (local_prompt, annotation_suffix);
1119 strcat (local_prompt, "\n");
1120 }
9e0b60a8 1121
d318976c 1122 if (linebuffer == 0)
9e0b60a8 1123 {
d318976c
FN
1124 linelength = 80;
1125 linebuffer = (char *) xmalloc (linelength);
9e0b60a8 1126 }
9e0b60a8 1127
d318976c 1128 p = linebuffer;
9e0b60a8 1129
d318976c
FN
1130 /* Control-C quits instantly if typed while in this loop
1131 since it should not wait until the user types a newline. */
1132 immediate_quit++;
1133#ifdef STOP_SIGNAL
1134 if (job_control)
73bc900d 1135 {
d318976c
FN
1136 if (event_loop_p)
1137 signal (STOP_SIGNAL, handle_stop_sig);
1138 else
1139 signal (STOP_SIGNAL, stop_sig);
1140 }
1141#endif
1142
1143 while (1)
9e0b60a8 1144 {
d318976c
FN
1145 /* Make sure that all output has been output. Some machines may let
1146 you get away with leaving out some of the gdb_flush, but not all. */
1147 wrap_here ("");
1148 gdb_flush (gdb_stdout);
1149 gdb_flush (gdb_stderr);
1150
1151 if (source_file_name != NULL)
1152 {
1153 ++source_line_number;
1154 sprintf (source_error,
1155 "%s%s:%d: Error in sourced command file:\n",
1156 source_pre_error,
1157 source_file_name,
1158 source_line_number);
1159 error_pre_print = source_error;
1160 }
1161
1162 if (annotation_level > 1 && instream == stdin)
1163 {
1164 printf_unfiltered ("\n\032\032pre-");
1165 printf_unfiltered (annotation_suffix);
1166 printf_unfiltered ("\n");
1167 }
1168
1169 /* Don't use fancy stuff if not talking to stdin. */
1170 if (readline_hook && instream == NULL)
1171 {
1172 rl = (*readline_hook) (local_prompt);
1173 }
1174 else if (command_editing_p && instream == stdin && ISATTY (instream))
1175 {
1176 rl = readline (local_prompt);
1177 }
9e0b60a8 1178 else
d318976c
FN
1179 {
1180 rl = gdb_readline (local_prompt);
1181 }
9e0b60a8 1182
d318976c
FN
1183 if (annotation_level > 1 && instream == stdin)
1184 {
1185 printf_unfiltered ("\n\032\032post-");
1186 printf_unfiltered (annotation_suffix);
1187 printf_unfiltered ("\n");
1188 }
9e0b60a8 1189
d318976c 1190 if (!rl || rl == (char *) EOF)
9e0b60a8 1191 {
d318976c
FN
1192 got_eof = 1;
1193 break;
9e0b60a8 1194 }
d318976c
FN
1195 if (strlen (rl) + 1 + (p - linebuffer) > linelength)
1196 {
1197 linelength = strlen (rl) + 1 + (p - linebuffer);
1198 nline = (char *) xrealloc (linebuffer, linelength);
1199 p += nline - linebuffer;
1200 linebuffer = nline;
1201 }
1202 p1 = rl;
1203 /* Copy line. Don't copy null at end. (Leaves line alone
1204 if this was just a newline) */
1205 while (*p1)
1206 *p++ = *p1++;
9e0b60a8 1207
b8c9b27d 1208 xfree (rl); /* Allocated in readline. */
9e0b60a8 1209
d318976c
FN
1210 if (p == linebuffer || *(p - 1) != '\\')
1211 break;
9e0b60a8 1212
d318976c
FN
1213 p--; /* Put on top of '\'. */
1214 local_prompt = (char *) 0;
1215 }
9e0b60a8 1216
d318976c
FN
1217#ifdef STOP_SIGNAL
1218 if (job_control)
1219 signal (STOP_SIGNAL, SIG_DFL);
1220#endif
1221 immediate_quit--;
9e0b60a8 1222
d318976c
FN
1223 if (got_eof)
1224 return NULL;
9e0b60a8 1225
d318976c
FN
1226#define SERVER_COMMAND_LENGTH 7
1227 server_command =
1228 (p - linebuffer > SERVER_COMMAND_LENGTH)
1229 && STREQN (linebuffer, "server ", SERVER_COMMAND_LENGTH);
1230 if (server_command)
9e0b60a8 1231 {
d318976c
FN
1232 /* Note that we don't set `line'. Between this and the check in
1233 dont_repeat, this insures that repeating will still do the
1234 right thing. */
1235 *p = '\0';
1236 return linebuffer + SERVER_COMMAND_LENGTH;
9e0b60a8 1237 }
9e0b60a8 1238
d318976c
FN
1239 /* Do history expansion if that is wished. */
1240 if (history_expansion_p && instream == stdin
1241 && ISATTY (instream))
1242 {
1243 char *history_value;
1244 int expanded;
9e0b60a8 1245
d318976c
FN
1246 *p = '\0'; /* Insert null now. */
1247 expanded = history_expand (linebuffer, &history_value);
1248 if (expanded)
1249 {
1250 /* Print the changes. */
1251 printf_unfiltered ("%s\n", history_value);
9e0b60a8 1252
d318976c
FN
1253 /* If there was an error, call this function again. */
1254 if (expanded < 0)
1255 {
b8c9b27d 1256 xfree (history_value);
d318976c
FN
1257 return command_line_input (prompt_arg, repeat, annotation_suffix);
1258 }
1259 if (strlen (history_value) > linelength)
1260 {
1261 linelength = strlen (history_value) + 1;
1262 linebuffer = (char *) xrealloc (linebuffer, linelength);
1263 }
1264 strcpy (linebuffer, history_value);
1265 p = linebuffer + strlen (linebuffer);
b8c9b27d 1266 xfree (history_value);
d318976c
FN
1267 }
1268 }
9e0b60a8 1269
d318976c
FN
1270 /* If we just got an empty line, and that is supposed
1271 to repeat the previous command, return the value in the
1272 global buffer. */
1273 if (repeat && p == linebuffer)
1274 return line;
1275 for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++);
1276 if (repeat && !*p1)
1277 return line;
9e0b60a8 1278
d318976c 1279 *p = 0;
9e0b60a8 1280
d318976c
FN
1281 /* Add line to history if appropriate. */
1282 if (instream == stdin
1283 && ISATTY (stdin) && *linebuffer)
1284 add_history (linebuffer);
9e0b60a8 1285
d318976c
FN
1286 /* Note: lines consisting solely of comments are added to the command
1287 history. This is useful when you type a command, and then
1288 realize you don't want to execute it quite yet. You can comment
1289 out the command and then later fetch it from the value history
1290 and remove the '#'. The kill ring is probably better, but some
1291 people are in the habit of commenting things out. */
1292 if (*p1 == '#')
1293 *p1 = '\0'; /* Found a comment. */
9e0b60a8 1294
d318976c
FN
1295 /* Save into global buffer if appropriate. */
1296 if (repeat)
1297 {
1298 if (linelength > linesize)
1299 {
1300 line = xrealloc (line, linelength);
1301 linesize = linelength;
1302 }
1303 strcpy (line, linebuffer);
1304 return line;
1305 }
9e0b60a8 1306
d318976c 1307 return linebuffer;
9e0b60a8
JM
1308}
1309\f
1310/* Print the GDB banner. */
1311void
fba45db2 1312print_gdb_version (struct ui_file *stream)
9e0b60a8
JM
1313{
1314 /* From GNU coding standards, first line is meant to be easy for a
1315 program to parse, and is just canonical program name and version
1316 number, which starts after last space. */
1317
1318 fprintf_filtered (stream, "GNU gdb %s\n", version);
1319
1320 /* Second line is a copyright notice. */
1321
f86ddd7c 1322 fprintf_filtered (stream, "Copyright 2002 Free Software Foundation, Inc.\n");
9e0b60a8
JM
1323
1324 /* Following the copyright is a brief statement that the program is
1325 free software, that users are free to copy and change it on
1326 certain conditions, that it is covered by the GNU GPL, and that
1327 there is no warranty. */
1328
1329 fprintf_filtered (stream, "\
1330GDB is free software, covered by the GNU General Public License, and you are\n\
1331welcome to change it and/or distribute copies of it under certain conditions.\n\
1332Type \"show copying\" to see the conditions.\n\
1333There is absolutely no warranty for GDB. Type \"show warranty\" for details.\n");
1334
1335 /* After the required info we print the configuration information. */
1336
1337 fprintf_filtered (stream, "This GDB was configured as \"");
1338 if (!STREQ (host_name, target_name))
1339 {
1340 fprintf_filtered (stream, "--host=%s --target=%s", host_name, target_name);
1341 }
1342 else
1343 {
1344 fprintf_filtered (stream, "%s", host_name);
1345 }
1346 fprintf_filtered (stream, "\".");
1347}
9e0b60a8
JM
1348\f
1349/* get_prompt: access method for the GDB prompt string. */
1350
1351#define MAX_PROMPT_SIZE 256
1352
1353/*
1354 * int get_prompt_1 (char * buf);
1355 *
1356 * Work-horse for get_prompt (called via catch_errors).
1357 * Argument is buffer to hold the formatted prompt.
1358 *
1359 * Returns: 1 for success (use formatted prompt)
1360 * 0 for failure (use gdb_prompt_string).
c5aa993b 1361 */
9e0b60a8
JM
1362
1363static int gdb_prompt_escape;
1364
1365static int
710b33bd 1366get_prompt_1 (void *data)
9e0b60a8 1367{
710b33bd 1368 char *formatted_prompt = data;
9e0b60a8
JM
1369 char *local_prompt;
1370
6426a772 1371 if (event_loop_p)
9e0b60a8
JM
1372 local_prompt = PROMPT (0);
1373 else
1374 local_prompt = gdb_prompt_string;
1375
1376
1377 if (gdb_prompt_escape == 0)
1378 {
c5aa993b 1379 return 0; /* do no formatting */
9e0b60a8 1380 }
c5aa993b
JM
1381 else
1382 /* formatted prompt */
9e0b60a8 1383 {
c5aa993b 1384 char fmt[40], *promptp, *outp, *tmp;
3d6d86c6 1385 struct value *arg_val;
c5aa993b
JM
1386 DOUBLEST doubleval;
1387 LONGEST longval;
9e0b60a8
JM
1388 CORE_ADDR addrval;
1389
1390 int i, len;
1391 struct type *arg_type, *elt_type;
1392
1393 promptp = local_prompt;
c5aa993b 1394 outp = formatted_prompt;
9e0b60a8
JM
1395
1396 while (*promptp != '\0')
1397 {
1398 int available = MAX_PROMPT_SIZE - (outp - formatted_prompt) - 1;
1399
1400 if (*promptp != gdb_prompt_escape)
1401 {
c5aa993b 1402 if (available >= 1) /* overflow protect */
9e0b60a8
JM
1403 *outp++ = *promptp++;
1404 }
1405 else
1406 {
1407 /* GDB prompt string contains escape char. Parse for arg.
c5aa993b
JM
1408 Two consecutive escape chars followed by arg followed by
1409 a comma means to insert the arg using a default format.
1410 Otherwise a printf format string may be included between
1411 the two escape chars. eg:
1412 %%foo, insert foo using default format
1413 %2.2f%foo, insert foo using "%2.2f" format
1414 A mismatch between the format string and the data type
1415 of "foo" is an error (which we don't know how to protect
1416 against). */
9e0b60a8
JM
1417
1418 fmt[0] = '\0'; /* assume null format string */
1419 if (promptp[1] == gdb_prompt_escape) /* double esc char */
1420 {
1421 promptp += 2; /* skip past two escape chars. */
1422 }
1423 else
1424 {
1425 /* extract format string from between two esc chars */
1426 i = 0;
c5aa993b
JM
1427 do
1428 {
1429 fmt[i++] = *promptp++; /* copy format string */
1430 }
1431 while (i < sizeof (fmt) - 1 &&
1432 *promptp != gdb_prompt_escape &&
1433 *promptp != '\0');
9e0b60a8
JM
1434
1435 if (*promptp != gdb_prompt_escape)
1436 error ("Syntax error at prompt position %d",
1437 promptp - local_prompt);
1438 else
1439 {
1440 promptp++; /* skip second escape char */
1441 fmt[i++] = '\0'; /* terminate the format string */
1442 }
1443 }
1444
1445 arg_val = parse_to_comma_and_eval (&promptp);
1446 if (*promptp == ',')
c5aa993b 1447 promptp++; /* skip past the comma */
9e0b60a8 1448 arg_type = check_typedef (VALUE_TYPE (arg_val));
c5aa993b 1449 switch (TYPE_CODE (arg_type))
9e0b60a8
JM
1450 {
1451 case TYPE_CODE_ARRAY:
1452 elt_type = check_typedef (TYPE_TARGET_TYPE (arg_type));
c5aa993b 1453 if (TYPE_LENGTH (arg_type) > 0 &&
9e0b60a8
JM
1454 TYPE_LENGTH (elt_type) == 1 &&
1455 TYPE_CODE (elt_type) == TYPE_CODE_INT)
1456 {
1457 int len = TYPE_LENGTH (arg_type);
1458
1459 if (VALUE_LAZY (arg_val))
1460 value_fetch_lazy (arg_val);
1461 tmp = VALUE_CONTENTS (arg_val);
1462
1463 if (len > available)
c5aa993b 1464 len = available; /* overflow protect */
9e0b60a8
JM
1465
1466 /* FIXME: how to protect GDB from crashing
c5aa993b 1467 from bad user-supplied format string? */
9e0b60a8
JM
1468 if (fmt[0] != 0)
1469 sprintf (outp, fmt, tmp);
1470 else
1471 strncpy (outp, tmp, len);
1472 outp[len] = '\0';
1473 }
1474 break;
1475 case TYPE_CODE_PTR:
1476 elt_type = check_typedef (TYPE_TARGET_TYPE (arg_type));
1aa20aa8 1477 addrval = value_as_address (arg_val);
9e0b60a8
JM
1478
1479 if (TYPE_LENGTH (elt_type) == 1 &&
c5aa993b 1480 TYPE_CODE (elt_type) == TYPE_CODE_INT &&
9e0b60a8
JM
1481 addrval != 0)
1482 {
1483 /* display it as a string */
1484 char *default_fmt = "%s";
1485 char *tmp;
1486 int err = 0;
1487
1488 /* Limiting the number of bytes that the following call
c5aa993b
JM
1489 will read protects us from sprintf overflow later. */
1490 i = target_read_string (addrval, /* src */
1491 &tmp, /* dest */
1492 available, /* len */
9e0b60a8
JM
1493 &err);
1494 if (err) /* read failed */
1495 error ("%s on target_read", safe_strerror (err));
1496
1497 tmp[i] = '\0'; /* force-terminate string */
1498 /* FIXME: how to protect GDB from crashing
c5aa993b
JM
1499 from bad user-supplied format string? */
1500 sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
9e0b60a8 1501 tmp);
b8c9b27d 1502 xfree (tmp);
9e0b60a8
JM
1503 }
1504 else
1505 {
1506 /* display it as a pointer */
1507 char *default_fmt = "0x%x";
1508
1509 /* FIXME: how to protect GDB from crashing
c5aa993b
JM
1510 from bad user-supplied format string? */
1511 if (available >= 16 /*? */ ) /* overflow protect */
9e0b60a8
JM
1512 sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
1513 (long) addrval);
1514 }
1515 break;
1516 case TYPE_CODE_FLT:
1517 {
1518 char *default_fmt = "%g";
1519
1520 doubleval = value_as_double (arg_val);
1521 /* FIXME: how to protect GDB from crashing
1522 from bad user-supplied format string? */
c5aa993b 1523 if (available >= 16 /*? */ ) /* overflow protect */
9e0b60a8
JM
1524 sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
1525 (double) doubleval);
1526 break;
1527 }
1528 case TYPE_CODE_INT:
1529 {
1530 char *default_fmt = "%d";
1531
1532 longval = value_as_long (arg_val);
1533 /* FIXME: how to protect GDB from crashing
1534 from bad user-supplied format string? */
c5aa993b 1535 if (available >= 16 /*? */ ) /* overflow protect */
9e0b60a8
JM
1536 sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
1537 (long) longval);
1538 break;
1539 }
1540 case TYPE_CODE_BOOL:
1541 {
1542 /* no default format for bool */
1543 longval = value_as_long (arg_val);
c5aa993b 1544 if (available >= 8 /*? */ ) /* overflow protect */
9e0b60a8
JM
1545 {
1546 if (longval)
1547 strcpy (outp, "<true>");
1548 else
1549 strcpy (outp, "<false>");
1550 }
1551 break;
1552 }
1553 case TYPE_CODE_ENUM:
1554 {
1555 /* no default format for enum */
1556 longval = value_as_long (arg_val);
1557 len = TYPE_NFIELDS (arg_type);
1558 /* find enum name if possible */
1559 for (i = 0; i < len; i++)
1560 if (TYPE_FIELD_BITPOS (arg_type, i) == longval)
c5aa993b 1561 break; /* match -- end loop */
9e0b60a8
JM
1562
1563 if (i < len) /* enum name found */
1564 {
1565 char *name = TYPE_FIELD_NAME (arg_type, i);
1566
1567 strncpy (outp, name, available);
1568 /* in casel available < strlen (name), */
1569 outp[available] = '\0';
1570 }
1571 else
1572 {
c5aa993b 1573 if (available >= 16 /*? */ ) /* overflow protect */
d4f3574e 1574 sprintf (outp, "%ld", (long) longval);
9e0b60a8
JM
1575 }
1576 break;
1577 }
1578 case TYPE_CODE_VOID:
1579 *outp = '\0';
1580 break; /* void type -- no output */
1581 default:
1582 error ("bad data type at prompt position %d",
1583 promptp - local_prompt);
1584 break;
1585 }
1586 outp += strlen (outp);
1587 }
1588 }
1589 *outp++ = '\0'; /* terminate prompt string */
1590 return 1;
1591 }
1592}
1593
1594char *
fba45db2 1595get_prompt (void)
9e0b60a8
JM
1596{
1597 static char buf[MAX_PROMPT_SIZE];
1598
c5aa993b 1599 if (catch_errors (get_prompt_1, buf, "bad formatted prompt: ",
9e0b60a8
JM
1600 RETURN_MASK_ALL))
1601 {
c5aa993b 1602 return &buf[0]; /* successful formatted prompt */
9e0b60a8
JM
1603 }
1604 else
1605 {
1606 /* Prompt could not be formatted. */
6426a772 1607 if (event_loop_p)
9e0b60a8
JM
1608 return PROMPT (0);
1609 else
1610 return gdb_prompt_string;
1611 }
1612}
1613
1614void
fba45db2 1615set_prompt (char *s)
9e0b60a8
JM
1616{
1617/* ??rehrauer: I don't know why this fails, since it looks as though
1618 assignments to prompt are wrapped in calls to savestring...
c5aa993b 1619 if (prompt != NULL)
b8c9b27d 1620 xfree (prompt);
c5aa993b 1621 */
6426a772 1622 if (event_loop_p)
9e0b60a8
JM
1623 PROMPT (0) = savestring (s, strlen (s));
1624 else
1625 gdb_prompt_string = savestring (s, strlen (s));
1626}
9e0b60a8 1627\f
c5aa993b 1628
9e0b60a8
JM
1629/* If necessary, make the user confirm that we should quit. Return
1630 non-zero if we should quit, zero if we shouldn't. */
1631
1632int
fba45db2 1633quit_confirm (void)
9e0b60a8 1634{
39f77062 1635 if (! ptid_equal (inferior_ptid, null_ptid) && target_has_execution)
9e0b60a8
JM
1636 {
1637 char *s;
1638
1639 /* This is something of a hack. But there's no reliable way to
c5aa993b
JM
1640 see if a GUI is running. The `use_windows' variable doesn't
1641 cut it. */
9e0b60a8
JM
1642 if (init_ui_hook)
1643 s = "A debugging session is active.\nDo you still want to close the debugger?";
1644 else if (attach_flag)
1645 s = "The program is running. Quit anyway (and detach it)? ";
1646 else
1647 s = "The program is running. Exit anyway? ";
1648
c5aa993b 1649 if (!query (s))
9e0b60a8
JM
1650 return 0;
1651 }
1652
1653 return 1;
1654}
1655
1656/* Quit without asking for confirmation. */
1657
1658void
fba45db2 1659quit_force (char *args, int from_tty)
9e0b60a8
JM
1660{
1661 int exit_code = 0;
1662
1663 /* An optional expression may be used to cause gdb to terminate with the
1664 value of that expression. */
1665 if (args)
1666 {
3d6d86c6 1667 struct value *val = parse_and_eval (args);
9e0b60a8
JM
1668
1669 exit_code = (int) value_as_long (val);
1670 }
1671
39f77062 1672 if (! ptid_equal (inferior_ptid, null_ptid) && target_has_execution)
9e0b60a8
JM
1673 {
1674 if (attach_flag)
1675 target_detach (args, from_tty);
1676 else
1677 target_kill ();
1678 }
1679
1680 /* UDI wants this, to kill the TIP. */
1681 target_close (1);
1682
1683 /* Save the history information if it is appropriate to do so. */
1684 if (write_history_p && history_filename)
1685 write_history (history_filename);
1686
c5aa993b 1687 do_final_cleanups (ALL_CLEANUPS); /* Do any final cleanups before exiting */
9e0b60a8 1688
9e0b60a8
JM
1689 exit (exit_code);
1690}
1691
9e0b60a8
JM
1692/* Returns whether GDB is running on a terminal and whether the user
1693 desires that questions be asked of them on that terminal. */
1694
1695int
fba45db2 1696input_from_terminal_p (void)
9e0b60a8
JM
1697{
1698 return gdb_has_a_terminal () && (instream == stdin) & caution;
1699}
1700\f
9e0b60a8
JM
1701/* ARGSUSED */
1702static void
fba45db2 1703dont_repeat_command (char *ignored, int from_tty)
9e0b60a8 1704{
c5aa993b
JM
1705 *line = 0; /* Can't call dont_repeat here because we're not
1706 necessarily reading from stdin. */
9e0b60a8
JM
1707}
1708\f
1709/* Functions to manipulate command line editing control variables. */
1710
1711/* Number of commands to print in each call to show_commands. */
1712#define Hist_print 10
d318976c 1713void
fba45db2 1714show_commands (char *args, int from_tty)
9e0b60a8
JM
1715{
1716 /* Index for history commands. Relative to history_base. */
1717 int offset;
1718
1719 /* Number of the history entry which we are planning to display next.
1720 Relative to history_base. */
1721 static int num = 0;
1722
1723 /* The first command in the history which doesn't exist (i.e. one more
1724 than the number of the last command). Relative to history_base. */
1725 int hist_len;
1726
9e0b60a8
JM
1727 /* Print out some of the commands from the command history. */
1728 /* First determine the length of the history list. */
1729 hist_len = history_size;
1730 for (offset = 0; offset < history_size; offset++)
1731 {
1732 if (!history_get (history_base + offset))
1733 {
1734 hist_len = offset;
1735 break;
1736 }
1737 }
1738
1739 if (args)
1740 {
1741 if (args[0] == '+' && args[1] == '\0')
1742 /* "info editing +" should print from the stored position. */
1743 ;
1744 else
1745 /* "info editing <exp>" should print around command number <exp>. */
0e828ed1 1746 num = (parse_and_eval_long (args) - history_base) - Hist_print / 2;
9e0b60a8
JM
1747 }
1748 /* "show commands" means print the last Hist_print commands. */
1749 else
1750 {
1751 num = hist_len - Hist_print;
1752 }
1753
1754 if (num < 0)
1755 num = 0;
1756
1757 /* If there are at least Hist_print commands, we want to display the last
1758 Hist_print rather than, say, the last 6. */
1759 if (hist_len - num < Hist_print)
1760 {
1761 num = hist_len - Hist_print;
1762 if (num < 0)
1763 num = 0;
1764 }
1765
1766 for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
1767 {
1768 printf_filtered ("%5d %s\n", history_base + offset,
c5aa993b 1769 (history_get (history_base + offset))->line);
9e0b60a8
JM
1770 }
1771
1772 /* The next command we want to display is the next one that we haven't
1773 displayed yet. */
1774 num += Hist_print;
1775
1776 /* If the user repeats this command with return, it should do what
1777 "show commands +" does. This is unnecessary if arg is null,
1778 because "show commands +" is not useful after "show commands". */
1779 if (from_tty && args)
1780 {
1781 args[0] = '+';
1782 args[1] = '\0';
1783 }
1784}
1785
1786/* Called by do_setshow_command. */
1787/* ARGSUSED */
1788static void
fba45db2 1789set_history_size_command (char *args, int from_tty, struct cmd_list_element *c)
9e0b60a8
JM
1790{
1791 if (history_size == INT_MAX)
1792 unstifle_history ();
1793 else if (history_size >= 0)
1794 stifle_history (history_size);
1795 else
1796 {
1797 history_size = INT_MAX;
1798 error ("History size must be non-negative");
1799 }
1800}
1801
1802/* ARGSUSED */
d318976c 1803void
fba45db2 1804set_history (char *args, int from_tty)
9e0b60a8
JM
1805{
1806 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
1807 help_list (sethistlist, "set history ", -1, gdb_stdout);
1808}
1809
1810/* ARGSUSED */
d318976c 1811void
fba45db2 1812show_history (char *args, int from_tty)
9e0b60a8
JM
1813{
1814 cmd_show_list (showhistlist, from_tty, "");
1815}
1816
1817int info_verbose = 0; /* Default verbose msgs off */
1818
1819/* Called by do_setshow_command. An elaborate joke. */
1820/* ARGSUSED */
d318976c 1821void
fba45db2 1822set_verbose (char *args, int from_tty, struct cmd_list_element *c)
9e0b60a8
JM
1823{
1824 char *cmdname = "verbose";
1825 struct cmd_list_element *showcmd;
1826
1827 showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
1828
1829 if (info_verbose)
1830 {
1831 c->doc = "Set verbose printing of informational messages.";
1832 showcmd->doc = "Show verbose printing of informational messages.";
1833 }
1834 else
1835 {
1836 c->doc = "Set verbosity.";
1837 showcmd->doc = "Show verbosity.";
1838 }
1839}
1840
9e0b60a8
JM
1841/* Init the history buffer. Note that we are called after the init file(s)
1842 * have been read so that the user can change the history file via his
1843 * .gdbinit file (for instance). The GDBHISTFILE environment variable
1844 * overrides all of this.
1845 */
1846
1847void
fba45db2 1848init_history (void)
9e0b60a8
JM
1849{
1850 char *tmpenv;
1851
1852 tmpenv = getenv ("HISTSIZE");
1853 if (tmpenv)
1854 history_size = atoi (tmpenv);
1855 else if (!history_size)
1856 history_size = 256;
1857
1858 stifle_history (history_size);
1859
1860 tmpenv = getenv ("GDBHISTFILE");
1861 if (tmpenv)
c5aa993b
JM
1862 history_filename = savestring (tmpenv, strlen (tmpenv));
1863 else if (!history_filename)
1864 {
1865 /* We include the current directory so that if the user changes
1866 directories the file written will be the same as the one
1867 that was read. */
a0b3c4fd 1868#ifdef __MSDOS__
eb2f494a
AC
1869 /* No leading dots in file names are allowed on MSDOS. */
1870 history_filename = concat (current_directory, "/_gdb_history", NULL);
a0b3c4fd 1871#else
c5aa993b 1872 history_filename = concat (current_directory, "/.gdb_history", NULL);
a0b3c4fd 1873#endif
c5aa993b 1874 }
9e0b60a8
JM
1875 read_history (history_filename);
1876}
1877
1878static void
fba45db2 1879init_main (void)
9e0b60a8
JM
1880{
1881 struct cmd_list_element *c;
1882
1883 /* If we are running the asynchronous version,
1884 we initialize the prompts differently. */
6426a772 1885 if (!event_loop_p)
9e0b60a8 1886 {
c5aa993b 1887 gdb_prompt_string = savestring (DEFAULT_PROMPT, strlen (DEFAULT_PROMPT));
9e0b60a8
JM
1888 }
1889 else
1890 {
1891 /* initialize the prompt stack to a simple "(gdb) " prompt or to
96baa820 1892 whatever the DEFAULT_PROMPT is. */
9e0b60a8 1893 the_prompts.top = 0;
c5aa993b 1894 PREFIX (0) = "";
c5aa993b 1895 PROMPT (0) = savestring (DEFAULT_PROMPT, strlen (DEFAULT_PROMPT));
c5aa993b 1896 SUFFIX (0) = "";
9e0b60a8 1897 /* Set things up for annotation_level > 1, if the user ever decides
c5aa993b 1898 to use it. */
9e0b60a8
JM
1899 async_annotation_suffix = "prompt";
1900 /* Set the variable associated with the setshow prompt command. */
1901 new_async_prompt = savestring (PROMPT (0), strlen (PROMPT (0)));
0191bed7
EZ
1902
1903 /* If gdb was started with --annotate=2, this is equivalent to
1904 the user entering the command 'set annotate 2' at the gdb
1905 prompt, so we need to do extra processing. */
1906 if (annotation_level > 1)
1907 set_async_annotation_level (NULL, 0, NULL);
9e0b60a8
JM
1908 }
1909 gdb_prompt_escape = 0; /* default to none. */
1910
1911 /* Set the important stuff up for command editing. */
1912 command_editing_p = 1;
9e0b60a8
JM
1913 history_expansion_p = 0;
1914 write_history_p = 0;
1915
1916 /* Setup important stuff for command line editing. */
1917 rl_completion_entry_function = (int (*)()) readline_line_completion_function;
d318976c
FN
1918 rl_completer_word_break_characters =
1919 get_gdb_completer_word_break_characters ();
1920 rl_completer_quote_characters = get_gdb_completer_quote_characters ();
9e0b60a8
JM
1921 rl_readline_name = "gdb";
1922
467d8519
TT
1923 /* The name for this defun comes from Bash, where it originated.
1924 15 is Control-o, the same binding this function has in Bash. */
1925 rl_add_defun ("operate-and-get-next", gdb_rl_operate_and_get_next, 15);
1926
9e0b60a8
JM
1927 /* The set prompt command is different depending whether or not the
1928 async version is run. NOTE: this difference is going to
1929 disappear as we make the event loop be the default engine of
1930 gdb. */
6426a772 1931 if (!event_loop_p)
9e0b60a8
JM
1932 {
1933 add_show_from_set
c5aa993b 1934 (add_set_cmd ("prompt", class_support, var_string,
9e0b60a8
JM
1935 (char *) &gdb_prompt_string, "Set gdb's prompt",
1936 &setlist),
1937 &showlist);
1938 }
1939 else
1940 {
c5aa993b
JM
1941 c = add_set_cmd ("prompt", class_support, var_string,
1942 (char *) &new_async_prompt, "Set gdb's prompt",
9e0b60a8
JM
1943 &setlist);
1944 add_show_from_set (c, &showlist);
9f60d481 1945 set_cmd_sfunc (c, set_async_prompt);
9e0b60a8
JM
1946 }
1947
1948 add_show_from_set
c5aa993b 1949 (add_set_cmd ("prompt-escape-char", class_support, var_zinteger,
9e0b60a8
JM
1950 (char *) &gdb_prompt_escape,
1951 "Set escape character for formatting of gdb's prompt",
1952 &setlist),
1953 &showlist);
1954
9e0b60a8
JM
1955 add_com ("dont-repeat", class_support, dont_repeat_command, "Don't repeat this command.\n\
1956Primarily used inside of user-defined commands that should not be repeated when\n\
1957hitting return.");
1958
9e0b60a8
JM
1959 /* The set editing command is different depending whether or not the
1960 async version is run. NOTE: this difference is going to disappear
1961 as we make the event loop be the default engine of gdb. */
6426a772 1962 if (!event_loop_p)
9e0b60a8
JM
1963 {
1964 add_show_from_set
c5aa993b 1965 (add_set_cmd ("editing", class_support, var_boolean, (char *) &command_editing_p,
9e0b60a8
JM
1966 "Set editing of command lines as they are typed.\n\
1967Use \"on\" to enable the editing, and \"off\" to disable it.\n\
1968Without an argument, command line editing is enabled. To edit, use\n\
1969EMACS-like or VI-like commands like control-P or ESC.", &setlist),
1970 &showlist);
1971 }
1972 else
1973 {
c5aa993b 1974 c = add_set_cmd ("editing", class_support, var_boolean, (char *) &async_command_editing_p,
9e0b60a8
JM
1975 "Set editing of command lines as they are typed.\n\
1976Use \"on\" to enable the editing, and \"off\" to disable it.\n\
1977Without an argument, command line editing is enabled. To edit, use\n\
1978EMACS-like or VI-like commands like control-P or ESC.", &setlist);
1979
1980 add_show_from_set (c, &showlist);
9f60d481 1981 set_cmd_sfunc (c, set_async_editing_command);
9e0b60a8
JM
1982 }
1983
9e0b60a8 1984 add_show_from_set
c5aa993b
JM
1985 (add_set_cmd ("save", no_class, var_boolean, (char *) &write_history_p,
1986 "Set saving of the history record on exit.\n\
9e0b60a8
JM
1987Use \"on\" to enable the saving, and \"off\" to disable it.\n\
1988Without an argument, saving is enabled.", &sethistlist),
1989 &showhistlist);
1990
c5aa993b 1991 c = add_set_cmd ("size", no_class, var_integer, (char *) &history_size,
9e0b60a8
JM
1992 "Set the size of the command history, \n\
1993ie. the number of previous commands to keep a record of.", &sethistlist);
1994 add_show_from_set (c, &showhistlist);
9f60d481 1995 set_cmd_sfunc (c, set_history_size_command);
9e0b60a8 1996
7a1bd56a
EZ
1997 c = add_set_cmd ("filename", no_class, var_filename,
1998 (char *) &history_filename,
1999 "Set the filename in which to record the command history\n\
2000 (the list of previous commands of which a record is kept).", &sethistlist);
5ba2abeb 2001 set_cmd_completer (c, filename_completer);
7a1bd56a 2002 add_show_from_set (c, &showhistlist);
9e0b60a8
JM
2003
2004 add_show_from_set
2005 (add_set_cmd ("confirm", class_support, var_boolean,
c5aa993b 2006 (char *) &caution,
9e0b60a8
JM
2007 "Set whether to confirm potentially dangerous operations.",
2008 &setlist),
2009 &showlist);
2010
9e0b60a8
JM
2011 /* The set annotate command is different depending whether or not
2012 the async version is run. NOTE: this difference is going to
2013 disappear as we make the event loop be the default engine of
2014 gdb. */
6426a772 2015 if (!event_loop_p)
9e0b60a8 2016 {
c5aa993b
JM
2017 c = add_set_cmd ("annotate", class_obscure, var_zinteger,
2018 (char *) &annotation_level, "Set annotation_level.\n\
9e0b60a8
JM
20190 == normal; 1 == fullname (for use when running under emacs)\n\
20202 == output annotated suitably for use by programs that control GDB.",
2021 &setlist);
2022 c = add_show_from_set (c, &showlist);
2023 }
2024 else
2025 {
c5aa993b
JM
2026 c = add_set_cmd ("annotate", class_obscure, var_zinteger,
2027 (char *) &annotation_level, "Set annotation_level.\n\
9e0b60a8
JM
20280 == normal; 1 == fullname (for use when running under emacs)\n\
20292 == output annotated suitably for use by programs that control GDB.",
c5aa993b 2030 &setlist);
9e0b60a8 2031 add_show_from_set (c, &showlist);
9f60d481 2032 set_cmd_sfunc (c, set_async_annotation_level);
9e0b60a8 2033 }
6426a772 2034 if (event_loop_p)
104c1213
JM
2035 {
2036 add_show_from_set
2037 (add_set_cmd ("exec-done-display", class_support, var_boolean, (char *) &exec_done_display_p,
2038 "Set notification of completion for asynchronous execution commands.\n\
2039Use \"on\" to enable the notification, and \"off\" to disable it.", &setlist),
2040 &showlist);
2041 }
9e0b60a8 2042}
64cdedad
EZ
2043
2044void
2045gdb_init (char *argv0)
2046{
2047 if (pre_init_ui_hook)
2048 pre_init_ui_hook ();
2049
2050 /* Run the init function of each source file */
2051
2052 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
2053 current_directory = gdb_dirbuf;
2054
2055#ifdef __MSDOS__
2056 /* Make sure we return to the original directory upon exit, come
2057 what may, since the OS doesn't do that for us. */
2058 make_final_cleanup (do_chdir_cleanup, xstrdup (current_directory));
2059#endif
2060
2061 init_cmd_lists (); /* This needs to be done first */
2062 initialize_targets (); /* Setup target_terminal macros for utils.c */
2063 initialize_utils (); /* Make errors and warnings possible */
2064 initialize_all_files ();
2065 initialize_current_architecture ();
2066 init_cli_cmds();
2067 init_main (); /* But that omits this file! Do it now */
2068
2069 /* The signal handling mechanism is different depending whether or
2070 not the async version is run. NOTE: in the future we plan to make
2071 the event loop be the default engine of gdb, and this difference
2072 will disappear. */
2073 if (event_loop_p)
2074 async_init_signals ();
2075 else
2076 init_signals ();
2077
2078 /* We need a default language for parsing expressions, so simple things like
2079 "set width 0" won't fail if no language is explicitly set in a config file
2080 or implicitly set by reading an executable during startup. */
2081 set_language (language_c);
2082 expected_language = current_language; /* don't warn about the change. */
2083
1ad24239
KS
2084 /* Allow another UI to initialize. If the UI fails to initialize, and
2085 it wants GDB to revert to the CLI, it should clear init_ui_hook. */
2086 if (init_ui_hook)
2087 init_ui_hook (argv0);
2088
64cdedad
EZ
2089 /* Install the default UI */
2090 if (!init_ui_hook)
2091 {
2092 uiout = cli_out_new (gdb_stdout);
2093
2094 /* All the interpreters should have had a look at things by now.
2095 Initialize the selected interpreter. */
2096 if (interpreter_p)
2097 {
2098 fprintf_unfiltered (gdb_stderr, "Interpreter `%s' unrecognized.\n",
2099 interpreter_p);
2100 exit (1);
2101 }
2102 }
64cdedad 2103}
This page took 0.298844 seconds and 4 git commands to generate.