xref: /freebsd-13.1/stand/lua/hook.lua (revision 4b3c64f7)
1*aea262bfSKyle Evans--
2*aea262bfSKyle Evans-- SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*aea262bfSKyle Evans--
4*aea262bfSKyle Evans-- Copyright (c) 2018 Kyle Evans <[email protected]>
5*aea262bfSKyle Evans--
6*aea262bfSKyle Evans-- Redistribution and use in source and binary forms, with or without
7*aea262bfSKyle Evans-- modification, are permitted provided that the following conditions
8*aea262bfSKyle Evans-- are met:
9*aea262bfSKyle Evans-- 1. Redistributions of source code must retain the above copyright
10*aea262bfSKyle Evans--    notice, this list of conditions and the following disclaimer.
11*aea262bfSKyle Evans-- 2. Redistributions in binary form must reproduce the above copyright
12*aea262bfSKyle Evans--    notice, this list of conditions and the following disclaimer in the
13*aea262bfSKyle Evans--    documentation and/or other materials provided with the distribution.
14*aea262bfSKyle Evans--
15*aea262bfSKyle Evans-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*aea262bfSKyle Evans-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*aea262bfSKyle Evans-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*aea262bfSKyle Evans-- ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*aea262bfSKyle Evans-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*aea262bfSKyle Evans-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*aea262bfSKyle Evans-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*aea262bfSKyle Evans-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*aea262bfSKyle Evans-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*aea262bfSKyle Evans-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*aea262bfSKyle Evans-- SUCH DAMAGE.
26*aea262bfSKyle Evans--
27*aea262bfSKyle Evans-- $FreeBSD$
28*aea262bfSKyle Evans--
29*aea262bfSKyle Evans
30*aea262bfSKyle Evanslocal hook = {}
31*aea262bfSKyle Evans
32*aea262bfSKyle Evanslocal registered_hooks = {}
33*aea262bfSKyle Evans
34*aea262bfSKyle Evans-- Module exports
35*aea262bfSKyle Evans-- Register a hook type; these are the names that hooks may be registered for.
36*aea262bfSKyle Evans-- It is expected that modules will register all of their hook types upon
37*aea262bfSKyle Evans-- initial load. Other modules may then, at any time, register a hook for these
38*aea262bfSKyle Evans-- types.
39*aea262bfSKyle Evans--
40*aea262bfSKyle Evans-- Naming convention: hook types should be sensible named, preferably prefixed
41*aea262bfSKyle Evans-- with the name of the module that registered them. They would also ideally
42*aea262bfSKyle Evans-- describe an action that may or may not match a function name.
43*aea262bfSKyle Evans-- e.g. config.reloaded which takes place after config has been reloaded,
44*aea262bfSKyle Evans-- possibly from a different source.
45*aea262bfSKyle Evansfunction hook.registerType(hooktype)
46*aea262bfSKyle Evans	registered_hooks[hooktype] = {}
47*aea262bfSKyle Evansend
48*aea262bfSKyle Evans
49*aea262bfSKyle Evansfunction hook.register(hooktype, hookfunc)
50*aea262bfSKyle Evans	local selected_hooks = registered_hooks[hooktype]
51*aea262bfSKyle Evans	if selected_hooks == nil then
52*aea262bfSKyle Evans		print("Tried registering a hook for an unknown hook type: " ..
53*aea262bfSKyle Evans		    hooktype)
54*aea262bfSKyle Evans		return
55*aea262bfSKyle Evans	end
56*aea262bfSKyle Evans	selected_hooks[#selected_hooks + 1] = hookfunc
57*aea262bfSKyle Evans	registered_hooks[hooktype] = selected_hooks
58*aea262bfSKyle Evansend
59*aea262bfSKyle Evans
60*aea262bfSKyle Evans-- Takes a hooktype and runs all functions associated with that specific hook
61*aea262bfSKyle Evans-- type in the order that they were registered in. This ordering should likely
62*aea262bfSKyle Evans-- not be relied upon.
63*aea262bfSKyle Evansfunction hook.runAll(hooktype)
64*aea262bfSKyle Evans	local selected_hooks = registered_hooks[hooktype]
65*aea262bfSKyle Evans	if selected_hooks == nil then
66*aea262bfSKyle Evans		-- This message, and the print() above, should only be seen by
67*aea262bfSKyle Evans		-- developers. Hook type registration should have happened at
68*aea262bfSKyle Evans		-- module load, so if this hasn't happened then we have messed
69*aea262bfSKyle Evans		-- up the order in which we've loaded modules and we need to
70*aea262bfSKyle Evans		-- catch that as soon as possible.
71*aea262bfSKyle Evans		print("Tried to run hooks for an unknown hook type: " ..
72*aea262bfSKyle Evans		    hooktype)
73*aea262bfSKyle Evans		return 0
74*aea262bfSKyle Evans	end
75*aea262bfSKyle Evans	if #selected_hooks > 0 then
76*aea262bfSKyle Evans		for _, func in ipairs(selected_hooks) do
77*aea262bfSKyle Evans			func()
78*aea262bfSKyle Evans		end
79*aea262bfSKyle Evans	end
80*aea262bfSKyle Evans	return #selected_hooks
81*aea262bfSKyle Evansend
82*aea262bfSKyle Evans
83*aea262bfSKyle Evansreturn hook
84