From b8004555ea89db1423755010ce211c4390d05e18 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sun, 15 Dec 2019 15:41:58 -0600 Subject: [PATCH] Start cleaning up defines in janet.h --- README.md | 3 ++- src/core/io.c | 10 +++------- src/core/os.c | 17 ++++++++++------- src/include/janet.h | 24 +++++++++++++++++++----- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 8806daf6..fb4dd8eb 100644 --- a/README.md +++ b/README.md @@ -155,7 +155,8 @@ ninja -C build install Janet can be hacked on with pretty much any environment you like, but for IDE lovers, [Gnome Builder](https://wiki.gnome.org/Apps/Builder) is probably the best option, as it has excellent meson integration. It also offers code completion -for Janet's C API right out of the box, which is very useful for exploring. +for Janet's C API right out of the box, which is very useful for exploring. VSCode, Vim, +Emacs, and Atom will have syntax packages for the Janet language, though. ## Installation diff --git a/src/core/io.c b/src/core/io.c index 877dd30e..52b4e263 100644 --- a/src/core/io.c +++ b/src/core/io.c @@ -20,18 +20,14 @@ * IN THE SOFTWARE. */ -/* Compiler feature test macros for things */ -#define _DEFAULT_SOURCE -#define _BSD_SOURCE - -#include -#include - #ifndef JANET_AMALG #include #include "util.h" #endif +#include +#include + #ifndef JANET_WINDOWS #include #endif diff --git a/src/core/os.c b/src/core/os.c index cdaa7ead..5dadd93a 100644 --- a/src/core/os.c +++ b/src/core/os.c @@ -75,11 +75,11 @@ static Janet os_which(int32_t argc, Janet *argv) { return janet_ckeywordv(janet_stringify(JANET_OS_NAME)); #elif defined(JANET_WINDOWS) return janet_ckeywordv("windows"); -#elif defined(__APPLE__) +#elif defined(JANET_APPLE) return janet_ckeywordv("macos"); #elif defined(__EMSCRIPTEN__) return janet_ckeywordv("web"); -#elif defined(__linux__) +#elif defined(JANET_LINUX) return janet_ckeywordv("linux"); #elif defined(__FreeBSD__) return janet_ckeywordv("freebsd"); @@ -87,6 +87,8 @@ static Janet os_which(int32_t argc, Janet *argv) { return janet_ckeywordv("netbsd"); #elif defined(__OpenBSD__) return janet_ckeywordv("openbsd"); +#elif defined(JANET_BSD) + return janet_ckeywordv("bsd"); #else return janet_ckeywordv("posix"); #endif @@ -527,11 +529,9 @@ static Janet os_cryptorand(int32_t argc, Janet *argv) { v = v >> 8; } } -#elif defined(__linux__) || defined(__APPLE__) +#elif defined(JANET_LINUX) /* We should be able to call getrandom on linux, but it doesn't seem - to be uniformly supported on linux distros. Macos may support - arc4random_buf, but it needs investigation. - + to be uniformly supported on linux distros. In both cases, use this fallback path for now... */ int rc; int randfd; @@ -549,9 +549,12 @@ static Janet os_cryptorand(int32_t argc, Janet *argv) { n -= nread; } RETRY_EINTR(rc, close(randfd)); -#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) +#elif defined(JANET_BSD) || define(JANET_APPLE) + (void) genericerr; + arc4random_buf(buffer->data + offset, n); #else + (void) genericerr; janet_panic("cryptorand currently unsupported on this platform"); #endif return janet_wrap_buffer(buffer); diff --git a/src/include/janet.h b/src/include/janet.h index ef59cb84..271b4f2c 100644 --- a/src/include/janet.h +++ b/src/include/janet.h @@ -45,6 +45,23 @@ extern "C" { * detection for unsupported platforms */ +/* Check for any flavor of BSD (except apple) */ +#if defined(__FreeBSD__) || defined(__DragonFly__) || \ + defined(__NetBSD__) || defined(__OpenBSD__) +#define JANET_BSD 1 +#define _BSD_SOURCE 1 +#endif + +/* Check for Mac */ +#ifdef __APPLE__ +#define JANET_APPLE 1 +#endif + +/* Check for Linux */ +#ifdef __linux__ +#define JANET_LINUX 1 +#endif + /* Check Unix */ #if defined(_AIX) \ || defined(__APPLE__) /* Darwin */ \ @@ -58,11 +75,8 @@ extern "C" { || defined(__QNXNTO__) \ || defined(sun) || defined(__sun) /* Solaris */ \ || defined(unix) || defined(__unix) || defined(__unix__) -#define JANET_UNIX 1 -/* Enable certain posix features */ -#ifndef _POSIX_C_SOURCE +#define JANET_POSIX 1 #define _POSIX_C_SOURCE 200112L -#endif #elif defined(__EMSCRIPTEN__) #define JANET_WEB 1 #elif defined(WIN32) || defined(_WIN32) @@ -71,7 +85,7 @@ extern "C" { /* Check 64-bit vs 32-bit */ #if ((defined(__x86_64__) || defined(_M_X64)) \ - && (defined(JANET_UNIX) || defined(JANET_WINDOWS))) \ + && (defined(JANET_POSIX) || defined(JANET_WINDOWS))) \ || (defined(_WIN64)) /* Windows 64 bit */ \ || (defined(__ia64__) && defined(__LP64__)) /* Itanium in LP64 mode */ \ || defined(__alpha__) /* DEC Alpha */ \