Skip to content
This repository
Newer
Older
100644 208 lines (152 sloc) 5.706 kb
60818b95 » ry
2011-11-02 Add missing copyright headers
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
ff4a9d38 » bnoordhuis
2012-03-10 core: use proper #include directives
22 #include "node.h"
23 #include "node_buffer.h"
24 #include "req_wrap.h"
25 #include "handle_wrap.h"
26 #include "stream_wrap.h"
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
27 #include "tty_wrap.h"
c1ae6ea2 » ry
2011-09-20 Add TTYWrap
28
29 namespace node {
30
7bdd05bd » bnoordhuis
2013-02-13 stream_wrap, udp_wrap: add read-only fd property
31 using v8::Arguments;
32 using v8::Context;
33 using v8::Function;
34 using v8::FunctionTemplate;
c1ae6ea2 » ry
2011-09-20 Add TTYWrap
35 using v8::Handle;
7bdd05bd » bnoordhuis
2013-02-13 stream_wrap, udp_wrap: add read-only fd property
36 using v8::HandleScope;
37 using v8::Integer;
c1ae6ea2 » ry
2011-09-20 Add TTYWrap
38 using v8::Local;
7bdd05bd » bnoordhuis
2013-02-13 stream_wrap, udp_wrap: add read-only fd property
39 using v8::Object;
c1ae6ea2 » ry
2011-09-20 Add TTYWrap
40 using v8::Persistent;
7bdd05bd » bnoordhuis
2013-02-13 stream_wrap, udp_wrap: add read-only fd property
41 using v8::PropertyAttribute;
c1ae6ea2 » ry
2011-09-20 Add TTYWrap
42 using v8::String;
43 using v8::TryCatch;
44 using v8::Undefined;
7bdd05bd » bnoordhuis
2013-02-13 stream_wrap, udp_wrap: add read-only fd property
45 using v8::Value;
c1ae6ea2 » ry
2011-09-20 Add TTYWrap
46
47
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
48 void TTYWrap::Initialize(Handle<Object> target) {
49 StreamWrap::Initialize(target);
c1ae6ea2 » ry
2011-09-20 Add TTYWrap
50
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
51 HandleScope scope;
c1ae6ea2 » ry
2011-09-20 Add TTYWrap
52
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
53 Local<FunctionTemplate> t = FunctionTemplate::New(New);
54 t->SetClassName(String::NewSymbol("TTY"));
c1ae6ea2 » ry
2011-09-20 Add TTYWrap
55
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
56 t->InstanceTemplate()->SetInternalFieldCount(1);
c1ae6ea2 » ry
2011-09-20 Add TTYWrap
57
7bdd05bd » bnoordhuis
2013-02-13 stream_wrap, udp_wrap: add read-only fd property
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
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
67 NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
68 NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);
0e57aafb » piscisaureus
2012-05-07 Optimize writing strings with Socket.write
69
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
70 NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
71 NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
c1ae6ea2 » ry
2011-09-20 Add TTYWrap
72
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
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);
74b6426e » ry
2011-09-20 Initial pass at new TTY js layer
77
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
78 NODE_SET_PROTOTYPE_METHOD(t, "getWindowSize", TTYWrap::GetWindowSize);
79 NODE_SET_PROTOTYPE_METHOD(t, "setRawMode", SetRawMode);
c1ae6ea2 » ry
2011-09-20 Add TTYWrap
80
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
81 NODE_SET_METHOD(target, "isTTY", IsTTY);
82 NODE_SET_METHOD(target, "guessHandleType", GuessHandleType);
c1ae6ea2 » ry
2011-09-20 Add TTYWrap
83
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
84 target->Set(String::NewSymbol("TTY"), t->GetFunction());
85 }
1de156ab » ry
2011-09-22 Bind/use uv_guess_handle
86
87
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
88 TTYWrap* TTYWrap::Unwrap(Local<Object> obj) {
89 assert(!obj.IsEmpty());
90 assert(obj->InternalFieldCount() > 0);
51f6e6a9 » bnoordhuis
2013-02-25 src, test: downgrade to v8 3.14 api
91 return static_cast<TTYWrap*>(obj->GetPointerFromInternalField(0));
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
92 }
1de156ab » ry
2011-09-22 Bind/use uv_guess_handle
93
94
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
95 uv_tty_t* TTYWrap::UVHandle() {
96 return &handle_;
97 }
1de156ab » ry
2011-09-22 Bind/use uv_guess_handle
98
99
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
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);
c1ae6ea2 » ry
2011-09-20 Add TTYWrap
106
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
107 switch (t) {
e99dff46 » bnoordhuis
2013-03-14 deps: upgrade libuv to 7b66ea1
108 case UV_TCP:
109 return scope.Close(String::New("TCP"));
110
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
111 case UV_TTY:
112 return scope.Close(String::New("TTY"));
189dd8f8 » piscisaureus
2011-11-04 Fix line endings and trailing whitespace
113
e99dff46 » bnoordhuis
2013-03-14 deps: upgrade libuv to 7b66ea1
114 case UV_UDP:
115 return scope.Close(String::New("UDP"));
116
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
117 case UV_NAMED_PIPE:
118 return scope.Close(String::New("PIPE"));
74b6426e » ry
2011-09-20 Initial pass at new TTY js layer
119
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
120 case UV_FILE:
121 return scope.Close(String::New("FILE"));
74b6426e » ry
2011-09-20 Initial pass at new TTY js layer
122
3a6314db » bnoordhuis
2012-07-12 net: fix net.Server.listen({fd:x}) error reporting
123 case UV_UNKNOWN_HANDLE:
124 return scope.Close(String::New("UNKNOWN"));
125
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
126 default:
127 assert(0);
51f6e6a9 » bnoordhuis
2013-02-25 src, test: downgrade to v8 3.14 api
128 return v8::Undefined();
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
129 }
130 }
74b6426e » ry
2011-09-20 Initial pass at new TTY js layer
131
132
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
133 Handle<Value> TTYWrap::IsTTY(const Arguments& args) {
134 HandleScope scope;
135 int fd = args[0]->Int32Value();
136 assert(fd >= 0);
109f73b2 » bnoordhuis
2013-01-07 src: pass node_isolate to True() and False()
137
138 if (uv_guess_handle(fd) == UV_TTY) {
51f6e6a9 » bnoordhuis
2013-02-25 src, test: downgrade to v8 3.14 api
139 return v8::True();
109f73b2 » bnoordhuis
2013-01-07 src: pass node_isolate to True() and False()
140 }
141
51f6e6a9 » bnoordhuis
2013-02-25 src, test: downgrade to v8 3.14 api
142 return v8::False();
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
143 }
74b6426e » ry
2011-09-20 Initial pass at new TTY js layer
144
145
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
146 Handle<Value> TTYWrap::GetWindowSize(const Arguments& args) {
147 HandleScope scope;
74b6426e » ry
2011-09-20 Initial pass at new TTY js layer
148
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
149 UNWRAP(TTYWrap)
74b6426e » ry
2011-09-20 Initial pass at new TTY js layer
150
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
151 int width, height;
152 int r = uv_tty_get_winsize(&wrap->handle_, &width, &height);
74b6426e » ry
2011-09-20 Initial pass at new TTY js layer
153
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
154 if (r) {
155 SetErrno(uv_last_error(uv_default_loop()));
51f6e6a9 » bnoordhuis
2013-02-25 src, test: downgrade to v8 3.14 api
156 return v8::Undefined();
74b6426e » ry
2011-09-20 Initial pass at new TTY js layer
157 }
158
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
159 Local<v8::Array> a = v8::Array::New(2);
51f6e6a9 » bnoordhuis
2013-02-25 src, test: downgrade to v8 3.14 api
160 a->Set(0, Integer::New(width));
161 a->Set(1, Integer::New(height));
c1ae6ea2 » ry
2011-09-20 Add TTYWrap
162
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
163 return scope.Close(a);
164 }
c1ae6ea2 » ry
2011-09-20 Add TTYWrap
165
166
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
167 Handle<Value> TTYWrap::SetRawMode(const Arguments& args) {
168 HandleScope scope;
c1ae6ea2 » ry
2011-09-20 Add TTYWrap
169
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
170 UNWRAP(TTYWrap)
171
172 int r = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
c1ae6ea2 » ry
2011-09-20 Add TTYWrap
173
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
174 if (r) {
175 SetErrno(uv_last_error(uv_default_loop()));
c1ae6ea2 » ry
2011-09-20 Add TTYWrap
176 }
177
51f6e6a9 » bnoordhuis
2013-02-25 src, test: downgrade to v8 3.14 api
178 return scope.Close(Integer::New(r));
af98fc9d » indutny
2012-05-16 child_process: new stdio API for .spawn() method
179 }
180
181
182 Handle<Value> TTYWrap::New(const Arguments& args) {
183 HandleScope scope;
184
185 // This constructor should not be exposed to public javascript.
186 // Therefore we assert that we are not trying to call this as a
187 // normal function.
188 assert(args.IsConstructCall());
189
190 int fd = args[0]->Int32Value();
191 assert(fd >= 0);
192
193 TTYWrap* wrap = new TTYWrap(args.This(), fd, args[1]->IsTrue());
194 assert(wrap);
195 wrap->UpdateWriteQueueSize();
196
197 return scope.Close(args.This());
198 }
199
200
201 TTYWrap::TTYWrap(Handle<Object> object, int fd, bool readable)
202 : StreamWrap(object, (uv_stream_t*)&handle_) {
203 uv_tty_init(uv_default_loop(), &handle_, fd, readable);
204 }
c1ae6ea2 » ry
2011-09-20 Add TTYWrap
205
206 } // namespace node
207
cdcb1118 » bnoordhuis
2011-11-09 Remove stray NODE_MODULE() semi-colons.
208 NODE_MODULE(node_tty_wrap, node::TTYWrap::Initialize)
Something went wrong with that request. Please try again.