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