2 -- A console and a window to execute commands in lua
4 -- (c) 2006 Luis E. Garcia Ontanon <luis@ontanon.org>
8 -- Wireshark - Network traffic analyzer
9 -- By Gerald Combs <gerald@wireshark.org>
10 -- Copyright 1998 Gerald Combs
12 -- This program is free software; you can redistribute it and/or
13 -- modify it under the terms of the GNU General Public License
14 -- as published by the Free Software Foundation; either version 2
15 -- of the License, or (at your option) any later version.
17 -- This program is distributed in the hope that it will be useful,
18 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
19 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 -- GNU General Public License for more details.
22 -- You should have received a copy of the GNU General Public License
23 -- along with this program; if not, write to the Free Software
24 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 if (gui_enabled()) then
28 -- Note that everything is "local" to this "if then"
29 -- this way we don't add globals
32 local function evaluate_lua()
33 local w
= TextWindow
.new("Evaluate Lua")
38 -- get the window's text and remove the result
39 local text
= string.gsub(w
:get_text(),"%c*--%[%[.*--%]%]$","")
41 -- if the text begins with '=' then convert = into return
42 text
= string.gsub(text
,"^=","return ")
45 local result
= assert(loadstring(text
))()
47 if (result
~= nil) then
48 w
:set(text
.. '\n\n--[[ Result:\n' .. result
.. '\n--]]')
50 w
:set(text
.. '\n\n--[[ Evaluated --]]')
54 w
:add_button("Evaluate",eval
)
57 local console_open
= false
59 local date = rawget(os
,"date") -- use rawget to avoid disabled's os.__index
61 if type(date) ~= "function" then
62 -- 'os' has been disabled, use a dummy function for date
63 date = function() return "" end
67 local function run_console()
68 if console_open
then return end
71 local w
= TextWindow
.new("Console")
73 -- save original logger functions
82 -- define new logger functions that append text to the window
83 function critical(x
) w
:append( date() .. " CRITICAL: " .. tostring(x
) .. "\n") end
84 function warn(x
) w
:append( date() .. " WARN: " .. tostring(x
) .. "\n") end
85 function message(x
) w
:append( date() .. " MESSAGE: " .. tostring(x
) .. "\n") end
86 function info(x
) w
:append( date() .. " INFO: " .. tostring(x
) .. "\n") end
87 function debug(x
) w
:append( date() .. " DEBUG: " .. tostring(x
) .. "\n") end
89 -- when the window gets closed restore the original logger functions
90 local function at_close()
91 critical
= orig
.critical
93 message
= orig
.message
100 w
:set_atclose(at_close
)
101 info("Console opened")
104 function ref_manual()
105 browser_open_url("http://www.wireshark.org/docs/wsug_html_chunked/wsluarm.html")
109 browser_open_url("http://wiki.wireshark.org/Lua")
112 register_menu("Lua/Evaluate", evaluate_lua
, MENU_TOOLS_UNSORTED
)
113 register_menu("Lua/Console", run_console
, MENU_TOOLS_UNSORTED
)
114 register_menu("Lua/Manual", ref_manual
, MENU_TOOLS_UNSORTED
)
115 register_menu("Lua/Wiki", wiki_page
, MENU_TOOLS_UNSORTED
)