Skip to content

Commit b99cde0

Browse files
committed
Refactor shell prompt handling and configuration options, fix some potential bugs
1 parent c78e433 commit b99cde0

File tree

3 files changed

+74
-66
lines changed

3 files changed

+74
-66
lines changed

components/finsh/Kconfig

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,43 +32,45 @@ if RT_USING_MSH
3232
if FINSH_USING_HISTORY
3333
config FINSH_HISTORY_LINES
3434
int "The command history line number"
35-
default 10
35+
default 5
3636
endif
3737

3838
config FINSH_USING_SNAPSHOT
3939
bool "Enable command Ctrl+Z/Ctrl+Y snapshot feature"
4040
default n
41+
help
42+
Enable snapshot feature for command history. Ctrl+Z undoes recent edits,
43+
Ctrl+Y redoes undone edits. This feature requires additional memory
44+
based on FINSH_SNAPSHOT_DEPTH settings.
4145
if FINSH_USING_SNAPSHOT
4246
config FINSH_SNAPSHOT_DEPTH
4347
int "The command snapshot depth"
4448
default 10
49+
help
50+
The number of command snapshots to keep.
4551
endif
4652

4753
config FINSH_USING_WORD_OPERATION
4854
bool "Enable Ctrl+W/Backspace/Ctrl+Left/Right word-based cursor operations"
55+
default n
4956
help
5057
Enable word-based cursor operations in the shell:
5158
- Ctrl+Backspace: delete previous word
5259
- Ctrl+Left/Right Arrow: move cursor by word
5360
- Ctrl+W: delete word before cursor
5461
This feature improves command line editing efficiency.
55-
default n
56-
62+
5763
config FINSH_USING_EXTEND_FEATURE
5864
bool "Enable insert/delete/home/end extend feature"
5965
default n
60-
61-
# Deprecated alias for backward compatibility
62-
# English: Deprecated. Use FINSH_USING_EXTEND_FEATURE instead.
63-
# 中文:已弃用。请使用 FINSH_USING_EXTEND_FEATURE。
64-
config FINSH_USING_FUNC_EXT
65-
bool "DEPRECATED: Use FINSH_USING_EXTEND_FEATURE instead"
66-
default n
67-
select FINSH_USING_EXTEND_FEATURE
6866
help
69-
English: This option is deprecated and will be removed in future releases.
70-
Please migrate to FINSH_USING_EXTEND_FEATURE.
71-
中文:此选项已弃用,未来版本将移除。请迁移到 FINSH_USING_EXTEND_FEATURE。
67+
Enable insert/delete/home/end extend feature in the shell:
68+
- Insert: toggle insert mode
69+
- Delete: delete character backward
70+
- Home: move cursor to the beginning of the command
71+
- End: move cursor to the end of the command
72+
This feature improves command line editing efficiency.
73+
7274
config FINSH_USING_SYMTAB
7375
bool "Using symbol table for commands"
7476
default y
@@ -93,8 +95,8 @@ if RT_USING_MSH
9395
bool "Enable the prompt mode in default"
9496
default y
9597

96-
config FINSH_PROMPT_WORD_DEFAULT
97-
string "The default prompt word"
98+
config FINSH_PROMPT_TEXT_DEFAULT
99+
string "The default prompt text"
98100
default "msh "
99101

100102
config FINSH_USING_AUTH

components/finsh/shell.c

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
600609
static 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)
886893
static 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)

components/finsh/shell.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@
5555
#endif /* !FINSH_SNAPSHOT_DEPTH */
5656
#endif /* FINSH_USING_SNAPSHOT */
5757

58-
#ifndef FINSH_PROMPT_WORD_DEFAULT
59-
#define FINSH_PROMPT_WORD_DEFAULT "msh "
60-
#endif /* !FINSH_PROMPT_WORD_DEFAULT */
58+
#ifndef FINSH_PROMPT_TEXT_DEFAULT
59+
#define FINSH_PROMPT_TEXT_DEFAULT "msh "
60+
#endif /* !FINSH_PROMPT_TEXT_DEFAULT */
6161

6262
enum finsh_input_state
6363
{
@@ -157,20 +157,20 @@ char finsh_getchar(void);
157157
void finsh_thread_entry_sethook(void (*hook)(void));
158158
#endif /* RT_USING_HOOK */
159159

160-
int finsh_set_prompt_word(const char *prompt);
161-
const char *finsh_get_prompt_word(void);
162-
#define FINSH_PROMPT finsh_get_prompt_word()
160+
int finsh_set_prompt_text(const char *text);
161+
const char *finsh_get_prompt_text(void);
162+
#define FINSH_PROMPT finsh_get_prompt_text()
163163

164-
void finsh_set_prompt(rt_bool_t prompt);
165-
rt_bool_t finsh_get_prompt(void);
164+
void finsh_set_prompt_enabled(rt_bool_t enabled);
165+
rt_bool_t finsh_get_prompt_enabled(void);
166166

167167
#if !defined(RT_USING_POSIX_STDIO) && defined(RT_USING_DEVICE)
168168
void finsh_set_device(const char *device_name);
169169
const char *finsh_get_device(void);
170170
#endif /* !defined(RT_USING_POSIX_STDIO) && defined(RT_USING_DEVICE) */
171171

172-
void finsh_set_echo(rt_bool_t echo);
173-
rt_bool_t finsh_get_echo(void);
172+
void finsh_set_echo_enabled(rt_bool_t enabled);
173+
rt_bool_t finsh_get_echo_enabled(void);
174174

175175
#ifdef FINSH_USING_AUTH
176176
int finsh_set_password(const char *password);

0 commit comments

Comments
 (0)