1 //===-- Terminal.h ----------------------------------------------*- 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 #ifndef liblldb_Terminal_h_
11 #define liblldb_Terminal_h_
12 #if defined(__cplusplus)
14 #include "lldb/lldb-private.h"
15 #include "lldb/Host/Config.h"
19 namespace lldb_private {
25 Terminal (int fd = -1) :
38 GetFileDescriptor () const
44 SetFileDescriptor (int fd)
50 FileDescriptorIsValid () const
62 SetEcho (bool enabled);
65 SetCanonical (bool enabled);
68 int m_fd; // This may or may not be a terminal file descriptor
72 //----------------------------------------------------------------------
73 /// @class State Terminal.h "lldb/Host/Terminal.h"
74 /// @brief A terminal state saving/restoring class.
76 /// This class can be used to remember the terminal state for a file
77 /// descriptor and later restore that state as it originally was.
78 //----------------------------------------------------------------------
82 //------------------------------------------------------------------
83 /// Default constructor
84 //------------------------------------------------------------------
87 //------------------------------------------------------------------
89 //------------------------------------------------------------------
92 //------------------------------------------------------------------
93 /// Save the TTY state for \a fd.
95 /// Save the current state of the TTY for the file descriptor "fd"
96 /// and if "save_process_group" is true, attempt to save the process
97 /// group info for the TTY.
100 /// The file descriptor to save the state of.
102 /// @param[in] save_process_group
103 /// If \b true, save the process group settings, else do not
104 /// save the process group settings for a TTY.
107 /// Returns \b true if \a fd describes a TTY and if the state
108 /// was able to be saved, \b false otherwise.
109 //------------------------------------------------------------------
111 Save (int fd, bool save_process_group);
113 //------------------------------------------------------------------
114 /// Restore the TTY state to the cached state.
116 /// Restore the state of the TTY using the cached values from a
117 /// previous call to TerminalState::Save(int,bool).
120 /// Returns \b true if the TTY state was successfully restored,
121 /// \b false otherwise.
122 //------------------------------------------------------------------
126 //------------------------------------------------------------------
127 /// Test for valid cached TTY state information.
130 /// Returns \b true if this object has valid saved TTY state
131 /// settings that can be used to restore a previous state,
132 /// \b false otherwise.
133 //------------------------------------------------------------------
142 //------------------------------------------------------------------
143 /// Test if tflags is valid.
146 /// Returns \b true if \a m_tflags is valid and can be restored,
147 /// \b false otherwise.
148 //------------------------------------------------------------------
150 TFlagsIsValid() const;
152 //------------------------------------------------------------------
153 /// Test if ttystate is valid.
156 /// Returns \b true if \a m_ttystate is valid and can be
157 /// restored, \b false otherwise.
158 //------------------------------------------------------------------
160 TTYStateIsValid() const;
162 //------------------------------------------------------------------
163 /// Test if the process group information is valid.
166 /// Returns \b true if \a m_process_group is valid and can be
167 /// restored, \b false otherwise.
168 //------------------------------------------------------------------
170 ProcessGroupIsValid() const;
172 //------------------------------------------------------------------
174 //------------------------------------------------------------------
175 Terminal m_tty; ///< A terminal
176 int m_tflags; ///< Cached tflags information.
177 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
178 std::unique_ptr<struct termios> m_termios_ap; ///< Cached terminal state information.
180 lldb::pid_t m_process_group;///< Cached process group information.
184 //----------------------------------------------------------------------
185 /// @class TerminalStateSwitcher Terminal.h "lldb/Host/Terminal.h"
186 /// @brief A TTY state switching class.
188 /// This class can be used to remember 2 TTY states for a given file
189 /// descriptor and switch between the two states.
190 //----------------------------------------------------------------------
191 class TerminalStateSwitcher
194 //------------------------------------------------------------------
196 //------------------------------------------------------------------
197 TerminalStateSwitcher();
199 //------------------------------------------------------------------
201 //------------------------------------------------------------------
202 ~TerminalStateSwitcher();
204 //------------------------------------------------------------------
205 /// Get the number of possible states to save.
208 /// The number of states that this TTY switcher object contains.
209 //------------------------------------------------------------------
211 GetNumberOfStates() const;
213 //------------------------------------------------------------------
214 /// Restore the TTY state for state at index \a idx.
217 /// Returns \b true if the TTY state was successfully restored,
218 /// \b false otherwise.
219 //------------------------------------------------------------------
221 Restore (uint32_t idx) const;
223 //------------------------------------------------------------------
224 /// Save the TTY state information for the state at index \a idx.
225 /// The TTY state is saved for the file descriptor \a fd and
226 /// the process group information will also be saved if requested
227 /// by \a save_process_group.
230 /// The index into the state array where the state should be
234 /// The file descriptor for which to save the settings.
236 /// @param[in] save_process_group
237 /// If \b true, save the process group information for the TTY.
240 /// Returns \b true if the save was successful, \b false
242 //------------------------------------------------------------------
244 Save (uint32_t idx, int fd, bool save_process_group);
247 //------------------------------------------------------------------
249 //------------------------------------------------------------------
250 mutable uint32_t m_currentState; ///< The currently active TTY state index.
251 TerminalState m_ttystates[2]; ///< The array of TTY states that holds saved TTY info.
254 } // namespace lldb_private
256 #endif // #if defined(__cplusplus)
257 #endif // #ifndef liblldb_Terminal_h_