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