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