From f980e91b60bd0ea9c37bcab0461eb240aef13cfc Mon Sep 17 00:00:00 2001 From: andrea321123 <60961828+andrea321123@users.noreply.github.com> Date: Sat, 16 Apr 2022 11:54:55 +0200 Subject: [PATCH] Handle Ctrl+D, Ctrl+Z properly * Fix #105 * Check if last char is EOF --- cli/repl.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/cli/repl.c b/cli/repl.c index 11f44a9..a74ee34 100644 --- a/cli/repl.c +++ b/cli/repl.c @@ -35,12 +35,15 @@ const char* read_line(uint32_t* length) { // Read a line from stdin and returns it without the line ending. static void readLine(ByteBuffer* buff) { + char c; + do { - char c = (char)fgetc(stdin); - if (c == EOF || c == '\n') break; + c = (char)fgetc(stdin); + if (c == '\n') break; byteBufferWrite(buff, (uint8_t)c); - } while (true); + + } while (c != EOF); byteBufferWrite(buff, '\0'); } @@ -92,6 +95,12 @@ int repl(PKVM* vm, const PkCompileOptions* options) { readLine(&line); bool is_empty = is_str_empty((const char*)line.data); + // If the line contains EOF, REPL should be stopped. + if (line.count >= 2 && *((char*)(line.data) + line.count -2) == EOF) { + fprintf(stdout, "\n"); + break; + } + // If the line is empty, we don't have to compile it. if (is_empty && !need_more_lines) { byteBufferClear(&line);