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`.
This commit is contained in:
Marco Casaroli 2024-08-06 23:53:41 +02:00 committed by Alan Carvalho de Assis
parent 4104019e1c
commit 8fba726a7d
2 changed files with 21 additions and 2 deletions

View File

@ -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,
"<path> [<path> [<path> ...]]"),
CMD_MAP("cat", cmd_cat, 1, CONFIG_NSH_MAXARGUMENTS,
"[<path> [<path> [<path> ...]]]"),
#endif
#ifndef CONFIG_DISABLE_ENVIRON

View File

@ -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