Add various functions for Lua directory handling and path info
[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 -- $Id$
9 --
10 -- Wireshark - Network traffic analyzer
11 -- By Gerald Combs <gerald@wireshark.org>
12 -- Copyright 1998 Gerald Combs
13 --
14 -- This program is free software; you can redistribute it and/or
15 -- modify it under the terms of the GNU General Public License
16 -- as published by the Free Software Foundation; either version 2
17 -- of the License, or (at your option) any later version.
18 --
19 -- This program is distributed in the hope that it will be useful,
20 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
21 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 -- GNU General Public License for more details.
23 --
24 -- You should have received a copy of the GNU General Public License
25 -- along with this program; if not, write to the Free Software
26 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27
28 -- Set disable_lua to true to disable Lua support.
29 disable_lua = false
30
31 if disable_lua then
32     return
33 end
34
35 -- If set and we are running with special privileges this setting
36 -- tells whether scripts other than this one are to be run.
37 run_user_scripts_when_superuser = false
38
39
40 -- disable potentialy harmful lua functions when running superuser
41 if running_superuser then
42     local hint = "has been disabled due to running Wireshark as superuser. See http://wiki.wireshark.org/CaptureSetup/CapturePrivileges for help in running Wireshark as an unprivileged user."
43     local disabled_lib = {}
44     setmetatable(disabled_lib,{ __index = function() error("this package ".. hint) end } );
45
46     dofile = function() error("dofile " .. hint) end
47     loadfile = function() error("loadfile " .. hint) end
48     loadlib = function() error("loadlib " .. hint) end
49     require = function() error("require " .. hint) end
50     os = disabled_lib
51     io = disabled_lib
52     file = disabled_lib
53 end
54
55 -- to avoid output to stdout which can cause problems lua's print ()
56 -- has been suppresed so that it yields an error.
57 -- have print() call info() instead.
58 if gui_enabled() then
59     print = info
60 end
61
62 function typeof(obj)
63     local mt = getmetatable(obj)
64     return mt and mt.__typeof or obj.__typeof or type(obj)
65 end
66
67 -- the following function is since 1.11.3
68 function file_exists(name)
69    local f = io.open(name,"r")
70    if f ~= nil then io.close(f) return true else return false end
71 end
72
73 -- %WTAP_ENCAPS%
74
75 -- %WTAP_FILETYPES%
76
77 -- %WTAP_COMMENTTYPES%
78
79 -- %FT_TYPES%
80
81 -- the following table is since 1.11.3
82 -- %WTAP_PRESENCE_FLAGS%
83
84 -- %BASES%
85
86 -- %ENCODINGS%
87
88 -- %EXPERT%
89
90 -- the following table is since 1.11.3
91 -- %EXPERT_TABLE%
92
93 -- %MENU_GROUPS%
94
95 -- other useful constants
96 GUI_ENABLED = gui_enabled()
97 DATA_DIR = Dir.global_config_path()
98 USER_DIR = Dir.personal_config_path()
99
100 -- deprecated function names
101 datafile_path = Dir.global_config_path
102 persconffile_path = Dir.personal_config_path
103
104
105 dofile(DATA_DIR.."console.lua")
106 --dofile(DATA_DIR.."dtd_gen.lua")