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