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