FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node/src/node_stdio.cc at master · tilgovi/node · GitHub
tilgovi
/
node
Public
forked from
nodejs/node-v0.x-archive
Notifications
You must be signed in to change notification settings
Fork
1
Star
2
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
node
/
src
/
node_stdio.cc
Copy path
More file actions
More file actions
Latest commit
History
History
History
236 lines (174 loc) · 4.62 KB
Breadcrumbs
node
/
src
/
node_stdio.cc
Copy path
File metadata and controls
236 lines (174 loc) · 4.62 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#
include
<
node_stdio.h
>
#
include
<
node_events.h
>
#
include
<
coupling.h
>
#
include
<
unistd.h
>
#
include
<
fcntl.h
>
using
namespace
v8
;
using
namespace
node
;
static
Persistent<Object> stdio;
static
Persistent<Function> emit;
static
struct
coupling
*stdin_coupling =
NULL
;
static
struct
coupling
*stdout_coupling =
NULL
;
static
int
stdin_fd = -
1
;
static
int
stdout_fd = -
1
;
static
evcom_reader in;
static
evcom_writer out;
static
enum
encoding stdin_encoding;
static
void
EmitInput
(Local<Value> input)
{
HandleScope scope;
Local<Value> argv[
2
] = {
String::NewSymbol
(
"
data
"
), input };
emit->
Call
(stdio,
2
, argv);
}
static
void
EmitClose
(
void
)
{
HandleScope scope;
Local<Value> argv[
1
] = {
String::NewSymbol
(
"
close
"
) };
emit->
Call
(stdio,
1
, argv);
}
/*
STDERR IS ALWAY SYNC
*/
static
Handle<Value>
WriteError
(
const
Arguments& args)
{
HandleScope scope;
if
(args.
Length
() <
1
)
return
Undefined
();
String::Utf8Value
msg
(args[
0
]->
ToString
());
fprintf
(stderr,
"
%s
"
, *msg);
fflush
(stderr);
return
Undefined
();
}
static
Handle<Value>
Write
(
const
Arguments& args)
{
HandleScope scope;
if
(args.
Length
() ==
0
) {
return
ThrowException
(
Exception::Error
(
String::New
(
"
Bad argument
"
)));
}
enum
encoding enc =
UTF8
;
if
(args.
Length
() >
1
) enc =
ParseEncoding
(args[
1
],
UTF8
);
ssize_t
len =
DecodeBytes
(args[
0
], enc);
if
(len <
0
) {
Local<Value> exception =
Exception::TypeError
(
String::New
(
"
Bad argument
"
));
return
ThrowException
(exception);
}
char
buf[len];
ssize_t
written =
DecodeWrite
(buf, len, args[
0
], enc);
assert
(written == len);
evcom_writer_write
(&out, buf, len);
return
Undefined
();
}
static
void
detach_in
(evcom_reader *r)
{
assert
(r == &in);
HandleScope scope;
EmitClose
();
evcom_reader_detach
(&in);
if
(stdin_coupling) {
coupling_destroy
(stdin_coupling);
stdin_coupling =
NULL
;
}
stdin_fd = -
1
;
}
static
void
detach_out
(evcom_writer* w)
{
assert
(w == &out);
evcom_writer_detach
(&out);
if
(stdout_coupling) {
coupling_destroy
(stdout_coupling);
stdout_coupling =
NULL
;
}
stdout_fd = -
1
;
}
static
void
on_read
(evcom_reader *r,
const
void
*buf,
size_t
len)
{
assert
(r == &in);
HandleScope scope;
if
(!len) {
return
;
}
Local<Value> data =
Encode
(buf, len, stdin_encoding);
EmitInput
(data);
}
static
inline
int
set_nonblock
(
int
fd)
{
int
flags =
fcntl
(fd,
F_GETFL
,
0
);
if
(flags == -
1
)
return
-
1
;
int
r =
fcntl
(fd,
F_SETFL
, flags |
O_NONBLOCK
);
if
(r == -
1
)
return
-
1
;
return
0
;
}
static
Handle<Value>
Open
(
const
Arguments& args)
{
HandleScope scope;
if
(stdin_fd >=
0
) {
return
ThrowException
(
Exception::Error
(
String::New
(
"
stdin already open
"
)));
}
stdin_encoding =
UTF8
;
if
(args.
Length
() >
0
) {
stdin_encoding =
ParseEncoding
(args[
0
]);
}
if
(
isatty
(
STDIN_FILENO
)) {
//
XXX selecting on tty fds wont work in windows.
//
Must ALWAYS make a coupling on shitty platforms.
stdin_fd =
STDIN_FILENO
;
}
else
{
stdin_coupling =
coupling_new_pull
(
STDIN_FILENO
);
stdin_fd =
coupling_nonblocking_fd
(stdin_coupling);
}
set_nonblock
(stdin_fd);
evcom_reader_init
(&in);
in.
on_read
= on_read;
in.
on_close
= detach_in;
evcom_reader_set
(&in, stdin_fd);
evcom_reader_attach
(
EV_DEFAULT_
&in);
return
Undefined
();
}
static
Handle<Value>
Close
(
const
Arguments& args)
{
HandleScope scope;
assert
(stdio == args.
Holder
());
if
(stdin_fd <
0
) {
return
ThrowException
(
Exception::Error
(
String::New
(
"
stdin not open
"
)));
}
evcom_reader_close
(&in);
return
Undefined
();
}
void
Stdio::Initialize
(v8::Handle<v8::Object> target)
{
HandleScope scope;
Local<Object> stdio_local =
EventEmitter::constructor_template->
GetFunction
()->
NewInstance
(
0
,
NULL
);
stdio = Persistent<Object>::
New
(stdio_local);
NODE_SET_METHOD
(stdio,
"
open
"
, Open);
NODE_SET_METHOD
(stdio,
"
write
"
, Write);
NODE_SET_METHOD
(stdio,
"
writeError
"
, WriteError);
NODE_SET_METHOD
(stdio,
"
close
"
, Close);
target->
Set
(
String::NewSymbol
(
"
stdio
"
), stdio);
Local<Value> emit_v = stdio->
Get
(
String::NewSymbol
(
"
emit
"
));
assert
(emit_v->
IsFunction
());
Local<Function> emit_f = Local<Function>::
Cast
(emit_v);
emit = Persistent<Function>::
New
(emit_f);
if
(
isatty
(
STDOUT_FILENO
)) {
//
XXX selecting on tty fds wont work in windows.
//
Must ALWAYS make a coupling on shitty platforms.
stdout_fd =
STDOUT_FILENO
;
}
else
{
stdout_coupling =
coupling_new_push
(
STDOUT_FILENO
);
stdout_fd =
coupling_nonblocking_fd
(stdout_coupling);
}
set_nonblock
(stdout_fd);
evcom_writer_init
(&out);
out.
on_close
= detach_out;
evcom_writer_set
(&out, stdout_fd);
evcom_writer_attach
(
EV_DEFAULT_
&out);
}
Back
|
FazBrowse Home
|
New Git URL