nshlib: Move commoin initialization from console_main to nsh_initialize

to avoid the code duplication

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2022-10-15 17:31:04 +08:00 committed by hartmannathan
parent 964747dd36
commit 0d210c3227
6 changed files with 44 additions and 140 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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

View File

@ -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);

View File

@ -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 (; ; )

View File

@ -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;
}