From a46da0a7ae25fa07b1f4c57418e661d70a9b90e3 Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Sat, 18 Oct 2025 10:48:31 +0200 Subject: [PATCH] tb Enable/disable command logging dynamically + setlogcmds builtin --- user/tb/config.h | 20 ++++++++++++++++++++ user/tb/interp.c | 6 ++++-- user/tb/interp.h | 2 +- user/tb/main.c | 22 +++++----------------- user/tb/runtime.c | 26 +++++++++++++++++++++++++- user/tb/runtime.h | 1 + 6 files changed, 56 insertions(+), 21 deletions(-) create mode 100644 user/tb/config.h diff --git a/user/tb/config.h b/user/tb/config.h new file mode 100644 index 0000000..a8b48f9 --- /dev/null +++ b/user/tb/config.h @@ -0,0 +1,20 @@ +#ifndef TB_CONFIG_H_ +#define TB_CONFIG_H_ +typedef struct { + char *modestr; + enum { + MODE_INTERACTIVE = 1, + MODE_RUNFILE = 2, + MODE_RUNSTRING = 3, + } mode; + + char *filepath; + + bool logcmds; + + char *runstring; +} Config; + +extern Config CONFIG; + +#endif // TB_CONFIG_H_ diff --git a/user/tb/interp.c b/user/tb/interp.c index e04c3a1..c2bf1d5 100644 --- a/user/tb/interp.c +++ b/user/tb/interp.c @@ -4,6 +4,7 @@ #include "interp.h" #include "runtime.h" #include "macros.h" +#include "config.h" extern PID_t PID; extern Dev_t ps2kbdev; @@ -124,6 +125,7 @@ void tz_classify(Tokenizer *tz) { else IF_RTCMD(stackpop) else IF_RTCMD(eachfile) else IF_RTCMD(mkaliasbn) + else IF_RTCMD(setlogcmds) else { tk->type = TOK_MISC; } @@ -166,7 +168,7 @@ void tz_expandspecial(Tokenizer *tz) { #define LINE_MAX 1024 -bool interp_runstring(char *string, InterpResult **res, bool logcmds, bool interactive) { +bool interp_runstring(char *string, InterpResult **res, bool interactive) { *res = &RES; string_memset(RES.errmsg, 0, sizeof(RES.errmsg)); @@ -175,7 +177,7 @@ bool interp_runstring(char *string, InterpResult **res, bool logcmds, bool inter char *line = string_tokenizealloc_linecontinue(string, "\n"); while (line != NULL) { - if (logcmds) { + if (CONFIG.logcmds) { uprintf("+%s\n", line); } diff --git a/user/tb/interp.h b/user/tb/interp.h index 902b3fc..ca5c2fb 100644 --- a/user/tb/interp.h +++ b/user/tb/interp.h @@ -31,6 +31,6 @@ typedef struct { Token *tokens; } Tokenizer; -bool interp_runstring(char *string, InterpResult **res, bool logcmds, bool interactive); +bool interp_runstring(char *string, InterpResult **res, bool interactive); #endif // TB_INTERP_H_ diff --git a/user/tb/main.c b/user/tb/main.c index 48098ff..95ca7ce 100644 --- a/user/tb/main.c +++ b/user/tb/main.c @@ -3,26 +3,14 @@ #include #include "interp.h" #include "macros.h" +#include "config.h" #define LINEBUF_MAX 1024 PID_t PID; Dev_t ps2kbdev; -struct { - char *modestr; - enum { - MODE_INTERACTIVE = 1, - MODE_RUNFILE = 2, - MODE_RUNSTRING = 3, - } mode; - - char *filepath; - - bool logcmds; - - char *runstring; -} CONFIG; +Config CONFIG; static Arg ARGS[] = { ARG("-m", ARG_STRING, &CONFIG.modestr), @@ -78,7 +66,7 @@ void do_file(char *filepath) { } InterpResult *res; - bool ok = interp_runstring((char *)buf, &res, CONFIG.logcmds, CONFIG.mode == MODE_INTERACTIVE); + bool ok = interp_runstring((char *)buf, &res, CONFIG.mode == MODE_INTERACTIVE); if (!ok) { uprintf("Interpreter error:\n"); uprintf("%s\n", res->errmsg); @@ -137,7 +125,7 @@ void do_mode_interactive(void) { uprintf(ANSIQ_GR_RESET); uprintf("\n"); InterpResult *res; - if (!interp_runstring(linebuf, &res, CONFIG.logcmds, CONFIG.mode == MODE_INTERACTIVE)) { + if (!interp_runstring(linebuf, &res, CONFIG.mode == MODE_INTERACTIVE)) { LOG(LOG_ERR, "%s\n", res->errmsg); } } @@ -173,7 +161,7 @@ void main(void) { if (CONFIG.runstring[len] == '\'') { CONFIG.runstring[len] = ' '; } - if (!interp_runstring(CONFIG.runstring, &res, CONFIG.logcmds, false)) { + if (!interp_runstring(CONFIG.runstring, &res, false)) { LOG(LOG_ERR, "%s\n", res->errmsg); } } diff --git a/user/tb/runtime.c b/user/tb/runtime.c index 4763039..9c2097e 100644 --- a/user/tb/runtime.c +++ b/user/tb/runtime.c @@ -3,6 +3,7 @@ #include #include "runtime.h" #include "interp.h" +#include "config.h" RtStringV *RTSTRINGV_STACK = NULL; @@ -242,7 +243,7 @@ bool rt_eachfile(Token *tks) { InterpResult save; string_memcpy(&save, &RES, sizeof(save)); InterpResult *res; - interp_runstring(s, &res, false, false); + interp_runstring(s, &res, false); string_memcpy(&RES, &save, sizeof(save)); /* ufree(s); */ @@ -278,6 +279,28 @@ bool rt_mkaliasbn(Token *tks) { return true; } +bool rt_setlogcmds(Token *tks) { + Token *tk = tks; + + if (tk == NULL) { + return false; + } + + bool set; + + if (string_strcmp(tk->str, "yes") == 0) { + set = true; + } else if (string_strcmp(tk->str, "no") == 0) { + set = false; + } else { + return false; + } + + CONFIG.logcmds = set; + + return true; +} + void rt_init(void) { RTCMD("print", &rt_print); RTCMD("mkalias", &rt_mkalias); @@ -287,4 +310,5 @@ void rt_init(void) { RTCMD("stackpop", &rt_stackpop); RTCMD("eachfile", &rt_eachfile); RTCMD("mkaliasbn", &rt_mkaliasbn); + RTCMD("setlogcmds", &rt_setlogcmds); } diff --git a/user/tb/runtime.h b/user/tb/runtime.h index 95882d4..19a30ff 100644 --- a/user/tb/runtime.h +++ b/user/tb/runtime.h @@ -38,6 +38,7 @@ bool rt_stackpush(struct Token *); bool rt_stackpop(struct Token *); bool rt_eachfile(struct Token *); bool rt_mkaliasbn(struct Token *); +bool rt_setlogcmds(struct Token *); void rtstringv_stackpushcopy(char *s, size_t len); char *rtstringv_stackpop(void);