8eae0acc3b100b20972549288361859d7c7a3166
[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 "interps.h"
22 #include "event-top.h"
23 #include "ui-out.h"
24 #include "cli-out.h"
25 #include "top.h"                /* for "execute_command" */
26 #include "infrun.h"
27 #include "observer.h"
28
29 /* The console interpreter.  */
30 struct cli_interp
31 {
32   /* The ui_out for the console interpreter.  */
33   struct ui_out *cli_uiout;
34 };
35
36 /* The interpreter for the console interpreter.  */
37 static struct interp *cli_interp;
38
39 /* Longjmp-safe wrapper for "execute_command".  */
40 static struct gdb_exception safe_execute_command (struct ui_out *uiout,
41                                                   char *command, 
42                                                   int from_tty);
43
44 /* Observers for several run control events.  If the interpreter is
45    quiet (i.e., another interpreter is being run with
46    interpreter-exec), print nothing.  */
47
48 /* Observer for the normal_stop notification.  */
49
50 static void
51 cli_on_normal_stop (struct bpstats *bs, int print_frame)
52 {
53   if (!interp_quiet_p (cli_interp))
54     {
55       if (print_frame)
56         print_stop_event (interp_ui_out (cli_interp));
57     }
58 }
59
60 /* Observer for the signal_received notification.  */
61
62 static void
63 cli_on_signal_received (enum gdb_signal siggnal)
64 {
65   if (!interp_quiet_p (cli_interp))
66     print_signal_received_reason (interp_ui_out (cli_interp), siggnal);
67 }
68
69 /* Observer for the end_stepping_range notification.  */
70
71 static void
72 cli_on_end_stepping_range (void)
73 {
74   if (!interp_quiet_p (cli_interp))
75     print_end_stepping_range_reason (interp_ui_out (cli_interp));
76 }
77
78 /* Observer for the signalled notification.  */
79
80 static void
81 cli_on_signal_exited (enum gdb_signal siggnal)
82 {
83   if (!interp_quiet_p (cli_interp))
84     print_signal_exited_reason (interp_ui_out (cli_interp), siggnal);
85 }
86
87 /* Observer for the exited notification.  */
88
89 static void
90 cli_on_exited (int exitstatus)
91 {
92   if (!interp_quiet_p (cli_interp))
93     print_exited_reason (interp_ui_out (cli_interp), exitstatus);
94 }
95
96 /* Observer for the no_history notification.  */
97
98 static void
99 cli_on_no_history (void)
100 {
101   if (!interp_quiet_p (cli_interp))
102     print_no_history_reason (interp_ui_out (cli_interp));
103 }
104
105 /* Observer for the sync_execution_done notification.  */
106
107 static void
108 cli_on_sync_execution_done (void)
109 {
110   if (!interp_quiet_p (cli_interp))
111     display_gdb_prompt (NULL);
112 }
113
114 /* Observer for the command_error notification.  */
115
116 static void
117 cli_on_command_error (void)
118 {
119   if (!interp_quiet_p (cli_interp))
120     display_gdb_prompt (NULL);
121 }
122
123 /* These implement the cli out interpreter: */
124
125 static void *
126 cli_interpreter_init (struct interp *self, int top_level)
127 {
128   if (top_level)
129     cli_interp = self;
130
131   /* If changing this, remember to update tui-interp.c as well.  */
132   observer_attach_normal_stop (cli_on_normal_stop);
133   observer_attach_end_stepping_range (cli_on_end_stepping_range);
134   observer_attach_signal_received (cli_on_signal_received);
135   observer_attach_signal_exited (cli_on_signal_exited);
136   observer_attach_exited (cli_on_exited);
137   observer_attach_no_history (cli_on_no_history);
138   observer_attach_sync_execution_done (cli_on_sync_execution_done);
139   observer_attach_command_error (cli_on_command_error);
140
141   return interp_data (self);
142 }
143
144 static int
145 cli_interpreter_resume (void *data)
146 {
147   struct cli_interp *cli = (struct cli_interp *) data;
148   struct ui_file *stream;
149
150   /*sync_execution = 1; */
151
152   /* gdb_setup_readline will change gdb_stdout.  If the CLI was
153      previously writing to gdb_stdout, then set it to the new
154      gdb_stdout afterwards.  */
155
156   stream = cli_out_set_stream (cli->cli_uiout, gdb_stdout);
157   if (stream != gdb_stdout)
158     {
159       cli_out_set_stream (cli->cli_uiout, stream);
160       stream = NULL;
161     }
162
163   gdb_setup_readline ();
164
165   if (stream != NULL)
166     cli_out_set_stream (cli->cli_uiout, gdb_stdout);
167
168   return 1;
169 }
170
171 static int
172 cli_interpreter_suspend (void *data)
173 {
174   gdb_disable_readline ();
175   return 1;
176 }
177
178 static struct gdb_exception
179 cli_interpreter_exec (void *data, const char *command_str)
180 {
181   struct cli_interp *cli = (struct cli_interp *) data;
182   struct ui_file *old_stream;
183   struct gdb_exception result;
184
185   /* FIXME: cagney/2003-02-01: Need to const char *propogate
186      safe_execute_command.  */
187   char *str = (char *) alloca (strlen (command_str) + 1);
188   strcpy (str, command_str);
189
190   /* gdb_stdout could change between the time cli_uiout was
191      initialized and now.  Since we're probably using a different
192      interpreter which has a new ui_file for gdb_stdout, use that one
193      instead of the default.
194
195      It is important that it gets reset everytime, since the user
196      could set gdb to use a different interpreter.  */
197   old_stream = cli_out_set_stream (cli->cli_uiout, gdb_stdout);
198   result = safe_execute_command (cli->cli_uiout, str, 1);
199   cli_out_set_stream (cli->cli_uiout, old_stream);
200   return result;
201 }
202
203 static struct gdb_exception
204 safe_execute_command (struct ui_out *command_uiout, char *command, int from_tty)
205 {
206   struct gdb_exception e = exception_none;
207   struct ui_out *saved_uiout;
208
209   /* Save and override the global ``struct ui_out'' builder.  */
210   saved_uiout = current_uiout;
211   current_uiout = command_uiout;
212
213   TRY
214     {
215       execute_command (command, from_tty);
216     }
217   CATCH (exception, RETURN_MASK_ALL)
218     {
219       e = exception;
220     }
221   END_CATCH
222
223   /* Restore the global builder.  */
224   current_uiout = saved_uiout;
225
226   /* FIXME: cagney/2005-01-13: This shouldn't be needed.  Instead the
227      caller should print the exception.  */
228   exception_print (gdb_stderr, e);
229   return e;
230 }
231
232 static struct ui_out *
233 cli_ui_out (struct interp *self)
234 {
235   struct cli_interp *cli = (struct cli_interp *) interp_data (self);
236
237   return cli->cli_uiout;
238 }
239
240 /* The CLI interpreter's vtable.  */
241
242 static const struct interp_procs cli_interp_procs = {
243   cli_interpreter_init,         /* init_proc */
244   cli_interpreter_resume,       /* resume_proc */
245   cli_interpreter_suspend,      /* suspend_proc */
246   cli_interpreter_exec,         /* exec_proc */
247   cli_ui_out,                   /* ui_out_proc */
248   NULL,                         /* set_logging_proc */
249   cli_command_loop              /* command_loop_proc */
250 };
251
252 /* Factory for CLI interpreters.  */
253
254 static struct interp *
255 cli_interp_factory (const char *name)
256 {
257   struct cli_interp *cli = XNEW (struct cli_interp);
258
259   /* Create a default uiout builder for the CLI.  */
260   cli->cli_uiout = cli_out_new (gdb_stdout);
261
262   return interp_new (name, &cli_interp_procs, cli);
263 }
264
265 /* Standard gdb initialization hook.  */
266 extern initialize_file_ftype _initialize_cli_interp; /* -Wmissing-prototypes */
267
268 void
269 _initialize_cli_interp (void)
270 {
271   interp_factory_register (INTERP_CONSOLE, cli_interp_factory);
272 }