home *** CD-ROM | disk | FTP | other *** search
/ Mikrobitti Huvi & Hyöty 2007 / MBHH07.iso / Internet / Muut / Asenna / Wireshark.exe / console.lua < prev    next >
Encoding:
Text File  |  2006-10-31  |  2.7 KB  |  93 lines

  1. -- console
  2. -- A console and a window to execute commands in lua
  3. --
  4. -- (c) 2006 Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
  5. --
  6. -- $Id: console.lua 19593 2006-10-18 17:59:49Z lego $
  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.     local function evaluate_lua()
  29.         local w = TextWindow.new("Evaluate Lua")
  30.         w:set_editable()
  31.  
  32.         function eval()
  33.             local text = string.gsub(w:get_text(),"%c*--%[%[.*--%]%]$","")
  34.             text = string.gsub(text,"^=","return ")
  35.  
  36.             local result = assert(loadstring(text))()
  37.  
  38.             if (result ~= nil) then
  39.                 w:set(text .. '\n\n--[[ Result:\n' .. result .. '\n--]]')
  40.             else
  41.                 w:set(text .. '\n\n--[[  Evaluated --]]')
  42.             end
  43.         end
  44.  
  45.        w:add_button("Evaluate",eval)
  46.     end
  47.  
  48.     local console_open = false
  49.  
  50.     local date = rawget(os,"date") -- use rawget to avoid disabled's os.__index
  51.  
  52.     if type(date) ~= "function" then
  53.         -- 'os' has been disabled use a dummy function for date
  54.         date = function() return "" end
  55.     end
  56.  
  57.     local function run_console()
  58.         if console_open then return end
  59.         console_open = true
  60.  
  61.         local w = TextWindow.new("Console")
  62.  
  63.         local orig = {
  64.             critical = critical,
  65.             warn = warn,
  66.             message = message,
  67.             info = info,
  68.             debug = debug
  69.         }
  70.  
  71.         function critical(x)  w:append( date() .. " CRITICAL: " .. tostring(x) .. "\n") end
  72.         function warn(x)  w:append( date() .. " WARN: " .. tostring(x) .. "\n") end
  73.         function message(x)  w:append( date() .. " MESSAGE: " .. tostring(x) .. "\n") end
  74.         function info(x)  w:append( date() .. " INFO: " .. tostring(x) .. "\n") end
  75.         function debug(x)  w:append( date() .. " DEBUG: " .. tostring(x) .. "\n") end
  76.  
  77.         function at_close()
  78.             critical = orig.critical
  79.             warn = orig.warn
  80.             message = orig.message
  81.             info = orig.info
  82.             debug = orig.debug
  83.  
  84.             console_open = false
  85.         end
  86.  
  87.         w:set_atclose(at_close)
  88.         info("Console opened")
  89.     end
  90.  
  91.     register_menu("Lua/Evaluate",evaluate_lua,MENU_TOOLS)
  92.     register_menu("Lua/Console",run_console,MENU_TOOLS)
  93. end