summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt2
-rw-r--r--include/endian.h23
-rw-r--r--include/ucode/platform.h79
-rw-r--r--include/ucode/types.h3
-rw-r--r--lexer.c2
-rw-r--r--lib.c209
-rw-r--r--lib/fs.c5
-rw-r--r--lib/nl80211.c2
-rw-r--r--lib/rtnl.c2
-rw-r--r--lib/uloop.c6
-rw-r--r--platform.c242
-rw-r--r--program.c2
-rw-r--r--source.c2
-rw-r--r--types.c1
-rw-r--r--vallist.c2
-rw-r--r--vm.c35
16 files changed, 344 insertions, 273 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d8dd34a..280cbb4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -50,7 +50,7 @@ INCLUDE(FindPkgConfig)
PKG_CHECK_MODULES(JSONC REQUIRED json-c)
INCLUDE_DIRECTORIES(${JSONC_INCLUDE_DIRS})
-SET(UCODE_SOURCES lexer.c lib.c vm.c chunk.c vallist.c compiler.c source.c types.c program.c)
+SET(UCODE_SOURCES lexer.c lib.c vm.c chunk.c vallist.c compiler.c source.c types.c program.c platform.c)
ADD_LIBRARY(libucode SHARED ${UCODE_SOURCES})
SET(SOVERSION 0 CACHE STRING "Override ucode library version")
SET_TARGET_PROPERTIES(libucode PROPERTIES OUTPUT_NAME ucode SOVERSION ${SOVERSION})
diff --git a/include/endian.h b/include/endian.h
deleted file mode 100644
index 198cf7c..0000000
--- a/include/endian.h
+++ /dev/null
@@ -1,23 +0,0 @@
-#ifdef __APPLE__
-
-# include <machine/endian.h>
-# include <libkern/OSByteOrder.h>
-
-# define htobe16(x) OSSwapHostToBigInt16(x)
-# define htole16(x) OSSwapHostToLittleInt16(x)
-# define be16toh(x) OSSwapBigToHostInt16(x)
-# define le16toh(x) OSSwapLittleToHostInt16(x)
-
-# define htobe32(x) OSSwapHostToBigInt32(x)
-# define htole32(x) OSSwapHostToLittleInt32(x)
-# define be32toh(x) OSSwapBigToHostInt32(x)
-# define le32toh(x) OSSwapLittleToHostInt32(x)
-
-# define htobe64(x) OSSwapHostToBigInt64(x)
-# define htole64(x) OSSwapHostToLittleInt64(x)
-# define be64toh(x) OSSwapBigToHostInt64(x)
-# define le64toh(x) OSSwapLittleToHostInt64(x)
-
-#else
-# include_next <endian.h>
-#endif
diff --git a/include/ucode/platform.h b/include/ucode/platform.h
new file mode 100644
index 0000000..52eb391
--- /dev/null
+++ b/include/ucode/platform.h
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2023 Jo-Philipp Wich <jo@mein.io>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef UCODE_PLATFORM_H
+#define UCODE_PLATFORM_H
+
+#include <signal.h>
+#include <fcntl.h>
+#include <time.h>
+
+#include "ucode/util.h"
+
+#ifdef NSIG
+# define UC_SYSTEM_SIGNAL_COUNT NSIG
+#else
+# define UC_SYSTEM_SIGNAL_COUNT (_SIGMAX + 1)
+#endif
+
+extern const char *uc_system_signal_names[];
+
+#if defined(__linux__)
+# include <endian.h>
+# include <sys/sysmacros.h>
+#elif defined(__APPLE__)
+# include <unistd.h>
+# include <crt_externs.h>
+# include <machine/endian.h>
+# include <libkern/OSByteOrder.h>
+
+# define htobe16(x) OSSwapHostToBigInt16(x)
+# define htole16(x) OSSwapHostToLittleInt16(x)
+# define be16toh(x) OSSwapBigToHostInt16(x)
+# define le16toh(x) OSSwapLittleToHostInt16(x)
+
+# define htobe32(x) OSSwapHostToBigInt32(x)
+# define htole32(x) OSSwapHostToLittleInt32(x)
+# define be32toh(x) OSSwapBigToHostInt32(x)
+# define le32toh(x) OSSwapLittleToHostInt32(x)
+
+# define htobe64(x) OSSwapHostToBigInt64(x)
+# define htole64(x) OSSwapHostToLittleInt64(x)
+# define be64toh(x) OSSwapBigToHostInt64(x)
+# define le64toh(x) OSSwapLittleToHostInt64(x)
+
+# define environ (*_NSGetEnviron())
+
+__hidden int pipe2(int[2], int);
+__hidden int sigtimedwait(const sigset_t *, siginfo_t *, const struct timespec *);
+
+static inline int
+execvpe(const char *program, char **argv, char **envp)
+{
+ char **saved = environ;
+ int rc;
+
+ environ = envp;
+ rc = execvp(program, argv);
+ environ = saved;
+
+ return rc;
+}
+#else
+# error Unsupported platform
+#endif
+
+#endif /* UCODE_PLATFORM_H */
diff --git a/include/ucode/types.h b/include/ucode/types.h
index 641c469..7fcc7f1 100644
--- a/include/ucode/types.h
+++ b/include/ucode/types.h
@@ -24,6 +24,7 @@
#include <json-c/json.h>
#include "util.h"
+#include "platform.h"
/* Value types and generic value header */
@@ -314,7 +315,7 @@ struct uc_vm {
uc_exception_handler_t *exhandler;
FILE *output;
struct {
- uint64_t raised[((NSIG + 63) & ~63) / 64];
+ uint64_t raised[((UC_SYSTEM_SIGNAL_COUNT + 63) & ~63) / 64];
uc_value_t *handler;
struct sigaction sa;
int sigpipe[2];
diff --git a/lexer.c b/lexer.c
index 934a531..28188c3 100644
--- a/lexer.c
+++ b/lexer.c
@@ -23,11 +23,11 @@
#include <regex.h>
#include <math.h>
#include <errno.h>
-#include <endian.h>
#include "ucode/vm.h"
#include "ucode/lib.h"
#include "ucode/lexer.h"
+#include "ucode/platform.h"
struct keyword {
unsigned type;
diff --git a/lib.c b/lib.c
index b06c02b..a16190c 100644
--- a/lib.c
+++ b/lib.c
@@ -50,6 +50,7 @@
#include "ucode/lib.h"
#include "ucode/source.h"
#include "ucode/program.h"
+#include "ucode/platform.h"
static void
format_context_line(uc_stringbuf_t *buf, const char *line, size_t off, bool compact)
@@ -3734,107 +3735,6 @@ uc_warn(uc_vm_t *vm, size_t nargs)
return uc_print_common(vm, nargs, stderr);
}
-#ifdef __APPLE__
-/*
- * sigtimedwait() implementation based on
- * https://comp.unix.programmer.narkive.com/rEDH0sPT/sigtimedwait-implementation
- * and
- * https://github.com/wahern/lunix/blob/master/src/unix.c
- */
-static void
-sigtimedwait_consume_signal(int signo)
-{
-}
-
-static int
-sigtimedwait(const sigset_t *set, siginfo_t *info, const struct timespec *timeout)
-{
- struct timespec elapsed = { 0, 0 }, sleep, rem;
- sigset_t pending, unblock, omask;
- struct sigaction sa, osa;
- int signo;
- bool lt;
-
- while (true) {
- sigemptyset(&pending);
- sigpending(&pending);
-
- for (signo = 1; signo < NSIG; signo++) {
- if (!sigismember(set, signo) || !sigismember(&pending, signo))
- continue;
-
- sa.sa_handler = sigtimedwait_consume_signal;
- sa.sa_flags = 0;
- sigfillset(&sa.sa_mask);
-
- sigaction(signo, &sa, &osa);
-
- sigemptyset(&unblock);
- sigaddset(&unblock, signo);
- sigprocmask(SIG_UNBLOCK, &unblock, &omask);
- sigprocmask(SIG_SETMASK, &omask, NULL);
-
- sigaction(signo, &osa, NULL);
-
- if (info) {
- memset(info, 0, sizeof(*info));
- info->si_signo = signo;
- }
-
- return signo;
- }
-
- sleep.tv_sec = 0;
- sleep.tv_nsec = 200000000L; /* 2/10th second */
- rem = sleep;
-
- if (nanosleep(&sleep, &rem) == 0) {
- elapsed.tv_sec += sleep.tv_sec;
- elapsed.tv_nsec += sleep.tv_nsec;
-
- if (elapsed.tv_nsec > 1000000000) {
- elapsed.tv_sec++;
- elapsed.tv_nsec -= 1000000000;
- }
- }
- else if (errno == EINTR) {
- sleep.tv_sec -= rem.tv_sec;
- sleep.tv_nsec -= rem.tv_nsec;
-
- if (sleep.tv_nsec < 0) {
- sleep.tv_sec--;
- sleep.tv_nsec += 1000000000;
- }
-
- elapsed.tv_sec += sleep.tv_sec;
- elapsed.tv_nsec += sleep.tv_nsec;
-
- if (elapsed.tv_nsec > 1000000000) {
- elapsed.tv_sec++;
- elapsed.tv_nsec -= 1000000000;
- }
- }
- else {
- return errno;
- }
-
- lt = timeout
- ? ((elapsed.tv_sec == timeout->tv_sec)
- ? (elapsed.tv_nsec < timeout->tv_nsec)
- : (elapsed.tv_sec < timeout->tv_sec))
- : true;
-
- if (!lt)
- break;
- }
-
- errno = EAGAIN;
-
- return -1;
-}
-
-#endif
-
/**
* Executes the given command, waits for completion, and returns the resulting
* exit code.
@@ -5501,7 +5401,6 @@ uc_loadstring(uc_vm_t *vm, size_t nargs)
* @example
* loadfile("./templates/example.uc"); // function main() { ... }
*/
-
static uc_value_t *
uc_loadfile(uc_vm_t *vm, size_t nargs)
{
@@ -5646,100 +5545,6 @@ uc_callfunc(uc_vm_t *vm, size_t nargs)
return res;
}
-
-static const char *signal_names[] = {
-#if defined(SIGINT)
- [SIGINT] = "INT",
-#endif
-#if defined(SIGILL)
- [SIGILL] = "ILL",
-#endif
-#if defined(SIGABRT)
- [SIGABRT] = "ABRT",
-#endif
-#if defined(SIGFPE)
- [SIGFPE] = "FPE",
-#endif
-#if defined(SIGSEGV)
- [SIGSEGV] = "SEGV",
-#endif
-#if defined(SIGTERM)
- [SIGTERM] = "TERM",
-#endif
-#if defined(SIGHUP)
- [SIGHUP] = "HUP",
-#endif
-#if defined(SIGQUIT)
- [SIGQUIT] = "QUIT",
-#endif
-#if defined(SIGTRAP)
- [SIGTRAP] = "TRAP",
-#endif
-#if defined(SIGKILL)
- [SIGKILL] = "KILL",
-#endif
-#if defined(SIGPIPE)
- [SIGPIPE] = "PIPE",
-#endif
-#if defined(SIGALRM)
- [SIGALRM] = "ALRM",
-#endif
-#if defined(SIGSTKFLT)
- [SIGSTKFLT] = "STKFLT",
-#endif
-#if defined(SIGPWR)
- [SIGPWR] = "PWR",
-#endif
-#if defined(SIGBUS)
- [SIGBUS] = "BUS",
-#endif
-#if defined(SIGSYS)
- [SIGSYS] = "SYS",
-#endif
-#if defined(SIGURG)
- [SIGURG] = "URG",
-#endif
-#if defined(SIGSTOP)
- [SIGSTOP] = "STOP",
-#endif
-#if defined(SIGTSTP)
- [SIGTSTP] = "TSTP",
-#endif
-#if defined(SIGCONT)
- [SIGCONT] = "CONT",
-#endif
-#if defined(SIGCHLD)
- [SIGCHLD] = "CHLD",
-#endif
-#if defined(SIGTTIN)
- [SIGTTIN] = "TTIN",
-#endif
-#if defined(SIGTTOU)
- [SIGTTOU] = "TTOU",
-#endif
-#if defined(SIGPOLL)
- [SIGPOLL] = "POLL",
-#endif
-#if defined(SIGXFSZ)
- [SIGXFSZ] = "XFSZ",
-#endif
-#if defined(SIGXCPU)
- [SIGXCPU] = "XCPU",
-#endif
-#if defined(SIGVTALRM)
- [SIGVTALRM] = "VTALRM",
-#endif
-#if defined(SIGPROF)
- [SIGPROF] = "PROF",
-#endif
-#if defined(SIGUSR1)
- [SIGUSR1] = "USR1",
-#endif
-#if defined(SIGUSR2)
- [SIGUSR2] = "USR2",
-#endif
-};
-
/**
* Set or query process signal handler function.
*
@@ -5822,7 +5627,10 @@ uc_signal(uc_vm_t *vm, size_t nargs)
if (ucv_type(signame) == UC_INTEGER) {
sig = (int)ucv_int64_get(signame);
- if (errno || sig >= (int)ARRAY_SIZE(signal_names) || !signal_names[sig])
+ if (errno || sig < 0 || sig >= UC_SYSTEM_SIGNAL_COUNT)
+ return NULL;
+
+ if (!uc_system_signal_names[sig])
return NULL;
}
else if (ucv_type(signame) == UC_STRING) {
@@ -5831,11 +5639,12 @@ uc_signal(uc_vm_t *vm, size_t nargs)
if (!strncasecmp(sigstr, "SIG", 3))
sigstr += 3;
- for (sig = 0; sig < (int)ARRAY_SIZE(signal_names); sig++)
- if (signal_names[sig] && !strcasecmp(signal_names[sig], sigstr))
+ for (sig = 0; sig < UC_SYSTEM_SIGNAL_COUNT; sig++)
+ if (uc_system_signal_names[sig] &&
+ !strcasecmp(uc_system_signal_names[sig], sigstr))
break;
- if (sig == (int)ARRAY_SIZE(signal_names))
+ if (sig == UC_SYSTEM_SIGNAL_COUNT)
return NULL;
}
else {
diff --git a/lib/fs.c b/lib/fs.c
index e00f7fa..0f03ad4 100644
--- a/lib/fs.c
+++ b/lib/fs.c
@@ -60,11 +60,8 @@
#include <limits.h>
#include <fcntl.h>
-#ifndef __APPLE__
-#include <sys/sysmacros.h> /* major(), minor() */
-#endif
-
#include "ucode/module.h"
+#include "ucode/platform.h"
#define err_return(err) do { last_error = err; return NULL; } while(0)
diff --git a/lib/nl80211.c b/lib/nl80211.c
index c7bd5fa..49aea32 100644
--- a/lib/nl80211.c
+++ b/lib/nl80211.c
@@ -24,7 +24,6 @@ limitations under the License.
#include <limits.h>
#include <math.h>
#include <assert.h>
-#include <endian.h>
#include <fcntl.h>
#include <poll.h>
@@ -43,6 +42,7 @@ limitations under the License.
#include <libubox/uloop.h>
#include "ucode/module.h"
+#include "ucode/platform.h"
#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
diff --git a/lib/rtnl.c b/lib/rtnl.c
index b5afde4..e5efa25 100644
--- a/lib/rtnl.c
+++ b/lib/rtnl.c
@@ -24,7 +24,6 @@ limitations under the License.
#include <limits.h>
#include <math.h>
#include <assert.h>
-#include <endian.h>
#include <netinet/ether.h>
#include <arpa/inet.h>
@@ -52,6 +51,7 @@ limitations under the License.
#include <libubox/uloop.h>
#include "ucode/module.h"
+#include "ucode/platform.h"
#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
diff --git a/lib/uloop.c b/lib/uloop.c
index 99cd984..d8d702f 100644
--- a/lib/uloop.c
+++ b/lib/uloop.c
@@ -23,6 +23,7 @@
#include <libubox/uloop.h>
#include "ucode/module.h"
+#include "ucode/platform.h"
#define err_return(err) do { last_error = err; return NULL; } while(0)
@@ -535,13 +536,8 @@ uc_uloop_process(uc_vm_t *vm, size_t nargs)
free(buf);
}
-#ifdef __APPLE__
- execve((const char *)ucv_string_get(executable),
- (char * const *)argp, (char * const *)envp);
-#else
execvpe((const char *)ucv_string_get(executable),
(char * const *)argp, (char * const *)envp);
-#endif
_exit(-1);
}
diff --git a/platform.c b/platform.c
new file mode 100644
index 0000000..63a79d4
--- /dev/null
+++ b/platform.c
@@ -0,0 +1,242 @@
+/*
+ * Copyright (C) 2023 Jo-Philipp Wich <jo@mein.io>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <errno.h>
+
+#include "ucode/platform.h"
+
+const char *uc_system_signal_names[UC_SYSTEM_SIGNAL_COUNT] = {
+#if defined(SIGINT)
+ [SIGINT] = "INT",
+#endif
+#if defined(SIGILL)
+ [SIGILL] = "ILL",
+#endif
+#if defined(SIGABRT)
+ [SIGABRT] = "ABRT",
+#endif
+#if defined(SIGFPE)
+ [SIGFPE] = "FPE",
+#endif
+#if defined(SIGSEGV)
+ [SIGSEGV] = "SEGV",
+#endif
+#if defined(SIGTERM)
+ [SIGTERM] = "TERM",
+#endif
+#if defined(SIGHUP)
+ [SIGHUP] = "HUP",
+#endif
+#if defined(SIGQUIT)
+ [SIGQUIT] = "QUIT",
+#endif
+#if defined(SIGTRAP)
+ [SIGTRAP] = "TRAP",
+#endif
+#if defined(SIGKILL)
+ [SIGKILL] = "KILL",
+#endif
+#if defined(SIGPIPE)
+ [SIGPIPE] = "PIPE",
+#endif
+#if defined(SIGALRM)
+ [SIGALRM] = "ALRM",
+#endif
+#if defined(SIGSTKFLT)
+ [SIGSTKFLT] = "STKFLT",
+#endif
+#if defined(SIGPWR)
+ [SIGPWR] = "PWR",
+#endif
+#if defined(SIGBUS)
+ [SIGBUS] = "BUS",
+#endif
+#if defined(SIGSYS)
+ [SIGSYS] = "SYS",
+#endif
+#if defined(SIGURG)
+ [SIGURG] = "URG",
+#endif
+#if defined(SIGSTOP)
+ [SIGSTOP] = "STOP",
+#endif
+#if defined(SIGTSTP)
+ [SIGTSTP] = "TSTP",
+#endif
+#if defined(SIGCONT)
+ [SIGCONT] = "CONT",
+#endif
+#if defined(SIGCHLD)
+ [SIGCHLD] = "CHLD",
+#endif
+#if defined(SIGTTIN)
+ [SIGTTIN] = "TTIN",
+#endif
+#if defined(SIGTTOU)
+ [SIGTTOU] = "TTOU",
+#endif
+#if defined(SIGPOLL)
+ [SIGPOLL] = "POLL",
+#endif
+#if defined(SIGXFSZ)
+ [SIGXFSZ] = "XFSZ",
+#endif
+#if defined(SIGXCPU)
+ [SIGXCPU] = "XCPU",
+#endif
+#if defined(SIGVTALRM)
+ [SIGVTALRM] = "VTALRM",
+#endif
+#if defined(SIGPROF)
+ [SIGPROF] = "PROF",
+#endif
+#if defined(SIGUSR1)
+ [SIGUSR1] = "USR1",
+#endif
+#if defined(SIGUSR2)
+ [SIGUSR2] = "USR2",
+#endif
+};
+
+
+#ifdef __APPLE__
+int
+pipe2(int pipefd[2], int flags)
+{
+ if (pipe(pipefd) != 0)
+ return -1;
+
+ if (flags & O_CLOEXEC) {
+ if (fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) != 0 ||
+ fcntl(pipefd[1], F_SETFD, FD_CLOEXEC) != 0) {
+ close(pipefd[0]);
+ close(pipefd[1]);
+
+ return -1;
+ }
+
+ flags &= ~O_CLOEXEC;
+ }
+
+ if (fcntl(pipefd[0], F_SETFL, flags) != 0 ||
+ fcntl(pipefd[1], F_SETFL, flags) != 0) {
+ close(pipefd[0]);
+ close(pipefd[1]);
+
+ return -1;
+ }
+
+ return 0;
+}
+
+/*
+ * sigtimedwait() implementation based on
+ * https://comp.unix.programmer.narkive.com/rEDH0sPT/sigtimedwait-implementation
+ * and
+ * https://github.com/wahern/lunix/blob/master/src/unix.c
+ */
+static void
+sigtimedwait_consume_signal(int signo)
+{
+}
+
+int
+sigtimedwait(const sigset_t *set, siginfo_t *info, const struct timespec *timeout)
+{
+ struct timespec elapsed = { 0, 0 }, sleep, rem;
+ sigset_t pending, unblock, omask;
+ struct sigaction sa, osa;
+ int signo;
+ bool lt;
+
+ while (true) {
+ sigemptyset(&pending);
+ sigpending(&pending);
+
+ for (signo = 1; signo < NSIG; signo++) {
+ if (!sigismember(set, signo) || !sigismember(&pending, signo))
+ continue;
+
+ sa.sa_handler = sigtimedwait_consume_signal;
+ sa.sa_flags = 0;
+ sigfillset(&sa.sa_mask);
+
+ sigaction(signo, &sa, &osa);
+
+ sigemptyset(&unblock);
+ sigaddset(&unblock, signo);
+ sigprocmask(SIG_UNBLOCK, &unblock, &omask);
+ sigprocmask(SIG_SETMASK, &omask, NULL);
+
+ sigaction(signo, &osa, NULL);
+
+ if (info) {
+ memset(info, 0, sizeof(*info));
+ info->si_signo = signo;
+ }
+
+ return signo;
+ }
+
+ sleep.tv_sec = 0;
+ sleep.tv_nsec = 200000000L; /* 2/10th second */
+ rem = sleep;
+
+ if (nanosleep(&sleep, &rem) == 0) {
+ elapsed.tv_sec += sleep.tv_sec;
+ elapsed.tv_nsec += sleep.tv_nsec;
+
+ if (elapsed.tv_nsec > 1000000000) {
+ elapsed.tv_sec++;
+ elapsed.tv_nsec -= 1000000000;
+ }
+ }
+ else if (errno == EINTR) {
+ sleep.tv_sec -= rem.tv_sec;
+ sleep.tv_nsec -= rem.tv_nsec;
+
+ if (sleep.tv_nsec < 0) {
+ sleep.tv_sec--;
+ sleep.tv_nsec += 1000000000;
+ }
+
+ elapsed.tv_sec += sleep.tv_sec;
+ elapsed.tv_nsec += sleep.tv_nsec;
+
+ if (elapsed.tv_nsec > 1000000000) {
+ elapsed.tv_sec++;
+ elapsed.tv_nsec -= 1000000000;
+ }
+ }
+ else {
+ return errno;
+ }
+
+ lt = timeout
+ ? ((elapsed.tv_sec == timeout->tv_sec)
+ ? (elapsed.tv_nsec < timeout->tv_nsec)
+ : (elapsed.tv_sec < timeout->tv_sec))
+ : true;
+
+ if (!lt)
+ break;
+ }
+
+ errno = EAGAIN;
+
+ return -1;
+}
+#endif
diff --git a/program.c b/program.c
index abd525a..d2d27fb 100644
--- a/program.c
+++ b/program.c
@@ -16,12 +16,12 @@
#include <assert.h>
#include <errno.h>
-#include <endian.h>
#include "ucode/program.h"
#include "ucode/source.h"
#include "ucode/vallist.h"
#include "ucode/chunk.h"
+#include "ucode/platform.h"
uc_program_t *
diff --git a/source.c b/source.c
index ba218ff..39295f6 100644
--- a/source.c
+++ b/source.c
@@ -16,9 +16,9 @@
#include <string.h>
#include <errno.h>
-#include <endian.h>
#include "ucode/source.h"
+#include "ucode/platform.h"
uc_source_t *
diff --git a/types.c b/types.c
index d3aae0a..226c874 100644
--- a/types.c
+++ b/types.c
@@ -17,7 +17,6 @@
#include <stdarg.h>
#include <stdlib.h>
#include <assert.h>
-#include <endian.h>
#include <errno.h>
#include <math.h>
#include <ctype.h>
diff --git a/vallist.c b/vallist.c
index 44709bf..886ede0 100644
--- a/vallist.c
+++ b/vallist.c
@@ -15,7 +15,6 @@
*/
#include <string.h> /* memcpy(), memset() */
-#include <endian.h> /* htobe64(), be64toh() */
#include <math.h> /* isnan(), INFINITY */
#include <ctype.h> /* isspace(), isdigit(), isxdigit() */
#include <assert.h>
@@ -27,6 +26,7 @@
#include "ucode/program.h"
#include "ucode/vallist.h"
#include "ucode/vm.h"
+#include "ucode/platform.h"
#define TAG_TYPE uint64_t
#define TAG_BITS 3
diff --git a/vm.c b/vm.c
index d3c26bf..f4bc308 100644
--- a/vm.c
+++ b/vm.c
@@ -28,6 +28,7 @@
#include "ucode/compiler.h"
#include "ucode/program.h"
#include "ucode/lib.h" /* uc_error_context_format() */
+#include "ucode/platform.h"
#undef __insn
#define __insn(_name) #_name,
@@ -154,36 +155,6 @@ uc_vm_signal_handler(int sig)
uc_vm_signal_raise(signal_handler_vm, sig);
}
-#ifdef __APPLE__
-static int pipe2(int pipefd[2], int flags)
-{
- if (pipe(pipefd) != 0)
- return -1;
-
- if (flags & O_CLOEXEC) {
- if (fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) != 0 ||
- fcntl(pipefd[1], F_SETFD, FD_CLOEXEC) != 0) {
- close(pipefd[0]);
- close(pipefd[1]);
-
- return -1;
- }
-
- flags &= ~O_CLOEXEC;
- }
-
- if (fcntl(pipefd[0], F_SETFL, flags) != 0 ||
- fcntl(pipefd[1], F_SETFL, flags) != 0) {
- close(pipefd[0]);
- close(pipefd[1]);
-
- return -1;
- }
-
- return 0;
-}
-#endif
-
static void
uc_vm_signal_handlers_setup(uc_vm_t *vm)
{
@@ -200,7 +171,7 @@ uc_vm_signal_handlers_setup(uc_vm_t *vm)
signal_handler_vm = vm;
- vm->signal.handler = ucv_array_new_length(vm, NSIG - 1);
+ vm->signal.handler = ucv_array_new_length(vm, UC_SYSTEM_SIGNAL_COUNT);
vm->signal.sa.sa_handler = uc_vm_signal_handler;
vm->signal.sa.sa_flags = SA_RESTART | SA_ONSTACK;
@@ -3182,7 +3153,7 @@ uc_vm_signal_raise(uc_vm_t *vm, int signo)
{
uint8_t signum = signo;
- if (signo <= 0 || signo >= NSIG)
+ if (signo <= 0 || signo >= UC_SYSTEM_SIGNAL_COUNT)
return;
vm->signal.raised[signo / 64] |= (1ull << (signo % 64));