From 33eb0b9eef3360396aa0f650f8e1e01baf6dc181 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 1 Oct 2023 19:13:06 -0600 Subject: cli: Add a command to show cmdline history There is a function for this but it is never used. Showing the history is a useful feature, so add a new 'history' command. Signed-off-by: Simon Glass --- include/cli.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'include/cli.h') diff --git a/include/cli.h b/include/cli.h index 094a6602d70..ac09c80c784 100644 --- a/include/cli.h +++ b/include/cli.h @@ -229,4 +229,7 @@ void cli_ch_init(struct cli_ch_state *cch); */ int cli_ch_process(struct cli_ch_state *cch, int ichar); +/** cread_print_hist_list() - Print the command-line history list */ +void cread_print_hist_list(void); + #endif -- cgit v1.2.3 From be5c2edd10cc0c5c1b938fc03bc7e7c35801a0bd Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 1 Oct 2023 19:13:11 -0600 Subject: cli: Convert cread_line() to use a struct for the main vars We want to reuse the editing code elsewhere. As a first step, move the common variables into a struct. This will allow us to eventually put the contents of the inner loop in a function, so it can be called from elsewhere. Signed-off-by: Simon Glass --- include/cli.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'include/cli.h') diff --git a/include/cli.h b/include/cli.h index ac09c80c784..1bc1ab8035c 100644 --- a/include/cli.h +++ b/include/cli.h @@ -8,6 +8,7 @@ #define __CLI_H #include +#include /** * struct cli_ch_state - state information for reading cmdline characters @@ -24,6 +25,19 @@ struct cli_ch_state { bool emitting; }; +/** + * struct cli_line_state - state of the line editor + * + * @num: Current cursor position, where 0 is the start + * @eol_num: Number of characters in the buffer + * @insert: true if in 'insert' mode + */ +struct cli_line_state { + uint num; + uint eol_num; + bool insert; +}; + /** * Go into the command loop * -- cgit v1.2.3 From e5509ce87b29f773b045200735bec2903b72eed4 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 1 Oct 2023 19:13:13 -0600 Subject: cli: Create a function to process characters Move most of the inner loop from cread_line() into a new function. This will allow using it from other code. This involves adding a few more members to the state struct. Signed-off-by: Simon Glass --- include/cli.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'include/cli.h') diff --git a/include/cli.h b/include/cli.h index 1bc1ab8035c..bbc53276435 100644 --- a/include/cli.h +++ b/include/cli.h @@ -31,11 +31,16 @@ struct cli_ch_state { * @num: Current cursor position, where 0 is the start * @eol_num: Number of characters in the buffer * @insert: true if in 'insert' mode + * @buf: Buffer containing line + * @prompt: Prompt for the line */ struct cli_line_state { uint num; uint eol_num; + uint len; bool insert; + char *buf; + const char *prompt; }; /** @@ -243,6 +248,16 @@ void cli_ch_init(struct cli_ch_state *cch); */ int cli_ch_process(struct cli_ch_state *cch, int ichar); +/** + * cread_line_process_ch() - Process a character for line input + * + * @cls: CLI line state + * @ichar: Character to process + * Return: 0 if input is complete, with line in cls->buf, -EINTR if input was + * cancelled with Ctrl-C, -EAGAIN if more characters are needed + */ +int cread_line_process_ch(struct cli_line_state *cls, char ichar); + /** cread_print_hist_list() - Print the command-line history list */ void cread_print_hist_list(void); -- cgit v1.2.3 From 8fc041fe4c34bb6108444ee9970151f43add0ce9 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 1 Oct 2023 19:13:15 -0600 Subject: cli: Allow history to be disabled When inputting text outside the command line we don't want history to be accessible. Add an option to control this. Signed-off-by: Simon Glass --- include/cli.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'include/cli.h') diff --git a/include/cli.h b/include/cli.h index bbc53276435..252bdb70ab0 100644 --- a/include/cli.h +++ b/include/cli.h @@ -31,6 +31,7 @@ struct cli_ch_state { * @num: Current cursor position, where 0 is the start * @eol_num: Number of characters in the buffer * @insert: true if in 'insert' mode + * @history: true if history should be accessible * @buf: Buffer containing line * @prompt: Prompt for the line */ @@ -39,6 +40,7 @@ struct cli_line_state { uint eol_num; uint len; bool insert; + bool history; char *buf; const char *prompt; }; -- cgit v1.2.3 From 3b487bf51115bfb9a0f85667a5608fc57788107c Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 1 Oct 2023 19:13:16 -0600 Subject: cli: Allow command completion to be disabled When inputting text outside the command line we don't want to use tab for command completion. Add an option to control this. Signed-off-by: Simon Glass --- include/cli.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'include/cli.h') diff --git a/include/cli.h b/include/cli.h index 252bdb70ab0..ad3cb4499fe 100644 --- a/include/cli.h +++ b/include/cli.h @@ -32,6 +32,7 @@ struct cli_ch_state { * @eol_num: Number of characters in the buffer * @insert: true if in 'insert' mode * @history: true if history should be accessible + * @cmd_complete: true if tab completion should be enabled * @buf: Buffer containing line * @prompt: Prompt for the line */ @@ -41,6 +42,7 @@ struct cli_line_state { uint len; bool insert; bool history; + bool cmd_complete; char *buf; const char *prompt; }; -- cgit v1.2.3 From 39ee32166f607d4e30a74d46f82adb39e4134ec4 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 1 Oct 2023 19:13:17 -0600 Subject: cli: Add a function to set up a new cread Create a init function so that it is easy to use command-line reading. Signed-off-by: Simon Glass --- include/cli.h | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'include/cli.h') diff --git a/include/cli.h b/include/cli.h index ad3cb4499fe..e183d561369 100644 --- a/include/cli.h +++ b/include/cli.h @@ -32,7 +32,8 @@ struct cli_ch_state { * @eol_num: Number of characters in the buffer * @insert: true if in 'insert' mode * @history: true if history should be accessible - * @cmd_complete: true if tab completion should be enabled + * @cmd_complete: true if tab completion should be enabled (requires @prompt to + * be set) * @buf: Buffer containing line * @prompt: Prompt for the line */ @@ -262,6 +263,20 @@ int cli_ch_process(struct cli_ch_state *cch, int ichar); */ int cread_line_process_ch(struct cli_line_state *cls, char ichar); +/** + * cli_cread_init() - Set up a new cread struct + * + * Sets up a new cread state, with history and cmd_complete set to false + * + * After calling this, you can use cread_line_process_ch() to process characters + * received from the user. + * + * @cls: CLI line state + * @buf: Text buffer containing the initial text + * @buf_size: Buffer size, including nul terminator + */ +void cli_cread_init(struct cli_line_state *cls, char *buf, uint buf_size); + /** cread_print_hist_list() - Print the command-line history list */ void cread_print_hist_list(void); -- cgit v1.2.3