xref: /freebsd/stand/lua/loader.lua (revision 48230b85ac16ea26f3c8883bf6d72884392c8a3c)
1088b4f5fSWarner Losh--
2088b4f5fSWarner Losh-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
3afad05b2SKyle Evans-- Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
4088b4f5fSWarner Losh-- All rights reserved.
5088b4f5fSWarner Losh--
6088b4f5fSWarner Losh-- Redistribution and use in source and binary forms, with or without
7088b4f5fSWarner Losh-- modification, are permitted provided that the following conditions
8088b4f5fSWarner Losh-- are met:
9088b4f5fSWarner Losh-- 1. Redistributions of source code must retain the above copyright
10088b4f5fSWarner Losh--    notice, this list of conditions and the following disclaimer.
11088b4f5fSWarner Losh-- 2. Redistributions in binary form must reproduce the above copyright
12088b4f5fSWarner Losh--    notice, this list of conditions and the following disclaimer in the
13088b4f5fSWarner Losh--    documentation and/or other materials provided with the distribution.
14088b4f5fSWarner Losh--
15088b4f5fSWarner Losh-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16088b4f5fSWarner Losh-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17088b4f5fSWarner Losh-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18088b4f5fSWarner Losh-- ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19088b4f5fSWarner Losh-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20088b4f5fSWarner Losh-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21088b4f5fSWarner Losh-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22088b4f5fSWarner Losh-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23088b4f5fSWarner Losh-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24088b4f5fSWarner Losh-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25088b4f5fSWarner Losh-- SUCH DAMAGE.
26088b4f5fSWarner Losh--
27088b4f5fSWarner Losh-- $FreeBSD$
28088b4f5fSWarner Losh--
29088b4f5fSWarner Losh
30aedd6be5SKyle Evanslocal config = require("config")
31aedd6be5SKyle Evanslocal menu = require("menu")
32aedd6be5SKyle Evanslocal password = require("password")
33*48230b85SKyle Evanslocal local_module
34*48230b85SKyle Evans
35*48230b85SKyle Evanslocal result, errstr, errnoval = lfs.attributes("/boot/lua/local.lua")
36*48230b85SKyle Evans-- Effectively discard any errors; we'll just act if it succeeds.
37*48230b85SKyle Evansif result ~= nil then
38*48230b85SKyle Evans	local_module = require("local")
39*48230b85SKyle Evansend
40088b4f5fSWarner Losh
41afad05b2SKyle Evans-- Declares a global function cli_execute that attempts to dispatch the
42afad05b2SKyle Evans-- arguments passed as a lua function. This gives lua a chance to intercept
43afad05b2SKyle Evans-- builtin CLI commands like "boot"
44afad05b2SKyle Evansfunction cli_execute(...)
45aedd6be5SKyle Evans	local argv = {...}
4662daefa5SKyle Evans	-- Just in case...
479f71d421SKyle Evans	if #argv == 0 then
48aedd6be5SKyle Evans		loader.command(...)
49aedd6be5SKyle Evans		return
5062daefa5SKyle Evans	end
5162daefa5SKyle Evans
52aedd6be5SKyle Evans	local cmd_name = argv[1]
53aedd6be5SKyle Evans	local cmd = _G[cmd_name]
549f71d421SKyle Evans	if cmd ~= nil and type(cmd) == "function" then
55afad05b2SKyle Evans		-- Pass argv wholesale into cmd. We could omit argv[0] since the
56afad05b2SKyle Evans		-- traditional reasons for including it don't necessarily apply,
57afad05b2SKyle Evans		-- it may not be totally redundant if we want to have one global
58afad05b2SKyle Evans		-- handling multiple commands
59aedd6be5SKyle Evans		cmd(...)
60afad05b2SKyle Evans	else
61aedd6be5SKyle Evans		loader.command(...)
62afad05b2SKyle Evans	end
63afad05b2SKyle Evans
64afad05b2SKyle Evansend
65afad05b2SKyle Evans
66aedd6be5SKyle Evansconfig.load()
67aedd6be5SKyle Evanspassword.check()
68aedd6be5SKyle Evansmenu.run()
69