nshlib/nsh_session: Handle the command arguments in any order

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2021-10-31 21:36:07 +08:00 committed by Gustavo Henrique Nihei
parent 991e491c94
commit a0f773b318

View File

@ -70,6 +70,7 @@ int nsh_session(FAR struct console_stdio_s *pstate,
{
FAR struct nsh_vtbl_s *vtbl;
int ret = EXIT_FAILURE;
int i;
DEBUGASSERT(pstate);
vtbl = &pstate->cn_vtbl;
@ -113,8 +114,52 @@ int nsh_session(FAR struct console_stdio_s *pstate,
#endif
}
if (argc < 2)
/* Process the command line option */
for (i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-h") == 0)
{
nsh_output(vtbl, "Usage: %s [<script-path>|-c <command>]\n",
argv[0]);
return EXIT_SUCCESS;
}
else if (strcmp(argv[i], "-c") == 0)
{
/* Process the inline command */
if (i + 1 < argc)
{
return nsh_parse(vtbl, argv[i + 1]);
}
else
{
nsh_error(vtbl, g_fmtargrequired, argv[0]);
return EXIT_FAILURE;
}
}
else if (argv[i][0] != '-')
{
break;
}
/* Unknown option */
nsh_error(vtbl, g_fmtsyntax, argv[0]);
return EXIT_FAILURE;
}
if (i < argc)
{
#ifndef CONFIG_NSH_DISABLESCRIPT
/* Execute the shell script */
return nsh_script(vtbl, argv[0], argv[i]);
#else
return EXIT_FAILURE;
#endif
}
/* Then enter the command line parsing loop */
for (; ; )
@ -176,29 +221,6 @@ int nsh_session(FAR struct console_stdio_s *pstate,
nsh_parse(vtbl, pstate->cn_line);
fflush(pstate->cn_outstream);
}
}
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 defined(CONFIG_FILE_STREAM) && !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 */
ret = nsh_parse(vtbl, argv[2]);
#ifdef CONFIG_FILE_STREAM
fflush(pstate->cn_outstream);
#endif
}
return ret;
}