From: Marek VavruĊĦa Date: Wed, 8 Apr 2015 15:45:53 +0000 (+0200) Subject: daemon/lua: interpreter is able to pretty-print expressions and results X-Git-Tag: v1.0.0-beta1~265^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c84060730ca8234731d2a9a8761212aa6b94e71b;p=thirdparty%2Fknot-resolver.git daemon/lua: interpreter is able to pretty-print expressions and results --- diff --git a/daemon/engine.c b/daemon/engine.c index 401140790..8c88b4eda 100644 --- a/daemon/engine.c +++ b/daemon/engine.c @@ -166,13 +166,13 @@ void engine_deinit(struct engine *engine) } /** 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) @@ -182,23 +182,11 @@ 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); } @@ -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)) /** 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) { diff --git a/daemon/lua/sandbox.lua b/daemon/lua/sandbox.lua index 6249f489a..09beab955 100644 --- a/daemon/lua/sandbox.lua +++ b/daemon/lua/sandbox.lua @@ -35,3 +35,40 @@ if setfenv then -- Lua 5.1 and less 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