summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2011-09-20 13:38:07 -0700
committerRyan Dahl <ry@tinyclouds.org>2011-09-20 13:39:48 -0700
commitc1ae6ea2f2abc6ca019cae9a171b5b52c725da68 (patch)
tree55084e85ba420f62a0a07ecfd84ef2df722ac2a3 /src
parent6b98a6397468a802603d1566a45b4b578ae64ed4 (diff)
downloadnode-c1ae6ea2f2abc6ca019cae9a171b5b52c725da68.tar.gz
Add TTYWrap
Diffstat (limited to 'src')
-rw-r--r--src/node_extensions.h1
-rw-r--r--src/tty_wrap.cc85
2 files changed, 86 insertions, 0 deletions
diff --git a/src/node_extensions.h b/src/node_extensions.h
index 19e0777e7..d2e486d1d 100644
--- a/src/node_extensions.h
+++ b/src/node_extensions.h
@@ -49,6 +49,7 @@ NODE_EXT_LIST_ITEM(node_udp_wrap)
NODE_EXT_LIST_ITEM(node_pipe_wrap)
NODE_EXT_LIST_ITEM(node_cares_wrap)
NODE_EXT_LIST_ITEM(node_stdio_wrap)
+NODE_EXT_LIST_ITEM(node_tty_wrap)
NODE_EXT_LIST_ITEM(node_process_wrap)
NODE_EXT_LIST_END
diff --git a/src/tty_wrap.cc b/src/tty_wrap.cc
new file mode 100644
index 000000000..692b2bbaf
--- /dev/null
+++ b/src/tty_wrap.cc
@@ -0,0 +1,85 @@
+#include <node.h>
+#include <node_buffer.h>
+#include <req_wrap.h>
+#include <handle_wrap.h>
+#include <stream_wrap.h>
+
+namespace node {
+
+using v8::Object;
+using v8::Handle;
+using v8::Local;
+using v8::Persistent;
+using v8::Value;
+using v8::HandleScope;
+using v8::FunctionTemplate;
+using v8::String;
+using v8::Function;
+using v8::TryCatch;
+using v8::Context;
+using v8::Arguments;
+using v8::Integer;
+using v8::Undefined;
+
+
+class TTYWrap : StreamWrap {
+ public:
+ static void Initialize(Handle<Object> target) {
+ StreamWrap::Initialize(target);
+
+ HandleScope scope;
+
+ Local<FunctionTemplate> t = FunctionTemplate::New(New);
+ t->SetClassName(String::NewSymbol("TTY"));
+
+ t->InstanceTemplate()->SetInternalFieldCount(1);
+
+ NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
+
+ NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
+ NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
+ NODE_SET_PROTOTYPE_METHOD(t, "write", StreamWrap::Write);
+ NODE_SET_PROTOTYPE_METHOD(t, "write", StreamWrap::Write);
+
+ NODE_SET_METHOD(target, "isTTY", IsTTY);
+
+ target->Set(String::NewSymbol("TTY"), t->GetFunction());
+ }
+
+ private:
+ static Handle<Value> IsTTY(const Arguments& args) {
+ HandleScope scope;
+ int fd = args[0]->Int32Value();
+ assert(fd >= 0);
+ return uv_is_tty(fd) ? v8::True() : v8::False();
+ }
+
+ static Handle<Value> New(const Arguments& args) {
+ HandleScope scope;
+
+ // This constructor should not be exposed to public javascript.
+ // Therefore we assert that we are not trying to call this as a
+ // normal function.
+ assert(args.IsConstructCall());
+
+ int fd = args[0]->Int32Value();
+ assert(fd >= 0);
+
+ TTYWrap* wrap = new TTYWrap(args.This(), fd);
+ assert(wrap);
+ wrap->UpdateWriteQueueSize();
+
+ return scope.Close(args.This());
+ }
+
+ TTYWrap(Handle<Object> object, int fd)
+ : StreamWrap(object, (uv_stream_t*)&handle_) {
+ uv_tty_init(uv_default_loop(), &handle_, fd);
+ }
+
+ uv_tty_t handle_;
+};
+
+} // namespace node
+
+NODE_MODULE(node_tty_wrap, node::TTYWrap::Initialize);