mirror of
https://github.com/janet-lang/janet
synced 2026-04-20 13:51:28 +00:00
Compare commits
5 Commits
v1.41.2
...
posix-spaw
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a62d1c35d4 | ||
|
|
9a737d1fc4 | ||
|
|
03672cc82b | ||
|
|
73af64e6ff | ||
|
|
2262759751 |
@@ -55,6 +55,7 @@
|
|||||||
#include <sys/utime.h>
|
#include <sys/utime.h>
|
||||||
#include <io.h>
|
#include <io.h>
|
||||||
#include <process.h>
|
#include <process.h>
|
||||||
|
#define JANET_SPAWN_CHDIR
|
||||||
#else
|
#else
|
||||||
#include <spawn.h>
|
#include <spawn.h>
|
||||||
#include <utime.h>
|
#include <utime.h>
|
||||||
@@ -73,6 +74,20 @@ extern char **environ;
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Detect availability of posix_spawn_file_actions_addchdir_np. Since
|
||||||
|
* this doesn't seem to follow any standard, just a common extension, we
|
||||||
|
* must enumerate supported systems for availability. Define JANET_SPAWN_NO_CHDIR
|
||||||
|
* to disable this. */
|
||||||
|
#ifndef JANET_SPAWN_NO_CHDIR
|
||||||
|
#ifdef __GLIBC__
|
||||||
|
#define JANET_SPAWN_CHDIR
|
||||||
|
#elif defined(JANET_APPLE) /* Some older versions may not work here. */
|
||||||
|
#define JANET_SPAWN_CHDIR
|
||||||
|
#elif defined(__FreeBSD__) /* Not all BSDs work, for example openBSD doesn't seem to support this */
|
||||||
|
#define JANET_SPAWN_CHDIR
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Not POSIX, but all Unixes but Solaris have this function. */
|
/* Not POSIX, but all Unixes but Solaris have this function. */
|
||||||
#if defined(JANET_POSIX) && !defined(__sun)
|
#if defined(JANET_POSIX) && !defined(__sun)
|
||||||
time_t timegm(struct tm *tm);
|
time_t timegm(struct tm *tm);
|
||||||
@@ -1182,6 +1197,21 @@ static Janet os_execute_impl(int32_t argc, Janet *argv, JanetExecuteMode mode) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Optional working directory. Available for both os/execute and os/spawn. */
|
||||||
|
const char *chdir_path = NULL;
|
||||||
|
if (argc > 2) {
|
||||||
|
JanetDictView tab = janet_getdictionary(argv, 2);
|
||||||
|
Janet workdir = janet_dictionary_get(tab.kvs, tab.cap, janet_ckeywordv("cd"));
|
||||||
|
if (janet_checktype(workdir, JANET_STRING)) {
|
||||||
|
chdir_path = (const char *) janet_unwrap_string(workdir);
|
||||||
|
#ifndef JANET_SPAWN_CHDIR
|
||||||
|
janet_panicf(":cd argument not supported on this system - %s", chdir_path);
|
||||||
|
#endif
|
||||||
|
} else if (!janet_checktype(workdir, JANET_NIL)) {
|
||||||
|
janet_panicf("expected string for :cd argumnet, got %v", workdir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Clean up if any of the pipes have any issues */
|
/* Clean up if any of the pipes have any issues */
|
||||||
if (pipe_errflag) {
|
if (pipe_errflag) {
|
||||||
if (pipe_in != JANET_HANDLE_NONE) close_handle(pipe_in);
|
if (pipe_in != JANET_HANDLE_NONE) close_handle(pipe_in);
|
||||||
@@ -1196,6 +1226,7 @@ static Janet os_execute_impl(int32_t argc, Janet *argv, JanetExecuteMode mode) {
|
|||||||
SECURITY_ATTRIBUTES saAttr;
|
SECURITY_ATTRIBUTES saAttr;
|
||||||
PROCESS_INFORMATION processInfo;
|
PROCESS_INFORMATION processInfo;
|
||||||
STARTUPINFO startupInfo;
|
STARTUPINFO startupInfo;
|
||||||
|
LPCSTR lpCurrentDirectory = NULL;
|
||||||
memset(&saAttr, 0, sizeof(saAttr));
|
memset(&saAttr, 0, sizeof(saAttr));
|
||||||
memset(&processInfo, 0, sizeof(processInfo));
|
memset(&processInfo, 0, sizeof(processInfo));
|
||||||
memset(&startupInfo, 0, sizeof(startupInfo));
|
memset(&startupInfo, 0, sizeof(startupInfo));
|
||||||
@@ -1212,6 +1243,10 @@ static Janet os_execute_impl(int32_t argc, Janet *argv, JanetExecuteMode mode) {
|
|||||||
}
|
}
|
||||||
const char *path = (const char *) janet_unwrap_string(exargs.items[0]);
|
const char *path = (const char *) janet_unwrap_string(exargs.items[0]);
|
||||||
|
|
||||||
|
if (chdir_path != NULL) {
|
||||||
|
lpCurrentDirectory = chdir_path;
|
||||||
|
}
|
||||||
|
|
||||||
/* Do IO redirection */
|
/* Do IO redirection */
|
||||||
|
|
||||||
if (pipe_in != JANET_HANDLE_NONE) {
|
if (pipe_in != JANET_HANDLE_NONE) {
|
||||||
@@ -1248,7 +1283,7 @@ static Janet os_execute_impl(int32_t argc, Janet *argv, JanetExecuteMode mode) {
|
|||||||
TRUE, /* handle inheritance */
|
TRUE, /* handle inheritance */
|
||||||
0, /* flags */
|
0, /* flags */
|
||||||
use_environ ? NULL : envp, /* pass in environment */
|
use_environ ? NULL : envp, /* pass in environment */
|
||||||
NULL, /* use parents starting directory */
|
lpCurrentDirectory,
|
||||||
&startupInfo,
|
&startupInfo,
|
||||||
&processInfo)) {
|
&processInfo)) {
|
||||||
cp_failed = 1;
|
cp_failed = 1;
|
||||||
@@ -1305,6 +1340,15 @@ static Janet os_execute_impl(int32_t argc, Janet *argv, JanetExecuteMode mode) {
|
|||||||
/* Posix spawn setup */
|
/* Posix spawn setup */
|
||||||
posix_spawn_file_actions_t actions;
|
posix_spawn_file_actions_t actions;
|
||||||
posix_spawn_file_actions_init(&actions);
|
posix_spawn_file_actions_init(&actions);
|
||||||
|
#ifdef JANET_SPAWN_CHDIR
|
||||||
|
if (chdir_path != NULL) {
|
||||||
|
#ifdef JANET_SPAWN_CHDIR_NO_NP
|
||||||
|
posix_spawn_file_actions_addchdir(&actions, chdir_path);
|
||||||
|
#else
|
||||||
|
posix_spawn_file_actions_addchdir_np(&actions, chdir_path);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
#endif
|
||||||
if (pipe_in != JANET_HANDLE_NONE) {
|
if (pipe_in != JANET_HANDLE_NONE) {
|
||||||
posix_spawn_file_actions_adddup2(&actions, pipe_in, 0);
|
posix_spawn_file_actions_adddup2(&actions, pipe_in, 0);
|
||||||
posix_spawn_file_actions_addclose(&actions, pipe_in);
|
posix_spawn_file_actions_addclose(&actions, pipe_in);
|
||||||
|
|||||||
Reference in New Issue
Block a user