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 <[email protected]> AuthorDate: Sat Nov 13 22:18:04 2021 +0800 system/readline: Make it work without CONFIG_FILE_STREAM Signed-off-by: Xiang Xiao <[email protected]> --- 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 */
