Skip to content

Commit

Permalink
Merge pull request chjj#57 from KiNgMaR/utf8_toggle
Browse files Browse the repository at this point in the history
Allow retrieval of non-UTF8 data from terminal
  • Loading branch information
Tyriar authored Mar 24, 2017
2 parents 87b6938 + 57f4866 commit d15a483
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ export interface IPtyForkOptions {
env?: ProcessEnv;
uid?: number;
gid?: number;
encoding?: string;
}

export interface IPtyOpenOptions {
cols?: number;
rows?: number;
encoding?: string;
}
18 changes: 11 additions & 7 deletions src/unix/pty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,13 @@ pty_after_close(uv_handle_t *);

/**
* PtyFork
* pty.fork(file, args, env, cwd, cols, rows, uid, gid, onexit)
* pty.fork(file, args, env, cwd, cols, rows, uid, gid, utf8, onexit)
*/

NAN_METHOD(PtyFork) {
Nan::HandleScope scope;

if (info.Length() != 9
if (info.Length() != 10
|| !info[0]->IsString() // file
|| !info[1]->IsArray() // args
|| !info[2]->IsArray() // env
Expand All @@ -154,10 +154,11 @@ NAN_METHOD(PtyFork) {
|| !info[5]->IsNumber() // rows
|| !info[6]->IsNumber() // uid
|| !info[7]->IsNumber() // gid
|| !info[8]->IsFunction() // onexit
|| !info[8]->IsBoolean() // utf8
|| !info[9]->IsFunction() // onexit
) {
return Nan::ThrowError(
"Usage: pty.fork(file, args, env, cwd, cols, rows, uid, gid, onexit)");
"Usage: pty.fork(file, args, env, cwd, cols, rows, uid, gid, utf8, onexit)");
}

// Make sure the process still listens to SIGINT
Expand Down Expand Up @@ -204,11 +205,14 @@ NAN_METHOD(PtyFork) {
// termios
struct termios t = termios();
struct termios *term = &t;
term->c_iflag = ICRNL | IXON | IXANY | IMAXBEL | BRKINT;
if (info[8]->ToBoolean()->Value()) {
#if defined(IUTF8)
term->c_iflag = ICRNL | IXON | IXANY | IMAXBEL | BRKINT | IUTF8;
term->c_iflag |= IUTF8;
#else
term->c_iflag = ICRNL | IXON | IXANY | IMAXBEL | BRKINT | UTF8;
term->c_iflag |= UTF8;
#endif
}
term->c_oflag = OPOST | ONLCR;
term->c_cflag = CREAD | CS8 | HUPCL;
term->c_lflag = ICANON | ISIG | IEXTEN | ECHO | ECHOE | ECHOK | ECHOKE | ECHOCTL;
Expand Down Expand Up @@ -299,7 +303,7 @@ NAN_METHOD(PtyFork) {
pty_baton *baton = new pty_baton();
baton->exit_code = 0;
baton->signal_code = 0;
baton->cb.Reset(Local<Function>::Cast(info[8]));
baton->cb.Reset(Local<Function>::Cast(info[9]));
baton->pid = pid;
baton->async.data = baton;

Expand Down
17 changes: 13 additions & 4 deletions src/unixTerminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export class UnixTerminal extends Terminal {
env.TERM = name;
const parsedEnv = this._parseEnv(env);

const encoding = (opt.encoding === undefined ? 'utf8' : opt.encoding);

const onexit = (code: any, signal: any) => {
// XXX Sometimes a data event is emitted after exit. Wait til socket is
// destroyed.
Expand All @@ -73,10 +75,12 @@ export class UnixTerminal extends Terminal {
};

// fork
const term = pty.fork(file, args, parsedEnv, cwd, cols, rows, uid, gid, onexit);
const term = pty.fork(file, args, parsedEnv, cwd, cols, rows, uid, gid, (encoding === 'utf8'), onexit);

this.socket = new PipeSocket(term.fd);
this.socket.setEncoding('utf8');
if (encoding !== null) {
this.socket.setEncoding(encoding);
}
this.socket.resume();

// setup
Expand Down Expand Up @@ -149,16 +153,21 @@ export class UnixTerminal extends Terminal {

const cols = opt.cols || Terminal.DEFAULT_COLS;
const rows = opt.rows || Terminal.DEFAULT_ROWS;
const encoding = (opt.encoding === undefined ? 'utf8' : opt.encoding);

// open
const term = pty.open(cols, rows);

self.master = new PipeSocket(term.master);
self.master.setEncoding('utf8');
if (encoding !== null) {
self.master.setEncoding(encoding);
}
self.master.resume();

self.slave = new PipeSocket(term.slave);
self.slave.setEncoding('utf8');
if (encoding !== null) {
self.slave.setEncoding(encoding);
}
self.slave.resume();

self.socket = self.master;
Expand Down

0 comments on commit d15a483

Please sign in to comment.