diff --git a/src/boot/system_test.c b/src/boot/system_test.c index ba13571e..0b4c65b5 100644 --- a/src/boot/system_test.c +++ b/src/boot/system_test.c @@ -51,9 +51,11 @@ int system_test() { assert(janet_equals(janet_wrap_number(1.4), janet_wrap_number(1.4))); assert(janet_equals(janet_wrap_number(3.14159265), janet_wrap_number(3.14159265))); #ifdef NAN -#ifndef JANET_PLAN9 - assert(janet_checktype(janet_wrap_number(NAN), JANET_NUMBER)); +#ifdef JANET_PLAN9 + // Plan 9 traps NaNs by default; disable that. + setfcr(0); #endif + assert(janet_checktype(janet_wrap_number(NAN), JANET_NUMBER)); #else assert(janet_checktype(janet_wrap_number(0.0 / 0.0), JANET_NUMBER)); #endif diff --git a/src/core/os.c b/src/core/os.c index 66140c41..e183bf43 100644 --- a/src/core/os.c +++ b/src/core/os.c @@ -29,6 +29,11 @@ #include +#ifdef JANET_PLAN9 +#include +#endif + + #ifndef JANET_REDUCED_OS #include @@ -288,7 +293,20 @@ JANET_CORE_FN(os_exit, return janet_wrap_nil(); } -#ifndef JANET_REDUCED_OS +#ifdef JANET_PLAN9 + +JANET_CORE_FN(os_isatty, + "(os/isatty &opt file)", + "Returns true if `file` is a terminal. If `file` is not specified, " + "it will default to standard output.") { + janet_arity(argc, 0, 1); + FILE *f = (argc == 1) ? janet_getfile(argv, 0, NULL) : stdout; + int fd = fileno(f); + if (fd == -1) janet_panic(janet_strerror(errno)); + return janet_wrap_boolean(isatty(fd)); +} + +#elif !defined(JANET_REDUCED_OS) JANET_CORE_FN(os_cpu_count, "(os/cpu-count &opt dflt)", @@ -2847,7 +2865,9 @@ void janet_lib_os(JanetTable *env) { JANET_CORE_REG("os/which", os_which), JANET_CORE_REG("os/arch", os_arch), JANET_CORE_REG("os/compiler", os_compiler), -#ifndef JANET_REDUCED_OS +#ifdef JANET_PLAN9 + JANET_CORE_REG("os/isatty", os_isatty), +#elif !defined(JANET_REDUCED_OS) /* misc (un-sandboxed) */ JANET_CORE_REG("os/cpu-count", os_cpu_count), diff --git a/src/include/janet.h b/src/include/janet.h index 4b47a7ae..0401bf0e 100644 --- a/src/include/janet.h +++ b/src/include/janet.h @@ -305,10 +305,6 @@ extern "C" { #define JANET_STACK_MAX 0x7fffffff #endif -#ifdef JANET_PLAN9 -#undef NAN -#endif - /* Use nanboxed values - uses 8 bytes per value instead of 12 or 16. * To turn of nanboxing, for debugging purposes or for certain * architectures (Nanboxing only tested on x86 and x64), comment out diff --git a/src/mainclient/shell.c b/src/mainclient/shell.c index 93546db2..beb938dd 100644 --- a/src/mainclient/shell.c +++ b/src/mainclient/shell.c @@ -309,7 +309,9 @@ static int curpos(void) { char buf[32]; int cols, rows; unsigned int i = 0; +#ifndef JANET_PLAN9 if (write_console("\x1b[6n", 4) != 4) return -1; +#endif while (i < sizeof(buf) - 1) { if (read_console(buf + i, 1) != 1) break; if (buf[i] == 'R') break; @@ -327,6 +329,10 @@ static int getcols(void) { CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); return (int)(csbi.srWindow.Right - csbi.srWindow.Left + 1); +#elif defined(JANET_PLAN9) + //FIXME(noam): this can probably read /dev/window to get window size, and + //divide by the active font width, since plan9 requires fixed-width glyphs. + return 80; #else struct winsize ws; if (ioctl(1, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) { @@ -1133,6 +1139,10 @@ int main(int argc, char **argv) { JanetArray *args; JanetTable *env; +#ifdef JANET_PLAN9 + setfcr(0); +#endif + #ifdef _WIN32 setup_console_output(); #endif