Make the intepreters output to all UIs
[deliverable/binutils-gdb.git] / gdb / cli / cli-interp.c
1 /* CLI Definitions for GDB, the GNU debugger.
2
3 Copyright (C) 2002-2016 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 3 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, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "interps.h"
22 #include "event-top.h"
23 #include "ui-out.h"
24 #include "cli-out.h"
25 #include "top.h" /* for "execute_command" */
26 #include "infrun.h"
27 #include "observer.h"
28
29 /* The console interpreter. */
30 struct cli_interp
31 {
32 /* The ui_out for the console interpreter. */
33 struct ui_out *cli_uiout;
34 };
35
36 /* Returns the INTERP's data cast as cli_interp if INTERP is a CLI,
37 and returns NULL otherwise. */
38
39 static struct cli_interp *
40 as_cli_interp (struct interp *interp)
41 {
42 if (strcmp (interp_name (interp), INTERP_CONSOLE) == 0)
43 return (struct cli_interp *) interp_data (interp);
44 return NULL;
45 }
46
47 /* Longjmp-safe wrapper for "execute_command". */
48 static struct gdb_exception safe_execute_command (struct ui_out *uiout,
49 char *command,
50 int from_tty);
51
52 /* Observers for several run control events. If the interpreter is
53 quiet (i.e., another interpreter is being run with
54 interpreter-exec), print nothing. */
55
56 /* Observer for the normal_stop notification. */
57
58 static void
59 cli_on_normal_stop (struct bpstats *bs, int print_frame)
60 {
61 struct switch_thru_all_uis state;
62
63 SWITCH_THRU_ALL_UIS (state)
64 {
65 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
66
67 if (cli == NULL)
68 continue;
69
70 if (print_frame)
71 print_stop_event (cli->cli_uiout);
72 }
73 }
74
75 /* Observer for the signal_received notification. */
76
77 static void
78 cli_on_signal_received (enum gdb_signal siggnal)
79 {
80 struct switch_thru_all_uis state;
81
82 SWITCH_THRU_ALL_UIS (state)
83 {
84 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
85
86 if (cli == NULL)
87 continue;
88
89 print_signal_received_reason (cli->cli_uiout, siggnal);
90 }
91 }
92
93 /* Observer for the end_stepping_range notification. */
94
95 static void
96 cli_on_end_stepping_range (void)
97 {
98 struct switch_thru_all_uis state;
99
100 SWITCH_THRU_ALL_UIS (state)
101 {
102 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
103
104 if (cli == NULL)
105 continue;
106
107 print_end_stepping_range_reason (cli->cli_uiout);
108 }
109 }
110
111 /* Observer for the signalled notification. */
112
113 static void
114 cli_on_signal_exited (enum gdb_signal siggnal)
115 {
116 struct switch_thru_all_uis state;
117
118 SWITCH_THRU_ALL_UIS (state)
119 {
120 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
121
122 if (cli == NULL)
123 continue;
124
125 print_signal_exited_reason (cli->cli_uiout, siggnal);
126 }
127 }
128
129 /* Observer for the exited notification. */
130
131 static void
132 cli_on_exited (int exitstatus)
133 {
134 struct switch_thru_all_uis state;
135
136 SWITCH_THRU_ALL_UIS (state)
137 {
138 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
139
140 if (cli == NULL)
141 continue;
142
143 print_exited_reason (cli->cli_uiout, exitstatus);
144 }
145 }
146
147 /* Observer for the no_history notification. */
148
149 static void
150 cli_on_no_history (void)
151 {
152 struct switch_thru_all_uis state;
153
154 SWITCH_THRU_ALL_UIS (state)
155 {
156 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
157
158 if (cli == NULL)
159 continue;
160
161 print_no_history_reason (cli->cli_uiout);
162 }
163 }
164
165 /* Observer for the sync_execution_done notification. */
166
167 static void
168 cli_on_sync_execution_done (void)
169 {
170 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
171
172 if (cli == NULL)
173 return;
174
175 display_gdb_prompt (NULL);
176 }
177
178 /* Observer for the command_error notification. */
179
180 static void
181 cli_on_command_error (void)
182 {
183 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
184
185 if (cli == NULL)
186 return;
187
188 display_gdb_prompt (NULL);
189 }
190
191 /* These implement the cli out interpreter: */
192
193 static void *
194 cli_interpreter_init (struct interp *self, int top_level)
195 {
196 return interp_data (self);
197 }
198
199 static int
200 cli_interpreter_resume (void *data)
201 {
202 struct cli_interp *cli = (struct cli_interp *) data;
203 struct ui_file *stream;
204
205 /*sync_execution = 1; */
206
207 /* gdb_setup_readline will change gdb_stdout. If the CLI was
208 previously writing to gdb_stdout, then set it to the new
209 gdb_stdout afterwards. */
210
211 stream = cli_out_set_stream (cli->cli_uiout, gdb_stdout);
212 if (stream != gdb_stdout)
213 {
214 cli_out_set_stream (cli->cli_uiout, stream);
215 stream = NULL;
216 }
217
218 gdb_setup_readline ();
219
220 if (stream != NULL)
221 cli_out_set_stream (cli->cli_uiout, gdb_stdout);
222
223 return 1;
224 }
225
226 static int
227 cli_interpreter_suspend (void *data)
228 {
229 gdb_disable_readline ();
230 return 1;
231 }
232
233 static struct gdb_exception
234 cli_interpreter_exec (void *data, const char *command_str)
235 {
236 struct cli_interp *cli = (struct cli_interp *) data;
237 struct ui_file *old_stream;
238 struct gdb_exception result;
239
240 /* FIXME: cagney/2003-02-01: Need to const char *propogate
241 safe_execute_command. */
242 char *str = (char *) alloca (strlen (command_str) + 1);
243 strcpy (str, command_str);
244
245 /* gdb_stdout could change between the time cli_uiout was
246 initialized and now. Since we're probably using a different
247 interpreter which has a new ui_file for gdb_stdout, use that one
248 instead of the default.
249
250 It is important that it gets reset everytime, since the user
251 could set gdb to use a different interpreter. */
252 old_stream = cli_out_set_stream (cli->cli_uiout, gdb_stdout);
253 result = safe_execute_command (cli->cli_uiout, str, 1);
254 cli_out_set_stream (cli->cli_uiout, old_stream);
255 return result;
256 }
257
258 static struct gdb_exception
259 safe_execute_command (struct ui_out *command_uiout, char *command, int from_tty)
260 {
261 struct gdb_exception e = exception_none;
262 struct ui_out *saved_uiout;
263
264 /* Save and override the global ``struct ui_out'' builder. */
265 saved_uiout = current_uiout;
266 current_uiout = command_uiout;
267
268 TRY
269 {
270 execute_command (command, from_tty);
271 }
272 CATCH (exception, RETURN_MASK_ALL)
273 {
274 e = exception;
275 }
276 END_CATCH
277
278 /* Restore the global builder. */
279 current_uiout = saved_uiout;
280
281 /* FIXME: cagney/2005-01-13: This shouldn't be needed. Instead the
282 caller should print the exception. */
283 exception_print (gdb_stderr, e);
284 return e;
285 }
286
287 static struct ui_out *
288 cli_ui_out (struct interp *self)
289 {
290 struct cli_interp *cli = (struct cli_interp *) interp_data (self);
291
292 return cli->cli_uiout;
293 }
294
295 /* The CLI interpreter's vtable. */
296
297 static const struct interp_procs cli_interp_procs = {
298 cli_interpreter_init, /* init_proc */
299 cli_interpreter_resume, /* resume_proc */
300 cli_interpreter_suspend, /* suspend_proc */
301 cli_interpreter_exec, /* exec_proc */
302 cli_ui_out, /* ui_out_proc */
303 NULL, /* set_logging_proc */
304 cli_command_loop /* command_loop_proc */
305 };
306
307 /* Factory for CLI interpreters. */
308
309 static struct interp *
310 cli_interp_factory (const char *name)
311 {
312 struct cli_interp *cli = XNEW (struct cli_interp);
313
314 /* Create a default uiout builder for the CLI. */
315 cli->cli_uiout = cli_out_new (gdb_stdout);
316
317 return interp_new (name, &cli_interp_procs, cli);
318 }
319
320 /* Standard gdb initialization hook. */
321 extern initialize_file_ftype _initialize_cli_interp; /* -Wmissing-prototypes */
322
323 void
324 _initialize_cli_interp (void)
325 {
326 interp_factory_register (INTERP_CONSOLE, cli_interp_factory);
327
328 /* If changing this, remember to update tui-interp.c as well. */
329 observer_attach_normal_stop (cli_on_normal_stop);
330 observer_attach_end_stepping_range (cli_on_end_stepping_range);
331 observer_attach_signal_received (cli_on_signal_received);
332 observer_attach_signal_exited (cli_on_signal_exited);
333 observer_attach_exited (cli_on_exited);
334 observer_attach_no_history (cli_on_no_history);
335 observer_attach_sync_execution_done (cli_on_sync_execution_done);
336 observer_attach_command_error (cli_on_command_error);
337 }
This page took 0.039608 seconds and 5 git commands to generate.