-
Notifications
You must be signed in to change notification settings - Fork 146
/
stdioctl.cpp
202 lines (185 loc) · 5.78 KB
/
stdioctl.cpp
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <internal/stdioctl.h>
#include <internal/getenv.h>
#ifdef _TV_UNIX
#include <unistd.h>
#include <fcntl.h>
StdioCtl StdioCtl::instance;
void StdioCtl::setUp()
{
ttyfd = -1;
infile = outfile = nullptr;
if (!getEnv<const char*>("TVISION_USE_STDIO"))
ttyfd = ::open("/dev/tty", O_RDWR);
if (ttyfd != -1)
{
for (auto &fd : fds)
fd = ttyfd;
infile = ::fdopen(ttyfd, "r");
files[0] = infile;
outfile = ::fdopen(ttyfd, "w");
files[1] = outfile;
files[2] = outfile;
}
else
{
for (int i = 0; i < 3; ++i)
fds[i] = i;
files[0] = stdin;
files[1] = stdout;
files[2] = stderr;
}
}
void StdioCtl::tearDown()
{
if (ttyfd != -1)
{
::fflush(infile);
::fflush(outfile);
::close(ttyfd);
::fclose(infile);
::fclose(outfile);
ttyfd = -1;
infile = outfile = nullptr;
for (int i = 0; i < 3; ++i)
{
fds[i] = -1;
files[i] = nullptr;
}
}
}
#elif defined(_WIN32)
namespace stdioctl
{
static bool isValid(HANDLE h)
{
return h && h != INVALID_HANDLE_VALUE;
}
static bool isConsole(HANDLE h)
{
DWORD mode;
return GetConsoleMode(h, &mode);
}
} // namespace stdioctl
StdioCtl StdioCtl::instance;
void StdioCtl::setUp()
{
// The console can be accessed in two ways: through GetStdHandle() or through
// CreateFile(). GetStdHandle() will be unable to return a console handle
// if standard handles have been redirected.
//
// Additionally, we want to spawn a new console when none is visible to the user.
// This might happen under two circumstances:
//
// 1. The console crashed. This is easy to detect because all console operations
// fail on the console handles.
// 2. The console exists somehow but cannot be made visible, not even by doing
// GetConsoleWindow() and then ShowWindow(SW_SHOW). This is what happens
// under Git Bash without pseudoconsole support. In this case, none of the
// standard handles is a console, yet the handles returned by CreateFile()
// still work.
//
// So, in order to find out if a console needs to be allocated, we
// check whether at least of the standard handles is a console. If none
// of them is, we allocate a new console. Yes, this will always spawn a
// console if all three standard handles are redirected, but this is not
// a common use case.
//
// Then comes the question of whether to access the console through GetStdHandle()
// or through CreateFile(). CreateFile() has the advantage of not being affected
// by standard handle redirection. However, I have found that some terminal
// emulators (i.e. ConEmu) behave unexpectedly when using screen buffers
// opened with CreateFile(). So we will use the standard handles whenever possible.
//
// It is worth mentioning that the handles returned by CreateFile() have to be
// closed, but the ones returned by GetStdHandle() must not. So we have to remember
// this information for each console handle.
//
// We also need to remember whether we allocated a console or not, so that
// we can free it when tearing down. If we don't, weird things may happen.
using namespace stdioctl;
tearDown();
static constexpr struct { DWORD std; int index; } channels[] =
{
{STD_INPUT_HANDLE, input},
{STD_OUTPUT_HANDLE, startupOutput},
{STD_ERROR_HANDLE, startupOutput},
};
bool haveConsole = false;
for (const auto &c : channels)
{
HANDLE h = GetStdHandle(c.std);
if (isConsole(h))
{
haveConsole = true;
if (!isValid(cn[c.index].handle))
cn[c.index] = {h, false};
}
}
if (!haveConsole)
{
FreeConsole();
AllocConsole();
ownsConsole = true;
}
if (!isValid(cn[input].handle))
{
cn[input].handle = CreateFile(
"CONIN$",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ,
nullptr,
OPEN_EXISTING,
0,
0);
cn[input].owning = true;
}
if (!isValid(cn[startupOutput].handle))
{
cn[startupOutput].handle = CreateFile(
"CONOUT$",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE,
nullptr,
OPEN_EXISTING,
0,
0);
cn[startupOutput].owning = true;
}
cn[activeOutput].handle = CreateConsoleScreenBuffer(
GENERIC_READ | GENERIC_WRITE,
0,
nullptr,
CONSOLE_TEXTMODE_BUFFER,
nullptr);
cn[activeOutput].owning = true;
{
CONSOLE_SCREEN_BUFFER_INFO sbInfo {};
GetConsoleScreenBufferInfo(cn[startupOutput].handle, &sbInfo);
// Force the screen buffer size to match the window size.
// The Console API guarantees this, but some implementations
// are not compliant (e.g. Wine).
sbInfo.dwSize.X = sbInfo.srWindow.Right - sbInfo.srWindow.Left + 1;
sbInfo.dwSize.Y = sbInfo.srWindow.Bottom - sbInfo.srWindow.Top + 1;
SetConsoleScreenBufferSize(cn[activeOutput].handle, sbInfo.dwSize);
}
SetConsoleActiveScreenBuffer(cn[activeOutput].handle);
}
void StdioCtl::tearDown()
{
using namespace stdioctl;
if (isValid(cn[startupOutput].handle))
SetConsoleActiveScreenBuffer(cn[startupOutput].handle);
for (auto &c : cn)
if (isValid(c.handle))
{
if (c.owning)
CloseHandle(c.handle);
c.handle = INVALID_HANDLE_VALUE;
}
if (ownsConsole)
{
FreeConsole();
ownsConsole = false;
}
}
#endif // _TV_UNIX