Files
my-os-project2/user/tb/main.c
2025-09-13 15:43:31 +02:00

77 lines
1.6 KiB
C

#include <stdint.h>
#include <stddef.h>
#include <uprintf.h>
#include <args/args.h>
#include <string/string.h>
#include <string/char.h>
#include <log.h>
#include <ansiq/all.h>
#include <system/system.h>
#include <sysdefs/ipcpipe.h>
static struct {
char *modestr;
enum { MODE_INTERACTIVE = 1, MODE_RUNFILE = 2 } mode;
} CONFIG;
#define LINEBUF_MAX 1024
static Arg ARGS[] = {
ARG("-m", ARG_STRING, &CONFIG.modestr),
ARG_END(),
};
void set_config(void) {
int32_t ret;
if ((ret = parse_args(args(), argslen(), ARGS)) < 0) {
uprintf("Could not parse args: %d\n", ret);
}
if (CONFIG.modestr != NULL) {
if (string_strcmp(CONFIG.modestr, "interactive") == 0) {
CONFIG.mode = MODE_INTERACTIVE;
} else if (string_strcmp(CONFIG.modestr, "runfile") == 0) {
CONFIG.mode = MODE_RUNFILE;
} else {
LOG(LOG_ERR, "Unknown mode %s\n", CONFIG.modestr);
}
} else {
CONFIG.mode = MODE_RUNFILE;
}
uprintf("CONFIG.mode = %d\n", CONFIG.mode);
}
void process_cmd(char *cmdtext) {
}
void do_mode_interactive(void) {
char linebuf[LINEBUF_MAX];
size_t cursor;
for(;;) {
uprintf("tb# ");
cursor = 0;
string_memset(linebuf, 0, LINEBUF_MAX);
char c = 0;
while (c != '\n') {
int32_t rd = ipcpipe(IPCPIPE_SELFPID, IPCPIPE_IN, IPCPIPE_READ, (uint8_t *)&c, 1);
if (rd > 0 && cursor < LINEBUF_MAX) {
linebuf[cursor++] = c;
}
}
linebuf[cursor - 1] = '\0';
uprintf("\n");
process_cmd(linebuf);
}
}
void main(void) {
set_config();
if (CONFIG.mode == MODE_INTERACTIVE) {
do_mode_interactive();
}
}