-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathstdio_win.cc
152 lines (135 loc) · 3.6 KB
/
stdio_win.cc
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
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "platform/globals.h"
#if defined(DART_HOST_OS_WINDOWS)
#include "bin/stdio.h"
// These are not always defined in the header files. See:
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms686033(v=vs.85).aspx
#ifndef ENABLE_VIRTUAL_TERMINAL_INPUT
#define ENABLE_VIRTUAL_TERMINAL_INPUT 0x0200
#endif
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif
namespace dart {
namespace bin {
bool Stdin::ReadByte(intptr_t fd, int* byte) {
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
uint8_t buffer[1];
DWORD read = 0;
BOOL success = ReadFile(h, buffer, 1, &read, nullptr);
if (!success && (GetLastError() != ERROR_BROKEN_PIPE)) {
return false;
}
*byte = (read == 1) ? buffer[0] : -1;
return true;
}
bool Stdin::GetEchoMode(intptr_t fd, bool* enabled) {
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
if (!GetConsoleMode(h, &mode)) {
return false;
}
*enabled = ((mode & ENABLE_ECHO_INPUT) != 0);
return true;
}
bool Stdin::SetEchoMode(intptr_t fd, bool enabled) {
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
if (!GetConsoleMode(h, &mode)) {
return false;
}
if (enabled) {
mode |= ENABLE_ECHO_INPUT;
} else {
mode &= ~ENABLE_ECHO_INPUT;
}
return SetConsoleMode(h, mode);
}
bool Stdin::GetEchoNewlineMode(intptr_t fd, bool* enabled) {
*enabled = false;
return true;
}
bool Stdin::SetEchoNewlineMode(intptr_t fd, bool enabled) {
if (enabled) {
SetLastError(ERROR_NOT_CAPABLE);
return false;
}
return true;
}
bool Stdin::GetLineMode(intptr_t fd, bool* enabled) {
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
if (!GetConsoleMode(h, &mode)) {
return false;
}
*enabled = (mode & ENABLE_LINE_INPUT) != 0;
return true;
}
bool Stdin::SetLineMode(intptr_t fd, bool enabled) {
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
if (!GetConsoleMode(h, &mode)) {
return false;
}
if (enabled) {
mode |= ENABLE_LINE_INPUT;
} else {
mode &= ~ENABLE_LINE_INPUT;
}
return SetConsoleMode(h, mode);
}
bool Stdin::AnsiSupported(intptr_t fd, bool* supported) {
ASSERT(supported != nullptr);
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
if (h == INVALID_HANDLE_VALUE) {
*supported = false;
return true;
}
DWORD mode;
if (!GetConsoleMode(h, &mode)) {
*supported = false;
return true;
}
*supported = (mode & ENABLE_VIRTUAL_TERMINAL_INPUT) != 0;
return true;
}
bool Stdout::GetTerminalSize(intptr_t fd, int size[2]) {
HANDLE h;
if (fd == 1) {
h = GetStdHandle(STD_OUTPUT_HANDLE);
} else {
h = GetStdHandle(STD_ERROR_HANDLE);
}
CONSOLE_SCREEN_BUFFER_INFO info;
if (!GetConsoleScreenBufferInfo(h, &info)) {
return false;
}
size[0] = info.srWindow.Right - info.srWindow.Left + 1;
size[1] = info.srWindow.Bottom - info.srWindow.Top + 1;
return true;
}
bool Stdout::AnsiSupported(intptr_t fd, bool* supported) {
ASSERT(supported != nullptr);
HANDLE h;
if (fd == 1) {
h = GetStdHandle(STD_OUTPUT_HANDLE);
} else {
h = GetStdHandle(STD_ERROR_HANDLE);
}
if (h == INVALID_HANDLE_VALUE) {
*supported = false;
return true;
}
DWORD mode;
if (!GetConsoleMode(h, &mode)) {
*supported = false;
return true;
}
*supported = (mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0;
return true;
}
} // namespace bin
} // namespace dart
#endif // defined(DART_HOST_OS_WINDOWS)