Get rid of writefmt functions
This commit is contained in:
@ -11,6 +11,6 @@ CFLAGS += -m64 \
|
||||
-fno-builtin \
|
||||
-fno-omit-frame-pointer \
|
||||
-fno-strict-aliasing \
|
||||
-O1 \
|
||||
-O0 \
|
||||
-mno-tls-direct-seg-refs \
|
||||
#-fsanitize=undefined
|
||||
# -fsanitize=undefined
|
||||
|
@ -14,7 +14,6 @@ SRCFILES := $(call GRABSRC, \
|
||||
args \
|
||||
util \
|
||||
ubsan \
|
||||
write \
|
||||
umalloc \
|
||||
)
|
||||
|
||||
|
@ -1,13 +1,13 @@
|
||||
#ifndef ULIB_ASSERT_H_
|
||||
#define ULIB_ASSERT_H_
|
||||
|
||||
#include <write/write.h>
|
||||
#include <util/util.h>
|
||||
#include <uprintf.h>
|
||||
|
||||
#define ASSERT(X, fmt, ...) \
|
||||
do { \
|
||||
if (!(X)) { \
|
||||
writefmt("ASSERT {s}:{d} in {s} "fmt, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \
|
||||
uprintf("ASSERT %s:%d in %s "fmt, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \
|
||||
quit(); \
|
||||
} \
|
||||
} while(0)
|
||||
|
@ -2,7 +2,6 @@
|
||||
#define ULIB_LOG_H_
|
||||
|
||||
#include <uprintf.h>
|
||||
#include <write/write.h>
|
||||
|
||||
enum {
|
||||
LOG_ERR,
|
||||
@ -13,6 +12,6 @@ enum {
|
||||
|
||||
static const char *_LOG_STR[] = { "ERROR", "DEBUG", "INFO", "WARNING" };
|
||||
|
||||
#define LOG(mode, fmt, ...) writefmt("{s}: "fmt, _LOG_STR[(mode)], ##__VA_ARGS__);
|
||||
#define LOG(mode, fmt, ...) uprintf("%s: "fmt, _LOG_STR[(mode)], ##__VA_ARGS__);
|
||||
|
||||
#endif // ULIB_LOG_H_
|
||||
|
@ -1,7 +1,6 @@
|
||||
#include <stddef.h>
|
||||
#include <string/string.h>
|
||||
#include <umalloc/umalloc.h>
|
||||
#include <write/write.h>
|
||||
|
||||
size_t string_len(const char *s) {
|
||||
size_t l = 0;
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <write/write.h>
|
||||
#include <uprintf.h>
|
||||
|
||||
struct source_location {
|
||||
const char *file;
|
||||
@ -96,7 +96,7 @@ struct pointer_overflow_data {
|
||||
|
||||
static void ubsan_print(struct source_location *loc, const char *message)
|
||||
{
|
||||
writefmt("UBSAN error {s}:{d}:{d} {s}\n", loc->file, loc->line, loc->column, message);
|
||||
uprintf("UBSAN error %s:%d:%d %s\n", loc->file, loc->line, loc->column, message);
|
||||
}
|
||||
|
||||
void __ubsan_handle_add_overflow(struct overflow_data *data)
|
||||
|
@ -13,7 +13,6 @@
|
||||
#include <linklist.h>
|
||||
#include <log.h>
|
||||
#include <assert.h>
|
||||
#include <write/write.h>
|
||||
#include <umalloc/umalloc.h>
|
||||
|
||||
#include <errors.h>
|
||||
|
@ -6,7 +6,6 @@
|
||||
#include <errors.h>
|
||||
#include <linklist.h>
|
||||
#include <string/string.h>
|
||||
#include <write/write.h>
|
||||
#include <util/util.h>
|
||||
#include <assert.h>
|
||||
|
||||
|
@ -1,167 +0,0 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <limits.h>
|
||||
#include <system/system.h>
|
||||
#include <sysdefs/devctl.h>
|
||||
#include <write/write.h>
|
||||
#include <umalloc/umalloc.h>
|
||||
#include <string/string.h>
|
||||
#include <util/util.h>
|
||||
|
||||
char *utoa(uintmax_t v, int base, bool uppercase) {
|
||||
if (base < 2 || base > 36) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static char buf[sizeof(uintmax_t) * CHAR_BIT + 3];
|
||||
const char *digits = uppercase ? "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" : "0123456789abcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
size_t i = sizeof(buf) - 1;
|
||||
buf[i] = '\0';
|
||||
|
||||
if (v == 0) {
|
||||
buf[--i] = '0';
|
||||
return &buf[i];
|
||||
}
|
||||
|
||||
while (v != 0) {
|
||||
if (i == 0) {
|
||||
return NULL;
|
||||
}
|
||||
buf[--i] = digits[v % base];
|
||||
v /= base;
|
||||
}
|
||||
|
||||
return &buf[i];
|
||||
}
|
||||
|
||||
char *itoa(intmax_t v, int base, bool uppercase) {
|
||||
if (base < 2 || base > 36) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static char buf[sizeof(uintmax_t) * CHAR_BIT + 3];
|
||||
const char *digits = uppercase ? "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" : "0123456789abcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
size_t i = sizeof(buf) - 1;
|
||||
buf[i] = '\0';
|
||||
|
||||
if (v == 0) {
|
||||
buf[--i] = '0';
|
||||
return &buf[i];
|
||||
}
|
||||
|
||||
bool neg = (v < 0);
|
||||
uintmax_t u;
|
||||
if (neg) {
|
||||
u = (uintmax_t)(-(v + 1)) + 1;
|
||||
} else {
|
||||
u = (uintmax_t)v;
|
||||
}
|
||||
|
||||
while (u != 0) {
|
||||
if (i == 0) {
|
||||
return NULL;
|
||||
}
|
||||
buf[--i] = digits[u % base];
|
||||
u /= base;
|
||||
}
|
||||
|
||||
if (neg) {
|
||||
if (i == 0) {
|
||||
return NULL;
|
||||
}
|
||||
buf[--i] = '-';
|
||||
}
|
||||
|
||||
return &buf[i];
|
||||
}
|
||||
|
||||
#define FMTBUF_MAX (1024 * 4)
|
||||
|
||||
extern Dev_t termdev;
|
||||
|
||||
enum {
|
||||
WRITE_FORMATMODE,
|
||||
WRITE_NORMALMODE,
|
||||
};
|
||||
|
||||
const char *convstr1 = "0123456789abcdef";
|
||||
const char *convstr2 = "0123456789ABCDEF";
|
||||
|
||||
size_t writefmt(char *fmt, ...) {
|
||||
va_list list;
|
||||
va_start(list, fmt);
|
||||
size_t count = writevfmt(fmt, list);
|
||||
va_end(list);
|
||||
return count;
|
||||
}
|
||||
|
||||
size_t writevsfmt(char *buf, char *fmt, va_list list) {
|
||||
size_t c = 0;
|
||||
int WRITE_STATE = WRITE_NORMALMODE;
|
||||
|
||||
for (size_t i = 0; fmt[i] != '\0'; i++) {
|
||||
if (WRITE_STATE == WRITE_NORMALMODE) {
|
||||
switch (fmt[i]) {
|
||||
case '{':
|
||||
WRITE_STATE = WRITE_FORMATMODE;
|
||||
break;
|
||||
default:
|
||||
buf[c++] = fmt[i];
|
||||
break;
|
||||
}
|
||||
} else if (WRITE_STATE == WRITE_FORMATMODE) {
|
||||
switch (fmt[i]) {
|
||||
case '}':
|
||||
WRITE_STATE = WRITE_NORMALMODE;
|
||||
break;
|
||||
case 's': {
|
||||
char *string = va_arg(list, char *);
|
||||
while (string && *string) { buf[c++] = *string; string++; }
|
||||
} break;
|
||||
case 'd': {
|
||||
int int1 = va_arg(list, int);
|
||||
char *string = itoa(int1, 10, false);
|
||||
while (string && *string) { buf[c++] = *string; string++; }
|
||||
} break;
|
||||
case 'x': {
|
||||
unsigned int int1 = va_arg(list, unsigned int);
|
||||
char *string = utoa(int1, 16, false);
|
||||
while (string && *string) { buf[c++] = *string; string++; }
|
||||
} break;
|
||||
case 'X': {
|
||||
unsigned int int1 = va_arg(list, unsigned int);
|
||||
char *string = utoa(int1, 16, true);
|
||||
while (string && *string) { buf[c++] = *string; string++; }
|
||||
} break;
|
||||
case 'p': {
|
||||
uintptr_t ptr = (uintptr_t)va_arg(list, void *);
|
||||
char *string = utoa(ptr, 16, false);
|
||||
while (string && *string) { buf[c++] = *string; string++; }
|
||||
} break;
|
||||
case 'P': {
|
||||
uintptr_t ptr = (uintptr_t)va_arg(list, void *);
|
||||
char *string = utoa(ptr, 16, true);
|
||||
while (string && *string) { buf[c++] = *string; string++; }
|
||||
} break;
|
||||
case 'c': {
|
||||
int c1 = va_arg(list, int);
|
||||
buf[c++] = (char)c1;
|
||||
} break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
size_t writevfmt(char *fmt, va_list list) {
|
||||
char buf[FMTBUF_MAX];
|
||||
string_memset(buf, 0, FMTBUF_MAX);
|
||||
size_t count = writevsfmt(buf, fmt, list);
|
||||
devctl(&termdev, 0x00, buf, count, 0);
|
||||
return count;
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
#ifndef ULIB_WRITE_WRITE_H_
|
||||
#define ULIB_WRITE_WRITE_H_
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
size_t writefmt(char *fmt, ...);
|
||||
size_t writevfmt(char *fmt, va_list list);
|
||||
size_t writevsfmt(char *buf, char *fmt, va_list list);
|
||||
|
||||
#endif // ULIB_WRITE_WRITE_H_
|
@ -1,7 +1,7 @@
|
||||
ENTRY(_start)
|
||||
|
||||
SECTIONS {
|
||||
. = 0x1000;
|
||||
. = 0x400000;
|
||||
|
||||
.text ALIGN(4K):
|
||||
{
|
||||
|
@ -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(;;);
|
||||
}
|
||||
|
112
user/tb/interp.c
112
user/tb/interp.c
@ -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");
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user