Support both the generic remote protocol and the one for HP-PRO targets.
[binutils-gdb.git] / gdb / top.c
1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 "language.h"
32 #include "terminal.h" /* For job_control. */
33 #include "annotate.h"
34 #include <setjmp.h>
35 #include "top.h"
36
37 /* readline include files */
38 #include "readline.h"
39 #include "history.h"
40
41 /* readline defines this. */
42 #undef savestring
43
44 #include <sys/types.h>
45 #ifdef USG
46 /* What is this for? X_OK? */
47 #include <unistd.h>
48 #endif
49
50 #include <string.h>
51 #ifndef NO_SYS_FILE
52 #include <sys/file.h>
53 #endif
54 #include <sys/param.h>
55 #include <sys/stat.h>
56 #include <ctype.h>
57
58 /* Prototypes for local functions */
59
60 static char *
61 symbol_completion_function PARAMS ((char *, int));
62
63 static void
64 command_loop_marker PARAMS ((int));
65
66 static void
67 init_main PARAMS ((void));
68
69 static void
70 init_cmd_lists PARAMS ((void));
71
72 static void
73 float_handler PARAMS ((int));
74
75 static void
76 init_signals PARAMS ((void));
77
78 static void
79 set_verbose PARAMS ((char *, int, struct cmd_list_element *));
80
81 static void
82 show_history PARAMS ((char *, int));
83
84 static void
85 set_history PARAMS ((char *, int));
86
87 static void
88 set_history_size_command PARAMS ((char *, int, struct cmd_list_element *));
89
90 static void
91 show_commands PARAMS ((char *, int));
92
93 static void
94 echo_command PARAMS ((char *, int));
95
96 static void
97 pwd_command PARAMS ((char *, int));
98
99 static void
100 show_version PARAMS ((char *, int));
101
102 static void
103 document_command PARAMS ((char *, int));
104
105 static void
106 define_command PARAMS ((char *, int));
107
108 static void
109 validate_comname PARAMS ((char *));
110
111 static void
112 help_command PARAMS ((char *, int));
113
114 static void
115 show_command PARAMS ((char *, int));
116
117 static void
118 info_command PARAMS ((char *, int));
119
120 static void
121 complete_command PARAMS ((char *, int));
122
123 static void
124 do_nothing PARAMS ((int));
125
126 static int
127 quit_cover PARAMS ((char *));
128
129 static void
130 disconnect PARAMS ((int));
131
132 static void
133 source_cleanup PARAMS ((FILE *));
134
135 /* If this definition isn't overridden by the header files, assume
136 that isatty and fileno exist on this system. */
137 #ifndef ISATTY
138 #define ISATTY(FP) (isatty (fileno (FP)))
139 #endif
140
141 /* Initialization file name for gdb. This is overridden in some configs. */
142
143 #ifndef GDBINIT_FILENAME
144 #define GDBINIT_FILENAME ".gdbinit"
145 #endif
146 char gdbinit[] = GDBINIT_FILENAME;
147 int inhibit_gdbinit = 0;
148
149 /* Disable windows if non-zero */
150
151 int no_windows = 0;
152
153 /* Version number of GDB, as a string. */
154
155 extern char *version;
156
157 /* Canonical host name as a string. */
158
159 extern char *host_name;
160
161 /* Canonical target name as a string. */
162
163 extern char *target_name;
164
165 extern char lang_frame_mismatch_warn[]; /* language.c */
166
167 /* Flag for whether we want all the "from_tty" gubbish printed. */
168
169 int caution = 1; /* Default is yes, sigh. */
170
171 /*
172 * Define all cmd_list_element's
173 */
174
175 /* Chain containing all defined commands. */
176
177 struct cmd_list_element *cmdlist;
178
179 /* Chain containing all defined info subcommands. */
180
181 struct cmd_list_element *infolist;
182
183 /* Chain containing all defined enable subcommands. */
184
185 struct cmd_list_element *enablelist;
186
187 /* Chain containing all defined disable subcommands. */
188
189 struct cmd_list_element *disablelist;
190
191 /* Chain containing all defined delete subcommands. */
192
193 struct cmd_list_element *deletelist;
194
195 /* Chain containing all defined "enable breakpoint" subcommands. */
196
197 struct cmd_list_element *enablebreaklist;
198
199 /* Chain containing all defined set subcommands */
200
201 struct cmd_list_element *setlist;
202
203 /* Chain containing all defined unset subcommands */
204
205 struct cmd_list_element *unsetlist;
206
207 /* Chain containing all defined show subcommands. */
208
209 struct cmd_list_element *showlist;
210
211 /* Chain containing all defined \"set history\". */
212
213 struct cmd_list_element *sethistlist;
214
215 /* Chain containing all defined \"show history\". */
216
217 struct cmd_list_element *showhistlist;
218
219 /* Chain containing all defined \"unset history\". */
220
221 struct cmd_list_element *unsethistlist;
222
223 /* Chain containing all defined maintenance subcommands. */
224
225 #if MAINTENANCE_CMDS
226 struct cmd_list_element *maintenancelist;
227 #endif
228
229 /* Chain containing all defined "maintenance info" subcommands. */
230
231 #if MAINTENANCE_CMDS
232 struct cmd_list_element *maintenanceinfolist;
233 #endif
234
235 /* Chain containing all defined "maintenance print" subcommands. */
236
237 #if MAINTENANCE_CMDS
238 struct cmd_list_element *maintenanceprintlist;
239 #endif
240
241 struct cmd_list_element *setprintlist;
242
243 struct cmd_list_element *showprintlist;
244
245 struct cmd_list_element *setchecklist;
246
247 struct cmd_list_element *showchecklist;
248
249 /* stdio stream that command input is being read from. Set to stdin normally.
250 Set by source_command to the file we are sourcing. Set to NULL if we are
251 executing a user-defined command. */
252
253 FILE *instream;
254
255 /* Current working directory. */
256
257 char *current_directory;
258
259 /* The directory name is actually stored here (usually). */
260 char gdb_dirbuf[1024];
261
262 /* Function to call before reading a command, if nonzero.
263 The function receives two args: an input stream,
264 and a prompt string. */
265
266 void (*window_hook) PARAMS ((FILE *, char *));
267
268 int epoch_interface;
269 int xgdb_verbose;
270
271 /* gdb prints this when reading a command interactively */
272 static char *prompt;
273
274 /* Buffer used for reading command lines, and the size
275 allocated for it so far. */
276
277 char *line;
278 int linesize = 100;
279
280 /* Nonzero if the current command is modified by "server ". This
281 affects things like recording into the command history, comamnds
282 repeating on RETURN, etc. This is so a user interface (emacs, GUI,
283 whatever) can issue its own commands and also send along commands
284 from the user, and have the user not notice that the user interface
285 is issuing commands too. */
286 int server_command;
287
288 /* Baud rate specified for talking to serial target systems. Default
289 is left as -1, so targets can choose their own defaults. */
290 /* FIXME: This means that "show remotebaud" and gr_files_info can print -1
291 or (unsigned int)-1. This is a Bad User Interface. */
292
293 int baud_rate = -1;
294
295 /* Non-zero tells remote* modules to output debugging info. */
296
297 int remote_debug = 0;
298
299 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
300
301 #ifndef STOP_SIGNAL
302 #ifdef SIGTSTP
303 #define STOP_SIGNAL SIGTSTP
304 static void stop_sig PARAMS ((int));
305 #endif
306 #endif
307
308 /* Some System V have job control but not sigsetmask(). */
309 #if !defined (HAVE_SIGSETMASK)
310 #if !defined (USG)
311 #define HAVE_SIGSETMASK 1
312 #else
313 #define HAVE_SIGSETMASK 0
314 #endif
315 #endif
316
317 #if 0 == (HAVE_SIGSETMASK)
318 #define sigsetmask(n)
319 #endif
320
321 /* Hooks for alternate command interfaces. */
322
323 /* Called after most modules have been initialized, but before taking users
324 command file. */
325
326 void (*init_ui_hook) PARAMS ((void));
327
328 /* Called instead of command_loop at top level. Can be invoked via
329 return_to_top_level. */
330
331 void (*command_loop_hook) PARAMS ((void));
332
333 /* Called instead of fputs for all output. */
334
335 void (*fputs_unfiltered_hook) PARAMS ((const char *linebuffer));
336
337 /* Called from print_frame_info to list the line we stopped in. */
338
339 void (*print_frame_info_listing_hook) PARAMS ((struct symtab *s, int line,
340 int stopline, int noerror));
341 /* Replaces most of query. */
342
343 int (*query_hook) PARAMS (());
344
345 /* Called from gdb_flush to flush output. */
346
347 void (*flush_hook) PARAMS ((FILE *stream));
348
349 /* Called as appropriate to notify the interface of the specified breakpoint
350 conditions. */
351
352 void (*create_breakpoint_hook) PARAMS ((struct breakpoint *b));
353 void (*delete_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
354 void (*enable_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
355 void (*disable_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
356 \f
357 /* Where to go for return_to_top_level (RETURN_ERROR). */
358 jmp_buf error_return;
359 /* Where to go for return_to_top_level (RETURN_QUIT). */
360 jmp_buf quit_return;
361
362 /* Return for reason REASON. This generally gets back to the command
363 loop, but can be caught via catch_errors. */
364
365 NORETURN void
366 return_to_top_level (reason)
367 enum return_reason reason;
368 {
369 quit_flag = 0;
370 immediate_quit = 0;
371
372 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
373 I can think of a reason why that is vital, though). */
374 bpstat_clear_actions(stop_bpstat); /* Clear queued breakpoint commands */
375
376 disable_current_display ();
377 do_cleanups (ALL_CLEANUPS);
378
379 if (annotation_level > 1)
380 switch (reason)
381 {
382 case RETURN_QUIT:
383 annotate_quit ();
384 break;
385 case RETURN_ERROR:
386 annotate_error ();
387 break;
388 }
389
390 (NORETURN void) longjmp
391 (reason == RETURN_ERROR ? error_return : quit_return, 1);
392 }
393
394 /* Call FUNC with arg ARGS, catching any errors. If there is no
395 error, return the value returned by FUNC. If there is an error,
396 print ERRSTRING, print the specific error message, then return
397 zero.
398
399 Must not be called with immediate_quit in effect (bad things might
400 happen, say we got a signal in the middle of a memcpy to quit_return).
401 This is an OK restriction; with very few exceptions immediate_quit can
402 be replaced by judicious use of QUIT.
403
404 MASK specifies what to catch; it is normally set to
405 RETURN_MASK_ALL, if for no other reason than that the code which
406 calls catch_errors might not be set up to deal with a quit which
407 isn't caught. But if the code can deal with it, it generally
408 should be RETURN_MASK_ERROR, unless for some reason it is more
409 useful to abort only the portion of the operation inside the
410 catch_errors. Note that quit should return to the command line
411 fairly quickly, even if some further processing is being done. */
412
413 int
414 catch_errors (func, args, errstring, mask)
415 int (*func) PARAMS ((char *));
416 PTR args;
417 char *errstring;
418 return_mask mask;
419 {
420 jmp_buf saved_error;
421 jmp_buf saved_quit;
422 jmp_buf tmp_jmp;
423 int val;
424 struct cleanup *saved_cleanup_chain;
425 char *saved_error_pre_print;
426
427 saved_cleanup_chain = save_cleanups ();
428 saved_error_pre_print = error_pre_print;
429
430 if (mask & RETURN_MASK_ERROR)
431 memcpy ((char *)saved_error, (char *)error_return, sizeof (jmp_buf));
432 if (mask & RETURN_MASK_QUIT)
433 memcpy (saved_quit, quit_return, sizeof (jmp_buf));
434 error_pre_print = errstring;
435
436 if (setjmp (tmp_jmp) == 0)
437 {
438 if (mask & RETURN_MASK_ERROR)
439 memcpy (error_return, tmp_jmp, sizeof (jmp_buf));
440 if (mask & RETURN_MASK_QUIT)
441 memcpy (quit_return, tmp_jmp, sizeof (jmp_buf));
442 val = (*func) (args);
443 }
444 else
445 val = 0;
446
447 restore_cleanups (saved_cleanup_chain);
448
449 error_pre_print = saved_error_pre_print;
450 if (mask & RETURN_MASK_ERROR)
451 memcpy (error_return, saved_error, sizeof (jmp_buf));
452 if (mask & RETURN_MASK_QUIT)
453 memcpy (quit_return, saved_quit, sizeof (jmp_buf));
454 return val;
455 }
456
457 /* Handler for SIGHUP. */
458
459 static void
460 disconnect (signo)
461 int signo;
462 {
463 catch_errors (quit_cover, NULL,
464 "Could not kill the program being debugged", RETURN_MASK_ALL);
465 signal (SIGHUP, SIG_DFL);
466 kill (getpid (), SIGHUP);
467 }
468
469 /* Just a little helper function for disconnect(). */
470
471 static int
472 quit_cover (s)
473 char *s;
474 {
475 caution = 0; /* Throw caution to the wind -- we're exiting.
476 This prevents asking the user dumb questions. */
477 quit_command((char *)0, 0);
478 return 0;
479 }
480 \f
481 /* Line number we are currently in in a file which is being sourced. */
482 static int source_line_number;
483
484 /* Name of the file we are sourcing. */
485 static char *source_file_name;
486
487 /* Buffer containing the error_pre_print used by the source stuff.
488 Malloc'd. */
489 static char *source_error;
490 static int source_error_allocated;
491
492 /* Something to glom on to the start of error_pre_print if source_file_name
493 is set. */
494 static char *source_pre_error;
495
496 /* Clean up on error during a "source" command (or execution of a
497 user-defined command). */
498
499 static void
500 source_cleanup (stream)
501 FILE *stream;
502 {
503 /* Restore the previous input stream. */
504 instream = stream;
505 }
506
507 /* Read commands from STREAM. */
508 void
509 read_command_file (stream)
510 FILE *stream;
511 {
512 struct cleanup *cleanups;
513
514 cleanups = make_cleanup (source_cleanup, instream);
515 instream = stream;
516 command_loop ();
517 do_cleanups (cleanups);
518 }
519 \f
520 extern void init_proc ();
521
522 void
523 gdb_init ()
524 {
525 /* Run the init function of each source file */
526
527 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
528 current_directory = gdb_dirbuf;
529
530 init_cmd_lists (); /* This needs to be done first */
531 initialize_targets (); /* Setup target_terminal macros for utils.c */
532 initialize_utils (); /* Make errors and warnings possible */
533 initialize_all_files ();
534 init_main (); /* But that omits this file! Do it now */
535 init_signals ();
536
537 init_proc ();
538
539 /* We need a default language for parsing expressions, so simple things like
540 "set width 0" won't fail if no language is explicitly set in a config file
541 or implicitly set by reading an executable during startup. */
542 set_language (language_c);
543 expected_language = current_language; /* don't warn about the change. */
544
545 if (init_ui_hook)
546 init_ui_hook ();
547 }
548
549 void
550 execute_user_command (c, args)
551 struct cmd_list_element *c;
552 char *args;
553 {
554 register struct command_line *cmdlines;
555 struct cleanup *old_chain;
556
557 if (args)
558 error ("User-defined commands cannot take arguments.");
559
560 cmdlines = c->user_commands;
561 if (cmdlines == 0)
562 /* Null command */
563 return;
564
565 /* Set the instream to 0, indicating execution of a
566 user-defined function. */
567 old_chain = make_cleanup (source_cleanup, instream);
568 instream = (FILE *) 0;
569 while (cmdlines)
570 {
571 execute_command (cmdlines->line, 0);
572 cmdlines = cmdlines->next;
573 }
574 do_cleanups (old_chain);
575 }
576
577 /* Execute the line P as a command.
578 Pass FROM_TTY as second argument to the defining function. */
579
580 void
581 execute_command (p, from_tty)
582 char *p;
583 int from_tty;
584 {
585 register struct cmd_list_element *c;
586 register enum language flang;
587 static int warned = 0;
588
589 free_all_values ();
590
591 /* This can happen when command_line_input hits end of file. */
592 if (p == NULL)
593 return;
594
595 while (*p == ' ' || *p == '\t') p++;
596 if (*p)
597 {
598 char *arg;
599
600 c = lookup_cmd (&p, cmdlist, "", 0, 1);
601 /* Pass null arg rather than an empty one. */
602 arg = *p ? p : 0;
603
604 /* If this command has been hooked, run the hook first. */
605 if (c->hook)
606 execute_user_command (c->hook, (char *)0);
607
608 if (c->class == class_user)
609 execute_user_command (c, arg);
610 else if (c->type == set_cmd || c->type == show_cmd)
611 do_setshow_command (arg, from_tty & caution, c);
612 else if (c->function.cfunc == NO_FUNCTION)
613 error ("That is not a command, just a help topic.");
614 else
615 (*c->function.cfunc) (arg, from_tty & caution);
616 }
617
618 /* Tell the user if the language has changed (except first time). */
619 if (current_language != expected_language)
620 {
621 if (language_mode == language_mode_auto) {
622 language_info (1); /* Print what changed. */
623 }
624 warned = 0;
625 }
626
627 /* Warn the user if the working language does not match the
628 language of the current frame. Only warn the user if we are
629 actually running the program, i.e. there is a stack. */
630 /* FIXME: This should be cacheing the frame and only running when
631 the frame changes. */
632 if (target_has_stack)
633 {
634 flang = get_frame_language ();
635 if (!warned
636 && flang != language_unknown
637 && flang != current_language->la_language)
638 {
639 printf_filtered ("%s\n", lang_frame_mismatch_warn);
640 warned = 1;
641 }
642 }
643 }
644
645 /* ARGSUSED */
646 static void
647 command_loop_marker (foo)
648 int foo;
649 {
650 }
651
652 /* Read commands from `instream' and execute them
653 until end of file or error reading instream. */
654 void
655 command_loop ()
656 {
657 struct cleanup *old_chain;
658 char *command;
659 int stdin_is_tty = ISATTY (stdin);
660
661 while (!feof (instream))
662 {
663 if (window_hook && instream == stdin)
664 (*window_hook) (instream, prompt);
665
666 quit_flag = 0;
667 if (instream == stdin && stdin_is_tty)
668 reinitialize_more_filter ();
669 old_chain = make_cleanup (command_loop_marker, 0);
670 command = command_line_input (instream == stdin ? prompt : (char *) NULL,
671 instream == stdin, "prompt");
672 if (command == 0)
673 return;
674 execute_command (command, instream == stdin);
675 /* Do any commands attached to breakpoint we stopped at. */
676 bpstat_do_actions (&stop_bpstat);
677 do_cleanups (old_chain);
678 }
679 }
680 \f
681 /* Commands call this if they do not want to be repeated by null lines. */
682
683 void
684 dont_repeat ()
685 {
686 if (server_command)
687 return;
688
689 /* If we aren't reading from standard input, we are saving the last
690 thing read from stdin in line and don't want to delete it. Null lines
691 won't repeat here in any case. */
692 if (instream == stdin)
693 *line = 0;
694 }
695 \f
696 /* Read a line from the stream "instream" without command line editing.
697
698 It prints PRROMPT once at the start.
699 Action is compatible with "readline", e.g. space for the result is
700 malloc'd and should be freed by the caller.
701
702 A NULL return means end of file. */
703 char *
704 gdb_readline (prrompt)
705 char *prrompt;
706 {
707 int c;
708 char *result;
709 int input_index = 0;
710 int result_size = 80;
711
712 if (prrompt)
713 {
714 /* Don't use a _filtered function here. It causes the assumed
715 character position to be off, since the newline we read from
716 the user is not accounted for. */
717 fputs_unfiltered (prrompt, gdb_stdout);
718 /* start-sanitize-mpw */
719 #ifdef MPW
720 /* Move to a new line so the entered line doesn't have a prompt
721 on the front of it. */
722 fputs_unfiltered ("\n", gdb_stdout);
723 #endif /* MPW */
724 /* end-sanitize-mpw */
725 gdb_flush (gdb_stdout);
726 }
727
728 result = (char *) xmalloc (result_size);
729
730 while (1)
731 {
732 /* Read from stdin if we are executing a user defined command.
733 This is the right thing for prompt_for_continue, at least. */
734 c = fgetc (instream ? instream : stdin);
735
736 if (c == EOF)
737 {
738 if (input_index > 0)
739 /* The last line does not end with a newline. Return it, and
740 if we are called again fgetc will still return EOF and
741 we'll return NULL then. */
742 break;
743 free (result);
744 return NULL;
745 }
746
747 if (c == '\n')
748 break;
749
750 result[input_index++] = c;
751 while (input_index >= result_size)
752 {
753 result_size *= 2;
754 result = (char *) xrealloc (result, result_size);
755 }
756 }
757
758 result[input_index++] = '\0';
759 return result;
760 }
761
762 /* Variables which control command line editing and history
763 substitution. These variables are given default values at the end
764 of this file. */
765 static int command_editing_p;
766 static int history_expansion_p;
767 static int write_history_p;
768 static int history_size;
769 static char *history_filename;
770
771 /* readline uses the word breaks for two things:
772 (1) In figuring out where to point the TEXT parameter to the
773 rl_completion_entry_function. Since we don't use TEXT for much,
774 it doesn't matter a lot what the word breaks are for this purpose, but
775 it does affect how much stuff M-? lists.
776 (2) If one of the matches contains a word break character, readline
777 will quote it. That's why we switch between
778 gdb_completer_word_break_characters and
779 gdb_completer_command_word_break_characters. I'm not sure when
780 we need this behavior (perhaps for funky characters in C++ symbols?). */
781
782 /* Variables which are necessary for fancy command line editing. */
783 char *gdb_completer_word_break_characters =
784 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
785
786 /* When completing on command names, we remove '-' from the list of
787 word break characters, since we use it in command names. If the
788 readline library sees one in any of the current completion strings,
789 it thinks that the string needs to be quoted and automatically supplies
790 a leading quote. */
791 char *gdb_completer_command_word_break_characters =
792 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
793
794 /* Characters that can be used to quote completion strings. Note that we
795 can't include '"' because the gdb C parser treats such quoted sequences
796 as strings. */
797 char *gdb_completer_quote_characters =
798 "'";
799
800 /* Functions that are used as part of the fancy command line editing. */
801
802 /* This can be used for functions which don't want to complete on symbols
803 but don't want to complete on anything else either. */
804 /* ARGSUSED */
805 char **
806 noop_completer (text, prefix)
807 char *text;
808 char *prefix;
809 {
810 return NULL;
811 }
812
813 /* Complete on filenames. */
814 char **
815 filename_completer (text, word)
816 char *text;
817 char *word;
818 {
819 /* From readline. */
820 extern char *filename_completion_function ();
821 int subsequent_name;
822 char **return_val;
823 int return_val_used;
824 int return_val_alloced;
825
826 return_val_used = 0;
827 /* Small for testing. */
828 return_val_alloced = 1;
829 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
830
831 subsequent_name = 0;
832 while (1)
833 {
834 char *p;
835 p = filename_completion_function (text, subsequent_name);
836 if (return_val_used >= return_val_alloced)
837 {
838 return_val_alloced *= 2;
839 return_val =
840 (char **) xrealloc (return_val,
841 return_val_alloced * sizeof (char *));
842 }
843 if (p == NULL)
844 {
845 return_val[return_val_used++] = p;
846 break;
847 }
848 /* Like emacs, don't complete on old versions. Especially useful
849 in the "source" command. */
850 if (p[strlen (p) - 1] == '~')
851 continue;
852
853 {
854 char *q;
855 if (word == text)
856 /* Return exactly p. */
857 return_val[return_val_used++] = p;
858 else if (word > text)
859 {
860 /* Return some portion of p. */
861 q = xmalloc (strlen (p) + 5);
862 strcpy (q, p + (word - text));
863 return_val[return_val_used++] = q;
864 free (p);
865 }
866 else
867 {
868 /* Return some of TEXT plus p. */
869 q = xmalloc (strlen (p) + (text - word) + 5);
870 strncpy (q, word, text - word);
871 q[text - word] = '\0';
872 strcat (q, p);
873 return_val[return_val_used++] = q;
874 free (p);
875 }
876 }
877 subsequent_name = 1;
878 }
879 #if 0
880 /* There is no way to do this just long enough to affect quote inserting
881 without also affecting the next completion. This should be fixed in
882 readline. FIXME. */
883 /* Insure that readline does the right thing
884 with respect to inserting quotes. */
885 rl_completer_word_break_characters = "";
886 #endif
887 return return_val;
888 }
889
890 /* Here are some useful test cases for completion. FIXME: These should
891 be put in the test suite. They should be tested with both M-? and TAB.
892
893 "show output-" "radix"
894 "show output" "-radix"
895 "p" ambiguous (commands starting with p--path, print, printf, etc.)
896 "p " ambiguous (all symbols)
897 "info t foo" no completions
898 "info t " no completions
899 "info t" ambiguous ("info target", "info terminal", etc.)
900 "info ajksdlfk" no completions
901 "info ajksdlfk " no completions
902 "info" " "
903 "info " ambiguous (all info commands)
904 "p \"a" no completions (string constant)
905 "p 'a" ambiguous (all symbols starting with a)
906 "p b-a" ambiguous (all symbols starting with a)
907 "p b-" ambiguous (all symbols)
908 "file Make" "file" (word break hard to screw up here)
909 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
910 */
911
912 /* Generate completions one by one for the completer. Each time we are
913 called return another potential completion to the caller. The function
914 is misnamed; it just completes on commands or passes the buck to the
915 command's completer function; the stuff specific to symbol completion
916 is in make_symbol_completion_list.
917
918 TEXT is readline's idea of the "word" we are looking at; we don't really
919 like readline's ideas about word breaking so we ignore it.
920
921 MATCHES is the number of matches that have currently been collected from
922 calling this completion function. When zero, then we need to initialize,
923 otherwise the initialization has already taken place and we can just
924 return the next potential completion string.
925
926 Returns NULL if there are no more completions, else a pointer to a string
927 which is a possible completion.
928
929 RL_LINE_BUFFER is available to be looked at; it contains the entire text
930 of the line. RL_POINT is the offset in that line of the cursor. You
931 should pretend that the line ends at RL_POINT. */
932
933 static char *
934 symbol_completion_function (text, matches)
935 char *text;
936 int matches;
937 {
938 static char **list = (char **)NULL; /* Cache of completions */
939 static int index; /* Next cached completion */
940 char *output = NULL;
941 char *tmp_command, *p;
942 /* Pointer within tmp_command which corresponds to text. */
943 char *word;
944 struct cmd_list_element *c, *result_list;
945
946 if (matches == 0)
947 {
948 /* The caller is beginning to accumulate a new set of completions, so
949 we need to find all of them now, and cache them for returning one at
950 a time on future calls. */
951
952 if (list)
953 {
954 /* Free the storage used by LIST, but not by the strings inside.
955 This is because rl_complete_internal () frees the strings. */
956 free ((PTR)list);
957 }
958 list = 0;
959 index = 0;
960
961 /* Choose the default set of word break characters to break completions.
962 If we later find out that we are doing completions on command strings
963 (as opposed to strings supplied by the individual command completer
964 functions, which can be any string) then we will switch to the
965 special word break set for command strings, which leaves out the
966 '-' character used in some commands. */
967
968 rl_completer_word_break_characters =
969 gdb_completer_word_break_characters;
970
971 /* Decide whether to complete on a list of gdb commands or on symbols. */
972 tmp_command = (char *) alloca (rl_point + 1);
973 p = tmp_command;
974
975 strncpy (tmp_command, rl_line_buffer, rl_point);
976 tmp_command[rl_point] = '\0';
977 /* Since text always contains some number of characters leading up
978 to rl_point, we can find the equivalent position in tmp_command
979 by subtracting that many characters from the end of tmp_command. */
980 word = tmp_command + rl_point - strlen (text);
981
982 if (rl_point == 0)
983 {
984 /* An empty line we want to consider ambiguous; that is, it
985 could be any command. */
986 c = (struct cmd_list_element *) -1;
987 result_list = 0;
988 }
989 else
990 {
991 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
992 }
993
994 /* Move p up to the next interesting thing. */
995 while (*p == ' ' || *p == '\t')
996 {
997 p++;
998 }
999
1000 if (!c)
1001 {
1002 /* It is an unrecognized command. So there are no
1003 possible completions. */
1004 list = NULL;
1005 }
1006 else if (c == (struct cmd_list_element *) -1)
1007 {
1008 char *q;
1009
1010 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1011 doesn't advance over that thing itself. Do so now. */
1012 q = p;
1013 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
1014 ++q;
1015 if (q != tmp_command + rl_point)
1016 {
1017 /* There is something beyond the ambiguous
1018 command, so there are no possible completions. For
1019 example, "info t " or "info t foo" does not complete
1020 to anything, because "info t" can be "info target" or
1021 "info terminal". */
1022 list = NULL;
1023 }
1024 else
1025 {
1026 /* We're trying to complete on the command which was ambiguous.
1027 This we can deal with. */
1028 if (result_list)
1029 {
1030 list = complete_on_cmdlist (*result_list->prefixlist, p,
1031 word);
1032 }
1033 else
1034 {
1035 list = complete_on_cmdlist (cmdlist, p, word);
1036 }
1037 /* Insure that readline does the right thing with respect to
1038 inserting quotes. */
1039 rl_completer_word_break_characters =
1040 gdb_completer_command_word_break_characters;
1041 }
1042 }
1043 else
1044 {
1045 /* We've recognized a full command. */
1046
1047 if (p == tmp_command + rl_point)
1048 {
1049 /* There is no non-whitespace in the line beyond the command. */
1050
1051 if (p[-1] == ' ' || p[-1] == '\t')
1052 {
1053 /* The command is followed by whitespace; we need to complete
1054 on whatever comes after command. */
1055 if (c->prefixlist)
1056 {
1057 /* It is a prefix command; what comes after it is
1058 a subcommand (e.g. "info "). */
1059 list = complete_on_cmdlist (*c->prefixlist, p, word);
1060
1061 /* Insure that readline does the right thing
1062 with respect to inserting quotes. */
1063 rl_completer_word_break_characters =
1064 gdb_completer_command_word_break_characters;
1065 }
1066 else
1067 {
1068 /* It is a normal command; what comes after it is
1069 completed by the command's completer function. */
1070 list = (*c->completer) (p, word);
1071 }
1072 }
1073 else
1074 {
1075 /* The command is not followed by whitespace; we need to
1076 complete on the command itself. e.g. "p" which is a
1077 command itself but also can complete to "print", "ptype"
1078 etc. */
1079 char *q;
1080
1081 /* Find the command we are completing on. */
1082 q = p;
1083 while (q > tmp_command)
1084 {
1085 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
1086 --q;
1087 else
1088 break;
1089 }
1090
1091 list = complete_on_cmdlist (result_list, q, word);
1092
1093 /* Insure that readline does the right thing
1094 with respect to inserting quotes. */
1095 rl_completer_word_break_characters =
1096 gdb_completer_command_word_break_characters;
1097 }
1098 }
1099 else
1100 {
1101 /* There is non-whitespace beyond the command. */
1102
1103 if (c->prefixlist && !c->allow_unknown)
1104 {
1105 /* It is an unrecognized subcommand of a prefix command,
1106 e.g. "info adsfkdj". */
1107 list = NULL;
1108 }
1109 else
1110 {
1111 /* It is a normal command. */
1112 list = (*c->completer) (p, word);
1113 }
1114 }
1115 }
1116 }
1117
1118 /* If we found a list of potential completions during initialization then
1119 dole them out one at a time. The vector of completions is NULL
1120 terminated, so after returning the last one, return NULL (and continue
1121 to do so) each time we are called after that, until a new list is
1122 available. */
1123
1124 if (list)
1125 {
1126 output = list[index];
1127 if (output)
1128 {
1129 index++;
1130 }
1131 }
1132
1133 #if 0
1134 /* Can't do this because readline hasn't yet checked the word breaks
1135 for figuring out whether to insert a quote. */
1136 if (output == NULL)
1137 /* Make sure the word break characters are set back to normal for the
1138 next time that readline tries to complete something. */
1139 rl_completer_word_break_characters =
1140 gdb_completer_word_break_characters;
1141 #endif
1142
1143 return (output);
1144 }
1145
1146 /* Skip over a possibly quoted word (as defined by the quote characters
1147 and word break characters the completer uses). Returns pointer to the
1148 location after the "word". */
1149
1150 char *
1151 skip_quoted (str)
1152 char *str;
1153 {
1154 char quote_char = '\0';
1155 char *scan;
1156
1157 for (scan = str; *scan != '\0'; scan++)
1158 {
1159 if (quote_char != '\0')
1160 {
1161 /* Ignore everything until the matching close quote char */
1162 if (*scan == quote_char)
1163 {
1164 /* Found matching close quote. */
1165 scan++;
1166 break;
1167 }
1168 }
1169 else if (strchr (gdb_completer_quote_characters, *scan))
1170 {
1171 /* Found start of a quoted string. */
1172 quote_char = *scan;
1173 }
1174 else if (strchr (gdb_completer_word_break_characters, *scan))
1175 {
1176 break;
1177 }
1178 }
1179 return (scan);
1180 }
1181
1182 \f
1183 #ifdef STOP_SIGNAL
1184 static void
1185 stop_sig (signo)
1186 int signo;
1187 {
1188 #if STOP_SIGNAL == SIGTSTP
1189 signal (SIGTSTP, SIG_DFL);
1190 sigsetmask (0);
1191 kill (getpid (), SIGTSTP);
1192 signal (SIGTSTP, stop_sig);
1193 #else
1194 signal (STOP_SIGNAL, stop_sig);
1195 #endif
1196 printf_unfiltered ("%s", prompt);
1197 gdb_flush (gdb_stdout);
1198
1199 /* Forget about any previous command -- null line now will do nothing. */
1200 dont_repeat ();
1201 }
1202 #endif /* STOP_SIGNAL */
1203
1204 /* Initialize signal handlers. */
1205 static void
1206 do_nothing (signo)
1207 int signo;
1208 {
1209 }
1210
1211 static void
1212 init_signals ()
1213 {
1214 signal (SIGINT, request_quit);
1215
1216 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1217 passed to the inferior, which we don't want. It would be
1218 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1219 on BSD4.3 systems using vfork, that can affect the
1220 GDB process as well as the inferior (the signal handling tables
1221 might be in memory, shared between the two). Since we establish
1222 a handler for SIGQUIT, when we call exec it will set the signal
1223 to SIG_DFL for us. */
1224 signal (SIGQUIT, do_nothing);
1225 if (signal (SIGHUP, do_nothing) != SIG_IGN)
1226 signal (SIGHUP, disconnect);
1227 signal (SIGFPE, float_handler);
1228
1229 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1230 signal (SIGWINCH, SIGWINCH_HANDLER);
1231 #endif
1232 }
1233 \f
1234 /* Read one line from the command input stream `instream'
1235 into the local static buffer `linebuffer' (whose current length
1236 is `linelength').
1237 The buffer is made bigger as necessary.
1238 Returns the address of the start of the line.
1239
1240 NULL is returned for end of file.
1241
1242 *If* the instream == stdin & stdin is a terminal, the line read
1243 is copied into the file line saver (global var char *line,
1244 length linesize) so that it can be duplicated.
1245
1246 This routine either uses fancy command line editing or
1247 simple input as the user has requested. */
1248
1249 char *
1250 command_line_input (prrompt, repeat, annotation_suffix)
1251 char *prrompt;
1252 int repeat;
1253 char *annotation_suffix;
1254 {
1255 static char *linebuffer = 0;
1256 static unsigned linelength = 0;
1257 register char *p;
1258 char *p1;
1259 char *rl;
1260 char *local_prompt = prrompt;
1261 register int c;
1262 char *nline;
1263 char got_eof = 0;
1264
1265 if (annotation_level > 1 && instream == stdin)
1266 {
1267 local_prompt = alloca ((prrompt == NULL ? 0 : strlen (prrompt))
1268 + strlen (annotation_suffix) + 40);
1269 if (prrompt == NULL)
1270 local_prompt[0] = '\0';
1271 else
1272 strcpy (local_prompt, prrompt);
1273 strcat (local_prompt, "\n\032\032");
1274 strcat (local_prompt, annotation_suffix);
1275 strcat (local_prompt, "\n");
1276 }
1277
1278 if (linebuffer == 0)
1279 {
1280 linelength = 80;
1281 linebuffer = (char *) xmalloc (linelength);
1282 }
1283
1284 p = linebuffer;
1285
1286 /* Control-C quits instantly if typed while in this loop
1287 since it should not wait until the user types a newline. */
1288 immediate_quit++;
1289 #ifdef STOP_SIGNAL
1290 if (job_control)
1291 signal (STOP_SIGNAL, stop_sig);
1292 #endif
1293
1294 while (1)
1295 {
1296 /* Make sure that all output has been output. Some machines may let
1297 you get away with leaving out some of the gdb_flush, but not all. */
1298 wrap_here ("");
1299 gdb_flush (gdb_stdout);
1300 gdb_flush (gdb_stderr);
1301
1302 if (source_file_name != NULL)
1303 {
1304 ++source_line_number;
1305 sprintf (source_error,
1306 "%s%s:%d: Error in sourced command file:\n",
1307 source_pre_error,
1308 source_file_name,
1309 source_line_number);
1310 error_pre_print = source_error;
1311 }
1312
1313 if (annotation_level > 1 && instream == stdin)
1314 {
1315 printf_unfiltered ("\n\032\032pre-");
1316 printf_unfiltered (annotation_suffix);
1317 printf_unfiltered ("\n");
1318 }
1319
1320 /* Don't use fancy stuff if not talking to stdin. */
1321 if (command_editing_p && instream == stdin
1322 && ISATTY (instream))
1323 rl = readline (local_prompt);
1324 else
1325 rl = gdb_readline (local_prompt);
1326
1327 if (annotation_level > 1 && instream == stdin)
1328 {
1329 printf_unfiltered ("\n\032\032post-");
1330 printf_unfiltered (annotation_suffix);
1331 printf_unfiltered ("\n");
1332 }
1333
1334 if (!rl || rl == (char *) EOF)
1335 {
1336 got_eof = 1;
1337 break;
1338 }
1339 if (strlen(rl) + 1 + (p - linebuffer) > linelength)
1340 {
1341 linelength = strlen(rl) + 1 + (p - linebuffer);
1342 nline = (char *) xrealloc (linebuffer, linelength);
1343 p += nline - linebuffer;
1344 linebuffer = nline;
1345 }
1346 p1 = rl;
1347 /* Copy line. Don't copy null at end. (Leaves line alone
1348 if this was just a newline) */
1349 while (*p1)
1350 *p++ = *p1++;
1351
1352 free (rl); /* Allocated in readline. */
1353
1354 if (p == linebuffer || *(p - 1) != '\\')
1355 break;
1356
1357 p--; /* Put on top of '\'. */
1358 local_prompt = (char *) 0;
1359 }
1360
1361 #ifdef STOP_SIGNAL
1362 if (job_control)
1363 signal (STOP_SIGNAL, SIG_DFL);
1364 #endif
1365 immediate_quit--;
1366
1367 if (got_eof)
1368 return NULL;
1369
1370 #define SERVER_COMMAND_LENGTH 7
1371 server_command =
1372 (p - linebuffer > SERVER_COMMAND_LENGTH)
1373 && STREQN (linebuffer, "server ", SERVER_COMMAND_LENGTH);
1374 if (server_command)
1375 {
1376 /* Note that we don't set `line'. Between this and the check in
1377 dont_repeat, this insures that repeating will still do the
1378 right thing. */
1379 *p = '\0';
1380 return linebuffer + SERVER_COMMAND_LENGTH;
1381 }
1382
1383 /* Do history expansion if that is wished. */
1384 if (history_expansion_p && instream == stdin
1385 && ISATTY (instream))
1386 {
1387 char *history_value;
1388 int expanded;
1389
1390 *p = '\0'; /* Insert null now. */
1391 expanded = history_expand (linebuffer, &history_value);
1392 if (expanded)
1393 {
1394 /* Print the changes. */
1395 printf_unfiltered ("%s\n", history_value);
1396
1397 /* If there was an error, call this function again. */
1398 if (expanded < 0)
1399 {
1400 free (history_value);
1401 return command_line_input (prrompt, repeat, annotation_suffix);
1402 }
1403 if (strlen (history_value) > linelength)
1404 {
1405 linelength = strlen (history_value) + 1;
1406 linebuffer = (char *) xrealloc (linebuffer, linelength);
1407 }
1408 strcpy (linebuffer, history_value);
1409 p = linebuffer + strlen(linebuffer);
1410 free (history_value);
1411 }
1412 }
1413
1414 /* If we just got an empty line, and that is supposed
1415 to repeat the previous command, return the value in the
1416 global buffer. */
1417 if (repeat)
1418 {
1419 if (p == linebuffer)
1420 return line;
1421 p1 = linebuffer;
1422 while (*p1 == ' ' || *p1 == '\t')
1423 p1++;
1424 if (!*p1)
1425 return line;
1426 }
1427
1428 *p = 0;
1429
1430 /* Add line to history if appropriate. */
1431 if (instream == stdin
1432 && ISATTY (stdin) && *linebuffer)
1433 add_history (linebuffer);
1434
1435 /* Note: lines consisting solely of comments are added to the command
1436 history. This is useful when you type a command, and then
1437 realize you don't want to execute it quite yet. You can comment
1438 out the command and then later fetch it from the value history
1439 and remove the '#'. The kill ring is probably better, but some
1440 people are in the habit of commenting things out. */
1441 p1 = linebuffer;
1442 while ((c = *p1++) != '\0')
1443 {
1444 if (c == '"')
1445 while ((c = *p1++) != '"')
1446 {
1447 /* Make sure an escaped '"' doesn't make us think the string
1448 is ended. */
1449 if (c == '\\')
1450 parse_escape (&p1);
1451 if (c == '\0')
1452 break;
1453 }
1454 else if (c == '\'')
1455 while ((c = *p1++) != '\'')
1456 {
1457 /* Make sure an escaped '\'' doesn't make us think the string
1458 is ended. */
1459 if (c == '\\')
1460 parse_escape (&p1);
1461 if (c == '\0')
1462 break;
1463 }
1464 else if (c == '#')
1465 {
1466 /* Found a comment. */
1467 p1[-1] = '\0';
1468 break;
1469 }
1470 }
1471
1472 /* Save into global buffer if appropriate. */
1473 if (repeat)
1474 {
1475 if (linelength > linesize)
1476 {
1477 line = xrealloc (line, linelength);
1478 linesize = linelength;
1479 }
1480 strcpy (line, linebuffer);
1481 return line;
1482 }
1483
1484 return linebuffer;
1485 }
1486 \f
1487 /* Read lines from the input stream
1488 and accumulate them in a chain of struct command_line's
1489 which is then returned. */
1490
1491 struct command_line *
1492 read_command_lines ()
1493 {
1494 struct command_line *first = 0;
1495 register struct command_line *next, *tail = 0;
1496 register char *p, *p1;
1497 struct cleanup *old_chain = 0;
1498
1499 while (1)
1500 {
1501 dont_repeat ();
1502 p = command_line_input ((char *) NULL, instream == stdin, "commands");
1503 if (p == NULL)
1504 /* Treat end of file like "end". */
1505 break;
1506
1507 /* Remove leading and trailing blanks. */
1508 while (*p == ' ' || *p == '\t') p++;
1509 p1 = p + strlen (p);
1510 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t')) p1--;
1511
1512 /* Is this "end"? */
1513 if (p1 - p == 3 && !strncmp (p, "end", 3))
1514 break;
1515
1516 /* No => add this line to the chain of command lines. */
1517 next = (struct command_line *) xmalloc (sizeof (struct command_line));
1518 next->line = savestring (p, p1 - p);
1519 next->next = 0;
1520 if (tail)
1521 {
1522 tail->next = next;
1523 }
1524 else
1525 {
1526 /* We just read the first line.
1527 From now on, arrange to throw away the lines we have
1528 if we quit or get an error while inside this function. */
1529 first = next;
1530 old_chain = make_cleanup (free_command_lines, &first);
1531 }
1532 tail = next;
1533 }
1534
1535 dont_repeat ();
1536
1537 /* Now we are about to return the chain to our caller,
1538 so freeing it becomes his responsibility. */
1539 if (first)
1540 discard_cleanups (old_chain);
1541 return first;
1542 }
1543
1544 /* Free a chain of struct command_line's. */
1545
1546 void
1547 free_command_lines (lptr)
1548 struct command_line **lptr;
1549 {
1550 register struct command_line *l = *lptr;
1551 register struct command_line *next;
1552
1553 while (l)
1554 {
1555 next = l->next;
1556 free (l->line);
1557 free ((PTR)l);
1558 l = next;
1559 }
1560 }
1561 \f
1562 /* Add an element to the list of info subcommands. */
1563
1564 void
1565 add_info (name, fun, doc)
1566 char *name;
1567 void (*fun) PARAMS ((char *, int));
1568 char *doc;
1569 {
1570 add_cmd (name, no_class, fun, doc, &infolist);
1571 }
1572
1573 /* Add an alias to the list of info subcommands. */
1574
1575 void
1576 add_info_alias (name, oldname, abbrev_flag)
1577 char *name;
1578 char *oldname;
1579 int abbrev_flag;
1580 {
1581 add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
1582 }
1583
1584 /* The "info" command is defined as a prefix, with allow_unknown = 0.
1585 Therefore, its own definition is called only for "info" with no args. */
1586
1587 /* ARGSUSED */
1588 static void
1589 info_command (arg, from_tty)
1590 char *arg;
1591 int from_tty;
1592 {
1593 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
1594 help_list (infolist, "info ", -1, gdb_stdout);
1595 }
1596
1597 /* The "complete" command is used by Emacs to implement completion. */
1598
1599 /* ARGSUSED */
1600 static void
1601 complete_command (arg, from_tty)
1602 char *arg;
1603 int from_tty;
1604 {
1605 int i;
1606 char *completion;
1607
1608 dont_repeat ();
1609
1610 if (arg == NULL)
1611 {
1612 rl_line_buffer[0] = '\0';
1613 rl_point = 0;
1614 }
1615 else
1616 {
1617 strcpy (rl_line_buffer, arg);
1618 rl_point = strlen (arg);
1619 }
1620
1621 for (completion = symbol_completion_function (rl_line_buffer, i = 0);
1622 completion;
1623 completion = symbol_completion_function (rl_line_buffer, ++i))
1624 printf_unfiltered ("%s\n", completion);
1625 }
1626
1627 /* The "show" command with no arguments shows all the settings. */
1628
1629 /* ARGSUSED */
1630 static void
1631 show_command (arg, from_tty)
1632 char *arg;
1633 int from_tty;
1634 {
1635 cmd_show_list (showlist, from_tty, "");
1636 }
1637 \f
1638 /* Add an element to the list of commands. */
1639
1640 void
1641 add_com (name, class, fun, doc)
1642 char *name;
1643 enum command_class class;
1644 void (*fun) PARAMS ((char *, int));
1645 char *doc;
1646 {
1647 add_cmd (name, class, fun, doc, &cmdlist);
1648 }
1649
1650 /* Add an alias or abbreviation command to the list of commands. */
1651
1652 void
1653 add_com_alias (name, oldname, class, abbrev_flag)
1654 char *name;
1655 char *oldname;
1656 enum command_class class;
1657 int abbrev_flag;
1658 {
1659 add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
1660 }
1661
1662 void
1663 error_no_arg (why)
1664 char *why;
1665 {
1666 error ("Argument required (%s).", why);
1667 }
1668
1669 /* ARGSUSED */
1670 static void
1671 help_command (command, from_tty)
1672 char *command;
1673 int from_tty; /* Ignored */
1674 {
1675 help_cmd (command, gdb_stdout);
1676 }
1677 \f
1678 static void
1679 validate_comname (comname)
1680 char *comname;
1681 {
1682 register char *p;
1683
1684 if (comname == 0)
1685 error_no_arg ("name of command to define");
1686
1687 p = comname;
1688 while (*p)
1689 {
1690 if (!isalnum(*p) && *p != '-')
1691 error ("Junk in argument list: \"%s\"", p);
1692 p++;
1693 }
1694 }
1695
1696 /* This is just a placeholder in the command data structures. */
1697 static void
1698 user_defined_command (ignore, from_tty)
1699 char *ignore;
1700 int from_tty;
1701 {
1702 }
1703
1704 static void
1705 define_command (comname, from_tty)
1706 char *comname;
1707 int from_tty;
1708 {
1709 register struct command_line *cmds;
1710 register struct cmd_list_element *c, *newc, *hookc = 0;
1711 char *tem = comname;
1712 #define HOOK_STRING "hook-"
1713 #define HOOK_LEN 5
1714
1715 validate_comname (comname);
1716
1717 /* Look it up, and verify that we got an exact match. */
1718 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
1719 if (c && !STREQ (comname, c->name))
1720 c = 0;
1721
1722 if (c)
1723 {
1724 if (c->class == class_user || c->class == class_alias)
1725 tem = "Redefine command \"%s\"? ";
1726 else
1727 tem = "Really redefine built-in command \"%s\"? ";
1728 if (!query (tem, c->name))
1729 error ("Command \"%s\" not redefined.", c->name);
1730 }
1731
1732 /* If this new command is a hook, then mark the command which it
1733 is hooking. Note that we allow hooking `help' commands, so that
1734 we can hook the `stop' pseudo-command. */
1735
1736 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
1737 {
1738 /* Look up cmd it hooks, and verify that we got an exact match. */
1739 tem = comname+HOOK_LEN;
1740 hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
1741 if (hookc && !STREQ (comname+HOOK_LEN, hookc->name))
1742 hookc = 0;
1743 if (!hookc)
1744 {
1745 warning ("Your new `%s' command does not hook any existing command.",
1746 comname);
1747 if (!query ("Proceed? ", (char *)0))
1748 error ("Not confirmed.");
1749 }
1750 }
1751
1752 comname = savestring (comname, strlen (comname));
1753
1754 /* If the rest of the commands will be case insensitive, this one
1755 should behave in the same manner. */
1756 for (tem = comname; *tem; tem++)
1757 if (isupper(*tem)) *tem = tolower(*tem);
1758
1759 if (from_tty)
1760 {
1761 printf_unfiltered ("Type commands for definition of \"%s\".\n\
1762 End with a line saying just \"end\".\n", comname);
1763 gdb_flush (gdb_stdout);
1764 }
1765
1766 cmds = read_command_lines ();
1767
1768 if (c && c->class == class_user)
1769 free_command_lines (&c->user_commands);
1770
1771 newc = add_cmd (comname, class_user, user_defined_command,
1772 (c && c->class == class_user)
1773 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
1774 newc->user_commands = cmds;
1775
1776 /* If this new command is a hook, then mark both commands as being
1777 tied. */
1778 if (hookc)
1779 {
1780 hookc->hook = newc; /* Target gets hooked. */
1781 newc->hookee = hookc; /* We are marked as hooking target cmd. */
1782 }
1783 }
1784
1785 static void
1786 document_command (comname, from_tty)
1787 char *comname;
1788 int from_tty;
1789 {
1790 struct command_line *doclines;
1791 register struct cmd_list_element *c;
1792 char *tem = comname;
1793
1794 validate_comname (comname);
1795
1796 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1797
1798 if (c->class != class_user)
1799 error ("Command \"%s\" is built-in.", comname);
1800
1801 if (from_tty)
1802 printf_unfiltered ("Type documentation for \"%s\".\n\
1803 End with a line saying just \"end\".\n", comname);
1804
1805 doclines = read_command_lines ();
1806
1807 if (c->doc) free (c->doc);
1808
1809 {
1810 register struct command_line *cl1;
1811 register int len = 0;
1812
1813 for (cl1 = doclines; cl1; cl1 = cl1->next)
1814 len += strlen (cl1->line) + 1;
1815
1816 c->doc = (char *) xmalloc (len + 1);
1817 *c->doc = 0;
1818
1819 for (cl1 = doclines; cl1; cl1 = cl1->next)
1820 {
1821 strcat (c->doc, cl1->line);
1822 if (cl1->next)
1823 strcat (c->doc, "\n");
1824 }
1825 }
1826
1827 free_command_lines (&doclines);
1828 }
1829 \f
1830 void
1831 print_gnu_advertisement ()
1832 {
1833 printf_unfiltered ("\
1834 GDB is free software and you are welcome to distribute copies of it\n\
1835 under certain conditions; type \"show copying\" to see the conditions.\n\
1836 There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\
1837 ");
1838 }
1839
1840 void
1841 print_gdb_version (stream)
1842 GDB_FILE *stream;
1843 {
1844 fprintf_filtered (stream, "\
1845 GDB %s (%s", version, host_name);
1846
1847 if (!STREQ (host_name, target_name))
1848 fprintf_filtered (stream, " --target %s", target_name);
1849
1850 fprintf_filtered (stream, "), ");
1851 wrap_here("");
1852 fprintf_filtered (stream, "Copyright 1994 Free Software Foundation, Inc.");
1853 }
1854
1855 /* ARGSUSED */
1856 static void
1857 show_version (args, from_tty)
1858 char *args;
1859 int from_tty;
1860 {
1861 immediate_quit++;
1862 print_gnu_advertisement ();
1863 print_gdb_version (gdb_stdout);
1864 printf_filtered ("\n");
1865 immediate_quit--;
1866 }
1867 \f
1868 /* xgdb calls this to reprint the usual GDB prompt. Obsolete now that xgdb
1869 is obsolete. */
1870
1871 void
1872 print_prompt ()
1873 {
1874 printf_unfiltered ("%s", prompt);
1875 gdb_flush (gdb_stdout);
1876 }
1877 \f
1878 void
1879 quit_command (args, from_tty)
1880 char *args;
1881 int from_tty;
1882 {
1883 if (inferior_pid != 0 && target_has_execution)
1884 {
1885 if (attach_flag)
1886 {
1887 if (query ("The program is running. Quit anyway (and detach it)? "))
1888 target_detach (args, from_tty);
1889 else
1890 error ("Not confirmed.");
1891 }
1892 else
1893 {
1894 if (query ("The program is running. Quit anyway (and kill it)? "))
1895 target_kill ();
1896 else
1897 error ("Not confirmed.");
1898 }
1899 }
1900 /* UDI wants this, to kill the TIP. */
1901 target_close (1);
1902
1903 /* Save the history information if it is appropriate to do so. */
1904 if (write_history_p && history_filename)
1905 write_history (history_filename);
1906
1907 exit (0);
1908 }
1909
1910 /* Returns whether GDB is running on a terminal and whether the user
1911 desires that questions be asked of them on that terminal. */
1912
1913 int
1914 input_from_terminal_p ()
1915 {
1916 return gdb_has_a_terminal () && (instream == stdin) & caution;
1917 }
1918 \f
1919 /* ARGSUSED */
1920 static void
1921 pwd_command (args, from_tty)
1922 char *args;
1923 int from_tty;
1924 {
1925 if (args) error ("The \"pwd\" command does not take an argument: %s", args);
1926 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
1927
1928 if (!STREQ (gdb_dirbuf, current_directory))
1929 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
1930 current_directory, gdb_dirbuf);
1931 else
1932 printf_unfiltered ("Working directory %s.\n", current_directory);
1933 }
1934
1935 void
1936 cd_command (dir, from_tty)
1937 char *dir;
1938 int from_tty;
1939 {
1940 int len;
1941 /* Found something other than leading repetitions of "/..". */
1942 int found_real_path;
1943 char *p;
1944
1945 /* If the new directory is absolute, repeat is a no-op; if relative,
1946 repeat might be useful but is more likely to be a mistake. */
1947 dont_repeat ();
1948
1949 if (dir == 0)
1950 error_no_arg ("new working directory");
1951
1952 dir = tilde_expand (dir);
1953 make_cleanup (free, dir);
1954
1955 if (chdir (dir) < 0)
1956 perror_with_name (dir);
1957
1958 len = strlen (dir);
1959 dir = savestring (dir, len - (len > 1 && dir[len-1] == '/'));
1960 if (dir[0] == '/')
1961 current_directory = dir;
1962 else
1963 {
1964 if (current_directory[0] == '/' && current_directory[1] == '\0')
1965 current_directory = concat (current_directory, dir, NULL);
1966 else
1967 current_directory = concat (current_directory, "/", dir, NULL);
1968 free (dir);
1969 }
1970
1971 /* Now simplify any occurrences of `.' and `..' in the pathname. */
1972
1973 found_real_path = 0;
1974 for (p = current_directory; *p;)
1975 {
1976 if (p[0] == '/' && p[1] == '.' && (p[2] == 0 || p[2] == '/'))
1977 strcpy (p, p + 2);
1978 else if (p[0] == '/' && p[1] == '.' && p[2] == '.'
1979 && (p[3] == 0 || p[3] == '/'))
1980 {
1981 if (found_real_path)
1982 {
1983 /* Search backwards for the directory just before the "/.."
1984 and obliterate it and the "/..". */
1985 char *q = p;
1986 while (q != current_directory && q[-1] != '/')
1987 --q;
1988
1989 if (q == current_directory)
1990 /* current_directory is
1991 a relative pathname ("can't happen"--leave it alone). */
1992 ++p;
1993 else
1994 {
1995 strcpy (q - 1, p + 3);
1996 p = q - 1;
1997 }
1998 }
1999 else
2000 /* We are dealing with leading repetitions of "/..", for example
2001 "/../..", which is the Mach super-root. */
2002 p += 3;
2003 }
2004 else
2005 {
2006 found_real_path = 1;
2007 ++p;
2008 }
2009 }
2010
2011 forget_cached_source_info ();
2012
2013 if (from_tty)
2014 pwd_command ((char *) 0, 1);
2015 }
2016 \f
2017 struct source_cleanup_lines_args {
2018 int old_line;
2019 char *old_file;
2020 char *old_pre_error;
2021 char *old_error_pre_print;
2022 };
2023
2024 static void
2025 source_cleanup_lines (args)
2026 PTR args;
2027 {
2028 struct source_cleanup_lines_args *p =
2029 (struct source_cleanup_lines_args *)args;
2030 source_line_number = p->old_line;
2031 source_file_name = p->old_file;
2032 source_pre_error = p->old_pre_error;
2033 error_pre_print = p->old_error_pre_print;
2034 }
2035
2036 /* ARGSUSED */
2037 void
2038 source_command (args, from_tty)
2039 char *args;
2040 int from_tty;
2041 {
2042 FILE *stream;
2043 struct cleanup *old_cleanups;
2044 char *file = args;
2045 struct source_cleanup_lines_args old_lines;
2046 int needed_length;
2047
2048 if (file == NULL)
2049 {
2050 error ("source command requires pathname of file to source.");
2051 }
2052
2053 file = tilde_expand (file);
2054 old_cleanups = make_cleanup (free, file);
2055
2056 stream = fopen (file, FOPEN_RT);
2057 if (stream == 0)
2058 perror_with_name (file);
2059
2060 make_cleanup (fclose, stream);
2061
2062 old_lines.old_line = source_line_number;
2063 old_lines.old_file = source_file_name;
2064 old_lines.old_pre_error = source_pre_error;
2065 old_lines.old_error_pre_print = error_pre_print;
2066 make_cleanup (source_cleanup_lines, &old_lines);
2067 source_line_number = 0;
2068 source_file_name = file;
2069 source_pre_error = error_pre_print == NULL ? "" : error_pre_print;
2070 source_pre_error = savestring (source_pre_error, strlen (source_pre_error));
2071 make_cleanup (free, source_pre_error);
2072 /* This will get set every time we read a line. So it won't stay "" for
2073 long. */
2074 error_pre_print = "";
2075
2076 needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80;
2077 if (source_error_allocated < needed_length)
2078 {
2079 source_error_allocated *= 2;
2080 if (source_error_allocated < needed_length)
2081 source_error_allocated = needed_length;
2082 if (source_error == NULL)
2083 source_error = xmalloc (source_error_allocated);
2084 else
2085 source_error = xrealloc (source_error, source_error_allocated);
2086 }
2087
2088 read_command_file (stream);
2089
2090 do_cleanups (old_cleanups);
2091 }
2092
2093 /* ARGSUSED */
2094 static void
2095 echo_command (text, from_tty)
2096 char *text;
2097 int from_tty;
2098 {
2099 char *p = text;
2100 register int c;
2101
2102 if (text)
2103 while ((c = *p++) != '\0')
2104 {
2105 if (c == '\\')
2106 {
2107 /* \ at end of argument is used after spaces
2108 so they won't be lost. */
2109 if (*p == 0)
2110 return;
2111
2112 c = parse_escape (&p);
2113 if (c >= 0)
2114 printf_filtered ("%c", c);
2115 }
2116 else
2117 printf_filtered ("%c", c);
2118 }
2119
2120 /* Force this output to appear now. */
2121 wrap_here ("");
2122 gdb_flush (gdb_stdout);
2123 }
2124
2125 \f
2126 /* Functions to manipulate command line editing control variables. */
2127
2128 /* Number of commands to print in each call to show_commands. */
2129 #define Hist_print 10
2130 static void
2131 show_commands (args, from_tty)
2132 char *args;
2133 int from_tty;
2134 {
2135 /* Index for history commands. Relative to history_base. */
2136 int offset;
2137
2138 /* Number of the history entry which we are planning to display next.
2139 Relative to history_base. */
2140 static int num = 0;
2141
2142 /* The first command in the history which doesn't exist (i.e. one more
2143 than the number of the last command). Relative to history_base. */
2144 int hist_len;
2145
2146 extern HIST_ENTRY *history_get PARAMS ((int));
2147
2148 /* Print out some of the commands from the command history. */
2149 /* First determine the length of the history list. */
2150 hist_len = history_size;
2151 for (offset = 0; offset < history_size; offset++)
2152 {
2153 if (!history_get (history_base + offset))
2154 {
2155 hist_len = offset;
2156 break;
2157 }
2158 }
2159
2160 if (args)
2161 {
2162 if (args[0] == '+' && args[1] == '\0')
2163 /* "info editing +" should print from the stored position. */
2164 ;
2165 else
2166 /* "info editing <exp>" should print around command number <exp>. */
2167 num = (parse_and_eval_address (args) - history_base) - Hist_print / 2;
2168 }
2169 /* "show commands" means print the last Hist_print commands. */
2170 else
2171 {
2172 num = hist_len - Hist_print;
2173 }
2174
2175 if (num < 0)
2176 num = 0;
2177
2178 /* If there are at least Hist_print commands, we want to display the last
2179 Hist_print rather than, say, the last 6. */
2180 if (hist_len - num < Hist_print)
2181 {
2182 num = hist_len - Hist_print;
2183 if (num < 0)
2184 num = 0;
2185 }
2186
2187 for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
2188 {
2189 printf_filtered ("%5d %s\n", history_base + offset,
2190 (history_get (history_base + offset))->line);
2191 }
2192
2193 /* The next command we want to display is the next one that we haven't
2194 displayed yet. */
2195 num += Hist_print;
2196
2197 /* If the user repeats this command with return, it should do what
2198 "show commands +" does. This is unnecessary if arg is null,
2199 because "show commands +" is not useful after "show commands". */
2200 if (from_tty && args)
2201 {
2202 args[0] = '+';
2203 args[1] = '\0';
2204 }
2205 }
2206
2207 /* Called by do_setshow_command. */
2208 /* ARGSUSED */
2209 static void
2210 set_history_size_command (args, from_tty, c)
2211 char *args;
2212 int from_tty;
2213 struct cmd_list_element *c;
2214 {
2215 if (history_size == INT_MAX)
2216 unstifle_history ();
2217 else if (history_size >= 0)
2218 stifle_history (history_size);
2219 else
2220 {
2221 history_size = INT_MAX;
2222 error ("History size must be non-negative");
2223 }
2224 }
2225
2226 /* ARGSUSED */
2227 static void
2228 set_history (args, from_tty)
2229 char *args;
2230 int from_tty;
2231 {
2232 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
2233 help_list (sethistlist, "set history ", -1, gdb_stdout);
2234 }
2235
2236 /* ARGSUSED */
2237 static void
2238 show_history (args, from_tty)
2239 char *args;
2240 int from_tty;
2241 {
2242 cmd_show_list (showhistlist, from_tty, "");
2243 }
2244
2245 int info_verbose = 0; /* Default verbose msgs off */
2246
2247 /* Called by do_setshow_command. An elaborate joke. */
2248 /* ARGSUSED */
2249 static void
2250 set_verbose (args, from_tty, c)
2251 char *args;
2252 int from_tty;
2253 struct cmd_list_element *c;
2254 {
2255 char *cmdname = "verbose";
2256 struct cmd_list_element *showcmd;
2257
2258 showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
2259
2260 if (info_verbose)
2261 {
2262 c->doc = "Set verbose printing of informational messages.";
2263 showcmd->doc = "Show verbose printing of informational messages.";
2264 }
2265 else
2266 {
2267 c->doc = "Set verbosity.";
2268 showcmd->doc = "Show verbosity.";
2269 }
2270 }
2271
2272 static void
2273 float_handler (signo)
2274 int signo;
2275 {
2276 /* This message is based on ANSI C, section 4.7. Note that integer
2277 divide by zero causes this, so "float" is a misnomer. */
2278 signal (SIGFPE, float_handler);
2279 error ("Erroneous arithmetic operation.");
2280 }
2281
2282 \f
2283 static void
2284 init_cmd_lists ()
2285 {
2286 cmdlist = NULL;
2287 infolist = NULL;
2288 enablelist = NULL;
2289 disablelist = NULL;
2290 deletelist = NULL;
2291 enablebreaklist = NULL;
2292 setlist = NULL;
2293 unsetlist = NULL;
2294 showlist = NULL;
2295 sethistlist = NULL;
2296 showhistlist = NULL;
2297 unsethistlist = NULL;
2298 #if MAINTENANCE_CMDS
2299 maintenancelist = NULL;
2300 maintenanceinfolist = NULL;
2301 maintenanceprintlist = NULL;
2302 #endif
2303 setprintlist = NULL;
2304 showprintlist = NULL;
2305 setchecklist = NULL;
2306 showchecklist = NULL;
2307 }
2308
2309 /* Init the history buffer. Note that we are called after the init file(s)
2310 * have been read so that the user can change the history file via his
2311 * .gdbinit file (for instance). The GDBHISTFILE environment variable
2312 * overrides all of this.
2313 */
2314
2315 void
2316 init_history()
2317 {
2318 char *tmpenv;
2319
2320 tmpenv = getenv ("HISTSIZE");
2321 if (tmpenv)
2322 history_size = atoi (tmpenv);
2323 else if (!history_size)
2324 history_size = 256;
2325
2326 stifle_history (history_size);
2327
2328 tmpenv = getenv ("GDBHISTFILE");
2329 if (tmpenv)
2330 history_filename = savestring (tmpenv, strlen(tmpenv));
2331 else if (!history_filename) {
2332 /* We include the current directory so that if the user changes
2333 directories the file written will be the same as the one
2334 that was read. */
2335 history_filename = concat (current_directory, "/.gdb_history", NULL);
2336 }
2337 read_history (history_filename);
2338 }
2339
2340 static void
2341 init_main ()
2342 {
2343 struct cmd_list_element *c;
2344
2345 #ifdef DEFAULT_PROMPT
2346 prompt = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT));
2347 #else
2348 prompt = savestring ("(gdb) ", 6);
2349 #endif
2350
2351 /* Set the important stuff up for command editing. */
2352 command_editing_p = 1;
2353 history_expansion_p = 0;
2354 write_history_p = 0;
2355
2356 /* Setup important stuff for command line editing. */
2357 rl_completion_entry_function = (int (*)()) symbol_completion_function;
2358 rl_completer_word_break_characters = gdb_completer_word_break_characters;
2359 rl_completer_quote_characters = gdb_completer_quote_characters;
2360 rl_readline_name = "gdb";
2361
2362 /* Define the classes of commands.
2363 They will appear in the help list in the reverse of this order. */
2364
2365 add_cmd ("internals", class_maintenance, NO_FUNCTION,
2366 "Maintenance commands.\n\
2367 Some gdb commands are provided just for use by gdb maintainers.\n\
2368 These commands are subject to frequent change, and may not be as\n\
2369 well documented as user commands.",
2370 &cmdlist);
2371 add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist);
2372 add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist);
2373 add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\
2374 The commands in this class are those defined by the user.\n\
2375 Use the \"define\" command to define a command.", &cmdlist);
2376 add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist);
2377 add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist);
2378 add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist);
2379 add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist);
2380 add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist);
2381 add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\
2382 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
2383 counting from zero for the innermost (currently executing) frame.\n\n\
2384 At any time gdb identifies one frame as the \"selected\" frame.\n\
2385 Variable lookups are done with respect to the selected frame.\n\
2386 When the program being debugged stops, gdb selects the innermost frame.\n\
2387 The commands below can be used to select other frames by number or address.",
2388 &cmdlist);
2389 add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist);
2390
2391 add_com ("pwd", class_files, pwd_command,
2392 "Print working directory. This is used for your program as well.");
2393 c = add_cmd ("cd", class_files, cd_command,
2394 "Set working directory to DIR for debugger and program being debugged.\n\
2395 The change does not take effect for the program being debugged\n\
2396 until the next time it is started.", &cmdlist);
2397 c->completer = filename_completer;
2398
2399 add_show_from_set
2400 (add_set_cmd ("prompt", class_support, var_string, (char *)&prompt,
2401 "Set gdb's prompt",
2402 &setlist),
2403 &showlist);
2404
2405 add_com ("echo", class_support, echo_command,
2406 "Print a constant string. Give string as argument.\n\
2407 C escape sequences may be used in the argument.\n\
2408 No newline is added at the end of the argument;\n\
2409 use \"\\n\" if you want a newline to be printed.\n\
2410 Since leading and trailing whitespace are ignored in command arguments,\n\
2411 if you want to print some you must use \"\\\" before leading whitespace\n\
2412 to be printed or after trailing whitespace.");
2413 add_com ("document", class_support, document_command,
2414 "Document a user-defined command.\n\
2415 Give command name as argument. Give documentation on following lines.\n\
2416 End with a line of just \"end\".");
2417 add_com ("define", class_support, define_command,
2418 "Define a new command name. Command name is argument.\n\
2419 Definition appears on following lines, one command per line.\n\
2420 End with a line of just \"end\".\n\
2421 Use the \"document\" command to give documentation for the new command.\n\
2422 Commands defined in this way do not take arguments.");
2423
2424 #ifdef __STDC__
2425 c = add_cmd ("source", class_support, source_command,
2426 "Read commands from a file named FILE.\n\
2427 Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\
2428 when gdb is started.", &cmdlist);
2429 #else
2430 /* Punt file name, we can't help it easily. */
2431 c = add_cmd ("source", class_support, source_command,
2432 "Read commands from a file named FILE.\n\
2433 Note that the file \".gdbinit\" is read automatically in this way\n\
2434 when gdb is started.", &cmdlist);
2435 #endif
2436 c->completer = filename_completer;
2437
2438 add_com ("quit", class_support, quit_command, "Exit gdb.");
2439 add_com ("help", class_support, help_command, "Print list of commands.");
2440 add_com_alias ("q", "quit", class_support, 1);
2441 add_com_alias ("h", "help", class_support, 1);
2442
2443
2444 c = add_set_cmd ("verbose", class_support, var_boolean, (char *)&info_verbose,
2445 "Set ",
2446 &setlist),
2447 add_show_from_set (c, &showlist);
2448 c->function.sfunc = set_verbose;
2449 set_verbose (NULL, 0, c);
2450
2451 add_show_from_set
2452 (add_set_cmd ("editing", class_support, var_boolean, (char *)&command_editing_p,
2453 "Set editing of command lines as they are typed.\n\
2454 Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
2455 Without an argument, command line editing is enabled. To edit, use\n\
2456 EMACS-like or VI-like commands like control-P or ESC.", &setlist),
2457 &showlist);
2458
2459 add_prefix_cmd ("history", class_support, set_history,
2460 "Generic command for setting command history parameters.",
2461 &sethistlist, "set history ", 0, &setlist);
2462 add_prefix_cmd ("history", class_support, show_history,
2463 "Generic command for showing command history parameters.",
2464 &showhistlist, "show history ", 0, &showlist);
2465
2466 add_show_from_set
2467 (add_set_cmd ("expansion", no_class, var_boolean, (char *)&history_expansion_p,
2468 "Set history expansion on command input.\n\
2469 Without an argument, history expansion is enabled.", &sethistlist),
2470 &showhistlist);
2471
2472 add_show_from_set
2473 (add_set_cmd ("save", no_class, var_boolean, (char *)&write_history_p,
2474 "Set saving of the history record on exit.\n\
2475 Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
2476 Without an argument, saving is enabled.", &sethistlist),
2477 &showhistlist);
2478
2479 c = add_set_cmd ("size", no_class, var_integer, (char *)&history_size,
2480 "Set the size of the command history, \n\
2481 ie. the number of previous commands to keep a record of.", &sethistlist);
2482 add_show_from_set (c, &showhistlist);
2483 c->function.sfunc = set_history_size_command;
2484
2485 add_show_from_set
2486 (add_set_cmd ("filename", no_class, var_filename, (char *)&history_filename,
2487 "Set the filename in which to record the command history\n\
2488 (the list of previous commands of which a record is kept).", &sethistlist),
2489 &showhistlist);
2490
2491 add_show_from_set
2492 (add_set_cmd ("confirm", class_support, var_boolean,
2493 (char *)&caution,
2494 "Set whether to confirm potentially dangerous operations.",
2495 &setlist),
2496 &showlist);
2497
2498 add_prefix_cmd ("info", class_info, info_command,
2499 "Generic command for showing things about the program being debugged.",
2500 &infolist, "info ", 0, &cmdlist);
2501 add_com_alias ("i", "info", class_info, 1);
2502
2503 add_com ("complete", class_obscure, complete_command,
2504 "List the completions for the rest of the line as a command.");
2505
2506 add_prefix_cmd ("show", class_info, show_command,
2507 "Generic command for showing things about the debugger.",
2508 &showlist, "show ", 0, &cmdlist);
2509 /* Another way to get at the same thing. */
2510 add_info ("set", show_command, "Show all GDB settings.");
2511
2512 add_cmd ("commands", no_class, show_commands,
2513 "Show the the history of commands you typed.\n\
2514 You can supply a command number to start with, or a `+' to start after\n\
2515 the previous command number shown.",
2516 &showlist);
2517
2518 add_cmd ("version", no_class, show_version,
2519 "Show what version of GDB this is.", &showlist);
2520
2521 /* If target is open when baud changes, it doesn't take effect until the
2522 next open (I think, not sure). */
2523 add_show_from_set (add_set_cmd ("remotebaud", no_class,
2524 var_zinteger, (char *)&baud_rate,
2525 "Set baud rate for remote serial I/O.\n\
2526 This value is used to set the speed of the serial port when debugging\n\
2527 using remote targets.", &setlist),
2528 &showlist);
2529
2530 add_show_from_set (
2531 add_set_cmd ("remotedebug", no_class, var_zinteger, (char *)&remote_debug,
2532 "Set debugging of remote protocol.\n\
2533 When enabled, each packet sent or received with the remote target\n\
2534 is displayed.", &setlist),
2535 &showlist);
2536 }