xref: /freebsd/stand/lua/hook.lua.8 (revision 2008043f386721d58158e37e0d7e50df8095942d)
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.Dd April 28, 2020
28.Dt HOOK.LUA 8
29.Os
30.Sh NAME
31.Nm hook.lua
32.Nd FreeBSD hook module
33.Sh DESCRIPTION
34.Nm
35contains functionality for defining hook types and attaching hooks.
36Hooks are functions used to attach custom behaviors at pre-defined points in
37loader execution.
38These pre-defined points are what we refer to as
39.Dq hook types .
40Hooks may also take an optional data parameter, which may or may not be
41populated by the caller.
42.Pp
43Before using the functionality provided by
44.Nm ,
45it must be included with a statement such as the following:
46.Pp
47.Dl local hook = require("hook")
48.Ss Exported functions
49The following functions are exported from
50.Nm :
51.Bl -tag -width hook.registerType -offset indent
52.It Fn hook.registerType hooktype
53Adds
54.Ev hooktype
55as a recognized hook type.
56This allows functions to be added to run when hooks of this type are invoked
57using
58.Fn hook.runAll hooktype .
59.It Fn hook.register hooktype hookfunc
60Register
61.Ev hookfunc
62to be run when hooks of type
63.Ev hooktype
64are invoked.
65.It Fn hook.runAll hooktype
66Invoke all hooks registered for type
67.Ev hooktype .
68Hooks are invoked in the order in which they are registered.
69.El
70.Ss Hook Naming Guidelines
71Hook names should consist of the name of the module they are defined in, as well
72as a verb describing when the hook is executed, separated by a period.
73For example,
74.Dq config.reloaded
75is defined in the
76.Xr config.lua 8
77module and run when the configuration is reloaded.
78.Sh EXAMPLES
79To register a hook to be run when configuration is reloaded:
80.Pp
81.Bd -literal -offset indent -compact
82local hook = require("hook")
83
84local function configuration_was_reloaded()
85	print("Configuration was reloaded!")
86end
87
88hook.register("config.reloaded", configuration_was_reloaded)
89.Ed
90.Sh AUTHORS
91The
92.Nm
93file was originally written by
94.An Kyle Evans Aq Mt kevans@FreeBSD.org .
95