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