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