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