2 -- SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 -- Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
5 -- All rights reserved.
7 -- Redistribution and use in source and binary forms, with or without
8 -- modification, are permitted provided that the following conditions
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.
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
31 local config = require("config")
32 local core = require("core")
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
41 local function parseBootArgs(argv, with_kernel)
42 if with_kernel == nil then
55 for _, v in ipairs(argv) do
56 if with_kernel and v:sub(1,1) ~= "-" then
59 argstr = argstr .. " " .. v
63 return kernel_name, argstr
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"
72 -- This function intentionally does not follow our general naming guideline for
73 -- functions. This is global pollution, but the clearly separated 'cli' looks
74 -- more like a module indicator to serve as a hint of where to look for the
75 -- corresponding definition.
76 function cli_execute(...)
80 return loader.command(...)
83 local cmd_name = argv[1]
84 local cmd = cli[cmd_name]
85 if cmd ~= nil and type(cmd) == "function" then
86 -- Pass argv wholesale into cmd. We could omit argv[0] since the
87 -- traditional reasons for including it don't necessarily apply,
88 -- it may not be totally redundant if we want to have one global
89 -- handling multiple commands
92 return loader.command(...)
97 function cli_execute_unparsed(str)
98 return cli_execute(loader.parse(str))
103 function cli.boot(...)
104 local _, argv = cli.arguments(...)
105 local kernel, argstr = parseBootArgs(argv)
106 if kernel ~= nil then
107 loader.perform("unload")
108 config.selectKernel(kernel)
113 function cli.autoboot(...)
114 local _, argv = cli.arguments(...)
115 local argstr = parseBootArgs(argv, false)
116 core.autoboot(argstr)
119 -- Used for splitting cli varargs into cmd_name and the rest of argv
120 function cli.arguments(...)
123 cmd_name, argv = core.popFrontTable(argv)
124 return cmd_name, argv