You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by gu...@apache.org on 2021/11/09 12:35:45 UTC
[incubator-nuttx-apps] branch master updated: nshlib/nsh_session:
Handle the command arguments in any order
This is an automated email from the ASF dual-hosted git repository.
gustavonihei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git
The following commit(s) were added to refs/heads/master by this push:
new a0f773b nshlib/nsh_session: Handle the command arguments in any order
a0f773b is described below
commit a0f773b3181395d673b3ba34906c85bc53bc64fc
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sun Oct 31 21:36:07 2021 +0800
nshlib/nsh_session: Handle the command arguments in any order
Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
nshlib/nsh_session.c | 152 +++++++++++++++++++++++++++++----------------------
1 file changed, 87 insertions(+), 65 deletions(-)
diff --git a/nshlib/nsh_session.c b/nshlib/nsh_session.c
index e7cfff7..e001606 100644
--- a/nshlib/nsh_session.c
+++ b/nshlib/nsh_session.c
@@ -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,91 +114,112 @@ int nsh_session(FAR struct console_stdio_s *pstate,
#endif
}
- if (argc < 2)
+ /* Process the command line option */
+
+ for (i = 1; i < argc; i++)
{
- /* Then enter the command line parsing loop */
+ 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 */
- for (; ; )
+ 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] != '-')
{
- /* For the case of debugging the USB console...
- * dump collected USB trace data
- */
+ break;
+ }
-#ifdef CONFIG_NSH_USBDEV_TRACE
- nsh_usbtrace();
-#endif
+ /* Unknown option */
- /* Get the next line of input. readline() returns EOF
- * on end-of-file or any read failure.
- */
+ nsh_error(vtbl, g_fmtsyntax, argv[0]);
+ return EXIT_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.
- */
+ if (i < argc)
+ {
+#ifndef CONFIG_NSH_DISABLESCRIPT
+ /* Execute the shell script */
- 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;
- }
+ return nsh_script(vtbl, argv[0], argv[i]);
#else
- /* Display the prompt string */
+ return EXIT_FAILURE;
+#endif
+ }
- fputs(g_nshprompt, pstate->cn_outstream);
- fflush(pstate->cn_outstream);
+ /* Then enter the command line parsing loop */
- /* 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.
- */
+ for (; ; )
+ {
+ /* For the case of debugging the USB console...
+ * dump collected USB trace data
+ */
- 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;
- }
+#ifdef CONFIG_NSH_USBDEV_TRACE
+ nsh_usbtrace();
#endif
- /* Parse process the command */
+ /* Get the next line of input. readline() returns EOF
+ * on end-of-file or any read failure.
+ */
- nsh_parse(vtbl, pstate->cn_line);
- fflush(pstate->cn_outstream);
+#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 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 */
+#else
+ /* Display the prompt string */
- ret = nsh_script(vtbl, argv[0], argv[1]);
+ 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
- }
- else if (argc >= 3)
- {
+
/* Parse process the command */
- ret = nsh_parse(vtbl, argv[2]);
-#ifdef CONFIG_FILE_STREAM
+ nsh_parse(vtbl, pstate->cn_line);
fflush(pstate->cn_outstream);
-#endif
}
return ret;