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 -- shim for the pager module that just doesn't do it.
37 -- XXX Remove after 12.2 goes EoL.
39 open = function() end,
40 close = function() end,
41 output = function(str)
48 -- Parses arguments to boot and returns two values: kernel_name, argstr
49 -- Defaults to nil and "" respectively.
50 -- This will also parse arguments to autoboot, but the with_kernel argument
51 -- will need to be explicitly overwritten to false
52 local function parseBootArgs(argv, with_kernel)
53 if with_kernel == nil then
66 for _, v in ipairs(argv) do
67 if with_kernel and v:sub(1,1) ~= "-" then
70 argstr = argstr .. " " .. v
74 return kernel_name, argstr
80 local function setModule(module, loading)
81 if loading and config.enableModule(module) then
82 print(module .. " will be loaded")
83 elseif not loading and config.disableModule(module) then
84 print(module .. " will not be loaded")
88 -- Declares a global function cli_execute that attempts to dispatch the
89 -- arguments passed as a lua function. This gives lua a chance to intercept
90 -- builtin CLI commands like "boot"
91 -- This function intentionally does not follow our general naming guideline for
92 -- functions. This is global pollution, but the clearly separated 'cli' looks
93 -- more like a module indicator to serve as a hint of where to look for the
94 -- corresponding definition.
95 function cli_execute(...)
99 return loader.command(...)
102 local cmd_name = argv[1]
103 local cmd = cli[cmd_name]
104 if cmd ~= nil and type(cmd) == "function" then
105 -- Pass argv wholesale into cmd. We could omit argv[0] since the
106 -- traditional reasons for including it don't necessarily apply,
107 -- it may not be totally redundant if we want to have one global
108 -- handling multiple commands
111 return loader.command(...)
116 function cli_execute_unparsed(str)
117 return cli_execute(loader.parse(str))
122 function cli.boot(...)
123 local _, argv = cli.arguments(...)
124 local kernel, argstr = parseBootArgs(argv)
125 if kernel ~= nil then
126 loader.perform("unload")
127 config.selectKernel(kernel)
132 function cli.autoboot(...)
133 local _, argv = cli.arguments(...)
134 local argstr = parseBootArgs(argv, false)
135 core.autoboot(argstr)
138 cli['boot-conf'] = function(...)
139 local _, argv = cli.arguments(...)
140 local kernel, argstr = parseBootArgs(argv)
141 if kernel ~= nil then
142 loader.perform("unload")
143 config.selectKernel(kernel)
145 core.autoboot(argstr)
148 cli['read-conf'] = function(...)
149 local _, argv = cli.arguments(...)
150 config.readConf(assert(core.popFrontTable(argv)))
153 cli['reload-conf'] = function()
157 cli["enable-module"] = function(...)
158 local _, argv = cli.arguments(...)
160 print("usage error: enable-module module")
164 setModule(argv[1], true)
167 cli["disable-module"] = function(...)
168 local _, argv = cli.arguments(...)
170 print("usage error: disable-module module")
174 setModule(argv[1], false)
177 cli["toggle-module"] = function(...)
178 local _, argv = cli.arguments(...)
180 print("usage error: toggle-module module")
184 local module = argv[1]
185 setModule(module, not config.isModuleEnabled(module))
188 cli["show-module-options"] = function()
189 local module_info = config.getModuleInfo()
190 local modules = module_info['modules']
191 local blacklist = module_info['blacklist']
194 for module, info in pairs(modules) do
196 lines[#lines + 1] = ""
199 lines[#lines + 1] = "Name: " .. module
201 lines[#lines + 1] = "Path: " .. info.name
205 lines[#lines + 1] = "Type: " .. info.type
209 lines[#lines + 1] = "Flags: " .. info.flags
213 lines[#lines + 1] = "Before load: " .. info.before
217 lines[#lines + 1] = "After load: " .. info.after
221 lines[#lines + 1] = "Error: " .. info.error
225 if blacklist[module] and not info.force then
226 status = "Blacklisted"
227 elseif info.load == "YES" then
230 status = "Don't load"
233 lines[#lines + 1] = "Status: " .. status
237 for i, v in ipairs(lines) do
238 pager.output(v .. "\n")
243 -- Used for splitting cli varargs into cmd_name and the rest of argv
244 function cli.arguments(...)
247 cmd_name, argv = core.popFrontTable(argv)
248 return cmd_name, argv