diff --git a/nshlib/nsh.h b/nshlib/nsh.h index 8d0cb8d9c6..a417788333 100644 --- a/nshlib/nsh.h +++ b/nshlib/nsh.h @@ -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 diff --git a/nshlib/nsh_fileapps.c b/nshlib/nsh_fileapps.c index e279ea2d42..89094b284d 100644 --- a/nshlib/nsh_fileapps.c +++ b/nshlib/nsh_fileapps.c @@ -35,6 +35,8 @@ #include #include #include +#include + #include #include "nsh.h" @@ -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; @@ -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) { diff --git a/nshlib/nsh_parse.c b/nshlib/nsh_parse.c index b4fa64be75..80eed77035 100644 --- a/nshlib/nsh_parse.c +++ b/nshlib/nsh_parse.c @@ -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