1 /* CLI Definitions for GDB, the GNU debugger.
3 Copyright (C) 2002-2016 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" */
29 /* The console interpreter. */
32 /* The ui_out for the console interpreter. */
33 struct ui_out *cli_uiout;
36 /* The interpreter for the console interpreter. */
37 static struct interp *cli_interp;
39 /* Longjmp-safe wrapper for "execute_command". */
40 static struct gdb_exception safe_execute_command (struct ui_out *uiout,
44 /* Observers for several run control events. If the interpreter is
45 quiet (i.e., another interpreter is being run with
46 interpreter-exec), print nothing. */
48 /* Observer for the normal_stop notification. */
51 cli_on_normal_stop (struct bpstats *bs, int print_frame)
53 if (!interp_quiet_p (cli_interp))
56 print_stop_event (interp_ui_out (cli_interp));
60 /* Observer for the signal_received notification. */
63 cli_on_signal_received (enum gdb_signal siggnal)
65 if (!interp_quiet_p (cli_interp))
66 print_signal_received_reason (interp_ui_out (cli_interp), siggnal);
69 /* Observer for the end_stepping_range notification. */
72 cli_on_end_stepping_range (void)
74 if (!interp_quiet_p (cli_interp))
75 print_end_stepping_range_reason (interp_ui_out (cli_interp));
78 /* Observer for the signalled notification. */
81 cli_on_signal_exited (enum gdb_signal siggnal)
83 if (!interp_quiet_p (cli_interp))
84 print_signal_exited_reason (interp_ui_out (cli_interp), siggnal);
87 /* Observer for the exited notification. */
90 cli_on_exited (int exitstatus)
92 if (!interp_quiet_p (cli_interp))
93 print_exited_reason (interp_ui_out (cli_interp), exitstatus);
96 /* Observer for the no_history notification. */
99 cli_on_no_history (void)
101 if (!interp_quiet_p (cli_interp))
102 print_no_history_reason (interp_ui_out (cli_interp));
105 /* Observer for the sync_execution_done notification. */
108 cli_on_sync_execution_done (void)
110 if (!interp_quiet_p (cli_interp))
111 display_gdb_prompt (NULL);
114 /* Observer for the command_error notification. */
117 cli_on_command_error (void)
119 if (!interp_quiet_p (cli_interp))
120 display_gdb_prompt (NULL);
123 /* These implement the cli out interpreter: */
126 cli_interpreter_init (struct interp *self, int top_level)
131 /* If changing this, remember to update tui-interp.c as well. */
132 observer_attach_normal_stop (cli_on_normal_stop);
133 observer_attach_end_stepping_range (cli_on_end_stepping_range);
134 observer_attach_signal_received (cli_on_signal_received);
135 observer_attach_signal_exited (cli_on_signal_exited);
136 observer_attach_exited (cli_on_exited);
137 observer_attach_no_history (cli_on_no_history);
138 observer_attach_sync_execution_done (cli_on_sync_execution_done);
139 observer_attach_command_error (cli_on_command_error);
141 return interp_data (self);
145 cli_interpreter_resume (void *data)
147 struct cli_interp *cli = (struct cli_interp *) data;
148 struct ui_file *stream;
150 /*sync_execution = 1; */
152 /* gdb_setup_readline will change gdb_stdout. If the CLI was
153 previously writing to gdb_stdout, then set it to the new
154 gdb_stdout afterwards. */
156 stream = cli_out_set_stream (cli->cli_uiout, gdb_stdout);
157 if (stream != gdb_stdout)
159 cli_out_set_stream (cli->cli_uiout, stream);
163 gdb_setup_readline ();
166 cli_out_set_stream (cli->cli_uiout, gdb_stdout);
172 cli_interpreter_suspend (void *data)
174 gdb_disable_readline ();
178 static struct gdb_exception
179 cli_interpreter_exec (void *data, const char *command_str)
181 struct cli_interp *cli = (struct cli_interp *) data;
182 struct ui_file *old_stream;
183 struct gdb_exception result;
185 /* FIXME: cagney/2003-02-01: Need to const char *propogate
186 safe_execute_command. */
187 char *str = (char *) alloca (strlen (command_str) + 1);
188 strcpy (str, command_str);
190 /* gdb_stdout could change between the time cli_uiout was
191 initialized and now. Since we're probably using a different
192 interpreter which has a new ui_file for gdb_stdout, use that one
193 instead of the default.
195 It is important that it gets reset everytime, since the user
196 could set gdb to use a different interpreter. */
197 old_stream = cli_out_set_stream (cli->cli_uiout, gdb_stdout);
198 result = safe_execute_command (cli->cli_uiout, str, 1);
199 cli_out_set_stream (cli->cli_uiout, old_stream);
203 static struct gdb_exception
204 safe_execute_command (struct ui_out *command_uiout, char *command, int from_tty)
206 struct gdb_exception e = exception_none;
207 struct ui_out *saved_uiout;
209 /* Save and override the global ``struct ui_out'' builder. */
210 saved_uiout = current_uiout;
211 current_uiout = command_uiout;
215 execute_command (command, from_tty);
217 CATCH (exception, RETURN_MASK_ALL)
223 /* Restore the global builder. */
224 current_uiout = saved_uiout;
226 /* FIXME: cagney/2005-01-13: This shouldn't be needed. Instead the
227 caller should print the exception. */
228 exception_print (gdb_stderr, e);
232 static struct ui_out *
233 cli_ui_out (struct interp *self)
235 struct cli_interp *cli = (struct cli_interp *) interp_data (self);
237 return cli->cli_uiout;
240 /* The CLI interpreter's vtable. */
242 static const struct interp_procs cli_interp_procs = {
243 cli_interpreter_init, /* init_proc */
244 cli_interpreter_resume, /* resume_proc */
245 cli_interpreter_suspend, /* suspend_proc */
246 cli_interpreter_exec, /* exec_proc */
247 cli_ui_out, /* ui_out_proc */
248 NULL, /* set_logging_proc */
249 cli_command_loop /* command_loop_proc */
252 /* Factory for CLI interpreters. */
254 static struct interp *
255 cli_interp_factory (const char *name)
257 struct cli_interp *cli = XNEW (struct cli_interp);
259 /* Create a default uiout builder for the CLI. */
260 cli->cli_uiout = cli_out_new (gdb_stdout);
262 return interp_new (name, &cli_interp_procs, cli);
265 /* Standard gdb initialization hook. */
266 extern initialize_file_ftype _initialize_cli_interp; /* -Wmissing-prototypes */
269 _initialize_cli_interp (void)
271 interp_factory_register (INTERP_CONSOLE, cli_interp_factory);