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