Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 203 lines (148 sloc) 5.578 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
ff4a9d3 @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"
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
27 #include "tty_wrap.h"
c1ae6ea @ry Add TTYWrap
ry authored
28
29 namespace node {
30
7bdd05b @bnoordhuis stream_wrap, udp_wrap: add read-only fd property
bnoordhuis authored
31 using v8::Arguments;
32 using v8::Context;
33 using v8::Function;
34 using v8::FunctionTemplate;
c1ae6ea @ry Add TTYWrap
ry authored
35 using v8::Handle;
7bdd05b @bnoordhuis stream_wrap, udp_wrap: add read-only fd property
bnoordhuis authored
36 using v8::HandleScope;
37 using v8::Integer;
c1ae6ea @ry Add TTYWrap
ry authored
38 using v8::Local;
7bdd05b @bnoordhuis stream_wrap, udp_wrap: add read-only fd property
bnoordhuis authored
39 using v8::Object;
c1ae6ea @ry Add TTYWrap
ry authored
40 using v8::Persistent;
7bdd05b @bnoordhuis stream_wrap, udp_wrap: add read-only fd property
bnoordhuis authored
41 using v8::PropertyAttribute;
c1ae6ea @ry Add TTYWrap
ry authored
42 using v8::String;
43 using v8::TryCatch;
44 using v8::Undefined;
7bdd05b @bnoordhuis stream_wrap, udp_wrap: add read-only fd property
bnoordhuis authored
45 using v8::Value;
c1ae6ea @ry Add TTYWrap
ry authored
46
47
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
48 void TTYWrap::Initialize(Handle<Object> target) {
49 StreamWrap::Initialize(target);
c1ae6ea @ry Add TTYWrap
ry authored
50
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
51 HandleScope scope;
c1ae6ea @ry Add TTYWrap
ry authored
52
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
53 Local<FunctionTemplate> t = FunctionTemplate::New(New);
54 t->SetClassName(String::NewSymbol("TTY"));
c1ae6ea @ry Add TTYWrap
ry authored
55
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
56 t->InstanceTemplate()->SetInternalFieldCount(1);
c1ae6ea @ry Add TTYWrap
ry authored
57
7bdd05b @bnoordhuis stream_wrap, udp_wrap: add read-only fd property
bnoordhuis authored
58 enum PropertyAttribute attributes =
59 static_cast<PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
60 t->InstanceTemplate()->SetAccessor(String::New("fd"),
61 StreamWrap::GetFD,
62 NULL,
63 Handle<Value>(),
64 v8::DEFAULT,
65 attributes);
66
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
67 NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
68 NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);
0e57aaf @piscisaureus Optimize writing strings with Socket.write
piscisaureus authored
69
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
70 NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
71 NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
c1ae6ea @ry Add TTYWrap
ry authored
72
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
73 NODE_SET_PROTOTYPE_METHOD(t, "writeBuffer", StreamWrap::WriteBuffer);
74 NODE_SET_PROTOTYPE_METHOD(t, "writeAsciiString", StreamWrap::WriteAsciiString);
75 NODE_SET_PROTOTYPE_METHOD(t, "writeUtf8String", StreamWrap::WriteUtf8String);
76 NODE_SET_PROTOTYPE_METHOD(t, "writeUcs2String", StreamWrap::WriteUcs2String);
74b6426 @ry Initial pass at new TTY js layer
ry authored
77
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
78 NODE_SET_PROTOTYPE_METHOD(t, "getWindowSize", TTYWrap::GetWindowSize);
79 NODE_SET_PROTOTYPE_METHOD(t, "setRawMode", SetRawMode);
c1ae6ea @ry Add TTYWrap
ry authored
80
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
81 NODE_SET_METHOD(target, "isTTY", IsTTY);
82 NODE_SET_METHOD(target, "guessHandleType", GuessHandleType);
c1ae6ea @ry Add TTYWrap
ry authored
83
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
84 target->Set(String::NewSymbol("TTY"), t->GetFunction());
85 }
1de156a @ry Bind/use uv_guess_handle
ry authored
86
87
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
88 TTYWrap* TTYWrap::Unwrap(Local<Object> obj) {
89 assert(!obj.IsEmpty());
90 assert(obj->InternalFieldCount() > 0);
51f6e6a @bnoordhuis src, test: downgrade to v8 3.14 api
bnoordhuis authored
91 return static_cast<TTYWrap*>(obj->GetPointerFromInternalField(0));
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
92 }
1de156a @ry Bind/use uv_guess_handle
ry authored
93
94
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
95 uv_tty_t* TTYWrap::UVHandle() {
96 return &handle_;
97 }
1de156a @ry Bind/use uv_guess_handle
ry authored
98
99
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
100 Handle<Value> TTYWrap::GuessHandleType(const Arguments& args) {
101 HandleScope scope;
102 int fd = args[0]->Int32Value();
103 assert(fd >= 0);
104
105 uv_handle_type t = uv_guess_handle(fd);
c1ae6ea @ry Add TTYWrap
ry authored
106
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
107 switch (t) {
108 case UV_TTY:
109 return scope.Close(String::New("TTY"));
189dd8f @piscisaureus Fix line endings and trailing whitespace
piscisaureus authored
110
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
111 case UV_NAMED_PIPE:
112 return scope.Close(String::New("PIPE"));
74b6426 @ry Initial pass at new TTY js layer
ry authored
113
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
114 case UV_FILE:
115 return scope.Close(String::New("FILE"));
74b6426 @ry Initial pass at new TTY js layer
ry authored
116
3a6314d @bnoordhuis net: fix net.Server.listen({fd:x}) error reporting
bnoordhuis authored
117 case UV_UNKNOWN_HANDLE:
118 return scope.Close(String::New("UNKNOWN"));
119
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
120 default:
121 assert(0);
51f6e6a @bnoordhuis src, test: downgrade to v8 3.14 api
bnoordhuis authored
122 return v8::Undefined();
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
123 }
124 }
74b6426 @ry Initial pass at new TTY js layer
ry authored
125
126
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
127 Handle<Value> TTYWrap::IsTTY(const Arguments& args) {
128 HandleScope scope;
129 int fd = args[0]->Int32Value();
130 assert(fd >= 0);
109f73b @bnoordhuis src: pass node_isolate to True() and False()
bnoordhuis authored
131
132 if (uv_guess_handle(fd) == UV_TTY) {
51f6e6a @bnoordhuis src, test: downgrade to v8 3.14 api
bnoordhuis authored
133 return v8::True();
109f73b @bnoordhuis src: pass node_isolate to True() and False()
bnoordhuis authored
134 }
135
51f6e6a @bnoordhuis src, test: downgrade to v8 3.14 api
bnoordhuis authored
136 return v8::False();
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
137 }
74b6426 @ry Initial pass at new TTY js layer
ry authored
138
139
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
140 Handle<Value> TTYWrap::GetWindowSize(const Arguments& args) {
141 HandleScope scope;
74b6426 @ry Initial pass at new TTY js layer
ry authored
142
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
143 UNWRAP(TTYWrap)
74b6426 @ry Initial pass at new TTY js layer
ry authored
144
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
145 int width, height;
146 int r = uv_tty_get_winsize(&wrap->handle_, &width, &height);
74b6426 @ry Initial pass at new TTY js layer
ry authored
147
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
148 if (r) {
149 SetErrno(uv_last_error(uv_default_loop()));
51f6e6a @bnoordhuis src, test: downgrade to v8 3.14 api
bnoordhuis authored
150 return v8::Undefined();
74b6426 @ry Initial pass at new TTY js layer
ry authored
151 }
152
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
153 Local<v8::Array> a = v8::Array::New(2);
51f6e6a @bnoordhuis src, test: downgrade to v8 3.14 api
bnoordhuis authored
154 a->Set(0, Integer::New(width));
155 a->Set(1, Integer::New(height));
c1ae6ea @ry Add TTYWrap
ry authored
156
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
157 return scope.Close(a);
158 }
c1ae6ea @ry Add TTYWrap
ry authored
159
160
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
161 Handle<Value> TTYWrap::SetRawMode(const Arguments& args) {
162 HandleScope scope;
c1ae6ea @ry Add TTYWrap
ry authored
163
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
164 UNWRAP(TTYWrap)
165
166 int r = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
c1ae6ea @ry Add TTYWrap
ry authored
167
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
168 if (r) {
169 SetErrno(uv_last_error(uv_default_loop()));
c1ae6ea @ry Add TTYWrap
ry authored
170 }
171
51f6e6a @bnoordhuis src, test: downgrade to v8 3.14 api
bnoordhuis authored
172 return scope.Close(Integer::New(r));
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
173 }
174
175
176 Handle<Value> TTYWrap::New(const Arguments& args) {
177 HandleScope scope;
178
179 // This constructor should not be exposed to public javascript.
180 // Therefore we assert that we are not trying to call this as a
181 // normal function.
182 assert(args.IsConstructCall());
183
184 int fd = args[0]->Int32Value();
185 assert(fd >= 0);
186
187 TTYWrap* wrap = new TTYWrap(args.This(), fd, args[1]->IsTrue());
188 assert(wrap);
189 wrap->UpdateWriteQueueSize();
190
191 return scope.Close(args.This());
192 }
193
194
195 TTYWrap::TTYWrap(Handle<Object> object, int fd, bool readable)
196 : StreamWrap(object, (uv_stream_t*)&handle_) {
197 uv_tty_init(uv_default_loop(), &handle_, fd, readable);
198 }
c1ae6ea @ry Add TTYWrap
ry authored
199
200 } // namespace node
201
cdcb111 @bnoordhuis Remove stray NODE_MODULE() semi-colons.
bnoordhuis authored
202 NODE_MODULE(node_tty_wrap, node::TTYWrap::Initialize)
Something went wrong with that request. Please try again.