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"
15 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
20 using namespace lldb_private;
23 Terminal::IsATerminal () const
26 return m_fd >= 0 && ::isatty (m_fd);
31 Terminal::SetEcho (bool enabled)
33 if (FileDescriptorIsValid())
35 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
38 struct termios fd_termios;
39 if (::tcgetattr(m_fd, &fd_termios) == 0)
41 bool set_corectly = false;
44 if (fd_termios.c_lflag & ECHO)
47 fd_termios.c_lflag |= ECHO;
51 if (fd_termios.c_lflag & ECHO)
52 fd_termios.c_lflag &= ~ECHO;
59 return ::tcsetattr (m_fd, TCSANOW, &fd_termios) == 0;
62 #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
68 Terminal::SetCanonical (bool enabled)
70 if (FileDescriptorIsValid())
72 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
75 struct termios fd_termios;
76 if (::tcgetattr(m_fd, &fd_termios) == 0)
78 bool set_corectly = false;
81 if (fd_termios.c_lflag & ICANON)
84 fd_termios.c_lflag |= ICANON;
88 if (fd_termios.c_lflag & ICANON)
89 fd_termios.c_lflag &= ~ICANON;
96 return ::tcsetattr (m_fd, TCSANOW, &fd_termios) == 0;
99 #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
104 //----------------------------------------------------------------------
105 // Default constructor
106 //----------------------------------------------------------------------
107 TerminalState::TerminalState() :
110 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
117 //----------------------------------------------------------------------
119 //----------------------------------------------------------------------
120 TerminalState::~TerminalState()
125 TerminalState::Clear ()
129 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
130 m_termios_ap.reset();
132 m_process_group = -1;
135 //----------------------------------------------------------------------
136 // Save the current state of the TTY for the file descriptor "fd"
137 // and if "save_process_group" is true, attempt to save the process
138 // group info for the TTY.
139 //----------------------------------------------------------------------
141 TerminalState::Save (int fd, bool save_process_group)
143 m_tty.SetFileDescriptor(fd);
144 if (m_tty.IsATerminal())
146 #ifndef LLDB_DISABLE_POSIX
147 m_tflags = ::fcntl (fd, F_GETFL, 0);
149 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
150 if (m_termios_ap.get() == NULL)
151 m_termios_ap.reset (new struct termios);
152 int err = ::tcgetattr (fd, m_termios_ap.get());
154 m_termios_ap.reset();
155 #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
156 #ifndef LLDB_DISABLE_POSIX
157 if (save_process_group)
158 m_process_group = ::tcgetpgrp (0);
160 m_process_group = -1;
167 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
168 m_termios_ap.reset();
170 m_process_group = -1;
175 //----------------------------------------------------------------------
176 // Restore the state of the TTY using the cached values from a
177 // previous call to Save().
178 //----------------------------------------------------------------------
180 TerminalState::Restore () const
184 const int fd = m_tty.GetFileDescriptor();
185 #ifndef LLDB_DISABLE_POSIX
187 fcntl (fd, F_SETFL, m_tflags);
190 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
191 if (TTYStateIsValid())
192 tcsetattr (fd, TCSANOW, m_termios_ap.get());
193 #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
195 #ifndef LLDB_DISABLE_POSIX
196 if (ProcessGroupIsValid())
198 // Save the original signal handler.
199 void (*saved_sigttou_callback) (int) = NULL;
200 saved_sigttou_callback = (void (*)(int)) signal (SIGTTOU, SIG_IGN);
201 // Set the process group
202 tcsetpgrp (fd, m_process_group);
203 // Restore the original signal handler.
204 signal (SIGTTOU, saved_sigttou_callback);
215 //----------------------------------------------------------------------
216 // Returns true if this object has valid saved TTY state settings
217 // that can be used to restore a previous state.
218 //----------------------------------------------------------------------
220 TerminalState::IsValid() const
222 return m_tty.FileDescriptorIsValid () && (TFlagsIsValid() || TTYStateIsValid());
225 //----------------------------------------------------------------------
226 // Returns true if m_tflags is valid
227 //----------------------------------------------------------------------
229 TerminalState::TFlagsIsValid() const
231 return m_tflags != -1;
234 //----------------------------------------------------------------------
235 // Returns true if m_ttystate is valid
236 //----------------------------------------------------------------------
238 TerminalState::TTYStateIsValid() const
240 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
241 return m_termios_ap.get() != 0;
247 //----------------------------------------------------------------------
248 // Returns true if m_process_group is valid
249 //----------------------------------------------------------------------
251 TerminalState::ProcessGroupIsValid() const
253 return m_process_group != -1;
256 //------------------------------------------------------------------
258 //------------------------------------------------------------------
259 TerminalStateSwitcher::TerminalStateSwitcher () :
260 m_currentState(UINT32_MAX)
264 //------------------------------------------------------------------
266 //------------------------------------------------------------------
267 TerminalStateSwitcher::~TerminalStateSwitcher ()
271 //------------------------------------------------------------------
272 // Returns the number of states that this switcher contains
273 //------------------------------------------------------------------
275 TerminalStateSwitcher::GetNumberOfStates() const
277 return sizeof(m_ttystates)/sizeof(TerminalState);
280 //------------------------------------------------------------------
281 // Restore the state at index "idx".
283 // Returns true if the restore was successful, false otherwise.
284 //------------------------------------------------------------------
286 TerminalStateSwitcher::Restore (uint32_t idx) const
288 const uint32_t num_states = GetNumberOfStates();
289 if (idx >= num_states)
292 // See if we already are in this state?
293 if (m_currentState < num_states && (idx == m_currentState) && m_ttystates[idx].IsValid())
296 // Set the state to match the index passed in and only update the
297 // current state if there are no errors.
298 if (m_ttystates[idx].Restore())
300 m_currentState = idx;
304 // We failed to set the state. The tty state was invalid or not
309 //------------------------------------------------------------------
310 // Save the state at index "idx" for file descriptor "fd" and
311 // save the process group if requested.
313 // Returns true if the restore was successful, false otherwise.
314 //------------------------------------------------------------------
316 TerminalStateSwitcher::Save (uint32_t idx, int fd, bool save_process_group)
318 const uint32_t num_states = GetNumberOfStates();
319 if (idx < num_states)
320 return m_ttystates[idx].Save(fd, save_process_group);