import gdb-2000-01-31 snapshot
[deliverable/binutils-gdb.git] / gdb / top.c
CommitLineData
c906108c 1/* Top level stuff for GDB, the GNU debugger.
085dd6e6 2 Copyright 1986, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 1999
c906108c
SS
3 Free Software Foundation, Inc.
4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b
JM
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
c906108c
SS
21
22#include "defs.h"
23#include "gdbcmd.h"
24#include "call-cmds.h"
25#include "symtab.h"
26#include "inferior.h"
27#include "signals.h"
28#include "target.h"
29#include "breakpoint.h"
30#include "gdbtypes.h"
31#include "expression.h"
32#include "value.h"
33#include "language.h"
c5aa993b 34#include "terminal.h" /* For job_control. */
c906108c 35#include "annotate.h"
c906108c 36#include "top.h"
d4f3574e 37#include "version.h"
c906108c
SS
38
39/* readline include files */
40#include <readline/readline.h>
41#include <readline/history.h>
42
43/* readline defines this. */
44#undef savestring
45
46#include <sys/types.h>
c906108c 47
c4093a6a 48#include <setjmp.h>
2acceee2 49
c2c6d25f 50#include "event-top.h"
c906108c
SS
51#include "gdb_string.h"
52#include "gdb_stat.h"
53#include <ctype.h>
54
c906108c
SS
55/* Prototypes for local functions */
56
57static void dont_repeat_command PARAMS ((char *, int));
58
59static void source_cleanup_lines PARAMS ((PTR));
60
61static void user_defined_command PARAMS ((char *, int));
62
63static void init_signals PARAMS ((void));
64
65#ifdef STOP_SIGNAL
66static void stop_sig PARAMS ((int));
67#endif
68
c5aa993b 69static char *line_completion_function PARAMS ((char *, int, char *, int));
c906108c 70
c5aa993b 71static char *readline_line_completion_function PARAMS ((char *, int));
c906108c 72
392a587b 73/* NOTE 1999-04-29: this function will be static again, after we make the
cd0fc7c3
SS
74 event loop be the default command loop for gdb, and we merge
75 event-top.c into this file, top.c */
76/* static */ void command_loop_marker PARAMS ((int));
77
c906108c
SS
78static void while_command PARAMS ((char *, int));
79
80static void if_command PARAMS ((char *, int));
81
82static struct command_line *
c5aa993b 83 build_command_line PARAMS ((enum command_control_type, char *));
c906108c
SS
84
85static struct command_line *
c5aa993b 86 get_command_line PARAMS ((enum command_control_type, char *));
c906108c
SS
87
88static void realloc_body_list PARAMS ((struct command_line *, int));
89
90static enum misc_command_type read_next_line PARAMS ((struct command_line **));
91
92static enum command_control_type
93recurse_read_control_structure PARAMS ((struct command_line *));
94
c5aa993b 95static struct cleanup *setup_user_args PARAMS ((char *));
c906108c 96
c5aa993b 97static char *locate_arg PARAMS ((char *));
c906108c 98
c5aa993b 99static char *insert_args PARAMS ((char *));
c906108c
SS
100
101static void arg_cleanup PARAMS ((void));
102
103static void init_main PARAMS ((void));
104
105static void init_cmd_lists PARAMS ((void));
106
107static void float_handler PARAMS ((int));
108
109static void init_signals PARAMS ((void));
110
111static void set_verbose PARAMS ((char *, int, struct cmd_list_element *));
112
113static void show_history PARAMS ((char *, int));
114
115static void set_history PARAMS ((char *, int));
116
117static void set_history_size_command PARAMS ((char *, int,
118 struct cmd_list_element *));
119
120static void show_commands PARAMS ((char *, int));
121
122static void echo_command PARAMS ((char *, int));
123
124static void pwd_command PARAMS ((char *, int));
125
126static void show_version PARAMS ((char *, int));
127
128static void document_command PARAMS ((char *, int));
129
130static void define_command PARAMS ((char *, int));
131
132static void validate_comname PARAMS ((char *));
133
134static void help_command PARAMS ((char *, int));
135
136static void show_command PARAMS ((char *, int));
137
138static void info_command PARAMS ((char *, int));
139
140static void complete_command PARAMS ((char *, int));
141
142static void do_nothing PARAMS ((int));
143
144#ifdef SIGHUP
392a587b 145/* NOTE 1999-04-29: This function will be static again, once we modify
cd0fc7c3
SS
146 gdb to use the event loop as the default command loop and we merge
147 event-top.c into this file, top.c */
148/* static */ int quit_cover PARAMS ((PTR));
c906108c
SS
149
150static void disconnect PARAMS ((int));
151#endif
152
153static void source_cleanup PARAMS ((FILE *));
154
104c1213
JM
155/* Default command line prompt. This is overriden in some configs. */
156
157#ifndef DEFAULT_PROMPT
158#define DEFAULT_PROMPT "(gdb) "
c906108c
SS
159#endif
160
161/* Initialization file name for gdb. This is overridden in some configs. */
162
163#ifndef GDBINIT_FILENAME
164#define GDBINIT_FILENAME ".gdbinit"
165#endif
166char gdbinit[] = GDBINIT_FILENAME;
167
168int inhibit_gdbinit = 0;
169
170/* If nonzero, and GDB has been configured to be able to use windows,
171 attempt to open them upon startup. */
172
173int use_windows = 1;
174
c906108c
SS
175extern char lang_frame_mismatch_warn[]; /* language.c */
176
177/* Flag for whether we want all the "from_tty" gubbish printed. */
178
c5aa993b 179int caution = 1; /* Default is yes, sigh. */
c906108c
SS
180
181/* Define all cmd_list_elements. */
182
183/* Chain containing all defined commands. */
184
185struct cmd_list_element *cmdlist;
186
187/* Chain containing all defined info subcommands. */
188
189struct cmd_list_element *infolist;
190
191/* Chain containing all defined enable subcommands. */
192
193struct cmd_list_element *enablelist;
194
195/* Chain containing all defined disable subcommands. */
196
197struct cmd_list_element *disablelist;
198
199/* Chain containing all defined toggle subcommands. */
200
201struct cmd_list_element *togglelist;
202
203/* Chain containing all defined stop subcommands. */
204
205struct cmd_list_element *stoplist;
206
207/* Chain containing all defined delete subcommands. */
208
209struct cmd_list_element *deletelist;
210
211/* Chain containing all defined "enable breakpoint" subcommands. */
212
213struct cmd_list_element *enablebreaklist;
214
215/* Chain containing all defined set subcommands */
216
217struct cmd_list_element *setlist;
218
219/* Chain containing all defined unset subcommands */
220
221struct cmd_list_element *unsetlist;
222
223/* Chain containing all defined show subcommands. */
224
225struct cmd_list_element *showlist;
226
227/* Chain containing all defined \"set history\". */
228
229struct cmd_list_element *sethistlist;
230
231/* Chain containing all defined \"show history\". */
232
233struct cmd_list_element *showhistlist;
234
235/* Chain containing all defined \"unset history\". */
236
237struct cmd_list_element *unsethistlist;
238
239/* Chain containing all defined maintenance subcommands. */
240
c906108c 241struct cmd_list_element *maintenancelist;
c906108c
SS
242
243/* Chain containing all defined "maintenance info" subcommands. */
244
c906108c 245struct cmd_list_element *maintenanceinfolist;
c906108c
SS
246
247/* Chain containing all defined "maintenance print" subcommands. */
248
c906108c 249struct cmd_list_element *maintenanceprintlist;
c906108c
SS
250
251struct cmd_list_element *setprintlist;
252
253struct cmd_list_element *showprintlist;
254
255struct cmd_list_element *setchecklist;
256
257struct cmd_list_element *showchecklist;
258
259/* stdio stream that command input is being read from. Set to stdin normally.
260 Set by source_command to the file we are sourcing. Set to NULL if we are
261 executing a user-defined command or interacting via a GUI. */
262
263FILE *instream;
264
265/* Current working directory. */
266
267char *current_directory;
268
269/* The directory name is actually stored here (usually). */
270char gdb_dirbuf[1024];
271
272/* Function to call before reading a command, if nonzero.
273 The function receives two args: an input stream,
274 and a prompt string. */
275
276void (*window_hook) PARAMS ((FILE *, char *));
277
278int epoch_interface;
279int xgdb_verbose;
280
281/* gdb prints this when reading a command interactively */
c5aa993b
JM
282static char *gdb_prompt_string; /* the global prompt string */
283extern char *get_prompt PARAMS ((void)); /* access function for prompt string */
c906108c
SS
284
285/* Buffer used for reading command lines, and the size
286 allocated for it so far. */
287
288char *line;
289int linesize = 100;
290
291/* Nonzero if the current command is modified by "server ". This
c2d11a7d 292 affects things like recording into the command history, commands
c906108c
SS
293 repeating on RETURN, etc. This is so a user interface (emacs, GUI,
294 whatever) can issue its own commands and also send along commands
295 from the user, and have the user not notice that the user interface
296 is issuing commands too. */
297int server_command;
298
299/* Baud rate specified for talking to serial target systems. Default
300 is left as -1, so targets can choose their own defaults. */
301/* FIXME: This means that "show remotebaud" and gr_files_info can print -1
302 or (unsigned int)-1. This is a Bad User Interface. */
303
304int baud_rate = -1;
305
306/* Timeout limit for response from target. */
307
308int remote_timeout = 20; /* Set default to 20 */
309
310/* Non-zero tells remote* modules to output debugging info. */
311
312int remote_debug = 0;
313
43ff13b4
JM
314/* Non-zero means the target is running. Note: this is different from
315 saying that there is an active target and we are stopped at a
316 breakpoint, for instance. This is a real indicator whether the
317 target is off and running, which gdb is doing something else. */
318int target_executing = 0;
319
c906108c
SS
320/* Level of control structure. */
321static int control_level;
322
323/* Structure for arguments to user defined functions. */
324#define MAXUSERARGS 10
325struct user_args
c5aa993b
JM
326 {
327 struct user_args *next;
328 struct
329 {
330 char *arg;
331 int len;
332 }
333 a[MAXUSERARGS];
334 int count;
335 }
336 *user_args;
c906108c
SS
337
338/* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
339
340#ifndef STOP_SIGNAL
341#ifdef SIGTSTP
342#define STOP_SIGNAL SIGTSTP
343static void stop_sig PARAMS ((int));
344#endif
345#endif
346
347/* Some System V have job control but not sigsetmask(). */
348#if !defined (HAVE_SIGSETMASK)
349#if !defined (USG)
350#define HAVE_SIGSETMASK 1
351#else
352#define HAVE_SIGSETMASK 0
353#endif
354#endif
355
356#if 0 == (HAVE_SIGSETMASK)
357#define sigsetmask(n)
358#endif
359
360/* Hooks for alternate command interfaces. */
361
362/* Called after most modules have been initialized, but before taking users
363 command file. */
364
365void (*init_ui_hook) PARAMS ((char *argv0));
7a292a7a
SS
366
367/* This hook is called from within gdb's many mini-event loops which could
368 steal control from a real user interface's event loop. It returns
369 non-zero if the user is requesting a detach, zero otherwise. */
370
371int (*ui_loop_hook) PARAMS ((int));
c906108c
SS
372
373/* Called instead of command_loop at top level. Can be invoked via
374 return_to_top_level. */
375
376void (*command_loop_hook) PARAMS ((void));
377
378
c906108c
SS
379/* Called from print_frame_info to list the line we stopped in. */
380
c5aa993b 381void (*print_frame_info_listing_hook) PARAMS ((struct symtab * s, int line,
c906108c
SS
382 int stopline, int noerror));
383/* Replaces most of query. */
384
385int (*query_hook) PARAMS ((const char *, va_list));
386
387/* Replaces most of warning. */
388
389void (*warning_hook) PARAMS ((const char *, va_list));
390
c906108c
SS
391/* These three functions support getting lines of text from the user. They
392 are used in sequence. First readline_begin_hook is called with a text
393 string that might be (for example) a message for the user to type in a
394 sequence of commands to be executed at a breakpoint. If this function
395 calls back to a GUI, it might take this opportunity to pop up a text
396 interaction window with this message. Next, readline_hook is called
397 with a prompt that is emitted prior to collecting the user input.
398 It can be called multiple times. Finally, readline_end_hook is called
399 to notify the GUI that we are done with the interaction window and it
400 can close it. */
401
c5aa993b
JM
402void (*readline_begin_hook) PARAMS ((char *,...));
403char *(*readline_hook) PARAMS ((char *));
c906108c
SS
404void (*readline_end_hook) PARAMS ((void));
405
406/* Called as appropriate to notify the interface of the specified breakpoint
407 conditions. */
408
c5aa993b
JM
409void (*create_breakpoint_hook) PARAMS ((struct breakpoint * bpt));
410void (*delete_breakpoint_hook) PARAMS ((struct breakpoint * bpt));
411void (*modify_breakpoint_hook) PARAMS ((struct breakpoint * bpt));
c906108c 412
6426a772
JM
413/* Called as appropriate to notify the interface that we have attached
414 to or detached from an already running process. */
415
416void (*attach_hook) PARAMS ((void));
417void (*detach_hook) PARAMS ((void));
418
c906108c
SS
419/* Called during long calculations to allow GUI to repair window damage, and to
420 check for stop buttons, etc... */
421
422void (*interactive_hook) PARAMS ((void));
423
424/* Called when the registers have changed, as a hint to a GUI
425 to minimize window update. */
426
427void (*registers_changed_hook) PARAMS ((void));
428
429/* Tell the GUI someone changed the register REGNO. -1 means
430 that the caller does not know which register changed or
c5aa993b 431 that several registers have changed (see value_assign). */
c906108c
SS
432void (*register_changed_hook) PARAMS ((int regno));
433
434/* Tell the GUI someone changed LEN bytes of memory at ADDR */
435void (*memory_changed_hook) PARAMS ((CORE_ADDR addr, int len));
436
437/* Called when going to wait for the target. Usually allows the GUI to run
438 while waiting for target events. */
439
c5aa993b 440int (*target_wait_hook) PARAMS ((int pid, struct target_waitstatus * status));
c906108c
SS
441
442/* Used by UI as a wrapper around command execution. May do various things
443 like enabling/disabling buttons, etc... */
444
c5aa993b 445void (*call_command_hook) PARAMS ((struct cmd_list_element * c, char *cmd,
c906108c
SS
446 int from_tty));
447
96baa820
JM
448/* Called after a `set' command has finished. Is only run if the
449 `set' command succeeded. */
450
451void (*set_hook) PARAMS ((struct cmd_list_element *c));
452
c906108c
SS
453/* Called when the current thread changes. Argument is thread id. */
454
455void (*context_hook) PARAMS ((int id));
456
457/* Takes control from error (). Typically used to prevent longjmps out of the
458 middle of the GUI. Usually used in conjunction with a catch routine. */
459
c5aa993b
JM
460NORETURN void (*error_hook)
461PARAMS ((void)) ATTR_NORETURN;
c906108c 462\f
c5aa993b 463
c4093a6a
JM
464/* Generally one should use catch_errors rather than manipulating these
465 directly. The exception is main(). */
466#if defined(HAVE_SIGSETJMP)
467#define SIGJMP_BUF sigjmp_buf
468#define SIGSETJMP(buf) sigsetjmp(buf, 1)
469#define SIGLONGJMP(buf,val) siglongjmp(buf,val)
470#else
471#define SIGJMP_BUF jmp_buf
472#define SIGSETJMP(buf) setjmp(buf)
473#define SIGLONGJMP(buf,val) longjmp(buf,val)
474#endif
475
c906108c 476/* Where to go for return_to_top_level (RETURN_ERROR). */
c4093a6a 477static SIGJMP_BUF error_return;
c906108c 478/* Where to go for return_to_top_level (RETURN_QUIT). */
c4093a6a 479static SIGJMP_BUF quit_return;
c906108c
SS
480
481/* Return for reason REASON. This generally gets back to the command
482 loop, but can be caught via catch_errors. */
483
c2d11a7d 484NORETURN void
6426a772 485return_to_top_level (reason)
c906108c
SS
486 enum return_reason reason;
487{
488 quit_flag = 0;
489 immediate_quit = 0;
490
491 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
492 I can think of a reason why that is vital, though). */
c5aa993b 493 bpstat_clear_actions (stop_bpstat); /* Clear queued breakpoint commands */
c906108c
SS
494
495 disable_current_display ();
496 do_cleanups (ALL_CLEANUPS);
c4093a6a 497 if (event_loop_p && target_can_async_p () && !target_executing)
43ff13b4 498 do_exec_cleanups (ALL_CLEANUPS);
6426a772
JM
499 if (event_loop_p && sync_execution)
500 do_exec_error_cleanups (ALL_CLEANUPS);
c906108c
SS
501
502 if (annotation_level > 1)
503 switch (reason)
504 {
505 case RETURN_QUIT:
506 annotate_quit ();
507 break;
508 case RETURN_ERROR:
509 annotate_error ();
510 break;
511 }
512
513 (NORETURN void) SIGLONGJMP
514 (reason == RETURN_ERROR ? error_return : quit_return, 1);
515}
516
517/* Call FUNC with arg ARGS, catching any errors. If there is no
518 error, return the value returned by FUNC. If there is an error,
519 print ERRSTRING, print the specific error message, then return
520 zero.
521
522 Must not be called with immediate_quit in effect (bad things might
523 happen, say we got a signal in the middle of a memcpy to quit_return).
524 This is an OK restriction; with very few exceptions immediate_quit can
525 be replaced by judicious use of QUIT.
526
527 MASK specifies what to catch; it is normally set to
528 RETURN_MASK_ALL, if for no other reason than that the code which
529 calls catch_errors might not be set up to deal with a quit which
530 isn't caught. But if the code can deal with it, it generally
531 should be RETURN_MASK_ERROR, unless for some reason it is more
532 useful to abort only the portion of the operation inside the
533 catch_errors. Note that quit should return to the command line
534 fairly quickly, even if some further processing is being done. */
535
11cf8741
JM
536/* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
537 error() et.al. could maintain a set of flags that indicate the the
538 current state of each of the longjmp buffers. This would give the
539 longjmp code the chance to detect a longjmp botch (before it gets
540 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
541 code also randomly used a SET_TOP_LEVEL macro that directly
542 initialize the longjmp buffers. */
543
544/* MAYBE: cagney/1999-11-05: Since the SET_TOP_LEVEL macro has been
545 eliminated it is now possible to use the stack to directly store
546 each longjmp buffer. The global code would just need to update a
547 pointer (onto the stack - ulgh!?) indicating the current longjmp
548 buffers. It would certainly improve the performance of the longjmp
549 code since the memcpy's would be eliminated. */
550
551/* MAYBE: cagney/1999-11-05: Should the catch_erros and cleanups code
552 be consolidated into a single file instead of being distributed
553 between utils.c and top.c? */
554
c906108c
SS
555int
556catch_errors (func, args, errstring, mask)
557 catch_errors_ftype *func;
558 PTR args;
559 char *errstring;
560 return_mask mask;
561{
562 SIGJMP_BUF saved_error;
563 SIGJMP_BUF saved_quit;
564 SIGJMP_BUF tmp_jmp;
565 int val;
566 struct cleanup *saved_cleanup_chain;
567 char *saved_error_pre_print;
568 char *saved_quit_pre_print;
569
570 saved_cleanup_chain = save_cleanups ();
571 saved_error_pre_print = error_pre_print;
572 saved_quit_pre_print = quit_pre_print;
573
574 if (mask & RETURN_MASK_ERROR)
575 {
c5aa993b 576 memcpy ((char *) saved_error, (char *) error_return, sizeof (SIGJMP_BUF));
c906108c
SS
577 error_pre_print = errstring;
578 }
579 if (mask & RETURN_MASK_QUIT)
580 {
581 memcpy (saved_quit, quit_return, sizeof (SIGJMP_BUF));
582 quit_pre_print = errstring;
583 }
584
585 if (SIGSETJMP (tmp_jmp) == 0)
586 {
587 if (mask & RETURN_MASK_ERROR)
588 memcpy (error_return, tmp_jmp, sizeof (SIGJMP_BUF));
589 if (mask & RETURN_MASK_QUIT)
590 memcpy (quit_return, tmp_jmp, sizeof (SIGJMP_BUF));
591 val = (*func) (args);
11cf8741
JM
592 /* FIXME: cagney/1999-11-05: A correct FUNC implementaton will
593 clean things up (restoring the cleanup chain) to the state
594 they were just prior to the call. Technically, this means
595 that the below restore_cleanups call is redundant.
596 Unfortunatly, many FUNC's are not that well behaved.
597 restore_cleanups should either be replaced with a do_cleanups
598 call (to cover the problem) or an assertion check to detect
599 bad FUNCs code. */
c906108c
SS
600 }
601 else
602 val = 0;
603
604 restore_cleanups (saved_cleanup_chain);
605
606 if (mask & RETURN_MASK_ERROR)
607 {
608 memcpy (error_return, saved_error, sizeof (SIGJMP_BUF));
609 error_pre_print = saved_error_pre_print;
610 }
611 if (mask & RETURN_MASK_QUIT)
612 {
613 memcpy (quit_return, saved_quit, sizeof (SIGJMP_BUF));
614 quit_pre_print = saved_quit_pre_print;
615 }
616 return val;
617}
618
11cf8741
JM
619struct captured_command_args
620 {
621 catch_command_errors_ftype *command;
622 char *arg;
623 int from_tty;
624 };
625
626static int
627do_captured_command (void *data)
628{
629 struct captured_command_args *context = data;
630 context->command (context->arg, context->from_tty);
631 /* FIXME: cagney/1999-11-07: Technically this do_cleanups() call
632 isn't needed. Instead an assertion check could be made that
633 simply confirmed that the called function correctly cleaned up
634 after its self. Unfortunatly, old code (prior to 1999-11-04) in
635 main.c was calling SET_TOP_LEVEL(), calling the command function,
636 and then *always* calling do_cleanups(). For the moment we
637 remain ``bug compatible'' with that old code.. */
638 do_cleanups (ALL_CLEANUPS);
639 return 1;
640}
641
642int
643catch_command_errors (catch_command_errors_ftype *command,
644 char *arg, int from_tty, return_mask mask)
645{
646 struct captured_command_args args;
647 args.command = command;
648 args.arg = arg;
649 args.from_tty = from_tty;
650 return catch_errors (do_captured_command, &args, "", mask);
651}
652
653
c906108c
SS
654/* Handler for SIGHUP. */
655
656#ifdef SIGHUP
657static void
658disconnect (signo)
c5aa993b 659 int signo;
c906108c
SS
660{
661 catch_errors (quit_cover, NULL,
c5aa993b 662 "Could not kill the program being debugged", RETURN_MASK_ALL);
c906108c
SS
663 signal (SIGHUP, SIG_DFL);
664 kill (getpid (), SIGHUP);
665}
666
667/* Just a little helper function for disconnect(). */
668
392a587b 669/* NOTE 1999-04-29: This function will be static again, once we modify
cd0fc7c3
SS
670 gdb to use the event loop as the default command loop and we merge
671 event-top.c into this file, top.c */
672/* static */ int
c906108c
SS
673quit_cover (s)
674 PTR s;
675{
c5aa993b
JM
676 caution = 0; /* Throw caution to the wind -- we're exiting.
677 This prevents asking the user dumb questions. */
678 quit_command ((char *) 0, 0);
c906108c
SS
679 return 0;
680}
681#endif /* defined SIGHUP */
682\f
683/* Line number we are currently in in a file which is being sourced. */
392a587b 684/* NOTE 1999-04-29: This variable will be static again, once we modify
cd0fc7c3
SS
685 gdb to use the event loop as the default command loop and we merge
686 event-top.c into this file, top.c */
687/* static */ int source_line_number;
c906108c
SS
688
689/* Name of the file we are sourcing. */
392a587b 690/* NOTE 1999-04-29: This variable will be static again, once we modify
cd0fc7c3
SS
691 gdb to use the event loop as the default command loop and we merge
692 event-top.c into this file, top.c */
693/* static */ char *source_file_name;
c906108c
SS
694
695/* Buffer containing the error_pre_print used by the source stuff.
696 Malloc'd. */
392a587b 697/* NOTE 1999-04-29: This variable will be static again, once we modify
cd0fc7c3
SS
698 gdb to use the event loop as the default command loop and we merge
699 event-top.c into this file, top.c */
700/* static */ char *source_error;
c906108c
SS
701static int source_error_allocated;
702
703/* Something to glom on to the start of error_pre_print if source_file_name
704 is set. */
392a587b 705/* NOTE 1999-04-29: This variable will be static again, once we modify
cd0fc7c3
SS
706 gdb to use the event loop as the default command loop and we merge
707 event-top.c into this file, top.c */
708/* static */ char *source_pre_error;
c906108c
SS
709
710/* Clean up on error during a "source" command (or execution of a
711 user-defined command). */
712
713static void
714source_cleanup (stream)
715 FILE *stream;
716{
717 /* Restore the previous input stream. */
718 instream = stream;
719}
720
721/* Read commands from STREAM. */
722void
723read_command_file (stream)
724 FILE *stream;
725{
726 struct cleanup *cleanups;
727
728 cleanups = make_cleanup ((make_cleanup_func) source_cleanup, instream);
729 instream = stream;
c5aa993b 730 command_loop ();
c906108c
SS
731 do_cleanups (cleanups);
732}
733\f
734extern void init_proc PARAMS ((void));
735
736void (*pre_init_ui_hook) PARAMS ((void));
737
738void
739gdb_init (argv0)
740 char *argv0;
741{
742 if (pre_init_ui_hook)
743 pre_init_ui_hook ();
744
745 /* Run the init function of each source file */
746
747 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
748 current_directory = gdb_dirbuf;
749
a0b3c4fd
JM
750#ifdef __MSDOS__
751 /* Make sure we return to the original directory upon exit, come
752 what may, since the OS doesn't do that for us. */
753 make_final_cleanup ((make_cleanup_func) chdir, strsave (current_directory));
754#endif
755
c5aa993b
JM
756 init_cmd_lists (); /* This needs to be done first */
757 initialize_targets (); /* Setup target_terminal macros for utils.c */
758 initialize_utils (); /* Make errors and warnings possible */
c906108c 759 initialize_all_files ();
e514a9d6 760 initialize_current_architecture ();
c5aa993b 761 init_main (); /* But that omits this file! Do it now */
cd0fc7c3
SS
762
763 /* The signal handling mechanism is different depending whether or
764 not the async version is run. NOTE: in the future we plan to make
765 the event loop be the default engine of gdb, and this difference
766 will disappear. */
6426a772 767 if (event_loop_p)
cd0fc7c3
SS
768 async_init_signals ();
769 else
c5aa993b 770 init_signals ();
c906108c 771
c906108c
SS
772 /* We need a default language for parsing expressions, so simple things like
773 "set width 0" won't fail if no language is explicitly set in a config file
774 or implicitly set by reading an executable during startup. */
775 set_language (language_c);
c5aa993b 776 expected_language = current_language; /* don't warn about the change. */
c906108c 777
0f71a2f6
JM
778 /* All the interpreters should have had a look at things by now.
779 Initialize the selected interpreter. */
c906108c
SS
780 if (init_ui_hook)
781 init_ui_hook (argv0);
782}
783
784/* Allocate, initialize a new command line structure for one of the
785 control commands (if/while). */
786
787static struct command_line *
788build_command_line (type, args)
789 enum command_control_type type;
790 char *args;
791{
792 struct command_line *cmd;
793
794 if (args == NULL)
795 error ("if/while commands require arguments.\n");
796
c5aa993b 797 cmd = (struct command_line *) xmalloc (sizeof (struct command_line));
c906108c
SS
798 cmd->next = NULL;
799 cmd->control_type = type;
800
801 cmd->body_count = 1;
802 cmd->body_list
c5aa993b
JM
803 = (struct command_line **) xmalloc (sizeof (struct command_line *)
804 * cmd->body_count);
c906108c
SS
805 memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
806 cmd->line = savestring (args, strlen (args));
807 return cmd;
808}
809
810/* Build and return a new command structure for the control commands
811 such as "if" and "while". */
812
813static struct command_line *
814get_command_line (type, arg)
815 enum command_control_type type;
816 char *arg;
817{
818 struct command_line *cmd;
819 struct cleanup *old_chain = NULL;
820
821 /* Allocate and build a new command line structure. */
822 cmd = build_command_line (type, arg);
823
824 old_chain = make_cleanup ((make_cleanup_func) free_command_lines, &cmd);
825
826 /* Read in the body of this command. */
827 if (recurse_read_control_structure (cmd) == invalid_control)
828 {
829 warning ("error reading in control structure\n");
830 do_cleanups (old_chain);
831 return NULL;
832 }
833
834 discard_cleanups (old_chain);
835 return cmd;
836}
837
838/* Recursively print a command (including full control structures). */
839void
9e086581 840print_command_line (cmd, depth, stream)
c906108c
SS
841 struct command_line *cmd;
842 unsigned int depth;
9e086581 843 GDB_FILE *stream;
c906108c
SS
844{
845 unsigned int i;
846
847 if (depth)
848 {
849 for (i = 0; i < depth; i++)
9e086581 850 fputs_filtered (" ", stream);
c906108c
SS
851 }
852
853 /* A simple command, print it and return. */
854 if (cmd->control_type == simple_control)
855 {
9e086581
JM
856 fputs_filtered (cmd->line, stream);
857 fputs_filtered ("\n", stream);
c906108c
SS
858 return;
859 }
860
861 /* loop_continue to jump to the start of a while loop, print it
862 and return. */
863 if (cmd->control_type == continue_control)
864 {
9e086581 865 fputs_filtered ("loop_continue\n", stream);
c906108c
SS
866 return;
867 }
868
869 /* loop_break to break out of a while loop, print it and return. */
870 if (cmd->control_type == break_control)
871 {
9e086581 872 fputs_filtered ("loop_break\n", stream);
c906108c
SS
873 return;
874 }
875
876 /* A while command. Recursively print its subcommands before returning. */
877 if (cmd->control_type == while_control)
878 {
879 struct command_line *list;
9e086581
JM
880 fputs_filtered ("while ", stream);
881 fputs_filtered (cmd->line, stream);
882 fputs_filtered ("\n", stream);
c906108c
SS
883 list = *cmd->body_list;
884 while (list)
885 {
9e086581 886 print_command_line (list, depth + 1, stream);
c906108c
SS
887 list = list->next;
888 }
889 }
890
891 /* An if command. Recursively print both arms before returning. */
892 if (cmd->control_type == if_control)
893 {
9e086581
JM
894 fputs_filtered ("if ", stream);
895 fputs_filtered (cmd->line, stream);
896 fputs_filtered ("\n", stream);
c906108c 897 /* The true arm. */
9e086581 898 print_command_line (cmd->body_list[0], depth + 1, stream);
c906108c
SS
899
900 /* Show the false arm if it exists. */
901 if (cmd->body_count == 2)
c5aa993b
JM
902 {
903 if (depth)
904 {
905 for (i = 0; i < depth; i++)
906 fputs_filtered (" ", stream);
907 }
908 fputs_filtered ("else\n", stream);
909 print_command_line (cmd->body_list[1], depth + 1, stream);
910 }
c906108c
SS
911 if (depth)
912 {
913 for (i = 0; i < depth; i++)
9e086581 914 fputs_filtered (" ", stream);
c906108c 915 }
9e086581 916 fputs_filtered ("end\n", stream);
c906108c
SS
917 }
918}
919
920/* Execute the command in CMD. */
921
922enum command_control_type
923execute_control_command (cmd)
924 struct command_line *cmd;
925{
926 struct expression *expr;
927 struct command_line *current;
928 struct cleanup *old_chain = 0;
929 value_ptr val;
930 value_ptr val_mark;
931 int loop;
932 enum command_control_type ret;
933 char *new_line;
934
935 switch (cmd->control_type)
936 {
937 case simple_control:
938 /* A simple command, execute it and return. */
939 new_line = insert_args (cmd->line);
940 if (!new_line)
941 return invalid_control;
c5aa993b
JM
942 old_chain = make_cleanup ((make_cleanup_func) free_current_contents,
943 &new_line);
c906108c
SS
944 execute_command (new_line, 0);
945 ret = cmd->control_type;
946 break;
947
948 case continue_control:
949 case break_control:
950 /* Return for "continue", and "break" so we can either
c5aa993b 951 continue the loop at the top, or break out. */
c906108c
SS
952 ret = cmd->control_type;
953 break;
954
955 case while_control:
956 {
957 /* Parse the loop control expression for the while statement. */
958 new_line = insert_args (cmd->line);
959 if (!new_line)
960 return invalid_control;
c5aa993b
JM
961 old_chain = make_cleanup ((make_cleanup_func) free_current_contents,
962 &new_line);
c906108c
SS
963 expr = parse_expression (new_line);
964 make_cleanup ((make_cleanup_func) free_current_contents, &expr);
c5aa993b 965
c906108c
SS
966 ret = simple_control;
967 loop = 1;
968
969 /* Keep iterating so long as the expression is true. */
970 while (loop == 1)
971 {
972 int cond_result;
973
974 QUIT;
975
976 /* Evaluate the expression. */
977 val_mark = value_mark ();
978 val = evaluate_expression (expr);
979 cond_result = value_true (val);
980 value_free_to_mark (val_mark);
981
982 /* If the value is false, then break out of the loop. */
983 if (!cond_result)
984 break;
985
986 /* Execute the body of the while statement. */
987 current = *cmd->body_list;
988 while (current)
989 {
990 ret = execute_control_command (current);
991
992 /* If we got an error, or a "break" command, then stop
993 looping. */
994 if (ret == invalid_control || ret == break_control)
995 {
996 loop = 0;
997 break;
998 }
999
1000 /* If we got a "continue" command, then restart the loop
1001 at this point. */
1002 if (ret == continue_control)
1003 break;
c5aa993b 1004
c906108c 1005 /* Get the next statement. */
c5aa993b 1006 current = current->next;
c906108c
SS
1007 }
1008 }
1009
1010 /* Reset RET so that we don't recurse the break all the way down. */
1011 if (ret == break_control)
1012 ret = simple_control;
1013
1014 break;
1015 }
1016
1017 case if_control:
1018 {
1019 new_line = insert_args (cmd->line);
1020 if (!new_line)
1021 return invalid_control;
c5aa993b
JM
1022 old_chain = make_cleanup ((make_cleanup_func) free_current_contents,
1023 &new_line);
c906108c
SS
1024 /* Parse the conditional for the if statement. */
1025 expr = parse_expression (new_line);
1026 make_cleanup ((make_cleanup_func) free_current_contents, &expr);
1027
1028 current = NULL;
1029 ret = simple_control;
1030
1031 /* Evaluate the conditional. */
1032 val_mark = value_mark ();
1033 val = evaluate_expression (expr);
1034
1035 /* Choose which arm to take commands from based on the value of the
1036 conditional expression. */
1037 if (value_true (val))
1038 current = *cmd->body_list;
1039 else if (cmd->body_count == 2)
1040 current = *(cmd->body_list + 1);
1041 value_free_to_mark (val_mark);
1042
1043 /* Execute commands in the given arm. */
1044 while (current)
1045 {
1046 ret = execute_control_command (current);
1047
1048 /* If we got an error, get out. */
1049 if (ret != simple_control)
1050 break;
1051
1052 /* Get the next statement in the body. */
1053 current = current->next;
1054 }
1055
1056 break;
1057 }
1058
1059 default:
1060 warning ("Invalid control type in command structure.");
1061 return invalid_control;
1062 }
1063
1064 if (old_chain)
1065 do_cleanups (old_chain);
1066
1067 return ret;
1068}
1069
1070/* "while" command support. Executes a body of statements while the
1071 loop condition is nonzero. */
1072
1073static void
1074while_command (arg, from_tty)
1075 char *arg;
1076 int from_tty;
1077{
1078 struct command_line *command = NULL;
1079
1080 control_level = 1;
1081 command = get_command_line (while_control, arg);
1082
1083 if (command == NULL)
1084 return;
1085
1086 execute_control_command (command);
1087 free_command_lines (&command);
1088}
1089
1090/* "if" command support. Execute either the true or false arm depending
1091 on the value of the if conditional. */
1092
1093static void
1094if_command (arg, from_tty)
1095 char *arg;
1096 int from_tty;
1097{
1098 struct command_line *command = NULL;
1099
1100 control_level = 1;
1101 command = get_command_line (if_control, arg);
1102
1103 if (command == NULL)
1104 return;
1105
1106 execute_control_command (command);
1107 free_command_lines (&command);
1108}
1109
1110/* Cleanup */
1111static void
1112arg_cleanup ()
1113{
1114 struct user_args *oargs = user_args;
1115 if (!user_args)
96baa820 1116 internal_error ("Internal error, arg_cleanup called with no user args.\n");
c906108c
SS
1117
1118 user_args = user_args->next;
1119 free (oargs);
1120}
1121
1122/* Bind the incomming arguments for a user defined command to
1123 $arg0, $arg1 ... $argMAXUSERARGS. */
1124
1125static struct cleanup *
1126setup_user_args (p)
1127 char *p;
1128{
1129 struct user_args *args;
1130 struct cleanup *old_chain;
1131 unsigned int arg_count = 0;
1132
c5aa993b 1133 args = (struct user_args *) xmalloc (sizeof (struct user_args));
c906108c
SS
1134 memset (args, 0, sizeof (struct user_args));
1135
1136 args->next = user_args;
1137 user_args = args;
1138
1139 old_chain = make_cleanup ((make_cleanup_func) arg_cleanup, 0);
1140
1141 if (p == NULL)
1142 return old_chain;
1143
1144 while (*p)
1145 {
1146 char *start_arg;
1147 int squote = 0;
1148 int dquote = 0;
1149 int bsquote = 0;
1150
1151 if (arg_count >= MAXUSERARGS)
1152 {
1153 error ("user defined function may only have %d arguments.\n",
1154 MAXUSERARGS);
1155 return old_chain;
1156 }
1157
1158 /* Strip whitespace. */
1159 while (*p == ' ' || *p == '\t')
1160 p++;
1161
1162 /* P now points to an argument. */
1163 start_arg = p;
1164 user_args->a[arg_count].arg = p;
1165
1166 /* Get to the end of this argument. */
1167 while (*p)
1168 {
1169 if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
1170 break;
1171 else
1172 {
1173 if (bsquote)
1174 bsquote = 0;
1175 else if (*p == '\\')
1176 bsquote = 1;
1177 else if (squote)
1178 {
1179 if (*p == '\'')
1180 squote = 0;
1181 }
1182 else if (dquote)
1183 {
1184 if (*p == '"')
1185 dquote = 0;
1186 }
1187 else
1188 {
1189 if (*p == '\'')
1190 squote = 1;
1191 else if (*p == '"')
1192 dquote = 1;
1193 }
1194 p++;
1195 }
1196 }
1197
1198 user_args->a[arg_count].len = p - start_arg;
1199 arg_count++;
1200 user_args->count++;
1201 }
1202 return old_chain;
1203}
1204
1205/* Given character string P, return a point to the first argument ($arg),
1206 or NULL if P contains no arguments. */
1207
1208static char *
1209locate_arg (p)
1210 char *p;
1211{
1212 while ((p = strchr (p, '$')))
1213 {
1214 if (strncmp (p, "$arg", 4) == 0 && isdigit (p[4]))
1215 return p;
1216 p++;
1217 }
1218 return NULL;
1219}
1220
1221/* Insert the user defined arguments stored in user_arg into the $arg
1222 arguments found in line, with the updated copy being placed into nline. */
1223
1224static char *
1225insert_args (line)
1226 char *line;
1227{
1228 char *p, *save_line, *new_line;
1229 unsigned len, i;
1230
1231 /* First we need to know how much memory to allocate for the new line. */
1232 save_line = line;
1233 len = 0;
1234 while ((p = locate_arg (line)))
1235 {
1236 len += p - line;
1237 i = p[4] - '0';
c5aa993b 1238
c906108c
SS
1239 if (i >= user_args->count)
1240 {
1241 error ("Missing argument %d in user function.\n", i);
1242 return NULL;
1243 }
1244 len += user_args->a[i].len;
1245 line = p + 5;
1246 }
1247
1248 /* Don't forget the tail. */
1249 len += strlen (line);
1250
1251 /* Allocate space for the new line and fill it in. */
c5aa993b 1252 new_line = (char *) xmalloc (len + 1);
c906108c
SS
1253 if (new_line == NULL)
1254 return NULL;
1255
1256 /* Restore pointer to beginning of old line. */
1257 line = save_line;
1258
1259 /* Save pointer to beginning of new line. */
1260 save_line = new_line;
1261
1262 while ((p = locate_arg (line)))
1263 {
1264 int i, len;
1265
1266 memcpy (new_line, line, p - line);
1267 new_line += p - line;
1268 i = p[4] - '0';
1269
1270 len = user_args->a[i].len;
1271 if (len)
1272 {
1273 memcpy (new_line, user_args->a[i].arg, len);
1274 new_line += len;
1275 }
1276 line = p + 5;
1277 }
1278 /* Don't forget the tail. */
1279 strcpy (new_line, line);
1280
1281 /* Return a pointer to the beginning of the new line. */
1282 return save_line;
1283}
1284
1285void
1286execute_user_command (c, args)
1287 struct cmd_list_element *c;
1288 char *args;
1289{
1290 register struct command_line *cmdlines;
1291 struct cleanup *old_chain;
1292 enum command_control_type ret;
1293
1294 old_chain = setup_user_args (args);
1295
1296 cmdlines = c->user_commands;
1297 if (cmdlines == 0)
1298 /* Null command */
1299 return;
1300
1301 /* Set the instream to 0, indicating execution of a
1302 user-defined function. */
1303 old_chain = make_cleanup ((make_cleanup_func) source_cleanup, instream);
1304 instream = (FILE *) 0;
1305 while (cmdlines)
1306 {
1307 ret = execute_control_command (cmdlines);
1308 if (ret != simple_control && ret != break_control)
1309 {
1310 warning ("Error in control structure.\n");
1311 break;
1312 }
1313 cmdlines = cmdlines->next;
1314 }
1315 do_cleanups (old_chain);
1316}
1317
1318/* Execute the line P as a command.
1319 Pass FROM_TTY as second argument to the defining function. */
1320
1321void
1322execute_command (p, from_tty)
1323 char *p;
1324 int from_tty;
1325{
1326 register struct cmd_list_element *c;
1327 register enum language flang;
1328 static int warned = 0;
1329 /* FIXME: These should really be in an appropriate header file */
1330 extern void serial_log_command PARAMS ((const char *));
1331
1332 free_all_values ();
1333
1334 /* Force cleanup of any alloca areas if using C alloca instead of
1335 a builtin alloca. */
1336 alloca (0);
1337
1338 /* This can happen when command_line_input hits end of file. */
1339 if (p == NULL)
c5aa993b 1340 return;
c906108c
SS
1341
1342 serial_log_command (p);
1343
c5aa993b
JM
1344 while (*p == ' ' || *p == '\t')
1345 p++;
c906108c
SS
1346 if (*p)
1347 {
1348 char *arg;
1349
1350 c = lookup_cmd (&p, cmdlist, "", 0, 1);
43ff13b4
JM
1351
1352 /* If the target is running, we allow only a limited set of
1353 commands. */
6426a772 1354 if (event_loop_p && target_can_async_p () && target_executing)
43ff13b4
JM
1355 if (!strcmp (c->name, "help")
1356 && !strcmp (c->name, "pwd")
1357 && !strcmp (c->name, "show")
1358 && !strcmp (c->name, "stop"))
1359 error ("Cannot execute this command while the target is running.");
1360
c906108c
SS
1361 /* Pass null arg rather than an empty one. */
1362 arg = *p ? p : 0;
1363
1364 /* Clear off trailing whitespace, except for set and complete command. */
1365 if (arg && c->type != set_cmd && c->function.cfunc != complete_command)
1366 {
1367 p = arg + strlen (arg) - 1;
1368 while (p >= arg && (*p == ' ' || *p == '\t'))
1369 p--;
1370 *(p + 1) = '\0';
1371 }
1372
1373 /* If this command has been hooked, run the hook first. */
1374 if (c->hook)
c5aa993b 1375 execute_user_command (c->hook, (char *) 0);
c906108c
SS
1376
1377 if (c->class == class_user)
1378 execute_user_command (c, arg);
1379 else if (c->type == set_cmd || c->type == show_cmd)
1380 do_setshow_command (arg, from_tty & caution, c);
1381 else if (c->function.cfunc == NO_FUNCTION)
1382 error ("That is not a command, just a help topic.");
1383 else if (call_command_hook)
1384 call_command_hook (c, arg, from_tty & caution);
1385 else
1386 (*c->function.cfunc) (arg, from_tty & caution);
c5aa993b 1387 }
c906108c
SS
1388
1389 /* Tell the user if the language has changed (except first time). */
1390 if (current_language != expected_language)
c5aa993b
JM
1391 {
1392 if (language_mode == language_mode_auto)
1393 {
1394 language_info (1); /* Print what changed. */
1395 }
1396 warned = 0;
c906108c 1397 }
c906108c
SS
1398
1399 /* Warn the user if the working language does not match the
1400 language of the current frame. Only warn the user if we are
1401 actually running the program, i.e. there is a stack. */
1402 /* FIXME: This should be cacheing the frame and only running when
1403 the frame changes. */
1404
1405 if (target_has_stack)
1406 {
1407 flang = get_frame_language ();
1408 if (!warned
1409 && flang != language_unknown
1410 && flang != current_language->la_language)
1411 {
1412 printf_filtered ("%s\n", lang_frame_mismatch_warn);
1413 warned = 1;
1414 }
1415 }
1416}
1417
1418/* ARGSUSED */
392a587b 1419/* NOTE 1999-04-29: This function will be static again, once we modify
cd0fc7c3
SS
1420 gdb to use the event loop as the default command loop and we merge
1421 event-top.c into this file, top.c */
1422/* static */ void
c906108c
SS
1423command_loop_marker (foo)
1424 int foo;
1425{
1426}
1427
1428/* Read commands from `instream' and execute them
1429 until end of file or error reading instream. */
1430
1431void
1432command_loop ()
1433{
1434 struct cleanup *old_chain;
1435 char *command;
1436 int stdin_is_tty = ISATTY (stdin);
1437 long time_at_cmd_start;
1438#ifdef HAVE_SBRK
1439 long space_at_cmd_start = 0;
1440#endif
1441 extern int display_time;
1442 extern int display_space;
1443
1444 while (instream && !feof (instream))
1445 {
1446#if defined(TUI)
1447 extern int insert_mode;
1448#endif
1449 if (window_hook && instream == stdin)
0f71a2f6 1450 (*window_hook) (instream, get_prompt ());
c906108c
SS
1451
1452 quit_flag = 0;
1453 if (instream == stdin && stdin_is_tty)
1454 reinitialize_more_filter ();
1455 old_chain = make_cleanup ((make_cleanup_func) command_loop_marker, 0);
1456
1457#if defined(TUI)
1458 /* A bit of paranoia: I want to make sure the "insert_mode" global
1459 * is clear except when it is being used for command-line editing
1460 * (see tuiIO.c, utils.c); otherwise normal output will
1461 * get messed up in the TUI. So clear it before/after
1462 * the command-line-input call. - RT
1463 */
1464 insert_mode = 0;
1465#endif
1466 /* Get a command-line. This calls the readline package. */
c5aa993b 1467 command = command_line_input (instream == stdin ?
0f71a2f6 1468 get_prompt () : (char *) NULL,
c906108c
SS
1469 instream == stdin, "prompt");
1470#if defined(TUI)
1471 insert_mode = 0;
1472#endif
1473 if (command == 0)
1474 return;
1475
1476 time_at_cmd_start = get_run_time ();
1477
1478 if (display_space)
1479 {
1480#ifdef HAVE_SBRK
1481 extern char **environ;
1482 char *lim = (char *) sbrk (0);
1483
1484 space_at_cmd_start = (long) (lim - (char *) &environ);
1485#endif
1486 }
1487
1488 execute_command (command, instream == stdin);
1489 /* Do any commands attached to breakpoint we stopped at. */
1490 bpstat_do_actions (&stop_bpstat);
1491 do_cleanups (old_chain);
1492
1493 if (display_time)
1494 {
1495 long cmd_time = get_run_time () - time_at_cmd_start;
1496
1497 printf_unfiltered ("Command execution time: %ld.%06ld\n",
1498 cmd_time / 1000000, cmd_time % 1000000);
1499 }
1500
1501 if (display_space)
1502 {
1503#ifdef HAVE_SBRK
1504 extern char **environ;
1505 char *lim = (char *) sbrk (0);
1506 long space_now = lim - (char *) &environ;
1507 long space_diff = space_now - space_at_cmd_start;
1508
1509 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1510 space_now,
1511 (space_diff >= 0 ? '+' : '-'),
1512 space_diff);
1513#endif
1514 }
1515 }
1516}
392a587b 1517
9e0b60a8
JM
1518\f
1519/* Commands call this if they do not want to be repeated by null lines. */
1520
1521void
1522dont_repeat ()
1523{
1524 if (server_command)
1525 return;
1526
1527 /* If we aren't reading from standard input, we are saving the last
1528 thing read from stdin in line and don't want to delete it. Null lines
1529 won't repeat here in any case. */
1530 if (instream == stdin)
1531 *line = 0;
1532}
1533\f
1534/* Read a line from the stream "instream" without command line editing.
1535
1536 It prints PROMPT_ARG once at the start.
1537 Action is compatible with "readline", e.g. space for the result is
1538 malloc'd and should be freed by the caller.
1539
1540 A NULL return means end of file. */
1541char *
1542gdb_readline (prompt_arg)
1543 char *prompt_arg;
1544{
1545 int c;
1546 char *result;
1547 int input_index = 0;
1548 int result_size = 80;
1549
1550 if (prompt_arg)
1551 {
1552 /* Don't use a _filtered function here. It causes the assumed
c5aa993b
JM
1553 character position to be off, since the newline we read from
1554 the user is not accounted for. */
9e0b60a8
JM
1555 fputs_unfiltered (prompt_arg, gdb_stdout);
1556#ifdef MPW
1557 /* Move to a new line so the entered line doesn't have a prompt
c5aa993b 1558 on the front of it. */
9e0b60a8
JM
1559 fputs_unfiltered ("\n", gdb_stdout);
1560#endif /* MPW */
1561 gdb_flush (gdb_stdout);
1562 }
1563
1564 result = (char *) xmalloc (result_size);
1565
1566 while (1)
1567 {
1568 /* Read from stdin if we are executing a user defined command.
c5aa993b 1569 This is the right thing for prompt_for_continue, at least. */
9e0b60a8
JM
1570 c = fgetc (instream ? instream : stdin);
1571
1572 if (c == EOF)
1573 {
1574 if (input_index > 0)
1575 /* The last line does not end with a newline. Return it, and
1576 if we are called again fgetc will still return EOF and
1577 we'll return NULL then. */
1578 break;
1579 free (result);
1580 return NULL;
1581 }
1582
1583 if (c == '\n')
1584#ifndef CRLF_SOURCE_FILES
1585 break;
1586#else
1587 {
1588 if (input_index > 0 && result[input_index - 1] == '\r')
1589 input_index--;
1590 break;
1591 }
1592#endif
1593
1594 result[input_index++] = c;
1595 while (input_index >= result_size)
1596 {
1597 result_size *= 2;
1598 result = (char *) xrealloc (result, result_size);
1599 }
1600 }
1601
1602 result[input_index++] = '\0';
1603 return result;
1604}
1605
1606/* Variables which control command line editing and history
1607 substitution. These variables are given default values at the end
1608 of this file. */
1609static int command_editing_p;
1610/* NOTE 1999-04-29: This variable will be static again, once we modify
1611 gdb to use the event loop as the default command loop and we merge
1612 event-top.c into this file, top.c */
1613/* static */ int history_expansion_p;
1614static int write_history_p;
1615static int history_size;
1616static char *history_filename;
1617
1618/* readline uses the word breaks for two things:
1619 (1) In figuring out where to point the TEXT parameter to the
1620 rl_completion_entry_function. Since we don't use TEXT for much,
1621 it doesn't matter a lot what the word breaks are for this purpose, but
1622 it does affect how much stuff M-? lists.
1623 (2) If one of the matches contains a word break character, readline
1624 will quote it. That's why we switch between
1625 gdb_completer_word_break_characters and
1626 gdb_completer_command_word_break_characters. I'm not sure when
1627 we need this behavior (perhaps for funky characters in C++ symbols?). */
1628
1629/* Variables which are necessary for fancy command line editing. */
1630char *gdb_completer_word_break_characters =
c5aa993b 1631" \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
9e0b60a8
JM
1632
1633/* When completing on command names, we remove '-' from the list of
1634 word break characters, since we use it in command names. If the
1635 readline library sees one in any of the current completion strings,
1636 it thinks that the string needs to be quoted and automatically supplies
1637 a leading quote. */
1638char *gdb_completer_command_word_break_characters =
c5aa993b 1639" \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
9e0b60a8
JM
1640
1641/* Characters that can be used to quote completion strings. Note that we
1642 can't include '"' because the gdb C parser treats such quoted sequences
1643 as strings. */
1644char *gdb_completer_quote_characters =
c5aa993b 1645"'";
9e0b60a8
JM
1646
1647/* Functions that are used as part of the fancy command line editing. */
1648
1649/* This can be used for functions which don't want to complete on symbols
1650 but don't want to complete on anything else either. */
1651/* ARGSUSED */
1652char **
1653noop_completer (text, prefix)
1654 char *text;
1655 char *prefix;
1656{
1657 return NULL;
1658}
1659
1660/* Complete on filenames. */
1661char **
1662filename_completer (text, word)
1663 char *text;
1664 char *word;
1665{
1666 /* From readline. */
1667 extern char *filename_completion_function PARAMS ((char *, int));
1668 int subsequent_name;
1669 char **return_val;
1670 int return_val_used;
1671 int return_val_alloced;
1672
1673 return_val_used = 0;
1674 /* Small for testing. */
1675 return_val_alloced = 1;
1676 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
1677
1678 subsequent_name = 0;
1679 while (1)
1680 {
1681 char *p;
1682 p = filename_completion_function (text, subsequent_name);
1683 if (return_val_used >= return_val_alloced)
1684 {
1685 return_val_alloced *= 2;
1686 return_val =
1687 (char **) xrealloc (return_val,
1688 return_val_alloced * sizeof (char *));
1689 }
1690 if (p == NULL)
1691 {
1692 return_val[return_val_used++] = p;
1693 break;
1694 }
1695 /* Like emacs, don't complete on old versions. Especially useful
c5aa993b 1696 in the "source" command. */
9e0b60a8
JM
1697 if (p[strlen (p) - 1] == '~')
1698 continue;
1699
1700 {
1701 char *q;
1702 if (word == text)
1703 /* Return exactly p. */
1704 return_val[return_val_used++] = p;
1705 else if (word > text)
1706 {
1707 /* Return some portion of p. */
1708 q = xmalloc (strlen (p) + 5);
1709 strcpy (q, p + (word - text));
1710 return_val[return_val_used++] = q;
1711 free (p);
1712 }
1713 else
1714 {
1715 /* Return some of TEXT plus p. */
1716 q = xmalloc (strlen (p) + (text - word) + 5);
1717 strncpy (q, word, text - word);
1718 q[text - word] = '\0';
1719 strcat (q, p);
1720 return_val[return_val_used++] = q;
1721 free (p);
1722 }
1723 }
1724 subsequent_name = 1;
1725 }
1726#if 0
1727 /* There is no way to do this just long enough to affect quote inserting
1728 without also affecting the next completion. This should be fixed in
1729 readline. FIXME. */
1730 /* Insure that readline does the right thing
1731 with respect to inserting quotes. */
1732 rl_completer_word_break_characters = "";
1733#endif
1734 return return_val;
1735}
1736
1737/* Here are some useful test cases for completion. FIXME: These should
1738 be put in the test suite. They should be tested with both M-? and TAB.
1739
1740 "show output-" "radix"
1741 "show output" "-radix"
1742 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1743 "p " ambiguous (all symbols)
1744 "info t foo" no completions
1745 "info t " no completions
1746 "info t" ambiguous ("info target", "info terminal", etc.)
1747 "info ajksdlfk" no completions
1748 "info ajksdlfk " no completions
1749 "info" " "
1750 "info " ambiguous (all info commands)
1751 "p \"a" no completions (string constant)
1752 "p 'a" ambiguous (all symbols starting with a)
1753 "p b-a" ambiguous (all symbols starting with a)
1754 "p b-" ambiguous (all symbols)
1755 "file Make" "file" (word break hard to screw up here)
1756 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
c5aa993b 1757 */
9e0b60a8
JM
1758
1759/* Generate completions one by one for the completer. Each time we are
1760 called return another potential completion to the caller.
1761 line_completion just completes on commands or passes the buck to the
1762 command's completer function, the stuff specific to symbol completion
1763 is in make_symbol_completion_list.
1764
1765 TEXT is the caller's idea of the "word" we are looking at.
1766
1767 MATCHES is the number of matches that have currently been collected from
1768 calling this completion function. When zero, then we need to initialize,
1769 otherwise the initialization has already taken place and we can just
1770 return the next potential completion string.
1771
1772 LINE_BUFFER is available to be looked at; it contains the entire text
1773 of the line. POINT is the offset in that line of the cursor. You
1774 should pretend that the line ends at POINT.
1775
1776 Returns NULL if there are no more completions, else a pointer to a string
1777 which is a possible completion, it is the caller's responsibility to
1778 free the string. */
1779
1780static char *
1781line_completion_function (text, matches, line_buffer, point)
1782 char *text;
1783 int matches;
1784 char *line_buffer;
1785 int point;
1786{
c5aa993b
JM
1787 static char **list = (char **) NULL; /* Cache of completions */
1788 static int index; /* Next cached completion */
9e0b60a8
JM
1789 char *output = NULL;
1790 char *tmp_command, *p;
1791 /* Pointer within tmp_command which corresponds to text. */
1792 char *word;
1793 struct cmd_list_element *c, *result_list;
1794
1795 if (matches == 0)
1796 {
1797 /* The caller is beginning to accumulate a new set of completions, so
c5aa993b
JM
1798 we need to find all of them now, and cache them for returning one at
1799 a time on future calls. */
9e0b60a8
JM
1800
1801 if (list)
1802 {
1803 /* Free the storage used by LIST, but not by the strings inside.
1804 This is because rl_complete_internal () frees the strings. */
c5aa993b 1805 free ((PTR) list);
9e0b60a8
JM
1806 }
1807 list = 0;
1808 index = 0;
1809
1810 /* Choose the default set of word break characters to break completions.
c5aa993b
JM
1811 If we later find out that we are doing completions on command strings
1812 (as opposed to strings supplied by the individual command completer
1813 functions, which can be any string) then we will switch to the
1814 special word break set for command strings, which leaves out the
1815 '-' character used in some commands. */
9e0b60a8
JM
1816
1817 rl_completer_word_break_characters =
c5aa993b 1818 gdb_completer_word_break_characters;
9e0b60a8
JM
1819
1820 /* Decide whether to complete on a list of gdb commands or on symbols. */
1821 tmp_command = (char *) alloca (point + 1);
1822 p = tmp_command;
1823
1824 strncpy (tmp_command, line_buffer, point);
1825 tmp_command[point] = '\0';
1826 /* Since text always contains some number of characters leading up
c5aa993b
JM
1827 to point, we can find the equivalent position in tmp_command
1828 by subtracting that many characters from the end of tmp_command. */
9e0b60a8
JM
1829 word = tmp_command + point - strlen (text);
1830
1831 if (point == 0)
1832 {
1833 /* An empty line we want to consider ambiguous; that is, it
1834 could be any command. */
1835 c = (struct cmd_list_element *) -1;
1836 result_list = 0;
1837 }
1838 else
1839 {
1840 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
1841 }
1842
1843 /* Move p up to the next interesting thing. */
1844 while (*p == ' ' || *p == '\t')
1845 {
1846 p++;
1847 }
1848
1849 if (!c)
1850 {
1851 /* It is an unrecognized command. So there are no
1852 possible completions. */
1853 list = NULL;
1854 }
1855 else if (c == (struct cmd_list_element *) -1)
1856 {
1857 char *q;
1858
1859 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1860 doesn't advance over that thing itself. Do so now. */
1861 q = p;
1862 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
1863 ++q;
1864 if (q != tmp_command + point)
1865 {
1866 /* There is something beyond the ambiguous
c5aa993b
JM
1867 command, so there are no possible completions. For
1868 example, "info t " or "info t foo" does not complete
1869 to anything, because "info t" can be "info target" or
1870 "info terminal". */
9e0b60a8
JM
1871 list = NULL;
1872 }
1873 else
1874 {
1875 /* We're trying to complete on the command which was ambiguous.
c5aa993b 1876 This we can deal with. */
9e0b60a8
JM
1877 if (result_list)
1878 {
1879 list = complete_on_cmdlist (*result_list->prefixlist, p,
1880 word);
1881 }
1882 else
1883 {
1884 list = complete_on_cmdlist (cmdlist, p, word);
1885 }
1886 /* Insure that readline does the right thing with respect to
c5aa993b 1887 inserting quotes. */
9e0b60a8
JM
1888 rl_completer_word_break_characters =
1889 gdb_completer_command_word_break_characters;
1890 }
1891 }
1892 else
1893 {
1894 /* We've recognized a full command. */
1895
1896 if (p == tmp_command + point)
1897 {
1898 /* There is no non-whitespace in the line beyond the command. */
1899
1900 if (p[-1] == ' ' || p[-1] == '\t')
1901 {
1902 /* The command is followed by whitespace; we need to complete
1903 on whatever comes after command. */
1904 if (c->prefixlist)
1905 {
1906 /* It is a prefix command; what comes after it is
c5aa993b 1907 a subcommand (e.g. "info "). */
9e0b60a8
JM
1908 list = complete_on_cmdlist (*c->prefixlist, p, word);
1909
1910 /* Insure that readline does the right thing
c5aa993b 1911 with respect to inserting quotes. */
9e0b60a8
JM
1912 rl_completer_word_break_characters =
1913 gdb_completer_command_word_break_characters;
1914 }
1915 else if (c->enums)
1916 {
1917 list = complete_on_enum (c->enums, p, word);
1918 rl_completer_word_break_characters =
1919 gdb_completer_command_word_break_characters;
1920 }
1921 else
1922 {
1923 /* It is a normal command; what comes after it is
c5aa993b 1924 completed by the command's completer function. */
9e0b60a8
JM
1925 list = (*c->completer) (p, word);
1926 }
1927 }
1928 else
1929 {
1930 /* The command is not followed by whitespace; we need to
1931 complete on the command itself. e.g. "p" which is a
1932 command itself but also can complete to "print", "ptype"
1933 etc. */
1934 char *q;
1935
1936 /* Find the command we are completing on. */
1937 q = p;
1938 while (q > tmp_command)
1939 {
1940 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
1941 --q;
1942 else
1943 break;
1944 }
1945
1946 list = complete_on_cmdlist (result_list, q, word);
1947
1948 /* Insure that readline does the right thing
1949 with respect to inserting quotes. */
1950 rl_completer_word_break_characters =
1951 gdb_completer_command_word_break_characters;
1952 }
1953 }
1954 else
1955 {
1956 /* There is non-whitespace beyond the command. */
1957
1958 if (c->prefixlist && !c->allow_unknown)
1959 {
1960 /* It is an unrecognized subcommand of a prefix command,
1961 e.g. "info adsfkdj". */
1962 list = NULL;
1963 }
1964 else if (c->enums)
1965 {
1966 list = complete_on_enum (c->enums, p, word);
1967 }
1968 else
1969 {
1970 /* It is a normal command. */
1971 list = (*c->completer) (p, word);
1972 }
1973 }
1974 }
1975 }
1976
1977 /* If we found a list of potential completions during initialization then
1978 dole them out one at a time. The vector of completions is NULL
1979 terminated, so after returning the last one, return NULL (and continue
1980 to do so) each time we are called after that, until a new list is
1981 available. */
1982
1983 if (list)
1984 {
1985 output = list[index];
1986 if (output)
1987 {
1988 index++;
1989 }
1990 }
1991
1992#if 0
1993 /* Can't do this because readline hasn't yet checked the word breaks
1994 for figuring out whether to insert a quote. */
1995 if (output == NULL)
1996 /* Make sure the word break characters are set back to normal for the
1997 next time that readline tries to complete something. */
1998 rl_completer_word_break_characters =
1999 gdb_completer_word_break_characters;
2000#endif
2001
2002 return (output);
2003}
2004
2005/* Line completion interface function for readline. */
2006
2007static char *
2008readline_line_completion_function (text, matches)
2009 char *text;
2010 int matches;
2011{
2012 return line_completion_function (text, matches, rl_line_buffer, rl_point);
2013}
2014
2015/* Skip over a possibly quoted word (as defined by the quote characters
2016 and word break characters the completer uses). Returns pointer to the
2017 location after the "word". */
2018
2019char *
2020skip_quoted (str)
2021 char *str;
2022{
2023 char quote_char = '\0';
2024 char *scan;
2025
2026 for (scan = str; *scan != '\0'; scan++)
2027 {
2028 if (quote_char != '\0')
2029 {
2030 /* Ignore everything until the matching close quote char */
2031 if (*scan == quote_char)
2032 {
2033 /* Found matching close quote. */
2034 scan++;
2035 break;
2036 }
2037 }
2038 else if (strchr (gdb_completer_quote_characters, *scan))
2039 {
2040 /* Found start of a quoted string. */
2041 quote_char = *scan;
2042 }
2043 else if (strchr (gdb_completer_word_break_characters, *scan))
2044 {
2045 break;
2046 }
2047 }
2048 return (scan);
2049}
9e0b60a8 2050\f
c5aa993b 2051
9e0b60a8
JM
2052#ifdef STOP_SIGNAL
2053static void
2054stop_sig (signo)
c5aa993b 2055 int signo;
9e0b60a8
JM
2056{
2057#if STOP_SIGNAL == SIGTSTP
2058 signal (SIGTSTP, SIG_DFL);
2059 sigsetmask (0);
2060 kill (getpid (), SIGTSTP);
2061 signal (SIGTSTP, stop_sig);
2062#else
2063 signal (STOP_SIGNAL, stop_sig);
2064#endif
2065 printf_unfiltered ("%s", get_prompt ());
2066 gdb_flush (gdb_stdout);
2067
2068 /* Forget about any previous command -- null line now will do nothing. */
2069 dont_repeat ();
2070}
2071#endif /* STOP_SIGNAL */
2072
2073/* Initialize signal handlers. */
2074static void
2075do_nothing (signo)
c5aa993b 2076 int signo;
9e0b60a8
JM
2077{
2078 /* Under System V the default disposition of a signal is reinstated after
2079 the signal is caught and delivered to an application process. On such
2080 systems one must restore the replacement signal handler if one wishes
2081 to continue handling the signal in one's program. On BSD systems this
2082 is not needed but it is harmless, and it simplifies the code to just do
2083 it unconditionally. */
2084 signal (signo, do_nothing);
2085}
2086
2087static void
2088init_signals ()
2089{
2090 signal (SIGINT, request_quit);
2091
2092 /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
2093 to the inferior and breakpoints will be ignored. */
2094#ifdef SIGTRAP
2095 signal (SIGTRAP, SIG_DFL);
2096#endif
2097
2098 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
2099 passed to the inferior, which we don't want. It would be
2100 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
2101 on BSD4.3 systems using vfork, that can affect the
2102 GDB process as well as the inferior (the signal handling tables
2103 might be in memory, shared between the two). Since we establish
2104 a handler for SIGQUIT, when we call exec it will set the signal
2105 to SIG_DFL for us. */
2106 signal (SIGQUIT, do_nothing);
2107#ifdef SIGHUP
2108 if (signal (SIGHUP, do_nothing) != SIG_IGN)
2109 signal (SIGHUP, disconnect);
2110#endif
2111 signal (SIGFPE, float_handler);
2112
2113#if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
2114 signal (SIGWINCH, SIGWINCH_HANDLER);
2115#endif
2116}
2117\f
2118/* Read one line from the command input stream `instream'
2119 into the local static buffer `linebuffer' (whose current length
2120 is `linelength').
2121 The buffer is made bigger as necessary.
2122 Returns the address of the start of the line.
2123
2124 NULL is returned for end of file.
2125
2126 *If* the instream == stdin & stdin is a terminal, the line read
2127 is copied into the file line saver (global var char *line,
2128 length linesize) so that it can be duplicated.
2129
2130 This routine either uses fancy command line editing or
2131 simple input as the user has requested. */
2132
2133char *
2134command_line_input (prompt_arg, repeat, annotation_suffix)
2135 char *prompt_arg;
2136 int repeat;
2137 char *annotation_suffix;
2138{
2139 static char *linebuffer = 0;
2140 static unsigned linelength = 0;
2141 register char *p;
2142 char *p1;
2143 char *rl;
2144 char *local_prompt = prompt_arg;
2145 char *nline;
2146 char got_eof = 0;
2147
2148 /* The annotation suffix must be non-NULL. */
2149 if (annotation_suffix == NULL)
2150 annotation_suffix = "";
2151
2152 if (annotation_level > 1 && instream == stdin)
2153 {
2154 local_prompt = alloca ((prompt_arg == NULL ? 0 : strlen (prompt_arg))
2155 + strlen (annotation_suffix) + 40);
2156 if (prompt_arg == NULL)
2157 local_prompt[0] = '\0';
2158 else
2159 strcpy (local_prompt, prompt_arg);
2160 strcat (local_prompt, "\n\032\032");
2161 strcat (local_prompt, annotation_suffix);
2162 strcat (local_prompt, "\n");
2163 }
2164
2165 if (linebuffer == 0)
2166 {
2167 linelength = 80;
2168 linebuffer = (char *) xmalloc (linelength);
2169 }
2170
2171 p = linebuffer;
2172
2173 /* Control-C quits instantly if typed while in this loop
2174 since it should not wait until the user types a newline. */
2175 immediate_quit++;
2176#ifdef STOP_SIGNAL
2177 if (job_control)
2178 {
6426a772 2179 if (event_loop_p)
9e0b60a8
JM
2180 signal (STOP_SIGNAL, handle_stop_sig);
2181 else
2182 signal (STOP_SIGNAL, stop_sig);
2183 }
2184#endif
2185
2186 while (1)
2187 {
2188 /* Make sure that all output has been output. Some machines may let
c5aa993b 2189 you get away with leaving out some of the gdb_flush, but not all. */
9e0b60a8
JM
2190 wrap_here ("");
2191 gdb_flush (gdb_stdout);
2192 gdb_flush (gdb_stderr);
2193
2194 if (source_file_name != NULL)
2195 {
2196 ++source_line_number;
2197 sprintf (source_error,
2198 "%s%s:%d: Error in sourced command file:\n",
2199 source_pre_error,
2200 source_file_name,
2201 source_line_number);
2202 error_pre_print = source_error;
2203 }
2204
2205 if (annotation_level > 1 && instream == stdin)
2206 {
2207 printf_unfiltered ("\n\032\032pre-");
2208 printf_unfiltered (annotation_suffix);
2209 printf_unfiltered ("\n");
2210 }
2211
2212 /* Don't use fancy stuff if not talking to stdin. */
2213 if (readline_hook && instream == NULL)
2214 {
2215 rl = (*readline_hook) (local_prompt);
2216 }
2217 else if (command_editing_p && instream == stdin && ISATTY (instream))
2218 {
2219 rl = readline (local_prompt);
2220 }
2221 else
2222 {
2223 rl = gdb_readline (local_prompt);
2224 }
2225
2226 if (annotation_level > 1 && instream == stdin)
2227 {
2228 printf_unfiltered ("\n\032\032post-");
2229 printf_unfiltered (annotation_suffix);
2230 printf_unfiltered ("\n");
2231 }
2232
2233 if (!rl || rl == (char *) EOF)
2234 {
2235 got_eof = 1;
2236 break;
2237 }
c5aa993b 2238 if (strlen (rl) + 1 + (p - linebuffer) > linelength)
9e0b60a8 2239 {
c5aa993b 2240 linelength = strlen (rl) + 1 + (p - linebuffer);
9e0b60a8
JM
2241 nline = (char *) xrealloc (linebuffer, linelength);
2242 p += nline - linebuffer;
2243 linebuffer = nline;
2244 }
2245 p1 = rl;
2246 /* Copy line. Don't copy null at end. (Leaves line alone
2247 if this was just a newline) */
2248 while (*p1)
2249 *p++ = *p1++;
2250
c5aa993b 2251 free (rl); /* Allocated in readline. */
9e0b60a8
JM
2252
2253 if (p == linebuffer || *(p - 1) != '\\')
2254 break;
2255
2256 p--; /* Put on top of '\'. */
2257 local_prompt = (char *) 0;
c5aa993b 2258 }
9e0b60a8
JM
2259
2260#ifdef STOP_SIGNAL
2261 if (job_control)
2262 signal (STOP_SIGNAL, SIG_DFL);
2263#endif
2264 immediate_quit--;
2265
2266 if (got_eof)
2267 return NULL;
2268
2269#define SERVER_COMMAND_LENGTH 7
2270 server_command =
2271 (p - linebuffer > SERVER_COMMAND_LENGTH)
c5aa993b 2272 && STREQN (linebuffer, "server ", SERVER_COMMAND_LENGTH);
9e0b60a8
JM
2273 if (server_command)
2274 {
2275 /* Note that we don't set `line'. Between this and the check in
c5aa993b
JM
2276 dont_repeat, this insures that repeating will still do the
2277 right thing. */
9e0b60a8
JM
2278 *p = '\0';
2279 return linebuffer + SERVER_COMMAND_LENGTH;
2280 }
2281
2282 /* Do history expansion if that is wished. */
2283 if (history_expansion_p && instream == stdin
2284 && ISATTY (instream))
2285 {
2286 char *history_value;
2287 int expanded;
2288
2289 *p = '\0'; /* Insert null now. */
2290 expanded = history_expand (linebuffer, &history_value);
2291 if (expanded)
2292 {
2293 /* Print the changes. */
2294 printf_unfiltered ("%s\n", history_value);
2295
2296 /* If there was an error, call this function again. */
2297 if (expanded < 0)
2298 {
2299 free (history_value);
2300 return command_line_input (prompt_arg, repeat, annotation_suffix);
2301 }
2302 if (strlen (history_value) > linelength)
2303 {
2304 linelength = strlen (history_value) + 1;
2305 linebuffer = (char *) xrealloc (linebuffer, linelength);
2306 }
2307 strcpy (linebuffer, history_value);
c5aa993b 2308 p = linebuffer + strlen (linebuffer);
9e0b60a8
JM
2309 free (history_value);
2310 }
2311 }
2312
2313 /* If we just got an empty line, and that is supposed
2314 to repeat the previous command, return the value in the
2315 global buffer. */
2316 if (repeat && p == linebuffer)
2317 return line;
c5aa993b 2318 for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++);
9e0b60a8
JM
2319 if (repeat && !*p1)
2320 return line;
2321
2322 *p = 0;
2323
2324 /* Add line to history if appropriate. */
2325 if (instream == stdin
2326 && ISATTY (stdin) && *linebuffer)
2327 add_history (linebuffer);
2328
2329 /* Note: lines consisting solely of comments are added to the command
2330 history. This is useful when you type a command, and then
2331 realize you don't want to execute it quite yet. You can comment
2332 out the command and then later fetch it from the value history
2333 and remove the '#'. The kill ring is probably better, but some
2334 people are in the habit of commenting things out. */
2335 if (*p1 == '#')
c5aa993b 2336 *p1 = '\0'; /* Found a comment. */
9e0b60a8
JM
2337
2338 /* Save into global buffer if appropriate. */
2339 if (repeat)
2340 {
2341 if (linelength > linesize)
2342 {
2343 line = xrealloc (line, linelength);
2344 linesize = linelength;
2345 }
2346 strcpy (line, linebuffer);
2347 return line;
2348 }
2349
2350 return linebuffer;
2351}
2352\f
2353
2354/* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2355 code bodies. This is typically used when we encounter an "else"
2356 clause for an "if" command. */
2357
2358static void
2359realloc_body_list (command, new_length)
2360 struct command_line *command;
2361 int new_length;
2362{
2363 int n;
2364 struct command_line **body_list;
2365
2366 n = command->body_count;
2367
2368 /* Nothing to do? */
2369 if (new_length <= n)
2370 return;
2371
2372 body_list = (struct command_line **)
2373 xmalloc (sizeof (struct command_line *) * new_length);
2374
2375 memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
2376
2377 free (command->body_list);
2378 command->body_list = body_list;
2379 command->body_count = new_length;
2380}
2381
2382/* Read one line from the input stream. If the command is an "else" or
2383 "end", return such an indication to the caller. */
2384
2385static enum misc_command_type
2386read_next_line (command)
2387 struct command_line **command;
2388{
2389 char *p, *p1, *prompt_ptr, control_prompt[256];
2390 int i = 0;
2391
2392 if (control_level >= 254)
2393 error ("Control nesting too deep!\n");
2394
2395 /* Set a prompt based on the nesting of the control commands. */
2396 if (instream == stdin || (instream == 0 && readline_hook != NULL))
2397 {
2398 for (i = 0; i < control_level; i++)
2399 control_prompt[i] = ' ';
2400 control_prompt[i] = '>';
c5aa993b
JM
2401 control_prompt[i + 1] = '\0';
2402 prompt_ptr = (char *) &control_prompt[0];
9e0b60a8
JM
2403 }
2404 else
2405 prompt_ptr = NULL;
2406
2407 p = command_line_input (prompt_ptr, instream == stdin, "commands");
2408
2409 /* Not sure what to do here. */
2410 if (p == NULL)
2411 return end_command;
2412
2413 /* Strip leading and trailing whitespace. */
2414 while (*p == ' ' || *p == '\t')
2415 p++;
2416
2417 p1 = p + strlen (p);
2418 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
2419 p1--;
2420
2421 /* Blanks and comments don't really do anything, but we need to
2422 distinguish them from else, end and other commands which can be
2423 executed. */
2424 if (p1 == p || p[0] == '#')
2425 return nop_command;
c5aa993b 2426
9e0b60a8
JM
2427 /* Is this the end of a simple, while, or if control structure? */
2428 if (p1 - p == 3 && !strncmp (p, "end", 3))
2429 return end_command;
2430
2431 /* Is the else clause of an if control structure? */
2432 if (p1 - p == 4 && !strncmp (p, "else", 4))
2433 return else_command;
2434
2435 /* Check for while, if, break, continue, etc and build a new command
2436 line structure for them. */
2437 if (p1 - p > 5 && !strncmp (p, "while", 5))
2438 *command = build_command_line (while_control, p + 6);
2439 else if (p1 - p > 2 && !strncmp (p, "if", 2))
2440 *command = build_command_line (if_control, p + 3);
2441 else if (p1 - p == 10 && !strncmp (p, "loop_break", 10))
2442 {
2443 *command = (struct command_line *)
2444 xmalloc (sizeof (struct command_line));
2445 (*command)->next = NULL;
2446 (*command)->line = NULL;
2447 (*command)->control_type = break_control;
2448 (*command)->body_count = 0;
2449 (*command)->body_list = NULL;
2450 }
2451 else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13))
2452 {
2453 *command = (struct command_line *)
2454 xmalloc (sizeof (struct command_line));
2455 (*command)->next = NULL;
2456 (*command)->line = NULL;
2457 (*command)->control_type = continue_control;
2458 (*command)->body_count = 0;
2459 (*command)->body_list = NULL;
2460 }
2461 else
2462 {
2463 /* A normal command. */
2464 *command = (struct command_line *)
2465 xmalloc (sizeof (struct command_line));
2466 (*command)->next = NULL;
2467 (*command)->line = savestring (p, p1 - p);
2468 (*command)->control_type = simple_control;
2469 (*command)->body_count = 0;
2470 (*command)->body_list = NULL;
c5aa993b 2471 }
9e0b60a8
JM
2472
2473 /* Nothing special. */
2474 return ok_command;
2475}
2476
2477/* Recursively read in the control structures and create a command_line
2478 structure from them.
2479
2480 The parent_control parameter is the control structure in which the
2481 following commands are nested. */
2482
2483static enum command_control_type
2484recurse_read_control_structure (current_cmd)
2485 struct command_line *current_cmd;
2486{
2487 int current_body, i;
2488 enum misc_command_type val;
2489 enum command_control_type ret;
2490 struct command_line **body_ptr, *child_tail, *next;
2491
2492 child_tail = NULL;
2493 current_body = 1;
2494
2495 /* Sanity checks. */
2496 if (current_cmd->control_type == simple_control)
2497 {
2498 error ("Recursed on a simple control type\n");
2499 return invalid_control;
2500 }
2501
2502 if (current_body > current_cmd->body_count)
2503 {
2504 error ("Allocated body is smaller than this command type needs\n");
2505 return invalid_control;
2506 }
2507
2508 /* Read lines from the input stream and build control structures. */
2509 while (1)
2510 {
2511 dont_repeat ();
2512
2513 next = NULL;
2514 val = read_next_line (&next);
2515
2516 /* Just skip blanks and comments. */
2517 if (val == nop_command)
2518 continue;
2519
2520 if (val == end_command)
2521 {
2522 if (current_cmd->control_type == while_control
2523 || current_cmd->control_type == if_control)
2524 {
2525 /* Success reading an entire control structure. */
2526 ret = simple_control;
2527 break;
2528 }
2529 else
2530 {
2531 ret = invalid_control;
2532 break;
2533 }
2534 }
c5aa993b 2535
9e0b60a8
JM
2536 /* Not the end of a control structure. */
2537 if (val == else_command)
2538 {
2539 if (current_cmd->control_type == if_control
2540 && current_body == 1)
2541 {
2542 realloc_body_list (current_cmd, 2);
2543 current_body = 2;
2544 child_tail = NULL;
2545 continue;
2546 }
2547 else
2548 {
2549 ret = invalid_control;
2550 break;
2551 }
2552 }
2553
2554 if (child_tail)
2555 {
2556 child_tail->next = next;
2557 }
2558 else
2559 {
2560 body_ptr = current_cmd->body_list;
2561 for (i = 1; i < current_body; i++)
2562 body_ptr++;
2563
2564 *body_ptr = next;
2565
2566 }
2567
2568 child_tail = next;
2569
2570 /* If the latest line is another control structure, then recurse
c5aa993b 2571 on it. */
9e0b60a8
JM
2572 if (next->control_type == while_control
2573 || next->control_type == if_control)
2574 {
2575 control_level++;
2576 ret = recurse_read_control_structure (next);
2577 control_level--;
2578
2579 if (ret != simple_control)
2580 break;
2581 }
2582 }
2583
2584 dont_repeat ();
2585
2586 return ret;
2587}
2588
2589/* Read lines from the input stream and accumulate them in a chain of
2590 struct command_line's, which is then returned. For input from a
2591 terminal, the special command "end" is used to mark the end of the
2592 input, and is not included in the returned chain of commands. */
2593
2594#define END_MESSAGE "End with a line saying just \"end\"."
2595
2596struct command_line *
2597read_command_lines (prompt_arg, from_tty)
2598 char *prompt_arg;
2599 int from_tty;
2600{
2601 struct command_line *head, *tail, *next;
2602 struct cleanup *old_chain;
2603 enum command_control_type ret;
2604 enum misc_command_type val;
2605
c5aa993b 2606 control_level = 0;
9e0b60a8
JM
2607 if (readline_begin_hook)
2608 {
2609 /* Note - intentional to merge messages with no newline */
2610 (*readline_begin_hook) ("%s %s\n", prompt_arg, END_MESSAGE);
2611 }
2612 else if (from_tty && input_from_terminal_p ())
2613 {
2614 printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
2615 gdb_flush (gdb_stdout);
2616 }
2617
2618 head = tail = NULL;
2619 old_chain = NULL;
2620
2621 while (1)
2622 {
2623 val = read_next_line (&next);
2624
2625 /* Ignore blank lines or comments. */
2626 if (val == nop_command)
2627 continue;
2628
2629 if (val == end_command)
2630 {
2631 ret = simple_control;
2632 break;
2633 }
2634
2635 if (val != ok_command)
2636 {
2637 ret = invalid_control;
2638 break;
2639 }
2640
2641 if (next->control_type == while_control
2642 || next->control_type == if_control)
2643 {
2644 control_level++;
2645 ret = recurse_read_control_structure (next);
2646 control_level--;
2647
2648 if (ret == invalid_control)
2649 break;
2650 }
c5aa993b 2651
9e0b60a8
JM
2652 if (tail)
2653 {
2654 tail->next = next;
2655 }
2656 else
2657 {
2658 head = next;
c5aa993b
JM
2659 old_chain = make_cleanup ((make_cleanup_func) free_command_lines,
2660 &head);
9e0b60a8
JM
2661 }
2662 tail = next;
2663 }
2664
2665 dont_repeat ();
2666
2667 if (head)
2668 {
2669 if (ret != invalid_control)
2670 {
2671 discard_cleanups (old_chain);
2672 }
2673 else
2674 do_cleanups (old_chain);
2675 }
2676
2677 if (readline_end_hook)
2678 {
2679 (*readline_end_hook) ();
2680 }
2681 return (head);
2682}
2683
2684/* Free a chain of struct command_line's. */
2685
2686void
2687free_command_lines (lptr)
c5aa993b 2688 struct command_line **lptr;
9e0b60a8
JM
2689{
2690 register struct command_line *l = *lptr;
2691 register struct command_line *next;
2692 struct command_line **blist;
2693 int i;
2694
2695 while (l)
2696 {
2697 if (l->body_count > 0)
2698 {
2699 blist = l->body_list;
2700 for (i = 0; i < l->body_count; i++, blist++)
2701 free_command_lines (blist);
2702 }
2703 next = l->next;
2704 free (l->line);
c5aa993b 2705 free ((PTR) l);
9e0b60a8
JM
2706 l = next;
2707 }
2708}
2709\f
2710/* Add an element to the list of info subcommands. */
2711
2712void
2713add_info (name, fun, doc)
2714 char *name;
2715 void (*fun) PARAMS ((char *, int));
2716 char *doc;
2717{
2718 add_cmd (name, no_class, fun, doc, &infolist);
2719}
2720
2721/* Add an alias to the list of info subcommands. */
2722
2723void
2724add_info_alias (name, oldname, abbrev_flag)
2725 char *name;
2726 char *oldname;
2727 int abbrev_flag;
2728{
2729 add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
2730}
2731
2732/* The "info" command is defined as a prefix, with allow_unknown = 0.
2733 Therefore, its own definition is called only for "info" with no args. */
2734
2735/* ARGSUSED */
2736static void
2737info_command (arg, from_tty)
2738 char *arg;
2739 int from_tty;
2740{
2741 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2742 help_list (infolist, "info ", -1, gdb_stdout);
2743}
2744
2745/* The "complete" command is used by Emacs to implement completion. */
2746
2747/* ARGSUSED */
2748static void
2749complete_command (arg, from_tty)
2750 char *arg;
2751 int from_tty;
2752{
2753 int i;
2754 int argpoint;
2755 char *completion;
2756
2757 dont_repeat ();
2758
2759 if (arg == NULL)
2760 arg = "";
2761 argpoint = strlen (arg);
2762
2763 for (completion = line_completion_function (arg, i = 0, arg, argpoint);
2764 completion;
2765 completion = line_completion_function (arg, ++i, arg, argpoint))
2766 {
2767 printf_unfiltered ("%s\n", completion);
2768 free (completion);
2769 }
2770}
2771
2772/* The "show" command with no arguments shows all the settings. */
2773
2774/* ARGSUSED */
2775static void
2776show_command (arg, from_tty)
2777 char *arg;
2778 int from_tty;
2779{
2780 cmd_show_list (showlist, from_tty, "");
2781}
2782\f
2783/* Add an element to the list of commands. */
2784
2785void
2786add_com (name, class, fun, doc)
2787 char *name;
2788 enum command_class class;
2789 void (*fun) PARAMS ((char *, int));
2790 char *doc;
2791{
2792 add_cmd (name, class, fun, doc, &cmdlist);
2793}
2794
2795/* Add an alias or abbreviation command to the list of commands. */
2796
2797void
2798add_com_alias (name, oldname, class, abbrev_flag)
2799 char *name;
2800 char *oldname;
2801 enum command_class class;
2802 int abbrev_flag;
2803{
2804 add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
2805}
2806
2807void
2808error_no_arg (why)
2809 char *why;
2810{
2811 error ("Argument required (%s).", why);
2812}
2813
2814/* ARGSUSED */
2815static void
2816help_command (command, from_tty)
2817 char *command;
c5aa993b 2818 int from_tty; /* Ignored */
9e0b60a8
JM
2819{
2820 help_cmd (command, gdb_stdout);
2821}
2822\f
2823static void
2824validate_comname (comname)
2825 char *comname;
2826{
2827 register char *p;
2828
2829 if (comname == 0)
2830 error_no_arg ("name of command to define");
2831
2832 p = comname;
2833 while (*p)
2834 {
c5aa993b 2835 if (!isalnum (*p) && *p != '-' && *p != '_')
9e0b60a8
JM
2836 error ("Junk in argument list: \"%s\"", p);
2837 p++;
2838 }
2839}
2840
2841/* This is just a placeholder in the command data structures. */
2842static void
2843user_defined_command (ignore, from_tty)
2844 char *ignore;
2845 int from_tty;
2846{
2847}
2848
2849static void
2850define_command (comname, from_tty)
2851 char *comname;
2852 int from_tty;
2853{
2854 register struct command_line *cmds;
2855 register struct cmd_list_element *c, *newc, *hookc = 0;
2856 char *tem = comname;
2857 char tmpbuf[128];
2858#define HOOK_STRING "hook-"
2859#define HOOK_LEN 5
2860
2861 validate_comname (comname);
2862
2863 /* Look it up, and verify that we got an exact match. */
2864 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
2865 if (c && !STREQ (comname, c->name))
2866 c = 0;
2867
2868 if (c)
2869 {
2870 if (c->class == class_user || c->class == class_alias)
2871 tem = "Redefine command \"%s\"? ";
2872 else
2873 tem = "Really redefine built-in command \"%s\"? ";
2874 if (!query (tem, c->name))
2875 error ("Command \"%s\" not redefined.", c->name);
2876 }
2877
2878 /* If this new command is a hook, then mark the command which it
2879 is hooking. Note that we allow hooking `help' commands, so that
2880 we can hook the `stop' pseudo-command. */
2881
2882 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
2883 {
2884 /* Look up cmd it hooks, and verify that we got an exact match. */
c5aa993b 2885 tem = comname + HOOK_LEN;
9e0b60a8 2886 hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
c5aa993b 2887 if (hookc && !STREQ (comname + HOOK_LEN, hookc->name))
9e0b60a8
JM
2888 hookc = 0;
2889 if (!hookc)
2890 {
2891 warning ("Your new `%s' command does not hook any existing command.",
2892 comname);
2893 if (!query ("Proceed? "))
2894 error ("Not confirmed.");
2895 }
2896 }
2897
2898 comname = savestring (comname, strlen (comname));
2899
2900 /* If the rest of the commands will be case insensitive, this one
2901 should behave in the same manner. */
2902 for (tem = comname; *tem; tem++)
c5aa993b
JM
2903 if (isupper (*tem))
2904 *tem = tolower (*tem);
9e0b60a8
JM
2905
2906 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comname);
2907 cmds = read_command_lines (tmpbuf, from_tty);
2908
2909 if (c && c->class == class_user)
2910 free_command_lines (&c->user_commands);
2911
2912 newc = add_cmd (comname, class_user, user_defined_command,
c5aa993b
JM
2913 (c && c->class == class_user)
2914 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
9e0b60a8
JM
2915 newc->user_commands = cmds;
2916
2917 /* If this new command is a hook, then mark both commands as being
2918 tied. */
2919 if (hookc)
2920 {
2921 hookc->hook = newc; /* Target gets hooked. */
2922 newc->hookee = hookc; /* We are marked as hooking target cmd. */
2923 }
2924}
2925
2926static void
2927document_command (comname, from_tty)
2928 char *comname;
2929 int from_tty;
2930{
2931 struct command_line *doclines;
2932 register struct cmd_list_element *c;
2933 char *tem = comname;
2934 char tmpbuf[128];
2935
2936 validate_comname (comname);
2937
2938 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
2939
2940 if (c->class != class_user)
2941 error ("Command \"%s\" is built-in.", comname);
2942
2943 sprintf (tmpbuf, "Type documentation for \"%s\".", comname);
2944 doclines = read_command_lines (tmpbuf, from_tty);
2945
c5aa993b
JM
2946 if (c->doc)
2947 free (c->doc);
9e0b60a8
JM
2948
2949 {
2950 register struct command_line *cl1;
2951 register int len = 0;
2952
2953 for (cl1 = doclines; cl1; cl1 = cl1->next)
2954 len += strlen (cl1->line) + 1;
2955
2956 c->doc = (char *) xmalloc (len + 1);
2957 *c->doc = 0;
2958
2959 for (cl1 = doclines; cl1; cl1 = cl1->next)
2960 {
2961 strcat (c->doc, cl1->line);
2962 if (cl1->next)
2963 strcat (c->doc, "\n");
2964 }
2965 }
2966
2967 free_command_lines (&doclines);
2968}
2969\f
2970/* Print the GDB banner. */
2971void
2972print_gdb_version (stream)
c5aa993b 2973 GDB_FILE *stream;
9e0b60a8
JM
2974{
2975 /* From GNU coding standards, first line is meant to be easy for a
2976 program to parse, and is just canonical program name and version
2977 number, which starts after last space. */
2978
2979 fprintf_filtered (stream, "GNU gdb %s\n", version);
2980
2981 /* Second line is a copyright notice. */
2982
2983 fprintf_filtered (stream, "Copyright 1998 Free Software Foundation, Inc.\n");
2984
2985 /* Following the copyright is a brief statement that the program is
2986 free software, that users are free to copy and change it on
2987 certain conditions, that it is covered by the GNU GPL, and that
2988 there is no warranty. */
2989
2990 fprintf_filtered (stream, "\
2991GDB is free software, covered by the GNU General Public License, and you are\n\
2992welcome to change it and/or distribute copies of it under certain conditions.\n\
2993Type \"show copying\" to see the conditions.\n\
2994There is absolutely no warranty for GDB. Type \"show warranty\" for details.\n");
2995
2996 /* After the required info we print the configuration information. */
2997
2998 fprintf_filtered (stream, "This GDB was configured as \"");
2999 if (!STREQ (host_name, target_name))
3000 {
3001 fprintf_filtered (stream, "--host=%s --target=%s", host_name, target_name);
3002 }
3003 else
3004 {
3005 fprintf_filtered (stream, "%s", host_name);
3006 }
3007 fprintf_filtered (stream, "\".");
3008}
3009
3010/* ARGSUSED */
3011static void
3012show_version (args, from_tty)
3013 char *args;
3014 int from_tty;
3015{
3016 immediate_quit++;
3017 print_gdb_version (gdb_stdout);
3018 printf_filtered ("\n");
3019 immediate_quit--;
3020}
3021\f
3022/* get_prompt: access method for the GDB prompt string. */
3023
3024#define MAX_PROMPT_SIZE 256
3025
3026/*
3027 * int get_prompt_1 (char * buf);
3028 *
3029 * Work-horse for get_prompt (called via catch_errors).
3030 * Argument is buffer to hold the formatted prompt.
3031 *
3032 * Returns: 1 for success (use formatted prompt)
3033 * 0 for failure (use gdb_prompt_string).
c5aa993b 3034 */
9e0b60a8
JM
3035
3036static int gdb_prompt_escape;
3037
3038static int
3039get_prompt_1 (formatted_prompt)
3040 char *formatted_prompt;
3041{
3042 char *local_prompt;
3043
6426a772 3044 if (event_loop_p)
9e0b60a8
JM
3045 local_prompt = PROMPT (0);
3046 else
3047 local_prompt = gdb_prompt_string;
3048
3049
3050 if (gdb_prompt_escape == 0)
3051 {
c5aa993b 3052 return 0; /* do no formatting */
9e0b60a8 3053 }
c5aa993b
JM
3054 else
3055 /* formatted prompt */
9e0b60a8 3056 {
c5aa993b 3057 char fmt[40], *promptp, *outp, *tmp;
9e0b60a8 3058 value_ptr arg_val;
c5aa993b
JM
3059 DOUBLEST doubleval;
3060 LONGEST longval;
9e0b60a8
JM
3061 CORE_ADDR addrval;
3062
3063 int i, len;
3064 struct type *arg_type, *elt_type;
3065
3066 promptp = local_prompt;
c5aa993b 3067 outp = formatted_prompt;
9e0b60a8
JM
3068
3069 while (*promptp != '\0')
3070 {
3071 int available = MAX_PROMPT_SIZE - (outp - formatted_prompt) - 1;
3072
3073 if (*promptp != gdb_prompt_escape)
3074 {
c5aa993b 3075 if (available >= 1) /* overflow protect */
9e0b60a8
JM
3076 *outp++ = *promptp++;
3077 }
3078 else
3079 {
3080 /* GDB prompt string contains escape char. Parse for arg.
c5aa993b
JM
3081 Two consecutive escape chars followed by arg followed by
3082 a comma means to insert the arg using a default format.
3083 Otherwise a printf format string may be included between
3084 the two escape chars. eg:
3085 %%foo, insert foo using default format
3086 %2.2f%foo, insert foo using "%2.2f" format
3087 A mismatch between the format string and the data type
3088 of "foo" is an error (which we don't know how to protect
3089 against). */
9e0b60a8
JM
3090
3091 fmt[0] = '\0'; /* assume null format string */
3092 if (promptp[1] == gdb_prompt_escape) /* double esc char */
3093 {
3094 promptp += 2; /* skip past two escape chars. */
3095 }
3096 else
3097 {
3098 /* extract format string from between two esc chars */
3099 i = 0;
c5aa993b
JM
3100 do
3101 {
3102 fmt[i++] = *promptp++; /* copy format string */
3103 }
3104 while (i < sizeof (fmt) - 1 &&
3105 *promptp != gdb_prompt_escape &&
3106 *promptp != '\0');
9e0b60a8
JM
3107
3108 if (*promptp != gdb_prompt_escape)
3109 error ("Syntax error at prompt position %d",
3110 promptp - local_prompt);
3111 else
3112 {
3113 promptp++; /* skip second escape char */
3114 fmt[i++] = '\0'; /* terminate the format string */
3115 }
3116 }
3117
3118 arg_val = parse_to_comma_and_eval (&promptp);
3119 if (*promptp == ',')
c5aa993b 3120 promptp++; /* skip past the comma */
9e0b60a8 3121 arg_type = check_typedef (VALUE_TYPE (arg_val));
c5aa993b 3122 switch (TYPE_CODE (arg_type))
9e0b60a8
JM
3123 {
3124 case TYPE_CODE_ARRAY:
3125 elt_type = check_typedef (TYPE_TARGET_TYPE (arg_type));
c5aa993b 3126 if (TYPE_LENGTH (arg_type) > 0 &&
9e0b60a8
JM
3127 TYPE_LENGTH (elt_type) == 1 &&
3128 TYPE_CODE (elt_type) == TYPE_CODE_INT)
3129 {
3130 int len = TYPE_LENGTH (arg_type);
3131
3132 if (VALUE_LAZY (arg_val))
3133 value_fetch_lazy (arg_val);
3134 tmp = VALUE_CONTENTS (arg_val);
3135
3136 if (len > available)
c5aa993b 3137 len = available; /* overflow protect */
9e0b60a8
JM
3138
3139 /* FIXME: how to protect GDB from crashing
c5aa993b 3140 from bad user-supplied format string? */
9e0b60a8
JM
3141 if (fmt[0] != 0)
3142 sprintf (outp, fmt, tmp);
3143 else
3144 strncpy (outp, tmp, len);
3145 outp[len] = '\0';
3146 }
3147 break;
3148 case TYPE_CODE_PTR:
3149 elt_type = check_typedef (TYPE_TARGET_TYPE (arg_type));
3150 addrval = value_as_pointer (arg_val);
3151
3152 if (TYPE_LENGTH (elt_type) == 1 &&
c5aa993b 3153 TYPE_CODE (elt_type) == TYPE_CODE_INT &&
9e0b60a8
JM
3154 addrval != 0)
3155 {
3156 /* display it as a string */
3157 char *default_fmt = "%s";
3158 char *tmp;
3159 int err = 0;
3160
3161 /* Limiting the number of bytes that the following call
c5aa993b
JM
3162 will read protects us from sprintf overflow later. */
3163 i = target_read_string (addrval, /* src */
3164 &tmp, /* dest */
3165 available, /* len */
9e0b60a8
JM
3166 &err);
3167 if (err) /* read failed */
3168 error ("%s on target_read", safe_strerror (err));
3169
3170 tmp[i] = '\0'; /* force-terminate string */
3171 /* FIXME: how to protect GDB from crashing
c5aa993b
JM
3172 from bad user-supplied format string? */
3173 sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
9e0b60a8
JM
3174 tmp);
3175 free (tmp);
3176 }
3177 else
3178 {
3179 /* display it as a pointer */
3180 char *default_fmt = "0x%x";
3181
3182 /* FIXME: how to protect GDB from crashing
c5aa993b
JM
3183 from bad user-supplied format string? */
3184 if (available >= 16 /*? */ ) /* overflow protect */
9e0b60a8
JM
3185 sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
3186 (long) addrval);
3187 }
3188 break;
3189 case TYPE_CODE_FLT:
3190 {
3191 char *default_fmt = "%g";
3192
3193 doubleval = value_as_double (arg_val);
3194 /* FIXME: how to protect GDB from crashing
3195 from bad user-supplied format string? */
c5aa993b 3196 if (available >= 16 /*? */ ) /* overflow protect */
9e0b60a8
JM
3197 sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
3198 (double) doubleval);
3199 break;
3200 }
3201 case TYPE_CODE_INT:
3202 {
3203 char *default_fmt = "%d";
3204
3205 longval = value_as_long (arg_val);
3206 /* FIXME: how to protect GDB from crashing
3207 from bad user-supplied format string? */
c5aa993b 3208 if (available >= 16 /*? */ ) /* overflow protect */
9e0b60a8
JM
3209 sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
3210 (long) longval);
3211 break;
3212 }
3213 case TYPE_CODE_BOOL:
3214 {
3215 /* no default format for bool */
3216 longval = value_as_long (arg_val);
c5aa993b 3217 if (available >= 8 /*? */ ) /* overflow protect */
9e0b60a8
JM
3218 {
3219 if (longval)
3220 strcpy (outp, "<true>");
3221 else
3222 strcpy (outp, "<false>");
3223 }
3224 break;
3225 }
3226 case TYPE_CODE_ENUM:
3227 {
3228 /* no default format for enum */
3229 longval = value_as_long (arg_val);
3230 len = TYPE_NFIELDS (arg_type);
3231 /* find enum name if possible */
3232 for (i = 0; i < len; i++)
3233 if (TYPE_FIELD_BITPOS (arg_type, i) == longval)
c5aa993b 3234 break; /* match -- end loop */
9e0b60a8
JM
3235
3236 if (i < len) /* enum name found */
3237 {
3238 char *name = TYPE_FIELD_NAME (arg_type, i);
3239
3240 strncpy (outp, name, available);
3241 /* in casel available < strlen (name), */
3242 outp[available] = '\0';
3243 }
3244 else
3245 {
c5aa993b 3246 if (available >= 16 /*? */ ) /* overflow protect */
d4f3574e 3247 sprintf (outp, "%ld", (long) longval);
9e0b60a8
JM
3248 }
3249 break;
3250 }
3251 case TYPE_CODE_VOID:
3252 *outp = '\0';
3253 break; /* void type -- no output */
3254 default:
3255 error ("bad data type at prompt position %d",
3256 promptp - local_prompt);
3257 break;
3258 }
3259 outp += strlen (outp);
3260 }
3261 }
3262 *outp++ = '\0'; /* terminate prompt string */
3263 return 1;
3264 }
3265}
3266
3267char *
3268get_prompt ()
3269{
3270 static char buf[MAX_PROMPT_SIZE];
3271
c5aa993b 3272 if (catch_errors (get_prompt_1, buf, "bad formatted prompt: ",
9e0b60a8
JM
3273 RETURN_MASK_ALL))
3274 {
c5aa993b 3275 return &buf[0]; /* successful formatted prompt */
9e0b60a8
JM
3276 }
3277 else
3278 {
3279 /* Prompt could not be formatted. */
6426a772 3280 if (event_loop_p)
9e0b60a8
JM
3281 return PROMPT (0);
3282 else
3283 return gdb_prompt_string;
3284 }
3285}
3286
3287void
3288set_prompt (s)
3289 char *s;
3290{
3291/* ??rehrauer: I don't know why this fails, since it looks as though
3292 assignments to prompt are wrapped in calls to savestring...
c5aa993b
JM
3293 if (prompt != NULL)
3294 free (prompt);
3295 */
6426a772 3296 if (event_loop_p)
9e0b60a8
JM
3297 PROMPT (0) = savestring (s, strlen (s));
3298 else
3299 gdb_prompt_string = savestring (s, strlen (s));
3300}
9e0b60a8 3301\f
c5aa993b 3302
9e0b60a8
JM
3303/* If necessary, make the user confirm that we should quit. Return
3304 non-zero if we should quit, zero if we shouldn't. */
3305
3306int
3307quit_confirm ()
3308{
3309 if (inferior_pid != 0 && target_has_execution)
3310 {
3311 char *s;
3312
3313 /* This is something of a hack. But there's no reliable way to
c5aa993b
JM
3314 see if a GUI is running. The `use_windows' variable doesn't
3315 cut it. */
9e0b60a8
JM
3316 if (init_ui_hook)
3317 s = "A debugging session is active.\nDo you still want to close the debugger?";
3318 else if (attach_flag)
3319 s = "The program is running. Quit anyway (and detach it)? ";
3320 else
3321 s = "The program is running. Exit anyway? ";
3322
c5aa993b 3323 if (!query (s))
9e0b60a8
JM
3324 return 0;
3325 }
3326
3327 return 1;
3328}
3329
3330/* Quit without asking for confirmation. */
3331
3332void
3333quit_force (args, from_tty)
3334 char *args;
3335 int from_tty;
3336{
3337 int exit_code = 0;
3338
3339 /* An optional expression may be used to cause gdb to terminate with the
3340 value of that expression. */
3341 if (args)
3342 {
3343 value_ptr val = parse_and_eval (args);
3344
3345 exit_code = (int) value_as_long (val);
3346 }
3347
3348 if (inferior_pid != 0 && target_has_execution)
3349 {
3350 if (attach_flag)
3351 target_detach (args, from_tty);
3352 else
3353 target_kill ();
3354 }
3355
3356 /* UDI wants this, to kill the TIP. */
3357 target_close (1);
3358
3359 /* Save the history information if it is appropriate to do so. */
3360 if (write_history_p && history_filename)
3361 write_history (history_filename);
3362
c5aa993b 3363 do_final_cleanups (ALL_CLEANUPS); /* Do any final cleanups before exiting */
9e0b60a8
JM
3364
3365#if defined(TUI)
3366 /* tuiDo((TuiOpaqueFuncPtr)tuiCleanUp); */
3367 /* The above does not need to be inside a tuiDo(), since
3368 * it is not manipulating the curses screen, but rather,
3369 * it is tearing it down.
3370 */
3371 if (tui_version)
c5aa993b 3372 tuiCleanUp ();
9e0b60a8
JM
3373#endif
3374
3375 exit (exit_code);
3376}
3377
3378/* Handle the quit command. */
3379
3380void
3381quit_command (args, from_tty)
3382 char *args;
3383 int from_tty;
3384{
c5aa993b 3385 if (!quit_confirm ())
9e0b60a8
JM
3386 error ("Not confirmed.");
3387 quit_force (args, from_tty);
3388}
3389
3390/* Returns whether GDB is running on a terminal and whether the user
3391 desires that questions be asked of them on that terminal. */
3392
3393int
3394input_from_terminal_p ()
3395{
3396 return gdb_has_a_terminal () && (instream == stdin) & caution;
3397}
3398\f
3399/* ARGSUSED */
3400static void
3401pwd_command (args, from_tty)
3402 char *args;
3403 int from_tty;
3404{
c5aa993b
JM
3405 if (args)
3406 error ("The \"pwd\" command does not take an argument: %s", args);
9e0b60a8
JM
3407 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
3408
3409 if (!STREQ (gdb_dirbuf, current_directory))
3410 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
c5aa993b 3411 current_directory, gdb_dirbuf);
9e0b60a8
JM
3412 else
3413 printf_unfiltered ("Working directory %s.\n", current_directory);
3414}
3415
3416void
3417cd_command (dir, from_tty)
3418 char *dir;
3419 int from_tty;
3420{
3421 int len;
3422 /* Found something other than leading repetitions of "/..". */
3423 int found_real_path;
3424 char *p;
3425
3426 /* If the new directory is absolute, repeat is a no-op; if relative,
3427 repeat might be useful but is more likely to be a mistake. */
3428 dont_repeat ();
3429
3430 if (dir == 0)
3431 error_no_arg ("new working directory");
3432
3433 dir = tilde_expand (dir);
3434 make_cleanup (free, dir);
3435
3436 if (chdir (dir) < 0)
3437 perror_with_name (dir);
3438
a0b3c4fd
JM
3439#if defined(_WIN32) || defined(__MSDOS__)
3440 /* There's too much mess with DOSish names like "d:", "d:.",
3441 "d:./foo" etc. Instead of having lots of special #ifdef'ed code,
3442 simply get the canonicalized name of the current directory. */
3443 dir = getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
3444#endif
3445
9e0b60a8 3446 len = strlen (dir);
a0b3c4fd
JM
3447 if (SLASH_P (dir[len-1]))
3448 {
3449 /* Remove the trailing slash unless this is a root directory
3450 (including a drive letter on non-Unix systems). */
3451 if (!(len == 1) /* "/" */
3452#if defined(_WIN32) || defined(__MSDOS__)
3453 && !(!SLASH_P (*dir) && ROOTED_P (dir) && len <= 3) /* "d:/" */
3454#endif
3455 )
3456 len--;
3457 }
3458
3459 dir = savestring (dir, len);
c5aa993b 3460 if (ROOTED_P (dir))
9e0b60a8
JM
3461 current_directory = dir;
3462 else
3463 {
a0b3c4fd 3464 if (SLASH_P (current_directory[strlen (current_directory) - 1]))
9e0b60a8
JM
3465 current_directory = concat (current_directory, dir, NULL);
3466 else
3467 current_directory = concat (current_directory, SLASH_STRING, dir, NULL);
3468 free (dir);
3469 }
3470
3471 /* Now simplify any occurrences of `.' and `..' in the pathname. */
3472
3473 found_real_path = 0;
3474 for (p = current_directory; *p;)
3475 {
3476 if (SLASH_P (p[0]) && p[1] == '.' && (p[2] == 0 || SLASH_P (p[2])))
3477 strcpy (p, p + 2);
3478 else if (SLASH_P (p[0]) && p[1] == '.' && p[2] == '.'
3479 && (p[3] == 0 || SLASH_P (p[3])))
3480 {
3481 if (found_real_path)
3482 {
3483 /* Search backwards for the directory just before the "/.."
c5aa993b 3484 and obliterate it and the "/..". */
9e0b60a8 3485 char *q = p;
c5aa993b 3486 while (q != current_directory && !SLASH_P (q[-1]))
9e0b60a8
JM
3487 --q;
3488
3489 if (q == current_directory)
3490 /* current_directory is
3491 a relative pathname ("can't happen"--leave it alone). */
3492 ++p;
3493 else
3494 {
3495 strcpy (q - 1, p + 3);
3496 p = q - 1;
3497 }
3498 }
3499 else
3500 /* We are dealing with leading repetitions of "/..", for example
3501 "/../..", which is the Mach super-root. */
3502 p += 3;
3503 }
3504 else
3505 {
3506 found_real_path = 1;
3507 ++p;
3508 }
3509 }
3510
3511 forget_cached_source_info ();
3512
3513 if (from_tty)
3514 pwd_command ((char *) 0, 1);
3515}
3516\f
c5aa993b
JM
3517struct source_cleanup_lines_args
3518{
9e0b60a8
JM
3519 int old_line;
3520 char *old_file;
3521 char *old_pre_error;
3522 char *old_error_pre_print;
3523};
3524
3525static void
3526source_cleanup_lines (args)
3527 PTR args;
3528{
3529 struct source_cleanup_lines_args *p =
c5aa993b 3530 (struct source_cleanup_lines_args *) args;
9e0b60a8
JM
3531 source_line_number = p->old_line;
3532 source_file_name = p->old_file;
3533 source_pre_error = p->old_pre_error;
3534 error_pre_print = p->old_error_pre_print;
3535}
3536
3537/* ARGSUSED */
3538void
3539source_command (args, from_tty)
3540 char *args;
3541 int from_tty;
3542{
3543 FILE *stream;
3544 struct cleanup *old_cleanups;
3545 char *file = args;
3546 struct source_cleanup_lines_args old_lines;
3547 int needed_length;
3548
3549 if (file == NULL)
3550 {
3551 error ("source command requires pathname of file to source.");
3552 }
3553
3554 file = tilde_expand (file);
3555 old_cleanups = make_cleanup (free, file);
3556
3557 stream = fopen (file, FOPEN_RT);
3558 if (!stream)
3559 {
3560 if (from_tty)
3561 perror_with_name (file);
3562 else
3563 return;
3564 }
3565
3566 make_cleanup ((make_cleanup_func) fclose, stream);
3567
3568 old_lines.old_line = source_line_number;
3569 old_lines.old_file = source_file_name;
3570 old_lines.old_pre_error = source_pre_error;
3571 old_lines.old_error_pre_print = error_pre_print;
3572 make_cleanup (source_cleanup_lines, &old_lines);
3573 source_line_number = 0;
3574 source_file_name = file;
3575 source_pre_error = error_pre_print == NULL ? "" : error_pre_print;
3576 source_pre_error = savestring (source_pre_error, strlen (source_pre_error));
3577 make_cleanup (free, source_pre_error);
3578 /* This will get set every time we read a line. So it won't stay "" for
3579 long. */
3580 error_pre_print = "";
3581
3582 needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80;
3583 if (source_error_allocated < needed_length)
3584 {
3585 source_error_allocated *= 2;
3586 if (source_error_allocated < needed_length)
3587 source_error_allocated = needed_length;
3588 if (source_error == NULL)
3589 source_error = xmalloc (source_error_allocated);
3590 else
3591 source_error = xrealloc (source_error, source_error_allocated);
3592 }
3593
3594 read_command_file (stream);
3595
3596 do_cleanups (old_cleanups);
3597}
3598
3599/* ARGSUSED */
3600static void
3601echo_command (text, from_tty)
3602 char *text;
3603 int from_tty;
3604{
3605 char *p = text;
3606 register int c;
3607
3608 if (text)
3609 while ((c = *p++) != '\0')
3610 {
3611 if (c == '\\')
3612 {
3613 /* \ at end of argument is used after spaces
3614 so they won't be lost. */
3615 if (*p == 0)
3616 return;
3617
3618 c = parse_escape (&p);
3619 if (c >= 0)
3620 printf_filtered ("%c", c);
3621 }
3622 else
3623 printf_filtered ("%c", c);
3624 }
3625
3626 /* Force this output to appear now. */
3627 wrap_here ("");
3628 gdb_flush (gdb_stdout);
3629}
3630
3631/* ARGSUSED */
3632static void
3633dont_repeat_command (ignored, from_tty)
3634 char *ignored;
3635 int from_tty;
3636{
c5aa993b
JM
3637 *line = 0; /* Can't call dont_repeat here because we're not
3638 necessarily reading from stdin. */
9e0b60a8
JM
3639}
3640\f
3641/* Functions to manipulate command line editing control variables. */
3642
3643/* Number of commands to print in each call to show_commands. */
3644#define Hist_print 10
3645static void
3646show_commands (args, from_tty)
3647 char *args;
3648 int from_tty;
3649{
3650 /* Index for history commands. Relative to history_base. */
3651 int offset;
3652
3653 /* Number of the history entry which we are planning to display next.
3654 Relative to history_base. */
3655 static int num = 0;
3656
3657 /* The first command in the history which doesn't exist (i.e. one more
3658 than the number of the last command). Relative to history_base. */
3659 int hist_len;
3660
3661 extern HIST_ENTRY *history_get PARAMS ((int));
3662
3663 /* Print out some of the commands from the command history. */
3664 /* First determine the length of the history list. */
3665 hist_len = history_size;
3666 for (offset = 0; offset < history_size; offset++)
3667 {
3668 if (!history_get (history_base + offset))
3669 {
3670 hist_len = offset;
3671 break;
3672 }
3673 }
3674
3675 if (args)
3676 {
3677 if (args[0] == '+' && args[1] == '\0')
3678 /* "info editing +" should print from the stored position. */
3679 ;
3680 else
3681 /* "info editing <exp>" should print around command number <exp>. */
3682 num = (parse_and_eval_address (args) - history_base) - Hist_print / 2;
3683 }
3684 /* "show commands" means print the last Hist_print commands. */
3685 else
3686 {
3687 num = hist_len - Hist_print;
3688 }
3689
3690 if (num < 0)
3691 num = 0;
3692
3693 /* If there are at least Hist_print commands, we want to display the last
3694 Hist_print rather than, say, the last 6. */
3695 if (hist_len - num < Hist_print)
3696 {
3697 num = hist_len - Hist_print;
3698 if (num < 0)
3699 num = 0;
3700 }
3701
3702 for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
3703 {
3704 printf_filtered ("%5d %s\n", history_base + offset,
c5aa993b 3705 (history_get (history_base + offset))->line);
9e0b60a8
JM
3706 }
3707
3708 /* The next command we want to display is the next one that we haven't
3709 displayed yet. */
3710 num += Hist_print;
3711
3712 /* If the user repeats this command with return, it should do what
3713 "show commands +" does. This is unnecessary if arg is null,
3714 because "show commands +" is not useful after "show commands". */
3715 if (from_tty && args)
3716 {
3717 args[0] = '+';
3718 args[1] = '\0';
3719 }
3720}
3721
3722/* Called by do_setshow_command. */
3723/* ARGSUSED */
3724static void
3725set_history_size_command (args, from_tty, c)
3726 char *args;
3727 int from_tty;
3728 struct cmd_list_element *c;
3729{
3730 if (history_size == INT_MAX)
3731 unstifle_history ();
3732 else if (history_size >= 0)
3733 stifle_history (history_size);
3734 else
3735 {
3736 history_size = INT_MAX;
3737 error ("History size must be non-negative");
3738 }
3739}
3740
3741/* ARGSUSED */
3742static void
3743set_history (args, from_tty)
3744 char *args;
3745 int from_tty;
3746{
3747 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3748 help_list (sethistlist, "set history ", -1, gdb_stdout);
3749}
3750
3751/* ARGSUSED */
3752static void
3753show_history (args, from_tty)
3754 char *args;
3755 int from_tty;
3756{
3757 cmd_show_list (showhistlist, from_tty, "");
3758}
3759
3760int info_verbose = 0; /* Default verbose msgs off */
3761
3762/* Called by do_setshow_command. An elaborate joke. */
3763/* ARGSUSED */
3764static void
3765set_verbose (args, from_tty, c)
3766 char *args;
3767 int from_tty;
3768 struct cmd_list_element *c;
3769{
3770 char *cmdname = "verbose";
3771 struct cmd_list_element *showcmd;
3772
3773 showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
3774
3775 if (info_verbose)
3776 {
3777 c->doc = "Set verbose printing of informational messages.";
3778 showcmd->doc = "Show verbose printing of informational messages.";
3779 }
3780 else
3781 {
3782 c->doc = "Set verbosity.";
3783 showcmd->doc = "Show verbosity.";
3784 }
3785}
3786
3787static void
3788float_handler (signo)
c5aa993b 3789 int signo;
9e0b60a8
JM
3790{
3791 /* This message is based on ANSI C, section 4.7. Note that integer
3792 divide by zero causes this, so "float" is a misnomer. */
3793 signal (SIGFPE, float_handler);
3794 error ("Erroneous arithmetic operation.");
3795}
9e0b60a8 3796\f
c5aa993b 3797
9e0b60a8
JM
3798static void
3799init_cmd_lists ()
3800{
3801 cmdlist = NULL;
3802 infolist = NULL;
3803 enablelist = NULL;
3804 disablelist = NULL;
3805 togglelist = NULL;
3806 stoplist = NULL;
3807 deletelist = NULL;
3808 enablebreaklist = NULL;
3809 setlist = NULL;
3810 unsetlist = NULL;
3811 showlist = NULL;
3812 sethistlist = NULL;
3813 showhistlist = NULL;
3814 unsethistlist = NULL;
3815 maintenancelist = NULL;
3816 maintenanceinfolist = NULL;
3817 maintenanceprintlist = NULL;
3818 setprintlist = NULL;
3819 showprintlist = NULL;
3820 setchecklist = NULL;
3821 showchecklist = NULL;
3822}
3823
3824/* Init the history buffer. Note that we are called after the init file(s)
3825 * have been read so that the user can change the history file via his
3826 * .gdbinit file (for instance). The GDBHISTFILE environment variable
3827 * overrides all of this.
3828 */
3829
3830void
c5aa993b 3831init_history ()
9e0b60a8
JM
3832{
3833 char *tmpenv;
3834
3835 tmpenv = getenv ("HISTSIZE");
3836 if (tmpenv)
3837 history_size = atoi (tmpenv);
3838 else if (!history_size)
3839 history_size = 256;
3840
3841 stifle_history (history_size);
3842
3843 tmpenv = getenv ("GDBHISTFILE");
3844 if (tmpenv)
c5aa993b
JM
3845 history_filename = savestring (tmpenv, strlen (tmpenv));
3846 else if (!history_filename)
3847 {
3848 /* We include the current directory so that if the user changes
3849 directories the file written will be the same as the one
3850 that was read. */
a0b3c4fd
JM
3851#ifdef __MSDOS__
3852 /* No leading dots in file names are allowed on MSDOS. */
3853 history_filename = concat (current_directory, "/_gdb_history", NULL);
3854#else
c5aa993b 3855 history_filename = concat (current_directory, "/.gdb_history", NULL);
a0b3c4fd 3856#endif
c5aa993b 3857 }
9e0b60a8
JM
3858 read_history (history_filename);
3859}
3860
3861static void
3862init_main ()
3863{
3864 struct cmd_list_element *c;
3865
3866 /* If we are running the asynchronous version,
3867 we initialize the prompts differently. */
6426a772 3868 if (!event_loop_p)
9e0b60a8 3869 {
c5aa993b 3870 gdb_prompt_string = savestring (DEFAULT_PROMPT, strlen (DEFAULT_PROMPT));
9e0b60a8
JM
3871 }
3872 else
3873 {
3874 /* initialize the prompt stack to a simple "(gdb) " prompt or to
96baa820 3875 whatever the DEFAULT_PROMPT is. */
9e0b60a8 3876 the_prompts.top = 0;
c5aa993b 3877 PREFIX (0) = "";
c5aa993b 3878 PROMPT (0) = savestring (DEFAULT_PROMPT, strlen (DEFAULT_PROMPT));
c5aa993b 3879 SUFFIX (0) = "";
9e0b60a8 3880 /* Set things up for annotation_level > 1, if the user ever decides
c5aa993b 3881 to use it. */
9e0b60a8
JM
3882 async_annotation_suffix = "prompt";
3883 /* Set the variable associated with the setshow prompt command. */
3884 new_async_prompt = savestring (PROMPT (0), strlen (PROMPT (0)));
3885 }
3886 gdb_prompt_escape = 0; /* default to none. */
3887
3888 /* Set the important stuff up for command editing. */
3889 command_editing_p = 1;
9e0b60a8
JM
3890 history_expansion_p = 0;
3891 write_history_p = 0;
3892
3893 /* Setup important stuff for command line editing. */
3894 rl_completion_entry_function = (int (*)()) readline_line_completion_function;
3895 rl_completer_word_break_characters = gdb_completer_word_break_characters;
3896 rl_completer_quote_characters = gdb_completer_quote_characters;
3897 rl_readline_name = "gdb";
3898
3899 /* Define the classes of commands.
3900 They will appear in the help list in the reverse of this order. */
3901
3902 add_cmd ("internals", class_maintenance, NO_FUNCTION,
3903 "Maintenance commands.\n\
3904Some gdb commands are provided just for use by gdb maintainers.\n\
3905These commands are subject to frequent change, and may not be as\n\
3906well documented as user commands.",
3907 &cmdlist);
3908 add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist);
3909 add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist);
3910 add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\
3911The commands in this class are those defined by the user.\n\
3912Use the \"define\" command to define a command.", &cmdlist);
3913 add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist);
3914 if (!dbx_commands)
3915 add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist);
3916 add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist);
3917 add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist);
3918 add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist);
3919 add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\
3920The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
3921counting from zero for the innermost (currently executing) frame.\n\n\
3922At any time gdb identifies one frame as the \"selected\" frame.\n\
3923Variable lookups are done with respect to the selected frame.\n\
3924When the program being debugged stops, gdb selects the innermost frame.\n\
3925The commands below can be used to select other frames by number or address.",
3926 &cmdlist);
3927 add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist);
3928
3929 add_com ("pwd", class_files, pwd_command,
c5aa993b 3930 "Print working directory. This is used for your program as well.");
9e0b60a8 3931 c = add_cmd ("cd", class_files, cd_command,
c5aa993b 3932 "Set working directory to DIR for debugger and program being debugged.\n\
9e0b60a8
JM
3933The change does not take effect for the program being debugged\n\
3934until the next time it is started.", &cmdlist);
3935 c->completer = filename_completer;
3936
3937 /* The set prompt command is different depending whether or not the
3938 async version is run. NOTE: this difference is going to
3939 disappear as we make the event loop be the default engine of
3940 gdb. */
6426a772 3941 if (!event_loop_p)
9e0b60a8
JM
3942 {
3943 add_show_from_set
c5aa993b 3944 (add_set_cmd ("prompt", class_support, var_string,
9e0b60a8
JM
3945 (char *) &gdb_prompt_string, "Set gdb's prompt",
3946 &setlist),
3947 &showlist);
3948 }
3949 else
3950 {
c5aa993b
JM
3951 c = add_set_cmd ("prompt", class_support, var_string,
3952 (char *) &new_async_prompt, "Set gdb's prompt",
9e0b60a8
JM
3953 &setlist);
3954 add_show_from_set (c, &showlist);
3955 c->function.sfunc = set_async_prompt;
3956 }
3957
3958 add_show_from_set
c5aa993b 3959 (add_set_cmd ("prompt-escape-char", class_support, var_zinteger,
9e0b60a8
JM
3960 (char *) &gdb_prompt_escape,
3961 "Set escape character for formatting of gdb's prompt",
3962 &setlist),
3963 &showlist);
3964
3965 add_com ("echo", class_support, echo_command,
3966 "Print a constant string. Give string as argument.\n\
3967C escape sequences may be used in the argument.\n\
3968No newline is added at the end of the argument;\n\
3969use \"\\n\" if you want a newline to be printed.\n\
3970Since leading and trailing whitespace are ignored in command arguments,\n\
3971if you want to print some you must use \"\\\" before leading whitespace\n\
3972to be printed or after trailing whitespace.");
3973 add_com ("document", class_support, document_command,
3974 "Document a user-defined command.\n\
3975Give command name as argument. Give documentation on following lines.\n\
3976End with a line of just \"end\".");
3977 add_com ("define", class_support, define_command,
3978 "Define a new command name. Command name is argument.\n\
3979Definition appears on following lines, one command per line.\n\
3980End with a line of just \"end\".\n\
3981Use the \"document\" command to give documentation for the new command.\n\
3982Commands defined in this way may have up to ten arguments.");
3983
3984#ifdef __STDC__
3985 c = add_cmd ("source", class_support, source_command,
c5aa993b 3986 "Read commands from a file named FILE.\n\
9e0b60a8
JM
3987Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\
3988when gdb is started.", &cmdlist);
3989#else
3990 /* Punt file name, we can't help it easily. */
3991 c = add_cmd ("source", class_support, source_command,
c5aa993b 3992 "Read commands from a file named FILE.\n\
9e0b60a8
JM
3993Note that the file \".gdbinit\" is read automatically in this way\n\
3994when gdb is started.", &cmdlist);
3995#endif
3996 c->completer = filename_completer;
3997
3998 add_com ("quit", class_support, quit_command, "Exit gdb.");
3999 add_com ("help", class_support, help_command, "Print list of commands.");
4000 add_com_alias ("q", "quit", class_support, 1);
4001 add_com_alias ("h", "help", class_support, 1);
4002
4003 add_com ("dont-repeat", class_support, dont_repeat_command, "Don't repeat this command.\n\
4004Primarily used inside of user-defined commands that should not be repeated when\n\
4005hitting return.");
4006
c5aa993b 4007 c = add_set_cmd ("verbose", class_support, var_boolean, (char *) &info_verbose,
9e0b60a8
JM
4008 "Set ",
4009 &setlist),
c5aa993b 4010 add_show_from_set (c, &showlist);
9e0b60a8
JM
4011 c->function.sfunc = set_verbose;
4012 set_verbose (NULL, 0, c);
4013
4014 /* The set editing command is different depending whether or not the
4015 async version is run. NOTE: this difference is going to disappear
4016 as we make the event loop be the default engine of gdb. */
6426a772 4017 if (!event_loop_p)
9e0b60a8
JM
4018 {
4019 add_show_from_set
c5aa993b 4020 (add_set_cmd ("editing", class_support, var_boolean, (char *) &command_editing_p,
9e0b60a8
JM
4021 "Set editing of command lines as they are typed.\n\
4022Use \"on\" to enable the editing, and \"off\" to disable it.\n\
4023Without an argument, command line editing is enabled. To edit, use\n\
4024EMACS-like or VI-like commands like control-P or ESC.", &setlist),
4025 &showlist);
4026 }
4027 else
4028 {
c5aa993b 4029 c = add_set_cmd ("editing", class_support, var_boolean, (char *) &async_command_editing_p,
9e0b60a8
JM
4030 "Set editing of command lines as they are typed.\n\
4031Use \"on\" to enable the editing, and \"off\" to disable it.\n\
4032Without an argument, command line editing is enabled. To edit, use\n\
4033EMACS-like or VI-like commands like control-P or ESC.", &setlist);
4034
4035 add_show_from_set (c, &showlist);
4036 c->function.sfunc = set_async_editing_command;
4037 }
4038
4039 add_prefix_cmd ("history", class_support, set_history,
4040 "Generic command for setting command history parameters.",
4041 &sethistlist, "set history ", 0, &setlist);
4042 add_prefix_cmd ("history", class_support, show_history,
4043 "Generic command for showing command history parameters.",
4044 &showhistlist, "show history ", 0, &showlist);
4045
4046 add_show_from_set
c5aa993b
JM
4047 (add_set_cmd ("expansion", no_class, var_boolean, (char *) &history_expansion_p,
4048 "Set history expansion on command input.\n\
9e0b60a8
JM
4049Without an argument, history expansion is enabled.", &sethistlist),
4050 &showhistlist);
4051
4052 add_show_from_set
c5aa993b
JM
4053 (add_set_cmd ("save", no_class, var_boolean, (char *) &write_history_p,
4054 "Set saving of the history record on exit.\n\
9e0b60a8
JM
4055Use \"on\" to enable the saving, and \"off\" to disable it.\n\
4056Without an argument, saving is enabled.", &sethistlist),
4057 &showhistlist);
4058
c5aa993b 4059 c = add_set_cmd ("size", no_class, var_integer, (char *) &history_size,
9e0b60a8
JM
4060 "Set the size of the command history, \n\
4061ie. the number of previous commands to keep a record of.", &sethistlist);
4062 add_show_from_set (c, &showhistlist);
4063 c->function.sfunc = set_history_size_command;
4064
4065 add_show_from_set
c5aa993b
JM
4066 (add_set_cmd ("filename", no_class, var_filename, (char *) &history_filename,
4067 "Set the filename in which to record the command history\n\
9e0b60a8
JM
4068 (the list of previous commands of which a record is kept).", &sethistlist),
4069 &showhistlist);
4070
4071 add_show_from_set
4072 (add_set_cmd ("confirm", class_support, var_boolean,
c5aa993b 4073 (char *) &caution,
9e0b60a8
JM
4074 "Set whether to confirm potentially dangerous operations.",
4075 &setlist),
4076 &showlist);
4077
4078 add_prefix_cmd ("info", class_info, info_command,
c5aa993b 4079 "Generic command for showing things about the program being debugged.",
9e0b60a8
JM
4080 &infolist, "info ", 0, &cmdlist);
4081 add_com_alias ("i", "info", class_info, 1);
4082
4083 add_com ("complete", class_obscure, complete_command,
4084 "List the completions for the rest of the line as a command.");
4085
4086 add_prefix_cmd ("show", class_info, show_command,
4087 "Generic command for showing things about the debugger.",
4088 &showlist, "show ", 0, &cmdlist);
4089 /* Another way to get at the same thing. */
4090 add_info ("set", show_command, "Show all GDB settings.");
4091
4092 add_cmd ("commands", no_class, show_commands,
4093 "Show the history of commands you typed.\n\
4094You can supply a command number to start with, or a `+' to start after\n\
4095the previous command number shown.",
4096 &showlist);
4097
4098 add_cmd ("version", no_class, show_version,
4099 "Show what version of GDB this is.", &showlist);
4100
4101 add_com ("while", class_support, while_command,
c5aa993b 4102 "Execute nested commands WHILE the conditional expression is non zero.\n\
9e0b60a8
JM
4103The conditional expression must follow the word `while' and must in turn be\n\
4104followed by a new line. The nested commands must be entered one per line,\n\
4105and should be terminated by the word `end'.");
4106
4107 add_com ("if", class_support, if_command,
c5aa993b 4108 "Execute nested commands once IF the conditional expression is non zero.\n\
9e0b60a8
JM
4109The conditional expression must follow the word `if' and must in turn be\n\
4110followed by a new line. The nested commands must be entered one per line,\n\
4111and should be terminated by the word 'else' or `end'. If an else clause\n\
4112is used, the same rules apply to its nested commands as to the first ones.");
4113
4114 /* If target is open when baud changes, it doesn't take effect until the
4115 next open (I think, not sure). */
4116 add_show_from_set (add_set_cmd ("remotebaud", no_class,
c5aa993b 4117 var_zinteger, (char *) &baud_rate,
9e0b60a8
JM
4118 "Set baud rate for remote serial I/O.\n\
4119This value is used to set the speed of the serial port when debugging\n\
4120using remote targets.", &setlist),
4121 &showlist);
4122
4123 add_show_from_set (
c5aa993b
JM
4124 add_set_cmd ("remotedebug", no_class, var_zinteger, (char *) &remote_debug,
4125 "Set debugging of remote protocol.\n\
9e0b60a8
JM
4126When enabled, each packet sent or received with the remote target\n\
4127is displayed.", &setlist),
c5aa993b 4128 &showlist);
9e0b60a8
JM
4129
4130 add_show_from_set (
c5aa993b
JM
4131 add_set_cmd ("remotetimeout", no_class, var_integer, (char *) &remote_timeout,
4132 "Set timeout limit to wait for target to respond.\n\
9e0b60a8
JM
4133This value is used to set the time limit for gdb to wait for a response\n\
4134from the target.", &setlist),
c5aa993b 4135 &showlist);
9e0b60a8
JM
4136
4137 /* The set annotate command is different depending whether or not
4138 the async version is run. NOTE: this difference is going to
4139 disappear as we make the event loop be the default engine of
4140 gdb. */
6426a772 4141 if (!event_loop_p)
9e0b60a8 4142 {
c5aa993b
JM
4143 c = add_set_cmd ("annotate", class_obscure, var_zinteger,
4144 (char *) &annotation_level, "Set annotation_level.\n\
9e0b60a8
JM
41450 == normal; 1 == fullname (for use when running under emacs)\n\
41462 == output annotated suitably for use by programs that control GDB.",
4147 &setlist);
4148 c = add_show_from_set (c, &showlist);
4149 }
4150 else
4151 {
c5aa993b
JM
4152 c = add_set_cmd ("annotate", class_obscure, var_zinteger,
4153 (char *) &annotation_level, "Set annotation_level.\n\
9e0b60a8
JM
41540 == normal; 1 == fullname (for use when running under emacs)\n\
41552 == output annotated suitably for use by programs that control GDB.",
c5aa993b 4156 &setlist);
9e0b60a8
JM
4157 add_show_from_set (c, &showlist);
4158 c->function.sfunc = set_async_annotation_level;
4159 }
6426a772 4160 if (event_loop_p)
104c1213
JM
4161 {
4162 add_show_from_set
4163 (add_set_cmd ("exec-done-display", class_support, var_boolean, (char *) &exec_done_display_p,
4164 "Set notification of completion for asynchronous execution commands.\n\
4165Use \"on\" to enable the notification, and \"off\" to disable it.", &setlist),
4166 &showlist);
4167 }
9e0b60a8 4168}
This page took 0.219595 seconds and 4 git commands to generate.