1130803Smarcel/* CLI Definitions for GDB, the GNU debugger.
2130803Smarcel
3130803Smarcel   Copyright 2002, 2003 Free Software Foundation, Inc.
4130803Smarcel
5130803Smarcel   This file is part of GDB.
6130803Smarcel
7130803Smarcel   This program is free software; you can redistribute it and/or modify
8130803Smarcel   it under the terms of the GNU General Public License as published by
9130803Smarcel   the Free Software Foundation; either version 2 of the License, or
10130803Smarcel   (at your option) any later version.
11130803Smarcel
12130803Smarcel   This program is distributed in the hope that it will be useful,
13130803Smarcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
14130803Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15130803Smarcel   GNU General Public License for more details.
16130803Smarcel
17130803Smarcel   You should have received a copy of the GNU General Public License
18130803Smarcel   along with this program; if not, write to the Free Software
19130803Smarcel   Foundation, Inc., 59 Temple Place - Suite 330,
20130803Smarcel   Boston, MA 02111-1307, USA.  */
21130803Smarcel
22130803Smarcel#include "defs.h"
23130803Smarcel#include "interps.h"
24130803Smarcel#include "wrapper.h"
25130803Smarcel#include "event-top.h"
26130803Smarcel#include "ui-out.h"
27130803Smarcel#include "cli-out.h"
28130803Smarcel#include "top.h"		/* for "execute_command" */
29130803Smarcel#include "gdb_string.h"
30130803Smarcel
31130803Smarcelstruct ui_out *cli_uiout;
32130803Smarcel
33130803Smarcel/* These are the ui_out and the interpreter for the console interpreter. */
34130803Smarcel
35130803Smarcel/* Longjmp-safe wrapper for "execute_command" */
36130803Smarcelstatic int do_captured_execute_command (struct ui_out *uiout, void *data);
37130803Smarcelstatic enum gdb_rc safe_execute_command (struct ui_out *uiout, char *command,
38130803Smarcel					 int from_tty);
39130803Smarcelstruct captured_execute_command_args
40130803Smarcel{
41130803Smarcel  char *command;
42130803Smarcel  int from_tty;
43130803Smarcel};
44130803Smarcel
45130803Smarcel/* These implement the cli out interpreter: */
46130803Smarcel
47130803Smarcelstatic void *
48130803Smarcelcli_interpreter_init (void)
49130803Smarcel{
50130803Smarcel  return NULL;
51130803Smarcel}
52130803Smarcel
53130803Smarcelstatic int
54130803Smarcelcli_interpreter_resume (void *data)
55130803Smarcel{
56130803Smarcel  struct ui_file *stream;
57130803Smarcel
58130803Smarcel  /*sync_execution = 1; */
59130803Smarcel
60130803Smarcel  /* gdb_setup_readline will change gdb_stdout.  If the CLI was previously
61130803Smarcel     writing to gdb_stdout, then set it to the new gdb_stdout afterwards.  */
62130803Smarcel
63130803Smarcel  stream = cli_out_set_stream (cli_uiout, gdb_stdout);
64130803Smarcel  if (stream != gdb_stdout)
65130803Smarcel    {
66130803Smarcel      cli_out_set_stream (cli_uiout, stream);
67130803Smarcel      stream = NULL;
68130803Smarcel    }
69130803Smarcel
70130803Smarcel  gdb_setup_readline ();
71130803Smarcel
72130803Smarcel  if (stream != NULL)
73130803Smarcel    cli_out_set_stream (cli_uiout, gdb_stdout);
74130803Smarcel
75130803Smarcel  return 1;
76130803Smarcel}
77130803Smarcel
78130803Smarcelstatic int
79130803Smarcelcli_interpreter_suspend (void *data)
80130803Smarcel{
81130803Smarcel  gdb_disable_readline ();
82130803Smarcel  return 1;
83130803Smarcel}
84130803Smarcel
85130803Smarcel/* Don't display the prompt if we are set quiet.  */
86130803Smarcelstatic int
87130803Smarcelcli_interpreter_display_prompt_p (void *data)
88130803Smarcel{
89130803Smarcel  if (interp_quiet_p (NULL))
90130803Smarcel    return 0;
91130803Smarcel  else
92130803Smarcel    return 1;
93130803Smarcel}
94130803Smarcel
95130803Smarcelstatic int
96130803Smarcelcli_interpreter_exec (void *data, const char *command_str)
97130803Smarcel{
98130803Smarcel  int result;
99130803Smarcel  struct ui_file *old_stream;
100130803Smarcel
101130803Smarcel  /* FIXME: cagney/2003-02-01: Need to const char *propogate
102130803Smarcel     safe_execute_command.  */
103130803Smarcel  char *str = strcpy (alloca (strlen (command_str) + 1), command_str);
104130803Smarcel
105130803Smarcel  /* gdb_stdout could change between the time cli_uiout was initialized
106130803Smarcel     and now. Since we're probably using a different interpreter which has
107130803Smarcel     a new ui_file for gdb_stdout, use that one instead of the default.
108130803Smarcel
109130803Smarcel     It is important that it gets reset everytime, since the user could
110130803Smarcel     set gdb to use a different interpreter. */
111130803Smarcel  old_stream = cli_out_set_stream (cli_uiout, gdb_stdout);
112130803Smarcel  result = safe_execute_command (cli_uiout, str, 1);
113130803Smarcel  cli_out_set_stream (cli_uiout, old_stream);
114130803Smarcel  return result;
115130803Smarcel}
116130803Smarcel
117130803Smarcelstatic int
118130803Smarceldo_captured_execute_command (struct ui_out *uiout, void *data)
119130803Smarcel{
120130803Smarcel  struct captured_execute_command_args *args =
121130803Smarcel    (struct captured_execute_command_args *) data;
122130803Smarcel  execute_command (args->command, args->from_tty);
123130803Smarcel  return GDB_RC_OK;
124130803Smarcel}
125130803Smarcel
126130803Smarcelstatic enum gdb_rc
127130803Smarcelsafe_execute_command (struct ui_out *uiout, char *command, int from_tty)
128130803Smarcel{
129130803Smarcel  struct captured_execute_command_args args;
130130803Smarcel  args.command = command;
131130803Smarcel  args.from_tty = from_tty;
132130803Smarcel  return catch_exceptions (uiout, do_captured_execute_command, &args,
133130803Smarcel			   NULL, RETURN_MASK_ALL);
134130803Smarcel}
135130803Smarcel
136130803Smarcel
137130803Smarcel/* standard gdb initialization hook */
138130803Smarcelextern initialize_file_ftype _initialize_cli_interp; /* -Wmissing-prototypes */
139130803Smarcel
140130803Smarcelvoid
141130803Smarcel_initialize_cli_interp (void)
142130803Smarcel{
143130803Smarcel  static const struct interp_procs procs = {
144130803Smarcel    cli_interpreter_init,	/* init_proc */
145130803Smarcel    cli_interpreter_resume,	/* resume_proc */
146130803Smarcel    cli_interpreter_suspend,	/* suspend_proc */
147130803Smarcel    cli_interpreter_exec,	/* exec_proc */
148130803Smarcel    cli_interpreter_display_prompt_p	/* prompt_proc_p */
149130803Smarcel  };
150130803Smarcel  struct interp *cli_interp;
151130803Smarcel
152130803Smarcel  /* Create a default uiout builder for the CLI. */
153130803Smarcel  cli_uiout = cli_out_new (gdb_stdout);
154130803Smarcel  cli_interp = interp_new (INTERP_CONSOLE, NULL, cli_uiout, &procs);
155130803Smarcel
156130803Smarcel  interp_add (cli_interp);
157130803Smarcel}
158