2018-03-29 00:50:20 +00:00
|
|
|
/*
|
2018-05-18 03:41:20 +00:00
|
|
|
* Copyright (c) 2018 Calvin Rose
|
2018-03-29 00:50:20 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#include <janet/janet.h>
|
2018-03-29 00:50:20 +00:00
|
|
|
#include <stdlib.h>
|
2018-05-13 00:31:28 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2018-05-13 00:31:28 +00:00
|
|
|
#include <Windows.h>
|
2018-05-19 05:09:56 +00:00
|
|
|
#include <direct.h>
|
2018-05-13 00:31:28 +00:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
2018-07-07 01:50:59 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <stdio.h>
|
2018-05-13 00:31:28 +00:00
|
|
|
#endif
|
2018-03-29 00:50:20 +00:00
|
|
|
|
2018-09-23 01:46:50 +00:00
|
|
|
/* For macos */
|
|
|
|
#ifdef __MACH__
|
|
|
|
#include <mach/clock.h>
|
|
|
|
#include <mach/mach.h>
|
|
|
|
#endif
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet os_which(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 0, 0);
|
|
|
|
(void) argv;
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_ckeywordv("windows");
|
2018-08-07 04:54:47 +00:00
|
|
|
#elif __APPLE__
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_ckeywordv("macos");
|
2018-10-17 03:08:26 +00:00
|
|
|
#elif defined(__EMSCRIPTEN__)
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_ckeywordv("web");
|
2018-08-07 04:54:47 +00:00
|
|
|
#else
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_ckeywordv("posix");
|
2018-08-07 04:54:47 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet os_execute(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 1, -1);
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetBuffer *buffer = janet_buffer(10);
|
2019-01-06 01:09:03 +00:00
|
|
|
for (int32_t i = 0; i < argc; i++) {
|
|
|
|
const uint8_t *argstring = janet_getstring(argv, i);
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_bytes(buffer, argstring, janet_string_length(argstring));
|
2019-01-06 01:09:03 +00:00
|
|
|
if (i != argc - 1) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_u8(buffer, ' ');
|
2018-07-07 01:50:59 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_u8(buffer, 0);
|
2018-07-07 01:50:59 +00:00
|
|
|
|
|
|
|
/* Convert to wide chars */
|
|
|
|
wchar_t *sys_str = malloc(buffer->count * sizeof(wchar_t));
|
|
|
|
if (NULL == sys_str) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2018-07-07 01:50:59 +00:00
|
|
|
}
|
|
|
|
int nwritten = MultiByteToWideChar(
|
|
|
|
CP_UTF8,
|
|
|
|
MB_PRECOMPOSED,
|
|
|
|
buffer->data,
|
|
|
|
buffer->count,
|
|
|
|
sys_str,
|
|
|
|
buffer->count);
|
|
|
|
if (nwritten == 0) {
|
|
|
|
free(sys_str);
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panic("could not create process");
|
2018-07-07 01:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
STARTUPINFO si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
|
|
|
|
ZeroMemory(&si, sizeof(si));
|
|
|
|
si.cb = sizeof(si);
|
|
|
|
ZeroMemory(&pi, sizeof(pi));
|
|
|
|
|
2018-11-16 21:24:10 +00:00
|
|
|
// Start the child process.
|
2018-07-07 01:50:59 +00:00
|
|
|
if(!CreateProcess(NULL,
|
2018-08-06 01:13:14 +00:00
|
|
|
(LPSTR) sys_str,
|
2018-07-07 01:50:59 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
FALSE,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&si,
|
|
|
|
&pi)) {
|
|
|
|
free(sys_str);
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panic("could not create process");
|
2018-07-07 01:50:59 +00:00
|
|
|
}
|
|
|
|
free(sys_str);
|
|
|
|
|
|
|
|
// Wait until child process exits.
|
|
|
|
WaitForSingleObject(pi.hProcess, INFINITE);
|
|
|
|
|
2018-07-08 18:22:40 +00:00
|
|
|
// Close process and thread handles.
|
|
|
|
WORD status;
|
2018-08-06 01:13:14 +00:00
|
|
|
GetExitCodeProcess(pi.hProcess, (LPDWORD)&status);
|
2018-07-07 01:50:59 +00:00
|
|
|
CloseHandle(pi.hProcess);
|
|
|
|
CloseHandle(pi.hThread);
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_integer(status);
|
2018-07-07 01:50:59 +00:00
|
|
|
}
|
|
|
|
#else
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet os_execute(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 1, -1);
|
|
|
|
const uint8_t **child_argv = malloc(sizeof(uint8_t *) * (argc + 1));
|
|
|
|
if (NULL == child_argv) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2018-07-07 01:50:59 +00:00
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
for (int32_t i = 0; i < argc; i++) {
|
|
|
|
child_argv[i] = janet_getstring(argv, i);
|
2018-07-07 01:50:59 +00:00
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
child_argv[argc] = NULL;
|
2018-07-07 01:50:59 +00:00
|
|
|
|
|
|
|
/* Fork child process */
|
2018-11-16 07:34:50 +00:00
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid < 0) {
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panic("failed to execute");
|
2018-11-16 07:34:50 +00:00
|
|
|
} else if (pid == 0) {
|
2019-01-06 01:09:03 +00:00
|
|
|
if (-1 == execve((const char *)child_argv[0], (char **)child_argv, NULL)) {
|
2018-07-07 01:50:59 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int status;
|
2018-11-16 07:34:50 +00:00
|
|
|
waitpid(pid, &status, 0);
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_integer(status);
|
2018-07-07 01:50:59 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet os_shell(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 0, 1);
|
|
|
|
const char *cmd = argc
|
|
|
|
? (const char *)janet_getstring(argv, 0)
|
|
|
|
: NULL;
|
2018-03-29 00:50:20 +00:00
|
|
|
int stat = system(cmd);
|
2019-01-06 01:09:03 +00:00
|
|
|
return argc
|
|
|
|
? janet_wrap_integer(stat)
|
|
|
|
: janet_wrap_boolean(stat);
|
2018-03-29 00:50:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet os_getenv(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 1, 1);
|
|
|
|
const uint8_t *k = janet_getstring(argv, 0);
|
2018-06-08 19:58:23 +00:00
|
|
|
const char *cstr = (const char *) k;
|
2018-03-29 00:50:20 +00:00
|
|
|
const char *res = getenv(cstr);
|
2019-01-06 01:09:03 +00:00
|
|
|
return (res && cstr)
|
|
|
|
? janet_cstringv(res)
|
|
|
|
: janet_wrap_nil();
|
2018-03-29 00:50:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet os_setenv(int32_t argc, Janet *argv) {
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2018-06-08 19:58:23 +00:00
|
|
|
#define SETENV(K,V) _putenv_s(K, V)
|
|
|
|
#define UNSETENV(K) _putenv_s(K, "")
|
2018-03-29 00:50:20 +00:00
|
|
|
#else
|
2018-06-08 19:58:23 +00:00
|
|
|
#define SETENV(K,V) setenv(K, V, 1)
|
|
|
|
#define UNSETENV(K) unsetenv(K)
|
|
|
|
#endif
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_arity(argc, 1, 2);
|
|
|
|
const uint8_t *k = janet_getstring(argv, 0);
|
|
|
|
const char *ks = (const char *) k;
|
|
|
|
if (argc == 1 || janet_checktype(argv[1], JANET_NIL)) {
|
2018-06-08 19:58:23 +00:00
|
|
|
UNSETENV(ks);
|
2018-03-29 00:50:20 +00:00
|
|
|
} else {
|
2019-01-06 01:09:03 +00:00
|
|
|
const uint8_t *v = janet_getstring(argv, 1);
|
|
|
|
SETENV(ks, (const char *)v);
|
2018-03-29 00:50:20 +00:00
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_nil();
|
2018-03-29 00:50:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet os_exit(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 0, 1);
|
|
|
|
if (argc == 0) {
|
2018-05-13 00:31:28 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
2019-01-06 01:09:03 +00:00
|
|
|
} else if (janet_checkint(argv[0])) {
|
|
|
|
exit(janet_unwrap_integer(argv[0]));
|
2018-05-13 00:31:28 +00:00
|
|
|
} else {
|
2018-06-08 19:58:23 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2018-05-13 00:31:28 +00:00
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_nil();
|
2018-05-13 00:31:28 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet os_time(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 0, 0);
|
|
|
|
(void) argv;
|
2018-09-23 01:46:50 +00:00
|
|
|
double dtime = (double)(time(NULL));
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_number(dtime);
|
2018-09-23 01:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clock shims */
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2018-09-23 01:46:50 +00:00
|
|
|
static int gettime(struct timespec *spec) {
|
2018-07-09 01:10:15 +00:00
|
|
|
int64_t wintime = 0LL;
|
2018-07-08 23:27:11 +00:00
|
|
|
GetSystemTimeAsFileTime((FILETIME*)&wintime);
|
2018-07-09 01:10:15 +00:00
|
|
|
/* Windows epoch is January 1, 1601 apparently*/
|
|
|
|
wintime -= 116444736000000000LL;
|
2018-07-08 23:27:11 +00:00
|
|
|
spec->tv_sec = wintime / 10000000LL;
|
|
|
|
/* Resolution is 100 nanoseconds. */
|
|
|
|
spec->tv_nsec = wintime % 10000000LL * 100;
|
|
|
|
return 0;
|
|
|
|
}
|
2018-09-23 01:46:50 +00:00
|
|
|
#elif defined(__MACH__)
|
|
|
|
static int gettime(struct timespec *spec) {
|
|
|
|
clock_serv_t cclock;
|
|
|
|
mach_timespec_t mts;
|
|
|
|
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
|
|
|
|
clock_get_time(cclock, &mts);
|
|
|
|
mach_port_deallocate(mach_task_self(), cclock);
|
|
|
|
spec->tv_sec = mts.tv_sec;
|
|
|
|
spec->tv_nsec = mts.tv_nsec;
|
|
|
|
return 0;
|
2018-09-12 01:33:50 +00:00
|
|
|
}
|
2018-11-16 21:24:10 +00:00
|
|
|
#else
|
2018-09-23 01:46:50 +00:00
|
|
|
#define gettime(TV) clock_gettime(CLOCK_MONOTONIC, (TV))
|
|
|
|
#endif
|
2018-09-12 01:33:50 +00:00
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet os_clock(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 0, 0);
|
|
|
|
(void) argv;
|
2018-07-08 23:27:11 +00:00
|
|
|
struct timespec tv;
|
2019-01-06 01:09:03 +00:00
|
|
|
if (gettime(&tv)) janet_panic("could not get time");
|
2018-07-08 23:27:11 +00:00
|
|
|
double dtime = tv.tv_sec + (tv.tv_nsec / 1E9);
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_number(dtime);
|
2018-05-13 00:31:28 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet os_sleep(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 1, 1);
|
|
|
|
double delay = janet_getnumber(argv, 0);
|
|
|
|
if (delay < 0) janet_panic("invalid argument to sleep");
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2018-07-05 16:55:11 +00:00
|
|
|
Sleep((DWORD) (delay * 1000));
|
2018-05-13 00:31:28 +00:00
|
|
|
#else
|
2018-07-07 01:50:59 +00:00
|
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = (time_t) delay;
|
|
|
|
ts.tv_nsec = (delay <= UINT32_MAX)
|
|
|
|
? (long)((delay - ((uint32_t)delay)) * 1000000000)
|
|
|
|
: 0;
|
|
|
|
nanosleep(&ts, NULL);
|
2018-05-13 00:31:28 +00:00
|
|
|
#endif
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_nil();
|
2018-03-29 00:50:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet os_cwd(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 0, 0);
|
|
|
|
(void) argv;
|
2018-05-19 05:09:56 +00:00
|
|
|
char buf[FILENAME_MAX];
|
|
|
|
char *ptr;
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2018-05-19 05:09:56 +00:00
|
|
|
ptr = _getcwd(buf, FILENAME_MAX);
|
|
|
|
#else
|
|
|
|
ptr = getcwd(buf, FILENAME_MAX);
|
|
|
|
#endif
|
2019-01-06 01:09:03 +00:00
|
|
|
if (NULL == ptr) janet_panic("could not get current directory");
|
|
|
|
return janet_cstringv(ptr);
|
2018-05-19 05:09:56 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static const JanetReg cfuns[] = {
|
2018-12-01 03:49:21 +00:00
|
|
|
{"os/which", os_which,
|
|
|
|
"(os/which)\n\n"
|
2018-11-16 07:34:50 +00:00
|
|
|
"Check the current operating system. Returns one of:\n\n"
|
|
|
|
"\t:windows - Microsoft Windows\n"
|
|
|
|
"\t:macos - Apple macos\n"
|
|
|
|
"\t:posix - A POSIX compatible system (default)"
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"os/execute", os_execute,
|
|
|
|
"(os/execute program & args)\n\n"
|
2018-11-16 07:34:50 +00:00
|
|
|
"Execute a program on the system and pass it string arguments. Returns "
|
|
|
|
"the exit status of the program."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"os/shell", os_shell,
|
|
|
|
"(os/shell str)\n\n"
|
2018-11-16 07:34:50 +00:00
|
|
|
"Pass a command string str directly to the system shell."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"os/exit", os_exit,
|
|
|
|
"(os/exit x)\n\n"
|
2018-11-16 07:34:50 +00:00
|
|
|
"Exit from janet with an exit code equal to x. If x is not an integer, "
|
|
|
|
"the exit with status equal the hash of x."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"os/getenv", os_getenv,
|
|
|
|
"(os/getenv variable)\n\n"
|
2018-11-16 07:34:50 +00:00
|
|
|
"Get the string value of an environment variable."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"os/setenv", os_setenv,
|
|
|
|
"(os/setenv variable value)\n\n"
|
2018-11-16 07:34:50 +00:00
|
|
|
"Set an environment variable."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"os/time", os_time,
|
|
|
|
"(os/time)\n\n"
|
2018-11-16 07:34:50 +00:00
|
|
|
"Get the current time expressed as the number of seconds since "
|
|
|
|
"January 1, 1970, the Unix epoch. Returns a real number."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"os/clock", os_clock,
|
|
|
|
"(os/clock)\n\n"
|
2018-11-16 07:34:50 +00:00
|
|
|
"Return the number of seconds since some fixed point in time. The clock "
|
|
|
|
"is guaranteed to be non decreased in real time."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"os/sleep", os_sleep,
|
|
|
|
"(os/sleep nsec)\n\n"
|
2018-11-16 07:34:50 +00:00
|
|
|
"Suspend the program for nsec seconds. 'nsec' can be a real number. Returns "
|
|
|
|
"nil."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"os/cwd", os_cwd,
|
|
|
|
"(os/cwd)\n\n"
|
2018-11-16 07:34:50 +00:00
|
|
|
"Returns the current working directory."
|
|
|
|
},
|
2018-11-15 20:45:41 +00:00
|
|
|
{NULL, NULL, NULL}
|
2018-03-29 00:50:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Module entry point */
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_lib_os(JanetTable *env) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_cfuns(env, NULL, cfuns);
|
2018-03-29 00:50:20 +00:00
|
|
|
}
|