GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tty_wrap.cc Lines: 61 61 100.0 %
Date: 2022-05-22 04:15:48 Branches: 19 32 59.4 %

Line Branch Exec Source
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 "node_external_reference.h"
28
#include "stream_base-inl.h"
29
#include "stream_wrap.h"
30
#include "util-inl.h"
31
32
namespace node {
33
34
using v8::Array;
35
using v8::Context;
36
using v8::FunctionCallbackInfo;
37
using v8::FunctionTemplate;
38
using v8::Integer;
39
using v8::Local;
40
using v8::Object;
41
using v8::String;
42
using v8::Value;
43
44
5184
void TTYWrap::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
45
5184
  registry->Register(New);
46
5184
  registry->Register(GetWindowSize);
47
5184
  registry->Register(SetRawMode);
48
5184
  registry->Register(IsTTY);
49
5184
}
50
51
4736
void TTYWrap::Initialize(Local<Object> target,
52
                         Local<Value> unused,
53
                         Local<Context> context,
54
                         void* priv) {
55
4736
  Environment* env = Environment::GetCurrent(context);
56
57
4736
  Local<String> ttyString = FIXED_ONE_BYTE_STRING(env->isolate(), "TTY");
58
59
4736
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
60
4736
  t->SetClassName(ttyString);
61
9472
  t->InstanceTemplate()->SetInternalFieldCount(StreamBase::kInternalFieldCount);
62
4736
  t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
63
64
4736
  env->SetProtoMethodNoSideEffect(t, "getWindowSize", TTYWrap::GetWindowSize);
65
4736
  env->SetProtoMethod(t, "setRawMode", SetRawMode);
66
67
4736
  env->SetMethodNoSideEffect(target, "isTTY", IsTTY);
68
69
  Local<Value> func;
70
14208
  if (t->GetFunction(env->context()).ToLocal(&func) &&
71

14208
      target->Set(env->context(), ttyString, func).IsJust()) {
72
4736
    env->set_tty_constructor_template(t);
73
  }
74
4736
}
75
76
77
346
void TTYWrap::IsTTY(const FunctionCallbackInfo<Value>& args) {
78
346
  Environment* env = Environment::GetCurrent(args);
79
  int fd;
80
692
  if (!args[0]->Int32Value(env->context()).To(&fd)) return;
81
346
  CHECK_GE(fd, 0);
82
346
  bool rc = uv_guess_handle(fd) == UV_TTY;
83
692
  args.GetReturnValue().Set(rc);
84
}
85
86
87
32
void TTYWrap::GetWindowSize(const FunctionCallbackInfo<Value>& args) {
88
32
  Environment* env = Environment::GetCurrent(args);
89
90
  TTYWrap* wrap;
91
32
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
92
                          args.Holder(),
93
                          args.GetReturnValue().Set(UV_EBADF));
94
32
  CHECK(args[0]->IsArray());
95
96
  int width, height;
97
32
  int err = uv_tty_get_winsize(&wrap->handle_, &width, &height);
98
99
32
  if (err == 0) {
100
64
    Local<Array> a = args[0].As<Array>();
101
96
    a->Set(env->context(), 0, Integer::New(env->isolate(), width)).Check();
102
96
    a->Set(env->context(), 1, Integer::New(env->isolate(), height)).Check();
103
  }
104
105
64
  args.GetReturnValue().Set(err);
106
}
107
108
109
8
void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
110
  TTYWrap* wrap;
111
8
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
112
                          args.Holder(),
113
                          args.GetReturnValue().Set(UV_EBADF));
114
8
  int err = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
115
16
  args.GetReturnValue().Set(err);
116
}
117
118
119
48
void TTYWrap::New(const FunctionCallbackInfo<Value>& args) {
120
48
  Environment* env = Environment::GetCurrent(args);
121
122
  // This constructor should not be exposed to public javascript.
123
  // Therefore we assert that we are not trying to call this as a
124
  // normal function.
125
48
  CHECK(args.IsConstructCall());
126
127
  int fd;
128
96
  if (!args[0]->Int32Value(env->context()).To(&fd)) return;
129
48
  CHECK_GE(fd, 0);
130
131
48
  int err = 0;
132
48
  new TTYWrap(env, args.This(), fd, &err);
133
48
  if (err != 0) {
134
4
    env->CollectUVExceptionInfo(args[1], err, "uv_tty_init");
135
4
    args.GetReturnValue().SetUndefined();
136
  }
137
}
138
139
140
48
TTYWrap::TTYWrap(Environment* env,
141
                 Local<Object> object,
142
                 int fd,
143
48
                 int* init_err)
144
    : LibuvStreamWrap(env,
145
                      object,
146
48
                      reinterpret_cast<uv_stream_t*>(&handle_),
147
48
                      AsyncWrap::PROVIDER_TTYWRAP) {
148
48
  *init_err = uv_tty_init(env->event_loop(), &handle_, fd, 0);
149
48
  set_fd(fd);
150
48
  if (*init_err != 0)
151
2
    MarkAsUninitialized();
152
48
}
153
154
}  // namespace node
155
156
5252
NODE_MODULE_CONTEXT_AWARE_INTERNAL(tty_wrap, node::TTYWrap::Initialize)
157
5184
NODE_MODULE_EXTERNAL_REFERENCE(tty_wrap,
158
                               node::TTYWrap::RegisterExternalReferences)