feat(console): Add max_cmdline_args to esp_console_repl_config_t

This commit is contained in:
Guillaume Souchere
2026-05-04 07:59:15 +02:00
parent 4170686dca
commit ec576cfde3
5 changed files with 82 additions and 3 deletions
+2
View File
@@ -55,6 +55,7 @@ typedef struct {
BaseType_t task_core_id; //!< repl task affinity, i.e. which core the task is pinned to
const char *prompt; //!< prompt (NULL represents default: "esp> ")
size_t max_cmdline_length; //!< maximum length of a command line. If 0, default value will be used
size_t max_cmdline_args; //!< maximum number of command line arguments to parse. If 0, default value will be used
} esp_console_repl_config_t;
/**
@@ -70,6 +71,7 @@ typedef struct {
.task_core_id = tskNO_AFFINITY, \
.prompt = NULL, \
.max_cmdline_length = 0, \
.max_cmdline_args = 0, \
}
typedef enum {
+8 -1
View File
@@ -169,17 +169,23 @@ __attribute__((weak)) esp_err_t esp_console_internal_set_event_fd(esp_console_re
return ESP_OK;
}
esp_err_t esp_console_common_init(size_t max_cmdline_length, esp_console_repl_com_t *repl_com)
esp_err_t esp_console_common_init(size_t max_cmdline_length, size_t max_cmdline_args, esp_console_repl_com_t *repl_com)
{
esp_err_t ret = ESP_OK;
/* Initialize the console */
esp_console_config_t console_config = ESP_CONSOLE_CONFIG_DEFAULT();
repl_com->max_cmdline_length = console_config.max_cmdline_length;
repl_com->max_cmdline_args = console_config.max_cmdline_args;
/* Replace the default command line length if passed as a parameter */
if (max_cmdline_length != 0) {
console_config.max_cmdline_length = max_cmdline_length;
repl_com->max_cmdline_length = max_cmdline_length;
}
/* Replace the default command line args if passed as a parameter */
if (max_cmdline_args != 0) {
console_config.max_cmdline_args = max_cmdline_args;
repl_com->max_cmdline_args = max_cmdline_args;
}
#if CONFIG_LOG_COLORS
console_config.hint_color = atoi(LOG_COLOR_CYAN);
@@ -288,6 +294,7 @@ esp_err_t esp_console_new_repl_stdio(const esp_console_repl_config_t *repl_confi
/* initialize console, common part */
ret = esp_console_common_init(repl_config->max_cmdline_length,
repl_config->max_cmdline_args,
&universal_repl->repl_com);
if (ret != ESP_OK) {
goto _exit;
+3 -1
View File
@@ -72,7 +72,9 @@ static esp_err_t esp_console_new_repl_uart_legacy(const esp_console_dev_uart_con
}
// initialize console, common part
ret = esp_console_common_init(repl_config->max_cmdline_length, &uart_repl->repl_com);
ret = esp_console_common_init(repl_config->max_cmdline_length,
repl_config->max_cmdline_args,
&uart_repl->repl_com);
if (ret != ESP_OK) {
goto _exit;
}
@@ -41,6 +41,7 @@ typedef struct {
const char *history_save_path;
TaskHandle_t task_hdl; // REPL task handle
size_t max_cmdline_length; // Maximum length of a command line. If 0, default value will be used.
size_t max_cmdline_args; // Maximum number of command line arguments to parse. If 0, default value will be used.
} esp_console_repl_com_t;
typedef struct {
@@ -56,7 +57,7 @@ _Static_assert(offsetof(esp_console_repl_universal_t, repl_com) == 0,
void esp_console_repl_task(void *args);
esp_err_t esp_console_common_init(size_t max_cmdline_length, esp_console_repl_com_t *repl_com);
esp_err_t esp_console_common_init(size_t max_cmdline_length, size_t max_cmdline_args, esp_console_repl_com_t *repl_com);
esp_err_t esp_console_common_deinit(esp_console_repl_com_t *repl_com);
esp_err_t esp_console_internal_set_event_fd(esp_console_repl_com_t *repl_com);
esp_err_t esp_console_setup_prompt(const char *prompt, esp_console_repl_com_t *repl_com);
@@ -37,6 +37,26 @@ typedef struct {
static esp_console_repl_t *s_repl = NULL;
/* Shared state for the repl config limits test */
static int s_cmd_captured_argc;
static char s_cmd_captured_last_arg[32];
static int do_capture_args_cmd(int argc, char **argv)
{
s_cmd_captured_argc = argc;
if (argc > 1) {
strncpy(s_cmd_captured_last_arg, argv[argc - 1], sizeof(s_cmd_captured_last_arg) - 1);
s_cmd_captured_last_arg[sizeof(s_cmd_captured_last_arg) - 1] = '\0';
}
return 0;
}
static const esp_console_cmd_t s_capture_args_cmd = {
.command = "capture",
.help = "Capture argc and last arg for testing",
.func = do_capture_args_cmd,
};
static int do_hello_cmd_with_context(void *context, int argc, char **argv)
{
cmd_context_t *cmd_context = (cmd_context_t *)context;
@@ -442,3 +462,50 @@ TEST_CASE("esp console repl custom_uart test", "[console][ignore]")
printf("ByeBye\r\n");
}
#endif // !CONFIG_IDF_TARGET_LINUX
TEST_CASE("esp console repl config respects max_cmdline_args and max_cmdline_length", "[console][ignore]")
{
set_leak_threshold(248);
/*
* Use small limits to make truncation observable:
* - max_cmdline_args = 4: split_argv stops when argc reaches argv_size-1 = 3,
* so "capture arg1 arg2 arg3" drops "arg3" and the command receives argc == 3.
* - max_cmdline_length = 20: strlcpy copies at most 19 chars, so the 20-char
* input "capture longarg12345" is truncated to "capture longarg1234" and the
* last argument received by the command is "longarg1234".
*/
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
repl_config.max_cmdline_args = 4;
repl_config.max_cmdline_length = 20;
TEST_ESP_OK(esp_console_new_repl_stdio(&repl_config, &s_repl));
TEST_ESP_OK(esp_console_cmd_register(&s_capture_args_cmd));
/* --- Verify max_cmdline_args ---
* Input has 4 tokens (command + 3 args). The limit allows at most
* argv_size-1 = 3 tokens, so the last token ("arg3") is dropped.
*/
s_cmd_captured_argc = -1;
int ret;
TEST_ESP_OK(esp_console_run("capture arg1 arg2 arg3", &ret));
TEST_ASSERT_EQUAL(0, ret);
TEST_ASSERT_EQUAL(3, s_cmd_captured_argc);
/* --- Verify max_cmdline_length ---
* "capture longarg12345" is exactly 20 chars. strlcpy(buf, input, 20)
* retains at most 19 chars, yielding "capture longarg1234".
* The command therefore receives "longarg1234" as its only argument.
*/
s_cmd_captured_argc = -1;
memset(s_cmd_captured_last_arg, 0, sizeof(s_cmd_captured_last_arg));
TEST_ESP_OK(esp_console_run("capture longarg12345", &ret));
TEST_ASSERT_EQUAL(0, ret);
TEST_ASSERT_EQUAL(2, s_cmd_captured_argc);
TEST_ASSERT_EQUAL_STRING("longarg1234", s_cmd_captured_last_arg);
/* Start and immediately stop the REPL for a clean teardown */
TEST_ESP_OK(esp_console_start_repl(s_repl));
vTaskDelay(pdMS_TO_TICKS(100));
TEST_ESP_OK(esp_console_stop_repl(s_repl));
}