1 //===-- Terminal.h ----------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_Terminal_h_
10 #define liblldb_Terminal_h_
11 #if defined(__cplusplus)
13 #include "lldb/Host/Config.h"
14 #include "lldb/lldb-private.h"
18 namespace lldb_private {
22 Terminal(int fd = -1) : m_fd(fd) {}
26 bool IsATerminal() const;
28 int GetFileDescriptor() const { return m_fd; }
30 void SetFileDescriptor(int fd) { m_fd = fd; }
32 bool FileDescriptorIsValid() const { return m_fd != -1; }
34 void Clear() { m_fd = -1; }
36 bool SetEcho(bool enabled);
38 bool SetCanonical(bool enabled);
41 int m_fd; // This may or may not be a terminal file descriptor
44 /// \class State Terminal.h "lldb/Host/Terminal.h"
45 /// A terminal state saving/restoring class.
47 /// This class can be used to remember the terminal state for a file
48 /// descriptor and later restore that state as it originally was.
51 /// Default constructor
57 /// Save the TTY state for \a fd.
59 /// Save the current state of the TTY for the file descriptor "fd" and if
60 /// "save_process_group" is true, attempt to save the process group info for
64 /// The file descriptor to save the state of.
66 /// \param[in] save_process_group
67 /// If \b true, save the process group settings, else do not
68 /// save the process group settings for a TTY.
71 /// Returns \b true if \a fd describes a TTY and if the state
72 /// was able to be saved, \b false otherwise.
73 bool Save(int fd, bool save_process_group);
75 /// Restore the TTY state to the cached state.
77 /// Restore the state of the TTY using the cached values from a previous
78 /// call to TerminalState::Save(int,bool).
81 /// Returns \b true if the TTY state was successfully restored,
82 /// \b false otherwise.
85 /// Test for valid cached TTY state information.
88 /// Returns \b true if this object has valid saved TTY state
89 /// settings that can be used to restore a previous state,
90 /// \b false otherwise.
96 /// Test if tflags is valid.
99 /// Returns \b true if \a m_tflags is valid and can be restored,
100 /// \b false otherwise.
101 bool TFlagsIsValid() const;
103 /// Test if ttystate is valid.
106 /// Returns \b true if \a m_ttystate is valid and can be
107 /// restored, \b false otherwise.
108 bool TTYStateIsValid() const;
110 /// Test if the process group information is valid.
113 /// Returns \b true if \a m_process_group is valid and can be
114 /// restored, \b false otherwise.
115 bool ProcessGroupIsValid() const;
118 Terminal m_tty; ///< A terminal
119 int m_tflags; ///< Cached tflags information.
120 #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
121 std::unique_ptr<struct termios>
122 m_termios_up; ///< Cached terminal state information.
124 lldb::pid_t m_process_group; ///< Cached process group information.
127 /// \class TerminalStateSwitcher Terminal.h "lldb/Host/Terminal.h"
128 /// A TTY state switching class.
130 /// This class can be used to remember 2 TTY states for a given file
131 /// descriptor and switch between the two states.
132 class TerminalStateSwitcher {
135 TerminalStateSwitcher();
138 ~TerminalStateSwitcher();
140 /// Get the number of possible states to save.
143 /// The number of states that this TTY switcher object contains.
144 uint32_t GetNumberOfStates() const;
146 /// Restore the TTY state for state at index \a idx.
149 /// Returns \b true if the TTY state was successfully restored,
150 /// \b false otherwise.
151 bool Restore(uint32_t idx) const;
153 /// Save the TTY state information for the state at index \a idx. The TTY
154 /// state is saved for the file descriptor \a fd and the process group
155 /// information will also be saved if requested by \a save_process_group.
158 /// The index into the state array where the state should be
162 /// The file descriptor for which to save the settings.
164 /// \param[in] save_process_group
165 /// If \b true, save the process group information for the TTY.
168 /// Returns \b true if the save was successful, \b false
170 bool Save(uint32_t idx, int fd, bool save_process_group);
174 mutable uint32_t m_currentState; ///< The currently active TTY state index.
176 m_ttystates[2]; ///< The array of TTY states that holds saved TTY info.
179 } // namespace lldb_private
181 #endif // #if defined(__cplusplus)
182 #endif // #ifndef liblldb_Terminal_h_