From Masashi Honma:
[obnox/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 -- $Id$
7 -- 
8 -- Wireshark - Network traffic analyzer
9 -- By Gerald Combs <gerald@wireshark.org>
10 -- Copyright 1998 Gerald Combs
11 --
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.
16 --
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.
21 --
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
26
27 if (gui_enabled()) then 
28         -- Note that everything is "local" to this "if then" 
29         -- this way we don't add globals
30
31         -- Evaluate Window
32         local function evaluate_lua()
33                 local w = TextWindow.new("Evaluate Lua")
34                 w:set_editable()
35
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*--%[%[.*--%]%]$","")
40
41                         -- if the text begins with '=' then convert = into return
42                         text = string.gsub(text,"^=","return ")
43
44                         -- evaluate text
45                         local result = assert(loadstring(text))()
46
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
53
54            w:add_button("Evaluate",eval)
55         end
56
57         local console_open = false
58
59         local date = rawget(os,"date") -- use rawget to avoid disabled's os.__index
60
61         if type(date) ~= "function" then
62                 -- 'os' has been disabled, use a dummy function for date
63                 date = function() return "" end
64         end
65
66         -- Console Window
67         local function run_console()
68                 if console_open then return end
69                 console_open = true
70
71                 local w = TextWindow.new("Console")
72
73                 -- save original logger functions
74                 local orig = {
75                         critical = critical,
76                         warn = warn,
77                         message = message,
78                         info = info,
79                         debug = debug
80                 }
81
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
88
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
96
97                         console_open = false
98                 end
99
100                 w:set_atclose(at_close)
101                 info("Console opened")
102         end
103
104         function wiki_page()
105                 browser_open_url("http://wiki.wireshark.org/Lua")
106         end
107
108         register_menu("Lua/Evaluate", evaluate_lua, MENU_TOOLS_UNSORTED)
109         register_menu("Lua/Console", run_console, MENU_TOOLS_UNSORTED)
110         register_menu("Lua/Wiki", wiki_page, MENU_TOOLS_UNSORTED)
111 end