lualoader: Add ability to intercept cli commands

If we failed to execute the input line as pure lua, run the command through
parse for consistent argument parsing. Pass the parsed arguments through to
a global "cli_execute" written in Lua, which is expected to either handle it
or pass it back through to interp_builtin_cmd (via loader.command).

lua-handled cli commands will then exist as globals in whatever module they
most belong in, and invocations at the loader prompt will magically dispatch
to them if they exist.

Reviewed by:	imp
Differential Revision:	https://reviews.freebsd.org/D14450
This commit is contained in:
Kyle Evans 2018-02-20 17:46:50 +00:00
parent a83546e591
commit afad05b233
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=329644
2 changed files with 47 additions and 8 deletions

View File

@ -128,7 +128,7 @@ interp_init(void)
int
interp_run(const char *line)
{
int argc;
int argc, nargc;
char **argv;
lua_State *luap;
struct interp_lua_softc *softc = &lua_softc;
@ -137,19 +137,39 @@ interp_run(const char *line)
luap = softc->luap;
LDBG("executing line...");
if ((status = luaL_dostring(luap, line)) != 0) {
/*
* If we could not parse the line as Lua syntax,
* try parsing it as a loader command.
*/
lua_pop(luap, 1);
/*
* The line wasn't executable as lua; run it through parse to
* to get consistent parsing of command line arguments, then
* run it through cli_execute. If that fails, then we'll try it
* as a builtin.
*/
if (parse(&argc, &argv, line) == 0) {
status = interp_builtin_cmd(argc, argv);
if (status != CMD_OK)
lua_getglobal(luap, "cli_execute");
for (nargc = 0; nargc < argc; ++nargc) {
lua_pushstring(luap, argv[nargc]);
}
status = lua_pcall(luap, argc, 1, 0);
lua_pop(luap, 1);
if (status != 0) {
/*
* Lua cli_execute will pass the function back
* through loader.command, which is a proxy to
* interp_builtin_cmd. If we failed to interpret
* the command, though, then there's a chance
* that didn't happen. Call interp_builtin_cmd
* directly if our lua_pcall was not successful.
*/
status = interp_builtin_cmd(argc, argv);
}
if (status != 0) {
printf("Command failed\n");
status = CMD_ERROR;
}
free(argv);
} else {
printf("Failed to parse \'%s\'\n", line);
status = -1;
status = CMD_ERROR;
}
}

View File

@ -1,5 +1,6 @@
--
-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
-- Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
-- All rights reserved.
--
-- Redistribution and use in source and binary forms, with or without
@ -30,6 +31,24 @@ config = require("config");
menu = require("menu");
password = require("password");
-- Declares a global function cli_execute that attempts to dispatch the
-- arguments passed as a lua function. This gives lua a chance to intercept
-- builtin CLI commands like "boot"
function cli_execute(...)
local cmd_name, cmd_args = ...;
local cmd = _G[cmd_name];
if (cmd ~= nil) and (type(cmd) == "function") then
-- Pass argv wholesale into cmd. We could omit argv[0] since the
-- traditional reasons for including it don't necessarily apply,
-- it may not be totally redundant if we want to have one global
-- handling multiple commands
cmd(...);
else
loader.command(...);
end
end
config.load();
password.check();
menu.run();