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