Skip to content

Commit

Permalink
Works!
Browse files Browse the repository at this point in the history
  • Loading branch information
aredridel committed Sep 12, 2010
1 parent 27ff552 commit 123555c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 39 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This is a trivial extension, mostly for learning.

Feel free to absorb as you see fit.
63 changes: 24 additions & 39 deletions ext/ioctl.cc
Original file line number Diff line number Diff line change
@@ -1,59 +1,44 @@
#include <v8.h>
#include <node.h>
#include <sys/ioctl.h>

using namespace v8;

static Persistent<String> WINSZ_SYMBOL;

class Ioctl : public Object {
public:
static void Initialize (Handle<Object> target) {
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(New);
static Handle<Value> getwinsz (const Arguments &args) {
if (args.Length() < 1) return Undefined();

t->Inherit(Object::constructor_template);
t->InstanceTemplate()->SetInternalFieldCount(1);

t->PrototypeTemplate()->SetAccessor(WINSZ_SYMBOL, WinSZGetter);

}

static Handle<Value> WinSZGetter (Local<String> property, const AccessorInfo& info) {
int fd = 1;
int fd = args[0]->ToInteger()->Value();
#ifdef TIOCGSIZE
struct ttysize win;
struct ttysize win;
#elif defined(TIOCGWINSZ)
struct winsize win;
struct winsize win;
#endif
HandleScope scope;
Local<Array> winsz = Array::New(4);
HandleScope scope;
Local<Array> winsz = Array::New(4);

#ifdef TIOCGSIZE
if (ioctl (fd, TIOCGSIZE, &win))
return scope.Close(Integer::New(0));
winsz->Set(Integer::New(win.ts_lines), 0);
winsz->Set(Integer::New(win.ts_cols), 1);
winsz->Set(Integer::New(win.ts_xxx), 2);
winsz->Set(Integer::New(win.ts_yyy), 3);
if (ioctl (fd, TIOCGSIZE, &win))
return scope.Close(Number::New(0));
winsz->Set(Number::New(0), Integer::New(win.ts_lines));
winsz->Set(Number::New(1), Integer::New(win.ts_cols));
winsz->Set(Number::New(2), Integer::New(win.ts_xxx));
winsz->Set(Number::New(3), Integer::New(win.ts_yyy));
#else
if (ioctl (fd, TIOCGWINSZ, &win))
return scope.Close(Integer::New(0));
winsz->Set(Integer::New(win.ws_row), 0);
winsz->Set(Integer::New(win.ws_col), 1);
winsz->Set(Integer::New(win.ws_xpixel), 2);
winsz->Set(Integer::New(win.ws_ypixel), 3);
if (ioctl (fd, TIOCGWINSZ, &win))
return scope.Close(Integer::New(0));
winsz->Set(Number::New(0), Integer::New(win.ws_row));
winsz->Set(Number::New(1), Integer::New(win.ws_col));
winsz->Set(Number::New(2), Integer::New(win.ws_xpixel));
winsz->Set(Number::New(3), Integer::New(win.ws_ypixel));
#endif

return scope.Close(winsz);
}


return scope.Close(winsz);
}

extern "C" void
init (Handle<Object> target)
{
HandleScope scope;
Ioctl.Initialize(target);
target->Set(String::New("hello"), String::New("World"));
init (Handle<Object> target) {
HandleScope scope;
NODE_SET_METHOD(target, "getwinsz", getwinsz);
}

0 comments on commit 123555c

Please sign in to comment.