This is an automated email from the ASF dual-hosted git repository.
acassis 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 75455d378 nshlib: Reuse nsh_session logic in nsh_telnetmain
75455d378 is described below
commit 75455d3788601e7730a17a228c31d254e4fbfd61
Author: Xiang Xiao <[email protected]>
AuthorDate: Mon Oct 17 05:17:41 2022 +0800
nshlib: Reuse nsh_session logic in nsh_telnetmain
to avoid the code duplication
Signed-off-by: Xiang Xiao <[email protected]>
---
nshlib/nsh.h | 9 +++-
nshlib/nsh_altconsole.c | 2 +-
nshlib/nsh_consolemain.c | 2 +-
nshlib/nsh_session.c | 22 +++++++--
nshlib/nsh_stdsession.c | 22 +++++++--
nshlib/nsh_system.c | 2 +-
nshlib/nsh_telnetd.c | 122 ++---------------------------------------------
nshlib/nsh_usbconsole.c | 2 +-
8 files changed, 55 insertions(+), 128 deletions(-)
diff --git a/nshlib/nsh.h b/nshlib/nsh.h
index 30823ae77..3d8c87f46 100644
--- a/nshlib/nsh.h
+++ b/nshlib/nsh.h
@@ -647,6 +647,13 @@
* Public Types
****************************************************************************/
+enum nsh_login_e
+{
+ NSH_LOGIN_NONE = 0, /* Don't Performs the login sequence */
+ NSH_LOGIN_LOCAL, /* Performs the login sequence as local user */
+ NSH_LOGIN_TELNET /* Performs the login sequence as telnet client
*/
+};
+
#ifndef CONFIG_NSH_DISABLE_ITEF
/* State when parsing and if-then-else sequence */
@@ -866,7 +873,7 @@ int nsh_loginscript(FAR struct nsh_vtbl_s *vtbl);
struct console_stdio_s;
int nsh_session(FAR struct console_stdio_s *pstate,
- bool login, int argc, FAR char *argv[]);
+ int login, int argc, FAR char *argv[]);
int nsh_parse(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline);
/****************************************************************************
diff --git a/nshlib/nsh_altconsole.c b/nshlib/nsh_altconsole.c
index 750fc845f..49aad333b 100644
--- a/nshlib/nsh_altconsole.c
+++ b/nshlib/nsh_altconsole.c
@@ -273,7 +273,7 @@ int nsh_consolemain(int argc, FAR char *argv[])
/* Execute the session */
- nsh_session(pstate, true, argc, argv);
+ nsh_session(pstate, NSH_LOGIN_LOCAL, argc, argv);
/* We lost the connection. Wait for the keyboard to
* be re-connected.
diff --git a/nshlib/nsh_consolemain.c b/nshlib/nsh_consolemain.c
index 463c30b02..29e138bf6 100644
--- a/nshlib/nsh_consolemain.c
+++ b/nshlib/nsh_consolemain.c
@@ -71,7 +71,7 @@ int nsh_consolemain(int argc, FAR char *argv[])
/* Execute the session */
- ret = nsh_session(pstate, true, argc, argv);
+ ret = nsh_session(pstate, NSH_LOGIN_LOCAL, argc, argv);
/* Exit upon return */
diff --git a/nshlib/nsh_session.c b/nshlib/nsh_session.c
index 8eb7fe360..20b5ba2af 100644
--- a/nshlib/nsh_session.c
+++ b/nshlib/nsh_session.c
@@ -66,7 +66,7 @@
****************************************************************************/
int nsh_session(FAR struct console_stdio_s *pstate,
- bool login, int argc, FAR char *argv[])
+ int login, int argc, FAR char *argv[])
{
FAR struct nsh_vtbl_s *vtbl;
int ret = EXIT_FAILURE;
@@ -74,9 +74,9 @@ int nsh_session(FAR struct console_stdio_s *pstate,
DEBUGASSERT(pstate);
vtbl = &pstate->cn_vtbl;
- if (login)
- {
#ifdef CONFIG_NSH_CONSOLE_LOGIN
+ if (login == NSH_LOGIN_LOCAL)
+ {
/* Login User and Password Check */
if (nsh_login(pstate) != OK)
@@ -84,8 +84,24 @@ int nsh_session(FAR struct console_stdio_s *pstate,
nsh_exit(vtbl, 1);
return -1; /* nsh_exit does not return */
}
+ }
+ else
#endif /* CONFIG_NSH_CONSOLE_LOGIN */
+#ifdef CONFIG_NSH_TELNET_LOGIN
+ if (login == NSH_LOGIN_TELNET)
+ {
+ /* Login User and Password Check */
+
+ if (nsh_telnetlogin(pstate) != OK)
+ {
+ nsh_exit(vtbl, 1);
+ return -1; /* nsh_exit does not return */
+ }
+ }
+#endif /* CONFIG_NSH_TELNET_LOGIN */
+ if (login != NSH_LOGIN_NONE)
+ {
/* Present a greeting and possibly a Message of the Day (MOTD) */
fputs(g_nshgreeting, pstate->cn_outstream);
diff --git a/nshlib/nsh_stdsession.c b/nshlib/nsh_stdsession.c
index 5d423f137..f33832960 100644
--- a/nshlib/nsh_stdsession.c
+++ b/nshlib/nsh_stdsession.c
@@ -65,7 +65,7 @@
****************************************************************************/
int nsh_session(FAR struct console_stdio_s *pstate,
- bool login, int argc, FAR char *argv[])
+ int login, int argc, FAR char *argv[])
{
FAR struct nsh_vtbl_s *vtbl;
int ret = EXIT_FAILURE;
@@ -74,9 +74,9 @@ int nsh_session(FAR struct console_stdio_s *pstate,
DEBUGASSERT(pstate);
vtbl = &pstate->cn_vtbl;
- if (login)
- {
#ifdef CONFIG_NSH_CONSOLE_LOGIN
+ if (login == NSH_LOGIN_LOCAL)
+ {
/* Login User and Password Check */
if (nsh_stdlogin(pstate) != OK)
@@ -84,8 +84,24 @@ int nsh_session(FAR struct console_stdio_s *pstate,
nsh_exit(vtbl, 1);
return -1; /* nsh_exit does not return */
}
+ }
+ else
#endif /* CONFIG_NSH_CONSOLE_LOGIN */
+#ifdef CONFIG_NSH_TELNET_LOGIN
+ if (login == NSH_LOGIN_TELNET)
+ {
+ /* Login User and Password Check */
+
+ if (nsh_telnetlogin(pstate) != OK)
+ {
+ nsh_exit(vtbl, 1);
+ return -1; /* nsh_exit does not return */
+ }
+ }
+#endif /* CONFIG_NSH_TELNET_LOGIN */
+ if (login != NSH_LOGIN_NONE)
+ {
/* Present a greeting and possibly a Message of the Day (MOTD) */
printf("%s", g_nshgreeting);
diff --git a/nshlib/nsh_system.c b/nshlib/nsh_system.c
index 12bbed5b7..7603dfc94 100644
--- a/nshlib/nsh_system.c
+++ b/nshlib/nsh_system.c
@@ -45,7 +45,7 @@ static int nsh_system_(int argc, FAR char *argv[], int isctty)
/* Execute the session */
- ret = nsh_session(pstate, false, argc, argv);
+ ret = nsh_session(pstate, NSH_LOGIN_NONE, argc, argv);
/* Exit upon return */
diff --git a/nshlib/nsh_telnetd.c b/nshlib/nsh_telnetd.c
index 47f86d6b8..a773b3ec9 100644
--- a/nshlib/nsh_telnetd.c
+++ b/nshlib/nsh_telnetd.c
@@ -67,131 +67,19 @@ enum telnetd_state_e
int nsh_telnetmain(int argc, FAR char *argv[])
{
- UNUSED(argc);
- UNUSED(argv);
-
FAR struct console_stdio_s *pstate = nsh_newconsole(true);
- FAR struct nsh_vtbl_s *vtbl;
int ret;
DEBUGASSERT(pstate != NULL);
- vtbl = &pstate->cn_vtbl;
-
- ninfo("Session [%d] Started\n", getpid());
-
-#ifdef CONFIG_NSH_TELNET_LOGIN
- /* Login User and Password Check */
-
- if (nsh_telnetlogin(pstate) != OK)
- {
- nsh_exit(vtbl, 1);
- return -1; /* nsh_exit does not return */
- }
-#endif /* CONFIG_NSH_TELNET_LOGIN */
-
- /* The following logic mostly the same as the login in nsh_session.c. It
- * differs only in that gets() is called to get the command instead of
- * readline().
- */
-
- /* Present a greeting and possibly a Message of the Day (MOTD) */
-
- fputs(g_nshgreeting, pstate->cn_outstream);
-
-#ifdef CONFIG_NSH_MOTD
-# ifdef CONFIG_NSH_PLATFORM_MOTD
- /* Output the platform message of the day */
-
- platform_motd(vtbl->iobuffer, IOBUFFERSIZE);
- fprintf(pstate->cn_outstream, "%s\n", vtbl->iobuffer);
-
-# else
- /* Output the fixed message of the day */
-
- fprintf(pstate->cn_outstream, "%s\n", g_nshmotd);
-# endif
-#endif
-
- fflush(pstate->cn_outstream);
-
- /* Execute the login script */
-
-#ifdef CONFIG_NSH_ROMFSRC
- nsh_loginscript(vtbl);
-#endif
-
- /* Then enter the command line parsing loop */
-
- for (; ; )
- {
- /* Get the next line of input from the Telnet client */
-
-#ifdef CONFIG_TELNET_CHARACTER_MODE
-#ifdef CONFIG_NSH_CLE
- /* cle() returns a negated errno value on failure (errno is not set) */
-
- ret = cle(pstate->cn_line, g_nshprompt, CONFIG_NSH_LINELEN,
- INSTREAM(pstate), OUTSTREAM(pstate));
- if (ret < 0)
- {
- fprintf(pstate->cn_errstream, g_fmtcmdfailed, "nsh_telnetmain",
- "cle", NSH_ERRNO_OF(-ret));
- nsh_exit(vtbl, 1);
- }
-#else
- /* Display the prompt string */
-
- fputs(g_nshprompt, pstate->cn_outstream);
- fflush(pstate->cn_outstream);
- /* readline() returns EOF on failure (errno is not set) */
-
- 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_telnetmain",
- "readline", NSH_ERRNO);
- nsh_exit(vtbl, 1);
- }
-#endif
-#else
- /* Display the prompt string */
+ /* Execute the session */
- fputs(g_nshprompt, pstate->cn_outstream);
- fflush(pstate->cn_outstream);
+ ret = nsh_session(pstate, NSH_LOGIN_TELNET, argc, argv);
- /* fgets() returns NULL on failure (errno will be set) */
+ /* Exit upon return */
- if (fgets(pstate->cn_line, CONFIG_NSH_LINELEN,
- INSTREAM(pstate)) == NULL)
- {
- fprintf(pstate->cn_errstream, g_fmtcmdfailed, "nsh_telnetmain",
- "fgets", NSH_ERRNO);
- nsh_exit(vtbl, 1);
- }
-
- ret = strlen(pstate->cn_line);
-#endif
-
- /* Parse process the received Telnet command */
-
- nsh_parse(vtbl, pstate->cn_line);
- fflush(pstate->cn_outstream);
- }
-
- /* Clean up */
-
- nsh_exit(vtbl, 0);
-
- /* We do not get here, but this is necessary to keep some compilers happy */
-
- UNUSED(ret);
- return OK;
+ nsh_exit(&pstate->cn_vtbl, ret);
+ return ret;
}
/****************************************************************************
diff --git a/nshlib/nsh_usbconsole.c b/nshlib/nsh_usbconsole.c
index 47512a953..04233a990 100644
--- a/nshlib/nsh_usbconsole.c
+++ b/nshlib/nsh_usbconsole.c
@@ -292,7 +292,7 @@ int nsh_consolemain(int argc, FAR char *argv[])
/* Execute the session */
- nsh_session(pstate, true, argc, argv);
+ nsh_session(pstate, NSH_LOGIN_LOCAL, argc, argv);
/* Switch to /dev/null because we probably no longer have a
* valid console device.