From 6df5b8a703c57364fd9c6f302ad495810418f612 Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 30 Oct 2022 10:08:52 +0100 Subject: [PATCH] Make backspace work --- README.md | 1 - apps/src/session.c | 23 ++++++++++++++++++----- libs/libc/src/file.cpp | 15 +++++++++++++-- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 473cae46..c7879e0f 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,6 @@ A simple kernel and userspace for the x86_64 platform, written mostly in C++ and ## Notes - The default user is named 'selene' and you can log into it with the password 'moon'. -- Beware of backspace, as it looks like it works but it doesn't work except when entering shell commands. So if you make a mistake while entering a username or password, you'll have to try again. ## Setup To build and run Luna, you will need to build a [GCC Cross-Compiler](https://wiki.osdev.org/Why_do_I_need_a_Cross_Compiler%3F) and cross-binutils for `x86_64-luna`. (Yes, Luna is advanced enough that it can use its own [OS-Specific Toolchain](https://wiki.osdev.org/OS_Specific_Toolchain), instead of a bare metal target like `x86_64-elf`. It is the first of my OS projects to be able to do so. The patches for Binutils and GCC are [binutils.patch](tools/binutils.patch) and [gcc.patch](tools/gcc.patch)). diff --git a/apps/src/session.c b/apps/src/session.c index 58ea1afb..90704812 100644 --- a/apps/src/session.c +++ b/apps/src/session.c @@ -10,6 +10,7 @@ static char* echoing_fgets(char* buf, size_t size, FILE* stream) { char* s = buf; memset(buf, 0, size); + size_t oldsize = size; while (size) { int c = fgetc(stream); @@ -23,12 +24,24 @@ static char* echoing_fgets(char* buf, size_t size, FILE* stream) return NULL; }; } - size--; - *buf = (char)c; - buf++; + if ((char)c == '\b') + { + if (size != oldsize) + { + buf--; + size++; + putchar('\b'); + } + } + else + { + size--; + *buf = (char)c; + buf++; + putchar((char)c); + if ((char)c == '\n') return s; + } *buf = 0; - putchar((char)c); - if ((char)c == '\n') return s; } return s; } diff --git a/libs/libc/src/file.cpp b/libs/libc/src/file.cpp index 77b5c9c6..147a82e0 100644 --- a/libs/libc/src/file.cpp +++ b/libs/libc/src/file.cpp @@ -154,8 +154,19 @@ extern "C" buf++; break; } - buf++; - size--; + if (*buf == '\b') + { + if (size != original_size) + { + buf--; + size++; + } + } + else + { + buf++; + size--; + } } else {