Merge remote-tracking branch 'ry/v0.6' into v0.6-merge
[platform/upstream/nodejs.git] / src / tty_wrap.cc
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 "node.h"
23 #include "node_buffer.h"
24 #include "req_wrap.h"
25 #include "handle_wrap.h"
26 #include "stream_wrap.h"
27
28 namespace node {
29
30 using v8::Object;
31 using v8::Handle;
32 using v8::Local;
33 using v8::Persistent;
34 using v8::Value;
35 using v8::HandleScope;
36 using v8::FunctionTemplate;
37 using v8::String;
38 using v8::Function;
39 using v8::TryCatch;
40 using v8::Context;
41 using v8::Arguments;
42 using v8::Integer;
43 using v8::Undefined;
44
45 #define UNWRAP \
46   assert(!args.Holder().IsEmpty()); \
47   assert(args.Holder()->InternalFieldCount() > 0); \
48   TTYWrap* wrap =  \
49       static_cast<TTYWrap*>(args.Holder()->GetPointerFromInternalField(0)); \
50   if (!wrap) { \
51     uv_err_t err; \
52     err.code = UV_EBADF; \
53     SetErrno(err); \
54     return scope.Close(Integer::New(-1)); \
55   }
56
57
58 class TTYWrap : StreamWrap {
59  public:
60   static void Initialize(Handle<Object> target) {
61     StreamWrap::Initialize(target);
62
63     HandleScope scope;
64
65     Local<FunctionTemplate> t = FunctionTemplate::New(New);
66     t->SetClassName(String::NewSymbol("TTY"));
67
68     t->InstanceTemplate()->SetInternalFieldCount(1);
69
70     NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
71     NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);
72     NODE_SET_PROTOTYPE_METHOD(t, "ref", HandleWrap::Ref);
73
74     NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
75     NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
76
77     NODE_SET_PROTOTYPE_METHOD(t, "writeBuffer", StreamWrap::WriteBuffer);
78     NODE_SET_PROTOTYPE_METHOD(t, "writeAsciiString", StreamWrap::WriteAsciiString);
79     NODE_SET_PROTOTYPE_METHOD(t, "writeUtf8String", StreamWrap::WriteUtf8String);
80     NODE_SET_PROTOTYPE_METHOD(t, "writeUcs2String", StreamWrap::WriteUcs2String);
81
82     NODE_SET_PROTOTYPE_METHOD(t, "getWindowSize", TTYWrap::GetWindowSize);
83     NODE_SET_PROTOTYPE_METHOD(t, "setRawMode", SetRawMode);
84
85     NODE_SET_METHOD(target, "isTTY", IsTTY);
86     NODE_SET_METHOD(target, "guessHandleType", GuessHandleType);
87
88     target->Set(String::NewSymbol("TTY"), t->GetFunction());
89   }
90
91  private:
92   static Handle<Value> GuessHandleType(const Arguments& args) {
93     HandleScope scope;
94     int fd = args[0]->Int32Value();
95     assert(fd >= 0);
96
97     uv_handle_type t = uv_guess_handle(fd);
98
99     switch (t) {
100       case UV_TTY:
101         return scope.Close(String::New("TTY"));
102
103       case UV_NAMED_PIPE:
104         return scope.Close(String::New("PIPE"));
105
106       case UV_FILE:
107         return scope.Close(String::New("FILE"));
108
109       default:
110         assert(0);
111         return v8::Undefined();
112     }
113   }
114
115   static Handle<Value> IsTTY(const Arguments& args) {
116     HandleScope scope;
117     int fd = args[0]->Int32Value();
118     assert(fd >= 0);
119     return uv_guess_handle(fd) == UV_TTY ? v8::True() : v8::False();
120   }
121
122   static Handle<Value> GetWindowSize(const Arguments& args) {
123     HandleScope scope;
124
125     UNWRAP
126
127     int width, height;
128     int r = uv_tty_get_winsize(&wrap->handle_, &width, &height);
129
130     if (r) {
131       SetErrno(uv_last_error(uv_default_loop()));
132       return v8::Undefined();
133     }
134
135     Local<v8::Array> a = v8::Array::New(2);
136     a->Set(0, Integer::New(width));
137     a->Set(1, Integer::New(height));
138
139     return scope.Close(a);
140   }
141
142   static Handle<Value> SetRawMode(const Arguments& args) {
143     HandleScope scope;
144
145     UNWRAP
146
147     int r = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
148
149     if (r) {
150       SetErrno(uv_last_error(uv_default_loop()));
151     }
152
153     return scope.Close(Integer::New(r));
154   }
155
156   static Handle<Value> New(const Arguments& args) {
157     HandleScope scope;
158
159     // This constructor should not be exposed to public javascript.
160     // Therefore we assert that we are not trying to call this as a
161     // normal function.
162     assert(args.IsConstructCall());
163
164     int fd = args[0]->Int32Value();
165     assert(fd >= 0);
166
167     TTYWrap* wrap = new TTYWrap(args.This(), fd, args[1]->IsTrue());
168     assert(wrap);
169     wrap->UpdateWriteQueueSize();
170
171     return scope.Close(args.This());
172   }
173
174   TTYWrap(Handle<Object> object, int fd, bool readable)
175       : StreamWrap(object, (uv_stream_t*)&handle_) {
176     uv_tty_init(uv_default_loop(), &handle_, fd, readable);
177   }
178
179   uv_tty_t handle_;
180 };
181
182 }  // namespace node
183
184 NODE_MODULE(node_tty_wrap, node::TTYWrap::Initialize)