From ba69471edd732efa8a9b435299b82bfc482e0756 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Sun, 10 Oct 2021 15:31:18 +0000 Subject: init/shell: basic readline() --- src/init/main.c | 5 +---- src/init/shell.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/init/shell.h | 3 +++ 3 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 src/init/shell.c create mode 100644 src/init/shell.h (limited to 'src') diff --git a/src/init/main.c b/src/init/main.c index 401e4a0..464fadd 100644 --- a/src/init/main.c +++ b/src/init/main.c @@ -27,10 +27,7 @@ int main(void) { fs_test(); test_await(); - - char c; - while (_syscall_read(tty_fd, &c, 1, 0)) - _syscall_write(tty_fd, &c, 1, 0); + shell_loop(); _syscall_exit(0); } diff --git a/src/init/shell.c b/src/init/shell.c new file mode 100644 index 0000000..d61f46f --- /dev/null +++ b/src/init/shell.c @@ -0,0 +1,44 @@ +#include +#include +#include + +#define PROMPT "$ " + +static int tty_fd = 0; // TODO put in stdlib + +static int readline(char *buf, size_t max) { + char c; + size_t pos = 0; + while (_syscall_read(tty_fd, &c, 1, 0)) { + switch (c) { + case '\b': + case 0x7f: + /* for some reason backspace outputs 0x7f (DEL) */ + if (pos != 0) { + printf("\b \b"); + pos--; + } + break; + case '\r': + printf("\n"); + buf[pos++] = '\0'; + return pos; + default: + if (pos < max) { + _syscall_write(tty_fd, &c, 1, 0); + buf[pos] = c; + pos++; + } + } + } + return -1; // error +} + +void shell_loop(void) { + static char cmd[256]; + for (;;) { + printf(PROMPT); + readline(cmd, 256); + printf(" %s\n", cmd); + } +} diff --git a/src/init/shell.h b/src/init/shell.h new file mode 100644 index 0000000..fb27410 --- /dev/null +++ b/src/init/shell.h @@ -0,0 +1,3 @@ +#pragma once + +void shell_loop(void); -- cgit v1.2.3