1254721Semaste//===-- Terminal.h ----------------------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#ifndef liblldb_Terminal_h_
11254721Semaste#define liblldb_Terminal_h_
12254721Semaste#if defined(__cplusplus)
13254721Semaste
14254721Semaste#include "lldb/lldb-private.h"
15263363Semaste#include "lldb/Host/Config.h"
16254721Semaste
17254721Semastestruct termios;
18254721Semaste
19254721Semastenamespace lldb_private {
20254721Semaste
21254721Semasteclass Terminal
22254721Semaste{
23254721Semastepublic:
24254721Semaste
25254721Semaste    Terminal (int fd = -1) :
26254721Semaste        m_fd (fd)
27254721Semaste    {
28254721Semaste    }
29254721Semaste
30254721Semaste    ~Terminal ()
31254721Semaste    {
32254721Semaste    }
33254721Semaste
34254721Semaste    bool
35254721Semaste    IsATerminal () const;
36254721Semaste
37254721Semaste    int
38254721Semaste    GetFileDescriptor () const
39254721Semaste    {
40254721Semaste        return m_fd;
41254721Semaste    }
42254721Semaste
43254721Semaste    void
44254721Semaste    SetFileDescriptor (int fd)
45254721Semaste    {
46254721Semaste        m_fd = fd;
47254721Semaste    }
48254721Semaste
49254721Semaste    bool
50254721Semaste    FileDescriptorIsValid () const
51254721Semaste    {
52254721Semaste        return m_fd != -1;
53254721Semaste    }
54254721Semaste
55254721Semaste    void
56254721Semaste    Clear ()
57254721Semaste    {
58254721Semaste        m_fd = -1;
59254721Semaste    }
60254721Semaste
61254721Semaste    bool
62254721Semaste    SetEcho (bool enabled);
63254721Semaste
64254721Semaste    bool
65254721Semaste    SetCanonical (bool enabled);
66254721Semaste
67254721Semasteprotected:
68254721Semaste    int m_fd;   // This may or may not be a terminal file descriptor
69254721Semaste};
70254721Semaste
71254721Semaste
72254721Semaste//----------------------------------------------------------------------
73254721Semaste/// @class State Terminal.h "lldb/Host/Terminal.h"
74254721Semaste/// @brief A terminal state saving/restoring class.
75254721Semaste///
76254721Semaste/// This class can be used to remember the terminal state for a file
77254721Semaste/// descriptor and later restore that state as it originally was.
78254721Semaste//----------------------------------------------------------------------
79254721Semasteclass TerminalState
80254721Semaste{
81254721Semastepublic:
82254721Semaste    //------------------------------------------------------------------
83254721Semaste    /// Default constructor
84254721Semaste    //------------------------------------------------------------------
85254721Semaste    TerminalState();
86254721Semaste
87254721Semaste    //------------------------------------------------------------------
88254721Semaste    /// Destructor
89254721Semaste    //------------------------------------------------------------------
90254721Semaste    ~TerminalState();
91254721Semaste
92254721Semaste    //------------------------------------------------------------------
93254721Semaste    /// Save the TTY state for \a fd.
94254721Semaste    ///
95254721Semaste    /// Save the current state of the TTY for the file descriptor "fd"
96254721Semaste    /// and if "save_process_group" is true, attempt to save the process
97254721Semaste    /// group info for the TTY.
98254721Semaste    ///
99254721Semaste    /// @param[in] fd
100254721Semaste    ///     The file descriptor to save the state of.
101254721Semaste    ///
102254721Semaste    /// @param[in] save_process_group
103254721Semaste    ///     If \b true, save the process group settings, else do not
104254721Semaste    ///     save the process group setttings for a TTY.
105254721Semaste    ///
106254721Semaste    /// @return
107254721Semaste    ///     Returns \b true if \a fd describes a TTY and if the state
108254721Semaste    ///     was able to be saved, \b false otherwise.
109254721Semaste    //------------------------------------------------------------------
110254721Semaste    bool
111254721Semaste    Save (int fd, bool save_process_group);
112254721Semaste
113254721Semaste    //------------------------------------------------------------------
114254721Semaste    /// Restore the TTY state to the cached state.
115254721Semaste    ///
116254721Semaste    /// Restore the state of the TTY using the cached values from a
117254721Semaste    /// previous call to TerminalState::Save(int,bool).
118254721Semaste    ///
119254721Semaste    /// @return
120254721Semaste    ///     Returns \b true if the TTY state was successfully restored,
121254721Semaste    ///     \b false otherwise.
122254721Semaste    //------------------------------------------------------------------
123254721Semaste    bool
124254721Semaste    Restore () const;
125254721Semaste
126254721Semaste    //------------------------------------------------------------------
127254721Semaste    /// Test for valid cached TTY state information.
128254721Semaste    ///
129254721Semaste    /// @return
130254721Semaste    ///     Returns \b true if this object has valid saved TTY state
131254721Semaste    ///     settings that can be used to restore a previous state,
132254721Semaste    ///     \b false otherwise.
133254721Semaste    //------------------------------------------------------------------
134254721Semaste    bool
135254721Semaste    IsValid() const;
136254721Semaste
137254721Semaste    void
138254721Semaste    Clear ();
139254721Semaste
140254721Semasteprotected:
141254721Semaste
142254721Semaste    //------------------------------------------------------------------
143254721Semaste    /// Test if tflags is valid.
144254721Semaste    ///
145254721Semaste    /// @return
146254721Semaste    ///     Returns \b true if \a m_tflags is valid and can be restored,
147254721Semaste    ///     \b false otherwise.
148254721Semaste    //------------------------------------------------------------------
149254721Semaste    bool
150254721Semaste    TFlagsIsValid() const;
151254721Semaste
152254721Semaste    //------------------------------------------------------------------
153254721Semaste    /// Test if ttystate is valid.
154254721Semaste    ///
155254721Semaste    /// @return
156254721Semaste    ///     Returns \b true if \a m_ttystate is valid and can be
157254721Semaste    ///     restored, \b false otherwise.
158254721Semaste    //------------------------------------------------------------------
159254721Semaste    bool
160254721Semaste    TTYStateIsValid() const;
161254721Semaste
162254721Semaste    //------------------------------------------------------------------
163254721Semaste    /// Test if the process group information is valid.
164254721Semaste    ///
165254721Semaste    /// @return
166254721Semaste    ///     Returns \b true if \a m_process_group is valid and can be
167254721Semaste    ///     restored, \b false otherwise.
168254721Semaste    //------------------------------------------------------------------
169254721Semaste    bool
170254721Semaste    ProcessGroupIsValid() const;
171254721Semaste
172254721Semaste    //------------------------------------------------------------------
173254721Semaste    // Member variables
174254721Semaste    //------------------------------------------------------------------
175254721Semaste    Terminal        m_tty;          ///< A terminal
176254721Semaste    int             m_tflags;       ///< Cached tflags information.
177263363Semaste#ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
178254721Semaste    std::unique_ptr<struct termios> m_termios_ap; ///< Cached terminal state information.
179263363Semaste#endif
180254721Semaste    lldb::pid_t     m_process_group;///< Cached process group information.
181254721Semaste
182254721Semaste};
183254721Semaste
184254721Semaste//----------------------------------------------------------------------
185254721Semaste/// @class TerminalStateSwitcher Terminal.h "lldb/Host/Terminal.h"
186254721Semaste/// @brief A TTY state switching class.
187254721Semaste///
188254721Semaste/// This class can be used to remember 2 TTY states for a given file
189254721Semaste/// descriptor and switch between the two states.
190254721Semaste//----------------------------------------------------------------------
191254721Semasteclass TerminalStateSwitcher
192254721Semaste{
193254721Semastepublic:
194254721Semaste    //------------------------------------------------------------------
195254721Semaste    /// Constructor
196254721Semaste    //------------------------------------------------------------------
197254721Semaste    TerminalStateSwitcher();
198254721Semaste
199254721Semaste    //------------------------------------------------------------------
200254721Semaste    /// Destructor
201254721Semaste    //------------------------------------------------------------------
202254721Semaste    ~TerminalStateSwitcher();
203254721Semaste
204254721Semaste    //------------------------------------------------------------------
205254721Semaste    /// Get the number of possible states to save.
206254721Semaste    ///
207254721Semaste    /// @return
208254721Semaste    ///     The number of states that this TTY switcher object contains.
209254721Semaste    //------------------------------------------------------------------
210254721Semaste    uint32_t
211254721Semaste    GetNumberOfStates() const;
212254721Semaste
213254721Semaste    //------------------------------------------------------------------
214254721Semaste    /// Restore the TTY state for state at index \a idx.
215254721Semaste    ///
216254721Semaste    /// @return
217254721Semaste    ///     Returns \b true if the TTY state was successfully restored,
218254721Semaste    ///     \b false otherwise.
219254721Semaste    //------------------------------------------------------------------
220254721Semaste    bool
221254721Semaste    Restore (uint32_t idx) const;
222254721Semaste
223254721Semaste    //------------------------------------------------------------------
224254721Semaste    /// Save the TTY state information for the state at index \a idx.
225254721Semaste    /// The TTY state is saved for the file descriptor \a fd and
226254721Semaste    /// the process group information will also be saved if requested
227254721Semaste    /// by \a save_process_group.
228254721Semaste    ///
229254721Semaste    /// @param[in] idx
230254721Semaste    ///     The index into the state array where the state should be
231254721Semaste    ///     saved.
232254721Semaste    ///
233254721Semaste    /// @param[in] fd
234254721Semaste    ///     The file descriptor for which to save the settings.
235254721Semaste    ///
236254721Semaste    /// @param[in] save_process_group
237254721Semaste    ///     If \b true, save the process group information for the TTY.
238254721Semaste    ///
239254721Semaste    /// @return
240254721Semaste    ///     Returns \b true if the save was successful, \b false
241254721Semaste    ///     otherwise.
242254721Semaste    //------------------------------------------------------------------
243254721Semaste    bool
244254721Semaste    Save (uint32_t idx, int fd, bool save_process_group);
245254721Semaste
246254721Semasteprotected:
247254721Semaste    //------------------------------------------------------------------
248254721Semaste    // Member variables
249254721Semaste    //------------------------------------------------------------------
250254721Semaste    mutable uint32_t m_currentState; ///< The currently active TTY state index.
251254721Semaste    TerminalState m_ttystates[2]; ///< The array of TTY states that holds saved TTY info.
252254721Semaste};
253254721Semaste
254254721Semaste} // namespace lldb_private
255254721Semaste
256254721Semaste#endif  // #if defined(__cplusplus)
257254721Semaste#endif  // #ifndef liblldb_Terminal_h_
258