PR gdb/13860 - Make MI sync vs async output (closer to) the same.
[external/binutils.git] / gdb / tui / tui-interp.c
1 /* TUI Interpreter definitions for GDB, the GNU debugger.
2
3    Copyright (C) 2003-2014 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 "top.h"
23 #include "event-top.h"
24 #include "event-loop.h"
25 #include "ui-out.h"
26 #include "cli-out.h"
27 #include "tui/tui-data.h"
28 #include "readline/readline.h"
29 #include "tui/tui-win.h"
30 #include "tui/tui.h"
31 #include "tui/tui-io.h"
32 #include "exceptions.h"
33 #include "infrun.h"
34 #include "observer.h"
35
36 static struct ui_out *tui_ui_out (struct interp *self);
37
38 /* Set to 1 when the TUI mode must be activated when we first start
39    gdb.  */
40 static int tui_start_enabled = 0;
41
42 /* The TUI interpreter.  */
43 static struct interp *tui_interp;
44
45 /* Cleanup the tui before exiting.  */
46
47 static void
48 tui_exit (void)
49 {
50   /* Disable the tui.  Curses mode is left leaving the screen in a
51      clean state (see endwin()).  */
52   tui_disable ();
53 }
54
55 /* True if TUI is the top-level interpreter.  */
56 static int tui_is_toplevel = 0;
57
58 /* Observers for several run control events.  If the interpreter is
59    quiet (i.e., another interpreter is being run with
60    interpreter-exec), print nothing.  */
61
62 /* Observer for the signal_received notification.  */
63
64 static void
65 tui_on_signal_received (enum gdb_signal siggnal)
66 {
67   if (!interp_quiet_p (tui_interp))
68     print_signal_received_reason (tui_ui_out (tui_interp), siggnal);
69 }
70
71 /* Observer for the end_stepping_range notification.  */
72
73 static void
74 tui_on_end_stepping_range (void)
75 {
76   if (!interp_quiet_p (tui_interp))
77     print_end_stepping_range_reason (tui_ui_out (tui_interp));
78 }
79
80 /* Observer for the signal_exited notification.  */
81
82 static void
83 tui_on_signal_exited (enum gdb_signal siggnal)
84 {
85   if (!interp_quiet_p (tui_interp))
86     print_signal_exited_reason (tui_ui_out (tui_interp), siggnal);
87 }
88
89 /* Observer for the exited notification.  */
90
91 static void
92 tui_on_exited (int exitstatus)
93 {
94   if (!interp_quiet_p (tui_interp))
95     print_exited_reason (tui_ui_out (tui_interp), exitstatus);
96 }
97
98 /* Observer for the no_history notification.  */
99
100 static void
101 tui_on_no_history (void)
102 {
103   if (!interp_quiet_p (tui_interp))
104     print_no_history_reason (tui_ui_out (tui_interp));
105 }
106
107 /* These implement the TUI interpreter.  */
108
109 static void *
110 tui_init (struct interp *self, int top_level)
111 {
112   tui_is_toplevel = top_level;
113
114   /* Install exit handler to leave the screen in a good shape.  */
115   atexit (tui_exit);
116
117   tui_initialize_static_data ();
118
119   tui_initialize_io ();
120   tui_initialize_win ();
121   if (ui_file_isatty (gdb_stdout))
122     tui_initialize_readline ();
123
124   /* If changing this, remember to update cli-interp.c as well.  */
125   observer_attach_signal_received (tui_on_signal_received);
126   observer_attach_end_stepping_range (tui_on_end_stepping_range);
127   observer_attach_signal_exited (tui_on_signal_exited);
128   observer_attach_exited (tui_on_exited);
129   observer_attach_no_history (tui_on_no_history);
130
131   return NULL;
132 }
133
134 /* True if enabling the TUI is allowed.  Example, if the top level
135    interpreter is MI, enabling curses will certainly lose.  */
136
137 int
138 tui_allowed_p (void)
139 {
140   /* Only if TUI is the top level interpreter.  Also don't try to
141      setup curses (and print funny control characters) if we're not
142      outputting to a terminal.  */
143   return tui_is_toplevel && ui_file_isatty (gdb_stdout);
144 }
145
146 static int
147 tui_resume (void *data)
148 {
149   struct ui_file *stream;
150
151   /* gdb_setup_readline will change gdb_stdout.  If the TUI was
152      previously writing to gdb_stdout, then set it to the new
153      gdb_stdout afterwards.  */
154
155   stream = cli_out_set_stream (tui_old_uiout, gdb_stdout);
156   if (stream != gdb_stdout)
157     {
158       cli_out_set_stream (tui_old_uiout, stream);
159       stream = NULL;
160     }
161
162   gdb_setup_readline ();
163
164   if (stream != NULL)
165     cli_out_set_stream (tui_old_uiout, gdb_stdout);
166
167   if (tui_start_enabled)
168     tui_enable ();
169   return 1;
170 }
171
172 static int
173 tui_suspend (void *data)
174 {
175   tui_start_enabled = tui_active;
176   tui_disable ();
177   return 1;
178 }
179
180 /* Display the prompt if we are silent.  */
181
182 static int
183 tui_display_prompt_p (void *data)
184 {
185   if (interp_quiet_p (NULL))
186     return 0;
187   else
188     return 1;
189 }
190
191 static struct ui_out *
192 tui_ui_out (struct interp *self)
193 {
194   if (tui_active)
195     return tui_out;
196   else
197     return tui_old_uiout;
198 }
199
200 static struct gdb_exception
201 tui_exec (void *data, const char *command_str)
202 {
203   internal_error (__FILE__, __LINE__, _("tui_exec called"));
204 }
205
206 /* Provide a prototype to silence -Wmissing-prototypes.  */
207 extern initialize_file_ftype _initialize_tui_interp;
208
209 void
210 _initialize_tui_interp (void)
211 {
212   static const struct interp_procs procs = {
213     tui_init,
214     tui_resume,
215     tui_suspend,
216     tui_exec,
217     tui_display_prompt_p,
218     tui_ui_out,
219     NULL,
220     cli_command_loop
221   };
222
223   /* Create a default uiout builder for the TUI.  */
224   tui_interp = interp_new (INTERP_TUI, &procs);
225   interp_add (tui_interp);
226   if (interpreter_p && strcmp (interpreter_p, INTERP_TUI) == 0)
227     tui_start_enabled = 1;
228
229   if (interpreter_p && strcmp (interpreter_p, INTERP_CONSOLE) == 0)
230     {
231       xfree (interpreter_p);
232       interpreter_p = xstrdup (INTERP_TUI);
233     }
234 }