Simplify starting the command event loop
[external/binutils.git] / gdb / cli / cli-interp.c
1 /* CLI Definitions for GDB, the GNU debugger.
2
3    Copyright (C) 2002-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-interp.h"
22 #include "interps.h"
23 #include "event-top.h"
24 #include "ui-out.h"
25 #include "cli-out.h"
26 #include "top.h"                /* for "execute_command" */
27 #include "event-top.h"
28 #include "infrun.h"
29 #include "observer.h"
30
31 /* The console interpreter.  */
32 struct cli_interp
33 {
34   /* The ui_out for the console interpreter.  */
35   struct ui_out *cli_uiout;
36 };
37
38 /* Returns the INTERP's data cast as cli_interp if INTERP is a CLI,
39    and returns NULL otherwise.  */
40
41 static struct cli_interp *
42 as_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 }
48
49 /* Longjmp-safe wrapper for "execute_command".  */
50 static struct gdb_exception safe_execute_command (struct ui_out *uiout,
51                                                   char *command, 
52                                                   int from_tty);
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
58 /* Observer for the normal_stop notification.  */
59
60 static void
61 cli_on_normal_stop (struct bpstats *bs, int print_frame)
62 {
63   struct switch_thru_all_uis state;
64
65   SWITCH_THRU_ALL_UIS (state)
66     {
67       struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
68
69       if (cli == NULL)
70         continue;
71
72       if (print_frame)
73         print_stop_event (cli->cli_uiout);
74     }
75 }
76
77 /* Observer for the signal_received notification.  */
78
79 static void
80 cli_on_signal_received (enum gdb_signal siggnal)
81 {
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     }
93 }
94
95 /* Observer for the end_stepping_range notification.  */
96
97 static void
98 cli_on_end_stepping_range (void)
99 {
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     }
111 }
112
113 /* Observer for the signalled notification.  */
114
115 static void
116 cli_on_signal_exited (enum gdb_signal siggnal)
117 {
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     }
129 }
130
131 /* Observer for the exited notification.  */
132
133 static void
134 cli_on_exited (int exitstatus)
135 {
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     }
147 }
148
149 /* Observer for the no_history notification.  */
150
151 static void
152 cli_on_no_history (void)
153 {
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     }
165 }
166
167 /* Observer for the sync_execution_done notification.  */
168
169 static void
170 cli_on_sync_execution_done (void)
171 {
172   struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
173
174   if (cli == NULL)
175     return;
176
177   display_gdb_prompt (NULL);
178 }
179
180 /* Observer for the command_error notification.  */
181
182 static void
183 cli_on_command_error (void)
184 {
185   struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
186
187   if (cli == NULL)
188     return;
189
190   display_gdb_prompt (NULL);
191 }
192
193 /* pre_command_loop implementation.  */
194
195 void
196 cli_interpreter_pre_command_loop (struct interp *self)
197 {
198   display_gdb_prompt (0);
199 }
200
201 /* These implement the cli out interpreter: */
202
203 static void *
204 cli_interpreter_init (struct interp *self, int top_level)
205 {
206   return interp_data (self);
207 }
208
209 static int
210 cli_interpreter_resume (void *data)
211 {
212   struct ui *ui = current_ui;
213   struct cli_interp *cli = (struct cli_interp *) data;
214   struct ui_file *stream;
215
216   /*sync_execution = 1; */
217
218   /* gdb_setup_readline will change gdb_stdout.  If the CLI was
219      previously writing to gdb_stdout, then set it to the new
220      gdb_stdout afterwards.  */
221
222   stream = cli_out_set_stream (cli->cli_uiout, gdb_stdout);
223   if (stream != gdb_stdout)
224     {
225       cli_out_set_stream (cli->cli_uiout, stream);
226       stream = NULL;
227     }
228
229   gdb_setup_readline (1);
230
231   ui->input_handler = command_line_handler;
232
233   if (stream != NULL)
234     cli_out_set_stream (cli->cli_uiout, gdb_stdout);
235
236   return 1;
237 }
238
239 static int
240 cli_interpreter_suspend (void *data)
241 {
242   gdb_disable_readline ();
243   return 1;
244 }
245
246 static struct gdb_exception
247 cli_interpreter_exec (void *data, const char *command_str)
248 {
249   struct cli_interp *cli = (struct cli_interp *) data;
250   struct ui_file *old_stream;
251   struct gdb_exception result;
252
253   /* FIXME: cagney/2003-02-01: Need to const char *propogate
254      safe_execute_command.  */
255   char *str = (char *) alloca (strlen (command_str) + 1);
256   strcpy (str, command_str);
257
258   /* gdb_stdout could change between the time cli_uiout was
259      initialized and now.  Since we're probably using a different
260      interpreter which has a new ui_file for gdb_stdout, use that one
261      instead of the default.
262
263      It is important that it gets reset everytime, since the user
264      could set gdb to use a different interpreter.  */
265   old_stream = cli_out_set_stream (cli->cli_uiout, gdb_stdout);
266   result = safe_execute_command (cli->cli_uiout, str, 1);
267   cli_out_set_stream (cli->cli_uiout, old_stream);
268   return result;
269 }
270
271 int
272 cli_interpreter_supports_command_editing (struct interp *interp)
273 {
274   return 1;
275 }
276
277 static struct gdb_exception
278 safe_execute_command (struct ui_out *command_uiout, char *command, int from_tty)
279 {
280   struct gdb_exception e = exception_none;
281   struct ui_out *saved_uiout;
282
283   /* Save and override the global ``struct ui_out'' builder.  */
284   saved_uiout = current_uiout;
285   current_uiout = command_uiout;
286
287   TRY
288     {
289       execute_command (command, from_tty);
290     }
291   CATCH (exception, RETURN_MASK_ALL)
292     {
293       e = exception;
294     }
295   END_CATCH
296
297   /* Restore the global builder.  */
298   current_uiout = saved_uiout;
299
300   /* FIXME: cagney/2005-01-13: This shouldn't be needed.  Instead the
301      caller should print the exception.  */
302   exception_print (gdb_stderr, e);
303   return e;
304 }
305
306 static struct ui_out *
307 cli_ui_out (struct interp *self)
308 {
309   struct cli_interp *cli = (struct cli_interp *) interp_data (self);
310
311   return cli->cli_uiout;
312 }
313
314 /* The CLI interpreter's vtable.  */
315
316 static const struct interp_procs cli_interp_procs = {
317   cli_interpreter_init,         /* init_proc */
318   cli_interpreter_resume,       /* resume_proc */
319   cli_interpreter_suspend,      /* suspend_proc */
320   cli_interpreter_exec,         /* exec_proc */
321   cli_ui_out,                   /* ui_out_proc */
322   NULL,                         /* set_logging_proc */
323   cli_interpreter_pre_command_loop, /* pre_command_loop_proc */
324   cli_interpreter_supports_command_editing, /* supports_command_editing_proc */
325 };
326
327 /* Factory for CLI interpreters.  */
328
329 static struct interp *
330 cli_interp_factory (const char *name)
331 {
332   struct cli_interp *cli = XNEW (struct cli_interp);
333
334   /* Create a default uiout builder for the CLI.  */
335   cli->cli_uiout = cli_out_new (gdb_stdout);
336
337   return interp_new (name, &cli_interp_procs, cli);
338 }
339
340 /* Standard gdb initialization hook.  */
341 extern initialize_file_ftype _initialize_cli_interp; /* -Wmissing-prototypes */
342
343 void
344 _initialize_cli_interp (void)
345 {
346   interp_factory_register (INTERP_CONSOLE, cli_interp_factory);
347
348   /* If changing this, remember to update tui-interp.c as well.  */
349   observer_attach_normal_stop (cli_on_normal_stop);
350   observer_attach_end_stepping_range (cli_on_end_stepping_range);
351   observer_attach_signal_received (cli_on_signal_received);
352   observer_attach_signal_exited (cli_on_signal_exited);
353   observer_attach_exited (cli_on_exited);
354   observer_attach_no_history (cli_on_no_history);
355   observer_attach_sync_execution_done (cli_on_sync_execution_done);
356   observer_attach_command_error (cli_on_command_error);
357 }