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