From 3f9302561c960f69e453a5b42e8043130a1fc351 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Sat, 18 Apr 2020 22:27:10 +0800 Subject: [PATCH] nshlib: Enhance nsh to execute the shell script and support the interactive shell too Signed-off-by: Xiang Xiao --- nshlib/nsh_system.c | 47 ++++++++++++++++++++++++++++-------------- system/popen/popen.c | 7 ++++--- system/system/system.c | 7 ++++--- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/nshlib/nsh_system.c b/nshlib/nsh_system.c index 1549bdcdd..e866660dd 100644 --- a/nshlib/nsh_system.c +++ b/nshlib/nsh_system.c @@ -41,6 +41,7 @@ #include #include +#include #include #include "nsh.h" @@ -71,30 +72,44 @@ int nsh_system(int argc, char *argv[]) { - /* Expect argc == 2 with argv[1] being the command to execute */ + FAR struct console_stdio_s *pstate = nsh_newconsole(); + FAR struct nsh_vtbl_s *vtbl; + int ret = EXIT_FAILURE; - if (argc >= 2) + DEBUGASSERT(pstate != NULL); + vtbl = &pstate->cn_vtbl; + + if (argc < 2) { - FAR struct console_stdio_s *pstate = nsh_newconsole(); - FAR struct nsh_vtbl_s *vtbl; + /* Execute the interactive shell */ - DEBUGASSERT(pstate != NULL); - vtbl = &pstate->cn_vtbl; + ret = nsh_session(pstate, false); + } + else if (strcmp(argv[1], "-h") == 0) + { + ret = nsh_output(vtbl, "Usage: %s [|-c ]\n", + argv[0]); + } + else if (strcmp(argv[1], "-c") != 0) + { +#if CONFIG_NFILE_STREAMS > 0 && !defined(CONFIG_NSH_DISABLESCRIPT) + /* Execute the shell script */ + ret = nsh_script(vtbl, argv[0], argv[1]); +#endif + } + else if (argc >= 3) + { /* Parse process the command */ - nsh_parse(vtbl, argv[1]); + ret = nsh_parse(vtbl, argv[2]); #if CONFIG_NFILE_STREAMS > 0 fflush(pstate->cn_outstream); #endif - - /* Exit upon return */ - - nsh_exit(&pstate->cn_vtbl, OK); - return EXIT_SUCCESS; - } - else - { - return EXIT_FAILURE; } + + /* Exit upon return */ + + nsh_exit(&pstate->cn_vtbl, ret); + return ret; } diff --git a/system/popen/popen.c b/system/popen/popen.c index dfc201ddb..99c0ed3c1 100644 --- a/system/popen/popen.c +++ b/system/popen/popen.c @@ -128,7 +128,7 @@ FILE *popen(FAR const char *command, FAR const char *mode) struct sched_param param; posix_spawnattr_t attr; posix_spawn_file_actions_t file_actions; - FAR char *argv[2]; + FAR char *argv[3]; int fd[2]; int oldfd; int newfd; @@ -258,8 +258,9 @@ FILE *popen(FAR const char *command, FAR const char *mode) * appropriately. */ - argv[0] = (FAR char *)command; - argv[1] = NULL; + argv[0] = "-c"; + argv[1] = (FAR char *)command; + argv[2] = NULL; #ifdef CONFIG_SYSTEM_POPEN_SHPATH errcode = posix_spawn(&container->shell, CONFIG_SYSTEM_POPEN_SHPATH, diff --git a/system/system/system.c b/system/system/system.c index 76749c293..5f3f1828d 100644 --- a/system/system/system.c +++ b/system/system/system.c @@ -74,7 +74,7 @@ int system(FAR const char *cmd) { - FAR char *argv[2]; + FAR char *argv[3]; struct sched_param param; posix_spawnattr_t attr; pid_t pid; @@ -141,8 +141,9 @@ int system(FAR const char *cmd) /* Spawn nsh_system() which will execute the command under the shell. */ - argv[0] = (FAR char *)cmd; - argv[1] = NULL; + argv[0] = "-c"; + argv[1] = (FAR char *)cmd; + argv[2] = NULL; #ifdef CONFIG_SYSTEM_SYSTEM_SHPATH errcode = posix_spawn(&pid, CONFIG_SYSTEM_SYSTEM_SHPATH, NULL, &attr,