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