From 93060aa59df830d2a4906138dec5253f161aeaf0 Mon Sep 17 00:00:00 2001 From: wangchengdong Date: Tue, 9 Sep 2025 09:12:31 +0800 Subject: [PATCH] nshlib/nsh_builtin.c : Add support to run builtin dirrectly as command Add a new config: NSH_BUILTIN_AS_COMMAND. Provide a new implementation for nsh_builtin when CONFIG_NSH_BUILTIN_APPS_AS_COMMAND is enabled. Signed-off-by: Chengdong Wang --- nshlib/CMakeLists.txt | 4 +++- nshlib/Kconfig | 8 ++++++++ nshlib/Makefile | 2 ++ nshlib/nsh_command.c | 29 +++++++++++++++++++++++++---- nshlib/nsh_parse.c | 8 ++++---- 5 files changed, 42 insertions(+), 9 deletions(-) diff --git a/nshlib/CMakeLists.txt b/nshlib/CMakeLists.txt index ddfe1f87e..86b1b06ae 100644 --- a/nshlib/CMakeLists.txt +++ b/nshlib/CMakeLists.txt @@ -51,7 +51,9 @@ if(CONFIG_NSH_LIBRARY) list(APPEND CSRCS nsh_fsutils.c) if(CONFIG_NSH_BUILTIN_APPS) - list(APPEND CSRCS nsh_builtin.c) + if(NOT CONFIG_NSH_BUILTIN_AS_COMMAND) + list(APPEND CSRCS nsh_builtin.c) + endif() endif() if(CONFIG_NSH_FILE_APPS) diff --git a/nshlib/Kconfig b/nshlib/Kconfig index fa7215ce2..7fd49bbc7 100644 --- a/nshlib/Kconfig +++ b/nshlib/Kconfig @@ -242,6 +242,14 @@ config NSH_BUILTIN_APPS more information). This options requires support for builtin applications (BUILTIN). +config NSH_BUILTIN_AS_COMMAND + bool "Run builtin apps as command" + default n + depends on BUILTIN + ---help--- + Enable to run builtin application directly without creating + a separate thread. + config NSH_FILE_APPS bool "Enable execution of program files" default n diff --git a/nshlib/Makefile b/nshlib/Makefile index bf6ec06f7..26dbbd0de 100644 --- a/nshlib/Makefile +++ b/nshlib/Makefile @@ -36,8 +36,10 @@ endif CSRCS += nsh_fsutils.c ifeq ($(CONFIG_NSH_BUILTIN_APPS),y) +ifneq ($(CONFIG_NSH_BUILTIN_AS_COMMAND),y) CSRCS += nsh_builtin.c endif +endif ifeq ($(CONFIG_NSH_FILE_APPS),y) CSRCS += nsh_fileapps.c diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c index 7bbaf69be..3911e0844 100644 --- a/nshlib/nsh_command.c +++ b/nshlib/nsh_command.c @@ -1213,10 +1213,14 @@ static int cmd_expr(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv) int nsh_command(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char *argv[]) { - const struct cmdmap_s *cmdmap; - const char *cmd; - nsh_cmd_t handler = cmd_unrecognized; - int ret; + const struct cmdmap_s *cmdmap; + const char *cmd; + nsh_cmd_t handler = cmd_unrecognized; +#ifdef CONFIG_NSH_BUILTIN_AS_COMMAND + const struct builtin_s *builtin; + int index; +#endif + int ret; /* The form of argv is: * @@ -1228,6 +1232,23 @@ int nsh_command(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char *argv[]) cmd = argv[0]; +#ifdef CONFIG_NSH_BUILTIN_AS_COMMAND + /* Check if the command is available in the builtin list */ + + index = builtin_isavail(cmd); + + if (index > 0) + { + /* Get the builtin structure by index */ + + builtin = builtin_for_index(index); + if (builtin != NULL) + { + return (builtin->main)(argc, (FAR char **)argv); + } + } +#endif + /* See if the command is one that we understand */ for (cmdmap = g_cmdmap; cmdmap->cmd; cmdmap++) diff --git a/nshlib/nsh_parse.c b/nshlib/nsh_parse.c index e21381b9f..ab5d39050 100644 --- a/nshlib/nsh_parse.c +++ b/nshlib/nsh_parse.c @@ -512,12 +512,12 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl, * * 1. Load a file from file system if possible. An external command on a * file system with the provided name (and on the defined PATH) takes - * precendence over any other source of a command by that name. This + * precedence over any other source of a command by that name. This * allows the user to replace a built-in command with a command on a` * file system * * 2. If not, run a built-in application of that name if possible. A - * built-in application will take precendence over any NSH command. + * built-in application will take precedence over any NSH command. * * 3. If not, run an NSH command line command if possible. * @@ -540,7 +540,7 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl, * Note the priority is not effected by nice-ness. */ -#ifdef CONFIG_NSH_BUILTIN_APPS +#if defined(CONFIG_NSH_BUILTIN_APPS) && !defined(CONFIG_NSH_BUILTIN_AS_COMMAND) ret = nsh_builtin(vtbl, argv[0], argv, param); if (ret >= 0) { @@ -637,7 +637,7 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl, sh_argv[3] = NULL; /* np.np_bg still there, try use nsh_builtin or nsh_fileapp to - * dispatch the backgroud by sh -c "" + * dispatch the background by sh -c "" */ ret = nsh_execute(vtbl, 4, sh_argv, param);