MySQL 8.0.31
Source Code Documentation
tty.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 2022, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24
25#ifndef MYSQLHARNESS_TTY_INCLUDED
26#define MYSQLHARNESS_TTY_INCLUDED
27
28#include "harness_export.h"
29
30#include <cstdint>
31#include <ostream>
32
33#ifdef _WIN32
34#include <ntverp.h> // VER_PRODUCTBUILD
35#include <windows.h> // GetConsoleMode
36#else
37#include <termios.h> // tcgetattr
38#include <unistd.h>
39#endif
40
41class HARNESS_EXPORT Tty {
42 public:
43 using fd_type = int;
44#ifdef _WIN32
45 using state_type = DWORD;
46#else
47 using state_type = struct termios;
48#endif
49 static fd_type fd_from_stream(std::ostream &os);
50 static fd_type fd_from_stream(std::istream &is);
51
52 Tty(fd_type fd) : fd_{fd} {
53 try {
54 state_ = attrs();
55 is_saved_ = true;
56 } catch (const std::system_error &) {
57 }
58 }
59
60 // disable copy (enable it explicitly if needed)
61 Tty(const Tty &) = delete;
62 Tty &operator=(const Tty &) = delete;
63
64 /**
65 * restore the state of the Tty if we changed it.
66 */
67 ~Tty() {
68 if (is_saved_) {
69 try {
70 restore_attrs();
71 } catch (const std::system_error &) {
72 }
73 }
74 }
75
76 class Flags {
77 public:
78#ifdef _WIN32
79 class Win32 {
80 public:
81 class Input {
82 public:
83 static constexpr const size_t kEcho = ENABLE_ECHO_INPUT;
84 static constexpr const size_t kExtendedFlags = ENABLE_EXTENDED_FLAGS;
85 static constexpr const size_t kInsertMode = ENABLE_INSERT_MODE;
86 static constexpr const size_t kLineInput = ENABLE_LINE_INPUT;
87 static constexpr const size_t kMouseInput = ENABLE_MOUSE_INPUT;
88 static constexpr const size_t kProcessedInput = ENABLE_PROCESSED_INPUT;
89 static constexpr const size_t kQuickEditMode = ENABLE_QUICK_EDIT_MODE;
90 static constexpr const size_t kWindowInput = ENABLE_WINDOW_INPUT;
91#if VER_PRODUCTBUILD >= 10011
92 // winsdk 10.0.14393.0 and later
93 static constexpr const size_t kVirtualTerminalInput =
94 ENABLE_VIRTUAL_TERMINAL_INPUT;
95#endif
96 };
97 class Output {
98 public:
99 static constexpr const size_t kProcessedOutput =
100 ENABLE_PROCESSED_OUTPUT;
101 static constexpr const size_t kWrapAtEolOutput =
102 ENABLE_WRAP_AT_EOL_OUTPUT;
103#if VER_PRODUCTBUILD >= 10011
104 // winsdk 10.0.14393.0 and later
105 static constexpr const size_t kVirtualTerminalProcessing =
106 ENABLE_VIRTUAL_TERMINAL_PROCESSING;
107 static constexpr const size_t kDisableNewlineAutoReturn =
108 DISABLE_NEWLINE_AUTO_RETURN;
109 static constexpr const size_t kLvbGridWorldwide =
110 ENABLE_LVB_GRID_WORLDWIDE;
111#endif
112 };
113 };
114#else
115 class Posix {
116 public:
117 class Local {
118 public:
119 static constexpr const size_t kGenerateSignal = ISIG;
120 static constexpr const size_t kCanonicalMode = ICANON;
121#ifdef XCASE
122 // not on freebsd, macosx
123 static constexpr const size_t kConvertCase = XCASE;
124#endif
125 static constexpr const size_t kEcho = ECHO;
126 static constexpr const size_t kEchoWithErase = ECHOE;
127 static constexpr const size_t kEchoWithKill = ECHOK;
128 static constexpr const size_t kEchoWithNewline = ECHONL;
129 static constexpr const size_t kEchoWithControl = ECHOCTL;
130 static constexpr const size_t kEchoWithPrint = ECHOPRT;
131 static constexpr const size_t kEchoWithKillEares = ECHOKE;
132 // static constexpr size_t kEchoIfReading = DEFECHO;
133 static constexpr const size_t kOutputFlushed = FLUSHO;
134 static constexpr const size_t kNoFlush = NOFLSH;
135 static constexpr const size_t kToStop = TOSTOP;
136 static constexpr const size_t kPending = PENDIN;
137 static constexpr const size_t kExtendedInputProcessing = IEXTEN;
138 };
139 class Control {
140 public:
141#ifdef CBAUD
142 // not on freebsd, macosx
143 static constexpr const size_t kBaudSpeedMask = CBAUD;
144#endif
145#ifdef CBAUDEX
146 // not on freebsd, macosx, solaris
147 static constexpr const size_t kBaudSpeedMaskExtra = CBAUDEX;
148#endif
149#ifdef CIBAUDEX
150 // not on solaris
151 static constexpr const size_t kInputBaudSpeedMaskExtra = CIBAUDEX;
152#endif
153 static constexpr const size_t kCharacterSizeMask = CSIZE;
154 static constexpr const size_t kTwoStopBits = CSTOPB;
155 static constexpr const size_t kEnableReceiver = CREAD;
156 static constexpr const size_t kParityCheckGenerator = PARENB;
157 static constexpr const size_t kParityOdd = PARODD;
158 static constexpr const size_t kHangupOnClose = HUPCL;
159 static constexpr const size_t kIgnoreControlLines = CLOCAL;
160#ifdef LOBLK
161 static constexpr const size_t kBlockOutputNonCurrentShellLayer = LOBLK;
162#endif
163#ifdef CIBAUD
164 // not on freebsd, macosx
165 static constexpr const size_t kInputSpeedMask = CIBAUD;
166#endif
167#ifdef CMSPAR
168 // not on freebsd, solaris
169 static constexpr const size_t kStickParity = CMSPAR;
170#endif
171#ifdef CRTSXOFF
172 // not on solaris
173 static constexpr const size_t kEnableSoftFlowControl = CRTSXOFF;
174#endif
175 static constexpr const size_t kEnableHardFlowControl = CRTSCTS;
176 };
177 class Output {
178 public:
179 static constexpr const size_t kOutputProcessing = OPOST;
180 static constexpr const size_t kMapNewlineCarriageReturn = ONLCR;
181#ifdef OLCUC
182 // not on freebsd, macosx
183 static constexpr const size_t kMapLowercaseUppercase = OLCUC;
184#endif
185 static constexpr const size_t kMapCarriageReturnNewline = OCRNL;
186 static constexpr const size_t kNoOutputCarriageReturnOnColumnZero =
187 ONOCR;
188 static constexpr const size_t kNoOutputCarriageReturn = ONLRET;
189#ifdef OFILL
190 // not on freebsd
191 static constexpr const size_t kSendFillCharacter = OFILL;
192#endif
193#ifdef OFDEL
194 // not on freebsd
195 static constexpr const size_t kFillCharacterIsDelete = OFDEL;
196#endif
197#ifdef NLDLY
198 // not on freebsd
199 static constexpr const size_t kNewlineDelayMask = NLDLY;
200#endif
201#ifdef CRDLY
202 // not on freebsd
203 static constexpr const size_t kCarriangeReturnDelayMask = CRDLY;
204#endif
205 static constexpr const size_t kHorizontalTabDelayMask = TABDLY;
206#ifdef BSDLY
207 // not on freebsd
208 static constexpr const size_t kBackspaceDelayMask = BSDLY;
209#endif
210#ifdef VTDLY
211 // not on freebsd
212 static constexpr const size_t kVerticalTabDelayMask = VTDLY;
213#endif
214#ifdef FFDLY
215 // not on freebsd
216 static constexpr const size_t kFormfeedDelayMask = FFDLY;
217#endif
218 };
219 class Input {
220 public:
221 static constexpr const size_t kIgnoreBreak = IGNBRK;
222 static constexpr const size_t kBreakInt = BRKINT;
223 static constexpr const size_t kIgnoreParityError = IGNPAR;
224 static constexpr const size_t kParityErrorMark = PARMRK;
225 static constexpr const size_t kInputParityChecking = INPCK;
226 static constexpr const size_t kStripCharacter = ISTRIP;
227 static constexpr const size_t kMapNewlineCarriageReturn = INLCR;
228 static constexpr const size_t kIgnoreCarriageReturn = IGNCR;
229 static constexpr const size_t kMapCarriageReturnNewline = ICRNL;
230#ifdef IUCLC
231 // not on freebsd, macosx
232 static constexpr const size_t kMapUppercaseLowercase = IUCLC;
233#endif
234 static constexpr const size_t kStartStopOutputControl = IXON;
235 static constexpr const size_t kAnyCharacterRestartOutput = IXANY;
236 static constexpr const size_t kStartStopInputControl = IXOFF;
237 static constexpr const size_t kEchoBellOnInputLong = IMAXBEL;
238#ifdef IUTF8
239 // linux only
240 static constexpr const size_t kInputIsUtf8 = IUTF8;
241#endif
242 };
243 };
244#endif
245 };
246
247 std::pair<uint64_t, uint64_t> window_size() const;
248
249 state_type attrs() const;
250
251 void attrs(state_type &tp);
252
253 void restore_attrs() { attrs(state_); }
254
255 void echo(bool on);
256
257 bool is_tty() const;
258
259 bool ensure_vt100();
260
261 private:
264 bool is_saved_{false};
265};
266
267#endif
Definition: tty.h:139
Definition: tty.h:219
Definition: tty.h:117
Definition: tty.h:177
Definition: tty.h:115
Definition: tty.h:76
Definition: tty.h:41
Tty(fd_type fd)
Definition: tty.h:52
Tty(const Tty &)=delete
Tty & operator=(const Tty &)=delete
fd_type fd_
Definition: tty.h:262
state_type state_
Definition: tty.h:263
int fd_type
Definition: tty.h:43
void restore_attrs()
Definition: tty.h:253
termios state_type
Definition: tty.h:47
~Tty()
restore the state of the Tty if we changed it.
Definition: tty.h:67
#define ECHO
Definition: lexyy.cc:1051
#define window_size
Definition: log_event.cc:160