2020-05-18 16:51:10 +03:00
|
|
|
#include "idris_support.h"
|
|
|
|
#include "idris_file.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
2020-06-16 13:53:36 +03:00
|
|
|
#include <stdlib.h>
|
2021-07-03 07:44:23 +03:00
|
|
|
#include <string.h>
|
2022-09-21 13:13:15 +03:00
|
|
|
#include <time.h>
|
2021-03-26 21:27:25 +03:00
|
|
|
#include <unistd.h>
|
2020-05-18 16:51:10 +03:00
|
|
|
|
2021-05-17 16:07:53 +03:00
|
|
|
int _argc;
|
|
|
|
char **_argv;
|
|
|
|
|
2020-05-19 14:11:07 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
extern char **_environ;
|
2020-05-24 20:54:41 +03:00
|
|
|
#include "windows/win_utils.h"
|
2020-05-19 14:11:07 +03:00
|
|
|
#define environ _environ
|
|
|
|
#else
|
2022-09-21 13:13:15 +03:00
|
|
|
extern char **environ;
|
2020-05-19 14:11:07 +03:00
|
|
|
#endif
|
2020-05-18 16:51:10 +03:00
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
int idris2_isNull(void *ptr) { return (ptr == NULL); }
|
2020-05-18 16:51:10 +03:00
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
void *idris2_getNull() { return NULL; }
|
2020-08-20 13:52:51 +03:00
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
char *idris2_getString(void *p) { return (char *)p; }
|
2020-05-18 16:51:10 +03:00
|
|
|
|
|
|
|
int idris2_getErrno() {
|
2021-03-26 21:27:25 +03:00
|
|
|
#ifdef _WIN32
|
2022-09-21 13:13:15 +03:00
|
|
|
return win32_getErrno();
|
2021-03-26 21:27:25 +03:00
|
|
|
#else
|
2022-09-21 13:13:15 +03:00
|
|
|
return errno;
|
2021-03-26 21:27:25 +03:00
|
|
|
#endif
|
2020-05-18 16:51:10 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
char *idris2_strerror(int errnum) { return strerror(errnum); }
|
2021-07-03 07:44:23 +03:00
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
char *idris2_getStr() {
|
|
|
|
char *inp = idris2_readLine(stdin);
|
|
|
|
// Remove trailing newline; easier to do this than in PrimIO which
|
|
|
|
// doesn't have the relevant functions available yet
|
|
|
|
for (char *c = inp; *c != '\0'; ++c) {
|
|
|
|
if (*c == '\n' || *c == '\r') {
|
|
|
|
*c = '\0';
|
2020-05-18 16:51:10 +03:00
|
|
|
}
|
2022-09-21 13:13:15 +03:00
|
|
|
}
|
|
|
|
return inp;
|
2020-05-18 16:51:10 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
void idris2_putStr(char *f) { idris2_writeLine(stdout, f); }
|
2020-05-18 16:51:10 +03:00
|
|
|
|
|
|
|
void idris2_sleep(int sec) {
|
2020-05-24 20:54:41 +03:00
|
|
|
#ifdef _WIN32
|
2022-09-21 13:13:15 +03:00
|
|
|
win32_sleep(sec * 1000);
|
2020-05-24 20:54:41 +03:00
|
|
|
#else
|
2022-09-21 13:13:15 +03:00
|
|
|
struct timespec t;
|
|
|
|
t.tv_sec = sec;
|
|
|
|
t.tv_nsec = 0;
|
2020-05-18 16:51:10 +03:00
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
// TODO: `nanosleep` can fail
|
|
|
|
// TODO: `nanosleep` can return early due to interrupt
|
|
|
|
nanosleep(&t, NULL);
|
2020-05-24 20:54:41 +03:00
|
|
|
#endif
|
2020-05-18 16:51:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void idris2_usleep(int usec) {
|
2020-05-24 20:54:41 +03:00
|
|
|
#ifdef _WIN32
|
2022-09-21 13:13:15 +03:00
|
|
|
win32_sleep(usec / 1000);
|
2020-05-24 20:54:41 +03:00
|
|
|
#else
|
2022-09-21 13:13:15 +03:00
|
|
|
struct timespec t;
|
|
|
|
t.tv_sec = usec / 1000000;
|
|
|
|
t.tv_nsec = (usec % 1000000) * 1000;
|
2020-05-18 16:51:10 +03:00
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
nanosleep(&t, NULL);
|
2020-05-24 20:54:41 +03:00
|
|
|
#endif
|
2020-05-18 16:51:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int idris2_time() {
|
2022-09-21 13:13:15 +03:00
|
|
|
return time(NULL); // RTS needs to have 32 bit integers at least
|
2020-05-18 16:51:10 +03:00
|
|
|
}
|
|
|
|
|
2021-05-17 16:07:53 +03:00
|
|
|
void idris2_setArgs(int argc, char *argv[]) {
|
2022-09-21 13:13:15 +03:00
|
|
|
_argc = argc;
|
|
|
|
_argv = argv;
|
2021-05-17 16:07:53 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
int idris2_getArgCount() { return _argc; }
|
2021-05-17 16:07:53 +03:00
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
char *idris2_getArg(int n) { return _argv[n]; }
|
2021-05-17 16:07:53 +03:00
|
|
|
|
2022-09-21 13:13:15 +03:00
|
|
|
char *idris2_getEnvPair(int i) { return *(environ + i); }
|
2020-06-16 13:09:22 +03:00
|
|
|
|
|
|
|
int idris2_setenv(const char *name, const char *value, int overwrite) {
|
|
|
|
#ifdef _WIN32
|
2022-09-21 13:13:15 +03:00
|
|
|
return win32_modenv(name, value, overwrite);
|
2020-06-16 13:09:22 +03:00
|
|
|
#else
|
2022-09-21 13:13:15 +03:00
|
|
|
return setenv(name, value, overwrite);
|
2020-06-16 13:09:22 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int idris2_unsetenv(const char *name) {
|
|
|
|
#ifdef _WIN32
|
2022-09-21 13:13:15 +03:00
|
|
|
return win32_modenv(name, "", 1);
|
2020-06-16 13:09:22 +03:00
|
|
|
#else
|
2022-09-21 13:13:15 +03:00
|
|
|
return unsetenv(name);
|
2020-06-16 13:09:22 +03:00
|
|
|
#endif
|
2020-08-20 13:52:51 +03:00
|
|
|
}
|
2021-03-26 21:27:25 +03:00
|
|
|
|
2021-05-25 18:45:46 +03:00
|
|
|
int idris2_getPID() {
|
|
|
|
#ifdef _WIN32
|
2022-09-21 13:13:15 +03:00
|
|
|
return win32_getPID();
|
2021-05-25 18:45:46 +03:00
|
|
|
#else
|
2022-09-21 13:13:15 +03:00
|
|
|
return getpid();
|
2021-05-25 18:45:46 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-03-26 21:27:25 +03:00
|
|
|
// get the number of processors configured
|
|
|
|
long idris2_getNProcessors() {
|
|
|
|
#ifdef _WIN32
|
2022-09-21 13:13:15 +03:00
|
|
|
return win32_getNProcessors();
|
2021-03-26 21:27:25 +03:00
|
|
|
#else
|
2022-09-21 13:13:15 +03:00
|
|
|
return sysconf(_SC_NPROCESSORS_CONF);
|
2021-03-26 21:27:25 +03:00
|
|
|
#endif
|
|
|
|
}
|