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