Skip to content

Commit

Permalink
nsh_fileapp: handle input redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
casaroli authored and acassis committed Aug 8, 2024
1 parent 8fba726 commit e46347e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
3 changes: 2 additions & 1 deletion nshlib/nsh.h
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,8 @@ int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,

#ifdef CONFIG_NSH_FILE_APPS
int nsh_fileapp(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
FAR char **argv, FAR const char *redirfile, int oflags);
FAR char **argv, FAR const char *redirfile_in,
FAR const char *redirfile_out, int oflags);
#endif

#ifndef CONFIG_DISABLE_ENVIRON
Expand Down
26 changes: 23 additions & 3 deletions nshlib/nsh_fileapps.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include <errno.h>
#include <string.h>
#include <libgen.h>
#include <fcntl.h>

#include <nuttx/lib/builtin.h>

#include "nsh.h"
Expand Down Expand Up @@ -68,7 +70,8 @@
****************************************************************************/

int nsh_fileapp(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
FAR char **argv, FAR const char *redirfile, int oflags)
FAR char **argv, FAR const char *redirfile_in,
FAR const char *redirfile_out, int oflags)
{
posix_spawn_file_actions_t file_actions;
posix_spawnattr_t attr;
Expand Down Expand Up @@ -104,11 +107,28 @@ int nsh_fileapp(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
goto errout_with_actions;
}

/* Handle redirection of input */

if (redirfile_in)
{
/* Set up to close open redirfile and set to stdin (0) */

ret = posix_spawn_file_actions_addopen(&file_actions, 0,
redirfile_in, O_RDONLY, 0);
if (ret != 0)
{
nsh_error(vtbl, g_fmtcmdfailed, cmd,
"posix_spawn_file_actions_addopen",
NSH_ERRNO);
goto errout_with_actions;
}
}

/* Handle re-direction of output */

if (redirfile)
if (redirfile_out)
{
ret = posix_spawn_file_actions_addopen(&file_actions, 1, redirfile,
ret = posix_spawn_file_actions_addopen(&file_actions, 1, redirfile_out,
oflags, 0644);
if (ret != 0)
{
Expand Down
3 changes: 2 additions & 1 deletion nshlib/nsh_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,8 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
*/

#ifdef CONFIG_NSH_FILE_APPS
ret = nsh_fileapp(vtbl, argv[0], argv, redirfile_out, oflags);
ret = nsh_fileapp(vtbl, argv[0], argv, redirfile_in,
redirfile_out, oflags);
if (ret >= 0)
{
/* nsh_fileapp() returned 0 or 1. This means that the built-in
Expand Down

0 comments on commit e46347e

Please sign in to comment.