xref: /dflybsd-src/contrib/gdb-7/gdb/interps.c (revision 5796c8dc12c637f18a1740c26afd8d40ffa9b719)
1*5796c8dcSSimon Schubert /* Manages interpreters for GDB, the GNU debugger.
2*5796c8dcSSimon Schubert 
3*5796c8dcSSimon Schubert    Copyright (C) 2000, 2002, 2003, 2007, 2008, 2009
4*5796c8dcSSimon Schubert    Free Software Foundation, Inc.
5*5796c8dcSSimon Schubert 
6*5796c8dcSSimon Schubert    Written by Jim Ingham <jingham@apple.com> of Apple Computer, Inc.
7*5796c8dcSSimon Schubert 
8*5796c8dcSSimon Schubert    This file is part of GDB.
9*5796c8dcSSimon Schubert 
10*5796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
11*5796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
12*5796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
13*5796c8dcSSimon Schubert    (at your option) any later version.
14*5796c8dcSSimon Schubert 
15*5796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
16*5796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
17*5796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*5796c8dcSSimon Schubert    GNU General Public License for more details.
19*5796c8dcSSimon Schubert 
20*5796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
21*5796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
22*5796c8dcSSimon Schubert 
23*5796c8dcSSimon Schubert /* This is just a first cut at separating out the "interpreter"
24*5796c8dcSSimon Schubert    functions of gdb into self-contained modules.  There are a couple
25*5796c8dcSSimon Schubert    of open areas that need to be sorted out:
26*5796c8dcSSimon Schubert 
27*5796c8dcSSimon Schubert    1) The interpreter explicitly contains a UI_OUT, and can insert itself
28*5796c8dcSSimon Schubert    into the event loop, but it doesn't explicitly contain hooks for readline.
29*5796c8dcSSimon Schubert    I did this because it seems to me many interpreters won't want to use
30*5796c8dcSSimon Schubert    the readline command interface, and it is probably simpler to just let
31*5796c8dcSSimon Schubert    them take over the input in their resume proc.  */
32*5796c8dcSSimon Schubert 
33*5796c8dcSSimon Schubert #include "defs.h"
34*5796c8dcSSimon Schubert #include "gdbcmd.h"
35*5796c8dcSSimon Schubert #include "ui-out.h"
36*5796c8dcSSimon Schubert #include "event-loop.h"
37*5796c8dcSSimon Schubert #include "event-top.h"
38*5796c8dcSSimon Schubert #include "interps.h"
39*5796c8dcSSimon Schubert #include "completer.h"
40*5796c8dcSSimon Schubert #include "gdb_string.h"
41*5796c8dcSSimon Schubert #include "gdb_assert.h"
42*5796c8dcSSimon Schubert #include "top.h"		/* For command_loop.  */
43*5796c8dcSSimon Schubert #include "exceptions.h"
44*5796c8dcSSimon Schubert 
45*5796c8dcSSimon Schubert struct interp
46*5796c8dcSSimon Schubert {
47*5796c8dcSSimon Schubert   /* This is the name in "-i=" and set interpreter. */
48*5796c8dcSSimon Schubert   const char *name;
49*5796c8dcSSimon Schubert 
50*5796c8dcSSimon Schubert   /* Interpreters are stored in a linked list, this is the next
51*5796c8dcSSimon Schubert      one...  */
52*5796c8dcSSimon Schubert   struct interp *next;
53*5796c8dcSSimon Schubert 
54*5796c8dcSSimon Schubert   /* This is a cookie that an instance of the interpreter can use.
55*5796c8dcSSimon Schubert      This is a bit confused right now as the exact initialization
56*5796c8dcSSimon Schubert      sequence for it, and how it relates to the interpreter's uiout
57*5796c8dcSSimon Schubert      object is a bit confused.  */
58*5796c8dcSSimon Schubert   void *data;
59*5796c8dcSSimon Schubert 
60*5796c8dcSSimon Schubert   /* Has the init_proc been run? */
61*5796c8dcSSimon Schubert   int inited;
62*5796c8dcSSimon Schubert 
63*5796c8dcSSimon Schubert   /* This is the ui_out used to collect results for this interpreter.
64*5796c8dcSSimon Schubert      It can be a formatter for stdout, as is the case for the console
65*5796c8dcSSimon Schubert      & mi outputs, or it might be a result formatter.  */
66*5796c8dcSSimon Schubert   struct ui_out *interpreter_out;
67*5796c8dcSSimon Schubert 
68*5796c8dcSSimon Schubert   const struct interp_procs *procs;
69*5796c8dcSSimon Schubert   int quiet_p;
70*5796c8dcSSimon Schubert };
71*5796c8dcSSimon Schubert 
72*5796c8dcSSimon Schubert /* Functions local to this file. */
73*5796c8dcSSimon Schubert static void initialize_interps (void);
74*5796c8dcSSimon Schubert static char **interpreter_completer (struct cmd_list_element *cmd,
75*5796c8dcSSimon Schubert 				     char *text, char *word);
76*5796c8dcSSimon Schubert 
77*5796c8dcSSimon Schubert /* The magic initialization routine for this module. */
78*5796c8dcSSimon Schubert 
79*5796c8dcSSimon Schubert void _initialize_interpreter (void);
80*5796c8dcSSimon Schubert 
81*5796c8dcSSimon Schubert /* Variables local to this file: */
82*5796c8dcSSimon Schubert 
83*5796c8dcSSimon Schubert static struct interp *interp_list = NULL;
84*5796c8dcSSimon Schubert static struct interp *current_interpreter = NULL;
85*5796c8dcSSimon Schubert static struct interp *top_level_interpreter_ptr = NULL;
86*5796c8dcSSimon Schubert 
87*5796c8dcSSimon Schubert static int interpreter_initialized = 0;
88*5796c8dcSSimon Schubert 
89*5796c8dcSSimon Schubert /* interp_new - This allocates space for a new interpreter,
90*5796c8dcSSimon Schubert    fills the fields from the inputs, and returns a pointer to the
91*5796c8dcSSimon Schubert    interpreter. */
92*5796c8dcSSimon Schubert struct interp *
93*5796c8dcSSimon Schubert interp_new (const char *name, void *data, struct ui_out *uiout,
94*5796c8dcSSimon Schubert 	    const struct interp_procs *procs)
95*5796c8dcSSimon Schubert {
96*5796c8dcSSimon Schubert   struct interp *new_interp;
97*5796c8dcSSimon Schubert 
98*5796c8dcSSimon Schubert   new_interp = XMALLOC (struct interp);
99*5796c8dcSSimon Schubert 
100*5796c8dcSSimon Schubert   new_interp->name = xstrdup (name);
101*5796c8dcSSimon Schubert   new_interp->data = data;
102*5796c8dcSSimon Schubert   new_interp->interpreter_out = uiout;
103*5796c8dcSSimon Schubert   new_interp->quiet_p = 0;
104*5796c8dcSSimon Schubert   new_interp->procs = procs;
105*5796c8dcSSimon Schubert   new_interp->inited = 0;
106*5796c8dcSSimon Schubert 
107*5796c8dcSSimon Schubert   return new_interp;
108*5796c8dcSSimon Schubert }
109*5796c8dcSSimon Schubert 
110*5796c8dcSSimon Schubert /* Add interpreter INTERP to the gdb interpreter list.  The
111*5796c8dcSSimon Schubert    interpreter must not have previously been added.  */
112*5796c8dcSSimon Schubert void
113*5796c8dcSSimon Schubert interp_add (struct interp *interp)
114*5796c8dcSSimon Schubert {
115*5796c8dcSSimon Schubert   if (!interpreter_initialized)
116*5796c8dcSSimon Schubert     initialize_interps ();
117*5796c8dcSSimon Schubert 
118*5796c8dcSSimon Schubert   gdb_assert (interp_lookup (interp->name) == NULL);
119*5796c8dcSSimon Schubert 
120*5796c8dcSSimon Schubert   interp->next = interp_list;
121*5796c8dcSSimon Schubert   interp_list = interp;
122*5796c8dcSSimon Schubert }
123*5796c8dcSSimon Schubert 
124*5796c8dcSSimon Schubert /* This sets the current interpreter to be INTERP.  If INTERP has not
125*5796c8dcSSimon Schubert    been initialized, then this will also run the init proc.  If the
126*5796c8dcSSimon Schubert    init proc is successful, return 1, if it fails, set the old
127*5796c8dcSSimon Schubert    interpreter back in place and return 0.  If we can't restore the
128*5796c8dcSSimon Schubert    old interpreter, then raise an internal error, since we are in
129*5796c8dcSSimon Schubert    pretty bad shape at this point.
130*5796c8dcSSimon Schubert 
131*5796c8dcSSimon Schubert    The TOP_LEVEL parameter tells if this new interpreter is
132*5796c8dcSSimon Schubert    the top-level one.  The top-level is what is requested
133*5796c8dcSSimon Schubert    on the command line, and is responsible for reporting general
134*5796c8dcSSimon Schubert    notification about target state changes.  For example, if
135*5796c8dcSSimon Schubert    MI is the top-level interpreter, then it will always report
136*5796c8dcSSimon Schubert    events such as target stops and new thread creation, even if they
137*5796c8dcSSimon Schubert    are caused by CLI commands.  */
138*5796c8dcSSimon Schubert int
139*5796c8dcSSimon Schubert interp_set (struct interp *interp, int top_level)
140*5796c8dcSSimon Schubert {
141*5796c8dcSSimon Schubert   struct interp *old_interp = current_interpreter;
142*5796c8dcSSimon Schubert   int first_time = 0;
143*5796c8dcSSimon Schubert   char buffer[64];
144*5796c8dcSSimon Schubert 
145*5796c8dcSSimon Schubert   /* If we already have an interpreter, then trying to
146*5796c8dcSSimon Schubert      set top level interpreter is kinda pointless.  */
147*5796c8dcSSimon Schubert   gdb_assert (!top_level || !current_interpreter);
148*5796c8dcSSimon Schubert   gdb_assert (!top_level || !top_level_interpreter_ptr);
149*5796c8dcSSimon Schubert 
150*5796c8dcSSimon Schubert   if (current_interpreter != NULL)
151*5796c8dcSSimon Schubert     {
152*5796c8dcSSimon Schubert       do_all_continuations ();
153*5796c8dcSSimon Schubert       ui_out_flush (uiout);
154*5796c8dcSSimon Schubert       if (current_interpreter->procs->suspend_proc
155*5796c8dcSSimon Schubert 	  && !current_interpreter->procs->suspend_proc (current_interpreter->
156*5796c8dcSSimon Schubert 							data))
157*5796c8dcSSimon Schubert 	{
158*5796c8dcSSimon Schubert 	  error (_("Could not suspend interpreter \"%s\"."),
159*5796c8dcSSimon Schubert 		 current_interpreter->name);
160*5796c8dcSSimon Schubert 	}
161*5796c8dcSSimon Schubert     }
162*5796c8dcSSimon Schubert   else
163*5796c8dcSSimon Schubert     {
164*5796c8dcSSimon Schubert       first_time = 1;
165*5796c8dcSSimon Schubert     }
166*5796c8dcSSimon Schubert 
167*5796c8dcSSimon Schubert   current_interpreter = interp;
168*5796c8dcSSimon Schubert   if (top_level)
169*5796c8dcSSimon Schubert     top_level_interpreter_ptr = interp;
170*5796c8dcSSimon Schubert 
171*5796c8dcSSimon Schubert   /* We use interpreter_p for the "set interpreter" variable, so we need
172*5796c8dcSSimon Schubert      to make sure we have a malloc'ed copy for the set command to free. */
173*5796c8dcSSimon Schubert   if (interpreter_p != NULL
174*5796c8dcSSimon Schubert       && strcmp (current_interpreter->name, interpreter_p) != 0)
175*5796c8dcSSimon Schubert     {
176*5796c8dcSSimon Schubert       xfree (interpreter_p);
177*5796c8dcSSimon Schubert 
178*5796c8dcSSimon Schubert       interpreter_p = xstrdup (current_interpreter->name);
179*5796c8dcSSimon Schubert     }
180*5796c8dcSSimon Schubert 
181*5796c8dcSSimon Schubert   uiout = interp->interpreter_out;
182*5796c8dcSSimon Schubert 
183*5796c8dcSSimon Schubert   /* Run the init proc.  If it fails, try to restore the old interp. */
184*5796c8dcSSimon Schubert 
185*5796c8dcSSimon Schubert   if (!interp->inited)
186*5796c8dcSSimon Schubert     {
187*5796c8dcSSimon Schubert       if (interp->procs->init_proc != NULL)
188*5796c8dcSSimon Schubert 	{
189*5796c8dcSSimon Schubert 	  interp->data = interp->procs->init_proc (top_level);
190*5796c8dcSSimon Schubert 	}
191*5796c8dcSSimon Schubert       interp->inited = 1;
192*5796c8dcSSimon Schubert     }
193*5796c8dcSSimon Schubert 
194*5796c8dcSSimon Schubert   /* Clear out any installed interpreter hooks/event handlers.  */
195*5796c8dcSSimon Schubert   clear_interpreter_hooks ();
196*5796c8dcSSimon Schubert 
197*5796c8dcSSimon Schubert   if (interp->procs->resume_proc != NULL
198*5796c8dcSSimon Schubert       && (!interp->procs->resume_proc (interp->data)))
199*5796c8dcSSimon Schubert     {
200*5796c8dcSSimon Schubert       if (old_interp == NULL || !interp_set (old_interp, 0))
201*5796c8dcSSimon Schubert 	internal_error (__FILE__, __LINE__,
202*5796c8dcSSimon Schubert 			_("Failed to initialize new interp \"%s\" %s"),
203*5796c8dcSSimon Schubert 			interp->name, "and could not restore old interp!\n");
204*5796c8dcSSimon Schubert       return 0;
205*5796c8dcSSimon Schubert     }
206*5796c8dcSSimon Schubert 
207*5796c8dcSSimon Schubert   /* Finally, put up the new prompt to show that we are indeed here.
208*5796c8dcSSimon Schubert      Also, display_gdb_prompt for the console does some readline magic
209*5796c8dcSSimon Schubert      which is needed for the console interpreter, at least... */
210*5796c8dcSSimon Schubert 
211*5796c8dcSSimon Schubert   if (!first_time)
212*5796c8dcSSimon Schubert     {
213*5796c8dcSSimon Schubert       if (!interp_quiet_p (interp))
214*5796c8dcSSimon Schubert 	{
215*5796c8dcSSimon Schubert 	  sprintf (buffer, "Switching to interpreter \"%.24s\".\n",
216*5796c8dcSSimon Schubert 		   interp->name);
217*5796c8dcSSimon Schubert 	  ui_out_text (uiout, buffer);
218*5796c8dcSSimon Schubert 	}
219*5796c8dcSSimon Schubert       display_gdb_prompt (NULL);
220*5796c8dcSSimon Schubert     }
221*5796c8dcSSimon Schubert 
222*5796c8dcSSimon Schubert   return 1;
223*5796c8dcSSimon Schubert }
224*5796c8dcSSimon Schubert 
225*5796c8dcSSimon Schubert /* interp_lookup - Looks up the interpreter for NAME.  If no such
226*5796c8dcSSimon Schubert    interpreter exists, return NULL, otherwise return a pointer to the
227*5796c8dcSSimon Schubert    interpreter.  */
228*5796c8dcSSimon Schubert struct interp *
229*5796c8dcSSimon Schubert interp_lookup (const char *name)
230*5796c8dcSSimon Schubert {
231*5796c8dcSSimon Schubert   struct interp *interp;
232*5796c8dcSSimon Schubert 
233*5796c8dcSSimon Schubert   if (name == NULL || strlen (name) == 0)
234*5796c8dcSSimon Schubert     return NULL;
235*5796c8dcSSimon Schubert 
236*5796c8dcSSimon Schubert   for (interp = interp_list; interp != NULL; interp = interp->next)
237*5796c8dcSSimon Schubert     {
238*5796c8dcSSimon Schubert       if (strcmp (interp->name, name) == 0)
239*5796c8dcSSimon Schubert 	return interp;
240*5796c8dcSSimon Schubert     }
241*5796c8dcSSimon Schubert 
242*5796c8dcSSimon Schubert   return NULL;
243*5796c8dcSSimon Schubert }
244*5796c8dcSSimon Schubert 
245*5796c8dcSSimon Schubert /* Returns the current interpreter. */
246*5796c8dcSSimon Schubert 
247*5796c8dcSSimon Schubert struct ui_out *
248*5796c8dcSSimon Schubert interp_ui_out (struct interp *interp)
249*5796c8dcSSimon Schubert {
250*5796c8dcSSimon Schubert   if (interp != NULL)
251*5796c8dcSSimon Schubert     return interp->interpreter_out;
252*5796c8dcSSimon Schubert 
253*5796c8dcSSimon Schubert   return current_interpreter->interpreter_out;
254*5796c8dcSSimon Schubert }
255*5796c8dcSSimon Schubert 
256*5796c8dcSSimon Schubert /* Returns true if the current interp is the passed in name. */
257*5796c8dcSSimon Schubert int
258*5796c8dcSSimon Schubert current_interp_named_p (const char *interp_name)
259*5796c8dcSSimon Schubert {
260*5796c8dcSSimon Schubert   if (current_interpreter)
261*5796c8dcSSimon Schubert     return (strcmp (current_interpreter->name, interp_name) == 0);
262*5796c8dcSSimon Schubert 
263*5796c8dcSSimon Schubert   return 0;
264*5796c8dcSSimon Schubert }
265*5796c8dcSSimon Schubert 
266*5796c8dcSSimon Schubert /* This is called in display_gdb_prompt.  If the proc returns a zero
267*5796c8dcSSimon Schubert    value, display_gdb_prompt will return without displaying the
268*5796c8dcSSimon Schubert    prompt.  */
269*5796c8dcSSimon Schubert int
270*5796c8dcSSimon Schubert current_interp_display_prompt_p (void)
271*5796c8dcSSimon Schubert {
272*5796c8dcSSimon Schubert   if (current_interpreter == NULL
273*5796c8dcSSimon Schubert       || current_interpreter->procs->prompt_proc_p == NULL)
274*5796c8dcSSimon Schubert     return 0;
275*5796c8dcSSimon Schubert   else
276*5796c8dcSSimon Schubert     return current_interpreter->procs->prompt_proc_p (current_interpreter->
277*5796c8dcSSimon Schubert 						      data);
278*5796c8dcSSimon Schubert }
279*5796c8dcSSimon Schubert 
280*5796c8dcSSimon Schubert /* Run the current command interpreter's main loop.  */
281*5796c8dcSSimon Schubert void
282*5796c8dcSSimon Schubert current_interp_command_loop (void)
283*5796c8dcSSimon Schubert {
284*5796c8dcSSimon Schubert   /* Somewhat messy.  For the moment prop up all the old ways of
285*5796c8dcSSimon Schubert      selecting the command loop.  `deprecated_command_loop_hook'
286*5796c8dcSSimon Schubert      should be deprecated.  */
287*5796c8dcSSimon Schubert   if (deprecated_command_loop_hook != NULL)
288*5796c8dcSSimon Schubert     deprecated_command_loop_hook ();
289*5796c8dcSSimon Schubert   else if (current_interpreter != NULL
290*5796c8dcSSimon Schubert 	   && current_interpreter->procs->command_loop_proc != NULL)
291*5796c8dcSSimon Schubert     current_interpreter->procs->command_loop_proc (current_interpreter->data);
292*5796c8dcSSimon Schubert   else
293*5796c8dcSSimon Schubert     cli_command_loop ();
294*5796c8dcSSimon Schubert }
295*5796c8dcSSimon Schubert 
296*5796c8dcSSimon Schubert int
297*5796c8dcSSimon Schubert interp_quiet_p (struct interp *interp)
298*5796c8dcSSimon Schubert {
299*5796c8dcSSimon Schubert   if (interp != NULL)
300*5796c8dcSSimon Schubert     return interp->quiet_p;
301*5796c8dcSSimon Schubert   else
302*5796c8dcSSimon Schubert     return current_interpreter->quiet_p;
303*5796c8dcSSimon Schubert }
304*5796c8dcSSimon Schubert 
305*5796c8dcSSimon Schubert static int
306*5796c8dcSSimon Schubert interp_set_quiet (struct interp *interp, int quiet)
307*5796c8dcSSimon Schubert {
308*5796c8dcSSimon Schubert   int old_val = interp->quiet_p;
309*5796c8dcSSimon Schubert   interp->quiet_p = quiet;
310*5796c8dcSSimon Schubert   return old_val;
311*5796c8dcSSimon Schubert }
312*5796c8dcSSimon Schubert 
313*5796c8dcSSimon Schubert /* interp_exec - This executes COMMAND_STR in the current
314*5796c8dcSSimon Schubert    interpreter. */
315*5796c8dcSSimon Schubert int
316*5796c8dcSSimon Schubert interp_exec_p (struct interp *interp)
317*5796c8dcSSimon Schubert {
318*5796c8dcSSimon Schubert   return interp->procs->exec_proc != NULL;
319*5796c8dcSSimon Schubert }
320*5796c8dcSSimon Schubert 
321*5796c8dcSSimon Schubert struct gdb_exception
322*5796c8dcSSimon Schubert interp_exec (struct interp *interp, const char *command_str)
323*5796c8dcSSimon Schubert {
324*5796c8dcSSimon Schubert   if (interp->procs->exec_proc != NULL)
325*5796c8dcSSimon Schubert     {
326*5796c8dcSSimon Schubert       return interp->procs->exec_proc (interp->data, command_str);
327*5796c8dcSSimon Schubert     }
328*5796c8dcSSimon Schubert   return exception_none;
329*5796c8dcSSimon Schubert }
330*5796c8dcSSimon Schubert 
331*5796c8dcSSimon Schubert /* A convenience routine that nulls out all the common command hooks.
332*5796c8dcSSimon Schubert    Use it when removing your interpreter in its suspend proc.  */
333*5796c8dcSSimon Schubert void
334*5796c8dcSSimon Schubert clear_interpreter_hooks (void)
335*5796c8dcSSimon Schubert {
336*5796c8dcSSimon Schubert   deprecated_init_ui_hook = 0;
337*5796c8dcSSimon Schubert   deprecated_print_frame_info_listing_hook = 0;
338*5796c8dcSSimon Schubert   /*print_frame_more_info_hook = 0; */
339*5796c8dcSSimon Schubert   deprecated_query_hook = 0;
340*5796c8dcSSimon Schubert   deprecated_warning_hook = 0;
341*5796c8dcSSimon Schubert   deprecated_interactive_hook = 0;
342*5796c8dcSSimon Schubert   deprecated_readline_begin_hook = 0;
343*5796c8dcSSimon Schubert   deprecated_readline_hook = 0;
344*5796c8dcSSimon Schubert   deprecated_readline_end_hook = 0;
345*5796c8dcSSimon Schubert   deprecated_register_changed_hook = 0;
346*5796c8dcSSimon Schubert   deprecated_memory_changed_hook = 0;
347*5796c8dcSSimon Schubert   deprecated_context_hook = 0;
348*5796c8dcSSimon Schubert   deprecated_target_wait_hook = 0;
349*5796c8dcSSimon Schubert   deprecated_call_command_hook = 0;
350*5796c8dcSSimon Schubert   deprecated_error_begin_hook = 0;
351*5796c8dcSSimon Schubert   deprecated_command_loop_hook = 0;
352*5796c8dcSSimon Schubert }
353*5796c8dcSSimon Schubert 
354*5796c8dcSSimon Schubert /* This is a lazy init routine, called the first time the interpreter
355*5796c8dcSSimon Schubert    module is used.  I put it here just in case, but I haven't thought
356*5796c8dcSSimon Schubert    of a use for it yet.  I will probably bag it soon, since I don't
357*5796c8dcSSimon Schubert    think it will be necessary.  */
358*5796c8dcSSimon Schubert static void
359*5796c8dcSSimon Schubert initialize_interps (void)
360*5796c8dcSSimon Schubert {
361*5796c8dcSSimon Schubert   interpreter_initialized = 1;
362*5796c8dcSSimon Schubert   /* Don't know if anything needs to be done here... */
363*5796c8dcSSimon Schubert }
364*5796c8dcSSimon Schubert 
365*5796c8dcSSimon Schubert static void
366*5796c8dcSSimon Schubert interpreter_exec_cmd (char *args, int from_tty)
367*5796c8dcSSimon Schubert {
368*5796c8dcSSimon Schubert   struct interp *old_interp, *interp_to_use;
369*5796c8dcSSimon Schubert   char **prules = NULL;
370*5796c8dcSSimon Schubert   char **trule = NULL;
371*5796c8dcSSimon Schubert   unsigned int nrules;
372*5796c8dcSSimon Schubert   unsigned int i;
373*5796c8dcSSimon Schubert   int old_quiet, use_quiet;
374*5796c8dcSSimon Schubert 
375*5796c8dcSSimon Schubert   if (args == NULL)
376*5796c8dcSSimon Schubert     error_no_arg (_("interpreter-exec command"));
377*5796c8dcSSimon Schubert 
378*5796c8dcSSimon Schubert   prules = gdb_buildargv (args);
379*5796c8dcSSimon Schubert   make_cleanup_freeargv (prules);
380*5796c8dcSSimon Schubert 
381*5796c8dcSSimon Schubert   nrules = 0;
382*5796c8dcSSimon Schubert   for (trule = prules; *trule != NULL; trule++)
383*5796c8dcSSimon Schubert     nrules++;
384*5796c8dcSSimon Schubert 
385*5796c8dcSSimon Schubert   if (nrules < 2)
386*5796c8dcSSimon Schubert     error (_("usage: interpreter-exec <interpreter> [ <command> ... ]"));
387*5796c8dcSSimon Schubert 
388*5796c8dcSSimon Schubert   old_interp = current_interpreter;
389*5796c8dcSSimon Schubert 
390*5796c8dcSSimon Schubert   interp_to_use = interp_lookup (prules[0]);
391*5796c8dcSSimon Schubert   if (interp_to_use == NULL)
392*5796c8dcSSimon Schubert     error (_("Could not find interpreter \"%s\"."), prules[0]);
393*5796c8dcSSimon Schubert 
394*5796c8dcSSimon Schubert   /* Temporarily set interpreters quiet */
395*5796c8dcSSimon Schubert   old_quiet = interp_set_quiet (old_interp, 1);
396*5796c8dcSSimon Schubert   use_quiet = interp_set_quiet (interp_to_use, 1);
397*5796c8dcSSimon Schubert 
398*5796c8dcSSimon Schubert   if (!interp_set (interp_to_use, 0))
399*5796c8dcSSimon Schubert     error (_("Could not switch to interpreter \"%s\"."), prules[0]);
400*5796c8dcSSimon Schubert 
401*5796c8dcSSimon Schubert   for (i = 1; i < nrules; i++)
402*5796c8dcSSimon Schubert     {
403*5796c8dcSSimon Schubert       struct gdb_exception e = interp_exec (interp_to_use, prules[i]);
404*5796c8dcSSimon Schubert       if (e.reason < 0)
405*5796c8dcSSimon Schubert 	{
406*5796c8dcSSimon Schubert 	  interp_set (old_interp, 0);
407*5796c8dcSSimon Schubert 	  interp_set_quiet (interp_to_use, use_quiet);
408*5796c8dcSSimon Schubert 	  interp_set_quiet (old_interp, old_quiet);
409*5796c8dcSSimon Schubert 	  error (_("error in command: \"%s\"."), prules[i]);
410*5796c8dcSSimon Schubert 	}
411*5796c8dcSSimon Schubert     }
412*5796c8dcSSimon Schubert 
413*5796c8dcSSimon Schubert   interp_set (old_interp, 0);
414*5796c8dcSSimon Schubert   interp_set_quiet (interp_to_use, use_quiet);
415*5796c8dcSSimon Schubert   interp_set_quiet (old_interp, old_quiet);
416*5796c8dcSSimon Schubert }
417*5796c8dcSSimon Schubert 
418*5796c8dcSSimon Schubert /* List the possible interpreters which could complete the given text. */
419*5796c8dcSSimon Schubert static char **
420*5796c8dcSSimon Schubert interpreter_completer (struct cmd_list_element *ignore, char *text, char *word)
421*5796c8dcSSimon Schubert {
422*5796c8dcSSimon Schubert   int alloced = 0;
423*5796c8dcSSimon Schubert   int textlen;
424*5796c8dcSSimon Schubert   int num_matches;
425*5796c8dcSSimon Schubert   char **matches;
426*5796c8dcSSimon Schubert   struct interp *interp;
427*5796c8dcSSimon Schubert 
428*5796c8dcSSimon Schubert   /* We expect only a very limited number of interpreters, so just
429*5796c8dcSSimon Schubert      allocate room for all of them plus one for the last that must be NULL
430*5796c8dcSSimon Schubert      to correctly end the list. */
431*5796c8dcSSimon Schubert   for (interp = interp_list; interp != NULL; interp = interp->next)
432*5796c8dcSSimon Schubert     ++alloced;
433*5796c8dcSSimon Schubert   matches = (char **) xcalloc (alloced + 1, sizeof (char *));
434*5796c8dcSSimon Schubert 
435*5796c8dcSSimon Schubert   num_matches = 0;
436*5796c8dcSSimon Schubert   textlen = strlen (text);
437*5796c8dcSSimon Schubert   for (interp = interp_list; interp != NULL; interp = interp->next)
438*5796c8dcSSimon Schubert     {
439*5796c8dcSSimon Schubert       if (strncmp (interp->name, text, textlen) == 0)
440*5796c8dcSSimon Schubert 	{
441*5796c8dcSSimon Schubert 	  matches[num_matches] =
442*5796c8dcSSimon Schubert 	    (char *) xmalloc (strlen (word) + strlen (interp->name) + 1);
443*5796c8dcSSimon Schubert 	  if (word == text)
444*5796c8dcSSimon Schubert 	    strcpy (matches[num_matches], interp->name);
445*5796c8dcSSimon Schubert 	  else if (word > text)
446*5796c8dcSSimon Schubert 	    {
447*5796c8dcSSimon Schubert 	      /* Return some portion of interp->name */
448*5796c8dcSSimon Schubert 	      strcpy (matches[num_matches], interp->name + (word - text));
449*5796c8dcSSimon Schubert 	    }
450*5796c8dcSSimon Schubert 	  else
451*5796c8dcSSimon Schubert 	    {
452*5796c8dcSSimon Schubert 	      /* Return some of text plus interp->name */
453*5796c8dcSSimon Schubert 	      strncpy (matches[num_matches], word, text - word);
454*5796c8dcSSimon Schubert 	      matches[num_matches][text - word] = '\0';
455*5796c8dcSSimon Schubert 	      strcat (matches[num_matches], interp->name);
456*5796c8dcSSimon Schubert 	    }
457*5796c8dcSSimon Schubert 	  ++num_matches;
458*5796c8dcSSimon Schubert 	}
459*5796c8dcSSimon Schubert     }
460*5796c8dcSSimon Schubert 
461*5796c8dcSSimon Schubert   if (num_matches == 0)
462*5796c8dcSSimon Schubert     {
463*5796c8dcSSimon Schubert       xfree (matches);
464*5796c8dcSSimon Schubert       matches = NULL;
465*5796c8dcSSimon Schubert     }
466*5796c8dcSSimon Schubert 
467*5796c8dcSSimon Schubert   return matches;
468*5796c8dcSSimon Schubert }
469*5796c8dcSSimon Schubert 
470*5796c8dcSSimon Schubert struct interp *
471*5796c8dcSSimon Schubert top_level_interpreter (void)
472*5796c8dcSSimon Schubert {
473*5796c8dcSSimon Schubert   return top_level_interpreter_ptr;
474*5796c8dcSSimon Schubert }
475*5796c8dcSSimon Schubert 
476*5796c8dcSSimon Schubert void *
477*5796c8dcSSimon Schubert top_level_interpreter_data (void)
478*5796c8dcSSimon Schubert {
479*5796c8dcSSimon Schubert   gdb_assert (top_level_interpreter_ptr);
480*5796c8dcSSimon Schubert   return top_level_interpreter_ptr->data;
481*5796c8dcSSimon Schubert }
482*5796c8dcSSimon Schubert 
483*5796c8dcSSimon Schubert /* This just adds the "interpreter-exec" command.  */
484*5796c8dcSSimon Schubert void
485*5796c8dcSSimon Schubert _initialize_interpreter (void)
486*5796c8dcSSimon Schubert {
487*5796c8dcSSimon Schubert   struct cmd_list_element *c;
488*5796c8dcSSimon Schubert 
489*5796c8dcSSimon Schubert   c = add_cmd ("interpreter-exec", class_support,
490*5796c8dcSSimon Schubert 	       interpreter_exec_cmd, _("\
491*5796c8dcSSimon Schubert Execute a command in an interpreter.  It takes two arguments:\n\
492*5796c8dcSSimon Schubert The first argument is the name of the interpreter to use.\n\
493*5796c8dcSSimon Schubert The second argument is the command to execute.\n"), &cmdlist);
494*5796c8dcSSimon Schubert   set_cmd_completer (c, interpreter_completer);
495*5796c8dcSSimon Schubert }
496