One more time...
[deliverable/binutils-gdb.git] / gdb / main.c
1 /* Top level `main' program for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "defs.h"
21 #include "gdbcmd.h"
22 #include "symtab.h"
23 #include "inferior.h"
24 #include "signals.h"
25 #include "target.h"
26 #include "breakpoint.h"
27 #include "gdbtypes.h"
28 #include "expression.h"
29 #include "language.h"
30
31 #include "getopt.h"
32
33 /* readline include files */
34 #include "readline.h"
35 #include "history.h"
36
37 /* readline defines this. */
38 #undef savestring
39
40 #ifdef USG
41 #include <sys/types.h>
42 #include <unistd.h>
43 #endif
44
45 #include <string.h>
46 #ifndef NO_SYS_FILE
47 #include <sys/file.h>
48 #endif
49 #include <setjmp.h>
50 #include <sys/param.h>
51 #include <sys/stat.h>
52 #include <ctype.h>
53
54 #ifdef SET_STACK_LIMIT_HUGE
55 #include <sys/time.h>
56 #include <sys/resource.h>
57
58 int original_stack_limit;
59 #endif
60
61 /* Prototypes for local functions */
62
63 static char *
64 symbol_completion_function PARAMS ((char *, int));
65
66 static void
67 command_loop PARAMS ((void));
68
69 static void
70 command_loop_marker PARAMS ((int));
71
72 static void
73 print_gdb_version PARAMS ((void));
74
75 static void
76 quit_command PARAMS ((char *, int));
77
78 static void
79 initialize_main PARAMS ((void));
80
81 static void
82 initialize_history PARAMS ((void));
83
84 static void
85 initialize_cmd_lists PARAMS ((void));
86
87 static void
88 float_handler PARAMS ((int));
89
90 static void
91 source_command PARAMS ((char *, int));
92
93 static void
94 cd_command PARAMS ((char *, int));
95
96 static void
97 print_gnu_advertisement PARAMS ((void));
98
99 static void
100 init_signals PARAMS ((void));
101
102 static void
103 read_command_file PARAMS ((FILE *));
104
105 static void
106 set_verbose PARAMS ((char *, int, struct cmd_list_element *));
107
108 static void
109 show_history PARAMS ((char *, int));
110
111 static void
112 set_history PARAMS ((char *, int));
113
114 static void
115 set_history_size_command PARAMS ((char *, int, struct cmd_list_element *));
116
117 static void
118 show_commands PARAMS ((char *, int));
119
120 static void
121 dump_me_command PARAMS ((char *, int));
122
123 static void
124 echo_command PARAMS ((char *, int));
125
126 static void
127 pwd_command PARAMS ((char *, int));
128
129 static void
130 show_version PARAMS ((char *, int));
131
132 static void
133 document_command PARAMS ((char *, int));
134
135 static void
136 define_command PARAMS ((char *, int));
137
138 static void
139 validate_comname PARAMS ((char *));
140
141 static void
142 help_command PARAMS ((char *, int));
143
144 static void
145 show_command PARAMS ((char *, int));
146
147 static void
148 info_command PARAMS ((char *, int));
149
150 static void
151 do_nothing PARAMS ((int));
152
153 static void
154 disconnect PARAMS ((int));
155
156 static void
157 source_cleanup PARAMS ((FILE *));
158
159 /* If this definition isn't overridden by the header files, assume
160 that isatty and fileno exist on this system. */
161 #ifndef ISATTY
162 #define ISATTY(FP) (isatty (fileno (FP)))
163 #endif
164
165 /* Initialization file name for gdb. This is overridden in some configs. */
166
167 #ifndef GDBINIT_FILENAME
168 #define GDBINIT_FILENAME ".gdbinit"
169 #endif
170 char gdbinit[] = GDBINIT_FILENAME;
171
172 #define ALL_CLEANUPS ((struct cleanup *)0)
173
174 /* Version number of GDB, as a string. */
175
176 extern char *version;
177
178 /* Message to be printed before the error message, when an error occurs. */
179
180 extern char *error_pre_print;
181
182 /* Message to be printed before the warning message, when a warning occurs. */
183
184 extern char *warning_pre_print;
185
186 extern char lang_frame_mismatch_warn[]; /* language.c */
187
188 /* Whether GDB's stdin is on a terminal. */
189
190 extern int gdb_has_a_terminal; /* inflow.c */
191
192 /* Flag for whether we want all the "from_tty" gubbish printed. */
193
194 int caution = 1; /* Default is yes, sigh. */
195
196 /*
197 * Define all cmd_list_element's
198 */
199
200 /* Chain containing all defined commands. */
201
202 struct cmd_list_element *cmdlist;
203
204 /* Chain containing all defined info subcommands. */
205
206 struct cmd_list_element *infolist;
207
208 /* Chain containing all defined enable subcommands. */
209
210 struct cmd_list_element *enablelist;
211
212 /* Chain containing all defined disable subcommands. */
213
214 struct cmd_list_element *disablelist;
215
216 /* Chain containing all defined delete subcommands. */
217
218 struct cmd_list_element *deletelist;
219
220 /* Chain containing all defined "enable breakpoint" subcommands. */
221
222 struct cmd_list_element *enablebreaklist;
223
224 /* Chain containing all defined set subcommands */
225
226 struct cmd_list_element *setlist;
227
228 /* Chain containing all defined show subcommands. */
229 struct cmd_list_element *showlist;
230
231 /* Chain containing all defined \"set history\". */
232
233 struct cmd_list_element *sethistlist;
234
235 /* Chain containing all defined \"show history\". */
236 struct cmd_list_element *showhistlist;
237
238 /* Chain containing all defined \"unset history\". */
239
240 struct cmd_list_element *unsethistlist;
241
242 /* stdio stream that command input is being read from. */
243
244 FILE *instream;
245
246 /* Current working directory. */
247
248 char *current_directory;
249
250 /* The directory name is actually stored here (usually). */
251 static char dirbuf[1024];
252
253 /* Function to call before reading a command, if nonzero.
254 The function receives two args: an input stream,
255 and a prompt string. */
256
257 void (*window_hook) PARAMS ((FILE *, char *));
258
259 extern int frame_file_full_name;
260 extern int mapped_symbol_files;
261 extern int readnow_symbol_files;
262
263 int epoch_interface;
264 int xgdb_verbose;
265
266 /* gdb prints this when reading a command interactively */
267 static char *prompt;
268
269 /* Buffer used for reading command lines, and the size
270 allocated for it so far. */
271
272 char *line;
273 int linesize = 100;
274
275 /* Baud rate specified for talking to serial target systems. Default
276 is left as a zero pointer, so targets can choose their own defaults. */
277
278 char *baud_rate;
279
280 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
281
282 #ifndef STOP_SIGNAL
283 #ifdef SIGTSTP
284 #define STOP_SIGNAL SIGTSTP
285 static void stop_sig PARAMS ((int));
286 #endif
287 #endif
288
289 /* Some System V have job control but not sigsetmask(). */
290 #if !defined (HAVE_SIGSETMASK)
291 #define HAVE_SIGSETMASK !defined (USG)
292 #endif
293
294 #if 0 == (HAVE_SIGSETMASK)
295 #define sigsetmask(n)
296 #endif
297 \f
298 /* This is how `error' returns to command level. */
299
300 jmp_buf to_top_level;
301
302 NORETURN void
303 return_to_top_level ()
304 {
305 quit_flag = 0;
306 immediate_quit = 0;
307 bpstat_clear_actions(stop_bpstat); /* Clear queued breakpoint commands */
308 disable_current_display ();
309 do_cleanups (ALL_CLEANUPS);
310 (NORETURN void) longjmp (to_top_level, 1);
311 }
312
313 /* Call FUNC with arg ARGS, catching any errors.
314 If there is no error, return the value returned by FUNC.
315 If there is an error, print ERRSTRING, print the specific error message,
316 then return zero. */
317
318 int
319 catch_errors (func, args, errstring)
320 int (*func) PARAMS ((char *));
321 char *args;
322 char *errstring;
323 {
324 jmp_buf saved;
325 int val;
326 struct cleanup *saved_cleanup_chain;
327 char *saved_error_pre_print;
328
329 saved_cleanup_chain = save_cleanups ();
330 saved_error_pre_print = error_pre_print;
331
332 memcpy ((char *)saved, (char *)to_top_level, sizeof (jmp_buf));
333 error_pre_print = errstring;
334
335 if (setjmp (to_top_level) == 0)
336 val = (*func) (args);
337 else
338 val = 0;
339
340 restore_cleanups (saved_cleanup_chain);
341
342 error_pre_print = saved_error_pre_print;
343 memcpy ((char *)to_top_level, (char *)saved, sizeof (jmp_buf));
344 return val;
345 }
346
347 /* Handler for SIGHUP. */
348
349 static void
350 disconnect (signo)
351 int signo;
352 {
353 kill_inferior_fast ();
354 signal (signo, SIG_DFL);
355 kill (getpid (), SIGHUP);
356 }
357 \f
358 /* Clean up on error during a "source" command (or execution of a
359 user-defined command). */
360
361 static void
362 source_cleanup (stream)
363 FILE *stream;
364 {
365 /* Restore the previous input stream. */
366 instream = stream;
367 }
368
369 /* Read commands from STREAM. */
370 static void
371 read_command_file (stream)
372 FILE *stream;
373 {
374 struct cleanup *cleanups;
375
376 cleanups = make_cleanup (source_cleanup, instream);
377 instream = stream;
378 command_loop ();
379 do_cleanups (cleanups);
380 }
381 \f
382 int
383 main (argc, argv)
384 int argc;
385 char **argv;
386 {
387 int count;
388 static int inhibit_gdbinit = 0;
389 static int quiet = 0;
390 static int batch = 0;
391
392 /* Pointers to various arguments from command line. */
393 char *symarg = NULL;
394 char *execarg = NULL;
395 char *corearg = NULL;
396 char *cdarg = NULL;
397 char *ttyarg = NULL;
398
399 /* Pointers to all arguments of +command option. */
400 char **cmdarg;
401 /* Allocated size of cmdarg. */
402 int cmdsize;
403 /* Number of elements of cmdarg used. */
404 int ncmd;
405
406 /* Indices of all arguments of +directory option. */
407 char **dirarg;
408 /* Allocated size. */
409 int dirsize;
410 /* Number of elements used. */
411 int ndir;
412
413 register int i;
414
415 /* This needs to happen before the first use of malloc. */
416 init_malloc ((PTR) NULL);
417
418 #if defined (ALIGN_STACK_ON_STARTUP)
419 i = (int) &count & 0x3;
420 if (i != 0)
421 alloca (4 - i);
422 #endif
423
424 /* If error() is called from initialization code, just exit */
425 if (setjmp (to_top_level)) {
426 exit(1);
427 }
428
429 cmdsize = 1;
430 cmdarg = (char **) xmalloc (cmdsize * sizeof (*cmdarg));
431 ncmd = 0;
432 dirsize = 1;
433 dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
434 ndir = 0;
435
436 quit_flag = 0;
437 line = (char *) xmalloc (linesize);
438 line[0] = '\0'; /* Terminate saved (now empty) cmd line */
439 instream = stdin;
440
441 getcwd (dirbuf, sizeof (dirbuf));
442 current_directory = dirbuf;
443
444 #ifdef SET_STACK_LIMIT_HUGE
445 {
446 struct rlimit rlim;
447
448 /* Set the stack limit huge so that alloca (particularly stringtab
449 * in dbxread.c) does not fail. */
450 getrlimit (RLIMIT_STACK, &rlim);
451 original_stack_limit = rlim.rlim_cur;
452 rlim.rlim_cur = rlim.rlim_max;
453 setrlimit (RLIMIT_STACK, &rlim);
454 }
455 #endif /* SET_STACK_LIMIT_HUGE */
456
457 /* Parse arguments and options. */
458 {
459 int c;
460 static int print_help;
461 /* When var field is 0, use flag field to record the equivalent
462 short option (or arbitrary numbers starting at 10 for those
463 with no equivalent). */
464 static struct option long_options[] =
465 {
466 {"readnow", no_argument, &readnow_symbol_files, 1},
467 {"r", no_argument, &readnow_symbol_files, 1},
468 {"mapped", no_argument, &mapped_symbol_files, 1},
469 {"m", no_argument, &mapped_symbol_files, 1},
470 {"quiet", no_argument, &quiet, 1},
471 {"q", no_argument, &quiet, 1},
472 {"nx", no_argument, &inhibit_gdbinit, 1},
473 {"n", no_argument, &inhibit_gdbinit, 1},
474 {"batch", no_argument, &batch, 1},
475 {"epoch", no_argument, &epoch_interface, 1},
476 {"fullname", no_argument, &frame_file_full_name, 1},
477 {"f", no_argument, &frame_file_full_name, 1},
478 {"help", no_argument, &print_help, 1},
479 {"se", required_argument, 0, 10},
480 {"symbols", required_argument, 0, 's'},
481 {"s", required_argument, 0, 's'},
482 {"exec", required_argument, 0, 'e'},
483 {"e", required_argument, 0, 'e'},
484 {"core", required_argument, 0, 'c'},
485 {"c", required_argument, 0, 'c'},
486 {"command", required_argument, 0, 'x'},
487 {"x", required_argument, 0, 'x'},
488 {"directory", required_argument, 0, 'd'},
489 {"cd", required_argument, 0, 11},
490 {"tty", required_argument, 0, 't'},
491 {"baud", required_argument, 0, 'b'},
492 {"b", required_argument, 0, 'b'},
493 /* Allow machine descriptions to add more options... */
494 #ifdef ADDITIONAL_OPTIONS
495 ADDITIONAL_OPTIONS
496 #endif
497 {0, no_argument, 0, 0},
498 };
499
500 while (1)
501 {
502 int option_index;
503
504 c = getopt_long_only (argc, argv, "",
505 long_options, &option_index);
506 if (c == EOF)
507 break;
508
509 /* Long option that takes an argument. */
510 if (c == 0 && long_options[option_index].flag == 0)
511 c = long_options[option_index].val;
512
513 switch (c)
514 {
515 case 0:
516 /* Long option that just sets a flag. */
517 break;
518 case 10:
519 symarg = optarg;
520 execarg = optarg;
521 break;
522 case 11:
523 cdarg = optarg;
524 break;
525 case 's':
526 symarg = optarg;
527 break;
528 case 'e':
529 execarg = optarg;
530 break;
531 case 'c':
532 corearg = optarg;
533 break;
534 case 'x':
535 cmdarg[ncmd++] = optarg;
536 if (ncmd >= cmdsize)
537 {
538 cmdsize *= 2;
539 cmdarg = (char **) xrealloc ((char *)cmdarg,
540 cmdsize * sizeof (*cmdarg));
541 }
542 break;
543 case 'd':
544 dirarg[ndir++] = optarg;
545 if (ndir >= dirsize)
546 {
547 dirsize *= 2;
548 dirarg = (char **) xrealloc ((char *)dirarg,
549 dirsize * sizeof (*dirarg));
550 }
551 break;
552 case 't':
553 ttyarg = optarg;
554 break;
555 case 'q':
556 quiet = 1;
557 break;
558 case 'b':
559 baud_rate = optarg;
560 break;
561 #ifdef ADDITIONAL_OPTION_CASES
562 ADDITIONAL_OPTION_CASES
563 #endif
564 case '?':
565 fprintf (stderr,
566 "Use `%s +help' for a complete list of options.\n",
567 argv[0]);
568 exit (1);
569 }
570
571 }
572 if (print_help)
573 {
574 fputs ("\
575 This is GDB, the GNU debugger. Use the command\n\
576 gdb [options] [executable [core-file]]\n\
577 to enter the debugger.\n\
578 \n\
579 Options available are:\n\
580 -help Print this message.\n\
581 -quiet Do not print version number on startup.\n\
582 -fullname Output information used by emacs-GDB interface.\n\
583 -epoch Output information used by epoch emacs-GDB interface.\n\
584 -batch Exit after processing options.\n\
585 -nx Do not read .gdbinit file.\n\
586 -tty=TTY Use TTY for input/output by the program being debugged.\n\
587 -cd=DIR Change current directory to DIR.\n\
588 -directory=DIR Search for source files in DIR.\n\
589 -command=FILE Execute GDB commands from FILE.\n\
590 -symbols=SYMFILE Read symbols from SYMFILE.\n\
591 -exec=EXECFILE Use EXECFILE as the executable.\n\
592 -se=FILE Use FILE as symbol file and executable file.\n\
593 -core=COREFILE Analyze the core dump COREFILE.\n\
594 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
595 -mapped Use mapped symbol files if supported on this system.\n\
596 -readnow Fully read symbol files on first access.\n\
597 ", stderr);
598 #ifdef ADDITIONAL_OPTION_HELP
599 fputs (ADDITIONAL_OPTION_HELP, stderr);
600 #endif
601 fputs ("\n\
602 For more information, type \"help\" from within GDB, or consult the\n\
603 GDB manual (available as on-line info or a printed manual).\n", stderr);
604 /* Exiting after printing this message seems like
605 the most useful thing to do. */
606 exit (0);
607 }
608
609 /* OK, that's all the options. The other arguments are filenames. */
610 count = 0;
611 for (; optind < argc; optind++)
612 switch (++count)
613 {
614 case 1:
615 symarg = argv[optind];
616 execarg = argv[optind];
617 break;
618 case 2:
619 corearg = argv[optind];
620 break;
621 case 3:
622 fprintf (stderr,
623 "Excess command line arguments ignored. (%s%s)\n",
624 argv[optind], (optind == argc - 1) ? "" : " ...");
625 break;
626 }
627 if (batch)
628 quiet = 1;
629 }
630
631 /* Run the init function of each source file */
632
633 initialize_cmd_lists (); /* This needs to be done first */
634 initialize_all_files ();
635 initialize_main (); /* But that omits this file! Do it now */
636 init_signals ();
637
638 if (!quiet)
639 {
640 /* Print all the junk at the top, with trailing "..." if we are about
641 to read a symbol file (possibly slowly). */
642 print_gnu_advertisement ();
643 print_gdb_version ();
644 if (symarg)
645 printf_filtered ("..");
646 wrap_here("");
647 fflush (stdout); /* Force to screen during slow operations */
648 }
649
650 error_pre_print = "\n\n";
651 warning_pre_print = "\n\nwarning: ";
652
653 /* Now perform all the actions indicated by the arguments. */
654 if (cdarg != NULL)
655 {
656 if (!setjmp (to_top_level))
657 {
658 cd_command (cdarg, 0);
659 init_source_path ();
660 }
661 }
662 do_cleanups (ALL_CLEANUPS);
663
664 for (i = 0; i < ndir; i++)
665 if (!setjmp (to_top_level))
666 directory_command (dirarg[i], 0);
667 free (dirarg);
668 do_cleanups (ALL_CLEANUPS);
669
670 if (execarg != NULL
671 && symarg != NULL
672 && strcmp (execarg, symarg) == 0)
673 {
674 /* The exec file and the symbol-file are the same. If we can't open
675 it, better only print one error message. */
676 if (!setjmp (to_top_level))
677 {
678 exec_file_command (execarg, !batch);
679 symbol_file_command (symarg, 0);
680 }
681 }
682 else
683 {
684 if (execarg != NULL)
685 if (!setjmp (to_top_level))
686 exec_file_command (execarg, !batch);
687 if (symarg != NULL)
688 if (!setjmp (to_top_level))
689 symbol_file_command (symarg, 0);
690 }
691 do_cleanups (ALL_CLEANUPS);
692
693 /* After the symbol file has been read, print a newline to get us
694 beyond the copyright line... But errors should still set off
695 the error message with a (single) blank line. */
696 if (!quiet)
697 printf_filtered ("\n");
698 error_pre_print = "\n";
699 warning_pre_print = "\nwarning: ";
700
701 /* Set the initial language. */
702 {
703 struct partial_symtab *pst = find_main_psymtab ();
704 enum language lang = language_unknown;
705 if (pst == NULL) ;
706 #if 0
707 /* A better solution would set the language when reading the psymtab.
708 This would win for symbol file formats that encode the langauge,
709 such as DWARF. But, we don't do that yet. FIXME */
710 else if (pst->language != language_unknown)
711 lang = pst->language;
712 #endif
713 else if (pst->filename != NULL)
714 lang = deduce_language_from_filename (pst->filename);
715 if (lang == language_unknown) /* Make C the default language */
716 lang = language_c;
717 set_language (lang);
718 }
719
720 if (corearg != NULL)
721 if (!setjmp (to_top_level))
722 core_file_command (corearg, !batch);
723 else if (isdigit (corearg[0]) && !setjmp (to_top_level))
724 attach_command (corearg, !batch);
725 do_cleanups (ALL_CLEANUPS);
726
727 if (ttyarg != NULL)
728 if (!setjmp (to_top_level))
729 tty_command (ttyarg, !batch);
730 do_cleanups (ALL_CLEANUPS);
731
732 #ifdef ADDITIONAL_OPTION_HANDLER
733 ADDITIONAL_OPTION_HANDLER;
734 #endif
735
736 /* Error messages should no longer be distinguished with extra output. */
737 error_pre_print = 0;
738 warning_pre_print = "warning: ";
739
740 {
741 struct stat homebuf, cwdbuf;
742 char *homedir, *homeinit;
743
744 /* Read init file, if it exists in home directory */
745 homedir = getenv ("HOME");
746 if (homedir)
747 {
748 homeinit = (char *) alloca (strlen (getenv ("HOME")) +
749 strlen (gdbinit) + 10);
750 strcpy (homeinit, getenv ("HOME"));
751 strcat (homeinit, "/");
752 strcat (homeinit, gdbinit);
753 if (!inhibit_gdbinit && access (homeinit, R_OK) == 0)
754 if (!setjmp (to_top_level))
755 source_command (homeinit, 0);
756 do_cleanups (ALL_CLEANUPS);
757
758 /* Do stats; no need to do them elsewhere since we'll only
759 need them if homedir is set. Make sure that they are
760 zero in case one of them fails (this guarantees that they
761 won't match if either exists). */
762
763 memset (&homebuf, 0, sizeof (struct stat));
764 memset (&cwdbuf, 0, sizeof (struct stat));
765
766 stat (homeinit, &homebuf);
767 stat (gdbinit, &cwdbuf); /* We'll only need this if
768 homedir was set. */
769 }
770
771 /* Read the input file in the current directory, *if* it isn't
772 the same file (it should exist, also). */
773
774 if (!homedir
775 || bcmp ((char *) &homebuf,
776 (char *) &cwdbuf,
777 sizeof (struct stat)))
778 if (!inhibit_gdbinit && access (gdbinit, R_OK) == 0)
779 if (!setjmp (to_top_level))
780 source_command (gdbinit, 0);
781 do_cleanups (ALL_CLEANUPS);
782 }
783
784 for (i = 0; i < ncmd; i++)
785 if (!setjmp (to_top_level))
786 {
787 if (cmdarg[i][0] == '-' && cmdarg[i][1] == '\0')
788 read_command_file (stdin);
789 else
790 source_command (cmdarg[i], !batch);
791 do_cleanups (ALL_CLEANUPS);
792 }
793 free (cmdarg);
794
795 /* Read in the old history after all the command files have been read. */
796 initialize_history();
797
798 if (batch)
799 {
800 /* We have hit the end of the batch file. */
801 exit (0);
802 }
803
804 /* Do any host- or target-specific hacks. This is used for i960 targets
805 to force the user to set a nindy target and spec its parameters. */
806
807 #ifdef BEFORE_MAIN_LOOP_HOOK
808 BEFORE_MAIN_LOOP_HOOK;
809 #endif
810
811 /* The command loop. */
812
813 while (1)
814 {
815 if (!setjmp (to_top_level))
816 {
817 do_cleanups (ALL_CLEANUPS); /* Do complete cleanup */
818 command_loop ();
819 quit_command ((char *)0, instream == stdin);
820 }
821 }
822 /* No exit -- exit is through quit_command. */
823 }
824
825 /* Execute the line P as a command.
826 Pass FROM_TTY as second argument to the defining function. */
827
828 void
829 execute_command (p, from_tty)
830 char *p;
831 int from_tty;
832 {
833 register struct cmd_list_element *c;
834 register struct command_line *cmdlines;
835 register enum language flang;
836 static const struct language_defn *saved_language = 0;
837 static int warned = 0;
838
839 free_all_values ();
840
841 /* This can happen when command_line_input hits end of file. */
842 if (p == NULL)
843 return;
844
845 while (*p == ' ' || *p == '\t') p++;
846 if (*p)
847 {
848 char *arg;
849
850 c = lookup_cmd (&p, cmdlist, "", 0, 1);
851 /* Pass null arg rather than an empty one. */
852 arg = *p ? p : 0;
853 if (c->class == class_user)
854 {
855 struct cleanup *old_chain;
856
857 if (*p)
858 error ("User-defined commands cannot take arguments.");
859 cmdlines = c->user_commands;
860 if (cmdlines == 0)
861 /* Null command */
862 return;
863
864 /* Set the instream to 0, indicating execution of a
865 user-defined function. */
866 old_chain = make_cleanup (source_cleanup, instream);
867 instream = (FILE *) 0;
868 while (cmdlines)
869 {
870 execute_command (cmdlines->line, 0);
871 cmdlines = cmdlines->next;
872 }
873 do_cleanups (old_chain);
874 }
875 else if (c->type == set_cmd || c->type == show_cmd)
876 do_setshow_command (arg, from_tty & caution, c);
877 else if (c->function.cfunc == NO_FUNCTION)
878 error ("That is not a command, just a help topic.");
879 else
880 (*c->function.cfunc) (arg, from_tty & caution);
881 }
882
883 /* Tell the user if the language has changed (except first time). */
884 if (current_language != saved_language)
885 {
886 if (language_mode == language_mode_auto) {
887 if (saved_language)
888 language_info (1); /* Print what changed. */
889 }
890 saved_language = current_language;
891 warned = 0;
892 }
893
894 /* Warn the user if the working language does not match the
895 language of the current frame. Only warn the user if we are
896 actually running the program, i.e. there is a stack. */
897 /* FIXME: This should be cacheing the frame and only running when
898 the frame changes. */
899 if (target_has_stack)
900 {
901 flang = get_frame_language ();
902 if (!warned
903 && flang != language_unknown
904 && flang != current_language->la_language)
905 {
906 printf_filtered ("%s\n", lang_frame_mismatch_warn);
907 warned = 1;
908 }
909 }
910 }
911
912 /* ARGSUSED */
913 static void
914 command_loop_marker (foo)
915 int foo;
916 {
917 }
918
919 /* Read commands from `instream' and execute them
920 until end of file or error reading instream. */
921 static void
922 command_loop ()
923 {
924 struct cleanup *old_chain;
925 char *command;
926 int stdin_is_tty = ISATTY (stdin);
927
928 while (!feof (instream))
929 {
930 if (window_hook && instream == stdin)
931 (*window_hook) (instream, prompt);
932
933 quit_flag = 0;
934 if (instream == stdin && stdin_is_tty)
935 reinitialize_more_filter ();
936 old_chain = make_cleanup (command_loop_marker, 0);
937 command = command_line_input (instream == stdin ? prompt : (char *) NULL,
938 instream == stdin);
939 if (command == 0)
940 return;
941 execute_command (command, instream == stdin);
942 /* Do any commands attached to breakpoint we stopped at. */
943 bpstat_do_actions (&stop_bpstat);
944 do_cleanups (old_chain);
945 }
946 }
947 \f
948 /* Commands call this if they do not want to be repeated by null lines. */
949
950 void
951 dont_repeat ()
952 {
953 /* If we aren't reading from standard input, we are saving the last
954 thing read from stdin in line and don't want to delete it. Null lines
955 won't repeat here in any case. */
956 if (instream == stdin)
957 *line = 0;
958 }
959 \f
960 /* Read a line from the stream "instream" without command line editing.
961
962 It prints PRROMPT once at the start.
963 Action is compatible with "readline", e.g. space for the result is
964 malloc'd and should be freed by the caller.
965
966 A NULL return means end of file. */
967 char *
968 gdb_readline (prrompt)
969 char *prrompt;
970 {
971 int c;
972 char *result;
973 int input_index = 0;
974 int result_size = 80;
975
976 if (prrompt)
977 {
978 printf (prrompt);
979 fflush (stdout);
980 }
981
982 result = (char *) xmalloc (result_size);
983
984 while (1)
985 {
986 /* Read from stdin if we are executing a user defined command.
987 This is the right thing for prompt_for_continue, at least. */
988 c = fgetc (instream ? instream : stdin);
989
990 if (c == EOF)
991 {
992 free (result);
993 return NULL;
994 }
995
996 if (c == '\n')
997 break;
998
999 result[input_index++] = c;
1000 while (input_index >= result_size)
1001 {
1002 result_size *= 2;
1003 result = (char *) xrealloc (result, result_size);
1004 }
1005 }
1006
1007 result[input_index++] = '\0';
1008 return result;
1009 }
1010
1011 /* Variables which control command line editing and history
1012 substitution. These variables are given default values at the end
1013 of this file. */
1014 static int command_editing_p;
1015 static int history_expansion_p;
1016 static int write_history_p;
1017 static int history_size;
1018 static char *history_filename;
1019
1020 /* Variables which are necessary for fancy command line editing. */
1021 char *gdb_completer_word_break_characters =
1022 " \t\n!@#$%^&*()-+=|~`}{[]\"';:?/>.<,";
1023
1024 /* Functions that are used as part of the fancy command line editing. */
1025
1026 /* This can be used for functions which don't want to complete on symbols
1027 but don't want to complete on anything else either. */
1028 /* ARGSUSED */
1029 char **
1030 noop_completer (text)
1031 char *text;
1032 {
1033 return NULL;
1034 }
1035
1036 /* Generate symbol names one by one for the completer. If STATE is
1037 zero, then we need to initialize, otherwise the initialization has
1038 already taken place. TEXT is what we expect the symbol to start
1039 with. RL_LINE_BUFFER is available to be looked at; it contains the
1040 entire text of the line. RL_POINT is the offset in that line of
1041 the cursor. You should pretend that the line ends at RL_POINT.
1042 The result is NULL if there are no more completions, else a char
1043 string which is a possible completion. */
1044
1045 static char *
1046 symbol_completion_function (text, state)
1047 char *text;
1048 int state;
1049 {
1050 static char **list = (char **)NULL;
1051 static int index;
1052 char *output;
1053 extern char *rl_line_buffer;
1054 extern int rl_point;
1055 char *tmp_command, *p;
1056 struct cmd_list_element *c, *result_list;
1057
1058 if (!state)
1059 {
1060 /* Free the storage used by LIST, but not by the strings inside. This is
1061 because rl_complete_internal () frees the strings. */
1062 if (list)
1063 free (list);
1064 list = 0;
1065 index = 0;
1066
1067 /* Decide whether to complete on a list of gdb commands or on
1068 symbols. */
1069 tmp_command = (char *) alloca (rl_point + 1);
1070 p = tmp_command;
1071
1072 strncpy (tmp_command, rl_line_buffer, rl_point);
1073 tmp_command[rl_point] = '\0';
1074
1075 if (rl_point == 0)
1076 {
1077 /* An empty line we want to consider ambiguous; that is,
1078 it could be any command. */
1079 c = (struct cmd_list_element *) -1;
1080 result_list = 0;
1081 }
1082 else
1083 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
1084
1085 /* Move p up to the next interesting thing. */
1086 while (*p == ' ' || *p == '\t')
1087 p++;
1088
1089 if (!c)
1090 /* He's typed something unrecognizable. Sigh. */
1091 list = (char **) 0;
1092 else if (c == (struct cmd_list_element *) -1)
1093 {
1094 /* If we didn't recognize everything up to the thing that
1095 needs completing, and we don't know what command it is
1096 yet, we are in trouble. Part of the trouble might be
1097 that the list of delimiters used by readline includes
1098 '-', which we use in commands. Check for this. */
1099 if (p + strlen(text) != tmp_command + rl_point) {
1100 if (tmp_command[rl_point - strlen(text) - 1] == '-')
1101 text = p;
1102 else {
1103 /* This really should not produce an error. Better would
1104 be to pretend to hit RETURN here; this would produce a
1105 response like "Ambiguous command: foo, foobar, etc",
1106 and leave the line available for re-entry with ^P. Instead,
1107 this error blows away the user's typed input without
1108 any way to get it back. */
1109 error (" Unrecognized command.");
1110 }
1111 }
1112
1113 /* He's typed something ambiguous. This is easier. */
1114 if (result_list)
1115 list = complete_on_cmdlist (*result_list->prefixlist, text);
1116 else
1117 list = complete_on_cmdlist (cmdlist, text);
1118 }
1119 else
1120 {
1121 /* If we've gotten this far, gdb has recognized a full
1122 command. There are several possibilities:
1123
1124 1) We need to complete on the command.
1125 2) We need to complete on the possibilities coming after
1126 the command.
1127 2) We need to complete the text of what comes after the
1128 command. */
1129
1130 if (!*p && *text)
1131 /* Always (might be longer versions of thie command). */
1132 list = complete_on_cmdlist (result_list, text);
1133 else if (!*p && !*text)
1134 {
1135 if (c->prefixlist)
1136 list = complete_on_cmdlist (*c->prefixlist, "");
1137 else
1138 list = (*c->completer) ("");
1139 }
1140 else
1141 {
1142 if (c->prefixlist && !c->allow_unknown)
1143 {
1144 #if 0
1145 /* Something like "info adsfkdj". But error() is not
1146 the proper response; just return no completions
1147 instead. */
1148 *p = '\0';
1149 error ("\"%s\" command requires a subcommand.",
1150 tmp_command);
1151 #else
1152 list = NULL;
1153 #endif
1154 }
1155 else
1156 list = (*c->completer) (text);
1157 }
1158 }
1159 }
1160
1161 /* If the debugged program wasn't compiled with symbols, or if we're
1162 clearly completing on a command and no command matches, return
1163 NULL. */
1164 if (!list)
1165 return ((char *)NULL);
1166
1167 output = list[index];
1168 if (output)
1169 index++;
1170
1171 return (output);
1172 }
1173 \f
1174 #ifdef STOP_SIGNAL
1175 static void
1176 stop_sig (signo)
1177 int signo;
1178 {
1179 #if STOP_SIGNAL == SIGTSTP
1180 signal (SIGTSTP, SIG_DFL);
1181 sigsetmask (0);
1182 kill (getpid (), SIGTSTP);
1183 signal (SIGTSTP, stop_sig);
1184 #else
1185 signal (STOP_SIGNAL, stop_sig);
1186 #endif
1187 printf ("%s", prompt);
1188 fflush (stdout);
1189
1190 /* Forget about any previous command -- null line now will do nothing. */
1191 dont_repeat ();
1192 }
1193 #endif /* STOP_SIGNAL */
1194
1195 /* Initialize signal handlers. */
1196 static void
1197 do_nothing (signo)
1198 int signo;
1199 {
1200 }
1201
1202 static void
1203 init_signals ()
1204 {
1205 signal (SIGINT, request_quit);
1206
1207 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1208 passed to the inferior, which we don't want. It would be
1209 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1210 on BSD4.3 systems using vfork, that can affect the
1211 GDB process as well as the inferior (the signal handling tables
1212 might be in memory, shared between the two). Since we establish
1213 a handler for SIGQUIT, when we call exec it will set the signal
1214 to SIG_DFL for us. */
1215 signal (SIGQUIT, do_nothing);
1216 if (signal (SIGHUP, do_nothing) != SIG_IGN)
1217 signal (SIGHUP, disconnect);
1218 signal (SIGFPE, float_handler);
1219
1220 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1221 signal (SIGWINCH, SIGWINCH_HANDLER);
1222 #endif
1223 }
1224 \f
1225 /* Read one line from the command input stream `instream'
1226 into the local static buffer `linebuffer' (whose current length
1227 is `linelength').
1228 The buffer is made bigger as necessary.
1229 Returns the address of the start of the line.
1230
1231 NULL is returned for end of file.
1232
1233 *If* the instream == stdin & stdin is a terminal, the line read
1234 is copied into the file line saver (global var char *line,
1235 length linesize) so that it can be duplicated.
1236
1237 This routine either uses fancy command line editing or
1238 simple input as the user has requested. */
1239
1240 char *
1241 command_line_input (prrompt, repeat)
1242 char *prrompt;
1243 int repeat;
1244 {
1245 static char *linebuffer = 0;
1246 static unsigned linelength = 0;
1247 register char *p;
1248 char *p1;
1249 char *rl;
1250 char *local_prompt = prrompt;
1251 register int c;
1252 char *nline;
1253 char got_eof = 0;
1254
1255 if (linebuffer == 0)
1256 {
1257 linelength = 80;
1258 linebuffer = (char *) xmalloc (linelength);
1259 }
1260
1261 p = linebuffer;
1262
1263 /* Control-C quits instantly if typed while in this loop
1264 since it should not wait until the user types a newline. */
1265 immediate_quit++;
1266 #ifdef STOP_SIGNAL
1267 signal (STOP_SIGNAL, stop_sig);
1268 #endif
1269
1270 while (1)
1271 {
1272 /* Reports are that some Sys V's don't flush stdout/err on reads
1273 from stdin, when stdin/out are sockets rather than ttys. So we
1274 have to do it ourselves, to make emacs-gdb and xxgdb work.
1275 On other machines, doing this once per input should be a cheap nop. */
1276 fflush (stdout);
1277 fflush (stderr);
1278
1279 /* Don't use fancy stuff if not talking to stdin. */
1280 if (command_editing_p && instream == stdin
1281 && ISATTY (instream))
1282 rl = readline (local_prompt);
1283 else
1284 rl = gdb_readline (local_prompt);
1285
1286 if (!rl || rl == (char *) EOF)
1287 {
1288 got_eof = 1;
1289 break;
1290 }
1291 if (strlen(rl) + 1 + (p - linebuffer) > linelength)
1292 {
1293 linelength = strlen(rl) + 1 + (p - linebuffer);
1294 nline = (char *) xrealloc (linebuffer, linelength);
1295 p += nline - linebuffer;
1296 linebuffer = nline;
1297 }
1298 p1 = rl;
1299 /* Copy line. Don't copy null at end. (Leaves line alone
1300 if this was just a newline) */
1301 while (*p1)
1302 *p++ = *p1++;
1303
1304 free (rl); /* Allocated in readline. */
1305
1306 if (p == linebuffer || *(p - 1) != '\\')
1307 break;
1308
1309 p--; /* Put on top of '\'. */
1310 local_prompt = (char *) 0;
1311 }
1312
1313 #ifdef STOP_SIGNAL
1314 signal (SIGTSTP, SIG_DFL);
1315 #endif
1316 immediate_quit--;
1317
1318 if (got_eof)
1319 return NULL;
1320
1321 /* Do history expansion if that is wished. */
1322 if (history_expansion_p && instream == stdin
1323 && ISATTY (instream))
1324 {
1325 char *history_value;
1326 int expanded;
1327
1328 *p = '\0'; /* Insert null now. */
1329 expanded = history_expand (linebuffer, &history_value);
1330 if (expanded)
1331 {
1332 /* Print the changes. */
1333 printf ("%s\n", history_value);
1334
1335 /* If there was an error, call this function again. */
1336 if (expanded < 0)
1337 {
1338 free (history_value);
1339 return command_line_input (prrompt, repeat);
1340 }
1341 if (strlen (history_value) > linelength)
1342 {
1343 linelength = strlen (history_value) + 1;
1344 linebuffer = (char *) xrealloc (linebuffer, linelength);
1345 }
1346 strcpy (linebuffer, history_value);
1347 p = linebuffer + strlen(linebuffer);
1348 free (history_value);
1349 }
1350 }
1351
1352 /* If we just got an empty line, and that is supposed
1353 to repeat the previous command, return the value in the
1354 global buffer. */
1355 if (repeat)
1356 {
1357 if (p == linebuffer)
1358 return line;
1359 p1 = linebuffer;
1360 while (*p1 == ' ' || *p1 == '\t')
1361 p1++;
1362 if (!*p1)
1363 return line;
1364 }
1365
1366 *p = 0;
1367
1368 /* Add line to history if appropriate. */
1369 if (instream == stdin
1370 && ISATTY (stdin) && *linebuffer)
1371 add_history (linebuffer);
1372
1373 /* Note: lines consisting soley of comments are added to the command
1374 history. This is useful when you type a command, and then
1375 realize you don't want to execute it quite yet. You can comment
1376 out the command and then later fetch it from the value history
1377 and remove the '#'. The kill ring is probably better, but some
1378 people are in the habit of commenting things out. */
1379 p1 = linebuffer;
1380 while ((c = *p1++) != '\0')
1381 {
1382 if (c == '"')
1383 while ((c = *p1++) != '"')
1384 {
1385 /* Make sure an escaped '"' doesn't make us think the string
1386 is ended. */
1387 if (c == '\\')
1388 parse_escape (&p1);
1389 if (c == '\0')
1390 break;
1391 }
1392 else if (c == '\'')
1393 while ((c = *p1++) != '\'')
1394 {
1395 /* Make sure an escaped '\'' doesn't make us think the string
1396 is ended. */
1397 if (c == '\\')
1398 parse_escape (&p1);
1399 if (c == '\0')
1400 break;
1401 }
1402 else if (c == '#')
1403 {
1404 /* Found a comment. */
1405 p1[-1] = '\0';
1406 break;
1407 }
1408 }
1409
1410 /* Save into global buffer if appropriate. */
1411 if (repeat)
1412 {
1413 if (linelength > linesize)
1414 {
1415 line = xrealloc (line, linelength);
1416 linesize = linelength;
1417 }
1418 strcpy (line, linebuffer);
1419 return line;
1420 }
1421
1422 return linebuffer;
1423 }
1424 \f
1425 /* Read lines from the input stream
1426 and accumulate them in a chain of struct command_line's
1427 which is then returned. */
1428
1429 struct command_line *
1430 read_command_lines ()
1431 {
1432 struct command_line *first = 0;
1433 register struct command_line *next, *tail = 0;
1434 register char *p, *p1;
1435 struct cleanup *old_chain = 0;
1436
1437 while (1)
1438 {
1439 dont_repeat ();
1440 p = command_line_input ((char *) NULL, instream == stdin);
1441 if (p == NULL)
1442 /* Treat end of file like "end". */
1443 break;
1444
1445 /* Remove leading and trailing blanks. */
1446 while (*p == ' ' || *p == '\t') p++;
1447 p1 = p + strlen (p);
1448 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t')) p1--;
1449
1450 /* Is this "end"? */
1451 if (p1 - p == 3 && !strncmp (p, "end", 3))
1452 break;
1453
1454 /* No => add this line to the chain of command lines. */
1455 next = (struct command_line *) xmalloc (sizeof (struct command_line));
1456 next->line = savestring (p, p1 - p);
1457 next->next = 0;
1458 if (tail)
1459 {
1460 tail->next = next;
1461 }
1462 else
1463 {
1464 /* We just read the first line.
1465 From now on, arrange to throw away the lines we have
1466 if we quit or get an error while inside this function. */
1467 first = next;
1468 old_chain = make_cleanup (free_command_lines, &first);
1469 }
1470 tail = next;
1471 }
1472
1473 dont_repeat ();
1474
1475 /* Now we are about to return the chain to our caller,
1476 so freeing it becomes his responsibility. */
1477 if (first)
1478 discard_cleanups (old_chain);
1479 return first;
1480 }
1481
1482 /* Free a chain of struct command_line's. */
1483
1484 void
1485 free_command_lines (lptr)
1486 struct command_line **lptr;
1487 {
1488 register struct command_line *l = *lptr;
1489 register struct command_line *next;
1490
1491 while (l)
1492 {
1493 next = l->next;
1494 free (l->line);
1495 free (l);
1496 l = next;
1497 }
1498 }
1499 \f
1500 /* Add an element to the list of info subcommands. */
1501
1502 void
1503 add_info (name, fun, doc)
1504 char *name;
1505 void (*fun) PARAMS ((char *, int));
1506 char *doc;
1507 {
1508 add_cmd (name, no_class, fun, doc, &infolist);
1509 }
1510
1511 /* Add an alias to the list of info subcommands. */
1512
1513 void
1514 add_info_alias (name, oldname, abbrev_flag)
1515 char *name;
1516 char *oldname;
1517 int abbrev_flag;
1518 {
1519 add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
1520 }
1521
1522 /* The "info" command is defined as a prefix, with allow_unknown = 0.
1523 Therefore, its own definition is called only for "info" with no args. */
1524
1525 /* ARGSUSED */
1526 static void
1527 info_command (arg, from_tty)
1528 char *arg;
1529 int from_tty;
1530 {
1531 printf ("\"info\" must be followed by the name of an info command.\n");
1532 help_list (infolist, "info ", -1, stdout);
1533 }
1534
1535 /* The "show" command with no arguments shows all the settings. */
1536
1537 /* ARGSUSED */
1538 static void
1539 show_command (arg, from_tty)
1540 char *arg;
1541 int from_tty;
1542 {
1543 cmd_show_list (showlist, from_tty, "");
1544 }
1545 \f
1546 /* Add an element to the list of commands. */
1547
1548 void
1549 add_com (name, class, fun, doc)
1550 char *name;
1551 enum command_class class;
1552 void (*fun) PARAMS ((char *, int));
1553 char *doc;
1554 {
1555 add_cmd (name, class, fun, doc, &cmdlist);
1556 }
1557
1558 /* Add an alias or abbreviation command to the list of commands. */
1559
1560 void
1561 add_com_alias (name, oldname, class, abbrev_flag)
1562 char *name;
1563 char *oldname;
1564 enum command_class class;
1565 int abbrev_flag;
1566 {
1567 add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
1568 }
1569
1570 void
1571 error_no_arg (why)
1572 char *why;
1573 {
1574 error ("Argument required (%s).", why);
1575 }
1576
1577 /* ARGSUSED */
1578 static void
1579 help_command (command, from_tty)
1580 char *command;
1581 int from_tty; /* Ignored */
1582 {
1583 help_cmd (command, stdout);
1584 }
1585 \f
1586 static void
1587 validate_comname (comname)
1588 char *comname;
1589 {
1590 register char *p;
1591
1592 if (comname == 0)
1593 error_no_arg ("name of command to define");
1594
1595 p = comname;
1596 while (*p)
1597 {
1598 if (!isalnum(*p) && *p != '-')
1599 error ("Junk in argument list: \"%s\"", p);
1600 p++;
1601 }
1602 }
1603
1604 static void
1605 define_command (comname, from_tty)
1606 char *comname;
1607 int from_tty;
1608 {
1609 register struct command_line *cmds;
1610 register struct cmd_list_element *c, *newc;
1611 char *tem = comname;
1612
1613 validate_comname (comname);
1614
1615 /* Look it up, and verify that we got an exact match. */
1616 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
1617 if (c && 0 != strcmp (comname, c->name))
1618 c = 0;
1619
1620 if (c)
1621 {
1622 if (c->class == class_user || c->class == class_alias)
1623 tem = "Redefine command \"%s\"? ";
1624 else
1625 tem = "Really redefine built-in command \"%s\"? ";
1626 if (!query (tem, c->name))
1627 error ("Command \"%s\" not redefined.", c->name);
1628 }
1629
1630 comname = savestring (comname, strlen (comname));
1631
1632 /* If the rest of the commands will be case insensetive, this one
1633 should behave in the same manner. */
1634 for (tem = comname; *tem; tem++)
1635 if (isupper(*tem)) *tem = tolower(*tem);
1636
1637 if (from_tty)
1638 {
1639 printf ("Type commands for definition of \"%s\".\n\
1640 End with a line saying just \"end\".\n", comname);
1641 fflush (stdout);
1642 }
1643
1644 cmds = read_command_lines ();
1645
1646 if (c && c->class == class_user)
1647 free_command_lines (&c->user_commands);
1648
1649 newc = add_cmd (comname, class_user, not_just_help_class_command,
1650 (c && c->class == class_user)
1651 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
1652 newc->user_commands = cmds;
1653 }
1654
1655 static void
1656 document_command (comname, from_tty)
1657 char *comname;
1658 int from_tty;
1659 {
1660 struct command_line *doclines;
1661 register struct cmd_list_element *c;
1662 char *tem = comname;
1663
1664 validate_comname (comname);
1665
1666 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1667
1668 if (c->class != class_user)
1669 error ("Command \"%s\" is built-in.", comname);
1670
1671 if (from_tty)
1672 printf ("Type documentation for \"%s\".\n\
1673 End with a line saying just \"end\".\n", comname);
1674
1675 doclines = read_command_lines ();
1676
1677 if (c->doc) free (c->doc);
1678
1679 {
1680 register struct command_line *cl1;
1681 register int len = 0;
1682
1683 for (cl1 = doclines; cl1; cl1 = cl1->next)
1684 len += strlen (cl1->line) + 1;
1685
1686 c->doc = (char *) xmalloc (len + 1);
1687 *c->doc = 0;
1688
1689 for (cl1 = doclines; cl1; cl1 = cl1->next)
1690 {
1691 strcat (c->doc, cl1->line);
1692 if (cl1->next)
1693 strcat (c->doc, "\n");
1694 }
1695 }
1696
1697 free_command_lines (&doclines);
1698 }
1699 \f
1700 static void
1701 print_gnu_advertisement()
1702 {
1703 printf ("\
1704 GDB is free software and you are welcome to distribute copies of it\n\
1705 under certain conditions; type \"show copying\" to see the conditions.\n\
1706 There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\
1707 ");
1708 }
1709
1710 static void
1711 print_gdb_version ()
1712 {
1713 printf_filtered ("\
1714 GDB %s, Copyright 1992 Free Software Foundation, Inc.",
1715 version);
1716 }
1717
1718 /* ARGSUSED */
1719 static void
1720 show_version (args, from_tty)
1721 char *args;
1722 int from_tty;
1723 {
1724 immediate_quit++;
1725 print_gnu_advertisement ();
1726 print_gdb_version ();
1727 printf_filtered ("\n");
1728 immediate_quit--;
1729 }
1730 \f
1731 /* xgdb calls this to reprint the usual GDB prompt. */
1732
1733 void
1734 print_prompt ()
1735 {
1736 printf ("%s", prompt);
1737 fflush (stdout);
1738 }
1739 \f
1740 static void
1741 quit_command (args, from_tty)
1742 char *args;
1743 int from_tty;
1744 {
1745 if (inferior_pid != 0 && target_has_execution)
1746 {
1747 if (query ("The program is running. Quit anyway? "))
1748 {
1749 target_kill ();
1750 }
1751 else
1752 error ("Not confirmed.");
1753 }
1754 /* Save the history information if it is appropriate to do so. */
1755 if (write_history_p && history_filename)
1756 write_history (history_filename);
1757 exit (0);
1758 }
1759
1760 /* Returns whether GDB is running on a terminal and whether the user
1761 desires that questions be asked of them on that terminal. */
1762
1763 int
1764 input_from_terminal_p ()
1765 {
1766 return gdb_has_a_terminal && (instream == stdin) & caution;
1767 }
1768 \f
1769 /* ARGSUSED */
1770 static void
1771 pwd_command (args, from_tty)
1772 char *args;
1773 int from_tty;
1774 {
1775 if (args) error ("The \"pwd\" command does not take an argument: %s", args);
1776 getcwd (dirbuf, sizeof (dirbuf));
1777
1778 if (strcmp (dirbuf, current_directory))
1779 printf ("Working directory %s\n (canonically %s).\n",
1780 current_directory, dirbuf);
1781 else
1782 printf ("Working directory %s.\n", current_directory);
1783 }
1784
1785 static void
1786 cd_command (dir, from_tty)
1787 char *dir;
1788 int from_tty;
1789 {
1790 int len;
1791 int change;
1792
1793 /* If the new directory is absolute, repeat is a no-op; if relative,
1794 repeat might be useful but is more likely to be a mistake. */
1795 dont_repeat ();
1796
1797 if (dir == 0)
1798 error_no_arg ("new working directory");
1799
1800 dir = tilde_expand (dir);
1801 make_cleanup (free, dir);
1802
1803 if (chdir (dir) < 0)
1804 perror_with_name (dir);
1805
1806 len = strlen (dir);
1807 dir = savestring (dir, len - (len > 1 && dir[len-1] == '/'));
1808 if (dir[0] == '/')
1809 current_directory = dir;
1810 else
1811 {
1812 current_directory = concat (current_directory, "/", dir, NULL);
1813 free (dir);
1814 }
1815
1816 /* Now simplify any occurrences of `.' and `..' in the pathname. */
1817
1818 change = 1;
1819 while (change)
1820 {
1821 char *p;
1822 change = 0;
1823
1824 for (p = current_directory; *p;)
1825 {
1826 if (!strncmp (p, "/./", 2)
1827 && (p[2] == 0 || p[2] == '/'))
1828 strcpy (p, p + 2);
1829 else if (!strncmp (p, "/..", 3)
1830 && (p[3] == 0 || p[3] == '/')
1831 && p != current_directory)
1832 {
1833 char *q = p;
1834 while (q != current_directory && q[-1] != '/') q--;
1835 if (q != current_directory)
1836 {
1837 strcpy (q-1, p+3);
1838 p = q-1;
1839 }
1840 }
1841 else p++;
1842 }
1843 }
1844
1845 forget_cached_source_info ();
1846
1847 if (from_tty)
1848 pwd_command ((char *) 0, 1);
1849 }
1850 \f
1851 /* ARGSUSED */
1852 static void
1853 source_command (args, from_tty)
1854 char *args;
1855 int from_tty;
1856 {
1857 FILE *stream;
1858 struct cleanup *cleanups;
1859 char *file = args;
1860
1861 if (file == 0)
1862 /* Let source without arguments read .gdbinit. */
1863 file = gdbinit;
1864
1865 file = tilde_expand (file);
1866 make_cleanup (free, file);
1867
1868 stream = fopen (file, "r");
1869 if (stream == 0)
1870 perror_with_name (file);
1871
1872 cleanups = make_cleanup (fclose, stream);
1873
1874 read_command_file (stream);
1875
1876 do_cleanups (cleanups);
1877 }
1878
1879 /* ARGSUSED */
1880 static void
1881 echo_command (text, from_tty)
1882 char *text;
1883 int from_tty;
1884 {
1885 char *p = text;
1886 register int c;
1887
1888 if (text)
1889 while (c = *p++)
1890 {
1891 if (c == '\\')
1892 {
1893 /* \ at end of argument is used after spaces
1894 so they won't be lost. */
1895 if (*p == 0)
1896 return;
1897
1898 c = parse_escape (&p);
1899 if (c >= 0)
1900 printf_filtered ("%c", c);
1901 }
1902 else
1903 printf_filtered ("%c", c);
1904 }
1905
1906 /* Force this output to appear now. */
1907 wrap_here ("");
1908 fflush (stdout);
1909 }
1910
1911 /* ARGSUSED */
1912 static void
1913 dump_me_command (args, from_tty)
1914 char *args;
1915 int from_tty;
1916 {
1917 if (query ("Should GDB dump core? "))
1918 {
1919 signal (SIGQUIT, SIG_DFL);
1920 kill (getpid (), SIGQUIT);
1921 }
1922 }
1923 \f
1924 /* Functions to manipulate command line editing control variables. */
1925
1926 /* Number of commands to print in each call to show_commands. */
1927 #define Hist_print 10
1928 static void
1929 show_commands (args, from_tty)
1930 char *args;
1931 int from_tty;
1932 {
1933 /* Index for history commands. Relative to history_base. */
1934 int offset;
1935
1936 /* Number of the history entry which we are planning to display next.
1937 Relative to history_base. */
1938 static int num = 0;
1939
1940 /* The first command in the history which doesn't exist (i.e. one more
1941 than the number of the last command). Relative to history_base. */
1942 int hist_len;
1943
1944 extern struct _hist_entry *history_get PARAMS ((int));
1945 extern int history_base;
1946
1947 /* Print out some of the commands from the command history. */
1948 /* First determine the length of the history list. */
1949 hist_len = history_size;
1950 for (offset = 0; offset < history_size; offset++)
1951 {
1952 if (!history_get (history_base + offset))
1953 {
1954 hist_len = offset;
1955 break;
1956 }
1957 }
1958
1959 if (args)
1960 {
1961 if (args[0] == '+' && args[1] == '\0')
1962 /* "info editing +" should print from the stored position. */
1963 ;
1964 else
1965 /* "info editing <exp>" should print around command number <exp>. */
1966 num = (parse_and_eval_address (args) - history_base) - Hist_print / 2;
1967 }
1968 /* "show commands" means print the last Hist_print commands. */
1969 else
1970 {
1971 num = hist_len - Hist_print;
1972 }
1973
1974 if (num < 0)
1975 num = 0;
1976
1977 /* If there are at least Hist_print commands, we want to display the last
1978 Hist_print rather than, say, the last 6. */
1979 if (hist_len - num < Hist_print)
1980 {
1981 num = hist_len - Hist_print;
1982 if (num < 0)
1983 num = 0;
1984 }
1985
1986 for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
1987 {
1988 printf_filtered ("%5d %s\n", history_base + offset,
1989 (history_get (history_base + offset))->line);
1990 }
1991
1992 /* The next command we want to display is the next one that we haven't
1993 displayed yet. */
1994 num += Hist_print;
1995
1996 /* If the user repeats this command with return, it should do what
1997 "show commands +" does. This is unnecessary if arg is null,
1998 because "show commands +" is not useful after "show commands". */
1999 if (from_tty && args)
2000 {
2001 args[0] = '+';
2002 args[1] = '\0';
2003 }
2004 }
2005
2006 /* Called by do_setshow_command. */
2007 /* ARGSUSED */
2008 static void
2009 set_history_size_command (args, from_tty, c)
2010 char *args;
2011 int from_tty;
2012 struct cmd_list_element *c;
2013 {
2014 if (history_size == UINT_MAX)
2015 unstifle_history ();
2016 else if (history_size >= 0)
2017 stifle_history (history_size);
2018 else
2019 {
2020 history_size = UINT_MAX;
2021 error ("History size must be non-negative");
2022 }
2023 }
2024
2025 /* ARGSUSED */
2026 static void
2027 set_history (args, from_tty)
2028 char *args;
2029 int from_tty;
2030 {
2031 printf ("\"set history\" must be followed by the name of a history subcommand.\n");
2032 help_list (sethistlist, "set history ", -1, stdout);
2033 }
2034
2035 /* ARGSUSED */
2036 static void
2037 show_history (args, from_tty)
2038 char *args;
2039 int from_tty;
2040 {
2041 cmd_show_list (showhistlist, from_tty, "");
2042 }
2043
2044 int info_verbose = 0; /* Default verbose msgs off */
2045
2046 /* Called by do_setshow_command. An elaborate joke. */
2047 /* ARGSUSED */
2048 static void
2049 set_verbose (args, from_tty, c)
2050 char *args;
2051 int from_tty;
2052 struct cmd_list_element *c;
2053 {
2054 char *cmdname = "verbose";
2055 struct cmd_list_element *showcmd;
2056
2057 showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
2058
2059 if (info_verbose)
2060 {
2061 c->doc = "Set verbose printing of informational messages.";
2062 showcmd->doc = "Show verbose printing of informational messages.";
2063 }
2064 else
2065 {
2066 c->doc = "Set verbosity.";
2067 showcmd->doc = "Show verbosity.";
2068 }
2069 }
2070
2071 static void
2072 float_handler (signo)
2073 int signo;
2074 {
2075 /* This message is based on ANSI C, section 4.7. Note that integer
2076 divide by zero causes this, so "float" is a misnomer. */
2077 error ("Erroneous arithmetic operation.");
2078 }
2079
2080 /* Return whether we are running a batch file or from terminal. */
2081 int
2082 batch_mode ()
2083 {
2084 return !(instream == stdin && ISATTY (stdin));
2085 }
2086
2087 \f
2088 static void
2089 initialize_cmd_lists ()
2090 {
2091 cmdlist = (struct cmd_list_element *) 0;
2092 infolist = (struct cmd_list_element *) 0;
2093 enablelist = (struct cmd_list_element *) 0;
2094 disablelist = (struct cmd_list_element *) 0;
2095 deletelist = (struct cmd_list_element *) 0;
2096 enablebreaklist = (struct cmd_list_element *) 0;
2097 setlist = (struct cmd_list_element *) 0;
2098 showlist = NULL;
2099 sethistlist = (struct cmd_list_element *) 0;
2100 showhistlist = NULL;
2101 unsethistlist = (struct cmd_list_element *) 0;
2102 }
2103
2104 /* Init the history buffer. Note that we are called after the init file(s)
2105 * have been read so that the user can change the history file via his
2106 * .gdbinit file (for instance). The GDBHISTFILE environment variable
2107 * overrides all of this.
2108 */
2109
2110 static void
2111 initialize_history()
2112 {
2113 char *tmpenv;
2114
2115 tmpenv = getenv ("HISTSIZE");
2116 if (tmpenv)
2117 history_size = atoi (tmpenv);
2118 else if (!history_size)
2119 history_size = 256;
2120
2121 stifle_history (history_size);
2122
2123 tmpenv = getenv ("GDBHISTFILE");
2124 if (tmpenv)
2125 history_filename = savestring (tmpenv, strlen(tmpenv));
2126 else if (!history_filename) {
2127 /* We include the current directory so that if the user changes
2128 directories the file written will be the same as the one
2129 that was read. */
2130 history_filename = concat (current_directory, "/.gdb_history", NULL);
2131 }
2132 read_history (history_filename);
2133 }
2134
2135 static void
2136 initialize_main ()
2137 {
2138 struct cmd_list_element *c;
2139
2140 #ifdef DEFAULT_PROMPT
2141 prompt = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT));
2142 #else
2143 prompt = savestring ("(gdb) ", 6);
2144 #endif
2145
2146 /* Set the important stuff up for command editing. */
2147 command_editing_p = 1;
2148 history_expansion_p = 0;
2149 write_history_p = 0;
2150
2151 /* Setup important stuff for command line editing. */
2152 rl_completion_entry_function = (int (*)()) symbol_completion_function;
2153 rl_completer_word_break_characters = gdb_completer_word_break_characters;
2154 rl_readline_name = "gdb";
2155
2156 /* Define the classes of commands.
2157 They will appear in the help list in the reverse of this order. */
2158
2159 add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist);
2160 add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist);
2161 add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\
2162 The commands in this class are those defined by the user.\n\
2163 Use the \"define\" command to define a command.", &cmdlist);
2164 add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist);
2165 add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist);
2166 add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist);
2167 add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist);
2168 add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist);
2169 add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\
2170 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
2171 counting from zero for the innermost (currently executing) frame.\n\n\
2172 At any time gdb identifies one frame as the \"selected\" frame.\n\
2173 Variable lookups are done with respect to the selected frame.\n\
2174 When the program being debugged stops, gdb selects the innermost frame.\n\
2175 The commands below can be used to select other frames by number or address.",
2176 &cmdlist);
2177 add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist);
2178
2179 add_com ("pwd", class_files, pwd_command,
2180 "Print working directory. This is used for your program as well.");
2181 add_com ("cd", class_files, cd_command,
2182 "Set working directory to DIR for debugger and program being debugged.\n\
2183 The change does not take effect for the program being debugged\n\
2184 until the next time it is started.");
2185
2186 add_show_from_set
2187 (add_set_cmd ("prompt", class_support, var_string, (char *)&prompt,
2188 "Set gdb's prompt",
2189 &setlist),
2190 &showlist);
2191
2192 add_com ("echo", class_support, echo_command,
2193 "Print a constant string. Give string as argument.\n\
2194 C escape sequences may be used in the argument.\n\
2195 No newline is added at the end of the argument;\n\
2196 use \"\\n\" if you want a newline to be printed.\n\
2197 Since leading and trailing whitespace are ignored in command arguments,\n\
2198 if you want to print some you must use \"\\\" before leading whitespace\n\
2199 to be printed or after trailing whitespace.");
2200 add_com ("document", class_support, document_command,
2201 "Document a user-defined command.\n\
2202 Give command name as argument. Give documentation on following lines.\n\
2203 End with a line of just \"end\".");
2204 add_com ("define", class_support, define_command,
2205 "Define a new command name. Command name is argument.\n\
2206 Definition appears on following lines, one command per line.\n\
2207 End with a line of just \"end\".\n\
2208 Use the \"document\" command to give documentation for the new command.\n\
2209 Commands defined in this way do not take arguments.");
2210
2211 #ifdef __STDC__
2212 add_com ("source", class_support, source_command,
2213 "Read commands from a file named FILE.\n\
2214 Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\
2215 when gdb is started.");
2216 #else
2217 /* Punt file name, we can't help it easily. */
2218 add_com ("source", class_support, source_command,
2219 "Read commands from a file named FILE.\n\
2220 Note that the file \".gdbinit\" is read automatically in this way\n\
2221 when gdb is started.");
2222 #endif
2223
2224 add_com ("quit", class_support, quit_command, "Exit gdb.");
2225 add_com ("help", class_support, help_command, "Print list of commands.");
2226 add_com_alias ("q", "quit", class_support, 1);
2227 add_com_alias ("h", "help", class_support, 1);
2228
2229
2230 c = add_set_cmd ("verbose", class_support, var_boolean, (char *)&info_verbose,
2231 "Set ",
2232 &setlist),
2233 add_show_from_set (c, &showlist);
2234 c->function.sfunc = set_verbose;
2235 set_verbose (NULL, 0, c);
2236
2237 add_com ("dump-me", class_obscure, dump_me_command,
2238 "Get fatal error; make debugger dump its core.");
2239
2240 add_show_from_set
2241 (add_set_cmd ("editing", class_support, var_boolean, (char *)&command_editing_p,
2242 "Set editing of command lines as they are typed.\n\
2243 Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
2244 Without an argument, command line editing is enabled. To edit, use\n\
2245 EMACS-like or VI-like commands like control-P or ESC.", &setlist),
2246 &showlist);
2247
2248 add_prefix_cmd ("history", class_support, set_history,
2249 "Generic command for setting command history parameters.",
2250 &sethistlist, "set history ", 0, &setlist);
2251 add_prefix_cmd ("history", class_support, show_history,
2252 "Generic command for showing command history parameters.",
2253 &showhistlist, "show history ", 0, &showlist);
2254
2255 add_show_from_set
2256 (add_set_cmd ("expansion", no_class, var_boolean, (char *)&history_expansion_p,
2257 "Set history expansion on command input.\n\
2258 Without an argument, history expansion is enabled.", &sethistlist),
2259 &showhistlist);
2260
2261 add_show_from_set
2262 (add_set_cmd ("save", no_class, var_boolean, (char *)&write_history_p,
2263 "Set saving of the history record on exit.\n\
2264 Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
2265 Without an argument, saving is enabled.", &sethistlist),
2266 &showhistlist);
2267
2268 c = add_set_cmd ("size", no_class, var_uinteger, (char *)&history_size,
2269 "Set the size of the command history, \n\
2270 ie. the number of previous commands to keep a record of.", &sethistlist);
2271 add_show_from_set (c, &showhistlist);
2272 c->function.sfunc = set_history_size_command;
2273
2274 add_show_from_set
2275 (add_set_cmd ("filename", no_class, var_filename, (char *)&history_filename,
2276 "Set the filename in which to record the command history\n\
2277 (the list of previous commands of which a record is kept).", &sethistlist),
2278 &showhistlist);
2279
2280 add_show_from_set
2281 (add_set_cmd ("confirm", class_support, var_boolean,
2282 (char *)&caution,
2283 "Set whether to confirm potentially dangerous operations.",
2284 &setlist),
2285 &showlist);
2286
2287 add_prefix_cmd ("info", class_info, info_command,
2288 "Generic command for showing things about the program being debugged.",
2289 &infolist, "info ", 0, &cmdlist);
2290 add_com_alias ("i", "info", class_info, 1);
2291
2292 add_prefix_cmd ("show", class_info, show_command,
2293 "Generic command for showing things about the debugger.",
2294 &showlist, "show ", 0, &cmdlist);
2295 /* Another way to get at the same thing. */
2296 add_info ("set", show_command, "Show all GDB settings.");
2297
2298 add_cmd ("commands", no_class, show_commands,
2299 "Show the the history of commands you typed.\n\
2300 You can supply a command number to start with, or a `+' to start after\n\
2301 the previous command number shown.",
2302 &showlist);
2303
2304 add_cmd ("version", no_class, show_version,
2305 "Show what version of GDB this is.", &showlist);
2306 }
This page took 0.077912 seconds and 4 git commands to generate.