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/Host/Config.h"
15 #include "lldb/lldb-private.h"
19 namespace lldb_private {
23 Terminal(int fd = -1) : m_fd(fd) {}
27 bool IsATerminal() const;
29 int GetFileDescriptor() const { return m_fd; }
31 void SetFileDescriptor(int fd) { m_fd = fd; }
33 bool FileDescriptorIsValid() const { return m_fd != -1; }
35 void Clear() { m_fd = -1; }
37 bool SetEcho(bool enabled);
39 bool SetCanonical(bool enabled);
42 int m_fd; // This may or may not be a terminal file descriptor
45 //----------------------------------------------------------------------
46 /// @class State Terminal.h "lldb/Host/Terminal.h"
47 /// @brief A terminal state saving/restoring class.
49 /// This class can be used to remember the terminal state for a file
50 /// descriptor and later restore that state as it originally was.
51 //----------------------------------------------------------------------
54 //------------------------------------------------------------------
55 /// Default constructor
56 //------------------------------------------------------------------
59 //------------------------------------------------------------------
61 //------------------------------------------------------------------
64 //------------------------------------------------------------------
65 /// Save the TTY state for \a fd.
67 /// Save the current state of the TTY for the file descriptor "fd"
68 /// and if "save_process_group" is true, attempt to save the process
69 /// group info for the TTY.
72 /// The file descriptor to save the state of.
74 /// @param[in] save_process_group
75 /// If \b true, save the process group settings, else do not
76 /// save the process group settings for a TTY.
79 /// Returns \b true if \a fd describes a TTY and if the state
80 /// was able to be saved, \b false otherwise.
81 //------------------------------------------------------------------
82 bool Save(int fd, bool save_process_group);
84 //------------------------------------------------------------------
85 /// Restore the TTY state to the cached state.
87 /// Restore the state of the TTY using the cached values from a
88 /// previous call to TerminalState::Save(int,bool).
91 /// Returns \b true if the TTY state was successfully restored,
92 /// \b false otherwise.
93 //------------------------------------------------------------------
96 //------------------------------------------------------------------
97 /// Test for valid cached TTY state information.
100 /// Returns \b true if this object has valid saved TTY state
101 /// settings that can be used to restore a previous state,
102 /// \b false otherwise.
103 //------------------------------------------------------------------
104 bool IsValid() const;
109 //------------------------------------------------------------------
110 /// Test if tflags is valid.
113 /// Returns \b true if \a m_tflags is valid and can be restored,
114 /// \b false otherwise.
115 //------------------------------------------------------------------
116 bool TFlagsIsValid() const;
118 //------------------------------------------------------------------
119 /// Test if ttystate is valid.
122 /// Returns \b true if \a m_ttystate is valid and can be
123 /// restored, \b false otherwise.
124 //------------------------------------------------------------------
125 bool TTYStateIsValid() const;
127 //------------------------------------------------------------------
128 /// Test if the process group information is valid.
131 /// Returns \b true if \a m_process_group is valid and can be
132 /// restored, \b false otherwise.
133 //------------------------------------------------------------------
134 bool ProcessGroupIsValid() const;
136 //------------------------------------------------------------------
138 //------------------------------------------------------------------
139 Terminal m_tty; ///< A terminal
140 int m_tflags; ///< Cached tflags information.
141 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
142 std::unique_ptr<struct termios>
143 m_termios_ap; ///< Cached terminal state information.
145 lldb::pid_t m_process_group; ///< Cached process group information.
148 //----------------------------------------------------------------------
149 /// @class TerminalStateSwitcher Terminal.h "lldb/Host/Terminal.h"
150 /// @brief A TTY state switching class.
152 /// This class can be used to remember 2 TTY states for a given file
153 /// descriptor and switch between the two states.
154 //----------------------------------------------------------------------
155 class TerminalStateSwitcher {
157 //------------------------------------------------------------------
159 //------------------------------------------------------------------
160 TerminalStateSwitcher();
162 //------------------------------------------------------------------
164 //------------------------------------------------------------------
165 ~TerminalStateSwitcher();
167 //------------------------------------------------------------------
168 /// Get the number of possible states to save.
171 /// The number of states that this TTY switcher object contains.
172 //------------------------------------------------------------------
173 uint32_t GetNumberOfStates() const;
175 //------------------------------------------------------------------
176 /// Restore the TTY state for state at index \a idx.
179 /// Returns \b true if the TTY state was successfully restored,
180 /// \b false otherwise.
181 //------------------------------------------------------------------
182 bool Restore(uint32_t idx) const;
184 //------------------------------------------------------------------
185 /// Save the TTY state information for the state at index \a idx.
186 /// The TTY state is saved for the file descriptor \a fd and
187 /// the process group information will also be saved if requested
188 /// by \a save_process_group.
191 /// The index into the state array where the state should be
195 /// The file descriptor for which to save the settings.
197 /// @param[in] save_process_group
198 /// If \b true, save the process group information for the TTY.
201 /// Returns \b true if the save was successful, \b false
203 //------------------------------------------------------------------
204 bool Save(uint32_t idx, int fd, bool save_process_group);
207 //------------------------------------------------------------------
209 //------------------------------------------------------------------
210 mutable uint32_t m_currentState; ///< The currently active TTY state index.
212 m_ttystates[2]; ///< The array of TTY states that holds saved TTY info.
215 } // namespace lldb_private
217 #endif // #if defined(__cplusplus)
218 #endif // #ifndef liblldb_Terminal_h_