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