From 4dd04a32a5aa84499c76bd8d488c9576b19c4a07 Mon Sep 17 00:00:00 2001 From: david Date: Sun, 31 Jan 2010 19:38:13 +0000 Subject: [PATCH] Make stdnse.format_output return nil when passed an empty table. Before it was returning an empty string, causing blank output entries for scripts with no output: 80/tcp open http |_citrix-enum-apps-xml: |_citrix-enum-servers-xml: | http-headers: | Date: Sun, 31 Jan 2010 19:28:13 GMT | Server: Apache/2.2.3 (CentOS) | Accept-Ranges: bytes | Content-Length: 739 | Connection: close | Content-Type: text/html; charset=UTF-8 | |_ (Request type: HEAD) |_http-date: Sun, 31 Jan 2010 19:28:14 GMT; +1s from local time. |_html-title: Go ahead and ScanMe! --- nselib/stdnse.lua | 117 +++++++++++++++++++++++++--------------------- 1 file changed, 64 insertions(+), 53 deletions(-) diff --git a/nselib/stdnse.lua b/nselib/stdnse.lua index 07dc3020d..1677eff31 100644 --- a/nselib/stdnse.lua +++ b/nselib/stdnse.lua @@ -365,55 +365,9 @@ local function format_get_indent(indent, at_end) return str end ----Takes a table of output on the commandline and formats it for display to the --- user. This is basically done by converting an array of nested tables into a --- string. In addition to numbered array elements, each table can have a 'name' --- and a 'warning' value. The 'name' will be displayed above the table, and --- 'warning' will be displayed, with a 'WARNING' tag, if and only if debugging --- is enabled. --- --- Here's an example of a table: --- --- local domains = {} --- domains['name'] = "DOMAINS" --- table.insert(domains, 'Domain 1') --- table.insert(domains, 'Domain 2') --- --- local names = {} --- names['name'] = "NAMES" --- names['warning'] = "Not all names could be determined!" --- table.insert(names, "Name 1") --- --- local response = {} --- table.insert(response, "Apple pie") --- table.insert(response, domains) --- table.insert(response, names) --- --- return stdnse.format_output(true, response) --- --- --- With debugging enabled, this is the output: --- --- Host script results: --- | smb-enum-domains: --- | Apple pie --- | DOMAINS --- | Domain 1 --- | Domain 2 --- | NAMES (WARNING: Not all names could be determined!) --- |_ Name 1 --- --- ---@param status A boolean value dictating whether or not the script succeeded. --- If status is false, and debugging is enabled, 'ERROR' is prepended --- to every line. If status is false and ebugging is disabled, no output --- occurs. ---@param data The table of output. ---@param indent Used for indentation on recursive calls; should generally be set to --- nil when callling from a script. -function format_output(status, data, indent) - -- Don't bother if we don't have any data - if(#data == 0) then +-- A helper for format_output (see below). +local function format_output_sub(status, data, indent) + if (#data == 0) then return "" end @@ -422,9 +376,6 @@ function format_output(status, data, indent) return data[1] end - -- If data is nil, die with an error (I keep doing that by accident) - assert(data, "No data was passed to format_output()") - -- Used to put 'ERROR: ' in front of all lines on error messages local prefix = "" -- Initialize the output string to blank (or, if we're at the top, add a newline) @@ -472,7 +423,7 @@ function format_output(status, data, indent) insert(new_indent, true) end - output = output .. format_output(status, value, new_indent) + output = output .. format_output_sub(status, value, new_indent) elseif(type(value) == 'string') then if(i ~= #data) then @@ -486,6 +437,66 @@ function format_output(status, data, indent) return output end +---Takes a table of output on the commandline and formats it for display to the +-- user. This is basically done by converting an array of nested tables into a +-- string. In addition to numbered array elements, each table can have a 'name' +-- and a 'warning' value. The 'name' will be displayed above the table, and +-- 'warning' will be displayed, with a 'WARNING' tag, if and only if debugging +-- is enabled. +-- +-- Here's an example of a table: +-- +-- local domains = {} +-- domains['name'] = "DOMAINS" +-- table.insert(domains, 'Domain 1') +-- table.insert(domains, 'Domain 2') +-- +-- local names = {} +-- names['name'] = "NAMES" +-- names['warning'] = "Not all names could be determined!" +-- table.insert(names, "Name 1") +-- +-- local response = {} +-- table.insert(response, "Apple pie") +-- table.insert(response, domains) +-- table.insert(response, names) +-- +-- return stdnse.format_output(true, response) +-- +-- +-- With debugging enabled, this is the output: +-- +-- Host script results: +-- | smb-enum-domains: +-- | Apple pie +-- | DOMAINS +-- | Domain 1 +-- | Domain 2 +-- | NAMES (WARNING: Not all names could be determined!) +-- |_ Name 1 +-- +-- +--@param status A boolean value dictating whether or not the script succeeded. +-- If status is false, and debugging is enabled, 'ERROR' is prepended +-- to every line. If status is false and ebugging is disabled, no output +-- occurs. +--@param data The table of output. +--@param indent Used for indentation on recursive calls; should generally be set to +-- nil when callling from a script. +-- @return nil, if data is empty, otherwise a +-- multiline string. +function format_output(status, data, indent) + -- If data is nil, die with an error (I keep doing that by accident) + assert(data, "No data was passed to format_output()") + + -- Don't bother if we don't have any data + if (#data == 0) then + return nil + end + + return format_output_sub(status, data, indent) +end + --- This function allows you to create worker threads that may perform -- network tasks in parallel with your script thread. --