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 <wangchengdong@lixiang.com>
This commit is contained in:
wangchengdong 2025-09-09 09:12:31 +08:00 committed by Xiang Xiao
parent 1b9d420e79
commit 93060aa59d
5 changed files with 42 additions and 9 deletions

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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++)

View file

@ -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);