023016e51cdb04cffcbe63498b88898092f726ab
[metze/wireshark/wip.git] / epan / wslua / console.lua
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
13 if (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 = {
61                         critical = critical,
62                         warn = warn,
63                         message = message,
64                         info = info,
65                         debug = debug
66                 }
67
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
74
75                 -- when the window gets closed restore the original logger functions
76                 local function at_close()
77                         critical = orig.critical
78                         warn = orig.warn
79                         message = orig.message
80                         info = orig.info
81                         debug = orig.debug
82
83                         console_open = false
84                 end
85
86                 w:set_atclose(at_close)
87                 info("Console opened")
88         end
89
90         function ref_manual()
91                 browser_open_url("https://www.wireshark.org/docs/wsdg_html_chunked/wsluarm.html")
92         end
93
94         function wiki_page()
95                 browser_open_url("https://wiki.wireshark.org/Lua")
96         end
97
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)
102 end