Commit c8406073 authored by Marek Vavruša's avatar Marek Vavruša

daemon/lua: interpreter is able to pretty-print expressions and results

parent 9a16eceb
...@@ -166,13 +166,13 @@ void engine_deinit(struct engine *engine) ...@@ -166,13 +166,13 @@ void engine_deinit(struct engine *engine)
} }
/** Execute current chunk in the sandbox */ /** 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 #if LUA_VERSION_NUM >= 502
lua_getglobal(L, "_SANDBOX"); lua_getglobal(L, "_SANDBOX");
lua_setupvalue(L, -2, 1); lua_setupvalue(L, -2, 1);
#endif #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) int engine_cmd(struct engine *engine, const char *str)
...@@ -182,23 +182,11 @@ int engine_cmd(struct engine *engine, const char *str) ...@@ -182,23 +182,11 @@ int engine_cmd(struct engine *engine, const char *str)
} }
/* Evaluate results */ /* Evaluate results */
int ret = luaL_loadstring(engine->L, str); lua_getglobal(engine->L, "eval_cmd");
if (ret == 0) { lua_pushstring(engine->L, str);
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);
}
/* Check result. */ /* Check result. */
if (ret != 0) { if (l_sandboxcall(engine->L, 1) != 0) {
return kr_error(EINVAL); return kr_error(EINVAL);
} }
...@@ -210,7 +198,7 @@ int engine_cmd(struct engine *engine, const char *str) ...@@ -210,7 +198,7 @@ int engine_cmd(struct engine *engine, const char *str)
(luaL_loadbuffer((L), (arr), (len), (name)) || lua_pcall((L), 0, LUA_MULTRET, 0)) (luaL_loadbuffer((L), (arr), (len), (name)) || lua_pcall((L), 0, LUA_MULTRET, 0))
/** Load file in a sandbox environment. */ /** Load file in a sandbox environment. */
#define l_dosandboxfile(L, filename) \ #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) static int engine_loadconf(struct engine *engine)
{ {
......
...@@ -35,3 +35,40 @@ if setfenv then -- Lua 5.1 and less ...@@ -35,3 +35,40 @@ if setfenv then -- Lua 5.1 and less
else -- Lua 5.2+ else -- Lua 5.2+
_SANDBOX = make_sandbox(_ENV) _SANDBOX = make_sandbox(_ENV)
end 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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment