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