Skip to content
Newer
Older
100644 181 lines (136 sloc) 5.51 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"
6978e99 @indutny process: abstract out HandleToStream
indutny authored
28 #include "node_wrap.h"
c1ae6ea @ry Add TTYWrap
ry authored
29
30 namespace node {
31
7bdd05b @bnoordhuis stream_wrap, udp_wrap: add read-only fd property
bnoordhuis authored
32 using v8::Function;
110a9cd @bnoordhuis lib, src: upgrade after v8 api change
bnoordhuis authored
33 using v8::FunctionCallbackInfo;
7bdd05b @bnoordhuis stream_wrap, udp_wrap: add read-only fd property
bnoordhuis authored
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;
40 using v8::PropertyAttribute;
c1ae6ea @ry Add TTYWrap
ry authored
41 using v8::String;
7bdd05b @bnoordhuis stream_wrap, udp_wrap: add read-only fd property
bnoordhuis authored
42 using v8::Value;
c1ae6ea @ry Add TTYWrap
ry authored
43
44
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
45 void TTYWrap::Initialize(Handle<Object> target) {
46 StreamWrap::Initialize(target);
c1ae6ea @ry Add TTYWrap
ry authored
47
f65e14e @trevnorris src: pass Isolate to all applicable api
trevnorris authored
48 HandleScope scope(node_isolate);
c1ae6ea @ry Add TTYWrap
ry authored
49
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
50 Local<FunctionTemplate> t = FunctionTemplate::New(New);
51 t->SetClassName(String::NewSymbol("TTY"));
c1ae6ea @ry Add TTYWrap
ry authored
52
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
53 t->InstanceTemplate()->SetInternalFieldCount(1);
c1ae6ea @ry Add TTYWrap
ry authored
54
7bdd05b @bnoordhuis stream_wrap, udp_wrap: add read-only fd property
bnoordhuis authored
55 enum PropertyAttribute attributes =
56 static_cast<PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
57 t->InstanceTemplate()->SetAccessor(String::New("fd"),
58 StreamWrap::GetFD,
59 NULL,
60 Handle<Value>(),
61 v8::DEFAULT,
62 attributes);
63
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
64 NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
65 NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);
0e57aaf @piscisaureus Optimize writing strings with Socket.write
piscisaureus authored
66
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
67 NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
68 NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
c1ae6ea @ry Add TTYWrap
ry authored
69
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
70 NODE_SET_PROTOTYPE_METHOD(t, "writeBuffer", StreamWrap::WriteBuffer);
71 NODE_SET_PROTOTYPE_METHOD(t, "writeAsciiString", StreamWrap::WriteAsciiString);
72 NODE_SET_PROTOTYPE_METHOD(t, "writeUtf8String", StreamWrap::WriteUtf8String);
73 NODE_SET_PROTOTYPE_METHOD(t, "writeUcs2String", StreamWrap::WriteUcs2String);
74b6426 @ry Initial pass at new TTY js layer
ry authored
74
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
75 NODE_SET_PROTOTYPE_METHOD(t, "getWindowSize", TTYWrap::GetWindowSize);
76 NODE_SET_PROTOTYPE_METHOD(t, "setRawMode", SetRawMode);
c1ae6ea @ry Add TTYWrap
ry authored
77
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
78 NODE_SET_METHOD(target, "isTTY", IsTTY);
79 NODE_SET_METHOD(target, "guessHandleType", GuessHandleType);
c1ae6ea @ry Add TTYWrap
ry authored
80
110a9cd @bnoordhuis lib, src: upgrade after v8 api change
bnoordhuis authored
81 ttyConstructorTmpl.Reset(node_isolate, t);
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
82 target->Set(String::NewSymbol("TTY"), t->GetFunction());
83 }
1de156a @ry Bind/use uv_guess_handle
ry authored
84
85
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
86 TTYWrap* TTYWrap::Unwrap(Local<Object> obj) {
87 assert(!obj.IsEmpty());
88 assert(obj->InternalFieldCount() > 0);
0bba590 @trevnorris bindings: update api
trevnorris authored
89 return static_cast<TTYWrap*>(obj->GetAlignedPointerFromInternalField(0));
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
90 }
1de156a @ry Bind/use uv_guess_handle
ry authored
91
92
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
93 uv_tty_t* TTYWrap::UVHandle() {
94 return &handle_;
95 }
1de156a @ry Bind/use uv_guess_handle
ry authored
96
97
110a9cd @bnoordhuis lib, src: upgrade after v8 api change
bnoordhuis authored
98 void TTYWrap::GuessHandleType(const FunctionCallbackInfo<Value>& args) {
f65e14e @trevnorris src: pass Isolate to all applicable api
trevnorris authored
99 HandleScope scope(node_isolate);
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
100 int fd = args[0]->Int32Value();
101 assert(fd >= 0);
102
103 uv_handle_type t = uv_guess_handle(fd);
110a9cd @bnoordhuis lib, src: upgrade after v8 api change
bnoordhuis authored
104 const char* type = NULL;
c1ae6ea @ry Add TTYWrap
ry authored
105
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
106 switch (t) {
110a9cd @bnoordhuis lib, src: upgrade after v8 api change
bnoordhuis authored
107 case UV_TCP: type = "TCP"; break;
108 case UV_TTY: type = "TTY"; break;
109 case UV_UDP: type = "UDP"; break;
110 case UV_FILE: type = "FILE"; break;
111 case UV_NAMED_PIPE: type = "PIPE"; break;
112 case UV_UNKNOWN_HANDLE: type = "UNKNOWN"; break;
113 default:
114 abort();
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
115 }
110a9cd @bnoordhuis lib, src: upgrade after v8 api change
bnoordhuis authored
116
117 args.GetReturnValue().Set(String::New(type));
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
118 }
74b6426 @ry Initial pass at new TTY js layer
ry authored
119
120
110a9cd @bnoordhuis lib, src: upgrade after v8 api change
bnoordhuis authored
121 void TTYWrap::IsTTY(const FunctionCallbackInfo<Value>& args) {
f65e14e @trevnorris src: pass Isolate to all applicable api
trevnorris authored
122 HandleScope scope(node_isolate);
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
123 int fd = args[0]->Int32Value();
124 assert(fd >= 0);
110a9cd @bnoordhuis lib, src: upgrade after v8 api change
bnoordhuis authored
125 bool rc = uv_guess_handle(fd) == UV_TTY;
126 args.GetReturnValue().Set(rc);
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
127 }
74b6426 @ry Initial pass at new TTY js layer
ry authored
128
129
110a9cd @bnoordhuis lib, src: upgrade after v8 api change
bnoordhuis authored
130 void TTYWrap::GetWindowSize(const FunctionCallbackInfo<Value>& args) {
f65e14e @trevnorris src: pass Isolate to all applicable api
trevnorris authored
131 HandleScope scope(node_isolate);
74b6426 @ry Initial pass at new TTY js layer
ry authored
132
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
133 UNWRAP(TTYWrap)
74b6426 @ry Initial pass at new TTY js layer
ry authored
134
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
135 int width, height;
136 int r = uv_tty_get_winsize(&wrap->handle_, &width, &height);
110a9cd @bnoordhuis lib, src: upgrade after v8 api change
bnoordhuis authored
137 if (r) return SetErrno(uv_last_error(uv_default_loop()));
74b6426 @ry Initial pass at new TTY js layer
ry authored
138
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
139 Local<v8::Array> a = v8::Array::New(2);
f65e14e @trevnorris src: pass Isolate to all applicable api
trevnorris authored
140 a->Set(0, Integer::New(width, node_isolate));
141 a->Set(1, Integer::New(height, node_isolate));
110a9cd @bnoordhuis lib, src: upgrade after v8 api change
bnoordhuis authored
142 args.GetReturnValue().Set(a);
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
143 }
c1ae6ea @ry Add TTYWrap
ry authored
144
145
110a9cd @bnoordhuis lib, src: upgrade after v8 api change
bnoordhuis authored
146 void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
f65e14e @trevnorris src: pass Isolate to all applicable api
trevnorris authored
147 HandleScope scope(node_isolate);
c1ae6ea @ry Add TTYWrap
ry authored
148
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
149 UNWRAP(TTYWrap)
150
151 int r = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
110a9cd @bnoordhuis lib, src: upgrade after v8 api change
bnoordhuis authored
152 if (r) SetErrno(uv_last_error(uv_default_loop()));
153 args.GetReturnValue().Set(r);
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
154 }
155
156
110a9cd @bnoordhuis lib, src: upgrade after v8 api change
bnoordhuis authored
157 void TTYWrap::New(const FunctionCallbackInfo<Value>& args) {
f65e14e @trevnorris src: pass Isolate to all applicable api
trevnorris authored
158 HandleScope scope(node_isolate);
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
159
160 // This constructor should not be exposed to public javascript.
161 // Therefore we assert that we are not trying to call this as a
162 // normal function.
163 assert(args.IsConstructCall());
164
165 int fd = args[0]->Int32Value();
166 assert(fd >= 0);
167
168 TTYWrap* wrap = new TTYWrap(args.This(), fd, args[1]->IsTrue());
169 wrap->UpdateWriteQueueSize();
170 }
171
172
173 TTYWrap::TTYWrap(Handle<Object> object, int fd, bool readable)
4d68dae @bnoordhuis src: replace c-style casts with c++-style casts
bnoordhuis authored
174 : StreamWrap(object, reinterpret_cast<uv_stream_t*>(&handle_)) {
af98fc9 @indutny child_process: new stdio API for .spawn() method
indutny authored
175 uv_tty_init(uv_default_loop(), &handle_, fd, readable);
176 }
c1ae6ea @ry Add TTYWrap
ry authored
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.