[PATCH 13/36] cli: Add a function to set up a new cread
Simon Glass
sjg at chromium.org
Tue Sep 19 20:08:43 CEST 2023
Create a init function so that it is easy to use command-line reading.
Signed-off-by: Simon Glass <sjg at chromium.org>
---
common/cli_readline.c | 24 +++++++++++++++---------
include/cli.h | 17 ++++++++++++++++-
2 files changed, 31 insertions(+), 10 deletions(-)
diff --git a/common/cli_readline.c b/common/cli_readline.c
index 23913a857a9..06b8d465044 100644
--- a/common/cli_readline.c
+++ b/common/cli_readline.c
@@ -424,28 +424,34 @@ int cread_line_process_ch(struct cli_line_state *cls, char ichar)
return -EAGAIN;
}
+void cli_cread_init(struct cli_line_state *cls, char *buf, uint buf_size)
+{
+ int init_len = strlen(buf);
+
+ memset(cls, '\0', sizeof(struct cli_line_state));
+ cls->insert = true;
+ cls->buf = buf;
+ cls->len = buf_size;
+
+ if (init_len)
+ cread_add_str(buf, init_len, 0, &cls->num, &cls->eol_num, buf,
+ buf_size);
+}
+
static int cread_line(const char *const prompt, char *buf, unsigned int *len,
int timeout)
{
struct cli_ch_state s_cch, *cch = &s_cch;
struct cli_line_state s_cls, *cls = &s_cls;
char ichar;
- int init_len = strlen(buf);
int first = 1;
cli_ch_init(cch);
- memset(cls, '\0', sizeof(struct cli_line_state));
- cls->insert = true;
- cls->len = *len;
+ cli_cread_init(cls, buf, *len);
cls->prompt = prompt;
- cls->buf = buf;
cls->history = true;
cls->cmd_complete = true;
- if (init_len)
- cread_add_str(buf, init_len, 1, &cls->num, &cls->eol_num, buf,
- *len);
-
while (1) {
int ret;
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);
--
2.42.0.459.ge4e396fd5e-goog
More information about the U-Boot
mailing list