HACK: pinfo->private_data points to smb_info again
[wireshark-wip.git] / epan / wslua / console.lua
blobae065640eae4cd297753383d31daa2ff8177fee7
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 -- $Id$
7 --
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
31 -- Evaluate Window
32 local function evaluate_lua()
33 local w = TextWindow.new("Evaluate Lua")
34 w:set_editable()
36 -- button callback
37 local function eval()
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 ")
44 -- evaluate text
45 local result = assert(loadstring(text))()
47 if (result ~= nil) then
48 w:set(text .. '\n\n--[[ Result:\n' .. result .. '\n--]]')
49 else
50 w:set(text .. '\n\n--[[ Evaluated --]]')
51 end
52 end
54 w:add_button("Evaluate",eval)
55 end
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
64 end
66 -- Console Window
67 local function run_console()
68 if console_open then return end
69 console_open = true
71 local w = TextWindow.new("Console")
73 -- save original logger functions
74 local orig = {
75 critical = critical,
76 warn = warn,
77 message = message,
78 info = info,
79 debug = debug
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
92 warn = orig.warn
93 message = orig.message
94 info = orig.info
95 debug = orig.debug
97 console_open = false
98 end
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")
108 function wiki_page()
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)