tb Implement a string stack
This commit is contained in:
@ -4,6 +4,38 @@
|
||||
#include "runtime.h"
|
||||
#include "interp.h"
|
||||
|
||||
RtStringV *RTSTRINGV_STACK = NULL;
|
||||
|
||||
void rtstringv_stackpushcopy(char *s, size_t len) {
|
||||
char *c = umalloc(len+1);
|
||||
string_memcpy(c, s, len);
|
||||
c[len] = '\0';
|
||||
RtStringV *v = umalloc(sizeof(*v));
|
||||
v->data = c;
|
||||
LL_APPEND(RTSTRINGV_STACK, v);
|
||||
}
|
||||
|
||||
char *rtstringv_stackpop(void) {
|
||||
RtStringV *v;
|
||||
LL_BACK(RTSTRINGV_STACK, v);
|
||||
if (v == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
LL_REMOVE(RTSTRINGV_STACK, v);
|
||||
char *d = v->data;
|
||||
ufree(v);
|
||||
return d;
|
||||
}
|
||||
|
||||
char *rtstringv_stackpeek(void) {
|
||||
RtStringV *v;
|
||||
LL_BACK(RTSTRINGV_STACK, v);
|
||||
if (v == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
return v->data;
|
||||
}
|
||||
|
||||
extern PID_t PID;
|
||||
|
||||
RtCmd *RTCMDS = NULL;
|
||||
@ -25,7 +57,6 @@ bool rt_print(Token *tks) {
|
||||
uprintf(" ");
|
||||
}
|
||||
}
|
||||
uprintf("\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -52,7 +83,7 @@ bool rt_PID(Token *tks) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool rt_subsh(Token *tks) {
|
||||
bool rt_do(Token *tks) {
|
||||
bool ok = true;
|
||||
|
||||
char *prepended_args[] = { "-m", "runstring", "-preload", "base:/scripts/rc.tb", "-rs" };
|
||||
@ -100,11 +131,15 @@ bool rt_subsh(Token *tks) {
|
||||
string_memset(buf, 0, sizeof(buf));
|
||||
|
||||
r = ipcpipe(app, IPCPIPE_OUT, IPCPIPE_READ, (uint8_t *)buf, sizeof(buf)-1);
|
||||
stringbuffer_appendcstr(&outbuf, buf);
|
||||
if (r > 0) {
|
||||
stringbuffer_appendcstr(&outbuf, buf);
|
||||
}
|
||||
}
|
||||
|
||||
ipcpipe(PID, 10, IPCPIPE_DELETE, NULL, 0);
|
||||
|
||||
rtstringv_stackpushcopy(outbuf.data, outbuf.count);
|
||||
|
||||
stringbuffer_free(&outbuf);
|
||||
|
||||
done:
|
||||
@ -116,9 +151,26 @@ done:
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool rt_stackpush(Token *tks) {
|
||||
Token *tk, *tktmp;
|
||||
LL_FOREACH_SAFE(tks, tk, tktmp) {
|
||||
rtstringv_stackpushcopy(tk->str, string_len(tk->str));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool rt_stackpop(Token *tks) {
|
||||
(void)tks;
|
||||
char *s = rtstringv_stackpop();
|
||||
if (s) ufree(s);
|
||||
return true;
|
||||
}
|
||||
|
||||
void rt_init(void) {
|
||||
RTCMD("%print", &rt_print);
|
||||
RTCMD("%mkalias", &rt_mkalias);
|
||||
RTCMD("%PID", &rt_PID);
|
||||
RTCMD("%subsh", &rt_subsh);
|
||||
RTCMD("%do", &rt_do);
|
||||
RTCMD("%stackpush", &rt_stackpush);
|
||||
RTCMD("%stackpop", &rt_stackpop);
|
||||
}
|
||||
|
Reference in New Issue
Block a user