stand: separate the command lookup from the command execution

Factor out interp_lookup_cmd to search for a command from
interp_builtin_cmd. This simplifies the latter and can be used to expand
lua to ask if a command exists.

Sponsored by:		Netflix
Differential Revision:	https://reviews.freebsd.org/D36363
This commit is contained in:
Warner Losh 2022-09-01 11:05:42 -06:00
parent 991aef9795
commit 113dfadd5c

View File

@ -152,6 +152,19 @@ interp_emit_prompt(void)
free(pr); free(pr);
} }
static struct bootblk_command *
interp_lookup_cmd(const char *cmd)
{
struct bootblk_command **cmdp;
/* search the command set for the command */
SET_FOREACH(cmdp, Xcommand_set) {
if (((*cmdp)->c_name != NULL) && !strcmp(cmd, (*cmdp)->c_name))
return (*cmdp);
}
return (NULL);
}
/* /*
* Perform a builtin command * Perform a builtin command
*/ */
@ -159,27 +172,21 @@ int
interp_builtin_cmd(int argc, char *argv[]) interp_builtin_cmd(int argc, char *argv[])
{ {
int result; int result;
struct bootblk_command **cmdp; struct bootblk_command *cmd;
bootblk_cmd_t *cmd;
if (argc < 1) if (argc < 1)
return(CMD_OK); return (CMD_OK);
/* set return defaults; a successful command will override these */ /* set return defaults; a successful command will override these */
command_errmsg = command_errbuf; command_errmsg = command_errbuf;
strcpy(command_errbuf, "no error message"); strcpy(command_errbuf, "no error message");
cmd = NULL;
result = CMD_ERROR; result = CMD_ERROR;
/* search the command set for the command */ cmd = interp_lookup_cmd(argv[0]);
SET_FOREACH(cmdp, Xcommand_set) { if (cmd != NULL && cmd->c_fn) {
if (((*cmdp)->c_name != NULL) && !strcmp(argv[0], (*cmdp)->c_name)) result = cmd->c_fn(argc, argv);
cmd = (*cmdp)->c_fn;
}
if (cmd != NULL) {
result = (cmd)(argc, argv);
} else { } else {
command_errmsg = "unknown command"; command_errmsg = "unknown command";
} }
return(result); return (result);
} }