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