From 24a90b24e887843d9aec25243e11a314b89be4f0 Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Fri, 19 Sep 2025 19:09:54 +0200 Subject: [PATCH] tb Handle keyboard inside of the shell interactive mode --- user/init/main.c | 16 ++-------------- user/tb/main.c | 42 +++++++++++++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/user/init/main.c b/user/init/main.c index 94f9c32..c25ef3e 100644 --- a/user/init/main.c +++ b/user/init/main.c @@ -16,23 +16,11 @@ void tb_runinitscript(void) { char *tbargs[] = { "-m", "runfile", "-f", "base:/scripts/init.tb", "-logcmds", "yes" }; int32_t tb = processctl(-1, PCTL_SPAWN, (uint64_t)"base:/bin/tb", (uint64_t)&tbargs, ARRLEN(tbargs)); - ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_MAKE, NULL, 0); - ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_ADD_BCAST, NULL, 1); + ipcpipe(tb, IPCPIPE_IN, IPCPIPE_ADD_BCAST, NULL, 1); processctl(tb, PCTL_RUN, 0, 0, 0); - while(processctl(tb, PCTL_POLLSTATE, 0, 0, 0) != 4) { - int32_t kbchr; - int32_t read = ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_READ, (uint8_t *)&kbchr, sizeof(kbchr)); - if (read > 0) { - uint8_t c = kbchr & 0xff; - if (string_chr_isascii(c)) { - if (c != 0) { - ipcpipe(tb, IPCPIPE_IN, IPCPIPE_WRITE, &c, 1); - } - } - } - } + while(processctl(tb, PCTL_POLLSTATE, 0, 0, 0) != 4); } void main(void) { diff --git a/user/tb/main.c b/user/tb/main.c index 402b9dc..4e1dac5 100644 --- a/user/tb/main.c +++ b/user/tb/main.c @@ -15,6 +15,11 @@ #include #include "interp.h" +// keys +#define C(X) ((X)-'@') + +#define LINEBUF_MAX 1024 + uint64_t PID; struct { @@ -26,8 +31,6 @@ struct { bool logcmds; } CONFIG; -#define LINEBUF_MAX 1024 - static Arg ARGS[] = { ARG("-m", ARG_STRING, &CONFIG.modestr), ARG("-f", ARG_STRING, &CONFIG.filepath), @@ -99,19 +102,40 @@ void do_mode_interactive(void) { char linebuf[LINEBUF_MAX]; size_t cursor; for(;;) { + begin: uprintf("tb# "); cursor = 0; string_memset(linebuf, 0, LINEBUF_MAX); - char c = 0; - while (c != '\n') { - int32_t rd = ipcpipe(PID, IPCPIPE_IN, IPCPIPE_READ, (uint8_t *)&c, 1); - if (rd > 0 && cursor < LINEBUF_MAX) { - linebuf[cursor++] = c; - uprintf("%c", c); + + int32_t kbchr = 0; + for (;;) { + int32_t nrd = ipcpipe(PID, IPCPIPE_IN, IPCPIPE_READ, (uint8_t *)&kbchr, sizeof(kbchr)); + if (nrd > 0) { + switch (kbchr) { + case C('C'): + case 0xE9: + uprintf("\n"); + goto begin; + break; + } + + char chr = kbchr & 0xFF; + + if (chr == '\n') { + break; + } + + if (string_chr_isascii(chr) && chr != 0 && cursor < LINEBUF_MAX) { + linebuf[cursor++] = chr; + uprintf("%c", chr); + } } } - linebuf[cursor - 1] = '\0'; + + if (cursor < LINEBUF_MAX) { + linebuf[cursor] = '\0'; + } uprintf("\n"); InterpResult *res; if (!interp_runstring(linebuf, &res, CONFIG.logcmds)) {