You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ma...@apache.org on 2020/10/29 11:44:08 UTC

[incubator-nuttx-apps] branch master updated (36b1be0 -> 305bd0e)

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

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


    from 36b1be0  Fixed coding std issues
     new e76ab9c  Remove all fclose with stdin, stdout and stderr
     new 305bd0e  nshlib: Fix nsh_configstdio() in nsh_usbconsole.c

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 examples/nxterm/nxterm_main.c    |  3 ---
 graphics/nxwm/src/cnxterm.cxx    | 12 -----------
 graphics/twm4nx/apps/cnxterm.cxx | 12 -----------
 nshlib/nsh_altconsole.c          | 15 ++++----------
 nshlib/nsh_usbconsole.c          | 43 ++++++++++++++++++----------------------
 5 files changed, 23 insertions(+), 62 deletions(-)


[incubator-nuttx-apps] 02/02: nshlib: Fix nsh_configstdio() in nsh_usbconsole.c

Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 305bd0e448ff7b3fb7a5adb042596df99778a783
Author: Masayuki Ishikawa <ma...@gmail.com>
AuthorDate: Thu Oct 29 16:23:53 2020 +0900

    nshlib: Fix nsh_configstdio() in nsh_usbconsole.c
    
    Summary:
    - This commit fixes nsh_configstdio() to setup stdout and stderr
    
    Impact:
    - Affect nsh_usbconsole only
    
    Testing:
    - Tested with stm32f4discovery:usbnsh
    
    Signed-off-by: Masayuki Ishikawa <Ma...@jp.sony.com>
---
 nshlib/nsh_usbconsole.c | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)

diff --git a/nshlib/nsh_usbconsole.c b/nshlib/nsh_usbconsole.c
index c323f28..28f63bc 100644
--- a/nshlib/nsh_usbconsole.c
+++ b/nshlib/nsh_usbconsole.c
@@ -75,7 +75,7 @@
  *
  ****************************************************************************/
 
-static void nsh_configstdio(int fd)
+static void nsh_configstdio(int fd, FAR struct console_stdio_s *pstate)
 {
   /* Make sure the stdout, and stderr are flushed */
 
@@ -87,6 +87,16 @@ static void nsh_configstdio(int fd)
   dup2(fd, 0);
   dup2(fd, 1);
   dup2(fd, 2);
+
+  /* Setup the stdout */
+
+  pstate->cn_outfd     = 1;
+  pstate->cn_outstream = fdopen(1, "a");
+
+  /* Setup the stderr */
+
+  pstate->cn_errfd     = 2;
+  pstate->cn_errstream = fdopen(2, "a");
 }
 
 /****************************************************************************
@@ -97,7 +107,7 @@ static void nsh_configstdio(int fd)
  *
  ****************************************************************************/
 
-static int nsh_nullstdio(void)
+static int nsh_nullstdio(FAR struct console_stdio_s *pstate)
 {
   int fd;
 
@@ -108,7 +118,7 @@ static int nsh_nullstdio(void)
     {
       /* Configure standard I/O to use /dev/null */
 
-      nsh_configstdio(fd);
+      nsh_configstdio(fd, pstate);
 
       /* We can close the original file descriptor now (unless it was one of
        * 0-2)
@@ -133,7 +143,7 @@ static int nsh_nullstdio(void)
  *
  ****************************************************************************/
 
-static int nsh_waitusbready(void)
+static int nsh_waitusbready(FAR struct console_stdio_s *pstate)
 {
   char inch;
   ssize_t nbytes;
@@ -214,7 +224,7 @@ restart:
 
   /* Configure standard I/O */
 
-  nsh_configstdio(fd);
+  nsh_configstdio(fd, pstate);
 
   /* We can close the original file descriptor (unless it was one of 0-2) */
 
@@ -296,7 +306,7 @@ int nsh_consolemain(int argc, FAR char *argv[])
   /* Configure to use /dev/null if we do not have a valid console. */
 
 #ifndef CONFIG_DEV_CONSOLE
-  nsh_nullstdio();
+  nsh_nullstdio(pstate);
 #endif
 
   /* Execute the one-time start-up script (output may go to /dev/null) */
@@ -325,7 +335,7 @@ int nsh_consolemain(int argc, FAR char *argv[])
        * standard I/O to the USB serial device.
        */
 
-      ret = nsh_waitusbready();
+      ret = nsh_waitusbready(pstate);
       UNUSED(ret); /* Eliminate warning if not used */
       DEBUGASSERT(ret == OK);
 
@@ -337,7 +347,7 @@ int nsh_consolemain(int argc, FAR char *argv[])
        * valid console device.
        */
 
-      nsh_nullstdio();
+      nsh_nullstdio(pstate);
     }
 }
 


[incubator-nuttx-apps] 01/02: Remove all fclose with stdin, stdout and stderr

Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit e76ab9c868c56e39e37deed954623a988104180d
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Tue Oct 13 15:53:19 2020 +0800

    Remove all fclose with stdin, stdout and stderr
    
    since it is wrong to close the builtin stream and specially note
    https://pubs.opengroup.org/onlinepubs/9699919799/functions/fclose.html:
    
    Since after the call to fclose() any use of stream results in
    undefined behavior, fclose() should not be used on stdin, stdout,
    or stderr except immediately before process termination (see XBD
    Process Termination), so as to avoid triggering undefined behavior
    in other standard interfaces that rely on these streams. If there
    are any atexit() handlers registered by the application, such a
    call to fclose() should not occur until the last handler is
    finishing. Once fclose() has been used to close stdin, stdout, or
    stderr, there is no standard way to reopen any of these streams.
    
    and it is also unnecessary because the stream always get flushed.
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 examples/nxterm/nxterm_main.c    |  3 ---
 graphics/nxwm/src/cnxterm.cxx    | 12 ------------
 graphics/twm4nx/apps/cnxterm.cxx | 12 ------------
 nshlib/nsh_altconsole.c          | 15 ++++-----------
 nshlib/nsh_usbconsole.c          | 21 +++------------------
 5 files changed, 7 insertions(+), 56 deletions(-)

diff --git a/examples/nxterm/nxterm_main.c b/examples/nxterm/nxterm_main.c
index 491ee94..8c96787 100644
--- a/examples/nxterm/nxterm_main.c
+++ b/examples/nxterm/nxterm_main.c
@@ -391,9 +391,6 @@ int main(int argc, FAR char *argv[])
   fflush(stdout);
   fflush(stderr);
 
-  fclose(stdout);
-  fclose(stderr);
-
   dup2(fd, 1);
   dup2(fd, 2);
 
diff --git a/graphics/nxwm/src/cnxterm.cxx b/graphics/nxwm/src/cnxterm.cxx
index 8273b69..38e8250 100644
--- a/graphics/nxwm/src/cnxterm.cxx
+++ b/graphics/nxwm/src/cnxterm.cxx
@@ -504,23 +504,11 @@ int CNxTerm::nxterm(int argc, char *argv[])
   std::fflush(stderr);
 
 #ifdef CONFIG_NXTERM_NXKBDIN
-  std::fclose(stdin);
-#endif
-  std::fclose(stdout);
-  std::fclose(stderr);
-
-#ifdef CONFIG_NXTERM_NXKBDIN
   std::dup2(fd, 0);
 #endif
   std::dup2(fd, 1);
   std::dup2(fd, 2);
 
-#ifdef CONFIG_NXTERM_NXKBDIN
-  std::fdopen(0, "r");
-#endif
-  std::fdopen(1, "w");
-  std::fdopen(2, "w");
-
   // And we can close our original driver file descriptor
 
   if (fd > 2)
diff --git a/graphics/twm4nx/apps/cnxterm.cxx b/graphics/twm4nx/apps/cnxterm.cxx
index d895c81..f67fdd4 100644
--- a/graphics/twm4nx/apps/cnxterm.cxx
+++ b/graphics/twm4nx/apps/cnxterm.cxx
@@ -418,23 +418,11 @@ int CNxTerm::nxterm(int argc, char *argv[])
   std::fflush(stderr);
 
 #ifdef CONFIG_NXTERM_NXKBDIN
-  std::fclose(stdin);
-#endif
-  std::fclose(stdout);
-  std::fclose(stderr);
-
-#ifdef CONFIG_NXTERM_NXKBDIN
   std::dup2(fd, 0);
 #endif
   std::dup2(fd, 1);
   std::dup2(fd, 2);
 
-#ifdef CONFIG_NXTERM_NXKBDIN
-  std::fdopen(0, "r");
-#endif
-  std::fdopen(1, "w");
-  std::fdopen(2, "w");
-
   // And we can close our original driver file descriptor
 
   if (fd > 2)
diff --git a/nshlib/nsh_altconsole.c b/nshlib/nsh_altconsole.c
index 88bf540..1de4306 100644
--- a/nshlib/nsh_altconsole.c
+++ b/nshlib/nsh_altconsole.c
@@ -78,9 +78,9 @@ static int nsh_clone_console(FAR struct console_stdio_s *pstate)
       return -ENODEV;
     }
 
-  /* Close stderr: we only close stderr if we opened the alternative one */
+  /* Flush stderr: we only flush stderr if we opened the alternative one */
 
-  fclose(stderr);
+  fflush(stderr);
 
   /* Associate the new opened file descriptor to stderr */
 
@@ -101,9 +101,9 @@ static int nsh_clone_console(FAR struct console_stdio_s *pstate)
       return -ENODEV;
     }
 
-  /* Close stdout: we only close stdout if we opened the alternative one */
+  /* Flush stdout: we only flush stdout if we opened the alternative one */
 
-  fclose(stdout);
+  fflush(stdout);
 
   /* Associate the new opened file descriptor to stdout */
 
@@ -122,7 +122,6 @@ static int nsh_clone_console(FAR struct console_stdio_s *pstate)
   pstate->cn_errstream = fdopen(pstate->cn_errfd, "a");
   if (!pstate->cn_errstream)
     {
-      close(pstate->cn_errfd);
       free(pstate);
       return -EIO;
     }
@@ -133,7 +132,6 @@ static int nsh_clone_console(FAR struct console_stdio_s *pstate)
   pstate->cn_outstream = fdopen(pstate->cn_outfd, "a");
   if (!pstate->cn_outstream)
     {
-      close(pstate->cn_outfd);
       free(pstate);
       return -EIO;
     }
@@ -202,10 +200,6 @@ static int nsh_wait_inputdev(FAR struct console_stdio_s *pstate,
     }
   while (fd < 0);
 
-  /* Close stdin: we only closed stdin if we opened the alternative one */
-
-  fclose(stdin);
-
   /* Okay.. we have successfully opened the input device.  Did
    * we just re-open fd 0?
    */
@@ -225,7 +219,6 @@ static int nsh_wait_inputdev(FAR struct console_stdio_s *pstate,
       pstate->cn_constream = fdopen(pstate->cn_confd, "r+");
       if (!pstate->cn_constream)
         {
-          close(pstate->cn_confd);
           free(pstate);
           return -EIO;
         }
diff --git a/nshlib/nsh_usbconsole.c b/nshlib/nsh_usbconsole.c
index d7069dd..c323f28 100644
--- a/nshlib/nsh_usbconsole.c
+++ b/nshlib/nsh_usbconsole.c
@@ -77,31 +77,16 @@
 
 static void nsh_configstdio(int fd)
 {
-  /* Make sure the stdin, stdout, and stderr are closed */
+  /* Make sure the stdout, and stderr are flushed */
 
-  fclose(stdin);
-  fclose(stdout);
-  fclose(stderr);
+  fflush(stdout);
+  fflush(stderr);
 
   /* Dup the fd to create standard fd 0-2 */
 
   dup2(fd, 0);
   dup2(fd, 1);
   dup2(fd, 2);
-
-  /* fdopen to get the stdin, stdout and stderr streams. The following logic
-   * depends on the fact that the library layer will allocate FILEs in order.
-   * And since we closed stdin, stdout, and stderr above, that is what we
-   * should get.
-   *
-   * fd = 0 is stdin  (read-only)
-   * fd = 1 is stdout (write-only, append)
-   * fd = 2 is stderr (write-only, append)
-   */
-
-  fdopen(0, "r");
-  fdopen(1, "a");
-  fdopen(2, "a");
 }
 
 /****************************************************************************