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