]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
daemon/lua: interpreter is able to pretty-print expressions and results
authorMarek Vavruša <marek.vavrusa@nic.cz>
Wed, 8 Apr 2015 15:45:53 +0000 (17:45 +0200)
committerMarek Vavruša <marek.vavrusa@nic.cz>
Wed, 8 Apr 2015 15:45:53 +0000 (17:45 +0200)
daemon/engine.c
daemon/lua/sandbox.lua

index 4011407908784c4aec85d90a4d620f558591f6d9..8c88b4edaac55a4a0cb375d9712fc3254a362203 100644 (file)
@@ -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)
 {
index 6249f489a94fd457d2aa6c3fa2bffec5d2ae265e..09beab95586ff5eae6453ef41715974e116197d3 100644 (file)
@@ -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