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