]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Host/Terminal.h
Merge llvm, clang, lld and lldb trunk r300890, and update build glue.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Host / Terminal.h
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:
23   Terminal(int fd = -1) : m_fd(fd) {}
24
25   ~Terminal() {}
26
27   bool IsATerminal() const;
28
29   int GetFileDescriptor() const { return m_fd; }
30
31   void SetFileDescriptor(int fd) { m_fd = fd; }
32
33   bool FileDescriptorIsValid() const { return m_fd != -1; }
34
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 /// @brief 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"
68   /// and if "save_process_group" is true, attempt to save the process
69   /// group info for 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
88   /// previous 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 /// @brief 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.
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.
189   ///
190   /// @param[in] idx
191   ///     The index into the state array where the state should be
192   ///     saved.
193   ///
194   /// @param[in] fd
195   ///     The file descriptor for which to save the settings.
196   ///
197   /// @param[in] save_process_group
198   ///     If \b true, save the process group information for the TTY.
199   ///
200   /// @return
201   ///     Returns \b true if the save was successful, \b false
202   ///     otherwise.
203   //------------------------------------------------------------------
204   bool Save(uint32_t idx, int fd, bool save_process_group);
205
206 protected:
207   //------------------------------------------------------------------
208   // Member variables
209   //------------------------------------------------------------------
210   mutable uint32_t m_currentState; ///< The currently active TTY state index.
211   TerminalState
212       m_ttystates[2]; ///< The array of TTY states that holds saved TTY info.
213 };
214
215 } // namespace lldb_private
216
217 #endif // #if defined(__cplusplus)
218 #endif // #ifndef liblldb_Terminal_h_