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