Compare commits

...

3 Commits

Author SHA1 Message Date
24a90b24e8 tb Handle keyboard inside of the shell interactive mode 2025-09-19 19:09:54 +02:00
d7153bf0b6 pctl display bad subcommand 2025-09-19 18:24:48 +02:00
ad56890ee9 Adopt pipe handle inheritance 2025-09-19 18:12:36 +02:00
5 changed files with 51 additions and 62 deletions

View File

@ -1,2 +1,3 @@
@print "this is an init script!" @print "this is an init script!"
base:/bin/pctl ls base:/bin/pctl ls
base:/bin/tb -m interactive

View File

@ -10,45 +10,25 @@
#include <dlmalloc/malloc.h> #include <dlmalloc/malloc.h>
#include <errors.h> #include <errors.h>
#define SUBPROC_PIPE_OUT 0x1f
uint64_t PID; uint64_t PID;
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(tb, IPCPIPE_IN, IPCPIPE_ADD_BCAST, NULL, 1);
processctl(tb, PCTL_RUN, 0, 0, 0);
while(processctl(tb, PCTL_POLLSTATE, 0, 0, 0) != 4);
}
void main(void) { void main(void) {
PID = (uint64_t)processctl(-1, PCTL_GETPID, 0, 0, 0); PID = (uint64_t)processctl(-1, PCTL_GETPID, 0, 0, 0);
ipcpipe(PID, SUBPROC_PIPE_OUT, IPCPIPE_MAKE, NULL, 0); tb_runinitscript();
char *tbargs[] = { "-m", "runfile", "-f", "base:/scripts/init.tb", "-logcmds", "yes" }; uprintf("Shell exited! Please reboot the system.\n");
int32_t tb = processctl(-1, PCTL_SPAWN, (uint64_t)"base:/bin/tb", (uint64_t)&tbargs, ARRLEN(tbargs));
ipcpipe(tb, IPCPIPE_OUT, IPCPIPE_REPLACE, (uint8_t *)PID, SUBPROC_PIPE_OUT);
ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_MAKE, NULL, 0);
ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_ADD_BCAST, NULL, 1);
#define OUTBUF_MAX 1024
char *outbuf = dlmalloc(OUTBUF_MAX);
processctl(tb, PCTL_RUN, 0, 0, 0);
for(;;) {
string_memset(outbuf, 0, OUTBUF_MAX);
int32_t nrd = ipcpipe(PID, SUBPROC_PIPE_OUT, IPCPIPE_READ, (uint8_t *)outbuf, sizeof(outbuf));
if (nrd > 0) {
uprintf("%s", outbuf);
}
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);
}
}
}
}
for(;;); for(;;);
} }

View File

@ -15,6 +15,6 @@ void main(void) {
if (string_strcmp(cmd, "ls") == 0) { if (string_strcmp(cmd, "ls") == 0) {
pctl_ls(); pctl_ls();
} else { } else {
uprintf("pctl: unknown command\n"); uprintf("pctl: unknown command %s\n", cmd);
} }
} }

View File

@ -13,7 +13,7 @@
#include "interp.h" #include "interp.h"
#include "runtime.h" #include "runtime.h"
#define SUBPROC_PIPE_OUT 0x1f /* #define SUBPROC_PIPE_OUT 31 */
extern uint64_t PID; extern uint64_t PID;
@ -210,34 +210,18 @@ bool interp_runstring(const char *string, InterpResult **res, bool logcmds) {
argtk = argtk->next; argtk = argtk->next;
} }
#define OUTBUF_MAX 1024 int32_t app = processctl(-1, PCTL_SPAWN, (uint64_t)appname, (uint64_t)args1, argslen1);
char *outbuf = dlmalloc(OUTBUF_MAX);
string_memset(outbuf, 0, OUTBUF_MAX);
ipcpipe(PID, SUBPROC_PIPE_OUT, IPCPIPE_MAKE, NULL, 0);
int32_t app = processctl(-1, PCTL_SPAWN, (uint64_t)(char *)appname, (uint64_t)args1, argslen1);
if (app < 0) { if (app < 0) {
usprintf(RES.errmsg, "Could not run %s: %s\n", appname, ERRSTRING(app)); usprintf(RES.errmsg, "Could not run %s: %s\n", appname, ERRSTRING(app));
ok = false; ok = false;
goto cleanup; goto cleanup;
} }
ipcpipe(app, IPCPIPE_OUT, IPCPIPE_REPLACE, (uint8_t *)PID, SUBPROC_PIPE_OUT);
ipcpipe(app, IPCPIPE_IN, IPCPIPE_REPLACE, (uint8_t *)PID, IPCPIPE_IN);
processctl(app, PCTL_RUN, 0, 0, 0); processctl(app, PCTL_RUN, 0, 0, 0);
do { while(processctl(app, PCTL_POLLSTATE, 0, 0, 0) != 4);
int32_t nrd = ipcpipe(PID, SUBPROC_PIPE_OUT, IPCPIPE_READ, (uint8_t *)outbuf, OUTBUF_MAX);
if (nrd > 0) {
uprintf("%s", outbuf);
string_memset(outbuf, 0, OUTBUF_MAX);
}
} while(processctl(app, PCTL_POLLSTATE, 0, 0, 0) != 4);
cleanup: cleanup:
ipcpipe(PID, SUBPROC_PIPE_OUT, IPCPIPE_DELETE, NULL, 0);
dlfree(outbuf);
for (size_t j = 0; j < argslen1; j++) { for (size_t j = 0; j < argslen1; j++) {
dlfree(args1[j]); dlfree(args1[j]);
} }

View File

@ -15,6 +15,11 @@
#include <util/util.h> #include <util/util.h>
#include "interp.h" #include "interp.h"
// keys
#define C(X) ((X)-'@')
#define LINEBUF_MAX 1024
uint64_t PID; uint64_t PID;
struct { struct {
@ -26,8 +31,6 @@ struct {
bool logcmds; bool logcmds;
} CONFIG; } CONFIG;
#define LINEBUF_MAX 1024
static Arg ARGS[] = { static Arg ARGS[] = {
ARG("-m", ARG_STRING, &CONFIG.modestr), ARG("-m", ARG_STRING, &CONFIG.modestr),
ARG("-f", ARG_STRING, &CONFIG.filepath), ARG("-f", ARG_STRING, &CONFIG.filepath),
@ -99,19 +102,40 @@ void do_mode_interactive(void) {
char linebuf[LINEBUF_MAX]; char linebuf[LINEBUF_MAX];
size_t cursor; size_t cursor;
for(;;) { for(;;) {
begin:
uprintf("tb# "); uprintf("tb# ");
cursor = 0; cursor = 0;
string_memset(linebuf, 0, LINEBUF_MAX); string_memset(linebuf, 0, LINEBUF_MAX);
char c = 0;
while (c != '\n') { int32_t kbchr = 0;
int32_t rd = ipcpipe(PID, IPCPIPE_IN, IPCPIPE_READ, (uint8_t *)&c, 1); for (;;) {
if (rd > 0 && cursor < LINEBUF_MAX) { int32_t nrd = ipcpipe(PID, IPCPIPE_IN, IPCPIPE_READ, (uint8_t *)&kbchr, sizeof(kbchr));
linebuf[cursor++] = c; if (nrd > 0) {
uprintf("%c", c); 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"); uprintf("\n");
InterpResult *res; InterpResult *res;
if (!interp_runstring(linebuf, &res, CONFIG.logcmds)) { if (!interp_runstring(linebuf, &res, CONFIG.logcmds)) {