You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by gn...@apache.org on 2020/04/19 14:07:21 UTC

[incubator-nuttx-apps] 07/09: nshlib: Enhance nsh to execute the shell script

This is an automated email from the ASF dual-hosted git repository.

gnutt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git

commit 3f9302561c960f69e453a5b42e8043130a1fc351
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sat Apr 18 22:27:10 2020 +0800

    nshlib: Enhance nsh to execute the shell script
    
    and support the interactive shell too
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 nshlib/nsh_system.c    | 45 ++++++++++++++++++++++++++++++---------------
 system/popen/popen.c   |  7 ++++---
 system/system/system.c |  7 ++++---
 3 files changed, 38 insertions(+), 21 deletions(-)

diff --git a/nshlib/nsh_system.c b/nshlib/nsh_system.c
index 1549bdc..e866660 100644
--- a/nshlib/nsh_system.c
+++ b/nshlib/nsh_system.c
@@ -41,6 +41,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <assert.h>
 
 #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 [<script-path>|-c <command>]\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 */
+  /* Exit upon return */
 
-      nsh_exit(&pstate->cn_vtbl, OK);
-      return EXIT_SUCCESS;
-    }
-  else
-    {
-      return EXIT_FAILURE;
-    }
+  nsh_exit(&pstate->cn_vtbl, ret);
+  return ret;
 }
diff --git a/system/popen/popen.c b/system/popen/popen.c
index dfc201d..99c0ed3 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 76749c2..5f3f182 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,