40346908d97b81fb27f97fdabb64ee0b140eacca
[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 ", stderr);
596 #ifdef ADDITIONAL_OPTION_HELP
597 fputs (ADDITIONAL_OPTION_HELP, stderr);
598 #endif
599 fputs ("\n\
600 For more information, type \"help\" from within GDB, or consult the\n\
601 GDB manual (available as on-line info or a printed manual).\n", stderr);
602 /* Exiting after printing this message seems like
603 the most useful thing to do. */
604 exit (0);
605 }
606
607 /* OK, that's all the options. The other arguments are filenames. */
608 count = 0;
609 for (; optind < argc; optind++)
610 switch (++count)
611 {
612 case 1:
613 symarg = argv[optind];
614 execarg = argv[optind];
615 break;
616 case 2:
617 corearg = argv[optind];
618 break;
619 case 3:
620 fprintf (stderr,
621 "Excess command line arguments ignored. (%s%s)\n",
622 argv[optind], (optind == argc - 1) ? "" : " ...");
623 break;
624 }
625 if (batch)
626 quiet = 1;
627 }
628
629 /* Run the init function of each source file */
630
631 initialize_cmd_lists (); /* This needs to be done first */
632 initialize_all_files ();
633 initialize_main (); /* But that omits this file! Do it now */
634 init_signals ();
635
636 if (!quiet)
637 {
638 /* Print all the junk at the top, with trailing "..." if we are about
639 to read a symbol file (possibly slowly). */
640 print_gnu_advertisement ();
641 print_gdb_version ();
642 if (symarg)
643 printf_filtered ("..");
644 wrap_here("");
645 fflush (stdout); /* Force to screen during slow operations */
646 }
647
648 error_pre_print = "\n\n";
649 warning_pre_print = "\n\nwarning: ";
650
651 /* Now perform all the actions indicated by the arguments. */
652 if (cdarg != NULL)
653 {
654 if (!setjmp (to_top_level))
655 {
656 cd_command (cdarg, 0);
657 init_source_path ();
658 }
659 }
660 do_cleanups (ALL_CLEANUPS);
661
662 for (i = 0; i < ndir; i++)
663 if (!setjmp (to_top_level))
664 directory_command (dirarg[i], 0);
665 free (dirarg);
666 do_cleanups (ALL_CLEANUPS);
667
668 if (execarg != NULL
669 && symarg != NULL
670 && strcmp (execarg, symarg) == 0)
671 {
672 /* The exec file and the symbol-file are the same. If we can't open
673 it, better only print one error message. */
674 if (!setjmp (to_top_level))
675 {
676 exec_file_command (execarg, !batch);
677 symbol_file_command (symarg, 0);
678 }
679 }
680 else
681 {
682 if (execarg != NULL)
683 if (!setjmp (to_top_level))
684 exec_file_command (execarg, !batch);
685 if (symarg != NULL)
686 if (!setjmp (to_top_level))
687 symbol_file_command (symarg, 0);
688 }
689 do_cleanups (ALL_CLEANUPS);
690
691 /* After the symbol file has been read, print a newline to get us
692 beyond the copyright line... But errors should still set off
693 the error message with a (single) blank line. */
694 if (!quiet)
695 printf_filtered ("\n");
696 error_pre_print = "\n";
697 warning_pre_print = "\nwarning: ";
698
699 /* Set the initial language. */
700 {
701 struct partial_symtab *pst = find_main_psymtab ();
702 enum language lang = language_unknown;
703 if (pst == NULL) ;
704 #if 0
705 /* A better solution would set the language when reading the psymtab.
706 This would win for symbol file formats that encode the langauge,
707 such as DWARF. But, we don't do that yet. FIXME */
708 else if (pst->language != language_unknown)
709 lang = pst->language;
710 #endif
711 else if (pst->filename != NULL)
712 lang = deduce_language_from_filename (pst->filename);
713 if (lang == language_unknown) /* Make C the default language */
714 lang = language_c;
715 set_language (lang);
716 }
717
718 if (corearg != NULL)
719 if (!setjmp (to_top_level))
720 core_file_command (corearg, !batch);
721 else if (isdigit (corearg[0]) && !setjmp (to_top_level))
722 attach_command (corearg, !batch);
723 do_cleanups (ALL_CLEANUPS);
724
725 if (ttyarg != NULL)
726 if (!setjmp (to_top_level))
727 tty_command (ttyarg, !batch);
728 do_cleanups (ALL_CLEANUPS);
729
730 #ifdef ADDITIONAL_OPTION_HANDLER
731 ADDITIONAL_OPTION_HANDLER;
732 #endif
733
734 /* Error messages should no longer be distinguished with extra output. */
735 error_pre_print = 0;
736 warning_pre_print = "warning: ";
737
738 {
739 struct stat homebuf, cwdbuf;
740 char *homedir, *homeinit;
741
742 /* Read init file, if it exists in home directory */
743 homedir = getenv ("HOME");
744 if (homedir)
745 {
746 homeinit = (char *) alloca (strlen (getenv ("HOME")) +
747 strlen (gdbinit) + 10);
748 strcpy (homeinit, getenv ("HOME"));
749 strcat (homeinit, "/");
750 strcat (homeinit, gdbinit);
751 if (!inhibit_gdbinit && access (homeinit, R_OK) == 0)
752 if (!setjmp (to_top_level))
753 source_command (homeinit, 0);
754 do_cleanups (ALL_CLEANUPS);
755
756 /* Do stats; no need to do them elsewhere since we'll only
757 need them if homedir is set. Make sure that they are
758 zero in case one of them fails (this guarantees that they
759 won't match if either exists). */
760
761 memset (&homebuf, 0, sizeof (struct stat));
762 memset (&cwdbuf, 0, sizeof (struct stat));
763
764 stat (homeinit, &homebuf);
765 stat (gdbinit, &cwdbuf); /* We'll only need this if
766 homedir was set. */
767 }
768
769 /* Read the input file in the current directory, *if* it isn't
770 the same file (it should exist, also). */
771
772 if (!homedir
773 || bcmp ((char *) &homebuf,
774 (char *) &cwdbuf,
775 sizeof (struct stat)))
776 if (!inhibit_gdbinit && access (gdbinit, R_OK) == 0)
777 if (!setjmp (to_top_level))
778 source_command (gdbinit, 0);
779 do_cleanups (ALL_CLEANUPS);
780 }
781
782 for (i = 0; i < ncmd; i++)
783 if (!setjmp (to_top_level))
784 {
785 if (cmdarg[i][0] == '-' && cmdarg[i][1] == '\0')
786 read_command_file (stdin);
787 else
788 source_command (cmdarg[i], !batch);
789 do_cleanups (ALL_CLEANUPS);
790 }
791 free (cmdarg);
792
793 /* Read in the old history after all the command files have been read. */
794 initialize_history();
795
796 if (batch)
797 {
798 /* We have hit the end of the batch file. */
799 exit (0);
800 }
801
802 /* Do any host- or target-specific hacks. This is used for i960 targets
803 to force the user to set a nindy target and spec its parameters. */
804
805 #ifdef BEFORE_MAIN_LOOP_HOOK
806 BEFORE_MAIN_LOOP_HOOK;
807 #endif
808
809 /* The command loop. */
810
811 while (1)
812 {
813 if (!setjmp (to_top_level))
814 {
815 do_cleanups (ALL_CLEANUPS); /* Do complete cleanup */
816 command_loop ();
817 quit_command ((char *)0, instream == stdin);
818 }
819 }
820 /* No exit -- exit is through quit_command. */
821 }
822
823 /* Execute the line P as a command.
824 Pass FROM_TTY as second argument to the defining function. */
825
826 void
827 execute_command (p, from_tty)
828 char *p;
829 int from_tty;
830 {
831 register struct cmd_list_element *c;
832 register struct command_line *cmdlines;
833 register enum language flang;
834 static const struct language_defn *saved_language = 0;
835 static int warned = 0;
836
837 free_all_values ();
838
839 /* This can happen when command_line_input hits end of file. */
840 if (p == NULL)
841 return;
842
843 while (*p == ' ' || *p == '\t') p++;
844 if (*p)
845 {
846 char *arg;
847
848 c = lookup_cmd (&p, cmdlist, "", 0, 1);
849 /* Pass null arg rather than an empty one. */
850 arg = *p ? p : 0;
851 if (c->class == class_user)
852 {
853 struct cleanup *old_chain;
854
855 if (*p)
856 error ("User-defined commands cannot take arguments.");
857 cmdlines = c->user_commands;
858 if (cmdlines == 0)
859 /* Null command */
860 return;
861
862 /* Set the instream to 0, indicating execution of a
863 user-defined function. */
864 old_chain = make_cleanup (source_cleanup, instream);
865 instream = (FILE *) 0;
866 while (cmdlines)
867 {
868 execute_command (cmdlines->line, 0);
869 cmdlines = cmdlines->next;
870 }
871 do_cleanups (old_chain);
872 }
873 else if (c->type == set_cmd || c->type == show_cmd)
874 do_setshow_command (arg, from_tty & caution, c);
875 else if (c->function.cfunc == NO_FUNCTION)
876 error ("That is not a command, just a help topic.");
877 else
878 (*c->function.cfunc) (arg, from_tty & caution);
879 }
880
881 /* Tell the user if the language has changed (except first time). */
882 if (current_language != saved_language)
883 {
884 if (language_mode == language_mode_auto) {
885 if (saved_language)
886 language_info (1); /* Print what changed. */
887 }
888 saved_language = current_language;
889 warned = 0;
890 }
891
892 /* Warn the user if the working language does not match the
893 language of the current frame. Only warn the user if we are
894 actually running the program, i.e. there is a stack. */
895 /* FIXME: This should be cacheing the frame and only running when
896 the frame changes. */
897 if (target_has_stack)
898 {
899 flang = get_frame_language ();
900 if (!warned
901 && flang != language_unknown
902 && flang != current_language->la_language)
903 {
904 printf_filtered ("%s\n", lang_frame_mismatch_warn);
905 warned = 1;
906 }
907 }
908 }
909
910 /* ARGSUSED */
911 static void
912 command_loop_marker (foo)
913 int foo;
914 {
915 }
916
917 /* Read commands from `instream' and execute them
918 until end of file or error reading instream. */
919 static void
920 command_loop ()
921 {
922 struct cleanup *old_chain;
923 char *command;
924 int stdin_is_tty = ISATTY (stdin);
925
926 while (!feof (instream))
927 {
928 if (window_hook && instream == stdin)
929 (*window_hook) (instream, prompt);
930
931 quit_flag = 0;
932 if (instream == stdin && stdin_is_tty)
933 reinitialize_more_filter ();
934 old_chain = make_cleanup (command_loop_marker, 0);
935 command = command_line_input (instream == stdin ? prompt : (char *) NULL,
936 instream == stdin);
937 if (command == 0)
938 return;
939 execute_command (command, instream == stdin);
940 /* Do any commands attached to breakpoint we stopped at. */
941 bpstat_do_actions (&stop_bpstat);
942 do_cleanups (old_chain);
943 }
944 }
945 \f
946 /* Commands call this if they do not want to be repeated by null lines. */
947
948 void
949 dont_repeat ()
950 {
951 /* If we aren't reading from standard input, we are saving the last
952 thing read from stdin in line and don't want to delete it. Null lines
953 won't repeat here in any case. */
954 if (instream == stdin)
955 *line = 0;
956 }
957 \f
958 /* Read a line from the stream "instream" without command line editing.
959
960 It prints PRROMPT once at the start.
961 Action is compatible with "readline", e.g. space for the result is
962 malloc'd and should be freed by the caller.
963
964 A NULL return means end of file. */
965 char *
966 gdb_readline (prrompt)
967 char *prrompt;
968 {
969 int c;
970 char *result;
971 int input_index = 0;
972 int result_size = 80;
973
974 if (prrompt)
975 {
976 printf (prrompt);
977 fflush (stdout);
978 }
979
980 result = (char *) xmalloc (result_size);
981
982 while (1)
983 {
984 /* Read from stdin if we are executing a user defined command.
985 This is the right thing for prompt_for_continue, at least. */
986 c = fgetc (instream ? instream : stdin);
987
988 if (c == EOF)
989 {
990 free (result);
991 return NULL;
992 }
993
994 if (c == '\n')
995 break;
996
997 result[input_index++] = c;
998 while (input_index >= result_size)
999 {
1000 result_size *= 2;
1001 result = (char *) xrealloc (result, result_size);
1002 }
1003 }
1004
1005 result[input_index++] = '\0';
1006 return result;
1007 }
1008
1009 /* Variables which control command line editing and history
1010 substitution. These variables are given default values at the end
1011 of this file. */
1012 static int command_editing_p;
1013 static int history_expansion_p;
1014 static int write_history_p;
1015 static int history_size;
1016 static char *history_filename;
1017
1018 /* Variables which are necessary for fancy command line editing. */
1019 char *gdb_completer_word_break_characters =
1020 " \t\n!@#$%^&*()-+=|~`}{[]\"';:?/>.<,";
1021
1022 /* Functions that are used as part of the fancy command line editing. */
1023
1024 /* This can be used for functions which don't want to complete on symbols
1025 but don't want to complete on anything else either. */
1026 /* ARGSUSED */
1027 char **
1028 noop_completer (text)
1029 char *text;
1030 {
1031 return NULL;
1032 }
1033
1034 /* Generate symbol names one by one for the completer. If STATE is
1035 zero, then we need to initialize, otherwise the initialization has
1036 already taken place. TEXT is what we expect the symbol to start
1037 with. RL_LINE_BUFFER is available to be looked at; it contains the
1038 entire text of the line. RL_POINT is the offset in that line of
1039 the cursor. You should pretend that the line ends at RL_POINT.
1040 The result is NULL if there are no more completions, else a char
1041 string which is a possible completion. */
1042
1043 static char *
1044 symbol_completion_function (text, state)
1045 char *text;
1046 int state;
1047 {
1048 static char **list = (char **)NULL;
1049 static int index;
1050 char *output;
1051 extern char *rl_line_buffer;
1052 extern int rl_point;
1053 char *tmp_command, *p;
1054 struct cmd_list_element *c, *result_list;
1055
1056 if (!state)
1057 {
1058 /* Free the storage used by LIST, but not by the strings inside. This is
1059 because rl_complete_internal () frees the strings. */
1060 if (list)
1061 free (list);
1062 list = 0;
1063 index = 0;
1064
1065 /* Decide whether to complete on a list of gdb commands or on
1066 symbols. */
1067 tmp_command = (char *) alloca (rl_point + 1);
1068 p = tmp_command;
1069
1070 strncpy (tmp_command, rl_line_buffer, rl_point);
1071 tmp_command[rl_point] = '\0';
1072
1073 if (rl_point == 0)
1074 {
1075 /* An empty line we want to consider ambiguous; that is,
1076 it could be any command. */
1077 c = (struct cmd_list_element *) -1;
1078 result_list = 0;
1079 }
1080 else
1081 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
1082
1083 /* Move p up to the next interesting thing. */
1084 while (*p == ' ' || *p == '\t')
1085 p++;
1086
1087 if (!c)
1088 /* He's typed something unrecognizable. Sigh. */
1089 list = (char **) 0;
1090 else if (c == (struct cmd_list_element *) -1)
1091 {
1092 /* If we didn't recognize everything up to the thing that
1093 needs completing, and we don't know what command it is
1094 yet, we are in trouble. Part of the trouble might be
1095 that the list of delimiters used by readline includes
1096 '-', which we use in commands. Check for this. */
1097 if (p + strlen(text) != tmp_command + rl_point) {
1098 if (tmp_command[rl_point - strlen(text) - 1] == '-')
1099 text = p;
1100 else {
1101 /* This really should not produce an error. Better would
1102 be to pretend to hit RETURN here; this would produce a
1103 response like "Ambiguous command: foo, foobar, etc",
1104 and leave the line available for re-entry with ^P. Instead,
1105 this error blows away the user's typed input without
1106 any way to get it back. */
1107 error (" Unrecognized command.");
1108 }
1109 }
1110
1111 /* He's typed something ambiguous. This is easier. */
1112 if (result_list)
1113 list = complete_on_cmdlist (*result_list->prefixlist, text);
1114 else
1115 list = complete_on_cmdlist (cmdlist, text);
1116 }
1117 else
1118 {
1119 /* If we've gotten this far, gdb has recognized a full
1120 command. There are several possibilities:
1121
1122 1) We need to complete on the command.
1123 2) We need to complete on the possibilities coming after
1124 the command.
1125 2) We need to complete the text of what comes after the
1126 command. */
1127
1128 if (!*p && *text)
1129 /* Always (might be longer versions of thie command). */
1130 list = complete_on_cmdlist (result_list, text);
1131 else if (!*p && !*text)
1132 {
1133 if (c->prefixlist)
1134 list = complete_on_cmdlist (*c->prefixlist, "");
1135 else
1136 list = (*c->completer) ("");
1137 }
1138 else
1139 {
1140 if (c->prefixlist && !c->allow_unknown)
1141 {
1142 #if 0
1143 /* Something like "info adsfkdj". But error() is not
1144 the proper response; just return no completions
1145 instead. */
1146 *p = '\0';
1147 error ("\"%s\" command requires a subcommand.",
1148 tmp_command);
1149 #else
1150 list = NULL;
1151 #endif
1152 }
1153 else
1154 list = (*c->completer) (text);
1155 }
1156 }
1157 }
1158
1159 /* If the debugged program wasn't compiled with symbols, or if we're
1160 clearly completing on a command and no command matches, return
1161 NULL. */
1162 if (!list)
1163 return ((char *)NULL);
1164
1165 output = list[index];
1166 if (output)
1167 index++;
1168
1169 return (output);
1170 }
1171 \f
1172 #ifdef STOP_SIGNAL
1173 static void
1174 stop_sig (signo)
1175 int signo;
1176 {
1177 #if STOP_SIGNAL == SIGTSTP
1178 signal (SIGTSTP, SIG_DFL);
1179 sigsetmask (0);
1180 kill (getpid (), SIGTSTP);
1181 signal (SIGTSTP, stop_sig);
1182 #else
1183 signal (STOP_SIGNAL, stop_sig);
1184 #endif
1185 printf ("%s", prompt);
1186 fflush (stdout);
1187
1188 /* Forget about any previous command -- null line now will do nothing. */
1189 dont_repeat ();
1190 }
1191 #endif /* STOP_SIGNAL */
1192
1193 /* Initialize signal handlers. */
1194 static void
1195 do_nothing (signo)
1196 int signo;
1197 {
1198 }
1199
1200 static void
1201 init_signals ()
1202 {
1203 signal (SIGINT, request_quit);
1204
1205 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1206 passed to the inferior, which we don't want. It would be
1207 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1208 on BSD4.3 systems using vfork, that can affect the
1209 GDB process as well as the inferior (the signal handling tables
1210 might be in memory, shared between the two). Since we establish
1211 a handler for SIGQUIT, when we call exec it will set the signal
1212 to SIG_DFL for us. */
1213 signal (SIGQUIT, do_nothing);
1214 if (signal (SIGHUP, do_nothing) != SIG_IGN)
1215 signal (SIGHUP, disconnect);
1216 signal (SIGFPE, float_handler);
1217 }
1218 \f
1219 /* Read one line from the command input stream `instream'
1220 into the local static buffer `linebuffer' (whose current length
1221 is `linelength').
1222 The buffer is made bigger as necessary.
1223 Returns the address of the start of the line.
1224
1225 NULL is returned for end of file.
1226
1227 *If* the instream == stdin & stdin is a terminal, the line read
1228 is copied into the file line saver (global var char *line,
1229 length linesize) so that it can be duplicated.
1230
1231 This routine either uses fancy command line editing or
1232 simple input as the user has requested. */
1233
1234 char *
1235 command_line_input (prrompt, repeat)
1236 char *prrompt;
1237 int repeat;
1238 {
1239 static char *linebuffer = 0;
1240 static unsigned linelength = 0;
1241 register char *p;
1242 char *p1;
1243 char *rl;
1244 char *local_prompt = prrompt;
1245 register int c;
1246 char *nline;
1247 char got_eof = 0;
1248
1249 if (linebuffer == 0)
1250 {
1251 linelength = 80;
1252 linebuffer = (char *) xmalloc (linelength);
1253 }
1254
1255 p = linebuffer;
1256
1257 /* Control-C quits instantly if typed while in this loop
1258 since it should not wait until the user types a newline. */
1259 immediate_quit++;
1260 #ifdef STOP_SIGNAL
1261 signal (STOP_SIGNAL, stop_sig);
1262 #endif
1263
1264 while (1)
1265 {
1266 /* Reports are that some Sys V's don't flush stdout/err on reads
1267 from stdin, when stdin/out are sockets rather than ttys. So we
1268 have to do it ourselves, to make emacs-gdb and xxgdb work.
1269 On other machines, doing this once per input should be a cheap nop. */
1270 fflush (stdout);
1271 fflush (stderr);
1272
1273 /* Don't use fancy stuff if not talking to stdin. */
1274 if (command_editing_p && instream == stdin
1275 && ISATTY (instream))
1276 rl = readline (local_prompt);
1277 else
1278 rl = gdb_readline (local_prompt);
1279
1280 if (!rl || rl == (char *) EOF)
1281 {
1282 got_eof = 1;
1283 break;
1284 }
1285 if (strlen(rl) + 1 + (p - linebuffer) > linelength)
1286 {
1287 linelength = strlen(rl) + 1 + (p - linebuffer);
1288 nline = (char *) xrealloc (linebuffer, linelength);
1289 p += nline - linebuffer;
1290 linebuffer = nline;
1291 }
1292 p1 = rl;
1293 /* Copy line. Don't copy null at end. (Leaves line alone
1294 if this was just a newline) */
1295 while (*p1)
1296 *p++ = *p1++;
1297
1298 free (rl); /* Allocated in readline. */
1299
1300 if (p == linebuffer || *(p - 1) != '\\')
1301 break;
1302
1303 p--; /* Put on top of '\'. */
1304 local_prompt = (char *) 0;
1305 }
1306
1307 #ifdef STOP_SIGNAL
1308 signal (SIGTSTP, SIG_DFL);
1309 #endif
1310 immediate_quit--;
1311
1312 if (got_eof)
1313 return NULL;
1314
1315 /* Do history expansion if that is wished. */
1316 if (history_expansion_p && instream == stdin
1317 && ISATTY (instream))
1318 {
1319 char *history_value;
1320 int expanded;
1321
1322 *p = '\0'; /* Insert null now. */
1323 expanded = history_expand (linebuffer, &history_value);
1324 if (expanded)
1325 {
1326 /* Print the changes. */
1327 printf ("%s\n", history_value);
1328
1329 /* If there was an error, call this function again. */
1330 if (expanded < 0)
1331 {
1332 free (history_value);
1333 return command_line_input (prrompt, repeat);
1334 }
1335 if (strlen (history_value) > linelength)
1336 {
1337 linelength = strlen (history_value) + 1;
1338 linebuffer = (char *) xrealloc (linebuffer, linelength);
1339 }
1340 strcpy (linebuffer, history_value);
1341 p = linebuffer + strlen(linebuffer);
1342 free (history_value);
1343 }
1344 }
1345
1346 /* If we just got an empty line, and that is supposed
1347 to repeat the previous command, return the value in the
1348 global buffer. */
1349 if (repeat)
1350 {
1351 if (p == linebuffer)
1352 return line;
1353 p1 = linebuffer;
1354 while (*p1 == ' ' || *p1 == '\t')
1355 p1++;
1356 if (!*p1)
1357 return line;
1358 }
1359
1360 *p = 0;
1361
1362 /* Add line to history if appropriate. */
1363 if (instream == stdin
1364 && ISATTY (stdin) && *linebuffer)
1365 add_history (linebuffer);
1366
1367 /* Note: lines consisting soley of comments are added to the command
1368 history. This is useful when you type a command, and then
1369 realize you don't want to execute it quite yet. You can comment
1370 out the command and then later fetch it from the value history
1371 and remove the '#'. The kill ring is probably better, but some
1372 people are in the habit of commenting things out. */
1373 p1 = linebuffer;
1374 while ((c = *p1++) != '\0')
1375 {
1376 if (c == '"')
1377 while ((c = *p1++) != '"')
1378 {
1379 /* Make sure an escaped '"' doesn't make us think the string
1380 is ended. */
1381 if (c == '\\')
1382 parse_escape (&p1);
1383 if (c == '\0')
1384 break;
1385 }
1386 else if (c == '\'')
1387 while ((c = *p1++) != '\'')
1388 {
1389 /* Make sure an escaped '\'' doesn't make us think the string
1390 is ended. */
1391 if (c == '\\')
1392 parse_escape (&p1);
1393 if (c == '\0')
1394 break;
1395 }
1396 else if (c == '#')
1397 {
1398 /* Found a comment. */
1399 p1[-1] = '\0';
1400 break;
1401 }
1402 }
1403
1404 /* Save into global buffer if appropriate. */
1405 if (repeat)
1406 {
1407 if (linelength > linesize)
1408 {
1409 line = xrealloc (line, linelength);
1410 linesize = linelength;
1411 }
1412 strcpy (line, linebuffer);
1413 return line;
1414 }
1415
1416 return linebuffer;
1417 }
1418 \f
1419 /* Read lines from the input stream
1420 and accumulate them in a chain of struct command_line's
1421 which is then returned. */
1422
1423 struct command_line *
1424 read_command_lines ()
1425 {
1426 struct command_line *first = 0;
1427 register struct command_line *next, *tail = 0;
1428 register char *p, *p1;
1429 struct cleanup *old_chain = 0;
1430
1431 while (1)
1432 {
1433 dont_repeat ();
1434 p = command_line_input ((char *) NULL, instream == stdin);
1435 if (p == NULL)
1436 /* Treat end of file like "end". */
1437 break;
1438
1439 /* Remove leading and trailing blanks. */
1440 while (*p == ' ' || *p == '\t') p++;
1441 p1 = p + strlen (p);
1442 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t')) p1--;
1443
1444 /* Is this "end"? */
1445 if (p1 - p == 3 && !strncmp (p, "end", 3))
1446 break;
1447
1448 /* No => add this line to the chain of command lines. */
1449 next = (struct command_line *) xmalloc (sizeof (struct command_line));
1450 next->line = savestring (p, p1 - p);
1451 next->next = 0;
1452 if (tail)
1453 {
1454 tail->next = next;
1455 }
1456 else
1457 {
1458 /* We just read the first line.
1459 From now on, arrange to throw away the lines we have
1460 if we quit or get an error while inside this function. */
1461 first = next;
1462 old_chain = make_cleanup (free_command_lines, &first);
1463 }
1464 tail = next;
1465 }
1466
1467 dont_repeat ();
1468
1469 /* Now we are about to return the chain to our caller,
1470 so freeing it becomes his responsibility. */
1471 if (first)
1472 discard_cleanups (old_chain);
1473 return first;
1474 }
1475
1476 /* Free a chain of struct command_line's. */
1477
1478 void
1479 free_command_lines (lptr)
1480 struct command_line **lptr;
1481 {
1482 register struct command_line *l = *lptr;
1483 register struct command_line *next;
1484
1485 while (l)
1486 {
1487 next = l->next;
1488 free (l->line);
1489 free (l);
1490 l = next;
1491 }
1492 }
1493 \f
1494 /* Add an element to the list of info subcommands. */
1495
1496 void
1497 add_info (name, fun, doc)
1498 char *name;
1499 void (*fun) PARAMS ((char *, int));
1500 char *doc;
1501 {
1502 add_cmd (name, no_class, fun, doc, &infolist);
1503 }
1504
1505 /* Add an alias to the list of info subcommands. */
1506
1507 void
1508 add_info_alias (name, oldname, abbrev_flag)
1509 char *name;
1510 char *oldname;
1511 int abbrev_flag;
1512 {
1513 add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
1514 }
1515
1516 /* The "info" command is defined as a prefix, with allow_unknown = 0.
1517 Therefore, its own definition is called only for "info" with no args. */
1518
1519 /* ARGSUSED */
1520 static void
1521 info_command (arg, from_tty)
1522 char *arg;
1523 int from_tty;
1524 {
1525 printf ("\"info\" must be followed by the name of an info command.\n");
1526 help_list (infolist, "info ", -1, stdout);
1527 }
1528
1529 /* The "show" command with no arguments shows all the settings. */
1530
1531 /* ARGSUSED */
1532 static void
1533 show_command (arg, from_tty)
1534 char *arg;
1535 int from_tty;
1536 {
1537 cmd_show_list (showlist, from_tty, "");
1538 }
1539 \f
1540 /* Add an element to the list of commands. */
1541
1542 void
1543 add_com (name, class, fun, doc)
1544 char *name;
1545 enum command_class class;
1546 void (*fun) PARAMS ((char *, int));
1547 char *doc;
1548 {
1549 add_cmd (name, class, fun, doc, &cmdlist);
1550 }
1551
1552 /* Add an alias or abbreviation command to the list of commands. */
1553
1554 void
1555 add_com_alias (name, oldname, class, abbrev_flag)
1556 char *name;
1557 char *oldname;
1558 enum command_class class;
1559 int abbrev_flag;
1560 {
1561 add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
1562 }
1563
1564 void
1565 error_no_arg (why)
1566 char *why;
1567 {
1568 error ("Argument required (%s).", why);
1569 }
1570
1571 /* ARGSUSED */
1572 static void
1573 help_command (command, from_tty)
1574 char *command;
1575 int from_tty; /* Ignored */
1576 {
1577 help_cmd (command, stdout);
1578 }
1579 \f
1580 static void
1581 validate_comname (comname)
1582 char *comname;
1583 {
1584 register char *p;
1585
1586 if (comname == 0)
1587 error_no_arg ("name of command to define");
1588
1589 p = comname;
1590 while (*p)
1591 {
1592 if (!(*p >= 'A' && *p <= 'Z')
1593 && !(*p >= 'a' && *p <= 'z')
1594 && !(*p >= '0' && *p <= '9')
1595 && *p != '-')
1596 error ("Junk in argument list: \"%s\"", p);
1597 p++;
1598 }
1599 }
1600
1601 #ifdef IBM6000
1602
1603 lowercase (char *str)
1604 {
1605 while (*str) {
1606
1607 /* isupper(), tolower() are function calls in AIX. */
1608 if ( *str >= 'A' && *str <= 'Z')
1609 *str = *str + 'a' - 'A';
1610 ++str;
1611 }
1612 }
1613 #endif
1614
1615 static void
1616 define_command (comname, from_tty)
1617 char *comname;
1618 int from_tty;
1619 {
1620 register struct command_line *cmds;
1621 register struct cmd_list_element *c, *newc;
1622 char *tem = comname;
1623
1624 validate_comname (comname);
1625
1626 #ifdef IBM6000
1627
1628 /* If the rest of the commands will be case insensetive, this one
1629 should behave in the same manner. */
1630
1631 lowercase (comname);
1632 #endif
1633
1634 /* Look it up, and verify that we got an exact match. */
1635 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
1636 if (c && 0 != strcmp (comname, c->name))
1637 c = 0;
1638
1639 if (c)
1640 {
1641 if (c->class == class_user || c->class == class_alias)
1642 tem = "Redefine command \"%s\"? ";
1643 else
1644 tem = "Really redefine built-in command \"%s\"? ";
1645 if (!query (tem, comname))
1646 error ("Command \"%s\" not redefined.", comname);
1647 }
1648
1649 if (from_tty)
1650 {
1651 printf ("Type commands for definition of \"%s\".\n\
1652 End with a line saying just \"end\".\n", comname);
1653 fflush (stdout);
1654 }
1655 comname = savestring (comname, strlen (comname));
1656
1657 cmds = read_command_lines ();
1658
1659 if (c && c->class == class_user)
1660 free_command_lines (&c->user_commands);
1661
1662 newc = add_cmd (comname, class_user, not_just_help_class_command,
1663 (c && c->class == class_user)
1664 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
1665 newc->user_commands = cmds;
1666 }
1667
1668 static void
1669 document_command (comname, from_tty)
1670 char *comname;
1671 int from_tty;
1672 {
1673 struct command_line *doclines;
1674 register struct cmd_list_element *c;
1675 char *tem = comname;
1676
1677 validate_comname (comname);
1678
1679 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1680
1681 if (c->class != class_user)
1682 error ("Command \"%s\" is built-in.", comname);
1683
1684 if (from_tty)
1685 printf ("Type documentation for \"%s\".\n\
1686 End with a line saying just \"end\".\n", comname);
1687
1688 doclines = read_command_lines ();
1689
1690 if (c->doc) free (c->doc);
1691
1692 {
1693 register struct command_line *cl1;
1694 register int len = 0;
1695
1696 for (cl1 = doclines; cl1; cl1 = cl1->next)
1697 len += strlen (cl1->line) + 1;
1698
1699 c->doc = (char *) xmalloc (len + 1);
1700 *c->doc = 0;
1701
1702 for (cl1 = doclines; cl1; cl1 = cl1->next)
1703 {
1704 strcat (c->doc, cl1->line);
1705 if (cl1->next)
1706 strcat (c->doc, "\n");
1707 }
1708 }
1709
1710 free_command_lines (&doclines);
1711 }
1712 \f
1713 static void
1714 print_gnu_advertisement()
1715 {
1716 printf ("\
1717 GDB is free software and you are welcome to distribute copies of it\n\
1718 under certain conditions; type \"show copying\" to see the conditions.\n\
1719 There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\
1720 ");
1721 }
1722
1723 static void
1724 print_gdb_version ()
1725 {
1726 printf_filtered ("\
1727 GDB %s, Copyright 1992 Free Software Foundation, Inc.",
1728 version);
1729 }
1730
1731 /* ARGSUSED */
1732 static void
1733 show_version (args, from_tty)
1734 char *args;
1735 int from_tty;
1736 {
1737 immediate_quit++;
1738 print_gnu_advertisement ();
1739 print_gdb_version ();
1740 printf_filtered ("\n");
1741 immediate_quit--;
1742 }
1743 \f
1744 /* xgdb calls this to reprint the usual GDB prompt. */
1745
1746 void
1747 print_prompt ()
1748 {
1749 printf ("%s", prompt);
1750 fflush (stdout);
1751 }
1752 \f
1753 static void
1754 quit_command (args, from_tty)
1755 char *args;
1756 int from_tty;
1757 {
1758 if (inferior_pid != 0 && target_has_execution)
1759 {
1760 if (query ("The program is running. Quit anyway? "))
1761 {
1762 target_kill ();
1763 }
1764 else
1765 error ("Not confirmed.");
1766 }
1767 /* Save the history information if it is appropriate to do so. */
1768 if (write_history_p && history_filename)
1769 write_history (history_filename);
1770 exit (0);
1771 }
1772
1773 /* Returns whether GDB is running on a terminal and whether the user
1774 desires that questions be asked of them on that terminal. */
1775
1776 int
1777 input_from_terminal_p ()
1778 {
1779 return gdb_has_a_terminal && (instream == stdin) & caution;
1780 }
1781 \f
1782 /* ARGSUSED */
1783 static void
1784 pwd_command (args, from_tty)
1785 char *args;
1786 int from_tty;
1787 {
1788 if (args) error ("The \"pwd\" command does not take an argument: %s", args);
1789 getcwd (dirbuf, sizeof (dirbuf));
1790
1791 if (strcmp (dirbuf, current_directory))
1792 printf ("Working directory %s\n (canonically %s).\n",
1793 current_directory, dirbuf);
1794 else
1795 printf ("Working directory %s.\n", current_directory);
1796 }
1797
1798 static void
1799 cd_command (dir, from_tty)
1800 char *dir;
1801 int from_tty;
1802 {
1803 int len;
1804 int change;
1805
1806 /* If the new directory is absolute, repeat is a no-op; if relative,
1807 repeat might be useful but is more likely to be a mistake. */
1808 dont_repeat ();
1809
1810 if (dir == 0)
1811 error_no_arg ("new working directory");
1812
1813 dir = tilde_expand (dir);
1814 make_cleanup (free, dir);
1815
1816 if (chdir (dir) < 0)
1817 perror_with_name (dir);
1818
1819 len = strlen (dir);
1820 dir = savestring (dir, len - (len > 1 && dir[len-1] == '/'));
1821 if (dir[0] == '/')
1822 current_directory = dir;
1823 else
1824 {
1825 current_directory = concat (current_directory, "/", dir, NULL);
1826 free (dir);
1827 }
1828
1829 /* Now simplify any occurrences of `.' and `..' in the pathname. */
1830
1831 change = 1;
1832 while (change)
1833 {
1834 char *p;
1835 change = 0;
1836
1837 for (p = current_directory; *p;)
1838 {
1839 if (!strncmp (p, "/./", 2)
1840 && (p[2] == 0 || p[2] == '/'))
1841 strcpy (p, p + 2);
1842 else if (!strncmp (p, "/..", 3)
1843 && (p[3] == 0 || p[3] == '/')
1844 && p != current_directory)
1845 {
1846 char *q = p;
1847 while (q != current_directory && q[-1] != '/') q--;
1848 if (q != current_directory)
1849 {
1850 strcpy (q-1, p+3);
1851 p = q-1;
1852 }
1853 }
1854 else p++;
1855 }
1856 }
1857
1858 forget_cached_source_info ();
1859
1860 if (from_tty)
1861 pwd_command ((char *) 0, 1);
1862 }
1863 \f
1864 /* ARGSUSED */
1865 static void
1866 source_command (args, from_tty)
1867 char *args;
1868 int from_tty;
1869 {
1870 FILE *stream;
1871 struct cleanup *cleanups;
1872 char *file = args;
1873
1874 if (file == 0)
1875 /* Let source without arguments read .gdbinit. */
1876 file = gdbinit;
1877
1878 file = tilde_expand (file);
1879 make_cleanup (free, file);
1880
1881 stream = fopen (file, "r");
1882 if (stream == 0)
1883 perror_with_name (file);
1884
1885 cleanups = make_cleanup (fclose, stream);
1886
1887 read_command_file (stream);
1888
1889 do_cleanups (cleanups);
1890 }
1891
1892 /* ARGSUSED */
1893 static void
1894 echo_command (text, from_tty)
1895 char *text;
1896 int from_tty;
1897 {
1898 char *p = text;
1899 register int c;
1900
1901 if (text)
1902 while (c = *p++)
1903 {
1904 if (c == '\\')
1905 {
1906 /* \ at end of argument is used after spaces
1907 so they won't be lost. */
1908 if (*p == 0)
1909 return;
1910
1911 c = parse_escape (&p);
1912 if (c >= 0)
1913 printf_filtered ("%c", c);
1914 }
1915 else
1916 printf_filtered ("%c", c);
1917 }
1918
1919 /* Force this output to appear now. */
1920 wrap_here ("");
1921 fflush (stdout);
1922 }
1923
1924 /* ARGSUSED */
1925 static void
1926 dump_me_command (args, from_tty)
1927 char *args;
1928 int from_tty;
1929 {
1930 if (query ("Should GDB dump core? "))
1931 {
1932 signal (SIGQUIT, SIG_DFL);
1933 kill (getpid (), SIGQUIT);
1934 }
1935 }
1936 \f
1937 /* Functions to manipulate command line editing control variables. */
1938
1939 /* Number of commands to print in each call to show_commands. */
1940 #define Hist_print 10
1941 static void
1942 show_commands (args, from_tty)
1943 char *args;
1944 int from_tty;
1945 {
1946 /* Index for history commands. Relative to history_base. */
1947 int offset;
1948
1949 /* Number of the history entry which we are planning to display next.
1950 Relative to history_base. */
1951 static int num = 0;
1952
1953 /* The first command in the history which doesn't exist (i.e. one more
1954 than the number of the last command). Relative to history_base. */
1955 int hist_len;
1956
1957 extern struct _hist_entry *history_get PARAMS ((int));
1958 extern int history_base;
1959
1960 /* Print out some of the commands from the command history. */
1961 /* First determine the length of the history list. */
1962 hist_len = history_size;
1963 for (offset = 0; offset < history_size; offset++)
1964 {
1965 if (!history_get (history_base + offset))
1966 {
1967 hist_len = offset;
1968 break;
1969 }
1970 }
1971
1972 if (args)
1973 {
1974 if (args[0] == '+' && args[1] == '\0')
1975 /* "info editing +" should print from the stored position. */
1976 ;
1977 else
1978 /* "info editing <exp>" should print around command number <exp>. */
1979 num = (parse_and_eval_address (args) - history_base) - Hist_print / 2;
1980 }
1981 /* "show commands" means print the last Hist_print commands. */
1982 else
1983 {
1984 num = hist_len - Hist_print;
1985 }
1986
1987 if (num < 0)
1988 num = 0;
1989
1990 /* If there are at least Hist_print commands, we want to display the last
1991 Hist_print rather than, say, the last 6. */
1992 if (hist_len - num < Hist_print)
1993 {
1994 num = hist_len - Hist_print;
1995 if (num < 0)
1996 num = 0;
1997 }
1998
1999 for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
2000 {
2001 printf_filtered ("%5d %s\n", history_base + offset,
2002 (history_get (history_base + offset))->line);
2003 }
2004
2005 /* The next command we want to display is the next one that we haven't
2006 displayed yet. */
2007 num += Hist_print;
2008
2009 /* If the user repeats this command with return, it should do what
2010 "show commands +" does. This is unnecessary if arg is null,
2011 because "show commands +" is not useful after "show commands". */
2012 if (from_tty && args)
2013 {
2014 args[0] = '+';
2015 args[1] = '\0';
2016 }
2017 }
2018
2019 /* Called by do_setshow_command. */
2020 /* ARGSUSED */
2021 static void
2022 set_history_size_command (args, from_tty, c)
2023 char *args;
2024 int from_tty;
2025 struct cmd_list_element *c;
2026 {
2027 if (history_size == UINT_MAX)
2028 unstifle_history ();
2029 else
2030 stifle_history (history_size);
2031 }
2032
2033 /* ARGSUSED */
2034 static void
2035 set_history (args, from_tty)
2036 char *args;
2037 int from_tty;
2038 {
2039 printf ("\"set history\" must be followed by the name of a history subcommand.\n");
2040 help_list (sethistlist, "set history ", -1, stdout);
2041 }
2042
2043 /* ARGSUSED */
2044 static void
2045 show_history (args, from_tty)
2046 char *args;
2047 int from_tty;
2048 {
2049 cmd_show_list (showhistlist, from_tty, "");
2050 }
2051
2052 int info_verbose = 0; /* Default verbose msgs off */
2053
2054 /* Called by do_setshow_command. An elaborate joke. */
2055 /* ARGSUSED */
2056 static void
2057 set_verbose (args, from_tty, c)
2058 char *args;
2059 int from_tty;
2060 struct cmd_list_element *c;
2061 {
2062 char *cmdname = "verbose";
2063 struct cmd_list_element *showcmd;
2064
2065 showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
2066
2067 if (info_verbose)
2068 {
2069 c->doc = "Set verbose printing of informational messages.";
2070 showcmd->doc = "Show verbose printing of informational messages.";
2071 }
2072 else
2073 {
2074 c->doc = "Set verbosity.";
2075 showcmd->doc = "Show verbosity.";
2076 }
2077 }
2078
2079 static void
2080 float_handler (signo)
2081 int signo;
2082 {
2083 /* This message is based on ANSI C, section 4.7. Note that integer
2084 divide by zero causes this, so "float" is a misnomer. */
2085 error ("Erroneous arithmetic operation.");
2086 }
2087
2088 /* Return whether we are running a batch file or from terminal. */
2089 int
2090 batch_mode ()
2091 {
2092 return !(instream == stdin && ISATTY (stdin));
2093 }
2094
2095 \f
2096 static void
2097 initialize_cmd_lists ()
2098 {
2099 cmdlist = (struct cmd_list_element *) 0;
2100 infolist = (struct cmd_list_element *) 0;
2101 enablelist = (struct cmd_list_element *) 0;
2102 disablelist = (struct cmd_list_element *) 0;
2103 deletelist = (struct cmd_list_element *) 0;
2104 enablebreaklist = (struct cmd_list_element *) 0;
2105 setlist = (struct cmd_list_element *) 0;
2106 showlist = NULL;
2107 sethistlist = (struct cmd_list_element *) 0;
2108 showhistlist = NULL;
2109 unsethistlist = (struct cmd_list_element *) 0;
2110 }
2111
2112 /* Init the history buffer. Note that we are called after the init file(s)
2113 * have been read so that the user can change the history file via his
2114 * .gdbinit file (for instance). The GDBHISTFILE environment variable
2115 * overrides all of this.
2116 */
2117
2118 static void
2119 initialize_history()
2120 {
2121 char *tmpenv;
2122
2123 tmpenv = getenv ("HISTSIZE");
2124 if (tmpenv)
2125 history_size = atoi (tmpenv);
2126 else if (!history_size)
2127 history_size = 256;
2128
2129 stifle_history (history_size);
2130
2131 tmpenv = getenv ("GDBHISTFILE");
2132 if (tmpenv)
2133 history_filename = savestring (tmpenv, strlen(tmpenv));
2134 else if (!history_filename) {
2135 /* We include the current directory so that if the user changes
2136 directories the file written will be the same as the one
2137 that was read. */
2138 history_filename = concat (current_directory, "/.gdb_history", NULL);
2139 }
2140 read_history (history_filename);
2141 }
2142
2143 static void
2144 initialize_main ()
2145 {
2146 struct cmd_list_element *c;
2147
2148 #ifdef DEFAULT_PROMPT
2149 prompt = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT));
2150 #else
2151 prompt = savestring ("(gdb) ", 6);
2152 #endif
2153
2154 /* Set the important stuff up for command editing. */
2155 command_editing_p = 1;
2156 history_expansion_p = 0;
2157 write_history_p = 0;
2158
2159 /* Setup important stuff for command line editing. */
2160 rl_completion_entry_function = (int (*)()) symbol_completion_function;
2161 rl_completer_word_break_characters = gdb_completer_word_break_characters;
2162 rl_readline_name = "gdb";
2163
2164 /* Define the classes of commands.
2165 They will appear in the help list in the reverse of this order. */
2166
2167 add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist);
2168 add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist);
2169 add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\
2170 The commands in this class are those defined by the user.\n\
2171 Use the \"define\" command to define a command.", &cmdlist);
2172 add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist);
2173 add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist);
2174 add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist);
2175 add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist);
2176 add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist);
2177 add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\
2178 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
2179 counting from zero for the innermost (currently executing) frame.\n\n\
2180 At any time gdb identifies one frame as the \"selected\" frame.\n\
2181 Variable lookups are done with respect to the selected frame.\n\
2182 When the program being debugged stops, gdb selects the innermost frame.\n\
2183 The commands below can be used to select other frames by number or address.",
2184 &cmdlist);
2185 add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist);
2186
2187 add_com ("pwd", class_files, pwd_command,
2188 "Print working directory. This is used for your program as well.");
2189 add_com ("cd", class_files, cd_command,
2190 "Set working directory to DIR for debugger and program being debugged.\n\
2191 The change does not take effect for the program being debugged\n\
2192 until the next time it is started.");
2193
2194 add_show_from_set
2195 (add_set_cmd ("prompt", class_support, var_string, (char *)&prompt,
2196 "Set gdb's prompt",
2197 &setlist),
2198 &showlist);
2199
2200 add_com ("echo", class_support, echo_command,
2201 "Print a constant string. Give string as argument.\n\
2202 C escape sequences may be used in the argument.\n\
2203 No newline is added at the end of the argument;\n\
2204 use \"\\n\" if you want a newline to be printed.\n\
2205 Since leading and trailing whitespace are ignored in command arguments,\n\
2206 if you want to print some you must use \"\\\" before leading whitespace\n\
2207 to be printed or after trailing whitespace.");
2208 add_com ("document", class_support, document_command,
2209 "Document a user-defined command.\n\
2210 Give command name as argument. Give documentation on following lines.\n\
2211 End with a line of just \"end\".");
2212 add_com ("define", class_support, define_command,
2213 "Define a new command name. Command name is argument.\n\
2214 Definition appears on following lines, one command per line.\n\
2215 End with a line of just \"end\".\n\
2216 Use the \"document\" command to give documentation for the new command.\n\
2217 Commands defined in this way do not take arguments.");
2218
2219 #ifdef __STDC__
2220 add_com ("source", class_support, source_command,
2221 "Read commands from a file named FILE.\n\
2222 Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\
2223 when gdb is started.");
2224 #else
2225 /* Punt file name, we can't help it easily. */
2226 add_com ("source", class_support, source_command,
2227 "Read commands from a file named FILE.\n\
2228 Note that the file \".gdbinit\" is read automatically in this way\n\
2229 when gdb is started.");
2230 #endif
2231
2232 add_com ("quit", class_support, quit_command, "Exit gdb.");
2233 add_com ("help", class_support, help_command, "Print list of commands.");
2234 add_com_alias ("q", "quit", class_support, 1);
2235 add_com_alias ("h", "help", class_support, 1);
2236
2237
2238 c = add_set_cmd ("verbose", class_support, var_boolean, (char *)&info_verbose,
2239 "Set ",
2240 &setlist),
2241 add_show_from_set (c, &showlist);
2242 c->function.sfunc = set_verbose;
2243 set_verbose (NULL, 0, c);
2244
2245 add_com ("dump-me", class_obscure, dump_me_command,
2246 "Get fatal error; make debugger dump its core.");
2247
2248 add_show_from_set
2249 (add_set_cmd ("editing", class_support, var_boolean, (char *)&command_editing_p,
2250 "Set editing of command lines as they are typed.\n\
2251 Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
2252 Without an argument, command line editing is enabled. To edit, use\n\
2253 EMACS-like or VI-like commands like control-P or ESC.", &setlist),
2254 &showlist);
2255
2256 add_prefix_cmd ("history", class_support, set_history,
2257 "Generic command for setting command history parameters.",
2258 &sethistlist, "set history ", 0, &setlist);
2259 add_prefix_cmd ("history", class_support, show_history,
2260 "Generic command for showing command history parameters.",
2261 &showhistlist, "show history ", 0, &showlist);
2262
2263 add_show_from_set
2264 (add_set_cmd ("expansion", no_class, var_boolean, (char *)&history_expansion_p,
2265 "Set history expansion on command input.\n\
2266 Without an argument, history expansion is enabled.", &sethistlist),
2267 &showhistlist);
2268
2269 add_show_from_set
2270 (add_set_cmd ("save", no_class, var_boolean, (char *)&write_history_p,
2271 "Set saving of the history record on exit.\n\
2272 Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
2273 Without an argument, saving is enabled.", &sethistlist),
2274 &showhistlist);
2275
2276 c = add_set_cmd ("size", no_class, var_uinteger, (char *)&history_size,
2277 "Set the size of the command history, \n\
2278 ie. the number of previous commands to keep a record of.", &sethistlist);
2279 add_show_from_set (c, &showhistlist);
2280 c->function.sfunc = set_history_size_command;
2281
2282 add_show_from_set
2283 (add_set_cmd ("filename", no_class, var_filename, (char *)&history_filename,
2284 "Set the filename in which to record the command history\n\
2285 (the list of previous commands of which a record is kept).", &sethistlist),
2286 &showhistlist);
2287
2288 add_show_from_set
2289 (add_set_cmd ("confirm", class_support, var_boolean,
2290 (char *)&caution,
2291 "Set whether to confirm potentially dangerous operations.",
2292 &setlist),
2293 &showlist);
2294
2295 add_prefix_cmd ("info", class_info, info_command,
2296 "Generic command for showing things about the program being debugged.",
2297 &infolist, "info ", 0, &cmdlist);
2298 add_com_alias ("i", "info", class_info, 1);
2299
2300 add_prefix_cmd ("show", class_info, show_command,
2301 "Generic command for showing things about the debugger.",
2302 &showlist, "show ", 0, &cmdlist);
2303 /* Another way to get at the same thing. */
2304 add_info ("set", show_command, "Show all GDB settings.");
2305
2306 add_cmd ("commands", no_class, show_commands,
2307 "Show the the history of commands you typed.\n\
2308 You can supply a command number to start with, or a `+' to start after\n\
2309 the previous command number shown.",
2310 &showlist);
2311
2312 add_cmd ("version", no_class, show_version,
2313 "Show what version of GDB this is.", &showlist);
2314 }