FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node/src/node_stdio.cc at master · gembin/node · GitHub
gembin
/
node
Public
forked from
nodejs/node-v0.x-archive
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
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
332 lines (251 loc) · 8.49 KB
Breadcrumbs
node
/
src
/
node_stdio.cc
Copy path
File metadata and controls
332 lines (251 loc) · 8.49 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//
Copyright Joyent, Inc. and other Node contributors.
//
//
Permission is hereby granted, free of charge, to any person obtaining a
//
copy of this software and associated documentation files (the
//
"Software"), to deal in the Software without restriction, including
//
without limitation the rights to use, copy, modify, merge, publish,
//
distribute, sublicense, and/or sell copies of the Software, and to permit
//
persons to whom the Software is furnished to do so, subject to the
//
following conditions:
//
//
The above copyright notice and this permission notice shall be included
//
in all copies or substantial portions of the Software.
//
//
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
//
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
//
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
//
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
//
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
//
USE OR OTHER DEALINGS IN THE SOFTWARE.
#
include
<
node_stdio.h
>
#
include
<
node_events.h
>
#
include
<
unistd.h
>
#
include
<
fcntl.h
>
#
include
<
string.h
>
#
include
<
errno.h
>
#
if
defined(__APPLE__) || defined(__OpenBSD__)
#
include
<
util.h
>
#
elif
__FreeBSD__
#
include
<
libutil.h
>
#
elif
defined(__sun)
#
include
<
stropts.h
>
//
for openpty ioctls
#
else
#
include
<
pty.h
>
#
endif
#
include
<
termios.h
>
#
include
<
sys/ioctl.h
>
#
include
<
stdlib.h
>
using
namespace
v8
;
namespace
node
{
static
int
stdout_flags = -
1
;
static
int
stdin_flags = -
1
;
static
struct
termios
orig_termios;
/*
in order to restore at exit
*/
static
int
rawmode =
0
;
/*
for atexit() function to check if restore is needed
*/
static
int
EnableRawMode
(
int
fd) {
struct
termios
raw;
if
(rawmode)
return
0
;
//
if (!isatty(fd)) goto fatal;
if
(
tcgetattr
(fd, &orig_termios) == -
1
)
goto
fatal;
raw = orig_termios;
/*
modify the original mode
*/
/*
input modes: no break, no CR to NL, no parity check, no strip char,
* no start/stop output control.
*/
raw.
c_iflag
&= ~(
BRKINT
|
ICRNL
|
INPCK
|
ISTRIP
|
IXON
);
/*
output modes
*/
raw.
c_oflag
|= (
ONLCR
);
/*
control modes - set 8 bit chars
*/
raw.
c_cflag
|= (
CS8
);
/*
local modes - choing off, canonical off, no extended functions,
* no signal chars (^Z,^C)
*/
raw.
c_lflag
&= ~(
ECHO
|
ICANON
|
IEXTEN
|
ISIG
);
/*
control chars - set return condition: min number of bytes and timer.
* We want read to return every single byte, without timeout.
*/
raw.
c_cc
[
VMIN
] =
1
; raw.
c_cc
[
VTIME
] =
0
;
/*
1 byte, no timer
*/
/*
put terminal in raw mode after flushing
*/
if
(
tcsetattr
(fd,
TCSAFLUSH
, &raw) <
0
)
goto
fatal;
rawmode =
1
;
return
0
;
fatal:
errno =
ENOTTY
;
return
-
1
;
}
void
Stdio::DisableRawMode
(
int
fd) {
/*
Don't even check the return value as it's too late.
*/
if
(rawmode &&
tcsetattr
(fd,
TCSAFLUSH
, &orig_termios) != -
1
) {
rawmode =
0
;
}
}
//
process.binding('stdio').setRawMode(true);
static
Handle<Value>
SetRawMode
(
const
Arguments& args) {
HandleScope scope;
if
(args[
0
]->
IsFalse
()) {
Stdio::DisableRawMode
(
STDIN_FILENO
);
}
else
{
if
(
0
!=
EnableRawMode
(
STDIN_FILENO
)) {
return
ThrowException
(
ErrnoException
(errno,
"
EnableRawMode
"
));
}
}
return
rawmode ?
True
() :
False
();
}
//
process.binding('stdio').getWindowSize(fd);
//
returns [row, col]
static
Handle<Value>
GetWindowSize
(
const
Arguments& args) {
HandleScope scope;
int
fd = args[
0
]->
IntegerValue
();
struct
winsize
ws;
if
(
ioctl
(fd,
TIOCGWINSZ
, &ws) <
0
) {
return
ThrowException
(
ErrnoException
(errno,
"
ioctl
"
));
}
Local<Array> ret =
Array::New
(
2
);
ret->
Set
(
0
,
Integer::NewFromUnsigned
(ws.
ws_row
));
ret->
Set
(
1
,
Integer::NewFromUnsigned
(ws.
ws_col
));
return
scope.
Close
(ret);
}
//
process.binding('stdio').setWindowSize(fd, row, col);
static
Handle<Value>
SetWindowSize
(
const
Arguments& args) {
HandleScope scope;
int
fd = args[
0
]->
IntegerValue
();
int
row = args[
1
]->
IntegerValue
();
int
col = args[
2
]->
IntegerValue
();
struct
winsize
ws;
ws.
ws_row
= row;
ws.
ws_col
= col;
ws.
ws_xpixel
=
0
;
ws.
ws_ypixel
=
0
;
if
(
ioctl
(fd,
TIOCSWINSZ
, &ws) <
0
) {
return
ThrowException
(
ErrnoException
(errno,
"
ioctl
"
));
}
return
True
();
}
static
Handle<Value>
IsATTY
(
const
Arguments& args) {
HandleScope scope;
int
fd = args[
0
]->
IntegerValue
();
int
r =
isatty
(fd);
return
scope.
Close
(r ?
True
() :
False
());
}
/*
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 < (
size_t
) 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
(
isatty
(
STDIN_FILENO
)) {
//
XXX selecting on tty fds wont work in windows.
//
Must ALWAYS make a coupling on shitty platforms.
stdin_flags =
fcntl
(
STDIN_FILENO
,
F_GETFL
,
0
);
if
(stdin_flags == -
1
) {
//
TODO DRY
return
ThrowException
(
Exception::Error
(
String::New
(
"
fcntl error!
"
)));
}
int
r =
fcntl
(
STDIN_FILENO
,
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_FILENO
));
}
static
bool
IsBlocking
(
int
fd) {
if
(
isatty
(fd))
return
false
;
struct
stat
s;
if
(
fstat
(fd, &s)) {
perror
(
"
fstat
"
);
return
true
;
}
return
!
S_ISSOCK
(s.
st_mode
) && !
S_ISFIFO
(s.
st_mode
);
}
static
Handle<Value>
IsStdinBlocking
(
const
Arguments& arg) {
return
IsBlocking
(
STDIN_FILENO
) ?
True
() :
False
();
}
static
Handle<Value>
IsStdoutBlocking
(
const
Arguments& args) {
return
IsBlocking
(
STDOUT_FILENO
) ?
True
() :
False
();
}
static
Handle<Value>
OpenPTY
(
const
Arguments& args) {
HandleScope scope;
int
master_fd, slave_fd;
#
ifdef
__sun
typedef
void
(*sighandler)(
int
);
//
TODO move to platform files.
master_fd =
open
(
"
/dev/ptmx
"
,
O_RDWR
|
O_NOCTTY
);
sighandler sig_saved =
signal
(
SIGCHLD
,
SIG_DFL
);
grantpt
(master_fd);
unlockpt
(master_fd);
signal
(
SIGCHLD
, sig_saved);
char
*slave_name =
ptsname
(master_fd);
slave_fd =
open
(slave_name,
O_RDWR
);
ioctl
(slave_fd,
I_PUSH
,
"
ptem
"
);
ioctl
(slave_fd,
I_PUSH
,
"
ldterm
"
);
ioctl
(slave_fd,
I_PUSH
,
"
ttcompat
"
);
#
else
int
r =
openpty
(&master_fd, &slave_fd,
NULL
,
NULL
,
NULL
);
if
(r == -
1
) {
return
ThrowException
(
ErrnoException
(errno,
"
openpty
"
));
}
#
endif
Local<Array> a =
Array::New
(
2
);
a->
Set
(
0
,
Integer::New
(master_fd));
a->
Set
(
1
,
Integer::New
(slave_fd));
return
scope.
Close
(a);
}
void
Stdio::Flush
() {
if
(stdin_flags != -
1
) {
fcntl
(
STDIN_FILENO
,
F_SETFL
, stdin_flags & ~
O_NONBLOCK
);
}
if
(stdout_flags != -
1
) {
fcntl
(
STDOUT_FILENO
,
F_SETFL
, stdout_flags & ~
O_NONBLOCK
);
}
fflush
(stdout);
fflush
(stderr);
}
static
void
HandleSIGCONT
(
int
signum) {
if
(rawmode) {
rawmode =
0
;
EnableRawMode
(
STDIN_FILENO
);
}
}
void
Stdio::Initialize
(v8::Handle<v8::Object> target) {
HandleScope scope;
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_FILENO
,
F_GETFL
,
0
);
fcntl
(
STDOUT_FILENO
,
F_SETFL
, stdout_flags |
O_NONBLOCK
);
}
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,
"
openStdin
"
, OpenStdin);
NODE_SET_METHOD
(target,
"
isStdoutBlocking
"
, IsStdoutBlocking);
NODE_SET_METHOD
(target,
"
isStdinBlocking
"
, IsStdinBlocking);
NODE_SET_METHOD
(target,
"
setRawMode
"
, SetRawMode);
NODE_SET_METHOD
(target,
"
getWindowSize
"
, GetWindowSize);
NODE_SET_METHOD
(target,
"
setWindowSize
"
, SetWindowSize);
NODE_SET_METHOD
(target,
"
isatty
"
, IsATTY);
NODE_SET_METHOD
(target,
"
openpty
"
, OpenPTY);
struct
sigaction
sa;
memset
(&sa,
0
,
sizeof
(sa));
sa.
sa_handler
= HandleSIGCONT;
sigaction
(
SIGCONT
, &sa,
NULL
);
}
}
//
namespace node
NODE_MODULE
(node_stdio, node::Stdio::Initialize);
Back
|
FazBrowse Home
|
New Git URL