tb Handle keyboard inside of the shell interactive mode

This commit is contained in:
2025-09-19 19:09:54 +02:00
parent d7153bf0b6
commit 24a90b24e8
2 changed files with 35 additions and 23 deletions

View File

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

View File

@ -15,6 +15,11 @@
#include <util/util.h>
#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)) {