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
|
#include <node_stdio.h>
#include <node_events.h>
#include <unistd.h>
#include <fcntl.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#else
# include <string.h>
#endif
#include <errno.h>
using namespace v8;
namespace node {
static int stdin_fd = -1;
static int stdout_fd = -1;
static int stdout_flags = -1;
static int stdin_flags = -1;
/* STDERR IS ALWAY SYNC ALWAYS UTF8 */
static Handle<Value>
WriteError (const Arguments& args)
{
HandleScope scope;
if (args.Length() < 1)
return Undefined();
String::Utf8Value msg(args[0]->ToString());
ssize_t r;
size_t written = 0;
while (written < msg.length()) {
r = write(STDERR_FILENO, (*msg) + written, msg.length() - written);
if (r < 0) {
if (errno == EAGAIN || errno == EIO) {
usleep(100);
continue;
}
return ThrowException(ErrnoException(errno, "write"));
}
written += (size_t)r;
}
return Undefined();
}
static Handle<Value> OpenStdin(const Arguments& args) {
HandleScope scope;
if (stdin_fd >= 0) {
return ThrowException(Exception::Error(String::New("stdin already open")));
}
stdin_fd = STDIN_FILENO;
if (isatty(STDIN_FILENO)) {
// XXX selecting on tty fds wont work in windows.
// Must ALWAYS make a coupling on shitty platforms.
stdin_flags = fcntl(stdin_fd, F_GETFL, 0);
if (stdin_flags == -1) {
// TODO DRY
return ThrowException(Exception::Error(String::New("fcntl error!")));
}
int r = fcntl(stdin_fd, F_SETFL, stdin_flags | O_NONBLOCK);
if (r == -1) {
// TODO DRY
return ThrowException(Exception::Error(String::New("fcntl error!")));
}
}
return scope.Close(Integer::New(stdin_fd));
}
static Handle<Value>
IsStdinBlocking (const Arguments& args)
{
HandleScope scope;
return scope.Close(Boolean::New(isatty(STDIN_FILENO)));
}
static Handle<Value>
IsStdoutBlocking (const Arguments& args)
{
HandleScope scope;
bool tty = isatty(STDOUT_FILENO);
return scope.Close(Boolean::New(!tty));
}
void Stdio::Flush() {
if (stdin_flags != -1) {
fcntl(stdin_fd, F_SETFL, stdin_flags & ~O_NONBLOCK);
}
if (stdout_fd >= 0) {
if (stdout_flags != -1) {
fcntl(stdout_fd, F_SETFL, stdout_flags & ~O_NONBLOCK);
}
close(stdout_fd);
stdout_fd = -1;
}
}
void Stdio::Initialize(v8::Handle<v8::Object> target) {
HandleScope scope;
stdout_fd = STDOUT_FILENO;
if (isatty(STDOUT_FILENO)) {
// XXX selecting on tty fds wont work in windows.
// Must ALWAYS make a coupling on shitty platforms.
stdout_flags = fcntl(stdout_fd, F_GETFL, 0);
int r = fcntl(stdout_fd, F_SETFL, stdout_flags | O_NONBLOCK);
}
target->Set(String::NewSymbol("stdoutFD"), Integer::New(stdout_fd));
NODE_SET_METHOD(target, "writeError", WriteError);
NODE_SET_METHOD(target, "openStdin", OpenStdin);
NODE_SET_METHOD(target, "isStdoutBlocking", IsStdoutBlocking);
NODE_SET_METHOD(target, "isStdinBlocking", IsStdinBlocking);
}
} // namespace node
|