tb Command-like scripts

This commit is contained in:
2025-10-19 23:00:46 +02:00
parent 01da863b8b
commit a96ea5edb6
2 changed files with 48 additions and 21 deletions

View File

@ -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;
}
}
}
}