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 2021/12/01 04:06:11 UTC

[incubator-nuttx-apps] branch master updated (0c21a89 -> 58586f8)

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

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


    from 0c21a89  testing/ostest: remove spurious redefinitions of NULL
     new c843936  system/readline: Make it work without CONFIG_FILE_STREAM
     new 58586f8  system/cle: Make it work without CONFIG_FILE_STREAM

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:
 include/system/cle.h                          |   7 +-
 include/system/readline.h                     |  30 +++--
 system/cle/cle.c                              |  34 +++--
 system/readline/Makefile                      |   2 +-
 system/readline/readline.c                    | 185 +-------------------------
 system/readline/{readline.c => readline_fd.c} |  49 ++-----
 6 files changed, 67 insertions(+), 240 deletions(-)
 copy system/readline/{readline.c => readline_fd.c} (77%)

[incubator-nuttx-apps] 02/02: system/cle: Make it work without CONFIG_FILE_STREAM

Posted by xi...@apache.org.
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

commit 58586f86e3360c9b6a0d525f40f6bb82f44094ef
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Mon Nov 15 02:39:35 2021 +0800

    system/cle: Make it work without CONFIG_FILE_STREAM
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 include/system/cle.h |  7 ++++++-
 system/cle/cle.c     | 34 ++++++++++++++++++++--------------
 2 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/include/system/cle.h b/include/system/cle.h
index 6c0fb2a..3e31635 100644
--- a/include/system/cle.h
+++ b/include/system/cle.h
@@ -53,7 +53,7 @@ extern "C"
  ****************************************************************************/
 
 /****************************************************************************
- * Name: cle
+ * Name: cle/cle_fd
  *
  * Description:
  *   EMACS-like command line editor.  This is actually more like readline
@@ -61,8 +61,13 @@ extern "C"
  *
  ****************************************************************************/
 
+int cle_fd(FAR char *line, FAR const char *prompt, uint16_t linelen,
+           int infd, int outfd);
+
+#ifdef CONFIG_FILE_STREAM
 int cle(FAR char *line, FAR const char *prompt, uint16_t linelen,
         FAR FILE *instream, FAR FILE *outstream);
+#endif
 
 #undef EXTERN
 #ifdef __cplusplus
diff --git a/system/cle/cle.c b/system/cle/cle.c
index 68d62a0..d49066f 100644
--- a/system/cle/cle.c
+++ b/system/cle/cle.c
@@ -142,8 +142,8 @@ struct cle_s
   uint16_t coloffs;         /* Left cursor offset */
   uint16_t linelen;         /* Size of the line buffer */
   uint16_t nchars;          /* Size of data in the line buffer */
-  FAR FILE *ins;            /* Input file stream */
-  FAR FILE *outs;           /* Output file stream */
+  int infd;                 /* Input file handle */
+  int outfd;                /* Output file handle */
   FAR char *line;           /* Line buffer */
   FAR const char *prompt;   /* Prompt, in case we have to re-print it */
 };
@@ -257,7 +257,6 @@ static void cle_write(FAR struct cle_s *priv, FAR const char *buffer,
                       uint16_t buflen)
 {
   ssize_t nwritten;
-  uint16_t  nremaining = buflen;
 
   /* Loop until all bytes have been successfully written (or until a
    * unrecoverable error is encountered)
@@ -267,7 +266,7 @@ static void cle_write(FAR struct cle_s *priv, FAR const char *buffer,
     {
       /* Put the next gulp */
 
-      nwritten = fwrite(buffer, sizeof(char), buflen, priv->outs);
+      nwritten = write(priv->outfd, buffer, buflen);
 
       /* Handle write errors.  write() should neve return 0. */
 
@@ -291,12 +290,11 @@ static void cle_write(FAR struct cle_s *priv, FAR const char *buffer,
 
       else
         {
-          nremaining -= nwritten;
+          buffer += nwritten;
+          buflen -= nwritten;
         }
     }
-  while (nremaining > 0);
-
-  fflush(priv->outs);
+  while (buflen > 0);
 }
 
 /****************************************************************************
@@ -333,7 +331,7 @@ static int cle_getch(FAR struct cle_s *priv)
     {
       /* Read one character from the incoming stream */
 
-      nread = fread (&buffer, sizeof(char), 1, priv->ins);
+      nread = read(priv->infd, &buffer, 1);
 
       /* Check for error or end-of-file. */
 
@@ -1135,7 +1133,7 @@ static int cle_editloop(FAR struct cle_s *priv)
  ****************************************************************************/
 
 /****************************************************************************
- * Name: cle
+ * Name: cle/cle_fd
  *
  * Description:
  *   EMACS-like command line editor.  This is actually more like readline
@@ -1143,8 +1141,8 @@ static int cle_editloop(FAR struct cle_s *priv)
  *
  ****************************************************************************/
 
-int cle(FAR char *line, const char *prompt, uint16_t linelen,
-        FILE *instream, FILE *outstream)
+int cle_fd(FAR char *line, FAR const char *prompt, uint16_t linelen,
+           int infd, int outfd)
 {
   FAR struct cle_s priv;
   uint16_t column;
@@ -1157,8 +1155,8 @@ int cle(FAR char *line, const char *prompt, uint16_t linelen,
   priv.linelen  = linelen;
   priv.line     = line;
 
-  priv.ins      = instream;
-  priv.outs     = outstream;
+  priv.infd     = infd;
+  priv.outfd    = outfd;
 
   /* Store the prompt in case we need to re-print it */
 
@@ -1225,3 +1223,11 @@ int cle(FAR char *line, const char *prompt, uint16_t linelen,
 
   return ret;
 }
+
+#ifdef CONFIG_FILE_STREAM
+int cle(FAR char *line, FAR const char *prompt, uint16_t linelen,
+        FAR FILE *instream, FAR FILE *outstream)
+{
+  return cle_fd(line, prompt, linelen, instream->fs_fd, outstream->fs_fd);
+}
+#endif

[incubator-nuttx-apps] 01/02: system/readline: Make it work without CONFIG_FILE_STREAM

Posted by xi...@apache.org.
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

commit c8439368ac3af3852118ed212f587335eca36743
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sat Nov 13 22:18:04 2021 +0800

    system/readline: Make it work without CONFIG_FILE_STREAM
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 include/system/readline.h                     |  30 +++--
 system/readline/Makefile                      |   2 +-
 system/readline/readline.c                    | 185 +-------------------------
 system/readline/{readline.c => readline_fd.c} |  49 ++-----
 4 files changed, 41 insertions(+), 225 deletions(-)

diff --git a/include/system/readline.h b/include/system/readline.h
index f99c891..7db0f89 100644
--- a/include/system/readline.h
+++ b/include/system/readline.h
@@ -143,26 +143,26 @@ FAR const struct extmatch_vtable_s *
 #endif
 
 /****************************************************************************
- * Name: readline
+ * Name: readline_fd
  *
- *   readline() reads in at most one less than 'buflen' characters from
- *   'instream' and stores them into the buffer pointed to by 'buf'.
- *   Characters are echoed on 'outstream'.  Reading stops after an EOF or a
+ *   readline_fd() reads in at most one less than 'buflen' characters from
+ *   'infd' and stores them into the buffer pointed to by 'buf'.
+ *   Characters are echoed on 'outfd'.  Reading stops after an EOF or a
  *   newline.  If a newline is read, it is stored into the buffer.  A null
  *   terminator is stored after the last character in the buffer.
  *
- *   This version of realine assumes that we are reading and writing to
- *   a VT100 console.  This will not work well if 'instream' or 'outstream'
+ *   This version of readline_fd assumes that we are reading and writing to
+ *   a VT100 console.  This will not work well if 'infd' or 'outfd'
  *   corresponds to a raw byte steam.
  *
  *   This function is inspired by the GNU readline but is an entirely
  *   different creature.
  *
  * Input Parameters:
- *   buf       - The user allocated buffer to be filled.
- *   buflen    - the size of the buffer.
- *   instream  - The stream to read characters from
- *   outstream - The stream to each characters to.
+ *   buf    - The user allocated buffer to be filled.
+ *   buflen - the size of the buffer.
+ *   infd   - The file to read characters from
+ *   outfd  - The file to each characters to.
  *
  * Returned values:
  *   On success, the (positive) number of bytes transferred is returned.
@@ -171,6 +171,16 @@ FAR const struct extmatch_vtable_s *
  *
  ****************************************************************************/
 
+ssize_t readline_fd(FAR char *buf, int buflen, int infd, int outfd);
+
+/****************************************************************************
+ * Name: readline
+ *
+ *   readline() is same to readline_fd() but accept a file stream instead
+ *   of a file handle.
+ *
+ ****************************************************************************/
+
 #ifdef CONFIG_FILE_STREAM
 ssize_t readline(FAR char *buf, int buflen, FILE *instream, FILE *outstream);
 #endif
diff --git a/system/readline/Makefile b/system/readline/Makefile
index 7d457f7..2e9af64 100644
--- a/system/readline/Makefile
+++ b/system/readline/Makefile
@@ -22,6 +22,6 @@ include $(APPDIR)/Make.defs
 
 # The Readline Library
 
-CSRCS = readline.c readline_common.c
+CSRCS = readline.c readline_fd.c readline_common.c
 
 include $(APPDIR)/Application.mk
diff --git a/system/readline/readline.c b/system/readline/readline.c
index 47b345d..8f263c2 100644
--- a/system/readline/readline.c
+++ b/system/readline/readline.c
@@ -24,151 +24,10 @@
 
 #include <nuttx/config.h>
 
-#include <sys/types.h>
 #include <stdio.h>
-#include <unistd.h>
-#include <errno.h>
 #include <assert.h>
 
 #include "system/readline.h"
-#include "readline.h"
-
-/****************************************************************************
- * Pre-processor Definitions
- ****************************************************************************/
-
-/****************************************************************************
- * Private Type Declarations
- ****************************************************************************/
-
-struct readline_s
-{
-  struct rl_common_s vtbl;
-  int infd;
-#ifdef CONFIG_READLINE_ECHO
-  int outfd;
-#endif
-};
-
-/****************************************************************************
- * Private Function Prototypes
- ****************************************************************************/
-
-/****************************************************************************
- * Public Data
- ****************************************************************************/
-
-/****************************************************************************
- * Private Data
- ****************************************************************************/
-
-/****************************************************************************
- * Private Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: readline_getc
- ****************************************************************************/
-
-static int readline_getc(FAR struct rl_common_s *vtbl)
-{
-  FAR struct readline_s *priv = (FAR struct readline_s *)vtbl;
-  char buffer;
-  ssize_t nread;
-
-  DEBUGASSERT(priv);
-
-  /* Loop until we successfully read a character (or until an unexpected
-   * error occurs).
-   */
-
-  do
-    {
-      /* Read one character from the incoming stream */
-
-      nread = read(priv->infd, &buffer, 1);
-
-      /* Check for end-of-file. */
-
-      if (nread == 0)
-        {
-          /* Return EOF on end-of-file */
-
-          return EOF;
-        }
-
-      /* Check if an error occurred */
-
-      else if (nread < 0)
-        {
-          /* EINTR is not really an error; it simply means that a signal was
-           * received while waiting for input.
-           */
-
-          int errcode = errno;
-          if (errcode != EINTR)
-            {
-              /* Return EOF on any errors that we cannot handle */
-
-              return EOF;
-            }
-        }
-    }
-  while (nread < 1);
-
-  /* On success, return the character that was read */
-
-  return (int)buffer;
-}
-
-/****************************************************************************
- * Name: readline_putc
- ****************************************************************************/
-
-#ifdef CONFIG_READLINE_ECHO
-static void readline_putc(FAR struct rl_common_s *vtbl, int ch)
-{
-  FAR struct readline_s *priv = (FAR struct readline_s *)vtbl;
-  char buffer = ch;
-  ssize_t nwritten;
-
-  DEBUGASSERT(priv);
-
-  /* Loop until we successfully write a character (or until an unexpected
-   * error occurs).
-   */
-
-  do
-    {
-      /* Write the character to the outgoing stream */
-
-      nwritten = write(priv->outfd, &buffer, 1);
-
-      /* Check for irrecoverable write errors. */
-
-      if (nwritten < 0 && errno != EINTR)
-        {
-          break;
-        }
-    }
-  while (nwritten < 1);
-}
-#endif
-
-/****************************************************************************
- * Name: readline_write
- ****************************************************************************/
-
-#ifdef CONFIG_READLINE_ECHO
-static void readline_write(FAR struct rl_common_s *vtbl,
-                           FAR const char *buffer, size_t buflen)
-{
-  FAR struct readline_s *priv = (FAR struct readline_s *)vtbl;
-  DEBUGASSERT(priv && buffer && buflen > 0);
-
-  write(priv->outfd, buffer, buflen);
-}
-#endif
 
 /****************************************************************************
  * Public Functions
@@ -177,52 +36,20 @@ static void readline_write(FAR struct rl_common_s *vtbl,
 /****************************************************************************
  * Name: readline
  *
- *   readline() reads in at most one less than 'buflen' characters from
- *   'instream' and stores them into the buffer pointed to by 'buf'.
- *   Characters are echoed on 'outstream'.  Reading stops after an EOF or a
- *   newline.  If a newline is read, it is stored into the buffer.  A null
- *   terminator is stored after the last character in the buffer.
- *
- *   This version of realine assumes that we are reading and writing to
- *   a VT100 console.  This will not work well if 'instream' or 'outstream'
- *   corresponds to a raw byte steam.
- *
- *   This function is inspired by the GNU readline but is an entirely
- *   different creature.
- *
- * Input Parameters:
- *   buf       - The user allocated buffer to be filled.
- *   buflen    - the size of the buffer.
- *   instream  - The stream to read characters from
- *   outstream - The stream to each characters to.
- *
- * Returned values:
- *   On success, the (positive) number of bytes transferred is returned.
- *   EOF is returned to indicate either an end of file condition or a
- *   failure.
+ *   readline() is same to readline_fd() but accept a file stream instead
+ *   of a file handle.
  *
  ****************************************************************************/
 
+#ifdef CONFIG_FILE_STREAM
 ssize_t readline(FAR char *buf, int buflen, FILE *instream, FILE *outstream)
 {
-  struct readline_s vtbl;
-
   /* Sanity checks */
 
   DEBUGASSERT(instream && outstream);
 
-  /* Set up the vtbl structure */
-
-  vtbl.vtbl.rl_getc  = readline_getc;
-  vtbl.infd          = instream->fs_fd;
+  /* Let readline_fd do the work */
 
-#ifdef CONFIG_READLINE_ECHO
-  vtbl.vtbl.rl_putc  = readline_putc;
-  vtbl.vtbl.rl_write = readline_write;
-  vtbl.outfd         = outstream->fs_fd;
-#endif
-
-  /* The let the common readline logic do the work */
-
-  return readline_common(&vtbl.vtbl, buf, buflen);
+  return readline_fd(buf, buflen, instream->fs_fd, outstream->fs_fd);
 }
+#endif
diff --git a/system/readline/readline.c b/system/readline/readline_fd.c
similarity index 77%
copy from system/readline/readline.c
copy to system/readline/readline_fd.c
index 47b345d..6d134f0 100644
--- a/system/readline/readline.c
+++ b/system/readline/readline_fd.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * apps/system/readline/readline.c
+ * apps/system/readline/readline_fd.c
  *
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -25,7 +25,6 @@
 #include <nuttx/config.h>
 
 #include <sys/types.h>
-#include <stdio.h>
 #include <unistd.h>
 #include <errno.h>
 #include <assert.h>
@@ -34,10 +33,6 @@
 #include "readline.h"
 
 /****************************************************************************
- * Pre-processor Definitions
- ****************************************************************************/
-
-/****************************************************************************
  * Private Type Declarations
  ****************************************************************************/
 
@@ -51,18 +46,6 @@ struct readline_s
 };
 
 /****************************************************************************
- * Private Function Prototypes
- ****************************************************************************/
-
-/****************************************************************************
- * Public Data
- ****************************************************************************/
-
-/****************************************************************************
- * Private Data
- ****************************************************************************/
-
-/****************************************************************************
  * Private Functions
  ****************************************************************************/
 
@@ -175,26 +158,26 @@ static void readline_write(FAR struct rl_common_s *vtbl,
  ****************************************************************************/
 
 /****************************************************************************
- * Name: readline
+ * Name: readline_fd
  *
- *   readline() reads in at most one less than 'buflen' characters from
- *   'instream' and stores them into the buffer pointed to by 'buf'.
- *   Characters are echoed on 'outstream'.  Reading stops after an EOF or a
+ *   readline_fd() reads in at most one less than 'buflen' characters from
+ *   'infd' and stores them into the buffer pointed to by 'buf'.
+ *   Characters are echoed on 'outfd'.  Reading stops after an EOF or a
  *   newline.  If a newline is read, it is stored into the buffer.  A null
  *   terminator is stored after the last character in the buffer.
  *
- *   This version of realine assumes that we are reading and writing to
- *   a VT100 console.  This will not work well if 'instream' or 'outstream'
+ *   This version of readline_fd assumes that we are reading and writing to
+ *   a VT100 console.  This will not work well if 'infd' or 'outfd'
  *   corresponds to a raw byte steam.
  *
  *   This function is inspired by the GNU readline but is an entirely
  *   different creature.
  *
  * Input Parameters:
- *   buf       - The user allocated buffer to be filled.
- *   buflen    - the size of the buffer.
- *   instream  - The stream to read characters from
- *   outstream - The stream to each characters to.
+ *   buf    - The user allocated buffer to be filled.
+ *   buflen - the size of the buffer.
+ *   infd   - The file to read characters from
+ *   outfd  - The file to each characters to.
  *
  * Returned values:
  *   On success, the (positive) number of bytes transferred is returned.
@@ -203,23 +186,19 @@ static void readline_write(FAR struct rl_common_s *vtbl,
  *
  ****************************************************************************/
 
-ssize_t readline(FAR char *buf, int buflen, FILE *instream, FILE *outstream)
+ssize_t readline_fd(FAR char *buf, int buflen, int infd, int outfd)
 {
   struct readline_s vtbl;
 
-  /* Sanity checks */
-
-  DEBUGASSERT(instream && outstream);
-
   /* Set up the vtbl structure */
 
   vtbl.vtbl.rl_getc  = readline_getc;
-  vtbl.infd          = instream->fs_fd;
+  vtbl.infd          = infd;
 
 #ifdef CONFIG_READLINE_ECHO
   vtbl.vtbl.rl_putc  = readline_putc;
   vtbl.vtbl.rl_write = readline_write;
-  vtbl.outfd         = outstream->fs_fd;
+  vtbl.outfd         = outfd;
 #endif
 
   /* The let the common readline logic do the work */