Skip to content

Commit 3da204c

Browse files
JianyuWang0623xiaoxiang781216
authored andcommitted
Pack parameters of nsh_execute() as struct nsh_exec_param_s
1. Input redirect flags currently is hardcode, passing by arguments maybe better. 2. Only support redirect to path_name, redirect to fd is needed for pipeline. Test 1. Redirect in cat < /etc/init.d/rc.sysinit 2. Redirect with FIFO mkfifo /dev/testfifo cat /dev/testfifo & ls > /dev/testfifo 3. NSH Params set name `uname` echo $name Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
1 parent 27b5021 commit 3da204c

File tree

8 files changed

+181
-133
lines changed

8 files changed

+181
-133
lines changed

builtin/exec_builtin.c

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,7 @@
4545
* Input Parameter:
4646
* filename - Name of the linked-in binary to be started.
4747
* argv - Argument list
48-
* redirfile_in - If input is redirected, this parameter will be non-NULL
49-
* and will provide the full path to the file.
50-
* redirfile_out - If output is redirected, this parameter will be non-NULL
51-
* and will provide the full path to the file.
52-
* oflags - If output is redirected, this parameter will provide the
53-
* open flags to use. This will support file replacement
54-
* of appending to an existing file.
48+
* param - Parameters for execute.
5549
*
5650
* Returned Value:
5751
* This is an end-user function, so it follows the normal convention:
@@ -61,13 +55,12 @@
6155
****************************************************************************/
6256

6357
int exec_builtin(FAR const char *appname, FAR char * const *argv,
64-
FAR const char *redirfile_in, FAR const char *redirfile_out,
65-
int oflags)
58+
FAR const struct nsh_param_s *param)
6659
{
6760
FAR const struct builtin_s *builtin;
6861
posix_spawnattr_t attr;
6962
posix_spawn_file_actions_t file_actions;
70-
struct sched_param param;
63+
struct sched_param sched;
7164
pid_t pid;
7265
int index;
7366
int ret;
@@ -106,8 +99,8 @@ int exec_builtin(FAR const char *appname, FAR char * const *argv,
10699

107100
/* Set the correct task size and priority */
108101

109-
param.sched_priority = builtin->priority;
110-
ret = posix_spawnattr_setschedparam(&attr, &param);
102+
sched.sched_priority = builtin->priority;
103+
ret = posix_spawnattr_setschedparam(&attr, &sched);
111104
if (ret != 0)
112105
{
113106
goto errout_with_actions;
@@ -147,33 +140,40 @@ int exec_builtin(FAR const char *appname, FAR char * const *argv,
147140

148141
#endif
149142

150-
/* Is input being redirected? */
151-
152-
if (redirfile_in)
143+
if (param)
153144
{
154-
/* Set up to close open redirfile and set to stdin (0) */
145+
/* Is input being redirected? */
155146

156-
ret = posix_spawn_file_actions_addopen(&file_actions, 0,
157-
redirfile_in, O_RDONLY, 0);
158-
if (ret != 0)
147+
if (param->file_in)
159148
{
160-
serr("ERROR: posix_spawn_file_actions_addopen failed: %d\n", ret);
161-
goto errout_with_actions;
149+
/* Set up to close open redirfile and set to stdin (0) */
150+
151+
ret = posix_spawn_file_actions_addopen(&file_actions, 0,
152+
param->file_in,
153+
param->oflags_in, 0);
154+
if (ret != 0)
155+
{
156+
serr("ERROR: posix_spawn_file_actions_addopen failed: %d\n",
157+
ret);
158+
goto errout_with_actions;
159+
}
162160
}
163-
}
164161

165-
/* Is output being redirected? */
166-
167-
if (redirfile_out)
168-
{
169-
/* Set up to close open redirfile and set to stdout (1) */
162+
/* Is output being redirected? */
170163

171-
ret = posix_spawn_file_actions_addopen(&file_actions, 1,
172-
redirfile_out, oflags, 0644);
173-
if (ret != 0)
164+
if (param->file_out)
174165
{
175-
serr("ERROR: posix_spawn_file_actions_addopen failed: %d\n", ret);
176-
goto errout_with_actions;
166+
/* Set up to close open redirfile and set to stdout (1) */
167+
168+
ret = posix_spawn_file_actions_addopen(&file_actions, 1,
169+
param->file_out,
170+
param->oflags_out, 0644);
171+
if (ret != 0)
172+
{
173+
serr("ERROR: posix_spawn_file_actions_addopen failed: %d\n",
174+
ret);
175+
goto errout_with_actions;
176+
}
177177
}
178178
}
179179

include/builtin/builtin.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <sys/types.h>
3131

3232
#include <nuttx/lib/builtin.h>
33+
#include <nshlib/nshlib.h>
3334

3435
/****************************************************************************
3536
* Pre-processor Definitions
@@ -66,13 +67,7 @@ extern "C"
6667
* Input Parameter:
6768
* filename - Name of the linked-in binary to be started.
6869
* argv - Argument list
69-
* redirfile_in - If input is redirected, this parameter will be non-NULL
70-
* and will provide the full path to the file.
71-
* redirfile_out - If output is redirected, this parameter will be non-NULL
72-
* and will provide the full path to the file.
73-
* oflags - If output is redirected, this parameter will provide the
74-
* open flags to use. This will support file replacement
75-
* of appending to an existing file.
70+
* param - Parameters for execute.
7671
*
7772
* Returned Value:
7873
* This is an end-user function, so it follows the normal convention:
@@ -82,8 +77,7 @@ extern "C"
8277
****************************************************************************/
8378

8479
int exec_builtin(FAR const char *appname, FAR char * const *argv,
85-
FAR const char *redirfile_in, FAR const char *redirfile_out,
86-
int oflags);
80+
FAR const struct nsh_param_s *param);
8781

8882
#undef EXTERN
8983
#if defined(__cplusplus)

include/nshlib/nshlib.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,25 @@
6565
# define SCHED_NSH SCHED_FIFO
6666
#endif
6767

68+
struct nsh_param_s
69+
{
70+
/* Redirect input/output through `fd` OR `path_name`
71+
*
72+
* Select one:
73+
* 1. Using fd_in/fd_out as oldfd for dup2() if greater than -1.
74+
* 2. Using file_in/file_out as full path to the file if it is
75+
* not NULL, and oflags_in/oflags_out as flags for open().
76+
*/
77+
78+
int fd_in;
79+
int fd_out;
80+
81+
int oflags_in;
82+
int oflags_out;
83+
FAR const char *file_in;
84+
FAR const char *file_out;
85+
};
86+
6887
/****************************************************************************
6988
* Public Data
7089
****************************************************************************/

nshlib/nsh.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#endif
4040

4141
#include <nuttx/usb/usbdev_trace.h>
42+
#include <nshlib/nshlib.h>
4243

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

859860
#ifdef CONFIG_NSH_BUILTIN_APPS
860861
int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
861-
FAR char **argv, FAR const char *redirfile_in,
862-
FAR const char *redirfile_out, int oflags);
862+
FAR char **argv, FAR const struct nsh_param_s *param);
863863
#endif
864864

865865
#ifdef CONFIG_NSH_FILE_APPS
866866
int nsh_fileapp(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
867-
FAR char **argv, FAR const char *redirfile_in,
868-
FAR const char *redirfile_out, int oflags);
867+
FAR char **argv, FAR const struct nsh_param_s *param);
869868
#endif
870869

871870
#ifndef CONFIG_DISABLE_ENVIRON

nshlib/nsh_builtin.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@
6969
****************************************************************************/
7070

7171
int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
72-
FAR char **argv, FAR const char *redirfile_in,
73-
FAR const char *redirfile_out, int oflags)
72+
FAR char **argv,
73+
FAR const struct nsh_param_s *param)
7474
{
7575
#if !defined(CONFIG_NSH_DISABLEBG) && defined(CONFIG_SCHED_CHILD_STATUS)
7676
struct sigaction act;
@@ -102,7 +102,7 @@ int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
102102
* applications.
103103
*/
104104

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

235235
sigaction(SIGCHLD, &old, NULL);
236236
# endif
237-
struct sched_param param;
238-
sched_getparam(ret, &param);
239-
nsh_output(vtbl, "%s [%d:%d]\n", cmd, ret, param.sched_priority);
237+
struct sched_param sched;
238+
sched_getparam(ret, &sched);
239+
nsh_output(vtbl, "%s [%d:%d]\n", cmd, ret, sched.sched_priority);
240240

241241
/* Backgrounded commands always 'succeed' as long as we can start
242242
* them.

nshlib/nsh_fileapps.c

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@
7070
****************************************************************************/
7171

7272
int nsh_fileapp(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
73-
FAR char **argv, FAR const char *redirfile_in,
74-
FAR const char *redirfile_out, int oflags)
73+
FAR char **argv, FAR const struct nsh_param_s *param)
7574
{
7675
posix_spawn_file_actions_t file_actions;
7776
posix_spawnattr_t attr;
@@ -107,39 +106,46 @@ int nsh_fileapp(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
107106
goto errout_with_actions;
108107
}
109108

110-
/* Handle redirection of input */
111-
112-
if (redirfile_in)
109+
if (param)
113110
{
114-
/* Set up to close open redirfile and set to stdin (0) */
111+
/* Handle redirection of input */
115112

116-
ret = posix_spawn_file_actions_addopen(&file_actions, 0,
117-
redirfile_in, O_RDONLY, 0);
118-
if (ret != 0)
113+
if (param->file_in)
119114
{
120-
nsh_error(vtbl, g_fmtcmdfailed, cmd,
121-
"posix_spawn_file_actions_addopen",
122-
NSH_ERRNO);
123-
goto errout_with_actions;
115+
/* Set up to close open redirfile and set to stdin (0) */
116+
117+
ret = posix_spawn_file_actions_addopen(&file_actions, 0,
118+
param->file_in,
119+
param->oflags_in,
120+
0);
121+
if (ret != 0)
122+
{
123+
nsh_error(vtbl, g_fmtcmdfailed, cmd,
124+
"posix_spawn_file_actions_addopen",
125+
NSH_ERRNO);
126+
goto errout_with_actions;
127+
}
124128
}
125-
}
126129

127-
/* Handle re-direction of output */
130+
/* Handle re-direction of output */
128131

129-
if (redirfile_out)
130-
{
131-
ret = posix_spawn_file_actions_addopen(&file_actions, 1, redirfile_out,
132-
oflags, 0644);
133-
if (ret != 0)
132+
if (param->file_out)
134133
{
135-
/* posix_spawn_file_actions_addopen returns a positive errno
136-
* value on failure.
137-
*/
134+
ret = posix_spawn_file_actions_addopen(&file_actions, 1,
135+
param->file_out,
136+
param->oflags_out,
137+
0644);
138+
if (ret != 0)
139+
{
140+
/* posix_spawn_file_actions_addopen returns a positive errno
141+
* value on failure.
142+
*/
138143

139-
nsh_error(vtbl, g_fmtcmdfailed, cmd,
140-
"posix_spawn_file_actions_addopen",
141-
NSH_ERRNO);
142-
goto errout_with_attrs;
144+
nsh_error(vtbl, g_fmtcmdfailed, cmd,
145+
"posix_spawn_file_actions_addopen",
146+
NSH_ERRNO);
147+
goto errout_with_attrs;
148+
}
143149
}
144150
}
145151

@@ -151,7 +157,7 @@ int nsh_fileapp(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
151157
if (index >= 0)
152158
{
153159
FAR const struct builtin_s *builtin;
154-
struct sched_param param;
160+
struct sched_param sched;
155161

156162
/* Get information about the builtin */
157163

@@ -164,8 +170,8 @@ int nsh_fileapp(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
164170

165171
/* Set the correct task size and priority */
166172

167-
param.sched_priority = builtin->priority;
168-
ret = posix_spawnattr_setschedparam(&attr, &param);
173+
sched.sched_priority = builtin->priority;
174+
ret = posix_spawnattr_setschedparam(&attr, &sched);
169175
if (ret != 0)
170176
{
171177
goto errout_with_actions;
@@ -298,9 +304,9 @@ int nsh_fileapp(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
298304

299305
#if !defined(CONFIG_SCHED_WAITPID) || !defined(CONFIG_NSH_DISABLEBG)
300306
{
301-
struct sched_param param;
302-
sched_getparam(ret, &param);
303-
nsh_output(vtbl, "%s [%d:%d]\n", cmd, ret, param.sched_priority);
307+
struct sched_param sched;
308+
sched_getparam(ret, &sched);
309+
nsh_output(vtbl, "%s [%d:%d]\n", cmd, ret, sched.sched_priority);
304310

305311
/* Backgrounded commands always 'succeed' as long as we can start
306312
* them.

0 commit comments

Comments
 (0)