1 /* CLI Definitions for GDB, the GNU debugger.
3 Copyright (C) 2002-2014 Free Software Foundation, Inc.
5 This file is part of GDB.
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "event-top.h"
25 #include "top.h" /* for "execute_command" */
27 #include "exceptions.h"
31 /* These are the ui_out and the interpreter for the console
33 struct ui_out *cli_uiout;
34 static struct interp *cli_interp;
36 /* Longjmp-safe wrapper for "execute_command". */
37 static struct gdb_exception safe_execute_command (struct ui_out *uiout,
41 /* Observers for several run control events. If the interpreter is
42 quiet (i.e., another interpreter is being run with
43 interpreter-exec), print nothing. */
45 /* Observer for the signal_received notification. */
48 cli_on_signal_received (enum gdb_signal siggnal)
50 if (!interp_quiet_p (cli_interp))
51 print_signal_received_reason (cli_uiout, siggnal);
54 /* Observer for the end_stepping_range notification. */
57 cli_on_end_stepping_range (void)
59 if (!interp_quiet_p (cli_interp))
60 print_end_stepping_range_reason (cli_uiout);
63 /* Observer for the signalled notification. */
66 cli_on_signal_exited (enum gdb_signal siggnal)
68 if (!interp_quiet_p (cli_interp))
69 print_signal_exited_reason (cli_uiout, siggnal);
72 /* Observer for the exited notification. */
75 cli_on_exited (int exitstatus)
77 if (!interp_quiet_p (cli_interp))
78 print_exited_reason (cli_uiout, exitstatus);
81 /* Observer for the no_history notification. */
84 cli_on_no_history (void)
86 if (!interp_quiet_p (cli_interp))
87 print_no_history_reason (cli_uiout);
90 /* These implement the cli out interpreter: */
93 cli_interpreter_init (struct interp *self, int top_level)
95 /* If changing this, remember to update tui-interp.c as well. */
96 observer_attach_end_stepping_range (cli_on_end_stepping_range);
97 observer_attach_signal_received (cli_on_signal_received);
98 observer_attach_signal_exited (cli_on_signal_exited);
99 observer_attach_exited (cli_on_exited);
100 observer_attach_no_history (cli_on_no_history);
106 cli_interpreter_resume (void *data)
108 struct ui_file *stream;
110 /*sync_execution = 1; */
112 /* gdb_setup_readline will change gdb_stdout. If the CLI was
113 previously writing to gdb_stdout, then set it to the new
114 gdb_stdout afterwards. */
116 stream = cli_out_set_stream (cli_uiout, gdb_stdout);
117 if (stream != gdb_stdout)
119 cli_out_set_stream (cli_uiout, stream);
123 gdb_setup_readline ();
126 cli_out_set_stream (cli_uiout, gdb_stdout);
132 cli_interpreter_suspend (void *data)
134 gdb_disable_readline ();
138 /* Don't display the prompt if we are set quiet. */
140 cli_interpreter_display_prompt_p (void *data)
142 if (interp_quiet_p (NULL))
148 static struct gdb_exception
149 cli_interpreter_exec (void *data, const char *command_str)
151 struct ui_file *old_stream;
152 struct gdb_exception result;
154 /* FIXME: cagney/2003-02-01: Need to const char *propogate
155 safe_execute_command. */
156 char *str = strcpy (alloca (strlen (command_str) + 1), command_str);
158 /* gdb_stdout could change between the time cli_uiout was
159 initialized and now. Since we're probably using a different
160 interpreter which has a new ui_file for gdb_stdout, use that one
161 instead of the default.
163 It is important that it gets reset everytime, since the user
164 could set gdb to use a different interpreter. */
165 old_stream = cli_out_set_stream (cli_uiout, gdb_stdout);
166 result = safe_execute_command (cli_uiout, str, 1);
167 cli_out_set_stream (cli_uiout, old_stream);
171 static struct gdb_exception
172 safe_execute_command (struct ui_out *command_uiout, char *command, int from_tty)
174 volatile struct gdb_exception e;
175 struct ui_out *saved_uiout;
177 /* Save and override the global ``struct ui_out'' builder. */
178 saved_uiout = current_uiout;
179 current_uiout = command_uiout;
181 TRY_CATCH (e, RETURN_MASK_ALL)
183 execute_command (command, from_tty);
186 /* Restore the global builder. */
187 current_uiout = saved_uiout;
189 /* FIXME: cagney/2005-01-13: This shouldn't be needed. Instead the
190 caller should print the exception. */
191 exception_print (gdb_stderr, e);
195 static struct ui_out *
196 cli_ui_out (struct interp *self)
201 /* Standard gdb initialization hook. */
202 extern initialize_file_ftype _initialize_cli_interp; /* -Wmissing-prototypes */
205 _initialize_cli_interp (void)
207 static const struct interp_procs procs = {
208 cli_interpreter_init, /* init_proc */
209 cli_interpreter_resume, /* resume_proc */
210 cli_interpreter_suspend, /* suspend_proc */
211 cli_interpreter_exec, /* exec_proc */
212 cli_interpreter_display_prompt_p, /* prompt_proc_p */
213 cli_ui_out, /* ui_out_proc */
214 NULL, /* set_logging_proc */
215 cli_command_loop /* command_loop_proc */
218 /* Create a default uiout builder for the CLI. */
219 cli_uiout = cli_out_new (gdb_stdout);
220 cli_interp = interp_new (INTERP_CONSOLE, &procs);
222 interp_add (cli_interp);