1331281Skevans--
2331281Skevans-- SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3331281Skevans--
4331281Skevans-- Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
5331281Skevans-- All rights reserved.
6331281Skevans--
7331281Skevans-- Redistribution and use in source and binary forms, with or without
8331281Skevans-- modification, are permitted provided that the following conditions
9331281Skevans-- are met:
10331281Skevans-- 1. Redistributions of source code must retain the above copyright
11331281Skevans--    notice, this list of conditions and the following disclaimer.
12331281Skevans-- 2. Redistributions in binary form must reproduce the above copyright
13331281Skevans--    notice, this list of conditions and the following disclaimer in the
14331281Skevans--    documentation and/or other materials provided with the distribution.
15331281Skevans--
16331281Skevans-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17331281Skevans-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18331281Skevans-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19331281Skevans-- ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20331281Skevans-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21331281Skevans-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22331281Skevans-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23331281Skevans-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24331281Skevans-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25331281Skevans-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26331281Skevans-- SUCH DAMAGE.
27331281Skevans--
28331281Skevans-- $FreeBSD: stable/11/stand/lua/hook.lua 344220 2019-02-17 02:39:17Z kevans $
29331281Skevans--
30331281Skevans
31331281Skevanslocal hook = {}
32331281Skevans
33331281Skevanslocal registered_hooks = {}
34331281Skevans
35331281Skevans-- Module exports
36331281Skevans-- Register a hook type; these are the names that hooks may be registered for.
37331281Skevans-- It is expected that modules will register all of their hook types upon
38331281Skevans-- initial load. Other modules may then, at any time, register a hook for these
39331281Skevans-- types.
40331281Skevans--
41331281Skevans-- Naming convention: hook types should be sensible named, preferably prefixed
42331281Skevans-- with the name of the module that registered them. They would also ideally
43331281Skevans-- describe an action that may or may not match a function name.
44331281Skevans-- e.g. config.reloaded which takes place after config has been reloaded,
45331281Skevans-- possibly from a different source.
46331281Skevansfunction hook.registerType(hooktype)
47331281Skevans	registered_hooks[hooktype] = {}
48331281Skevansend
49331281Skevans
50331281Skevansfunction hook.register(hooktype, hookfunc)
51331281Skevans	local selected_hooks = registered_hooks[hooktype]
52331281Skevans	if selected_hooks == nil then
53331281Skevans		print("Tried registering a hook for an unknown hook type: " ..
54331281Skevans		    hooktype)
55331281Skevans		return
56331281Skevans	end
57331281Skevans	selected_hooks[#selected_hooks + 1] = hookfunc
58331281Skevans	registered_hooks[hooktype] = selected_hooks
59331281Skevansend
60331281Skevans
61331281Skevans-- Takes a hooktype and runs all functions associated with that specific hook
62331281Skevans-- type in the order that they were registered in. This ordering should likely
63331281Skevans-- not be relied upon.
64331281Skevansfunction hook.runAll(hooktype)
65331281Skevans	local selected_hooks = registered_hooks[hooktype]
66331281Skevans	if selected_hooks == nil then
67331281Skevans		-- This message, and the print() above, should only be seen by
68331281Skevans		-- developers. Hook type registration should have happened at
69331281Skevans		-- module load, so if this hasn't happened then we have messed
70331281Skevans		-- up the order in which we've loaded modules and we need to
71331281Skevans		-- catch that as soon as possible.
72331281Skevans		print("Tried to run hooks for an unknown hook type: " ..
73331281Skevans		    hooktype)
74331281Skevans		return 0
75331281Skevans	end
76331281Skevans	if #selected_hooks > 0 then
77331281Skevans		for _, func in ipairs(selected_hooks) do
78331281Skevans			func()
79331281Skevans		end
80331281Skevans	end
81331281Skevans	return #selected_hooks
82331281Skevansend
83331281Skevans
84331281Skevansreturn hook
85