Permalink
Browse files

Merge pull request #6 from rlidwka/master

openpty()
  • Loading branch information...
2 parents 120ca9d + 07a7efc commit c5ddfcdb6ed5e7cbcf7080dee4357996bdfe1108 @chjj committed Mar 10, 2012
Showing with 90 additions and 0 deletions.
  1. +20 −0 lib/pty.js
  2. +41 −0 src/pty.cc
  3. +29 −0 test/openpty.js
View
20 lib/pty.js
@@ -97,6 +97,26 @@ Terminal.createTerminal = function(file, args, opt) {
return new Terminal(file, args, opt);
};
+Terminal.openpty = function(opt) {
+ if (!opt) opt = {};
+ var socket;
+ var cols = opt.cols || 80;
+ var rows = opt.rows || 30;
+ var ptys = pty.open(cols, rows);
+
+ socket = new net.Socket(ptys.fdm);
+ socket.setEncoding('utf8');
+ socket.resume();
+ ptys.master = socket;
+
+ socket = new net.Socket(ptys.fds);
+ socket.setEncoding('utf8');
+ socket.resume();
+ ptys.slave = socket;
+
+ return ptys;
+};
+
/**
* Events
*/
View
41 src/pty.cc
@@ -11,6 +11,7 @@
* man tty_ioctl
* man tcsetattr
* man forkpty
+ * man openpty
*/
#include <v8.h>
@@ -81,6 +82,45 @@ pty_getproc(int, char *);
extern "C" void
init(Handle<Object>);
+static Handle<Value>
+PtyOpen(const Arguments& args) {
+ HandleScope scope;
+ if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
+ return ThrowException(Exception::Error(
+ String::New("cols and rows must be numbers.")));
+ }
+
+ // size
+ struct winsize winp = {};
+ Local<Integer> cols = args[0]->ToInteger();
+ Local<Integer> rows = args[1]->ToInteger();
+ winp.ws_col = cols->Value();
+ winp.ws_row = rows->Value();
+
+ // open the pty
+ int master;
+ int slave;
+ char name[40];
+ pid_t pid = openpty(&master, &slave, name, NULL, &winp);
+ //login_tty(slave);
+
+ if (pty_nonblock(master) == -1) {
+ return ThrowException(Exception::Error(
+ String::New("Could not set master fd to nonblocking.")));
+ }
+ if (pty_nonblock(slave) == -1) {
+ return ThrowException(Exception::Error(
+ String::New("Could not set slave fd to nonblocking.")));
+ }
+
+ Local<Object> obj = Object::New();
+ obj->Set(String::New("fdm"), Number::New(master));
+ obj->Set(String::New("fds"), Number::New(slave));
+ obj->Set(String::New("ptyname"), String::New(name));
+
+ return scope.Close(obj);
+}
+
/**
* PtyFork
*/
@@ -400,6 +440,7 @@ pty_getproc(int fd, char *tty) {
extern "C" void
init(Handle<Object> target) {
HandleScope scope;
+ NODE_SET_METHOD(target, "open", PtyOpen);
NODE_SET_METHOD(target, "fork", PtyFork);
NODE_SET_METHOD(target, "resize", PtyResize);
NODE_SET_METHOD(target, "process", PtyGetProc);
View
29 test/openpty.js
@@ -0,0 +1,29 @@
+
+var assert = require('assert');
+var pty = require('../lib/pty.js');
+var tty = require('tty');
+var term = pty.openpty();
+
+assert(tty.isatty(term.master));
+assert(tty.isatty(term.slave));
+
+var slavebuf = '';
+term.slave.on('data', function(data) {
+ slavebuf += data;
+});
+
+var masterbuf = '';
+term.master.on('data', function(data) {
+ masterbuf += data;
+});
+
+setTimeout(function() {
+ assert.equal(masterbuf, "slave\r\nmaster\r\n");
+ assert.equal(slavebuf, "master\n");
+ term.slave.destroy();
+ term.master.destroy();
+}, 200);
+
+term.slave.write("slave\n");
+term.master.write("master\n");
+

0 comments on commit c5ddfcd

Please sign in to comment.