From 4104019e1cf8895dd6128cff90952be2e4ec90ee Mon Sep 17 00:00:00 2001 From: Marco Casaroli Date: Tue, 6 Aug 2024 23:52:34 +0200 Subject: [PATCH] feat(nsh): add console read This allows programs such as `cat` to read from the console (that could be redirected). --- nshlib/nsh_console.c | 29 +++++++++++++++++++++++++++++ nshlib/nsh_console.h | 3 +++ 2 files changed, 32 insertions(+) diff --git a/nshlib/nsh_console.c b/nshlib/nsh_console.c index 444a65421..6b19ef3e1 100644 --- a/nshlib/nsh_console.c +++ b/nshlib/nsh_console.c @@ -139,6 +139,34 @@ static ssize_t nsh_consolewrite(FAR struct nsh_vtbl_s *vtbl, return ret; } +/**************************************************************************** + * Name: nsh_consoleread + * + * Description: + * read a buffer to the remote shell window. + * + * Currently only used by cat. + * + ****************************************************************************/ + +static ssize_t nsh_consoleread(FAR struct nsh_vtbl_s *vtbl, + FAR void *buffer, size_t nbytes) +{ + FAR struct console_stdio_s *pstate = (FAR struct console_stdio_s *)vtbl; + ssize_t ret; + + /* Read the data to the output stream */ + + ret = read(INFD(pstate), buffer, nbytes); + if (ret < 0) + { + _err("ERROR: [%d] Failed to read buffer: %d\n", + INFD(pstate), errno); + } + + return ret; +} + /**************************************************************************** * Name: nsh_consolewrite * @@ -380,6 +408,7 @@ FAR struct console_stdio_s *nsh_newconsole(bool isctty) #endif pstate->cn_vtbl.release = nsh_consolerelease; pstate->cn_vtbl.write = nsh_consolewrite; + pstate->cn_vtbl.read = nsh_consoleread; pstate->cn_vtbl.ioctl = nsh_consoleioctl; pstate->cn_vtbl.output = nsh_consoleoutput; #ifndef CONFIG_NSH_DISABLE_ERROR_PRINT diff --git a/nshlib/nsh_console.h b/nshlib/nsh_console.h index 7f950460a..a03ea664a 100644 --- a/nshlib/nsh_console.h +++ b/nshlib/nsh_console.h @@ -44,6 +44,7 @@ #define nsh_clone(v) (v)->clone(v) #define nsh_release(v) (v)->release(v) #define nsh_write(v,b,n) (v)->write(v,b,n) +#define nsh_read(v,b,n) (v)->read(v,b,n) #define nsh_ioctl(v,c,a) (v)->ioctl(v,c,a) #define nsh_linebuffer(v) (v)->linebuffer(v) #define nsh_redirect(v,fi,fo,s) (v)->redirect(v,fi,fo,s) @@ -113,6 +114,8 @@ struct nsh_vtbl_s void (*release)(FAR struct nsh_vtbl_s *vtbl); ssize_t (*write)(FAR struct nsh_vtbl_s *vtbl, FAR const void *buffer, size_t nbytes); + ssize_t (*read)(FAR struct nsh_vtbl_s *vtbl, FAR void *buffer, + size_t nbytes); int (*ioctl)(FAR struct nsh_vtbl_s *vtbl, int cmd, unsigned long arg); #ifndef CONFIG_NSH_DISABLE_ERROR_PRINT int (*error)(FAR struct nsh_vtbl_s *vtbl, FAR const char *fmt, ...)