1 /* Copyright (C) 2023 Free Software Foundation, Inc.
3 This file is part of GDB.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "cli/cli-cmds.h"
22 #include "event-top.h"
23 #include "gdbsupport/buildargv.h"
24 #include "gdbsupport/filestuff.h"
25 #include "gdbsupport/gdb_file.h"
26 #include "gdbsupport/scoped_fd.h"
35 struct ui
*current_ui
;
38 /* The highest UI number ever assigned. */
40 static int highest_ui_num
;
44 ui::ui (FILE *instream_
, FILE *outstream_
, FILE *errstream_
)
45 : num (++highest_ui_num
),
46 stdin_stream (instream_
),
48 outstream (outstream_
),
49 errstream (errstream_
),
50 input_fd (fileno (instream
)),
51 m_input_interactive_p (ISATTY (instream
)),
52 m_gdb_stdout (new pager_file (new stdio_file (outstream
))),
53 m_gdb_stdin (new stdio_file (instream
)),
54 m_gdb_stderr (new stderr_file (errstream
)),
55 m_gdb_stdlog (new timestamped_file (m_gdb_stderr
))
57 unbuffer_stream (instream_
);
65 for (last
= ui_list
; last
->next
!= NULL
; last
= last
->next
)
73 struct ui
*ui
, *uiprev
;
77 for (ui
= ui_list
; ui
!= NULL
; uiprev
= ui
, ui
= ui
->next
)
81 gdb_assert (ui
!= NULL
);
94 /* Returns whether GDB is running on an interactive terminal. */
97 ui::input_interactive_p () const
102 if (interactive_mode
!= AUTO_BOOLEAN_AUTO
)
103 return interactive_mode
== AUTO_BOOLEAN_TRUE
;
105 return m_input_interactive_p
;
109 /* When there is an event ready on the stdin file descriptor, instead
110 of calling readline directly throught the callback function, or
111 instead of calling gdb_readline_no_editing_callback, give gdb a
112 chance to detect errors and do something. */
115 stdin_event_handler (int error
, gdb_client_data client_data
)
117 struct ui
*ui
= (struct ui
*) client_data
;
121 /* Switch to the main UI, so diagnostics always go there. */
122 current_ui
= main_ui
;
124 ui
->unregister_file_handler ();
127 /* If stdin died, we may as well kill gdb. */
128 gdb_printf (gdb_stderr
, _("error detected on stdin\n"));
129 quit_command ((char *) 0, 0);
133 /* Simply delete the UI. */
139 /* Switch to the UI whose input descriptor woke up the event
143 /* This makes sure a ^C immediately followed by further input is
144 always processed in that order. E.g,. with input like
145 "^Cprint 1\n", the SIGINT handler runs, marks the async
146 signal handler, and then select/poll may return with stdin
147 ready, instead of -1/EINTR. The
148 gdb.base/double-prompt-target-event-error.exp test exercises
154 call_stdin_event_handler_again_p
= 0;
155 ui
->call_readline (client_data
);
157 while (call_stdin_event_handler_again_p
!= 0);
164 ui::register_file_handler ()
167 add_file_handler (input_fd
, stdin_event_handler
, this,
168 string_printf ("ui-%d", num
), true);
174 ui::unregister_file_handler ()
177 delete_file_handler (input_fd
);
180 /* Open file named NAME for read/write, making sure not to make it the
181 controlling terminal. */
184 open_terminal_stream (const char *name
)
186 scoped_fd fd
= gdb_open_cloexec (name
, O_RDWR
| O_NOCTTY
, 0);
188 perror_with_name (_("opening terminal failed"));
190 return fd
.to_file ("w+");
193 /* Implementation of the "new-ui" command. */
196 new_ui_command (const char *args
, int from_tty
)
199 const char *interpreter_name
;
200 const char *tty_name
;
204 gdb_argv
argv (args
);
205 argc
= argv
.count ();
208 error (_("Usage: new-ui INTERPRETER TTY"));
210 interpreter_name
= argv
[0];
214 scoped_restore save_ui
= make_scoped_restore (¤t_ui
);
216 /* Open specified terminal. Note: we used to open it three times,
217 once for each of stdin/stdout/stderr, but that does not work
218 with Windows named pipes. */
219 gdb_file_up stream
= open_terminal_stream (tty_name
);
221 std::unique_ptr
<ui
> ui
222 (new struct ui (stream
.get (), stream
.get (), stream
.get ()));
226 current_ui
= ui
.get ();
228 set_top_level_interpreter (interpreter_name
);
230 top_level_interpreter ()->pre_command_loop ();
232 /* Make sure the file is not closed. */
238 gdb_printf ("New UI allocated\n");
241 void _initialize_ui ();
245 cmd_list_element
*c
= add_cmd ("new-ui", class_support
, new_ui_command
, _("\
247 Usage: new-ui INTERPRETER TTY\n\
248 The first argument is the name of the interpreter to run.\n\
249 The second argument is the terminal the UI runs on."), &cmdlist
);
250 set_cmd_completer (c
, interpreter_completer
);