From 56bef77cceccfdf790c0bf030634514d9e3e3c71 Mon Sep 17 00:00:00 2001 From: Ali Mashtizadeh Date: Fri, 22 Sep 2023 21:07:27 -0400 Subject: [PATCH] Fix backspace emulation in the serial console and in fgets() --- lib/libc/file.c | 5 +++++ sys/dev/x86/sercons.c | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/lib/libc/file.c b/lib/libc/file.c index e728cf4..2347a22 100644 --- a/lib/libc/file.c +++ b/lib/libc/file.c @@ -171,6 +171,11 @@ fgets(char *str, int size, FILE *fh) int ch = fgetc(fh); if (ch == EOF) return NULL; + if (ch == '\b') { + if (i > 0) + i -= 2; + continue; + } str[i] = (char)ch; if (ch == '\n') { str[i + 1] = '\0'; diff --git a/sys/dev/x86/sercons.c b/sys/dev/x86/sercons.c index 6536e6d..6616b98 100644 --- a/sys/dev/x86/sercons.c +++ b/sys/dev/x86/sercons.c @@ -91,6 +91,12 @@ void Serial_Putc(char ch) // Timeout! } outb(base + UART_OFFSET_DATA, ch); + + if (ch == '\b') { + Serial_Putc(0x1B); + Serial_Putc('['); + Serial_Putc('P'); + } } void Serial_Puts(const char *str)