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