Add several configurable options - #379

This commit is contained in:
Calvin Rose 2020-05-09 12:00:01 -05:00
parent 6b986fecb0
commit b1f76139a7
9 changed files with 62 additions and 42 deletions

View File

@ -2575,6 +2575,9 @@
(def- importers {'import true 'import* true 'use true 'dofile true 'require true})
# conditional compilation for reduced os
(def- getenv-alias (if-let [entry (in _env 'os/getenv)] (entry :value) (fn [&])))
(defn cli-main
"Entrance for the Janet CLI tool. Call this functions with the command line
arguments as an array or tuple of strings to invoke the CLI interface."
@ -2592,8 +2595,8 @@
(var *debug* false)
(var *compile-only* false)
(if-let [jp (os/getenv "JANET_PATH")] (setdyn :syspath jp))
(if-let [jp (os/getenv "JANET_HEADERPATH")] (setdyn :headerpath jp))
(if-let [jp (getenv-alias "JANET_PATH")] (setdyn :syspath jp))
(if-let [jp (getenv-alias "JANET_HEADERPATH")] (setdyn :headerpath jp))
# Flag handlers
(def handlers
@ -2708,7 +2711,7 @@
(put _env 'is-safe-def nil)
(put _env 'safe-forms nil)
(put _env 'importers nil)
(put _env 'getenv-alias nil)
###
###

View File

@ -45,15 +45,20 @@
/* #define JANET_NO_DOCSTRINGS */
/* #define JANET_NO_SOURCEMAPS */
/* #define JANET_REDUCED_OS */
/* Other settings */
/* #define JANET_NO_PROCESSES */
/* #define JANET_NO_ASSEMBLER */
/* #define JANET_NO_PEG */
/* #define JANET_NO_NET */
/* #define JANET_NO_TYPED_ARRAY */
/* #define JANET_NO_INT_TYPES */
/* Other settings */
/* #define JANET_NO_PRF */
/* #define JANET_NO_UTC_MKTIME */
/* #define JANET_NO_SYMLINKS */
/* #define JANET_OUT_OF_MEMORY do { printf("janet out of memory\n"); exit(1); } while (0) */
/* #define JANET_EXIT(msg) do { printf("C assert failed executing janet: %s\n", msg); exit(1); } while (0) */
/* #define JANET_TOP_LEVEL_SIGNAL(msg) call_my_function((msg), stderr) */
/* #define JANET_RECURSION_GUARD 1024 */
/* #define JANET_MAX_PROTO_DEPTH 200 */
/* #define JANET_MAX_MACRO_EXPAND 200 */

View File

@ -27,6 +27,15 @@
#include "fiber.h"
#endif
JANET_NO_RETURN static void janet_top_level_signal(const char *msg) {
#ifdef JANET_TOP_LEVEL_SIGNAL
JANET_TOP_LEVEL_SIGNAL(msg);
#else
fputs(msg, stdout);
exit(1);
#endif
}
void janet_signalv(JanetSignal sig, Janet message) {
if (janet_vm_return_reg != NULL) {
*janet_vm_return_reg = message;
@ -37,8 +46,8 @@ void janet_signalv(JanetSignal sig, Janet message) {
longjmp(*janet_vm_jmp_buf, sig);
#endif
} else {
fputs((const char *)janet_formatc("janet top level signal - %v\n", message), stdout);
exit(1);
const char *str = (const char *)janet_formatc("janet top level signal - %v\n", message);
janet_top_level_signal(str);
}
}

View File

@ -533,7 +533,7 @@ void *janet_srealloc(void *mem, size_t size) {
if (i == 0) break;
}
}
janet_exit("invalid janet_srealloc");
JANET_EXIT("invalid janet_srealloc");
}
void janet_sfinalizer(void *mem, JanetScratchFinalizer finalizer) {
@ -554,5 +554,5 @@ void janet_sfree(void *mem) {
if (i == 0) break;
}
}
janet_exit("invalid janet_sfree");
JANET_EXIT("invalid janet_sfree");
}

View File

@ -93,20 +93,13 @@ static Janet makef(FILE *f, int flags) {
/* While we would like fopen to set cloexec by default (like O_CLOEXEC) with the e flag, that is
* not standard. */
if (!(flags & JANET_FILE_NOT_CLOSEABLE))
fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
#endif
return janet_wrap_abstract(iof);
}
/* Open a process */
#ifdef __EMSCRIPTEN__
static Janet cfun_io_popen(int32_t argc, Janet *argv) {
(void) argc;
(void) argv;
janet_panic("not implemented on this platform");
return janet_wrap_nil();
}
#else
#ifndef JANET_NO_PROCESSES
static Janet cfun_io_popen(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
const uint8_t *fname = janet_getstring(argv, 0);
@ -655,6 +648,7 @@ static const JanetReg io_cfuns[] = {
"for the relative number of bytes to seek in the file. n may be a real "
"number to handle large files of more the 4GB. Returns the file handle.")
},
#ifndef JANET_NO_PROCESSES
{
"file/popen", cfun_io_popen,
JDOC("(file/popen path &opt mode)\n\n"
@ -663,6 +657,7 @@ static const JanetReg io_cfuns[] = {
"process can be read from the file. In :w mode, the stdin of the process "
"can be written to. Returns the new file.")
},
#endif
{NULL, NULL, NULL}
};

View File

@ -182,18 +182,9 @@ static Janet os_exit(int32_t argc, Janet *argv) {
return janet_wrap_nil();
}
#ifdef JANET_REDUCED_OS
/* Provide a dud os/getenv so boot.janet and init.janet work, but nothing else */
static Janet os_getenv(int32_t argc, Janet *argv) {
(void) argv;
janet_arity(argc, 1, 2);
return janet_wrap_nil();
}
#else
/* Provide full os functionality */
#ifndef JANET_REDUCED_OS
#ifndef JANET_NO_PROCESSES
/* Get env for os_execute */
static char **os_execute_env(int32_t argc, const Janet *argv) {
char **envp = NULL;
@ -444,6 +435,8 @@ static Janet os_shell(int32_t argc, Janet *argv) {
: janet_wrap_boolean(stat);
}
#endif /* JANET_NO_PROCESSES */
static Janet os_environ(int32_t argc, Janet *argv) {
(void) argv;
janet_fixarity(argc, 0);
@ -764,8 +757,8 @@ static Janet os_mktime(int32_t argc, Janet *argv) {
t = mktime(&t_info);
} else {
/* utc time */
#ifdef __sun
janet_panic("os/mktime UTC not supported on Solaris");
#ifdef JANET_NO_UTC_MKTIME
janet_panic("os/mktime UTC not supported on this platform");
return janet_wrap_nil();
#else
t = timegm(&t_info);
@ -779,6 +772,12 @@ static Janet os_mktime(int32_t argc, Janet *argv) {
return janet_wrap_number((double)t);
}
#ifdef JANET_NO_SYMLINKS
#define j_symlink link
#else
#define j_symlink symlink
#endif
static Janet os_link(int32_t argc, Janet *argv) {
janet_arity(argc, 2, 3);
#ifdef JANET_WINDOWS
@ -789,7 +788,7 @@ static Janet os_link(int32_t argc, Janet *argv) {
#else
const char *oldpath = janet_getcstring(argv, 0);
const char *newpath = janet_getcstring(argv, 1);
int res = ((argc == 3 && janet_truthy(argv[2])) ? symlink : link)(oldpath, newpath);
int res = ((argc == 3 && janet_truthy(argv[2])) ? j_symlink : link)(oldpath, newpath);
if (-1 == res) janet_panicf("%s: %s -> %s", strerror(errno), oldpath, newpath);
return janet_wrap_nil();
#endif
@ -805,12 +804,14 @@ static Janet os_symlink(int32_t argc, Janet *argv) {
#else
const char *oldpath = janet_getcstring(argv, 0);
const char *newpath = janet_getcstring(argv, 1);
int res = symlink(oldpath, newpath);
int res = j_symlink(oldpath, newpath);
if (-1 == res) janet_panicf("%s: %s -> %s", strerror(errno), oldpath, newpath);
return janet_wrap_nil();
#endif
}
#undef j_symlink
static Janet os_mkdir(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
const char *path = janet_getcstring(argv, 0);
@ -1264,11 +1265,6 @@ static const JanetReg os_cfuns[] = {
"\t:netbsd\n"
"\t:posix - A POSIX compatible system (default)")
},
{
"os/getenv", os_getenv,
JDOC("(os/getenv variable &opt dflt)\n\n"
"Get the string value of an environment variable.")
},
{
"os/arch", os_arch,
JDOC("(os/arch)\n\n"
@ -1287,6 +1283,11 @@ static const JanetReg os_cfuns[] = {
JDOC("(os/environ)\n\n"
"Get a copy of the os environment table.")
},
{
"os/getenv", os_getenv,
JDOC("(os/getenv variable &opt dflt)\n\n"
"Get the string value of an environment variable.")
},
{
"os/dir", os_dir,
JDOC("(os/dir dir &opt array)\n\n"
@ -1377,6 +1378,7 @@ static const JanetReg os_cfuns[] = {
JDOC("(os/readlink path)\n\n"
"Read the contents of a symbolic link. Does not work on Windows.\n")
},
#ifndef JANET_NO_PROCESSES
{
"os/execute", os_execute,
JDOC("(os/execute args &opts flags env)\n\n"
@ -1394,6 +1396,7 @@ static const JanetReg os_cfuns[] = {
JDOC("(os/shell str)\n\n"
"Pass a command string str directly to the system shell.")
},
#endif
{
"os/setenv", os_setenv,
JDOC("(os/setenv variable value)\n\n"

View File

@ -145,7 +145,7 @@ void janetc_regalloc_free(JanetcRegisterAllocator *ra, int32_t reg) {
int32_t janetc_regalloc_temp(JanetcRegisterAllocator *ra, JanetcRegisterTemp nth) {
int32_t oldmax = ra->max;
if (ra->regtemps & (1 << nth)) {
janet_exit("regtemp already allocated");
JANET_EXIT("regtemp already allocated");
}
ra->regtemps |= 1 << nth;
int32_t reg = janetc_regalloc_1(ra);

View File

@ -32,9 +32,9 @@
#include <errno.h>
/* Handle runtime errors */
#ifndef janet_exit
#ifndef JANET_EXIT
#include <stdio.h>
#define janet_exit(m) do { \
#define JANET_EXIT(m) do { \
fprintf(stderr, "C runtime error at line %d in file %s: %s\n",\
__LINE__,\
__FILE__,\
@ -44,7 +44,7 @@
#endif
#define janet_assert(c, m) do { \
if (!(c)) janet_exit((m)); \
if (!(c)) JANET_EXIT((m)); \
} while (0)
/* What to do when out of memory */

View File

@ -132,6 +132,11 @@ extern "C" {
#define JANET_NO_DYNAMIC_MODULES
#endif
/* Check sun */
#ifdef __sun
#define JANET_NO_UTC_MKTIME
#endif
/* Define how global janet state is declared */
#ifdef JANET_SINGLE_THREADED
#define JANET_THREAD_LOCAL