xref: /dflybsd-src/contrib/gdb-7/gdb/interps.c (revision cf7f2e2d389e8012d562650bd94d7e433f449d6e)
15796c8dcSSimon Schubert /* Manages interpreters for GDB, the GNU debugger.
25796c8dcSSimon Schubert 
3*cf7f2e2dSJohn Marino    Copyright (C) 2000, 2002, 2003, 2007, 2008, 2009, 2010
45796c8dcSSimon Schubert    Free Software Foundation, Inc.
55796c8dcSSimon Schubert 
65796c8dcSSimon Schubert    Written by Jim Ingham <jingham@apple.com> of Apple Computer, Inc.
75796c8dcSSimon Schubert 
85796c8dcSSimon Schubert    This file is part of GDB.
95796c8dcSSimon Schubert 
105796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
115796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
125796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
135796c8dcSSimon Schubert    (at your option) any later version.
145796c8dcSSimon Schubert 
155796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
165796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
175796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
185796c8dcSSimon Schubert    GNU General Public License for more details.
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
215796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
225796c8dcSSimon Schubert 
235796c8dcSSimon Schubert /* This is just a first cut at separating out the "interpreter"
245796c8dcSSimon Schubert    functions of gdb into self-contained modules.  There are a couple
255796c8dcSSimon Schubert    of open areas that need to be sorted out:
265796c8dcSSimon Schubert 
275796c8dcSSimon Schubert    1) The interpreter explicitly contains a UI_OUT, and can insert itself
285796c8dcSSimon Schubert    into the event loop, but it doesn't explicitly contain hooks for readline.
295796c8dcSSimon Schubert    I did this because it seems to me many interpreters won't want to use
305796c8dcSSimon Schubert    the readline command interface, and it is probably simpler to just let
315796c8dcSSimon Schubert    them take over the input in their resume proc.  */
325796c8dcSSimon Schubert 
335796c8dcSSimon Schubert #include "defs.h"
345796c8dcSSimon Schubert #include "gdbcmd.h"
355796c8dcSSimon Schubert #include "ui-out.h"
365796c8dcSSimon Schubert #include "event-loop.h"
375796c8dcSSimon Schubert #include "event-top.h"
385796c8dcSSimon Schubert #include "interps.h"
395796c8dcSSimon Schubert #include "completer.h"
405796c8dcSSimon Schubert #include "gdb_string.h"
415796c8dcSSimon Schubert #include "gdb_assert.h"
425796c8dcSSimon Schubert #include "top.h"		/* For command_loop.  */
435796c8dcSSimon Schubert #include "exceptions.h"
445796c8dcSSimon Schubert 
455796c8dcSSimon Schubert struct interp
465796c8dcSSimon Schubert {
475796c8dcSSimon Schubert   /* This is the name in "-i=" and set interpreter. */
485796c8dcSSimon Schubert   const char *name;
495796c8dcSSimon Schubert 
505796c8dcSSimon Schubert   /* Interpreters are stored in a linked list, this is the next
515796c8dcSSimon Schubert      one...  */
525796c8dcSSimon Schubert   struct interp *next;
535796c8dcSSimon Schubert 
545796c8dcSSimon Schubert   /* This is a cookie that an instance of the interpreter can use.
555796c8dcSSimon Schubert      This is a bit confused right now as the exact initialization
565796c8dcSSimon Schubert      sequence for it, and how it relates to the interpreter's uiout
575796c8dcSSimon Schubert      object is a bit confused.  */
585796c8dcSSimon Schubert   void *data;
595796c8dcSSimon Schubert 
605796c8dcSSimon Schubert   /* Has the init_proc been run? */
615796c8dcSSimon Schubert   int inited;
625796c8dcSSimon Schubert 
635796c8dcSSimon Schubert   /* This is the ui_out used to collect results for this interpreter.
645796c8dcSSimon Schubert      It can be a formatter for stdout, as is the case for the console
655796c8dcSSimon Schubert      & mi outputs, or it might be a result formatter.  */
665796c8dcSSimon Schubert   struct ui_out *interpreter_out;
675796c8dcSSimon Schubert 
685796c8dcSSimon Schubert   const struct interp_procs *procs;
695796c8dcSSimon Schubert   int quiet_p;
705796c8dcSSimon Schubert };
715796c8dcSSimon Schubert 
725796c8dcSSimon Schubert /* Functions local to this file. */
735796c8dcSSimon Schubert static void initialize_interps (void);
745796c8dcSSimon Schubert static char **interpreter_completer (struct cmd_list_element *cmd,
755796c8dcSSimon Schubert 				     char *text, char *word);
765796c8dcSSimon Schubert 
775796c8dcSSimon Schubert /* The magic initialization routine for this module. */
785796c8dcSSimon Schubert 
795796c8dcSSimon Schubert void _initialize_interpreter (void);
805796c8dcSSimon Schubert 
815796c8dcSSimon Schubert /* Variables local to this file: */
825796c8dcSSimon Schubert 
835796c8dcSSimon Schubert static struct interp *interp_list = NULL;
845796c8dcSSimon Schubert static struct interp *current_interpreter = NULL;
855796c8dcSSimon Schubert static struct interp *top_level_interpreter_ptr = NULL;
865796c8dcSSimon Schubert 
875796c8dcSSimon Schubert static int interpreter_initialized = 0;
885796c8dcSSimon Schubert 
895796c8dcSSimon Schubert /* interp_new - This allocates space for a new interpreter,
905796c8dcSSimon Schubert    fills the fields from the inputs, and returns a pointer to the
915796c8dcSSimon Schubert    interpreter. */
925796c8dcSSimon Schubert struct interp *
935796c8dcSSimon Schubert interp_new (const char *name, void *data, struct ui_out *uiout,
945796c8dcSSimon Schubert 	    const struct interp_procs *procs)
955796c8dcSSimon Schubert {
965796c8dcSSimon Schubert   struct interp *new_interp;
975796c8dcSSimon Schubert 
985796c8dcSSimon Schubert   new_interp = XMALLOC (struct interp);
995796c8dcSSimon Schubert 
1005796c8dcSSimon Schubert   new_interp->name = xstrdup (name);
1015796c8dcSSimon Schubert   new_interp->data = data;
1025796c8dcSSimon Schubert   new_interp->interpreter_out = uiout;
1035796c8dcSSimon Schubert   new_interp->quiet_p = 0;
1045796c8dcSSimon Schubert   new_interp->procs = procs;
1055796c8dcSSimon Schubert   new_interp->inited = 0;
1065796c8dcSSimon Schubert 
1075796c8dcSSimon Schubert   return new_interp;
1085796c8dcSSimon Schubert }
1095796c8dcSSimon Schubert 
1105796c8dcSSimon Schubert /* Add interpreter INTERP to the gdb interpreter list.  The
1115796c8dcSSimon Schubert    interpreter must not have previously been added.  */
1125796c8dcSSimon Schubert void
1135796c8dcSSimon Schubert interp_add (struct interp *interp)
1145796c8dcSSimon Schubert {
1155796c8dcSSimon Schubert   if (!interpreter_initialized)
1165796c8dcSSimon Schubert     initialize_interps ();
1175796c8dcSSimon Schubert 
1185796c8dcSSimon Schubert   gdb_assert (interp_lookup (interp->name) == NULL);
1195796c8dcSSimon Schubert 
1205796c8dcSSimon Schubert   interp->next = interp_list;
1215796c8dcSSimon Schubert   interp_list = interp;
1225796c8dcSSimon Schubert }
1235796c8dcSSimon Schubert 
1245796c8dcSSimon Schubert /* This sets the current interpreter to be INTERP.  If INTERP has not
1255796c8dcSSimon Schubert    been initialized, then this will also run the init proc.  If the
1265796c8dcSSimon Schubert    init proc is successful, return 1, if it fails, set the old
1275796c8dcSSimon Schubert    interpreter back in place and return 0.  If we can't restore the
1285796c8dcSSimon Schubert    old interpreter, then raise an internal error, since we are in
1295796c8dcSSimon Schubert    pretty bad shape at this point.
1305796c8dcSSimon Schubert 
1315796c8dcSSimon Schubert    The TOP_LEVEL parameter tells if this new interpreter is
1325796c8dcSSimon Schubert    the top-level one.  The top-level is what is requested
1335796c8dcSSimon Schubert    on the command line, and is responsible for reporting general
1345796c8dcSSimon Schubert    notification about target state changes.  For example, if
1355796c8dcSSimon Schubert    MI is the top-level interpreter, then it will always report
1365796c8dcSSimon Schubert    events such as target stops and new thread creation, even if they
1375796c8dcSSimon Schubert    are caused by CLI commands.  */
1385796c8dcSSimon Schubert int
1395796c8dcSSimon Schubert interp_set (struct interp *interp, int top_level)
1405796c8dcSSimon Schubert {
1415796c8dcSSimon Schubert   struct interp *old_interp = current_interpreter;
1425796c8dcSSimon Schubert   int first_time = 0;
1435796c8dcSSimon Schubert   char buffer[64];
1445796c8dcSSimon Schubert 
1455796c8dcSSimon Schubert   /* If we already have an interpreter, then trying to
1465796c8dcSSimon Schubert      set top level interpreter is kinda pointless.  */
1475796c8dcSSimon Schubert   gdb_assert (!top_level || !current_interpreter);
1485796c8dcSSimon Schubert   gdb_assert (!top_level || !top_level_interpreter_ptr);
1495796c8dcSSimon Schubert 
1505796c8dcSSimon Schubert   if (current_interpreter != NULL)
1515796c8dcSSimon Schubert     {
1525796c8dcSSimon Schubert       do_all_continuations ();
1535796c8dcSSimon Schubert       ui_out_flush (uiout);
1545796c8dcSSimon Schubert       if (current_interpreter->procs->suspend_proc
1555796c8dcSSimon Schubert 	  && !current_interpreter->procs->suspend_proc (current_interpreter->
1565796c8dcSSimon Schubert 							data))
1575796c8dcSSimon Schubert 	{
1585796c8dcSSimon Schubert 	  error (_("Could not suspend interpreter \"%s\"."),
1595796c8dcSSimon Schubert 		 current_interpreter->name);
1605796c8dcSSimon Schubert 	}
1615796c8dcSSimon Schubert     }
1625796c8dcSSimon Schubert   else
1635796c8dcSSimon Schubert     {
1645796c8dcSSimon Schubert       first_time = 1;
1655796c8dcSSimon Schubert     }
1665796c8dcSSimon Schubert 
1675796c8dcSSimon Schubert   current_interpreter = interp;
1685796c8dcSSimon Schubert   if (top_level)
1695796c8dcSSimon Schubert     top_level_interpreter_ptr = interp;
1705796c8dcSSimon Schubert 
1715796c8dcSSimon Schubert   /* We use interpreter_p for the "set interpreter" variable, so we need
1725796c8dcSSimon Schubert      to make sure we have a malloc'ed copy for the set command to free. */
1735796c8dcSSimon Schubert   if (interpreter_p != NULL
1745796c8dcSSimon Schubert       && strcmp (current_interpreter->name, interpreter_p) != 0)
1755796c8dcSSimon Schubert     {
1765796c8dcSSimon Schubert       xfree (interpreter_p);
1775796c8dcSSimon Schubert 
1785796c8dcSSimon Schubert       interpreter_p = xstrdup (current_interpreter->name);
1795796c8dcSSimon Schubert     }
1805796c8dcSSimon Schubert 
1815796c8dcSSimon Schubert   uiout = interp->interpreter_out;
1825796c8dcSSimon Schubert 
1835796c8dcSSimon Schubert   /* Run the init proc.  If it fails, try to restore the old interp. */
1845796c8dcSSimon Schubert 
1855796c8dcSSimon Schubert   if (!interp->inited)
1865796c8dcSSimon Schubert     {
1875796c8dcSSimon Schubert       if (interp->procs->init_proc != NULL)
1885796c8dcSSimon Schubert 	{
1895796c8dcSSimon Schubert 	  interp->data = interp->procs->init_proc (top_level);
1905796c8dcSSimon Schubert 	}
1915796c8dcSSimon Schubert       interp->inited = 1;
1925796c8dcSSimon Schubert     }
1935796c8dcSSimon Schubert 
1945796c8dcSSimon Schubert   /* Clear out any installed interpreter hooks/event handlers.  */
1955796c8dcSSimon Schubert   clear_interpreter_hooks ();
1965796c8dcSSimon Schubert 
1975796c8dcSSimon Schubert   if (interp->procs->resume_proc != NULL
1985796c8dcSSimon Schubert       && (!interp->procs->resume_proc (interp->data)))
1995796c8dcSSimon Schubert     {
2005796c8dcSSimon Schubert       if (old_interp == NULL || !interp_set (old_interp, 0))
2015796c8dcSSimon Schubert 	internal_error (__FILE__, __LINE__,
2025796c8dcSSimon Schubert 			_("Failed to initialize new interp \"%s\" %s"),
2035796c8dcSSimon Schubert 			interp->name, "and could not restore old interp!\n");
2045796c8dcSSimon Schubert       return 0;
2055796c8dcSSimon Schubert     }
2065796c8dcSSimon Schubert 
2075796c8dcSSimon Schubert   /* Finally, put up the new prompt to show that we are indeed here.
2085796c8dcSSimon Schubert      Also, display_gdb_prompt for the console does some readline magic
2095796c8dcSSimon Schubert      which is needed for the console interpreter, at least... */
2105796c8dcSSimon Schubert 
2115796c8dcSSimon Schubert   if (!first_time)
2125796c8dcSSimon Schubert     {
2135796c8dcSSimon Schubert       if (!interp_quiet_p (interp))
2145796c8dcSSimon Schubert 	{
2155796c8dcSSimon Schubert 	  sprintf (buffer, "Switching to interpreter \"%.24s\".\n",
2165796c8dcSSimon Schubert 		   interp->name);
2175796c8dcSSimon Schubert 	  ui_out_text (uiout, buffer);
2185796c8dcSSimon Schubert 	}
2195796c8dcSSimon Schubert       display_gdb_prompt (NULL);
2205796c8dcSSimon Schubert     }
2215796c8dcSSimon Schubert 
2225796c8dcSSimon Schubert   return 1;
2235796c8dcSSimon Schubert }
2245796c8dcSSimon Schubert 
2255796c8dcSSimon Schubert /* interp_lookup - Looks up the interpreter for NAME.  If no such
2265796c8dcSSimon Schubert    interpreter exists, return NULL, otherwise return a pointer to the
2275796c8dcSSimon Schubert    interpreter.  */
2285796c8dcSSimon Schubert struct interp *
2295796c8dcSSimon Schubert interp_lookup (const char *name)
2305796c8dcSSimon Schubert {
2315796c8dcSSimon Schubert   struct interp *interp;
2325796c8dcSSimon Schubert 
2335796c8dcSSimon Schubert   if (name == NULL || strlen (name) == 0)
2345796c8dcSSimon Schubert     return NULL;
2355796c8dcSSimon Schubert 
2365796c8dcSSimon Schubert   for (interp = interp_list; interp != NULL; interp = interp->next)
2375796c8dcSSimon Schubert     {
2385796c8dcSSimon Schubert       if (strcmp (interp->name, name) == 0)
2395796c8dcSSimon Schubert 	return interp;
2405796c8dcSSimon Schubert     }
2415796c8dcSSimon Schubert 
2425796c8dcSSimon Schubert   return NULL;
2435796c8dcSSimon Schubert }
2445796c8dcSSimon Schubert 
2455796c8dcSSimon Schubert /* Returns the current interpreter. */
2465796c8dcSSimon Schubert 
2475796c8dcSSimon Schubert struct ui_out *
2485796c8dcSSimon Schubert interp_ui_out (struct interp *interp)
2495796c8dcSSimon Schubert {
2505796c8dcSSimon Schubert   if (interp != NULL)
2515796c8dcSSimon Schubert     return interp->interpreter_out;
2525796c8dcSSimon Schubert 
2535796c8dcSSimon Schubert   return current_interpreter->interpreter_out;
2545796c8dcSSimon Schubert }
2555796c8dcSSimon Schubert 
2565796c8dcSSimon Schubert /* Returns true if the current interp is the passed in name. */
2575796c8dcSSimon Schubert int
2585796c8dcSSimon Schubert current_interp_named_p (const char *interp_name)
2595796c8dcSSimon Schubert {
2605796c8dcSSimon Schubert   if (current_interpreter)
2615796c8dcSSimon Schubert     return (strcmp (current_interpreter->name, interp_name) == 0);
2625796c8dcSSimon Schubert 
2635796c8dcSSimon Schubert   return 0;
2645796c8dcSSimon Schubert }
2655796c8dcSSimon Schubert 
2665796c8dcSSimon Schubert /* This is called in display_gdb_prompt.  If the proc returns a zero
2675796c8dcSSimon Schubert    value, display_gdb_prompt will return without displaying the
2685796c8dcSSimon Schubert    prompt.  */
2695796c8dcSSimon Schubert int
2705796c8dcSSimon Schubert current_interp_display_prompt_p (void)
2715796c8dcSSimon Schubert {
2725796c8dcSSimon Schubert   if (current_interpreter == NULL
2735796c8dcSSimon Schubert       || current_interpreter->procs->prompt_proc_p == NULL)
2745796c8dcSSimon Schubert     return 0;
2755796c8dcSSimon Schubert   else
2765796c8dcSSimon Schubert     return current_interpreter->procs->prompt_proc_p (current_interpreter->
2775796c8dcSSimon Schubert 						      data);
2785796c8dcSSimon Schubert }
2795796c8dcSSimon Schubert 
2805796c8dcSSimon Schubert /* Run the current command interpreter's main loop.  */
2815796c8dcSSimon Schubert void
2825796c8dcSSimon Schubert current_interp_command_loop (void)
2835796c8dcSSimon Schubert {
2845796c8dcSSimon Schubert   /* Somewhat messy.  For the moment prop up all the old ways of
2855796c8dcSSimon Schubert      selecting the command loop.  `deprecated_command_loop_hook'
2865796c8dcSSimon Schubert      should be deprecated.  */
2875796c8dcSSimon Schubert   if (deprecated_command_loop_hook != NULL)
2885796c8dcSSimon Schubert     deprecated_command_loop_hook ();
2895796c8dcSSimon Schubert   else if (current_interpreter != NULL
2905796c8dcSSimon Schubert 	   && current_interpreter->procs->command_loop_proc != NULL)
2915796c8dcSSimon Schubert     current_interpreter->procs->command_loop_proc (current_interpreter->data);
2925796c8dcSSimon Schubert   else
2935796c8dcSSimon Schubert     cli_command_loop ();
2945796c8dcSSimon Schubert }
2955796c8dcSSimon Schubert 
2965796c8dcSSimon Schubert int
2975796c8dcSSimon Schubert interp_quiet_p (struct interp *interp)
2985796c8dcSSimon Schubert {
2995796c8dcSSimon Schubert   if (interp != NULL)
3005796c8dcSSimon Schubert     return interp->quiet_p;
3015796c8dcSSimon Schubert   else
3025796c8dcSSimon Schubert     return current_interpreter->quiet_p;
3035796c8dcSSimon Schubert }
3045796c8dcSSimon Schubert 
3055796c8dcSSimon Schubert static int
3065796c8dcSSimon Schubert interp_set_quiet (struct interp *interp, int quiet)
3075796c8dcSSimon Schubert {
3085796c8dcSSimon Schubert   int old_val = interp->quiet_p;
309*cf7f2e2dSJohn Marino 
3105796c8dcSSimon Schubert   interp->quiet_p = quiet;
3115796c8dcSSimon Schubert   return old_val;
3125796c8dcSSimon Schubert }
3135796c8dcSSimon Schubert 
3145796c8dcSSimon Schubert /* interp_exec - This executes COMMAND_STR in the current
3155796c8dcSSimon Schubert    interpreter. */
3165796c8dcSSimon Schubert int
3175796c8dcSSimon Schubert interp_exec_p (struct interp *interp)
3185796c8dcSSimon Schubert {
3195796c8dcSSimon Schubert   return interp->procs->exec_proc != NULL;
3205796c8dcSSimon Schubert }
3215796c8dcSSimon Schubert 
3225796c8dcSSimon Schubert struct gdb_exception
3235796c8dcSSimon Schubert interp_exec (struct interp *interp, const char *command_str)
3245796c8dcSSimon Schubert {
3255796c8dcSSimon Schubert   if (interp->procs->exec_proc != NULL)
3265796c8dcSSimon Schubert     {
3275796c8dcSSimon Schubert       return interp->procs->exec_proc (interp->data, command_str);
3285796c8dcSSimon Schubert     }
3295796c8dcSSimon Schubert   return exception_none;
3305796c8dcSSimon Schubert }
3315796c8dcSSimon Schubert 
3325796c8dcSSimon Schubert /* A convenience routine that nulls out all the common command hooks.
3335796c8dcSSimon Schubert    Use it when removing your interpreter in its suspend proc.  */
3345796c8dcSSimon Schubert void
3355796c8dcSSimon Schubert clear_interpreter_hooks (void)
3365796c8dcSSimon Schubert {
3375796c8dcSSimon Schubert   deprecated_init_ui_hook = 0;
3385796c8dcSSimon Schubert   deprecated_print_frame_info_listing_hook = 0;
3395796c8dcSSimon Schubert   /*print_frame_more_info_hook = 0; */
3405796c8dcSSimon Schubert   deprecated_query_hook = 0;
3415796c8dcSSimon Schubert   deprecated_warning_hook = 0;
3425796c8dcSSimon Schubert   deprecated_interactive_hook = 0;
3435796c8dcSSimon Schubert   deprecated_readline_begin_hook = 0;
3445796c8dcSSimon Schubert   deprecated_readline_hook = 0;
3455796c8dcSSimon Schubert   deprecated_readline_end_hook = 0;
3465796c8dcSSimon Schubert   deprecated_register_changed_hook = 0;
3475796c8dcSSimon Schubert   deprecated_context_hook = 0;
3485796c8dcSSimon Schubert   deprecated_target_wait_hook = 0;
3495796c8dcSSimon Schubert   deprecated_call_command_hook = 0;
3505796c8dcSSimon Schubert   deprecated_error_begin_hook = 0;
3515796c8dcSSimon Schubert   deprecated_command_loop_hook = 0;
3525796c8dcSSimon Schubert }
3535796c8dcSSimon Schubert 
3545796c8dcSSimon Schubert /* This is a lazy init routine, called the first time the interpreter
3555796c8dcSSimon Schubert    module is used.  I put it here just in case, but I haven't thought
3565796c8dcSSimon Schubert    of a use for it yet.  I will probably bag it soon, since I don't
3575796c8dcSSimon Schubert    think it will be necessary.  */
3585796c8dcSSimon Schubert static void
3595796c8dcSSimon Schubert initialize_interps (void)
3605796c8dcSSimon Schubert {
3615796c8dcSSimon Schubert   interpreter_initialized = 1;
3625796c8dcSSimon Schubert   /* Don't know if anything needs to be done here... */
3635796c8dcSSimon Schubert }
3645796c8dcSSimon Schubert 
3655796c8dcSSimon Schubert static void
3665796c8dcSSimon Schubert interpreter_exec_cmd (char *args, int from_tty)
3675796c8dcSSimon Schubert {
3685796c8dcSSimon Schubert   struct interp *old_interp, *interp_to_use;
3695796c8dcSSimon Schubert   char **prules = NULL;
3705796c8dcSSimon Schubert   char **trule = NULL;
3715796c8dcSSimon Schubert   unsigned int nrules;
3725796c8dcSSimon Schubert   unsigned int i;
3735796c8dcSSimon Schubert   int old_quiet, use_quiet;
3745796c8dcSSimon Schubert 
3755796c8dcSSimon Schubert   if (args == NULL)
3765796c8dcSSimon Schubert     error_no_arg (_("interpreter-exec command"));
3775796c8dcSSimon Schubert 
3785796c8dcSSimon Schubert   prules = gdb_buildargv (args);
3795796c8dcSSimon Schubert   make_cleanup_freeargv (prules);
3805796c8dcSSimon Schubert 
3815796c8dcSSimon Schubert   nrules = 0;
3825796c8dcSSimon Schubert   for (trule = prules; *trule != NULL; trule++)
3835796c8dcSSimon Schubert     nrules++;
3845796c8dcSSimon Schubert 
3855796c8dcSSimon Schubert   if (nrules < 2)
3865796c8dcSSimon Schubert     error (_("usage: interpreter-exec <interpreter> [ <command> ... ]"));
3875796c8dcSSimon Schubert 
3885796c8dcSSimon Schubert   old_interp = current_interpreter;
3895796c8dcSSimon Schubert 
3905796c8dcSSimon Schubert   interp_to_use = interp_lookup (prules[0]);
3915796c8dcSSimon Schubert   if (interp_to_use == NULL)
3925796c8dcSSimon Schubert     error (_("Could not find interpreter \"%s\"."), prules[0]);
3935796c8dcSSimon Schubert 
3945796c8dcSSimon Schubert   /* Temporarily set interpreters quiet */
3955796c8dcSSimon Schubert   old_quiet = interp_set_quiet (old_interp, 1);
3965796c8dcSSimon Schubert   use_quiet = interp_set_quiet (interp_to_use, 1);
3975796c8dcSSimon Schubert 
3985796c8dcSSimon Schubert   if (!interp_set (interp_to_use, 0))
3995796c8dcSSimon Schubert     error (_("Could not switch to interpreter \"%s\"."), prules[0]);
4005796c8dcSSimon Schubert 
4015796c8dcSSimon Schubert   for (i = 1; i < nrules; i++)
4025796c8dcSSimon Schubert     {
4035796c8dcSSimon Schubert       struct gdb_exception e = interp_exec (interp_to_use, prules[i]);
404*cf7f2e2dSJohn Marino 
4055796c8dcSSimon Schubert       if (e.reason < 0)
4065796c8dcSSimon Schubert 	{
4075796c8dcSSimon Schubert 	  interp_set (old_interp, 0);
4085796c8dcSSimon Schubert 	  interp_set_quiet (interp_to_use, use_quiet);
4095796c8dcSSimon Schubert 	  interp_set_quiet (old_interp, old_quiet);
4105796c8dcSSimon Schubert 	  error (_("error in command: \"%s\"."), prules[i]);
4115796c8dcSSimon Schubert 	}
4125796c8dcSSimon Schubert     }
4135796c8dcSSimon Schubert 
4145796c8dcSSimon Schubert   interp_set (old_interp, 0);
4155796c8dcSSimon Schubert   interp_set_quiet (interp_to_use, use_quiet);
4165796c8dcSSimon Schubert   interp_set_quiet (old_interp, old_quiet);
4175796c8dcSSimon Schubert }
4185796c8dcSSimon Schubert 
4195796c8dcSSimon Schubert /* List the possible interpreters which could complete the given text. */
4205796c8dcSSimon Schubert static char **
4215796c8dcSSimon Schubert interpreter_completer (struct cmd_list_element *ignore, char *text, char *word)
4225796c8dcSSimon Schubert {
4235796c8dcSSimon Schubert   int alloced = 0;
4245796c8dcSSimon Schubert   int textlen;
4255796c8dcSSimon Schubert   int num_matches;
4265796c8dcSSimon Schubert   char **matches;
4275796c8dcSSimon Schubert   struct interp *interp;
4285796c8dcSSimon Schubert 
4295796c8dcSSimon Schubert   /* We expect only a very limited number of interpreters, so just
4305796c8dcSSimon Schubert      allocate room for all of them plus one for the last that must be NULL
4315796c8dcSSimon Schubert      to correctly end the list. */
4325796c8dcSSimon Schubert   for (interp = interp_list; interp != NULL; interp = interp->next)
4335796c8dcSSimon Schubert     ++alloced;
4345796c8dcSSimon Schubert   matches = (char **) xcalloc (alloced + 1, sizeof (char *));
4355796c8dcSSimon Schubert 
4365796c8dcSSimon Schubert   num_matches = 0;
4375796c8dcSSimon Schubert   textlen = strlen (text);
4385796c8dcSSimon Schubert   for (interp = interp_list; interp != NULL; interp = interp->next)
4395796c8dcSSimon Schubert     {
4405796c8dcSSimon Schubert       if (strncmp (interp->name, text, textlen) == 0)
4415796c8dcSSimon Schubert 	{
4425796c8dcSSimon Schubert 	  matches[num_matches] =
4435796c8dcSSimon Schubert 	    (char *) xmalloc (strlen (word) + strlen (interp->name) + 1);
4445796c8dcSSimon Schubert 	  if (word == text)
4455796c8dcSSimon Schubert 	    strcpy (matches[num_matches], interp->name);
4465796c8dcSSimon Schubert 	  else if (word > text)
4475796c8dcSSimon Schubert 	    {
4485796c8dcSSimon Schubert 	      /* Return some portion of interp->name */
4495796c8dcSSimon Schubert 	      strcpy (matches[num_matches], interp->name + (word - text));
4505796c8dcSSimon Schubert 	    }
4515796c8dcSSimon Schubert 	  else
4525796c8dcSSimon Schubert 	    {
4535796c8dcSSimon Schubert 	      /* Return some of text plus interp->name */
4545796c8dcSSimon Schubert 	      strncpy (matches[num_matches], word, text - word);
4555796c8dcSSimon Schubert 	      matches[num_matches][text - word] = '\0';
4565796c8dcSSimon Schubert 	      strcat (matches[num_matches], interp->name);
4575796c8dcSSimon Schubert 	    }
4585796c8dcSSimon Schubert 	  ++num_matches;
4595796c8dcSSimon Schubert 	}
4605796c8dcSSimon Schubert     }
4615796c8dcSSimon Schubert 
4625796c8dcSSimon Schubert   if (num_matches == 0)
4635796c8dcSSimon Schubert     {
4645796c8dcSSimon Schubert       xfree (matches);
4655796c8dcSSimon Schubert       matches = NULL;
4665796c8dcSSimon Schubert     }
4675796c8dcSSimon Schubert 
4685796c8dcSSimon Schubert   return matches;
4695796c8dcSSimon Schubert }
4705796c8dcSSimon Schubert 
4715796c8dcSSimon Schubert struct interp *
4725796c8dcSSimon Schubert top_level_interpreter (void)
4735796c8dcSSimon Schubert {
4745796c8dcSSimon Schubert   return top_level_interpreter_ptr;
4755796c8dcSSimon Schubert }
4765796c8dcSSimon Schubert 
4775796c8dcSSimon Schubert void *
4785796c8dcSSimon Schubert top_level_interpreter_data (void)
4795796c8dcSSimon Schubert {
4805796c8dcSSimon Schubert   gdb_assert (top_level_interpreter_ptr);
4815796c8dcSSimon Schubert   return top_level_interpreter_ptr->data;
4825796c8dcSSimon Schubert }
4835796c8dcSSimon Schubert 
4845796c8dcSSimon Schubert /* This just adds the "interpreter-exec" command.  */
4855796c8dcSSimon Schubert void
4865796c8dcSSimon Schubert _initialize_interpreter (void)
4875796c8dcSSimon Schubert {
4885796c8dcSSimon Schubert   struct cmd_list_element *c;
4895796c8dcSSimon Schubert 
4905796c8dcSSimon Schubert   c = add_cmd ("interpreter-exec", class_support,
4915796c8dcSSimon Schubert 	       interpreter_exec_cmd, _("\
4925796c8dcSSimon Schubert Execute a command in an interpreter.  It takes two arguments:\n\
4935796c8dcSSimon Schubert The first argument is the name of the interpreter to use.\n\
4945796c8dcSSimon Schubert The second argument is the command to execute.\n"), &cmdlist);
4955796c8dcSSimon Schubert   set_cmd_completer (c, interpreter_completer);
4965796c8dcSSimon Schubert }
497