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