Handle Ctrl+D, Ctrl+Z properly

* Fix #105

* Check if last char is EOF
This commit is contained in:
andrea321123 2022-04-16 11:54:55 +02:00 committed by GitHub
parent 925ba3504d
commit f980e91b60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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