From 222e846881b81d2eb782335cb75a5d65f9d39068 Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Sat, 20 Sep 2025 12:41:54 +0200 Subject: [PATCH] tb Allow the user to kill current process in interactive mode (Ctrl+S) --- user/tb/interp.c | 14 ++++++++++++-- user/tb/interp.h | 2 +- user/tb/macros.h | 7 +++++++ user/tb/main.c | 8 +++----- 4 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 user/tb/macros.h diff --git a/user/tb/interp.c b/user/tb/interp.c index b293fb5..355778a 100644 --- a/user/tb/interp.c +++ b/user/tb/interp.c @@ -3,6 +3,7 @@ #include #include "interp.h" #include "runtime.h" +#include "macros.h" extern uint64_t PID; @@ -161,7 +162,7 @@ bool interp_readline(char *data, const char **bgptr, const char **endptr) { return true; } -bool interp_runstring(const char *string, InterpResult **res, bool logcmds) { +bool interp_runstring(const char *string, InterpResult **res, bool logcmds, bool interactive) { *res = &RES; string_memset(RES.errmsg, 0, sizeof(RES.errmsg)); @@ -230,16 +231,25 @@ bool interp_runstring(const char *string, InterpResult **res, bool logcmds) { processctl(app, PCTL_RUN, 0, 0, 0); + uint8_t b; while(processctl(app, PCTL_POLLSTATE, 0, 0, 0) != 4) { + if (interactive) { + int32_t nrd = ipcpipe(PID, IPCPIPE_IN, IPCPIPE_READ, &b, 1); + if (nrd > 0 && b == C('S')) { + processctl(app, PCTL_KILL, 0, 0, 0); + goto cleanup; + } + } schedrelease(); } - cleanup: + cleanup: { for (size_t j = 0; j < argslen1; j++) { dlfree(args1[j]); } dlfree(args1); dlfree(appname); + } } tz_free(&tz); diff --git a/user/tb/interp.h b/user/tb/interp.h index 8c686d2..6c46f13 100644 --- a/user/tb/interp.h +++ b/user/tb/interp.h @@ -33,6 +33,6 @@ typedef struct { Token *tokens; } Tokenizer; -bool interp_runstring(const char *string, InterpResult **res, bool logcmds); +bool interp_runstring(const char *string, InterpResult **res, bool logcmds, bool interactive); #endif // TB_INTERP_H_ diff --git a/user/tb/macros.h b/user/tb/macros.h new file mode 100644 index 0000000..8e6d04e --- /dev/null +++ b/user/tb/macros.h @@ -0,0 +1,7 @@ +#ifndef TB_MACROS_H_ +#define TB_MACROS_H_ + +// keys +#define C(X) ((X)-'@') + +#endif // TB_MACROS_H_ diff --git a/user/tb/main.c b/user/tb/main.c index d8314f9..07626d4 100644 --- a/user/tb/main.c +++ b/user/tb/main.c @@ -2,9 +2,7 @@ #include #include #include "interp.h" - -// keys -#define C(X) ((X)-'@') +#include "macros.h" #define LINEBUF_MAX 1024 @@ -75,7 +73,7 @@ void do_file(char *filepath) { } InterpResult *res; - bool ok = interp_runstring((const char *)buf, &res, CONFIG.logcmds); + bool ok = interp_runstring((const char *)buf, &res, CONFIG.logcmds, CONFIG.mode == MODE_INTERACTIVE); if (!ok) { uprintf("Interpreter error:\n"); uprintf("%s\n", res->errmsg); @@ -130,7 +128,7 @@ void do_mode_interactive(void) { } uprintf("\n"); InterpResult *res; - if (!interp_runstring(linebuf, &res, CONFIG.logcmds)) { + if (!interp_runstring(linebuf, &res, CONFIG.logcmds, CONFIG.mode == MODE_INTERACTIVE)) { LOG(LOG_ERR, "%s\n", res->errmsg); } }