]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/tui/tui-interp.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / tui / tui-interp.c
CommitLineData
021e7609
AC
1/* TUI Interpreter definitions for GDB, the GNU debugger.
2
1d506c26 3 Copyright (C) 2003-2024 Free Software Foundation, Inc.
021e7609
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
021e7609
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/>. */
021e7609
AC
19
20#include "defs.h"
3c216924 21#include "cli/cli-interp.h"
021e7609 22#include "interps.h"
13d03262 23#include "ui.h"
021e7609 24#include "event-top.h"
400b5eca 25#include "gdbsupport/event-loop.h"
021e7609 26#include "ui-out.h"
95cd630e 27#include "cli-out.h"
d7b2e967 28#include "tui/tui-data.h"
d7b2e967 29#include "tui/tui-win.h"
021e7609 30#include "tui/tui.h"
d7b2e967 31#include "tui/tui-io.h"
fd664c91 32#include "infrun.h"
76727919 33#include "observable.h"
eaae60fd 34#include "gdbthread.h"
00431a78 35#include "inferior.h"
b0be6c91 36#include "main.h"
fd664c91 37
87d557ae 38/* Set to true when the TUI mode must be activated when we first start
1cc6d956 39 gdb. */
87d557ae 40static bool tui_start_enabled = false;
63858210 41
d6f9b0fb
PA
42class tui_interp final : public cli_interp_base
43{
44public:
45 explicit tui_interp (const char *name)
46 : cli_interp_base (name)
47 {}
48
49 void init (bool top_level) override;
50 void resume () override;
51 void suspend () override;
b885aea1 52 void exec (const char *command_str) override;
d6f9b0fb
PA
53 ui_out *interp_ui_out () override;
54};
55
021e7609
AC
56/* Cleanup the tui before exiting. */
57
58static void
59tui_exit (void)
60{
1cc6d956
MS
61 /* Disable the tui. Curses mode is left leaving the screen in a
62 clean state (see endwin()). */
021e7609
AC
63 tui_disable ();
64}
65
66/* These implement the TUI interpreter. */
67
d6f9b0fb
PA
68void
69tui_interp::init (bool top_level)
021e7609
AC
70{
71 /* Install exit handler to leave the screen in a good shape. */
72 atexit (tui_exit);
73
021e7609 74 tui_initialize_io ();
da5bd37e 75 if (gdb_stdout->isatty ())
72994b60
LS
76 {
77 tui_ensure_readline_initialized ();
78
79 /* This installs the SIGWINCH signal handler. The handler needs to do
80 readline calls (to rl_resize_terminal), so it must not be installed
81 unless readline is properly initialized. */
82 tui_initialize_win ();
83 }
021e7609
AC
84}
85
cd074e04
AB
86/* Used as the command handler for the tui. */
87
88static void
89tui_command_line_handler (gdb::unique_xmalloc_ptr<char> &&rl)
90{
91 /* When a tui enabled GDB is running in either tui mode or cli mode then
92 it is always the tui interpreter that is in use. As a result we end
93 up in here even in standard cli mode.
94
95 We only need to do any special actions when the tui is in use
96 though. When the tui is active the users return is not echoed to the
97 screen as a result the display will not automatically move us to the
98 next line. Here we manually insert a newline character and move the
99 cursor. */
100 if (tui_active)
101 tui_inject_newline_into_command_window ();
102
103 /* Now perform GDB's standard CLI command line handling. */
104 command_line_handler (std::move (rl));
105}
106
d6f9b0fb
PA
107void
108tui_interp::resume ()
021e7609 109{
3c216924 110 struct ui *ui = current_ui;
95cd630e
DJ
111 struct ui_file *stream;
112
1cc6d956
MS
113 /* gdb_setup_readline will change gdb_stdout. If the TUI was
114 previously writing to gdb_stdout, then set it to the new
115 gdb_stdout afterwards. */
95cd630e 116
112e8700 117 stream = tui_old_uiout->set_stream (gdb_stdout);
95cd630e
DJ
118 if (stream != gdb_stdout)
119 {
112e8700 120 tui_old_uiout->set_stream (stream);
95cd630e
DJ
121 stream = NULL;
122 }
123
3c216924
PA
124 gdb_setup_readline (1);
125
cd074e04 126 ui->input_handler = tui_command_line_handler;
95cd630e
DJ
127
128 if (stream != NULL)
112e8700 129 tui_old_uiout->set_stream (gdb_stdout);
95cd630e 130
63858210
SC
131 if (tui_start_enabled)
132 tui_enable ();
021e7609
AC
133}
134
d6f9b0fb
PA
135void
136tui_interp::suspend ()
021e7609 137{
1e28eebb 138 gdb_disable_readline ();
63858210 139 tui_start_enabled = tui_active;
021e7609 140 tui_disable ();
021e7609
AC
141}
142
d6f9b0fb
PA
143ui_out *
144tui_interp::interp_ui_out ()
4801a9a3
PA
145{
146 if (tui_active)
147 return tui_out;
148 else
149 return tui_old_uiout;
150}
151
b885aea1 152void
d6f9b0fb 153tui_interp::exec (const char *command_str)
021e7609 154{
f34652de 155 internal_error (_("tui_exec called"));
021e7609
AC
156}
157
8322445e
PA
158
159/* Factory for TUI interpreters. */
160
161static struct interp *
162tui_interp_factory (const char *name)
163{
d6f9b0fb 164 return new tui_interp (name);
8322445e
PA
165}
166
6c265988 167void _initialize_tui_interp ();
021e7609 168void
6c265988 169_initialize_tui_interp ()
021e7609 170{
8322445e
PA
171 interp_factory_register (INTERP_TUI, tui_interp_factory);
172
b2a696a8 173 if (interpreter_p == INTERP_TUI)
87d557ae 174 tui_start_enabled = true;
63858210 175
b2a696a8
TT
176 if (interpreter_p == INTERP_CONSOLE)
177 interpreter_p = INTERP_TUI;
73ab01a0 178
5227abd2
PA
179 /* There are no observers here because the CLI interpreter's
180 observers work for the TUI interpreter as well. See
181 cli-interp.c. */
021e7609 182}