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