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"
11 #include "llvm/ADT/STLExtras.h"
16 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
21 using namespace lldb_private;
24 Terminal::IsATerminal () const
27 return m_fd >= 0 && ::isatty (m_fd);
32 Terminal::SetEcho (bool enabled)
34 if (FileDescriptorIsValid())
36 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
39 struct termios fd_termios;
40 if (::tcgetattr(m_fd, &fd_termios) == 0)
42 bool set_corectly = false;
45 if (fd_termios.c_lflag & ECHO)
48 fd_termios.c_lflag |= ECHO;
52 if (fd_termios.c_lflag & ECHO)
53 fd_termios.c_lflag &= ~ECHO;
60 return ::tcsetattr (m_fd, TCSANOW, &fd_termios) == 0;
63 #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
69 Terminal::SetCanonical (bool enabled)
71 if (FileDescriptorIsValid())
73 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
76 struct termios fd_termios;
77 if (::tcgetattr(m_fd, &fd_termios) == 0)
79 bool set_corectly = false;
82 if (fd_termios.c_lflag & ICANON)
85 fd_termios.c_lflag |= ICANON;
89 if (fd_termios.c_lflag & ICANON)
90 fd_termios.c_lflag &= ~ICANON;
97 return ::tcsetattr (m_fd, TCSANOW, &fd_termios) == 0;
100 #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
105 //----------------------------------------------------------------------
106 // Default constructor
107 //----------------------------------------------------------------------
108 TerminalState::TerminalState() :
111 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
118 //----------------------------------------------------------------------
120 //----------------------------------------------------------------------
121 TerminalState::~TerminalState()
126 TerminalState::Clear ()
130 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
131 m_termios_ap.reset();
133 m_process_group = -1;
136 //----------------------------------------------------------------------
137 // Save the current state of the TTY for the file descriptor "fd"
138 // and if "save_process_group" is true, attempt to save the process
139 // group info for the TTY.
140 //----------------------------------------------------------------------
142 TerminalState::Save (int fd, bool save_process_group)
144 m_tty.SetFileDescriptor(fd);
145 if (m_tty.IsATerminal())
147 #ifndef LLDB_DISABLE_POSIX
148 m_tflags = ::fcntl (fd, F_GETFL, 0);
150 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
151 if (m_termios_ap.get() == NULL)
152 m_termios_ap.reset (new struct termios);
153 int err = ::tcgetattr (fd, m_termios_ap.get());
155 m_termios_ap.reset();
156 #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
157 #ifndef LLDB_DISABLE_POSIX
158 if (save_process_group)
159 m_process_group = ::tcgetpgrp (0);
161 m_process_group = -1;
168 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
169 m_termios_ap.reset();
171 m_process_group = -1;
176 //----------------------------------------------------------------------
177 // Restore the state of the TTY using the cached values from a
178 // previous call to Save().
179 //----------------------------------------------------------------------
181 TerminalState::Restore () const
183 #ifndef LLDB_DISABLE_POSIX
186 const int fd = m_tty.GetFileDescriptor();
188 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 if (ProcessGroupIsValid())
197 // Save the original signal handler.
198 void (*saved_sigttou_callback) (int) = NULL;
199 saved_sigttou_callback = (void (*)(int)) signal (SIGTTOU, SIG_IGN);
200 // Set the process group
201 tcsetpgrp (fd, m_process_group);
202 // Restore the original signal handler.
203 signal (SIGTTOU, saved_sigttou_callback);
214 //----------------------------------------------------------------------
215 // Returns true if this object has valid saved TTY state settings
216 // that can be used to restore a previous state.
217 //----------------------------------------------------------------------
219 TerminalState::IsValid() const
221 return m_tty.FileDescriptorIsValid () && (TFlagsIsValid() || TTYStateIsValid());
224 //----------------------------------------------------------------------
225 // Returns true if m_tflags is valid
226 //----------------------------------------------------------------------
228 TerminalState::TFlagsIsValid() const
230 return m_tflags != -1;
233 //----------------------------------------------------------------------
234 // Returns true if m_ttystate is valid
235 //----------------------------------------------------------------------
237 TerminalState::TTYStateIsValid() const
239 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
240 return m_termios_ap.get() != 0;
246 //----------------------------------------------------------------------
247 // Returns true if m_process_group is valid
248 //----------------------------------------------------------------------
250 TerminalState::ProcessGroupIsValid() const
252 return static_cast<int32_t>(m_process_group) != -1;
255 //------------------------------------------------------------------
257 //------------------------------------------------------------------
258 TerminalStateSwitcher::TerminalStateSwitcher () :
259 m_currentState(UINT32_MAX)
263 //------------------------------------------------------------------
265 //------------------------------------------------------------------
266 TerminalStateSwitcher::~TerminalStateSwitcher ()
270 //------------------------------------------------------------------
271 // Returns the number of states that this switcher contains
272 //------------------------------------------------------------------
274 TerminalStateSwitcher::GetNumberOfStates() const
276 return llvm::array_lengthof(m_ttystates);
279 //------------------------------------------------------------------
280 // Restore the state at index "idx".
282 // Returns true if the restore was successful, false otherwise.
283 //------------------------------------------------------------------
285 TerminalStateSwitcher::Restore (uint32_t idx) const
287 const uint32_t num_states = GetNumberOfStates();
288 if (idx >= num_states)
291 // See if we already are in this state?
292 if (m_currentState < num_states && (idx == m_currentState) && m_ttystates[idx].IsValid())
295 // Set the state to match the index passed in and only update the
296 // current state if there are no errors.
297 if (m_ttystates[idx].Restore())
299 m_currentState = idx;
303 // We failed to set the state. The tty state was invalid or not
308 //------------------------------------------------------------------
309 // Save the state at index "idx" for file descriptor "fd" and
310 // save the process group if requested.
312 // Returns true if the restore was successful, false otherwise.
313 //------------------------------------------------------------------
315 TerminalStateSwitcher::Save (uint32_t idx, int fd, bool save_process_group)
317 const uint32_t num_states = GetNumberOfStates();
318 if (idx < num_states)
319 return m_ttystates[idx].Save(fd, save_process_group);