15796c8dcSSimon Schubert /* Manages interpreters for GDB, the GNU debugger. 25796c8dcSSimon Schubert 3*a45ae5f8SJohn Marino Copyright (C) 2000, 2002-2003, 2007-2012 Free Software Foundation, 4*a45ae5f8SJohn Marino 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" 44*a45ae5f8SJohn Marino #include "continuations.h" 45*a45ae5f8SJohn Marino 46*a45ae5f8SJohn Marino /* True if the current interpreter in is async mode. See interps.h 47*a45ae5f8SJohn Marino for more details. This starts out disabled, until all the explicit 48*a45ae5f8SJohn Marino command line arguments (e.g., `gdb -ex "start" -ex "next"') are 49*a45ae5f8SJohn Marino processed. */ 50*a45ae5f8SJohn Marino int interpreter_async = 0; 515796c8dcSSimon Schubert 525796c8dcSSimon Schubert struct interp 535796c8dcSSimon Schubert { 545796c8dcSSimon Schubert /* This is the name in "-i=" and set interpreter. */ 555796c8dcSSimon Schubert const char *name; 565796c8dcSSimon Schubert 575796c8dcSSimon Schubert /* Interpreters are stored in a linked list, this is the next 585796c8dcSSimon Schubert one... */ 595796c8dcSSimon Schubert struct interp *next; 605796c8dcSSimon Schubert 615796c8dcSSimon Schubert /* This is a cookie that an instance of the interpreter can use. 625796c8dcSSimon Schubert This is a bit confused right now as the exact initialization 635796c8dcSSimon Schubert sequence for it, and how it relates to the interpreter's uiout 645796c8dcSSimon Schubert object is a bit confused. */ 655796c8dcSSimon Schubert void *data; 665796c8dcSSimon Schubert 675796c8dcSSimon Schubert /* Has the init_proc been run? */ 685796c8dcSSimon Schubert int inited; 695796c8dcSSimon Schubert 705796c8dcSSimon Schubert const struct interp_procs *procs; 715796c8dcSSimon Schubert int quiet_p; 725796c8dcSSimon Schubert }; 735796c8dcSSimon Schubert 745796c8dcSSimon Schubert /* Functions local to this file. */ 755796c8dcSSimon Schubert static void initialize_interps (void); 765796c8dcSSimon Schubert static char **interpreter_completer (struct cmd_list_element *cmd, 775796c8dcSSimon Schubert char *text, char *word); 785796c8dcSSimon Schubert 795796c8dcSSimon Schubert /* The magic initialization routine for this module. */ 805796c8dcSSimon Schubert 815796c8dcSSimon Schubert void _initialize_interpreter (void); 825796c8dcSSimon Schubert 835796c8dcSSimon Schubert /* Variables local to this file: */ 845796c8dcSSimon Schubert 855796c8dcSSimon Schubert static struct interp *interp_list = NULL; 865796c8dcSSimon Schubert static struct interp *current_interpreter = NULL; 875796c8dcSSimon Schubert static struct interp *top_level_interpreter_ptr = NULL; 885796c8dcSSimon Schubert 895796c8dcSSimon Schubert static int interpreter_initialized = 0; 905796c8dcSSimon Schubert 915796c8dcSSimon Schubert /* interp_new - This allocates space for a new interpreter, 925796c8dcSSimon Schubert fills the fields from the inputs, and returns a pointer to the 935796c8dcSSimon Schubert interpreter. */ 945796c8dcSSimon Schubert struct interp * 95*a45ae5f8SJohn Marino interp_new (const char *name, const struct interp_procs *procs) 965796c8dcSSimon Schubert { 975796c8dcSSimon Schubert struct interp *new_interp; 985796c8dcSSimon Schubert 995796c8dcSSimon Schubert new_interp = XMALLOC (struct interp); 1005796c8dcSSimon Schubert 1015796c8dcSSimon Schubert new_interp->name = xstrdup (name); 102*a45ae5f8SJohn Marino new_interp->data = NULL; 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 { 152*a45ae5f8SJohn Marino ui_out_flush (current_uiout); 1535796c8dcSSimon Schubert if (current_interpreter->procs->suspend_proc 1545796c8dcSSimon Schubert && !current_interpreter->procs->suspend_proc (current_interpreter-> 1555796c8dcSSimon Schubert data)) 1565796c8dcSSimon Schubert { 1575796c8dcSSimon Schubert error (_("Could not suspend interpreter \"%s\"."), 1585796c8dcSSimon Schubert current_interpreter->name); 1595796c8dcSSimon Schubert } 1605796c8dcSSimon Schubert } 1615796c8dcSSimon Schubert else 1625796c8dcSSimon Schubert { 1635796c8dcSSimon Schubert first_time = 1; 1645796c8dcSSimon Schubert } 1655796c8dcSSimon Schubert 1665796c8dcSSimon Schubert current_interpreter = interp; 1675796c8dcSSimon Schubert if (top_level) 1685796c8dcSSimon Schubert top_level_interpreter_ptr = interp; 1695796c8dcSSimon Schubert 1705796c8dcSSimon Schubert /* We use interpreter_p for the "set interpreter" variable, so we need 1715796c8dcSSimon Schubert to make sure we have a malloc'ed copy for the set command to free. */ 1725796c8dcSSimon Schubert if (interpreter_p != NULL 1735796c8dcSSimon Schubert && strcmp (current_interpreter->name, interpreter_p) != 0) 1745796c8dcSSimon Schubert { 1755796c8dcSSimon Schubert xfree (interpreter_p); 1765796c8dcSSimon Schubert 1775796c8dcSSimon Schubert interpreter_p = xstrdup (current_interpreter->name); 1785796c8dcSSimon Schubert } 1795796c8dcSSimon Schubert 1805796c8dcSSimon Schubert /* Run the init proc. If it fails, try to restore the old interp. */ 1815796c8dcSSimon Schubert 1825796c8dcSSimon Schubert if (!interp->inited) 1835796c8dcSSimon Schubert { 1845796c8dcSSimon Schubert if (interp->procs->init_proc != NULL) 1855796c8dcSSimon Schubert { 186*a45ae5f8SJohn Marino interp->data = interp->procs->init_proc (interp, top_level); 1875796c8dcSSimon Schubert } 1885796c8dcSSimon Schubert interp->inited = 1; 1895796c8dcSSimon Schubert } 1905796c8dcSSimon Schubert 191*a45ae5f8SJohn Marino /* Do this only after the interpreter is initialized. */ 192*a45ae5f8SJohn Marino current_uiout = interp->procs->ui_out_proc (interp); 193*a45ae5f8SJohn Marino 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); 217*a45ae5f8SJohn Marino ui_out_text (current_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) 251*a45ae5f8SJohn Marino return interp->procs->ui_out_proc (interp); 2525796c8dcSSimon Schubert 253*a45ae5f8SJohn Marino return current_interpreter->procs->ui_out_proc (current_interpreter); 254*a45ae5f8SJohn Marino } 255*a45ae5f8SJohn Marino 256*a45ae5f8SJohn Marino /* Returns the interpreter's cookie. */ 257*a45ae5f8SJohn Marino 258*a45ae5f8SJohn Marino void * 259*a45ae5f8SJohn Marino interp_data (struct interp *interp) 260*a45ae5f8SJohn Marino { 261*a45ae5f8SJohn Marino return interp->data; 262*a45ae5f8SJohn Marino } 263*a45ae5f8SJohn Marino 264*a45ae5f8SJohn Marino /* Returns the interpreter's name. */ 265*a45ae5f8SJohn Marino 266*a45ae5f8SJohn Marino const char * 267*a45ae5f8SJohn Marino interp_name (struct interp *interp) 268*a45ae5f8SJohn Marino { 269*a45ae5f8SJohn Marino return interp->name; 2705796c8dcSSimon Schubert } 2715796c8dcSSimon Schubert 2725796c8dcSSimon Schubert /* Returns true if the current interp is the passed in name. */ 2735796c8dcSSimon Schubert int 2745796c8dcSSimon Schubert current_interp_named_p (const char *interp_name) 2755796c8dcSSimon Schubert { 2765796c8dcSSimon Schubert if (current_interpreter) 2775796c8dcSSimon Schubert return (strcmp (current_interpreter->name, interp_name) == 0); 2785796c8dcSSimon Schubert 2795796c8dcSSimon Schubert return 0; 2805796c8dcSSimon Schubert } 2815796c8dcSSimon Schubert 2825796c8dcSSimon Schubert /* This is called in display_gdb_prompt. If the proc returns a zero 2835796c8dcSSimon Schubert value, display_gdb_prompt will return without displaying the 2845796c8dcSSimon Schubert prompt. */ 2855796c8dcSSimon Schubert int 2865796c8dcSSimon Schubert current_interp_display_prompt_p (void) 2875796c8dcSSimon Schubert { 2885796c8dcSSimon Schubert if (current_interpreter == NULL 2895796c8dcSSimon Schubert || current_interpreter->procs->prompt_proc_p == NULL) 2905796c8dcSSimon Schubert return 0; 2915796c8dcSSimon Schubert else 2925796c8dcSSimon Schubert return current_interpreter->procs->prompt_proc_p (current_interpreter-> 2935796c8dcSSimon Schubert data); 2945796c8dcSSimon Schubert } 2955796c8dcSSimon Schubert 2965796c8dcSSimon Schubert /* Run the current command interpreter's main loop. */ 2975796c8dcSSimon Schubert void 2985796c8dcSSimon Schubert current_interp_command_loop (void) 2995796c8dcSSimon Schubert { 3005796c8dcSSimon Schubert /* Somewhat messy. For the moment prop up all the old ways of 3015796c8dcSSimon Schubert selecting the command loop. `deprecated_command_loop_hook' 3025796c8dcSSimon Schubert should be deprecated. */ 3035796c8dcSSimon Schubert if (deprecated_command_loop_hook != NULL) 3045796c8dcSSimon Schubert deprecated_command_loop_hook (); 3055796c8dcSSimon Schubert else if (current_interpreter != NULL 3065796c8dcSSimon Schubert && current_interpreter->procs->command_loop_proc != NULL) 3075796c8dcSSimon Schubert current_interpreter->procs->command_loop_proc (current_interpreter->data); 3085796c8dcSSimon Schubert else 3095796c8dcSSimon Schubert cli_command_loop (); 3105796c8dcSSimon Schubert } 3115796c8dcSSimon Schubert 3125796c8dcSSimon Schubert int 3135796c8dcSSimon Schubert interp_quiet_p (struct interp *interp) 3145796c8dcSSimon Schubert { 3155796c8dcSSimon Schubert if (interp != NULL) 3165796c8dcSSimon Schubert return interp->quiet_p; 3175796c8dcSSimon Schubert else 3185796c8dcSSimon Schubert return current_interpreter->quiet_p; 3195796c8dcSSimon Schubert } 3205796c8dcSSimon Schubert 3215796c8dcSSimon Schubert static int 3225796c8dcSSimon Schubert interp_set_quiet (struct interp *interp, int quiet) 3235796c8dcSSimon Schubert { 3245796c8dcSSimon Schubert int old_val = interp->quiet_p; 325cf7f2e2dSJohn Marino 3265796c8dcSSimon Schubert interp->quiet_p = quiet; 3275796c8dcSSimon Schubert return old_val; 3285796c8dcSSimon Schubert } 3295796c8dcSSimon Schubert 3305796c8dcSSimon Schubert /* interp_exec - This executes COMMAND_STR in the current 3315796c8dcSSimon Schubert interpreter. */ 3325796c8dcSSimon Schubert int 3335796c8dcSSimon Schubert interp_exec_p (struct interp *interp) 3345796c8dcSSimon Schubert { 3355796c8dcSSimon Schubert return interp->procs->exec_proc != NULL; 3365796c8dcSSimon Schubert } 3375796c8dcSSimon Schubert 3385796c8dcSSimon Schubert struct gdb_exception 3395796c8dcSSimon Schubert interp_exec (struct interp *interp, const char *command_str) 3405796c8dcSSimon Schubert { 3415796c8dcSSimon Schubert if (interp->procs->exec_proc != NULL) 3425796c8dcSSimon Schubert { 3435796c8dcSSimon Schubert return interp->procs->exec_proc (interp->data, command_str); 3445796c8dcSSimon Schubert } 3455796c8dcSSimon Schubert return exception_none; 3465796c8dcSSimon Schubert } 3475796c8dcSSimon Schubert 3485796c8dcSSimon Schubert /* A convenience routine that nulls out all the common command hooks. 3495796c8dcSSimon Schubert Use it when removing your interpreter in its suspend proc. */ 3505796c8dcSSimon Schubert void 3515796c8dcSSimon Schubert clear_interpreter_hooks (void) 3525796c8dcSSimon Schubert { 3535796c8dcSSimon Schubert deprecated_init_ui_hook = 0; 3545796c8dcSSimon Schubert deprecated_print_frame_info_listing_hook = 0; 3555796c8dcSSimon Schubert /*print_frame_more_info_hook = 0; */ 3565796c8dcSSimon Schubert deprecated_query_hook = 0; 3575796c8dcSSimon Schubert deprecated_warning_hook = 0; 3585796c8dcSSimon Schubert deprecated_interactive_hook = 0; 3595796c8dcSSimon Schubert deprecated_readline_begin_hook = 0; 3605796c8dcSSimon Schubert deprecated_readline_hook = 0; 3615796c8dcSSimon Schubert deprecated_readline_end_hook = 0; 3625796c8dcSSimon Schubert deprecated_register_changed_hook = 0; 3635796c8dcSSimon Schubert deprecated_context_hook = 0; 3645796c8dcSSimon Schubert deprecated_target_wait_hook = 0; 3655796c8dcSSimon Schubert deprecated_call_command_hook = 0; 3665796c8dcSSimon Schubert deprecated_error_begin_hook = 0; 3675796c8dcSSimon Schubert deprecated_command_loop_hook = 0; 3685796c8dcSSimon Schubert } 3695796c8dcSSimon Schubert 3705796c8dcSSimon Schubert /* This is a lazy init routine, called the first time the interpreter 3715796c8dcSSimon Schubert module is used. I put it here just in case, but I haven't thought 3725796c8dcSSimon Schubert of a use for it yet. I will probably bag it soon, since I don't 3735796c8dcSSimon Schubert think it will be necessary. */ 3745796c8dcSSimon Schubert static void 3755796c8dcSSimon Schubert initialize_interps (void) 3765796c8dcSSimon Schubert { 3775796c8dcSSimon Schubert interpreter_initialized = 1; 3785796c8dcSSimon Schubert /* Don't know if anything needs to be done here... */ 3795796c8dcSSimon Schubert } 3805796c8dcSSimon Schubert 3815796c8dcSSimon Schubert static void 3825796c8dcSSimon Schubert interpreter_exec_cmd (char *args, int from_tty) 3835796c8dcSSimon Schubert { 3845796c8dcSSimon Schubert struct interp *old_interp, *interp_to_use; 3855796c8dcSSimon Schubert char **prules = NULL; 3865796c8dcSSimon Schubert char **trule = NULL; 3875796c8dcSSimon Schubert unsigned int nrules; 3885796c8dcSSimon Schubert unsigned int i; 3895796c8dcSSimon Schubert int old_quiet, use_quiet; 3905796c8dcSSimon Schubert 3915796c8dcSSimon Schubert if (args == NULL) 3925796c8dcSSimon Schubert error_no_arg (_("interpreter-exec command")); 3935796c8dcSSimon Schubert 3945796c8dcSSimon Schubert prules = gdb_buildargv (args); 3955796c8dcSSimon Schubert make_cleanup_freeargv (prules); 3965796c8dcSSimon Schubert 3975796c8dcSSimon Schubert nrules = 0; 3985796c8dcSSimon Schubert for (trule = prules; *trule != NULL; trule++) 3995796c8dcSSimon Schubert nrules++; 4005796c8dcSSimon Schubert 4015796c8dcSSimon Schubert if (nrules < 2) 4025796c8dcSSimon Schubert error (_("usage: interpreter-exec <interpreter> [ <command> ... ]")); 4035796c8dcSSimon Schubert 4045796c8dcSSimon Schubert old_interp = current_interpreter; 4055796c8dcSSimon Schubert 4065796c8dcSSimon Schubert interp_to_use = interp_lookup (prules[0]); 4075796c8dcSSimon Schubert if (interp_to_use == NULL) 4085796c8dcSSimon Schubert error (_("Could not find interpreter \"%s\"."), prules[0]); 4095796c8dcSSimon Schubert 410c50c785cSJohn Marino /* Temporarily set interpreters quiet. */ 4115796c8dcSSimon Schubert old_quiet = interp_set_quiet (old_interp, 1); 4125796c8dcSSimon Schubert use_quiet = interp_set_quiet (interp_to_use, 1); 4135796c8dcSSimon Schubert 4145796c8dcSSimon Schubert if (!interp_set (interp_to_use, 0)) 4155796c8dcSSimon Schubert error (_("Could not switch to interpreter \"%s\"."), prules[0]); 4165796c8dcSSimon Schubert 4175796c8dcSSimon Schubert for (i = 1; i < nrules; i++) 4185796c8dcSSimon Schubert { 4195796c8dcSSimon Schubert struct gdb_exception e = interp_exec (interp_to_use, prules[i]); 420cf7f2e2dSJohn Marino 4215796c8dcSSimon Schubert if (e.reason < 0) 4225796c8dcSSimon Schubert { 4235796c8dcSSimon Schubert interp_set (old_interp, 0); 4245796c8dcSSimon Schubert interp_set_quiet (interp_to_use, use_quiet); 4255796c8dcSSimon Schubert interp_set_quiet (old_interp, old_quiet); 4265796c8dcSSimon Schubert error (_("error in command: \"%s\"."), prules[i]); 4275796c8dcSSimon Schubert } 4285796c8dcSSimon Schubert } 4295796c8dcSSimon Schubert 4305796c8dcSSimon Schubert interp_set (old_interp, 0); 4315796c8dcSSimon Schubert interp_set_quiet (interp_to_use, use_quiet); 4325796c8dcSSimon Schubert interp_set_quiet (old_interp, old_quiet); 4335796c8dcSSimon Schubert } 4345796c8dcSSimon Schubert 4355796c8dcSSimon Schubert /* List the possible interpreters which could complete the given text. */ 4365796c8dcSSimon Schubert static char ** 4375796c8dcSSimon Schubert interpreter_completer (struct cmd_list_element *ignore, char *text, char *word) 4385796c8dcSSimon Schubert { 4395796c8dcSSimon Schubert int alloced = 0; 4405796c8dcSSimon Schubert int textlen; 4415796c8dcSSimon Schubert int num_matches; 4425796c8dcSSimon Schubert char **matches; 4435796c8dcSSimon Schubert struct interp *interp; 4445796c8dcSSimon Schubert 4455796c8dcSSimon Schubert /* We expect only a very limited number of interpreters, so just 4465796c8dcSSimon Schubert allocate room for all of them plus one for the last that must be NULL 4475796c8dcSSimon Schubert to correctly end the list. */ 4485796c8dcSSimon Schubert for (interp = interp_list; interp != NULL; interp = interp->next) 4495796c8dcSSimon Schubert ++alloced; 4505796c8dcSSimon Schubert matches = (char **) xcalloc (alloced + 1, sizeof (char *)); 4515796c8dcSSimon Schubert 4525796c8dcSSimon Schubert num_matches = 0; 4535796c8dcSSimon Schubert textlen = strlen (text); 4545796c8dcSSimon Schubert for (interp = interp_list; interp != NULL; interp = interp->next) 4555796c8dcSSimon Schubert { 4565796c8dcSSimon Schubert if (strncmp (interp->name, text, textlen) == 0) 4575796c8dcSSimon Schubert { 4585796c8dcSSimon Schubert matches[num_matches] = 4595796c8dcSSimon Schubert (char *) xmalloc (strlen (word) + strlen (interp->name) + 1); 4605796c8dcSSimon Schubert if (word == text) 4615796c8dcSSimon Schubert strcpy (matches[num_matches], interp->name); 4625796c8dcSSimon Schubert else if (word > text) 4635796c8dcSSimon Schubert { 464c50c785cSJohn Marino /* Return some portion of interp->name. */ 4655796c8dcSSimon Schubert strcpy (matches[num_matches], interp->name + (word - text)); 4665796c8dcSSimon Schubert } 4675796c8dcSSimon Schubert else 4685796c8dcSSimon Schubert { 469c50c785cSJohn Marino /* Return some of text plus interp->name. */ 4705796c8dcSSimon Schubert strncpy (matches[num_matches], word, text - word); 4715796c8dcSSimon Schubert matches[num_matches][text - word] = '\0'; 4725796c8dcSSimon Schubert strcat (matches[num_matches], interp->name); 4735796c8dcSSimon Schubert } 4745796c8dcSSimon Schubert ++num_matches; 4755796c8dcSSimon Schubert } 4765796c8dcSSimon Schubert } 4775796c8dcSSimon Schubert 4785796c8dcSSimon Schubert if (num_matches == 0) 4795796c8dcSSimon Schubert { 4805796c8dcSSimon Schubert xfree (matches); 4815796c8dcSSimon Schubert matches = NULL; 4825796c8dcSSimon Schubert } 4835796c8dcSSimon Schubert 4845796c8dcSSimon Schubert return matches; 4855796c8dcSSimon Schubert } 4865796c8dcSSimon Schubert 4875796c8dcSSimon Schubert struct interp * 4885796c8dcSSimon Schubert top_level_interpreter (void) 4895796c8dcSSimon Schubert { 4905796c8dcSSimon Schubert return top_level_interpreter_ptr; 4915796c8dcSSimon Schubert } 4925796c8dcSSimon Schubert 4935796c8dcSSimon Schubert void * 4945796c8dcSSimon Schubert top_level_interpreter_data (void) 4955796c8dcSSimon Schubert { 4965796c8dcSSimon Schubert gdb_assert (top_level_interpreter_ptr); 4975796c8dcSSimon Schubert return top_level_interpreter_ptr->data; 4985796c8dcSSimon Schubert } 4995796c8dcSSimon Schubert 5005796c8dcSSimon Schubert /* This just adds the "interpreter-exec" command. */ 5015796c8dcSSimon Schubert void 5025796c8dcSSimon Schubert _initialize_interpreter (void) 5035796c8dcSSimon Schubert { 5045796c8dcSSimon Schubert struct cmd_list_element *c; 5055796c8dcSSimon Schubert 5065796c8dcSSimon Schubert c = add_cmd ("interpreter-exec", class_support, 5075796c8dcSSimon Schubert interpreter_exec_cmd, _("\ 5085796c8dcSSimon Schubert Execute a command in an interpreter. It takes two arguments:\n\ 5095796c8dcSSimon Schubert The first argument is the name of the interpreter to use.\n\ 5105796c8dcSSimon Schubert The second argument is the command to execute.\n"), &cmdlist); 5115796c8dcSSimon Schubert set_cmd_completer (c, interpreter_completer); 5125796c8dcSSimon Schubert } 513