更新模板到最新状态

This commit is contained in:
LokLiang
2025-02-13 17:17:07 +08:00
parent 4e259f666c
commit f455349861
40 changed files with 1251 additions and 789 deletions

View File

@@ -10,6 +10,7 @@
*/
#include "sh.h"
#include "sh_vset.h"
#include <string.h>
#include <stdlib.h>
@@ -127,7 +128,7 @@ static const sh_key_t *_find_key(sh_t *sh_hdl, const char *code)
* 如果当前为模块模式,由优先在模块中查找。
*
* @param dest_reg_struct[out] 保存根命令结构的定义地址
* @param sub_cmd NULL -- 从根命令中遍查找; ! NULL 从已知的父命令节点中查找
* @param sub_cmd NULL -- 从根命令中遍查找; ! NULL 从已知的父命令节点中查找
* @param cmd 单元命令字符串
* @return const sh_cmd_t* NULL -- 没有记录这个命令; ! NULL 找到的命令节点
*/
@@ -399,8 +400,9 @@ static void _insert_str(sh_t *sh_hdl, const char *str)
*/
static void _read_param(int *argc, const char *argv[CONFIG_SH_MAX_PARAM], char *dest, const char *src)
{
bool end = false;
*argc = 0;
while (*argc < CONFIG_SH_MAX_PARAM)
while (end == false && *argc < CONFIG_SH_MAX_PARAM)
{
int len = 0;
@@ -485,6 +487,13 @@ static void _read_param(int *argc, const char *argv[CONFIG_SH_MAX_PARAM], char *
++src;
dest[len] = '\0';
}
else if (dest[len] == '#' && split_char == ' ') /* 注释 */
{
++src;
dest[len] = '\0';
end = true;
}
if (dest[len] == '\0')
{
break;
@@ -719,6 +728,16 @@ int sh_unregister_cmd(const sh_cmd_reg_t *sh_reg)
return -!ret;
}
/**
* @brief 取消所有已注册命令
*/
void sh_unregister_all(sh_t *sh_hdl)
{
sys_pslist_init(&s_cmd_list);
sys_pslist_init(&s_cmd_list_hide);
sys_pslist_init(&sh_hdl->cmd_list);
}
/**
* @brief 注册一组热键
* 适用于动态加载/卸载
@@ -1563,7 +1582,7 @@ static bool _do_completion_cmd(sh_t *sh_hdl, sh_cp_info_t *info, const sh_cmd_in
while (1)
{
if (sub_cmd == NULL) // 遍根命令链
if (sub_cmd == NULL) // 遍根命令链
{
const sh_cmd_reg_t *sh_reg;
sys_pslist_t *cmd_list[] = {
@@ -1587,7 +1606,7 @@ static bool _do_completion_cmd(sh_t *sh_hdl, sh_cp_info_t *info, const sh_cmd_in
}
}
}
else // 遍子命令数据
else // 遍子命令数据
{
sub_cmd = sub_cmd->sub_fn.sub_cmd;
for (int i = 0; sub_cmd[i].cmd != NULL; i++)
@@ -1603,7 +1622,7 @@ static bool _do_completion_cmd(sh_t *sh_hdl, sh_cp_info_t *info, const sh_cmd_in
info->print_count == 0 && // 没有打印任何可选项
err_status != _SH_CMD_STATUS_Incomplete && // 如果正在输出当前模块下的子命令,则只继续补全子命令
print_match == true && // 只打印匹配的备选项
flag_parent == 0 // 目前仅在 _cmd_select_param() 被置起,此时不再遍根命令链
flag_parent == 0 // 目前仅在 _cmd_select_param() 被置起,此时不再遍根命令链
)
{
sub_cmd = NULL; // 使从根命令链重新查找
@@ -1614,7 +1633,7 @@ static bool _do_completion_cmd(sh_t *sh_hdl, sh_cp_info_t *info, const sh_cmd_in
if (sh_hdl->select_cmd != NULL && // 当前状态为模块模式
info->match_num == 0 && // 没有找到可匹配的命令
err_status != _SH_CMD_STATUS_Incomplete && // 如果正在输出当前模块下的子命令,则只继续补全子命令
sub_cmd != NULL // 未遍过根命令链
sub_cmd != NULL // 未遍过根命令链
)
{
sub_cmd = NULL; // 使从根命令链重新查找
@@ -1739,7 +1758,7 @@ static int _calc_list_algin(sh_cp_info_t *info)
* @param argv 命令字符串指针
* @return sh_cmd_info_t 命令信息
*/
sh_cmd_info_t sh_cmd_info(sh_t *sh_hdl, int argc, const char *argv[])
sh_cmd_info_t sh_cmd_test(sh_t *sh_hdl, int argc, const char *argv[])
{
SYS_ASSERT(sh_hdl != NULL, "");
@@ -1808,12 +1827,12 @@ static sh_cp_op_t _sh_completion_cmd(sh_t *sh_hdl, int argc, const char *argv[],
/* 初始化 cmd_info 和 cp_info */
if (_arg_is_end(sh_hdl))
{
cmd_info = sh_cmd_info(sh_hdl, argc, argv);
cmd_info = sh_cmd_test(sh_hdl, argc, argv);
cp_info = _completion_init("");
}
else
{
cmd_info = sh_cmd_info(sh_hdl, --argc, argv);
cmd_info = sh_cmd_test(sh_hdl, --argc, argv);
if (cmd_info.err_status == _SH_CMD_STATUS_Success)
{
cp_info = _completion_init(argv[argc++]);
@@ -1892,7 +1911,7 @@ static sh_cp_op_t _sh_completion_cmd(sh_t *sh_hdl, int argc, const char *argv[],
}
else
{
cmd_info = sh_cmd_info(sh_hdl, argc, argv);
cmd_info = sh_cmd_test(sh_hdl, argc, argv);
cp_info = _completion_init(""); // 没有任何命令时直接打印所有根命令
}
@@ -1923,20 +1942,6 @@ static sh_cp_op_t _sh_completion_cmd(sh_t *sh_hdl, int argc, const char *argv[],
return sh_hdl->cp_operate;
}
/**
* @brief 根据输入的参数,结合当前命令行的状态,在已注册命令中查找并自执行一次自动补全命令/打印备选命令的全过程
*
* @param argc 命令数量
* @param argv 命令字符串指针
* @return sh_cp_op_t 自动补全命令/打印备选命令的执行结果
*/
sh_cp_op_t sh_completion_cmd(sh_t *sh_hdl, int argc, const char *argv[])
{
SYS_ASSERT(sh_hdl != NULL, "");
return _sh_completion_cmd(sh_hdl, argc, argv, 0);
}
/**
* @brief 仅在 cp_fn() 中可用,根据当前命令行内容,在给出的参数结构中查找并自动补全参数/打印备选参数的全过程
*
@@ -1998,7 +2003,8 @@ sh_cp_op_t sh_completion_param(sh_t *sh_hdl, const sh_cp_param_t *param)
* 自动补全/打印备行项 将在 cp_fn() 退出后自动执行。
* @ref @c fatfs_shell.c@_scan_files()/@_cmd_df_cp()
*
* @param arg_str 指定已输入的待自动补全的字符串当为NULL时表示按当前命令行自动填充
* @param arg_str 指定已输入的待自动补全的字符串当为NULL时表示按当前命令行自动填充
* 使用场景:在自动补全路径时, arg_str 表示仅关注路径的最后一部分,而不是整个路径,以便于在补全时只显示当前目录下的文件。
* @param res_str 候选字符串
* @param help 帮助描述信息。如果值为 NULL 则不会被打印
*/
@@ -2106,7 +2112,7 @@ void sh_ctrl_tab(sh_t *sh_hdl)
_read_param(&argc, argv, cmd_param, cmd_param);
/* 自动补全命令 */
sh_completion_cmd(sh_hdl, argc, argv);
_sh_completion_cmd(sh_hdl, argc, argv, 0);
}
/**
@@ -2133,7 +2139,7 @@ bool sh_ctrl_enter(sh_t *sh_hdl)
_apply_history(sh_hdl);
/* 根据 argc, argv解析出 info */
sh_cmd_info_t info = sh_cmd_info(sh_hdl, argc, argv);
sh_cmd_info_t info = sh_cmd_test(sh_hdl, argc, argv);
if (info.err_status == _SH_CMD_STATUS_Success)
{
_clear_argv(argc, argv);
@@ -2791,7 +2797,7 @@ SH_CMD_FN(_cmd_select)
{
if (argc)
{
sh_cmd_info_t cmd_info = sh_cmd_info(sh_hdl, argc, argv);
sh_cmd_info_t cmd_info = sh_cmd_test(sh_hdl, argc, argv);
if (cmd_info.err_status == _SH_CMD_STATUS_Incomplete)
{
sh_hdl->select_reg_struct = cmd_info.reg_struct;
@@ -2875,7 +2881,7 @@ SH_CMD_FN(_cmd_exit)
SH_CMD_FN(_cmd_print_help)
{
sh_cmd_info_t info = sh_cmd_info(sh_hdl, argc, argv);
sh_cmd_info_t info = sh_cmd_test(sh_hdl, argc, argv);
if (argc == 0)
{
@@ -2905,7 +2911,7 @@ SH_CMD_FN(_cmd_print_help)
total_len += 3;
for (int i = 1; i <= info.match_count; i++)
{
sh_cmd_info_t tmp = sh_cmd_info(sh_hdl, i, argv);
sh_cmd_info_t tmp = sh_cmd_test(sh_hdl, i, argv);
if (i == 1)
{
@@ -2949,7 +2955,7 @@ SH_CMD_FN(_cmd_print_help)
SH_CMD_CP_FN(_cmd_print_help_param)
{
sh_completion_cmd(sh_hdl, argc, argv);
_sh_completion_cmd(sh_hdl, argc, argv, 0);
}
SH_CMD_FN(_cmd_echo_on)
@@ -3025,6 +3031,13 @@ SH_REGISTER_CMD(
SH_SETUP_CMD("exit", "Exit parent command or disconnect", _cmd_exit, NULL), //
);
static int _vset_init(void)
{
vset_init(&g_uart_handle_vt100, NULL);
return 0;
}
INIT_EXPORT_COMPONENT(_vset_init);
/**
* @brief 用于显式初始化内部函数。
* 当自动初始化函数不适用时,使用此函数可强制初始化内部的一些功能。包含:
@@ -3047,5 +3060,7 @@ void sh_register_external(sh_vprint_fn out)
static uint8_t bank_mem[CONFIG_SH_MAX_LINE_LEN];
sh_config_history_mem(&g_uart_handle_vt100, history_mem, sizeof(history_mem));
sh_config_backup_mem(&g_uart_handle_vt100, bank_mem, sizeof(bank_mem));
_vset_init();
}
}