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