1 /* CLI Definitions for GDB, the GNU debugger.
3 Copyright (c) 2002-2003, 2007-2012 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "event-top.h"
26 #include "top.h" /* for "execute_command" */
27 #include "gdb_string.h"
28 #include "exceptions.h"
30 struct ui_out *cli_uiout;
32 /* These are the ui_out and the interpreter for the console
35 /* Longjmp-safe wrapper for "execute_command". */
36 static struct gdb_exception safe_execute_command (struct ui_out *uiout,
39 /* These implement the cli out interpreter: */
42 cli_interpreter_init (struct interp *self, int top_level)
48 cli_interpreter_resume (void *data)
50 struct ui_file *stream;
52 /*sync_execution = 1; */
54 /* gdb_setup_readline will change gdb_stdout. If the CLI was
55 previously writing to gdb_stdout, then set it to the new
56 gdb_stdout afterwards. */
58 stream = cli_out_set_stream (cli_uiout, gdb_stdout);
59 if (stream != gdb_stdout)
61 cli_out_set_stream (cli_uiout, stream);
65 gdb_setup_readline ();
68 cli_out_set_stream (cli_uiout, gdb_stdout);
74 cli_interpreter_suspend (void *data)
76 gdb_disable_readline ();
80 /* Don't display the prompt if we are set quiet. */
82 cli_interpreter_display_prompt_p (void *data)
84 if (interp_quiet_p (NULL))
90 static struct gdb_exception
91 cli_interpreter_exec (void *data, const char *command_str)
93 struct ui_file *old_stream;
94 struct gdb_exception result;
96 /* FIXME: cagney/2003-02-01: Need to const char *propogate
97 safe_execute_command. */
98 char *str = strcpy (alloca (strlen (command_str) + 1), command_str);
100 /* gdb_stdout could change between the time cli_uiout was
101 initialized and now. Since we're probably using a different
102 interpreter which has a new ui_file for gdb_stdout, use that one
103 instead of the default.
105 It is important that it gets reset everytime, since the user
106 could set gdb to use a different interpreter. */
107 old_stream = cli_out_set_stream (cli_uiout, gdb_stdout);
108 result = safe_execute_command (cli_uiout, str, 1);
109 cli_out_set_stream (cli_uiout, old_stream);
113 static struct gdb_exception
114 safe_execute_command (struct ui_out *command_uiout, char *command, int from_tty)
116 volatile struct gdb_exception e;
117 struct ui_out *saved_uiout;
119 /* Save and override the global ``struct ui_out'' builder. */
120 saved_uiout = current_uiout;
121 current_uiout = command_uiout;
123 TRY_CATCH (e, RETURN_MASK_ALL)
125 execute_command (command, from_tty);
128 /* Restore the global builder. */
129 current_uiout = saved_uiout;
131 /* FIXME: cagney/2005-01-13: This shouldn't be needed. Instead the
132 caller should print the exception. */
133 exception_print (gdb_stderr, e);
137 static struct ui_out *
138 cli_ui_out (struct interp *self)
143 /* Standard gdb initialization hook. */
144 extern initialize_file_ftype _initialize_cli_interp; /* -Wmissing-prototypes */
147 _initialize_cli_interp (void)
149 static const struct interp_procs procs = {
150 cli_interpreter_init, /* init_proc */
151 cli_interpreter_resume, /* resume_proc */
152 cli_interpreter_suspend, /* suspend_proc */
153 cli_interpreter_exec, /* exec_proc */
154 cli_interpreter_display_prompt_p, /* prompt_proc_p */
155 cli_ui_out /* ui_out_proc */
157 struct interp *cli_interp;
159 /* Create a default uiout builder for the CLI. */
160 cli_uiout = cli_out_new (gdb_stdout);
161 cli_interp = interp_new (INTERP_CONSOLE, &procs);
163 interp_add (cli_interp);