This is an automated email from the ASF dual-hosted git repository.

hartmannathan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git

commit 0d210c3227f2b49955e59e04d0f9c674223c9066
Author: Xiang Xiao <xiaoxi...@xiaomi.com>
AuthorDate: Sat Oct 15 17:31:04 2022 +0800

    nshlib: Move commoin initialization from console_main to nsh_initialize
    
    to avoid the code duplication
    
    Signed-off-by: Xiang Xiao <xiaoxi...@xiaomi.com>
---
 nshlib/nsh_altconsole.c  | 34 ----------------------------------
 nshlib/nsh_consolemain.c | 32 --------------------------------
 nshlib/nsh_init.c        | 43 +++++++++++++++++++++++++++++++++++++++++--
 nshlib/nsh_telnetd.c     | 38 --------------------------------------
 nshlib/nsh_usbconsole.c  | 30 ------------------------------
 system/nsh/nsh_main.c    |  7 +++----
 6 files changed, 44 insertions(+), 140 deletions(-)

diff --git a/nshlib/nsh_altconsole.c b/nshlib/nsh_altconsole.c
index f407ceffd..750fc845f 100644
--- a/nshlib/nsh_altconsole.c
+++ b/nshlib/nsh_altconsole.c
@@ -34,8 +34,6 @@
 #include "nsh.h"
 #include "nsh_console.h"
 
-#include "netutils/netinit.h"
-
 #if defined(CONFIG_NSH_ALTCONDEV) && !defined(HAVE_USB_CONSOLE)
 
 /****************************************************************************
@@ -251,38 +249,6 @@ int nsh_consolemain(int argc, FAR char *argv[])
 
   DEBUGASSERT(pstate);
 
-  /* Initialize any USB tracing options that were requested */
-
-#ifdef CONFIG_NSH_USBDEV_TRACE
-  usbtrace_enable(TRACE_BITSET);
-#endif
-
-#if defined(CONFIG_NSH_ROMFSETC) && !defined(CONFIG_NSH_DISABLESCRIPT)
-  /* Execute the system init script */
-
-  nsh_sysinitscript(&pstate->cn_vtbl);
-#endif
-
-#ifdef CONFIG_NSH_NETINIT
-  /* Bring up the network */
-
-  netinit_bringup();
-#endif
-
-#if defined(CONFIG_NSH_ARCHINIT) && defined(CONFIG_BOARDCTL_FINALINIT)
-  /* Perform architecture-specific final-initialization (if configured) */
-
-  boardctl(BOARDIOC_FINALINIT, 0);
-#endif
-
-  /* Execute the one-time start-up script.
-   * Any output will go to /dev/console.
-   */
-
-#ifdef CONFIG_NSH_ROMFSETC
-  nsh_initscript(&pstate->cn_vtbl);
-#endif
-
   /* First map stderr and stdout to alternative devices */
 
   ret = nsh_clone_console(pstate);
diff --git a/nshlib/nsh_consolemain.c b/nshlib/nsh_consolemain.c
index 13cb6eb94..463c30b02 100644
--- a/nshlib/nsh_consolemain.c
+++ b/nshlib/nsh_consolemain.c
@@ -32,8 +32,6 @@
 #include "nsh.h"
 #include "nsh_console.h"
 
-#include "netutils/netinit.h"
-
 #if !defined(CONFIG_NSH_ALTCONDEV) && !defined(HAVE_USB_CONSOLE) && \
     !defined(HAVE_USB_KEYBOARD)
 
@@ -71,36 +69,6 @@ int nsh_consolemain(int argc, FAR char *argv[])
 
   DEBUGASSERT(pstate != NULL);
 
-#ifdef CONFIG_NSH_USBDEV_TRACE
-  /* Initialize any USB tracing options that were requested */
-
-  usbtrace_enable(TRACE_BITSET);
-#endif
-
-#if defined(CONFIG_NSH_ROMFSETC) && !defined(CONFIG_NSH_DISABLESCRIPT)
-  /* Execute the system init script */
-
-  nsh_sysinitscript(&pstate->cn_vtbl);
-#endif
-
-#ifdef CONFIG_NSH_NETINIT
-  /* Bring up the network */
-
-  netinit_bringup();
-#endif
-
-#if defined(CONFIG_NSH_ARCHINIT) && defined(CONFIG_BOARDCTL_FINALINIT)
-  /* Perform architecture-specific final-initialization (if configured) */
-
-  boardctl(BOARDIOC_FINALINIT, 0);
-#endif
-
-#if defined(CONFIG_NSH_ROMFSETC) && !defined(CONFIG_NSH_DISABLESCRIPT)
-  /* Execute the start-up script */
-
-  nsh_initscript(&pstate->cn_vtbl);
-#endif
-
   /* Execute the session */
 
   ret = nsh_session(pstate, true, argc, argv);
diff --git a/nshlib/nsh_init.c b/nshlib/nsh_init.c
index 251e6c452..fdd9a9208 100644
--- a/nshlib/nsh_init.c
+++ b/nshlib/nsh_init.c
@@ -27,9 +27,11 @@
 #include <sys/boardctl.h>
 
 #include "system/readline.h"
+#include "netutils/netinit.h"
 #include "nshlib/nshlib.h"
 
 #include "nsh.h"
+#include "nsh_console.h"
 
 /****************************************************************************
  * Private Data
@@ -66,28 +68,65 @@ static const struct extmatch_vtable_s g_nsh_extmatch =
 
 void nsh_initialize(void)
 {
+#if defined(CONFIG_NSH_ROMFSETC) && !defined(CONFIG_NSH_DISABLESCRIPT)
+  FAR struct console_stdio_s *pstate;
+#endif
+
 #if defined(CONFIG_NSH_READLINE) && defined(CONFIG_READLINE_TABCOMPLETION)
   /* Configure the NSH prompt */
 
   readline_prompt(g_nshprompt);
 
-#ifdef CONFIG_READLINE_HAVE_EXTMATCH
+#  ifdef CONFIG_READLINE_HAVE_EXTMATCH
   /* Set up for tab completion on NSH commands */
 
   readline_extmatch(&g_nsh_extmatch);
-#endif
+#  endif
 #endif
 
   /* Mount the /etc filesystem */
 
   (void)nsh_romfsetc();
 
+#ifdef CONFIG_NSH_USBDEV_TRACE
+  /* Initialize any USB tracing options that were requested */
+
+  usbtrace_enable(TRACE_BITSET);
+#endif
+
 #ifdef CONFIG_NSH_ARCHINIT
   /* Perform architecture-specific initialization (if configured) */
 
   boardctl(BOARDIOC_INIT, 0);
 #endif
 
+#if defined(CONFIG_NSH_ROMFSETC) && !defined(CONFIG_NSH_DISABLESCRIPT)
+  pstate = nsh_newconsole(false);
+
+  /* Execute the system init script */
+
+  nsh_sysinitscript(&pstate->cn_vtbl);
+#endif
+
+#ifdef CONFIG_NSH_NETINIT
+  /* Bring up the network */
+
+  netinit_bringup();
+#endif
+
+#if defined(CONFIG_NSH_ARCHINIT) && defined(CONFIG_BOARDCTL_FINALINIT)
+  /* Perform architecture-specific final-initialization (if configured) */
+
+  boardctl(BOARDIOC_FINALINIT, 0);
+#endif
+
+#if defined(CONFIG_NSH_ROMFSETC) && !defined(CONFIG_NSH_DISABLESCRIPT)
+  /* Execute the start-up script */
+
+  nsh_initscript(&pstate->cn_vtbl);
+  nsh_release(&pstate->cn_vtbl);
+#endif
+
 #if defined(CONFIG_NSH_TELNET) && !defined(CONFIG_NSH_DISABLE_TELNETSTART) && \
   !defined(CONFIG_NETINIT_NETLOCAL)
   /* If the Telnet console is selected as a front-end, then start the
diff --git a/nshlib/nsh_telnetd.c b/nshlib/nsh_telnetd.c
index a7bd99790..47f86d6b8 100644
--- a/nshlib/nsh_telnetd.c
+++ b/nshlib/nsh_telnetd.c
@@ -31,7 +31,6 @@
 
 #include <arpa/inet.h>
 
-#include "netutils/netinit.h"
 #include "netutils/telnetd.h"
 
 #ifdef CONFIG_TELNET_CHARACTER_MODE
@@ -225,9 +224,6 @@ int nsh_telnetstart(sa_family_t family)
 
   if (state == TELNETD_NOTRUNNING)
     {
-#if defined(CONFIG_NSH_ROMFSETC) && !defined(CONFIG_NSH_CONSOLE)
-      FAR struct console_stdio_s *pstate;
-#endif
       struct telnetd_config_s config;
 
       /* There is a tiny race condition here if two tasks were to try to
@@ -236,40 +232,6 @@ int nsh_telnetstart(sa_family_t family)
 
       state = TELNETD_STARTED;
 
-      /* Initialize any USB tracing options that were requested.  If
-       * standard console is also defined, then we will defer this step to
-       * the standard console.
-       */
-
-#if defined(CONFIG_NSH_USBDEV_TRACE) && !defined(CONFIG_NSH_CONSOLE)
-      usbtrace_enable(TRACE_BITSET);
-#endif
-
-      /* Execute the startup script.  If standard console is also defined,
-       * then we will not bother with the initscript here (although it is
-       * safe to call nsh_initscript multiple times).
-       */
-
-#if defined(CONFIG_NSH_ROMFSETC) && !defined(CONFIG_NSH_CONSOLE)
-      pstate = nsh_newconsole();
-      nsh_initscript(&pstate->cn_vtbl);
-      nsh_release(&pstate->cn_vtbl);
-#endif
-
-#if defined(CONFIG_NSH_NETINIT) && !defined(CONFIG_NSH_CONSOLE)
-      /* Bring up the network */
-
-      netinit_bringup();
-#endif
-
-      /* Perform architecture-specific final-initialization(if configured) */
-
-#if defined(CONFIG_NSH_ARCHINIT) && \
-    defined(CONFIG_BOARDCTL_FINALINIT) && \
-    !defined(CONFIG_NSH_CONSOLE)
-      boardctl(BOARDIOC_FINALINIT, 0);
-#endif
-
       /* Configure the telnet daemon */
 
       config.d_port      = HTONS(CONFIG_NSH_TELNETD_PORT);
diff --git a/nshlib/nsh_usbconsole.c b/nshlib/nsh_usbconsole.c
index bdde01986..47512a953 100644
--- a/nshlib/nsh_usbconsole.c
+++ b/nshlib/nsh_usbconsole.c
@@ -248,12 +248,6 @@ int nsh_consolemain(int argc, FAR char *argv[])
 
   DEBUGASSERT(pstate);
 
-  /* Initialize any USB tracing options that were requested */
-
-#ifdef CONFIG_NSH_USBDEV_TRACE
-  usbtrace_enable(TRACE_BITSET);
-#endif
-
   /* Initialize the USB serial driver */
 
 #if defined(CONFIG_PL2303) || defined(CONFIG_CDCACM)
@@ -284,30 +278,6 @@ int nsh_consolemain(int argc, FAR char *argv[])
   nsh_nullstdio();
 #endif
 
-#if defined(CONFIG_NSH_ROMFSETC) && !defined(CONFIG_NSH_DISABLESCRIPT)
-  /* Execute the system init script */
-
-  nsh_sysinitscript(&pstate->cn_vtbl);
-#endif
-
-#ifdef CONFIG_NSH_NETINIT
-  /* Bring up the network */
-
-  netinit_bringup();
-#endif
-
-#if defined(CONFIG_NSH_ARCHINIT) && defined(CONFIG_BOARDCTL_FINALINIT)
-  /* Perform architecture-specific final-initialization (if configured) */
-
-  boardctl(BOARDIOC_FINALINIT, 0);
-#endif
-
-  /* Execute the one-time start-up script (output may go to /dev/null) */
-
-#ifdef CONFIG_NSH_ROMFSETC
-  nsh_initscript(&pstate->cn_vtbl);
-#endif
-
   /* Now loop, executing creating a session for each USB connection */
 
   for (; ; )
diff --git a/system/nsh/nsh_main.c b/system/nsh/nsh_main.c
index 7212fccbe..44d93cb73 100644
--- a/system/nsh/nsh_main.c
+++ b/system/nsh/nsh_main.c
@@ -100,8 +100,7 @@ int main(int argc, FAR char *argv[])
   struct boardioc_symtab_s symdesc;
 #endif
   struct sched_param param;
-  int exitval = 0;
-  int ret;
+  int ret = 0;
 
   /* Check the task priority that we were started with */
 
@@ -137,8 +136,8 @@ int main(int argc, FAR char *argv[])
    */
 
   fprintf(stderr, "ERROR: nsh_consolemain() returned: %d\n", ret);
-  exitval = 1;
+  ret = 1;
 #endif
 
-  return exitval;
+  return ret;
 }

Reply via email to