137 lines
3.1 KiB
C
137 lines
3.1 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>
|
|
#include <sysdefs/ioctl.h>
|
|
#include <sysdefs/processctl.h>
|
|
#include <dlmalloc/malloc.h>
|
|
#include <errors.h>
|
|
#include <util/util.h>
|
|
#include "interp.h"
|
|
|
|
uint64_t PID;
|
|
|
|
struct {
|
|
char *modestr;
|
|
enum { MODE_INTERACTIVE = 1, MODE_RUNFILE = 2 } mode;
|
|
|
|
char *filepath;
|
|
|
|
bool logcmds;
|
|
} CONFIG;
|
|
|
|
#define LINEBUF_MAX 1024
|
|
|
|
static Arg ARGS[] = {
|
|
ARG("-m", ARG_STRING, &CONFIG.modestr),
|
|
ARG("-f", ARG_STRING, &CONFIG.filepath),
|
|
ARG("-logcmds", ARG_BOOL, &CONFIG.logcmds),
|
|
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;
|
|
}
|
|
}
|
|
|
|
void process_cmd(char *cmdtext) {
|
|
|
|
}
|
|
|
|
void do_file(char *filepath) {
|
|
int32_t ret;
|
|
|
|
int32_t ioh = ioctl(IOCTL_NOHANDLE, IOCTL_OPENF, (uint64_t)filepath, IOCTL_F_READ, 0);
|
|
if (ioh < 0) {
|
|
LOG(LOG_ERR, "Could not open %s: %s\n", filepath, ERRSTRING(ioh));
|
|
return;
|
|
}
|
|
|
|
IoctlStat statbuf = {0};
|
|
|
|
ioctl(ioh, IOCTL_STAT, (uint64_t)&statbuf, 0, 0);
|
|
if (statbuf.type != IOCTLSTAT_FILE) {
|
|
LOG(LOG_ERR, "%s is not a file (%d)\n", filepath, statbuf.type);
|
|
return;
|
|
}
|
|
uint8_t *buf = dlmalloc(statbuf.size+1);
|
|
string_memset(buf, 0, statbuf.size+1);
|
|
|
|
if ((ret = ioctl(ioh, IOCTL_READ, (uint64_t)buf, statbuf.size, 0)) < 0) {
|
|
LOG(LOG_ERR, "Could not read %s (%d): %s\n", filepath, ioh, ERRSTRING(ioh));
|
|
goto done;
|
|
}
|
|
|
|
InterpResult *res;
|
|
bool ok = interp_runstring((const char *)buf, &res, CONFIG.logcmds);
|
|
if (!ok) {
|
|
uprintf("Interpreter error:\n");
|
|
uprintf("%s\n", res->errmsg);
|
|
goto done;
|
|
}
|
|
|
|
done:
|
|
dlfree(buf);
|
|
ioctl(ioh, IOCTL_CLOSEF, 0, 0, 0);
|
|
}
|
|
|
|
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");
|
|
InterpResult *res;
|
|
if (!interp_runstring(linebuf, &res, CONFIG.logcmds)) {
|
|
LOG(LOG_ERR, "%s\n", res->errmsg);
|
|
}
|
|
}
|
|
}
|
|
|
|
void main(void) {
|
|
PID = processctl(-1, PCTL_GETPID, 0, 0, 0);
|
|
|
|
set_config();
|
|
|
|
if (CONFIG.mode == MODE_INTERACTIVE) {
|
|
do_mode_interactive();
|
|
} else if (CONFIG.mode == MODE_RUNFILE) {
|
|
if (CONFIG.filepath == NULL) {
|
|
uprintf("No file provided\n");
|
|
return;
|
|
}
|
|
do_file(CONFIG.filepath);
|
|
}
|
|
}
|