nshlib/prompt: extend NSH prompt string management

Currently NSH prompt is defined at build time, thus improper for AMP cases
where the same NSH binary is used on different nodes as the same NSH prompt
shows on all nodes.

This patch attempts to support runtime prompt string population from ordered
sources:
  - the environment variable defined by NSH_PROMPT_ENV plus suffix
  - the NSH_PROMPT_STRING
  - the HOSTNAME plus suffix
The suffix is defined by NSH_PROMPT_SUFFIX so that to clearly separate the
command inputs.

Changes in `nshlib/`

- Kconfig:       add configs NSH_PROMPT_MAX/ENV/SUFFIX etc
- nsh.h:         adjust g_nshprompt defs, add nsh_update_prompt
- nsh_parse.c    relocate g_nshpromt to nsh_prompt.c
- nsh_init.c     revise to use nsh_update_prompt once
- nsh_session.c  revise to use methods in nsh_prompt.c
- Makefile       add nsh_prompt.c
- CMakeLists.txt add nsh_prompt.c

New additions in `nshlib/`

- nsh_prompt.c   prompt related data structures and methods.

Signed-off-by: Yanfeng Liu <yfliu2008@qq.com>
This commit is contained in:
Yanfeng Liu
2024-02-22 03:11:50 +08:00
committed by Xiang Xiao
parent f1137b3b55
commit 31908b3128
8 changed files with 147 additions and 12 deletions

View File

@@ -207,7 +207,7 @@ int nsh_session(FAR struct console_stdio_s *pstate,
* occurs. Either will cause the session to terminate.
*/
ret = cle_fd(pstate->cn_line, g_nshprompt, CONFIG_NSH_LINELEN,
ret = cle_fd(pstate->cn_line, nsh_prompt(), CONFIG_NSH_LINELEN,
INFD(pstate), OUTFD(pstate));
if (ret < 0)
{
@@ -218,7 +218,7 @@ int nsh_session(FAR struct console_stdio_s *pstate,
#else
/* Display the prompt string */
write(OUTFD(pstate), g_nshprompt, strlen(g_nshprompt));
write(OUTFD(pstate), nsh_prompt(), strlen(nsh_prompt()));
/* readline() normally returns the number of characters read, but
* will return EOF on end of file or if an error occurs. EOF
@@ -243,6 +243,7 @@ int nsh_session(FAR struct console_stdio_s *pstate,
/* Parse process the command */
nsh_parse(vtbl, pstate->cn_line);
nsh_update_prompt();
}
return ret;