Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pipeline support for nsh commandline #18

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 58 additions & 32 deletions builtin/exec_builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,7 @@
* Input Parameter:
* filename - Name of the linked-in binary to be started.
* argv - Argument list
* redirfile_in - If input is redirected, this parameter will be non-NULL
* and will provide the full path to the file.
* redirfile_out - If output is redirected, this parameter will be non-NULL
* and will provide the full path to the file.
* oflags - If output is redirected, this parameter will provide the
* open flags to use. This will support file replacement
* of appending to an existing file.
* param - Parameters for execute.
*
* Returned Value:
* This is an end-user function, so it follows the normal convention:
Expand All @@ -61,13 +55,12 @@
****************************************************************************/

int exec_builtin(FAR const char *appname, FAR char * const *argv,
FAR const char *redirfile_in, FAR const char *redirfile_out,
int oflags)
FAR const struct nsh_param_s *param)
{
FAR const struct builtin_s *builtin;
posix_spawnattr_t attr;
posix_spawn_file_actions_t file_actions;
struct sched_param param;
struct sched_param sched;
pid_t pid;
int index;
int ret;
Expand Down Expand Up @@ -106,8 +99,8 @@ int exec_builtin(FAR const char *appname, FAR char * const *argv,

/* Set the correct task size and priority */

param.sched_priority = builtin->priority;
ret = posix_spawnattr_setschedparam(&attr, &param);
sched.sched_priority = builtin->priority;
ret = posix_spawnattr_setschedparam(&attr, &sched);
if (ret != 0)
{
goto errout_with_actions;
Expand Down Expand Up @@ -147,34 +140,67 @@ int exec_builtin(FAR const char *appname, FAR char * const *argv,

#endif

/* Is input being redirected? */

if (redirfile_in)
if (param)
{
/* Set up to close open redirfile and set to stdin (0) */
/* Is input being redirected? */

ret = posix_spawn_file_actions_addopen(&file_actions, 0,
redirfile_in, O_RDONLY, 0);
if (ret != 0)
if (param->file_in)
{
serr("ERROR: posix_spawn_file_actions_addopen failed: %d\n", ret);
goto errout_with_actions;
/* Set up to close open redirfile and set to stdin (0) */

ret = posix_spawn_file_actions_addopen(&file_actions, 0,
param->file_in,
param->oflags_in, 0);
if (ret != 0)
{
serr("ERROR: posix_spawn_file_actions_addopen failed: %d\n",
ret);
goto errout_with_actions;
}
}
}

/* Is output being redirected? */
#ifdef CONFIG_NSH_PIPELINE
else if (param->fd_in != -1)
{
ret = posix_spawn_file_actions_adddup2(&file_actions,
param->fd_in, 0);
if (ret != 0)
{
serr("ERROR: posix_spawn_file_actions_adddup2 failed: %d\n",
ret);
goto errout_with_actions;
}
}
#endif

if (redirfile_out)
{
/* Set up to close open redirfile and set to stdout (1) */
/* Is output being redirected? */

ret = posix_spawn_file_actions_addopen(&file_actions, 1,
redirfile_out, oflags, 0644);
if (ret != 0)
if (param->file_out)
{
serr("ERROR: posix_spawn_file_actions_addopen failed: %d\n", ret);
goto errout_with_actions;
/* Set up to close open redirfile and set to stdout (1) */

ret = posix_spawn_file_actions_addopen(&file_actions, 1,
param->file_out,
param->oflags_out, 0644);
if (ret != 0)
{
serr("ERROR: posix_spawn_file_actions_addopen failed: %d\n",
ret);
goto errout_with_actions;
}
}
#ifdef CONFIG_NSH_PIPELINE
else if (param->fd_out != -1)
{
ret = posix_spawn_file_actions_adddup2(&file_actions,
param->fd_out, 1);
if (ret != 0)
{
serr("ERROR: posix_spawn_file_actions_adddup2 failed: %d\n",
ret);
goto errout_with_actions;
}
}
#endif
}

#ifdef CONFIG_LIBC_EXECFUNCS
Expand Down
12 changes: 3 additions & 9 deletions include/builtin/builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <sys/types.h>

#include <nuttx/lib/builtin.h>
#include <nshlib/nshlib.h>

/****************************************************************************
* Pre-processor Definitions
Expand Down Expand Up @@ -66,13 +67,7 @@ extern "C"
* Input Parameter:
* filename - Name of the linked-in binary to be started.
* argv - Argument list
* redirfile_in - If input is redirected, this parameter will be non-NULL
* and will provide the full path to the file.
* redirfile_out - If output is redirected, this parameter will be non-NULL
* and will provide the full path to the file.
* oflags - If output is redirected, this parameter will provide the
* open flags to use. This will support file replacement
* of appending to an existing file.
* param - Parameters for execute.
*
* Returned Value:
* This is an end-user function, so it follows the normal convention:
Expand All @@ -82,8 +77,7 @@ extern "C"
****************************************************************************/

int exec_builtin(FAR const char *appname, FAR char * const *argv,
FAR const char *redirfile_in, FAR const char *redirfile_out,
int oflags);
FAR const struct nsh_param_s *param);

#undef EXTERN
#if defined(__cplusplus)
Expand Down
19 changes: 19 additions & 0 deletions include/nshlib/nshlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@
# define SCHED_NSH SCHED_FIFO
#endif

struct nsh_param_s
{
/* Redirect input/output through `fd` OR `path_name`
*
* Select one:
* 1. Using fd_in/fd_out as oldfd for dup2() if greater than -1.
* 2. Using file_in/file_out as full path to the file if it is
* not NULL, and oflags_in/oflags_out as flags for open().
*/

int fd_in;
int fd_out;

int oflags_in;
int oflags_out;
FAR const char *file_in;
FAR const char *file_out;
};

/****************************************************************************
* Public Data
****************************************************************************/
Expand Down
7 changes: 7 additions & 0 deletions nshlib/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ config NSH_ALIAS_MAX_AMOUNT

endif # NSH_ALIAS

config NSH_PIPELINE
bool "Enable pipeline support"
default !DEFAULT_SMALL
depends on PIPES
---help---
Enable pipeline support for nsh.

endmenu # Command Line Configuration

config NSH_BUILTIN_APPS
Expand Down
7 changes: 3 additions & 4 deletions nshlib/nsh.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#endif

#include <nuttx/usb/usbdev_trace.h>
#include <nshlib/nshlib.h>

/****************************************************************************
* Pre-processor Definitions
Expand Down Expand Up @@ -858,14 +859,12 @@ int nsh_command(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char *argv[]);

#ifdef CONFIG_NSH_BUILTIN_APPS
int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
FAR char **argv, FAR const char *redirfile_in,
FAR const char *redirfile_out, int oflags);
FAR char **argv, FAR const struct nsh_param_s *param);
#endif

#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_in,
FAR const char *redirfile_out, int oflags);
FAR char **argv, FAR const struct nsh_param_s *param);
#endif

#ifndef CONFIG_DISABLE_ENVIRON
Expand Down
12 changes: 6 additions & 6 deletions nshlib/nsh_builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@
****************************************************************************/

int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
FAR char **argv, FAR const char *redirfile_in,
FAR const char *redirfile_out, int oflags)
FAR char **argv,
FAR const struct nsh_param_s *param)
{
#if !defined(CONFIG_NSH_DISABLEBG) && defined(CONFIG_SCHED_CHILD_STATUS)
struct sigaction act;
Expand Down Expand Up @@ -102,7 +102,7 @@ int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
* applications.
*/

ret = exec_builtin(cmd, argv, redirfile_in, redirfile_out, oflags);
ret = exec_builtin(cmd, argv, param);
if (ret >= 0)
{
/* The application was successfully started with pre-emption disabled.
Expand Down Expand Up @@ -234,9 +234,9 @@ int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,

sigaction(SIGCHLD, &old, NULL);
# endif
struct sched_param param;
sched_getparam(ret, &param);
nsh_output(vtbl, "%s [%d:%d]\n", cmd, ret, param.sched_priority);
struct sched_param sched;
sched_getparam(ret, &sched);
nsh_output(vtbl, "%s [%d:%d]\n", cmd, ret, sched.sched_priority);

/* Backgrounded commands always 'succeed' as long as we can start
* them.
Expand Down
Loading
Loading