Allow wtap_read() and wtap_seek_read() to return records other than packets.
[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 checks if a file exists
68 -- since 1.11.3
69 function file_exists(name)
70    local f = io.open(name,"r")
71    if f ~= nil then io.close(f) return true else return false end
72 end
73
74 -- the following function prepends the given directory name to
75 -- the package.path, so that a 'require "foo"' will work if 'foo'
76 -- is in the directory name given to this function. For example,
77 -- if your Lua file will do a 'require "foo"' and the foo.lua
78 -- file is in a local directory (local to your script) named 'bar',
79 -- then call this function before doing your 'require', by doing
80 --     package.prepend_path("bar")
81 -- and that will let Wireshark's Lua find the file "bar/foo.lua"
82 -- when you later do 'require "foo"'
83 --
84 -- Because this function resides here in init.lua, it does not
85 -- have the same environment as your script, so it has to get it
86 -- using the debug library, which is why the code appears so
87 -- cumbersome.
88 --
89 -- since 1.11.3
90 function package.prepend_path(name)
91     local debug = require "debug"
92     -- get the function calling this package.prepend_path function
93     local dt = debug.getinfo(2, "f")
94     if not dt then
95         error("could not retrieve debug info table")
96     end
97     -- get its upvalue
98     local _, val = debug.getupvalue(dt.func, 1)
99     if not val or type(val) ~= 'table' then
100         error("No calling function upvalue or it is not a table")
101     end
102     -- get the __DIR__ field in its upvalue table
103     local dir = val["__DIR__"]
104     -- get the platform-specific directory separator character
105     local sep = package.config:sub(1,1)
106     -- prepend the dir and given name to path
107     if dir and dir:len() > 0 then
108         package.path = dir .. sep .. name .. sep .. "?.lua;" .. package.path
109     end
110     -- also prepend just the name as a directory
111     package.path = name .. sep .. "?.lua;" .. package.path
112 end
113
114 -- %WTAP_ENCAPS%
115
116 -- %WTAP_FILETYPES%
117
118 -- %WTAP_COMMENTTYPES%
119
120 -- %FT_TYPES%
121
122 -- the following table is since 1.12
123 -- %WTAP_REC_TYPES%
124
125 -- the following table is since 1.11.3
126 -- %WTAP_PRESENCE_FLAGS%
127
128 -- %BASES%
129
130 -- %ENCODINGS%
131
132 -- %EXPERT%
133
134 -- the following table is since 1.11.3
135 -- %EXPERT_TABLE%
136
137 -- %MENU_GROUPS%
138
139 -- other useful constants
140 GUI_ENABLED = gui_enabled()
141 DATA_DIR = Dir.global_config_path()
142 USER_DIR = Dir.personal_config_path()
143
144 -- deprecated function names
145 datafile_path = Dir.global_config_path
146 persconffile_path = Dir.personal_config_path
147
148
149 dofile(DATA_DIR.."console.lua")
150 --dofile(DATA_DIR.."dtd_gen.lua")