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
22 #include "tty_wrap.h"
23
24 #include "env-inl.h"
25 #include "handle_wrap.h"
26 #include "node_buffer.h"
27 #include "stream_base-inl.h"
28 #include "stream_wrap.h"
29 #include "util-inl.h"
30
31 namespace node {
32
33 using v8::Array;
34 using v8::Context;
35 using v8::FunctionCallbackInfo;
36 using v8::FunctionTemplate;
37 using v8::Integer;
38 using v8::Local;
39 using v8::Object;
40 using v8::String;
41 using v8::Value;
42
Initialize(Local<Object> target,Local<Value> unused,Local<Context> context,void * priv)43 void TTYWrap::Initialize(Local<Object> target,
44 Local<Value> unused,
45 Local<Context> context,
46 void* priv) {
47 Environment* env = Environment::GetCurrent(context);
48
49 Local<String> ttyString = FIXED_ONE_BYTE_STRING(env->isolate(), "TTY");
50
51 Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
52 t->SetClassName(ttyString);
53 t->InstanceTemplate()->SetInternalFieldCount(StreamBase::kInternalFieldCount);
54 t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
55
56 env->SetProtoMethodNoSideEffect(t, "getWindowSize", TTYWrap::GetWindowSize);
57 env->SetProtoMethod(t, "setRawMode", SetRawMode);
58
59 env->SetMethodNoSideEffect(target, "isTTY", IsTTY);
60
61 Local<Value> func;
62 if (t->GetFunction(env->context()).ToLocal(&func) &&
63 target->Set(env->context(), ttyString, func).IsJust()) {
64 env->set_tty_constructor_template(t);
65 }
66 }
67
68
IsTTY(const FunctionCallbackInfo<Value> & args)69 void TTYWrap::IsTTY(const FunctionCallbackInfo<Value>& args) {
70 Environment* env = Environment::GetCurrent(args);
71 int fd;
72 if (!args[0]->Int32Value(env->context()).To(&fd)) return;
73 CHECK_GE(fd, 0);
74 bool rc = uv_guess_handle(fd) == UV_TTY;
75 args.GetReturnValue().Set(rc);
76 }
77
78
GetWindowSize(const FunctionCallbackInfo<Value> & args)79 void TTYWrap::GetWindowSize(const FunctionCallbackInfo<Value>& args) {
80 Environment* env = Environment::GetCurrent(args);
81
82 TTYWrap* wrap;
83 ASSIGN_OR_RETURN_UNWRAP(&wrap,
84 args.Holder(),
85 args.GetReturnValue().Set(UV_EBADF));
86 CHECK(args[0]->IsArray());
87
88 int width, height;
89 int err = uv_tty_get_winsize(&wrap->handle_, &width, &height);
90
91 if (err == 0) {
92 Local<Array> a = args[0].As<Array>();
93 a->Set(env->context(), 0, Integer::New(env->isolate(), width)).Check();
94 a->Set(env->context(), 1, Integer::New(env->isolate(), height)).Check();
95 }
96
97 args.GetReturnValue().Set(err);
98 }
99
100
SetRawMode(const FunctionCallbackInfo<Value> & args)101 void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
102 TTYWrap* wrap;
103 ASSIGN_OR_RETURN_UNWRAP(&wrap,
104 args.Holder(),
105 args.GetReturnValue().Set(UV_EBADF));
106 int err = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
107 args.GetReturnValue().Set(err);
108 }
109
110
New(const FunctionCallbackInfo<Value> & args)111 void TTYWrap::New(const FunctionCallbackInfo<Value>& args) {
112 Environment* env = Environment::GetCurrent(args);
113
114 // This constructor should not be exposed to public javascript.
115 // Therefore we assert that we are not trying to call this as a
116 // normal function.
117 CHECK(args.IsConstructCall());
118
119 int fd;
120 if (!args[0]->Int32Value(env->context()).To(&fd)) return;
121 CHECK_GE(fd, 0);
122
123 int err = 0;
124 new TTYWrap(env, args.This(), fd, args[1]->IsTrue(), &err);
125 if (err != 0) {
126 env->CollectUVExceptionInfo(args[2], err, "uv_tty_init");
127 args.GetReturnValue().SetUndefined();
128 }
129 }
130
131
TTYWrap(Environment * env,Local<Object> object,int fd,bool readable,int * init_err)132 TTYWrap::TTYWrap(Environment* env,
133 Local<Object> object,
134 int fd,
135 bool readable,
136 int* init_err)
137 : LibuvStreamWrap(env,
138 object,
139 reinterpret_cast<uv_stream_t*>(&handle_),
140 AsyncWrap::PROVIDER_TTYWRAP) {
141 *init_err = uv_tty_init(env->event_loop(), &handle_, fd, readable);
142 set_fd(fd);
143 if (*init_err != 0)
144 MarkAsUninitialized();
145 }
146
147 } // namespace node
148
149 NODE_MODULE_CONTEXT_AWARE_INTERNAL(tty_wrap, node::TTYWrap::Initialize)
150