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