@@ -112,20 +112,20 @@ void finsh_thread_entry_sethook(void (*hook)(void))
112112 * to the internal prompt buffer, ensuring it does not exceed the maximum buffer size.
113113 * If the input prompt is RT_NULL, it prints an error message and returns an error code.
114114 *
115- * @param prompt The new prompt string to set.
115+ * @param text The new prompt string to set.
116116 *
117117 * @return 0 on success, -RT_EINVAL if the prompt is RT_NULL.
118118 */
119- int finsh_set_prompt_word (const char * prompt )
119+ int finsh_set_prompt_text (const char * text )
120120{
121- if (!prompt )
121+ if (!text )
122122 {
123- rt_kprintf ("Invalid prompt!\n" );
123+ rt_kprintf ("Invalid prompt text !\n" );
124124 return - RT_EINVAL ;
125125 }
126126
127127 rt_memset (finsh_prompt , 0 , sizeof (finsh_prompt ));
128- rt_strncpy (finsh_prompt , prompt , RT_CONSOLEBUF_SIZE );
128+ rt_strncpy (finsh_prompt , text , RT_CONSOLEBUF_SIZE );
129129 finsh_prompt_length = rt_strlen (finsh_prompt );
130130
131131 return 0 ;
@@ -136,13 +136,13 @@ int finsh_set_prompt_word(const char *prompt)
136136 *
137137 * This function returns the current prompt string for the FinSH shell.
138138 * If the prompt mode is disabled, it returns an empty string.
139- * If the prompt string is not set, it sets the default prompt ("msh " ).
139+ * If the prompt string is not set, it sets the default prompt (FINSH_PROMPT_TEXT_DEFAULT ).
140140 * When POSIX and workdir support are enabled, it appends the current working directory to the prompt.
141141 * Finally, it appends a '>' character to the prompt if there is enough space.
142142 *
143143 * @return Pointer to the prompt string.
144144 */
145- const char * finsh_get_prompt_word (void )
145+ const char * finsh_get_prompt_text (void )
146146{
147147 rt_size_t len ;
148148
@@ -151,12 +151,12 @@ const char *finsh_get_prompt_word(void)
151151 /* check prompt mode */
152152 if (!shell -> is_prompt )
153153 {
154- finsh_prompt [RT_CONSOLEBUF_SIZE ] = 0 ;
154+ finsh_prompt [RT_CONSOLEBUF_SIZE ] = '\0' ;
155155 return & finsh_prompt [RT_CONSOLEBUF_SIZE ];
156156 }
157157
158158 if (!finsh_prompt [0 ])
159- finsh_set_prompt_word ( FINSH_PROMPT_WORD_DEFAULT );
159+ finsh_set_prompt_text ( FINSH_PROMPT_TEXT_DEFAULT );
160160
161161 len = finsh_prompt_length ;
162162#if defined(DFS_USING_POSIX ) && defined(DFS_USING_WORKDIR )
@@ -167,7 +167,7 @@ const char *finsh_get_prompt_word(void)
167167 if ((len + 2 ) < RT_CONSOLEBUF_SIZE )
168168 {
169169 finsh_prompt [len ++ ] = '>' ;
170- finsh_prompt [len ++ ] = 0 ;
170+ finsh_prompt [len ] = '\0' ;
171171 }
172172
173173 return finsh_prompt ;
@@ -179,12 +179,12 @@ const char *finsh_get_prompt_word(void)
179179 * This function enables or disables the prompt mode for the FinSH shell.
180180 * If the parameter is 0, the prompt mode is disabled; any other value enables the prompt mode.
181181 *
182- * @param prompt The prompt mode flag (0 to disable, non-zero to enable).
182+ * @param enabled The prompt mode flag (RT_FALSE to disable, RT_TRUE to enable).
183183 */
184- void finsh_set_prompt (rt_bool_t prompt )
184+ void finsh_set_prompt_enabled (rt_bool_t enabled )
185185{
186186 RT_ASSERT (shell != RT_NULL );
187- shell -> is_prompt = prompt ;
187+ shell -> is_prompt = enabled ;
188188}
189189
190190/**
@@ -195,7 +195,7 @@ void finsh_set_prompt(rt_bool_t prompt)
195195 *
196196 * @return The prompt mode status: 0 means prompt mode is disabled, non-zero means enabled.
197197 */
198- rt_bool_t finsh_get_prompt (void )
198+ rt_bool_t finsh_get_prompt_enabled (void )
199199{
200200 RT_ASSERT (shell != RT_NULL );
201201 return shell -> is_prompt ;
@@ -206,10 +206,10 @@ static rt_err_t finsh_rx_ind(rt_device_t dev, rt_size_t size)
206206{
207207 RT_ASSERT (shell != RT_NULL );
208208
209- if (size )
209+ if (size > 0 )
210210 rt_sem_release (& shell -> rx_notice );
211211
212- return 0 ;
212+ return RT_EOK ;
213213}
214214
215215/**
@@ -274,7 +274,7 @@ void finsh_set_device(const char *device_name)
274274}
275275
276276/**
277- * Get the name of the current input device used by the finsh shell.
277+ * @brief Get the name of the current input device used by the finsh shell.
278278 *
279279 * @return The name of the device as a string.
280280 */
@@ -286,17 +286,17 @@ const char *finsh_get_device(void)
286286#endif /* !defined(RT_USING_POSIX_STDIO) && defined(RT_USING_DEVICE) */
287287
288288/**
289- * Set the echo mode of the finsh shell.
289+ * @brief Set the echo mode of the finsh shell.
290290 *
291291 * When echo mode is enabled, the shell will display user input characters.
292292 * When disabled, user input will not be echoed to the terminal.
293293 *
294- * @param echo RT_TRUE to enable echo, RT_FALSE to disable echo .
294+ * @param enabled The echo mode flag ( RT_FALSE to disable, RT_TRUE to enable) .
295295 */
296- void finsh_set_echo (rt_bool_t echo )
296+ void finsh_set_echo_enabled (rt_bool_t enabled )
297297{
298298 RT_ASSERT (shell != RT_NULL );
299- shell -> is_echo = echo ;
299+ shell -> is_echo = enabled ;
300300}
301301
302302/**
@@ -307,7 +307,7 @@ void finsh_set_echo(rt_bool_t echo)
307307 *
308308 * @return RT_TRUE if echo mode is enabled, RT_FALSE otherwise.
309309 */
310- rt_bool_t finsh_get_echo (void )
310+ rt_bool_t finsh_get_echo_enabled (void )
311311{
312312 RT_ASSERT (shell != RT_NULL );
313313 return shell -> is_echo ;
@@ -489,10 +489,11 @@ static struct finsh_history *finsh_history_realloc(struct finsh_history *history
489489 return finsh_history_alloc (cmd , cmd_length );
490490
491491#ifdef RT_USING_HEAP
492- if ( cmd_length > rt_strlen ( history -> cmd ))
493- history -> cmd = (char * )rt_realloc (history -> cmd , cmd_length + 1 );
492+ char * cmd_ptr = history -> cmd ;
493+ history -> cmd = (char * )rt_realloc (cmd_ptr , cmd_length + 1 );
494494 if (!history -> cmd )
495495 {
496+ rt_free (cmd_ptr );
496497 rt_free (history );
497498 rt_kprintf ("Failed to allocate memory for history command!\n" );
498499 return RT_NULL ;
@@ -523,7 +524,7 @@ static struct finsh_snapshot *finsh_snapshot_alloc(char *cmd, rt_size_t cmd_leng
523524 }
524525 if (i >= FINSH_SNAPSHOT_DEPTH )
525526 {
526- rt_kprintf ("No available snapshot buffer!\n" );
527+ rt_kprintf ("No available snapshot buffer zone !\n" );
527528 return RT_NULL ;
528529 }
529530#else
@@ -577,10 +578,11 @@ static struct finsh_snapshot *finsh_snapshot_realloc(struct finsh_snapshot *snap
577578 return finsh_snapshot_alloc (cmd , cmd_length , cmd_cursor );
578579
579580#ifdef RT_USING_HEAP
580- if ( cmd_length > rt_strlen ( snap -> cmd ))
581- snap -> cmd = (char * )rt_realloc (snap -> cmd , cmd_length + 1 );
581+ char * cmd_ptr = snap -> cmd ;
582+ snap -> cmd = (char * )rt_realloc (cmd_ptr , cmd_length + 1 );
582583 if (!snap -> cmd )
583584 {
585+ rt_free (cmd_ptr );
584586 rt_free (snap );
585587 rt_kprintf ("Failed to allocate memory for snapshot command!\n" );
586588 return RT_NULL ;
@@ -597,6 +599,13 @@ static struct finsh_snapshot *finsh_snapshot_realloc(struct finsh_snapshot *snap
597599}
598600#endif /* FINSH_USING_SNAPSHOT */
599601
602+ rt_inline void _finsh_clear_command (void )
603+ {
604+ rt_memset (shell -> cmd , 0 , sizeof (shell -> cmd ));
605+ shell -> cmd_length = 0 ;
606+ shell -> cmd_cursor = 0 ;
607+ }
608+
600609static int finsh_shell_init (void )
601610{
602611#if !defined(RT_USING_POSIX_STDIO ) && defined(RT_USING_DEVICE )
@@ -624,9 +633,7 @@ static int finsh_shell_init(void)
624633#endif /* !RT_USING_HEAP */
625634#endif /* FINSH_USING_HISTORY */
626635
627- rt_memset (shell -> cmd , 0 , sizeof (shell -> cmd ));
628- shell -> cmd_length = 0 ;
629- shell -> cmd_cursor = 0 ;
636+ _finsh_clear_command ();
630637
631638 shell -> extend_key = 0 ;
632639 shell -> is_insert = RT_FALSE ;
@@ -758,9 +765,9 @@ char finsh_getchar(void)
758765}
759766
760767/* clang-format off */
761- #define finsh_printf (fmt , ...) do { if (shell->is_echo) rt_kprintf(fmt, ##__VA_ARGS__); } while (0)
762- #define finsh_puts (str ) do { if (shell->is_echo) rt_kputs(str); } while (0)
763- #define finsh_putc (ch ) do { if (shell->is_echo) rt_kprintf("%c", ch); } while (0)
768+ #define finsh_printf (fmt , ...) do { if (shell->is_echo) rt_kprintf(fmt, ##__VA_ARGS__); }while (0)
769+ #define finsh_puts (str ) do { if (shell->is_echo) rt_kputs(str); } while (0)
770+ #define finsh_putc (ch ) do { if (shell->is_echo) rt_kprintf("%c", ch); } while (0)
764771/* clang-format on */
765772
766773#ifdef FINSH_USING_SNAPSHOT
@@ -886,10 +893,11 @@ static void finsh_auto_complete(void)
886893static void finsh_render_line (void )
887894{
888895#ifdef _WIN32
896+ #define FINSH_WIN32_CLEAR_WIDTH 60
889897 int i ;
890898
891899 finsh_putc ('\r' );
892- for (i = 0 ; i <= 60 ; i ++ )
900+ for (i = 0 ; i <= FINSH_WIN32_CLEAR_WIDTH ; i ++ )
893901 finsh_putc (' ' );
894902 finsh_putc ('\r' );
895903#else /* _WIN32 */
@@ -1090,9 +1098,7 @@ static void finsh_handle_enter_key(void)
10901098 rt_kprintf ("\n" );
10911099 msh_exec (shell -> cmd , shell -> cmd_length );
10921100 rt_kprintf (FINSH_PROMPT );
1093- rt_memset (shell -> cmd , 0 , sizeof (shell -> cmd ));
1094- shell -> cmd_cursor = 0 ;
1095- shell -> cmd_length = 0 ;
1101+ _finsh_clear_command ();
10961102
10971103#ifdef FINSH_USING_SNAPSHOT
10981104 rt_list_for_each_entry_safe (snap , n , & shell -> snapshot_list , list )
@@ -1196,10 +1202,10 @@ static void finsh_handle_normal_key(char ch)
11961202 }
11971203#endif /* FINSH_USING_SNAPSHOT */
11981204
1199- if (shell -> cmd_length >= FINSH_CMD_SIZE )
1205+ if (shell -> cmd_length > FINSH_CMD_SIZE )
12001206 {
1201- shell -> cmd_length = 0 ;
1202- shell -> cmd_cursor = 0 ;
1207+ _finsh_clear_command () ;
1208+ rt_kprintf ( "Command length exceeds the maximum limit!\n" ) ;
12031209 }
12041210
12051211 if (shell -> cmd_cursor >= shell -> cmd_length )
0 commit comments