nsh: Accept the command line arguments like sh

Usage: nsh [<script-path>|-c <command>]

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Change-Id: Ifb76b27e7fd09d26b1f6e48c391fed0972018041
This commit is contained in:
Xiang Xiao
2020-08-17 18:28:50 +08:00
committed by David Sidrane
parent 78e8090842
commit 9d80399bd5
14 changed files with 203 additions and 182 deletions

View File

@@ -42,6 +42,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef CONFIG_NSH_CLE
# include "system/cle.h"
@@ -79,10 +80,11 @@
*
****************************************************************************/
int nsh_session(FAR struct console_stdio_s *pstate, bool login)
int nsh_session(FAR struct console_stdio_s *pstate,
bool login, int argc, FAR char *argv[])
{
FAR struct nsh_vtbl_s *vtbl;
int ret;
int ret = EXIT_FAILURE;
DEBUGASSERT(pstate);
vtbl = &pstate->cn_vtbl;
@@ -126,70 +128,92 @@ int nsh_session(FAR struct console_stdio_s *pstate, bool login)
#endif
}
/* Then enter the command line parsing loop */
for (; ; )
if (argc < 2)
{
/* For the case of debugging the USB console...
* dump collected USB trace data
*/
/* Then enter the command line parsing loop */
#ifdef CONFIG_NSH_USBDEV_TRACE
nsh_usbtrace();
#endif
/* Get the next line of input. readline() returns EOF on end-of-file
* or any read failure.
*/
#ifdef CONFIG_NSH_CLE
/* cle() normally returns the number of characters read, but will
* return a negated errno value on end of file or if an error occurs.
* Either will cause the session to terminate.
*/
ret = cle(pstate->cn_line, g_nshprompt, CONFIG_NSH_LINELEN,
INSTREAM(pstate), OUTSTREAM(pstate));
if (ret < 0)
for (; ; )
{
fprintf(pstate->cn_errstream, g_fmtcmdfailed, "nsh_session",
"cle", NSH_ERRNO_OF(-ret));
continue;
}
#else
/* Display the prompt string */
fputs(g_nshprompt, pstate->cn_outstream);
fflush(pstate->cn_outstream);
/* readline() normally returns the number of characters read, but will
* return EOF on end of file or if an error occurs. EOF
* will cause the session to terminate.
*/
ret = readline(pstate->cn_line, CONFIG_NSH_LINELEN,
INSTREAM(pstate), OUTSTREAM(pstate));
if (ret == EOF)
{
/* NOTE: readline() does not set the errno variable, but perhaps we
* will be lucky and it will still be valid.
/* For the case of debugging the USB console...
* dump collected USB trace data
*/
fprintf(pstate->cn_errstream, g_fmtcmdfailed, "nsh_session",
"readline", NSH_ERRNO);
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
#ifdef CONFIG_NSH_USBDEV_TRACE
nsh_usbtrace();
#endif
/* Get the next line of input. readline() returns EOF
* on end-of-file or any read failure.
*/
#ifdef CONFIG_NSH_CLE
/* cle() normally returns the number of characters read, but will
* return a negated errno value on end of file or if an error
* occurs. Either will cause the session to terminate.
*/
ret = cle(pstate->cn_line, g_nshprompt, CONFIG_NSH_LINELEN,
INSTREAM(pstate), OUTSTREAM(pstate));
if (ret < 0)
{
fprintf(pstate->cn_errstream, g_fmtcmdfailed, "nsh_session",
"cle", NSH_ERRNO_OF(-ret));
continue;
}
#else
/* Display the prompt string */
fputs(g_nshprompt, pstate->cn_outstream);
fflush(pstate->cn_outstream);
/* readline() normally returns the number of characters read, but
* will return EOF on end of file or if an error occurs. EOF
* will cause the session to terminate.
*/
ret = readline(pstate->cn_line, CONFIG_NSH_LINELEN,
INSTREAM(pstate), OUTSTREAM(pstate));
if (ret == EOF)
{
/* NOTE: readline() does not set the errno variable, but
* perhaps we will be lucky and it will still be valid.
*/
fprintf(pstate->cn_errstream, g_fmtcmdfailed, "nsh_session",
"readline", NSH_ERRNO);
ret = EXIT_SUCCESS;
break;
}
#endif
/* Parse process the command */
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_NFILE_STREAMS) && !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, pstate->cn_line);
ret = nsh_parse(vtbl, argv[2]);
#ifdef CONFIG_NFILE_STREAMS
fflush(pstate->cn_outstream);
#endif
}
/* We do not get here, but this is necessary to keep some compilers happy.
* But others will complain that this code is not reachable.
*/
return EXIT_SUCCESS;
return ret;
}