Lua: ensure that DATA_DIR and USER_DIR have trailing slashes
[metze/wireshark/wip.git] / epan / wslua / template-init.lua
1 -- init.lua
2 --
3 -- initialize wireshark's lua
4 --
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.
7 --
8 -- Wireshark - Network traffic analyzer
9 -- By Gerald Combs <gerald@wireshark.org>
10 -- Copyright 1998 Gerald Combs
11 --
12 -- SPDX-License-Identifier: GPL-2.0-or-later
13
14 -- Set disable_lua to true to disable Lua support.
15 disable_lua = false
16
17 if disable_lua then
18     return
19 end
20
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
24
25
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 } );
31
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
36     os = disabled_lib
37     io = disabled_lib
38     file = disabled_lib
39 end
40
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.
44 if gui_enabled() then
45     print = info
46 end
47
48 function typeof(obj)
49     local mt = getmetatable(obj)
50     return mt and mt.__typeof or obj.__typeof or type(obj)
51 end
52
53 -- the following function checks if a file exists
54 -- since 1.11.3
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
58 end
59
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"'
69 --
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
73 -- cumbersome.
74 --
75 -- since 1.11.3
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")
80     if not dt then
81         error("could not retrieve debug info table")
82     end
83     -- get its upvalue
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")
87     end
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
95     end
96     -- also prepend just the name as a directory
97     package.path = name .. sep .. "?.lua;" .. package.path
98 end
99
100 %WTAP_ENCAPS%
101
102 %WTAP_FILETYPES%
103
104 %WTAP_TSPRECS%
105
106 %WTAP_COMMENTTYPES%
107
108 %FT_TYPES%
109
110 -- the following table is since 2.0
111 %FT_FRAME_TYPES%
112
113 -- the following table is since 1.12
114 %WTAP_REC_TYPES%
115
116 -- the following table is since 1.11.3
117 %WTAP_PRESENCE_FLAGS%
118
119 %BASES%
120
121 %ENCODINGS%
122
123 %EXPERT%
124
125 -- the following table is since 1.11.3
126 %EXPERT_TABLE%
127
128 %MENU_GROUPS%
129
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)
135
136 -- deprecated function names
137 datafile_path = Dir.global_config_path
138 persconffile_path = Dir.personal_config_path
139
140
141 dofile(DATA_DIR.."console.lua")
142 --dofile(DATA_DIR.."dtd_gen.lua")