From 40b7dcedf82952d0e4b1bf3ac56dbb1e418774bf Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Fri, 19 Sep 2025 19:55:35 +0200 Subject: [PATCH] tb Command aliases, preloading scripts --- base/scripts/init.tb | 2 +- base/scripts/rc.tb | 1 + user/tb/interp.c | 24 ++++++++++++++++++++++-- user/tb/main.c | 14 ++++++++++---- user/tb/runtime.c | 22 ++++++++++++++++++++++ user/tb/runtime.h | 10 ++++++++++ 6 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 base/scripts/rc.tb diff --git a/base/scripts/init.tb b/base/scripts/init.tb index 7b873c3..4d715a9 100644 --- a/base/scripts/init.tb +++ b/base/scripts/init.tb @@ -1,3 +1,3 @@ @print "this is an init script!" base:/bin/pctl ls -base:/bin/tb -m interactive +base:/bin/tb -m interactive -preload base:/scripts/rc.tb diff --git a/base/scripts/rc.tb b/base/scripts/rc.tb new file mode 100644 index 0000000..74e6f70 --- /dev/null +++ b/base/scripts/rc.tb @@ -0,0 +1 @@ +@mkalias pctl base:/bin/pctl diff --git a/user/tb/interp.c b/user/tb/interp.c index 69ba865..e61e91c 100644 --- a/user/tb/interp.c +++ b/user/tb/interp.c @@ -13,8 +13,6 @@ #include "interp.h" #include "runtime.h" -/* #define SUBPROC_PIPE_OUT 31 */ - extern uint64_t PID; static InterpResult RES; @@ -98,6 +96,27 @@ void tz_classify(Tokenizer *tz) { dlfree(tmpbuf); } +void tz_expandspecial(Tokenizer *tz) { + Token *tk = tz->tokens; + while (tk) { + if (tk->ptr[0] == '$' && tk->len > 1) { + char *aliasbuf = dlmalloc(RTALIAS_NAMEBUF_MAX); + usnprintf(aliasbuf, RTALIAS_NAMEBUF_MAX, "%.*s", (int)tk->len-1, &tk->ptr[1]); + RtAlias *alias = RTALIASES; + while (alias) { + if (string_strcmp(alias->namebuf, aliasbuf) == 0) { + tk->ptr = alias->valbuf; + tk->len = string_len(alias->valbuf); + break; + } + alias = alias->next; + } + dlfree(aliasbuf); + } + tk = tk->next; + } +} + bool interp_readline(char *data, const char **bgptr, const char **endptr) { static char *nextstart; if (data) { @@ -178,6 +197,7 @@ bool interp_runstring(const char *string, InterpResult **res, bool logcmds) { } tz_classify(&tz); + tz_expandspecial(&tz); Token *cmdtk = tz.tokens; if (cmdtk->type == TOK_CMD) { diff --git a/user/tb/main.c b/user/tb/main.c index 4e1dac5..21b8c01 100644 --- a/user/tb/main.c +++ b/user/tb/main.c @@ -29,15 +29,20 @@ struct { char *filepath; bool logcmds; + + char *preloadpath; } CONFIG; static Arg ARGS[] = { ARG("-m", ARG_STRING, &CONFIG.modestr), ARG("-f", ARG_STRING, &CONFIG.filepath), ARG("-logcmds", ARG_BOOL, &CONFIG.logcmds), + ARG("-preload", ARG_STRING, &CONFIG.preloadpath), ARG_END(), }; +void do_file(char *filepath); + void set_config(void) { int32_t ret; if ((ret = parse_args(args(), argslen(), ARGS)) < 0) { @@ -57,10 +62,6 @@ void set_config(void) { } } -void process_cmd(char *cmdtext) { - -} - void do_file(char *filepath) { int32_t ret; @@ -148,6 +149,11 @@ void main(void) { PID = processctl(-1, PCTL_GETPID, 0, 0, 0); set_config(); + + if (CONFIG.preloadpath != NULL) { + LOG(LOG_INF, "Preloading script: %s\n", CONFIG.preloadpath); + do_file(CONFIG.preloadpath); + } if (CONFIG.mode == MODE_INTERACTIVE) { do_mode_interactive(); diff --git a/user/tb/runtime.c b/user/tb/runtime.c index 769e0e1..8dce508 100644 --- a/user/tb/runtime.c +++ b/user/tb/runtime.c @@ -2,11 +2,13 @@ #include #include #include +#include #include #include "runtime.h" #include "interp.h" RtCmd *RTCMDS = NULL; +RtAlias *RTALIASES = NULL; #define RTCMD(name, _fn) \ do { \ @@ -29,6 +31,26 @@ bool rt_print(Token *tks) { return true; } +bool rt_mkalias(Token *tks) { + RtAlias *alias = dlmalloc(sizeof(*alias)); + string_memset(alias, 0, sizeof(*alias)); + Token *tk = tks; + size_t i = 0; + while (tk) { + if (i == 0) { + usprintf(alias->namebuf, "%.*s", (int)tk->len, tk->ptr); + } else { + size_t off = string_len(alias->valbuf); + usprintf(alias->valbuf + off, "%.*s", (int)tk->len, tk->ptr); + } + i++; + tk = tk->next; + } + LL_APPEND(RTALIASES, alias); + return true; +} + void rt_init(void) { RTCMD("@print", &rt_print); + RTCMD("@mkalias", &rt_mkalias); } diff --git a/user/tb/runtime.h b/user/tb/runtime.h index 33fbecd..9287488 100644 --- a/user/tb/runtime.h +++ b/user/tb/runtime.h @@ -11,8 +11,18 @@ typedef struct RtCmd { bool (*fn)(struct Token *tks); } RtCmd; +#define RTALIAS_NAMEBUF_MAX 0x100 +#define RTALIAS_VALBUF_MAX 0x1000 + +typedef struct RtAlias { + struct RtAlias *next; + char namebuf[RTALIAS_NAMEBUF_MAX]; + char valbuf[RTALIAS_VALBUF_MAX]; +} RtAlias; + void rt_init(void); extern RtCmd *RTCMDS; +extern RtAlias *RTALIASES; #endif // TB_RUNTIME_H_