"introduce msh_move_cursor so we can abstrace backspace"

This commit is contained in:
IoIxD 2026-01-21 13:51:42 -07:00
commit 0ed0d32b27
3 changed files with 34 additions and 14 deletions

View file

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

View file

@ -3,16 +3,14 @@
#include "syscall.h"
#include <linux/kd.h>
#include <signal.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/signal.h>
#include <sys/stat.h>
#include <sys/termios.h>
#include <sys/wait.h>
#include <termios.h>
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) {

View file

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