Browse Source

Merge branch 'new-tty-binding'

v0.7.4-release
Ryan Dahl 13 years ago
parent
commit
f63a1f8409
  1. 5
      deps/uv/common.gypi
  2. 28
      deps/uv/include/uv-private/uv-win.h
  3. 1
      deps/uv/src/unix/cares.c
  4. 19
      deps/uv/src/unix/fs.c
  5. 1
      deps/uv/src/unix/udp.c
  6. 3
      deps/uv/src/win/core.c
  7. 33
      deps/uv/src/win/handle.c
  8. 28
      deps/uv/src/win/internal.h
  9. 3
      deps/uv/src/win/pipe.c
  10. 6
      deps/uv/src/win/req.c
  11. 13
      deps/uv/src/win/stream.c
  12. 4
      deps/uv/src/win/tcp.c
  13. 1558
      deps/uv/src/win/tty.c
  14. 12
      deps/uv/test/test-list.h
  15. 47
      deps/uv/test/test-tcp-close.c
  16. 64
      deps/uv/test/test-tcp-write-error.c
  17. 40
      deps/uv/test/test-timer.c
  18. 4
      lib/readline.js
  19. 6
      lib/repl.js
  20. 0
      lib/tty_legacy.js
  21. 375
      lib/tty_uv.js
  22. 3
      node.gyp
  23. 123
      src/node.js
  24. 71
      src/tty_wrap.cc
  25. 9
      test/simple/test-module-load-list.js

5
deps/uv/common.gypi

@ -32,6 +32,11 @@
'LinkIncremental': 2, # enable incremental linking
},
},
'conditions': [
['OS != "win"', {
'defines': [ 'EV_VERIFY=2' ],
}],
]
},
'Release': {
'defines': [ 'NDEBUG' ],

28
deps/uv/include/uv-private/uv-win.h

@ -117,6 +117,7 @@ RB_HEAD(uv_timer_tree_s, uv_timer_s);
HANDLE pipeHandle; \
struct uv_pipe_accept_s* next_pending; \
} uv_pipe_accept_t; \
\
typedef struct uv_tcp_accept_s { \
UV_REQ_FIELDS \
SOCKET accept_socket; \
@ -181,6 +182,31 @@ RB_HEAD(uv_timer_tree_s, uv_timer_s);
struct { uv_pipe_connection_fields }; \
};
/* TODO: put the parser states in an union - TTY handles are always */
/* half-duplex so read-state can safely overlap write-state. */
#define UV_TTY_PRIVATE_FIELDS \
HANDLE handle; \
HANDLE read_line_handle; \
uv_buf_t read_line_buffer; \
HANDLE read_raw_wait; \
DWORD original_console_mode; \
/* Fields used for translating win */ \
/* keystrokes into vt100 characters */ \
char last_key[8]; \
unsigned char last_key_offset; \
unsigned char last_key_len; \
INPUT_RECORD last_input_record; \
WCHAR last_utf16_high_surrogate; \
/* utf8-to-utf16 conversion state */ \
unsigned char utf8_bytes_left; \
unsigned int utf8_codepoint; \
/* eol conversion state */ \
unsigned char previous_eol; \
/* ansi parser state */ \
unsigned char ansi_parser_state; \
unsigned char ansi_csi_argc; \
unsigned short ansi_csi_argv[4];
#define UV_TIMER_PRIVATE_FIELDS \
RB_ENTRY(uv_timer_s) tree_entry; \
int64_t due; \
@ -273,8 +299,6 @@ RB_HEAD(uv_timer_tree_s, uv_timer_s);
int is_path_dir; \
char* buffer;
#define UV_TTY_PRIVATE_FIELDS /* empty */
int uv_utf16_to_utf8(const wchar_t* utf16Buffer, size_t utf16Size,
char* utf8Buffer, size_t utf8Size);
int uv_utf8_to_utf16(const char* utf8Buffer, wchar_t* utf16Buffer,

1
deps/uv/src/unix/cares.c

@ -67,6 +67,7 @@ static uv_ares_task_t* uv__ares_task_create(int fd) {
if (h == NULL) {
uv_fatal_error(ENOMEM, "malloc");
return NULL;
}
h->sock = fd;

19
deps/uv/src/unix/fs.c

@ -149,19 +149,20 @@ static int uv__fs_after(eio_req* eio) {
case UV_FS_READLINK:
if (req->result == -1) {
req->ptr = NULL;
} else {
assert(req->result > 0);
if ((name = realloc(req->eio->ptr2, req->result + 1)) == NULL) {
/* Not enough memory. Reuse buffer, chop off last byte. */
name = req->eio->ptr2;
req->result--;
}
break;
}
assert(req->result > 0);
/* Make zero-terminated copy of req->eio->ptr2 */
if ((req->ptr = name = malloc(req->result + 1))) {
memcpy(name, req->eio->ptr2, req->result);
name[req->result] = '\0';
req->ptr = name;
req->result = 0;
}
else {
req->errorno = ENOMEM;
req->result = -1;
}
break;
default:

1
deps/uv/src/unix/udp.c

@ -301,6 +301,7 @@ static int uv__udp_bind(uv_udp_t* handle,
saved_errno = errno;
status = -1;
fd = -1;
/* Check for bad flags. */
if (flags & ~UV_UDP_IPV6ONLY) {

3
deps/uv/src/win/core.c

@ -48,6 +48,9 @@ static void uv_init(void) {
/* Initialize FS */
uv_fs_init();
/* Initialize console */
uv_console_init();
}

33
deps/uv/src/win/handle.c

@ -20,11 +20,36 @@
*/
#include <assert.h>
#include <io.h>
#include "uv.h"
#include "internal.h"
uv_handle_type uv_guess_handle(uv_file file) {
HANDLE handle = (HANDLE) _get_osfhandle(file);
DWORD mode;
switch (GetFileType(handle)) {
case FILE_TYPE_CHAR:
if (GetConsoleMode(handle, &mode)) {
return UV_TTY;
} else {
return UV_UNKNOWN_HANDLE;
}
case FILE_TYPE_PIPE:
return UV_NAMED_PIPE;
case FILE_TYPE_DISK:
return UV_FILE;
default:
return UV_UNKNOWN_HANDLE;
}
}
int uv_is_active(uv_handle_t* handle) {
switch (handle->type) {
case UV_TIMER:
@ -80,6 +105,10 @@ void uv_close(uv_handle_t* handle, uv_close_cb cb) {
}
return;
case UV_TTY:
uv_tty_close((uv_tty_t*) handle);
return;
case UV_UDP:
udp = (uv_udp_t*) handle;
uv_udp_recv_stop(udp);
@ -159,6 +188,10 @@ void uv_process_endgames(uv_loop_t* loop) {
uv_pipe_endgame(loop, (uv_pipe_t*) handle);
break;
case UV_TTY:
uv_tty_endgame(loop, (uv_tty_t*) handle);
break;
case UV_UDP:
uv_udp_endgame(loop, (uv_udp_t*) handle);
break;

28
deps/uv/src/win/internal.h

@ -64,6 +64,7 @@ void uv_process_timers(uv_loop_t* loop);
#define UV_HANDLE_UV_ALLOCED 0x20000
#define UV_HANDLE_SYNC_BYPASS_IOCP 0x40000
#define UV_HANDLE_ZERO_READ 0x80000
#define UV_HANDLE_TTY_RAW 0x100000
void uv_want_endgame(uv_loop_t* loop, uv_handle_t* handle);
void uv_process_endgames(uv_loop_t* loop);
@ -173,6 +174,33 @@ void uv_process_pipe_connect_req(uv_loop_t* loop, uv_pipe_t* handle,
void uv_process_pipe_shutdown_req(uv_loop_t* loop, uv_pipe_t* handle,
uv_shutdown_t* req);
/*
* TTY
*/
void uv_console_init();
int uv_tty_read_start(uv_tty_t* handle, uv_alloc_cb alloc_cb,
uv_read_cb read_cb);
int uv_tty_read_stop(uv_tty_t* handle);
int uv_tty_write(uv_loop_t* loop, uv_write_t* req, uv_tty_t* handle,
uv_buf_t bufs[], int bufcnt, uv_write_cb cb);
void uv_tty_close(uv_tty_t* handle);
void uv_process_tty_read_req(uv_loop_t* loop, uv_tty_t* handle,
uv_req_t* req);
void uv_process_tty_write_req(uv_loop_t* loop, uv_tty_t* handle,
uv_write_t* req);
/* TODO: remove me */
void uv_process_tty_accept_req(uv_loop_t* loop, uv_tty_t* handle,
uv_req_t* raw_req);
/* TODO: remove me */
void uv_process_tty_connect_req(uv_loop_t* loop, uv_tty_t* handle,
uv_connect_t* req);
void uv_tty_endgame(uv_loop_t* loop, uv_tty_t* handle);
/*
* Loop watchers
*/

3
deps/uv/src/win/pipe.c

@ -231,6 +231,7 @@ void uv_pipe_endgame(uv_loop_t* loop, uv_pipe_t* handle) {
if (result) {
/* Mark the handle as shut now to avoid going through this again. */
handle->flags |= UV_HANDLE_SHUT;
return;
} else {
/* Failure. */
@ -678,6 +679,8 @@ static void uv_pipe_queue_read(uv_loop_t* loop, uv_pipe_t* handle) {
/* Make this req pending reporting an error. */
SET_REQ_ERROR(req, WSAGetLastError());
uv_insert_pending_req(loop, req);
handle->flags |= UV_HANDLE_READ_PENDING;
handle->reqs_pending++;
return;
}

6
deps/uv/src/win/req.c

@ -87,6 +87,12 @@ static uv_req_t* uv_remove_pending_req(uv_loop_t* loop) {
req); \
break; \
\
case UV_TTY: \
uv_process_tty_##method##_req(loop, \
(uv_tty_t*) ((req)->handle_at), \
req); \
break; \
\
default: \
assert(0); \
} \

13
deps/uv/src/win/stream.c

@ -83,6 +83,8 @@ int uv_read_start(uv_stream_t* handle, uv_alloc_cb alloc_cb,
return uv_tcp_read_start((uv_tcp_t*)handle, alloc_cb, read_cb);
case UV_NAMED_PIPE:
return uv_pipe_read_start((uv_pipe_t*)handle, alloc_cb, read_cb);
case UV_TTY:
return uv_tty_read_start((uv_tty_t*) handle, alloc_cb, read_cb);
default:
assert(0);
return -1;
@ -91,9 +93,12 @@ int uv_read_start(uv_stream_t* handle, uv_alloc_cb alloc_cb,
int uv_read_stop(uv_stream_t* handle) {
handle->flags &= ~UV_HANDLE_READING;
return 0;
if (handle->type = UV_TTY) {
return uv_tty_read_stop((uv_tty_t*) handle);
} else {
handle->flags &= ~UV_HANDLE_READING;
return 0;
}
}
@ -106,6 +111,8 @@ int uv_write(uv_write_t* req, uv_stream_t* handle, uv_buf_t bufs[], int bufcnt,
return uv_tcp_write(loop, req, (uv_tcp_t*) handle, bufs, bufcnt, cb);
case UV_NAMED_PIPE:
return uv_pipe_write(loop, req, (uv_pipe_t*) handle, bufs, bufcnt, cb);
case UV_TTY:
return uv_tty_write(loop, req, (uv_tty_t*) handle, bufs, bufcnt, cb);
default:
assert(0);
uv_set_sys_error(loop, WSAEINVAL);

4
deps/uv/src/win/tcp.c

@ -128,7 +128,9 @@ void uv_tcp_endgame(uv_loop_t* loop, uv_tcp_t* handle) {
}
handle->shutdown_req->cb(handle->shutdown_req, status);
}
handle->reqs_pending--;
DECREASE_PENDING_REQ_COUNT(handle);
return;
}
if (handle->flags & UV_HANDLE_CLOSING &&

1558
deps/uv/src/win/tty.c

File diff suppressed because it is too large

12
deps/uv/test/test-list.h

@ -22,6 +22,8 @@
TEST_DECLARE (tty)
TEST_DECLARE (tcp_ping_pong)
TEST_DECLARE (tcp_ping_pong_v6)
TEST_DECLARE (tcp_ref)
TEST_DECLARE (tcp_ref2)
TEST_DECLARE (pipe_ping_pong)
TEST_DECLARE (delayed_accept)
TEST_DECLARE (tcp_writealot)
@ -52,6 +54,8 @@ TEST_DECLARE (connection_fail_doesnt_auto_close)
TEST_DECLARE (shutdown_eof)
TEST_DECLARE (callback_stack)
TEST_DECLARE (timer)
TEST_DECLARE (timer_ref)
TEST_DECLARE (timer_ref2)
TEST_DECLARE (timer_again)
TEST_DECLARE (idle_starvation)
TEST_DECLARE (loop_handles)
@ -104,6 +108,12 @@ HELPER_DECLARE (pipe_echo_server)
TASK_LIST_START
TEST_ENTRY (tty)
TEST_ENTRY (tcp_ref)
TEST_ENTRY (tcp_ref2)
TEST_HELPER (tcp_ref2, tcp4_echo_server)
TEST_ENTRY (tcp_ping_pong)
TEST_HELPER (tcp_ping_pong, tcp4_echo_server)
@ -154,6 +164,8 @@ TASK_LIST_START
TEST_HELPER (callback_stack, tcp4_echo_server)
TEST_ENTRY (timer)
TEST_ENTRY (timer_ref)
TEST_ENTRY (timer_ref2)
TEST_ENTRY (timer_again)

47
deps/uv/test/test-tcp-close.c

@ -127,3 +127,50 @@ TEST_IMPL(tcp_close) {
return 0;
}
TEST_IMPL(tcp_ref) {
uv_tcp_t never;
int r;
/* A tcp just initialized should count as one reference. */
r = uv_tcp_init(uv_default_loop(), &never);
ASSERT(r == 0);
/* One unref should set the loop ref count to zero. */
uv_unref(uv_default_loop());
/* Therefore this does not block */
uv_run(uv_default_loop());
return 0;
}
static void never_cb(uv_connect_t* conn_req, int status) {
FATAL("never_cb should never be called");
}
TEST_IMPL(tcp_ref2) {
uv_tcp_t never;
int r;
/* A tcp just initialized should count as one reference. */
r = uv_tcp_init(uv_default_loop(), &never);
ASSERT(r == 0);
r = uv_tcp_connect(&connect_req,
&never,
uv_ip4_addr("127.0.0.1", TEST_PORT),
never_cb);
ASSERT(r == 0);
/* One unref should set the loop ref count to zero. */
uv_unref(uv_default_loop());
/* Therefore this does not block */
uv_run(uv_default_loop());
return 0;
}

64
deps/uv/test/test-tcp-write-error.c

@ -35,10 +35,15 @@ static uv_buf_t alloc_cb(uv_handle_t* handle, size_t suggested_size);
static uv_tcp_t tcp_server;
static uv_tcp_t tcp_client;
static uv_tcp_t tcp_peer; /* client socket as accept()-ed by server */
static uv_write_t write_req;
static uv_connect_t connect_req;
static int write_cb_called;
static int write_cb_error_called;
typedef struct {
uv_write_t req;
uv_buf_t buf;
} write_req_t;
static void connection_cb(uv_stream_t* server, int status) {
@ -75,40 +80,47 @@ static void connect_cb(uv_connect_t* req, int status) {
size_t size;
char* data;
int r;
write_req_t* wr;
ASSERT(req == &connect_req);
ASSERT(status == 0);
size = 10*1024*1024;
data = malloc(size);
ASSERT(data != NULL);
memset(data, '$', size);
buf = uv_buf_init(data, size);
write_req.data = data;
r = uv_write(&write_req, req->handle, &buf, 1, write_cb);
ASSERT(r == 0);
/* Write queue should have been updated. */
ASSERT(req->handle->write_queue_size > 0);
/* write_queue_size <= size, part may have already been written. */
ASSERT(req->handle->write_queue_size <= size);
while (1) {
size = 10 * 1024 * 1024;
data = malloc(size);
ASSERT(data != NULL);
memset(data, '$', size);
buf = uv_buf_init(data, size);
wr = (write_req_t*) malloc(sizeof *wr);
wr->buf = buf;
wr->req.data = data;
r = uv_write(&(wr->req), req->handle, &wr->buf, 1, write_cb);
ASSERT(r == 0);
if (req->handle->write_queue_size > 0) {
break;
}
}
}
static void write_cb(uv_write_t* req, int status) {
ASSERT(req == &write_req);
ASSERT(status == -1);
write_req_t* wr;
wr = (write_req_t*) req;
/* This is what this test is all about. */
ASSERT(tcp_client.write_queue_size == 0);
if (status == -1) {
write_cb_error_called++;
}
free(write_req.data);
if (req->handle->write_queue_size == 0) {
uv_close((uv_handle_t*)&tcp_client, NULL);
}
uv_close((uv_handle_t*)&tcp_client, NULL);
free(wr->buf.base);
free(wr);
write_cb_called++;
}
@ -148,7 +160,9 @@ TEST_IMPL(tcp_write_error) {
r = uv_run(loop);
ASSERT(r == 0);
ASSERT(write_cb_called == 1);
ASSERT(write_cb_called > 0);
ASSERT(write_cb_error_called == 1);
ASSERT(tcp_client.write_queue_size == 0);
return 0;
}

40
deps/uv/test/test-timer.c

@ -130,3 +130,43 @@ TEST_IMPL(timer) {
return 0;
}
TEST_IMPL(timer_ref) {
uv_timer_t never;
int r;
/* A timer just initialized should count as one reference. */
r = uv_timer_init(uv_default_loop(), &never);
ASSERT(r == 0);
/* One unref should set the loop ref count to zero. */
uv_unref(uv_default_loop());
/* Therefore this does not block */
uv_run(uv_default_loop());
return 0;
}
TEST_IMPL(timer_ref2) {
uv_timer_t never;
int r;
/* A timer just initialized should count as one reference. */
r = uv_timer_init(uv_default_loop(), &never);
ASSERT(r == 0);
/* We start the timer, this should not effect the ref count. */
r = uv_timer_start(&never, never_cb, 1000, 1000);
ASSERT(r == 0);
/* One unref should set the loop ref count to zero. */
uv_unref(uv_default_loop());
/* Therefore this does not block */
uv_run(uv_default_loop());
return 0;
}

4
lib/readline.js

@ -95,12 +95,12 @@ function Interface(input, output, completer) {
this.history = [];
this.historyIndex = -1;
var winSize = tty.getWindowSize(output.fd);
var winSize = output.getWindowSize();
exports.columns = winSize[1];
if (process.listeners('SIGWINCH').length === 0) {
process.on('SIGWINCH', function() {
var winSize = tty.getWindowSize(output.fd);
var winSize = output.getWindowSize();
exports.columns = winSize[1];
});
}

6
lib/repl.js

@ -56,11 +56,7 @@ function hasOwnProperty(obj, prop) {
var context;
exports.disableColors = true;
if (process.platform != 'win32') {
exports.disableColors = process.env.NODE_DISABLE_COLORS ? true : false;
}
exports.disableColors = process.env.NODE_DISABLE_COLORS ? true : false;
// hack for require.resolve("./relative") to work properly.
module.filename = process.cwd() + '/repl';

0
lib/tty.js → lib/tty_legacy.js

375
lib/tty_uv.js

@ -0,0 +1,375 @@
// 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.
var assert = require('assert');
var inherits = require('util').inherits;
var net = require('net');
var TTY = process.binding('tty_wrap').TTY;
var isTTY = process.binding('tty_wrap').isTTY;
var stdinHandle;
exports.isatty = function(fd) {
return isTTY(fd);
};
exports.setRawMode = function(flag) {
assert.ok(stdinHandle, "stdin must be initialized before calling setRawMode");
stdinHandle.setRawMode(flag);
};
exports.getWindowSize = function() {
//throw new Error("implement me");
return 80;
};
exports.setWindowSize = function() {
throw new Error("implement me");
};
function ReadStream(fd) {
if (!(this instanceof ReadStream)) return new ReadStream(fd);
net.Socket.call(this, {
handle: new TTY(fd)
});
this.writable = false;
var self = this,
keypressListeners = this.listeners('keypress');
function onData(b) {
if (keypressListeners.length) {
self._emitKey(b);
} else {
// Nobody's watching anyway
self.removeListener('data', onData);
self.on('newListener', onNewListener);
}
}
function onNewListener(event) {
if (event == 'keypress') {
self.on('data', onData);
self.removeListener('newListener', onNewListener);
}
}
if (!stdinHandle) stdinHandle = this._handle;
this.on('newListener', onNewListener);
}
inherits(ReadStream, net.Socket);
exports.ReadStream = ReadStream;
ReadStream.prototype.isTTY = true;
/*
Some patterns seen in terminal key escape codes, derived from combos seen
at http://www.midnight-commander.org/browser/lib/tty/key.c
ESC letter
ESC [ letter
ESC [ modifier letter
ESC [ 1 ; modifier letter
ESC [ num char
ESC [ num ; modifier char
ESC O letter
ESC O modifier letter
ESC O 1 ; modifier letter
ESC N letter
ESC [ [ num ; modifier char
ESC [ [ 1 ; modifier letter
ESC ESC [ num char
ESC ESC O letter
- char is usually ~ but $ and ^ also happen with rxvt
- modifier is 1 +
(shift * 1) +
(left_alt * 2) +
(ctrl * 4) +
(right_alt * 8)
- two leading ESCs apparently mean the same as one leading ESC
*/
// Regexes used for ansi escape code splitting
var metaKeyCodeRe = /^(?:\x1b)([a-zA-Z0-9])$/;
var functionKeyCodeRe =
/^(?:\x1b+)(O|N|\[|\[\[)(?:(\d+)(?:;(\d+))?([~^$])|(?:1;)?(\d+)?([a-zA-Z]))/;
ReadStream.prototype._emitKey = function(s) {
var char,
key = {
name: undefined,
ctrl: false,
meta: false,
shift: false
},
parts;
if (Buffer.isBuffer(s)) {
if (s[0] > 127 && s[1] === undefined) {
s[0] -= 128;
s = '\x1b' + s.toString(this.encoding || 'utf-8');
} else {
s = s.toString(this.encoding || 'utf-8');
}
}
if (s === '\r' || s === '\n') {
// enter
key.name = 'enter';
} else if (s === '\t') {
// tab
key.name = 'tab';
} else if (s === '\b' || s === '\x7f' ||
s === '\x1b\x7f' || s === '\x1b\b') {
// backspace or ctrl+h
key.name = 'backspace';
key.meta = (s.charAt(0) === '\x1b');
} else if (s === '\x1b' || s === '\x1b\x1b') {
// escape key
key.name = 'escape';
key.meta = (s.length === 2);
} else if (s === ' ' || s === '\x1b ') {
key.name = 'space';
key.meta = (s.length === 2);
} else if (s <= '\x1a') {
// ctrl+letter
key.name = String.fromCharCode(s.charCodeAt(0) + 'a'.charCodeAt(0) - 1);
key.ctrl = true;
} else if (s.length === 1 && s >= 'a' && s <= 'z') {
// lowercase letter
key.name = s;
} else if (s.length === 1 && s >= 'A' && s <= 'Z') {
// shift+letter
key.name = s.toLowerCase();
key.shift = true;
} else if (parts = metaKeyCodeRe.exec(s)) {
// meta+character key
key.name = parts[1].toLowerCase();
key.meta = true;
key.shift = /^[A-Z]$/.test(parts[1]);
} else if (parts = functionKeyCodeRe.exec(s)) {
// ansi escape sequence
// reassemble the key code leaving out leading \x1b's,
// the modifier key bitflag and any meaningless "1;" sequence
var code = (parts[1] || '') + (parts[2] || '') +
(parts[4] || '') + (parts[6] || ''),
modifier = (parts[3] || parts[5] || 1) - 1;
// Parse the key modifier
key.ctrl = !!(modifier & 4);
key.meta = !!(modifier & 10);
key.shift = !!(modifier & 1);
// Parse the key itself
switch (code) {
/* xterm/gnome ESC O letter */
case 'OP': key.name = 'f1'; break;
case 'OQ': key.name = 'f2'; break;
case 'OR': key.name = 'f3'; break;
case 'OS': key.name = 'f4'; break;
/* xterm/rxvt ESC [ number ~ */
case '[11~': key.name = 'f1'; break;
case '[12~': key.name = 'f2'; break;
case '[13~': key.name = 'f3'; break;
case '[14~': key.name = 'f4'; break;
/* common */
case '[15~': key.name = 'f5'; break;
case '[17~': key.name = 'f6'; break;
case '[18~': key.name = 'f7'; break;
case '[19~': key.name = 'f8'; break;
case '[20~': key.name = 'f9'; break;
case '[21~': key.name = 'f10'; break;
case '[23~': key.name = 'f11'; break;
case '[24~': key.name = 'f12'; break;
/* xterm ESC [ letter */
case '[A': key.name = 'up'; break;
case '[B': key.name = 'down'; break;
case '[C': key.name = 'right'; break;
case '[D': key.name = 'left'; break;
case '[E': key.name = 'clear'; break;
case '[F': key.name = 'end'; break;
case '[H': key.name = 'home'; break;
/* xterm/gnome ESC O letter */
case 'OA': key.name = 'up'; break;
case 'OB': key.name = 'down'; break;
case 'OC': key.name = 'right'; break;
case 'OD': key.name = 'left'; break;
case 'OE': key.name = 'clear'; break;
case 'OF': key.name = 'end'; break;
case 'OH': key.name = 'home'; break;
/* xterm/rxvt ESC [ number ~ */
case '[1~': key.name = 'home'; break;
case '[2~': key.name = 'insert'; break;
case '[3~': key.name = 'delete'; break;
case '[4~': key.name = 'end'; break;
case '[5~': key.name = 'pageup'; break;
case '[6~': key.name = 'pagedown'; break;
/* putty */
case '[[5~': key.name = 'pageup'; break;
case '[[6~': key.name = 'pagedown'; break;
/* rxvt */
case '[7~': key.name = 'home'; break;
case '[8~': key.name = 'end'; break;
/* rxvt keys with modifiers */
case '[a': key.name = 'up'; key.shift = true; break;
case '[b': key.name = 'down'; key.shift = true; break;
case '[c': key.name = 'right'; key.shift = true; break;
case '[d': key.name = 'left'; key.shift = true; break;
case '[e': key.name = 'clear'; key.shift = true; break;
case '[2$': key.name = 'insert'; key.shift = true; break;
case '[3$': key.name = 'delete'; key.shift = true; break;
case '[5$': key.name = 'pageup'; key.shift = true; break;
case '[6$': key.name = 'pagedown'; key.shift = true; break;
case '[7$': key.name = 'home'; key.shift = true; break;
case '[8$': key.name = 'end'; key.shift = true; break;
case 'Oa': key.name = 'up'; key.ctrl = true; break;
case 'Ob': key.name = 'down'; key.ctrl = true; break;
case 'Oc': key.name = 'right'; key.ctrl = true; break;
case 'Od': key.name = 'left'; key.ctrl = true; break;
case 'Oe': key.name = 'clear'; key.ctrl = true; break;
case '[2^': key.name = 'insert'; key.ctrl = true; break;
case '[3^': key.name = 'delete'; key.ctrl = true; break;
case '[5^': key.name = 'pageup'; key.ctrl = true; break;
case '[6^': key.name = 'pagedown'; key.ctrl = true; break;
case '[7^': key.name = 'home'; key.ctrl = true; break;
case '[8^': key.name = 'end'; key.ctrl = true; break;
/* misc. */
case '[Z': key.name = 'tab'; key.shift = true; break;
}
} else if (s.length > 1 && s[0] !== '\x1b') {
// Got a longer-than-one string of characters.
// Probably a paste, since it wasn't a control sequence.
Array.prototype.forEach.call(s, this._emitKey, this);
return;
}
// Don't emit a key if no name was found
if (key.name === undefined) {
key = undefined;
}
if (s.length === 1) {
char = s;
}
if (key || char) {
this.emit('keypress', char, key);
}
};
function WriteStream(fd) {
if (!(this instanceof WriteStream)) return new WriteStream(fd);
net.Socket.call(this, {
handle: new TTY(fd)
});
this.readable = false;
}
inherits(WriteStream, net.Socket);
exports.WriteStream = WriteStream;
WriteStream.prototype.isTTY = true;
WriteStream.prototype.cursorTo = function(x, y) {
if (typeof x !== 'number' && typeof y !== 'number')
return;
if (typeof x !== 'number')
throw new Error("Can't set cursor row without also setting it's column");
if (typeof y !== 'number') {
this.write('\x1b[' + (x + 1) + 'G');
} else {
this.write('\x1b[' + (y + 1) + ';' + (x + 1) + 'H');
}
};
WriteStream.prototype.moveCursor = function(dx, dy) {
if (dx < 0) {
this.write('\x1b[' + (-dx) + 'D');
} else if (dx > 0) {
this.write('\x1b[' + dx + 'C');
}
if (dy < 0) {
this.write('\x1b[' + (-dy) + 'A');
} else if (dy > 0) {
this.write('\x1b[' + dy + 'B');
}
};
WriteStream.prototype.clearLine = function(dir) {
if (dir < 0) {
// to the beginning
this.write('\x1b[1K');
} else if (dir > 0) {
// to the end
this.write('\x1b[0K');
} else {
// entire line
this.write('\x1b[2K');
}
};
WriteStream.prototype.getWindowSize = function() {
return this._handle.getWindowSize();
};

3
node.gyp

@ -43,8 +43,9 @@
'lib/timers_legacy.js',
'lib/timers_uv.js',
'lib/tls.js',
'lib/tty.js',
'lib/tty_legacy.js',
'lib/tty_posix.js',
'lib/tty_uv.js',
'lib/tty_win32.js',
'lib/url.js',
'lib/util.js',

123
src/node.js

@ -108,10 +108,10 @@
} else {
var binding = process.binding('stdio');
var fd = binding.openStdin();
var Module = NativeModule.require('module');
if (NativeModule.require('tty').isatty(fd)) {
// If stdin is a TTY.
if (NativeModule.require('tty').isatty(0)) {
// REPL
Module.requireRepl().start();
@ -223,43 +223,54 @@
process.__defineGetter__('stdout', function() {
if (stdout) return stdout;
var binding = process.binding('stdio'),
fd = binding.stdoutFD;
var tty_wrap = process.binding('tty_wrap');
var binding = process.binding('stdio');
var fd = 1;
// Note stdout._type is used for test-module-load-list.js
if (binding.isatty(fd)) {
var tty = NativeModule.require('tty');
stdout = new tty.WriteStream(fd);
stdout._type = "tty";
// FIXME Hack to have stdout not keep the event loop alive.
// See https://github.com/joyent/node/issues/1726
binding.unref();
stdout.on('close', function() {
binding.ref();
});
} else if (binding.isStdoutBlocking()) {
var fs = NativeModule.require('fs');
stdout = new fs.WriteStream(null, {fd: fd});
stdout._type = "fs";
} else {
var net = NativeModule.require('net');
stdout = new net.Stream(fd);
// FIXME Hack to have stdout not keep the event loop alive.
// See https://github.com/joyent/node/issues/1726
binding.unref();
stdout.on('close', function() {
binding.ref();
});
// FIXME Should probably have an option in net.Stream to create a
// stream from an existing fd which is writable only. But for now
// we'll just add this hack and set the `readable` member to false.
// Test: ./node test/fixtures/echo.js < /etc/passwd
stdout.readable = false;
stdout._type = "pipe";
switch (tty_wrap.guessHandleType(fd)) {
case 'TTY':
var tty = NativeModule.require('tty');
stdout = new tty.WriteStream(fd);
stdout._type = "tty";
// FIXME Hack to have stdout not keep the event loop alive.
// See https://github.com/joyent/node/issues/1726
binding.unref();
stdout.on('close', function() {
binding.ref();
});
break;
case 'FILE':
var fs = NativeModule.require('fs');
stdout = new fs.WriteStream(null, {fd: fd});
stdout._type = "fs";
break;
case 'PIPE':
var net = NativeModule.require('net');
stdout = new net.Stream(fd);
// FIXME Should probably have an option in net.Stream to create a
// stream from an existing fd which is writable only. But for now
// we'll just add this hack and set the `readable` member to false.
// Test: ./node test/fixtures/echo.js < /etc/passwd
stdout.readable = false;
stdout._type = "pipe";
// FIXME Hack to have stdout not keep the event loop alive.
// See https://github.com/joyent/node/issues/1726
binding.unref();
stdout.on('close', function() {
binding.ref();
});
break;
default:
// Probably an error on in uv_guess_handle()
throw new Error("Implement me. Unknown stdout file type!");
}
// For supporting legacy API we put the FD here.
@ -280,19 +291,30 @@
process.__defineGetter__('stdin', function() {
if (stdin) return stdin;
var binding = process.binding('stdio'),
fd = binding.openStdin();
if (binding.isatty(fd)) {
var tty = NativeModule.require('tty');
stdin = new tty.ReadStream(fd);
} else if (binding.isStdinBlocking()) {
var fs = NativeModule.require('fs');
stdin = new fs.ReadStream(null, {fd: fd});
} else {
var net = NativeModule.require('net');
stdin = new net.Stream(fd);
stdin.readable = true;
var tty_wrap = process.binding('tty_wrap');
var binding = process.binding('stdio');
var fd = 0;
switch (tty_wrap.guessHandleType(fd)) {
case 'TTY':
var tty = NativeModule.require('tty');
stdin = new tty.ReadStream(fd);
break;
case 'FILE':
var fs = NativeModule.require('fs');
stdin = new fs.ReadStream(null, {fd: fd});
break;
case 'PIPE':
var net = NativeModule.require('net');
stdin = new net.Stream(fd);
stdin.readable = true;
break;
default:
// Probably an error on in uv_guess_handle()
throw new Error("Implement me. Unknown stdin file type!");
}
// For supporting legacy API we put the FD here.
@ -447,6 +469,9 @@
case 'net':
return process.features.uv ? 'net_uv' : 'net_legacy';
case 'tty':
return process.features.uv ? 'tty_uv' : 'tty_legacy';
case 'child_process':
return process.features.uv ? 'child_process_uv' :
'child_process_legacy';

71
src/tty_wrap.cc

@ -21,6 +21,16 @@ using v8::Arguments;
using v8::Integer;
using v8::Undefined;
#define UNWRAP \
assert(!args.Holder().IsEmpty()); \
assert(args.Holder()->InternalFieldCount() > 0); \
TTYWrap* wrap = \
static_cast<TTYWrap*>(args.Holder()->GetPointerFromInternalField(0)); \
if (!wrap) { \
SetErrno(UV_EBADF); \
return scope.Close(Integer::New(-1)); \
}
class TTYWrap : StreamWrap {
public:
@ -40,12 +50,39 @@ class TTYWrap : StreamWrap {
NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
NODE_SET_PROTOTYPE_METHOD(t, "write", StreamWrap::Write);
NODE_SET_PROTOTYPE_METHOD(t, "getWindowSize", TTYWrap::GetWindowSize);
NODE_SET_PROTOTYPE_METHOD(t, "setRawMode", SetRawMode);
NODE_SET_METHOD(target, "isTTY", IsTTY);
NODE_SET_METHOD(target, "guessHandleType", GuessHandleType);
target->Set(String::NewSymbol("TTY"), t->GetFunction());
}
private:
static Handle<Value> GuessHandleType(const Arguments& args) {
HandleScope scope;
int fd = args[0]->Int32Value();
assert(fd >= 0);
uv_handle_type t = uv_guess_handle(fd);
switch (t) {
case UV_TTY:
return scope.Close(String::New("TTY"));
case UV_NAMED_PIPE:
return scope.Close(String::New("PIPE"));
case UV_FILE:
return scope.Close(String::New("FILE"));
default:
assert(0);
return v8::Undefined();
}
}
static Handle<Value> IsTTY(const Arguments& args) {
HandleScope scope;
int fd = args[0]->Int32Value();
@ -53,6 +90,40 @@ class TTYWrap : StreamWrap {
return uv_guess_handle(fd) == UV_TTY ? v8::True() : v8::False();
}
static Handle<Value> GetWindowSize(const Arguments& args) {
HandleScope scope;
UNWRAP
int width, height;
int r = uv_tty_get_winsize(&wrap->handle_, &width, &height);
if (r) {
SetErrno(uv_last_error(uv_default_loop()).code);
return v8::Undefined();
}
Local<v8::Array> a = v8::Array::New(2);
a->Set(0, Integer::New(width));
a->Set(1, Integer::New(height));
return scope.Close(a);
}
static Handle<Value> SetRawMode(const Arguments& args) {
HandleScope scope;
UNWRAP
int r = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
if (r) {
SetErrno(uv_last_error(uv_default_loop()).code);
}
return scope.Close(Integer::New(r));
}
static Handle<Value> New(const Arguments& args) {
HandleScope scope;

9
test/simple/test-module-load-list.js

@ -78,25 +78,26 @@ if (!process.features.uv) {
case 'fs':
expected = expected.concat([
'NativeModule console',
'Binding tty_wrap',
]);
break;
case 'tty':
expected = expected.concat([
'NativeModule console',
'NativeModule tty',
'NativeModule tty_posix',
'Binding tty_wrap',
'NativeModule tty_uv',
'NativeModule net_uv',
'NativeModule timers_uv',
'Binding timer_wrap',
'NativeModule _linklist',
'Binding pipe_wrap',
'NativeModule _linklist'
]);
break;
case 'pipe':
expected = expected.concat([
'NativeModule console',
'Binding tty_wrap',
'NativeModule net_uv',
'NativeModule timers_uv',
'Binding timer_wrap',

Loading…
Cancel
Save