mirror of
https://github.com/lihop/godot-xterm.git
synced 2024-11-22 09:40:25 +01:00
Clang-format node-pty files
This commit is contained in:
parent
9c20579bc6
commit
4e6715329a
6 changed files with 364 additions and 381 deletions
|
@ -17,18 +17,18 @@
|
||||||
* Includes
|
* Includes
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <nan.h>
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <nan.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sys/ioctl.h>
|
|
||||||
#include <sys/wait.h>
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
|
||||||
/* forkpty */
|
/* forkpty */
|
||||||
/* http://www.gnu.org/software/gnulib/manual/html_node/forkpty.html */
|
/* http://www.gnu.org/software/gnulib/manual/html_node/forkpty.html */
|
||||||
|
@ -48,10 +48,10 @@
|
||||||
|
|
||||||
/* Some platforms name VWERASE and VDISCARD differently */
|
/* Some platforms name VWERASE and VDISCARD differently */
|
||||||
#if !defined(VWERASE) && defined(VWERSE)
|
#if !defined(VWERASE) && defined(VWERSE)
|
||||||
#define VWERASE VWERSE
|
#define VWERASE VWERSE
|
||||||
#endif
|
#endif
|
||||||
#if !defined(VDISCARD) && defined(VDISCRD)
|
#if !defined(VDISCARD) && defined(VDISCRD)
|
||||||
#define VDISCARD VDISCRD
|
#define VDISCARD VDISCRD
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* environ for execvpe */
|
/* environ for execvpe */
|
||||||
|
@ -65,11 +65,11 @@ extern char **environ;
|
||||||
|
|
||||||
/* for pty_getproc */
|
/* for pty_getproc */
|
||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
#elif defined(__APPLE__)
|
#elif defined(__APPLE__)
|
||||||
#include <sys/sysctl.h>
|
|
||||||
#include <libproc.h>
|
#include <libproc.h>
|
||||||
|
#include <sys/sysctl.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* NSIG - macro for highest signal + 1, should be defined */
|
/* NSIG - macro for highest signal + 1, should be defined */
|
||||||
|
@ -103,50 +103,33 @@ NAN_METHOD(PtyGetProc);
|
||||||
* Functions
|
* Functions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static int
|
static int pty_execvpe(const char *, char **, char **);
|
||||||
pty_execvpe(const char *, char **, char **);
|
|
||||||
|
|
||||||
static int
|
static int pty_nonblock(int);
|
||||||
pty_nonblock(int);
|
|
||||||
|
|
||||||
static char *
|
static char *pty_getproc(int, char *);
|
||||||
pty_getproc(int, char *);
|
|
||||||
|
|
||||||
static int
|
static int pty_openpty(int *, int *, char *, const struct termios *,
|
||||||
pty_openpty(int *, int *, char *,
|
const struct winsize *);
|
||||||
const struct termios *,
|
|
||||||
const struct winsize *);
|
|
||||||
|
|
||||||
static pid_t
|
static pid_t pty_forkpty(int *, char *, const struct termios *,
|
||||||
pty_forkpty(int *, char *,
|
const struct winsize *);
|
||||||
const struct termios *,
|
|
||||||
const struct winsize *);
|
|
||||||
|
|
||||||
static void
|
static void pty_waitpid(void *);
|
||||||
pty_waitpid(void *);
|
|
||||||
|
|
||||||
static void
|
static void pty_after_waitpid(uv_async_t *);
|
||||||
pty_after_waitpid(uv_async_t *);
|
|
||||||
|
|
||||||
static void
|
static void pty_after_close(uv_handle_t *);
|
||||||
pty_after_close(uv_handle_t *);
|
|
||||||
|
|
||||||
NAN_METHOD(PtyFork) {
|
NAN_METHOD(PtyFork) {
|
||||||
Nan::HandleScope scope;
|
Nan::HandleScope scope;
|
||||||
|
|
||||||
if (info.Length() != 10 ||
|
if (info.Length() != 10 || !info[0]->IsString() || !info[1]->IsArray() ||
|
||||||
!info[0]->IsString() ||
|
!info[2]->IsArray() || !info[3]->IsString() || !info[4]->IsNumber() ||
|
||||||
!info[1]->IsArray() ||
|
!info[5]->IsNumber() || !info[6]->IsNumber() || !info[7]->IsNumber() ||
|
||||||
!info[2]->IsArray() ||
|
!info[8]->IsBoolean() || !info[9]->IsFunction()) {
|
||||||
!info[3]->IsString() ||
|
return Nan::ThrowError("Usage: pty.fork(file, args, env, cwd, cols, rows, "
|
||||||
!info[4]->IsNumber() ||
|
"uid, gid, utf8, onexit)");
|
||||||
!info[5]->IsNumber() ||
|
|
||||||
!info[6]->IsNumber() ||
|
|
||||||
!info[7]->IsNumber() ||
|
|
||||||
!info[8]->IsBoolean() ||
|
|
||||||
!info[9]->IsFunction()) {
|
|
||||||
return Nan::ThrowError(
|
|
||||||
"Usage: pty.fork(file, args, env, cwd, cols, rows, uid, gid, utf8, onexit)");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// file
|
// file
|
||||||
|
@ -157,19 +140,19 @@ NAN_METHOD(PtyFork) {
|
||||||
v8::Local<v8::Array> argv_ = v8::Local<v8::Array>::Cast(info[1]);
|
v8::Local<v8::Array> argv_ = v8::Local<v8::Array>::Cast(info[1]);
|
||||||
int argc = argv_->Length();
|
int argc = argv_->Length();
|
||||||
int argl = argc + 1 + 1;
|
int argl = argc + 1 + 1;
|
||||||
char **argv = new char*[argl];
|
char **argv = new char *[argl];
|
||||||
argv[0] = strdup(*file);
|
argv[0] = strdup(*file);
|
||||||
argv[argl-1] = NULL;
|
argv[argl - 1] = NULL;
|
||||||
for (; i < argc; i++) {
|
for (; i < argc; i++) {
|
||||||
Nan::Utf8String arg(Nan::Get(argv_, i).ToLocalChecked());
|
Nan::Utf8String arg(Nan::Get(argv_, i).ToLocalChecked());
|
||||||
argv[i+1] = strdup(*arg);
|
argv[i + 1] = strdup(*arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
// env
|
// env
|
||||||
i = 0;
|
i = 0;
|
||||||
v8::Local<v8::Array> env_ = v8::Local<v8::Array>::Cast(info[2]);
|
v8::Local<v8::Array> env_ = v8::Local<v8::Array>::Cast(info[2]);
|
||||||
int envc = env_->Length();
|
int envc = env_->Length();
|
||||||
char **env = new char*[envc+1];
|
char **env = new char *[envc + 1];
|
||||||
env[envc] = NULL;
|
env[envc] = NULL;
|
||||||
for (; i < envc; i++) {
|
for (; i < envc; i++) {
|
||||||
Nan::Utf8String pair(Nan::Get(env_, i).ToLocalChecked());
|
Nan::Utf8String pair(Nan::Get(env_, i).ToLocalChecked());
|
||||||
|
@ -198,7 +181,8 @@ NAN_METHOD(PtyFork) {
|
||||||
}
|
}
|
||||||
term->c_oflag = OPOST | ONLCR;
|
term->c_oflag = OPOST | ONLCR;
|
||||||
term->c_cflag = CREAD | CS8 | HUPCL;
|
term->c_cflag = CREAD | CS8 | HUPCL;
|
||||||
term->c_lflag = ICANON | ISIG | IEXTEN | ECHO | ECHOE | ECHOK | ECHOKE | ECHOCTL;
|
term->c_lflag =
|
||||||
|
ICANON | ISIG | IEXTEN | ECHO | ECHOE | ECHOK | ECHOKE | ECHOCTL;
|
||||||
|
|
||||||
term->c_cc[VEOF] = 4;
|
term->c_cc[VEOF] = 4;
|
||||||
term->c_cc[VEOL] = -1;
|
term->c_cc[VEOL] = -1;
|
||||||
|
@ -217,10 +201,10 @@ NAN_METHOD(PtyFork) {
|
||||||
term->c_cc[VMIN] = 1;
|
term->c_cc[VMIN] = 1;
|
||||||
term->c_cc[VTIME] = 0;
|
term->c_cc[VTIME] = 0;
|
||||||
|
|
||||||
#if (__APPLE__)
|
#if (__APPLE__)
|
||||||
term->c_cc[VDSUSP] = 25;
|
term->c_cc[VDSUSP] = 25;
|
||||||
term->c_cc[VSTATUS] = 20;
|
term->c_cc[VSTATUS] = 20;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
cfsetispeed(term, B38400);
|
cfsetispeed(term, B38400);
|
||||||
cfsetospeed(term, B38400);
|
cfsetospeed(term, B38400);
|
||||||
|
@ -249,7 +233,7 @@ NAN_METHOD(PtyFork) {
|
||||||
sig_action.sa_handler = SIG_DFL;
|
sig_action.sa_handler = SIG_DFL;
|
||||||
sig_action.sa_flags = 0;
|
sig_action.sa_flags = 0;
|
||||||
sigemptyset(&sig_action.sa_mask);
|
sigemptyset(&sig_action.sa_mask);
|
||||||
for (int i = 0 ; i < NSIG ; i++) { // NSIG is a macro for all signals + 1
|
for (int i = 0; i < NSIG; i++) { // NSIG is a macro for all signals + 1
|
||||||
sigaction(i, &sig_action, NULL);
|
sigaction(i, &sig_action, NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -257,67 +241,66 @@ NAN_METHOD(PtyFork) {
|
||||||
pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
|
pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
|
||||||
|
|
||||||
if (pid) {
|
if (pid) {
|
||||||
for (i = 0; i < argl; i++) free(argv[i]);
|
for (i = 0; i < argl; i++)
|
||||||
|
free(argv[i]);
|
||||||
delete[] argv;
|
delete[] argv;
|
||||||
for (i = 0; i < envc; i++) free(env[i]);
|
for (i = 0; i < envc; i++)
|
||||||
|
free(env[i]);
|
||||||
delete[] env;
|
delete[] env;
|
||||||
free(cwd);
|
free(cwd);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (pid) {
|
switch (pid) {
|
||||||
case -1:
|
case -1:
|
||||||
return Nan::ThrowError("forkpty(3) failed.");
|
return Nan::ThrowError("forkpty(3) failed.");
|
||||||
case 0:
|
case 0:
|
||||||
if (strlen(cwd)) {
|
if (strlen(cwd)) {
|
||||||
if (chdir(cwd) == -1) {
|
if (chdir(cwd) == -1) {
|
||||||
perror("chdir(2) failed.");
|
perror("chdir(2) failed.");
|
||||||
_exit(1);
|
_exit(1);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (uid != -1 && gid != -1) {
|
if (uid != -1 && gid != -1) {
|
||||||
if (setgid(gid) == -1) {
|
if (setgid(gid) == -1) {
|
||||||
perror("setgid(2) failed.");
|
perror("setgid(2) failed.");
|
||||||
_exit(1);
|
_exit(1);
|
||||||
}
|
|
||||||
if (setuid(uid) == -1) {
|
|
||||||
perror("setuid(2) failed.");
|
|
||||||
_exit(1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if (setuid(uid) == -1) {
|
||||||
pty_execvpe(argv[0], argv, env);
|
perror("setuid(2) failed.");
|
||||||
|
_exit(1);
|
||||||
perror("execvp(3) failed.");
|
|
||||||
_exit(1);
|
|
||||||
default:
|
|
||||||
if (pty_nonblock(master) == -1) {
|
|
||||||
return Nan::ThrowError("Could not set master fd to nonblocking.");
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
v8::Local<v8::Object> obj = Nan::New<v8::Object>();
|
pty_execvpe(argv[0], argv, env);
|
||||||
Nan::Set(obj,
|
|
||||||
Nan::New<v8::String>("fd").ToLocalChecked(),
|
|
||||||
Nan::New<v8::Number>(master));
|
|
||||||
Nan::Set(obj,
|
|
||||||
Nan::New<v8::String>("pid").ToLocalChecked(),
|
|
||||||
Nan::New<v8::Number>(pid));
|
|
||||||
Nan::Set(obj,
|
|
||||||
Nan::New<v8::String>("pty").ToLocalChecked(),
|
|
||||||
Nan::New<v8::String>(ptsname(master)).ToLocalChecked());
|
|
||||||
|
|
||||||
pty_baton *baton = new pty_baton();
|
perror("execvp(3) failed.");
|
||||||
baton->exit_code = 0;
|
_exit(1);
|
||||||
baton->signal_code = 0;
|
default:
|
||||||
baton->cb.Reset(v8::Local<v8::Function>::Cast(info[9]));
|
if (pty_nonblock(master) == -1) {
|
||||||
baton->pid = pid;
|
return Nan::ThrowError("Could not set master fd to nonblocking.");
|
||||||
baton->async.data = baton;
|
}
|
||||||
|
|
||||||
uv_async_init(uv_default_loop(), &baton->async, pty_after_waitpid);
|
v8::Local<v8::Object> obj = Nan::New<v8::Object>();
|
||||||
|
Nan::Set(obj, Nan::New<v8::String>("fd").ToLocalChecked(),
|
||||||
|
Nan::New<v8::Number>(master));
|
||||||
|
Nan::Set(obj, Nan::New<v8::String>("pid").ToLocalChecked(),
|
||||||
|
Nan::New<v8::Number>(pid));
|
||||||
|
Nan::Set(obj, Nan::New<v8::String>("pty").ToLocalChecked(),
|
||||||
|
Nan::New<v8::String>(ptsname(master)).ToLocalChecked());
|
||||||
|
|
||||||
uv_thread_create(&baton->tid, pty_waitpid, static_cast<void*>(baton));
|
pty_baton *baton = new pty_baton();
|
||||||
|
baton->exit_code = 0;
|
||||||
|
baton->signal_code = 0;
|
||||||
|
baton->cb.Reset(v8::Local<v8::Function>::Cast(info[9]));
|
||||||
|
baton->pid = pid;
|
||||||
|
baton->async.data = baton;
|
||||||
|
|
||||||
return info.GetReturnValue().Set(obj);
|
uv_async_init(uv_default_loop(), &baton->async, pty_after_waitpid);
|
||||||
|
|
||||||
|
uv_thread_create(&baton->tid, pty_waitpid, static_cast<void *>(baton));
|
||||||
|
|
||||||
|
return info.GetReturnValue().Set(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
return info.GetReturnValue().SetUndefined();
|
return info.GetReturnValue().SetUndefined();
|
||||||
|
@ -326,9 +309,7 @@ NAN_METHOD(PtyFork) {
|
||||||
NAN_METHOD(PtyOpen) {
|
NAN_METHOD(PtyOpen) {
|
||||||
Nan::HandleScope scope;
|
Nan::HandleScope scope;
|
||||||
|
|
||||||
if (info.Length() != 2 ||
|
if (info.Length() != 2 || !info[0]->IsNumber() || !info[1]->IsNumber()) {
|
||||||
!info[0]->IsNumber() ||
|
|
||||||
!info[1]->IsNumber()) {
|
|
||||||
return Nan::ThrowError("Usage: pty.open(cols, rows)");
|
return Nan::ThrowError("Usage: pty.open(cols, rows)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -356,15 +337,12 @@ NAN_METHOD(PtyOpen) {
|
||||||
}
|
}
|
||||||
|
|
||||||
v8::Local<v8::Object> obj = Nan::New<v8::Object>();
|
v8::Local<v8::Object> obj = Nan::New<v8::Object>();
|
||||||
Nan::Set(obj,
|
Nan::Set(obj, Nan::New<v8::String>("master").ToLocalChecked(),
|
||||||
Nan::New<v8::String>("master").ToLocalChecked(),
|
Nan::New<v8::Number>(master));
|
||||||
Nan::New<v8::Number>(master));
|
Nan::Set(obj, Nan::New<v8::String>("slave").ToLocalChecked(),
|
||||||
Nan::Set(obj,
|
Nan::New<v8::Number>(slave));
|
||||||
Nan::New<v8::String>("slave").ToLocalChecked(),
|
Nan::Set(obj, Nan::New<v8::String>("pty").ToLocalChecked(),
|
||||||
Nan::New<v8::Number>(slave));
|
Nan::New<v8::String>(ptsname(master)).ToLocalChecked());
|
||||||
Nan::Set(obj,
|
|
||||||
Nan::New<v8::String>("pty").ToLocalChecked(),
|
|
||||||
Nan::New<v8::String>(ptsname(master)).ToLocalChecked());
|
|
||||||
|
|
||||||
return info.GetReturnValue().Set(obj);
|
return info.GetReturnValue().Set(obj);
|
||||||
}
|
}
|
||||||
|
@ -372,9 +350,7 @@ NAN_METHOD(PtyOpen) {
|
||||||
NAN_METHOD(PtyResize) {
|
NAN_METHOD(PtyResize) {
|
||||||
Nan::HandleScope scope;
|
Nan::HandleScope scope;
|
||||||
|
|
||||||
if (info.Length() != 3 ||
|
if (info.Length() != 3 || !info[0]->IsNumber() || !info[1]->IsNumber() ||
|
||||||
!info[0]->IsNumber() ||
|
|
||||||
!info[1]->IsNumber() ||
|
|
||||||
!info[2]->IsNumber()) {
|
!info[2]->IsNumber()) {
|
||||||
return Nan::ThrowError("Usage: pty.resize(fd, cols, rows)");
|
return Nan::ThrowError("Usage: pty.resize(fd, cols, rows)");
|
||||||
}
|
}
|
||||||
|
@ -389,10 +365,14 @@ NAN_METHOD(PtyResize) {
|
||||||
|
|
||||||
if (ioctl(fd, TIOCSWINSZ, &winp) == -1) {
|
if (ioctl(fd, TIOCSWINSZ, &winp) == -1) {
|
||||||
switch (errno) {
|
switch (errno) {
|
||||||
case EBADF: return Nan::ThrowError("ioctl(2) failed, EBADF");
|
case EBADF:
|
||||||
case EFAULT: return Nan::ThrowError("ioctl(2) failed, EFAULT");
|
return Nan::ThrowError("ioctl(2) failed, EBADF");
|
||||||
case EINVAL: return Nan::ThrowError("ioctl(2) failed, EINVAL");
|
case EFAULT:
|
||||||
case ENOTTY: return Nan::ThrowError("ioctl(2) failed, ENOTTY");
|
return Nan::ThrowError("ioctl(2) failed, EFAULT");
|
||||||
|
case EINVAL:
|
||||||
|
return Nan::ThrowError("ioctl(2) failed, EINVAL");
|
||||||
|
case ENOTTY:
|
||||||
|
return Nan::ThrowError("ioctl(2) failed, ENOTTY");
|
||||||
}
|
}
|
||||||
return Nan::ThrowError("ioctl(2) failed");
|
return Nan::ThrowError("ioctl(2) failed");
|
||||||
}
|
}
|
||||||
|
@ -406,9 +386,7 @@ NAN_METHOD(PtyResize) {
|
||||||
NAN_METHOD(PtyGetProc) {
|
NAN_METHOD(PtyGetProc) {
|
||||||
Nan::HandleScope scope;
|
Nan::HandleScope scope;
|
||||||
|
|
||||||
if (info.Length() != 2 ||
|
if (info.Length() != 2 || !info[0]->IsNumber() || !info[1]->IsString()) {
|
||||||
!info[0]->IsNumber() ||
|
|
||||||
!info[1]->IsString()) {
|
|
||||||
return Nan::ThrowError("Usage: pty.process(fd, tty)");
|
return Nan::ThrowError("Usage: pty.process(fd, tty)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -434,8 +412,7 @@ NAN_METHOD(PtyGetProc) {
|
||||||
|
|
||||||
// execvpe(3) is not portable.
|
// execvpe(3) is not portable.
|
||||||
// http://www.gnu.org/software/gnulib/manual/html_node/execvpe.html
|
// http://www.gnu.org/software/gnulib/manual/html_node/execvpe.html
|
||||||
static int
|
static int pty_execvpe(const char *file, char **argv, char **envp) {
|
||||||
pty_execvpe(const char *file, char **argv, char **envp) {
|
|
||||||
char **old = environ;
|
char **old = environ;
|
||||||
environ = envp;
|
environ = envp;
|
||||||
int ret = execvp(file, argv);
|
int ret = execvp(file, argv);
|
||||||
|
@ -447,10 +424,10 @@ pty_execvpe(const char *file, char **argv, char **envp) {
|
||||||
* Nonblocking FD
|
* Nonblocking FD
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static int
|
static int pty_nonblock(int fd) {
|
||||||
pty_nonblock(int fd) {
|
|
||||||
int flags = fcntl(fd, F_GETFL, 0);
|
int flags = fcntl(fd, F_GETFL, 0);
|
||||||
if (flags == -1) return -1;
|
if (flags == -1)
|
||||||
|
return -1;
|
||||||
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -459,12 +436,11 @@ pty_nonblock(int fd) {
|
||||||
* Wait for SIGCHLD to read exit status.
|
* Wait for SIGCHLD to read exit status.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static void
|
static void pty_waitpid(void *data) {
|
||||||
pty_waitpid(void *data) {
|
|
||||||
int ret;
|
int ret;
|
||||||
int stat_loc;
|
int stat_loc;
|
||||||
|
|
||||||
pty_baton *baton = static_cast<pty_baton*>(data);
|
pty_baton *baton = static_cast<pty_baton *>(data);
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
|
|
||||||
|
@ -497,14 +473,13 @@ pty_waitpid(void *data) {
|
||||||
* Callback after exit status has been read.
|
* Callback after exit status has been read.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static void
|
static void pty_after_waitpid(uv_async_t *async) {
|
||||||
pty_after_waitpid(uv_async_t *async) {
|
|
||||||
Nan::HandleScope scope;
|
Nan::HandleScope scope;
|
||||||
pty_baton *baton = static_cast<pty_baton*>(async->data);
|
pty_baton *baton = static_cast<pty_baton *>(async->data);
|
||||||
|
|
||||||
v8::Local<v8::Value> argv[] = {
|
v8::Local<v8::Value> argv[] = {
|
||||||
Nan::New<v8::Integer>(baton->exit_code),
|
Nan::New<v8::Integer>(baton->exit_code),
|
||||||
Nan::New<v8::Integer>(baton->signal_code),
|
Nan::New<v8::Integer>(baton->signal_code),
|
||||||
};
|
};
|
||||||
|
|
||||||
v8::Local<v8::Function> cb = Nan::New<v8::Function>(baton->cb);
|
v8::Local<v8::Function> cb = Nan::New<v8::Function>(baton->cb);
|
||||||
|
@ -521,10 +496,9 @@ pty_after_waitpid(uv_async_t *async) {
|
||||||
* uv_close() callback - free handle data
|
* uv_close() callback - free handle data
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static void
|
static void pty_after_close(uv_handle_t *handle) {
|
||||||
pty_after_close(uv_handle_t *handle) {
|
|
||||||
uv_async_t *async = (uv_async_t *)handle;
|
uv_async_t *async = (uv_async_t *)handle;
|
||||||
pty_baton *baton = static_cast<pty_baton*>(async->data);
|
pty_baton *baton = static_cast<pty_baton *>(async->data);
|
||||||
delete baton;
|
delete baton;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -552,8 +526,7 @@ pty_after_close(uv_handle_t *handle) {
|
||||||
|
|
||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
|
|
||||||
static char *
|
static char *pty_getproc(int fd, char *tty) {
|
||||||
pty_getproc(int fd, char *tty) {
|
|
||||||
FILE *f;
|
FILE *f;
|
||||||
char *path, *buf;
|
char *path, *buf;
|
||||||
size_t len;
|
size_t len;
|
||||||
|
@ -566,7 +539,8 @@ pty_getproc(int fd, char *tty) {
|
||||||
}
|
}
|
||||||
|
|
||||||
r = asprintf(&path, "/proc/%lld/cmdline", (long long)pgrp);
|
r = asprintf(&path, "/proc/%lld/cmdline", (long long)pgrp);
|
||||||
if (r == -1 || path == NULL) return NULL;
|
if (r == -1 || path == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if ((f = fopen(path, "r")) == NULL) {
|
if ((f = fopen(path, "r")) == NULL) {
|
||||||
free(path);
|
free(path);
|
||||||
|
@ -578,9 +552,11 @@ pty_getproc(int fd, char *tty) {
|
||||||
len = 0;
|
len = 0;
|
||||||
buf = NULL;
|
buf = NULL;
|
||||||
while ((ch = fgetc(f)) != EOF) {
|
while ((ch = fgetc(f)) != EOF) {
|
||||||
if (ch == '\0') break;
|
if (ch == '\0')
|
||||||
|
break;
|
||||||
buf = (char *)realloc(buf, len + 2);
|
buf = (char *)realloc(buf, len + 2);
|
||||||
if (buf == NULL) return NULL;
|
if (buf == NULL)
|
||||||
|
return NULL;
|
||||||
buf[len++] = ch;
|
buf[len++] = ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -594,9 +570,8 @@ pty_getproc(int fd, char *tty) {
|
||||||
|
|
||||||
#elif defined(__APPLE__)
|
#elif defined(__APPLE__)
|
||||||
|
|
||||||
static char *
|
static char *pty_getproc(int fd, char *tty) {
|
||||||
pty_getproc(int fd, char *tty) {
|
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, 0};
|
||||||
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, 0 };
|
|
||||||
size_t size;
|
size_t size;
|
||||||
struct kinfo_proc kp;
|
struct kinfo_proc kp;
|
||||||
|
|
||||||
|
@ -618,10 +593,7 @@ pty_getproc(int fd, char *tty) {
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
static char *
|
static char *pty_getproc(int fd, char *tty) { return NULL; }
|
||||||
pty_getproc(int fd, char *tty) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -629,36 +601,43 @@ pty_getproc(int fd, char *tty) {
|
||||||
* openpty(3) / forkpty(3)
|
* openpty(3) / forkpty(3)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static int
|
static int pty_openpty(int *amaster, int *aslave, char *name,
|
||||||
pty_openpty(int *amaster,
|
const struct termios *termp,
|
||||||
int *aslave,
|
const struct winsize *winp) {
|
||||||
char *name,
|
|
||||||
const struct termios *termp,
|
|
||||||
const struct winsize *winp) {
|
|
||||||
#if defined(__sun)
|
#if defined(__sun)
|
||||||
char *slave_name;
|
char *slave_name;
|
||||||
int slave;
|
int slave;
|
||||||
int master = open("/dev/ptmx", O_RDWR | O_NOCTTY);
|
int master = open("/dev/ptmx", O_RDWR | O_NOCTTY);
|
||||||
if (master == -1) return -1;
|
if (master == -1)
|
||||||
if (amaster) *amaster = master;
|
return -1;
|
||||||
|
if (amaster)
|
||||||
|
*amaster = master;
|
||||||
|
|
||||||
if (grantpt(master) == -1) goto err;
|
if (grantpt(master) == -1)
|
||||||
if (unlockpt(master) == -1) goto err;
|
goto err;
|
||||||
|
if (unlockpt(master) == -1)
|
||||||
|
goto err;
|
||||||
|
|
||||||
slave_name = ptsname(master);
|
slave_name = ptsname(master);
|
||||||
if (slave_name == NULL) goto err;
|
if (slave_name == NULL)
|
||||||
if (name) strcpy(name, slave_name);
|
goto err;
|
||||||
|
if (name)
|
||||||
|
strcpy(name, slave_name);
|
||||||
|
|
||||||
slave = open(slave_name, O_RDWR | O_NOCTTY);
|
slave = open(slave_name, O_RDWR | O_NOCTTY);
|
||||||
if (slave == -1) goto err;
|
if (slave == -1)
|
||||||
if (aslave) *aslave = slave;
|
goto err;
|
||||||
|
if (aslave)
|
||||||
|
*aslave = slave;
|
||||||
|
|
||||||
ioctl(slave, I_PUSH, "ptem");
|
ioctl(slave, I_PUSH, "ptem");
|
||||||
ioctl(slave, I_PUSH, "ldterm");
|
ioctl(slave, I_PUSH, "ldterm");
|
||||||
ioctl(slave, I_PUSH, "ttcompat");
|
ioctl(slave, I_PUSH, "ttcompat");
|
||||||
|
|
||||||
if (termp) tcsetattr(slave, TCSAFLUSH, termp);
|
if (termp)
|
||||||
if (winp) ioctl(slave, TIOCSWINSZ, winp);
|
tcsetattr(slave, TCSAFLUSH, termp);
|
||||||
|
if (winp)
|
||||||
|
ioctl(slave, TIOCSWINSZ, winp);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
@ -670,46 +649,46 @@ err:
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static pid_t
|
static pid_t pty_forkpty(int *amaster, char *name, const struct termios *termp,
|
||||||
pty_forkpty(int *amaster,
|
const struct winsize *winp) {
|
||||||
char *name,
|
|
||||||
const struct termios *termp,
|
|
||||||
const struct winsize *winp) {
|
|
||||||
#if defined(__sun)
|
#if defined(__sun)
|
||||||
int master, slave;
|
int master, slave;
|
||||||
|
|
||||||
int ret = pty_openpty(&master, &slave, name, termp, winp);
|
int ret = pty_openpty(&master, &slave, name, termp, winp);
|
||||||
if (ret == -1) return -1;
|
if (ret == -1)
|
||||||
if (amaster) *amaster = master;
|
return -1;
|
||||||
|
if (amaster)
|
||||||
|
*amaster = master;
|
||||||
|
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
|
|
||||||
switch (pid) {
|
switch (pid) {
|
||||||
case -1: // error in fork, we are still in parent
|
case -1: // error in fork, we are still in parent
|
||||||
close(master);
|
close(master);
|
||||||
close(slave);
|
close(slave);
|
||||||
return -1;
|
return -1;
|
||||||
case 0: // we are in the child process
|
case 0: // we are in the child process
|
||||||
close(master);
|
close(master);
|
||||||
setsid();
|
setsid();
|
||||||
|
|
||||||
#if defined(TIOCSCTTY)
|
#if defined(TIOCSCTTY)
|
||||||
// glibc does this
|
// glibc does this
|
||||||
if (ioctl(slave, TIOCSCTTY, NULL) == -1) {
|
if (ioctl(slave, TIOCSCTTY, NULL) == -1) {
|
||||||
_exit(1);
|
_exit(1);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
dup2(slave, 0);
|
dup2(slave, 0);
|
||||||
dup2(slave, 1);
|
dup2(slave, 1);
|
||||||
dup2(slave, 2);
|
dup2(slave, 2);
|
||||||
|
|
||||||
if (slave > 2) close(slave);
|
if (slave > 2)
|
||||||
|
|
||||||
return 0;
|
|
||||||
default: // we are in the parent process
|
|
||||||
close(slave);
|
close(slave);
|
||||||
return pid;
|
|
||||||
|
return 0;
|
||||||
|
default: // we are in the parent process
|
||||||
|
close(slave);
|
||||||
|
return pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -718,7 +697,6 @@ pty_forkpty(int *amaster,
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Init
|
* Init
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -8,33 +8,37 @@
|
||||||
* with pseudo-terminal file descriptors.
|
* with pseudo-terminal file descriptors.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// node versions lower than 10 define this as 0x502 which disables many of the definitions needed to compile
|
// node versions lower than 10 define this as 0x502 which disables many of the
|
||||||
|
// definitions needed to compile
|
||||||
#include <node_version.h>
|
#include <node_version.h>
|
||||||
#if NODE_MODULE_VERSION <= 57
|
#if NODE_MODULE_VERSION <= 57
|
||||||
#define _WIN32_WINNT 0x600
|
#define _WIN32_WINNT 0x600
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "path_util.h"
|
||||||
|
#include <Shlwapi.h> // PathCombine, PathIsRelative
|
||||||
|
#include <Windows.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <nan.h>
|
#include <nan.h>
|
||||||
#include <Shlwapi.h> // PathCombine, PathIsRelative
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
|
||||||
#include <Windows.h>
|
|
||||||
#include <strsafe.h>
|
#include <strsafe.h>
|
||||||
#include "path_util.h"
|
#include <vector>
|
||||||
|
|
||||||
extern "C" void init(v8::Local<v8::Object>);
|
extern "C" void init(v8::Local<v8::Object>);
|
||||||
|
|
||||||
// Taken from the RS5 Windows SDK, but redefined here in case we're targeting <= 17134
|
// Taken from the RS5 Windows SDK, but redefined here in case we're targeting <=
|
||||||
|
// 17134
|
||||||
#ifndef PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE
|
#ifndef PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE
|
||||||
#define PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE \
|
#define PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE \
|
||||||
ProcThreadAttributeValue(22, FALSE, TRUE, FALSE)
|
ProcThreadAttributeValue(22, FALSE, TRUE, FALSE)
|
||||||
|
|
||||||
typedef VOID* HPCON;
|
typedef VOID *HPCON;
|
||||||
typedef HRESULT (__stdcall *PFNCREATEPSEUDOCONSOLE)(COORD c, HANDLE hIn, HANDLE hOut, DWORD dwFlags, HPCON* phpcon);
|
typedef HRESULT(__stdcall *PFNCREATEPSEUDOCONSOLE)(COORD c, HANDLE hIn,
|
||||||
typedef HRESULT (__stdcall *PFNRESIZEPSEUDOCONSOLE)(HPCON hpc, COORD newSize);
|
HANDLE hOut, DWORD dwFlags,
|
||||||
typedef void (__stdcall *PFNCLOSEPSEUDOCONSOLE)(HPCON hpc);
|
HPCON *phpcon);
|
||||||
|
typedef HRESULT(__stdcall *PFNRESIZEPSEUDOCONSOLE)(HPCON hpc, COORD newSize);
|
||||||
|
typedef void(__stdcall *PFNCLOSEPSEUDOCONSOLE)(HPCON hpc);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -50,15 +54,16 @@ struct pty_baton {
|
||||||
uv_async_t async;
|
uv_async_t async;
|
||||||
uv_thread_t tid;
|
uv_thread_t tid;
|
||||||
|
|
||||||
pty_baton(int _id, HANDLE _hIn, HANDLE _hOut, HPCON _hpc) : id(_id), hIn(_hIn), hOut(_hOut), hpc(_hpc) {};
|
pty_baton(int _id, HANDLE _hIn, HANDLE _hOut, HPCON _hpc)
|
||||||
|
: id(_id), hIn(_hIn), hOut(_hOut), hpc(_hpc){};
|
||||||
};
|
};
|
||||||
|
|
||||||
static std::vector<pty_baton*> ptyHandles;
|
static std::vector<pty_baton *> ptyHandles;
|
||||||
static volatile LONG ptyCounter;
|
static volatile LONG ptyCounter;
|
||||||
|
|
||||||
static pty_baton* get_pty_baton(int id) {
|
static pty_baton *get_pty_baton(int id) {
|
||||||
for (size_t i = 0; i < ptyHandles.size(); ++i) {
|
for (size_t i = 0; i < ptyHandles.size(); ++i) {
|
||||||
pty_baton* ptyHandle = ptyHandles[i];
|
pty_baton *ptyHandle = ptyHandles[i];
|
||||||
if (ptyHandle->id == id) {
|
if (ptyHandle->id == id) {
|
||||||
return ptyHandle;
|
return ptyHandle;
|
||||||
}
|
}
|
||||||
|
@ -68,10 +73,11 @@ static pty_baton* get_pty_baton(int id) {
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::vector<T> vectorFromString(const std::basic_string<T> &str) {
|
std::vector<T> vectorFromString(const std::basic_string<T> &str) {
|
||||||
return std::vector<T>(str.begin(), str.end());
|
return std::vector<T>(str.begin(), str.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
void throwNanError(const Nan::FunctionCallbackInfo<v8::Value>* info, const char* text, const bool getLastError) {
|
void throwNanError(const Nan::FunctionCallbackInfo<v8::Value> *info,
|
||||||
|
const char *text, const bool getLastError) {
|
||||||
std::stringstream errorText;
|
std::stringstream errorText;
|
||||||
errorText << text;
|
errorText << text;
|
||||||
if (getLastError) {
|
if (getLastError) {
|
||||||
|
@ -82,69 +88,59 @@ void throwNanError(const Nan::FunctionCallbackInfo<v8::Value>* info, const char*
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a new server named pipe. It has not yet been connected.
|
// Returns a new server named pipe. It has not yet been connected.
|
||||||
bool createDataServerPipe(bool write,
|
bool createDataServerPipe(bool write, std::wstring kind, HANDLE *hServer,
|
||||||
std::wstring kind,
|
std::wstring &name, const std::wstring &pipeName) {
|
||||||
HANDLE* hServer,
|
|
||||||
std::wstring &name,
|
|
||||||
const std::wstring &pipeName)
|
|
||||||
{
|
|
||||||
*hServer = INVALID_HANDLE_VALUE;
|
*hServer = INVALID_HANDLE_VALUE;
|
||||||
|
|
||||||
name = L"\\\\.\\pipe\\" + pipeName + L"-" + kind;
|
name = L"\\\\.\\pipe\\" + pipeName + L"-" + kind;
|
||||||
|
|
||||||
const DWORD winOpenMode = PIPE_ACCESS_INBOUND | PIPE_ACCESS_OUTBOUND | FILE_FLAG_FIRST_PIPE_INSTANCE/* | FILE_FLAG_OVERLAPPED */;
|
const DWORD winOpenMode =
|
||||||
|
PIPE_ACCESS_INBOUND | PIPE_ACCESS_OUTBOUND |
|
||||||
|
FILE_FLAG_FIRST_PIPE_INSTANCE /* | FILE_FLAG_OVERLAPPED */;
|
||||||
|
|
||||||
SECURITY_ATTRIBUTES sa = {};
|
SECURITY_ATTRIBUTES sa = {};
|
||||||
sa.nLength = sizeof(sa);
|
sa.nLength = sizeof(sa);
|
||||||
|
|
||||||
*hServer = CreateNamedPipeW(
|
*hServer = CreateNamedPipeW(name.c_str(),
|
||||||
name.c_str(),
|
/*dwOpenMode=*/winOpenMode,
|
||||||
/*dwOpenMode=*/winOpenMode,
|
/*dwPipeMode=*/PIPE_TYPE_BYTE |
|
||||||
/*dwPipeMode=*/PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
|
PIPE_READMODE_BYTE | PIPE_WAIT,
|
||||||
/*nMaxInstances=*/1,
|
/*nMaxInstances=*/1,
|
||||||
/*nOutBufferSize=*/0,
|
/*nOutBufferSize=*/0,
|
||||||
/*nInBufferSize=*/0,
|
/*nInBufferSize=*/0,
|
||||||
/*nDefaultTimeOut=*/30000,
|
/*nDefaultTimeOut=*/30000, &sa);
|
||||||
&sa);
|
|
||||||
|
|
||||||
return *hServer != INVALID_HANDLE_VALUE;
|
return *hServer != INVALID_HANDLE_VALUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
HRESULT CreateNamedPipesAndPseudoConsole(COORD size,
|
HRESULT CreateNamedPipesAndPseudoConsole(COORD size, DWORD dwFlags,
|
||||||
DWORD dwFlags,
|
HANDLE *phInput, HANDLE *phOutput,
|
||||||
HANDLE *phInput,
|
HPCON *phPC, std::wstring &inName,
|
||||||
HANDLE *phOutput,
|
std::wstring &outName,
|
||||||
HPCON* phPC,
|
const std::wstring &pipeName) {
|
||||||
std::wstring& inName,
|
|
||||||
std::wstring& outName,
|
|
||||||
const std::wstring& pipeName)
|
|
||||||
{
|
|
||||||
HANDLE hLibrary = LoadLibraryExW(L"kernel32.dll", 0, 0);
|
HANDLE hLibrary = LoadLibraryExW(L"kernel32.dll", 0, 0);
|
||||||
bool fLoadedDll = hLibrary != nullptr;
|
bool fLoadedDll = hLibrary != nullptr;
|
||||||
if (fLoadedDll)
|
if (fLoadedDll) {
|
||||||
{
|
PFNCREATEPSEUDOCONSOLE const pfnCreate =
|
||||||
PFNCREATEPSEUDOCONSOLE const pfnCreate = (PFNCREATEPSEUDOCONSOLE)GetProcAddress((HMODULE)hLibrary, "CreatePseudoConsole");
|
(PFNCREATEPSEUDOCONSOLE)GetProcAddress((HMODULE)hLibrary,
|
||||||
if (pfnCreate)
|
"CreatePseudoConsole");
|
||||||
{
|
if (pfnCreate) {
|
||||||
if (phPC == NULL || phInput == NULL || phOutput == NULL)
|
if (phPC == NULL || phInput == NULL || phOutput == NULL) {
|
||||||
{
|
|
||||||
return E_INVALIDARG;
|
return E_INVALIDARG;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool success = createDataServerPipe(true, L"in", phInput, inName, pipeName);
|
bool success =
|
||||||
if (!success)
|
createDataServerPipe(true, L"in", phInput, inName, pipeName);
|
||||||
{
|
if (!success) {
|
||||||
return HRESULT_FROM_WIN32(GetLastError());
|
return HRESULT_FROM_WIN32(GetLastError());
|
||||||
}
|
}
|
||||||
success = createDataServerPipe(false, L"out", phOutput, outName, pipeName);
|
success =
|
||||||
if (!success)
|
createDataServerPipe(false, L"out", phOutput, outName, pipeName);
|
||||||
{
|
if (!success) {
|
||||||
return HRESULT_FROM_WIN32(GetLastError());
|
return HRESULT_FROM_WIN32(GetLastError());
|
||||||
}
|
}
|
||||||
return pfnCreate(size, *phInput, *phOutput, dwFlags, phPC);
|
return pfnCreate(size, *phInput, *phOutput, dwFlags, phPC);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
// Failed to find CreatePseudoConsole in kernel32. This is likely because
|
// Failed to find CreatePseudoConsole in kernel32. This is likely because
|
||||||
// the user is not running a build of Windows that supports that API.
|
// the user is not running a build of Windows that supports that API.
|
||||||
// We should fall back to winpty in this case.
|
// We should fall back to winpty in this case.
|
||||||
|
@ -153,7 +149,8 @@ HRESULT CreateNamedPipesAndPseudoConsole(COORD size,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Failed to find kernel32. This is realy unlikely - honestly no idea how
|
// Failed to find kernel32. This is realy unlikely - honestly no idea how
|
||||||
// this is even possible to hit. But if it does happen, fall back to winpty.
|
// this is even possible to hit. But if it does happen, fall back to
|
||||||
|
// winpty.
|
||||||
return HRESULT_FROM_WIN32(GetLastError());
|
return HRESULT_FROM_WIN32(GetLastError());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -166,14 +163,11 @@ static NAN_METHOD(PtyStartProcess) {
|
||||||
std::unique_ptr<wchar_t[]> mutableCommandline;
|
std::unique_ptr<wchar_t[]> mutableCommandline;
|
||||||
PROCESS_INFORMATION _piClient{};
|
PROCESS_INFORMATION _piClient{};
|
||||||
|
|
||||||
if (info.Length() != 6 ||
|
if (info.Length() != 6 || !info[0]->IsString() || !info[1]->IsNumber() ||
|
||||||
!info[0]->IsString() ||
|
!info[2]->IsNumber() || !info[3]->IsBoolean() || !info[4]->IsString() ||
|
||||||
!info[1]->IsNumber() ||
|
|
||||||
!info[2]->IsNumber() ||
|
|
||||||
!info[3]->IsBoolean() ||
|
|
||||||
!info[4]->IsString() ||
|
|
||||||
!info[5]->IsBoolean()) {
|
!info[5]->IsBoolean()) {
|
||||||
Nan::ThrowError("Usage: pty.startProcess(file, cols, rows, debug, pipeName, inheritCursor)");
|
Nan::ThrowError("Usage: pty.startProcess(file, cols, rows, debug, "
|
||||||
|
"pipeName, inheritCursor)");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -204,7 +198,9 @@ static NAN_METHOD(PtyStartProcess) {
|
||||||
|
|
||||||
HANDLE hIn, hOut;
|
HANDLE hIn, hOut;
|
||||||
HPCON hpc;
|
HPCON hpc;
|
||||||
HRESULT hr = CreateNamedPipesAndPseudoConsole({cols, rows}, inheritCursor ? 1/*PSEUDOCONSOLE_INHERIT_CURSOR*/ : 0, &hIn, &hOut, &hpc, inName, outName, pipeName);
|
HRESULT hr = CreateNamedPipesAndPseudoConsole(
|
||||||
|
{cols, rows}, inheritCursor ? 1 /*PSEUDOCONSOLE_INHERIT_CURSOR*/ : 0,
|
||||||
|
&hIn, &hOut, &hpc, inName, outName, pipeName);
|
||||||
|
|
||||||
// Restore default handling of ctrl+c
|
// Restore default handling of ctrl+c
|
||||||
SetConsoleCtrlHandler(NULL, FALSE);
|
SetConsoleCtrlHandler(NULL, FALSE);
|
||||||
|
@ -215,28 +211,31 @@ static NAN_METHOD(PtyStartProcess) {
|
||||||
if (SUCCEEDED(hr)) {
|
if (SUCCEEDED(hr)) {
|
||||||
// We were able to instantiate a conpty
|
// We were able to instantiate a conpty
|
||||||
const int ptyId = InterlockedIncrement(&ptyCounter);
|
const int ptyId = InterlockedIncrement(&ptyCounter);
|
||||||
Nan::Set(marshal, Nan::New<v8::String>("pty").ToLocalChecked(), Nan::New<v8::Number>(ptyId));
|
Nan::Set(marshal, Nan::New<v8::String>("pty").ToLocalChecked(),
|
||||||
|
Nan::New<v8::Number>(ptyId));
|
||||||
ptyHandles.insert(ptyHandles.end(), new pty_baton(ptyId, hIn, hOut, hpc));
|
ptyHandles.insert(ptyHandles.end(), new pty_baton(ptyId, hIn, hOut, hpc));
|
||||||
} else {
|
} else {
|
||||||
Nan::ThrowError("Cannot launch conpty");
|
Nan::ThrowError("Cannot launch conpty");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Nan::Set(marshal, Nan::New<v8::String>("fd").ToLocalChecked(), Nan::New<v8::Number>(-1));
|
Nan::Set(marshal, Nan::New<v8::String>("fd").ToLocalChecked(),
|
||||||
|
Nan::New<v8::Number>(-1));
|
||||||
{
|
{
|
||||||
std::string coninPipeNameStr(inName.begin(), inName.end());
|
std::string coninPipeNameStr(inName.begin(), inName.end());
|
||||||
Nan::Set(marshal, Nan::New<v8::String>("conin").ToLocalChecked(), Nan::New<v8::String>(coninPipeNameStr).ToLocalChecked());
|
Nan::Set(marshal, Nan::New<v8::String>("conin").ToLocalChecked(),
|
||||||
|
Nan::New<v8::String>(coninPipeNameStr).ToLocalChecked());
|
||||||
|
|
||||||
std::string conoutPipeNameStr(outName.begin(), outName.end());
|
std::string conoutPipeNameStr(outName.begin(), outName.end());
|
||||||
Nan::Set(marshal, Nan::New<v8::String>("conout").ToLocalChecked(), Nan::New<v8::String>(conoutPipeNameStr).ToLocalChecked());
|
Nan::Set(marshal, Nan::New<v8::String>("conout").ToLocalChecked(),
|
||||||
|
Nan::New<v8::String>(conoutPipeNameStr).ToLocalChecked());
|
||||||
}
|
}
|
||||||
info.GetReturnValue().Set(marshal);
|
info.GetReturnValue().Set(marshal);
|
||||||
}
|
}
|
||||||
|
|
||||||
VOID CALLBACK OnProcessExitWinEvent(
|
VOID CALLBACK OnProcessExitWinEvent(_In_ PVOID context,
|
||||||
_In_ PVOID context,
|
_In_ BOOLEAN TimerOrWaitFired) {
|
||||||
_In_ BOOLEAN TimerOrWaitFired) {
|
pty_baton *baton = static_cast<pty_baton *>(context);
|
||||||
pty_baton *baton = static_cast<pty_baton*>(context);
|
|
||||||
|
|
||||||
// Fire OnProcessExit
|
// Fire OnProcessExit
|
||||||
uv_async_send(&baton->async);
|
uv_async_send(&baton->async);
|
||||||
|
@ -244,7 +243,7 @@ VOID CALLBACK OnProcessExitWinEvent(
|
||||||
|
|
||||||
static void OnProcessExit(uv_async_t *async) {
|
static void OnProcessExit(uv_async_t *async) {
|
||||||
Nan::HandleScope scope;
|
Nan::HandleScope scope;
|
||||||
pty_baton *baton = static_cast<pty_baton*>(async->data);
|
pty_baton *baton = static_cast<pty_baton *>(async->data);
|
||||||
|
|
||||||
UnregisterWait(baton->hWait);
|
UnregisterWait(baton->hWait);
|
||||||
|
|
||||||
|
@ -253,9 +252,7 @@ static void OnProcessExit(uv_async_t *async) {
|
||||||
GetExitCodeProcess(baton->hShell, &exitCode);
|
GetExitCodeProcess(baton->hShell, &exitCode);
|
||||||
|
|
||||||
// Call function
|
// Call function
|
||||||
v8::Local<v8::Value> args[1] = {
|
v8::Local<v8::Value> args[1] = {Nan::New<v8::Number>(exitCode)};
|
||||||
Nan::New<v8::Number>(exitCode)
|
|
||||||
};
|
|
||||||
|
|
||||||
Nan::AsyncResource asyncResource("node-pty.callback");
|
Nan::AsyncResource asyncResource("node-pty.callback");
|
||||||
baton->cb.Call(1, args, &asyncResource);
|
baton->cb.Call(1, args, &asyncResource);
|
||||||
|
@ -273,12 +270,8 @@ static NAN_METHOD(PtyConnect) {
|
||||||
std::stringstream errorText;
|
std::stringstream errorText;
|
||||||
BOOL fSuccess = FALSE;
|
BOOL fSuccess = FALSE;
|
||||||
|
|
||||||
if (info.Length() != 5 ||
|
if (info.Length() != 5 || !info[0]->IsNumber() || !info[1]->IsString() ||
|
||||||
!info[0]->IsNumber() ||
|
!info[2]->IsString() || !info[3]->IsArray() || !info[4]->IsFunction()) {
|
||||||
!info[1]->IsString() ||
|
|
||||||
!info[2]->IsString() ||
|
|
||||||
!info[3]->IsArray() ||
|
|
||||||
!info[4]->IsFunction()) {
|
|
||||||
Nan::ThrowError("Usage: pty.connect(id, cmdline, cwd, env, exitCallback)");
|
Nan::ThrowError("Usage: pty.connect(id, cmdline, cwd, env, exitCallback)");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -287,22 +280,27 @@ static NAN_METHOD(PtyConnect) {
|
||||||
const std::wstring cmdline(path_util::to_wstring(Nan::Utf8String(info[1])));
|
const std::wstring cmdline(path_util::to_wstring(Nan::Utf8String(info[1])));
|
||||||
const std::wstring cwd(path_util::to_wstring(Nan::Utf8String(info[2])));
|
const std::wstring cwd(path_util::to_wstring(Nan::Utf8String(info[2])));
|
||||||
const v8::Local<v8::Array> envValues = info[3].As<v8::Array>();
|
const v8::Local<v8::Array> envValues = info[3].As<v8::Array>();
|
||||||
const v8::Local<v8::Function> exitCallback = v8::Local<v8::Function>::Cast(info[4]);
|
const v8::Local<v8::Function> exitCallback =
|
||||||
|
v8::Local<v8::Function>::Cast(info[4]);
|
||||||
|
|
||||||
// Prepare command line
|
// Prepare command line
|
||||||
std::unique_ptr<wchar_t[]> mutableCommandline = std::make_unique<wchar_t[]>(cmdline.length() + 1);
|
std::unique_ptr<wchar_t[]> mutableCommandline =
|
||||||
HRESULT hr = StringCchCopyW(mutableCommandline.get(), cmdline.length() + 1, cmdline.c_str());
|
std::make_unique<wchar_t[]>(cmdline.length() + 1);
|
||||||
|
HRESULT hr = StringCchCopyW(mutableCommandline.get(), cmdline.length() + 1,
|
||||||
|
cmdline.c_str());
|
||||||
|
|
||||||
// Prepare cwd
|
// Prepare cwd
|
||||||
std::unique_ptr<wchar_t[]> mutableCwd = std::make_unique<wchar_t[]>(cwd.length() + 1);
|
std::unique_ptr<wchar_t[]> mutableCwd =
|
||||||
|
std::make_unique<wchar_t[]>(cwd.length() + 1);
|
||||||
hr = StringCchCopyW(mutableCwd.get(), cwd.length() + 1, cwd.c_str());
|
hr = StringCchCopyW(mutableCwd.get(), cwd.length() + 1, cwd.c_str());
|
||||||
|
|
||||||
// Prepare environment
|
// Prepare environment
|
||||||
std::wstring env;
|
std::wstring env;
|
||||||
if (!envValues.IsEmpty()) {
|
if (!envValues.IsEmpty()) {
|
||||||
std::wstringstream envBlock;
|
std::wstringstream envBlock;
|
||||||
for(uint32_t i = 0; i < envValues->Length(); i++) {
|
for (uint32_t i = 0; i < envValues->Length(); i++) {
|
||||||
std::wstring envValue(path_util::to_wstring(Nan::Utf8String(Nan::Get(envValues, i).ToLocalChecked())));
|
std::wstring envValue(path_util::to_wstring(
|
||||||
|
Nan::Utf8String(Nan::Get(envValues, i).ToLocalChecked())));
|
||||||
envBlock << envValue << L'\0';
|
envBlock << envValue << L'\0';
|
||||||
}
|
}
|
||||||
envBlock << L'\0';
|
envBlock << L'\0';
|
||||||
|
@ -312,7 +310,7 @@ static NAN_METHOD(PtyConnect) {
|
||||||
LPWSTR envArg = envV.empty() ? nullptr : envV.data();
|
LPWSTR envArg = envV.empty() ? nullptr : envV.data();
|
||||||
|
|
||||||
// Fetch pty handle from ID and start process
|
// Fetch pty handle from ID and start process
|
||||||
pty_baton* handle = get_pty_baton(id);
|
pty_baton *handle = get_pty_baton(id);
|
||||||
|
|
||||||
BOOL success = ConnectNamedPipe(handle->hIn, nullptr);
|
BOOL success = ConnectNamedPipe(handle->hIn, nullptr);
|
||||||
success = ConnectNamedPipe(handle->hOut, nullptr);
|
success = ConnectNamedPipe(handle->hOut, nullptr);
|
||||||
|
@ -328,35 +326,34 @@ static NAN_METHOD(PtyConnect) {
|
||||||
SIZE_T size = 0;
|
SIZE_T size = 0;
|
||||||
InitializeProcThreadAttributeList(NULL, 1, 0, &size);
|
InitializeProcThreadAttributeList(NULL, 1, 0, &size);
|
||||||
BYTE *attrList = new BYTE[size];
|
BYTE *attrList = new BYTE[size];
|
||||||
siEx.lpAttributeList = reinterpret_cast<PPROC_THREAD_ATTRIBUTE_LIST>(attrList);
|
siEx.lpAttributeList =
|
||||||
|
reinterpret_cast<PPROC_THREAD_ATTRIBUTE_LIST>(attrList);
|
||||||
|
|
||||||
fSuccess = InitializeProcThreadAttributeList(siEx.lpAttributeList, 1, 0, &size);
|
fSuccess =
|
||||||
|
InitializeProcThreadAttributeList(siEx.lpAttributeList, 1, 0, &size);
|
||||||
if (!fSuccess) {
|
if (!fSuccess) {
|
||||||
return throwNanError(&info, "InitializeProcThreadAttributeList failed", true);
|
return throwNanError(&info, "InitializeProcThreadAttributeList failed",
|
||||||
|
true);
|
||||||
}
|
}
|
||||||
fSuccess = UpdateProcThreadAttribute(siEx.lpAttributeList,
|
fSuccess = UpdateProcThreadAttribute(siEx.lpAttributeList, 0,
|
||||||
0,
|
|
||||||
PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE,
|
PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE,
|
||||||
handle->hpc,
|
handle->hpc, sizeof(HPCON), NULL, NULL);
|
||||||
sizeof(HPCON),
|
|
||||||
NULL,
|
|
||||||
NULL);
|
|
||||||
if (!fSuccess) {
|
if (!fSuccess) {
|
||||||
return throwNanError(&info, "UpdateProcThreadAttribute failed", true);
|
return throwNanError(&info, "UpdateProcThreadAttribute failed", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
PROCESS_INFORMATION piClient{};
|
PROCESS_INFORMATION piClient{};
|
||||||
fSuccess = !!CreateProcessW(
|
fSuccess = !!CreateProcessW(
|
||||||
nullptr,
|
nullptr, mutableCommandline.get(),
|
||||||
mutableCommandline.get(),
|
nullptr, // lpProcessAttributes
|
||||||
nullptr, // lpProcessAttributes
|
nullptr, // lpThreadAttributes
|
||||||
nullptr, // lpThreadAttributes
|
false, // bInheritHandles VERY IMPORTANT that this is false
|
||||||
false, // bInheritHandles VERY IMPORTANT that this is false
|
EXTENDED_STARTUPINFO_PRESENT |
|
||||||
EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT, // dwCreationFlags
|
CREATE_UNICODE_ENVIRONMENT, // dwCreationFlags
|
||||||
envArg, // lpEnvironment
|
envArg, // lpEnvironment
|
||||||
mutableCwd.get(), // lpCurrentDirectory
|
mutableCwd.get(), // lpCurrentDirectory
|
||||||
&siEx.StartupInfo, // lpStartupInfo
|
&siEx.StartupInfo, // lpStartupInfo
|
||||||
&piClient // lpProcessInformation
|
&piClient // lpProcessInformation
|
||||||
);
|
);
|
||||||
if (!fSuccess) {
|
if (!fSuccess) {
|
||||||
return throwNanError(&info, "Cannot create process", true);
|
return throwNanError(&info, "Cannot create process", true);
|
||||||
|
@ -371,20 +368,21 @@ static NAN_METHOD(PtyConnect) {
|
||||||
uv_async_init(uv_default_loop(), &handle->async, OnProcessExit);
|
uv_async_init(uv_default_loop(), &handle->async, OnProcessExit);
|
||||||
|
|
||||||
// Setup Windows wait for process exit event
|
// Setup Windows wait for process exit event
|
||||||
RegisterWaitForSingleObject(&handle->hWait, piClient.hProcess, OnProcessExitWinEvent, (PVOID)handle, INFINITE, WT_EXECUTEONLYONCE);
|
RegisterWaitForSingleObject(&handle->hWait, piClient.hProcess,
|
||||||
|
OnProcessExitWinEvent, (PVOID)handle, INFINITE,
|
||||||
|
WT_EXECUTEONLYONCE);
|
||||||
|
|
||||||
// Return
|
// Return
|
||||||
v8::Local<v8::Object> marshal = Nan::New<v8::Object>();
|
v8::Local<v8::Object> marshal = Nan::New<v8::Object>();
|
||||||
Nan::Set(marshal, Nan::New<v8::String>("pid").ToLocalChecked(), Nan::New<v8::Number>(piClient.dwProcessId));
|
Nan::Set(marshal, Nan::New<v8::String>("pid").ToLocalChecked(),
|
||||||
|
Nan::New<v8::Number>(piClient.dwProcessId));
|
||||||
info.GetReturnValue().Set(marshal);
|
info.GetReturnValue().Set(marshal);
|
||||||
}
|
}
|
||||||
|
|
||||||
static NAN_METHOD(PtyResize) {
|
static NAN_METHOD(PtyResize) {
|
||||||
Nan::HandleScope scope;
|
Nan::HandleScope scope;
|
||||||
|
|
||||||
if (info.Length() != 3 ||
|
if (info.Length() != 3 || !info[0]->IsNumber() || !info[1]->IsNumber() ||
|
||||||
!info[0]->IsNumber() ||
|
|
||||||
!info[1]->IsNumber() ||
|
|
||||||
!info[2]->IsNumber()) {
|
!info[2]->IsNumber()) {
|
||||||
Nan::ThrowError("Usage: pty.resize(id, cols, rows)");
|
Nan::ThrowError("Usage: pty.resize(id, cols, rows)");
|
||||||
return;
|
return;
|
||||||
|
@ -394,15 +392,15 @@ static NAN_METHOD(PtyResize) {
|
||||||
SHORT cols = info[1]->Uint32Value(Nan::GetCurrentContext()).FromJust();
|
SHORT cols = info[1]->Uint32Value(Nan::GetCurrentContext()).FromJust();
|
||||||
SHORT rows = info[2]->Uint32Value(Nan::GetCurrentContext()).FromJust();
|
SHORT rows = info[2]->Uint32Value(Nan::GetCurrentContext()).FromJust();
|
||||||
|
|
||||||
const pty_baton* handle = get_pty_baton(id);
|
const pty_baton *handle = get_pty_baton(id);
|
||||||
|
|
||||||
HANDLE hLibrary = LoadLibraryExW(L"kernel32.dll", 0, 0);
|
HANDLE hLibrary = LoadLibraryExW(L"kernel32.dll", 0, 0);
|
||||||
bool fLoadedDll = hLibrary != nullptr;
|
bool fLoadedDll = hLibrary != nullptr;
|
||||||
if (fLoadedDll)
|
if (fLoadedDll) {
|
||||||
{
|
PFNRESIZEPSEUDOCONSOLE const pfnResizePseudoConsole =
|
||||||
PFNRESIZEPSEUDOCONSOLE const pfnResizePseudoConsole = (PFNRESIZEPSEUDOCONSOLE)GetProcAddress((HMODULE)hLibrary, "ResizePseudoConsole");
|
(PFNRESIZEPSEUDOCONSOLE)GetProcAddress((HMODULE)hLibrary,
|
||||||
if (pfnResizePseudoConsole)
|
"ResizePseudoConsole");
|
||||||
{
|
if (pfnResizePseudoConsole) {
|
||||||
COORD size = {cols, rows};
|
COORD size = {cols, rows};
|
||||||
pfnResizePseudoConsole(handle->hpc, size);
|
pfnResizePseudoConsole(handle->hpc, size);
|
||||||
}
|
}
|
||||||
|
@ -414,23 +412,22 @@ static NAN_METHOD(PtyResize) {
|
||||||
static NAN_METHOD(PtyKill) {
|
static NAN_METHOD(PtyKill) {
|
||||||
Nan::HandleScope scope;
|
Nan::HandleScope scope;
|
||||||
|
|
||||||
if (info.Length() != 1 ||
|
if (info.Length() != 1 || !info[0]->IsNumber()) {
|
||||||
!info[0]->IsNumber()) {
|
|
||||||
Nan::ThrowError("Usage: pty.kill(id)");
|
Nan::ThrowError("Usage: pty.kill(id)");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int id = info[0]->Int32Value(Nan::GetCurrentContext()).FromJust();
|
int id = info[0]->Int32Value(Nan::GetCurrentContext()).FromJust();
|
||||||
|
|
||||||
const pty_baton* handle = get_pty_baton(id);
|
const pty_baton *handle = get_pty_baton(id);
|
||||||
|
|
||||||
HANDLE hLibrary = LoadLibraryExW(L"kernel32.dll", 0, 0);
|
HANDLE hLibrary = LoadLibraryExW(L"kernel32.dll", 0, 0);
|
||||||
bool fLoadedDll = hLibrary != nullptr;
|
bool fLoadedDll = hLibrary != nullptr;
|
||||||
if (fLoadedDll)
|
if (fLoadedDll) {
|
||||||
{
|
PFNCLOSEPSEUDOCONSOLE const pfnClosePseudoConsole =
|
||||||
PFNCLOSEPSEUDOCONSOLE const pfnClosePseudoConsole = (PFNCLOSEPSEUDOCONSOLE)GetProcAddress((HMODULE)hLibrary, "ClosePseudoConsole");
|
(PFNCLOSEPSEUDOCONSOLE)GetProcAddress((HMODULE)hLibrary,
|
||||||
if (pfnClosePseudoConsole)
|
"ClosePseudoConsole");
|
||||||
{
|
if (pfnClosePseudoConsole) {
|
||||||
pfnClosePseudoConsole(handle->hpc);
|
pfnClosePseudoConsole(handle->hpc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -441,8 +438,8 @@ static NAN_METHOD(PtyKill) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Init
|
* Init
|
||||||
*/
|
*/
|
||||||
|
|
||||||
extern "C" void init(v8::Local<v8::Object> target) {
|
extern "C" void init(v8::Local<v8::Object> target) {
|
||||||
Nan::HandleScope scope;
|
Nan::HandleScope scope;
|
||||||
|
|
|
@ -6,8 +6,7 @@
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
|
||||||
static NAN_METHOD(ApiConsoleProcessList) {
|
static NAN_METHOD(ApiConsoleProcessList) {
|
||||||
if (info.Length() != 1 ||
|
if (info.Length() != 1 || !info[0]->IsNumber()) {
|
||||||
!info[0]->IsNumber()) {
|
|
||||||
Nan::ThrowError("Usage: getConsoleProcessList(shellPid)");
|
Nan::ThrowError("Usage: getConsoleProcessList(shellPid)");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -21,10 +20,11 @@ static NAN_METHOD(ApiConsoleProcessList) {
|
||||||
Nan::ThrowError("AttachConsole failed");
|
Nan::ThrowError("AttachConsole failed");
|
||||||
}
|
}
|
||||||
auto processList = std::vector<DWORD>(64);
|
auto processList = std::vector<DWORD>(64);
|
||||||
auto processCount = GetConsoleProcessList(&processList[0], processList.size());
|
auto processCount =
|
||||||
|
GetConsoleProcessList(&processList[0], processList.size());
|
||||||
if (processList.size() < processCount) {
|
if (processList.size() < processCount) {
|
||||||
processList.resize(processCount);
|
processList.resize(processCount);
|
||||||
processCount = GetConsoleProcessList(&processList[0], processList.size());
|
processCount = GetConsoleProcessList(&processList[0], processList.size());
|
||||||
}
|
}
|
||||||
FreeConsole();
|
FreeConsole();
|
||||||
|
|
||||||
|
|
|
@ -4,14 +4,14 @@
|
||||||
* Copyright (c) 2018, Microsoft Corporation (MIT License).
|
* Copyright (c) 2018, Microsoft Corporation (MIT License).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <nan.h>
|
|
||||||
#include <Shlwapi.h> // PathCombine
|
#include <Shlwapi.h> // PathCombine
|
||||||
|
#include <nan.h>
|
||||||
|
|
||||||
#include "path_util.h"
|
#include "path_util.h"
|
||||||
|
|
||||||
namespace path_util {
|
namespace path_util {
|
||||||
|
|
||||||
const wchar_t* to_wstring(const Nan::Utf8String& str) {
|
const wchar_t *to_wstring(const Nan::Utf8String &str) {
|
||||||
const char *bytes = *str;
|
const char *bytes = *str;
|
||||||
unsigned int sizeOfStr = MultiByteToWideChar(CP_UTF8, 0, bytes, -1, NULL, 0);
|
unsigned int sizeOfStr = MultiByteToWideChar(CP_UTF8, 0, bytes, -1, NULL, 0);
|
||||||
wchar_t *output = new wchar_t[sizeOfStr];
|
wchar_t *output = new wchar_t[sizeOfStr];
|
||||||
|
@ -55,7 +55,7 @@ std::wstring get_shell_path(std::wstring filename) {
|
||||||
for (int i = 0; i < paths.size(); ++i) {
|
for (int i = 0; i < paths.size(); ++i) {
|
||||||
std::wstring path = paths[i];
|
std::wstring path = paths[i];
|
||||||
wchar_t searchPath[MAX_PATH];
|
wchar_t searchPath[MAX_PATH];
|
||||||
::PathCombineW(searchPath, const_cast<wchar_t*>(path.c_str()), filename_);
|
::PathCombineW(searchPath, const_cast<wchar_t *>(path.c_str()), filename_);
|
||||||
|
|
||||||
if (searchPath == NULL) {
|
if (searchPath == NULL) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -70,4 +70,4 @@ std::wstring get_shell_path(std::wstring filename) {
|
||||||
return shellpath;
|
return shellpath;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace path_util
|
} // namespace path_util
|
||||||
|
|
|
@ -13,10 +13,10 @@
|
||||||
|
|
||||||
namespace path_util {
|
namespace path_util {
|
||||||
|
|
||||||
const wchar_t* to_wstring(const Nan::Utf8String& str);
|
const wchar_t *to_wstring(const Nan::Utf8String &str);
|
||||||
bool file_exists(std::wstring filename);
|
bool file_exists(std::wstring filename);
|
||||||
std::wstring get_shell_path(std::wstring filename);
|
std::wstring get_shell_path(std::wstring filename);
|
||||||
|
|
||||||
} // namespace path_util
|
} // namespace path_util
|
||||||
|
|
||||||
#endif // NODE_PTY_PATH_UTIL_H_
|
#endif // NODE_PTY_PATH_UTIL_H_
|
||||||
|
|
|
@ -8,9 +8,9 @@
|
||||||
* with pseudo-terminal file descriptors.
|
* with pseudo-terminal file descriptors.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <Shlwapi.h> // PathCombine, PathIsRelative
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <nan.h>
|
#include <nan.h>
|
||||||
#include <Shlwapi.h> // PathCombine, PathIsRelative
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -21,21 +21,21 @@
|
||||||
#include "path_util.h"
|
#include "path_util.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Misc
|
* Misc
|
||||||
*/
|
*/
|
||||||
extern "C" void init(v8::Local<v8::Object>);
|
extern "C" void init(v8::Local<v8::Object>);
|
||||||
|
|
||||||
#define WINPTY_DBG_VARIABLE TEXT("WINPTYDBG")
|
#define WINPTY_DBG_VARIABLE TEXT("WINPTYDBG")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* winpty
|
* winpty
|
||||||
*/
|
*/
|
||||||
static std::vector<winpty_t *> ptyHandles;
|
static std::vector<winpty_t *> ptyHandles;
|
||||||
static volatile LONG ptyCounter;
|
static volatile LONG ptyCounter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helpers
|
* Helpers
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static winpty_t *get_pipe_handle(int handle) {
|
static winpty_t *get_pipe_handle(int handle) {
|
||||||
for (size_t i = 0; i < ptyHandles.size(); ++i) {
|
for (size_t i = 0; i < ptyHandles.size(); ++i) {
|
||||||
|
@ -73,14 +73,15 @@ void throw_winpty_error(const char *generalMsg, winpty_error_ptr_t error_ptr) {
|
||||||
static NAN_METHOD(PtyGetExitCode) {
|
static NAN_METHOD(PtyGetExitCode) {
|
||||||
Nan::HandleScope scope;
|
Nan::HandleScope scope;
|
||||||
|
|
||||||
if (info.Length() != 1 ||
|
if (info.Length() != 1 || !info[0]->IsNumber()) {
|
||||||
!info[0]->IsNumber()) {
|
|
||||||
Nan::ThrowError("Usage: pty.getExitCode(pidHandle)");
|
Nan::ThrowError("Usage: pty.getExitCode(pidHandle)");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
DWORD exitCode = 0;
|
DWORD exitCode = 0;
|
||||||
GetExitCodeProcess((HANDLE)info[0]->IntegerValue(Nan::GetCurrentContext()).FromJust(), &exitCode);
|
GetExitCodeProcess(
|
||||||
|
(HANDLE)info[0]->IntegerValue(Nan::GetCurrentContext()).FromJust(),
|
||||||
|
&exitCode);
|
||||||
|
|
||||||
info.GetReturnValue().Set(Nan::New<v8::Number>(exitCode));
|
info.GetReturnValue().Set(Nan::New<v8::Number>(exitCode));
|
||||||
}
|
}
|
||||||
|
@ -88,8 +89,7 @@ static NAN_METHOD(PtyGetExitCode) {
|
||||||
static NAN_METHOD(PtyGetProcessList) {
|
static NAN_METHOD(PtyGetProcessList) {
|
||||||
Nan::HandleScope scope;
|
Nan::HandleScope scope;
|
||||||
|
|
||||||
if (info.Length() != 1 ||
|
if (info.Length() != 1 || !info[0]->IsNumber()) {
|
||||||
!info[0]->IsNumber()) {
|
|
||||||
Nan::ThrowError("Usage: pty.getProcessList(pid)");
|
Nan::ThrowError("Usage: pty.getProcessList(pid)");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -103,7 +103,8 @@ static NAN_METHOD(PtyGetProcessList) {
|
||||||
}
|
}
|
||||||
int processList[64];
|
int processList[64];
|
||||||
const int processCount = 64;
|
const int processCount = 64;
|
||||||
int actualCount = winpty_get_console_process_list(pc, processList, processCount, nullptr);
|
int actualCount =
|
||||||
|
winpty_get_console_process_list(pc, processList, processCount, nullptr);
|
||||||
|
|
||||||
v8::Local<v8::Array> result = Nan::New<v8::Array>(actualCount);
|
v8::Local<v8::Array> result = Nan::New<v8::Array>(actualCount);
|
||||||
for (uint32_t i = 0; i < actualCount; i++) {
|
for (uint32_t i = 0; i < actualCount; i++) {
|
||||||
|
@ -115,15 +116,11 @@ static NAN_METHOD(PtyGetProcessList) {
|
||||||
static NAN_METHOD(PtyStartProcess) {
|
static NAN_METHOD(PtyStartProcess) {
|
||||||
Nan::HandleScope scope;
|
Nan::HandleScope scope;
|
||||||
|
|
||||||
if (info.Length() != 7 ||
|
if (info.Length() != 7 || !info[0]->IsString() || !info[1]->IsString() ||
|
||||||
!info[0]->IsString() ||
|
!info[2]->IsArray() || !info[3]->IsString() || !info[4]->IsNumber() ||
|
||||||
!info[1]->IsString() ||
|
!info[5]->IsNumber() || !info[6]->IsBoolean()) {
|
||||||
!info[2]->IsArray() ||
|
Nan::ThrowError(
|
||||||
!info[3]->IsString() ||
|
"Usage: pty.startProcess(file, cmdline, env, cwd, cols, rows, debug)");
|
||||||
!info[4]->IsNumber() ||
|
|
||||||
!info[5]->IsNumber() ||
|
|
||||||
!info[6]->IsBoolean()) {
|
|
||||||
Nan::ThrowError("Usage: pty.startProcess(file, cmdline, env, cwd, cols, rows, debug)");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,8 +137,9 @@ static NAN_METHOD(PtyStartProcess) {
|
||||||
|
|
||||||
std::wstringstream envBlock;
|
std::wstringstream envBlock;
|
||||||
|
|
||||||
for(uint32_t i = 0; i < envValues->Length(); i++) {
|
for (uint32_t i = 0; i < envValues->Length(); i++) {
|
||||||
std::wstring envValue(path_util::to_wstring(Nan::Utf8String(Nan::Get(envValues, i).ToLocalChecked())));
|
std::wstring envValue(path_util::to_wstring(
|
||||||
|
Nan::Utf8String(Nan::Get(envValues, i).ToLocalChecked())));
|
||||||
envBlock << envValue << L'\0';
|
envBlock << envValue << L'\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -170,11 +168,12 @@ static NAN_METHOD(PtyStartProcess) {
|
||||||
bool debug = Nan::To<bool>(info[6]).FromJust();
|
bool debug = Nan::To<bool>(info[6]).FromJust();
|
||||||
|
|
||||||
// Enable/disable debugging
|
// Enable/disable debugging
|
||||||
SetEnvironmentVariable(WINPTY_DBG_VARIABLE, debug ? "1" : NULL); // NULL = deletes variable
|
SetEnvironmentVariable(WINPTY_DBG_VARIABLE,
|
||||||
|
debug ? "1" : NULL); // NULL = deletes variable
|
||||||
|
|
||||||
// Create winpty config
|
// Create winpty config
|
||||||
winpty_error_ptr_t error_ptr = nullptr;
|
winpty_error_ptr_t error_ptr = nullptr;
|
||||||
winpty_config_t* winpty_config = winpty_config_new(0, &error_ptr);
|
winpty_config_t *winpty_config = winpty_config_new(0, &error_ptr);
|
||||||
if (winpty_config == nullptr) {
|
if (winpty_config == nullptr) {
|
||||||
throw_winpty_error("Error creating WinPTY config", error_ptr);
|
throw_winpty_error("Error creating WinPTY config", error_ptr);
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
@ -197,7 +196,9 @@ static NAN_METHOD(PtyStartProcess) {
|
||||||
ptyHandles.insert(ptyHandles.end(), pc);
|
ptyHandles.insert(ptyHandles.end(), pc);
|
||||||
|
|
||||||
// Create winpty spawn config
|
// Create winpty spawn config
|
||||||
winpty_spawn_config_t* config = winpty_spawn_config_new(WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN, shellpath.c_str(), cmdline, cwd, env.c_str(), &error_ptr);
|
winpty_spawn_config_t *config = winpty_spawn_config_new(
|
||||||
|
WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN, shellpath.c_str(), cmdline, cwd,
|
||||||
|
env.c_str(), &error_ptr);
|
||||||
if (config == nullptr) {
|
if (config == nullptr) {
|
||||||
throw_winpty_error("Error creating WinPTY spawn config", error_ptr);
|
throw_winpty_error("Error creating WinPTY spawn config", error_ptr);
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
@ -206,7 +207,8 @@ static NAN_METHOD(PtyStartProcess) {
|
||||||
|
|
||||||
// Spawn the new process
|
// Spawn the new process
|
||||||
HANDLE handle = nullptr;
|
HANDLE handle = nullptr;
|
||||||
BOOL spawnSuccess = winpty_spawn(pc, config, &handle, nullptr, nullptr, &error_ptr);
|
BOOL spawnSuccess =
|
||||||
|
winpty_spawn(pc, config, &handle, nullptr, nullptr, &error_ptr);
|
||||||
winpty_spawn_config_free(config);
|
winpty_spawn_config_free(config);
|
||||||
if (!spawnSuccess) {
|
if (!spawnSuccess) {
|
||||||
throw_winpty_error("Unable to start terminal process", error_ptr);
|
throw_winpty_error("Unable to start terminal process", error_ptr);
|
||||||
|
@ -216,20 +218,29 @@ static NAN_METHOD(PtyStartProcess) {
|
||||||
|
|
||||||
// Set return values
|
// Set return values
|
||||||
v8::Local<v8::Object> marshal = Nan::New<v8::Object>();
|
v8::Local<v8::Object> marshal = Nan::New<v8::Object>();
|
||||||
Nan::Set(marshal, Nan::New<v8::String>("innerPid").ToLocalChecked(), Nan::New<v8::Number>((int)GetProcessId(handle)));
|
Nan::Set(marshal, Nan::New<v8::String>("innerPid").ToLocalChecked(),
|
||||||
Nan::Set(marshal, Nan::New<v8::String>("innerPidHandle").ToLocalChecked(), Nan::New<v8::Number>((int)handle));
|
Nan::New<v8::Number>((int)GetProcessId(handle)));
|
||||||
Nan::Set(marshal, Nan::New<v8::String>("pid").ToLocalChecked(), Nan::New<v8::Number>((int)winpty_agent_process(pc)));
|
Nan::Set(marshal, Nan::New<v8::String>("innerPidHandle").ToLocalChecked(),
|
||||||
Nan::Set(marshal, Nan::New<v8::String>("pty").ToLocalChecked(), Nan::New<v8::Number>(InterlockedIncrement(&ptyCounter)));
|
Nan::New<v8::Number>((int)handle));
|
||||||
Nan::Set(marshal, Nan::New<v8::String>("fd").ToLocalChecked(), Nan::New<v8::Number>(-1));
|
Nan::Set(marshal, Nan::New<v8::String>("pid").ToLocalChecked(),
|
||||||
|
Nan::New<v8::Number>((int)winpty_agent_process(pc)));
|
||||||
|
Nan::Set(marshal, Nan::New<v8::String>("pty").ToLocalChecked(),
|
||||||
|
Nan::New<v8::Number>(InterlockedIncrement(&ptyCounter)));
|
||||||
|
Nan::Set(marshal, Nan::New<v8::String>("fd").ToLocalChecked(),
|
||||||
|
Nan::New<v8::Number>(-1));
|
||||||
{
|
{
|
||||||
LPCWSTR coninPipeName = winpty_conin_name(pc);
|
LPCWSTR coninPipeName = winpty_conin_name(pc);
|
||||||
std::wstring coninPipeNameWStr(coninPipeName);
|
std::wstring coninPipeNameWStr(coninPipeName);
|
||||||
std::string coninPipeNameStr(coninPipeNameWStr.begin(), coninPipeNameWStr.end());
|
std::string coninPipeNameStr(coninPipeNameWStr.begin(),
|
||||||
Nan::Set(marshal, Nan::New<v8::String>("conin").ToLocalChecked(), Nan::New<v8::String>(coninPipeNameStr).ToLocalChecked());
|
coninPipeNameWStr.end());
|
||||||
|
Nan::Set(marshal, Nan::New<v8::String>("conin").ToLocalChecked(),
|
||||||
|
Nan::New<v8::String>(coninPipeNameStr).ToLocalChecked());
|
||||||
LPCWSTR conoutPipeName = winpty_conout_name(pc);
|
LPCWSTR conoutPipeName = winpty_conout_name(pc);
|
||||||
std::wstring conoutPipeNameWStr(conoutPipeName);
|
std::wstring conoutPipeNameWStr(conoutPipeName);
|
||||||
std::string conoutPipeNameStr(conoutPipeNameWStr.begin(), conoutPipeNameWStr.end());
|
std::string conoutPipeNameStr(conoutPipeNameWStr.begin(),
|
||||||
Nan::Set(marshal, Nan::New<v8::String>("conout").ToLocalChecked(), Nan::New<v8::String>(conoutPipeNameStr).ToLocalChecked());
|
conoutPipeNameWStr.end());
|
||||||
|
Nan::Set(marshal, Nan::New<v8::String>("conout").ToLocalChecked(),
|
||||||
|
Nan::New<v8::String>(conoutPipeNameStr).ToLocalChecked());
|
||||||
}
|
}
|
||||||
info.GetReturnValue().Set(marshal);
|
info.GetReturnValue().Set(marshal);
|
||||||
|
|
||||||
|
@ -244,9 +255,7 @@ cleanup:
|
||||||
static NAN_METHOD(PtyResize) {
|
static NAN_METHOD(PtyResize) {
|
||||||
Nan::HandleScope scope;
|
Nan::HandleScope scope;
|
||||||
|
|
||||||
if (info.Length() != 3 ||
|
if (info.Length() != 3 || !info[0]->IsNumber() || !info[1]->IsNumber() ||
|
||||||
!info[0]->IsNumber() ||
|
|
||||||
!info[1]->IsNumber() ||
|
|
||||||
!info[2]->IsNumber()) {
|
!info[2]->IsNumber()) {
|
||||||
Nan::ThrowError("Usage: pty.resize(pid, cols, rows)");
|
Nan::ThrowError("Usage: pty.resize(pid, cols, rows)");
|
||||||
return;
|
return;
|
||||||
|
@ -274,15 +283,14 @@ static NAN_METHOD(PtyResize) {
|
||||||
static NAN_METHOD(PtyKill) {
|
static NAN_METHOD(PtyKill) {
|
||||||
Nan::HandleScope scope;
|
Nan::HandleScope scope;
|
||||||
|
|
||||||
if (info.Length() != 2 ||
|
if (info.Length() != 2 || !info[0]->IsNumber() || !info[1]->IsNumber()) {
|
||||||
!info[0]->IsNumber() ||
|
|
||||||
!info[1]->IsNumber()) {
|
|
||||||
Nan::ThrowError("Usage: pty.kill(pid, innerPidHandle)");
|
Nan::ThrowError("Usage: pty.kill(pid, innerPidHandle)");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int handle = info[0]->Int32Value(Nan::GetCurrentContext()).FromJust();
|
int handle = info[0]->Int32Value(Nan::GetCurrentContext()).FromJust();
|
||||||
HANDLE innerPidHandle = (HANDLE)info[1]->Int32Value(Nan::GetCurrentContext()).FromJust();
|
HANDLE innerPidHandle =
|
||||||
|
(HANDLE)info[1]->Int32Value(Nan::GetCurrentContext()).FromJust();
|
||||||
|
|
||||||
winpty_t *pc = get_pipe_handle(handle);
|
winpty_t *pc = get_pipe_handle(handle);
|
||||||
if (pc == nullptr) {
|
if (pc == nullptr) {
|
||||||
|
@ -297,8 +305,8 @@ static NAN_METHOD(PtyKill) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Init
|
* Init
|
||||||
*/
|
*/
|
||||||
|
|
||||||
extern "C" void init(v8::Local<v8::Object> target) {
|
extern "C" void init(v8::Local<v8::Object> target) {
|
||||||
Nan::HandleScope scope;
|
Nan::HandleScope scope;
|
||||||
|
|
Loading…
Reference in a new issue