WIP
This commit is contained in:
@ -5,10 +5,17 @@
|
||||
#include <string/char.h>
|
||||
#include <linklist.h>
|
||||
#include <dlmalloc/malloc.h>
|
||||
#include <system/system.h>
|
||||
#include <sysdefs/processctl.h>
|
||||
#include <sysdefs/ipcpipe.h>
|
||||
#include <uprintf.h>
|
||||
#include "interp.h"
|
||||
#include "runtime.h"
|
||||
|
||||
#define SUBPROC_PIPE_OUT 0x1f
|
||||
|
||||
extern uint64_t PID;
|
||||
|
||||
static InterpResult RES;
|
||||
|
||||
void tz_init(Tokenizer *tz, const char *str, size_t len) {
|
||||
@ -62,7 +69,7 @@ void tz_classify(Tokenizer *tz) {
|
||||
while (tk) {
|
||||
if (tk->ptr[0] == '"' && tk->ptr[tk->len - 1] == '"') {
|
||||
tk->type = TOK_STRING;
|
||||
} else {
|
||||
} else if (tk->ptr[0] == '@') {
|
||||
RtCmd *cmd = RTCMDS;
|
||||
while (cmd) {
|
||||
char tmpbuf[0xff] = {0};
|
||||
@ -74,6 +81,8 @@ void tz_classify(Tokenizer *tz) {
|
||||
}
|
||||
cmd = cmd->next;
|
||||
}
|
||||
} else {
|
||||
tk->type = TOK_MISC;
|
||||
}
|
||||
tk = tk->next;
|
||||
}
|
||||
@ -132,8 +141,9 @@ bool interp_readline(char *data, const char **bgptr, const char **endptr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool interp_runstring(const char *string, size_t len, InterpResult **res) {
|
||||
bool interp_runstring(const char *string, InterpResult **res) {
|
||||
*res = &RES;
|
||||
string_memset(RES.errmsg, 0, sizeof(RES.errmsg));
|
||||
|
||||
rt_init();
|
||||
bool ok = true;
|
||||
@ -156,19 +166,62 @@ bool interp_runstring(const char *string, size_t len, InterpResult **res) {
|
||||
tz_classify(&tz);
|
||||
|
||||
Token *cmdtk = tz.tokens;
|
||||
if (cmdtk->type != TOK_CMD) {
|
||||
ok = false;
|
||||
usprintf(RES.errmsg, "Expected cmd name, but got %.*s", (int)cmdtk->len, cmdtk->ptr);
|
||||
tz_free(&tz);
|
||||
goto done;
|
||||
}
|
||||
if (cmdtk->type == TOK_CMD) {
|
||||
ok = cmdtk->cmd->fn(cmdtk->next);
|
||||
if (!ok) {
|
||||
ok = false;
|
||||
usprintf(RES.errmsg, "cmd %.*s failed", (int)cmdtk->len, cmdtk->ptr);
|
||||
tz_free(&tz);
|
||||
goto done;
|
||||
}
|
||||
} else if (cmdtk->type == TOK_MISC) {
|
||||
char *appname = dlmalloc(1024);
|
||||
usprintf(appname, "%.*s", (int)cmdtk->len, cmdtk->ptr);
|
||||
|
||||
ok = cmdtk->cmd->fn(cmdtk->next);
|
||||
if (!ok) {
|
||||
ok = false;
|
||||
usprintf(RES.errmsg, "cmd %.*s failed", (int)cmdtk->len, cmdtk->ptr);
|
||||
tz_free(&tz);
|
||||
goto done;
|
||||
size_t argslen1 = 0;
|
||||
Token *argtk = cmdtk->next;
|
||||
while (argtk) {
|
||||
argslen1++;
|
||||
argtk = argtk->next;
|
||||
}
|
||||
|
||||
char **args1 = (char **)dlmalloc(sizeof(char *) * argslen1);
|
||||
argtk = cmdtk->next;
|
||||
size_t i = 0;
|
||||
while (argtk) {
|
||||
args1[i] = (char *)dlmalloc(PROC_ARG_MAX);
|
||||
string_memset(args1[i], 0, PROC_ARG_MAX);
|
||||
string_memcpy(args1[i], argtk->ptr, argtk->len);
|
||||
i++;
|
||||
argtk = argtk->next;
|
||||
}
|
||||
|
||||
#define OUTBUF_MAX 1024
|
||||
char *outbuf = dlmalloc(OUTBUF_MAX);
|
||||
|
||||
ipcpipe(PID, SUBPROC_PIPE_OUT, IPCPIPE_MAKE, NULL, 0);
|
||||
|
||||
int32_t app = processctl(-1, PCTL_SPAWN, (uint64_t)(char *)appname, (uint64_t)args1, argslen1);
|
||||
ipcpipe(app, IPCPIPE_OUT, IPCPIPE_REPLACE, (uint8_t *)PID, SUBPROC_PIPE_OUT);
|
||||
ipcpipe(app, IPCPIPE_IN, IPCPIPE_READ, (uint8_t *)PID, IPCPIPE_IN);
|
||||
processctl(app, PCTL_RUN, 0, 0, 0);
|
||||
|
||||
while (processctl(app, PCTL_POLLSTATE, 0, 0, 0) != 4) {
|
||||
string_memset(outbuf, 0, OUTBUF_MAX);
|
||||
int32_t nrd = ipcpipe(PID, SUBPROC_PIPE_OUT, IPCPIPE_READ, (uint8_t *)outbuf, sizeof(outbuf));
|
||||
if (nrd > 0) {
|
||||
uprintf("%s", outbuf);
|
||||
}
|
||||
}
|
||||
|
||||
dlfree(outbuf);
|
||||
|
||||
for (size_t j = 0; j < argslen1; j++) {
|
||||
dlfree(args1[j]);
|
||||
}
|
||||
dlfree(args1);
|
||||
|
||||
dlfree(appname);
|
||||
}
|
||||
|
||||
tz_free(&tz);
|
||||
|
Reference in New Issue
Block a user