1119418Sobrien/* Manages interpreters for GDB, the GNU debugger. 274534Scokane 361931Scokane Copyright 2000, 2002, 2003 Free Software Foundation, Inc. 461931Scokane 561931Scokane Written by Jim Ingham <jingham@apple.com> of Apple Computer, Inc. 661931Scokane 761931Scokane This file is part of GDB. 861931Scokane 961931Scokane This program is free software; you can redistribute it and/or modify 1061931Scokane it under the terms of the GNU General Public License as published by 1161931Scokane the Free Software Foundation; either version 2 of the License, or 1261931Scokane (at your option) any later version. 1361931Scokane 1461931Scokane This program is distributed in the hope that it will be useful, 1561931Scokane but WITHOUT ANY WARRANTY; without even the implied warranty of 1661931Scokane MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1761931Scokane GNU General Public License for more details. 1861931Scokane 1961931Scokane You should have received a copy of the GNU General Public License 2061931Scokane along with this program; if not, write to the Free Software 2161931Scokane Foundation, Inc., 59 Temple Place - Suite 330, 2261931Scokane Boston, MA 02111-1307, USA. */ 2361931Scokane 2461931Scokane/* This is just a first cut at separating out the "interpreter" 2561931Scokane functions of gdb into self-contained modules. There are a couple 2661931Scokane of open areas that need to be sorted out: 2761931Scokane 2861931Scokane 1) The interpreter explicitly contains a UI_OUT, and can insert itself 2961931Scokane into the event loop, but it doesn't explicitly contain hooks for readline. 3061931Scokane I did this because it seems to me many interpreters won't want to use 3161931Scokane the readline command interface, and it is probably simpler to just let 32119418Sobrien them take over the input in their resume proc. */ 33119418Sobrien 34119418Sobrien#include "defs.h" 3561911Scokane#include "gdbcmd.h" 3661911Scokane#include "ui-out.h" 3774534Scokane#include "event-loop.h" 3861911Scokane#include "event-top.h" 3961911Scokane#include "interps.h" 4061911Scokane#include "completer.h" 4161911Scokane#include "gdb_string.h" 4261911Scokane#include "gdb-events.h" 4361911Scokane#include "gdb_assert.h" 4461911Scokane#include "top.h" /* For command_loop. */ 4561911Scokane 4661911Scokanestruct interp 4761911Scokane{ 4861911Scokane /* This is the name in "-i=" and set interpreter. */ 4961911Scokane const char *name; 5061911Scokane 5161911Scokane /* Interpreters are stored in a linked list, this is the next 5261911Scokane one... */ 5361911Scokane struct interp *next; 5461911Scokane 5561911Scokane /* This is a cookie that an instance of the interpreter can use. 5661911Scokane This is a bit confused right now as the exact initialization 5761911Scokane sequence for it, and how it relates to the interpreter's uiout 5861911Scokane object is a bit confused. */ 59119287Simp void *data; 60119287Simp 6161911Scokane /* Has the init_proc been run? */ 6261911Scokane int inited; 6361911Scokane 6461911Scokane /* This is the ui_out used to collect results for this interpreter. 6561911Scokane It can be a formatter for stdout, as is the case for the console 6661911Scokane & mi outputs, or it might be a result formatter. */ 6761911Scokane struct ui_out *interpreter_out; 6861911Scokane 6961911Scokane const struct interp_procs *procs; 7061911Scokane int quiet_p; 7162028Scokane}; 7262028Scokane 7362028Scokane/* Functions local to this file. */ 7461931Scokanestatic void initialize_interps (void); 7561931Scokanestatic char **interpreter_completer (char *text, char *word); 7661931Scokane 7761911Scokane/* The magic initialization routine for this module. */ 7861911Scokane 7961911Scokanevoid _initialize_interpreter (void); 8061911Scokane 8161911Scokane/* Variables local to this file: */ 8261911Scokane 8361911Scokanestatic struct interp *interp_list = NULL; 8461911Scokanestatic struct interp *current_interpreter = NULL; 8561911Scokane 8661911Scokanestatic int interpreter_initialized = 0; 8761911Scokane 8861911Scokane/* interp_new - This allocates space for a new interpreter, 8961911Scokane fills the fields from the inputs, and returns a pointer to the 9061911Scokane interpreter. */ 9161911Scokanestruct interp * 9261911Scokaneinterp_new (const char *name, void *data, struct ui_out *uiout, 9361911Scokane const struct interp_procs *procs) 9461911Scokane{ 9561911Scokane struct interp *new_interp; 9661911Scokane 9761911Scokane new_interp = XMALLOC (struct interp); 9861911Scokane 9961911Scokane new_interp->name = xstrdup (name); 10061931Scokane new_interp->data = data; 10161989Scokane new_interp->interpreter_out = uiout; 10261931Scokane new_interp->quiet_p = 0; 10361931Scokane new_interp->procs = procs; 10461931Scokane new_interp->inited = 0; 10561911Scokane 10661911Scokane return new_interp; 107126080Sphk} 108126080Sphk 109111815Sphk/* Add interpreter INTERP to the gdb interpreter list. The 110111815Sphk interpreter must not have previously been added. */ 111111815Sphkvoid 112111815Sphkinterp_add (struct interp *interp) 113111815Sphk{ 11461911Scokane if (!interpreter_initialized) 11561911Scokane initialize_interps (); 11661911Scokane 11761911Scokane gdb_assert (interp_lookup (interp->name) == NULL); 11861911Scokane 11961911Scokane interp->next = interp_list; 12061911Scokane interp_list = interp; 12161911Scokane} 12261911Scokane 12361911Scokane/* This sets the current interpreter to be INTERP. If INTERP has not 12461911Scokane been initialized, then this will also run the init proc. If the 12561911Scokane init proc is successful, return 1, if it fails, set the old 12661911Scokane interpreter back in place and return 0. If we can't restore the 12761911Scokane old interpreter, then raise an internal error, since we are in 12861911Scokane pretty bad shape at this point. */ 12961911Scokaneint 13061911Scokaneinterp_set (struct interp *interp) 13161911Scokane{ 13265146Scokane struct interp *old_interp = current_interpreter; 13361911Scokane int first_time = 0; 13461911Scokane 13561911Scokane 13661911Scokane char buffer[64]; 13765146Scokane 13861911Scokane if (current_interpreter != NULL) 13961911Scokane { 14061911Scokane do_all_continuations (); 14161911Scokane ui_out_flush (uiout); 14261911Scokane if (current_interpreter->procs->suspend_proc 14361911Scokane && !current_interpreter->procs->suspend_proc (current_interpreter-> 14461911Scokane data)) 14561911Scokane { 14661911Scokane error ("Could not suspend interpreter \"%s\"\n", 14761911Scokane current_interpreter->name); 14861911Scokane } 14961911Scokane } 15061911Scokane else 15161911Scokane { 15261911Scokane first_time = 1; 15361911Scokane } 15461911Scokane 15561911Scokane current_interpreter = interp; 15661911Scokane 15761911Scokane /* We use interpreter_p for the "set interpreter" variable, so we need 15861911Scokane to make sure we have a malloc'ed copy for the set command to free. */ 15961911Scokane if (interpreter_p != NULL 16061911Scokane && strcmp (current_interpreter->name, interpreter_p) != 0) 16161911Scokane { 162119690Sjhb xfree (interpreter_p); 16361911Scokane 16461911Scokane interpreter_p = xstrdup (current_interpreter->name); 16561911Scokane } 16661911Scokane 16761911Scokane uiout = interp->interpreter_out; 16861911Scokane 16961911Scokane /* Run the init proc. If it fails, try to restore the old interp. */ 17061911Scokane 17161911Scokane if (!interp->inited) 17261911Scokane { 17361911Scokane if (interp->procs->init_proc != NULL) 17461911Scokane { 17561911Scokane interp->data = interp->procs->init_proc (); 17661911Scokane } 17761911Scokane interp->inited = 1; 17861911Scokane } 17961911Scokane 18061911Scokane /* Clear out any installed interpreter hooks/event handlers. */ 18161911Scokane clear_interpreter_hooks (); 18261911Scokane 18361911Scokane if (interp->procs->resume_proc != NULL 18461911Scokane && (!interp->procs->resume_proc (interp->data))) 18561911Scokane { 18661911Scokane if (old_interp == NULL || !interp_set (old_interp)) 18761911Scokane internal_error (__FILE__, __LINE__, 18861931Scokane "Failed to initialize new interp \"%s\" %s", 18961911Scokane interp->name, "and could not restore old interp!\n"); 19061911Scokane return 0; 19161911Scokane } 19261911Scokane 19361967Scokane /* Finally, put up the new prompt to show that we are indeed here. 19461911Scokane Also, display_gdb_prompt for the console does some readline magic 19561931Scokane which is needed for the console interpreter, at least... */ 19661911Scokane 19761911Scokane if (!first_time) 19861911Scokane { 19961911Scokane if (!interp_quiet_p (interp)) 20061911Scokane { 20161911Scokane sprintf (buffer, "Switching to interpreter \"%.24s\".\n", 20261931Scokane interp->name); 20361911Scokane ui_out_text (uiout, buffer); 20461911Scokane } 20561911Scokane display_gdb_prompt (NULL); 20661911Scokane } 20761911Scokane 20863488Scokane return 1; 20963488Scokane} 21063488Scokane 21164085Scokane/* interp_lookup - Looks up the interpreter for NAME. If no such 21263488Scokane interpreter exists, return NULL, otherwise return a pointer to the 21363488Scokane interpreter. */ 21463488Scokanestruct interp * 21563488Scokaneinterp_lookup (const char *name) 21663488Scokane{ 21763488Scokane struct interp *interp; 21863488Scokane 21963488Scokane if (name == NULL || strlen (name) == 0) 22063488Scokane return NULL; 22163488Scokane 22263488Scokane for (interp = interp_list; interp != NULL; interp = interp->next) 22363488Scokane { 22463488Scokane if (strcmp (interp->name, name) == 0) 22563488Scokane return interp; 22663488Scokane } 22763488Scokane 22865146Scokane return NULL; 22965146Scokane} 23065146Scokane 23163488Scokane/* Returns the current interpreter. */ 23263488Scokane 23363488Scokanestruct ui_out * 23463488Scokaneinterp_ui_out (struct interp *interp) 23563488Scokane{ 23663488Scokane if (interp != NULL) 23763488Scokane return interp->interpreter_out; 23863488Scokane 23963488Scokane return current_interpreter->interpreter_out; 24063488Scokane} 24165146Scokane 24263488Scokane/* Returns true if the current interp is the passed in name. */ 24363488Scokaneint 24463488Scokanecurrent_interp_named_p (const char *interp_name) 24563488Scokane{ 24663488Scokane if (current_interpreter) 24763488Scokane return (strcmp (current_interpreter->name, interp_name) == 0); 24861911Scokane 24961911Scokane return 0; 25061911Scokane} 25161911Scokane 25261911Scokane/* This is called in display_gdb_prompt. If the proc returns a zero 25361911Scokane value, display_gdb_prompt will return without displaying the 25461931Scokane prompt. */ 25561911Scokaneint 25661911Scokanecurrent_interp_display_prompt_p (void) 25761911Scokane{ 25861911Scokane if (current_interpreter == NULL 25963488Scokane || current_interpreter->procs->prompt_proc_p == NULL) 26061911Scokane return 0; 26161911Scokane else 26261911Scokane return current_interpreter->procs->prompt_proc_p (current_interpreter-> 26361911Scokane data); 26461911Scokane} 26561911Scokane 266104111Sphk/* Run the current command interpreter's main loop. */ 267108323Srwatsonvoid 26861911Scokanecurrent_interp_command_loop (void) 26961911Scokane{ 27061911Scokane /* Somewhat messy. For the moment prop up all the old ways of 27161911Scokane selecting the command loop. `command_loop_hook' should be 27261911Scokane deprecated. */ 27361911Scokane if (command_loop_hook != NULL) 27461911Scokane command_loop_hook (); 27561911Scokane else if (current_interpreter != NULL 27661911Scokane && current_interpreter->procs->command_loop_proc != NULL) 27761911Scokane current_interpreter->procs->command_loop_proc (current_interpreter->data); 27861911Scokane else if (event_loop_p) 27963488Scokane cli_command_loop (); 28061911Scokane else 28163488Scokane command_loop (); 28263488Scokane} 28363488Scokane 28463488Scokaneint 28563488Scokaneinterp_quiet_p (struct interp *interp) 28663488Scokane{ 28763488Scokane if (interp != NULL) 28864085Scokane return interp->quiet_p; 28963488Scokane else 29064085Scokane return current_interpreter->quiet_p; 29163488Scokane} 29263488Scokane 29361911Scokanestatic int 29461911Scokaneinterp_set_quiet (struct interp *interp, int quiet) 29561911Scokane{ 29661911Scokane int old_val = interp->quiet_p; 29761931Scokane interp->quiet_p = quiet; 29861911Scokane return old_val; 29961911Scokane} 30061911Scokane 30161989Scokane/* interp_exec - This executes COMMAND_STR in the current 30261989Scokane interpreter. */ 30361911Scokaneint 30461911Scokaneinterp_exec_p (struct interp *interp) 30561911Scokane{ 30661911Scokane return interp->procs->exec_proc != NULL; 30761911Scokane} 30861931Scokane 30961911Scokaneint 31061911Scokaneinterp_exec (struct interp *interp, const char *command_str) 31161911Scokane{ 31261911Scokane if (interp->procs->exec_proc != NULL) 31361911Scokane { 31461911Scokane return interp->procs->exec_proc (interp->data, command_str); 31561911Scokane } 31661911Scokane return 0; 31761911Scokane} 31861911Scokane 31961911Scokane/* A convenience routine that nulls out all the 32061911Scokane common command hooks. Use it when removing your interpreter in its 32161911Scokane suspend proc. */ 32261911Scokanevoid 32361911Scokaneclear_interpreter_hooks (void) 32461911Scokane{ 32561911Scokane init_ui_hook = 0; 32661911Scokane print_frame_info_listing_hook = 0; 32761911Scokane /*print_frame_more_info_hook = 0; */ 32861911Scokane query_hook = 0; 32961911Scokane warning_hook = 0; 33061911Scokane create_breakpoint_hook = 0; 33161911Scokane delete_breakpoint_hook = 0; 33261911Scokane modify_breakpoint_hook = 0; 33361911Scokane interactive_hook = 0; 33461911Scokane registers_changed_hook = 0; 33561911Scokane readline_begin_hook = 0; 33661911Scokane readline_hook = 0; 33761911Scokane readline_end_hook = 0; 33861911Scokane register_changed_hook = 0; 33961911Scokane memory_changed_hook = 0; 34063488Scokane context_hook = 0; 34161911Scokane target_wait_hook = 0; 34263488Scokane call_command_hook = 0; 34363488Scokane error_hook = 0; 34463488Scokane error_begin_hook = 0; 34563488Scokane command_loop_hook = 0; 34663488Scokane clear_gdb_event_hooks (); 34761911Scokane} 34863488Scokane 34961911Scokane/* This is a lazy init routine, called the first time 35063488Scokane the interpreter module is used. I put it here just in case, but I haven't 35163488Scokane thought of a use for it yet. I will probably bag it soon, since I don't 35263488Scokane think it will be necessary. */ 35363488Scokanestatic void 35463488Scokaneinitialize_interps (void) 35563488Scokane{ 35663488Scokane interpreter_initialized = 1; 35761911Scokane /* Don't know if anything needs to be done here... */ 35861911Scokane} 35961911Scokane 36061911Scokanestatic void 36161911Scokaneinterpreter_exec_cmd (char *args, int from_tty) 36261911Scokane{ 36361911Scokane struct interp *old_interp, *interp_to_use; 36461911Scokane char **prules = NULL; 36561911Scokane char **trule = NULL; 36661911Scokane unsigned int nrules; 36761911Scokane unsigned int i; 36861911Scokane int old_quiet, use_quiet; 36961911Scokane 37061931Scokane prules = buildargv (args); 37161911Scokane if (prules == NULL) 37261911Scokane { 37361911Scokane error ("unable to parse arguments"); 37461911Scokane } 37561911Scokane 37661911Scokane nrules = 0; 37761911Scokane if (prules != NULL) 37861911Scokane { 37961911Scokane for (trule = prules; *trule != NULL; trule++) 38061911Scokane { 38161911Scokane nrules++; 38261911Scokane } 38361911Scokane } 38461911Scokane 38561931Scokane if (nrules < 2) 38661911Scokane error ("usage: interpreter-exec <interpreter> [ <command> ... ]"); 38795092Smarcel 38895092Smarcel old_interp = current_interpreter; 38961911Scokane 39061911Scokane interp_to_use = interp_lookup (prules[0]); 39161911Scokane if (interp_to_use == NULL) 39261911Scokane error ("Could not find interpreter \"%s\".", prules[0]); 39361911Scokane 39461931Scokane /* Temporarily set interpreters quiet */ 39561911Scokane old_quiet = interp_set_quiet (old_interp, 1); 39661911Scokane use_quiet = interp_set_quiet (interp_to_use, 1); 39761911Scokane 39861911Scokane if (!interp_set (interp_to_use)) 39961911Scokane error ("Could not switch to interpreter \"%s\".", prules[0]); 40061911Scokane 40161911Scokane for (i = 1; i < nrules; i++) 40261911Scokane { 40361911Scokane if (!interp_exec (interp_to_use, prules[i])) 40483366Sjulian { 40561911Scokane interp_set (old_interp); 40661911Scokane interp_set_quiet (interp_to_use, old_quiet); 40761911Scokane error ("error in command: \"%s\".", prules[i]); 40861911Scokane break; 40961911Scokane } 41061911Scokane } 41161911Scokane 41261911Scokane interp_set (old_interp); 41361931Scokane interp_set_quiet (interp_to_use, use_quiet); 41483366Sjulian interp_set_quiet (old_interp, old_quiet); 41561911Scokane} 41661911Scokane 41761911Scokane/* List the possible interpreters which could complete the given text. */ 41861911Scokanestatic char ** 41961911Scokaneinterpreter_completer (char *text, char *word) 42061911Scokane{ 42161911Scokane int alloced = 0; 42283366Sjulian int textlen; 42361911Scokane int num_matches; 42461911Scokane char **matches; 42561911Scokane struct interp *interp; 42661911Scokane 42761911Scokane /* We expect only a very limited number of interpreters, so just 42861911Scokane allocate room for all of them. */ 42961911Scokane for (interp = interp_list; interp != NULL; interp = interp->next) 43061911Scokane ++alloced; 43161911Scokane matches = (char **) xmalloc (alloced * sizeof (char *)); 43261931Scokane 43383366Sjulian num_matches = 0; 43461911Scokane textlen = strlen (text); 43561911Scokane for (interp = interp_list; interp != NULL; interp = interp->next) 43661911Scokane { 43761911Scokane if (strncmp (interp->name, text, textlen) == 0) 43861911Scokane { 439112569Sjake matches[num_matches] = 44061911Scokane (char *) xmalloc (strlen (word) + strlen (interp->name) + 1); 44161911Scokane if (word == text) 44261911Scokane strcpy (matches[num_matches], interp->name); 44361911Scokane else if (word > text) 44461911Scokane { 44561911Scokane /* Return some portion of interp->name */ 44666910Scokane strcpy (matches[num_matches], interp->name + (word - text)); 44766910Scokane } 44866910Scokane else 44961911Scokane { 45061911Scokane /* Return some of text plus interp->name */ 45166910Scokane strncpy (matches[num_matches], word, text - word); 45266910Scokane matches[num_matches][text - word] = '\0'; 45366910Scokane strcat (matches[num_matches], interp->name); 45466910Scokane } 45566910Scokane ++num_matches; 45661911Scokane } 45766910Scokane } 45866910Scokane 45961911Scokane if (num_matches == 0) 46066910Scokane { 46161931Scokane xfree (matches); 46261911Scokane matches = NULL; 46361911Scokane } 46461911Scokane else if (num_matches < alloced) 46561911Scokane { 46666910Scokane matches = (char **) xrealloc ((char *) matches, ((num_matches + 1) 46761911Scokane * sizeof (char *))); 46866910Scokane matches[num_matches] = NULL; 46961911Scokane } 470111462Smux 471111462Smux return matches; 47266910Scokane} 47366910Scokane 47466910Scokane/* This just adds the "interpreter-exec" command. */ 47566910Scokanevoid 47666910Scokane_initialize_interpreter (void) 47766910Scokane{ 478111462Smux struct cmd_list_element *c; 479111462Smux 480111462Smux c = add_cmd ("interpreter-exec", class_support, 48166910Scokane interpreter_exec_cmd, 48266910Scokane "Execute a command in an interpreter. It takes two arguments:\n\ 48363488ScokaneThe first argument is the name of the interpreter to use.\n\ 48463488ScokaneThe second argument is the command to execute.\n", &cmdlist); 48566910Scokane set_cmd_completer (c, interpreter_completer); 48664085Scokane} 48763488Scokane