system/readline: correct the readline(3) prototype

Reference:
https://man.openbsd.org/readline.3

Signed-off-by: chao an <anchao@xiaomi.com>
This commit is contained in:
chao an 2023-10-13 18:42:20 +08:00 committed by Xiang Xiao
parent 6a814dd41f
commit 929f1009c7
14 changed files with 136 additions and 34 deletions

View File

@ -560,7 +560,8 @@ do_connect:
return 1; return 1;
} }
#else #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, /* Readline normally returns the number of characters read,
* but will return EOF on end of file or if an error occurs. * but will return EOF on end of file or if an error occurs.

View File

@ -647,7 +647,8 @@ int main(int argc, FAR char *argv[])
/* read a line from the terminal */ /* read a line from the terminal */
len = readline(buffer, sizeof(buffer), stdin, stdout); len = readline_stream(buffer, sizeof(buffer),
stdin, stdout);
buffer[len] = '\0'; buffer[len] = '\0';
if (len > 0) if (len > 0)
{ {

View File

@ -320,7 +320,8 @@ int main(int argc, FAR char *argv[])
goto out; goto out;
} }
#else #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, /* Readline normally returns the number of characters read,
* but will return EOF on end of file or if an error occurs. * but will return EOF on end of file or if an error occurs.

View File

@ -174,17 +174,31 @@ FAR const struct extmatch_vtable_s *
ssize_t readline_fd(FAR char *buf, int buflen, int infd, int outfd); 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 * readline_stream() is same to readline_fd() but accept a file stream
* of a file handle. * instead of a file handle.
* *
****************************************************************************/ ****************************************************************************/
#ifdef CONFIG_FILE_STREAM #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 #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 returned has the
* final newline removed, so only the text of the line remains.
*
****************************************************************************/
FAR char *readline(FAR const char *prompt);
/**************************************************************************** /****************************************************************************
* Name: std_readline * 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 #undef EXTERN
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -60,7 +60,7 @@ endif
ifeq ($(CONFIG_SYSTEM_READLINE),y) ifeq ($(CONFIG_SYSTEM_READLINE),y)
CFLAGS += -include "system/readline.h" CFLAGS += -include "system/readline.h"
CFLAGS += -D'lua_initreadline(L)=((void)L)' 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_saveline(L,line)={(void)L;(void)line;}'
CFLAGS += -D'lua_freeline(L,line)={(void)L;(void)b;}' CFLAGS += -D'lua_freeline(L,line)={(void)L;(void)b;}'
endif endif

View File

@ -431,7 +431,8 @@ int main(int argc, FAR char *argv[])
/* Read a line from the terminal */ /* Read a line from the terminal */
len = readline(buffer, sizeof(buffer), stdin, stdout); len = readline_stream(buffer, sizeof(buffer),
stdin, stdout);
if (len > 0) if (len > 0)
{ {
buffer[len] = '\0'; buffer[len] = '\0';

View File

@ -533,7 +533,8 @@ int main(int argc, FAR char *argv[])
/* Read a line from the terminal */ /* Read a line from the terminal */
len = readline(buffer, sizeof(buffer), stdin, stdout); len = readline_stream(buffer, sizeof(buffer),
stdin, stdout);
if (len > 0) if (len > 0)
{ {
buffer[len] = '\0'; buffer[len] = '\0';

View File

@ -770,7 +770,8 @@ int main(int argc, FAR char *argv[])
/* Read a line from the terminal */ /* Read a line from the terminal */
len = readline(buffer, sizeof(buffer), stdin, stdout); len = readline_stream(buffer, sizeof(buffer),
stdin, stdout);
if (len > 0) if (len > 0)
{ {
buffer[len] = '\0'; buffer[len] = '\0';

View File

@ -562,7 +562,8 @@ int main(int argc, FAR char *argv[])
/* Read a line from the terminal */ /* Read a line from the terminal */
len = readline(buffer, sizeof(buffer), stdin, stdout); len = readline_stream(buffer, sizeof(buffer),
stdin, stdout);
if (len > 0) if (len > 0)
{ {
buffer[len] = '\0'; buffer[len] = '\0';

View File

@ -20,6 +20,7 @@
if(CONFIG_SYSTEM_READLINE) 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() endif()

View File

@ -22,6 +22,6 @@ include $(APPDIR)/Make.defs
# The Readline Library # 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 include $(APPDIR)/Application.mk

View File

@ -25,10 +25,20 @@
#include <nuttx/config.h> #include <nuttx/config.h>
#include <stdio.h> #include <stdio.h>
#include <assert.h> #include <stdlib.h>
#include "system/readline.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 * Public Functions
****************************************************************************/ ****************************************************************************/
@ -36,26 +46,34 @@
/**************************************************************************** /****************************************************************************
* Name: readline * Name: readline
* *
* readline() is same to readline_fd() but accept a file stream instead * readline will read a line from the terminal and return it, using
* of a file handle. * 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 returned has the
* final newline removed, so only the text of the line remains.
* *
****************************************************************************/ ****************************************************************************/
#ifdef CONFIG_FILE_STREAM FAR char *readline(FAR const char *prompt)
ssize_t readline(FAR char *buf, int buflen, FILE *instream, FILE *outstream)
{ {
int instream_fd; FAR char *line = malloc(CONFIG_NSH_LINELEN);
int outstream_fd;
/* Sanity checks */ if (line != NULL)
{
DEBUGASSERT(instream && outstream); #ifdef CONFIG_READLINE_TABCOMPLETION
FAR const char *orig = readline_prompt(prompt);
/* Let readline_fd do the work */
instream_fd = fileno(instream);
outstream_fd = fileno(outstream);
return readline_fd(buf, buflen, instream_fd, outstream_fd);
}
#endif #endif
if (readline_fd(line, CONFIG_NSH_LINELEN,
STDIN_FILENO, STDOUT_FILENO) == 0)
{
free(line);
line = NULL;
}
#ifdef CONFIG_READLINE_TABCOMPLETION
readline_prompt(orig);
#endif
}
return line;
}

View File

@ -0,0 +1,62 @@
/****************************************************************************
* 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
* 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 <assert.h>
#include "system/readline.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: readline_stream
*
* readline_stream() is same to readline_fd() but accept a file stream
* instead of a file handle.
*
****************************************************************************/
#ifdef CONFIG_FILE_STREAM
ssize_t readline_stream(FAR char *buf, int buflen,
FAR FILE *instream, FAR FILE *outstream)
{
int instream_fd;
int outstream_fd;
/* Sanity checks */
DEBUGASSERT(instream && outstream);
/* Let readline_fd do the work */
instream_fd = fileno(instream);
outstream_fd = fileno(outstream);
return readline_fd(buf, buflen, instream_fd, outstream_fd);
}
#endif

View File

@ -35,7 +35,7 @@ static ssize_t prompt(const char *p, char *buf, size_t len)
{ {
fputs(p, stdout); fputs(p, stdout);
fflush(stdout); fflush(stdout);
return readline(buf, len, stdin, stdout); return readline_stream(buf, len, stdin, stdout);
} }
/**************************************************************************** /****************************************************************************