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 929f1009c system/readline: correct the readline(3) prototype 929f1009c is described below commit 929f1009c73a5bad78c7acb44f3fb48cfd8ab153 Author: chao an <anc...@xiaomi.com> AuthorDate: Fri Oct 13 18:42:20 2023 +0800 system/readline: correct the readline(3) prototype Reference: https://man.openbsd.org/readline.3 Signed-off-by: chao an <anc...@xiaomi.com> --- examples/ftpc/ftpc_main.c | 3 +- examples/lp503x/lp503x_main.c | 3 +- examples/nrf24l01_term/nrf24l01_term.c | 3 +- include/system/readline.h | 24 ++++++++--- interpreters/lua/Makefile | 2 +- system/nxcamera/nxcamera_main.c | 3 +- system/nxlooper/nxlooper_main.c | 3 +- system/nxplayer/nxplayer_main.c | 3 +- system/nxrecorder/nxrecorder_main.c | 3 +- system/readline/CMakeLists.txt | 3 +- system/readline/Makefile | 2 +- system/readline/readline.c | 50 +++++++++++++++-------- system/readline/{readline.c => readline_stream.c} | 11 ++--- testing/irtest/main.cxx | 2 +- 14 files changed, 78 insertions(+), 37 deletions(-) diff --git a/examples/ftpc/ftpc_main.c b/examples/ftpc/ftpc_main.c index 07a3d9efa..ba57e17be 100644 --- a/examples/ftpc/ftpc_main.c +++ b/examples/ftpc/ftpc_main.c @@ -560,7 +560,8 @@ do_connect: return 1; } #else - ret = readline(g_line, CONFIG_FTPC_LINELEN, stdin, stdout); + ret = readline_stream(g_line, CONFIG_FTPC_LINELEN, + stdin, stdout); /* Readline normally returns the number of characters read, * but will return EOF on end of file or if an error occurs. diff --git a/examples/lp503x/lp503x_main.c b/examples/lp503x/lp503x_main.c index d3c5430a4..644305248 100644 --- a/examples/lp503x/lp503x_main.c +++ b/examples/lp503x/lp503x_main.c @@ -647,7 +647,8 @@ int main(int argc, FAR char *argv[]) /* read a line from the terminal */ - len = readline(buffer, sizeof(buffer), stdin, stdout); + len = readline_stream(buffer, sizeof(buffer), + stdin, stdout); buffer[len] = '\0'; if (len > 0) { diff --git a/examples/nrf24l01_term/nrf24l01_term.c b/examples/nrf24l01_term/nrf24l01_term.c index 35754e110..91cc766df 100644 --- a/examples/nrf24l01_term/nrf24l01_term.c +++ b/examples/nrf24l01_term/nrf24l01_term.c @@ -320,7 +320,8 @@ int main(int argc, FAR char *argv[]) goto out; } #else - ret = readline(&buff[1], sizeof(buff) - 1, stdin, stdout); + ret = readline_stream(&buff[1], sizeof(buff) - 1, + stdin, stdout); /* Readline normally returns the number of characters read, * but will return EOF on end of file or if an error occurs. diff --git a/include/system/readline.h b/include/system/readline.h index 7db0f89eb..ad0872a82 100644 --- a/include/system/readline.h +++ b/include/system/readline.h @@ -174,17 +174,31 @@ FAR const struct extmatch_vtable_s * ssize_t readline_fd(FAR char *buf, int buflen, int infd, int outfd); /**************************************************************************** - * Name: readline + * Name: readline_stream * - * readline() is same to readline_fd() but accept a file stream instead - * of a file handle. + * readline_stream() 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); +ssize_t readline_stream(FAR char *buf, int buflen, + FAR FILE *instream, FAR FILE *outstream); #endif +/**************************************************************************** + * Name: readline + * + * readline will read a line from the terminal and return it, using + * prompt as a prompt. If prompt is NULL or the empty string, no prompt + * is issued. The line returned is allocated with malloc(3); + * the caller must free it when finished. The line re‐turned has the + * final newline removed, so only the text of the line remains. + * + ****************************************************************************/ + +FAR char *readline(FAR const char *prompt); + /**************************************************************************** * Name: std_readline * @@ -212,7 +226,7 @@ ssize_t readline(FAR char *buf, int buflen, FILE *instream, FILE *outstream); * ****************************************************************************/ -#define std_readline(b,s) readline(b,s,stdin,stdout) +#define std_readline(b,s) readline_stream(b,s,stdin,stdout) #undef EXTERN #ifdef __cplusplus diff --git a/interpreters/lua/Makefile b/interpreters/lua/Makefile index fe2e13453..6f9dbfcee 100644 --- a/interpreters/lua/Makefile +++ b/interpreters/lua/Makefile @@ -60,7 +60,7 @@ endif ifeq ($(CONFIG_SYSTEM_READLINE),y) CFLAGS += -include "system/readline.h" CFLAGS += -D'lua_initreadline(L)=((void)L)' -CFLAGS += -D'lua_readline(L,b,p)=((void)L,fputs(p,stdout),fflush(stdout),readline(b,LUA_MAXINPUT,stdin,stdout))' +CFLAGS += -D'lua_readline(L,b,p)=((void)L,fputs(p,stdout),fflush(stdout),readline_stream(b,LUA_MAXINPUT,stdin,stdout))' CFLAGS += -D'lua_saveline(L,line)={(void)L;(void)line;}' CFLAGS += -D'lua_freeline(L,line)={(void)L;(void)b;}' endif diff --git a/system/nxcamera/nxcamera_main.c b/system/nxcamera/nxcamera_main.c index 5ca84da11..e834932f4 100644 --- a/system/nxcamera/nxcamera_main.c +++ b/system/nxcamera/nxcamera_main.c @@ -431,7 +431,8 @@ int main(int argc, FAR char *argv[]) /* Read a line from the terminal */ - len = readline(buffer, sizeof(buffer), stdin, stdout); + len = readline_stream(buffer, sizeof(buffer), + stdin, stdout); if (len > 0) { buffer[len] = '\0'; diff --git a/system/nxlooper/nxlooper_main.c b/system/nxlooper/nxlooper_main.c index 4fb0f34ce..39be125ed 100644 --- a/system/nxlooper/nxlooper_main.c +++ b/system/nxlooper/nxlooper_main.c @@ -533,7 +533,8 @@ int main(int argc, FAR char *argv[]) /* Read a line from the terminal */ - len = readline(buffer, sizeof(buffer), stdin, stdout); + len = readline_stream(buffer, sizeof(buffer), + stdin, stdout); if (len > 0) { buffer[len] = '\0'; diff --git a/system/nxplayer/nxplayer_main.c b/system/nxplayer/nxplayer_main.c index c1f7f9dc0..dc9d60152 100644 --- a/system/nxplayer/nxplayer_main.c +++ b/system/nxplayer/nxplayer_main.c @@ -770,7 +770,8 @@ int main(int argc, FAR char *argv[]) /* Read a line from the terminal */ - len = readline(buffer, sizeof(buffer), stdin, stdout); + len = readline_stream(buffer, sizeof(buffer), + stdin, stdout); if (len > 0) { buffer[len] = '\0'; diff --git a/system/nxrecorder/nxrecorder_main.c b/system/nxrecorder/nxrecorder_main.c index c1c21fd50..bdfc2c3eb 100644 --- a/system/nxrecorder/nxrecorder_main.c +++ b/system/nxrecorder/nxrecorder_main.c @@ -562,7 +562,8 @@ int main(int argc, FAR char *argv[]) /* Read a line from the terminal */ - len = readline(buffer, sizeof(buffer), stdin, stdout); + len = readline_stream(buffer, sizeof(buffer), + stdin, stdout); if (len > 0) { buffer[len] = '\0'; diff --git a/system/readline/CMakeLists.txt b/system/readline/CMakeLists.txt index f7d05b3e8..e298f77ab 100644 --- a/system/readline/CMakeLists.txt +++ b/system/readline/CMakeLists.txt @@ -20,6 +20,7 @@ if(CONFIG_SYSTEM_READLINE) - target_sources(apps PRIVATE readline.c readline_fd.c readline_common.c) + target_sources(apps PRIVATE readline.c readline_fd.c readline_stream.c + readline_common.c) endif() diff --git a/system/readline/Makefile b/system/readline/Makefile index 2e9af64bd..d14e4f003 100644 --- a/system/readline/Makefile +++ b/system/readline/Makefile @@ -22,6 +22,6 @@ include $(APPDIR)/Make.defs # The Readline Library -CSRCS = readline.c readline_fd.c readline_common.c +CSRCS = readline.c readline_fd.c readline_stream.c readline_common.c include $(APPDIR)/Application.mk diff --git a/system/readline/readline.c b/system/readline/readline.c index cad9999e1..dc675deb7 100644 --- a/system/readline/readline.c +++ b/system/readline/readline.c @@ -25,10 +25,20 @@ #include <nuttx/config.h> #include <stdio.h> -#include <assert.h> +#include <stdlib.h> #include "system/readline.h" +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Maximum size of one command line (telnet or serial) */ + +#ifndef CONFIG_NSH_LINELEN +# define CONFIG_NSH_LINELEN 80 +#endif + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -36,26 +46,34 @@ /**************************************************************************** * Name: readline * - * readline() is same to readline_fd() but accept a file stream instead - * of a file handle. + * readline will read a line from the terminal and return it, using + * prompt as a prompt. If prompt is NULL or the empty string, no prompt + * is issued. The line returned is allocated with malloc(3); + * the caller must free it when finished. The line re‐turned has the + * final newline removed, so only the text of the line remains. * ****************************************************************************/ -#ifdef CONFIG_FILE_STREAM -ssize_t readline(FAR char *buf, int buflen, FILE *instream, FILE *outstream) +FAR char *readline(FAR const char *prompt) { - int instream_fd; - int outstream_fd; + FAR char *line = malloc(CONFIG_NSH_LINELEN); - /* Sanity checks */ - - DEBUGASSERT(instream && outstream); - - /* Let readline_fd do the work */ + if (line != NULL) + { +#ifdef CONFIG_READLINE_TABCOMPLETION + FAR const char *orig = readline_prompt(prompt); +#endif + if (readline_fd(line, CONFIG_NSH_LINELEN, + STDIN_FILENO, STDOUT_FILENO) == 0) + { + free(line); + line = NULL; + } - instream_fd = fileno(instream); - outstream_fd = fileno(outstream); +#ifdef CONFIG_READLINE_TABCOMPLETION + readline_prompt(orig); +#endif + } - return readline_fd(buf, buflen, instream_fd, outstream_fd); + return line; } -#endif diff --git a/system/readline/readline.c b/system/readline/readline_stream.c similarity index 86% copy from system/readline/readline.c copy to system/readline/readline_stream.c index cad9999e1..e2f4aeab9 100644 --- a/system/readline/readline.c +++ b/system/readline/readline_stream.c @@ -1,5 +1,5 @@ /**************************************************************************** - * apps/system/readline/readline.c + * apps/system/readline/readline_stream.c * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -34,15 +34,16 @@ ****************************************************************************/ /**************************************************************************** - * Name: readline + * Name: readline_stream * - * readline() is same to readline_fd() but accept a file stream instead - * of a file handle. + * readline_stream() 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) +ssize_t readline_stream(FAR char *buf, int buflen, + FAR FILE *instream, FAR FILE *outstream) { int instream_fd; int outstream_fd; diff --git a/testing/irtest/main.cxx b/testing/irtest/main.cxx index 7c3452c32..521110fd4 100644 --- a/testing/irtest/main.cxx +++ b/testing/irtest/main.cxx @@ -35,7 +35,7 @@ static ssize_t prompt(const char *p, char *buf, size_t len) { fputs(p, stdout); fflush(stdout); - return readline(buf, len, stdin, stdout); + return readline_stream(buf, len, stdin, stdout); } /****************************************************************************