Get rid of writefmt functions

This commit is contained in:
2025-09-28 20:10:41 +02:00
parent 96ce9233ff
commit 1fd6c4e221
15 changed files with 80 additions and 262 deletions

View File

@ -1,7 +1,7 @@
ENTRY(_start)
SECTIONS {
. = 0x1000;
. = 0x400000;
.text ALIGN(4K):
{

View File

@ -21,7 +21,7 @@ void main(void) {
tb_runinitscript();
writefmt("Shell exited! Please reboot the system.\n");
uprintf("Shell exited! Please reboot the system.\n");
for(;;);
}

View File

@ -17,7 +17,8 @@ void tz_init(Tokenizer *tz, char *str) {
void tz_free(Tokenizer *tz) {
Token *tk, *tktmp;
LL_FOREACH_SAFE(tz->tokens, tk, tktmp) {
dlfree(tk);
ufree(tk->str);
ufree(tk);
}
tz->tokens = NULL;
}
@ -54,50 +55,44 @@ void tz_tokenize(Tokenizer *tz) {
}
}
/* void tz_classify(Tokenizer *tz) { */
/* const int tmpbufsz = 256; */
/* char *tmpbuf = dlmalloc(tmpbufsz); */
void tz_classify(Tokenizer *tz) {
Token *tk, *tktmp;
LL_FOREACH_SAFE(tz->tokens, tk, tktmp) {
if (tk->str[0] == '"') {
tk->type = TOK_STRING;
} else if (tk->str[0] == '%') {
RtCmd *cmd, *cmdtmp;
LL_FOREACH_SAFE(RTCMDS, cmd, cmdtmp) {
if (string_strcmp(tk->str, cmd->cmdname) == 0) {
tk->type = TOK_CMD;
tk->cmd = cmd;
break;
}
}
} else {
tk->type = TOK_MISC;
}
}
}
/* Token *tk, *tktmp; */
/* LL_FOREACH_SAFE(tz->tokens, tk, tktmp) { */
/* if (tk->ptr[0] == '"' && tk->ptr[tk->len - 1] == '"') { */
/* tk->type = TOK_STRING; */
/* } else if (tk->ptr[0] == '%') { */
/* RtCmd *cmd, *cmdtmp; */
/* LL_FOREACH_SAFE(RTCMDS, cmd, cmdtmp) { */
/* string_memset(tmpbuf, 0, tmpbufsz); */
/* string_memcpy(tmpbuf, tk->ptr, MIN(tk->len, tmpbufsz)); */
/* if (string_strcmp(tmpbuf, cmd->cmdname) == 0) { */
/* tk->type = TOK_CMD; */
/* tk->cmd = cmd; */
/* break; */
/* } */
/* } */
/* } else { */
/* tk->type = TOK_MISC; */
/* } */
/* } */
/* dlfree(tmpbuf); */
/* } */
/* void tz_expandspecial(Tokenizer *tz) { */
/* Token *tk, *tktmp; */
/* LL_FOREACH_SAFE(tz->tokens, tk, tktmp) { */
/* if (tk->ptr[0] == '$' && tk->len > 1) { */
/* char aliasbuf[RTALIAS_NAMEBUF_MAX]; */
/* string_memset(aliasbuf, 0, sizeof(aliasbuf)); */
/* string_memcpy(aliasbuf, &tk->ptr[1], MIN(tk->len - 1, RTALIAS_NAMEBUF_MAX)); */
/* RtAlias *alias, *aliastmp; */
/* LL_FOREACH_SAFE(RTALIASES, alias, aliastmp) { */
/* if (string_strcmp(alias->namebuf, aliasbuf) == 0) { */
/* tk->ptr = alias->valbuf; */
/* tk->len = string_len(alias->valbuf); */
/* break; */
/* } */
/* } */
/* } */
/* } */
/* } */
void tz_expandspecial(Tokenizer *tz) {
Token *tk, *tktmp;
LL_FOREACH_SAFE(tz->tokens, tk, tktmp) {
/* if (tk->ptr[0] == '$' && tk->len > 1) { */
/* char aliasbuf[RTALIAS_NAMEBUF_MAX]; */
/* string_memset(aliasbuf, 0, sizeof(aliasbuf)); */
/* string_memcpy(aliasbuf, &tk->ptr[1], MIN(tk->len - 1, RTALIAS_NAMEBUF_MAX)); */
/* RtAlias *alias, *aliastmp; */
/* LL_FOREACH_SAFE(RTALIASES, alias, aliastmp) { */
/* if (string_strcmp(alias->namebuf, aliasbuf) == 0) { */
/* tk->ptr = alias->valbuf; */
/* tk->len = string_len(alias->valbuf); */
/* break; */
/* } */
/* } */
/* } */
}
}
#define LINE_MAX 1024
@ -110,27 +105,32 @@ bool interp_runstring(char *string, InterpResult **res, bool logcmds, bool inter
char *line = string_tokenizealloc(string, "\n");
while (line != NULL) {
if (logcmds) {
writefmt("+{s}\n", line);
uprintf("+%s\n", line);
}
Tokenizer tz; ZERO(&tz);
tz_init(&tz, line);
tz_tokenize(&tz);
/* Token tktmp; ZERO(&tktmp); */
/* while (tz_next(&tz, &tktmp)) { */
/* Token *tk = dlmalloc(sizeof(*tk)); */
/* tk->ptr = tktmp.ptr; */
/* tk->len = tktmp.len; */
/* LL_APPEND(tz.tokens, tk); */
/* } */
/* tz_classify(&tz); */
/* tz_expandspecial(&tz); */
tz_classify(&tz);
tz_expandspecial(&tz);
/* dlfree((void *)line); */
Token *cmdtk = tz.tokens;
if (cmdtk->type == TOK_CMD) {
ok = cmdtk->cmd->fn(cmdtk->next);
if (!ok) {
usprintf(RES.errmsg, "cmd %s failed", cmdtk->str);
goto next;
}
} else if (cmdtk->type == TOK_MISC) {
}
next:
tz_free(&tz);
ufree(line);
line = string_tokenizealloc(NULL, "\n");
}

View File

@ -32,7 +32,7 @@ void do_file(char *filepath);
void set_config(void) {
int32_t ret;
if ((ret = parse_args(args(), argslen(), ARGS)) < 0) {
writefmt("Could not parse args: {d}\n", ret);
uprintf("Could not parse args: %d\n", ret);
}
if (CONFIG.modestr != NULL) {
@ -57,7 +57,7 @@ void do_file(char *filepath) {
return;
}
IoctlStat statbuf = {0};
IoctlStat statbuf; ZERO(&statbuf);
ioctl(ioh, IOCTL_STAT, (uint64_t)&statbuf, 0, 0);
if (statbuf.type != IOCTLSTAT_FILE) {
@ -75,13 +75,13 @@ void do_file(char *filepath) {
InterpResult *res;
bool ok = interp_runstring((char *)buf, &res, CONFIG.logcmds, CONFIG.mode == MODE_INTERACTIVE);
if (!ok) {
writefmt("Interpreter error:\n");
writefmt("{s}\n", res->errmsg);
uprintf("Interpreter error:\n");
uprintf("%s\n", res->errmsg);
goto done;
}
done:
/* dlfree(buf); */
ufree(buf);
ioctl(ioh, IOCTL_CLOSEF, 0, 0, 0);
}
@ -90,7 +90,7 @@ void do_mode_interactive(void) {
size_t cursor;
for(;;) {
begin:
writefmt("tb# ");
uprintf("tb# ");
cursor = 0;
string_memset(linebuf, 0, LINEBUF_MAX);
@ -102,12 +102,12 @@ void do_mode_interactive(void) {
switch (b) {
case C('C'):
case 0xE9:
writefmt("\n");
uprintf("\n");
goto begin;
break;
case C('L'):
writefmt(ANSIQ_CUR_SET(0, 0));
writefmt(ANSIQ_SCR_CLR_ALL);
uprintf(ANSIQ_CUR_SET(0, 0));
uprintf(ANSIQ_SCR_CLR_ALL);
goto begin;
break;
}
@ -118,7 +118,7 @@ void do_mode_interactive(void) {
if (string_chr_isascii(b) && b != 0 && cursor < LINEBUF_MAX) {
linebuf[cursor++] = b;
writefmt("{c}", b);
uprintf("%c", b);
}
} else {
schedrelease();
@ -128,7 +128,7 @@ void do_mode_interactive(void) {
if (cursor < LINEBUF_MAX) {
linebuf[cursor] = '\0';
}
writefmt("\n");
uprintf("\n");
InterpResult *res;
if (!interp_runstring(linebuf, &res, CONFIG.logcmds, CONFIG.mode == MODE_INTERACTIVE)) {
LOG(LOG_ERR, "{s}\n", res->errmsg);
@ -150,7 +150,7 @@ void main(void) {
/* do_mode_interactive(); */
} else if (CONFIG.mode == MODE_RUNFILE) {
if (CONFIG.filepath == NULL) {
writefmt("No file provided\n");
uprintf("No file provided\n");
return;
}
do_file(CONFIG.filepath);

View File

@ -18,12 +18,12 @@ RtAlias *RTALIASES = NULL;
bool rt_print(Token *tks) {
Token *tk, *tktmp;
LL_FOREACH_SAFE(tks, tk, tktmp) {
writefmt("{s}", tk->str);
uprintf("%s", tk->str);
if (tk->next != NULL) {
writefmt(" ");
uprintf(" ");
}
}
writefmt("\n");
uprintf("\n");
return true;
}