1 //===-- Terminal.h ----------------------------------------------*- 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 #ifndef liblldb_Terminal_h_ 11 #define liblldb_Terminal_h_ 12 #if defined(__cplusplus) 13 14 #include "lldb/Host/Config.h" 15 #include "lldb/lldb-private.h" 16 17 struct termios; 18 19 namespace lldb_private { 20 21 class Terminal { 22 public: m_fd(fd)23 Terminal(int fd = -1) : m_fd(fd) {} 24 ~Terminal()25 ~Terminal() {} 26 27 bool IsATerminal() const; 28 GetFileDescriptor()29 int GetFileDescriptor() const { return m_fd; } 30 SetFileDescriptor(int fd)31 void SetFileDescriptor(int fd) { m_fd = fd; } 32 FileDescriptorIsValid()33 bool FileDescriptorIsValid() const { return m_fd != -1; } 34 Clear()35 void Clear() { m_fd = -1; } 36 37 bool SetEcho(bool enabled); 38 39 bool SetCanonical(bool enabled); 40 41 protected: 42 int m_fd; // This may or may not be a terminal file descriptor 43 }; 44 45 //---------------------------------------------------------------------- 46 /// @class State Terminal.h "lldb/Host/Terminal.h" 47 /// A terminal state saving/restoring class. 48 /// 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 //---------------------------------------------------------------------- 52 class TerminalState { 53 public: 54 //------------------------------------------------------------------ 55 /// Default constructor 56 //------------------------------------------------------------------ 57 TerminalState(); 58 59 //------------------------------------------------------------------ 60 /// Destructor 61 //------------------------------------------------------------------ 62 ~TerminalState(); 63 64 //------------------------------------------------------------------ 65 /// Save the TTY state for \a fd. 66 /// 67 /// Save the current state of the TTY for the file descriptor "fd" and if 68 /// "save_process_group" is true, attempt to save the process group info for 69 /// the TTY. 70 /// 71 /// @param[in] fd 72 /// The file descriptor to save the state of. 73 /// 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. 77 /// 78 /// @return 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); 83 84 //------------------------------------------------------------------ 85 /// Restore the TTY state to the cached state. 86 /// 87 /// Restore the state of the TTY using the cached values from a previous 88 /// call to TerminalState::Save(int,bool). 89 /// 90 /// @return 91 /// Returns \b true if the TTY state was successfully restored, 92 /// \b false otherwise. 93 //------------------------------------------------------------------ 94 bool Restore() const; 95 96 //------------------------------------------------------------------ 97 /// Test for valid cached TTY state information. 98 /// 99 /// @return 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; 105 106 void Clear(); 107 108 protected: 109 //------------------------------------------------------------------ 110 /// Test if tflags is valid. 111 /// 112 /// @return 113 /// Returns \b true if \a m_tflags is valid and can be restored, 114 /// \b false otherwise. 115 //------------------------------------------------------------------ 116 bool TFlagsIsValid() const; 117 118 //------------------------------------------------------------------ 119 /// Test if ttystate is valid. 120 /// 121 /// @return 122 /// Returns \b true if \a m_ttystate is valid and can be 123 /// restored, \b false otherwise. 124 //------------------------------------------------------------------ 125 bool TTYStateIsValid() const; 126 127 //------------------------------------------------------------------ 128 /// Test if the process group information is valid. 129 /// 130 /// @return 131 /// Returns \b true if \a m_process_group is valid and can be 132 /// restored, \b false otherwise. 133 //------------------------------------------------------------------ 134 bool ProcessGroupIsValid() const; 135 136 //------------------------------------------------------------------ 137 // Member variables 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. 144 #endif 145 lldb::pid_t m_process_group; ///< Cached process group information. 146 }; 147 148 //---------------------------------------------------------------------- 149 /// @class TerminalStateSwitcher Terminal.h "lldb/Host/Terminal.h" 150 /// A TTY state switching class. 151 /// 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 { 156 public: 157 //------------------------------------------------------------------ 158 /// Constructor 159 //------------------------------------------------------------------ 160 TerminalStateSwitcher(); 161 162 //------------------------------------------------------------------ 163 /// Destructor 164 //------------------------------------------------------------------ 165 ~TerminalStateSwitcher(); 166 167 //------------------------------------------------------------------ 168 /// Get the number of possible states to save. 169 /// 170 /// @return 171 /// The number of states that this TTY switcher object contains. 172 //------------------------------------------------------------------ 173 uint32_t GetNumberOfStates() const; 174 175 //------------------------------------------------------------------ 176 /// Restore the TTY state for state at index \a idx. 177 /// 178 /// @return 179 /// Returns \b true if the TTY state was successfully restored, 180 /// \b false otherwise. 181 //------------------------------------------------------------------ 182 bool Restore(uint32_t idx) const; 183 184 //------------------------------------------------------------------ 185 /// Save the TTY state information for the state at index \a idx. The TTY 186 /// state is saved for the file descriptor \a fd and the process group 187 /// information will also be saved if requested by \a save_process_group. 188 /// 189 /// @param[in] idx 190 /// The index into the state array where the state should be 191 /// saved. 192 /// 193 /// @param[in] fd 194 /// The file descriptor for which to save the settings. 195 /// 196 /// @param[in] save_process_group 197 /// If \b true, save the process group information for the TTY. 198 /// 199 /// @return 200 /// Returns \b true if the save was successful, \b false 201 /// otherwise. 202 //------------------------------------------------------------------ 203 bool Save(uint32_t idx, int fd, bool save_process_group); 204 205 protected: 206 //------------------------------------------------------------------ 207 // Member variables 208 //------------------------------------------------------------------ 209 mutable uint32_t m_currentState; ///< The currently active TTY state index. 210 TerminalState 211 m_ttystates[2]; ///< The array of TTY states that holds saved TTY info. 212 }; 213 214 } // namespace lldb_private 215 216 #endif // #if defined(__cplusplus) 217 #endif // #ifndef liblldb_Terminal_h_ 218