From 0ed0d32b27d0e29567f6e9d640a5c80caf024005 Mon Sep 17 00:00:00 2001 From: IoIxD Date: Wed, 21 Jan 2026 13:51:42 -0700 Subject: [PATCH] "introduce msh_move_cursor so we can abstrace backspace" --- src/main.c | 9 +++++++++ src/system/linux/linux.c | 27 +++++++++++++++++---------- src/system/system.h | 12 ++++++++---- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/main.c b/src/main.c index 9e3351f..fd19b0f 100644 --- a/src/main.c +++ b/src/main.c @@ -19,6 +19,15 @@ MSH_ENTRY_POINT { while ((out = msh_read_input(buf, &sz, 2048)), out > 0) { switch (out) { + case 0x7F: + if (sz > 0) { + msh_move_cursor(1); + msh_write(" ", 1); + msh_move_cursor(1); + buf[sz--] = '\0'; + } + + break; /* Ctrl-C */ case 0x3: msh_print_string("^C\n\r"); diff --git a/src/system/linux/linux.c b/src/system/linux/linux.c index 1ff4505..93e0ee6 100644 --- a/src/system/linux/linux.c +++ b/src/system/linux/linux.c @@ -3,16 +3,14 @@ #include "syscall.h" #include -#include #include #include #include +#include #include #include #include -#include - char msh_path_seperator = '/'; int msh_read_input(char *ptr, SIZE_TYPE *size, SIZE_TYPE n) { @@ -34,13 +32,6 @@ int msh_read_input(char *ptr, SIZE_TYPE *size, SIZE_TYPE n) { return -1; break; /* backspace */ - case 127: - if (*size <= 0) { - return 0; - } - SYSCALL(write, 1, "\33[D \33[D", 7); - *size -= 1; - break; default: break; } @@ -54,11 +45,27 @@ int msh_read_input(char *ptr, SIZE_TYPE *size, SIZE_TYPE n) { return ch; }; +void msh_move_cursor(signed int offset) { + int i; + char *cmd = "\33[D"; + if (offset <= 0) { + offset &= ~0xffffffff; + cmd = "\33[C"; + } + for (i = 0; i < offset; i++) { + SYSCALL(write, 1, cmd, 3); + } +}; + void msh_write(char *st, SIZE_TYPE size) { SYSCALL(write, 1, st, size); } int msh_execute(char *cmd, char **args) { SIZE_TYPE fork; + if (*cmd == '\0') { + return 0; + } + fork = (SIZE_TYPE)SYSCALL(fork, 0); if (fork == 0) { diff --git a/src/system/system.h b/src/system/system.h index 4095b58..41dd575 100644 --- a/src/system/system.h +++ b/src/system/system.h @@ -59,14 +59,18 @@ void msh_file_read(msh_file *file, char *ptr, SIZE_TYPE n); /* * Read user input into ptr, stopping if it has reached n characters. - * Also has to handle backspace since erasing characters is system-dependent. * RETURNS: - * -2 = if it ran out of space. - * -1 = if finished reading - * 0 < ascii character + * * -2 = if it ran out of space. + * * -1 = if finished reading + * * 0 < ascii character */ int msh_read_input(char *ptr, SIZE_TYPE *size, SIZE_TYPE n); +/* + * Move terminal cursor x characters to the right. Pass in negative to go left. + */ +void msh_move_cursor(signed int offset); + /* Write characters to terminal. */ void msh_write(char *st, SIZE_TYPE size);