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