xref: /dragonfly/contrib/gdb-7/gdb/interps.c (revision ef5ccd6c)
15796c8dcSSimon Schubert /* Manages interpreters for GDB, the GNU debugger.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 2000-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    Written by Jim Ingham <jingham@apple.com> of Apple Computer, Inc.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This file is part of GDB.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
105796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
115796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
125796c8dcSSimon Schubert    (at your option) any later version.
135796c8dcSSimon Schubert 
145796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
155796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
165796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
175796c8dcSSimon Schubert    GNU General Public License for more details.
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
205796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert /* This is just a first cut at separating out the "interpreter"
235796c8dcSSimon Schubert    functions of gdb into self-contained modules.  There are a couple
245796c8dcSSimon Schubert    of open areas that need to be sorted out:
255796c8dcSSimon Schubert 
265796c8dcSSimon Schubert    1) The interpreter explicitly contains a UI_OUT, and can insert itself
275796c8dcSSimon Schubert    into the event loop, but it doesn't explicitly contain hooks for readline.
285796c8dcSSimon Schubert    I did this because it seems to me many interpreters won't want to use
295796c8dcSSimon Schubert    the readline command interface, and it is probably simpler to just let
305796c8dcSSimon Schubert    them take over the input in their resume proc.  */
315796c8dcSSimon Schubert 
325796c8dcSSimon Schubert #include "defs.h"
335796c8dcSSimon Schubert #include "gdbcmd.h"
345796c8dcSSimon Schubert #include "ui-out.h"
355796c8dcSSimon Schubert #include "event-loop.h"
365796c8dcSSimon Schubert #include "event-top.h"
375796c8dcSSimon Schubert #include "interps.h"
385796c8dcSSimon Schubert #include "completer.h"
395796c8dcSSimon Schubert #include "gdb_string.h"
405796c8dcSSimon Schubert #include "gdb_assert.h"
415796c8dcSSimon Schubert #include "top.h"		/* For command_loop.  */
425796c8dcSSimon Schubert #include "exceptions.h"
43a45ae5f8SJohn Marino #include "continuations.h"
44a45ae5f8SJohn Marino 
45a45ae5f8SJohn Marino /* True if the current interpreter in is async mode.  See interps.h
46a45ae5f8SJohn Marino    for more details.  This starts out disabled, until all the explicit
47a45ae5f8SJohn Marino    command line arguments (e.g., `gdb -ex "start" -ex "next"') are
48a45ae5f8SJohn Marino    processed.  */
49a45ae5f8SJohn Marino int interpreter_async = 0;
505796c8dcSSimon Schubert 
515796c8dcSSimon Schubert struct interp
525796c8dcSSimon Schubert {
535796c8dcSSimon Schubert   /* This is the name in "-i=" and set interpreter.  */
545796c8dcSSimon Schubert   const char *name;
555796c8dcSSimon Schubert 
565796c8dcSSimon Schubert   /* Interpreters are stored in a linked list, this is the next
575796c8dcSSimon Schubert      one...  */
585796c8dcSSimon Schubert   struct interp *next;
595796c8dcSSimon Schubert 
605796c8dcSSimon Schubert   /* This is a cookie that an instance of the interpreter can use.
615796c8dcSSimon Schubert      This is a bit confused right now as the exact initialization
625796c8dcSSimon Schubert      sequence for it, and how it relates to the interpreter's uiout
635796c8dcSSimon Schubert      object is a bit confused.  */
645796c8dcSSimon Schubert   void *data;
655796c8dcSSimon Schubert 
665796c8dcSSimon Schubert   /* Has the init_proc been run?  */
675796c8dcSSimon Schubert   int inited;
685796c8dcSSimon Schubert 
695796c8dcSSimon Schubert   const struct interp_procs *procs;
705796c8dcSSimon Schubert   int quiet_p;
715796c8dcSSimon Schubert };
725796c8dcSSimon Schubert 
735796c8dcSSimon Schubert /* Functions local to this file.  */
745796c8dcSSimon Schubert static void initialize_interps (void);
755796c8dcSSimon Schubert 
765796c8dcSSimon Schubert /* The magic initialization routine for this module.  */
775796c8dcSSimon Schubert 
785796c8dcSSimon Schubert void _initialize_interpreter (void);
795796c8dcSSimon Schubert 
805796c8dcSSimon Schubert /* Variables local to this file: */
815796c8dcSSimon Schubert 
825796c8dcSSimon Schubert static struct interp *interp_list = NULL;
835796c8dcSSimon Schubert static struct interp *current_interpreter = NULL;
845796c8dcSSimon Schubert static struct interp *top_level_interpreter_ptr = NULL;
855796c8dcSSimon Schubert 
865796c8dcSSimon Schubert static int interpreter_initialized = 0;
875796c8dcSSimon Schubert 
885796c8dcSSimon Schubert /* interp_new - This allocates space for a new interpreter,
895796c8dcSSimon Schubert    fills the fields from the inputs, and returns a pointer to the
905796c8dcSSimon Schubert    interpreter.  */
915796c8dcSSimon Schubert struct interp *
interp_new(const char * name,const struct interp_procs * procs)92a45ae5f8SJohn Marino interp_new (const char *name, const struct interp_procs *procs)
935796c8dcSSimon Schubert {
945796c8dcSSimon Schubert   struct interp *new_interp;
955796c8dcSSimon Schubert 
965796c8dcSSimon Schubert   new_interp = XMALLOC (struct interp);
975796c8dcSSimon Schubert 
985796c8dcSSimon Schubert   new_interp->name = xstrdup (name);
99a45ae5f8SJohn Marino   new_interp->data = NULL;
1005796c8dcSSimon Schubert   new_interp->quiet_p = 0;
1015796c8dcSSimon Schubert   new_interp->procs = procs;
1025796c8dcSSimon Schubert   new_interp->inited = 0;
1035796c8dcSSimon Schubert 
1045796c8dcSSimon Schubert   return new_interp;
1055796c8dcSSimon Schubert }
1065796c8dcSSimon Schubert 
1075796c8dcSSimon Schubert /* Add interpreter INTERP to the gdb interpreter list.  The
1085796c8dcSSimon Schubert    interpreter must not have previously been added.  */
1095796c8dcSSimon Schubert void
interp_add(struct interp * interp)1105796c8dcSSimon Schubert interp_add (struct interp *interp)
1115796c8dcSSimon Schubert {
1125796c8dcSSimon Schubert   if (!interpreter_initialized)
1135796c8dcSSimon Schubert     initialize_interps ();
1145796c8dcSSimon Schubert 
1155796c8dcSSimon Schubert   gdb_assert (interp_lookup (interp->name) == NULL);
1165796c8dcSSimon Schubert 
1175796c8dcSSimon Schubert   interp->next = interp_list;
1185796c8dcSSimon Schubert   interp_list = interp;
1195796c8dcSSimon Schubert }
1205796c8dcSSimon Schubert 
1215796c8dcSSimon Schubert /* This sets the current interpreter to be INTERP.  If INTERP has not
1225796c8dcSSimon Schubert    been initialized, then this will also run the init proc.  If the
1235796c8dcSSimon Schubert    init proc is successful, return 1, if it fails, set the old
1245796c8dcSSimon Schubert    interpreter back in place and return 0.  If we can't restore the
1255796c8dcSSimon Schubert    old interpreter, then raise an internal error, since we are in
1265796c8dcSSimon Schubert    pretty bad shape at this point.
1275796c8dcSSimon Schubert 
1285796c8dcSSimon Schubert    The TOP_LEVEL parameter tells if this new interpreter is
1295796c8dcSSimon Schubert    the top-level one.  The top-level is what is requested
1305796c8dcSSimon Schubert    on the command line, and is responsible for reporting general
1315796c8dcSSimon Schubert    notification about target state changes.  For example, if
1325796c8dcSSimon Schubert    MI is the top-level interpreter, then it will always report
1335796c8dcSSimon Schubert    events such as target stops and new thread creation, even if they
1345796c8dcSSimon Schubert    are caused by CLI commands.  */
1355796c8dcSSimon Schubert int
interp_set(struct interp * interp,int top_level)1365796c8dcSSimon Schubert interp_set (struct interp *interp, int top_level)
1375796c8dcSSimon Schubert {
1385796c8dcSSimon Schubert   struct interp *old_interp = current_interpreter;
1395796c8dcSSimon Schubert   int first_time = 0;
1405796c8dcSSimon Schubert   char buffer[64];
1415796c8dcSSimon Schubert 
1425796c8dcSSimon Schubert   /* If we already have an interpreter, then trying to
1435796c8dcSSimon Schubert      set top level interpreter is kinda pointless.  */
1445796c8dcSSimon Schubert   gdb_assert (!top_level || !current_interpreter);
1455796c8dcSSimon Schubert   gdb_assert (!top_level || !top_level_interpreter_ptr);
1465796c8dcSSimon Schubert 
1475796c8dcSSimon Schubert   if (current_interpreter != NULL)
1485796c8dcSSimon Schubert     {
149a45ae5f8SJohn Marino       ui_out_flush (current_uiout);
1505796c8dcSSimon Schubert       if (current_interpreter->procs->suspend_proc
1515796c8dcSSimon Schubert 	  && !current_interpreter->procs->suspend_proc (current_interpreter->
1525796c8dcSSimon Schubert 							data))
1535796c8dcSSimon Schubert 	{
1545796c8dcSSimon Schubert 	  error (_("Could not suspend interpreter \"%s\"."),
1555796c8dcSSimon Schubert 		 current_interpreter->name);
1565796c8dcSSimon Schubert 	}
1575796c8dcSSimon Schubert     }
1585796c8dcSSimon Schubert   else
1595796c8dcSSimon Schubert     {
1605796c8dcSSimon Schubert       first_time = 1;
1615796c8dcSSimon Schubert     }
1625796c8dcSSimon Schubert 
1635796c8dcSSimon Schubert   current_interpreter = interp;
1645796c8dcSSimon Schubert   if (top_level)
1655796c8dcSSimon Schubert     top_level_interpreter_ptr = interp;
1665796c8dcSSimon Schubert 
1675796c8dcSSimon Schubert   /* We use interpreter_p for the "set interpreter" variable, so we need
1685796c8dcSSimon Schubert      to make sure we have a malloc'ed copy for the set command to free.  */
1695796c8dcSSimon Schubert   if (interpreter_p != NULL
1705796c8dcSSimon Schubert       && strcmp (current_interpreter->name, interpreter_p) != 0)
1715796c8dcSSimon Schubert     {
1725796c8dcSSimon Schubert       xfree (interpreter_p);
1735796c8dcSSimon Schubert 
1745796c8dcSSimon Schubert       interpreter_p = xstrdup (current_interpreter->name);
1755796c8dcSSimon Schubert     }
1765796c8dcSSimon Schubert 
1775796c8dcSSimon Schubert   /* Run the init proc.  If it fails, try to restore the old interp.  */
1785796c8dcSSimon Schubert 
1795796c8dcSSimon Schubert   if (!interp->inited)
1805796c8dcSSimon Schubert     {
1815796c8dcSSimon Schubert       if (interp->procs->init_proc != NULL)
1825796c8dcSSimon Schubert 	{
183a45ae5f8SJohn Marino 	  interp->data = interp->procs->init_proc (interp, top_level);
1845796c8dcSSimon Schubert 	}
1855796c8dcSSimon Schubert       interp->inited = 1;
1865796c8dcSSimon Schubert     }
1875796c8dcSSimon Schubert 
188a45ae5f8SJohn Marino   /* Do this only after the interpreter is initialized.  */
189a45ae5f8SJohn Marino   current_uiout = interp->procs->ui_out_proc (interp);
190a45ae5f8SJohn Marino 
1915796c8dcSSimon Schubert   /* Clear out any installed interpreter hooks/event handlers.  */
1925796c8dcSSimon Schubert   clear_interpreter_hooks ();
1935796c8dcSSimon Schubert 
1945796c8dcSSimon Schubert   if (interp->procs->resume_proc != NULL
1955796c8dcSSimon Schubert       && (!interp->procs->resume_proc (interp->data)))
1965796c8dcSSimon Schubert     {
1975796c8dcSSimon Schubert       if (old_interp == NULL || !interp_set (old_interp, 0))
1985796c8dcSSimon Schubert 	internal_error (__FILE__, __LINE__,
1995796c8dcSSimon Schubert 			_("Failed to initialize new interp \"%s\" %s"),
2005796c8dcSSimon Schubert 			interp->name, "and could not restore old interp!\n");
2015796c8dcSSimon Schubert       return 0;
2025796c8dcSSimon Schubert     }
2035796c8dcSSimon Schubert 
2045796c8dcSSimon Schubert   /* Finally, put up the new prompt to show that we are indeed here.
2055796c8dcSSimon Schubert      Also, display_gdb_prompt for the console does some readline magic
2065796c8dcSSimon Schubert      which is needed for the console interpreter, at least...  */
2075796c8dcSSimon Schubert 
2085796c8dcSSimon Schubert   if (!first_time)
2095796c8dcSSimon Schubert     {
2105796c8dcSSimon Schubert       if (!interp_quiet_p (interp))
2115796c8dcSSimon Schubert 	{
212*ef5ccd6cSJohn Marino 	  xsnprintf (buffer, sizeof (buffer),
213*ef5ccd6cSJohn Marino 		     "Switching to interpreter \"%.24s\".\n", interp->name);
214a45ae5f8SJohn Marino 	  ui_out_text (current_uiout, buffer);
2155796c8dcSSimon Schubert 	}
2165796c8dcSSimon Schubert       display_gdb_prompt (NULL);
2175796c8dcSSimon Schubert     }
2185796c8dcSSimon Schubert 
2195796c8dcSSimon Schubert   return 1;
2205796c8dcSSimon Schubert }
2215796c8dcSSimon Schubert 
2225796c8dcSSimon Schubert /* interp_lookup - Looks up the interpreter for NAME.  If no such
2235796c8dcSSimon Schubert    interpreter exists, return NULL, otherwise return a pointer to the
2245796c8dcSSimon Schubert    interpreter.  */
2255796c8dcSSimon Schubert struct interp *
interp_lookup(const char * name)2265796c8dcSSimon Schubert interp_lookup (const char *name)
2275796c8dcSSimon Schubert {
2285796c8dcSSimon Schubert   struct interp *interp;
2295796c8dcSSimon Schubert 
2305796c8dcSSimon Schubert   if (name == NULL || strlen (name) == 0)
2315796c8dcSSimon Schubert     return NULL;
2325796c8dcSSimon Schubert 
2335796c8dcSSimon Schubert   for (interp = interp_list; interp != NULL; interp = interp->next)
2345796c8dcSSimon Schubert     {
2355796c8dcSSimon Schubert       if (strcmp (interp->name, name) == 0)
2365796c8dcSSimon Schubert 	return interp;
2375796c8dcSSimon Schubert     }
2385796c8dcSSimon Schubert 
2395796c8dcSSimon Schubert   return NULL;
2405796c8dcSSimon Schubert }
2415796c8dcSSimon Schubert 
2425796c8dcSSimon Schubert /* Returns the current interpreter.  */
2435796c8dcSSimon Schubert 
2445796c8dcSSimon Schubert struct ui_out *
interp_ui_out(struct interp * interp)2455796c8dcSSimon Schubert interp_ui_out (struct interp *interp)
2465796c8dcSSimon Schubert {
2475796c8dcSSimon Schubert   if (interp != NULL)
248a45ae5f8SJohn Marino     return interp->procs->ui_out_proc (interp);
2495796c8dcSSimon Schubert 
250a45ae5f8SJohn Marino   return current_interpreter->procs->ui_out_proc (current_interpreter);
251a45ae5f8SJohn Marino }
252a45ae5f8SJohn Marino 
253*ef5ccd6cSJohn Marino int
current_interp_set_logging(int start_log,struct ui_file * out,struct ui_file * logfile)254*ef5ccd6cSJohn Marino current_interp_set_logging (int start_log, struct ui_file *out,
255*ef5ccd6cSJohn Marino 			    struct ui_file *logfile)
256*ef5ccd6cSJohn Marino {
257*ef5ccd6cSJohn Marino   if (current_interpreter == NULL
258*ef5ccd6cSJohn Marino       || current_interpreter->procs->set_logging_proc == NULL)
259*ef5ccd6cSJohn Marino     return 0;
260*ef5ccd6cSJohn Marino 
261*ef5ccd6cSJohn Marino   return current_interpreter->procs->set_logging_proc (current_interpreter,
262*ef5ccd6cSJohn Marino 						       start_log, out,
263*ef5ccd6cSJohn Marino 						       logfile);
264*ef5ccd6cSJohn Marino }
265*ef5ccd6cSJohn Marino 
266*ef5ccd6cSJohn Marino /* Temporarily overrides the current interpreter.  */
267*ef5ccd6cSJohn Marino struct interp *
interp_set_temp(const char * name)268*ef5ccd6cSJohn Marino interp_set_temp (const char *name)
269*ef5ccd6cSJohn Marino {
270*ef5ccd6cSJohn Marino   struct interp *interp = interp_lookup (name);
271*ef5ccd6cSJohn Marino   struct interp *old_interp = current_interpreter;
272*ef5ccd6cSJohn Marino 
273*ef5ccd6cSJohn Marino   if (interp)
274*ef5ccd6cSJohn Marino     current_interpreter = interp;
275*ef5ccd6cSJohn Marino   return old_interp;
276*ef5ccd6cSJohn Marino }
277*ef5ccd6cSJohn Marino 
278a45ae5f8SJohn Marino /* Returns the interpreter's cookie.  */
279a45ae5f8SJohn Marino 
280a45ae5f8SJohn Marino void *
interp_data(struct interp * interp)281a45ae5f8SJohn Marino interp_data (struct interp *interp)
282a45ae5f8SJohn Marino {
283a45ae5f8SJohn Marino   return interp->data;
284a45ae5f8SJohn Marino }
285a45ae5f8SJohn Marino 
286a45ae5f8SJohn Marino /* Returns the interpreter's name.  */
287a45ae5f8SJohn Marino 
288a45ae5f8SJohn Marino const char *
interp_name(struct interp * interp)289a45ae5f8SJohn Marino interp_name (struct interp *interp)
290a45ae5f8SJohn Marino {
291a45ae5f8SJohn Marino   return interp->name;
2925796c8dcSSimon Schubert }
2935796c8dcSSimon Schubert 
2945796c8dcSSimon Schubert /* Returns true if the current interp is the passed in name.  */
2955796c8dcSSimon Schubert int
current_interp_named_p(const char * interp_name)2965796c8dcSSimon Schubert current_interp_named_p (const char *interp_name)
2975796c8dcSSimon Schubert {
2985796c8dcSSimon Schubert   if (current_interpreter)
2995796c8dcSSimon Schubert     return (strcmp (current_interpreter->name, interp_name) == 0);
3005796c8dcSSimon Schubert 
3015796c8dcSSimon Schubert   return 0;
3025796c8dcSSimon Schubert }
3035796c8dcSSimon Schubert 
3045796c8dcSSimon Schubert /* This is called in display_gdb_prompt.  If the proc returns a zero
3055796c8dcSSimon Schubert    value, display_gdb_prompt will return without displaying the
3065796c8dcSSimon Schubert    prompt.  */
3075796c8dcSSimon Schubert int
current_interp_display_prompt_p(void)3085796c8dcSSimon Schubert current_interp_display_prompt_p (void)
3095796c8dcSSimon Schubert {
3105796c8dcSSimon Schubert   if (current_interpreter == NULL
3115796c8dcSSimon Schubert       || current_interpreter->procs->prompt_proc_p == NULL)
3125796c8dcSSimon Schubert     return 0;
3135796c8dcSSimon Schubert   else
3145796c8dcSSimon Schubert     return current_interpreter->procs->prompt_proc_p (current_interpreter->
3155796c8dcSSimon Schubert 						      data);
3165796c8dcSSimon Schubert }
3175796c8dcSSimon Schubert 
3185796c8dcSSimon Schubert /* Run the current command interpreter's main loop.  */
3195796c8dcSSimon Schubert void
current_interp_command_loop(void)3205796c8dcSSimon Schubert current_interp_command_loop (void)
3215796c8dcSSimon Schubert {
3225796c8dcSSimon Schubert   /* Somewhat messy.  For the moment prop up all the old ways of
3235796c8dcSSimon Schubert      selecting the command loop.  `deprecated_command_loop_hook'
3245796c8dcSSimon Schubert      should be deprecated.  */
3255796c8dcSSimon Schubert   if (deprecated_command_loop_hook != NULL)
3265796c8dcSSimon Schubert     deprecated_command_loop_hook ();
3275796c8dcSSimon Schubert   else if (current_interpreter != NULL
3285796c8dcSSimon Schubert 	   && current_interpreter->procs->command_loop_proc != NULL)
3295796c8dcSSimon Schubert     current_interpreter->procs->command_loop_proc (current_interpreter->data);
3305796c8dcSSimon Schubert   else
3315796c8dcSSimon Schubert     cli_command_loop ();
3325796c8dcSSimon Schubert }
3335796c8dcSSimon Schubert 
3345796c8dcSSimon Schubert int
interp_quiet_p(struct interp * interp)3355796c8dcSSimon Schubert interp_quiet_p (struct interp *interp)
3365796c8dcSSimon Schubert {
3375796c8dcSSimon Schubert   if (interp != NULL)
3385796c8dcSSimon Schubert     return interp->quiet_p;
3395796c8dcSSimon Schubert   else
3405796c8dcSSimon Schubert     return current_interpreter->quiet_p;
3415796c8dcSSimon Schubert }
3425796c8dcSSimon Schubert 
3435796c8dcSSimon Schubert static int
interp_set_quiet(struct interp * interp,int quiet)3445796c8dcSSimon Schubert interp_set_quiet (struct interp *interp, int quiet)
3455796c8dcSSimon Schubert {
3465796c8dcSSimon Schubert   int old_val = interp->quiet_p;
347cf7f2e2dSJohn Marino 
3485796c8dcSSimon Schubert   interp->quiet_p = quiet;
3495796c8dcSSimon Schubert   return old_val;
3505796c8dcSSimon Schubert }
3515796c8dcSSimon Schubert 
3525796c8dcSSimon Schubert /* interp_exec - This executes COMMAND_STR in the current
3535796c8dcSSimon Schubert    interpreter.  */
3545796c8dcSSimon Schubert int
interp_exec_p(struct interp * interp)3555796c8dcSSimon Schubert interp_exec_p (struct interp *interp)
3565796c8dcSSimon Schubert {
3575796c8dcSSimon Schubert   return interp->procs->exec_proc != NULL;
3585796c8dcSSimon Schubert }
3595796c8dcSSimon Schubert 
3605796c8dcSSimon Schubert struct gdb_exception
interp_exec(struct interp * interp,const char * command_str)3615796c8dcSSimon Schubert interp_exec (struct interp *interp, const char *command_str)
3625796c8dcSSimon Schubert {
3635796c8dcSSimon Schubert   if (interp->procs->exec_proc != NULL)
3645796c8dcSSimon Schubert     {
3655796c8dcSSimon Schubert       return interp->procs->exec_proc (interp->data, command_str);
3665796c8dcSSimon Schubert     }
3675796c8dcSSimon Schubert   return exception_none;
3685796c8dcSSimon Schubert }
3695796c8dcSSimon Schubert 
3705796c8dcSSimon Schubert /* A convenience routine that nulls out all the common command hooks.
3715796c8dcSSimon Schubert    Use it when removing your interpreter in its suspend proc.  */
3725796c8dcSSimon Schubert void
clear_interpreter_hooks(void)3735796c8dcSSimon Schubert clear_interpreter_hooks (void)
3745796c8dcSSimon Schubert {
3755796c8dcSSimon Schubert   deprecated_init_ui_hook = 0;
3765796c8dcSSimon Schubert   deprecated_print_frame_info_listing_hook = 0;
3775796c8dcSSimon Schubert   /*print_frame_more_info_hook = 0; */
3785796c8dcSSimon Schubert   deprecated_query_hook = 0;
3795796c8dcSSimon Schubert   deprecated_warning_hook = 0;
3805796c8dcSSimon Schubert   deprecated_interactive_hook = 0;
3815796c8dcSSimon Schubert   deprecated_readline_begin_hook = 0;
3825796c8dcSSimon Schubert   deprecated_readline_hook = 0;
3835796c8dcSSimon Schubert   deprecated_readline_end_hook = 0;
3845796c8dcSSimon Schubert   deprecated_register_changed_hook = 0;
3855796c8dcSSimon Schubert   deprecated_context_hook = 0;
3865796c8dcSSimon Schubert   deprecated_target_wait_hook = 0;
3875796c8dcSSimon Schubert   deprecated_call_command_hook = 0;
3885796c8dcSSimon Schubert   deprecated_error_begin_hook = 0;
3895796c8dcSSimon Schubert   deprecated_command_loop_hook = 0;
3905796c8dcSSimon Schubert }
3915796c8dcSSimon Schubert 
3925796c8dcSSimon Schubert /* This is a lazy init routine, called the first time the interpreter
3935796c8dcSSimon Schubert    module is used.  I put it here just in case, but I haven't thought
3945796c8dcSSimon Schubert    of a use for it yet.  I will probably bag it soon, since I don't
3955796c8dcSSimon Schubert    think it will be necessary.  */
3965796c8dcSSimon Schubert static void
initialize_interps(void)3975796c8dcSSimon Schubert initialize_interps (void)
3985796c8dcSSimon Schubert {
3995796c8dcSSimon Schubert   interpreter_initialized = 1;
4005796c8dcSSimon Schubert   /* Don't know if anything needs to be done here...  */
4015796c8dcSSimon Schubert }
4025796c8dcSSimon Schubert 
4035796c8dcSSimon Schubert static void
interpreter_exec_cmd(char * args,int from_tty)4045796c8dcSSimon Schubert interpreter_exec_cmd (char *args, int from_tty)
4055796c8dcSSimon Schubert {
4065796c8dcSSimon Schubert   struct interp *old_interp, *interp_to_use;
4075796c8dcSSimon Schubert   char **prules = NULL;
4085796c8dcSSimon Schubert   char **trule = NULL;
4095796c8dcSSimon Schubert   unsigned int nrules;
4105796c8dcSSimon Schubert   unsigned int i;
4115796c8dcSSimon Schubert   int old_quiet, use_quiet;
4125796c8dcSSimon Schubert 
4135796c8dcSSimon Schubert   if (args == NULL)
4145796c8dcSSimon Schubert     error_no_arg (_("interpreter-exec command"));
4155796c8dcSSimon Schubert 
4165796c8dcSSimon Schubert   prules = gdb_buildargv (args);
4175796c8dcSSimon Schubert   make_cleanup_freeargv (prules);
4185796c8dcSSimon Schubert 
4195796c8dcSSimon Schubert   nrules = 0;
4205796c8dcSSimon Schubert   for (trule = prules; *trule != NULL; trule++)
4215796c8dcSSimon Schubert     nrules++;
4225796c8dcSSimon Schubert 
4235796c8dcSSimon Schubert   if (nrules < 2)
4245796c8dcSSimon Schubert     error (_("usage: interpreter-exec <interpreter> [ <command> ... ]"));
4255796c8dcSSimon Schubert 
4265796c8dcSSimon Schubert   old_interp = current_interpreter;
4275796c8dcSSimon Schubert 
4285796c8dcSSimon Schubert   interp_to_use = interp_lookup (prules[0]);
4295796c8dcSSimon Schubert   if (interp_to_use == NULL)
4305796c8dcSSimon Schubert     error (_("Could not find interpreter \"%s\"."), prules[0]);
4315796c8dcSSimon Schubert 
432c50c785cSJohn Marino   /* Temporarily set interpreters quiet.  */
4335796c8dcSSimon Schubert   old_quiet = interp_set_quiet (old_interp, 1);
4345796c8dcSSimon Schubert   use_quiet = interp_set_quiet (interp_to_use, 1);
4355796c8dcSSimon Schubert 
4365796c8dcSSimon Schubert   if (!interp_set (interp_to_use, 0))
4375796c8dcSSimon Schubert     error (_("Could not switch to interpreter \"%s\"."), prules[0]);
4385796c8dcSSimon Schubert 
4395796c8dcSSimon Schubert   for (i = 1; i < nrules; i++)
4405796c8dcSSimon Schubert     {
4415796c8dcSSimon Schubert       struct gdb_exception e = interp_exec (interp_to_use, prules[i]);
442cf7f2e2dSJohn Marino 
4435796c8dcSSimon Schubert       if (e.reason < 0)
4445796c8dcSSimon Schubert 	{
4455796c8dcSSimon Schubert 	  interp_set (old_interp, 0);
4465796c8dcSSimon Schubert 	  interp_set_quiet (interp_to_use, use_quiet);
4475796c8dcSSimon Schubert 	  interp_set_quiet (old_interp, old_quiet);
4485796c8dcSSimon Schubert 	  error (_("error in command: \"%s\"."), prules[i]);
4495796c8dcSSimon Schubert 	}
4505796c8dcSSimon Schubert     }
4515796c8dcSSimon Schubert 
4525796c8dcSSimon Schubert   interp_set (old_interp, 0);
4535796c8dcSSimon Schubert   interp_set_quiet (interp_to_use, use_quiet);
4545796c8dcSSimon Schubert   interp_set_quiet (old_interp, old_quiet);
4555796c8dcSSimon Schubert }
4565796c8dcSSimon Schubert 
4575796c8dcSSimon Schubert /* List the possible interpreters which could complete the given text.  */
VEC(char_ptr)458*ef5ccd6cSJohn Marino static VEC (char_ptr) *
4595796c8dcSSimon Schubert interpreter_completer (struct cmd_list_element *ignore, char *text, char *word)
4605796c8dcSSimon Schubert {
4615796c8dcSSimon Schubert   int textlen;
462*ef5ccd6cSJohn Marino   VEC (char_ptr) *matches = NULL;
4635796c8dcSSimon Schubert   struct interp *interp;
4645796c8dcSSimon Schubert 
4655796c8dcSSimon Schubert   textlen = strlen (text);
4665796c8dcSSimon Schubert   for (interp = interp_list; interp != NULL; interp = interp->next)
4675796c8dcSSimon Schubert     {
4685796c8dcSSimon Schubert       if (strncmp (interp->name, text, textlen) == 0)
4695796c8dcSSimon Schubert 	{
470*ef5ccd6cSJohn Marino 	  char *match;
471*ef5ccd6cSJohn Marino 
472*ef5ccd6cSJohn Marino 	  match = (char *) xmalloc (strlen (word) + strlen (interp->name) + 1);
4735796c8dcSSimon Schubert 	  if (word == text)
474*ef5ccd6cSJohn Marino 	    strcpy (match, interp->name);
4755796c8dcSSimon Schubert 	  else if (word > text)
4765796c8dcSSimon Schubert 	    {
477c50c785cSJohn Marino 	      /* Return some portion of interp->name.  */
478*ef5ccd6cSJohn Marino 	      strcpy (match, interp->name + (word - text));
4795796c8dcSSimon Schubert 	    }
4805796c8dcSSimon Schubert 	  else
4815796c8dcSSimon Schubert 	    {
482c50c785cSJohn Marino 	      /* Return some of text plus interp->name.  */
483*ef5ccd6cSJohn Marino 	      strncpy (match, word, text - word);
484*ef5ccd6cSJohn Marino 	      match[text - word] = '\0';
485*ef5ccd6cSJohn Marino 	      strcat (match, interp->name);
4865796c8dcSSimon Schubert 	    }
487*ef5ccd6cSJohn Marino 	  VEC_safe_push (char_ptr, matches, match);
4885796c8dcSSimon Schubert 	}
4895796c8dcSSimon Schubert     }
4905796c8dcSSimon Schubert 
4915796c8dcSSimon Schubert   return matches;
4925796c8dcSSimon Schubert }
4935796c8dcSSimon Schubert 
4945796c8dcSSimon Schubert struct interp *
top_level_interpreter(void)4955796c8dcSSimon Schubert top_level_interpreter (void)
4965796c8dcSSimon Schubert {
4975796c8dcSSimon Schubert   return top_level_interpreter_ptr;
4985796c8dcSSimon Schubert }
4995796c8dcSSimon Schubert 
5005796c8dcSSimon Schubert void *
top_level_interpreter_data(void)5015796c8dcSSimon Schubert top_level_interpreter_data (void)
5025796c8dcSSimon Schubert {
5035796c8dcSSimon Schubert   gdb_assert (top_level_interpreter_ptr);
5045796c8dcSSimon Schubert   return top_level_interpreter_ptr->data;
5055796c8dcSSimon Schubert }
5065796c8dcSSimon Schubert 
5075796c8dcSSimon Schubert /* This just adds the "interpreter-exec" command.  */
5085796c8dcSSimon Schubert void
_initialize_interpreter(void)5095796c8dcSSimon Schubert _initialize_interpreter (void)
5105796c8dcSSimon Schubert {
5115796c8dcSSimon Schubert   struct cmd_list_element *c;
5125796c8dcSSimon Schubert 
5135796c8dcSSimon Schubert   c = add_cmd ("interpreter-exec", class_support,
5145796c8dcSSimon Schubert 	       interpreter_exec_cmd, _("\
5155796c8dcSSimon Schubert Execute a command in an interpreter.  It takes two arguments:\n\
5165796c8dcSSimon Schubert The first argument is the name of the interpreter to use.\n\
5175796c8dcSSimon Schubert The second argument is the command to execute.\n"), &cmdlist);
5185796c8dcSSimon Schubert   set_cmd_completer (c, interpreter_completer);
5195796c8dcSSimon Schubert }
520