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

13008
      target->Set(env->context(), ttyString, func).IsJust()) {
64
4336
    env->set_tty_constructor_template(t);
65
  }
66
4336
}
67
68
69
311
void TTYWrap::IsTTY(const FunctionCallbackInfo<Value>& args) {
70
311
  Environment* env = Environment::GetCurrent(args);
71
  int fd;
72
622
  if (!args[0]->Int32Value(env->context()).To(&fd)) return;
73
311
  CHECK_GE(fd, 0);
74
311
  bool rc = uv_guess_handle(fd) == UV_TTY;
75
622
  args.GetReturnValue().Set(rc);
76
}
77
78
79
32
void TTYWrap::GetWindowSize(const FunctionCallbackInfo<Value>& args) {
80
32
  Environment* env = Environment::GetCurrent(args);
81
82
  TTYWrap* wrap;
83
32
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
84
                          args.Holder(),
85
                          args.GetReturnValue().Set(UV_EBADF));
86
32
  CHECK(args[0]->IsArray());
87
88
  int width, height;
89
32
  int err = uv_tty_get_winsize(&wrap->handle_, &width, &height);
90
91
32
  if (err == 0) {
92
64
    Local<Array> a = args[0].As<Array>();
93
96
    a->Set(env->context(), 0, Integer::New(env->isolate(), width)).Check();
94
96
    a->Set(env->context(), 1, Integer::New(env->isolate(), height)).Check();
95
  }
96
97
64
  args.GetReturnValue().Set(err);
98
}
99
100
101
8
void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
102
  TTYWrap* wrap;
103
8
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
104
                          args.Holder(),
105
                          args.GetReturnValue().Set(UV_EBADF));
106
8
  int err = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
107
16
  args.GetReturnValue().Set(err);
108
}
109
110
111
48
void TTYWrap::New(const FunctionCallbackInfo<Value>& args) {
112
48
  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
48
  CHECK(args.IsConstructCall());
118
119
  int fd;
120
96
  if (!args[0]->Int32Value(env->context()).To(&fd)) return;
121
48
  CHECK_GE(fd, 0);
122
123
48
  int err = 0;
124
48
  new TTYWrap(env, args.This(), fd, &err);
125
48
  if (err != 0) {
126
4
    env->CollectUVExceptionInfo(args[1], err, "uv_tty_init");
127
4
    args.GetReturnValue().SetUndefined();
128
  }
129
}
130
131
132
48
TTYWrap::TTYWrap(Environment* env,
133
                 Local<Object> object,
134
                 int fd,
135
48
                 int* init_err)
136
    : LibuvStreamWrap(env,
137
                      object,
138
48
                      reinterpret_cast<uv_stream_t*>(&handle_),
139
48
                      AsyncWrap::PROVIDER_TTYWRAP) {
140
48
  *init_err = uv_tty_init(env->event_loop(), &handle_, fd, 0);
141
48
  set_fd(fd);
142
48
  if (*init_err != 0)
143
2
    MarkAsUninitialized();
144
48
}
145
146
}  // namespace node
147
148
4906
NODE_MODULE_CONTEXT_AWARE_INTERNAL(tty_wrap, node::TTYWrap::Initialize)