From 8fba726a7d7502553ca8d30baef07c124f18b01e Mon Sep 17 00:00:00 2001 From: Marco Casaroli Date: Tue, 6 Aug 2024 23:53:41 +0200 Subject: [PATCH] feat(nsh_cat): allow cat to read from stdin Now, if we run cat without arguments, it will just read from stdin. It can be used with redirect like `cat < infile > outfile`. --- nshlib/nsh_command.c | 4 ++-- nshlib/nsh_fscmds.c | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c index a505b1faa..0256fe2ac 100644 --- a/nshlib/nsh_command.c +++ b/nshlib/nsh_command.c @@ -158,8 +158,8 @@ static const struct cmdmap_s g_cmdmap[] = #endif #ifndef CONFIG_NSH_DISABLE_CAT - CMD_MAP("cat", cmd_cat, 2, CONFIG_NSH_MAXARGUMENTS, - " [ [ ...]]"), + CMD_MAP("cat", cmd_cat, 1, CONFIG_NSH_MAXARGUMENTS, + "[ [ [ ...]]]"), #endif #ifndef CONFIG_DISABLE_ENVIRON diff --git a/nshlib/nsh_fscmds.c b/nshlib/nsh_fscmds.c index ca07ba952..31687acd2 100644 --- a/nshlib/nsh_fscmds.c +++ b/nshlib/nsh_fscmds.c @@ -791,6 +791,25 @@ int cmd_cat(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv) } } + if (argc == 1) + { + char *buf = malloc(BUFSIZ); + + /* Dump from input */ + + while (true) + { + ssize_t n = nsh_read(vtbl, buf, BUFSIZ); + + if (n == 0) + break; + + nsh_write(vtbl, buf, n); + } + + free(buf); + } + return ret; } #endif