xref: /freebsd/stand/lua/menu.lua.8 (revision 229381deb270e8e240d20b572c63234a61cbd977)
1f520b4afSKyle Evans.\"
24d846d26SWarner Losh.\" SPDX-License-Identifier: BSD-2-Clause
3f520b4afSKyle Evans.\"
4f520b4afSKyle Evans.\" Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
5f520b4afSKyle Evans.\"
6f520b4afSKyle Evans.\" Redistribution and use in source and binary forms, with or without
7f520b4afSKyle Evans.\" modification, are permitted provided that the following conditions
8f520b4afSKyle Evans.\" are met:
9f520b4afSKyle Evans.\" 1. Redistributions of source code must retain the above copyright
10f520b4afSKyle Evans.\"    notice, this list of conditions and the following disclaimer.
11f520b4afSKyle Evans.\" 2. Redistributions in binary form must reproduce the above copyright
12f520b4afSKyle Evans.\"    notice, this list of conditions and the following disclaimer in the
13f520b4afSKyle Evans.\"    documentation and/or other materials provided with the distribution.
14f520b4afSKyle Evans.\"
15f520b4afSKyle Evans.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16f520b4afSKyle Evans.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17f520b4afSKyle Evans.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18f520b4afSKyle Evans.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19f520b4afSKyle Evans.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20f520b4afSKyle Evans.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21f520b4afSKyle Evans.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22f520b4afSKyle Evans.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23f520b4afSKyle Evans.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24f520b4afSKyle Evans.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25f520b4afSKyle Evans.\" SUCH DAMAGE.
26f520b4afSKyle Evans.\"
27e7ccd5b4SWarner Losh.Dd March 31, 2021
28f520b4afSKyle Evans.Dt MENU.LUA 8
29f520b4afSKyle Evans.Os
30f520b4afSKyle Evans.Sh NAME
31f520b4afSKyle Evans.Nm menu.lua
32f520b4afSKyle Evans.Nd FreeBSD dynamic menu boot module
33f520b4afSKyle Evans.Sh DESCRIPTION
34f520b4afSKyle Evans.Nm
35f520b4afSKyle Evanscontains the main functionality required to build a dynamic menu system.
36f520b4afSKyle EvansIt also contains definitions for the built-in menus, some of which are
37f520b4afSKyle Evansinfluenced by
38f520b4afSKyle Evans.Xr loader 8
39f520b4afSKyle Evansenvironment variables.
40f520b4afSKyle Evans.Pp
41f520b4afSKyle EvansBefore hooking into the functionality provided by
42f520b4afSKyle Evans.Nm ,
43f520b4afSKyle Evansit must be included with a statement such as the following:
44f520b4afSKyle Evans.Pp
45f520b4afSKyle Evans.Dl local menu = require("menu")
46f520b4afSKyle Evans.Ss MENU DEFINITIONS
47f520b4afSKyle EvansMenus are represented in
48f520b4afSKyle Evans.Nm
49f520b4afSKyle Evansas a table.
50f520b4afSKyle EvansThat table
51f520b4afSKyle Evans.Sy must
52f520b4afSKyle Evanscontain an
53f520b4afSKyle Evans.Va entries
54f520b4afSKyle Evanskey.
55f520b4afSKyle Evans.Pp
56f520b4afSKyle EvansIf the value of the
57f520b4afSKyle Evans.Va entries
58f520b4afSKyle Evanskey is itself a table, then each value in this table defines a single entry in
59f520b4afSKyle Evansthis menu.
60f520b4afSKyle EvansSee
61f520b4afSKyle Evans.Sx MENU ITEM DEFINITIONS
62f520b4afSKyle Evansfor the structure of each entry.
63f520b4afSKyle Evans.Pp
64f520b4afSKyle Evans.Va entries
65f520b4afSKyle Evansmay also be a function.
66f520b4afSKyle EvansThis function must return a table, each value of which defines a single entry
67f520b4afSKyle Evansin this menu.
68f520b4afSKyle EvansSee
69f520b4afSKyle Evans.Sx MENU ITEM DEFINITIONS .
70f520b4afSKyle Evans.Ss MENU ITEM DEFINITIONS
71f520b4afSKyle EvansThe following keys may be defined for a menu item:
72f520b4afSKyle Evans.Bl -tag -width disable-module_module -offset indent
73f520b4afSKyle Evans.It Ic entry_type
74f520b4afSKyle EvansThe type of this menu entry.
75f520b4afSKyle EvansSee
76f520b4afSKyle Evans.Sx MENU ITEM TYPES .
77f520b4afSKyle Evans.It Ic carousel_id
78f520b4afSKyle EvansA unique string id for this carousel.
79f520b4afSKyle EvansA carousel is a menu entry that rotates through a selection of items.
80f520b4afSKyle EvansUsed for storage of the carousel's current setting.
81f520b4afSKyle Evans.It Ic visible
82f520b4afSKyle EvansA lambda that returns
83f520b4afSKyle Evans.Dv true
84f520b4afSKyle Evansif this menu item should be visible and
85f520b4afSKyle Evans.Dv false
86f520b4afSKyle Evansif it should not be visible.
87f520b4afSKyle Evans.It Ic items
88f520b4afSKyle EvansA table (or a lambda that returns a table) of the possible choices for this
89f520b4afSKyle Evanscarousel.
90f520b4afSKyle Evans.It Ic name
91f520b4afSKyle EvansA string (or a lambda that returns a string) containing the current name of this
92f520b4afSKyle Evansitem.
93f520b4afSKyle Evans.It Ic func
94f520b4afSKyle EvansThe function executed when this entry is selected.
95f520b4afSKyle EvansEvery type except for
96f520b4afSKyle Evans.Ic core.MENU_SEPARATOR
97f520b4afSKyle Evansmay have a
98f520b4afSKyle Evans.Ic func .
99f520b4afSKyle Evans.It Ic submenu
100f520b4afSKyle EvansThe submenu menu definition to draw when this entry is selected.
101f520b4afSKyle Evans.It Ic alias
102f520b4afSKyle EvansA table of case-sensitive aliases for this menu entry.
103f520b4afSKyle EvansAll menu entries that can be selected may have any number of
104f520b4afSKyle Evans.Ic alias
105f520b4afSKyle Evansentries.
106f520b4afSKyle Evans.El
107f520b4afSKyle Evans.Pp
108f520b4afSKyle Evans.Ic entry_type
109f520b4afSKyle Evansis the only required key for every entry type.
110f520b4afSKyle Evans.Ic name
111f520b4afSKyle Evansis required for all entry types except for
112f520b4afSKyle Evans.Ic core.MENU_SEPARATOR .
113f520b4afSKyle Evans.Ss MENU ITEM TYPES
114f520b4afSKyle EvansThe menu item type constants are defined in
115f520b4afSKyle Evans.Xr core.lua 8 .
116f520b4afSKyle EvansThe following types are available:
117f520b4afSKyle Evans.Bl -tag -width core.MENU_CAROUSEL_ENTRY -offset indent
118f520b4afSKyle Evans.It Ic core.MENU_RETURN
119f520b4afSKyle EvansReturn to the parent menu.
120f520b4afSKyle EvansIf the current menu is the default menu,
121f520b4afSKyle Evans.Nm
122f520b4afSKyle Evanswill exit the menu and begin the autoboot sequence (if applicable).
123f520b4afSKyle EvansThis type of menu entry may execute
124f520b4afSKyle Evans.Ic func ,
125f520b4afSKyle Evanswhen selected, and has a
126f520b4afSKyle Evans.Ic name .
127f520b4afSKyle Evans.It Ic core.MENU_ENTRY
128f520b4afSKyle EvansA normal menu entry that executes
129f520b4afSKyle Evans.Ic func
130f520b4afSKyle Evanswhen selected, and has a
131f520b4afSKyle Evans.Ic name .
132f520b4afSKyle Evans.It Ic core.MENU_SEPARATOR
133f520b4afSKyle EvansA menu entry that serves as a separator.
134f520b4afSKyle EvansIt may have a
135f520b4afSKyle Evans.Ic name .
136f520b4afSKyle Evans.It Ic core.MENU_SUBMENU
137f520b4afSKyle EvansA menu entry that opens
138f520b4afSKyle Evans.Ic submenu
139f520b4afSKyle Evanswhen selected.
140f520b4afSKyle EvansIt may have a
141f520b4afSKyle Evans.Ic name .
142f520b4afSKyle Evans.It Ic core.MENU_CAROUSEL_ENTRY
143f520b4afSKyle EvansA menu entry that rotates through
144f520b4afSKyle Evans.Ic items
145f520b4afSKyle Evanslike a carousel.
146f520b4afSKyle Evans.Ic func
147f520b4afSKyle Evansis executed when selected, and the callback is passed the choice index, name of
148f520b4afSKyle Evansthe current choice, and the table of choices.
149f520b4afSKyle Evans.El
150f520b4afSKyle Evans.Ss EXPORTED MENUS
151f520b4afSKyle EvansThe following menus are exported by
152f520b4afSKyle Evans.Nm :
153f520b4afSKyle Evans.Bl -tag -width menu.boot_environments -offset indent
154f520b4afSKyle Evans.It Ic menu.default
155f520b4afSKyle EvansThe default menu to draw.
156f520b4afSKyle EvansSet to
157f520b4afSKyle Evans.Ic menu.welcome
158f520b4afSKyle Evansby default.
159f520b4afSKyle Evans.It Ic menu.welcome
160f520b4afSKyle EvansThe welcome menu.
161f520b4afSKyle EvansContains single and multi user boot options, as well as entries to access other
162f520b4afSKyle Evansmenus.
163f520b4afSKyle Evans.It Ic menu.boot_options
164f520b4afSKyle EvansThe "Boot Options" menu.
165f520b4afSKyle Evans.It Ic menu.boot_environments
166f520b4afSKyle EvansThe "Boot Environments" menu.
167f520b4afSKyle EvansThis menu is only visible if the system is booted on a ZFS partition and more
168f520b4afSKyle Evansthan one boot environment was detected at boot.
169f520b4afSKyle Evans.El
170f520b4afSKyle Evans.Sh EXAMPLES
171f520b4afSKyle EvansTo replace the default boot menu with a simple boot menu:
172f520b4afSKyle Evans.Pp
173f520b4afSKyle Evans.Bd -literal -offset indent -compact
174f520b4afSKyle Evanslocal core = require("core")
175f520b4afSKyle Evanslocal menu = require("menu")
176f520b4afSKyle Evans
177f520b4afSKyle Evansmenu.default = {
178f520b4afSKyle Evans	entries = {
179f520b4afSKyle Evans		{
180f520b4afSKyle Evans			entry_type = core.MENU_ENTRY,
181f520b4afSKyle Evans			name = "Boot",
182f520b4afSKyle Evans			func = core.boot,
183f520b4afSKyle Evans		},
184f520b4afSKyle Evans		{
185f520b4afSKyle Evans			entry_type = core.MENU_CAROUSEL_ENTRY,
186f520b4afSKyle Evans			carousel_id = "unique_boot_entry_name",
187f520b4afSKyle Evans			items = {"NO", "YES"},
188f520b4afSKyle Evans			name = function(_, choice, _)
189f520b4afSKyle Evans				return "Option: " .. choice
190f520b4afSKyle Evans			end,
191f520b4afSKyle Evans			func = function(_, _, _)
192f520b4afSKyle Evans				loader.setenv("some_envvar", "some_value")
193f520b4afSKyle Evans			end,
194f520b4afSKyle Evans		},
195f520b4afSKyle Evans	},
196f520b4afSKyle Evans}
197f520b4afSKyle Evans.Ed
198f520b4afSKyle Evans.Pp
199f520b4afSKyle EvansTo add another option to the welcome menu:
200f520b4afSKyle Evans.Pp
201f520b4afSKyle Evans.Bd -literal -offset indent -compact
202f520b4afSKyle Evanslocal core = require("core")
203f520b4afSKyle Evanslocal menu = require("menu")
204f520b4afSKyle Evans
205e7ccd5b4SWarner Loshlocal my_entry = {
206e7ccd5b4SWarner Losh	entry_type = core.MENU_ENTRY,
207e7ccd5b4SWarner Losh	name = "Fancy Boot",
208e7ccd5b4SWarner Losh	func = core.boot,
209e7ccd5b4SWarner Losh},
210e7ccd5b4SWarner Losh
211e7ccd5b4SWarner Loshlocal stock_entries = menu.welcome.entries
212e7ccd5b4SWarner Loshfunction menu.welcome.entries()
213e7ccd5b4SWarner Losh	local ents = stock_entries()
214e7ccd5b4SWarner Losh	ents[#ents + 1] = my_entry
215e7ccd5b4SWarner Losh	return ents
216e7ccd5b4SWarner Loshend
217e7ccd5b4SWarner Losh.Ed
218e7ccd5b4SWarner Losh.Pp
219e7ccd5b4SWarner LoshTo create a vendor submenu or other vendor menu option,
220e7ccd5b4SWarner Loshoverride
221e7ccd5b4SWarner Losh.Ic menu.welcome.all_entires.vendor
222e7ccd5b4SWarner Loshlike so:
223e7ccd5b4SWarner Losh.Pp
224e7ccd5b4SWarner Losh.Bd -literal -offset indent -compact
225e7ccd5b4SWarner Loshlocal core = require("core")
226e7ccd5b4SWarner Loshlocal menu = require("menu")
227e7ccd5b4SWarner Losh
228e7ccd5b4SWarner Losh-- Fill in with vendor specific entries
229e7ccd5b4SWarner Loshlocal vendor_options = {
230e7ccd5b4SWarner Losh	entries = {
231e7ccd5b4SWarner Losh	...
232e7ccd5b4SWarner Losh	},
233e7ccd5b4SWarner Losh}
234e7ccd5b4SWarner Losh
235f520b4afSKyle Evanslocal welcome_entries = menu.welcome.all_entries
236e7ccd5b4SWarner Loshwelcome_entries.vendor = {
237e7ccd5b4SWarner Losh        entry_type = core.MENU_SUBMENU,
238e7ccd5b4SWarner Losh        name = color.highlight("V") .. "endor Options",
239e7ccd5b4SWarner Losh        submenu = vendor_options,
240e7ccd5b4SWarner Losh        alias = {"v", "V"},
241556e66b7SWarner Losh	visible = function()
242556e66b7SWarner Losh		return true
243556e66b7SWarner Losh	end,
244f520b4afSKyle Evans}
245f520b4afSKyle Evans.Ed
246e7ccd5b4SWarner LoshIn the above example,
247e7ccd5b4SWarner Losh.Ic vendor_options
248e7ccd5b4SWarner Loshis a local variable that defines the vendor submenu.
249e7ccd5b4SWarner Losh.Pp
250e7ccd5b4SWarner LoshTo add an additional option, change the
251e7ccd5b4SWarner Losh.Ic menu.boot_options.entries
252e7ccd5b4SWarner Losharray.
253e7ccd5b4SWarner LoshThe following illustrates this concept:
254e7ccd5b4SWarner Losh.Pp
255e7ccd5b4SWarner Losh.Bd -literal -offset indent -compact
256e7ccd5b4SWarner Losh-- This is a silly example that rotates local_option through the values
257e7ccd5b4SWarner Losh-- 0 to 4.  local_option would still need to be used elsewhere.
258e7ccd5b4SWarner Loshlocal local_option = 0
259e7ccd5b4SWarner Losh
260e7ccd5b4SWarner Losh-- The `entries` of a menu may either be a table or a function.  In this
261e7ccd5b4SWarner Losh-- example we're augmenting a menu that just has a static table, but if we
262e7ccd5b4SWarner Losh-- wanted to be more robust then we would need to instead check the type
263e7ccd5b4SWarner Losh-- of `stock_options` here to determine our next move.
264e7ccd5b4SWarner Losh--
265e7ccd5b4SWarner Losh-- If `entries` is a table, then the stock menu system won't be changing it
266e7ccd5b4SWarner Losh-- so we can just add our menu option as we do below.
267e7ccd5b4SWarner Losh--
268e7ccd5b4SWarner Losh-- If `entries` is a function, then we would need to provide a new function to
269e7ccd5b4SWarner Losh-- replace `entries` that does a core.deepCopyTable() of the result and adds
270e7ccd5b4SWarner Losh-- the below item to it.  The deep copy is necessary to avoid duplicating our
271e7ccd5b4SWarner Losh-- new menu item and allowing the menu to alter its behavior however it pleases.
272e7ccd5b4SWarner Loshlocal stock_options = menu.boot_options.entries
273e7ccd5b4SWarner Loshstock_options[#stock_options + 1] = {
274e7ccd5b4SWarner Losh	entry_type = core.MENU_ENTRY,
275e7ccd5b4SWarner Losh	name = function()
276e7ccd5b4SWarner Losh		return color.highlight('L') ..
277e7ccd5b4SWarner Losh		    "ocal Option     : " .. local_option
278e7ccd5b4SWarner Losh	end,
279e7ccd5b4SWarner Losh	func = function()
280e7ccd5b4SWarner Losh		local_option = (local_option + 1) % 5
281e7ccd5b4SWarner Losh	end,
282e7ccd5b4SWarner Losh	alias= {"l", "L"}
283e7ccd5b4SWarner Losh}
284*229381deSGraham Percival.Ed
285f520b4afSKyle Evans.Sh SEE ALSO
286f520b4afSKyle Evans.Xr loader.conf 5 ,
287f520b4afSKyle Evans.Xr core.lua 8 ,
288f520b4afSKyle Evans.Xr loader 8
289f520b4afSKyle Evans.Sh HISTORY
290f520b4afSKyle EvansThe
291f520b4afSKyle Evans.Nm
292f520b4afSKyle Evansfile first appeared in
293f520b4afSKyle Evans.Fx 12.0 .
294f520b4afSKyle Evans.Sh AUTHORS
295f520b4afSKyle EvansThe
296f520b4afSKyle Evans.Nm
297f520b4afSKyle Evansfile was originally written by
298f520b4afSKyle Evans.An Pedro Souza Aq Mt pedrosouza@FreeBSD.org .
299f520b4afSKyle EvansLater work and this manual page was done by
300f520b4afSKyle Evans.An Kyle Evans Aq Mt kevans@FreeBSD.org .
301