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