nshlib: Enhance nsh to execute the shell script
and support the interactive shell too Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
parent
d307758c3c
commit
3f9302561c
@ -41,6 +41,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "nsh.h"
|
||||
@ -71,30 +72,44 @@
|
||||
|
||||
int nsh_system(int argc, char *argv[])
|
||||
{
|
||||
/* Expect argc == 2 with argv[1] being the command to execute */
|
||||
FAR struct console_stdio_s *pstate = nsh_newconsole();
|
||||
FAR struct nsh_vtbl_s *vtbl;
|
||||
int ret = EXIT_FAILURE;
|
||||
|
||||
if (argc >= 2)
|
||||
DEBUGASSERT(pstate != NULL);
|
||||
vtbl = &pstate->cn_vtbl;
|
||||
|
||||
if (argc < 2)
|
||||
{
|
||||
FAR struct console_stdio_s *pstate = nsh_newconsole();
|
||||
FAR struct nsh_vtbl_s *vtbl;
|
||||
/* Execute the interactive shell */
|
||||
|
||||
DEBUGASSERT(pstate != NULL);
|
||||
vtbl = &pstate->cn_vtbl;
|
||||
ret = nsh_session(pstate, false);
|
||||
}
|
||||
else if (strcmp(argv[1], "-h") == 0)
|
||||
{
|
||||
ret = nsh_output(vtbl, "Usage: %s [<script-path>|-c <command>]\n",
|
||||
argv[0]);
|
||||
}
|
||||
else if (strcmp(argv[1], "-c") != 0)
|
||||
{
|
||||
#if CONFIG_NFILE_STREAMS > 0 && !defined(CONFIG_NSH_DISABLESCRIPT)
|
||||
/* Execute the shell script */
|
||||
|
||||
ret = nsh_script(vtbl, argv[0], argv[1]);
|
||||
#endif
|
||||
}
|
||||
else if (argc >= 3)
|
||||
{
|
||||
/* Parse process the command */
|
||||
|
||||
nsh_parse(vtbl, argv[1]);
|
||||
ret = nsh_parse(vtbl, argv[2]);
|
||||
#if CONFIG_NFILE_STREAMS > 0
|
||||
fflush(pstate->cn_outstream);
|
||||
#endif
|
||||
|
||||
/* Exit upon return */
|
||||
|
||||
nsh_exit(&pstate->cn_vtbl, OK);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
else
|
||||
{
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* Exit upon return */
|
||||
|
||||
nsh_exit(&pstate->cn_vtbl, ret);
|
||||
return ret;
|
||||
}
|
||||
|
@ -128,7 +128,7 @@ FILE *popen(FAR const char *command, FAR const char *mode)
|
||||
struct sched_param param;
|
||||
posix_spawnattr_t attr;
|
||||
posix_spawn_file_actions_t file_actions;
|
||||
FAR char *argv[2];
|
||||
FAR char *argv[3];
|
||||
int fd[2];
|
||||
int oldfd;
|
||||
int newfd;
|
||||
@ -258,8 +258,9 @@ FILE *popen(FAR const char *command, FAR const char *mode)
|
||||
* appropriately.
|
||||
*/
|
||||
|
||||
argv[0] = (FAR char *)command;
|
||||
argv[1] = NULL;
|
||||
argv[0] = "-c";
|
||||
argv[1] = (FAR char *)command;
|
||||
argv[2] = NULL;
|
||||
|
||||
#ifdef CONFIG_SYSTEM_POPEN_SHPATH
|
||||
errcode = posix_spawn(&container->shell, CONFIG_SYSTEM_POPEN_SHPATH,
|
||||
|
@ -74,7 +74,7 @@
|
||||
|
||||
int system(FAR const char *cmd)
|
||||
{
|
||||
FAR char *argv[2];
|
||||
FAR char *argv[3];
|
||||
struct sched_param param;
|
||||
posix_spawnattr_t attr;
|
||||
pid_t pid;
|
||||
@ -141,8 +141,9 @@ int system(FAR const char *cmd)
|
||||
|
||||
/* Spawn nsh_system() which will execute the command under the shell. */
|
||||
|
||||
argv[0] = (FAR char *)cmd;
|
||||
argv[1] = NULL;
|
||||
argv[0] = "-c";
|
||||
argv[1] = (FAR char *)cmd;
|
||||
argv[2] = NULL;
|
||||
|
||||
#ifdef CONFIG_SYSTEM_SYSTEM_SHPATH
|
||||
errcode = posix_spawn(&pid, CONFIG_SYSTEM_SYSTEM_SHPATH, NULL, &attr,
|
||||
|
Loading…
Reference in New Issue
Block a user