MySQL 8.0.29
Source Code Documentation
tty.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 2021, 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 <ostream>
31
32#ifdef _WIN32
33#include <ntverp.h> // VER_PRODUCTBUILD
34#include <windows.h> // GetConsoleMode
35#else
36#include <termios.h> // tcgetattr
37#include <unistd.h>
38#endif
39
40class HARNESS_EXPORT Tty {
41 public:
42 using fd_type = int;
43#ifdef _WIN32
44 using state_type = DWORD;
45#else
46 using state_type = struct termios;
47#endif
48 static fd_type fd_from_stream(std::ostream &os);
49 static fd_type fd_from_stream(std::istream &is);
50
51 Tty(fd_type fd) : fd_{fd} {
52 try {
53 state_ = attrs();
54 is_saved_ = true;
55 } catch (const std::system_error &) {
56 }
57 }
58
59 // disable copy (enable it explicitly if needed)
60 Tty(const Tty &) = delete;
61 Tty &operator=(const Tty &) = delete;
62
63 /**
64 * restore the state of the Tty if we changed it.
65 */
66 ~Tty() {
67 if (is_saved_) {
68 try {
69 restore_attrs();
70 } catch (const std::system_error &) {
71 }
72 }
73 }
74
75 class Flags {
76 public:
77#ifdef _WIN32
78 class Win32 {
79 public:
80 class Input {
81 public:
82 static constexpr const size_t kEcho = ENABLE_ECHO_INPUT;
83 static constexpr const size_t kExtendedFlags = ENABLE_EXTENDED_FLAGS;
84 static constexpr const size_t kInsertMode = ENABLE_INSERT_MODE;
85 static constexpr const size_t kLineInput = ENABLE_LINE_INPUT;
86 static constexpr const size_t kMouseInput = ENABLE_MOUSE_INPUT;
87 static constexpr const size_t kProcessedInput = ENABLE_PROCESSED_INPUT;
88 static constexpr const size_t kQuickEditMode = ENABLE_QUICK_EDIT_MODE;
89 static constexpr const size_t kWindowInput = ENABLE_WINDOW_INPUT;
90#if VER_PRODUCTBUILD >= 10011
91 // winsdk 10.0.14393.0 and later
92 static constexpr const size_t kVirtualTerminalInput =
93 ENABLE_VIRTUAL_TERMINAL_INPUT;
94#endif
95 };
96 class Output {
97 public:
98 static constexpr const size_t kProcessedOutput =
99 ENABLE_PROCESSED_OUTPUT;
100 static constexpr const size_t kWrapAtEolOutput =
101 ENABLE_WRAP_AT_EOL_OUTPUT;
102#if VER_PRODUCTBUILD >= 10011
103 // winsdk 10.0.14393.0 and later
104 static constexpr const size_t kVirtualTerminalProcessing =
105 ENABLE_VIRTUAL_TERMINAL_PROCESSING;
106 static constexpr const size_t kDisableNewlineAutoReturn =
107 DISABLE_NEWLINE_AUTO_RETURN;
108 static constexpr const size_t kLvbGridWorldwide =
109 ENABLE_LVB_GRID_WORLDWIDE;
110#endif
111 };
112 };
113#else
114 class Posix {
115 public:
116 class Local {
117 public:
118 static constexpr const size_t kGenerateSignal = ISIG;
119 static constexpr const size_t kCanonicalMode = ICANON;
120#ifdef XCASE
121 // not on freebsd, macosx
122 static constexpr const size_t kConvertCase = XCASE;
123#endif
124 static constexpr const size_t kEcho = ECHO;
125 static constexpr const size_t kEchoWithErase = ECHOE;
126 static constexpr const size_t kEchoWithKill = ECHOK;
127 static constexpr const size_t kEchoWithNewline = ECHONL;
128 static constexpr const size_t kEchoWithControl = ECHOCTL;
129 static constexpr const size_t kEchoWithPrint = ECHOPRT;
130 static constexpr const size_t kEchoWithKillEares = ECHOKE;
131 // static constexpr size_t kEchoIfReading = DEFECHO;
132 static constexpr const size_t kOutputFlushed = FLUSHO;
133 static constexpr const size_t kNoFlush = NOFLSH;
134 static constexpr const size_t kToStop = TOSTOP;
135 static constexpr const size_t kPending = PENDIN;
136 static constexpr const size_t kExtendedInputProcessing = IEXTEN;
137 };
138 class Control {
139 public:
140#ifdef CBAUD
141 // not on freebsd, macosx
142 static constexpr const size_t kBaudSpeedMask = CBAUD;
143#endif
144#ifdef CBAUDEX
145 // not on freebsd, macosx, solaris
146 static constexpr const size_t kBaudSpeedMaskExtra = CBAUDEX;
147#endif
148#ifdef CIBAUDEX
149 // not on solaris
150 static constexpr const size_t kInputBaudSpeedMaskExtra = CIBAUDEX;
151#endif
152 static constexpr const size_t kCharacterSizeMask = CSIZE;
153 static constexpr const size_t kTwoStopBits = CSTOPB;
154 static constexpr const size_t kEnableReceiver = CREAD;
155 static constexpr const size_t kParityCheckGenerator = PARENB;
156 static constexpr const size_t kParityOdd = PARODD;
157 static constexpr const size_t kHangupOnClose = HUPCL;
158 static constexpr const size_t kIgnoreControlLines = CLOCAL;
159#ifdef LOBLK
160 static constexpr const size_t kBlockOutputNonCurrentShellLayer = LOBLK;
161#endif
162#ifdef CIBAUD
163 // not on freebsd, macosx
164 static constexpr const size_t kInputSpeedMask = CIBAUD;
165#endif
166#ifdef CMSPAR
167 // not on freebsd, solaris
168 static constexpr const size_t kStickParity = CMSPAR;
169#endif
170#ifdef CRTSXOFF
171 // not on solaris
172 static constexpr const size_t kEnableSoftFlowControl = CRTSXOFF;
173#endif
174 static constexpr const size_t kEnableHardFlowControl = CRTSCTS;
175 };
176 class Output {
177 public:
178 static constexpr const size_t kOutputProcessing = OPOST;
179 static constexpr const size_t kMapNewlineCarriageReturn = ONLCR;
180#ifdef OLCUC
181 // not on freebsd, macosx
182 static constexpr const size_t kMapLowercaseUppercase = OLCUC;
183#endif
184 static constexpr const size_t kMapCarriageReturnNewline = OCRNL;
185 static constexpr const size_t kNoOutputCarriageReturnOnColumnZero =
186 ONOCR;
187 static constexpr const size_t kNoOutputCarriageReturn = ONLRET;
188#ifdef OFILL
189 // not on freebsd
190 static constexpr const size_t kSendFillCharacter = OFILL;
191#endif
192#ifdef OFDEL
193 // not on freebsd
194 static constexpr const size_t kFillCharacterIsDelete = OFDEL;
195#endif
196#ifdef NLDLY
197 // not on freebsd
198 static constexpr const size_t kNewlineDelayMask = NLDLY;
199#endif
200#ifdef CRDLY
201 // not on freebsd
202 static constexpr const size_t kCarriangeReturnDelayMask = CRDLY;
203#endif
204 static constexpr const size_t kHorizontalTabDelayMask = TABDLY;
205#ifdef BSDLY
206 // not on freebsd
207 static constexpr const size_t kBackspaceDelayMask = BSDLY;
208#endif
209#ifdef VTDLY
210 // not on freebsd
211 static constexpr const size_t kVerticalTabDelayMask = VTDLY;
212#endif
213#ifdef FFDLY
214 // not on freebsd
215 static constexpr const size_t kFormfeedDelayMask = FFDLY;
216#endif
217 };
218 class Input {
219 public:
220 static constexpr const size_t kIgnoreBreak = IGNBRK;
221 static constexpr const size_t kBreakInt = BRKINT;
222 static constexpr const size_t kIgnoreParityError = IGNPAR;
223 static constexpr const size_t kParityErrorMark = PARMRK;
224 static constexpr const size_t kInputParityChecking = INPCK;
225 static constexpr const size_t kStripCharacter = ISTRIP;
226 static constexpr const size_t kMapNewlineCarriageReturn = INLCR;
227 static constexpr const size_t kIgnoreCarriageReturn = IGNCR;
228 static constexpr const size_t kMapCarriageReturnNewline = ICRNL;
229#ifdef IUCLC
230 // not on freebsd, macosx
231 static constexpr const size_t kMapUppercaseLowercase = IUCLC;
232#endif
233 static constexpr const size_t kStartStopOutputControl = IXON;
234 static constexpr const size_t kAnyCharacterRestartOutput = IXANY;
235 static constexpr const size_t kStartStopInputControl = IXOFF;
236 static constexpr const size_t kEchoBellOnInputLong = IMAXBEL;
237#ifdef IUTF8
238 // linux only
239 static constexpr const size_t kInputIsUtf8 = IUTF8;
240#endif
241 };
242 };
243#endif
244 };
245
246 std::pair<uint64_t, uint64_t> window_size() const;
247
248 state_type attrs() const;
249
250 void attrs(state_type &tp);
251
252 void restore_attrs() { attrs(state_); }
253
254 void echo(bool on);
255
256 bool is_tty() const;
257
258 bool ensure_vt100();
259
260 private:
263 bool is_saved_{false};
264};
265
266#endif
Definition: tty.h:138
Definition: tty.h:218
Definition: tty.h:116
Definition: tty.h:176
Definition: tty.h:114
Definition: tty.h:75
Definition: tty.h:40
Tty(fd_type fd)
Definition: tty.h:51
Tty(const Tty &)=delete
Tty & operator=(const Tty &)=delete
fd_type fd_
Definition: tty.h:261
state_type state_
Definition: tty.h:262
int fd_type
Definition: tty.h:42
void restore_attrs()
Definition: tty.h:252
termios state_type
Definition: tty.h:46
~Tty()
restore the state of the Tty if we changed it.
Definition: tty.h:66
#define ECHO
Definition: lexyy.cc:1051
#define window_size
Definition: log_event.cc:157