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