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 LLDB_HOST_TERMINAL_H
10#define LLDB_HOST_TERMINAL_H
11
12#include "lldb/lldb-private.h"
13#include "llvm/Support/Error.h"
14
15namespace lldb_private {
16
17class TerminalState;
18
19class Terminal {
20public:
21 enum class Parity {
27 };
28
29 enum class ParityCheck {
30 // No parity checking
32 // Replace erraneous bytes with NUL
34 // Ignore erraneous bytes
36 // Mark erraneous bytes by prepending them with \xFF\x00; real \xFF
37 // is escaped to \xFF\xFF
39 };
40
41 Terminal(int fd = -1) : m_fd(fd) {}
42
43 ~Terminal() = default;
44
45 bool IsATerminal() const;
46
47 int GetFileDescriptor() const { return m_fd; }
48
49 void SetFileDescriptor(int fd) { m_fd = fd; }
50
51 bool FileDescriptorIsValid() const { return m_fd != -1; }
52
53 void Clear() { m_fd = -1; }
54
55 llvm::Error SetEcho(bool enabled);
56
57 llvm::Error SetCanonical(bool enabled);
58
59 llvm::Error SetRaw();
60
61 llvm::Error SetBaudRate(unsigned int baud_rate);
62
63 llvm::Error SetStopBits(unsigned int stop_bits);
64
65 llvm::Error SetParity(Parity parity);
66
67 llvm::Error SetParityCheck(ParityCheck parity_check);
68
69 llvm::Error SetHardwareFlowControl(bool enabled);
70
71 /// Returns whether or not the current terminal supports Unicode rendering.
72 ///
73 /// The value is cached after the first computation.
74 ///
75 /// On POSIX systems, we check if the LANG environment variable contains the
76 /// substring "UTF-8", case insensitive.
77 ///
78 /// On Windows, we always return true since we use the `WriteConsoleW` API
79 /// internally. Note that the default Windows codepage (437) does not support
80 /// all Unicode characters. This function does not check the codepage.
81 static bool SupportsUnicode();
82
83protected:
84 struct Data;
85
86 int m_fd; // This may or may not be a terminal file descriptor
87
88 llvm::Expected<Data> GetData();
89 llvm::Error SetData(const Data &data);
90
91 friend class TerminalState;
92};
93
94/// \class TerminalState Terminal.h "lldb/Host/Terminal.h"
95/// A RAII-friendly terminal state saving/restoring class.
96///
97/// This class can be used to remember the terminal state for a file
98/// descriptor and later restore that state as it originally was.
100public:
101 /// Construct a new instance and optionally save terminal state.
102 ///
103 /// \param[in] term
104 /// The Terminal instance holding the file descriptor to save the state
105 /// of. If the instance is not associated with a fd, no state will
106 /// be saved.
107 ///
108 /// \param[in] save_process_group
109 /// If \b true, save the process group settings, else do not
110 /// save the process group settings for a TTY.
111 TerminalState(Terminal term = -1, bool save_process_group = false);
112
113 /// Destroy the instance, restoring terminal state if saved. If restoring
114 /// state is undesirable, the instance needs to be reset before destruction.
116
117 /// Save the TTY state for \a fd.
118 ///
119 /// Save the current state of the TTY for the file descriptor "fd" and if
120 /// "save_process_group" is true, attempt to save the process group info for
121 /// the TTY.
122 ///
123 /// \param[in] term
124 /// The Terminal instance holding fd to save.
125 ///
126 /// \param[in] save_process_group
127 /// If \b true, save the process group settings, else do not
128 /// save the process group settings for a TTY.
129 ///
130 /// \return
131 /// Returns \b true if \a fd describes a TTY and if the state
132 /// was able to be saved, \b false otherwise.
133 bool Save(Terminal term, bool save_process_group);
134
135 /// Restore the TTY state to the cached state.
136 ///
137 /// Restore the state of the TTY using the cached values from a previous
138 /// call to TerminalState::Save(int,bool).
139 ///
140 /// \return
141 /// Returns \b true if the TTY state was successfully restored,
142 /// \b false otherwise.
143 bool Restore() const;
144
145 /// Test for valid cached TTY state information.
146 ///
147 /// \return
148 /// Returns \b true if this object has valid saved TTY state
149 /// settings that can be used to restore a previous state,
150 /// \b false otherwise.
151 bool IsValid() const;
152
153 void Clear();
154
155protected:
156 /// Test if tflags is valid.
157 ///
158 /// \return
159 /// Returns \b true if \a m_tflags is valid and can be restored,
160 /// \b false otherwise.
161 bool TFlagsIsValid() const;
162
163 /// Test if ttystate is valid.
164 ///
165 /// \return
166 /// Returns \b true if \a m_ttystate is valid and can be
167 /// restored, \b false otherwise.
168 bool TTYStateIsValid() const;
169
170 /// Test if the process group information is valid.
171 ///
172 /// \return
173 /// Returns \b true if \a m_process_group is valid and can be
174 /// restored, \b false otherwise.
175 bool ProcessGroupIsValid() const;
176
177 // Member variables
178 Terminal m_tty; ///< A terminal
179 int m_tflags = -1; ///< Cached tflags information.
180 std::unique_ptr<Terminal::Data> m_data; ///< Platform-specific implementation.
181 lldb::pid_t m_process_group = -1; ///< Cached process group information.
182};
183
184} // namespace lldb_private
185
186#endif // LLDB_HOST_TERMINAL_H
A RAII-friendly terminal state saving/restoring class.
Definition Terminal.h:99
~TerminalState()
Destroy the instance, restoring terminal state if saved.
Definition Terminal.cpp:425
int m_tflags
Cached tflags information.
Definition Terminal.h:179
bool TFlagsIsValid() const
Test if tflags is valid.
Definition Terminal.cpp:485
bool Save(Terminal term, bool save_process_group)
Save the TTY state for fd.
Definition Terminal.cpp:434
std::unique_ptr< Terminal::Data > m_data
Platform-specific implementation.
Definition Terminal.h:180
bool TTYStateIsValid() const
Test if ttystate is valid.
Definition Terminal.cpp:487
bool Restore() const
Restore the TTY state to the cached state.
Definition Terminal.cpp:453
Terminal m_tty
A terminal.
Definition Terminal.h:178
lldb::pid_t m_process_group
Cached process group information.
Definition Terminal.h:181
TerminalState(Terminal term=-1, bool save_process_group=false)
Construct a new instance and optionally save terminal state.
Definition Terminal.cpp:420
bool IsValid() const
Test for valid cached TTY state information.
Definition Terminal.cpp:480
bool ProcessGroupIsValid() const
Test if the process group information is valid.
Definition Terminal.cpp:489
llvm::Error SetEcho(bool enabled)
Definition Terminal.cpp:76
llvm::Error SetRaw()
Definition Terminal.cpp:108
friend class TerminalState
Definition Terminal.h:91
Terminal(int fd=-1)
Definition Terminal.h:41
int GetFileDescriptor() const
Definition Terminal.h:47
llvm::Error SetParityCheck(ParityCheck parity_check)
Definition Terminal.cpp:357
llvm::Error SetCanonical(bool enabled)
Definition Terminal.cpp:92
bool IsATerminal() const
Definition Terminal.cpp:31
llvm::Error SetBaudRate(unsigned int baud_rate)
Definition Terminal.cpp:273
bool FileDescriptorIsValid() const
Definition Terminal.h:51
llvm::Error SetData(const Data &data)
Definition Terminal.cpp:61
llvm::Error SetHardwareFlowControl(bool enabled)
Definition Terminal.cpp:379
llvm::Expected< Data > GetData()
Definition Terminal.cpp:40
llvm::Error SetParity(Parity parity)
Definition Terminal.cpp:324
llvm::Error SetStopBits(unsigned int stop_bits)
Definition Terminal.cpp:299
void SetFileDescriptor(int fd)
Definition Terminal.h:49
static bool SupportsUnicode()
Returns whether or not the current terminal supports Unicode rendering.
Definition Terminal.cpp:403
A class that represents a running process on the host machine.
uint64_t pid_t
Definition lldb-types.h:83