1 //===-- Terminal.cpp --------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Host/Terminal.h"
11 #include "lldb/Host/Config.h"
12 #include "lldb/Host/PosixApi.h"
13 #include "llvm/ADT/STLExtras.h"
18 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
22 using namespace lldb_private;
24 bool Terminal::IsATerminal() const { return m_fd >= 0 && ::isatty(m_fd); }
26 bool Terminal::SetEcho(bool enabled) {
27 if (FileDescriptorIsValid()) {
28 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
30 struct termios fd_termios;
31 if (::tcgetattr(m_fd, &fd_termios) == 0) {
32 bool set_corectly = false;
34 if (fd_termios.c_lflag & ECHO)
37 fd_termios.c_lflag |= ECHO;
39 if (fd_termios.c_lflag & ECHO)
40 fd_termios.c_lflag &= ~ECHO;
47 return ::tcsetattr(m_fd, TCSANOW, &fd_termios) == 0;
50 #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
55 bool Terminal::SetCanonical(bool enabled) {
56 if (FileDescriptorIsValid()) {
57 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
59 struct termios fd_termios;
60 if (::tcgetattr(m_fd, &fd_termios) == 0) {
61 bool set_corectly = false;
63 if (fd_termios.c_lflag & ICANON)
66 fd_termios.c_lflag |= ICANON;
68 if (fd_termios.c_lflag & ICANON)
69 fd_termios.c_lflag &= ~ICANON;
76 return ::tcsetattr(m_fd, TCSANOW, &fd_termios) == 0;
79 #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
84 // Default constructor
85 TerminalState::TerminalState()
86 : m_tty(), m_tflags(-1),
87 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
94 TerminalState::~TerminalState() {}
96 void TerminalState::Clear() {
99 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
100 m_termios_up.reset();
102 m_process_group = -1;
105 // Save the current state of the TTY for the file descriptor "fd" and if
106 // "save_process_group" is true, attempt to save the process group info for the
108 bool TerminalState::Save(int fd, bool save_process_group) {
109 m_tty.SetFileDescriptor(fd);
110 if (m_tty.IsATerminal()) {
111 #ifndef LLDB_DISABLE_POSIX
112 m_tflags = ::fcntl(fd, F_GETFL, 0);
114 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
115 if (m_termios_up == nullptr)
116 m_termios_up.reset(new struct termios);
117 int err = ::tcgetattr(fd, m_termios_up.get());
119 m_termios_up.reset();
120 #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
121 #ifndef LLDB_DISABLE_POSIX
122 if (save_process_group)
123 m_process_group = ::tcgetpgrp(0);
125 m_process_group = -1;
130 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
131 m_termios_up.reset();
133 m_process_group = -1;
138 // Restore the state of the TTY using the cached values from a previous call to
140 bool TerminalState::Restore() const {
141 #ifndef LLDB_DISABLE_POSIX
143 const int fd = m_tty.GetFileDescriptor();
145 fcntl(fd, F_SETFL, m_tflags);
147 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
148 if (TTYStateIsValid())
149 tcsetattr(fd, TCSANOW, m_termios_up.get());
150 #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
152 if (ProcessGroupIsValid()) {
153 // Save the original signal handler.
154 void (*saved_sigttou_callback)(int) = nullptr;
155 saved_sigttou_callback = (void (*)(int))signal(SIGTTOU, SIG_IGN);
156 // Set the process group
157 tcsetpgrp(fd, m_process_group);
158 // Restore the original signal handler.
159 signal(SIGTTOU, saved_sigttou_callback);
167 // Returns true if this object has valid saved TTY state settings that can be
168 // used to restore a previous state.
169 bool TerminalState::IsValid() const {
170 return m_tty.FileDescriptorIsValid() &&
171 (TFlagsIsValid() || TTYStateIsValid());
174 // Returns true if m_tflags is valid
175 bool TerminalState::TFlagsIsValid() const { return m_tflags != -1; }
177 // Returns true if m_ttystate is valid
178 bool TerminalState::TTYStateIsValid() const {
179 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
180 return m_termios_up != nullptr;
186 // Returns true if m_process_group is valid
187 bool TerminalState::ProcessGroupIsValid() const {
188 return static_cast<int32_t>(m_process_group) != -1;
192 TerminalStateSwitcher::TerminalStateSwitcher() : m_currentState(UINT32_MAX) {}
195 TerminalStateSwitcher::~TerminalStateSwitcher() {}
197 // Returns the number of states that this switcher contains
198 uint32_t TerminalStateSwitcher::GetNumberOfStates() const {
199 return llvm::array_lengthof(m_ttystates);
202 // Restore the state at index "idx".
204 // Returns true if the restore was successful, false otherwise.
205 bool TerminalStateSwitcher::Restore(uint32_t idx) const {
206 const uint32_t num_states = GetNumberOfStates();
207 if (idx >= num_states)
210 // See if we already are in this state?
211 if (m_currentState < num_states && (idx == m_currentState) &&
212 m_ttystates[idx].IsValid())
215 // Set the state to match the index passed in and only update the current
216 // state if there are no errors.
217 if (m_ttystates[idx].Restore()) {
218 m_currentState = idx;
222 // We failed to set the state. The tty state was invalid or not initialized.
226 // Save the state at index "idx" for file descriptor "fd" and save the process
227 // group if requested.
229 // Returns true if the restore was successful, false otherwise.
230 bool TerminalStateSwitcher::Save(uint32_t idx, int fd,
231 bool save_process_group) {
232 const uint32_t num_states = GetNumberOfStates();
233 if (idx < num_states)
234 return m_ttystates[idx].Save(fd, save_process_group);