Skip to content

Commit 0dd6bca

Browse files
author
Noam Preil
committed
9front: expanded OS support
1 parent 8284b1f commit 0dd6bca

File tree

3 files changed

+48
-25
lines changed

3 files changed

+48
-25
lines changed

mkfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ TARG=janet
44
HFILES=src/include/janet.h src/conf/janetconf.h
55
JANET_PATH=/sys/lib/janet
66
BIN=/$objtype/bin/
7-
JANET_CONFIG=JANET_SINGLE_THREADED JANET_NO_DYNAMIC_MODULES JANET_NO_THREADS JANET_OS_NAME=9front JANET_ARCH_NAME=$objtype JANET_BUILD="9front" JANET_API='' JANET_NO_RETURN='' JANET_NO_REALPATH JANET_NO_UTC_MKTIME JANET_SIMPLE_GETLINE JANET_NO_FFI JANET_REDUCED_OS JANET_64 JANET_NO_ASSEMBLER JANET_NO_NET JANET_NO_EV
7+
DISABLED=PROCESSES EV NET ASSEMBLER FFI UTC_MKTIME REALPATH DYNAMIC_MODULES THREADS SYMLINKS LOCALES UMASK
8+
JANET_CONFIG=JANET_SINGLE_THREADED JANET_OS_NAME=9front JANET_ARCH_NAME=$objtype JANET_BUILD="9front" JANET_API='' JANET_NO_RETURN='' JANET_SIMPLE_GETLINE JANET_64 `{echo JANET_NO_^$DISABLED}
89
CFLAGS=-FTVBNcwp -D _POSIX_SOURCE -DJANET_PLAN9 -D_BSD_EXTENSION -D_LIMITS_EXTENSION -Isrc/include -Isrc/conf -I/sys/include/npe -Dtypestr=janettypestr -DJANET_API `{echo '-D'^$JANET_CONFIG}
910
BOOT_CFLAGS=$CFLAGS -DJANET_BOOTSTRAP
1011

@@ -22,6 +23,8 @@ JANET_BOOT_OBJECTS=`{echo $JANET_CORE_SOURCES $JANET_BOOT_SOURCES | sed -e 's/\.
2223
2324
OFILES=janet.$O src/mainclient/shell.$O
2425
26+
default:V:all
27+
2528
src/%.boot.$O: src/%.c $JANET_HEADERS $JANET_CORE_HEADERS $JANET_BOOT_HEADERS
2629
$CC $BOOT_CFLAGS -o $target $prereq(1)
2730

src/core/os.c

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,14 @@
6262
#include <process.h>
6363
#define JANET_SPAWN_CHDIR
6464
#else
65+
#ifndef JANET_PLAN9
6566
#include <spawn.h>
67+
#include <sys/wait.h>
68+
#endif
6669
#include <utime.h>
6770
#include <unistd.h>
6871
#include <dirent.h>
6972
#include <sys/types.h>
70-
#include <sys/wait.h>
7173
#ifdef JANET_APPLE
7274
#include <crt_externs.h>
7375
#define environ (*_NSGetEnviron())
@@ -293,20 +295,7 @@ JANET_CORE_FN(os_exit,
293295
return janet_wrap_nil();
294296
}
295297

296-
#ifdef JANET_PLAN9
297-
298-
JANET_CORE_FN(os_isatty,
299-
"(os/isatty &opt file)",
300-
"Returns true if `file` is a terminal. If `file` is not specified, "
301-
"it will default to standard output.") {
302-
janet_arity(argc, 0, 1);
303-
FILE *f = (argc == 1) ? janet_getfile(argv, 0, NULL) : stdout;
304-
int fd = fileno(f);
305-
if (fd == -1) janet_panic(janet_strerror(errno));
306-
return janet_wrap_boolean(isatty(fd));
307-
}
308-
309-
#elif !defined(JANET_REDUCED_OS)
298+
#ifndef JANET_REDUCED_OS
310299

311300
JANET_CORE_FN(os_cpu_count,
312301
"(os/cpu-count &opt dflt)",
@@ -1621,6 +1610,7 @@ JANET_CORE_FN(os_shell,
16211610

16221611
#endif /* JANET_NO_PROCESSES */
16231612

1613+
#ifndef JANET_PLAN9
16241614
JANET_CORE_FN(os_environ,
16251615
"(os/environ)",
16261616
"Get a copy of the OS environment table.") {
@@ -1652,6 +1642,7 @@ JANET_CORE_FN(os_environ,
16521642
janet_unlock_environ();
16531643
return janet_wrap_table(t);
16541644
}
1645+
#endif
16551646

16561647
JANET_CORE_FN(os_getenv,
16571648
"(os/getenv variable &opt dflt)",
@@ -1676,6 +1667,9 @@ JANET_CORE_FN(os_setenv,
16761667
#ifdef JANET_WINDOWS
16771668
#define SETENV(K,V) _putenv_s(K, V)
16781669
#define UNSETENV(K) _putenv_s(K, "")
1670+
#elif defined(JANET_PLAN9)
1671+
#define SETENV(K,V) putenv(K, V)
1672+
#define UNSETENV(K) unsetenv(K)
16791673
#else
16801674
#define SETENV(K,V) setenv(K, V, 1)
16811675
#define UNSETENV(K) unsetenv(K)
@@ -1848,6 +1842,8 @@ static struct tm *time_to_tm(const Janet *argv, int32_t argc, int32_t n, struct
18481842
_tzset();
18491843
localtime_s(t_infos, &t);
18501844
t_info = t_infos;
1845+
#elif defined(JANET_PLAN9)
1846+
t_info = localtime(&t);
18511847
#else
18521848
tzset();
18531849
t_info = localtime_r(&t, t_infos);
@@ -1857,6 +1853,8 @@ static struct tm *time_to_tm(const Janet *argv, int32_t argc, int32_t n, struct
18571853
#ifdef JANET_WINDOWS
18581854
gmtime_s(t_infos, &t);
18591855
t_info = t_infos;
1856+
#elif defined(JANET_PLAN9)
1857+
t_info = gmtime(&t);
18601858
#else
18611859
t_info = gmtime_r(&t, t_infos);
18621860
#endif
@@ -2031,6 +2029,7 @@ JANET_CORE_FN(os_mktime,
20312029
#define j_symlink symlink
20322030
#endif
20332031

2032+
#ifndef JANET_NO_LOCALES
20342033
JANET_CORE_FN(os_setlocale,
20352034
"(os/setlocale &opt locale category)",
20362035
"Set the system locale, which affects how dates and numbers are formatted. "
@@ -2067,19 +2066,20 @@ JANET_CORE_FN(os_setlocale,
20672066
if (old == NULL) return janet_wrap_nil();
20682067
return janet_cstringv(old);
20692068
}
2069+
#endif
20702070

20712071
JANET_CORE_FN(os_link,
20722072
"(os/link oldpath newpath &opt symlink)",
20732073
"Create a link at newpath that points to oldpath and returns nil. "
20742074
"Iff symlink is truthy, creates a symlink. "
20752075
"Iff symlink is falsey or not provided, "
2076-
"creates a hard link. Does not work on Windows.") {
2076+
"creates a hard link. Does not work on Windows or Plan 9.") {
20772077
janet_sandbox_assert(JANET_SANDBOX_FS_WRITE);
20782078
janet_arity(argc, 2, 3);
2079-
#ifdef JANET_WINDOWS
2079+
#if defined(JANET_WINDOWS) || defined(JANET_PLAN9)
20802080
(void) argc;
20812081
(void) argv;
2082-
janet_panic("not supported on Windows");
2082+
janet_panic("not supported on Windows or Plan 9");
20832083
#else
20842084
const char *oldpath = janet_getcstring(argv, 0);
20852085
const char *newpath = janet_getcstring(argv, 1);
@@ -2094,10 +2094,10 @@ JANET_CORE_FN(os_symlink,
20942094
"Create a symlink from oldpath to newpath, returning nil. Same as `(os/link oldpath newpath true)`.") {
20952095
janet_sandbox_assert(JANET_SANDBOX_FS_WRITE);
20962096
janet_fixarity(argc, 2);
2097-
#ifdef JANET_WINDOWS
2097+
#if defined(JANET_WINDOWS) || defined(JANET_PLAN9)
20982098
(void) argc;
20992099
(void) argv;
2100-
janet_panic("not supported on Windows");
2100+
janet_panic("not supported on Windows or Plan 9");
21012101
#else
21022102
const char *oldpath = janet_getcstring(argv, 0);
21032103
const char *newpath = janet_getcstring(argv, 1);
@@ -2135,6 +2135,8 @@ JANET_CORE_FN(os_rmdir,
21352135
const char *path = janet_getcstring(argv, 0);
21362136
#ifdef JANET_WINDOWS
21372137
int res = _rmdir(path);
2138+
#elif defined(JANET_PLAN9)
2139+
int res = remove(path);
21382140
#else
21392141
int res = rmdir(path);
21402142
#endif
@@ -2261,11 +2263,13 @@ static const uint8_t *janet_decode_mode(mode_t m) {
22612263
const char *str = "other";
22622264
if (S_ISREG(m)) str = "file";
22632265
else if (S_ISDIR(m)) str = "directory";
2266+
#ifndef JANET_PLAN9
22642267
else if (S_ISFIFO(m)) str = "fifo";
22652268
else if (S_ISBLK(m)) str = "block";
22662269
else if (S_ISSOCK(m)) str = "socket";
22672270
else if (S_ISLNK(m)) str = "link";
22682271
else if (S_ISCHR(m)) str = "character";
2272+
#endif
22692273
return janet_ckeyword(str);
22702274
}
22712275

@@ -2430,6 +2434,9 @@ static Janet os_stat_or_lstat(int do_lstat, int32_t argc, Janet *argv) {
24302434
#ifdef JANET_WINDOWS
24312435
(void) do_lstat;
24322436
int res = _stat(path, &st);
2437+
#elif defined(JANET_PLAN9)
2438+
(void)do_lstat;
2439+
int res = stat(path, &st);
24332440
#else
24342441
int res;
24352442
if (do_lstat) {
@@ -2490,9 +2497,13 @@ JANET_CORE_FN(os_chmod,
24902497
"Change file permissions, where `mode` is a permission string as returned by "
24912498
"`os/perm-string`, or an integer as returned by `os/perm-int`. "
24922499
"When `mode` is an integer, it is interpreted as a Unix permission value, best specified in octal, like "
2493-
"8r666 or 8r400. Windows will not differentiate between user, group, and other permissions, and thus will combine all of these permissions. Returns nil.") {
2500+
"8r666 or 8r400. Windows will not differentiate between user, group, and other permissions, and thus will combine all of these permissions. Returns nil."
2501+
"Unsupported on plan9.") {
24942502
janet_sandbox_assert(JANET_SANDBOX_FS_WRITE);
24952503
janet_fixarity(argc, 2);
2504+
#ifdef JANET_PLAN9
2505+
janet_panic("not supported on Plan 9");
2506+
#else
24962507
const char *path = janet_getcstring(argv, 0);
24972508
#ifdef JANET_WINDOWS
24982509
int res = _chmod(path, os_getmode(argv, 1));
@@ -2501,6 +2512,7 @@ JANET_CORE_FN(os_chmod,
25012512
#endif
25022513
if (-1 == res) janet_panicf("%s: %s", janet_strerror(errno), path);
25032514
return janet_wrap_nil();
2515+
#endif
25042516
}
25052517

25062518
#ifndef JANET_NO_UMASK
@@ -2865,9 +2877,7 @@ void janet_lib_os(JanetTable *env) {
28652877
JANET_CORE_REG("os/which", os_which),
28662878
JANET_CORE_REG("os/arch", os_arch),
28672879
JANET_CORE_REG("os/compiler", os_compiler),
2868-
#ifdef JANET_PLAN9
2869-
JANET_CORE_REG("os/isatty", os_isatty),
2870-
#elif !defined(JANET_REDUCED_OS)
2880+
#ifndef JANET_REDUCED_OS
28712881

28722882
/* misc (un-sandboxed) */
28732883
JANET_CORE_REG("os/cpu-count", os_cpu_count),
@@ -2881,10 +2891,14 @@ void janet_lib_os(JanetTable *env) {
28812891
JANET_CORE_REG("os/strftime", os_strftime),
28822892
JANET_CORE_REG("os/sleep", os_sleep),
28832893
JANET_CORE_REG("os/isatty", os_isatty),
2894+
#ifndef JANET_NO_LOCALES
28842895
JANET_CORE_REG("os/setlocale", os_setlocale),
2896+
#endif
28852897

28862898
/* env functions */
2899+
#ifndef JANET_PLAN9
28872900
JANET_CORE_REG("os/environ", os_environ),
2901+
#endif
28882902
JANET_CORE_REG("os/getenv", os_getenv),
28892903
JANET_CORE_REG("os/setenv", os_setenv),
28902904

src/core/util.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,11 +848,15 @@ int janet_checksize(Janet x) {
848848
return 0;
849849
double dval = janet_unwrap_number(x);
850850
if (dval != (double)((size_t) dval)) return 0;
851+
#ifdef JANET_PLAN9
852+
return dval <= SIZE_MAX;
853+
#else
851854
if (SIZE_MAX > JANET_INTMAX_INT64) {
852855
return dval <= JANET_INTMAX_INT64;
853856
} else {
854857
return dval <= SIZE_MAX;
855858
}
859+
#endif
856860
}
857861

858862
JanetTable *janet_get_core_table(const char *name) {
@@ -959,8 +963,10 @@ int janet_gettime(struct timespec *spec, enum JanetTimeSource source) {
959963
cid = CLOCK_REALTIME;
960964
} else if (source == JANET_TIME_MONOTONIC) {
961965
cid = CLOCK_MONOTONIC;
966+
#ifndef JANET_PLAN9
962967
} else if (source == JANET_TIME_CPUTIME) {
963968
cid = CLOCK_PROCESS_CPUTIME_ID;
969+
#endif
964970
}
965971
return clock_gettime(cid, spec);
966972
}

0 commit comments

Comments
 (0)