1 //===-- Terminal.cpp --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Host/Terminal.h"
12 #include "lldb/Host/Config.h"
13 #include "lldb/Host/PosixApi.h"
14 #include "llvm/ADT/STLExtras.h"
19 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
23 using namespace lldb_private;
25 bool Terminal::IsATerminal() const { return m_fd >= 0 && ::isatty(m_fd); }
27 bool Terminal::SetEcho(bool enabled) {
28 if (FileDescriptorIsValid()) {
29 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
31 struct termios fd_termios;
32 if (::tcgetattr(m_fd, &fd_termios) == 0) {
33 bool set_corectly = false;
35 if (fd_termios.c_lflag & ECHO)
38 fd_termios.c_lflag |= ECHO;
40 if (fd_termios.c_lflag & ECHO)
41 fd_termios.c_lflag &= ~ECHO;
48 return ::tcsetattr(m_fd, TCSANOW, &fd_termios) == 0;
51 #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
56 bool Terminal::SetCanonical(bool enabled) {
57 if (FileDescriptorIsValid()) {
58 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
60 struct termios fd_termios;
61 if (::tcgetattr(m_fd, &fd_termios) == 0) {
62 bool set_corectly = false;
64 if (fd_termios.c_lflag & ICANON)
67 fd_termios.c_lflag |= ICANON;
69 if (fd_termios.c_lflag & ICANON)
70 fd_termios.c_lflag &= ~ICANON;
77 return ::tcsetattr(m_fd, TCSANOW, &fd_termios) == 0;
80 #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
85 //----------------------------------------------------------------------
86 // Default constructor
87 //----------------------------------------------------------------------
88 TerminalState::TerminalState()
89 : m_tty(), m_tflags(-1),
90 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
96 //----------------------------------------------------------------------
98 //----------------------------------------------------------------------
99 TerminalState::~TerminalState() {}
101 void TerminalState::Clear() {
104 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
105 m_termios_ap.reset();
107 m_process_group = -1;
110 //----------------------------------------------------------------------
111 // Save the current state of the TTY for the file descriptor "fd" and if
112 // "save_process_group" is true, attempt to save the process group info for the
114 //----------------------------------------------------------------------
115 bool TerminalState::Save(int fd, bool save_process_group) {
116 m_tty.SetFileDescriptor(fd);
117 if (m_tty.IsATerminal()) {
118 #ifndef LLDB_DISABLE_POSIX
119 m_tflags = ::fcntl(fd, F_GETFL, 0);
121 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
122 if (m_termios_ap.get() == NULL)
123 m_termios_ap.reset(new struct termios);
124 int err = ::tcgetattr(fd, m_termios_ap.get());
126 m_termios_ap.reset();
127 #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
128 #ifndef LLDB_DISABLE_POSIX
129 if (save_process_group)
130 m_process_group = ::tcgetpgrp(0);
132 m_process_group = -1;
137 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
138 m_termios_ap.reset();
140 m_process_group = -1;
145 //----------------------------------------------------------------------
146 // Restore the state of the TTY using the cached values from a previous call to
148 //----------------------------------------------------------------------
149 bool TerminalState::Restore() const {
150 #ifndef LLDB_DISABLE_POSIX
152 const int fd = m_tty.GetFileDescriptor();
154 fcntl(fd, F_SETFL, m_tflags);
156 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
157 if (TTYStateIsValid())
158 tcsetattr(fd, TCSANOW, m_termios_ap.get());
159 #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
161 if (ProcessGroupIsValid()) {
162 // Save the original signal handler.
163 void (*saved_sigttou_callback)(int) = NULL;
164 saved_sigttou_callback = (void (*)(int))signal(SIGTTOU, SIG_IGN);
165 // Set the process group
166 tcsetpgrp(fd, m_process_group);
167 // Restore the original signal handler.
168 signal(SIGTTOU, saved_sigttou_callback);
176 //----------------------------------------------------------------------
177 // Returns true if this object has valid saved TTY state settings that can be
178 // used to restore a previous state.
179 //----------------------------------------------------------------------
180 bool TerminalState::IsValid() const {
181 return m_tty.FileDescriptorIsValid() &&
182 (TFlagsIsValid() || TTYStateIsValid());
185 //----------------------------------------------------------------------
186 // Returns true if m_tflags is valid
187 //----------------------------------------------------------------------
188 bool TerminalState::TFlagsIsValid() const { return m_tflags != -1; }
190 //----------------------------------------------------------------------
191 // Returns true if m_ttystate is valid
192 //----------------------------------------------------------------------
193 bool TerminalState::TTYStateIsValid() const {
194 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
195 return m_termios_ap.get() != 0;
201 //----------------------------------------------------------------------
202 // Returns true if m_process_group is valid
203 //----------------------------------------------------------------------
204 bool TerminalState::ProcessGroupIsValid() const {
205 return static_cast<int32_t>(m_process_group) != -1;
208 //------------------------------------------------------------------
210 //------------------------------------------------------------------
211 TerminalStateSwitcher::TerminalStateSwitcher() : m_currentState(UINT32_MAX) {}
213 //------------------------------------------------------------------
215 //------------------------------------------------------------------
216 TerminalStateSwitcher::~TerminalStateSwitcher() {}
218 //------------------------------------------------------------------
219 // Returns the number of states that this switcher contains
220 //------------------------------------------------------------------
221 uint32_t TerminalStateSwitcher::GetNumberOfStates() const {
222 return llvm::array_lengthof(m_ttystates);
225 //------------------------------------------------------------------
226 // Restore the state at index "idx".
228 // Returns true if the restore was successful, false otherwise.
229 //------------------------------------------------------------------
230 bool TerminalStateSwitcher::Restore(uint32_t idx) const {
231 const uint32_t num_states = GetNumberOfStates();
232 if (idx >= num_states)
235 // See if we already are in this state?
236 if (m_currentState < num_states && (idx == m_currentState) &&
237 m_ttystates[idx].IsValid())
240 // Set the state to match the index passed in and only update the current
241 // state if there are no errors.
242 if (m_ttystates[idx].Restore()) {
243 m_currentState = idx;
247 // We failed to set the state. The tty state was invalid or not initialized.
251 //------------------------------------------------------------------
252 // Save the state at index "idx" for file descriptor "fd" and save the process
253 // group if requested.
255 // Returns true if the restore was successful, false otherwise.
256 //------------------------------------------------------------------
257 bool TerminalStateSwitcher::Save(uint32_t idx, int fd,
258 bool save_process_group) {
259 const uint32_t num_states = GetNumberOfStates();
260 if (idx < num_states)
261 return m_ttystates[idx].Save(fd, save_process_group);