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 "lldb/Host/Config.h"
17 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
22 using namespace lldb_private;
25 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() :
116 //----------------------------------------------------------------------
118 //----------------------------------------------------------------------
119 TerminalState::~TerminalState()
124 TerminalState::Clear ()
128 m_termios_ap.reset();
129 m_process_group = -1;
132 //----------------------------------------------------------------------
133 // Save the current state of the TTY for the file descriptor "fd"
134 // and if "save_process_group" is true, attempt to save the process
135 // group info for the TTY.
136 //----------------------------------------------------------------------
138 TerminalState::Save (int fd, bool save_process_group)
140 m_tty.SetFileDescriptor(fd);
141 if (m_tty.IsATerminal())
143 m_tflags = ::fcntl (fd, F_GETFL, 0);
144 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
145 if (m_termios_ap.get() == NULL)
146 m_termios_ap.reset (new struct termios);
147 int err = ::tcgetattr (fd, m_termios_ap.get());
149 m_termios_ap.reset();
150 #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
151 if (save_process_group)
152 m_process_group = ::tcgetpgrp (0);
154 m_process_group = -1;
160 m_termios_ap.reset();
161 m_process_group = -1;
166 //----------------------------------------------------------------------
167 // Restore the state of the TTY using the cached values from a
168 // previous call to Save().
169 //----------------------------------------------------------------------
171 TerminalState::Restore () const
175 const int fd = m_tty.GetFileDescriptor();
177 fcntl (fd, F_SETFL, m_tflags);
179 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
180 if (TTYStateIsValid())
181 tcsetattr (fd, TCSANOW, m_termios_ap.get());
182 #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
184 if (ProcessGroupIsValid())
186 // Save the original signal handler.
187 void (*saved_sigttou_callback) (int) = NULL;
188 saved_sigttou_callback = (void (*)(int)) signal (SIGTTOU, SIG_IGN);
189 // Set the process group
190 tcsetpgrp (fd, m_process_group);
191 // Restore the original signal handler.
192 signal (SIGTTOU, saved_sigttou_callback);
202 //----------------------------------------------------------------------
203 // Returns true if this object has valid saved TTY state settings
204 // that can be used to restore a previous state.
205 //----------------------------------------------------------------------
207 TerminalState::IsValid() const
209 return m_tty.FileDescriptorIsValid () && (TFlagsIsValid() || TTYStateIsValid());
212 //----------------------------------------------------------------------
213 // Returns true if m_tflags is valid
214 //----------------------------------------------------------------------
216 TerminalState::TFlagsIsValid() const
218 return m_tflags != -1;
221 //----------------------------------------------------------------------
222 // Returns true if m_ttystate is valid
223 //----------------------------------------------------------------------
225 TerminalState::TTYStateIsValid() const
227 return m_termios_ap.get() != 0;
230 //----------------------------------------------------------------------
231 // Returns true if m_process_group is valid
232 //----------------------------------------------------------------------
234 TerminalState::ProcessGroupIsValid() const
236 return m_process_group != -1;
239 //------------------------------------------------------------------
241 //------------------------------------------------------------------
242 TerminalStateSwitcher::TerminalStateSwitcher () :
243 m_currentState(UINT32_MAX)
247 //------------------------------------------------------------------
249 //------------------------------------------------------------------
250 TerminalStateSwitcher::~TerminalStateSwitcher ()
254 //------------------------------------------------------------------
255 // Returns the number of states that this switcher contains
256 //------------------------------------------------------------------
258 TerminalStateSwitcher::GetNumberOfStates() const
260 return sizeof(m_ttystates)/sizeof(TerminalState);
263 //------------------------------------------------------------------
264 // Restore the state at index "idx".
266 // Returns true if the restore was successful, false otherwise.
267 //------------------------------------------------------------------
269 TerminalStateSwitcher::Restore (uint32_t idx) const
271 const uint32_t num_states = GetNumberOfStates();
272 if (idx >= num_states)
275 // See if we already are in this state?
276 if (m_currentState < num_states && (idx == m_currentState) && m_ttystates[idx].IsValid())
279 // Set the state to match the index passed in and only update the
280 // current state if there are no errors.
281 if (m_ttystates[idx].Restore())
283 m_currentState = idx;
287 // We failed to set the state. The tty state was invalid or not
292 //------------------------------------------------------------------
293 // Save the state at index "idx" for file descriptor "fd" and
294 // save the process group if requested.
296 // Returns true if the restore was successful, false otherwise.
297 //------------------------------------------------------------------
299 TerminalStateSwitcher::Save (uint32_t idx, int fd, bool save_process_group)
301 const uint32_t num_states = GetNumberOfStates();
302 if (idx < num_states)
303 return m_ttystates[idx].Save(fd, save_process_group);