]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/top.c
40955abe63d3a4e362cbdb40d210a13b2540ce9a
[thirdparty/binutils-gdb.git] / gdb / top.c
1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 1999
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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "defs.h"
23 #include "gdbcmd.h"
24 #include "call-cmds.h"
25 #include "symtab.h"
26 #include "inferior.h"
27 #include "signals.h"
28 #include "target.h"
29 #include "breakpoint.h"
30 #include "gdbtypes.h"
31 #include "expression.h"
32 #include "value.h"
33 #include "language.h"
34 #include "terminal.h" /* For job_control. */
35 #include "annotate.h"
36 #include "top.h"
37
38 /* readline include files */
39 #include <readline/readline.h>
40 #include <readline/history.h>
41
42 /* readline defines this. */
43 #undef savestring
44
45 #include <sys/types.h>
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49
50 #include "event-loop.h"
51 #include "gdb_string.h"
52 #include "gdb_stat.h"
53 #include <ctype.h>
54
55 /* Prototypes for local functions */
56
57 static void dont_repeat_command PARAMS ((char *, int));
58
59 static void source_cleanup_lines PARAMS ((PTR));
60
61 static void user_defined_command PARAMS ((char *, int));
62
63 static void init_signals PARAMS ((void));
64
65 #ifdef STOP_SIGNAL
66 static void stop_sig PARAMS ((int));
67 #endif
68
69 static char *line_completion_function PARAMS ((char *, int, char *, int));
70
71 static char *readline_line_completion_function PARAMS ((char *, int));
72
73 /* NOTE 1999-04-29: this function will be static again, after we make the
74 event loop be the default command loop for gdb, and we merge
75 event-top.c into this file, top.c */
76 /* static */ void command_loop_marker PARAMS ((int));
77
78 static void while_command PARAMS ((char *, int));
79
80 static void if_command PARAMS ((char *, int));
81
82 static struct command_line *
83 build_command_line PARAMS ((enum command_control_type, char *));
84
85 static struct command_line *
86 get_command_line PARAMS ((enum command_control_type, char *));
87
88 static void realloc_body_list PARAMS ((struct command_line *, int));
89
90 static enum misc_command_type read_next_line PARAMS ((struct command_line **));
91
92 static enum command_control_type
93 recurse_read_control_structure PARAMS ((struct command_line *));
94
95 static struct cleanup *setup_user_args PARAMS ((char *));
96
97 static char *locate_arg PARAMS ((char *));
98
99 static char *insert_args PARAMS ((char *));
100
101 static void arg_cleanup PARAMS ((void));
102
103 static void init_main PARAMS ((void));
104
105 static void init_cmd_lists PARAMS ((void));
106
107 static void float_handler PARAMS ((int));
108
109 static void init_signals PARAMS ((void));
110
111 static void set_verbose PARAMS ((char *, int, struct cmd_list_element *));
112
113 static void show_history PARAMS ((char *, int));
114
115 static void set_history PARAMS ((char *, int));
116
117 static void set_history_size_command PARAMS ((char *, int,
118 struct cmd_list_element *));
119
120 static void show_commands PARAMS ((char *, int));
121
122 static void echo_command PARAMS ((char *, int));
123
124 static void pwd_command PARAMS ((char *, int));
125
126 static void show_version PARAMS ((char *, int));
127
128 static void document_command PARAMS ((char *, int));
129
130 static void define_command PARAMS ((char *, int));
131
132 static void validate_comname PARAMS ((char *));
133
134 static void help_command PARAMS ((char *, int));
135
136 static void show_command PARAMS ((char *, int));
137
138 static void info_command PARAMS ((char *, int));
139
140 static void complete_command PARAMS ((char *, int));
141
142 static void do_nothing PARAMS ((int));
143
144 #ifdef SIGHUP
145 /* NOTE 1999-04-29: This function will be static again, once we modify
146 gdb to use the event loop as the default command loop and we merge
147 event-top.c into this file, top.c */
148 /* static */ int quit_cover PARAMS ((PTR));
149
150 static void disconnect PARAMS ((int));
151 #endif
152
153 static void source_cleanup PARAMS ((FILE *));
154
155 /* If this definition isn't overridden by the header files, assume
156 that isatty and fileno exist on this system. */
157 #ifndef ISATTY
158 #define ISATTY(FP) (isatty (fileno (FP)))
159 #endif
160
161 /* Initialization file name for gdb. This is overridden in some configs. */
162
163 #ifndef GDBINIT_FILENAME
164 #define GDBINIT_FILENAME ".gdbinit"
165 #endif
166 char gdbinit[] = GDBINIT_FILENAME;
167
168 int inhibit_gdbinit = 0;
169
170 /* If nonzero, and GDB has been configured to be able to use windows,
171 attempt to open them upon startup. */
172
173 int use_windows = 1;
174
175 /* Version number of GDB, as a string. */
176
177 extern char *version;
178
179 /* Canonical host name as a string. */
180
181 extern char *host_name;
182
183 /* Canonical target name as a string. */
184
185 extern char *target_name;
186
187 extern char lang_frame_mismatch_warn[]; /* language.c */
188
189 /* Flag for whether we want all the "from_tty" gubbish printed. */
190
191 int caution = 1; /* Default is yes, sigh. */
192
193 /* Define all cmd_list_elements. */
194
195 /* Chain containing all defined commands. */
196
197 struct cmd_list_element *cmdlist;
198
199 /* Chain containing all defined info subcommands. */
200
201 struct cmd_list_element *infolist;
202
203 /* Chain containing all defined enable subcommands. */
204
205 struct cmd_list_element *enablelist;
206
207 /* Chain containing all defined disable subcommands. */
208
209 struct cmd_list_element *disablelist;
210
211 /* Chain containing all defined toggle subcommands. */
212
213 struct cmd_list_element *togglelist;
214
215 /* Chain containing all defined stop subcommands. */
216
217 struct cmd_list_element *stoplist;
218
219 /* Chain containing all defined delete subcommands. */
220
221 struct cmd_list_element *deletelist;
222
223 /* Chain containing all defined "enable breakpoint" subcommands. */
224
225 struct cmd_list_element *enablebreaklist;
226
227 /* Chain containing all defined set subcommands */
228
229 struct cmd_list_element *setlist;
230
231 /* Chain containing all defined unset subcommands */
232
233 struct cmd_list_element *unsetlist;
234
235 /* Chain containing all defined show subcommands. */
236
237 struct cmd_list_element *showlist;
238
239 /* Chain containing all defined \"set history\". */
240
241 struct cmd_list_element *sethistlist;
242
243 /* Chain containing all defined \"show history\". */
244
245 struct cmd_list_element *showhistlist;
246
247 /* Chain containing all defined \"unset history\". */
248
249 struct cmd_list_element *unsethistlist;
250
251 /* Chain containing all defined maintenance subcommands. */
252
253 struct cmd_list_element *maintenancelist;
254
255 /* Chain containing all defined "maintenance info" subcommands. */
256
257 struct cmd_list_element *maintenanceinfolist;
258
259 /* Chain containing all defined "maintenance print" subcommands. */
260
261 struct cmd_list_element *maintenanceprintlist;
262
263 struct cmd_list_element *setprintlist;
264
265 struct cmd_list_element *showprintlist;
266
267 struct cmd_list_element *setchecklist;
268
269 struct cmd_list_element *showchecklist;
270
271 /* stdio stream that command input is being read from. Set to stdin normally.
272 Set by source_command to the file we are sourcing. Set to NULL if we are
273 executing a user-defined command or interacting via a GUI. */
274
275 FILE *instream;
276
277 /* Current working directory. */
278
279 char *current_directory;
280
281 /* The directory name is actually stored here (usually). */
282 char gdb_dirbuf[1024];
283
284 /* Function to call before reading a command, if nonzero.
285 The function receives two args: an input stream,
286 and a prompt string. */
287
288 void (*window_hook) PARAMS ((FILE *, char *));
289
290 int epoch_interface;
291 int xgdb_verbose;
292
293 /* gdb prints this when reading a command interactively */
294 static char *gdb_prompt_string; /* the global prompt string */
295 extern char *get_prompt PARAMS ((void)); /* access function for prompt string */
296
297 /* Buffer used for reading command lines, and the size
298 allocated for it so far. */
299
300 char *line;
301 int linesize = 100;
302
303 /* Nonzero if the current command is modified by "server ". This
304 affects things like recording into the command history, comamnds
305 repeating on RETURN, etc. This is so a user interface (emacs, GUI,
306 whatever) can issue its own commands and also send along commands
307 from the user, and have the user not notice that the user interface
308 is issuing commands too. */
309 int server_command;
310
311 /* Baud rate specified for talking to serial target systems. Default
312 is left as -1, so targets can choose their own defaults. */
313 /* FIXME: This means that "show remotebaud" and gr_files_info can print -1
314 or (unsigned int)-1. This is a Bad User Interface. */
315
316 int baud_rate = -1;
317
318 /* Timeout limit for response from target. */
319
320 int remote_timeout = 20; /* Set default to 20 */
321
322 /* Non-zero tells remote* modules to output debugging info. */
323
324 int remote_debug = 0;
325
326 /* Non-zero means the target is running. Note: this is different from
327 saying that there is an active target and we are stopped at a
328 breakpoint, for instance. This is a real indicator whether the
329 target is off and running, which gdb is doing something else. */
330 int target_executing = 0;
331
332 /* Level of control structure. */
333 static int control_level;
334
335 /* Structure for arguments to user defined functions. */
336 #define MAXUSERARGS 10
337 struct user_args
338 {
339 struct user_args *next;
340 struct
341 {
342 char *arg;
343 int len;
344 }
345 a[MAXUSERARGS];
346 int count;
347 }
348 *user_args;
349
350 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
351
352 #ifndef STOP_SIGNAL
353 #ifdef SIGTSTP
354 #define STOP_SIGNAL SIGTSTP
355 static void stop_sig PARAMS ((int));
356 #endif
357 #endif
358
359 /* Some System V have job control but not sigsetmask(). */
360 #if !defined (HAVE_SIGSETMASK)
361 #if !defined (USG)
362 #define HAVE_SIGSETMASK 1
363 #else
364 #define HAVE_SIGSETMASK 0
365 #endif
366 #endif
367
368 #if 0 == (HAVE_SIGSETMASK)
369 #define sigsetmask(n)
370 #endif
371
372 /* Hooks for alternate command interfaces. */
373
374 /* Called after most modules have been initialized, but before taking users
375 command file. */
376
377 void (*init_ui_hook) PARAMS ((char *argv0));
378
379 /* This hook is called from within gdb's many mini-event loops which could
380 steal control from a real user interface's event loop. It returns
381 non-zero if the user is requesting a detach, zero otherwise. */
382
383 int (*ui_loop_hook) PARAMS ((int));
384
385 /* Called instead of command_loop at top level. Can be invoked via
386 return_to_top_level. */
387
388 void (*command_loop_hook) PARAMS ((void));
389
390
391 /* Called instead of fputs for all output. */
392
393 void (*fputs_unfiltered_hook) PARAMS ((const char *linebuffer, GDB_FILE * stream));
394
395 /* Called from print_frame_info to list the line we stopped in. */
396
397 void (*print_frame_info_listing_hook) PARAMS ((struct symtab * s, int line,
398 int stopline, int noerror));
399 /* Replaces most of query. */
400
401 int (*query_hook) PARAMS ((const char *, va_list));
402
403 /* Replaces most of warning. */
404
405 void (*warning_hook) PARAMS ((const char *, va_list));
406
407 /* Called from gdb_flush to flush output. */
408
409 void (*flush_hook) PARAMS ((GDB_FILE * stream));
410
411 /* These three functions support getting lines of text from the user. They
412 are used in sequence. First readline_begin_hook is called with a text
413 string that might be (for example) a message for the user to type in a
414 sequence of commands to be executed at a breakpoint. If this function
415 calls back to a GUI, it might take this opportunity to pop up a text
416 interaction window with this message. Next, readline_hook is called
417 with a prompt that is emitted prior to collecting the user input.
418 It can be called multiple times. Finally, readline_end_hook is called
419 to notify the GUI that we are done with the interaction window and it
420 can close it. */
421
422 void (*readline_begin_hook) PARAMS ((char *,...));
423 char *(*readline_hook) PARAMS ((char *));
424 void (*readline_end_hook) PARAMS ((void));
425
426 /* Called as appropriate to notify the interface of the specified breakpoint
427 conditions. */
428
429 void (*create_breakpoint_hook) PARAMS ((struct breakpoint * bpt));
430 void (*delete_breakpoint_hook) PARAMS ((struct breakpoint * bpt));
431 void (*modify_breakpoint_hook) PARAMS ((struct breakpoint * bpt));
432
433 /* Called during long calculations to allow GUI to repair window damage, and to
434 check for stop buttons, etc... */
435
436 void (*interactive_hook) PARAMS ((void));
437
438 /* Called when the registers have changed, as a hint to a GUI
439 to minimize window update. */
440
441 void (*registers_changed_hook) PARAMS ((void));
442
443 /* Tell the GUI someone changed the register REGNO. -1 means
444 that the caller does not know which register changed or
445 that several registers have changed (see value_assign). */
446 void (*register_changed_hook) PARAMS ((int regno));
447
448 /* Tell the GUI that the disassembly flavor has changed */
449 void (*disassembly_flavor_hook) PARAMS((char *args, int from_tty));
450
451 /* Tell the GUI someone changed LEN bytes of memory at ADDR */
452 void (*memory_changed_hook) PARAMS ((CORE_ADDR addr, int len));
453
454 /* Called when going to wait for the target. Usually allows the GUI to run
455 while waiting for target events. */
456
457 int (*target_wait_hook) PARAMS ((int pid, struct target_waitstatus * status));
458
459 /* Used by UI as a wrapper around command execution. May do various things
460 like enabling/disabling buttons, etc... */
461
462 void (*call_command_hook) PARAMS ((struct cmd_list_element * c, char *cmd,
463 int from_tty));
464
465 /* Called after a `set' command has finished. Is only run if the
466 `set' command succeeded. */
467
468 void (*set_hook) PARAMS ((struct cmd_list_element *c));
469
470 /* Called when the current thread changes. Argument is thread id. */
471
472 void (*context_hook) PARAMS ((int id));
473
474 /* Takes control from error (). Typically used to prevent longjmps out of the
475 middle of the GUI. Usually used in conjunction with a catch routine. */
476
477 NORETURN void (*error_hook)
478 PARAMS ((void)) ATTR_NORETURN;
479 \f
480
481 /* Where to go for return_to_top_level (RETURN_ERROR). */
482 SIGJMP_BUF error_return;
483 /* Where to go for return_to_top_level (RETURN_QUIT). */
484 SIGJMP_BUF quit_return;
485
486 /* Return for reason REASON. This generally gets back to the command
487 loop, but can be caught via catch_errors. */
488
489 NORETURN void
490 return_to_top_level (reason)
491 enum return_reason reason;
492 {
493 quit_flag = 0;
494 immediate_quit = 0;
495
496 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
497 I can think of a reason why that is vital, though). */
498 bpstat_clear_actions (stop_bpstat); /* Clear queued breakpoint commands */
499
500 disable_current_display ();
501 do_cleanups (ALL_CLEANUPS);
502 if (async_p && target_has_async)
503 do_exec_cleanups (ALL_CLEANUPS);
504
505 if (annotation_level > 1)
506 switch (reason)
507 {
508 case RETURN_QUIT:
509 annotate_quit ();
510 break;
511 case RETURN_ERROR:
512 annotate_error ();
513 break;
514 }
515
516 (NORETURN void) SIGLONGJMP
517 (reason == RETURN_ERROR ? error_return : quit_return, 1);
518 }
519
520 /* Call FUNC with arg ARGS, catching any errors. If there is no
521 error, return the value returned by FUNC. If there is an error,
522 print ERRSTRING, print the specific error message, then return
523 zero.
524
525 Must not be called with immediate_quit in effect (bad things might
526 happen, say we got a signal in the middle of a memcpy to quit_return).
527 This is an OK restriction; with very few exceptions immediate_quit can
528 be replaced by judicious use of QUIT.
529
530 MASK specifies what to catch; it is normally set to
531 RETURN_MASK_ALL, if for no other reason than that the code which
532 calls catch_errors might not be set up to deal with a quit which
533 isn't caught. But if the code can deal with it, it generally
534 should be RETURN_MASK_ERROR, unless for some reason it is more
535 useful to abort only the portion of the operation inside the
536 catch_errors. Note that quit should return to the command line
537 fairly quickly, even if some further processing is being done. */
538
539 int
540 catch_errors (func, args, errstring, mask)
541 catch_errors_ftype *func;
542 PTR args;
543 char *errstring;
544 return_mask mask;
545 {
546 SIGJMP_BUF saved_error;
547 SIGJMP_BUF saved_quit;
548 SIGJMP_BUF tmp_jmp;
549 int val;
550 struct cleanup *saved_cleanup_chain;
551 char *saved_error_pre_print;
552 char *saved_quit_pre_print;
553
554 saved_cleanup_chain = save_cleanups ();
555 saved_error_pre_print = error_pre_print;
556 saved_quit_pre_print = quit_pre_print;
557
558 if (mask & RETURN_MASK_ERROR)
559 {
560 memcpy ((char *) saved_error, (char *) error_return, sizeof (SIGJMP_BUF));
561 error_pre_print = errstring;
562 }
563 if (mask & RETURN_MASK_QUIT)
564 {
565 memcpy (saved_quit, quit_return, sizeof (SIGJMP_BUF));
566 quit_pre_print = errstring;
567 }
568
569 if (SIGSETJMP (tmp_jmp) == 0)
570 {
571 if (mask & RETURN_MASK_ERROR)
572 memcpy (error_return, tmp_jmp, sizeof (SIGJMP_BUF));
573 if (mask & RETURN_MASK_QUIT)
574 memcpy (quit_return, tmp_jmp, sizeof (SIGJMP_BUF));
575 val = (*func) (args);
576 }
577 else
578 val = 0;
579
580 restore_cleanups (saved_cleanup_chain);
581
582 if (mask & RETURN_MASK_ERROR)
583 {
584 memcpy (error_return, saved_error, sizeof (SIGJMP_BUF));
585 error_pre_print = saved_error_pre_print;
586 }
587 if (mask & RETURN_MASK_QUIT)
588 {
589 memcpy (quit_return, saved_quit, sizeof (SIGJMP_BUF));
590 quit_pre_print = saved_quit_pre_print;
591 }
592 return val;
593 }
594
595 /* Handler for SIGHUP. */
596
597 #ifdef SIGHUP
598 static void
599 disconnect (signo)
600 int signo;
601 {
602 catch_errors (quit_cover, NULL,
603 "Could not kill the program being debugged", RETURN_MASK_ALL);
604 signal (SIGHUP, SIG_DFL);
605 kill (getpid (), SIGHUP);
606 }
607
608 /* Just a little helper function for disconnect(). */
609
610 /* NOTE 1999-04-29: This function will be static again, once we modify
611 gdb to use the event loop as the default command loop and we merge
612 event-top.c into this file, top.c */
613 /* static */ int
614 quit_cover (s)
615 PTR s;
616 {
617 caution = 0; /* Throw caution to the wind -- we're exiting.
618 This prevents asking the user dumb questions. */
619 quit_command ((char *) 0, 0);
620 return 0;
621 }
622 #endif /* defined SIGHUP */
623 \f
624 /* Line number we are currently in in a file which is being sourced. */
625 /* NOTE 1999-04-29: This variable will be static again, once we modify
626 gdb to use the event loop as the default command loop and we merge
627 event-top.c into this file, top.c */
628 /* static */ int source_line_number;
629
630 /* Name of the file we are sourcing. */
631 /* NOTE 1999-04-29: This variable will be static again, once we modify
632 gdb to use the event loop as the default command loop and we merge
633 event-top.c into this file, top.c */
634 /* static */ char *source_file_name;
635
636 /* Buffer containing the error_pre_print used by the source stuff.
637 Malloc'd. */
638 /* NOTE 1999-04-29: This variable will be static again, once we modify
639 gdb to use the event loop as the default command loop and we merge
640 event-top.c into this file, top.c */
641 /* static */ char *source_error;
642 static int source_error_allocated;
643
644 /* Something to glom on to the start of error_pre_print if source_file_name
645 is set. */
646 /* NOTE 1999-04-29: This variable will be static again, once we modify
647 gdb to use the event loop as the default command loop and we merge
648 event-top.c into this file, top.c */
649 /* static */ char *source_pre_error;
650
651 /* Clean up on error during a "source" command (or execution of a
652 user-defined command). */
653
654 static void
655 source_cleanup (stream)
656 FILE *stream;
657 {
658 /* Restore the previous input stream. */
659 instream = stream;
660 }
661
662 /* Read commands from STREAM. */
663 void
664 read_command_file (stream)
665 FILE *stream;
666 {
667 struct cleanup *cleanups;
668
669 cleanups = make_cleanup ((make_cleanup_func) source_cleanup, instream);
670 instream = stream;
671 command_loop ();
672 do_cleanups (cleanups);
673 }
674 \f
675 extern void init_proc PARAMS ((void));
676
677 void (*pre_init_ui_hook) PARAMS ((void));
678
679 void
680 gdb_init (argv0)
681 char *argv0;
682 {
683 if (pre_init_ui_hook)
684 pre_init_ui_hook ();
685
686 /* Run the init function of each source file */
687
688 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
689 current_directory = gdb_dirbuf;
690
691 #ifdef __MSDOS__
692 /* Make sure we return to the original directory upon exit, come
693 what may, since the OS doesn't do that for us. */
694 make_final_cleanup ((make_cleanup_func) chdir, strsave (current_directory));
695 #endif
696
697 init_cmd_lists (); /* This needs to be done first */
698 initialize_targets (); /* Setup target_terminal macros for utils.c */
699 initialize_utils (); /* Make errors and warnings possible */
700 initialize_all_files ();
701 init_main (); /* But that omits this file! Do it now */
702
703 /* The signal handling mechanism is different depending whether or
704 not the async version is run. NOTE: in the future we plan to make
705 the event loop be the default engine of gdb, and this difference
706 will disappear. */
707 if (async_p)
708 async_init_signals ();
709 else
710 init_signals ();
711
712 /* We need a default language for parsing expressions, so simple things like
713 "set width 0" won't fail if no language is explicitly set in a config file
714 or implicitly set by reading an executable during startup. */
715 set_language (language_c);
716 expected_language = current_language; /* don't warn about the change. */
717
718 /* All the interpreters should have had a look at things by now.
719 Initialize the selected interpreter. */
720 if (init_ui_hook)
721 init_ui_hook (argv0);
722 }
723
724 /* Allocate, initialize a new command line structure for one of the
725 control commands (if/while). */
726
727 static struct command_line *
728 build_command_line (type, args)
729 enum command_control_type type;
730 char *args;
731 {
732 struct command_line *cmd;
733
734 if (args == NULL)
735 error ("if/while commands require arguments.\n");
736
737 cmd = (struct command_line *) xmalloc (sizeof (struct command_line));
738 cmd->next = NULL;
739 cmd->control_type = type;
740
741 cmd->body_count = 1;
742 cmd->body_list
743 = (struct command_line **) xmalloc (sizeof (struct command_line *)
744 * cmd->body_count);
745 memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
746 cmd->line = savestring (args, strlen (args));
747 return cmd;
748 }
749
750 /* Build and return a new command structure for the control commands
751 such as "if" and "while". */
752
753 static struct command_line *
754 get_command_line (type, arg)
755 enum command_control_type type;
756 char *arg;
757 {
758 struct command_line *cmd;
759 struct cleanup *old_chain = NULL;
760
761 /* Allocate and build a new command line structure. */
762 cmd = build_command_line (type, arg);
763
764 old_chain = make_cleanup ((make_cleanup_func) free_command_lines, &cmd);
765
766 /* Read in the body of this command. */
767 if (recurse_read_control_structure (cmd) == invalid_control)
768 {
769 warning ("error reading in control structure\n");
770 do_cleanups (old_chain);
771 return NULL;
772 }
773
774 discard_cleanups (old_chain);
775 return cmd;
776 }
777
778 /* Recursively print a command (including full control structures). */
779 void
780 print_command_line (cmd, depth, stream)
781 struct command_line *cmd;
782 unsigned int depth;
783 GDB_FILE *stream;
784 {
785 unsigned int i;
786
787 if (depth)
788 {
789 for (i = 0; i < depth; i++)
790 fputs_filtered (" ", stream);
791 }
792
793 /* A simple command, print it and return. */
794 if (cmd->control_type == simple_control)
795 {
796 fputs_filtered (cmd->line, stream);
797 fputs_filtered ("\n", stream);
798 return;
799 }
800
801 /* loop_continue to jump to the start of a while loop, print it
802 and return. */
803 if (cmd->control_type == continue_control)
804 {
805 fputs_filtered ("loop_continue\n", stream);
806 return;
807 }
808
809 /* loop_break to break out of a while loop, print it and return. */
810 if (cmd->control_type == break_control)
811 {
812 fputs_filtered ("loop_break\n", stream);
813 return;
814 }
815
816 /* A while command. Recursively print its subcommands before returning. */
817 if (cmd->control_type == while_control)
818 {
819 struct command_line *list;
820 fputs_filtered ("while ", stream);
821 fputs_filtered (cmd->line, stream);
822 fputs_filtered ("\n", stream);
823 list = *cmd->body_list;
824 while (list)
825 {
826 print_command_line (list, depth + 1, stream);
827 list = list->next;
828 }
829 }
830
831 /* An if command. Recursively print both arms before returning. */
832 if (cmd->control_type == if_control)
833 {
834 fputs_filtered ("if ", stream);
835 fputs_filtered (cmd->line, stream);
836 fputs_filtered ("\n", stream);
837 /* The true arm. */
838 print_command_line (cmd->body_list[0], depth + 1, stream);
839
840 /* Show the false arm if it exists. */
841 if (cmd->body_count == 2)
842 {
843 if (depth)
844 {
845 for (i = 0; i < depth; i++)
846 fputs_filtered (" ", stream);
847 }
848 fputs_filtered ("else\n", stream);
849 print_command_line (cmd->body_list[1], depth + 1, stream);
850 }
851 if (depth)
852 {
853 for (i = 0; i < depth; i++)
854 fputs_filtered (" ", stream);
855 }
856 fputs_filtered ("end\n", stream);
857 }
858 }
859
860 /* Execute the command in CMD. */
861
862 enum command_control_type
863 execute_control_command (cmd)
864 struct command_line *cmd;
865 {
866 struct expression *expr;
867 struct command_line *current;
868 struct cleanup *old_chain = 0;
869 value_ptr val;
870 value_ptr val_mark;
871 int loop;
872 enum command_control_type ret;
873 char *new_line;
874
875 switch (cmd->control_type)
876 {
877 case simple_control:
878 /* A simple command, execute it and return. */
879 new_line = insert_args (cmd->line);
880 if (!new_line)
881 return invalid_control;
882 old_chain = make_cleanup ((make_cleanup_func) free_current_contents,
883 &new_line);
884 execute_command (new_line, 0);
885 ret = cmd->control_type;
886 break;
887
888 case continue_control:
889 case break_control:
890 /* Return for "continue", and "break" so we can either
891 continue the loop at the top, or break out. */
892 ret = cmd->control_type;
893 break;
894
895 case while_control:
896 {
897 /* Parse the loop control expression for the while statement. */
898 new_line = insert_args (cmd->line);
899 if (!new_line)
900 return invalid_control;
901 old_chain = make_cleanup ((make_cleanup_func) free_current_contents,
902 &new_line);
903 expr = parse_expression (new_line);
904 make_cleanup ((make_cleanup_func) free_current_contents, &expr);
905
906 ret = simple_control;
907 loop = 1;
908
909 /* Keep iterating so long as the expression is true. */
910 while (loop == 1)
911 {
912 int cond_result;
913
914 QUIT;
915
916 /* Evaluate the expression. */
917 val_mark = value_mark ();
918 val = evaluate_expression (expr);
919 cond_result = value_true (val);
920 value_free_to_mark (val_mark);
921
922 /* If the value is false, then break out of the loop. */
923 if (!cond_result)
924 break;
925
926 /* Execute the body of the while statement. */
927 current = *cmd->body_list;
928 while (current)
929 {
930 ret = execute_control_command (current);
931
932 /* If we got an error, or a "break" command, then stop
933 looping. */
934 if (ret == invalid_control || ret == break_control)
935 {
936 loop = 0;
937 break;
938 }
939
940 /* If we got a "continue" command, then restart the loop
941 at this point. */
942 if (ret == continue_control)
943 break;
944
945 /* Get the next statement. */
946 current = current->next;
947 }
948 }
949
950 /* Reset RET so that we don't recurse the break all the way down. */
951 if (ret == break_control)
952 ret = simple_control;
953
954 break;
955 }
956
957 case if_control:
958 {
959 new_line = insert_args (cmd->line);
960 if (!new_line)
961 return invalid_control;
962 old_chain = make_cleanup ((make_cleanup_func) free_current_contents,
963 &new_line);
964 /* Parse the conditional for the if statement. */
965 expr = parse_expression (new_line);
966 make_cleanup ((make_cleanup_func) free_current_contents, &expr);
967
968 current = NULL;
969 ret = simple_control;
970
971 /* Evaluate the conditional. */
972 val_mark = value_mark ();
973 val = evaluate_expression (expr);
974
975 /* Choose which arm to take commands from based on the value of the
976 conditional expression. */
977 if (value_true (val))
978 current = *cmd->body_list;
979 else if (cmd->body_count == 2)
980 current = *(cmd->body_list + 1);
981 value_free_to_mark (val_mark);
982
983 /* Execute commands in the given arm. */
984 while (current)
985 {
986 ret = execute_control_command (current);
987
988 /* If we got an error, get out. */
989 if (ret != simple_control)
990 break;
991
992 /* Get the next statement in the body. */
993 current = current->next;
994 }
995
996 break;
997 }
998
999 default:
1000 warning ("Invalid control type in command structure.");
1001 return invalid_control;
1002 }
1003
1004 if (old_chain)
1005 do_cleanups (old_chain);
1006
1007 return ret;
1008 }
1009
1010 /* "while" command support. Executes a body of statements while the
1011 loop condition is nonzero. */
1012
1013 static void
1014 while_command (arg, from_tty)
1015 char *arg;
1016 int from_tty;
1017 {
1018 struct command_line *command = NULL;
1019
1020 control_level = 1;
1021 command = get_command_line (while_control, arg);
1022
1023 if (command == NULL)
1024 return;
1025
1026 execute_control_command (command);
1027 free_command_lines (&command);
1028 }
1029
1030 /* "if" command support. Execute either the true or false arm depending
1031 on the value of the if conditional. */
1032
1033 static void
1034 if_command (arg, from_tty)
1035 char *arg;
1036 int from_tty;
1037 {
1038 struct command_line *command = NULL;
1039
1040 control_level = 1;
1041 command = get_command_line (if_control, arg);
1042
1043 if (command == NULL)
1044 return;
1045
1046 execute_control_command (command);
1047 free_command_lines (&command);
1048 }
1049
1050 /* Cleanup */
1051 static void
1052 arg_cleanup ()
1053 {
1054 struct user_args *oargs = user_args;
1055 if (!user_args)
1056 internal_error ("Internal error, arg_cleanup called with no user args.\n");
1057
1058 user_args = user_args->next;
1059 free (oargs);
1060 }
1061
1062 /* Bind the incomming arguments for a user defined command to
1063 $arg0, $arg1 ... $argMAXUSERARGS. */
1064
1065 static struct cleanup *
1066 setup_user_args (p)
1067 char *p;
1068 {
1069 struct user_args *args;
1070 struct cleanup *old_chain;
1071 unsigned int arg_count = 0;
1072
1073 args = (struct user_args *) xmalloc (sizeof (struct user_args));
1074 memset (args, 0, sizeof (struct user_args));
1075
1076 args->next = user_args;
1077 user_args = args;
1078
1079 old_chain = make_cleanup ((make_cleanup_func) arg_cleanup, 0);
1080
1081 if (p == NULL)
1082 return old_chain;
1083
1084 while (*p)
1085 {
1086 char *start_arg;
1087 int squote = 0;
1088 int dquote = 0;
1089 int bsquote = 0;
1090
1091 if (arg_count >= MAXUSERARGS)
1092 {
1093 error ("user defined function may only have %d arguments.\n",
1094 MAXUSERARGS);
1095 return old_chain;
1096 }
1097
1098 /* Strip whitespace. */
1099 while (*p == ' ' || *p == '\t')
1100 p++;
1101
1102 /* P now points to an argument. */
1103 start_arg = p;
1104 user_args->a[arg_count].arg = p;
1105
1106 /* Get to the end of this argument. */
1107 while (*p)
1108 {
1109 if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
1110 break;
1111 else
1112 {
1113 if (bsquote)
1114 bsquote = 0;
1115 else if (*p == '\\')
1116 bsquote = 1;
1117 else if (squote)
1118 {
1119 if (*p == '\'')
1120 squote = 0;
1121 }
1122 else if (dquote)
1123 {
1124 if (*p == '"')
1125 dquote = 0;
1126 }
1127 else
1128 {
1129 if (*p == '\'')
1130 squote = 1;
1131 else if (*p == '"')
1132 dquote = 1;
1133 }
1134 p++;
1135 }
1136 }
1137
1138 user_args->a[arg_count].len = p - start_arg;
1139 arg_count++;
1140 user_args->count++;
1141 }
1142 return old_chain;
1143 }
1144
1145 /* Given character string P, return a point to the first argument ($arg),
1146 or NULL if P contains no arguments. */
1147
1148 static char *
1149 locate_arg (p)
1150 char *p;
1151 {
1152 while ((p = strchr (p, '$')))
1153 {
1154 if (strncmp (p, "$arg", 4) == 0 && isdigit (p[4]))
1155 return p;
1156 p++;
1157 }
1158 return NULL;
1159 }
1160
1161 /* Insert the user defined arguments stored in user_arg into the $arg
1162 arguments found in line, with the updated copy being placed into nline. */
1163
1164 static char *
1165 insert_args (line)
1166 char *line;
1167 {
1168 char *p, *save_line, *new_line;
1169 unsigned len, i;
1170
1171 /* First we need to know how much memory to allocate for the new line. */
1172 save_line = line;
1173 len = 0;
1174 while ((p = locate_arg (line)))
1175 {
1176 len += p - line;
1177 i = p[4] - '0';
1178
1179 if (i >= user_args->count)
1180 {
1181 error ("Missing argument %d in user function.\n", i);
1182 return NULL;
1183 }
1184 len += user_args->a[i].len;
1185 line = p + 5;
1186 }
1187
1188 /* Don't forget the tail. */
1189 len += strlen (line);
1190
1191 /* Allocate space for the new line and fill it in. */
1192 new_line = (char *) xmalloc (len + 1);
1193 if (new_line == NULL)
1194 return NULL;
1195
1196 /* Restore pointer to beginning of old line. */
1197 line = save_line;
1198
1199 /* Save pointer to beginning of new line. */
1200 save_line = new_line;
1201
1202 while ((p = locate_arg (line)))
1203 {
1204 int i, len;
1205
1206 memcpy (new_line, line, p - line);
1207 new_line += p - line;
1208 i = p[4] - '0';
1209
1210 len = user_args->a[i].len;
1211 if (len)
1212 {
1213 memcpy (new_line, user_args->a[i].arg, len);
1214 new_line += len;
1215 }
1216 line = p + 5;
1217 }
1218 /* Don't forget the tail. */
1219 strcpy (new_line, line);
1220
1221 /* Return a pointer to the beginning of the new line. */
1222 return save_line;
1223 }
1224
1225 void
1226 execute_user_command (c, args)
1227 struct cmd_list_element *c;
1228 char *args;
1229 {
1230 register struct command_line *cmdlines;
1231 struct cleanup *old_chain;
1232 enum command_control_type ret;
1233
1234 old_chain = setup_user_args (args);
1235
1236 cmdlines = c->user_commands;
1237 if (cmdlines == 0)
1238 /* Null command */
1239 return;
1240
1241 /* Set the instream to 0, indicating execution of a
1242 user-defined function. */
1243 old_chain = make_cleanup ((make_cleanup_func) source_cleanup, instream);
1244 instream = (FILE *) 0;
1245 while (cmdlines)
1246 {
1247 ret = execute_control_command (cmdlines);
1248 if (ret != simple_control && ret != break_control)
1249 {
1250 warning ("Error in control structure.\n");
1251 break;
1252 }
1253 cmdlines = cmdlines->next;
1254 }
1255 do_cleanups (old_chain);
1256 }
1257
1258 /* Execute the line P as a command.
1259 Pass FROM_TTY as second argument to the defining function. */
1260
1261 void
1262 execute_command (p, from_tty)
1263 char *p;
1264 int from_tty;
1265 {
1266 register struct cmd_list_element *c;
1267 register enum language flang;
1268 static int warned = 0;
1269 /* FIXME: These should really be in an appropriate header file */
1270 extern void serial_log_command PARAMS ((const char *));
1271
1272 free_all_values ();
1273
1274 /* Force cleanup of any alloca areas if using C alloca instead of
1275 a builtin alloca. */
1276 alloca (0);
1277
1278 /* This can happen when command_line_input hits end of file. */
1279 if (p == NULL)
1280 return;
1281
1282 serial_log_command (p);
1283
1284 while (*p == ' ' || *p == '\t')
1285 p++;
1286 if (*p)
1287 {
1288 char *arg;
1289
1290 c = lookup_cmd (&p, cmdlist, "", 0, 1);
1291
1292 /* If the target is running, we allow only a limited set of
1293 commands. */
1294 if (async_p && target_has_async && target_executing)
1295 if (!strcmp (c->name, "help")
1296 && !strcmp (c->name, "pwd")
1297 && !strcmp (c->name, "show")
1298 && !strcmp (c->name, "stop"))
1299 error ("Cannot execute this command while the target is running.");
1300
1301 /* Pass null arg rather than an empty one. */
1302 arg = *p ? p : 0;
1303
1304 /* Clear off trailing whitespace, except for set and complete command. */
1305 if (arg && c->type != set_cmd && c->function.cfunc != complete_command)
1306 {
1307 p = arg + strlen (arg) - 1;
1308 while (p >= arg && (*p == ' ' || *p == '\t'))
1309 p--;
1310 *(p + 1) = '\0';
1311 }
1312
1313 /* If this command has been hooked, run the hook first. */
1314 if (c->hook)
1315 execute_user_command (c->hook, (char *) 0);
1316
1317 if (c->class == class_user)
1318 execute_user_command (c, arg);
1319 else if (c->type == set_cmd || c->type == show_cmd)
1320 do_setshow_command (arg, from_tty & caution, c);
1321 else if (c->function.cfunc == NO_FUNCTION)
1322 error ("That is not a command, just a help topic.");
1323 else if (call_command_hook)
1324 call_command_hook (c, arg, from_tty & caution);
1325 else
1326 (*c->function.cfunc) (arg, from_tty & caution);
1327 }
1328
1329 /* Tell the user if the language has changed (except first time). */
1330 if (current_language != expected_language)
1331 {
1332 if (language_mode == language_mode_auto)
1333 {
1334 language_info (1); /* Print what changed. */
1335 }
1336 warned = 0;
1337 }
1338
1339 /* Warn the user if the working language does not match the
1340 language of the current frame. Only warn the user if we are
1341 actually running the program, i.e. there is a stack. */
1342 /* FIXME: This should be cacheing the frame and only running when
1343 the frame changes. */
1344
1345 if (target_has_stack)
1346 {
1347 flang = get_frame_language ();
1348 if (!warned
1349 && flang != language_unknown
1350 && flang != current_language->la_language)
1351 {
1352 printf_filtered ("%s\n", lang_frame_mismatch_warn);
1353 warned = 1;
1354 }
1355 }
1356 }
1357
1358 /* ARGSUSED */
1359 /* NOTE 1999-04-29: This function will be static again, once we modify
1360 gdb to use the event loop as the default command loop and we merge
1361 event-top.c into this file, top.c */
1362 /* static */ void
1363 command_loop_marker (foo)
1364 int foo;
1365 {
1366 }
1367
1368 /* Read commands from `instream' and execute them
1369 until end of file or error reading instream. */
1370
1371 void
1372 command_loop ()
1373 {
1374 struct cleanup *old_chain;
1375 char *command;
1376 int stdin_is_tty = ISATTY (stdin);
1377 long time_at_cmd_start;
1378 #ifdef HAVE_SBRK
1379 long space_at_cmd_start = 0;
1380 #endif
1381 extern int display_time;
1382 extern int display_space;
1383
1384 while (instream && !feof (instream))
1385 {
1386 #if defined(TUI)
1387 extern int insert_mode;
1388 #endif
1389 if (window_hook && instream == stdin)
1390 (*window_hook) (instream, get_prompt ());
1391
1392 quit_flag = 0;
1393 if (instream == stdin && stdin_is_tty)
1394 reinitialize_more_filter ();
1395 old_chain = make_cleanup ((make_cleanup_func) command_loop_marker, 0);
1396
1397 #if defined(TUI)
1398 /* A bit of paranoia: I want to make sure the "insert_mode" global
1399 * is clear except when it is being used for command-line editing
1400 * (see tuiIO.c, utils.c); otherwise normal output will
1401 * get messed up in the TUI. So clear it before/after
1402 * the command-line-input call. - RT
1403 */
1404 insert_mode = 0;
1405 #endif
1406 /* Get a command-line. This calls the readline package. */
1407 command = command_line_input (instream == stdin ?
1408 get_prompt () : (char *) NULL,
1409 instream == stdin, "prompt");
1410 #if defined(TUI)
1411 insert_mode = 0;
1412 #endif
1413 if (command == 0)
1414 return;
1415
1416 time_at_cmd_start = get_run_time ();
1417
1418 if (display_space)
1419 {
1420 #ifdef HAVE_SBRK
1421 extern char **environ;
1422 char *lim = (char *) sbrk (0);
1423
1424 space_at_cmd_start = (long) (lim - (char *) &environ);
1425 #endif
1426 }
1427
1428 execute_command (command, instream == stdin);
1429 /* Do any commands attached to breakpoint we stopped at. */
1430 bpstat_do_actions (&stop_bpstat);
1431 do_cleanups (old_chain);
1432
1433 if (display_time)
1434 {
1435 long cmd_time = get_run_time () - time_at_cmd_start;
1436
1437 printf_unfiltered ("Command execution time: %ld.%06ld\n",
1438 cmd_time / 1000000, cmd_time % 1000000);
1439 }
1440
1441 if (display_space)
1442 {
1443 #ifdef HAVE_SBRK
1444 extern char **environ;
1445 char *lim = (char *) sbrk (0);
1446 long space_now = lim - (char *) &environ;
1447 long space_diff = space_now - space_at_cmd_start;
1448
1449 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1450 space_now,
1451 (space_diff >= 0 ? '+' : '-'),
1452 space_diff);
1453 #endif
1454 }
1455 }
1456 }
1457
1458 \f
1459 /* Commands call this if they do not want to be repeated by null lines. */
1460
1461 void
1462 dont_repeat ()
1463 {
1464 if (server_command)
1465 return;
1466
1467 /* If we aren't reading from standard input, we are saving the last
1468 thing read from stdin in line and don't want to delete it. Null lines
1469 won't repeat here in any case. */
1470 if (instream == stdin)
1471 *line = 0;
1472 }
1473 \f
1474 /* Read a line from the stream "instream" without command line editing.
1475
1476 It prints PROMPT_ARG once at the start.
1477 Action is compatible with "readline", e.g. space for the result is
1478 malloc'd and should be freed by the caller.
1479
1480 A NULL return means end of file. */
1481 char *
1482 gdb_readline (prompt_arg)
1483 char *prompt_arg;
1484 {
1485 int c;
1486 char *result;
1487 int input_index = 0;
1488 int result_size = 80;
1489
1490 if (prompt_arg)
1491 {
1492 /* Don't use a _filtered function here. It causes the assumed
1493 character position to be off, since the newline we read from
1494 the user is not accounted for. */
1495 fputs_unfiltered (prompt_arg, gdb_stdout);
1496 #ifdef MPW
1497 /* Move to a new line so the entered line doesn't have a prompt
1498 on the front of it. */
1499 fputs_unfiltered ("\n", gdb_stdout);
1500 #endif /* MPW */
1501 gdb_flush (gdb_stdout);
1502 }
1503
1504 result = (char *) xmalloc (result_size);
1505
1506 while (1)
1507 {
1508 /* Read from stdin if we are executing a user defined command.
1509 This is the right thing for prompt_for_continue, at least. */
1510 c = fgetc (instream ? instream : stdin);
1511
1512 if (c == EOF)
1513 {
1514 if (input_index > 0)
1515 /* The last line does not end with a newline. Return it, and
1516 if we are called again fgetc will still return EOF and
1517 we'll return NULL then. */
1518 break;
1519 free (result);
1520 return NULL;
1521 }
1522
1523 if (c == '\n')
1524 #ifndef CRLF_SOURCE_FILES
1525 break;
1526 #else
1527 {
1528 if (input_index > 0 && result[input_index - 1] == '\r')
1529 input_index--;
1530 break;
1531 }
1532 #endif
1533
1534 result[input_index++] = c;
1535 while (input_index >= result_size)
1536 {
1537 result_size *= 2;
1538 result = (char *) xrealloc (result, result_size);
1539 }
1540 }
1541
1542 result[input_index++] = '\0';
1543 return result;
1544 }
1545
1546 /* Variables which control command line editing and history
1547 substitution. These variables are given default values at the end
1548 of this file. */
1549 static int command_editing_p;
1550 /* NOTE 1999-04-29: This variable will be static again, once we modify
1551 gdb to use the event loop as the default command loop and we merge
1552 event-top.c into this file, top.c */
1553 /* static */ int history_expansion_p;
1554 static int write_history_p;
1555 static int history_size;
1556 static char *history_filename;
1557
1558 /* readline uses the word breaks for two things:
1559 (1) In figuring out where to point the TEXT parameter to the
1560 rl_completion_entry_function. Since we don't use TEXT for much,
1561 it doesn't matter a lot what the word breaks are for this purpose, but
1562 it does affect how much stuff M-? lists.
1563 (2) If one of the matches contains a word break character, readline
1564 will quote it. That's why we switch between
1565 gdb_completer_word_break_characters and
1566 gdb_completer_command_word_break_characters. I'm not sure when
1567 we need this behavior (perhaps for funky characters in C++ symbols?). */
1568
1569 /* Variables which are necessary for fancy command line editing. */
1570 char *gdb_completer_word_break_characters =
1571 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1572
1573 /* When completing on command names, we remove '-' from the list of
1574 word break characters, since we use it in command names. If the
1575 readline library sees one in any of the current completion strings,
1576 it thinks that the string needs to be quoted and automatically supplies
1577 a leading quote. */
1578 char *gdb_completer_command_word_break_characters =
1579 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1580
1581 /* Characters that can be used to quote completion strings. Note that we
1582 can't include '"' because the gdb C parser treats such quoted sequences
1583 as strings. */
1584 char *gdb_completer_quote_characters =
1585 "'";
1586
1587 /* Functions that are used as part of the fancy command line editing. */
1588
1589 /* This can be used for functions which don't want to complete on symbols
1590 but don't want to complete on anything else either. */
1591 /* ARGSUSED */
1592 char **
1593 noop_completer (text, prefix)
1594 char *text;
1595 char *prefix;
1596 {
1597 return NULL;
1598 }
1599
1600 /* Complete on filenames. */
1601 char **
1602 filename_completer (text, word)
1603 char *text;
1604 char *word;
1605 {
1606 /* From readline. */
1607 extern char *filename_completion_function PARAMS ((char *, int));
1608 int subsequent_name;
1609 char **return_val;
1610 int return_val_used;
1611 int return_val_alloced;
1612
1613 return_val_used = 0;
1614 /* Small for testing. */
1615 return_val_alloced = 1;
1616 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
1617
1618 subsequent_name = 0;
1619 while (1)
1620 {
1621 char *p;
1622 p = filename_completion_function (text, subsequent_name);
1623 if (return_val_used >= return_val_alloced)
1624 {
1625 return_val_alloced *= 2;
1626 return_val =
1627 (char **) xrealloc (return_val,
1628 return_val_alloced * sizeof (char *));
1629 }
1630 if (p == NULL)
1631 {
1632 return_val[return_val_used++] = p;
1633 break;
1634 }
1635 /* Like emacs, don't complete on old versions. Especially useful
1636 in the "source" command. */
1637 if (p[strlen (p) - 1] == '~')
1638 continue;
1639
1640 {
1641 char *q;
1642 if (word == text)
1643 /* Return exactly p. */
1644 return_val[return_val_used++] = p;
1645 else if (word > text)
1646 {
1647 /* Return some portion of p. */
1648 q = xmalloc (strlen (p) + 5);
1649 strcpy (q, p + (word - text));
1650 return_val[return_val_used++] = q;
1651 free (p);
1652 }
1653 else
1654 {
1655 /* Return some of TEXT plus p. */
1656 q = xmalloc (strlen (p) + (text - word) + 5);
1657 strncpy (q, word, text - word);
1658 q[text - word] = '\0';
1659 strcat (q, p);
1660 return_val[return_val_used++] = q;
1661 free (p);
1662 }
1663 }
1664 subsequent_name = 1;
1665 }
1666 #if 0
1667 /* There is no way to do this just long enough to affect quote inserting
1668 without also affecting the next completion. This should be fixed in
1669 readline. FIXME. */
1670 /* Insure that readline does the right thing
1671 with respect to inserting quotes. */
1672 rl_completer_word_break_characters = "";
1673 #endif
1674 return return_val;
1675 }
1676
1677 /* Here are some useful test cases for completion. FIXME: These should
1678 be put in the test suite. They should be tested with both M-? and TAB.
1679
1680 "show output-" "radix"
1681 "show output" "-radix"
1682 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1683 "p " ambiguous (all symbols)
1684 "info t foo" no completions
1685 "info t " no completions
1686 "info t" ambiguous ("info target", "info terminal", etc.)
1687 "info ajksdlfk" no completions
1688 "info ajksdlfk " no completions
1689 "info" " "
1690 "info " ambiguous (all info commands)
1691 "p \"a" no completions (string constant)
1692 "p 'a" ambiguous (all symbols starting with a)
1693 "p b-a" ambiguous (all symbols starting with a)
1694 "p b-" ambiguous (all symbols)
1695 "file Make" "file" (word break hard to screw up here)
1696 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1697 */
1698
1699 /* Generate completions one by one for the completer. Each time we are
1700 called return another potential completion to the caller.
1701 line_completion just completes on commands or passes the buck to the
1702 command's completer function, the stuff specific to symbol completion
1703 is in make_symbol_completion_list.
1704
1705 TEXT is the caller's idea of the "word" we are looking at.
1706
1707 MATCHES is the number of matches that have currently been collected from
1708 calling this completion function. When zero, then we need to initialize,
1709 otherwise the initialization has already taken place and we can just
1710 return the next potential completion string.
1711
1712 LINE_BUFFER is available to be looked at; it contains the entire text
1713 of the line. POINT is the offset in that line of the cursor. You
1714 should pretend that the line ends at POINT.
1715
1716 Returns NULL if there are no more completions, else a pointer to a string
1717 which is a possible completion, it is the caller's responsibility to
1718 free the string. */
1719
1720 static char *
1721 line_completion_function (text, matches, line_buffer, point)
1722 char *text;
1723 int matches;
1724 char *line_buffer;
1725 int point;
1726 {
1727 static char **list = (char **) NULL; /* Cache of completions */
1728 static int index; /* Next cached completion */
1729 char *output = NULL;
1730 char *tmp_command, *p;
1731 /* Pointer within tmp_command which corresponds to text. */
1732 char *word;
1733 struct cmd_list_element *c, *result_list;
1734
1735 if (matches == 0)
1736 {
1737 /* The caller is beginning to accumulate a new set of completions, so
1738 we need to find all of them now, and cache them for returning one at
1739 a time on future calls. */
1740
1741 if (list)
1742 {
1743 /* Free the storage used by LIST, but not by the strings inside.
1744 This is because rl_complete_internal () frees the strings. */
1745 free ((PTR) list);
1746 }
1747 list = 0;
1748 index = 0;
1749
1750 /* Choose the default set of word break characters to break completions.
1751 If we later find out that we are doing completions on command strings
1752 (as opposed to strings supplied by the individual command completer
1753 functions, which can be any string) then we will switch to the
1754 special word break set for command strings, which leaves out the
1755 '-' character used in some commands. */
1756
1757 rl_completer_word_break_characters =
1758 gdb_completer_word_break_characters;
1759
1760 /* Decide whether to complete on a list of gdb commands or on symbols. */
1761 tmp_command = (char *) alloca (point + 1);
1762 p = tmp_command;
1763
1764 strncpy (tmp_command, line_buffer, point);
1765 tmp_command[point] = '\0';
1766 /* Since text always contains some number of characters leading up
1767 to point, we can find the equivalent position in tmp_command
1768 by subtracting that many characters from the end of tmp_command. */
1769 word = tmp_command + point - strlen (text);
1770
1771 if (point == 0)
1772 {
1773 /* An empty line we want to consider ambiguous; that is, it
1774 could be any command. */
1775 c = (struct cmd_list_element *) -1;
1776 result_list = 0;
1777 }
1778 else
1779 {
1780 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
1781 }
1782
1783 /* Move p up to the next interesting thing. */
1784 while (*p == ' ' || *p == '\t')
1785 {
1786 p++;
1787 }
1788
1789 if (!c)
1790 {
1791 /* It is an unrecognized command. So there are no
1792 possible completions. */
1793 list = NULL;
1794 }
1795 else if (c == (struct cmd_list_element *) -1)
1796 {
1797 char *q;
1798
1799 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1800 doesn't advance over that thing itself. Do so now. */
1801 q = p;
1802 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
1803 ++q;
1804 if (q != tmp_command + point)
1805 {
1806 /* There is something beyond the ambiguous
1807 command, so there are no possible completions. For
1808 example, "info t " or "info t foo" does not complete
1809 to anything, because "info t" can be "info target" or
1810 "info terminal". */
1811 list = NULL;
1812 }
1813 else
1814 {
1815 /* We're trying to complete on the command which was ambiguous.
1816 This we can deal with. */
1817 if (result_list)
1818 {
1819 list = complete_on_cmdlist (*result_list->prefixlist, p,
1820 word);
1821 }
1822 else
1823 {
1824 list = complete_on_cmdlist (cmdlist, p, word);
1825 }
1826 /* Insure that readline does the right thing with respect to
1827 inserting quotes. */
1828 rl_completer_word_break_characters =
1829 gdb_completer_command_word_break_characters;
1830 }
1831 }
1832 else
1833 {
1834 /* We've recognized a full command. */
1835
1836 if (p == tmp_command + point)
1837 {
1838 /* There is no non-whitespace in the line beyond the command. */
1839
1840 if (p[-1] == ' ' || p[-1] == '\t')
1841 {
1842 /* The command is followed by whitespace; we need to complete
1843 on whatever comes after command. */
1844 if (c->prefixlist)
1845 {
1846 /* It is a prefix command; what comes after it is
1847 a subcommand (e.g. "info "). */
1848 list = complete_on_cmdlist (*c->prefixlist, p, word);
1849
1850 /* Insure that readline does the right thing
1851 with respect to inserting quotes. */
1852 rl_completer_word_break_characters =
1853 gdb_completer_command_word_break_characters;
1854 }
1855 else if (c->enums)
1856 {
1857 list = complete_on_enum (c->enums, p, word);
1858 rl_completer_word_break_characters =
1859 gdb_completer_command_word_break_characters;
1860 }
1861 else
1862 {
1863 /* It is a normal command; what comes after it is
1864 completed by the command's completer function. */
1865 list = (*c->completer) (p, word);
1866 }
1867 }
1868 else
1869 {
1870 /* The command is not followed by whitespace; we need to
1871 complete on the command itself. e.g. "p" which is a
1872 command itself but also can complete to "print", "ptype"
1873 etc. */
1874 char *q;
1875
1876 /* Find the command we are completing on. */
1877 q = p;
1878 while (q > tmp_command)
1879 {
1880 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
1881 --q;
1882 else
1883 break;
1884 }
1885
1886 list = complete_on_cmdlist (result_list, q, word);
1887
1888 /* Insure that readline does the right thing
1889 with respect to inserting quotes. */
1890 rl_completer_word_break_characters =
1891 gdb_completer_command_word_break_characters;
1892 }
1893 }
1894 else
1895 {
1896 /* There is non-whitespace beyond the command. */
1897
1898 if (c->prefixlist && !c->allow_unknown)
1899 {
1900 /* It is an unrecognized subcommand of a prefix command,
1901 e.g. "info adsfkdj". */
1902 list = NULL;
1903 }
1904 else if (c->enums)
1905 {
1906 list = complete_on_enum (c->enums, p, word);
1907 }
1908 else
1909 {
1910 /* It is a normal command. */
1911 list = (*c->completer) (p, word);
1912 }
1913 }
1914 }
1915 }
1916
1917 /* If we found a list of potential completions during initialization then
1918 dole them out one at a time. The vector of completions is NULL
1919 terminated, so after returning the last one, return NULL (and continue
1920 to do so) each time we are called after that, until a new list is
1921 available. */
1922
1923 if (list)
1924 {
1925 output = list[index];
1926 if (output)
1927 {
1928 index++;
1929 }
1930 }
1931
1932 #if 0
1933 /* Can't do this because readline hasn't yet checked the word breaks
1934 for figuring out whether to insert a quote. */
1935 if (output == NULL)
1936 /* Make sure the word break characters are set back to normal for the
1937 next time that readline tries to complete something. */
1938 rl_completer_word_break_characters =
1939 gdb_completer_word_break_characters;
1940 #endif
1941
1942 return (output);
1943 }
1944
1945 /* Line completion interface function for readline. */
1946
1947 static char *
1948 readline_line_completion_function (text, matches)
1949 char *text;
1950 int matches;
1951 {
1952 return line_completion_function (text, matches, rl_line_buffer, rl_point);
1953 }
1954
1955 /* Skip over a possibly quoted word (as defined by the quote characters
1956 and word break characters the completer uses). Returns pointer to the
1957 location after the "word". */
1958
1959 char *
1960 skip_quoted (str)
1961 char *str;
1962 {
1963 char quote_char = '\0';
1964 char *scan;
1965
1966 for (scan = str; *scan != '\0'; scan++)
1967 {
1968 if (quote_char != '\0')
1969 {
1970 /* Ignore everything until the matching close quote char */
1971 if (*scan == quote_char)
1972 {
1973 /* Found matching close quote. */
1974 scan++;
1975 break;
1976 }
1977 }
1978 else if (strchr (gdb_completer_quote_characters, *scan))
1979 {
1980 /* Found start of a quoted string. */
1981 quote_char = *scan;
1982 }
1983 else if (strchr (gdb_completer_word_break_characters, *scan))
1984 {
1985 break;
1986 }
1987 }
1988 return (scan);
1989 }
1990 \f
1991
1992 #ifdef STOP_SIGNAL
1993 static void
1994 stop_sig (signo)
1995 int signo;
1996 {
1997 #if STOP_SIGNAL == SIGTSTP
1998 signal (SIGTSTP, SIG_DFL);
1999 sigsetmask (0);
2000 kill (getpid (), SIGTSTP);
2001 signal (SIGTSTP, stop_sig);
2002 #else
2003 signal (STOP_SIGNAL, stop_sig);
2004 #endif
2005 printf_unfiltered ("%s", get_prompt ());
2006 gdb_flush (gdb_stdout);
2007
2008 /* Forget about any previous command -- null line now will do nothing. */
2009 dont_repeat ();
2010 }
2011 #endif /* STOP_SIGNAL */
2012
2013 /* Initialize signal handlers. */
2014 static void
2015 do_nothing (signo)
2016 int signo;
2017 {
2018 /* Under System V the default disposition of a signal is reinstated after
2019 the signal is caught and delivered to an application process. On such
2020 systems one must restore the replacement signal handler if one wishes
2021 to continue handling the signal in one's program. On BSD systems this
2022 is not needed but it is harmless, and it simplifies the code to just do
2023 it unconditionally. */
2024 signal (signo, do_nothing);
2025 }
2026
2027 static void
2028 init_signals ()
2029 {
2030 signal (SIGINT, request_quit);
2031
2032 /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
2033 to the inferior and breakpoints will be ignored. */
2034 #ifdef SIGTRAP
2035 signal (SIGTRAP, SIG_DFL);
2036 #endif
2037
2038 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
2039 passed to the inferior, which we don't want. It would be
2040 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
2041 on BSD4.3 systems using vfork, that can affect the
2042 GDB process as well as the inferior (the signal handling tables
2043 might be in memory, shared between the two). Since we establish
2044 a handler for SIGQUIT, when we call exec it will set the signal
2045 to SIG_DFL for us. */
2046 signal (SIGQUIT, do_nothing);
2047 #ifdef SIGHUP
2048 if (signal (SIGHUP, do_nothing) != SIG_IGN)
2049 signal (SIGHUP, disconnect);
2050 #endif
2051 signal (SIGFPE, float_handler);
2052
2053 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
2054 signal (SIGWINCH, SIGWINCH_HANDLER);
2055 #endif
2056 }
2057 \f
2058 /* Read one line from the command input stream `instream'
2059 into the local static buffer `linebuffer' (whose current length
2060 is `linelength').
2061 The buffer is made bigger as necessary.
2062 Returns the address of the start of the line.
2063
2064 NULL is returned for end of file.
2065
2066 *If* the instream == stdin & stdin is a terminal, the line read
2067 is copied into the file line saver (global var char *line,
2068 length linesize) so that it can be duplicated.
2069
2070 This routine either uses fancy command line editing or
2071 simple input as the user has requested. */
2072
2073 char *
2074 command_line_input (prompt_arg, repeat, annotation_suffix)
2075 char *prompt_arg;
2076 int repeat;
2077 char *annotation_suffix;
2078 {
2079 static char *linebuffer = 0;
2080 static unsigned linelength = 0;
2081 register char *p;
2082 char *p1;
2083 char *rl;
2084 char *local_prompt = prompt_arg;
2085 char *nline;
2086 char got_eof = 0;
2087
2088 /* The annotation suffix must be non-NULL. */
2089 if (annotation_suffix == NULL)
2090 annotation_suffix = "";
2091
2092 if (annotation_level > 1 && instream == stdin)
2093 {
2094 local_prompt = alloca ((prompt_arg == NULL ? 0 : strlen (prompt_arg))
2095 + strlen (annotation_suffix) + 40);
2096 if (prompt_arg == NULL)
2097 local_prompt[0] = '\0';
2098 else
2099 strcpy (local_prompt, prompt_arg);
2100 strcat (local_prompt, "\n\032\032");
2101 strcat (local_prompt, annotation_suffix);
2102 strcat (local_prompt, "\n");
2103 }
2104
2105 if (linebuffer == 0)
2106 {
2107 linelength = 80;
2108 linebuffer = (char *) xmalloc (linelength);
2109 }
2110
2111 p = linebuffer;
2112
2113 /* Control-C quits instantly if typed while in this loop
2114 since it should not wait until the user types a newline. */
2115 immediate_quit++;
2116 #ifdef STOP_SIGNAL
2117 if (job_control)
2118 {
2119 if (async_p)
2120 signal (STOP_SIGNAL, handle_stop_sig);
2121 else
2122 signal (STOP_SIGNAL, stop_sig);
2123 }
2124 #endif
2125
2126 while (1)
2127 {
2128 /* Make sure that all output has been output. Some machines may let
2129 you get away with leaving out some of the gdb_flush, but not all. */
2130 wrap_here ("");
2131 gdb_flush (gdb_stdout);
2132 gdb_flush (gdb_stderr);
2133
2134 if (source_file_name != NULL)
2135 {
2136 ++source_line_number;
2137 sprintf (source_error,
2138 "%s%s:%d: Error in sourced command file:\n",
2139 source_pre_error,
2140 source_file_name,
2141 source_line_number);
2142 error_pre_print = source_error;
2143 }
2144
2145 if (annotation_level > 1 && instream == stdin)
2146 {
2147 printf_unfiltered ("\n\032\032pre-");
2148 printf_unfiltered (annotation_suffix);
2149 printf_unfiltered ("\n");
2150 }
2151
2152 /* Don't use fancy stuff if not talking to stdin. */
2153 if (readline_hook && instream == NULL)
2154 {
2155 rl = (*readline_hook) (local_prompt);
2156 }
2157 else if (command_editing_p && instream == stdin && ISATTY (instream))
2158 {
2159 rl = readline (local_prompt);
2160 }
2161 else
2162 {
2163 rl = gdb_readline (local_prompt);
2164 }
2165
2166 if (annotation_level > 1 && instream == stdin)
2167 {
2168 printf_unfiltered ("\n\032\032post-");
2169 printf_unfiltered (annotation_suffix);
2170 printf_unfiltered ("\n");
2171 }
2172
2173 if (!rl || rl == (char *) EOF)
2174 {
2175 got_eof = 1;
2176 break;
2177 }
2178 if (strlen (rl) + 1 + (p - linebuffer) > linelength)
2179 {
2180 linelength = strlen (rl) + 1 + (p - linebuffer);
2181 nline = (char *) xrealloc (linebuffer, linelength);
2182 p += nline - linebuffer;
2183 linebuffer = nline;
2184 }
2185 p1 = rl;
2186 /* Copy line. Don't copy null at end. (Leaves line alone
2187 if this was just a newline) */
2188 while (*p1)
2189 *p++ = *p1++;
2190
2191 free (rl); /* Allocated in readline. */
2192
2193 if (p == linebuffer || *(p - 1) != '\\')
2194 break;
2195
2196 p--; /* Put on top of '\'. */
2197 local_prompt = (char *) 0;
2198 }
2199
2200 #ifdef STOP_SIGNAL
2201 if (job_control)
2202 signal (STOP_SIGNAL, SIG_DFL);
2203 #endif
2204 immediate_quit--;
2205
2206 if (got_eof)
2207 return NULL;
2208
2209 #define SERVER_COMMAND_LENGTH 7
2210 server_command =
2211 (p - linebuffer > SERVER_COMMAND_LENGTH)
2212 && STREQN (linebuffer, "server ", SERVER_COMMAND_LENGTH);
2213 if (server_command)
2214 {
2215 /* Note that we don't set `line'. Between this and the check in
2216 dont_repeat, this insures that repeating will still do the
2217 right thing. */
2218 *p = '\0';
2219 return linebuffer + SERVER_COMMAND_LENGTH;
2220 }
2221
2222 /* Do history expansion if that is wished. */
2223 if (history_expansion_p && instream == stdin
2224 && ISATTY (instream))
2225 {
2226 char *history_value;
2227 int expanded;
2228
2229 *p = '\0'; /* Insert null now. */
2230 expanded = history_expand (linebuffer, &history_value);
2231 if (expanded)
2232 {
2233 /* Print the changes. */
2234 printf_unfiltered ("%s\n", history_value);
2235
2236 /* If there was an error, call this function again. */
2237 if (expanded < 0)
2238 {
2239 free (history_value);
2240 return command_line_input (prompt_arg, repeat, annotation_suffix);
2241 }
2242 if (strlen (history_value) > linelength)
2243 {
2244 linelength = strlen (history_value) + 1;
2245 linebuffer = (char *) xrealloc (linebuffer, linelength);
2246 }
2247 strcpy (linebuffer, history_value);
2248 p = linebuffer + strlen (linebuffer);
2249 free (history_value);
2250 }
2251 }
2252
2253 /* If we just got an empty line, and that is supposed
2254 to repeat the previous command, return the value in the
2255 global buffer. */
2256 if (repeat && p == linebuffer)
2257 return line;
2258 for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++);
2259 if (repeat && !*p1)
2260 return line;
2261
2262 *p = 0;
2263
2264 /* Add line to history if appropriate. */
2265 if (instream == stdin
2266 && ISATTY (stdin) && *linebuffer)
2267 add_history (linebuffer);
2268
2269 /* Note: lines consisting solely of comments are added to the command
2270 history. This is useful when you type a command, and then
2271 realize you don't want to execute it quite yet. You can comment
2272 out the command and then later fetch it from the value history
2273 and remove the '#'. The kill ring is probably better, but some
2274 people are in the habit of commenting things out. */
2275 if (*p1 == '#')
2276 *p1 = '\0'; /* Found a comment. */
2277
2278 /* Save into global buffer if appropriate. */
2279 if (repeat)
2280 {
2281 if (linelength > linesize)
2282 {
2283 line = xrealloc (line, linelength);
2284 linesize = linelength;
2285 }
2286 strcpy (line, linebuffer);
2287 return line;
2288 }
2289
2290 return linebuffer;
2291 }
2292 \f
2293
2294 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2295 code bodies. This is typically used when we encounter an "else"
2296 clause for an "if" command. */
2297
2298 static void
2299 realloc_body_list (command, new_length)
2300 struct command_line *command;
2301 int new_length;
2302 {
2303 int n;
2304 struct command_line **body_list;
2305
2306 n = command->body_count;
2307
2308 /* Nothing to do? */
2309 if (new_length <= n)
2310 return;
2311
2312 body_list = (struct command_line **)
2313 xmalloc (sizeof (struct command_line *) * new_length);
2314
2315 memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
2316
2317 free (command->body_list);
2318 command->body_list = body_list;
2319 command->body_count = new_length;
2320 }
2321
2322 /* Read one line from the input stream. If the command is an "else" or
2323 "end", return such an indication to the caller. */
2324
2325 static enum misc_command_type
2326 read_next_line (command)
2327 struct command_line **command;
2328 {
2329 char *p, *p1, *prompt_ptr, control_prompt[256];
2330 int i = 0;
2331
2332 if (control_level >= 254)
2333 error ("Control nesting too deep!\n");
2334
2335 /* Set a prompt based on the nesting of the control commands. */
2336 if (instream == stdin || (instream == 0 && readline_hook != NULL))
2337 {
2338 for (i = 0; i < control_level; i++)
2339 control_prompt[i] = ' ';
2340 control_prompt[i] = '>';
2341 control_prompt[i + 1] = '\0';
2342 prompt_ptr = (char *) &control_prompt[0];
2343 }
2344 else
2345 prompt_ptr = NULL;
2346
2347 p = command_line_input (prompt_ptr, instream == stdin, "commands");
2348
2349 /* Not sure what to do here. */
2350 if (p == NULL)
2351 return end_command;
2352
2353 /* Strip leading and trailing whitespace. */
2354 while (*p == ' ' || *p == '\t')
2355 p++;
2356
2357 p1 = p + strlen (p);
2358 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
2359 p1--;
2360
2361 /* Blanks and comments don't really do anything, but we need to
2362 distinguish them from else, end and other commands which can be
2363 executed. */
2364 if (p1 == p || p[0] == '#')
2365 return nop_command;
2366
2367 /* Is this the end of a simple, while, or if control structure? */
2368 if (p1 - p == 3 && !strncmp (p, "end", 3))
2369 return end_command;
2370
2371 /* Is the else clause of an if control structure? */
2372 if (p1 - p == 4 && !strncmp (p, "else", 4))
2373 return else_command;
2374
2375 /* Check for while, if, break, continue, etc and build a new command
2376 line structure for them. */
2377 if (p1 - p > 5 && !strncmp (p, "while", 5))
2378 *command = build_command_line (while_control, p + 6);
2379 else if (p1 - p > 2 && !strncmp (p, "if", 2))
2380 *command = build_command_line (if_control, p + 3);
2381 else if (p1 - p == 10 && !strncmp (p, "loop_break", 10))
2382 {
2383 *command = (struct command_line *)
2384 xmalloc (sizeof (struct command_line));
2385 (*command)->next = NULL;
2386 (*command)->line = NULL;
2387 (*command)->control_type = break_control;
2388 (*command)->body_count = 0;
2389 (*command)->body_list = NULL;
2390 }
2391 else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13))
2392 {
2393 *command = (struct command_line *)
2394 xmalloc (sizeof (struct command_line));
2395 (*command)->next = NULL;
2396 (*command)->line = NULL;
2397 (*command)->control_type = continue_control;
2398 (*command)->body_count = 0;
2399 (*command)->body_list = NULL;
2400 }
2401 else
2402 {
2403 /* A normal command. */
2404 *command = (struct command_line *)
2405 xmalloc (sizeof (struct command_line));
2406 (*command)->next = NULL;
2407 (*command)->line = savestring (p, p1 - p);
2408 (*command)->control_type = simple_control;
2409 (*command)->body_count = 0;
2410 (*command)->body_list = NULL;
2411 }
2412
2413 /* Nothing special. */
2414 return ok_command;
2415 }
2416
2417 /* Recursively read in the control structures and create a command_line
2418 structure from them.
2419
2420 The parent_control parameter is the control structure in which the
2421 following commands are nested. */
2422
2423 static enum command_control_type
2424 recurse_read_control_structure (current_cmd)
2425 struct command_line *current_cmd;
2426 {
2427 int current_body, i;
2428 enum misc_command_type val;
2429 enum command_control_type ret;
2430 struct command_line **body_ptr, *child_tail, *next;
2431
2432 child_tail = NULL;
2433 current_body = 1;
2434
2435 /* Sanity checks. */
2436 if (current_cmd->control_type == simple_control)
2437 {
2438 error ("Recursed on a simple control type\n");
2439 return invalid_control;
2440 }
2441
2442 if (current_body > current_cmd->body_count)
2443 {
2444 error ("Allocated body is smaller than this command type needs\n");
2445 return invalid_control;
2446 }
2447
2448 /* Read lines from the input stream and build control structures. */
2449 while (1)
2450 {
2451 dont_repeat ();
2452
2453 next = NULL;
2454 val = read_next_line (&next);
2455
2456 /* Just skip blanks and comments. */
2457 if (val == nop_command)
2458 continue;
2459
2460 if (val == end_command)
2461 {
2462 if (current_cmd->control_type == while_control
2463 || current_cmd->control_type == if_control)
2464 {
2465 /* Success reading an entire control structure. */
2466 ret = simple_control;
2467 break;
2468 }
2469 else
2470 {
2471 ret = invalid_control;
2472 break;
2473 }
2474 }
2475
2476 /* Not the end of a control structure. */
2477 if (val == else_command)
2478 {
2479 if (current_cmd->control_type == if_control
2480 && current_body == 1)
2481 {
2482 realloc_body_list (current_cmd, 2);
2483 current_body = 2;
2484 child_tail = NULL;
2485 continue;
2486 }
2487 else
2488 {
2489 ret = invalid_control;
2490 break;
2491 }
2492 }
2493
2494 if (child_tail)
2495 {
2496 child_tail->next = next;
2497 }
2498 else
2499 {
2500 body_ptr = current_cmd->body_list;
2501 for (i = 1; i < current_body; i++)
2502 body_ptr++;
2503
2504 *body_ptr = next;
2505
2506 }
2507
2508 child_tail = next;
2509
2510 /* If the latest line is another control structure, then recurse
2511 on it. */
2512 if (next->control_type == while_control
2513 || next->control_type == if_control)
2514 {
2515 control_level++;
2516 ret = recurse_read_control_structure (next);
2517 control_level--;
2518
2519 if (ret != simple_control)
2520 break;
2521 }
2522 }
2523
2524 dont_repeat ();
2525
2526 return ret;
2527 }
2528
2529 /* Read lines from the input stream and accumulate them in a chain of
2530 struct command_line's, which is then returned. For input from a
2531 terminal, the special command "end" is used to mark the end of the
2532 input, and is not included in the returned chain of commands. */
2533
2534 #define END_MESSAGE "End with a line saying just \"end\"."
2535
2536 struct command_line *
2537 read_command_lines (prompt_arg, from_tty)
2538 char *prompt_arg;
2539 int from_tty;
2540 {
2541 struct command_line *head, *tail, *next;
2542 struct cleanup *old_chain;
2543 enum command_control_type ret;
2544 enum misc_command_type val;
2545
2546 control_level = 0;
2547 if (readline_begin_hook)
2548 {
2549 /* Note - intentional to merge messages with no newline */
2550 (*readline_begin_hook) ("%s %s\n", prompt_arg, END_MESSAGE);
2551 }
2552 else if (from_tty && input_from_terminal_p ())
2553 {
2554 printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
2555 gdb_flush (gdb_stdout);
2556 }
2557
2558 head = tail = NULL;
2559 old_chain = NULL;
2560
2561 while (1)
2562 {
2563 val = read_next_line (&next);
2564
2565 /* Ignore blank lines or comments. */
2566 if (val == nop_command)
2567 continue;
2568
2569 if (val == end_command)
2570 {
2571 ret = simple_control;
2572 break;
2573 }
2574
2575 if (val != ok_command)
2576 {
2577 ret = invalid_control;
2578 break;
2579 }
2580
2581 if (next->control_type == while_control
2582 || next->control_type == if_control)
2583 {
2584 control_level++;
2585 ret = recurse_read_control_structure (next);
2586 control_level--;
2587
2588 if (ret == invalid_control)
2589 break;
2590 }
2591
2592 if (tail)
2593 {
2594 tail->next = next;
2595 }
2596 else
2597 {
2598 head = next;
2599 old_chain = make_cleanup ((make_cleanup_func) free_command_lines,
2600 &head);
2601 }
2602 tail = next;
2603 }
2604
2605 dont_repeat ();
2606
2607 if (head)
2608 {
2609 if (ret != invalid_control)
2610 {
2611 discard_cleanups (old_chain);
2612 }
2613 else
2614 do_cleanups (old_chain);
2615 }
2616
2617 if (readline_end_hook)
2618 {
2619 (*readline_end_hook) ();
2620 }
2621 return (head);
2622 }
2623
2624 /* Free a chain of struct command_line's. */
2625
2626 void
2627 free_command_lines (lptr)
2628 struct command_line **lptr;
2629 {
2630 register struct command_line *l = *lptr;
2631 register struct command_line *next;
2632 struct command_line **blist;
2633 int i;
2634
2635 while (l)
2636 {
2637 if (l->body_count > 0)
2638 {
2639 blist = l->body_list;
2640 for (i = 0; i < l->body_count; i++, blist++)
2641 free_command_lines (blist);
2642 }
2643 next = l->next;
2644 free (l->line);
2645 free ((PTR) l);
2646 l = next;
2647 }
2648 }
2649 \f
2650 /* Add an element to the list of info subcommands. */
2651
2652 void
2653 add_info (name, fun, doc)
2654 char *name;
2655 void (*fun) PARAMS ((char *, int));
2656 char *doc;
2657 {
2658 add_cmd (name, no_class, fun, doc, &infolist);
2659 }
2660
2661 /* Add an alias to the list of info subcommands. */
2662
2663 void
2664 add_info_alias (name, oldname, abbrev_flag)
2665 char *name;
2666 char *oldname;
2667 int abbrev_flag;
2668 {
2669 add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
2670 }
2671
2672 /* The "info" command is defined as a prefix, with allow_unknown = 0.
2673 Therefore, its own definition is called only for "info" with no args. */
2674
2675 /* ARGSUSED */
2676 static void
2677 info_command (arg, from_tty)
2678 char *arg;
2679 int from_tty;
2680 {
2681 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2682 help_list (infolist, "info ", -1, gdb_stdout);
2683 }
2684
2685 /* The "complete" command is used by Emacs to implement completion. */
2686
2687 /* ARGSUSED */
2688 static void
2689 complete_command (arg, from_tty)
2690 char *arg;
2691 int from_tty;
2692 {
2693 int i;
2694 int argpoint;
2695 char *completion;
2696
2697 dont_repeat ();
2698
2699 if (arg == NULL)
2700 arg = "";
2701 argpoint = strlen (arg);
2702
2703 for (completion = line_completion_function (arg, i = 0, arg, argpoint);
2704 completion;
2705 completion = line_completion_function (arg, ++i, arg, argpoint))
2706 {
2707 printf_unfiltered ("%s\n", completion);
2708 free (completion);
2709 }
2710 }
2711
2712 /* The "show" command with no arguments shows all the settings. */
2713
2714 /* ARGSUSED */
2715 static void
2716 show_command (arg, from_tty)
2717 char *arg;
2718 int from_tty;
2719 {
2720 cmd_show_list (showlist, from_tty, "");
2721 }
2722 \f
2723 /* Add an element to the list of commands. */
2724
2725 void
2726 add_com (name, class, fun, doc)
2727 char *name;
2728 enum command_class class;
2729 void (*fun) PARAMS ((char *, int));
2730 char *doc;
2731 {
2732 add_cmd (name, class, fun, doc, &cmdlist);
2733 }
2734
2735 /* Add an alias or abbreviation command to the list of commands. */
2736
2737 void
2738 add_com_alias (name, oldname, class, abbrev_flag)
2739 char *name;
2740 char *oldname;
2741 enum command_class class;
2742 int abbrev_flag;
2743 {
2744 add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
2745 }
2746
2747 void
2748 error_no_arg (why)
2749 char *why;
2750 {
2751 error ("Argument required (%s).", why);
2752 }
2753
2754 /* ARGSUSED */
2755 static void
2756 help_command (command, from_tty)
2757 char *command;
2758 int from_tty; /* Ignored */
2759 {
2760 help_cmd (command, gdb_stdout);
2761 }
2762 \f
2763 static void
2764 validate_comname (comname)
2765 char *comname;
2766 {
2767 register char *p;
2768
2769 if (comname == 0)
2770 error_no_arg ("name of command to define");
2771
2772 p = comname;
2773 while (*p)
2774 {
2775 if (!isalnum (*p) && *p != '-' && *p != '_')
2776 error ("Junk in argument list: \"%s\"", p);
2777 p++;
2778 }
2779 }
2780
2781 /* This is just a placeholder in the command data structures. */
2782 static void
2783 user_defined_command (ignore, from_tty)
2784 char *ignore;
2785 int from_tty;
2786 {
2787 }
2788
2789 static void
2790 define_command (comname, from_tty)
2791 char *comname;
2792 int from_tty;
2793 {
2794 register struct command_line *cmds;
2795 register struct cmd_list_element *c, *newc, *hookc = 0;
2796 char *tem = comname;
2797 char tmpbuf[128];
2798 #define HOOK_STRING "hook-"
2799 #define HOOK_LEN 5
2800
2801 validate_comname (comname);
2802
2803 /* Look it up, and verify that we got an exact match. */
2804 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
2805 if (c && !STREQ (comname, c->name))
2806 c = 0;
2807
2808 if (c)
2809 {
2810 if (c->class == class_user || c->class == class_alias)
2811 tem = "Redefine command \"%s\"? ";
2812 else
2813 tem = "Really redefine built-in command \"%s\"? ";
2814 if (!query (tem, c->name))
2815 error ("Command \"%s\" not redefined.", c->name);
2816 }
2817
2818 /* If this new command is a hook, then mark the command which it
2819 is hooking. Note that we allow hooking `help' commands, so that
2820 we can hook the `stop' pseudo-command. */
2821
2822 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
2823 {
2824 /* Look up cmd it hooks, and verify that we got an exact match. */
2825 tem = comname + HOOK_LEN;
2826 hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
2827 if (hookc && !STREQ (comname + HOOK_LEN, hookc->name))
2828 hookc = 0;
2829 if (!hookc)
2830 {
2831 warning ("Your new `%s' command does not hook any existing command.",
2832 comname);
2833 if (!query ("Proceed? "))
2834 error ("Not confirmed.");
2835 }
2836 }
2837
2838 comname = savestring (comname, strlen (comname));
2839
2840 /* If the rest of the commands will be case insensitive, this one
2841 should behave in the same manner. */
2842 for (tem = comname; *tem; tem++)
2843 if (isupper (*tem))
2844 *tem = tolower (*tem);
2845
2846 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comname);
2847 cmds = read_command_lines (tmpbuf, from_tty);
2848
2849 if (c && c->class == class_user)
2850 free_command_lines (&c->user_commands);
2851
2852 newc = add_cmd (comname, class_user, user_defined_command,
2853 (c && c->class == class_user)
2854 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
2855 newc->user_commands = cmds;
2856
2857 /* If this new command is a hook, then mark both commands as being
2858 tied. */
2859 if (hookc)
2860 {
2861 hookc->hook = newc; /* Target gets hooked. */
2862 newc->hookee = hookc; /* We are marked as hooking target cmd. */
2863 }
2864 }
2865
2866 static void
2867 document_command (comname, from_tty)
2868 char *comname;
2869 int from_tty;
2870 {
2871 struct command_line *doclines;
2872 register struct cmd_list_element *c;
2873 char *tem = comname;
2874 char tmpbuf[128];
2875
2876 validate_comname (comname);
2877
2878 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
2879
2880 if (c->class != class_user)
2881 error ("Command \"%s\" is built-in.", comname);
2882
2883 sprintf (tmpbuf, "Type documentation for \"%s\".", comname);
2884 doclines = read_command_lines (tmpbuf, from_tty);
2885
2886 if (c->doc)
2887 free (c->doc);
2888
2889 {
2890 register struct command_line *cl1;
2891 register int len = 0;
2892
2893 for (cl1 = doclines; cl1; cl1 = cl1->next)
2894 len += strlen (cl1->line) + 1;
2895
2896 c->doc = (char *) xmalloc (len + 1);
2897 *c->doc = 0;
2898
2899 for (cl1 = doclines; cl1; cl1 = cl1->next)
2900 {
2901 strcat (c->doc, cl1->line);
2902 if (cl1->next)
2903 strcat (c->doc, "\n");
2904 }
2905 }
2906
2907 free_command_lines (&doclines);
2908 }
2909 \f
2910 /* Print the GDB banner. */
2911 void
2912 print_gdb_version (stream)
2913 GDB_FILE *stream;
2914 {
2915 /* From GNU coding standards, first line is meant to be easy for a
2916 program to parse, and is just canonical program name and version
2917 number, which starts after last space. */
2918
2919 fprintf_filtered (stream, "GNU gdb %s\n", version);
2920
2921 /* Second line is a copyright notice. */
2922
2923 fprintf_filtered (stream, "Copyright 1998 Free Software Foundation, Inc.\n");
2924
2925 /* Following the copyright is a brief statement that the program is
2926 free software, that users are free to copy and change it on
2927 certain conditions, that it is covered by the GNU GPL, and that
2928 there is no warranty. */
2929
2930 fprintf_filtered (stream, "\
2931 GDB is free software, covered by the GNU General Public License, and you are\n\
2932 welcome to change it and/or distribute copies of it under certain conditions.\n\
2933 Type \"show copying\" to see the conditions.\n\
2934 There is absolutely no warranty for GDB. Type \"show warranty\" for details.\n");
2935
2936 /* After the required info we print the configuration information. */
2937
2938 fprintf_filtered (stream, "This GDB was configured as \"");
2939 if (!STREQ (host_name, target_name))
2940 {
2941 fprintf_filtered (stream, "--host=%s --target=%s", host_name, target_name);
2942 }
2943 else
2944 {
2945 fprintf_filtered (stream, "%s", host_name);
2946 }
2947 fprintf_filtered (stream, "\".");
2948 }
2949
2950 /* ARGSUSED */
2951 static void
2952 show_version (args, from_tty)
2953 char *args;
2954 int from_tty;
2955 {
2956 immediate_quit++;
2957 print_gdb_version (gdb_stdout);
2958 printf_filtered ("\n");
2959 immediate_quit--;
2960 }
2961 \f
2962 /* get_prompt: access method for the GDB prompt string. */
2963
2964 #define MAX_PROMPT_SIZE 256
2965
2966 /*
2967 * int get_prompt_1 (char * buf);
2968 *
2969 * Work-horse for get_prompt (called via catch_errors).
2970 * Argument is buffer to hold the formatted prompt.
2971 *
2972 * Returns: 1 for success (use formatted prompt)
2973 * 0 for failure (use gdb_prompt_string).
2974 */
2975
2976 static int gdb_prompt_escape;
2977
2978 static int
2979 get_prompt_1 (formatted_prompt)
2980 char *formatted_prompt;
2981 {
2982 char *local_prompt;
2983
2984 if (async_p)
2985 local_prompt = PROMPT (0);
2986 else
2987 local_prompt = gdb_prompt_string;
2988
2989
2990 if (gdb_prompt_escape == 0)
2991 {
2992 return 0; /* do no formatting */
2993 }
2994 else
2995 /* formatted prompt */
2996 {
2997 char fmt[40], *promptp, *outp, *tmp;
2998 value_ptr arg_val;
2999 DOUBLEST doubleval;
3000 LONGEST longval;
3001 CORE_ADDR addrval;
3002
3003 int i, len;
3004 struct type *arg_type, *elt_type;
3005
3006 promptp = local_prompt;
3007 outp = formatted_prompt;
3008
3009 while (*promptp != '\0')
3010 {
3011 int available = MAX_PROMPT_SIZE - (outp - formatted_prompt) - 1;
3012
3013 if (*promptp != gdb_prompt_escape)
3014 {
3015 if (available >= 1) /* overflow protect */
3016 *outp++ = *promptp++;
3017 }
3018 else
3019 {
3020 /* GDB prompt string contains escape char. Parse for arg.
3021 Two consecutive escape chars followed by arg followed by
3022 a comma means to insert the arg using a default format.
3023 Otherwise a printf format string may be included between
3024 the two escape chars. eg:
3025 %%foo, insert foo using default format
3026 %2.2f%foo, insert foo using "%2.2f" format
3027 A mismatch between the format string and the data type
3028 of "foo" is an error (which we don't know how to protect
3029 against). */
3030
3031 fmt[0] = '\0'; /* assume null format string */
3032 if (promptp[1] == gdb_prompt_escape) /* double esc char */
3033 {
3034 promptp += 2; /* skip past two escape chars. */
3035 }
3036 else
3037 {
3038 /* extract format string from between two esc chars */
3039 i = 0;
3040 do
3041 {
3042 fmt[i++] = *promptp++; /* copy format string */
3043 }
3044 while (i < sizeof (fmt) - 1 &&
3045 *promptp != gdb_prompt_escape &&
3046 *promptp != '\0');
3047
3048 if (*promptp != gdb_prompt_escape)
3049 error ("Syntax error at prompt position %d",
3050 promptp - local_prompt);
3051 else
3052 {
3053 promptp++; /* skip second escape char */
3054 fmt[i++] = '\0'; /* terminate the format string */
3055 }
3056 }
3057
3058 arg_val = parse_to_comma_and_eval (&promptp);
3059 if (*promptp == ',')
3060 promptp++; /* skip past the comma */
3061 arg_type = check_typedef (VALUE_TYPE (arg_val));
3062 switch (TYPE_CODE (arg_type))
3063 {
3064 case TYPE_CODE_ARRAY:
3065 elt_type = check_typedef (TYPE_TARGET_TYPE (arg_type));
3066 if (TYPE_LENGTH (arg_type) > 0 &&
3067 TYPE_LENGTH (elt_type) == 1 &&
3068 TYPE_CODE (elt_type) == TYPE_CODE_INT)
3069 {
3070 int len = TYPE_LENGTH (arg_type);
3071
3072 if (VALUE_LAZY (arg_val))
3073 value_fetch_lazy (arg_val);
3074 tmp = VALUE_CONTENTS (arg_val);
3075
3076 if (len > available)
3077 len = available; /* overflow protect */
3078
3079 /* FIXME: how to protect GDB from crashing
3080 from bad user-supplied format string? */
3081 if (fmt[0] != 0)
3082 sprintf (outp, fmt, tmp);
3083 else
3084 strncpy (outp, tmp, len);
3085 outp[len] = '\0';
3086 }
3087 break;
3088 case TYPE_CODE_PTR:
3089 elt_type = check_typedef (TYPE_TARGET_TYPE (arg_type));
3090 addrval = value_as_pointer (arg_val);
3091
3092 if (TYPE_LENGTH (elt_type) == 1 &&
3093 TYPE_CODE (elt_type) == TYPE_CODE_INT &&
3094 addrval != 0)
3095 {
3096 /* display it as a string */
3097 char *default_fmt = "%s";
3098 char *tmp;
3099 int err = 0;
3100
3101 /* Limiting the number of bytes that the following call
3102 will read protects us from sprintf overflow later. */
3103 i = target_read_string (addrval, /* src */
3104 &tmp, /* dest */
3105 available, /* len */
3106 &err);
3107 if (err) /* read failed */
3108 error ("%s on target_read", safe_strerror (err));
3109
3110 tmp[i] = '\0'; /* force-terminate string */
3111 /* FIXME: how to protect GDB from crashing
3112 from bad user-supplied format string? */
3113 sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
3114 tmp);
3115 free (tmp);
3116 }
3117 else
3118 {
3119 /* display it as a pointer */
3120 char *default_fmt = "0x%x";
3121
3122 /* FIXME: how to protect GDB from crashing
3123 from bad user-supplied format string? */
3124 if (available >= 16 /*? */ ) /* overflow protect */
3125 sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
3126 (long) addrval);
3127 }
3128 break;
3129 case TYPE_CODE_FLT:
3130 {
3131 char *default_fmt = "%g";
3132
3133 doubleval = value_as_double (arg_val);
3134 /* FIXME: how to protect GDB from crashing
3135 from bad user-supplied format string? */
3136 if (available >= 16 /*? */ ) /* overflow protect */
3137 sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
3138 (double) doubleval);
3139 break;
3140 }
3141 case TYPE_CODE_INT:
3142 {
3143 char *default_fmt = "%d";
3144
3145 longval = value_as_long (arg_val);
3146 /* FIXME: how to protect GDB from crashing
3147 from bad user-supplied format string? */
3148 if (available >= 16 /*? */ ) /* overflow protect */
3149 sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
3150 (long) longval);
3151 break;
3152 }
3153 case TYPE_CODE_BOOL:
3154 {
3155 /* no default format for bool */
3156 longval = value_as_long (arg_val);
3157 if (available >= 8 /*? */ ) /* overflow protect */
3158 {
3159 if (longval)
3160 strcpy (outp, "<true>");
3161 else
3162 strcpy (outp, "<false>");
3163 }
3164 break;
3165 }
3166 case TYPE_CODE_ENUM:
3167 {
3168 /* no default format for enum */
3169 longval = value_as_long (arg_val);
3170 len = TYPE_NFIELDS (arg_type);
3171 /* find enum name if possible */
3172 for (i = 0; i < len; i++)
3173 if (TYPE_FIELD_BITPOS (arg_type, i) == longval)
3174 break; /* match -- end loop */
3175
3176 if (i < len) /* enum name found */
3177 {
3178 char *name = TYPE_FIELD_NAME (arg_type, i);
3179
3180 strncpy (outp, name, available);
3181 /* in casel available < strlen (name), */
3182 outp[available] = '\0';
3183 }
3184 else
3185 {
3186 if (available >= 16 /*? */ ) /* overflow protect */
3187 sprintf (outp, "%d", (long) longval);
3188 }
3189 break;
3190 }
3191 case TYPE_CODE_VOID:
3192 *outp = '\0';
3193 break; /* void type -- no output */
3194 default:
3195 error ("bad data type at prompt position %d",
3196 promptp - local_prompt);
3197 break;
3198 }
3199 outp += strlen (outp);
3200 }
3201 }
3202 *outp++ = '\0'; /* terminate prompt string */
3203 return 1;
3204 }
3205 }
3206
3207 char *
3208 get_prompt ()
3209 {
3210 static char buf[MAX_PROMPT_SIZE];
3211
3212 if (catch_errors (get_prompt_1, buf, "bad formatted prompt: ",
3213 RETURN_MASK_ALL))
3214 {
3215 return &buf[0]; /* successful formatted prompt */
3216 }
3217 else
3218 {
3219 /* Prompt could not be formatted. */
3220 if (async_p)
3221 return PROMPT (0);
3222 else
3223 return gdb_prompt_string;
3224 }
3225 }
3226
3227 void
3228 set_prompt (s)
3229 char *s;
3230 {
3231 /* ??rehrauer: I don't know why this fails, since it looks as though
3232 assignments to prompt are wrapped in calls to savestring...
3233 if (prompt != NULL)
3234 free (prompt);
3235 */
3236 if (async_p)
3237 PROMPT (0) = savestring (s, strlen (s));
3238 else
3239 gdb_prompt_string = savestring (s, strlen (s));
3240 }
3241 \f
3242
3243 /* If necessary, make the user confirm that we should quit. Return
3244 non-zero if we should quit, zero if we shouldn't. */
3245
3246 int
3247 quit_confirm ()
3248 {
3249 if (inferior_pid != 0 && target_has_execution)
3250 {
3251 char *s;
3252
3253 /* This is something of a hack. But there's no reliable way to
3254 see if a GUI is running. The `use_windows' variable doesn't
3255 cut it. */
3256 if (init_ui_hook)
3257 s = "A debugging session is active.\nDo you still want to close the debugger?";
3258 else if (attach_flag)
3259 s = "The program is running. Quit anyway (and detach it)? ";
3260 else
3261 s = "The program is running. Exit anyway? ";
3262
3263 if (!query (s))
3264 return 0;
3265 }
3266
3267 return 1;
3268 }
3269
3270 /* Quit without asking for confirmation. */
3271
3272 void
3273 quit_force (args, from_tty)
3274 char *args;
3275 int from_tty;
3276 {
3277 int exit_code = 0;
3278
3279 /* An optional expression may be used to cause gdb to terminate with the
3280 value of that expression. */
3281 if (args)
3282 {
3283 value_ptr val = parse_and_eval (args);
3284
3285 exit_code = (int) value_as_long (val);
3286 }
3287
3288 if (inferior_pid != 0 && target_has_execution)
3289 {
3290 if (attach_flag)
3291 target_detach (args, from_tty);
3292 else
3293 target_kill ();
3294 }
3295
3296 /* UDI wants this, to kill the TIP. */
3297 target_close (1);
3298
3299 /* Save the history information if it is appropriate to do so. */
3300 if (write_history_p && history_filename)
3301 write_history (history_filename);
3302
3303 do_final_cleanups (ALL_CLEANUPS); /* Do any final cleanups before exiting */
3304
3305 #if defined(TUI)
3306 /* tuiDo((TuiOpaqueFuncPtr)tuiCleanUp); */
3307 /* The above does not need to be inside a tuiDo(), since
3308 * it is not manipulating the curses screen, but rather,
3309 * it is tearing it down.
3310 */
3311 if (tui_version)
3312 tuiCleanUp ();
3313 #endif
3314
3315 exit (exit_code);
3316 }
3317
3318 /* Handle the quit command. */
3319
3320 void
3321 quit_command (args, from_tty)
3322 char *args;
3323 int from_tty;
3324 {
3325 if (!quit_confirm ())
3326 error ("Not confirmed.");
3327 quit_force (args, from_tty);
3328 }
3329
3330 /* Returns whether GDB is running on a terminal and whether the user
3331 desires that questions be asked of them on that terminal. */
3332
3333 int
3334 input_from_terminal_p ()
3335 {
3336 return gdb_has_a_terminal () && (instream == stdin) & caution;
3337 }
3338 \f
3339 /* ARGSUSED */
3340 static void
3341 pwd_command (args, from_tty)
3342 char *args;
3343 int from_tty;
3344 {
3345 if (args)
3346 error ("The \"pwd\" command does not take an argument: %s", args);
3347 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
3348
3349 if (!STREQ (gdb_dirbuf, current_directory))
3350 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
3351 current_directory, gdb_dirbuf);
3352 else
3353 printf_unfiltered ("Working directory %s.\n", current_directory);
3354 }
3355
3356 void
3357 cd_command (dir, from_tty)
3358 char *dir;
3359 int from_tty;
3360 {
3361 int len;
3362 /* Found something other than leading repetitions of "/..". */
3363 int found_real_path;
3364 char *p;
3365
3366 /* If the new directory is absolute, repeat is a no-op; if relative,
3367 repeat might be useful but is more likely to be a mistake. */
3368 dont_repeat ();
3369
3370 if (dir == 0)
3371 error_no_arg ("new working directory");
3372
3373 dir = tilde_expand (dir);
3374 make_cleanup (free, dir);
3375
3376 if (chdir (dir) < 0)
3377 perror_with_name (dir);
3378
3379 #if defined(_WIN32) || defined(__MSDOS__)
3380 /* There's too much mess with DOSish names like "d:", "d:.",
3381 "d:./foo" etc. Instead of having lots of special #ifdef'ed code,
3382 simply get the canonicalized name of the current directory. */
3383 dir = getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
3384 #endif
3385
3386 len = strlen (dir);
3387 if (SLASH_P (dir[len-1]))
3388 {
3389 /* Remove the trailing slash unless this is a root directory
3390 (including a drive letter on non-Unix systems). */
3391 if (!(len == 1) /* "/" */
3392 #if defined(_WIN32) || defined(__MSDOS__)
3393 && !(!SLASH_P (*dir) && ROOTED_P (dir) && len <= 3) /* "d:/" */
3394 #endif
3395 )
3396 len--;
3397 }
3398
3399 dir = savestring (dir, len);
3400 if (ROOTED_P (dir))
3401 current_directory = dir;
3402 else
3403 {
3404 if (SLASH_P (current_directory[strlen (current_directory) - 1]))
3405 current_directory = concat (current_directory, dir, NULL);
3406 else
3407 current_directory = concat (current_directory, SLASH_STRING, dir, NULL);
3408 free (dir);
3409 }
3410
3411 /* Now simplify any occurrences of `.' and `..' in the pathname. */
3412
3413 found_real_path = 0;
3414 for (p = current_directory; *p;)
3415 {
3416 if (SLASH_P (p[0]) && p[1] == '.' && (p[2] == 0 || SLASH_P (p[2])))
3417 strcpy (p, p + 2);
3418 else if (SLASH_P (p[0]) && p[1] == '.' && p[2] == '.'
3419 && (p[3] == 0 || SLASH_P (p[3])))
3420 {
3421 if (found_real_path)
3422 {
3423 /* Search backwards for the directory just before the "/.."
3424 and obliterate it and the "/..". */
3425 char *q = p;
3426 while (q != current_directory && !SLASH_P (q[-1]))
3427 --q;
3428
3429 if (q == current_directory)
3430 /* current_directory is
3431 a relative pathname ("can't happen"--leave it alone). */
3432 ++p;
3433 else
3434 {
3435 strcpy (q - 1, p + 3);
3436 p = q - 1;
3437 }
3438 }
3439 else
3440 /* We are dealing with leading repetitions of "/..", for example
3441 "/../..", which is the Mach super-root. */
3442 p += 3;
3443 }
3444 else
3445 {
3446 found_real_path = 1;
3447 ++p;
3448 }
3449 }
3450
3451 forget_cached_source_info ();
3452
3453 if (from_tty)
3454 pwd_command ((char *) 0, 1);
3455 }
3456 \f
3457 struct source_cleanup_lines_args
3458 {
3459 int old_line;
3460 char *old_file;
3461 char *old_pre_error;
3462 char *old_error_pre_print;
3463 };
3464
3465 static void
3466 source_cleanup_lines (args)
3467 PTR args;
3468 {
3469 struct source_cleanup_lines_args *p =
3470 (struct source_cleanup_lines_args *) args;
3471 source_line_number = p->old_line;
3472 source_file_name = p->old_file;
3473 source_pre_error = p->old_pre_error;
3474 error_pre_print = p->old_error_pre_print;
3475 }
3476
3477 /* ARGSUSED */
3478 void
3479 source_command (args, from_tty)
3480 char *args;
3481 int from_tty;
3482 {
3483 FILE *stream;
3484 struct cleanup *old_cleanups;
3485 char *file = args;
3486 struct source_cleanup_lines_args old_lines;
3487 int needed_length;
3488
3489 if (file == NULL)
3490 {
3491 error ("source command requires pathname of file to source.");
3492 }
3493
3494 file = tilde_expand (file);
3495 old_cleanups = make_cleanup (free, file);
3496
3497 stream = fopen (file, FOPEN_RT);
3498 if (!stream)
3499 {
3500 if (from_tty)
3501 perror_with_name (file);
3502 else
3503 return;
3504 }
3505
3506 make_cleanup ((make_cleanup_func) fclose, stream);
3507
3508 old_lines.old_line = source_line_number;
3509 old_lines.old_file = source_file_name;
3510 old_lines.old_pre_error = source_pre_error;
3511 old_lines.old_error_pre_print = error_pre_print;
3512 make_cleanup (source_cleanup_lines, &old_lines);
3513 source_line_number = 0;
3514 source_file_name = file;
3515 source_pre_error = error_pre_print == NULL ? "" : error_pre_print;
3516 source_pre_error = savestring (source_pre_error, strlen (source_pre_error));
3517 make_cleanup (free, source_pre_error);
3518 /* This will get set every time we read a line. So it won't stay "" for
3519 long. */
3520 error_pre_print = "";
3521
3522 needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80;
3523 if (source_error_allocated < needed_length)
3524 {
3525 source_error_allocated *= 2;
3526 if (source_error_allocated < needed_length)
3527 source_error_allocated = needed_length;
3528 if (source_error == NULL)
3529 source_error = xmalloc (source_error_allocated);
3530 else
3531 source_error = xrealloc (source_error, source_error_allocated);
3532 }
3533
3534 read_command_file (stream);
3535
3536 do_cleanups (old_cleanups);
3537 }
3538
3539 /* ARGSUSED */
3540 static void
3541 echo_command (text, from_tty)
3542 char *text;
3543 int from_tty;
3544 {
3545 char *p = text;
3546 register int c;
3547
3548 if (text)
3549 while ((c = *p++) != '\0')
3550 {
3551 if (c == '\\')
3552 {
3553 /* \ at end of argument is used after spaces
3554 so they won't be lost. */
3555 if (*p == 0)
3556 return;
3557
3558 c = parse_escape (&p);
3559 if (c >= 0)
3560 printf_filtered ("%c", c);
3561 }
3562 else
3563 printf_filtered ("%c", c);
3564 }
3565
3566 /* Force this output to appear now. */
3567 wrap_here ("");
3568 gdb_flush (gdb_stdout);
3569 }
3570
3571 /* ARGSUSED */
3572 static void
3573 dont_repeat_command (ignored, from_tty)
3574 char *ignored;
3575 int from_tty;
3576 {
3577 *line = 0; /* Can't call dont_repeat here because we're not
3578 necessarily reading from stdin. */
3579 }
3580 \f
3581 /* Functions to manipulate command line editing control variables. */
3582
3583 /* Number of commands to print in each call to show_commands. */
3584 #define Hist_print 10
3585 static void
3586 show_commands (args, from_tty)
3587 char *args;
3588 int from_tty;
3589 {
3590 /* Index for history commands. Relative to history_base. */
3591 int offset;
3592
3593 /* Number of the history entry which we are planning to display next.
3594 Relative to history_base. */
3595 static int num = 0;
3596
3597 /* The first command in the history which doesn't exist (i.e. one more
3598 than the number of the last command). Relative to history_base. */
3599 int hist_len;
3600
3601 extern HIST_ENTRY *history_get PARAMS ((int));
3602
3603 /* Print out some of the commands from the command history. */
3604 /* First determine the length of the history list. */
3605 hist_len = history_size;
3606 for (offset = 0; offset < history_size; offset++)
3607 {
3608 if (!history_get (history_base + offset))
3609 {
3610 hist_len = offset;
3611 break;
3612 }
3613 }
3614
3615 if (args)
3616 {
3617 if (args[0] == '+' && args[1] == '\0')
3618 /* "info editing +" should print from the stored position. */
3619 ;
3620 else
3621 /* "info editing <exp>" should print around command number <exp>. */
3622 num = (parse_and_eval_address (args) - history_base) - Hist_print / 2;
3623 }
3624 /* "show commands" means print the last Hist_print commands. */
3625 else
3626 {
3627 num = hist_len - Hist_print;
3628 }
3629
3630 if (num < 0)
3631 num = 0;
3632
3633 /* If there are at least Hist_print commands, we want to display the last
3634 Hist_print rather than, say, the last 6. */
3635 if (hist_len - num < Hist_print)
3636 {
3637 num = hist_len - Hist_print;
3638 if (num < 0)
3639 num = 0;
3640 }
3641
3642 for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
3643 {
3644 printf_filtered ("%5d %s\n", history_base + offset,
3645 (history_get (history_base + offset))->line);
3646 }
3647
3648 /* The next command we want to display is the next one that we haven't
3649 displayed yet. */
3650 num += Hist_print;
3651
3652 /* If the user repeats this command with return, it should do what
3653 "show commands +" does. This is unnecessary if arg is null,
3654 because "show commands +" is not useful after "show commands". */
3655 if (from_tty && args)
3656 {
3657 args[0] = '+';
3658 args[1] = '\0';
3659 }
3660 }
3661
3662 /* Called by do_setshow_command. */
3663 /* ARGSUSED */
3664 static void
3665 set_history_size_command (args, from_tty, c)
3666 char *args;
3667 int from_tty;
3668 struct cmd_list_element *c;
3669 {
3670 if (history_size == INT_MAX)
3671 unstifle_history ();
3672 else if (history_size >= 0)
3673 stifle_history (history_size);
3674 else
3675 {
3676 history_size = INT_MAX;
3677 error ("History size must be non-negative");
3678 }
3679 }
3680
3681 /* ARGSUSED */
3682 static void
3683 set_history (args, from_tty)
3684 char *args;
3685 int from_tty;
3686 {
3687 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3688 help_list (sethistlist, "set history ", -1, gdb_stdout);
3689 }
3690
3691 /* ARGSUSED */
3692 static void
3693 show_history (args, from_tty)
3694 char *args;
3695 int from_tty;
3696 {
3697 cmd_show_list (showhistlist, from_tty, "");
3698 }
3699
3700 int info_verbose = 0; /* Default verbose msgs off */
3701
3702 /* Called by do_setshow_command. An elaborate joke. */
3703 /* ARGSUSED */
3704 static void
3705 set_verbose (args, from_tty, c)
3706 char *args;
3707 int from_tty;
3708 struct cmd_list_element *c;
3709 {
3710 char *cmdname = "verbose";
3711 struct cmd_list_element *showcmd;
3712
3713 showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
3714
3715 if (info_verbose)
3716 {
3717 c->doc = "Set verbose printing of informational messages.";
3718 showcmd->doc = "Show verbose printing of informational messages.";
3719 }
3720 else
3721 {
3722 c->doc = "Set verbosity.";
3723 showcmd->doc = "Show verbosity.";
3724 }
3725 }
3726
3727 static void
3728 float_handler (signo)
3729 int signo;
3730 {
3731 /* This message is based on ANSI C, section 4.7. Note that integer
3732 divide by zero causes this, so "float" is a misnomer. */
3733 signal (SIGFPE, float_handler);
3734 error ("Erroneous arithmetic operation.");
3735 }
3736 \f
3737
3738 static void
3739 init_cmd_lists ()
3740 {
3741 cmdlist = NULL;
3742 infolist = NULL;
3743 enablelist = NULL;
3744 disablelist = NULL;
3745 togglelist = NULL;
3746 stoplist = NULL;
3747 deletelist = NULL;
3748 enablebreaklist = NULL;
3749 setlist = NULL;
3750 unsetlist = NULL;
3751 showlist = NULL;
3752 sethistlist = NULL;
3753 showhistlist = NULL;
3754 unsethistlist = NULL;
3755 maintenancelist = NULL;
3756 maintenanceinfolist = NULL;
3757 maintenanceprintlist = NULL;
3758 setprintlist = NULL;
3759 showprintlist = NULL;
3760 setchecklist = NULL;
3761 showchecklist = NULL;
3762 }
3763
3764 /* Init the history buffer. Note that we are called after the init file(s)
3765 * have been read so that the user can change the history file via his
3766 * .gdbinit file (for instance). The GDBHISTFILE environment variable
3767 * overrides all of this.
3768 */
3769
3770 void
3771 init_history ()
3772 {
3773 char *tmpenv;
3774
3775 tmpenv = getenv ("HISTSIZE");
3776 if (tmpenv)
3777 history_size = atoi (tmpenv);
3778 else if (!history_size)
3779 history_size = 256;
3780
3781 stifle_history (history_size);
3782
3783 tmpenv = getenv ("GDBHISTFILE");
3784 if (tmpenv)
3785 history_filename = savestring (tmpenv, strlen (tmpenv));
3786 else if (!history_filename)
3787 {
3788 /* We include the current directory so that if the user changes
3789 directories the file written will be the same as the one
3790 that was read. */
3791 #ifdef __MSDOS__
3792 /* No leading dots in file names are allowed on MSDOS. */
3793 history_filename = concat (current_directory, "/_gdb_history", NULL);
3794 #else
3795 history_filename = concat (current_directory, "/.gdb_history", NULL);
3796 #endif
3797 }
3798 read_history (history_filename);
3799 }
3800
3801 static void
3802 init_main ()
3803 {
3804 struct cmd_list_element *c;
3805
3806 /* If we are running the asynchronous version,
3807 we initialize the prompts differently. */
3808 if (!async_p)
3809 {
3810 #ifdef DEFAULT_PROMPT
3811 gdb_prompt_string = savestring (DEFAULT_PROMPT, strlen (DEFAULT_PROMPT));
3812 #else
3813 gdb_prompt_string = savestring ("(gdb) ", 6);
3814 #endif
3815 }
3816 else
3817 {
3818 /* initialize the prompt stack to a simple "(gdb) " prompt or to
3819 whatever the DEFAULT_PROMPT is. */
3820 the_prompts.top = 0;
3821 PREFIX (0) = "";
3822 #ifdef DEFAULT_PROMPT
3823 PROMPT (0) = savestring (DEFAULT_PROMPT, strlen (DEFAULT_PROMPT));
3824 #else
3825 PROMPT (0) = savestring ("(gdb) ", 6);
3826 #endif
3827 SUFFIX (0) = "";
3828 /* Set things up for annotation_level > 1, if the user ever decides
3829 to use it. */
3830 async_annotation_suffix = "prompt";
3831 /* Set the variable associated with the setshow prompt command. */
3832 new_async_prompt = savestring (PROMPT (0), strlen (PROMPT (0)));
3833 }
3834 gdb_prompt_escape = 0; /* default to none. */
3835
3836 /* Set the important stuff up for command editing. */
3837 command_editing_p = 1;
3838 history_expansion_p = 0;
3839 write_history_p = 0;
3840
3841 /* Setup important stuff for command line editing. */
3842 rl_completion_entry_function = (int (*)()) readline_line_completion_function;
3843 rl_completer_word_break_characters = gdb_completer_word_break_characters;
3844 rl_completer_quote_characters = gdb_completer_quote_characters;
3845 rl_readline_name = "gdb";
3846
3847 /* Define the classes of commands.
3848 They will appear in the help list in the reverse of this order. */
3849
3850 add_cmd ("internals", class_maintenance, NO_FUNCTION,
3851 "Maintenance commands.\n\
3852 Some gdb commands are provided just for use by gdb maintainers.\n\
3853 These commands are subject to frequent change, and may not be as\n\
3854 well documented as user commands.",
3855 &cmdlist);
3856 add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist);
3857 add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist);
3858 add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\
3859 The commands in this class are those defined by the user.\n\
3860 Use the \"define\" command to define a command.", &cmdlist);
3861 add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist);
3862 if (!dbx_commands)
3863 add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist);
3864 add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist);
3865 add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist);
3866 add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist);
3867 add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\
3868 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
3869 counting from zero for the innermost (currently executing) frame.\n\n\
3870 At any time gdb identifies one frame as the \"selected\" frame.\n\
3871 Variable lookups are done with respect to the selected frame.\n\
3872 When the program being debugged stops, gdb selects the innermost frame.\n\
3873 The commands below can be used to select other frames by number or address.",
3874 &cmdlist);
3875 add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist);
3876
3877 add_com ("pwd", class_files, pwd_command,
3878 "Print working directory. This is used for your program as well.");
3879 c = add_cmd ("cd", class_files, cd_command,
3880 "Set working directory to DIR for debugger and program being debugged.\n\
3881 The change does not take effect for the program being debugged\n\
3882 until the next time it is started.", &cmdlist);
3883 c->completer = filename_completer;
3884
3885 /* The set prompt command is different depending whether or not the
3886 async version is run. NOTE: this difference is going to
3887 disappear as we make the event loop be the default engine of
3888 gdb. */
3889 if (!async_p)
3890 {
3891 add_show_from_set
3892 (add_set_cmd ("prompt", class_support, var_string,
3893 (char *) &gdb_prompt_string, "Set gdb's prompt",
3894 &setlist),
3895 &showlist);
3896 }
3897 else
3898 {
3899 c = add_set_cmd ("prompt", class_support, var_string,
3900 (char *) &new_async_prompt, "Set gdb's prompt",
3901 &setlist);
3902 add_show_from_set (c, &showlist);
3903 c->function.sfunc = set_async_prompt;
3904 }
3905
3906 add_show_from_set
3907 (add_set_cmd ("prompt-escape-char", class_support, var_zinteger,
3908 (char *) &gdb_prompt_escape,
3909 "Set escape character for formatting of gdb's prompt",
3910 &setlist),
3911 &showlist);
3912
3913 add_com ("echo", class_support, echo_command,
3914 "Print a constant string. Give string as argument.\n\
3915 C escape sequences may be used in the argument.\n\
3916 No newline is added at the end of the argument;\n\
3917 use \"\\n\" if you want a newline to be printed.\n\
3918 Since leading and trailing whitespace are ignored in command arguments,\n\
3919 if you want to print some you must use \"\\\" before leading whitespace\n\
3920 to be printed or after trailing whitespace.");
3921 add_com ("document", class_support, document_command,
3922 "Document a user-defined command.\n\
3923 Give command name as argument. Give documentation on following lines.\n\
3924 End with a line of just \"end\".");
3925 add_com ("define", class_support, define_command,
3926 "Define a new command name. Command name is argument.\n\
3927 Definition appears on following lines, one command per line.\n\
3928 End with a line of just \"end\".\n\
3929 Use the \"document\" command to give documentation for the new command.\n\
3930 Commands defined in this way may have up to ten arguments.");
3931
3932 #ifdef __STDC__
3933 c = add_cmd ("source", class_support, source_command,
3934 "Read commands from a file named FILE.\n\
3935 Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\
3936 when gdb is started.", &cmdlist);
3937 #else
3938 /* Punt file name, we can't help it easily. */
3939 c = add_cmd ("source", class_support, source_command,
3940 "Read commands from a file named FILE.\n\
3941 Note that the file \".gdbinit\" is read automatically in this way\n\
3942 when gdb is started.", &cmdlist);
3943 #endif
3944 c->completer = filename_completer;
3945
3946 add_com ("quit", class_support, quit_command, "Exit gdb.");
3947 add_com ("help", class_support, help_command, "Print list of commands.");
3948 add_com_alias ("q", "quit", class_support, 1);
3949 add_com_alias ("h", "help", class_support, 1);
3950
3951 add_com ("dont-repeat", class_support, dont_repeat_command, "Don't repeat this command.\n\
3952 Primarily used inside of user-defined commands that should not be repeated when\n\
3953 hitting return.");
3954
3955 c = add_set_cmd ("verbose", class_support, var_boolean, (char *) &info_verbose,
3956 "Set ",
3957 &setlist),
3958 add_show_from_set (c, &showlist);
3959 c->function.sfunc = set_verbose;
3960 set_verbose (NULL, 0, c);
3961
3962 /* The set editing command is different depending whether or not the
3963 async version is run. NOTE: this difference is going to disappear
3964 as we make the event loop be the default engine of gdb. */
3965 if (!async_p)
3966 {
3967 add_show_from_set
3968 (add_set_cmd ("editing", class_support, var_boolean, (char *) &command_editing_p,
3969 "Set editing of command lines as they are typed.\n\
3970 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
3971 Without an argument, command line editing is enabled. To edit, use\n\
3972 EMACS-like or VI-like commands like control-P or ESC.", &setlist),
3973 &showlist);
3974 }
3975 else
3976 {
3977 c = add_set_cmd ("editing", class_support, var_boolean, (char *) &async_command_editing_p,
3978 "Set editing of command lines as they are typed.\n\
3979 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
3980 Without an argument, command line editing is enabled. To edit, use\n\
3981 EMACS-like or VI-like commands like control-P or ESC.", &setlist);
3982
3983 add_show_from_set (c, &showlist);
3984 c->function.sfunc = set_async_editing_command;
3985 }
3986
3987 add_prefix_cmd ("history", class_support, set_history,
3988 "Generic command for setting command history parameters.",
3989 &sethistlist, "set history ", 0, &setlist);
3990 add_prefix_cmd ("history", class_support, show_history,
3991 "Generic command for showing command history parameters.",
3992 &showhistlist, "show history ", 0, &showlist);
3993
3994 add_show_from_set
3995 (add_set_cmd ("expansion", no_class, var_boolean, (char *) &history_expansion_p,
3996 "Set history expansion on command input.\n\
3997 Without an argument, history expansion is enabled.", &sethistlist),
3998 &showhistlist);
3999
4000 add_show_from_set
4001 (add_set_cmd ("save", no_class, var_boolean, (char *) &write_history_p,
4002 "Set saving of the history record on exit.\n\
4003 Use \"on\" to enable the saving, and \"off\" to disable it.\n\
4004 Without an argument, saving is enabled.", &sethistlist),
4005 &showhistlist);
4006
4007 c = add_set_cmd ("size", no_class, var_integer, (char *) &history_size,
4008 "Set the size of the command history, \n\
4009 ie. the number of previous commands to keep a record of.", &sethistlist);
4010 add_show_from_set (c, &showhistlist);
4011 c->function.sfunc = set_history_size_command;
4012
4013 add_show_from_set
4014 (add_set_cmd ("filename", no_class, var_filename, (char *) &history_filename,
4015 "Set the filename in which to record the command history\n\
4016 (the list of previous commands of which a record is kept).", &sethistlist),
4017 &showhistlist);
4018
4019 add_show_from_set
4020 (add_set_cmd ("confirm", class_support, var_boolean,
4021 (char *) &caution,
4022 "Set whether to confirm potentially dangerous operations.",
4023 &setlist),
4024 &showlist);
4025
4026 add_prefix_cmd ("info", class_info, info_command,
4027 "Generic command for showing things about the program being debugged.",
4028 &infolist, "info ", 0, &cmdlist);
4029 add_com_alias ("i", "info", class_info, 1);
4030
4031 add_com ("complete", class_obscure, complete_command,
4032 "List the completions for the rest of the line as a command.");
4033
4034 add_prefix_cmd ("show", class_info, show_command,
4035 "Generic command for showing things about the debugger.",
4036 &showlist, "show ", 0, &cmdlist);
4037 /* Another way to get at the same thing. */
4038 add_info ("set", show_command, "Show all GDB settings.");
4039
4040 add_cmd ("commands", no_class, show_commands,
4041 "Show the history of commands you typed.\n\
4042 You can supply a command number to start with, or a `+' to start after\n\
4043 the previous command number shown.",
4044 &showlist);
4045
4046 add_cmd ("version", no_class, show_version,
4047 "Show what version of GDB this is.", &showlist);
4048
4049 add_com ("while", class_support, while_command,
4050 "Execute nested commands WHILE the conditional expression is non zero.\n\
4051 The conditional expression must follow the word `while' and must in turn be\n\
4052 followed by a new line. The nested commands must be entered one per line,\n\
4053 and should be terminated by the word `end'.");
4054
4055 add_com ("if", class_support, if_command,
4056 "Execute nested commands once IF the conditional expression is non zero.\n\
4057 The conditional expression must follow the word `if' and must in turn be\n\
4058 followed by a new line. The nested commands must be entered one per line,\n\
4059 and should be terminated by the word 'else' or `end'. If an else clause\n\
4060 is used, the same rules apply to its nested commands as to the first ones.");
4061
4062 /* If target is open when baud changes, it doesn't take effect until the
4063 next open (I think, not sure). */
4064 add_show_from_set (add_set_cmd ("remotebaud", no_class,
4065 var_zinteger, (char *) &baud_rate,
4066 "Set baud rate for remote serial I/O.\n\
4067 This value is used to set the speed of the serial port when debugging\n\
4068 using remote targets.", &setlist),
4069 &showlist);
4070
4071 add_show_from_set (
4072 add_set_cmd ("remotedebug", no_class, var_zinteger, (char *) &remote_debug,
4073 "Set debugging of remote protocol.\n\
4074 When enabled, each packet sent or received with the remote target\n\
4075 is displayed.", &setlist),
4076 &showlist);
4077
4078 add_show_from_set (
4079 add_set_cmd ("remotetimeout", no_class, var_integer, (char *) &remote_timeout,
4080 "Set timeout limit to wait for target to respond.\n\
4081 This value is used to set the time limit for gdb to wait for a response\n\
4082 from the target.", &setlist),
4083 &showlist);
4084
4085 /* The set annotate command is different depending whether or not
4086 the async version is run. NOTE: this difference is going to
4087 disappear as we make the event loop be the default engine of
4088 gdb. */
4089 if (!async_p)
4090 {
4091 c = add_set_cmd ("annotate", class_obscure, var_zinteger,
4092 (char *) &annotation_level, "Set annotation_level.\n\
4093 0 == normal; 1 == fullname (for use when running under emacs)\n\
4094 2 == output annotated suitably for use by programs that control GDB.",
4095 &setlist);
4096 c = add_show_from_set (c, &showlist);
4097 }
4098 else
4099 {
4100 c = add_set_cmd ("annotate", class_obscure, var_zinteger,
4101 (char *) &annotation_level, "Set annotation_level.\n\
4102 0 == normal; 1 == fullname (for use when running under emacs)\n\
4103 2 == output annotated suitably for use by programs that control GDB.",
4104 &setlist);
4105 add_show_from_set (c, &showlist);
4106 c->function.sfunc = set_async_annotation_level;
4107 }
4108 }