2 -- A console and a window to execute commands in lua
4 -- (c) 2006 Luis E. Garcia Ontanon <luis@ontanon.org>
6 -- Wireshark - Network traffic analyzer
7 -- By Gerald Combs <gerald@wireshark.org>
8 -- Copyright 1998 Gerald Combs
10 -- SPDX-License-Identifier: GPL-2.0-or-later
13 if (gui_enabled()) then
14 -- Note that everything is "local" to this "if then"
15 -- this way we don't add globals
18 local function evaluate_lua()
19 local w = TextWindow.new("Evaluate Lua")
24 -- get the window's text and remove the result
25 local text = string.gsub(w:get_text(),"%c*--%[%[.*--%]%]$","")
27 -- if the text begins with '=' then convert = into return
28 text = string.gsub(text,"^=","return ")
31 local result = assert(loadstring(text))()
33 if (result ~= nil) then
34 w:set(text .. '\n\n--[[ Result:\n' .. result .. '\n--]]')
36 w:set(text .. '\n\n--[[ Evaluated --]]')
40 w:add_button("Evaluate",eval)
43 local console_open = false
45 local date = rawget(os,"date") -- use rawget to avoid disabled's os.__index
47 if type(date) ~= "function" then
48 -- 'os' has been disabled, use a dummy function for date
49 date = function() return "" end
53 local function run_console()
54 if console_open then return end
57 local w = TextWindow.new("Console")
59 -- save original logger functions
68 -- define new logger functions that append text to the window
69 function critical(x) w:append( date() .. " CRITICAL: " .. tostring(x) .. "\n") end
70 function warn(x) w:append( date() .. " WARN: " .. tostring(x) .. "\n") end
71 function message(x) w:append( date() .. " MESSAGE: " .. tostring(x) .. "\n") end
72 function info(x) w:append( date() .. " INFO: " .. tostring(x) .. "\n") end
73 function debug(x) w:append( date() .. " DEBUG: " .. tostring(x) .. "\n") end
75 -- when the window gets closed restore the original logger functions
76 local function at_close()
77 critical = orig.critical
79 message = orig.message
86 w:set_atclose(at_close)
87 info("Console opened")
91 browser_open_url("https://www.wireshark.org/docs/wsdg_html_chunked/wsluarm.html")
95 browser_open_url("https://wiki.wireshark.org/Lua")
98 register_menu("Lua/Evaluate", evaluate_lua, MENU_TOOLS_UNSORTED)
99 register_menu("Lua/Console", run_console, MENU_TOOLS_UNSORTED)
100 register_menu("Lua/Manual", ref_manual, MENU_TOOLS_UNSORTED)
101 register_menu("Lua/Wiki", wiki_page, MENU_TOOLS_UNSORTED)