}
/** Execute current chunk in the sandbox */
-static int l_sandboxcall(lua_State *L)
+static int l_sandboxcall(lua_State *L, int argc)
{
#if LUA_VERSION_NUM >= 502
lua_getglobal(L, "_SANDBOX");
lua_setupvalue(L, -2, 1);
#endif
- return lua_pcall(L, 0, LUA_MULTRET, 0);
+ return lua_pcall(L, argc, LUA_MULTRET, 0);
}
int engine_cmd(struct engine *engine, const char *str)
}
/* Evaluate results */
- int ret = luaL_loadstring(engine->L, str);
- if (ret == 0) {
- ret = l_sandboxcall(engine->L);
- }
-
- /* Print results. */
- int nres = lua_gettop(engine->L);
- for (int i = 0; i < nres; ++i) {
- const char *out = lua_tostring(engine->L, -1);
- if (out != NULL) {
- printf("%s\n", out);
- }
- lua_pop(engine->L, 1);
- }
+ lua_getglobal(engine->L, "eval_cmd");
+ lua_pushstring(engine->L, str);
/* Check result. */
- if (ret != 0) {
+ if (l_sandboxcall(engine->L, 1) != 0) {
return kr_error(EINVAL);
}
(luaL_loadbuffer((L), (arr), (len), (name)) || lua_pcall((L), 0, LUA_MULTRET, 0))
/** Load file in a sandbox environment. */
#define l_dosandboxfile(L, filename) \
- (luaL_loadfile((L), (filename)) || l_sandboxcall((L)))
+ (luaL_loadfile((L), (filename)) || l_sandboxcall((L), 0))
static int engine_loadconf(struct engine *engine)
{
else -- Lua 5.2+
_SANDBOX = make_sandbox(_ENV)
end
+
+-- Pretty printing
+function table_print (tt, indent, done)
+ done = done or {}
+ indent = indent or 0
+ if type(tt) == "table" then
+ for key, value in pairs (tt) do
+ io.write(string.rep (" ", indent))
+ if type (value) == "table" and not done [value] then
+ done [value] = true
+ io.write(string.format("[%s] => {\n", tostring (key)));
+ table_print (value, indent + 4, done)
+ io.write(string.rep (" ", indent))
+ io.write("}\n");
+ else
+ io.write(string.format("[%s] => %s\n",
+ tostring (key), tostring(value)))
+ end
+ end
+ else
+ io.write(tostring(tt) .. "\n")
+ end
+end
+
+-- Interactive command evaluation
+function eval_cmd(line)
+ local chunk, err = loadstring('table_print('..line..')')
+ if err then
+ chunk, err = loadstring(line)
+ end
+ if not err then
+ status, err = pcall(chunk)
+ end
+ if err then
+ print(err)
+ end
+end
\ No newline at end of file