You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/07/22 05:52:39 UTC

[incubator-nuttx-apps] branch master updated: nshlib:support nsh can redirect

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

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


The following commit(s) were added to refs/heads/master by this push:
     new beb9188ce nshlib:support nsh can redirect
beb9188ce is described below

commit beb9188cee4978dc64369770cc6053c3c09bb2ad
Author: anjiahao <an...@xiaomi.com>
AuthorDate: Tue Jun 7 17:10:24 2022 +0800

    nshlib:support nsh can redirect
    
    Signed-off-by: anjiahao <an...@xiaomi.com>
---
 nshlib/Kconfig      |  6 ++++++
 nshlib/nsh_script.c | 44 ++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/nshlib/Kconfig b/nshlib/Kconfig
index b52b219ad..b75e95245 100644
--- a/nshlib/Kconfig
+++ b/nshlib/Kconfig
@@ -854,6 +854,12 @@ config NSH_FATMOUNTPT
 		will mount a FAT FS under /tmp. This is the location where the FAT
 		FS will be mounted.  Default is "/tmp".
 
+config NSH_SCRIPT_REDIRECT_PATH
+	string "rcS redirect output"
+	default ""
+	---help---
+		This option can redirect rcS output.such as /dev/log or other.
+
 endif # NSH_ROMFSETC
 endmenu # Scripting Support
 
diff --git a/nshlib/nsh_script.c b/nshlib/nsh_script.c
index 433f1529c..c855d6842 100644
--- a/nshlib/nsh_script.c
+++ b/nshlib/nsh_script.c
@@ -23,12 +23,49 @@
  ****************************************************************************/
 
 #include <nuttx/config.h>
+#include <fcntl.h>
 
 #include "nsh.h"
 #include "nsh_console.h"
 
 #if defined(CONFIG_FILE_STREAM) && !defined(CONFIG_NSH_DISABLESCRIPT)
 
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+#if defined(CONFIG_NSH_ROMFSETC) || defined(CONFIG_NSH_ROMFSRC)
+static int nsh_script_redirect(FAR struct nsh_vtbl_s *vtbl,
+                               FAR const char *cmd,
+                               FAR const char *path)
+{
+  uint8_t save[SAVE_SIZE];
+  int fd = -1;
+  int ret;
+
+  if (CONFIG_NSH_SCRIPT_REDIRECT_PATH[0])
+    {
+      fd = open(CONFIG_NSH_SCRIPT_REDIRECT_PATH, 0666);
+      if (fd > 0)
+        {
+          nsh_redirect(vtbl, fd, save);
+        }
+    }
+
+  ret = nsh_script(vtbl, cmd, path);
+  if (CONFIG_NSH_SCRIPT_REDIRECT_PATH[0])
+    {
+      if (fd > 0)
+        {
+          nsh_undirect(vtbl, save);
+          close(fd);
+        }
+    }
+
+  return ret;
+}
+#endif
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -157,7 +194,7 @@ int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
 #ifdef CONFIG_NSH_ROMFSETC
 int nsh_sysinitscript(FAR struct nsh_vtbl_s *vtbl)
 {
-  return nsh_script(vtbl, "sysinit", NSH_SYSINITPATH);
+  return nsh_script_redirect(vtbl, "sysinit", NSH_SYSINITPATH);
 }
 #endif
 
@@ -190,8 +227,7 @@ int nsh_initscript(FAR struct nsh_vtbl_s *vtbl)
 
   if (!already)
     {
-      ret = nsh_script(vtbl, "init", NSH_INITPATH);
-
+      ret = nsh_script_redirect(vtbl, "init", NSH_INITPATH);
 #ifndef CONFIG_NSH_DISABLESCRIPT
       /* Reset the option flags */
 
@@ -214,7 +250,7 @@ int nsh_initscript(FAR struct nsh_vtbl_s *vtbl)
 #ifdef CONFIG_NSH_ROMFSRC
 int nsh_loginscript(FAR struct nsh_vtbl_s *vtbl)
 {
-  return nsh_script(vtbl, "login", NSH_RCPATH);
+  return nsh_script_redirect(vtbl, "login", NSH_RCPATH);
 }
 #endif
 #endif /* CONFIG_NSH_ROMFSETC */