From 802a2d6b713978d9e4b2291137c93653ab95b201 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sat, 8 Jun 2019 15:27:13 -0400 Subject: [PATCH] Add more dynamic bindings for printing. --- src/core/capi.c | 2 +- src/core/corelib.c | 10 ++++++---- src/core/run.c | 5 +++-- src/mainclient/line.c | 14 +++++++++----- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/core/capi.c b/src/core/capi.c index 20f6f7f2..3cc4bd0b 100644 --- a/src/core/capi.c +++ b/src/core/capi.c @@ -60,7 +60,7 @@ void janet_printf(const char *format, ...) { va_start(args, format); janet_formatb(&buffer, format, args); va_end(args); - fwrite(buffer.data, buffer.count, 1, stdout); + fwrite(buffer.data, buffer.count, 1, janet_dynfile("out", stdout)); janet_buffer_deinit(&buffer); } diff --git a/src/core/corelib.c b/src/core/corelib.c index 228a34d4..a3e11162 100644 --- a/src/core/corelib.c +++ b/src/core/corelib.c @@ -275,19 +275,21 @@ static Janet janet_core_hash(int32_t argc, Janet *argv) { } static Janet janet_core_getline(int32_t argc, Janet *argv) { + FILE *in = janet_dynfile("in", stdin); + FILE *out = janet_dynfile("out", stdout); janet_arity(argc, 0, 2); JanetBuffer *buf = (argc >= 2) ? janet_getbuffer(argv, 1) : janet_buffer(10); if (argc >= 1) { const char *prompt = (const char *) janet_getstring(argv, 0); - printf("%s", prompt); - fflush(stdout); + fprintf(out, "%s", prompt); + fflush(out); } { buf->count = 0; int c; for (;;) { - c = fgetc(stdin); - if (feof(stdin) || c < 0) { + c = fgetc(in); + if (feof(in) || c < 0) { break; } janet_buffer_push_u8(buf, (uint8_t) c); diff --git a/src/core/run.c b/src/core/run.c index 151e81f9..94a199e8 100644 --- a/src/core/run.c +++ b/src/core/run.c @@ -28,6 +28,7 @@ /* Run a string */ int janet_dobytes(JanetTable *env, const uint8_t *bytes, int32_t len, const char *sourcePath, Janet *out) { JanetParser parser; + FILE *errf = janet_dynfile("err", stderr); int errflags = 0, done = 0; int32_t index = 0; Janet ret = janet_wrap_nil(); @@ -55,7 +56,7 @@ int janet_dobytes(JanetTable *env, const uint8_t *bytes, int32_t len, const char done = 1; } } else { - fprintf(stderr, "compile error in %s: %s\n", sourcePath, + fprintf(errf, "compile error in %s: %s\n", sourcePath, (const char *)cres.error); errflags |= 0x02; done = 1; @@ -69,7 +70,7 @@ int janet_dobytes(JanetTable *env, const uint8_t *bytes, int32_t len, const char break; case JANET_PARSE_ERROR: errflags |= 0x04; - fprintf(stderr, "parse error in %s: %s\n", + fprintf(errf, "parse error in %s: %s\n", sourcePath, janet_parser_error(&parser)); done = 1; break; diff --git a/src/mainclient/line.c b/src/mainclient/line.c index da2092e3..d5252cb0 100644 --- a/src/mainclient/line.c +++ b/src/mainclient/line.c @@ -32,11 +32,12 @@ Janet janet_line_getter(int32_t argc, Janet *argv) { } static void simpleline(JanetBuffer *buffer) { + FILE *in = janet_dynfile("in", stdin); buffer->count = 0; int c; for (;;) { - c = fgetc(stdin); - if (feof(stdin) || c < 0) { + c = fgetc(in); + if (feof(in) || c < 0) { break; } janet_buffer_push_u8(buffer, (uint8_t) c); @@ -56,7 +57,9 @@ void janet_line_deinit() { } void janet_line_get(const char *p, JanetBuffer *buffer) { - fputs(p, stdout); + FILE *out = janet_dynfile("out", stdout); + fputs(p, out); + fflush(p, out); simpleline(buffer); } @@ -450,6 +453,7 @@ void janet_line_get(const char *p, JanetBuffer *buffer) { prompt = p; buffer->count = 0; historyi = 0; + FILE *out = janet_dynfile("out", stdout); if (!isatty(STDIN_FILENO) || !checktermsupport()) { simpleline(buffer); return; @@ -463,12 +467,12 @@ void janet_line_get(const char *p, JanetBuffer *buffer) { if (sigint_flag) { raise(SIGINT); } else { - fputc('\n', stdout); + fputc('\n', out); } return; } norawmode(); - fputc('\n', stdout); + fputc('\n', out); janet_buffer_ensure(buffer, len + 1, 2); memcpy(buffer->data, buf, len); buffer->data[len] = '\n';