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