diff --git a/user/tb/interp.c b/user/tb/interp.c index aa3a0ac..f362274 100644 --- a/user/tb/interp.c +++ b/user/tb/interp.c @@ -136,29 +136,46 @@ void tz_expandspecial(Tokenizer *tz) { Token *tk, *tktmp; LL_FOREACH_SAFE(tz->tokens, tk, tktmp) { if (tk->str[0] == '$' && string_len(tk->str) > 1) { - if (string_strcmp(tk->str, "$stack") == 0) { - char *s = rtstringv_stackpeek(); + bool isnum; + STRING_CHECK_ALL(tk->str + 1, string_chr_isdigit, isnum); + if (isnum) { + // this is a special case for command-like scripts + char *endp; + unsigned long idx = string_conv_strtoul(tk->str + 1, &endp, 10); + idx += 2; // skip command itself and indicator / '.' + ufree(tk->str); - if (s == NULL) { - tk->str = umalloc(1); - tk->str[0] = '\0'; - } else { - size_t len = string_len(s); - char *copy = umalloc(len+1); - string_memcpy(copy, s, len); - copy[len] = '\0'; - tk->str = copy; - } + char *s = idx < argslen() ? args()[idx] : ""; + size_t len = string_len(s); + char *copy = umalloc(len+1); + string_memcpy(copy, s, len); + copy[len] = '\0'; + tk->str = copy; } else { - RtAlias *alias, *aliastmp; - LL_FOREACH_SAFE(RTALIASES, alias, aliastmp) { - if (string_strcmp(alias->namebuf, tk->str+1) == 0) { - ufree(tk->str); - size_t len = string_len(alias->valbuf)+1; - tk->str = umalloc(len); - string_memset(tk->str, 0, len); - string_memcpy(tk->str, alias->valbuf, string_len(alias->valbuf)); - break; + if (string_strcmp(tk->str, "$stack") == 0) { + char *s = rtstringv_stackpeek(); + ufree(tk->str); + if (s == NULL) { + tk->str = umalloc(1); + tk->str[0] = '\0'; + } else { + size_t len = string_len(s); + char *copy = umalloc(len+1); + string_memcpy(copy, s, len); + copy[len] = '\0'; + tk->str = copy; + } + } else { + RtAlias *alias, *aliastmp; + LL_FOREACH_SAFE(RTALIASES, alias, aliastmp) { + if (string_strcmp(alias->namebuf, tk->str+1) == 0) { + ufree(tk->str); + size_t len = string_len(alias->valbuf)+1; + tk->str = umalloc(len); + string_memset(tk->str, 0, len); + string_memcpy(tk->str, alias->valbuf, string_len(alias->valbuf)); + break; + } } } } diff --git a/user/tb/main.c b/user/tb/main.c index 2d6069a..a3e5dad 100644 --- a/user/tb/main.c +++ b/user/tb/main.c @@ -134,6 +134,16 @@ void do_mode_interactive(void) { void main(void) { PID = proc_getpid(); + // short command-like script was supplied + if (argslen() > 0 && args()[0][0] == '.') { + do_file("base:/scripts/rc.tb"); + char *path = umalloc(4096); + usprintf(path, "base:/scripts/%s.tb", args()[1]); + do_file(path); + ufree(path); + return; + } + set_config(); do_file("base:/scripts/rc.tb");