11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci#include "tty_wrap.h"
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci#include "env-inl.h"
251cb0ef41Sopenharmony_ci#include "handle_wrap.h"
261cb0ef41Sopenharmony_ci#include "node_buffer.h"
271cb0ef41Sopenharmony_ci#include "node_external_reference.h"
281cb0ef41Sopenharmony_ci#include "stream_base-inl.h"
291cb0ef41Sopenharmony_ci#include "stream_wrap.h"
301cb0ef41Sopenharmony_ci#include "util-inl.h"
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_cinamespace node {
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciusing v8::Array;
351cb0ef41Sopenharmony_ciusing v8::Context;
361cb0ef41Sopenharmony_ciusing v8::FunctionCallbackInfo;
371cb0ef41Sopenharmony_ciusing v8::FunctionTemplate;
381cb0ef41Sopenharmony_ciusing v8::Integer;
391cb0ef41Sopenharmony_ciusing v8::Isolate;
401cb0ef41Sopenharmony_ciusing v8::Local;
411cb0ef41Sopenharmony_ciusing v8::Object;
421cb0ef41Sopenharmony_ciusing v8::String;
431cb0ef41Sopenharmony_ciusing v8::Value;
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_civoid TTYWrap::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
461cb0ef41Sopenharmony_ci  registry->Register(New);
471cb0ef41Sopenharmony_ci  registry->Register(GetWindowSize);
481cb0ef41Sopenharmony_ci  registry->Register(SetRawMode);
491cb0ef41Sopenharmony_ci  registry->Register(IsTTY);
501cb0ef41Sopenharmony_ci}
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_civoid TTYWrap::Initialize(Local<Object> target,
531cb0ef41Sopenharmony_ci                         Local<Value> unused,
541cb0ef41Sopenharmony_ci                         Local<Context> context,
551cb0ef41Sopenharmony_ci                         void* priv) {
561cb0ef41Sopenharmony_ci  Environment* env = Environment::GetCurrent(context);
571cb0ef41Sopenharmony_ci  Isolate* isolate = env->isolate();
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  Local<String> ttyString = FIXED_ONE_BYTE_STRING(env->isolate(), "TTY");
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  Local<FunctionTemplate> t = NewFunctionTemplate(isolate, New);
621cb0ef41Sopenharmony_ci  t->SetClassName(ttyString);
631cb0ef41Sopenharmony_ci  t->InstanceTemplate()->SetInternalFieldCount(StreamBase::kInternalFieldCount);
641cb0ef41Sopenharmony_ci  t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  SetProtoMethodNoSideEffect(
671cb0ef41Sopenharmony_ci      isolate, t, "getWindowSize", TTYWrap::GetWindowSize);
681cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, t, "setRawMode", SetRawMode);
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  SetMethodNoSideEffect(context, target, "isTTY", IsTTY);
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  Local<Value> func;
731cb0ef41Sopenharmony_ci  if (t->GetFunction(context).ToLocal(&func) &&
741cb0ef41Sopenharmony_ci      target->Set(context, ttyString, func).IsJust()) {
751cb0ef41Sopenharmony_ci    env->set_tty_constructor_template(t);
761cb0ef41Sopenharmony_ci  }
771cb0ef41Sopenharmony_ci}
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_civoid TTYWrap::IsTTY(const FunctionCallbackInfo<Value>& args) {
811cb0ef41Sopenharmony_ci  Environment* env = Environment::GetCurrent(args);
821cb0ef41Sopenharmony_ci  int fd;
831cb0ef41Sopenharmony_ci  if (!args[0]->Int32Value(env->context()).To(&fd)) return;
841cb0ef41Sopenharmony_ci  CHECK_GE(fd, 0);
851cb0ef41Sopenharmony_ci  bool rc = uv_guess_handle(fd) == UV_TTY;
861cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(rc);
871cb0ef41Sopenharmony_ci}
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_civoid TTYWrap::GetWindowSize(const FunctionCallbackInfo<Value>& args) {
911cb0ef41Sopenharmony_ci  Environment* env = Environment::GetCurrent(args);
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci  TTYWrap* wrap;
941cb0ef41Sopenharmony_ci  ASSIGN_OR_RETURN_UNWRAP(&wrap,
951cb0ef41Sopenharmony_ci                          args.Holder(),
961cb0ef41Sopenharmony_ci                          args.GetReturnValue().Set(UV_EBADF));
971cb0ef41Sopenharmony_ci  CHECK(args[0]->IsArray());
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci  int width, height;
1001cb0ef41Sopenharmony_ci  int err = uv_tty_get_winsize(&wrap->handle_, &width, &height);
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci  if (err == 0) {
1031cb0ef41Sopenharmony_ci    Local<Array> a = args[0].As<Array>();
1041cb0ef41Sopenharmony_ci    a->Set(env->context(), 0, Integer::New(env->isolate(), width)).Check();
1051cb0ef41Sopenharmony_ci    a->Set(env->context(), 1, Integer::New(env->isolate(), height)).Check();
1061cb0ef41Sopenharmony_ci  }
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
1091cb0ef41Sopenharmony_ci}
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_civoid TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
1131cb0ef41Sopenharmony_ci  TTYWrap* wrap;
1141cb0ef41Sopenharmony_ci  ASSIGN_OR_RETURN_UNWRAP(&wrap,
1151cb0ef41Sopenharmony_ci                          args.Holder(),
1161cb0ef41Sopenharmony_ci                          args.GetReturnValue().Set(UV_EBADF));
1171cb0ef41Sopenharmony_ci  int err = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
1181cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
1191cb0ef41Sopenharmony_ci}
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_civoid TTYWrap::New(const FunctionCallbackInfo<Value>& args) {
1231cb0ef41Sopenharmony_ci  Environment* env = Environment::GetCurrent(args);
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci  // This constructor should not be exposed to public javascript.
1261cb0ef41Sopenharmony_ci  // Therefore we assert that we are not trying to call this as a
1271cb0ef41Sopenharmony_ci  // normal function.
1281cb0ef41Sopenharmony_ci  CHECK(args.IsConstructCall());
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci  int fd;
1311cb0ef41Sopenharmony_ci  if (!args[0]->Int32Value(env->context()).To(&fd)) return;
1321cb0ef41Sopenharmony_ci  CHECK_GE(fd, 0);
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ci  int err = 0;
1351cb0ef41Sopenharmony_ci  new TTYWrap(env, args.This(), fd, &err);
1361cb0ef41Sopenharmony_ci  if (err != 0) {
1371cb0ef41Sopenharmony_ci    env->CollectUVExceptionInfo(args[1], err, "uv_tty_init");
1381cb0ef41Sopenharmony_ci    args.GetReturnValue().SetUndefined();
1391cb0ef41Sopenharmony_ci  }
1401cb0ef41Sopenharmony_ci}
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ciTTYWrap::TTYWrap(Environment* env,
1441cb0ef41Sopenharmony_ci                 Local<Object> object,
1451cb0ef41Sopenharmony_ci                 int fd,
1461cb0ef41Sopenharmony_ci                 int* init_err)
1471cb0ef41Sopenharmony_ci    : LibuvStreamWrap(env,
1481cb0ef41Sopenharmony_ci                      object,
1491cb0ef41Sopenharmony_ci                      reinterpret_cast<uv_stream_t*>(&handle_),
1501cb0ef41Sopenharmony_ci                      AsyncWrap::PROVIDER_TTYWRAP) {
1511cb0ef41Sopenharmony_ci  *init_err = uv_tty_init(env->event_loop(), &handle_, fd, 0);
1521cb0ef41Sopenharmony_ci  set_fd(fd);
1531cb0ef41Sopenharmony_ci  if (*init_err != 0)
1541cb0ef41Sopenharmony_ci    MarkAsUninitialized();
1551cb0ef41Sopenharmony_ci}
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci}  // namespace node
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ciNODE_BINDING_CONTEXT_AWARE_INTERNAL(tty_wrap, node::TTYWrap::Initialize)
1601cb0ef41Sopenharmony_ciNODE_BINDING_EXTERNAL_REFERENCE(tty_wrap,
1611cb0ef41Sopenharmony_ci                                node::TTYWrap::RegisterExternalReferences)
162