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/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 95f32fd01 nsh: Decouple with CONFIG_FILE_STREAM
95f32fd01 is described below

commit 95f32fd018be7a66354942647d9e1c778ad5d488
Author: Huang Qi <huang...@xiaomi.com>
AuthorDate: Fri Feb 10 17:50:46 2023 +0800

    nsh: Decouple with CONFIG_FILE_STREAM
    
    Replace all fwrite/fread/fgets/... to write/read/...
    
    Before:
    ```
           text    data     bss     dec     hex filename
     109827     601    6608  117036   1c92c nuttx/nuttx
    ```
        After:
    ```
           text    data     bss     dec     hex filename
    108053     601    6608  115262   1c23e nuttx/nuttx
    ```
        After with CONFIG_FILE_STREAM disabled:
    ```
           text    data     bss     dec     hex filename
     105667     601    6608  112876   1b8ec nuttx/nuttx
    ```
    
    Signed-off-by: Huang Qi <huang...@xiaomi.com>
---
 nshlib/Makefile          |   7 --
 nshlib/README.md         |   2 +-
 nshlib/nsh.h             |   6 +-
 nshlib/nsh_altconsole.c  |  33 +------
 nshlib/nsh_builtin.c     |   4 +-
 nshlib/nsh_console.c     | 155 +++++------------------------
 nshlib/nsh_console.h     |  44 +++------
 nshlib/nsh_login.c       |  27 +++--
 nshlib/nsh_parse.c       |  30 +-----
 nshlib/nsh_script.c      |  33 +++----
 nshlib/nsh_session.c     |  25 ++---
 nshlib/nsh_stdlogin.c    | 253 -----------------------------------------------
 nshlib/nsh_stdsession.c  | 236 -------------------------------------------
 nshlib/nsh_telnetlogin.c |  34 +++----
 nshlib/nsh_timcmds.c     |   6 --
 system/nsh/nsh_main.c    |   2 +-
 16 files changed, 99 insertions(+), 798 deletions(-)

diff --git a/nshlib/Makefile b/nshlib/Makefile
index 03c163e7a..3987a3a7d 100644
--- a/nshlib/Makefile
+++ b/nshlib/Makefile
@@ -26,17 +26,10 @@ CSRCS  = nsh_init.c nsh_parse.c nsh_console.c nsh_script.c 
nsh_system.c
 CSRCS += nsh_command.c nsh_fscmds.c nsh_ddcmd.c nsh_proccmds.c nsh_mmcmds.c
 CSRCS += nsh_timcmds.c nsh_envcmds.c nsh_syscmds.c nsh_dbgcmds.c
 
-ifeq ($(CONFIG_FILE_STREAM),y)
 CSRCS += nsh_session.c
 ifeq ($(CONFIG_NSH_CONSOLE_LOGIN),y)
 CSRCS += nsh_login.c
 endif
-else
-CSRCS += nsh_stdsession.c
-ifeq ($(CONFIG_NSH_CONSOLE_LOGIN),y)
-CSRCS += nsh_stdlogin.c
-endif
-endif
 
 CSRCS += nsh_fsutils.c
 
diff --git a/nshlib/README.md b/nshlib/README.md
index 975df7ea1..336d72a69 100644
--- a/nshlib/README.md
+++ b/nshlib/README.md
@@ -1616,7 +1616,7 @@ rptun     | `CONFIG_RPTUN`
 set       | `CONFIG_NSH_VARS` || !`CONFIG_DISABLE_ENVIRON`
 shutdown  | `CONFIG_BOARDCTL_POWEROFF` || `CONFIG_BOARDCTL_RESET`
 sleep     | -
-source    | `CONFIG_FILE_STREAM` && !`CONFIG_NSH_DISABLESCRIPT`
+source    | !`CONFIG_NSH_DISABLESCRIPT`
 test      | !`CONFIG_NSH_DISABLESCRIPT`
 telnetd   | `CONFIG_NSH_TELNET` && `CONFIG_SYSTEM_TELNETD`
 time      | -
diff --git a/nshlib/nsh.h b/nshlib/nsh.h
index e072b1057..0ccd5992b 100644
--- a/nshlib/nsh.h
+++ b/nshlib/nsh.h
@@ -687,9 +687,7 @@ struct nsh_parser_s
 #ifndef CONFIG_NSH_DISABLEBG
   bool     np_bg;       /* true: The last command executed in background */
 #endif
-#ifdef CONFIG_FILE_STREAM
   bool     np_redirect; /* true: Output from the last command was re-directed 
*/
-#endif
   bool     np_fail;     /* true: The last command failed */
 #ifndef CONFIG_NSH_DISABLESCRIPT
   uint8_t  np_flags;    /* See nsh_npflags_e above */
@@ -699,7 +697,7 @@ struct nsh_parser_s
 #endif
 
 #ifndef CONFIG_NSH_DISABLESCRIPT
-  FILE    *np_stream;   /* Stream of current script */
+  int      np_fd;       /* Stream of current script */
 #ifndef CONFIG_NSH_DISABLE_LOOPS
   long     np_foffs;    /* File offset to the beginning of a line */
 #ifndef NSH_DISABLE_SEMICOLON
@@ -801,7 +799,7 @@ int nsh_usbconsole(void);
 #  define nsh_usbconsole() (-ENOSYS)
 #endif
 
-#if defined(CONFIG_FILE_STREAM) && !defined(CONFIG_NSH_DISABLESCRIPT)
+#ifndef CONFIG_NSH_DISABLESCRIPT
 int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
                FAR const char *path, bool log);
 #ifdef CONFIG_NSH_ROMFSETC
diff --git a/nshlib/nsh_altconsole.c b/nshlib/nsh_altconsole.c
index 49aad333b..82f284fda 100644
--- a/nshlib/nsh_altconsole.c
+++ b/nshlib/nsh_altconsole.c
@@ -60,10 +60,6 @@ static int nsh_clone_console(FAR struct console_stdio_s 
*pstate)
       return -ENODEV;
     }
 
-  /* Flush stderr: we only flush stderr if we opened the alternative one */
-
-  fflush(stderr);
-
   /* Associate the new opened file descriptor to stderr */
 
   dup2(fd, 2);
@@ -83,10 +79,6 @@ static int nsh_clone_console(FAR struct console_stdio_s 
*pstate)
       return -ENODEV;
     }
 
-  /* Flush stdout: we only flush stdout if we opened the alternative one */
-
-  fflush(stdout);
-
   /* Associate the new opened file descriptor to stdout */
 
   dup2(fd, 1);
@@ -100,23 +92,11 @@ static int nsh_clone_console(FAR struct console_stdio_s 
*pstate)
 
   /* Setup the stderr */
 
-  pstate->cn_errfd     = 2;
-  pstate->cn_errstream = fdopen(pstate->cn_errfd, "a");
-  if (!pstate->cn_errstream)
-    {
-      free(pstate);
-      return -EIO;
-    }
+  ERRFD(pstate) = 2;
 
   /* Setup the stdout */
 
-  pstate->cn_outfd     = 1;
-  pstate->cn_outstream = fdopen(pstate->cn_outfd, "a");
-  if (!pstate->cn_outstream)
-    {
-      free(pstate);
-      return -EIO;
-    }
+  OUTFD(pstate) = 1;
 
   return OK;
 }
@@ -196,15 +176,6 @@ static int nsh_wait_inputdev(FAR struct console_stdio_s 
*pstate,
 
       pstate->cn_confd = 0;
 
-      /* Create a standard C stream on the console device */
-
-      pstate->cn_constream = fdopen(pstate->cn_confd, "r+");
-      if (!pstate->cn_constream)
-        {
-          free(pstate);
-          return -EIO;
-        }
-
       /* Close the input device that we just opened */
 
       close(fd);
diff --git a/nshlib/nsh_builtin.c b/nshlib/nsh_builtin.c
index 59ec8d439..877af0fc1 100644
--- a/nshlib/nsh_builtin.c
+++ b/nshlib/nsh_builtin.c
@@ -136,7 +136,7 @@ int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char 
*cmd,
             {
               /* Setup up to receive SIGINT if control-C entered. */
 
-              tc = ioctl(stdout->fs_fd, TIOCSCTTY, ret);
+              tc = ioctl(STDOUT_FILENO, TIOCSCTTY, ret);
             }
 
           /* Wait for the application to exit.  We did lock the scheduler
@@ -201,7 +201,7 @@ int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char 
*cmd,
 
           if (vtbl->isctty && tc == 0)
             {
-              ioctl(stdout->fs_fd, TIOCNOTTY);
+              ioctl(STDOUT_FILENO, TIOCNOTTY);
             }
         }
 #  ifndef CONFIG_NSH_DISABLEBG
diff --git a/nshlib/nsh_console.c b/nshlib/nsh_console.c
index aa83af8e6..e12a88c80 100644
--- a/nshlib/nsh_console.c
+++ b/nshlib/nsh_console.c
@@ -42,15 +42,11 @@
  * Private Types
  ****************************************************************************/
 
-#ifdef CONFIG_FILE_STREAM
 struct serialsave_s
 {
   int   cn_errfd;     /* Re-directed error output file descriptor */
   int   cn_outfd;     /* Re-directed output file descriptor */
-  FILE *cn_errstream; /* Re-directed error output stream */
-  FILE *cn_outstream; /* Re-directed output stream */
 };
-#endif
 
 /****************************************************************************
  * Private Function Prototypes
@@ -78,35 +74,6 @@ static void nsh_consoleexit(FAR struct nsh_vtbl_s *vtbl, int 
exitstatus)
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: nsh_openifnotopen
- ****************************************************************************/
-
-static int nsh_openifnotopen(struct console_stdio_s *pstate)
-{
-  /* The stream is open in a lazy fashion.  This is done because the file
-   * descriptor may be opened on a different task than the stream.
-   */
-
-  if (!pstate->cn_outstream)
-    {
-      pstate->cn_outstream = fdopen(pstate->cn_outfd, "w");
-      if (!pstate->cn_outstream)
-        {
-          return ERROR;
-        }
-
-#if !defined(CONFIG_NSH_ALTCONDEV)
-      /* If the alternative console is not enabled then stderr = stdout */
-
-      pstate->cn_errfd     = pstate->cn_outfd;
-      pstate->cn_errstream = pstate->cn_outstream;
-#endif
-    }
-
-  return 0;
-}
-
 /****************************************************************************
  * Name: nsh_closeifnotclosed
  *
@@ -115,33 +82,22 @@ static int nsh_openifnotopen(struct console_stdio_s 
*pstate)
  *
  ****************************************************************************/
 
-#ifdef CONFIG_FILE_STREAM
 static void nsh_closeifnotclosed(struct console_stdio_s *pstate)
 {
-  if (pstate->cn_outstream == OUTSTREAM(pstate))
+  if (OUTFD(pstate) >= 0 && OUTFD(pstate) != STDOUT_FILENO)
     {
-      fflush(OUTSTREAM(pstate));
-      pstate->cn_outfd = OUTFD(pstate);
+      close(OUTFD(pstate));
     }
-  else
+
+  if (ERRFD(pstate) >= 0 && ERRFD(pstate) != STDERR_FILENO
+      && ERRFD(pstate) != OUTFD(pstate))
     {
-      if (pstate->cn_outstream)
-        {
-          fflush(pstate->cn_outstream);
-          fclose(pstate->cn_outstream);
-        }
-      else if (pstate->cn_outfd >= 0 && pstate->cn_outfd != OUTFD(pstate))
-        {
-          close(pstate->cn_outfd);
-        }
-
-      pstate->cn_errfd     = -1;
-      pstate->cn_outfd     = -1;
-      pstate->cn_errstream = NULL;
-      pstate->cn_outstream = NULL;
+      close(ERRFD(pstate));
     }
+
+  ERRFD(pstate) = -1;
+  OUTFD(pstate) = -1;
 }
-#endif
 
 /****************************************************************************
  * Name: nsh_consolewrite
@@ -159,29 +115,15 @@ static ssize_t nsh_consolewrite(FAR struct nsh_vtbl_s 
*vtbl,
   FAR struct console_stdio_s *pstate = (FAR struct console_stdio_s *)vtbl;
   ssize_t ret;
 
-  /* The stream is open in a lazy fashion.  This is done because the file
-   * descriptor may be opened on a different task than the stream.  The
-   * actual open will then occur with the first output from the new task.
-   */
-
-  if (nsh_openifnotopen(pstate) != 0)
-    {
-      return ERROR;
-    }
-
   /* Write the data to the output stream */
 
-  ret = fwrite(buffer, 1, nbytes, pstate->cn_outstream);
+  ret = write(OUTFD(pstate), buffer, nbytes);
   if (ret < 0)
     {
       _err("ERROR: [%d] Failed to send buffer: %d\n",
-          pstate->cn_outfd, errno);
+          OUTFD(pstate), errno);
     }
 
-  /* Flush the data to the output stream */
-
-  fflush(pstate->cn_outstream);
-
   return ret;
 }
 
@@ -200,18 +142,8 @@ static int nsh_consoleoutput(FAR struct nsh_vtbl_s *vtbl,
   va_list ap;
   int ret;
 
-  /* The stream is open in a lazy fashion.  This is done because the file
-   * descriptor may be opened on a different task than the stream.  The
-   * actual open will then occur with the first output from the new task.
-   */
-
-  if (nsh_openifnotopen(pstate) != 0)
-    {
-      return ERROR;
-    }
-
   va_start(ap, fmt);
-  ret = vfprintf(pstate->cn_outstream, fmt, ap);
+  ret = vdprintf(OUTFD(pstate), fmt, ap);
   va_end(ap);
 
   return ret;
@@ -232,18 +164,8 @@ static int nsh_erroroutput(FAR struct nsh_vtbl_s *vtbl,
   va_list ap;
   int ret;
 
-  /* The stream is open in a lazy fashion.  This is done because the file
-   * descriptor may be opened on a different task than the stream.  The
-   * actual open will then occur with the first output from the new task.
-   */
-
-  if (nsh_openifnotopen(pstate) != 0)
-    {
-      return ERROR;
-    }
-
   va_start(ap, fmt);
-  ret = vfprintf(pstate->cn_errstream, fmt, ap);
+  ret = vdprintf(ERRFD(pstate), fmt, ap);
   va_end(ap);
 
   return ret;
@@ -298,7 +220,7 @@ static void nsh_consolerelease(FAR struct nsh_vtbl_s *vtbl)
   /* Close the console stream */
 
 #ifdef CONFIG_NSH_ALTCONDEV
-  fclose(pstate->cn_constream);
+  close(pstate->cn_confd);
 #endif
 
 #ifdef CONFIG_NSH_VARS
@@ -351,44 +273,21 @@ static void nsh_consoleredirect(FAR struct nsh_vtbl_s 
*vtbl, int fd,
   FAR struct console_stdio_s *pstate = (FAR struct console_stdio_s *)vtbl;
   FAR struct serialsave_s *ssave  = (FAR struct serialsave_s *)save;
 
-  /* Case 1: Redirected foreground commands */
+  /* Redirected foreground commands */
 
   if (ssave)
     {
-      /* pstate->cn_outstream and cn_outfd refer refer to the
-       * currently opened output stream.  If the output stream is open, flush
-       * any pending output.
-       */
-
-      if (pstate->cn_outstream)
-        {
-          fflush(pstate->cn_errstream);
-          fflush(pstate->cn_outstream);
-        }
-
       /* Save the current fd and stream values.  These will be restored
        * when nsh_consoleundirect() is called.
        */
 
-      ssave->cn_errfd     = pstate->cn_errfd;
-      ssave->cn_outfd     = pstate->cn_outfd;
-      ssave->cn_errstream = pstate->cn_errstream;
-      ssave->cn_outstream = pstate->cn_outstream;
-    }
-  else
-    {
-      /* nsh_consoleclone() set pstate->cn_outfd and cn_outstream to refer
-       * to standard out.  We just want to leave these alone and overwrite
-       * them with the fd for the re-directed stream.
-       */
+      ERRFD(ssave) = ERRFD(pstate);
+      OUTFD(ssave) = OUTFD(pstate);
     }
 
-  /* In either case, set the fd of the new, re-directed output and nullify
-   * the output stream (it will be fdopen'ed if it is used).
-   */
+  /* Set the fd of the new. */
 
-  pstate->cn_outfd     = fd;
-  pstate->cn_outstream = NULL;
+  OUTFD(pstate) = fd;
 }
 
 /****************************************************************************
@@ -403,13 +302,11 @@ static void nsh_consoleundirect(FAR struct nsh_vtbl_s 
*vtbl,
                                 FAR uint8_t *save)
 {
   FAR struct console_stdio_s *pstate = (FAR struct console_stdio_s *)vtbl;
-  FAR struct serialsave_s *ssave  = (FAR struct serialsave_s *)save;
+  FAR struct serialsave_s *ssave = (FAR struct serialsave_s *)save;
 
   nsh_closeifnotclosed(pstate);
-  pstate->cn_errfd     = ssave->cn_errfd;
-  pstate->cn_outfd     = ssave->cn_outfd;
-  pstate->cn_errstream = ssave->cn_errstream;
-  pstate->cn_outstream = ssave->cn_outstream;
+  ERRFD(pstate) = ERRFD(ssave);
+  OUTFD(pstate) = OUTFD(ssave);
 }
 
 /****************************************************************************
@@ -462,20 +359,16 @@ FAR struct console_stdio_s *nsh_newconsole(bool isctty)
       pstate->cn_vtbl.np.np_flags = NSH_NP_SET_OPTIONS_INIT;
 #endif
 
-#ifdef CONFIG_FILE_STREAM
       pstate->cn_vtbl.redirect    = nsh_consoleredirect;
       pstate->cn_vtbl.undirect    = nsh_consoleundirect;
 
       /* Initialize the error stream */
 
-      pstate->cn_errfd            = ERRFD(pstate);
-      pstate->cn_errstream        = ERRSTREAM(pstate);
+      ERRFD(pstate)               = STDERR_FILENO;
 
       /* Initialize the output stream */
 
-      pstate->cn_outfd            = OUTFD(pstate);
-      pstate->cn_outstream        = OUTSTREAM(pstate);
-#endif
+      OUTFD(pstate)               = STDOUT_FILENO;
     }
 
   return pstate;
diff --git a/nshlib/nsh_console.h b/nshlib/nsh_console.h
index 5e1c02cc6..249480bae 100644
--- a/nshlib/nsh_console.h
+++ b/nshlib/nsh_console.h
@@ -64,33 +64,24 @@
 
 /* Are we using the NuttX console for I/O?  Or some other character device? */
 
-#ifdef CONFIG_FILE_STREAM
-#  ifdef CONFIG_NSH_ALTCONDEV
-
-#    if !defined(CONFIG_NSH_ALTSTDIN) && !defined(CONFIG_NSH_ALTSTDOUT) && \
-        !defined(CONFIG_NSH_ALTSTDERR)
-#      error CONFIG_NSH_ALTCONDEV selected but CONFIG_NSH_ALTSTDxxx not 
provided
-#    endif
-
-#    define INFD(p)      ((p)->cn_confd)
-#    define INSTREAM(p)  ((p)->cn_constream)
-#    define OUTFD(p)     ((p)->cn_outfd)
-#    define OUTSTREAM(p) ((p)->cn_outstream)
-#    define ERRFD(p)     ((p)->cn_errfd)
-#    define ERRSTREAM(p) ((p)->cn_errstream)
-
-#  else
-
-#    define INFD(p)      0
-#    define INSTREAM(p)  stdin
-#    define OUTFD(p)     1
-#    define OUTSTREAM(p) stdout
-#    define ERRFD(p)     2
-#    define ERRSTREAM(p) stderr
+#ifdef CONFIG_NSH_ALTCONDEV
 
+#  if !defined(CONFIG_NSH_ALTSTDIN) && !defined(CONFIG_NSH_ALTSTDOUT) && \
+      !defined(CONFIG_NSH_ALTSTDERR)
+#    error CONFIG_NSH_ALTCONDEV selected but CONFIG_NSH_ALTSTDxxx not provided
 #  endif
+
+#  define INFD(p)      ((p)->cn_confd)
+
+#else
+
+#  define INFD(p)      0
+
 #endif
 
+#  define OUTFD(p)     ((p)->cn_outfd)
+#  define ERRFD(p)     ((p)->cn_errfd)
+
 /****************************************************************************
  * Public Types
  ****************************************************************************/
@@ -148,18 +139,11 @@ struct console_stdio_s
 
   /* NSH input/output streams */
 
-#ifdef CONFIG_FILE_STREAM
 #ifdef CONFIG_NSH_ALTCONDEV
   int   cn_confd;     /* Console I/O file descriptor */
 #endif
   int   cn_outfd;     /* Output file descriptor (possibly redirected) */
   int   cn_errfd;     /* Error Output file descriptor (possibly redirected) */
-#ifdef CONFIG_NSH_ALTCONDEV
-  FILE *cn_constream; /* Console I/O stream (possibly redirected) */
-#endif
-  FILE *cn_outstream; /* Output stream */
-  FILE *cn_errstream; /* Error Output stream */
-#endif
 
 #ifdef CONFIG_NSH_VARS
   /* Allocation and size of NSH variables */
diff --git a/nshlib/nsh_login.c b/nshlib/nsh_login.c
index 4d3976136..19be58ae5 100644
--- a/nshlib/nsh_login.c
+++ b/nshlib/nsh_login.c
@@ -166,13 +166,12 @@ int nsh_login(FAR struct console_stdio_s *pstate)
       /* Ask for the login username */
 
       username[0] = '\0';
-      fputs(g_userprompt, pstate->cn_outstream);
-      fflush(pstate->cn_outstream);
+      write(OUTFD(pstate), g_userprompt, strlen(g_userprompt));
 
       /* readline() returns EOF on failure */
 
-      ret = readline(pstate->cn_line, CONFIG_NSH_LINELEN,
-                     INSTREAM(pstate), OUTSTREAM(pstate));
+      ret = readline_fd(pstate->cn_line, CONFIG_NSH_LINELEN,
+                        INFD(pstate), OUTFD(pstate));
       if (ret != EOF)
         {
           /* Parse out the username */
@@ -188,18 +187,16 @@ int nsh_login(FAR struct console_stdio_s *pstate)
 
 #ifdef CONFIG_NSH_PLATFORM_CHALLENGE
       platform_challenge(challenge, sizeof(challenge));
-      fputs(challenge, pstate->cn_outstream);
-      fflush(pstate->cn_outstream);
+      write(OUTFD(pstate), challenge, strlen(challenge));
 #endif
 
       /* Ask for the login password */
 
-      fputs(g_passwordprompt, pstate->cn_outstream);
-      fflush(pstate->cn_outstream);
+      write(OUTFD(pstate), g_passwordprompt, strlen(g_passwordprompt));
 
       password[0] = '\0';
-      if (fgets(pstate->cn_line, CONFIG_NSH_LINELEN,
-                INSTREAM(pstate)) != NULL)
+      if (readline_fd(pstate->cn_line, CONFIG_NSH_LINELEN,
+                      INFD(pstate), -1) > 0)
         {
           /* Parse out the password */
 
@@ -226,14 +223,13 @@ int nsh_login(FAR struct console_stdio_s *pstate)
 #  error No user verification method selected
 #endif
             {
-              fputs(g_loginsuccess, pstate->cn_outstream);
-              fflush(pstate->cn_outstream);
+              write(OUTFD(pstate), g_loginsuccess, strlen(g_loginsuccess));
               return OK;
             }
           else
             {
-              fputs(g_badcredentials, pstate->cn_outstream);
-              fflush(pstate->cn_outstream);
+              write(OUTFD(pstate), g_badcredentials,
+                    strlen(g_badcredentials));
 #if CONFIG_NSH_LOGIN_FAILDELAY > 0
               usleep(CONFIG_NSH_LOGIN_FAILDELAY * 1000L);
 #endif
@@ -243,8 +239,7 @@ int nsh_login(FAR struct console_stdio_s *pstate)
 
   /* Too many failed login attempts */
 
-  fputs(g_loginfailure, pstate->cn_outstream);
-  fflush(pstate->cn_outstream);
+  write(OUTFD(pstate), g_loginfailure, strlen(g_loginsuccess));
   return -1;
 }
 
diff --git a/nshlib/nsh_parse.c b/nshlib/nsh_parse.c
index 7f86a1702..169561e4c 100644
--- a/nshlib/nsh_parse.c
+++ b/nshlib/nsh_parse.c
@@ -338,7 +338,6 @@ static void nsh_releaseargs(struct cmdarg_s *arg)
   FAR struct nsh_vtbl_s *vtbl = arg->vtbl;
   int i;
 
-#ifdef CONFIG_FILE_STREAM
   /* If the output was redirected, then file descriptor should
    * be closed.  The created task has its one, independent copy of
    * the file descriptor
@@ -348,7 +347,6 @@ static void nsh_releaseargs(struct cmdarg_s *arg)
     {
       close(arg->fd);
     }
-#endif
 
   /* Released the cloned vtbl instance */
 
@@ -487,9 +485,7 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
                        int argc, FAR char *argv[],
                        FAR const char *redirfile, int oflags)
 {
-#if defined(CONFIG_FILE_STREAM) || !defined(CONFIG_NSH_DISABLEBG)
   int fd = -1;
-#endif
   int ret;
 
   /* DO NOT CHANGE THE ORDERING OF THE FOLLOWING STEPS
@@ -527,11 +523,7 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
    */
 
 #ifdef CONFIG_NSH_BUILTIN_APPS
-#ifdef CONFIG_FILE_STREAM
   ret = nsh_builtin(vtbl, argv[0], argv, redirfile, oflags);
-#else
-  ret = nsh_builtin(vtbl, argv[0], argv, NULL, 0);
-#endif
   if (ret >= 0)
     {
       /* nsh_builtin() returned 0 or 1.  This means that the built-in
@@ -587,7 +579,6 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
 
 #endif
 
-#ifdef CONFIG_FILE_STREAM
   /* Redirected output? */
 
   if (vtbl->np.np_redirect)
@@ -605,7 +596,6 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
           goto errout;
         }
     }
-#endif
 
   /* Handle the case where the command is executed in background.
    * However is app is to be started as built-in new process will
@@ -641,14 +631,12 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
           goto errout_with_redirect;
         }
 
-#ifdef CONFIG_FILE_STREAM
       /* Handle redirection of output via a file descriptor */
 
       if (vtbl->np.np_redirect)
         {
           nsh_redirect(bkgvtbl, fd, NULL);
         }
-#endif
 
       /* Get the execution priority of this task */
 
@@ -723,7 +711,6 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
   else
 #endif
     {
-#ifdef CONFIG_FILE_STREAM
       uint8_t save[SAVE_SIZE];
 
       /* Handle redirection of output via a file descriptor */
@@ -732,7 +719,6 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
         {
           nsh_redirect(vtbl, fd, save);
         }
-#endif
 
       /* Then execute the command in "foreground" -- i.e., while the user
        * waits for the next prompt.  nsh_command will return:
@@ -743,7 +729,6 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
 
       ret = nsh_command(vtbl, argc, argv);
 
-#ifdef CONFIG_FILE_STREAM
       /* Restore the original output.  Undirect will close the redirection
        * file descriptor.
        */
@@ -752,7 +737,6 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
         {
           nsh_undirect(vtbl, save);
         }
-#endif
 
       /* Mark errors so that it is possible to test for non-zero return
        * values in nsh scripts.
@@ -772,13 +756,11 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
 
 #ifndef CONFIG_NSH_DISABLEBG
 errout_with_redirect:
-#ifdef CONFIG_FILE_STREAM
   if (vtbl->np.np_redirect)
     {
       close(fd);
     }
 #endif
-#endif
 
 errout:
   return nsh_saveresult(vtbl, true);
@@ -1879,7 +1861,7 @@ static int nsh_loop(FAR struct nsh_vtbl_s *vtbl, FAR char 
**ppcmd,
 #endif
               np->np_lpstate[np->np_lpndx].lp_state == NSH_LOOP_WHILE ||
               np->np_lpstate[np->np_lpndx].lp_state == NSH_LOOP_UNTIL ||
-              np->np_stream == NULL || np->np_foffs < 0)
+              np->np_fd < 0 || np->np_foffs < 0)
             {
               nsh_error(vtbl, g_fmtcontext, cmd);
               goto errout;
@@ -1971,7 +1953,7 @@ static int nsh_loop(FAR struct nsh_vtbl_s *vtbl, FAR char 
**ppcmd,
             {
               /* Set the new file position to the top of the loop offset */
 
-              ret = fseek(np->np_stream,
+              ret = lseek(np->np_fd,
                           np->np_lpstate[np->np_lpndx].lp_topoffs,
                           SEEK_SET);
               if (ret < 0)
@@ -2392,9 +2374,7 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, 
FAR char *cmdline)
   int       oflags = 0;
   int       argc;
   int       ret;
-#ifdef CONFIG_FILE_STREAM
   bool      redirect_save = false;
-#endif
 
   /* Initialize parser state */
 
@@ -2405,9 +2385,7 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, 
FAR char *cmdline)
   vtbl->np.np_bg       = false;
 #endif
 
-#ifdef CONFIG_FILE_STREAM
   vtbl->np.np_redirect = false;
-#endif
 
   /* Parse out the command at the beginning of the line */
 
@@ -2550,7 +2528,6 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, 
FAR char *cmdline)
     }
 #endif
 
-#ifdef CONFIG_FILE_STREAM
   /* Check if the output was re-directed using > or >> */
 
   if (argc > 2)
@@ -2577,7 +2554,6 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, 
FAR char *cmdline)
           argc                -= 2;
         }
     }
-#endif
 
   /* Last argument vector must be empty */
 
@@ -2596,7 +2572,6 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, 
FAR char *cmdline)
 
   /* Free any allocated resources */
 
-#ifdef CONFIG_FILE_STREAM
   /* Free the redirected output file path */
 
   if (redirfile)
@@ -2604,7 +2579,6 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, 
FAR char *cmdline)
       nsh_freefullpath(redirfile);
       vtbl->np.np_redirect = redirect_save;
     }
-#endif
 
   NSH_MEMLIST_FREE(&memlist);
   return ret;
diff --git a/nshlib/nsh_script.c b/nshlib/nsh_script.c
index 0174c0fbb..99e61a60b 100644
--- a/nshlib/nsh_script.c
+++ b/nshlib/nsh_script.c
@@ -30,7 +30,9 @@
 #include "nsh.h"
 #include "nsh_console.h"
 
-#if defined(CONFIG_FILE_STREAM) && !defined(CONFIG_NSH_DISABLESCRIPT)
+#include <system/readline.h>
+
+#ifndef CONFIG_NSH_DISABLESCRIPT
 
 /****************************************************************************
  * Private Functions
@@ -85,9 +87,8 @@ int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const FAR 
char *cmd,
                FAR const char *path, bool log)
 {
   FAR char *fullpath;
-  FAR FILE *savestream;
+  int savestream;
   FAR char *buffer;
-  FAR char *pret;
   int ret = ERROR;
 
   /* The path to the script may relative to the current working directory */
@@ -105,12 +106,12 @@ int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const FAR 
char *cmd,
     {
       /* Save the parent stream in case of nested script processing */
 
-      savestream = vtbl->np.np_stream;
+      savestream = vtbl->np.np_fd;
 
       /* Open the file containing the script */
 
-      vtbl->np.np_stream = fopen(fullpath, "r");
-      if (!vtbl->np.np_stream)
+      vtbl->np.np_fd = open(fullpath, O_RDOK);
+      if (vtbl->np.np_fd < 0)
         {
           if (log)
             {
@@ -123,7 +124,7 @@ int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const FAR 
char *cmd,
 
           /* Restore the parent script stream */
 
-          vtbl->np.np_stream = savestream;
+          vtbl->np.np_fd = savestream;
           return ERROR;
         }
 
@@ -133,10 +134,6 @@ int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const FAR 
char *cmd,
 
       do
         {
-          /* Flush any output generated by the previous line */
-
-          fflush(stdout);
-
 #ifndef CONFIG_NSH_DISABLE_LOOPS
           /* Get the current file position.  This is used to control
            * looping.  If a loop begins in the next line, then this file
@@ -144,7 +141,7 @@ int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const FAR 
char *cmd,
            * script file.  Note that ftell will return -1 on failure.
            */
 
-          vtbl->np.np_foffs = ftell(vtbl->np.np_stream);
+          vtbl->np.np_foffs = lseek(vtbl->np.np_fd, 0, SEEK_CUR);
           vtbl->np.np_loffs = 0;
 
           if (vtbl->np.np_foffs < 0 && log)
@@ -155,8 +152,8 @@ int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const FAR 
char *cmd,
 
           /* Now read the next line from the script file */
 
-          pret = fgets(buffer, CONFIG_NSH_LINELEN, vtbl->np.np_stream);
-          if (pret)
+          ret = readline_fd(buffer, CONFIG_NSH_LINELEN, vtbl->np.np_fd, -1);
+          if (ret >= 0)
             {
               /* Parse process the command.  NOTE:  this is recursive...
                * we got to cmd_source via a call to nsh_parse.  So some
@@ -171,15 +168,15 @@ int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const FAR 
char *cmd,
               ret = nsh_parse(vtbl, buffer);
             }
         }
-      while (pret && (ret == OK || (vtbl->np.np_flags & NSH_PFLAG_IGNORE)));
+      while (ret >= 0 || (vtbl->np.np_flags & NSH_PFLAG_IGNORE));
 
       /* Close the script file */
 
-      fclose(vtbl->np.np_stream);
+      close(vtbl->np.np_fd);
 
       /* Restore the parent script stream */
 
-      vtbl->np.np_stream = savestream;
+      vtbl->np.np_fd = savestream;
     }
 
   /* Free the allocated path */
@@ -265,4 +262,4 @@ int nsh_loginscript(FAR struct nsh_vtbl_s *vtbl)
 #endif
 #endif /* CONFIG_NSH_ROMFSETC */
 
-#endif /* CONFIG_FILE_STREAM && !CONFIG_NSH_DISABLESCRIPT */
+#endif /* CONFIG_NSH_DISABLESCRIPT */
diff --git a/nshlib/nsh_session.c b/nshlib/nsh_session.c
index 5fe67f2db..4e5621552 100644
--- a/nshlib/nsh_session.c
+++ b/nshlib/nsh_session.c
@@ -104,24 +104,21 @@ int nsh_session(FAR struct console_stdio_s *pstate,
     {
       /* Present a greeting and possibly a Message of the Day (MOTD) */
 
-      fputs(g_nshgreeting, pstate->cn_outstream);
+      write(OUTFD(pstate), g_nshgreeting, strlen(g_nshgreeting));
 
 #ifdef CONFIG_NSH_MOTD
 # ifdef CONFIG_NSH_PLATFORM_MOTD
       /* Output the platform message of the day */
 
       platform_motd(vtbl->iobuffer, IOBUFFERSIZE);
-      fprintf(pstate->cn_outstream, "%s\n", vtbl->iobuffer);
-
+      dprintf(OUTFD(pstate), "%s\n", vtbl->iobuffer);
 # else
       /* Output the fixed message of the day */
 
-      fprintf(pstate->cn_outstream, "%s\n", g_nshmotd);
+      dprintf(OUTFD(pstate), "%s\n", g_nshmotd);
 # endif
 #endif
 
-      fflush(pstate->cn_outstream);
-
       /* Execute the login script */
 
 #ifdef CONFIG_NSH_ROMFSRC
@@ -194,34 +191,33 @@ int nsh_session(FAR struct console_stdio_s *pstate,
        * occurs. Either  will cause the session to terminate.
        */
 
-      ret = cle(pstate->cn_line, g_nshprompt, CONFIG_NSH_LINELEN,
-                INSTREAM(pstate), OUTSTREAM(pstate));
+      ret = cle_fd(pstate->cn_line, g_nshprompt, CONFIG_NSH_LINELEN,
+                   INFD(pstate), OUTFD(pstate));
       if (ret < 0)
         {
-          fprintf(pstate->cn_errstream, g_fmtcmdfailed, "nsh_session",
+          dprintf(ERRFD(pstate), g_fmtcmdfailed, "nsh_session",
                   "cle", NSH_ERRNO_OF(-ret));
           continue;
         }
 #else
       /* Display the prompt string */
 
-      fputs(g_nshprompt, pstate->cn_outstream);
-      fflush(pstate->cn_outstream);
+      write(OUTFD(pstate), g_nshprompt, strlen(g_nshprompt));
 
       /* readline() normally returns the number of characters read, but
        * will return EOF on end of file or if an error occurs.  EOF
        * will cause the session to terminate.
        */
 
-      ret = readline(pstate->cn_line, CONFIG_NSH_LINELEN,
-                    INSTREAM(pstate), OUTSTREAM(pstate));
+      ret = readline_fd(pstate->cn_line, CONFIG_NSH_LINELEN,
+                        INFD(pstate), OUTFD(pstate));
       if (ret == EOF)
         {
           /* NOTE: readline() does not set the errno variable, but
            * perhaps we will be lucky and it will still be valid.
            */
 
-          fprintf(pstate->cn_errstream, g_fmtcmdfailed, "nsh_session",
+          dprintf(ERRFD(pstate), g_fmtcmdfailed, "nsh_session",
                   "readline", NSH_ERRNO);
           ret = EXIT_SUCCESS;
           break;
@@ -231,7 +227,6 @@ int nsh_session(FAR struct console_stdio_s *pstate,
       /* Parse process the command */
 
       nsh_parse(vtbl, pstate->cn_line);
-      fflush(pstate->cn_outstream);
     }
 
   return ret;
diff --git a/nshlib/nsh_stdlogin.c b/nshlib/nsh_stdlogin.c
deleted file mode 100644
index 0cadf19cb..000000000
--- a/nshlib/nsh_stdlogin.c
+++ /dev/null
@@ -1,253 +0,0 @@
-/****************************************************************************
- * apps/nshlib/nsh_stdlogin.c
- *
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.  The
- * ASF licenses this file to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the
- * License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
- * License for the specific language governing permissions and limitations
- * under the License.
- *
- ****************************************************************************/
-
-/****************************************************************************
- * Included Files
- ****************************************************************************/
-
-#include <nuttx/config.h>
-
-#include <stdio.h>
-#include <string.h>
-#include <ctype.h>
-#include <unistd.h>
-
-#include "fsutils/passwd.h"
-#ifdef CONFIG_NSH_CLE
-#  include "system/cle.h"
-#else
-#  include "system/readline.h"
-#endif
-
-#include "nsh.h"
-#include "nsh_console.h"
-#include "nshlib/nshlib.h"
-
-#ifdef CONFIG_NSH_CONSOLE_LOGIN
-
-/****************************************************************************
- * Private Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: nsh_stdtoken
- ****************************************************************************/
-
-static void nsh_stdtoken(FAR struct console_stdio_s *pstate,
-                      FAR char *buffer, size_t buflen)
-{
-  FAR char *start;
-  FAR char *endp1;
-  bool quoted = false;
-
-  /* Find the start of token.  Either (1) the first non-white space
-   * character on the command line or (2) the character immediately after
-   * a quotation mark.
-   */
-
-  for (start = pstate->cn_line; *start; start++)
-    {
-      /* Does the token open with a quotation mark */
-
-      if (*start == '"')
-        {
-          /* Yes.. break out with start set to the character after the
-           * quotation mark.
-           */
-
-          quoted = true;
-          start++;
-          break;
-        }
-
-      /* No, then any non-whitespace is the first character of the token */
-
-      else if (!isspace(*start))
-        {
-          /* Break out with start set to the first character of the token */
-
-          break;
-        }
-    }
-
-  /* Find the terminating character after the token on the command line.  The
-   * terminating character is either (1) the matching quotation mark, or (2)
-   * any whitespace.
-   */
-
-  for (endp1 = start; *endp1; endp1++)
-    {
-      /* Did the token begin with a quotation mark? */
-
-      if (quoted)
-        {
-          /* Yes.. then only the matching quotation mark (or end of string)
-           * terminates
-           */
-
-          if (*endp1 == '"')
-            {
-              /* Break out... endp1 points to closing quotation mark */
-
-              break;
-            }
-        }
-
-      /* No.. any whitespace (or end of string) terminates */
-
-      else if (isspace(*endp1))
-        {
-          /* Break out... endp1 points to first while space encountered */
-
-          break;
-        }
-    }
-
-  /* Replace terminating character with a NUL terminator */
-
-  *endp1 = '\0';
-
-  /* Copied the token into the buffer */
-
-  strncpy(buffer, start, buflen);
-}
-
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: nsh_stdlogin
- *
- * Description:
- *   Prompt the user for a username and password.  Return a failure if valid
- *   credentials are not returned (after some retries.
- *
- ****************************************************************************/
-
-int nsh_stdlogin(FAR struct console_stdio_s *pstate)
-{
-  char username[16];
-  char password[128];
-#ifdef CONFIG_NSH_PLATFORM_CHALLENGE
-  char challenge[128];
-#endif
-  int ret;
-  int i;
-
-#ifdef CONFIG_NSH_PLATFORM_SKIP_LOGIN
-  if (platform_skip_login() == OK)
-    {
-      return OK;
-    }
-#endif
-
-  /* Loop for the configured number of retries */
-
-  for (i = 0; i < CONFIG_NSH_LOGIN_FAILCOUNT; i++)
-    {
-      /* Get the response, handling all possible cases */
-
-      username[0] = '\0';
-
-#ifdef CONFIG_NSH_CLE
-      /* cle() returns a negated errno value on failure */
-
-      ret = cle(pstate->cn_line, g_userprompt, CONFIG_NSH_LINELEN,
-                stdin, stdout);
-      if (ret >= 0)
-#else
-      /* Ask for the login username */
-
-      printf("%s", g_userprompt);
-
-      /* readline() returns EOF on failure */
-
-      ret = std_readline(pstate->cn_line, CONFIG_NSH_LINELEN);
-      if (ret != EOF)
-#endif
-        {
-          /* Parse out the username */
-
-          nsh_stdtoken(pstate, username, sizeof(username));
-        }
-
-      if (username[0] == '\0')
-        {
-          i--;
-          continue;
-        }
-
-#ifdef CONFIG_NSH_PLATFORM_CHALLENGE
-      platform_challenge(challenge, sizeof(challenge));
-      printf("%s", challenge);
-#endif
-      /* Ask for the login password */
-
-      printf("%s", g_passwordprompt);
-
-      password[0] = '\0';
-      if (fgets(pstate->cn_line, CONFIG_NSH_LINELEN, stdin) != NULL)
-        {
-          /* Parse out the password */
-
-          nsh_stdtoken(pstate, password, sizeof(password));
-
-          /* Verify the username and password */
-
-#if defined(CONFIG_NSH_LOGIN_PASSWD)
-          ret = passwd_verify(username, password);
-          if (PASSWORD_VERIFY_MATCH(ret))
-
-#elif defined(CONFIG_NSH_LOGIN_PLATFORM)
-#ifdef CONFIG_NSH_PLATFORM_CHALLENGE
-          ret = platform_user_verify(username, challenge, password);
-#else
-          ret = platform_user_verify(username, password);
-#endif
-          if (PASSWORD_VERIFY_MATCH(ret))
-
-#elif defined(CONFIG_NSH_LOGIN_FIXED)
-          if (strcmp(password, CONFIG_NSH_LOGIN_PASSWORD) == 0 &&
-              strcmp(username, CONFIG_NSH_LOGIN_USERNAME) == 0)
-#else
-#  error No user verification method selected
-#endif
-            {
-              printf("%s", g_loginsuccess);
-              return OK;
-            }
-          else
-            {
-              printf("%s", g_badcredentials);
-#if CONFIG_NSH_LOGIN_FAILDELAY > 0
-              usleep(CONFIG_NSH_LOGIN_FAILDELAY * 1000L);
-#endif
-            }
-        }
-    }
-
-  /* Too many failed login attempts */
-
-  printf("%s", g_loginfailure);
-  return -1;
-}
-
-#endif /* CONFIG_NSH_CONSOLE_LOGIN */
diff --git a/nshlib/nsh_stdsession.c b/nshlib/nsh_stdsession.c
deleted file mode 100644
index 17b56e7b5..000000000
--- a/nshlib/nsh_stdsession.c
+++ /dev/null
@@ -1,236 +0,0 @@
-/****************************************************************************
- * apps/nshlib/nsh_stdsession.c
- *
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.  The
- * ASF licenses this file to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the
- * License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
- * License for the specific language governing permissions and limitations
- * under the License.
- *
- ****************************************************************************/
-
-/****************************************************************************
- * Included Files
- ****************************************************************************/
-
-#include <nuttx/config.h>
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-
-#ifdef CONFIG_NSH_CLE
-#  include "system/cle.h"
-#else
-#  include "system/readline.h"
-#endif
-
-#include "nsh.h"
-#include "nsh_console.h"
-
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: nsh_session
- *
- * Description:
- *   This is the common session login on any NSH session that uses only stdin
- *   and stdout.
- *
- *   This function:
- *   - Performs the login sequence if so configured
- *   - Executes the NSH login script
- *   - Presents a greeting
- *   - Then provides a prompt then gets and processes the command line.
- *   - This continues until an error occurs, then the session returns.
- *
- * Input Parameters:
- *   pstate - Abstracts the underlying session.
- *
- * Returned Values:
- *   EXIT_SUCCESS or EXIT_FAILURE is returned.
- *
- ****************************************************************************/
-
-int nsh_session(FAR struct console_stdio_s *pstate,
-                int login, int argc, FAR char *argv[])
-{
-  FAR struct nsh_vtbl_s *vtbl;
-  int ret = EXIT_FAILURE;
-  int i;
-
-  DEBUGASSERT(pstate);
-  vtbl = &pstate->cn_vtbl;
-
-#ifdef CONFIG_NSH_CONSOLE_LOGIN
-  if (login == NSH_LOGIN_LOCAL)
-    {
-      /* Login User and Password Check */
-
-      if (nsh_stdlogin(pstate) != OK)
-        {
-          nsh_exit(vtbl, 1);
-          return -1; /* nsh_exit does not return */
-        }
-    }
-  else
-#endif /* CONFIG_NSH_CONSOLE_LOGIN */
-#ifdef CONFIG_NSH_TELNET_LOGIN
-  if (login == NSH_LOGIN_TELNET)
-    {
-      /* Login User and Password Check */
-
-      if (nsh_telnetlogin(pstate) != OK)
-        {
-          nsh_exit(vtbl, 1);
-          return -1; /* nsh_exit does not return */
-        }
-    }
-#endif /* CONFIG_NSH_TELNET_LOGIN */
-
-  if (login != NSH_LOGIN_NONE)
-    {
-      /* Present a greeting and possibly a Message of the Day (MOTD) */
-
-      printf("%s", g_nshgreeting);
-
-#ifdef CONFIG_NSH_MOTD
-# ifdef CONFIG_NSH_PLATFORM_MOTD
-      /* Output the platform message of the day */
-
-      platform_motd(vtbl->iobuffer, IOBUFFERSIZE);
-      printf("%s\n", vtbl->iobuffer);
-
-# else
-      /* Output the fixed message of the day */
-
-      printf("%s\n", g_nshmotd);
-# endif
-#endif
-
-      /* Execute the login script */
-
-#ifdef CONFIG_NSH_ROMFSRC
-      nsh_loginscript(vtbl);
-#endif
-    }
-
-  /* Process the command line option */
-
-  for (i = 1; i < argc; i++)
-    {
-      if (strcmp(argv[i], "-h") == 0)
-        {
-          nsh_output(vtbl, "Usage: %s [<script-path>|-c <command>]\n",
-                     argv[0]);
-          return EXIT_SUCCESS;
-        }
-      else if (strcmp(argv[i], "-c") == 0)
-        {
-          /* Process the inline command */
-
-          if (i + 1 < argc)
-            {
-              return nsh_parse(vtbl, argv[i + 1]);
-            }
-          else
-            {
-              nsh_error(vtbl, g_fmtargrequired, argv[0]);
-              return EXIT_FAILURE;
-            }
-        }
-      else if (argv[i][0] != '-')
-        {
-          break;
-        }
-
-      /* Unknown option */
-
-      nsh_error(vtbl, g_fmtsyntax, argv[0]);
-      return EXIT_FAILURE;
-    }
-
-  if (i < argc)
-    {
-#ifndef CONFIG_NSH_DISABLESCRIPT
-      /* Execute the shell script */
-
-      return nsh_script(vtbl, argv[0], argv[i], true);
-#else
-      return EXIT_FAILURE;
-#endif
-    }
-
-  /* Then enter the command line parsing loop */
-
-  for (; ; )
-    {
-      /* For the case of debugging the USB console...
-       * dump collected USB trace data
-       */
-
-#ifdef CONFIG_NSH_USBDEV_TRACE
-      nsh_usbtrace();
-#endif
-
-      /* Get the next line of input. readline() returns EOF
-       * on end-of-file or any read failure.
-       */
-
-#ifdef CONFIG_NSH_CLE
-      /* cle() normally returns the number of characters read, but will
-       * return a negated errno value on end of file or if an error
-       * occurs. Either  will cause the session to terminate.
-       */
-
-      ret = cle(pstate->cn_line, g_nshprompt, CONFIG_NSH_LINELEN,
-                stdin, stdout);
-      if (ret < 0)
-        {
-          printf(g_fmtcmdfailed, "nsh_session",
-                 "cle", NSH_ERRNO_OF(-ret));
-          continue;
-        }
-#else
-      /* Display the prompt string */
-
-      printf("%s", g_nshprompt);
-
-      /* readline() normally returns the number of characters read, but
-       * will return EOF on end of file or if an error occurs.  EOF
-       * will cause the session to terminate.
-       */
-
-      ret = std_readline(pstate->cn_line, CONFIG_NSH_LINELEN);
-      if (ret == EOF)
-        {
-          /* NOTE: readline() does not set the errno variable, but
-           * perhaps we will be lucky and it will still be valid.
-           */
-
-          printf(g_fmtcmdfailed, "nsh_session",
-                 "readline", NSH_ERRNO);
-          ret = EXIT_SUCCESS;
-          break;
-        }
-#endif
-
-      /* Parse process the command */
-
-      nsh_parse(vtbl, pstate->cn_line);
-    }
-
-  return ret;
-}
diff --git a/nshlib/nsh_telnetlogin.c b/nshlib/nsh_telnetlogin.c
index c8e6bdad4..6592403b6 100644
--- a/nshlib/nsh_telnetlogin.c
+++ b/nshlib/nsh_telnetlogin.c
@@ -34,6 +34,7 @@
 #include "nsh.h"
 #include "nsh_console.h"
 #include "nshlib/nshlib.h"
+#include "system/readline.h"
 
 #ifdef CONFIG_NSH_TELNET_LOGIN
 
@@ -65,8 +66,7 @@ static void nsh_telnetecho(FAR struct console_stdio_s *pstate,
   optbuf[1] = (is_use == TELNET_USE_ECHO) ? TELNET_WILL : TELNET_DO;
   optbuf[2] = 1;
   optbuf[3] = 0;
-  fputs((FAR char *)optbuf, pstate->cn_outstream);
-  fflush(pstate->cn_outstream);
+  write(OUTFD(pstate), optbuf, strlen((FAR const char *)optbuf));
 }
 
 /****************************************************************************
@@ -178,8 +178,7 @@ int nsh_telnetlogin(FAR struct console_stdio_s *pstate)
 
   /* Present the NSH Telnet greeting */
 
-  fputs(g_telnetgreeting, pstate->cn_outstream);
-  fflush(pstate->cn_outstream);
+  write(OUTFD(pstate), g_telnetgreeting, strlen(g_telnetgreeting));
 
   /* Loop for the configured number of retries */
 
@@ -187,12 +186,12 @@ int nsh_telnetlogin(FAR struct console_stdio_s *pstate)
     {
       /* Ask for the login username */
 
-      fputs(g_userprompt, pstate->cn_outstream);
-      fflush(pstate->cn_outstream);
+      write(OUTFD(pstate), g_userprompt, strlen(g_userprompt));
 
       username[0] = '\0';
-      if (fgets(pstate->cn_line, CONFIG_NSH_LINELEN,
-                INSTREAM(pstate)) != NULL)
+      if (readline_fd(pstate->cn_line, CONFIG_NSH_LINELEN,
+                      INFD(pstate), OUTFD(pstate)) >= 0)
+
         {
           /* Parse out the username */
 
@@ -207,18 +206,17 @@ int nsh_telnetlogin(FAR struct console_stdio_s *pstate)
 
 #ifdef CONFIG_NSH_PLATFORM_CHALLENGE
       platform_challenge(challenge, sizeof(challenge));
-      fputs(challenge, pstate->cn_outstream);
+      write(OUTFD(pstate), challenge, strlen(challenge));
 #endif
 
       /* Ask for the login password */
 
-      fputs(g_passwordprompt, pstate->cn_outstream);
-      fflush(pstate->cn_outstream);
+      write(OUTFD(pstate), g_passwordprompt, strlen(g_passwordprompt));
       nsh_telnetecho(pstate, TELNET_NOTUSE_ECHO);
 
       password[0] = '\0';
-      if (fgets(pstate->cn_line, CONFIG_NSH_LINELEN,
-                INSTREAM(pstate)) != NULL)
+      if (readline_fd(pstate->cn_line, CONFIG_NSH_LINELEN,
+                      INFD(pstate), OUTFD(pstate)) >= 0)
         {
           /* Parse out the password */
 
@@ -244,15 +242,14 @@ int nsh_telnetlogin(FAR struct console_stdio_s *pstate)
 #  error No user verification method selected
 #endif
             {
-              fputs(g_loginsuccess, pstate->cn_outstream);
-              fflush(pstate->cn_outstream);
+              write(OUTFD(pstate), g_loginsuccess, strlen(g_loginsuccess));
               nsh_telnetecho(pstate, TELNET_USE_ECHO);
               return OK;
             }
           else
             {
-              fputs(g_badcredentials, pstate->cn_outstream);
-              fflush(pstate->cn_outstream);
+              write(OUTFD(pstate), g_badcredentials,
+                    strlen(g_badcredentials));
 #if CONFIG_NSH_LOGIN_FAILDELAY > 0
               usleep(CONFIG_NSH_LOGIN_FAILDELAY * 1000L);
 #endif
@@ -264,8 +261,7 @@ int nsh_telnetlogin(FAR struct console_stdio_s *pstate)
 
   /* Too many failed login attempts */
 
-  fputs(g_loginfailure, pstate->cn_outstream);
-  fflush(pstate->cn_outstream);
+  write(OUTFD(pstate), g_loginfailure, strlen(g_loginfailure));
   return -1;
 }
 
diff --git a/nshlib/nsh_timcmds.c b/nshlib/nsh_timcmds.c
index 32d8675a0..19d53ebc1 100644
--- a/nshlib/nsh_timcmds.c
+++ b/nshlib/nsh_timcmds.c
@@ -297,9 +297,7 @@ int cmd_time(FAR struct nsh_vtbl_s *vtbl, int argc, FAR 
char **argv)
 #ifndef CONFIG_NSH_DISABLEBG
   bool bgsave;
 #endif
-#ifdef CONFIG_FILE_STREAM
   bool redirsave;
-#endif
   int ret;
 
   /* Get the current time */
@@ -316,9 +314,7 @@ int cmd_time(FAR struct nsh_vtbl_s *vtbl, int argc, FAR 
char **argv)
 #ifndef CONFIG_NSH_DISABLEBG
   bgsave    = vtbl->np.np_bg;
 #endif
-#ifdef CONFIG_FILE_STREAM
   redirsave = vtbl->np.np_redirect;
-#endif
 
   /* Execute the command */
 
@@ -357,9 +353,7 @@ int cmd_time(FAR struct nsh_vtbl_s *vtbl, int argc, FAR 
char **argv)
 #ifndef CONFIG_NSH_DISABLEBG
   vtbl->np.np_bg       = bgsave;
 #endif
-#ifdef CONFIG_FILE_STREAM
   vtbl->np.np_redirect = redirsave;
-#endif
 
   return ret;
 }
diff --git a/system/nsh/nsh_main.c b/system/nsh/nsh_main.c
index f4f8ab91d..01fcb2a6f 100644
--- a/system/nsh/nsh_main.c
+++ b/system/nsh/nsh_main.c
@@ -76,7 +76,7 @@ int main(int argc, FAR char *argv[])
    * is wrong.
    */
 
-  fprintf(stderr, "ERROR: nsh_consolemain() returned: %d\n", ret);
+  dprintf(STDERR_FILENO, "ERROR: nsh_consolemain() returned: %d\n", ret);
   ret = 1;
 #endif
 

Reply via email to