LLDB  mainline
Terminal.h
Go to the documentation of this file.
1 //===-- Terminal.h ----------------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef liblldb_Terminal_h_
10 #define liblldb_Terminal_h_
11 #if defined(__cplusplus)
12 
13 #include "lldb/Host/Config.h"
14 #include "lldb/lldb-private.h"
15 
16 struct termios;
17 
18 namespace lldb_private {
19 
20 class Terminal {
21 public:
22  Terminal(int fd = -1) : m_fd(fd) {}
23 
24  ~Terminal() {}
25 
26  bool IsATerminal() const;
27 
28  int GetFileDescriptor() const { return m_fd; }
29 
30  void SetFileDescriptor(int fd) { m_fd = fd; }
31 
32  bool FileDescriptorIsValid() const { return m_fd != -1; }
33 
34  void Clear() { m_fd = -1; }
35 
36  bool SetEcho(bool enabled);
37 
38  bool SetCanonical(bool enabled);
39 
40 protected:
41  int m_fd; // This may or may not be a terminal file descriptor
42 };
43 
44 /// \class State Terminal.h "lldb/Host/Terminal.h"
45 /// A terminal state saving/restoring class.
46 ///
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.
49 class TerminalState {
50 public:
51  /// Default constructor
52  TerminalState();
53 
54  /// Destructor
55  ~TerminalState();
56 
57  /// Save the TTY state for \a fd.
58  ///
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
61  /// the TTY.
62  ///
63  /// \param[in] fd
64  /// The file descriptor to save the state of.
65  ///
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.
69  ///
70  /// \return
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);
74 
75  /// Restore the TTY state to the cached state.
76  ///
77  /// Restore the state of the TTY using the cached values from a previous
78  /// call to TerminalState::Save(int,bool).
79  ///
80  /// \return
81  /// Returns \b true if the TTY state was successfully restored,
82  /// \b false otherwise.
83  bool Restore() const;
84 
85  /// Test for valid cached TTY state information.
86  ///
87  /// \return
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.
91  bool IsValid() const;
92 
93  void Clear();
94 
95 protected:
96  /// Test if tflags is valid.
97  ///
98  /// \return
99  /// Returns \b true if \a m_tflags is valid and can be restored,
100  /// \b false otherwise.
101  bool TFlagsIsValid() const;
102 
103  /// Test if ttystate is valid.
104  ///
105  /// \return
106  /// Returns \b true if \a m_ttystate is valid and can be
107  /// restored, \b false otherwise.
108  bool TTYStateIsValid() const;
109 
110  /// Test if the process group information is valid.
111  ///
112  /// \return
113  /// Returns \b true if \a m_process_group is valid and can be
114  /// restored, \b false otherwise.
115  bool ProcessGroupIsValid() const;
116 
117  // Member variables
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.
123 #endif
124  lldb::pid_t m_process_group; ///< Cached process group information.
125 };
126 
127 /// \class TerminalStateSwitcher Terminal.h "lldb/Host/Terminal.h"
128 /// A TTY state switching class.
129 ///
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 {
133 public:
134  /// Constructor
135  TerminalStateSwitcher();
136 
137  /// Destructor
138  ~TerminalStateSwitcher();
139 
140  /// Get the number of possible states to save.
141  ///
142  /// \return
143  /// The number of states that this TTY switcher object contains.
144  uint32_t GetNumberOfStates() const;
145 
146  /// Restore the TTY state for state at index \a idx.
147  ///
148  /// \return
149  /// Returns \b true if the TTY state was successfully restored,
150  /// \b false otherwise.
151  bool Restore(uint32_t idx) const;
152 
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.
156  ///
157  /// \param[in] idx
158  /// The index into the state array where the state should be
159  /// saved.
160  ///
161  /// \param[in] fd
162  /// The file descriptor for which to save the settings.
163  ///
164  /// \param[in] save_process_group
165  /// If \b true, save the process group information for the TTY.
166  ///
167  /// \return
168  /// Returns \b true if the save was successful, \b false
169  /// otherwise.
170  bool Save(uint32_t idx, int fd, bool save_process_group);
171 
172 protected:
173  // Member variables
174  mutable uint32_t m_currentState; ///< The currently active TTY state index.
175  TerminalState
176  m_ttystates[2]; ///< The array of TTY states that holds saved TTY info.
177 };
178 
179 } // namespace lldb_private
180 
181 #endif // #if defined(__cplusplus)
182 #endif // #ifndef liblldb_Terminal_h_
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
uint64_t pid_t
Definition: lldb-types.h:85