From 1c0c6daf4f4f013da96339a59584f72ad0a7b990 Mon Sep 17 00:00:00 2001 From: Emin Arslan Date: Sun, 3 May 2026 22:51:46 +0300 Subject: [PATCH] fix: replace direct stdio calls with portable I/O hooks Co-authored-by: aider (openrouter/moonshotai/kimi-k2.6) --- forth_core.c | 2 +- forth_interp.c | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/forth_core.c b/forth_core.c index fce46ce..2c4212d 100644 --- a/forth_core.c +++ b/forth_core.c @@ -78,7 +78,7 @@ void data_push(int64_t val) { int64_t data_pop(void) { if (data_sp < 0) { - fprintf(stderr, "Data stack underflow\n"); + forth_printf("Data stack underflow\n"); return 0; } return data_stack[data_sp--]; diff --git a/forth_interp.c b/forth_interp.c index d582f4c..0ca245d 100644 --- a/forth_interp.c +++ b/forth_interp.c @@ -87,15 +87,21 @@ void outer_interpreter(void) { while (1) { forth_printf("ok "); forth_fflush(); - if (fgets(line_buf, sizeof(line_buf), stdin) == NULL) { + size_t i = 0; + int c; + while (i < sizeof(line_buf) - 1) { + c = forth_getchar(); + if (c == EOF || c == '\n' || c == '\r') { + break; + } + line_buf[i++] = (char)c; + } + line_buf[i] = '\0'; + if (i == 0 && c == EOF) { break; } input_buf = line_buf; input_buf_cap = sizeof(line_buf); - size_t n = strlen(input_buf); - if (n > 0 && input_buf[n - 1] == '\n') { - input_buf[n - 1] = '\0'; - } input_ptr = input_buf; char* tok; while ((tok = next_token()) != NULL) {