Commit | Line | Data |
---|---|---|
4a8f6654 AC |
1 | /* CLI Definitions for GDB, the GNU debugger. |
2 | ||
4c38e0a4 JB |
3 | Copyright (c) 2002, 2003, 2007, 2008, 2009, 2010 |
4 | Free Software Foundation, Inc. | |
4a8f6654 AC |
5 | |
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 10 | the Free Software Foundation; either version 3 of the License, or |
4a8f6654 AC |
11 | (at your option) any later version. |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
4a8f6654 AC |
20 | |
21 | #include "defs.h" | |
22 | #include "interps.h" | |
23 | #include "wrapper.h" | |
24 | #include "event-top.h" | |
25 | #include "ui-out.h" | |
26 | #include "cli-out.h" | |
27 | #include "top.h" /* for "execute_command" */ | |
28 | #include "gdb_string.h" | |
60250e8b | 29 | #include "exceptions.h" |
4a8f6654 AC |
30 | |
31 | struct ui_out *cli_uiout; | |
32 | ||
ebcd3b23 MS |
33 | /* These are the ui_out and the interpreter for the console |
34 | interpreter. */ | |
4a8f6654 | 35 | |
4791eb66 | 36 | /* Longjmp-safe wrapper for "execute_command". */ |
71fff37b | 37 | static struct gdb_exception safe_execute_command (struct ui_out *uiout, |
ebcd3b23 MS |
38 | char *command, |
39 | int from_tty); | |
4a8f6654 AC |
40 | struct captured_execute_command_args |
41 | { | |
42 | char *command; | |
43 | int from_tty; | |
44 | }; | |
45 | ||
46 | /* These implement the cli out interpreter: */ | |
47 | ||
48 | static void * | |
683f2885 | 49 | cli_interpreter_init (int top_level) |
4a8f6654 AC |
50 | { |
51 | return NULL; | |
52 | } | |
53 | ||
54 | static int | |
55 | cli_interpreter_resume (void *data) | |
56 | { | |
38caaeec DJ |
57 | struct ui_file *stream; |
58 | ||
4a8f6654 | 59 | /*sync_execution = 1; */ |
38caaeec | 60 | |
ebcd3b23 MS |
61 | /* gdb_setup_readline will change gdb_stdout. If the CLI was |
62 | previously writing to gdb_stdout, then set it to the new | |
63 | gdb_stdout afterwards. */ | |
38caaeec DJ |
64 | |
65 | stream = cli_out_set_stream (cli_uiout, gdb_stdout); | |
66 | if (stream != gdb_stdout) | |
67 | { | |
68 | cli_out_set_stream (cli_uiout, stream); | |
69 | stream = NULL; | |
70 | } | |
71 | ||
4a8f6654 | 72 | gdb_setup_readline (); |
38caaeec DJ |
73 | |
74 | if (stream != NULL) | |
75 | cli_out_set_stream (cli_uiout, gdb_stdout); | |
76 | ||
4a8f6654 AC |
77 | return 1; |
78 | } | |
79 | ||
80 | static int | |
81 | cli_interpreter_suspend (void *data) | |
82 | { | |
83 | gdb_disable_readline (); | |
84 | return 1; | |
85 | } | |
86 | ||
87 | /* Don't display the prompt if we are set quiet. */ | |
88 | static int | |
89 | cli_interpreter_display_prompt_p (void *data) | |
90 | { | |
91 | if (interp_quiet_p (NULL)) | |
92 | return 0; | |
93 | else | |
94 | return 1; | |
95 | } | |
96 | ||
71fff37b | 97 | static struct gdb_exception |
4a8f6654 AC |
98 | cli_interpreter_exec (void *data, const char *command_str) |
99 | { | |
4a8f6654 | 100 | struct ui_file *old_stream; |
71fff37b | 101 | struct gdb_exception result; |
4a8f6654 AC |
102 | |
103 | /* FIXME: cagney/2003-02-01: Need to const char *propogate | |
104 | safe_execute_command. */ | |
105 | char *str = strcpy (alloca (strlen (command_str) + 1), command_str); | |
106 | ||
ebcd3b23 MS |
107 | /* gdb_stdout could change between the time cli_uiout was |
108 | initialized and now. Since we're probably using a different | |
109 | interpreter which has a new ui_file for gdb_stdout, use that one | |
110 | instead of the default. | |
4a8f6654 | 111 | |
ebcd3b23 MS |
112 | It is important that it gets reset everytime, since the user |
113 | could set gdb to use a different interpreter. */ | |
4a8f6654 AC |
114 | old_stream = cli_out_set_stream (cli_uiout, gdb_stdout); |
115 | result = safe_execute_command (cli_uiout, str, 1); | |
116 | cli_out_set_stream (cli_uiout, old_stream); | |
117 | return result; | |
118 | } | |
119 | ||
c1043fc2 | 120 | static void |
4a8f6654 AC |
121 | do_captured_execute_command (struct ui_out *uiout, void *data) |
122 | { | |
123 | struct captured_execute_command_args *args = | |
124 | (struct captured_execute_command_args *) data; | |
cdb27c12 | 125 | |
4a8f6654 | 126 | execute_command (args->command, args->from_tty); |
4a8f6654 AC |
127 | } |
128 | ||
71fff37b | 129 | static struct gdb_exception |
4a8f6654 AC |
130 | safe_execute_command (struct ui_out *uiout, char *command, int from_tty) |
131 | { | |
71fff37b | 132 | struct gdb_exception e; |
4a8f6654 | 133 | struct captured_execute_command_args args; |
cdb27c12 | 134 | |
4a8f6654 AC |
135 | args.command = command; |
136 | args.from_tty = from_tty; | |
8a076db9 AC |
137 | e = catch_exception (uiout, do_captured_execute_command, &args, |
138 | RETURN_MASK_ALL); | |
139 | /* FIXME: cagney/2005-01-13: This shouldn't be needed. Instead the | |
140 | caller should print the exception. */ | |
9cbc821d | 141 | exception_print (gdb_stderr, e); |
8a076db9 | 142 | return e; |
4a8f6654 AC |
143 | } |
144 | ||
145 | ||
4791eb66 | 146 | /* Standard gdb initialization hook. */ |
b9362cc7 AC |
147 | extern initialize_file_ftype _initialize_cli_interp; /* -Wmissing-prototypes */ |
148 | ||
4a8f6654 AC |
149 | void |
150 | _initialize_cli_interp (void) | |
151 | { | |
152 | static const struct interp_procs procs = { | |
153 | cli_interpreter_init, /* init_proc */ | |
154 | cli_interpreter_resume, /* resume_proc */ | |
155 | cli_interpreter_suspend, /* suspend_proc */ | |
156 | cli_interpreter_exec, /* exec_proc */ | |
157 | cli_interpreter_display_prompt_p /* prompt_proc_p */ | |
158 | }; | |
159 | struct interp *cli_interp; | |
160 | ||
4791eb66 | 161 | /* Create a default uiout builder for the CLI. */ |
4a8f6654 AC |
162 | cli_uiout = cli_out_new (gdb_stdout); |
163 | cli_interp = interp_new (INTERP_CONSOLE, NULL, cli_uiout, &procs); | |
164 | ||
165 | interp_add (cli_interp); | |
166 | } |