git: 3cb3b855b97c - stable/13 - stand: separate the command lookup from the command execution

From: Warner Losh <imp_at_FreeBSD.org>
Date: Tue, 24 Jan 2023 22:12:17 UTC
The branch stable/13 has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=3cb3b855b97ca8095c17d102b633374b4655aaed

commit 3cb3b855b97ca8095c17d102b633374b4655aaed
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2022-09-01 17:05:42 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2023-01-24 21:49:32 +0000

    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
    
    (cherry picked from commit 113dfadd5c8c2b8a566bf4d0e969e1dff62c9e2f)
---
 stand/common/interp.c | 31 +++++++++++++++++++------------
 1 file changed, 19 insertions(+), 12 deletions(-)

diff --git a/stand/common/interp.c b/stand/common/interp.c
index de5ea55eab41..227e7ad91e29 100644
--- a/stand/common/interp.c
+++ b/stand/common/interp.c
@@ -152,6 +152,19 @@ interp_emit_prompt(void)
 	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
  */
@@ -159,27 +172,21 @@ int
 interp_builtin_cmd(int argc, char *argv[])
 {
 	int			result;
-	struct bootblk_command	**cmdp;
-	bootblk_cmd_t		*cmd;
+	struct bootblk_command	*cmd;
 
 	if (argc < 1)
-		return(CMD_OK);
+		return (CMD_OK);
 
 	/* set return defaults; a successful command will override these */
 	command_errmsg = command_errbuf;
 	strcpy(command_errbuf, "no error message");
-	cmd = NULL;
 	result = CMD_ERROR;
 
-	/* search the command set for the command */
-	SET_FOREACH(cmdp, Xcommand_set) {
-		if (((*cmdp)->c_name != NULL) && !strcmp(argv[0], (*cmdp)->c_name))
-			cmd = (*cmdp)->c_fn;
-	}
-	if (cmd != NULL) {
-		result = (cmd)(argc, argv);
+	cmd = interp_lookup_cmd(argv[0]);
+	if (cmd != NULL && cmd->c_fn) {
+		result = cmd->c_fn(argc, argv);
 	} else {
 		command_errmsg = "unknown command";
 	}
-	return(result);
+	return (result);
 }