From: bert hubert Date: Tue, 26 Apr 2016 12:05:33 +0000 (+0200) Subject: fix up newlines in console mode, move to 'return *line*' and if that is a syntax... X-Git-Tag: rec-4.0.0-alpha3~40^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F3767%2Fhead;p=thirdparty%2Fpdns.git fix up newlines in console mode, move to 'return *line*' and if that is a syntax error, execute '*line*', also print out simple Lua tables as JSON --- diff --git a/pdns/dnsdist-console.cc b/pdns/dnsdist-console.cc index 8d00418b88..5c3523805b 100644 --- a/pdns/dnsdist-console.cc +++ b/pdns/dnsdist-console.cc @@ -3,6 +3,7 @@ #include #include #include "dolog.hh" +#include "ext/json11/json11.hpp" vector > g_confDelta; @@ -45,7 +46,7 @@ void doClient(ComboAddress server, const std::string& command) readn2(fd, resp.get(), len); msg.assign(resp.get(), len); msg=sodDecryptSym(msg, g_key, theirs); - cout< lock(g_luamutex); - g_outputBuffer.clear(); - resetLuaSideEffect(); - auto ret=g_lua.executeCode< - boost::optional< - boost::variant< - string, - shared_ptr - > - > - >(line); - - if(ret) { - if (const auto strValue = boost::get>(&*ret)) { - cout<<(*strValue)->getName()<(&*ret)) { - cout<<*strValue< lock(g_luamutex); + g_outputBuffer.clear(); + resetLuaSideEffect(); + auto ret=g_lua.executeCode< + boost::optional< + boost::variant< + string, + shared_ptr, + std::unordered_map + > + > + >(withReturn ? ("return "+line) : line); + + if(ret) { + if (const auto strValue = boost::get>(&*ret)) { + cout<<(*strValue)->getName()<(&*ret)) { + cout<<*strValue< >(&*ret)) { + using namespace json11; + Json::object o; + for(const auto& v : *um) + o[v.first]=v.second; + Json out = o; + cout< lock(g_luamutex); - g_outputBuffer.clear(); - resetLuaSideEffect(); - auto ret=g_lua.executeCode< - boost::optional< - boost::variant< - string, - shared_ptr - > - > - >(line); + bool withReturn=true; + retry:; + try { + std::lock_guard lock(g_luamutex); + + g_outputBuffer.clear(); + resetLuaSideEffect(); + auto ret=g_lua.executeCode< + boost::optional< + boost::variant< + string, + shared_ptr, + std::unordered_map + > + > + >(withReturn ? ("return "+line) : line); if(ret) { if (const auto strValue = boost::get>(&*ret)) { - response=(*strValue)->getName(); + response=(*strValue)->getName()+"\n"; } else if (const auto strValue = boost::get(&*ret)) { - response=*strValue; + response=*strValue+"\n"; } + else if(const auto um = boost::get >(&*ret)) { + using namespace json11; + Json::object o; + for(const auto& v : *um) + o[v.first]=v.second; + Json out = o; + response=out.dump()+"\n"; + } } else response=g_outputBuffer; if(!getLuaNoSideEffect()) feedConfigDelta(line); + } + catch(const LuaContext::SyntaxErrorException&) { + if(withReturn) { + withReturn=false; + goto retry; + } + throw; + } } catch(const LuaContext::ExecutionErrorException& e) { response = "Error: " + string(e.what()) + ": ";