1 //===-- Terminal.cpp --------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Host/Terminal.h"
11 #include "lldb/Host/Config.h"
12 
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include <signal.h>
16 
17 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
18 #include <termios.h>
19 #endif
20 
21 
22 using namespace lldb_private;
23 
24 bool
25 Terminal::IsATerminal () const
26 {
27     return m_fd >= 0 && ::isatty (m_fd);
28 }
29 
30 
31 bool
32 Terminal::SetEcho (bool enabled)
33 {
34     if (FileDescriptorIsValid())
35     {
36 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
37         if (IsATerminal ())
38         {
39             struct termios fd_termios;
40             if (::tcgetattr(m_fd, &fd_termios) == 0)
41             {
42                 bool set_corectly = false;
43                 if (enabled)
44                 {
45                     if (fd_termios.c_lflag & ECHO)
46                         set_corectly = true;
47                     else
48                         fd_termios.c_lflag |= ECHO;
49                 }
50                 else
51                 {
52                     if (fd_termios.c_lflag & ECHO)
53                         fd_termios.c_lflag &= ~ECHO;
54                     else
55                         set_corectly = true;
56                 }
57 
58                 if (set_corectly)
59                     return true;
60                 return ::tcsetattr (m_fd, TCSANOW, &fd_termios) == 0;
61             }
62         }
63 #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
64     }
65     return false;
66 }
67 
68 bool
69 Terminal::SetCanonical (bool enabled)
70 {
71     if (FileDescriptorIsValid())
72     {
73 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
74         if (IsATerminal ())
75         {
76             struct termios fd_termios;
77             if (::tcgetattr(m_fd, &fd_termios) == 0)
78             {
79                 bool set_corectly = false;
80                 if (enabled)
81                 {
82                     if (fd_termios.c_lflag & ICANON)
83                         set_corectly = true;
84                     else
85                         fd_termios.c_lflag |= ICANON;
86                 }
87                 else
88                 {
89                     if (fd_termios.c_lflag & ICANON)
90                         fd_termios.c_lflag &= ~ICANON;
91                     else
92                         set_corectly = true;
93                 }
94 
95                 if (set_corectly)
96                     return true;
97                 return ::tcsetattr (m_fd, TCSANOW, &fd_termios) == 0;
98             }
99         }
100 #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
101     }
102     return false;
103 }
104 
105 //----------------------------------------------------------------------
106 // Default constructor
107 //----------------------------------------------------------------------
108 TerminalState::TerminalState() :
109     m_tty(),
110     m_tflags(-1),
111     m_termios_ap(),
112     m_process_group(-1)
113 {
114 }
115 
116 //----------------------------------------------------------------------
117 // Destructor
118 //----------------------------------------------------------------------
119 TerminalState::~TerminalState()
120 {
121 }
122 
123 //----------------------------------------------------------------------
124 // Save the current state of the TTY for the file descriptor "fd"
125 // and if "save_process_group" is true, attempt to save the process
126 // group info for the TTY.
127 //----------------------------------------------------------------------
128 bool
129 TerminalState::Save (int fd, bool save_process_group)
130 {
131     m_tty.SetFileDescriptor(fd);
132     if (m_tty.IsATerminal())
133     {
134         m_tflags = ::fcntl (fd, F_GETFL, 0);
135 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
136         if (m_termios_ap.get() == NULL)
137             m_termios_ap.reset (new struct termios);
138         int err = ::tcgetattr (fd, m_termios_ap.get());
139         if (err != 0)
140             m_termios_ap.reset();
141 #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
142         if (save_process_group)
143             m_process_group = ::tcgetpgrp (0);
144         else
145             m_process_group = -1;
146     }
147     else
148     {
149         m_tty.Clear();
150         m_tflags = -1;
151         m_termios_ap.reset();
152         m_process_group = -1;
153     }
154     return IsValid();
155 }
156 
157 //----------------------------------------------------------------------
158 // Restore the state of the TTY using the cached values from a
159 // previous call to Save().
160 //----------------------------------------------------------------------
161 bool
162 TerminalState::Restore () const
163 {
164     if (IsValid())
165     {
166         const int fd = m_tty.GetFileDescriptor();
167         if (TFlagsIsValid())
168             fcntl (fd, F_SETFL, m_tflags);
169 
170 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
171         if (TTYStateIsValid())
172             tcsetattr (fd, TCSANOW, m_termios_ap.get());
173 #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
174 
175         if (ProcessGroupIsValid())
176         {
177             // Save the original signal handler.
178             void (*saved_sigttou_callback) (int) = NULL;
179             saved_sigttou_callback = (void (*)(int)) signal (SIGTTOU, SIG_IGN);
180             // Set the process group
181             tcsetpgrp (fd, m_process_group);
182             // Restore the original signal handler.
183             signal (SIGTTOU, saved_sigttou_callback);
184         }
185         return true;
186     }
187     return false;
188 }
189 
190 
191 
192 
193 //----------------------------------------------------------------------
194 // Returns true if this object has valid saved TTY state settings
195 // that can be used to restore a previous state.
196 //----------------------------------------------------------------------
197 bool
198 TerminalState::IsValid() const
199 {
200     return m_tty.FileDescriptorIsValid () && (TFlagsIsValid() || TTYStateIsValid());
201 }
202 
203 //----------------------------------------------------------------------
204 // Returns true if m_tflags is valid
205 //----------------------------------------------------------------------
206 bool
207 TerminalState::TFlagsIsValid() const
208 {
209     return m_tflags != -1;
210 }
211 
212 //----------------------------------------------------------------------
213 // Returns true if m_ttystate is valid
214 //----------------------------------------------------------------------
215 bool
216 TerminalState::TTYStateIsValid() const
217 {
218     return m_termios_ap.get() != 0;
219 }
220 
221 //----------------------------------------------------------------------
222 // Returns true if m_process_group is valid
223 //----------------------------------------------------------------------
224 bool
225 TerminalState::ProcessGroupIsValid() const
226 {
227     return m_process_group != -1;
228 }
229 
230 //------------------------------------------------------------------
231 // Constructor
232 //------------------------------------------------------------------
233 TerminalStateSwitcher::TerminalStateSwitcher () :
234     m_currentState(UINT32_MAX)
235 {
236 }
237 
238 //------------------------------------------------------------------
239 // Destructor
240 //------------------------------------------------------------------
241 TerminalStateSwitcher::~TerminalStateSwitcher ()
242 {
243 }
244 
245 //------------------------------------------------------------------
246 // Returns the number of states that this switcher contains
247 //------------------------------------------------------------------
248 uint32_t
249 TerminalStateSwitcher::GetNumberOfStates() const
250 {
251     return sizeof(m_ttystates)/sizeof(TerminalState);
252 }
253 
254 //------------------------------------------------------------------
255 // Restore the state at index "idx".
256 //
257 // Returns true if the restore was successful, false otherwise.
258 //------------------------------------------------------------------
259 bool
260 TerminalStateSwitcher::Restore (uint32_t idx) const
261 {
262     const uint32_t num_states = GetNumberOfStates();
263     if (idx >= num_states)
264         return false;
265 
266     // See if we already are in this state?
267     if (m_currentState < num_states && (idx == m_currentState) && m_ttystates[idx].IsValid())
268         return true;
269 
270     // Set the state to match the index passed in and only update the
271     // current state if there are no errors.
272     if (m_ttystates[idx].Restore())
273     {
274         m_currentState = idx;
275         return true;
276     }
277 
278     // We failed to set the state. The tty state was invalid or not
279     // initialized.
280     return false;
281 }
282 
283 //------------------------------------------------------------------
284 // Save the state at index "idx" for file descriptor "fd" and
285 // save the process group if requested.
286 //
287 // Returns true if the restore was successful, false otherwise.
288 //------------------------------------------------------------------
289 bool
290 TerminalStateSwitcher::Save (uint32_t idx, int fd, bool save_process_group)
291 {
292     const uint32_t num_states = GetNumberOfStates();
293     if (idx < num_states)
294         return m_ttystates[idx].Save(fd, save_process_group);
295     return false;
296 }
297 
298 
299