1
0
mirror of https://github.com/janet-lang/janet synced 2024-10-31 22:16:16 +00:00

Add more dynamic bindings for printing.

This commit is contained in:
Calvin Rose 2019-06-08 15:27:13 -04:00
parent d9a4ef05ac
commit 802a2d6b71
4 changed files with 19 additions and 12 deletions

View File

@ -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);
}

View File

@ -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);

View File

@ -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;

View File

@ -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';