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