forked from nodejs/node-v0.x-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_stdio_win32.cc
More file actions
162 lines (116 loc) · 3.99 KB
/
node_stdio_win32.cc
File metadata and controls
162 lines (116 loc) · 3.99 KB
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
#include <node.h>
#include <node_stdio.h>
#include <v8.h>
#include <errno.h>
#include <io.h>
#include <platform_win32.h>
using namespace v8;
namespace node {
#define THROW_ERROR(msg) \
return ThrowException(Exception::Error(String::New(msg)));
#define THROW_BAD_ARGS \
return ThrowException(Exception::TypeError(String::New("Bad argument")));
/*
* Flush stdout and stderr on node exit
* Not necessary on windows, so a no-op
*/
void Stdio::Flush() {
}
/*
* STDERR should always be blocking
*/
static Handle<Value> WriteError(const Arguments& args) {
HandleScope scope;
if (args.Length() < 1)
return Undefined();
String::Utf8Value msg(args[0]->ToString());
fprintf(stderr, "%s", reinterpret_cast<char*>(*msg));
return Undefined();
}
static Handle<Value> IsATTY(const Arguments& args) {
HandleScope scope;
int fd = args[0]->IntegerValue();
DWORD result;
int r = GetConsoleMode((HANDLE)_get_osfhandle(fd), &result);
return scope.Close(r ? True() : False());
}
/* Whether stdio is currently in raw mode */
/* -1 means that it has not been set */
static int rawMode = -1;
static void setRawMode(int newMode) {
DWORD flags;
BOOL result;
if (newMode != rawMode) {
if (newMode) {
// raw input
flags = ENABLE_WINDOW_INPUT;
} else {
// input not raw, but still processing enough messages to make the
// tty watcher work (this mode is not the windows default)
flags = ENABLE_ECHO_INPUT | ENABLE_INSERT_MODE | ENABLE_LINE_INPUT |
ENABLE_PROCESSED_INPUT | ENABLE_WINDOW_INPUT;
}
result = SetConsoleMode((HANDLE)_get_osfhandle(STDIN_FILENO), flags);
if (result) {
rawMode = newMode;
}
}
}
static Handle<Value> SetRawMode(const Arguments& args) {
HandleScope scope;
int newMode = !args[0]->IsFalse();
setRawMode(newMode);
if (newMode != rawMode) {
return ThrowException(ErrnoException(GetLastError(), "EnableRawMode"));
}
return scope.Close(rawMode ? True() : False());
}
void Stdio::DisableRawMode(int fd) {
if (rawMode == 1)
setRawMode(0);
}
static Handle<Value> OpenStdin(const Arguments& args) {
HandleScope scope;
setRawMode(0); // init into nonraw mode
return scope.Close(Integer::New(STDIN_FILENO));
}
static Handle<Value> IsStdinBlocking(const Arguments& args) {
// On windows stdin always blocks
return True();
}
static Handle<Value> IsStdoutBlocking(const Arguments& args) {
// On windows stdout always blocks
return True();
}
// process.binding('stdio').getWindowSize(fd);
// returns [row, col]
static Handle<Value> GetWindowSize (const Arguments& args) {
HandleScope scope;
int fd;
HANDLE handle;
CONSOLE_SCREEN_BUFFER_INFO info;
if (!args[0]->IsNumber())
THROW_BAD_ARGS
fd = args[0]->IntegerValue();
handle = (HANDLE)_get_osfhandle(fd);
if (!GetConsoleScreenBufferInfo(handle, &info))
return ThrowException(ErrnoException(GetLastError(), "GetConsoleScreenBufferInfo"));
Local<Array> ret = Array::New(2);
ret->Set(0, Integer::New(static_cast<int>(info.dwSize.Y)));
ret->Set(1, Integer::New(static_cast<int>(info.dwSize.X)));
return scope.Close(ret);
}
void Stdio::Initialize(v8::Handle<v8::Object> target) {
target->Set(String::NewSymbol("stdoutFD"), Integer::New(STDOUT_FILENO));
target->Set(String::NewSymbol("stderrFD"), Integer::New(STDERR_FILENO));
target->Set(String::NewSymbol("stdinFD"), Integer::New(STDIN_FILENO));
NODE_SET_METHOD(target, "writeError", WriteError);
NODE_SET_METHOD(target, "isatty", IsATTY);
NODE_SET_METHOD(target, "isStdoutBlocking", IsStdoutBlocking);
NODE_SET_METHOD(target, "isStdinBlocking", IsStdinBlocking);
NODE_SET_METHOD(target, "setRawMode", SetRawMode);
NODE_SET_METHOD(target, "openStdin", OpenStdin);
NODE_SET_METHOD(target, "getWindowSize", GetWindowSize);
}
} // namespace node
NODE_MODULE(node_stdio, node::Stdio::Initialize);