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"
18 namespace lldb_private {
24 Terminal (int fd = -1) :
37 GetFileDescriptor () const
43 SetFileDescriptor (int fd)
49 FileDescriptorIsValid () const
61 SetEcho (bool enabled);
64 SetCanonical (bool enabled);
67 int m_fd; // This may or may not be a terminal file descriptor
71 //----------------------------------------------------------------------
72 /// @class State Terminal.h "lldb/Host/Terminal.h"
73 /// @brief A terminal state saving/restoring class.
75 /// This class can be used to remember the terminal state for a file
76 /// descriptor and later restore that state as it originally was.
77 //----------------------------------------------------------------------
81 //------------------------------------------------------------------
82 /// Default constructor
83 //------------------------------------------------------------------
86 //------------------------------------------------------------------
88 //------------------------------------------------------------------
91 //------------------------------------------------------------------
92 /// Save the TTY state for \a fd.
94 /// Save the current state of the TTY for the file descriptor "fd"
95 /// and if "save_process_group" is true, attempt to save the process
96 /// group info for the TTY.
99 /// The file descriptor to save the state of.
101 /// @param[in] save_process_group
102 /// If \b true, save the process group settings, else do not
103 /// save the process group setttings for a TTY.
106 /// Returns \b true if \a fd describes a TTY and if the state
107 /// was able to be saved, \b false otherwise.
108 //------------------------------------------------------------------
110 Save (int fd, bool save_process_group);
112 //------------------------------------------------------------------
113 /// Restore the TTY state to the cached state.
115 /// Restore the state of the TTY using the cached values from a
116 /// previous call to TerminalState::Save(int,bool).
119 /// Returns \b true if the TTY state was successfully restored,
120 /// \b false otherwise.
121 //------------------------------------------------------------------
125 //------------------------------------------------------------------
126 /// Test for valid cached TTY state information.
129 /// Returns \b true if this object has valid saved TTY state
130 /// settings that can be used to restore a previous state,
131 /// \b false otherwise.
132 //------------------------------------------------------------------
141 //------------------------------------------------------------------
142 /// Test if tflags is valid.
145 /// Returns \b true if \a m_tflags is valid and can be restored,
146 /// \b false otherwise.
147 //------------------------------------------------------------------
149 TFlagsIsValid() const;
151 //------------------------------------------------------------------
152 /// Test if ttystate is valid.
155 /// Returns \b true if \a m_ttystate is valid and can be
156 /// restored, \b false otherwise.
157 //------------------------------------------------------------------
159 TTYStateIsValid() const;
161 //------------------------------------------------------------------
162 /// Test if the process group information is valid.
165 /// Returns \b true if \a m_process_group is valid and can be
166 /// restored, \b false otherwise.
167 //------------------------------------------------------------------
169 ProcessGroupIsValid() const;
171 //------------------------------------------------------------------
173 //------------------------------------------------------------------
174 Terminal m_tty; ///< A terminal
175 int m_tflags; ///< Cached tflags information.
176 std::unique_ptr<struct termios> m_termios_ap; ///< Cached terminal state information.
177 lldb::pid_t m_process_group;///< Cached process group information.
181 //----------------------------------------------------------------------
182 /// @class TerminalStateSwitcher Terminal.h "lldb/Host/Terminal.h"
183 /// @brief A TTY state switching class.
185 /// This class can be used to remember 2 TTY states for a given file
186 /// descriptor and switch between the two states.
187 //----------------------------------------------------------------------
188 class TerminalStateSwitcher
191 //------------------------------------------------------------------
193 //------------------------------------------------------------------
194 TerminalStateSwitcher();
196 //------------------------------------------------------------------
198 //------------------------------------------------------------------
199 ~TerminalStateSwitcher();
201 //------------------------------------------------------------------
202 /// Get the number of possible states to save.
205 /// The number of states that this TTY switcher object contains.
206 //------------------------------------------------------------------
208 GetNumberOfStates() const;
210 //------------------------------------------------------------------
211 /// Restore the TTY state for state at index \a idx.
214 /// Returns \b true if the TTY state was successfully restored,
215 /// \b false otherwise.
216 //------------------------------------------------------------------
218 Restore (uint32_t idx) const;
220 //------------------------------------------------------------------
221 /// Save the TTY state information for the state at index \a idx.
222 /// The TTY state is saved for the file descriptor \a fd and
223 /// the process group information will also be saved if requested
224 /// by \a save_process_group.
227 /// The index into the state array where the state should be
231 /// The file descriptor for which to save the settings.
233 /// @param[in] save_process_group
234 /// If \b true, save the process group information for the TTY.
237 /// Returns \b true if the save was successful, \b false
239 //------------------------------------------------------------------
241 Save (uint32_t idx, int fd, bool save_process_group);
244 //------------------------------------------------------------------
246 //------------------------------------------------------------------
247 mutable uint32_t m_currentState; ///< The currently active TTY state index.
248 TerminalState m_ttystates[2]; ///< The array of TTY states that holds saved TTY info.
251 } // namespace lldb_private
253 #endif // #if defined(__cplusplus)
254 #endif // #ifndef liblldb_Terminal_h_