Skip to content

Commit 93060aa

Browse files
wangchdoxiaoxiang781216
authored andcommitted
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>
1 parent 1b9d420 commit 93060aa

File tree

5 files changed

+42
-9
lines changed

5 files changed

+42
-9
lines changed

nshlib/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ if(CONFIG_NSH_LIBRARY)
5151
list(APPEND CSRCS nsh_fsutils.c)
5252

5353
if(CONFIG_NSH_BUILTIN_APPS)
54-
list(APPEND CSRCS nsh_builtin.c)
54+
if(NOT CONFIG_NSH_BUILTIN_AS_COMMAND)
55+
list(APPEND CSRCS nsh_builtin.c)
56+
endif()
5557
endif()
5658

5759
if(CONFIG_NSH_FILE_APPS)

nshlib/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,14 @@ config NSH_BUILTIN_APPS
242242
more information). This options requires support for builtin
243243
applications (BUILTIN).
244244

245+
config NSH_BUILTIN_AS_COMMAND
246+
bool "Run builtin apps as command"
247+
default n
248+
depends on BUILTIN
249+
---help---
250+
Enable to run builtin application directly without creating
251+
a separate thread.
252+
245253
config NSH_FILE_APPS
246254
bool "Enable execution of program files"
247255
default n

nshlib/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ endif
3636
CSRCS += nsh_fsutils.c
3737

3838
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
39+
ifneq ($(CONFIG_NSH_BUILTIN_AS_COMMAND),y)
3940
CSRCS += nsh_builtin.c
4041
endif
42+
endif
4143

4244
ifeq ($(CONFIG_NSH_FILE_APPS),y)
4345
CSRCS += nsh_fileapps.c

nshlib/nsh_command.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,10 +1213,14 @@ static int cmd_expr(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
12131213

12141214
int nsh_command(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char *argv[])
12151215
{
1216-
const struct cmdmap_s *cmdmap;
1217-
const char *cmd;
1218-
nsh_cmd_t handler = cmd_unrecognized;
1219-
int ret;
1216+
const struct cmdmap_s *cmdmap;
1217+
const char *cmd;
1218+
nsh_cmd_t handler = cmd_unrecognized;
1219+
#ifdef CONFIG_NSH_BUILTIN_AS_COMMAND
1220+
const struct builtin_s *builtin;
1221+
int index;
1222+
#endif
1223+
int ret;
12201224

12211225
/* The form of argv is:
12221226
*
@@ -1228,6 +1232,23 @@ int nsh_command(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char *argv[])
12281232

12291233
cmd = argv[0];
12301234

1235+
#ifdef CONFIG_NSH_BUILTIN_AS_COMMAND
1236+
/* Check if the command is available in the builtin list */
1237+
1238+
index = builtin_isavail(cmd);
1239+
1240+
if (index > 0)
1241+
{
1242+
/* Get the builtin structure by index */
1243+
1244+
builtin = builtin_for_index(index);
1245+
if (builtin != NULL)
1246+
{
1247+
return (builtin->main)(argc, (FAR char **)argv);
1248+
}
1249+
}
1250+
#endif
1251+
12311252
/* See if the command is one that we understand */
12321253

12331254
for (cmdmap = g_cmdmap; cmdmap->cmd; cmdmap++)

nshlib/nsh_parse.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,12 +512,12 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
512512
*
513513
* 1. Load a file from file system if possible. An external command on a
514514
* file system with the provided name (and on the defined PATH) takes
515-
* precendence over any other source of a command by that name. This
515+
* precedence over any other source of a command by that name. This
516516
* allows the user to replace a built-in command with a command on a`
517517
* file system
518518
*
519519
* 2. If not, run a built-in application of that name if possible. A
520-
* built-in application will take precendence over any NSH command.
520+
* built-in application will take precedence over any NSH command.
521521
*
522522
* 3. If not, run an NSH command line command if possible.
523523
*
@@ -540,7 +540,7 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
540540
* Note the priority is not effected by nice-ness.
541541
*/
542542

543-
#ifdef CONFIG_NSH_BUILTIN_APPS
543+
#if defined(CONFIG_NSH_BUILTIN_APPS) && !defined(CONFIG_NSH_BUILTIN_AS_COMMAND)
544544
ret = nsh_builtin(vtbl, argv[0], argv, param);
545545
if (ret >= 0)
546546
{
@@ -637,7 +637,7 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
637637
sh_argv[3] = NULL;
638638

639639
/* np.np_bg still there, try use nsh_builtin or nsh_fileapp to
640-
* dispatch the backgroud by sh -c ""
640+
* dispatch the background by sh -c ""
641641
*/
642642

643643
ret = nsh_execute(vtbl, 4, sh_argv, param);

0 commit comments

Comments
 (0)