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