GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tty_wrap.cc Lines: 61 61 100.0 %
Date: 2021-09-24 04:12:43 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
4852
void TTYWrap::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
45
4852
  registry->Register(New);
46
4852
  registry->Register(GetWindowSize);
47
4852
  registry->Register(SetRawMode);
48
4852
  registry->Register(IsTTY);
49
4852
}
50
51
4358
void TTYWrap::Initialize(Local<Object> target,
52
                         Local<Value> unused,
53
                         Local<Context> context,
54
                         void* priv) {
55
4358
  Environment* env = Environment::GetCurrent(context);
56
57
4358
  Local<String> ttyString = FIXED_ONE_BYTE_STRING(env->isolate(), "TTY");
58
59
4358
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
60
4358
  t->SetClassName(ttyString);
61
8716
  t->InstanceTemplate()->SetInternalFieldCount(StreamBase::kInternalFieldCount);
62
4358
  t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
63
64
4358
  env->SetProtoMethodNoSideEffect(t, "getWindowSize", TTYWrap::GetWindowSize);
65
4358
  env->SetProtoMethod(t, "setRawMode", SetRawMode);
66
67
4358
  env->SetMethodNoSideEffect(target, "isTTY", IsTTY);
68
69
  Local<Value> func;
70
13074
  if (t->GetFunction(env->context()).ToLocal(&func) &&
71

13074
      target->Set(env->context(), ttyString, func).IsJust()) {
72
4358
    env->set_tty_constructor_template(t);
73
  }
74
4358
}
75
76
77
311
void TTYWrap::IsTTY(const FunctionCallbackInfo<Value>& args) {
78
311
  Environment* env = Environment::GetCurrent(args);
79
  int fd;
80
622
  if (!args[0]->Int32Value(env->context()).To(&fd)) return;
81
311
  CHECK_GE(fd, 0);
82
311
  bool rc = uv_guess_handle(fd) == UV_TTY;
83
622
  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
4925
NODE_MODULE_CONTEXT_AWARE_INTERNAL(tty_wrap, node::TTYWrap::Initialize)
157
4852
NODE_MODULE_EXTERNAL_REFERENCE(tty_wrap,
158
                               node::TTYWrap::RegisterExternalReferences)