console.lua 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. if (gui_enabled()) then
  12. -- Note that everything is "local" to this "if then"
  13. -- this way we don't add globals
  14. -- Evaluate Window
  15. local function evaluate_lua()
  16. local w = TextWindow.new("Evaluate Lua")
  17. w:set_editable()
  18. -- button callback
  19. local function eval()
  20. -- get the window's text and remove the result
  21. local text = string.gsub(w:get_text(),"%c*--%[%[.*--%]%]$","")
  22. -- if the text begins with '=' then convert = into return
  23. text = string.gsub(text,"^=","return ")
  24. -- evaluate text
  25. local result = assert(loadstring(text))()
  26. if (result ~= nil) then
  27. w:set(text .. '\n\n--[[ Result:\n' .. result .. '\n--]]')
  28. else
  29. w:set(text .. '\n\n--[[ Evaluated --]]')
  30. end
  31. end
  32. w:add_button("Evaluate",eval)
  33. end
  34. local console_open = false
  35. local date = rawget(os,"date") -- use rawget to avoid disabled's os.__index
  36. if type(date) ~= "function" then
  37. -- 'os' has been disabled, use a dummy function for date
  38. date = function() return "" end
  39. end
  40. -- Console Window
  41. local function run_console()
  42. if console_open then return end
  43. console_open = true
  44. local w = TextWindow.new("Console")
  45. w:add_button("Clear", function() w:clear(); print("Console cleared") end)
  46. -- save original logger functions
  47. local orig_print = print
  48. -- define new logger functions that append text to the window
  49. function print(...)
  50. local arg = {...}
  51. local n = #arg
  52. w:append(date() .. " ")
  53. for i=1, n do
  54. if i > 1 then w:append("\t") end
  55. w:append(tostring(arg[i]))
  56. end
  57. w:append("\n")
  58. end
  59. -- when the window gets closed restore the original logger functions
  60. local function at_close()
  61. print = orig_print
  62. console_open = false
  63. end
  64. w:set_atclose(at_close)
  65. print("Console opened")
  66. end
  67. function ref_manual()
  68. browser_open_url("https://www.wireshark.org/docs/wsdg_html_chunked/wsluarm.html")
  69. end
  70. function wiki_page()
  71. browser_open_url("https://gitlab.com/wireshark/wireshark/-/wikis/Lua")
  72. end
  73. register_menu("Lua/Evaluate", evaluate_lua, MENU_TOOLS_UNSORTED)
  74. register_menu("Lua/Console", run_console, MENU_TOOLS_UNSORTED)
  75. register_menu("Lua/Manual", ref_manual, MENU_TOOLS_UNSORTED)
  76. register_menu("Lua/Wiki", wiki_page, MENU_TOOLS_UNSORTED)
  77. end