2 -- SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 -- Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
6 -- Redistribution and use in source and binary forms, with or without
7 -- modification, are permitted provided that the following conditions
9 -- 1. Redistributions of source code must retain the above copyright
10 -- notice, this list of conditions and the following disclaimer.
11 -- 2. Redistributions in binary form must reproduce the above copyright
12 -- notice, this list of conditions and the following disclaimer in the
13 -- documentation and/or other materials provided with the distribution.
15 -- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 -- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 -- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 -- ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 -- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 -- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 -- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 -- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 -- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 -- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 local config = require("config")
31 local core = require("core")
36 -- Parses arguments to boot and returns two values: kernel_name, argstr
37 -- Defaults to nil and "" respectively.
38 -- This will also parse arguments to autoboot, but the with_kernel argument
39 -- will need to be explicitly overwritten to false
40 local function parseBootArgs(argv, with_kernel)
41 if with_kernel == nil then
54 for _, v in ipairs(argv) do
55 if with_kernel and v:sub(1,1) ~= "-" then
58 argstr = argstr .. " " .. v
62 return kernel_name, argstr
68 -- Declares a global function cli_execute that attempts to dispatch the
69 -- arguments passed as a lua function. This gives lua a chance to intercept
70 -- builtin CLI commands like "boot"
71 -- This function intentionally does not follow our general naming guideline for
72 -- functions. This is global pollution, but the clearly separated 'cli' looks
73 -- more like a module indicator to serve as a hint of where to look for the
74 -- corresponding definition.
75 function cli_execute(...)
79 return loader.command(...)
82 local cmd_name = argv[1]
83 local cmd = cli[cmd_name]
84 if cmd ~= nil and type(cmd) == "function" then
85 -- Pass argv wholesale into cmd. We could omit argv[0] since the
86 -- traditional reasons for including it don't necessarily apply,
87 -- it may not be totally redundant if we want to have one global
88 -- handling multiple commands
91 return loader.command(...)
96 function cli_execute_unparsed(str)
97 return cli_execute(loader.parse(str))
102 function cli.boot(...)
103 local _, argv = cli.arguments(...)
104 local kernel, argstr = parseBootArgs(argv)
105 if kernel ~= nil then
106 loader.perform("unload")
107 config.selectKernel(kernel)
112 function cli.autoboot(...)
113 local _, argv = cli.arguments(...)
114 local argstr = parseBootArgs(argv, false)
115 core.autoboot(argstr)
118 cli['boot-conf'] = function(...)
119 local _, argv = cli.arguments(...)
120 local kernel, argstr = parseBootArgs(argv)
121 if kernel ~= nil then
122 loader.perform("unload")
123 config.selectKernel(kernel)
125 core.autoboot(argstr)
128 cli['read-conf'] = function(...)
129 local _, argv = cli.arguments(...)
130 config.readConf(assert(core.popFrontTable(argv)))
133 cli['reload-conf'] = function(...)
137 -- Used for splitting cli varargs into cmd_name and the rest of argv
138 function cli.arguments(...)
141 cmd_name, argv = core.popFrontTable(argv)
142 return cmd_name, argv