tb Enable/disable command logging dynamically + setlogcmds builtin
This commit is contained in:
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;
|
||||
@ -124,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;
|
||||
}
|
||||
@ -166,7 +168,7 @@ 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));
|
||||
|
||||
@ -175,7 +177,7 @@ bool interp_runstring(char *string, InterpResult **res, bool logcmds, bool inter
|
||||
|
||||
char *line = string_tokenizealloc_linecontinue(string, "\n");
|
||||
while (line != NULL) {
|
||||
if (logcmds) {
|
||||
if (CONFIG.logcmds) {
|
||||
uprintf("+%s\n", line);
|
||||
}
|
||||
|
||||
|
||||
@ -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