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