Class-ify ui_out
[deliverable/binutils-gdb.git] / gdb / tui / tui-interp.c
CommitLineData
021e7609
AC
1/* TUI Interpreter definitions for GDB, the GNU debugger.
2
618f726f 3 Copyright (C) 2003-2016 Free Software Foundation, Inc.
021e7609
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
021e7609
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/>. */
021e7609
AC
19
20#include "defs.h"
3c216924 21#include "cli/cli-interp.h"
021e7609
AC
22#include "interps.h"
23#include "top.h"
24#include "event-top.h"
25#include "event-loop.h"
26#include "ui-out.h"
95cd630e 27#include "cli-out.h"
d7b2e967 28#include "tui/tui-data.h"
021e7609 29#include "readline/readline.h"
d7b2e967 30#include "tui/tui-win.h"
021e7609 31#include "tui/tui.h"
d7b2e967 32#include "tui/tui-io.h"
fd664c91
PA
33#include "infrun.h"
34#include "observer.h"
eaae60fd 35#include "gdbthread.h"
fd664c91
PA
36
37static struct ui_out *tui_ui_out (struct interp *self);
021e7609 38
1cc6d956
MS
39/* Set to 1 when the TUI mode must be activated when we first start
40 gdb. */
63858210
SC
41static int tui_start_enabled = 0;
42
73ab01a0
PA
43/* Returns the INTERP if the INTERP is a TUI, and returns NULL
44 otherwise. */
45
46static struct interp *
47as_tui_interp (struct interp *interp)
48{
49 if (strcmp (interp_name (interp), INTERP_TUI) == 0)
50 return interp;
51 return NULL;
52}
fd664c91 53
021e7609
AC
54/* Cleanup the tui before exiting. */
55
56static void
57tui_exit (void)
58{
1cc6d956
MS
59 /* Disable the tui. Curses mode is left leaving the screen in a
60 clean state (see endwin()). */
021e7609
AC
61 tui_disable ();
62}
63
fd664c91
PA
64/* Observers for several run control events. If the interpreter is
65 quiet (i.e., another interpreter is being run with
66 interpreter-exec), print nothing. */
67
243a9253
PA
68/* Observer for the normal_stop notification. */
69
70static void
71tui_on_normal_stop (struct bpstats *bs, int print_frame)
72{
eaae60fd
PA
73 if (!print_frame)
74 return;
75
0e454242 76 SWITCH_THRU_ALL_UIS ()
243a9253 77 {
eaae60fd
PA
78 struct interp *interp = top_level_interpreter ();
79 struct interp *tui = as_tui_interp (interp);
80 struct thread_info *thread;
73ab01a0
PA
81
82 if (tui == NULL)
83 continue;
84
eaae60fd
PA
85 thread = inferior_thread ();
86 if (should_print_stop_to_console (interp, thread))
73ab01a0 87 print_stop_event (tui_ui_out (tui));
243a9253
PA
88 }
89}
90
fd664c91
PA
91/* Observer for the signal_received notification. */
92
93static void
94tui_on_signal_received (enum gdb_signal siggnal)
95{
0e454242 96 SWITCH_THRU_ALL_UIS ()
73ab01a0
PA
97 {
98 struct interp *tui = as_tui_interp (top_level_interpreter ());
99
100 if (tui == NULL)
101 continue;
102
103 print_signal_received_reason (tui_ui_out (tui), siggnal);
104 }
fd664c91
PA
105}
106
107/* Observer for the end_stepping_range notification. */
108
109static void
110tui_on_end_stepping_range (void)
111{
0e454242 112 SWITCH_THRU_ALL_UIS ()
73ab01a0
PA
113 {
114 struct interp *tui = as_tui_interp (top_level_interpreter ());
115
116 if (tui == NULL)
117 continue;
118
119 print_end_stepping_range_reason (tui_ui_out (tui));
120 }
fd664c91
PA
121}
122
123/* Observer for the signal_exited notification. */
124
125static void
126tui_on_signal_exited (enum gdb_signal siggnal)
127{
0e454242 128 SWITCH_THRU_ALL_UIS ()
73ab01a0
PA
129 {
130 struct interp *tui = as_tui_interp (top_level_interpreter ());
131
132 if (tui == NULL)
133 continue;
134
135 print_signal_exited_reason (tui_ui_out (tui), siggnal);
136 }
fd664c91
PA
137}
138
139/* Observer for the exited notification. */
140
141static void
142tui_on_exited (int exitstatus)
143{
0e454242 144 SWITCH_THRU_ALL_UIS ()
73ab01a0
PA
145 {
146 struct interp *tui = as_tui_interp (top_level_interpreter ());
147
148 if (tui == NULL)
149 continue;
150
151 print_exited_reason (tui_ui_out (tui), exitstatus);
152 }
fd664c91
PA
153}
154
155/* Observer for the no_history notification. */
156
157static void
158tui_on_no_history (void)
159{
0e454242 160 SWITCH_THRU_ALL_UIS ()
73ab01a0
PA
161 {
162 struct interp *tui = as_tui_interp (top_level_interpreter ());
163
164 if (tui == NULL)
165 continue;
166
167 print_no_history_reason (tui_ui_out (tui));
168 }
fd664c91
PA
169}
170
92bcb5f9
PA
171/* Observer for the sync_execution_done notification. */
172
173static void
174tui_on_sync_execution_done (void)
175{
73ab01a0
PA
176 struct interp *tui = as_tui_interp (top_level_interpreter ());
177
178 if (tui == NULL)
179 return;
180
181 display_gdb_prompt (NULL);
92bcb5f9
PA
182}
183
184/* Observer for the command_error notification. */
185
186static void
187tui_on_command_error (void)
188{
73ab01a0
PA
189 struct interp *tui = as_tui_interp (top_level_interpreter ());
190
191 if (tui == NULL)
192 return;
193
194 display_gdb_prompt (NULL);
92bcb5f9
PA
195}
196
4034d0ff
AT
197/* Observer for the user_selected_context_changed notification. */
198
199static void
200tui_on_user_selected_context_changed (user_selected_what selection)
201{
4034d0ff
AT
202 struct thread_info *tp;
203
204 /* This event is suppressed. */
205 if (cli_suppress_notification.user_selected_context)
206 return;
207
208 tp = find_thread_ptid (inferior_ptid);
209
0e454242 210 SWITCH_THRU_ALL_UIS ()
4034d0ff
AT
211 {
212 struct interp *tui = as_tui_interp (top_level_interpreter ());
213
214 if (tui == NULL)
215 continue;
216
217 if (selection & USER_SELECTED_INFERIOR)
218 print_selected_inferior (tui_ui_out (tui));
219
220 if (tp != NULL
221 && ((selection & (USER_SELECTED_THREAD | USER_SELECTED_FRAME))))
222 print_selected_thread_frame (tui_ui_out (tui), selection);
223
224 }
225}
226
021e7609
AC
227/* These implement the TUI interpreter. */
228
229static void *
4801a9a3 230tui_init (struct interp *self, int top_level)
021e7609
AC
231{
232 /* Install exit handler to leave the screen in a good shape. */
233 atexit (tui_exit);
234
dd1abb8c 235 tui_initialize_static_data ();
021e7609
AC
236
237 tui_initialize_io ();
9612b5ec 238 tui_initialize_win ();
1180b2c8
L
239 if (ui_file_isatty (gdb_stdout))
240 tui_initialize_readline ();
021e7609
AC
241
242 return NULL;
243}
244
245static int
246tui_resume (void *data)
247{
3c216924 248 struct ui *ui = current_ui;
95cd630e
DJ
249 struct ui_file *stream;
250
1cc6d956
MS
251 /* gdb_setup_readline will change gdb_stdout. If the TUI was
252 previously writing to gdb_stdout, then set it to the new
253 gdb_stdout afterwards. */
95cd630e 254
112e8700 255 stream = tui_old_uiout->set_stream (gdb_stdout);
95cd630e
DJ
256 if (stream != gdb_stdout)
257 {
112e8700 258 tui_old_uiout->set_stream (stream);
95cd630e
DJ
259 stream = NULL;
260 }
261
3c216924
PA
262 gdb_setup_readline (1);
263
264 ui->input_handler = command_line_handler;
95cd630e
DJ
265
266 if (stream != NULL)
112e8700 267 tui_old_uiout->set_stream (gdb_stdout);
95cd630e 268
63858210
SC
269 if (tui_start_enabled)
270 tui_enable ();
021e7609
AC
271 return 1;
272}
273
274static int
275tui_suspend (void *data)
276{
63858210 277 tui_start_enabled = tui_active;
021e7609
AC
278 tui_disable ();
279 return 1;
280}
281
4801a9a3
PA
282static struct ui_out *
283tui_ui_out (struct interp *self)
284{
285 if (tui_active)
286 return tui_out;
287 else
288 return tui_old_uiout;
289}
290
71fff37b 291static struct gdb_exception
021e7609
AC
292tui_exec (void *data, const char *command_str)
293{
e2e0b3e5 294 internal_error (__FILE__, __LINE__, _("tui_exec called"));
021e7609
AC
295}
296
8322445e
PA
297/* The TUI interpreter's vtable. */
298
299static const struct interp_procs tui_interp_procs = {
300 tui_init,
301 tui_resume,
302 tui_suspend,
303 tui_exec,
304 tui_ui_out,
305 NULL,
b2d86570 306 cli_interpreter_pre_command_loop,
3c216924 307 cli_interpreter_supports_command_editing,
8322445e
PA
308};
309
310/* Factory for TUI interpreters. */
311
312static struct interp *
313tui_interp_factory (const char *name)
314{
315 return interp_new (name, &tui_interp_procs, NULL);
316}
317
2c0b251b
PA
318/* Provide a prototype to silence -Wmissing-prototypes. */
319extern initialize_file_ftype _initialize_tui_interp;
320
021e7609
AC
321void
322_initialize_tui_interp (void)
323{
8322445e
PA
324 interp_factory_register (INTERP_TUI, tui_interp_factory);
325
cc4349ed 326 if (interpreter_p && strcmp (interpreter_p, INTERP_TUI) == 0)
63858210
SC
327 tui_start_enabled = 1;
328
329 if (interpreter_p && strcmp (interpreter_p, INTERP_CONSOLE) == 0)
330 {
331 xfree (interpreter_p);
cc4349ed 332 interpreter_p = xstrdup (INTERP_TUI);
63858210 333 }
73ab01a0
PA
334
335 /* If changing this, remember to update cli-interp.c as well. */
336 observer_attach_normal_stop (tui_on_normal_stop);
337 observer_attach_signal_received (tui_on_signal_received);
338 observer_attach_end_stepping_range (tui_on_end_stepping_range);
339 observer_attach_signal_exited (tui_on_signal_exited);
340 observer_attach_exited (tui_on_exited);
341 observer_attach_no_history (tui_on_no_history);
342 observer_attach_sync_execution_done (tui_on_sync_execution_done);
343 observer_attach_command_error (tui_on_command_error);
4034d0ff
AT
344 observer_attach_user_selected_context_changed
345 (tui_on_user_selected_context_changed);
021e7609 346}
This page took 1.470543 seconds and 4 git commands to generate.