Make current_ui_out be per UI
[deliverable/binutils-gdb.git] / gdb / cli / cli-interp.c
CommitLineData
4a8f6654
AC
1/* CLI Definitions for GDB, the GNU debugger.
2
618f726f 3 Copyright (C) 2002-2016 Free Software Foundation, Inc.
4a8f6654
AC
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
4a8f6654
AC
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
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
4a8f6654
AC
19
20#include "defs.h"
21#include "interps.h"
4a8f6654
AC
22#include "event-top.h"
23#include "ui-out.h"
24#include "cli-out.h"
25#include "top.h" /* for "execute_command" */
fd664c91
PA
26#include "infrun.h"
27#include "observer.h"
4a8f6654 28
8322445e
PA
29/* The console interpreter. */
30struct cli_interp
31{
32 /* The ui_out for the console interpreter. */
33 struct ui_out *cli_uiout;
34};
35
73ab01a0
PA
36/* Returns the INTERP's data cast as cli_interp if INTERP is a CLI,
37 and returns NULL otherwise. */
38
39static struct cli_interp *
40as_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}
4a8f6654 46
4791eb66 47/* Longjmp-safe wrapper for "execute_command". */
71fff37b 48static struct gdb_exception safe_execute_command (struct ui_out *uiout,
ebcd3b23
MS
49 char *command,
50 int from_tty);
fd664c91
PA
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
243a9253
PA
56/* Observer for the normal_stop notification. */
57
58static void
59cli_on_normal_stop (struct bpstats *bs, int print_frame)
60{
73ab01a0
PA
61 struct switch_thru_all_uis state;
62
63 SWITCH_THRU_ALL_UIS (state)
243a9253 64 {
73ab01a0
PA
65 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
66
67 if (cli == NULL)
68 continue;
69
243a9253 70 if (print_frame)
73ab01a0 71 print_stop_event (cli->cli_uiout);
243a9253
PA
72 }
73}
74
fd664c91
PA
75/* Observer for the signal_received notification. */
76
77static void
78cli_on_signal_received (enum gdb_signal siggnal)
79{
73ab01a0
PA
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 }
fd664c91
PA
91}
92
93/* Observer for the end_stepping_range notification. */
94
95static void
96cli_on_end_stepping_range (void)
97{
73ab01a0
PA
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 }
fd664c91
PA
109}
110
111/* Observer for the signalled notification. */
112
113static void
114cli_on_signal_exited (enum gdb_signal siggnal)
115{
73ab01a0
PA
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 }
fd664c91
PA
127}
128
129/* Observer for the exited notification. */
130
131static void
132cli_on_exited (int exitstatus)
133{
73ab01a0
PA
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 }
fd664c91
PA
145}
146
147/* Observer for the no_history notification. */
148
149static void
150cli_on_no_history (void)
151{
73ab01a0
PA
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 }
fd664c91
PA
163}
164
92bcb5f9
PA
165/* Observer for the sync_execution_done notification. */
166
167static void
168cli_on_sync_execution_done (void)
169{
73ab01a0
PA
170 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
171
172 if (cli == NULL)
173 return;
174
175 display_gdb_prompt (NULL);
92bcb5f9
PA
176}
177
178/* Observer for the command_error notification. */
179
180static void
181cli_on_command_error (void)
182{
73ab01a0
PA
183 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
184
185 if (cli == NULL)
186 return;
187
188 display_gdb_prompt (NULL);
92bcb5f9
PA
189}
190
4a8f6654
AC
191/* These implement the cli out interpreter: */
192
193static void *
4801a9a3 194cli_interpreter_init (struct interp *self, int top_level)
4a8f6654 195{
8322445e 196 return interp_data (self);
4a8f6654
AC
197}
198
199static int
200cli_interpreter_resume (void *data)
201{
8322445e 202 struct cli_interp *cli = (struct cli_interp *) data;
38caaeec
DJ
203 struct ui_file *stream;
204
4a8f6654 205 /*sync_execution = 1; */
38caaeec 206
ebcd3b23
MS
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. */
38caaeec 210
8322445e 211 stream = cli_out_set_stream (cli->cli_uiout, gdb_stdout);
38caaeec
DJ
212 if (stream != gdb_stdout)
213 {
8322445e 214 cli_out_set_stream (cli->cli_uiout, stream);
38caaeec
DJ
215 stream = NULL;
216 }
217
4a8f6654 218 gdb_setup_readline ();
38caaeec
DJ
219
220 if (stream != NULL)
8322445e 221 cli_out_set_stream (cli->cli_uiout, gdb_stdout);
38caaeec 222
4a8f6654
AC
223 return 1;
224}
225
226static int
227cli_interpreter_suspend (void *data)
228{
229 gdb_disable_readline ();
230 return 1;
231}
232
71fff37b 233static struct gdb_exception
4a8f6654
AC
234cli_interpreter_exec (void *data, const char *command_str)
235{
8322445e 236 struct cli_interp *cli = (struct cli_interp *) data;
4a8f6654 237 struct ui_file *old_stream;
71fff37b 238 struct gdb_exception result;
4a8f6654
AC
239
240 /* FIXME: cagney/2003-02-01: Need to const char *propogate
241 safe_execute_command. */
224c3ddb
SM
242 char *str = (char *) alloca (strlen (command_str) + 1);
243 strcpy (str, command_str);
4a8f6654 244
ebcd3b23
MS
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.
4a8f6654 249
ebcd3b23
MS
250 It is important that it gets reset everytime, since the user
251 could set gdb to use a different interpreter. */
8322445e
PA
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);
4a8f6654
AC
255 return result;
256}
257
71fff37b 258static struct gdb_exception
f9679975 259safe_execute_command (struct ui_out *command_uiout, char *command, int from_tty)
4a8f6654 260{
7556d4a4 261 struct gdb_exception e = exception_none;
f9679975
PA
262 struct ui_out *saved_uiout;
263
264 /* Save and override the global ``struct ui_out'' builder. */
79a45e25
PA
265 saved_uiout = current_uiout;
266 current_uiout = command_uiout;
cdb27c12 267
492d29ea 268 TRY
04bd08de
TT
269 {
270 execute_command (command, from_tty);
271 }
492d29ea 272 CATCH (exception, RETURN_MASK_ALL)
7556d4a4
PA
273 {
274 e = exception;
275 }
492d29ea 276 END_CATCH
f9679975
PA
277
278 /* Restore the global builder. */
79a45e25 279 current_uiout = saved_uiout;
f9679975 280
8a076db9
AC
281 /* FIXME: cagney/2005-01-13: This shouldn't be needed. Instead the
282 caller should print the exception. */
9cbc821d 283 exception_print (gdb_stderr, e);
8a076db9 284 return e;
4a8f6654
AC
285}
286
4801a9a3
PA
287static struct ui_out *
288cli_ui_out (struct interp *self)
289{
8322445e
PA
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
297static 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
309static struct interp *
310cli_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);
4801a9a3 318}
4a8f6654 319
4791eb66 320/* Standard gdb initialization hook. */
b9362cc7
AC
321extern initialize_file_ftype _initialize_cli_interp; /* -Wmissing-prototypes */
322
4a8f6654
AC
323void
324_initialize_cli_interp (void)
325{
8322445e 326 interp_factory_register (INTERP_CONSOLE, cli_interp_factory);
73ab01a0
PA
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);
4a8f6654 337}
This page took 0.826622 seconds and 4 git commands to generate.