tb Command aliases, preloading scripts
This commit is contained in:
@ -1,3 +1,3 @@
|
|||||||
@print "this is an init script!"
|
@print "this is an init script!"
|
||||||
base:/bin/pctl ls
|
base:/bin/pctl ls
|
||||||
base:/bin/tb -m interactive
|
base:/bin/tb -m interactive -preload base:/scripts/rc.tb
|
||||||
|
1
base/scripts/rc.tb
Normal file
1
base/scripts/rc.tb
Normal file
@ -0,0 +1 @@
|
|||||||
|
@mkalias pctl base:/bin/pctl
|
@ -13,8 +13,6 @@
|
|||||||
#include "interp.h"
|
#include "interp.h"
|
||||||
#include "runtime.h"
|
#include "runtime.h"
|
||||||
|
|
||||||
/* #define SUBPROC_PIPE_OUT 31 */
|
|
||||||
|
|
||||||
extern uint64_t PID;
|
extern uint64_t PID;
|
||||||
|
|
||||||
static InterpResult RES;
|
static InterpResult RES;
|
||||||
@ -98,6 +96,27 @@ void tz_classify(Tokenizer *tz) {
|
|||||||
dlfree(tmpbuf);
|
dlfree(tmpbuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void tz_expandspecial(Tokenizer *tz) {
|
||||||
|
Token *tk = tz->tokens;
|
||||||
|
while (tk) {
|
||||||
|
if (tk->ptr[0] == '$' && tk->len > 1) {
|
||||||
|
char *aliasbuf = dlmalloc(RTALIAS_NAMEBUF_MAX);
|
||||||
|
usnprintf(aliasbuf, RTALIAS_NAMEBUF_MAX, "%.*s", (int)tk->len-1, &tk->ptr[1]);
|
||||||
|
RtAlias *alias = RTALIASES;
|
||||||
|
while (alias) {
|
||||||
|
if (string_strcmp(alias->namebuf, aliasbuf) == 0) {
|
||||||
|
tk->ptr = alias->valbuf;
|
||||||
|
tk->len = string_len(alias->valbuf);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
alias = alias->next;
|
||||||
|
}
|
||||||
|
dlfree(aliasbuf);
|
||||||
|
}
|
||||||
|
tk = tk->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool interp_readline(char *data, const char **bgptr, const char **endptr) {
|
bool interp_readline(char *data, const char **bgptr, const char **endptr) {
|
||||||
static char *nextstart;
|
static char *nextstart;
|
||||||
if (data) {
|
if (data) {
|
||||||
@ -178,6 +197,7 @@ bool interp_runstring(const char *string, InterpResult **res, bool logcmds) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tz_classify(&tz);
|
tz_classify(&tz);
|
||||||
|
tz_expandspecial(&tz);
|
||||||
|
|
||||||
Token *cmdtk = tz.tokens;
|
Token *cmdtk = tz.tokens;
|
||||||
if (cmdtk->type == TOK_CMD) {
|
if (cmdtk->type == TOK_CMD) {
|
||||||
|
@ -29,15 +29,20 @@ struct {
|
|||||||
char *filepath;
|
char *filepath;
|
||||||
|
|
||||||
bool logcmds;
|
bool logcmds;
|
||||||
|
|
||||||
|
char *preloadpath;
|
||||||
} CONFIG;
|
} CONFIG;
|
||||||
|
|
||||||
static Arg ARGS[] = {
|
static Arg ARGS[] = {
|
||||||
ARG("-m", ARG_STRING, &CONFIG.modestr),
|
ARG("-m", ARG_STRING, &CONFIG.modestr),
|
||||||
ARG("-f", ARG_STRING, &CONFIG.filepath),
|
ARG("-f", ARG_STRING, &CONFIG.filepath),
|
||||||
ARG("-logcmds", ARG_BOOL, &CONFIG.logcmds),
|
ARG("-logcmds", ARG_BOOL, &CONFIG.logcmds),
|
||||||
|
ARG("-preload", ARG_STRING, &CONFIG.preloadpath),
|
||||||
ARG_END(),
|
ARG_END(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void do_file(char *filepath);
|
||||||
|
|
||||||
void set_config(void) {
|
void set_config(void) {
|
||||||
int32_t ret;
|
int32_t ret;
|
||||||
if ((ret = parse_args(args(), argslen(), ARGS)) < 0) {
|
if ((ret = parse_args(args(), argslen(), ARGS)) < 0) {
|
||||||
@ -57,10 +62,6 @@ void set_config(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void process_cmd(char *cmdtext) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void do_file(char *filepath) {
|
void do_file(char *filepath) {
|
||||||
int32_t ret;
|
int32_t ret;
|
||||||
|
|
||||||
@ -149,6 +150,11 @@ void main(void) {
|
|||||||
|
|
||||||
set_config();
|
set_config();
|
||||||
|
|
||||||
|
if (CONFIG.preloadpath != NULL) {
|
||||||
|
LOG(LOG_INF, "Preloading script: %s\n", CONFIG.preloadpath);
|
||||||
|
do_file(CONFIG.preloadpath);
|
||||||
|
}
|
||||||
|
|
||||||
if (CONFIG.mode == MODE_INTERACTIVE) {
|
if (CONFIG.mode == MODE_INTERACTIVE) {
|
||||||
do_mode_interactive();
|
do_mode_interactive();
|
||||||
} else if (CONFIG.mode == MODE_RUNFILE) {
|
} else if (CONFIG.mode == MODE_RUNFILE) {
|
||||||
|
@ -2,11 +2,13 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <linklist.h>
|
#include <linklist.h>
|
||||||
#include <dlmalloc/malloc.h>
|
#include <dlmalloc/malloc.h>
|
||||||
|
#include <string/string.h>
|
||||||
#include <uprintf.h>
|
#include <uprintf.h>
|
||||||
#include "runtime.h"
|
#include "runtime.h"
|
||||||
#include "interp.h"
|
#include "interp.h"
|
||||||
|
|
||||||
RtCmd *RTCMDS = NULL;
|
RtCmd *RTCMDS = NULL;
|
||||||
|
RtAlias *RTALIASES = NULL;
|
||||||
|
|
||||||
#define RTCMD(name, _fn) \
|
#define RTCMD(name, _fn) \
|
||||||
do { \
|
do { \
|
||||||
@ -29,6 +31,26 @@ bool rt_print(Token *tks) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool rt_mkalias(Token *tks) {
|
||||||
|
RtAlias *alias = dlmalloc(sizeof(*alias));
|
||||||
|
string_memset(alias, 0, sizeof(*alias));
|
||||||
|
Token *tk = tks;
|
||||||
|
size_t i = 0;
|
||||||
|
while (tk) {
|
||||||
|
if (i == 0) {
|
||||||
|
usprintf(alias->namebuf, "%.*s", (int)tk->len, tk->ptr);
|
||||||
|
} else {
|
||||||
|
size_t off = string_len(alias->valbuf);
|
||||||
|
usprintf(alias->valbuf + off, "%.*s", (int)tk->len, tk->ptr);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
tk = tk->next;
|
||||||
|
}
|
||||||
|
LL_APPEND(RTALIASES, alias);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void rt_init(void) {
|
void rt_init(void) {
|
||||||
RTCMD("@print", &rt_print);
|
RTCMD("@print", &rt_print);
|
||||||
|
RTCMD("@mkalias", &rt_mkalias);
|
||||||
}
|
}
|
||||||
|
@ -11,8 +11,18 @@ typedef struct RtCmd {
|
|||||||
bool (*fn)(struct Token *tks);
|
bool (*fn)(struct Token *tks);
|
||||||
} RtCmd;
|
} RtCmd;
|
||||||
|
|
||||||
|
#define RTALIAS_NAMEBUF_MAX 0x100
|
||||||
|
#define RTALIAS_VALBUF_MAX 0x1000
|
||||||
|
|
||||||
|
typedef struct RtAlias {
|
||||||
|
struct RtAlias *next;
|
||||||
|
char namebuf[RTALIAS_NAMEBUF_MAX];
|
||||||
|
char valbuf[RTALIAS_VALBUF_MAX];
|
||||||
|
} RtAlias;
|
||||||
|
|
||||||
void rt_init(void);
|
void rt_init(void);
|
||||||
|
|
||||||
extern RtCmd *RTCMDS;
|
extern RtCmd *RTCMDS;
|
||||||
|
extern RtAlias *RTALIASES;
|
||||||
|
|
||||||
#endif // TB_RUNTIME_H_
|
#endif // TB_RUNTIME_H_
|
||||||
|
Reference in New Issue
Block a user