From 58586f86e3360c9b6a0d525f40f6bb82f44094ef Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Mon, 15 Nov 2021 02:39:35 +0800 Subject: [PATCH] system/cle: Make it work without CONFIG_FILE_STREAM Signed-off-by: Xiang Xiao --- 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 6c0fb2a5d..3e3163522 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 68d62a0e6..d49066f39 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