Kaynağa Gözat

Merge pull request #1601 from aozima/shell_dev

update shell prompt.
Bernard Xiong 7 yıl önce
ebeveyn
işleme
33fb280140
2 değiştirilmiş dosya ile 75 ekleme ve 7 silme
  1. 69 3
      components/finsh/shell.c
  2. 6 4
      components/finsh/shell.h

+ 69 - 3
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 <rthw.h>
@@ -56,17 +57,54 @@ 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 <dfs_posix.h>
-#endif
+#endif /* RT_USING_DFS */
+
 const char *finsh_get_prompt()
 {
 #define _MSH_PROMPT "msh "
 #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;
+    }
+
+    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
@@ -82,7 +120,34 @@ const char *finsh_get_prompt()
 
     return finsh_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)
 {
@@ -789,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);

+ 6 - 4
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
@@ -86,6 +84,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 +117,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);