Terminal.h revision 263363
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/lldb-private.h"
15#include "lldb/Host/Config.h"
16
17struct termios;
18
19namespace lldb_private {
20
21class Terminal
22{
23public:
24
25    Terminal (int fd = -1) :
26        m_fd (fd)
27    {
28    }
29
30    ~Terminal ()
31    {
32    }
33
34    bool
35    IsATerminal () const;
36
37    int
38    GetFileDescriptor () const
39    {
40        return m_fd;
41    }
42
43    void
44    SetFileDescriptor (int fd)
45    {
46        m_fd = fd;
47    }
48
49    bool
50    FileDescriptorIsValid () const
51    {
52        return m_fd != -1;
53    }
54
55    void
56    Clear ()
57    {
58        m_fd = -1;
59    }
60
61    bool
62    SetEcho (bool enabled);
63
64    bool
65    SetCanonical (bool enabled);
66
67protected:
68    int m_fd;   // This may or may not be a terminal file descriptor
69};
70
71
72//----------------------------------------------------------------------
73/// @class State Terminal.h "lldb/Host/Terminal.h"
74/// @brief A terminal state saving/restoring class.
75///
76/// This class can be used to remember the terminal state for a file
77/// descriptor and later restore that state as it originally was.
78//----------------------------------------------------------------------
79class TerminalState
80{
81public:
82    //------------------------------------------------------------------
83    /// Default constructor
84    //------------------------------------------------------------------
85    TerminalState();
86
87    //------------------------------------------------------------------
88    /// Destructor
89    //------------------------------------------------------------------
90    ~TerminalState();
91
92    //------------------------------------------------------------------
93    /// Save the TTY state for \a fd.
94    ///
95    /// Save the current state of the TTY for the file descriptor "fd"
96    /// and if "save_process_group" is true, attempt to save the process
97    /// group info for the TTY.
98    ///
99    /// @param[in] fd
100    ///     The file descriptor to save the state of.
101    ///
102    /// @param[in] save_process_group
103    ///     If \b true, save the process group settings, else do not
104    ///     save the process group setttings for a TTY.
105    ///
106    /// @return
107    ///     Returns \b true if \a fd describes a TTY and if the state
108    ///     was able to be saved, \b false otherwise.
109    //------------------------------------------------------------------
110    bool
111    Save (int fd, bool save_process_group);
112
113    //------------------------------------------------------------------
114    /// Restore the TTY state to the cached state.
115    ///
116    /// Restore the state of the TTY using the cached values from a
117    /// previous call to TerminalState::Save(int,bool).
118    ///
119    /// @return
120    ///     Returns \b true if the TTY state was successfully restored,
121    ///     \b false otherwise.
122    //------------------------------------------------------------------
123    bool
124    Restore () const;
125
126    //------------------------------------------------------------------
127    /// Test for valid cached TTY state information.
128    ///
129    /// @return
130    ///     Returns \b true if this object has valid saved TTY state
131    ///     settings that can be used to restore a previous state,
132    ///     \b false otherwise.
133    //------------------------------------------------------------------
134    bool
135    IsValid() const;
136
137    void
138    Clear ();
139
140protected:
141
142    //------------------------------------------------------------------
143    /// Test if tflags is valid.
144    ///
145    /// @return
146    ///     Returns \b true if \a m_tflags is valid and can be restored,
147    ///     \b false otherwise.
148    //------------------------------------------------------------------
149    bool
150    TFlagsIsValid() const;
151
152    //------------------------------------------------------------------
153    /// Test if ttystate is valid.
154    ///
155    /// @return
156    ///     Returns \b true if \a m_ttystate is valid and can be
157    ///     restored, \b false otherwise.
158    //------------------------------------------------------------------
159    bool
160    TTYStateIsValid() const;
161
162    //------------------------------------------------------------------
163    /// Test if the process group information is valid.
164    ///
165    /// @return
166    ///     Returns \b true if \a m_process_group is valid and can be
167    ///     restored, \b false otherwise.
168    //------------------------------------------------------------------
169    bool
170    ProcessGroupIsValid() const;
171
172    //------------------------------------------------------------------
173    // Member variables
174    //------------------------------------------------------------------
175    Terminal        m_tty;          ///< A terminal
176    int             m_tflags;       ///< Cached tflags information.
177#ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
178    std::unique_ptr<struct termios> m_termios_ap; ///< Cached terminal state information.
179#endif
180    lldb::pid_t     m_process_group;///< Cached process group information.
181
182};
183
184//----------------------------------------------------------------------
185/// @class TerminalStateSwitcher Terminal.h "lldb/Host/Terminal.h"
186/// @brief A TTY state switching class.
187///
188/// This class can be used to remember 2 TTY states for a given file
189/// descriptor and switch between the two states.
190//----------------------------------------------------------------------
191class TerminalStateSwitcher
192{
193public:
194    //------------------------------------------------------------------
195    /// Constructor
196    //------------------------------------------------------------------
197    TerminalStateSwitcher();
198
199    //------------------------------------------------------------------
200    /// Destructor
201    //------------------------------------------------------------------
202    ~TerminalStateSwitcher();
203
204    //------------------------------------------------------------------
205    /// Get the number of possible states to save.
206    ///
207    /// @return
208    ///     The number of states that this TTY switcher object contains.
209    //------------------------------------------------------------------
210    uint32_t
211    GetNumberOfStates() const;
212
213    //------------------------------------------------------------------
214    /// Restore the TTY state for state at index \a idx.
215    ///
216    /// @return
217    ///     Returns \b true if the TTY state was successfully restored,
218    ///     \b false otherwise.
219    //------------------------------------------------------------------
220    bool
221    Restore (uint32_t idx) const;
222
223    //------------------------------------------------------------------
224    /// Save the TTY state information for the state at index \a idx.
225    /// The TTY state is saved for the file descriptor \a fd and
226    /// the process group information will also be saved if requested
227    /// by \a save_process_group.
228    ///
229    /// @param[in] idx
230    ///     The index into the state array where the state should be
231    ///     saved.
232    ///
233    /// @param[in] fd
234    ///     The file descriptor for which to save the settings.
235    ///
236    /// @param[in] save_process_group
237    ///     If \b true, save the process group information for the TTY.
238    ///
239    /// @return
240    ///     Returns \b true if the save was successful, \b false
241    ///     otherwise.
242    //------------------------------------------------------------------
243    bool
244    Save (uint32_t idx, int fd, bool save_process_group);
245
246protected:
247    //------------------------------------------------------------------
248    // Member variables
249    //------------------------------------------------------------------
250    mutable uint32_t m_currentState; ///< The currently active TTY state index.
251    TerminalState m_ttystates[2]; ///< The array of TTY states that holds saved TTY info.
252};
253
254} // namespace lldb_private
255
256#endif  // #if defined(__cplusplus)
257#endif  // #ifndef liblldb_Terminal_h_
258