tb Command-like scripts
This commit is contained in:
@ -136,29 +136,46 @@ void tz_expandspecial(Tokenizer *tz) {
|
|||||||
Token *tk, *tktmp;
|
Token *tk, *tktmp;
|
||||||
LL_FOREACH_SAFE(tz->tokens, tk, tktmp) {
|
LL_FOREACH_SAFE(tz->tokens, tk, tktmp) {
|
||||||
if (tk->str[0] == '$' && string_len(tk->str) > 1) {
|
if (tk->str[0] == '$' && string_len(tk->str) > 1) {
|
||||||
if (string_strcmp(tk->str, "$stack") == 0) {
|
bool isnum;
|
||||||
char *s = rtstringv_stackpeek();
|
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);
|
ufree(tk->str);
|
||||||
if (s == NULL) {
|
char *s = idx < argslen() ? args()[idx] : "";
|
||||||
tk->str = umalloc(1);
|
size_t len = string_len(s);
|
||||||
tk->str[0] = '\0';
|
char *copy = umalloc(len+1);
|
||||||
} else {
|
string_memcpy(copy, s, len);
|
||||||
size_t len = string_len(s);
|
copy[len] = '\0';
|
||||||
char *copy = umalloc(len+1);
|
tk->str = copy;
|
||||||
string_memcpy(copy, s, len);
|
|
||||||
copy[len] = '\0';
|
|
||||||
tk->str = copy;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
RtAlias *alias, *aliastmp;
|
if (string_strcmp(tk->str, "$stack") == 0) {
|
||||||
LL_FOREACH_SAFE(RTALIASES, alias, aliastmp) {
|
char *s = rtstringv_stackpeek();
|
||||||
if (string_strcmp(alias->namebuf, tk->str+1) == 0) {
|
ufree(tk->str);
|
||||||
ufree(tk->str);
|
if (s == NULL) {
|
||||||
size_t len = string_len(alias->valbuf)+1;
|
tk->str = umalloc(1);
|
||||||
tk->str = umalloc(len);
|
tk->str[0] = '\0';
|
||||||
string_memset(tk->str, 0, len);
|
} else {
|
||||||
string_memcpy(tk->str, alias->valbuf, string_len(alias->valbuf));
|
size_t len = string_len(s);
|
||||||
break;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -134,6 +134,16 @@ void do_mode_interactive(void) {
|
|||||||
void main(void) {
|
void main(void) {
|
||||||
PID = proc_getpid();
|
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();
|
set_config();
|
||||||
|
|
||||||
do_file("base:/scripts/rc.tb");
|
do_file("base:/scripts/rc.tb");
|
||||||
|
|||||||
Reference in New Issue
Block a user