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