xref: /freebsd-13-stable/stand/lua/hook.lua.8 (revision b144e70a3325e033163aa4e6e15d0446e245702d)
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 June 9, 2018
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 .
40.Pp
41Before using the functionality provided by
42.Nm ,
43it must be included with a statement such as the following:
44.Pp
45.Dl local hook = require("hook")
46.Ss Exported functions
47The following functions are exported from
48.Nm :
49.Bl -tag -width hook.registerType -offset indent
50.It Fn hook.registerType hooktype
51Adds
52.Ev hooktype
53as a recognized hook type.
54This allows functions to be added to run when hooks of this type are invoked
55using
56.Fn hook.runAll hooktype .
57.It Fn hook.register hooktype hookfunc
58Register
59.Ev hookfunc
60to be run when hooks of type
61.Ev hooktype
62are invoked.
63.It Fn hook.runAll hooktype
64Invoke all hooks registered for type
65.Ev hooktype .
66Hooks are invoked in the order in which they are registered.
67.El
68.Ss Hook Naming Guidelines
69Hook names should consist of the name of the module they are defined in, as well
70as a verb describing when the hook is executed, separated by a period.
71For example,
72.Dq config.reloaded
73is defined in the
74.Xr config.lua 8
75module and run when the configuration is reloaded.
76.Sh EXAMPLES
77To register a hook to be run when configuration is reloaded:
78.Pp
79.Bd -literal -offset indent -compact
80local hook = require("hook")
81
82local function configuration_was_reloaded()
83	print("Configuration was reloaded!")
84end
85
86hook.register("config.reloaded", configuration_was_reloaded)
87.Ed
88.Sh AUTHORS
89The
90.Nm
91file was originally written by
92.An Kyle Evans Aq Mt kevans@FreeBSD.org .
93