1-- console 2-- A console and a window to execute commands in lua 3-- 4-- (c) 2006 Luis E. Garcia Ontanon <luis@ontanon.org> 5-- 6-- Wireshark - Network traffic analyzer 7-- By Gerald Combs <gerald@wireshark.org> 8-- Copyright 1998 Gerald Combs 9-- 10-- SPDX-License-Identifier: GPL-2.0-or-later 11 12 13if (gui_enabled()) then 14 -- Note that everything is "local" to this "if then" 15 -- this way we don't add globals 16 17 -- Evaluate Window 18 local function evaluate_lua() 19 local w = TextWindow.new("Evaluate Lua") 20 w:set_editable() 21 22 -- button callback 23 local function eval() 24 -- get the window's text and remove the result 25 local text = string.gsub(w:get_text(),"%c*--%[%[.*--%]%]$","") 26 27 -- if the text begins with '=' then convert = into return 28 text = string.gsub(text,"^=","return ") 29 30 -- evaluate text 31 local result = assert(loadstring(text))() 32 33 if (result ~= nil) then 34 w:set(text .. '\n\n--[[ Result:\n' .. result .. '\n--]]') 35 else 36 w:set(text .. '\n\n--[[ Evaluated --]]') 37 end 38 end 39 40 w:add_button("Evaluate",eval) 41 end 42 43 local console_open = false 44 45 local date = rawget(os,"date") -- use rawget to avoid disabled's os.__index 46 47 if type(date) ~= "function" then 48 -- 'os' has been disabled, use a dummy function for date 49 date = function() return "" end 50 end 51 52 -- Console Window 53 local function run_console() 54 if console_open then return end 55 console_open = true 56 57 local w = TextWindow.new("Console") 58 59 -- save original logger functions 60 local orig_print = print 61 62 -- define new logger functions that append text to the window 63 function print(...) 64 local arg = {...} 65 local n = #arg 66 w:append(date() .. " ") 67 for i=1, n do 68 if i > 1 then w:append("\t") end 69 w:append(tostring(arg[i])) 70 end 71 w:append("\n") 72 end 73 74 -- when the window gets closed restore the original logger functions 75 local function at_close() 76 print = old_print 77 78 console_open = false 79 end 80 81 w:set_atclose(at_close) 82 print("Console opened") 83 end 84 85 function ref_manual() 86 browser_open_url("https://www.wireshark.org/docs/wsdg_html_chunked/wsluarm.html") 87 end 88 89 function wiki_page() 90 browser_open_url("https://gitlab.com/wireshark/wireshark/-/wikis/Lua") 91 end 92 93 register_menu("Lua/Evaluate", evaluate_lua, MENU_TOOLS_UNSORTED) 94 register_menu("Lua/Console", run_console, MENU_TOOLS_UNSORTED) 95 register_menu("Lua/Manual", ref_manual, MENU_TOOLS_UNSORTED) 96 register_menu("Lua/Wiki", wiki_page, MENU_TOOLS_UNSORTED) 97end 98