Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Bind/use uv_guess_handle
  • Loading branch information
ry committed Sep 23, 2011
1 parent cb362d0 commit fdeddb6
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 36 deletions.
94 changes: 58 additions & 36 deletions src/node.js
Expand Up @@ -223,32 +223,43 @@
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)) {
binding.unref();
var tty = NativeModule.require('tty');
stdout = new tty.WriteStream(fd);
stdout._type = "tty";
} else if (binding.isStdoutBlocking()) {
var fs = NativeModule.require('fs');
stdout = new fs.WriteStream(null, {fd: fd});
stdout._type = "fs";
} else {
binding.unref();

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";
switch (tty_wrap.guessHandleType(fd)) {
case 'TTY':
binding.unref();
var tty = NativeModule.require('tty');
stdout = new tty.WriteStream(fd);
stdout._type = "tty";
break;

case 'FILE':
var fs = NativeModule.require('fs');
stdout = new fs.WriteStream(null, {fd: fd});
stdout._type = "fs";
break;

case 'PIPE':
binding.unref();

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";
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.
Expand All @@ -269,19 +280,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.
Expand Down
26 changes: 26 additions & 0 deletions src/tty_wrap.cc
Expand Up @@ -54,11 +54,37 @@ class TTYWrap : StreamWrap {
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 String::New("TTY");

case UV_NAMED_PIPE:
return String::New("PIPE");

case UV_FILE:
return String::New("FILE");

default:
assert(0);
return v8::Undefined();
}
return uv_is_tty(fd) ? v8::True() : v8::False();

}

static Handle<Value> IsTTY(const Arguments& args) {
HandleScope scope;
int fd = args[0]->Int32Value();
Expand Down

0 comments on commit fdeddb6

Please sign in to comment.