From 3dad8a29a24b765697e3fdab7ff1c41661ee7375 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Wed, 20 Jan 2016 11:18:08 -0600 Subject: [PATCH] NSH now supports an MOTD string that will be presented after the greeting --- ChangeLog.txt | 2 ++ nshlib/Kconfig | 38 ++++++++++++++++++++++++++++++++++++++ nshlib/nsh.h | 9 +++++++-- nshlib/nsh_parse.c | 6 ++++++ nshlib/nsh_session.c | 23 ++++++++++++++++++++--- nshlib/nsh_stdsession.c | 20 ++++++++++++++++++-- nshlib/nsh_telnetd.c | 32 +++++++++++++++++++++++++------- 7 files changed, 116 insertions(+), 14 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index cbc0b097f..7990865e2 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1514,4 +1514,6 @@ file like /etc/passwd (2016-01-19). * apps/nshlib/nsh_passwdcmds.c: Add useradd, userdel, and passwd commands (2019-01-20). + * apps/nshlib: Now supports a Message of the Day (MOTD) that will + be presented after the NSH greeting (2015-01-20). diff --git a/nshlib/Kconfig b/nshlib/Kconfig index dc92cacc9..016b19bda 100644 --- a/nshlib/Kconfig +++ b/nshlib/Kconfig @@ -17,6 +17,44 @@ config NSH_LIBRARY if NSH_LIBRARY +menuconfig NSH_MOTD + bool "Message of the Day (MOTD)" + default n + ---help--- + Support a user-provided Message of the Day (MOTD) that will be + presented each time new NSH session is opened. + +if NSH_MOTD + +config NSH_PLATFORM_MOTD + bool "Platform MOTD" + default n + ---help--- + If this option is selected, the NSH will call into platform-specific + logic in order to get the MOTD. The function prototype for this + call is: + + void platform_motd(FAR char *buffer, size_t buflen); + + Where buffer is the location to return the MOTD and buflen is the + length of that buffer. The maximum size of the buffer is detemined + by NSH_FILEIOSIZE. An appopriate location for the + implementation of platform_motd would be within apps/platform/. + + One newline will be inserted after the platform-supplied message. + +config NSH_MOTD_STRING + string "MOTD String" + default "No MOTD string provided" + depends on !NSH_PLATFORM_MOTD + ---help--- + If NSH_MOTD is selected, but NSH_PLATFORM_MOTD is not, then a fixed + MOTD string will be used. That string is provided by this selection. + + One newline will be inserted after supplied MOTD message. + +endif # NSH_MOTD + menu "Command Line Configuration" choice diff --git a/nshlib/nsh.h b/nshlib/nsh.h index f33e31f19..1bfad7157 100644 --- a/nshlib/nsh.h +++ b/nshlib/nsh.h @@ -573,10 +573,12 @@ # undef NSH_HAVE_IOBUFFER #endif -/* The I/O buffer is needed for the ls, cp, and ps commands */ +/* The I/O buffer is needed for the ls, cp, and ps commands. It is also + * needed if the platform supplied MOTD is configured. + */ #if defined(CONFIG_NSH_DISABLE_LS) && defined(CONFIG_NSH_DISABLE_CP) && \ - defined(CONFIG_NSH_DISABLE_PS) + defined(CONFIG_NSH_DISABLE_PS) && !defined(CONFIG_NSH_PLATFORM_MOTD) # undef NSH_HAVE_IOBUFFER #endif @@ -798,6 +800,9 @@ typedef CODE int (*nsh_direntry_handler_t)(FAR struct nsh_vtbl_s *vtbl, ****************************************************************************/ extern const char g_nshgreeting[]; +#if defined(CONFIG_NSH_MOTD) && !defined(CONFIG_NSH_PLATFORM_MOTD) +extern const char g_nshmotd[]; +#endif #if defined(CONFIG_NSH_TELNET_LOGIN) && defined(CONFIG_NSH_TELNET) extern const char g_telnetgreeting[]; extern const char g_userprompt[]; diff --git a/nshlib/nsh_parse.c b/nshlib/nsh_parse.c index b487efd97..55ce08d1f 100644 --- a/nshlib/nsh_parse.c +++ b/nshlib/nsh_parse.c @@ -224,6 +224,12 @@ const char g_nshgreeting[] = "\nNuttShell (NSH) NuttX-" CONFIG_VERSION_STR const char g_nshgreeting[] = "\nNuttShell (NSH)\n"; #endif +/* Fixed Message of the Day (MOTD) */ + +#if defined(CONFIG_NSH_MOTD) && !defined(CONFIG_NSH_PLATFORM_MOTD) +const char g_nshmotd[] = CONFIG_NSH_MOTD_STRING; +#endif + /* Telnet login prompts */ #if defined(CONFIG_NSH_TELNET_LOGIN) && defined(CONFIG_NSH_TELNET) diff --git a/nshlib/nsh_session.c b/nshlib/nsh_session.c index de6710bfe..ddb00b8df 100644 --- a/nshlib/nsh_session.c +++ b/nshlib/nsh_session.c @@ -103,19 +103,36 @@ int nsh_session(FAR struct console_stdio_s *pstate) { + FAR struct nsh_vtbl_s *vtbl; int ret; DEBUGASSERT(pstate); + vtbl = &pstate->cn_vtbl; - /* Present a greeting */ + /* 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 - (void)nsh_loginscript(&pstate->cn_vtbl); + (void)nsh_loginscript(vtbl); #endif /* Then enter the command line parsing loop */ @@ -148,7 +165,7 @@ int nsh_session(FAR struct console_stdio_s *pstate) { /* Parse process the command */ - (void)nsh_parse(&pstate->cn_vtbl, pstate->cn_line); + (void)nsh_parse(vtbl, pstate->cn_line); fflush(pstate->cn_outstream); } diff --git a/nshlib/nsh_stdsession.c b/nshlib/nsh_stdsession.c index 07def5b3b..c45a9327b 100644 --- a/nshlib/nsh_stdsession.c +++ b/nshlib/nsh_stdsession.c @@ -102,14 +102,30 @@ int nsh_session(FAR struct console_stdio_s *pstate) { + FAR struct nsh_vtbl_s *vtbl; int ret; DEBUGASSERT(pstate); + vtbl = &pstate->cn_vtbl; - /* Present a greeting */ + /* Present a greeting and possibly a Message of the Day (MOTD) */ printf("%s", g_nshgreeting); +#ifdef CONFIG_NSH_MOTD +# ifdef CONFIG_NSH_PLATFORM_MOTD + /* Output the platform message of the day */ + + platform_motd(vtbl->iobuffer, IOBUFFERSIZE); + printf("%s\n", vtbl->iobuffer); + +# else + /* Output the fixed message of the day */ + + printf("%s\n", g_nshmotd); +# endif +#endif + /* Then enter the command line parsing loop */ for (;;) @@ -138,7 +154,7 @@ int nsh_session(FAR struct console_stdio_s *pstate) { /* Parse process the command */ - (void)nsh_parse(&pstate->cn_vtbl, pstate->cn_line); + (void)nsh_parse(vtbl, pstate->cn_line); } /* Readline normally returns the number of characters read, diff --git a/nshlib/nsh_telnetd.c b/nshlib/nsh_telnetd.c index 0602081cb..3047975ab 100644 --- a/nshlib/nsh_telnetd.c +++ b/nshlib/nsh_telnetd.c @@ -274,7 +274,10 @@ static int nsh_telnetlogin(FAR struct console_stdio_s *pstate) int nsh_telnetmain(int argc, char *argv[]) { FAR struct console_stdio_s *pstate = nsh_newconsole(); + FAR struct nsh_vtbl_s *vtbl; + DEBUGASSERT(pstate != NULL); + vtbl = &pstate->cn_vtbl; dbg("Session [%d] Started\n", getpid()); @@ -283,7 +286,7 @@ int nsh_telnetmain(int argc, char *argv[]) #ifdef CONFIG_NSH_TELNET_LOGIN if (nsh_telnetlogin(pstate) != OK) { - nsh_exit(&pstate->cn_vtbl, 1); + nsh_exit(vtbl, 1); return -1; /* nsh_exit does not return */ } #endif /* CONFIG_NSH_TELNET_LOGIN */ @@ -293,9 +296,24 @@ int nsh_telnetmain(int argc, char *argv[]) * readline(). */ - /* Present the NSH greeting */ + /* 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 startup script. If standard console is also defined, then @@ -304,13 +322,13 @@ int nsh_telnetmain(int argc, char *argv[]) */ #if defined(CONFIG_NSH_ROMFSETC) && !defined(CONFIG_NSH_CONSOLE) - (void)nsh_initscript(&pstate->cn_vtbl); + (void)nsh_initscript(vtbl); #endif /* Execute the login script */ #ifdef CONFIG_NSH_ROMFSRC - (void)nsh_loginscript(&pstate->cn_vtbl); + (void)nsh_loginscript(vtbl); #endif /* Then enter the command line parsing loop */ @@ -328,20 +346,20 @@ int nsh_telnetmain(int argc, char *argv[]) { /* Parse process the received Telnet command */ - (void)nsh_parse(&pstate->cn_vtbl, pstate->cn_line); + (void)nsh_parse(vtbl, pstate->cn_line); fflush(pstate->cn_outstream); } else { fprintf(pstate->cn_outstream, g_fmtcmdfailed, "nsh_telnetmain", "fgets", NSH_ERRNO); - nsh_exit(&pstate->cn_vtbl, 1); + nsh_exit(vtbl, 1); } } /* Clean up */ - nsh_exit(&pstate->cn_vtbl, 0); + nsh_exit(vtbl, 0); /* We do not get here, but this is necessary to keep some compilers happy */