xref: /freebsd/stand/lua/hook.lua.8 (revision d0b2dbfa)
1.\"
2.\" SPDX-License-Identifier: BSD-2-Clause
3.\"
4.\" Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25.\" SUCH DAMAGE.
26.\"
27.\" $FreeBSD$
28.\"
29.Dd April 28, 2020
30.Dt HOOK.LUA 8
31.Os
32.Sh NAME
33.Nm hook.lua
34.Nd FreeBSD hook module
35.Sh DESCRIPTION
36.Nm
37contains functionality for defining hook types and attaching hooks.
38Hooks are functions used to attach custom behaviors at pre-defined points in
39loader execution.
40These pre-defined points are what we refer to as
41.Dq hook types .
42Hooks may also take an optional data parameter, which may or may not be
43populated by the caller.
44.Pp
45Before using the functionality provided by
46.Nm ,
47it must be included with a statement such as the following:
48.Pp
49.Dl local hook = require("hook")
50.Ss Exported functions
51The following functions are exported from
52.Nm :
53.Bl -tag -width hook.registerType -offset indent
54.It Fn hook.registerType hooktype
55Adds
56.Ev hooktype
57as a recognized hook type.
58This allows functions to be added to run when hooks of this type are invoked
59using
60.Fn hook.runAll hooktype .
61.It Fn hook.register hooktype hookfunc
62Register
63.Ev hookfunc
64to be run when hooks of type
65.Ev hooktype
66are invoked.
67.It Fn hook.runAll hooktype
68Invoke all hooks registered for type
69.Ev hooktype .
70Hooks are invoked in the order in which they are registered.
71.El
72.Ss Hook Naming Guidelines
73Hook names should consist of the name of the module they are defined in, as well
74as a verb describing when the hook is executed, separated by a period.
75For example,
76.Dq config.reloaded
77is defined in the
78.Xr config.lua 8
79module and run when the configuration is reloaded.
80.Sh EXAMPLES
81To register a hook to be run when configuration is reloaded:
82.Pp
83.Bd -literal -offset indent -compact
84local hook = require("hook")
85
86local function configuration_was_reloaded()
87	print("Configuration was reloaded!")
88end
89
90hook.register("config.reloaded", configuration_was_reloaded)
91.Ed
92.Sh AUTHORS
93The
94.Nm
95file was originally written by
96.An Kyle Evans Aq Mt kevans@FreeBSD.org .
97