2006-10-17 18:29:24 +00:00
|
|
|
-- console
|
|
|
|
-- A console and a window to execute commands in lua
|
|
|
|
--
|
2008-08-05 21:03:46 +00:00
|
|
|
-- (c) 2006 Luis E. Garcia Ontanon <luis@ontanon.org>
|
2006-10-17 18:29:24 +00:00
|
|
|
--
|
|
|
|
-- Wireshark - Network traffic analyzer
|
|
|
|
-- By Gerald Combs <gerald@wireshark.org>
|
|
|
|
-- Copyright 1998 Gerald Combs
|
|
|
|
--
|
2018-02-09 09:12:31 +00:00
|
|
|
-- SPDX-License-Identifier: GPL-2.0-or-later
|
2006-10-17 18:29:24 +00:00
|
|
|
|
|
|
|
|
2006-10-17 18:20:44 +00:00
|
|
|
if (gui_enabled()) then
|
2006-12-23 03:45:46 +00:00
|
|
|
-- Note that everything is "local" to this "if then"
|
|
|
|
-- this way we don't add globals
|
|
|
|
|
|
|
|
-- Evaluate Window
|
2006-10-17 18:20:44 +00:00
|
|
|
local function evaluate_lua()
|
|
|
|
local w = TextWindow.new("Evaluate Lua")
|
2006-10-18 15:05:55 +00:00
|
|
|
w:set_editable()
|
2006-10-17 18:20:44 +00:00
|
|
|
|
2006-12-23 03:45:46 +00:00
|
|
|
-- button callback
|
|
|
|
local function eval()
|
|
|
|
-- get the window's text and remove the result
|
2006-10-17 18:20:44 +00:00
|
|
|
local text = string.gsub(w:get_text(),"%c*--%[%[.*--%]%]$","")
|
2006-12-23 03:45:46 +00:00
|
|
|
|
|
|
|
-- if the text begins with '=' then convert = into return
|
2006-10-17 18:20:44 +00:00
|
|
|
text = string.gsub(text,"^=","return ")
|
|
|
|
|
2006-12-23 03:45:46 +00:00
|
|
|
-- evaluate text
|
2006-10-17 18:20:44 +00:00
|
|
|
local result = assert(loadstring(text))()
|
|
|
|
|
|
|
|
if (result ~= nil) then
|
|
|
|
w:set(text .. '\n\n--[[ Result:\n' .. result .. '\n--]]')
|
|
|
|
else
|
|
|
|
w:set(text .. '\n\n--[[ Evaluated --]]')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
w:add_button("Evaluate",eval)
|
|
|
|
end
|
|
|
|
|
|
|
|
local console_open = false
|
|
|
|
|
2006-10-18 17:59:49 +00:00
|
|
|
local date = rawget(os,"date") -- use rawget to avoid disabled's os.__index
|
|
|
|
|
|
|
|
if type(date) ~= "function" then
|
2006-12-23 03:45:46 +00:00
|
|
|
-- 'os' has been disabled, use a dummy function for date
|
2006-10-18 17:59:49 +00:00
|
|
|
date = function() return "" end
|
|
|
|
end
|
|
|
|
|
2006-12-23 03:45:46 +00:00
|
|
|
-- Console Window
|
2006-10-17 18:20:44 +00:00
|
|
|
local function run_console()
|
|
|
|
if console_open then return end
|
|
|
|
console_open = true
|
|
|
|
|
|
|
|
local w = TextWindow.new("Console")
|
2022-03-23 15:29:07 +00:00
|
|
|
w:add_button("Clear", function() w:clear(); print("Console cleared") end)
|
2006-10-17 18:20:44 +00:00
|
|
|
|
2006-12-23 03:45:46 +00:00
|
|
|
-- save original logger functions
|
2018-09-06 14:24:24 +00:00
|
|
|
local orig_print = print
|
2006-10-17 18:20:44 +00:00
|
|
|
|
2006-12-23 03:45:46 +00:00
|
|
|
-- define new logger functions that append text to the window
|
2018-09-06 14:24:24 +00:00
|
|
|
function print(...)
|
|
|
|
local arg = {...}
|
|
|
|
local n = #arg
|
|
|
|
w:append(date() .. " ")
|
|
|
|
for i=1, n do
|
|
|
|
if i > 1 then w:append("\t") end
|
|
|
|
w:append(tostring(arg[i]))
|
|
|
|
end
|
|
|
|
w:append("\n")
|
|
|
|
end
|
2006-10-17 18:20:44 +00:00
|
|
|
|
2006-12-23 03:45:46 +00:00
|
|
|
-- when the window gets closed restore the original logger functions
|
|
|
|
local function at_close()
|
2022-03-23 15:29:07 +00:00
|
|
|
print = orig_print
|
2006-10-17 18:20:44 +00:00
|
|
|
|
|
|
|
console_open = false
|
|
|
|
end
|
|
|
|
|
|
|
|
w:set_atclose(at_close)
|
2018-09-06 14:24:24 +00:00
|
|
|
print("Console opened")
|
2006-10-17 18:20:44 +00:00
|
|
|
end
|
|
|
|
|
2011-01-31 21:13:20 +00:00
|
|
|
function ref_manual()
|
2015-11-26 12:44:33 +00:00
|
|
|
browser_open_url("https://www.wireshark.org/docs/wsdg_html_chunked/wsluarm.html")
|
2011-01-31 21:13:20 +00:00
|
|
|
end
|
2015-11-26 12:44:33 +00:00
|
|
|
|
2006-12-27 01:46:42 +00:00
|
|
|
function wiki_page()
|
2020-10-02 17:25:54 +00:00
|
|
|
browser_open_url("https://gitlab.com/wireshark/wireshark/-/wikis/Lua")
|
2006-12-27 01:46:42 +00:00
|
|
|
end
|
|
|
|
|
2009-06-11 13:08:06 +00:00
|
|
|
register_menu("Lua/Evaluate", evaluate_lua, MENU_TOOLS_UNSORTED)
|
|
|
|
register_menu("Lua/Console", run_console, MENU_TOOLS_UNSORTED)
|
2011-01-31 21:13:20 +00:00
|
|
|
register_menu("Lua/Manual", ref_manual, MENU_TOOLS_UNSORTED)
|
2009-06-11 13:08:06 +00:00
|
|
|
register_menu("Lua/Wiki", wiki_page, MENU_TOOLS_UNSORTED)
|
2006-12-23 03:45:46 +00:00
|
|
|
end
|