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