Nmap Development mailing list archives

[NSE patch] print_debug() function


From: Matthew Boyle <mb2263 () bristol ac uk>
Date: Fri, 22 Jun 2007 00:31:12 +0100

attached is a lua function which allows debug messages in scripts to use printf()-like formatting (via lua's own string.format() function):

print_debug([verbosity,] fmt, ...);

i've also included a patch to nmap.print_debug_unformatted(). it saves checking a second time if the first argument is a verbosity level, by making it compulsory.

(it also includes a fix to scripts/showHTMLTitle.nse, which seems to be the only script in svn using it at the moment.)

--matt

--
everybody stand back!  i know regular expressions.  [taptaptap]


print_debug = function(...)
        local verbosity = 1;
        if ((#arg > 1) and (tonumber(arg[1]))) then
                verbosity = table.remove(arg, 1);
        end
        
        nmap.print_debug_unformatted(verbosity, string.format(unpack(arg, start)));
end
Index: scripts/showHTMLTitle.nse
===================================================================
--- scripts/showHTMLTitle.nse   (revision 4975)
+++ scripts/showHTMLTitle.nse   (working copy)
@@ -50,7 +50,7 @@
        if title ~= nil then
                result = string.gsub(title , "[\n\r\t]", "")
                if string.len(title) > 50 then
-                       nmap.print_debug_unformatted("showHTMLTitle.nse: Title got truncated!");
+                       nmap.print_debug("showHTMLTitle.nse: Title got truncated!");
                        result = string.sub(result, 1, 62) .. "..."
                end
        else
Index: nse_nmaplib.cc
===================================================================
--- nse_nmaplib.cc      (revision 4975)
+++ nse_nmaplib.cc      (working copy)
@@ -399,16 +399,16 @@
 }
 
 static int l_print_debug_unformatted(lua_State *l) {
-       int verbosity=1, stack_counter(1);
+       int verbosity;
        const char *out;
 
-       if (lua_isnumber (l, 1) && (lua_gettop(l) > 1)) {
-               verbosity = lua_tointeger(l, 1);
-               if (verbosity > o.verbose) return 0;
-               stack_counter++;
-       }
-       out = luaL_checkstring(l, stack_counter);
-       log_write(LOG_STDOUT, "%s NSE DEBUG: %s\n", SCRIPT_ENGINE, out);
+       if (lua_gettop(l) != 2) return luaL_error(l, "Incorrect number of arguments\n");
+
+       verbosity = luaL_checkinteger(l, 1);
+       if (verbosity > o.verbose) return 0;
+       out = luaL_checkstring(l, 2);
+
+       log_write(LOG_STDOUT, "%s DEBUG: %s\n", SCRIPT_ENGINE, out);
        return 0;
 }
 

_______________________________________________
Sent through the nmap-dev mailing list
http://cgi.insecure.org/mailman/listinfo/nmap-dev
Archived at http://SecLists.Org

Current thread: