tb Implement a string stack

This commit is contained in:
2025-10-01 21:26:22 +02:00
parent 62cf07afc7
commit 73effcd52a
4 changed files with 101 additions and 13 deletions

View File

@ -122,15 +122,30 @@ void tz_expandspecial(Tokenizer *tz) {
Token *tk, *tktmp;
LL_FOREACH_SAFE(tz->tokens, tk, tktmp) {
if (tk->str[0] == '$' && string_len(tk->str) > 1) {
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;
}
}
}
}