1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#include "cmStdIoStream.h"
#include <algorithm>
#include <array>
#include <cstdio>
#include <istream> // IWYU pragma: keep
#include <ostream> // IWYU pragma: keep
#ifdef _WIN32
# include <windows.h>
# include <io.h> // for _get_osfhandle
#else
# include <string>
# include <cm/optional>
# include <cm/string_view>
# include <cmext/string_view>
# include <unistd.h>
#endif
#include "cm_fileno.hxx"
#ifndef _WIN32
# include "cmSystemTools.h"
#endif
namespace cm {
namespace StdIo {
namespace {
#ifdef _WIN32
# ifndef ENABLE_VIRTUAL_TERMINAL_INPUT
# define ENABLE_VIRTUAL_TERMINAL_INPUT 0x0200
# endif
# ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
# endif
#else
// List of known `TERM` names that support VT100 escape sequences.
// Order by `LC_COLLATE=C sort` to search using `std::lower_bound`.
std::array<cm::string_view, 56> const kVT100Names{ {
"Eterm"_s,
"alacritty"_s,
"alacritty-direct"_s,
"ansi"_s,
"color-xterm"_s,
"con132x25"_s,
"con132x30"_s,
"con132x43"_s,
"con132x60"_s,
"con80x25"_s,
"con80x28"_s,
"con80x30"_s,
"con80x43"_s,
"con80x50"_s,
"con80x60"_s,
"cons25"_s,
"console"_s,
"cygwin"_s,
"dtterm"_s,
"eterm-color"_s,
"gnome"_s,
"gnome-256color"_s,
"konsole"_s,
"konsole-256color"_s,
"kterm"_s,
"linux"_s,
"linux-c"_s,
"mach-color"_s,
"mlterm"_s,
"msys"_s,
"putty"_s,
"putty-256color"_s,
"rxvt"_s,
"rxvt-256color"_s,
"rxvt-cygwin"_s,
"rxvt-cygwin-native"_s,
"rxvt-unicode"_s,
"rxvt-unicode-256color"_s,
"screen"_s,
"screen-256color"_s,
"screen-256color-bce"_s,
"screen-bce"_s,
"screen-w"_s,
"screen.linux"_s,
"st-256color"_s,
"tmux"_s,
"tmux-256color"_s,
"vt100"_s,
"xterm"_s,
"xterm-16color"_s,
"xterm-256color"_s,
"xterm-88color"_s,
"xterm-color"_s,
"xterm-debian"_s,
"xterm-kitty"_s,
"xterm-termite"_s,
} };
bool TermIsVT100()
{
if (cm::optional<std::string> term = cmSystemTools::GetEnvVar("TERM")) {
// NOLINTNEXTLINE(readability-qualified-auto)
auto i = std::lower_bound(kVT100Names.begin(), kVT100Names.end(), *term);
if (i != kVT100Names.end() && *i == *term) {
return true;
}
}
return false;
}
#endif
} // anonymous namespace
Stream::Stream(std::ios& s, FILE* file, Direction direction)
: IOS_(s)
, FD_(cm_fileno(file))
{
#ifdef _WIN32
auto h = reinterpret_cast<HANDLE>(_get_osfhandle(this->FD_));
if (GetConsoleMode(h, &this->ConsoleOrigMode_)) {
this->Console_ = h;
DWORD vtMode = this->ConsoleOrigMode_ |
(direction == Direction::In ? ENABLE_VIRTUAL_TERMINAL_INPUT
: ENABLE_VIRTUAL_TERMINAL_PROCESSING);
if (SetConsoleMode(this->Console_, vtMode)) {
this->Kind_ = TermKind::VT100;
} else {
SetConsoleMode(this->Console_, this->ConsoleOrigMode_);
this->Kind_ = TermKind::Console;
}
}
#else
static_cast<void>(direction);
if (isatty(this->FD_) && TermIsVT100()) {
this->Kind_ = TermKind::VT100;
}
#endif
}
#ifdef _WIN32
Stream::~Stream()
{
this->Destroy();
}
void Stream::Destroy()
{
if (this->Console_) {
this->IOS_.rdbuf()->pubsync();
SetConsoleMode(this->Console_, this->ConsoleOrigMode_);
this->Console_ = nullptr;
}
}
#else
Stream::~Stream() = default;
#endif
IStream::IStream(std::istream& is, FILE* file)
: Stream(is, file, Direction::In)
{
}
std::istream& IStream::IOS() const
{
return dynamic_cast<std::istream&>(this->Stream::IOS());
}
OStream::OStream(std::ostream& os, FILE* file)
: Stream(os, file, Direction::Out)
{
}
std::ostream& OStream::IOS() const
{
return dynamic_cast<std::ostream&>(this->Stream::IOS());
}
}
}
|