From bd387f6826cb67bc3c4ba30d2e9ff95cd8d29aad Mon Sep 17 00:00:00 2001 From: batrick Date: Sun, 23 Jun 2013 02:40:28 +0000 Subject: [PATCH] With debugging, NSE prints out the script-args string and the pretty printed final script-args table. The rationale is, unfortunately shells interpret quotes differently and so it can be hard to tell exactly what NSE ends up seeing/producing. [Some discussion in #nmap on Freenode resulted in this addition.] --- nse_main.lua | 6 ++++++ nselib/stdnse.lua | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/nse_main.lua b/nse_main.lua index 4cae3f03e..5fa3cc229 100644 --- a/nse_main.lua +++ b/nse_main.lua @@ -1152,6 +1152,7 @@ end do -- Load script arguments (--script-args) local args = cnse.scriptargs or ""; + print_debug(1, "Script Arguments seen from CLI: %s", args); -- Parse a string in 'str' at 'start'. local function parse_string (str, start) @@ -1224,6 +1225,11 @@ do -- Load script arguments (--script-args) end nmap.registry.args = tmpargs end + if debugging() >= 2 then + local out = {} + rawget(stdnse, "pretty_printer")(nmap.registry.args, function (s) out[#out+1] = s end) + print_debug(2, concat(out)) + end end -- Update Missing Script Database? diff --git a/nselib/stdnse.lua b/nselib/stdnse.lua index c228a66ef..7ceb61ef0 100644 --- a/nselib/stdnse.lua +++ b/nselib/stdnse.lua @@ -24,6 +24,7 @@ local require = require; local select = select local setmetatable = setmetatable; local tonumber = tonumber; +local tostring = tostring; local type = type local ceil = math.ceil @@ -1159,4 +1160,39 @@ function output_table () return setmetatable({}, mt) end +--- A pretty printer for Lua objects. +-- +-- Takes an object (usually a table) and prints it using the +-- printer function. The printer function takes a sole string +-- argument and will be called repeatedly. +-- +-- @args obj The object to pretty print. +-- @args printer The printer function. +function pretty_printer (obj, printer) + if printer == nil then printer = print end + + local function aux (obj, spacing) + local t = type(obj) + if t == "table" then + printer "{\n" + for k, v in pairs(obj) do + local spacing = spacing.."\t" + printer(spacing) + printer "[" + aux(k, spacing) + printer "] = " + aux(v, spacing) + printer ",\n" + end + printer(spacing.."}") + elseif t == "string" then + printer(format("%q", obj)) + else + printer(tostring(obj)) + end + end + + return aux(obj, "") +end + return _ENV;