Compare commits
4 Commits
76faf0581d
...
d1d777ec75
| Author | SHA1 | Date | |
|---|---|---|---|
| d1d777ec75 | |||
| a46da0a7ae | |||
| d136c001df | |||
| 9212ff0e97 |
@ -1,4 +1,5 @@
|
||||
print 'this is an init script!\n'
|
||||
setlogcmds yes
|
||||
$tb -m runfile -f base:/scripts/mount.tb
|
||||
$pctl ls
|
||||
$tb -m interactive
|
||||
|
||||
@ -1 +1,2 @@
|
||||
eachfile !.gitkeep base:/bin mkaliasbn &EF-ELEM
|
||||
eachfile !.gitkeep base:/bin \
|
||||
mkaliasbn &EF-ELEM
|
||||
|
||||
@ -173,6 +173,76 @@ char *string_tokenizealloc(char *s, char *delim) {
|
||||
return w;
|
||||
}
|
||||
|
||||
char *string_tokenizealloc_linecontinue(char *s, char *delim) {
|
||||
static char *saved = NULL;
|
||||
|
||||
if (s) {
|
||||
saved = s;
|
||||
}
|
||||
|
||||
if (!delim || !saved) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (*saved && string_strchr(delim, *saved)) {
|
||||
saved++;
|
||||
}
|
||||
|
||||
if (!*saved) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *w = (char *)umalloc(sizeof(char) * STRING_TOKENIZEALLOC_TOK_SIZE);
|
||||
string_memset(w, 0, sizeof(char) * STRING_TOKENIZEALLOC_TOK_SIZE);
|
||||
|
||||
int k = 0;
|
||||
|
||||
while (*saved) {
|
||||
if (*saved == '\\') {
|
||||
char *p = saved;
|
||||
int bs_count = 0;
|
||||
|
||||
while (*p == '\\') {
|
||||
bs_count++;
|
||||
p++;
|
||||
}
|
||||
|
||||
if ((*p == '\n' || (*p == '\r' && (*p + 1) == '\n')) && (bs_count % 2 == 1)) {
|
||||
int tocopy = bs_count / 2;
|
||||
while (tocopy-- > 0 && k < STRING_TOKENIZEALLOC_TOK_SIZE - 1) {
|
||||
w[k++] = '\\';
|
||||
}
|
||||
|
||||
if (*p == '\r' & *(p + 1) == '\n') {
|
||||
saved = p + 2;
|
||||
} else {
|
||||
saved = p + 1;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (string_strchr(delim, *saved)) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (k >= STRING_TOKENIZEALLOC_TOK_SIZE - 1) {
|
||||
break;
|
||||
}
|
||||
|
||||
w[k++] = *saved++;
|
||||
}
|
||||
|
||||
w[k] = '\0';
|
||||
|
||||
if (*saved) {
|
||||
saved++;
|
||||
}
|
||||
|
||||
return w;
|
||||
}
|
||||
|
||||
// https://stackoverflow.com/questions/2488563/strcat-implementation
|
||||
char *string_combine(char *dest, const char *src) {
|
||||
size_t i, j;
|
||||
|
||||
@ -17,6 +17,7 @@ char *string_strcpy(char *dest, const char *src);
|
||||
char *string_strchr(const char *s, int c);
|
||||
int string_strncmp(const char * s1, const char * s2, size_t n);
|
||||
char *string_tokenizealloc(char *s, char *delim);
|
||||
char *string_tokenizealloc_linecontinue(char *s, char *delim);
|
||||
char *string_combine(char *dest, const char *src);
|
||||
void * string_memmove(void* dest, const void* src, unsigned int n);
|
||||
|
||||
|
||||
20
user/tb/config.h
Normal file
20
user/tb/config.h
Normal file
@ -0,0 +1,20 @@
|
||||
#ifndef TB_CONFIG_H_
|
||||
#define TB_CONFIG_H_
|
||||
typedef struct {
|
||||
char *modestr;
|
||||
enum {
|
||||
MODE_INTERACTIVE = 1,
|
||||
MODE_RUNFILE = 2,
|
||||
MODE_RUNSTRING = 3,
|
||||
} mode;
|
||||
|
||||
char *filepath;
|
||||
|
||||
bool logcmds;
|
||||
|
||||
char *runstring;
|
||||
} Config;
|
||||
|
||||
extern Config CONFIG;
|
||||
|
||||
#endif // TB_CONFIG_H_
|
||||
@ -4,6 +4,7 @@
|
||||
#include "interp.h"
|
||||
#include "runtime.h"
|
||||
#include "macros.h"
|
||||
#include "config.h"
|
||||
|
||||
extern PID_t PID;
|
||||
extern Dev_t ps2kbdev;
|
||||
@ -28,7 +29,13 @@ void tz_free(Tokenizer *tz) {
|
||||
|
||||
void tz_tokenize(Tokenizer *tz) {
|
||||
size_t len = string_len(tz->str);
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
size_t i = 0;
|
||||
|
||||
while (i < len && string_chr_isspace(tz->str[i])) {
|
||||
i++;
|
||||
}
|
||||
|
||||
for (; i < len; i++) {
|
||||
if (tz->str[i] == '\'') {
|
||||
char *str = umalloc(TZ_MAX_TK);
|
||||
string_memset(str, 0, TZ_MAX_TK);
|
||||
@ -118,6 +125,7 @@ void tz_classify(Tokenizer *tz) {
|
||||
else IF_RTCMD(stackpop)
|
||||
else IF_RTCMD(eachfile)
|
||||
else IF_RTCMD(mkaliasbn)
|
||||
else IF_RTCMD(setlogcmds)
|
||||
else {
|
||||
tk->type = TOK_MISC;
|
||||
}
|
||||
@ -160,23 +168,24 @@ void tz_expandspecial(Tokenizer *tz) {
|
||||
|
||||
#define LINE_MAX 1024
|
||||
|
||||
bool interp_runstring(char *string, InterpResult **res, bool logcmds, bool interactive) {
|
||||
bool interp_runstring(char *string, InterpResult **res, bool interactive) {
|
||||
*res = &RES;
|
||||
string_memset(RES.errmsg, 0, sizeof(RES.errmsg));
|
||||
|
||||
rt_init();
|
||||
bool ok = true;
|
||||
|
||||
char *line = string_tokenizealloc(string, "\n");
|
||||
char *line = string_tokenizealloc_linecontinue(string, "\n");
|
||||
while (line != NULL) {
|
||||
if (logcmds) {
|
||||
if (CONFIG.logcmds) {
|
||||
uprintf("+%s\n", line);
|
||||
}
|
||||
|
||||
bool skip;
|
||||
STRING_CHECK_ALL(line, string_chr_isspace, skip);
|
||||
if (skip) {
|
||||
line = string_tokenizealloc(NULL, "\n");
|
||||
ufree(line);
|
||||
line = string_tokenizealloc_linecontinue(NULL, "\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -250,7 +259,7 @@ bool interp_runstring(char *string, InterpResult **res, bool logcmds, bool inter
|
||||
next:
|
||||
tz_free(&tz);
|
||||
ufree(line);
|
||||
line = string_tokenizealloc(NULL, "\n");
|
||||
line = string_tokenizealloc_linecontinue(NULL, "\n");
|
||||
}
|
||||
|
||||
return ok;
|
||||
|
||||
@ -31,6 +31,6 @@ typedef struct {
|
||||
Token *tokens;
|
||||
} Tokenizer;
|
||||
|
||||
bool interp_runstring(char *string, InterpResult **res, bool logcmds, bool interactive);
|
||||
bool interp_runstring(char *string, InterpResult **res, bool interactive);
|
||||
|
||||
#endif // TB_INTERP_H_
|
||||
|
||||
@ -3,26 +3,14 @@
|
||||
#include <ulib.h>
|
||||
#include "interp.h"
|
||||
#include "macros.h"
|
||||
#include "config.h"
|
||||
|
||||
#define LINEBUF_MAX 1024
|
||||
|
||||
PID_t PID;
|
||||
Dev_t ps2kbdev;
|
||||
|
||||
struct {
|
||||
char *modestr;
|
||||
enum {
|
||||
MODE_INTERACTIVE = 1,
|
||||
MODE_RUNFILE = 2,
|
||||
MODE_RUNSTRING = 3,
|
||||
} mode;
|
||||
|
||||
char *filepath;
|
||||
|
||||
bool logcmds;
|
||||
|
||||
char *runstring;
|
||||
} CONFIG;
|
||||
Config CONFIG;
|
||||
|
||||
static Arg ARGS[] = {
|
||||
ARG("-m", ARG_STRING, &CONFIG.modestr),
|
||||
@ -78,7 +66,7 @@ void do_file(char *filepath) {
|
||||
}
|
||||
|
||||
InterpResult *res;
|
||||
bool ok = interp_runstring((char *)buf, &res, CONFIG.logcmds, CONFIG.mode == MODE_INTERACTIVE);
|
||||
bool ok = interp_runstring((char *)buf, &res, CONFIG.mode == MODE_INTERACTIVE);
|
||||
if (!ok) {
|
||||
uprintf("Interpreter error:\n");
|
||||
uprintf("%s\n", res->errmsg);
|
||||
@ -137,7 +125,7 @@ void do_mode_interactive(void) {
|
||||
uprintf(ANSIQ_GR_RESET);
|
||||
uprintf("\n");
|
||||
InterpResult *res;
|
||||
if (!interp_runstring(linebuf, &res, CONFIG.logcmds, CONFIG.mode == MODE_INTERACTIVE)) {
|
||||
if (!interp_runstring(linebuf, &res, CONFIG.mode == MODE_INTERACTIVE)) {
|
||||
LOG(LOG_ERR, "%s\n", res->errmsg);
|
||||
}
|
||||
}
|
||||
@ -173,7 +161,7 @@ void main(void) {
|
||||
if (CONFIG.runstring[len] == '\'') {
|
||||
CONFIG.runstring[len] = ' ';
|
||||
}
|
||||
if (!interp_runstring(CONFIG.runstring, &res, CONFIG.logcmds, false)) {
|
||||
if (!interp_runstring(CONFIG.runstring, &res, false)) {
|
||||
LOG(LOG_ERR, "%s\n", res->errmsg);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
#include <ulib.h>
|
||||
#include "runtime.h"
|
||||
#include "interp.h"
|
||||
#include "config.h"
|
||||
|
||||
RtStringV *RTSTRINGV_STACK = NULL;
|
||||
|
||||
@ -242,7 +243,7 @@ bool rt_eachfile(Token *tks) {
|
||||
|
||||
InterpResult save; string_memcpy(&save, &RES, sizeof(save));
|
||||
InterpResult *res;
|
||||
interp_runstring(s, &res, false, false);
|
||||
interp_runstring(s, &res, false);
|
||||
string_memcpy(&RES, &save, sizeof(save));
|
||||
|
||||
/* ufree(s); */
|
||||
@ -278,6 +279,28 @@ bool rt_mkaliasbn(Token *tks) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool rt_setlogcmds(Token *tks) {
|
||||
Token *tk = tks;
|
||||
|
||||
if (tk == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool set;
|
||||
|
||||
if (string_strcmp(tk->str, "yes") == 0) {
|
||||
set = true;
|
||||
} else if (string_strcmp(tk->str, "no") == 0) {
|
||||
set = false;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
CONFIG.logcmds = set;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void rt_init(void) {
|
||||
RTCMD("print", &rt_print);
|
||||
RTCMD("mkalias", &rt_mkalias);
|
||||
@ -287,4 +310,5 @@ void rt_init(void) {
|
||||
RTCMD("stackpop", &rt_stackpop);
|
||||
RTCMD("eachfile", &rt_eachfile);
|
||||
RTCMD("mkaliasbn", &rt_mkaliasbn);
|
||||
RTCMD("setlogcmds", &rt_setlogcmds);
|
||||
}
|
||||
|
||||
@ -38,6 +38,7 @@ bool rt_stackpush(struct Token *);
|
||||
bool rt_stackpop(struct Token *);
|
||||
bool rt_eachfile(struct Token *);
|
||||
bool rt_mkaliasbn(struct Token *);
|
||||
bool rt_setlogcmds(struct Token *);
|
||||
|
||||
void rtstringv_stackpushcopy(char *s, size_t len);
|
||||
char *rtstringv_stackpop(void);
|
||||
|
||||
Reference in New Issue
Block a user