fix(console): Made setting command context less error-prone and clearer

This commit is contained in:
Jakob Hasse
2024-01-11 10:53:29 +08:00
parent 260a2c7562
commit 6031b35803
3 changed files with 70 additions and 88 deletions

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2016-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2016-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -135,8 +135,15 @@ esp_err_t esp_console_cmd_register(const esp_console_cmd_t *cmd)
item->hint = buf;
}
item->argtable = cmd->argtable;
item->func = cmd->func;
item->func_w_context = cmd->func_w_context;
if (cmd->func) {
item->func = cmd->func;
} else {
// cmd->func_w_context is valid here according to check above
item->func_w_context = cmd->func_w_context;
item->context = cmd->context;
}
cmd_item_t *last = NULL;
cmd_item_t *it;
SLIST_FOREACH(it, &s_cmd_list, next) {
@@ -153,22 +160,6 @@ esp_err_t esp_console_cmd_register(const esp_console_cmd_t *cmd)
return ESP_OK;
}
esp_err_t esp_console_cmd_set_context(const char *cmd, void *context)
{
if (cmd == NULL ) {
return ESP_ERR_INVALID_ARG;
}
cmd_item_t *it;
SLIST_FOREACH(it, &s_cmd_list, next) {
if (strcmp(cmd, it->command) == 0) {
it->context = context;
return ESP_OK;
}
}
return ESP_ERR_NOT_FOUND;
}
void esp_console_get_completion(const char *buf, linenoiseCompletions *lc)
{
size_t len = strlen(buf);