Class-ify ui_out
[deliverable/binutils-gdb.git] / gdb / tui / tui-interp.c
1 /* TUI Interpreter definitions for GDB, the GNU debugger.
2
3 Copyright (C) 2003-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 "cli/cli-interp.h"
22 #include "interps.h"
23 #include "top.h"
24 #include "event-top.h"
25 #include "event-loop.h"
26 #include "ui-out.h"
27 #include "cli-out.h"
28 #include "tui/tui-data.h"
29 #include "readline/readline.h"
30 #include "tui/tui-win.h"
31 #include "tui/tui.h"
32 #include "tui/tui-io.h"
33 #include "infrun.h"
34 #include "observer.h"
35 #include "gdbthread.h"
36
37 static struct ui_out *tui_ui_out (struct interp *self);
38
39 /* Set to 1 when the TUI mode must be activated when we first start
40 gdb. */
41 static int tui_start_enabled = 0;
42
43 /* Returns the INTERP if the INTERP is a TUI, and returns NULL
44 otherwise. */
45
46 static struct interp *
47 as_tui_interp (struct interp *interp)
48 {
49 if (strcmp (interp_name (interp), INTERP_TUI) == 0)
50 return interp;
51 return NULL;
52 }
53
54 /* Cleanup the tui before exiting. */
55
56 static void
57 tui_exit (void)
58 {
59 /* Disable the tui. Curses mode is left leaving the screen in a
60 clean state (see endwin()). */
61 tui_disable ();
62 }
63
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
68 /* Observer for the normal_stop notification. */
69
70 static void
71 tui_on_normal_stop (struct bpstats *bs, int print_frame)
72 {
73 if (!print_frame)
74 return;
75
76 SWITCH_THRU_ALL_UIS ()
77 {
78 struct interp *interp = top_level_interpreter ();
79 struct interp *tui = as_tui_interp (interp);
80 struct thread_info *thread;
81
82 if (tui == NULL)
83 continue;
84
85 thread = inferior_thread ();
86 if (should_print_stop_to_console (interp, thread))
87 print_stop_event (tui_ui_out (tui));
88 }
89 }
90
91 /* Observer for the signal_received notification. */
92
93 static void
94 tui_on_signal_received (enum gdb_signal siggnal)
95 {
96 SWITCH_THRU_ALL_UIS ()
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 }
105 }
106
107 /* Observer for the end_stepping_range notification. */
108
109 static void
110 tui_on_end_stepping_range (void)
111 {
112 SWITCH_THRU_ALL_UIS ()
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 }
121 }
122
123 /* Observer for the signal_exited notification. */
124
125 static void
126 tui_on_signal_exited (enum gdb_signal siggnal)
127 {
128 SWITCH_THRU_ALL_UIS ()
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 }
137 }
138
139 /* Observer for the exited notification. */
140
141 static void
142 tui_on_exited (int exitstatus)
143 {
144 SWITCH_THRU_ALL_UIS ()
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 }
153 }
154
155 /* Observer for the no_history notification. */
156
157 static void
158 tui_on_no_history (void)
159 {
160 SWITCH_THRU_ALL_UIS ()
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 }
169 }
170
171 /* Observer for the sync_execution_done notification. */
172
173 static void
174 tui_on_sync_execution_done (void)
175 {
176 struct interp *tui = as_tui_interp (top_level_interpreter ());
177
178 if (tui == NULL)
179 return;
180
181 display_gdb_prompt (NULL);
182 }
183
184 /* Observer for the command_error notification. */
185
186 static void
187 tui_on_command_error (void)
188 {
189 struct interp *tui = as_tui_interp (top_level_interpreter ());
190
191 if (tui == NULL)
192 return;
193
194 display_gdb_prompt (NULL);
195 }
196
197 /* Observer for the user_selected_context_changed notification. */
198
199 static void
200 tui_on_user_selected_context_changed (user_selected_what selection)
201 {
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
210 SWITCH_THRU_ALL_UIS ()
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
227 /* These implement the TUI interpreter. */
228
229 static void *
230 tui_init (struct interp *self, int top_level)
231 {
232 /* Install exit handler to leave the screen in a good shape. */
233 atexit (tui_exit);
234
235 tui_initialize_static_data ();
236
237 tui_initialize_io ();
238 tui_initialize_win ();
239 if (ui_file_isatty (gdb_stdout))
240 tui_initialize_readline ();
241
242 return NULL;
243 }
244
245 static int
246 tui_resume (void *data)
247 {
248 struct ui *ui = current_ui;
249 struct ui_file *stream;
250
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. */
254
255 stream = tui_old_uiout->set_stream (gdb_stdout);
256 if (stream != gdb_stdout)
257 {
258 tui_old_uiout->set_stream (stream);
259 stream = NULL;
260 }
261
262 gdb_setup_readline (1);
263
264 ui->input_handler = command_line_handler;
265
266 if (stream != NULL)
267 tui_old_uiout->set_stream (gdb_stdout);
268
269 if (tui_start_enabled)
270 tui_enable ();
271 return 1;
272 }
273
274 static int
275 tui_suspend (void *data)
276 {
277 tui_start_enabled = tui_active;
278 tui_disable ();
279 return 1;
280 }
281
282 static struct ui_out *
283 tui_ui_out (struct interp *self)
284 {
285 if (tui_active)
286 return tui_out;
287 else
288 return tui_old_uiout;
289 }
290
291 static struct gdb_exception
292 tui_exec (void *data, const char *command_str)
293 {
294 internal_error (__FILE__, __LINE__, _("tui_exec called"));
295 }
296
297 /* The TUI interpreter's vtable. */
298
299 static 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,
306 cli_interpreter_pre_command_loop,
307 cli_interpreter_supports_command_editing,
308 };
309
310 /* Factory for TUI interpreters. */
311
312 static struct interp *
313 tui_interp_factory (const char *name)
314 {
315 return interp_new (name, &tui_interp_procs, NULL);
316 }
317
318 /* Provide a prototype to silence -Wmissing-prototypes. */
319 extern initialize_file_ftype _initialize_tui_interp;
320
321 void
322 _initialize_tui_interp (void)
323 {
324 interp_factory_register (INTERP_TUI, tui_interp_factory);
325
326 if (interpreter_p && strcmp (interpreter_p, INTERP_TUI) == 0)
327 tui_start_enabled = 1;
328
329 if (interpreter_p && strcmp (interpreter_p, INTERP_CONSOLE) == 0)
330 {
331 xfree (interpreter_p);
332 interpreter_p = xstrdup (INTERP_TUI);
333 }
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);
344 observer_attach_user_selected_context_changed
345 (tui_on_user_selected_context_changed);
346 }
This page took 0.035711 seconds and 4 git commands to generate.