3 -- initialize wireshark's lua
5 -- This file is going to be executed before any other lua script.
6 -- It can be used to load libraries, disable functions and more.
8 -- Wireshark - Network traffic analyzer
9 -- By Gerald Combs <gerald@wireshark.org>
10 -- Copyright 1998 Gerald Combs
12 -- SPDX-License-Identifier: GPL-2.0-or-later
14 -- Set disable_lua to true to disable Lua support.
21 -- If set and we are running with special privileges this setting
22 -- tells whether scripts other than this one are to be run.
23 run_user_scripts_when_superuser = false
26 -- disable potentialy harmful lua functions when running superuser
27 if running_superuser then
28 local hint = "has been disabled due to running Wireshark as superuser. See https://wiki.wireshark.org/CaptureSetup/CapturePrivileges for help in running Wireshark as an unprivileged user."
29 local disabled_lib = {}
30 setmetatable(disabled_lib,{ __index = function() error("this package ".. hint) end } );
32 dofile = function() error("dofile " .. hint) end
33 loadfile = function() error("loadfile " .. hint) end
34 loadlib = function() error("loadlib " .. hint) end
35 require = function() error("require " .. hint) end
41 -- to avoid output to stdout which can cause problems lua's print ()
42 -- has been suppresed so that it yields an error.
43 -- have print() call info() instead.
49 local mt = getmetatable(obj)
50 return mt and mt.__typeof or obj.__typeof or type(obj)
53 -- the following function checks if a file exists
55 function file_exists(name)
56 local f = io.open(name,"r")
57 if f ~= nil then io.close(f) return true else return false end
60 -- the following function prepends the given directory name to
61 -- the package.path, so that a 'require "foo"' will work if 'foo'
62 -- is in the directory name given to this function. For example,
63 -- if your Lua file will do a 'require "foo"' and the foo.lua
64 -- file is in a local directory (local to your script) named 'bar',
65 -- then call this function before doing your 'require', by doing
66 -- package.prepend_path("bar")
67 -- and that will let Wireshark's Lua find the file "bar/foo.lua"
68 -- when you later do 'require "foo"'
70 -- Because this function resides here in init.lua, it does not
71 -- have the same environment as your script, so it has to get it
72 -- using the debug library, which is why the code appears so
76 function package.prepend_path(name)
77 local debug = require "debug"
78 -- get the function calling this package.prepend_path function
79 local dt = debug.getinfo(2, "f")
81 error("could not retrieve debug info table")
84 local _, val = debug.getupvalue(dt.func, 1)
85 if not val or type(val) ~= 'table' then
86 error("No calling function upvalue or it is not a table")
88 -- get the __DIR__ field in its upvalue table
89 local dir = val["__DIR__"]
90 -- get the platform-specific directory separator character
91 local sep = package.config:sub(1,1)
92 -- prepend the dir and given name to path
93 if dir and dir:len() > 0 then
94 package.path = dir .. sep .. name .. sep .. "?.lua;" .. package.path
96 -- also prepend just the name as a directory
97 package.path = name .. sep .. "?.lua;" .. package.path
110 -- the following table is since 2.0
113 -- the following table is since 1.12
116 -- the following table is since 1.11.3
117 %WTAP_PRESENCE_FLAGS%
125 -- the following table is since 1.11.3
130 -- other useful constants
131 -- DATA_DIR and USER_DIR have a trailing directory separator.
132 GUI_ENABLED = gui_enabled()
133 DATA_DIR = Dir.global_config_path()..package.config:sub(1,1)
134 USER_DIR = Dir.personal_config_path()..package.config:sub(1,1)
136 -- deprecated function names
137 datafile_path = Dir.global_config_path
138 persconffile_path = Dir.personal_config_path
141 dofile(DATA_DIR.."console.lua")
142 --dofile(DATA_DIR.."dtd_gen.lua")