From 85e732ddafba1e2fdf0da3589332e161c16514b2 Mon Sep 17 00:00:00 2001 From: aozima Date: Mon, 2 Jul 2018 15:10:11 +0800 Subject: [PATCH 1/3] [shell] add finsh_get/set_prompt_mode. --- components/finsh/shell.c | 35 +++++++++++++++++++++++++++++++++++ components/finsh/shell.h | 4 ++++ 2 files changed, 39 insertions(+) diff --git a/components/finsh/shell.c b/components/finsh/shell.c index 6bdb418120..667e7ac12a 100644 --- a/components/finsh/shell.c +++ b/components/finsh/shell.c @@ -67,6 +67,13 @@ const char *finsh_get_prompt() #define _PROMPT "finsh " static char finsh_prompt[RT_CONSOLEBUF_SIZE + 1] = {0}; + /* check prompt mode */ + if (!shell->prompt_mode) + { + finsh_prompt[0] = '\0'; + return finsh_prompt; + } + #ifdef FINSH_USING_MSH if (msh_is_used()) strcpy(finsh_prompt, _MSH_PROMPT); else @@ -84,6 +91,34 @@ const char *finsh_get_prompt() } #endif +/** + * @ingroup finsh + * + * This function get the prompt mode of finsh shell. + * + * @return prompt the prompt mode, 0 disable prompt mode, other values enable prompt mode. + */ +rt_uint32_t finsh_get_prompt_mode(void) +{ + RT_ASSERT(shell != RT_NULL); + return shell->prompt_mode; +} + +/** + * @ingroup finsh + * + * This function set the prompt mode of finsh shell. + * + * The parameter 0 disable prompt mode, other values enable prompt mode. + * + * @param prompt the prompt mode + */ +void finsh_set_prompt_mode(rt_uint32_t prompt_mode) +{ + RT_ASSERT(shell != RT_NULL); + shell->prompt_mode = prompt_mode; +} + static char finsh_getchar(void) { #ifdef RT_USING_POSIX diff --git a/components/finsh/shell.h b/components/finsh/shell.h index 06b81de518..e01458adc5 100644 --- a/components/finsh/shell.h +++ b/components/finsh/shell.h @@ -86,6 +86,7 @@ struct finsh_shell enum input_stat stat; rt_uint8_t echo_mode:1; + rt_uint8_t prompt_mode: 1; #ifdef FINSH_USING_HISTORY rt_uint16_t current_history; @@ -118,6 +119,9 @@ int finsh_system_init(void); void finsh_set_device(const char* device_name); const char* finsh_get_device(void); +rt_uint32_t finsh_get_prompt_mode(void); +void finsh_set_prompt_mode(rt_uint32_t prompt_mode); + #ifdef FINSH_USING_AUTH rt_err_t finsh_set_password(const char *password); const char *finsh_get_password(void); From 4f1112f838fbd4b17e9d1e77cd36c72e32d0ae7f Mon Sep 17 00:00:00 2001 From: aozima Date: Mon, 2 Jul 2018 16:23:00 +0800 Subject: [PATCH 2/3] [shell] add finsh_set_prompt(). --- components/finsh/shell.c | 35 ++++++++++++++++++++++++++++++++--- components/finsh/shell.h | 6 ++---- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/components/finsh/shell.c b/components/finsh/shell.c index 667e7ac12a..0109a761bc 100644 --- a/components/finsh/shell.c +++ b/components/finsh/shell.c @@ -56,11 +56,35 @@ ALIGN(RT_ALIGN_SIZE) static char finsh_thread_stack[FINSH_THREAD_STACK_SIZE]; #endif struct finsh_shell *shell; +static char *finsh_prompt_custom = RT_NULL; + +#ifdef RT_USING_HEAP +int finsh_set_prompt(const char * prompt) +{ + if(finsh_prompt_custom) + { + rt_free(finsh_prompt_custom); + finsh_prompt_custom = RT_NULL; + } + + /* strdup */ + if(prompt) + { + finsh_prompt_custom = rt_malloc(strlen(prompt)+1); + if(finsh_prompt_custom) + { + strcpy(finsh_prompt_custom, prompt); + } + } + + return 0; +} +#endif /* RT_USING_HEAP */ -#if defined(FINSH_USING_MSH) || (defined(RT_USING_DFS) && defined(DFS_USING_WORKDIR)) #if defined(RT_USING_DFS) #include -#endif +#endif /* RT_USING_DFS */ + const char *finsh_get_prompt() { #define _MSH_PROMPT "msh " @@ -74,6 +98,12 @@ const char *finsh_get_prompt() return finsh_prompt; } + if(finsh_prompt_custom) + { + strncpy(finsh_prompt, finsh_prompt_custom, sizeof(finsh_prompt)-1); + return finsh_prompt; + } + #ifdef FINSH_USING_MSH if (msh_is_used()) strcpy(finsh_prompt, _MSH_PROMPT); else @@ -89,7 +119,6 @@ const char *finsh_get_prompt() return finsh_prompt; } -#endif /** * @ingroup finsh diff --git a/components/finsh/shell.h b/components/finsh/shell.h index e01458adc5..d0fd66ee78 100644 --- a/components/finsh/shell.h +++ b/components/finsh/shell.h @@ -44,12 +44,10 @@ #endif #define FINSH_OPTION_ECHO 0x01 -#if defined(FINSH_USING_MSH) || (defined(RT_USING_DFS) && defined(DFS_USING_WORKDIR)) + #define FINSH_PROMPT finsh_get_prompt() const char* finsh_get_prompt(void); -#else -#define FINSH_PROMPT "finsh>>" -#endif +int finsh_set_prompt(const char * prompt); #ifdef FINSH_USING_HISTORY #ifndef FINSH_HISTORY_LINES From efada8768e688504a961c0f185ffbecd46266300 Mon Sep 17 00:00:00 2001 From: aozima Date: Mon, 2 Jul 2018 19:46:37 +0800 Subject: [PATCH 3/3] [shell] set prompt mode is default enable. --- components/finsh/shell.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/components/finsh/shell.c b/components/finsh/shell.c index 0109a761bc..29034bb286 100644 --- a/components/finsh/shell.c +++ b/components/finsh/shell.c @@ -34,6 +34,7 @@ * 2011-02-23 Bernard fix variable section end issue of finsh shell * initialization when use GNU GCC compiler. * 2016-11-26 armink add password authentication + * 2018-07-02 aozima add custome prompt support. */ #include @@ -853,6 +854,7 @@ int finsh_system_init(void) #endif /* RT_USING_HEAP */ rt_sem_init(&(shell->rx_sem), "shrx", 0, 0); + finsh_set_prompt_mode(1); if (tid != NULL && result == RT_EOK) rt_thread_startup(tid);