Skip to content

Commit 98bbe9f

Browse files
author
Noam Preil
committed
expose os/isatty, fix floating point math
1 parent 2f69678 commit 98bbe9f

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

src/boot/system_test.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ int system_test() {
5151
assert(janet_equals(janet_wrap_number(1.4), janet_wrap_number(1.4)));
5252
assert(janet_equals(janet_wrap_number(3.14159265), janet_wrap_number(3.14159265)));
5353
#ifdef NAN
54-
#ifndef JANET_PLAN9
55-
assert(janet_checktype(janet_wrap_number(NAN), JANET_NUMBER));
54+
#ifdef JANET_PLAN9
55+
// Plan 9 traps NaNs by default; disable that.
56+
setfcr(0);
5657
#endif
58+
assert(janet_checktype(janet_wrap_number(NAN), JANET_NUMBER));
5759
#else
5860
assert(janet_checktype(janet_wrap_number(0.0 / 0.0), JANET_NUMBER));
5961
#endif

src/core/os.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929

3030
#include <stdlib.h>
3131

32+
#ifdef JANET_PLAN9
33+
#include <unistd.h>
34+
#endif
35+
36+
3237
#ifndef JANET_REDUCED_OS
3338

3439
#include <time.h>
@@ -288,7 +293,20 @@ JANET_CORE_FN(os_exit,
288293
return janet_wrap_nil();
289294
}
290295

291-
#ifndef JANET_REDUCED_OS
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)
292310

293311
JANET_CORE_FN(os_cpu_count,
294312
"(os/cpu-count &opt dflt)",
@@ -2847,7 +2865,9 @@ void janet_lib_os(JanetTable *env) {
28472865
JANET_CORE_REG("os/which", os_which),
28482866
JANET_CORE_REG("os/arch", os_arch),
28492867
JANET_CORE_REG("os/compiler", os_compiler),
2850-
#ifndef JANET_REDUCED_OS
2868+
#ifdef JANET_PLAN9
2869+
JANET_CORE_REG("os/isatty", os_isatty),
2870+
#elif !defined(JANET_REDUCED_OS)
28512871

28522872
/* misc (un-sandboxed) */
28532873
JANET_CORE_REG("os/cpu-count", os_cpu_count),

src/include/janet.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,6 @@ extern "C" {
305305
#define JANET_STACK_MAX 0x7fffffff
306306
#endif
307307

308-
#ifdef JANET_PLAN9
309-
#undef NAN
310-
#endif
311-
312308
/* Use nanboxed values - uses 8 bytes per value instead of 12 or 16.
313309
* To turn of nanboxing, for debugging purposes or for certain
314310
* architectures (Nanboxing only tested on x86 and x64), comment out

src/mainclient/shell.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,9 @@ static int curpos(void) {
309309
char buf[32];
310310
int cols, rows;
311311
unsigned int i = 0;
312+
#ifndef JANET_PLAN9
312313
if (write_console("\x1b[6n", 4) != 4) return -1;
314+
#endif
313315
while (i < sizeof(buf) - 1) {
314316
if (read_console(buf + i, 1) != 1) break;
315317
if (buf[i] == 'R') break;
@@ -327,6 +329,10 @@ static int getcols(void) {
327329
CONSOLE_SCREEN_BUFFER_INFO csbi;
328330
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
329331
return (int)(csbi.srWindow.Right - csbi.srWindow.Left + 1);
332+
#elif defined(JANET_PLAN9)
333+
//FIXME(noam): this can probably read /dev/window to get window size, and
334+
//divide by the active font width, since plan9 requires fixed-width glyphs.
335+
return 80;
330336
#else
331337
struct winsize ws;
332338
if (ioctl(1, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
@@ -1133,6 +1139,10 @@ int main(int argc, char **argv) {
11331139
JanetArray *args;
11341140
JanetTable *env;
11351141

1142+
#ifdef JANET_PLAN9
1143+
setfcr(0);
1144+
#endif
1145+
11361146
#ifdef _WIN32
11371147
setup_console_output();
11381148
#endif

0 commit comments

Comments
 (0)