Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 181 lines (139 sloc) 5.098 kB
60818b9 @ry Add missing copyright headers
ry authored
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
eef57dd @bnoordhuis core: use proper #include directives
bnoordhuis authored
22 #include "node.h"
23 #include "node_buffer.h"
24 #include "req_wrap.h"
25 #include "handle_wrap.h"
26 #include "stream_wrap.h"
c1ae6ea @ry Add TTYWrap
ry authored
27
28 namespace node {
29
30 using v8::Object;
31 using v8::Handle;
32 using v8::Local;
33 using v8::Persistent;
34 using v8::Value;
35 using v8::HandleScope;
36 using v8::FunctionTemplate;
37 using v8::String;
38 using v8::Function;
39 using v8::TryCatch;
40 using v8::Context;
41 using v8::Arguments;
42 using v8::Integer;
43 using v8::Undefined;
44
e1dc6e6 @ry Bind uv_tty_get_winsize
ry authored
45 #define UNWRAP \
46 assert(!args.Holder().IsEmpty()); \
47 assert(args.Holder()->InternalFieldCount() > 0); \
48 TTYWrap* wrap = \
49 static_cast<TTYWrap*>(args.Holder()->GetPointerFromInternalField(0)); \
50 if (!wrap) { \
6cc4292 @ry Display sys_errno when UV_UNKNOWN is returned
ry authored
51 uv_err_t err; \
52 err.code = UV_EBADF; \
53 SetErrno(err); \
e1dc6e6 @ry Bind uv_tty_get_winsize
ry authored
54 return scope.Close(Integer::New(-1)); \
55 }
56
c1ae6ea @ry Add TTYWrap
ry authored
57
58 class TTYWrap : StreamWrap {
59 public:
60 static void Initialize(Handle<Object> target) {
61 StreamWrap::Initialize(target);
62
63 HandleScope scope;
64
65 Local<FunctionTemplate> t = FunctionTemplate::New(New);
66 t->SetClassName(String::NewSymbol("TTY"));
67
68 t->InstanceTemplate()->SetInternalFieldCount(1);
69
70 NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
7e62bc9 @ry Move process.stdout unref hack to handle_wrap.cc
ry authored
71 NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);
cf20b6b @isaacs Fix #2257 pause/resume semantics for stdin
isaacs authored
72 NODE_SET_PROTOTYPE_METHOD(t, "ref", HandleWrap::Ref);
c1ae6ea @ry Add TTYWrap
ry authored
73
74 NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
75 NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
76 NODE_SET_PROTOTYPE_METHOD(t, "write", StreamWrap::Write);
77
74b6426 @ry Initial pass at new TTY js layer
ry authored
78 NODE_SET_PROTOTYPE_METHOD(t, "getWindowSize", TTYWrap::GetWindowSize);
79 NODE_SET_PROTOTYPE_METHOD(t, "setRawMode", SetRawMode);
80
c1ae6ea @ry Add TTYWrap
ry authored
81 NODE_SET_METHOD(target, "isTTY", IsTTY);
1de156a @ry Bind/use uv_guess_handle
ry authored
82 NODE_SET_METHOD(target, "guessHandleType", GuessHandleType);
c1ae6ea @ry Add TTYWrap
ry authored
83
84 target->Set(String::NewSymbol("TTY"), t->GetFunction());
85 }
86
87 private:
1de156a @ry Bind/use uv_guess_handle
ry authored
88 static Handle<Value> GuessHandleType(const Arguments& args) {
89 HandleScope scope;
90 int fd = args[0]->Int32Value();
91 assert(fd >= 0);
92
93 uv_handle_type t = uv_guess_handle(fd);
94
95 switch (t) {
96 case UV_TTY:
2e389c6 @ry scope.Close in GuessHandleType; don't use uv_is_tty
ry authored
97 return scope.Close(String::New("TTY"));
1de156a @ry Bind/use uv_guess_handle
ry authored
98
99 case UV_NAMED_PIPE:
2e389c6 @ry scope.Close in GuessHandleType; don't use uv_is_tty
ry authored
100 return scope.Close(String::New("PIPE"));
1de156a @ry Bind/use uv_guess_handle
ry authored
101
102 case UV_FILE:
2e389c6 @ry scope.Close in GuessHandleType; don't use uv_is_tty
ry authored
103 return scope.Close(String::New("FILE"));
1de156a @ry Bind/use uv_guess_handle
ry authored
104
105 default:
106 assert(0);
107 return v8::Undefined();
108 }
109 }
110
c1ae6ea @ry Add TTYWrap
ry authored
111 static Handle<Value> IsTTY(const Arguments& args) {
112 HandleScope scope;
113 int fd = args[0]->Int32Value();
114 assert(fd >= 0);
94bedc6 @ry Upgrade libuv to f20297f
ry authored
115 return uv_guess_handle(fd) == UV_TTY ? v8::True() : v8::False();
c1ae6ea @ry Add TTYWrap
ry authored
116 }
117
74b6426 @ry Initial pass at new TTY js layer
ry authored
118 static Handle<Value> GetWindowSize(const Arguments& args) {
119 HandleScope scope;
189dd8f @piscisaureus Fix line endings and trailing whitespace
piscisaureus authored
120
74b6426 @ry Initial pass at new TTY js layer
ry authored
121 UNWRAP
122
123 int width, height;
124 int r = uv_tty_get_winsize(&wrap->handle_, &width, &height);
125
126 if (r) {
6cc4292 @ry Display sys_errno when UV_UNKNOWN is returned
ry authored
127 SetErrno(uv_last_error(uv_default_loop()));
74b6426 @ry Initial pass at new TTY js layer
ry authored
128 return v8::Undefined();
129 }
130
131 Local<v8::Array> a = v8::Array::New(2);
132 a->Set(0, Integer::New(width));
133 a->Set(1, Integer::New(height));
134
135 return scope.Close(a);
136 }
137
138 static Handle<Value> SetRawMode(const Arguments& args) {
139 HandleScope scope;
140
e1dc6e6 @ry Bind uv_tty_get_winsize
ry authored
141 UNWRAP
74b6426 @ry Initial pass at new TTY js layer
ry authored
142
143 int r = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
144
145 if (r) {
6cc4292 @ry Display sys_errno when UV_UNKNOWN is returned
ry authored
146 SetErrno(uv_last_error(uv_default_loop()));
74b6426 @ry Initial pass at new TTY js layer
ry authored
147 }
148
149 return scope.Close(Integer::New(r));
150 }
151
c1ae6ea @ry Add TTYWrap
ry authored
152 static Handle<Value> New(const Arguments& args) {
153 HandleScope scope;
154
155 // This constructor should not be exposed to public javascript.
156 // Therefore we assert that we are not trying to call this as a
157 // normal function.
158 assert(args.IsConstructCall());
159
160 int fd = args[0]->Int32Value();
161 assert(fd >= 0);
162
7cf787a @ry Upgrade libuv to 5656e3
ry authored
163 TTYWrap* wrap = new TTYWrap(args.This(), fd, args[1]->IsTrue());
c1ae6ea @ry Add TTYWrap
ry authored
164 assert(wrap);
165 wrap->UpdateWriteQueueSize();
166
167 return scope.Close(args.This());
168 }
169
7cf787a @ry Upgrade libuv to 5656e3
ry authored
170 TTYWrap(Handle<Object> object, int fd, bool readable)
c1ae6ea @ry Add TTYWrap
ry authored
171 : StreamWrap(object, (uv_stream_t*)&handle_) {
7cf787a @ry Upgrade libuv to 5656e3
ry authored
172 uv_tty_init(uv_default_loop(), &handle_, fd, readable);
c1ae6ea @ry Add TTYWrap
ry authored
173 }
174
175 uv_tty_t handle_;
176 };
177
178 } // namespace node
179
cdcb111 @bnoordhuis Remove stray NODE_MODULE() semi-colons.
bnoordhuis authored
180 NODE_MODULE(node_tty_wrap, node::TTYWrap::Initialize)
Something went wrong with that request. Please try again.