xref: /freebsd/stand/lua/cli.lua (revision 07c17b2b00d8c1c8a2d58d4d8f99e64ec1182476)
1--
2-- SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3--
4-- Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
5-- All rights reserved.
6--
7-- Redistribution and use in source and binary forms, with or without
8-- modification, are permitted provided that the following conditions
9-- are met:
10-- 1. Redistributions of source code must retain the above copyright
11--    notice, this list of conditions and the following disclaimer.
12-- 2. Redistributions in binary form must reproduce the above copyright
13--    notice, this list of conditions and the following disclaimer in the
14--    documentation and/or other materials provided with the distribution.
15--
16-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19-- ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26-- SUCH DAMAGE.
27--
28-- $FreeBSD$
29--
30
31local config = require("config")
32local core = require("core")
33
34local cli = {}
35
36-- Internal function
37-- Parses arguments to boot and returns two values: kernel_name, argstr
38-- Defaults to nil and "" respectively.
39-- This will also parse arguments to autoboot, but the with_kernel argument
40-- will need to be explicitly overwritten to false
41local function parse_boot_args(argv, with_kernel)
42	if with_kernel == nil then
43		with_kernel = true
44	end
45	if #argv == 0 then
46		if with_kernel then
47			return nil, ""
48		else
49			return ""
50		end
51	end
52	local kernel_name
53	local argstr = ""
54
55	for _, v in ipairs(argv) do
56		if with_kernel and v:sub(1,1) ~= "-" then
57			kernel_name = v
58		else
59			argstr = argstr .. " " .. v
60		end
61	end
62	if with_kernel then
63		return kernel_name, argstr
64	else
65		return argstr
66	end
67end
68
69-- Declares a global function cli_execute that attempts to dispatch the
70-- arguments passed as a lua function. This gives lua a chance to intercept
71-- builtin CLI commands like "boot"
72function cli_execute(...)
73	local argv = {...}
74	-- Just in case...
75	if #argv == 0 then
76		loader.command(...)
77		return
78	end
79
80	local cmd_name = argv[1]
81	local cmd = cli[cmd_name]
82	if cmd ~= nil and type(cmd) == "function" then
83		-- Pass argv wholesale into cmd. We could omit argv[0] since the
84		-- traditional reasons for including it don't necessarily apply,
85		-- it may not be totally redundant if we want to have one global
86		-- handling multiple commands
87		cmd(...)
88	else
89		loader.command(...)
90	end
91
92end
93
94-- Module exports
95
96function cli.boot(...)
97	local _, argv = cli.arguments(...)
98	local kernel, argstr = parse_boot_args(argv)
99	if kernel ~= nil then
100		loader.perform("unload")
101		config.selectkernel(kernel)
102	end
103	core.boot(argstr)
104end
105
106function cli.autoboot(...)
107	local _, argv = cli.arguments(...)
108	local argstr = parse_boot_args(argv, false)
109	core.autoboot(argstr)
110end
111
112-- Used for splitting cli varargs into cmd_name and the rest of argv
113function cli.arguments(...)
114	local argv = {...}
115	local cmd_name
116	cmd_name, argv = core.popFrontTable(argv)
117	return cmd_name, argv
118end
119
120return cli
121