Implement tp_richcompare for gdb.Block
[binutils-gdb.git] / gdb / ui.c
blob80ee67dbae5a303b508eb6d5219de3396ed3df76
1 /* Copyright (C) 2023-2024 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/>. */
18 #include "ui.h"
20 #include "cli/cli-cmds.h"
21 #include "event-top.h"
22 #include "gdbsupport/buildargv.h"
23 #include "gdbsupport/filestuff.h"
24 #include "gdbsupport/gdb_file.h"
25 #include "gdbsupport/scoped_fd.h"
26 #include "interps.h"
27 #include "pager.h"
28 #include "main.h"
29 #include "top.h"
31 /* See top.h. */
33 struct ui *main_ui;
34 struct ui *current_ui;
35 struct ui *ui_list;
37 /* The highest UI number ever assigned. */
39 static int highest_ui_num;
41 /* See top.h. */
43 ui::ui (FILE *instream_, FILE *outstream_, FILE *errstream_)
44 : num (++highest_ui_num),
45 stdin_stream (instream_),
46 instream (instream_),
47 outstream (outstream_),
48 errstream (errstream_),
49 input_fd (fileno (instream)),
50 m_input_interactive_p (ISATTY (instream)),
51 m_gdb_stdout (new pager_file (new stdio_file (outstream))),
52 m_gdb_stdin (new stdio_file (instream)),
53 m_gdb_stderr (new stderr_file (errstream)),
54 m_gdb_stdlog (new timestamped_file (m_gdb_stderr))
56 unbuffer_stream (instream_);
58 if (ui_list == NULL)
59 ui_list = this;
60 else
62 struct ui *last;
64 for (last = ui_list; last->next != NULL; last = last->next)
66 last->next = this;
70 ui::~ui ()
72 struct ui *ui, *uiprev;
74 uiprev = NULL;
76 for (ui = ui_list; ui != NULL; uiprev = ui, ui = ui->next)
77 if (ui == this)
78 break;
80 gdb_assert (ui != NULL);
82 if (uiprev != NULL)
83 uiprev->next = next;
84 else
85 ui_list = next;
87 delete m_gdb_stdin;
88 delete m_gdb_stdout;
89 delete m_gdb_stderr;
93 /* Returns whether GDB is running on an interactive terminal. */
95 bool
96 ui::input_interactive_p () const
98 if (batch_flag)
99 return false;
101 if (interactive_mode != AUTO_BOOLEAN_AUTO)
102 return interactive_mode == AUTO_BOOLEAN_TRUE;
104 return m_input_interactive_p;
108 /* When there is an event ready on the stdin file descriptor, instead
109 of calling readline directly throught the callback function, or
110 instead of calling gdb_readline_no_editing_callback, give gdb a
111 chance to detect errors and do something. */
113 static void
114 stdin_event_handler (int error, gdb_client_data client_data)
116 struct ui *ui = (struct ui *) client_data;
118 if (error)
120 /* Switch to the main UI, so diagnostics always go there. */
121 current_ui = main_ui;
123 ui->unregister_file_handler ();
124 if (main_ui == ui)
126 /* If stdin died, we may as well kill gdb. */
127 gdb_printf (gdb_stderr, _("error detected on stdin\n"));
128 quit_command ((char *) 0, 0);
130 else
132 /* Simply delete the UI. */
133 delete ui;
136 else
138 /* Switch to the UI whose input descriptor woke up the event
139 loop. */
140 current_ui = ui;
142 /* This makes sure a ^C immediately followed by further input is
143 always processed in that order. E.g,. with input like
144 "^Cprint 1\n", the SIGINT handler runs, marks the async
145 signal handler, and then select/poll may return with stdin
146 ready, instead of -1/EINTR. The
147 gdb.base/double-prompt-target-event-error.exp test exercises
148 this. */
149 QUIT;
153 call_stdin_event_handler_again_p = 0;
154 ui->call_readline (client_data);
156 while (call_stdin_event_handler_again_p != 0);
160 /* See top.h. */
162 void
163 ui::register_file_handler ()
165 if (input_fd != -1)
166 add_file_handler (input_fd, stdin_event_handler, this,
167 string_printf ("ui-%d", num), true);
170 /* See top.h. */
172 void
173 ui::unregister_file_handler ()
175 if (input_fd != -1)
176 delete_file_handler (input_fd);
179 /* Open file named NAME for read/write, making sure not to make it the
180 controlling terminal. */
182 static gdb_file_up
183 open_terminal_stream (const char *name)
185 scoped_fd fd = gdb_open_cloexec (name, O_RDWR | O_NOCTTY, 0);
186 if (fd.get () < 0)
187 perror_with_name (_("opening terminal failed"));
189 return fd.to_file ("w+");
192 /* Implementation of the "new-ui" command. */
194 static void
195 new_ui_command (const char *args, int from_tty)
197 int argc;
198 const char *interpreter_name;
199 const char *tty_name;
201 dont_repeat ();
203 gdb_argv argv (args);
204 argc = argv.count ();
206 if (argc < 2)
207 error (_("Usage: new-ui INTERPRETER TTY"));
209 interpreter_name = argv[0];
210 tty_name = argv[1];
213 scoped_restore save_ui = make_scoped_restore (&current_ui);
215 /* Open specified terminal. Note: we used to open it three times,
216 once for each of stdin/stdout/stderr, but that does not work
217 with Windows named pipes. */
218 gdb_file_up stream = open_terminal_stream (tty_name);
220 std::unique_ptr<ui> ui
221 (new struct ui (stream.get (), stream.get (), stream.get ()));
223 ui->async = 1;
225 current_ui = ui.get ();
227 set_top_level_interpreter (interpreter_name);
229 top_level_interpreter ()->pre_command_loop ();
231 /* Make sure the file is not closed. */
232 stream.release ();
234 ui.release ();
237 gdb_printf ("New UI allocated\n");
240 void _initialize_ui ();
241 void
242 _initialize_ui ()
244 cmd_list_element *c = add_cmd ("new-ui", class_support, new_ui_command, _("\
245 Create a new UI.\n\
246 Usage: new-ui INTERPRETER TTY\n\
247 The first argument is the name of the interpreter to run.\n\
248 The second argument is the terminal the UI runs on."), &cmdlist);
249 set_cmd_completer (c, interpreter_completer);