重构 shell 相关结构体,更新节点名称以提高代码可读性
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -25,6 +25,16 @@
|
||||
#define CONFIG_SH_USE_STRTOD 1 /* 允许参数解析工具 sh_parse_value() 解析浮点数 */
|
||||
#endif
|
||||
|
||||
#define _SH_MIN_PARAM 10
|
||||
|
||||
#ifndef CONFIG_SH_MAX_PARAM
|
||||
#define CONFIG_SH_MAX_PARAM ((CONFIG_SH_MAX_LINE_LEN / 10) > _SH_MIN_PARAM ? (CONFIG_SH_MAX_LINE_LEN / 10) : _SH_MIN_PARAM) /* 以空格为分隔符的最多命令段数 */
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_SH_MAX_CMD_LEN
|
||||
#define CONFIG_SH_MAX_CMD_LEN ((CONFIG_SH_MAX_PARAM / 10) > 10 ? (CONFIG_SH_MAX_PARAM / 10) : 10) /* 最大单个命令长度(包含结束符 '\0' ) */
|
||||
#endif
|
||||
|
||||
typedef struct sh_obj_def sh_t;
|
||||
|
||||
typedef int (*sh_vprint_fn)(const char *format, va_list va); // 实现发送字符串到终端
|
||||
@@ -44,8 +54,8 @@ typedef struct
|
||||
{
|
||||
const char *arg_str; // 待分析的参数
|
||||
int arg_len; // 待解析的参数长度
|
||||
char match_str[CONFIG_SH_MAX_LINE_LEN / 4 * 4]; // 保存相同的字符部分
|
||||
int list_algin; // 配合 list_fmt ,确定格式中 %-ns 的 n 的值
|
||||
char match_str[CONFIG_SH_MAX_CMD_LEN + 1]; // 保存相同的字符部分
|
||||
int list_algin; // 配合 list_fmt, 确定格式中 %-ns 的 n 的值
|
||||
int match_num; // 可打印的列表的条目数
|
||||
int print_count; // 已打印计算
|
||||
} sh_cp_info_t;
|
||||
@@ -53,6 +63,7 @@ typedef struct
|
||||
typedef enum __packed
|
||||
{
|
||||
SH_CP_OP_NA, // 未发生任何动作
|
||||
SH_CP_OP_ERR, // 输入的命令错误而未发生任何动作
|
||||
SH_CP_OP_CP, // 执行了自动补全
|
||||
SH_CP_OP_PEND, // 正在列举选项
|
||||
SH_CP_OP_LIST, // 完成了列举选项
|
||||
@@ -64,7 +75,7 @@ typedef void (*sh_cp_fn)(sh_t *sh_hdl, int argc, const char *argv[], bool flag);
|
||||
|
||||
typedef enum
|
||||
{
|
||||
_SH_SUB_TYPE_VOID,
|
||||
_SH_SUB_TYPE_NUL,
|
||||
_SH_SUB_TYPE_CPFN,
|
||||
_SH_SUB_TYPE_SUB,
|
||||
} sh_fn_type_t;
|
||||
@@ -91,10 +102,10 @@ typedef struct sh_cmd
|
||||
|
||||
typedef struct
|
||||
{
|
||||
sys_psnode_t *node;
|
||||
const sh_cmd_t *cmd;
|
||||
const char *file;
|
||||
int line;
|
||||
sys_psnode_t *reg_node; // 注意时设置的节点
|
||||
const sh_cmd_t *cmd; // 命令数据
|
||||
const char *file; // 文件
|
||||
int line; // 行号
|
||||
} sh_cmd_reg_t;
|
||||
|
||||
typedef struct
|
||||
@@ -105,7 +116,7 @@ typedef struct
|
||||
|
||||
typedef struct
|
||||
{
|
||||
sys_psnode_t *node;
|
||||
sys_psnode_t *reg_node;
|
||||
const sh_key_t *key;
|
||||
} sh_key_reg_t;
|
||||
|
||||
@@ -113,17 +124,16 @@ typedef struct
|
||||
{
|
||||
enum
|
||||
{
|
||||
_SH_CMD_STATUS_Bad, // 未找到命令
|
||||
_SH_CMD_STATUS_Success, // 成功找到命令函数
|
||||
_SH_CMD_STATUS_Incomplete, // 命令不完整
|
||||
} err_status;
|
||||
_SH_CMD_STATUS_NotFound, // 命令不存在
|
||||
_SH_CMD_STATUS_NeedsSubcommand, // 命令不完整
|
||||
_SH_CMD_STATUS_Success, // 命令找到,可执行
|
||||
} match_status;
|
||||
|
||||
int match_count; // 表示有多少段为成功匹配的命令段
|
||||
unsigned cmd_count; // 命令部分的段落数
|
||||
|
||||
const sh_cmd_reg_t *reg_struct; // 根命令结构的定义地址
|
||||
|
||||
const sh_cmd_t *match_cmd; // 最后一个匹配的命令信息
|
||||
const sh_cmd_reg_t *reg_struct; // 匹配的 sh_cmd_reg_t 对象,用于记录命令
|
||||
|
||||
const sh_cmd_t *match_cmd; // 匹配的 cmd
|
||||
} sh_cmd_info_t;
|
||||
|
||||
typedef struct // 字符串值解释结果
|
||||
@@ -166,15 +176,13 @@ typedef struct sh_obj_def // 对象内存结构
|
||||
|
||||
sys_pslist_t cmd_list; // 已注册的仅属于对应的终端可见的根命令链
|
||||
char cmd_line[CONFIG_SH_MAX_LINE_LEN / 4 * 4]; // 当前命令缓存(包括 模块提示、命令行,不包括 提示符)
|
||||
char *cmd_buf; // 当前命令行在 cmd_line[] 中的指针
|
||||
char *cmd_buf; // 当前命令行在 cmd_line[] 中的指针。用于当 select_argc > 0 时,快速定位在 cmd_line[] 中的输入保存位置
|
||||
uint16_t cmd_stored; // 当前已缓存字符数(不包括 提示符 和 模块提示)
|
||||
uint16_t cmd_input; // 当前光标位置(0.._MAX_CMD_LEN)
|
||||
uint16_t sync_cursor; // 与当前光标同步的位置
|
||||
int cmd_return; // 指令执行的结果
|
||||
|
||||
const sh_cmd_reg_t *select_reg_struct; // 内部 select 命令
|
||||
const sh_cmd_t *select_cmd; // 内部 select 命令
|
||||
|
||||
int select_argc; // 选中的参数数量。当 select_argc > 0 时,表示当前处于模块模式,且已选中了 select_argc 个参数,字符串保存在 cmd_line[] 中,以'\0' 分隔
|
||||
char *cmd_history; // 命令的历史记录
|
||||
int history_size; // cmd_history 的长度
|
||||
uint16_t *history_index; // 历史记录信息
|
||||
@@ -200,7 +208,7 @@ typedef struct sh_obj_def // 对象内存结构
|
||||
#define _SH_DO_CONCAT(X, Y) X##Y
|
||||
#define _SH_CONCAT(X, Y) _SH_DO_CONCAT(X, Y)
|
||||
#define _SH_NAME(NAME) _SH_CONCAT(_SH_CONCAT(_SH_CONCAT(__, NAME), __), __LINE__)
|
||||
#define _SH_GENERIC_SUB(SUB) (__builtin_types_compatible_p(__typeof(SUB), void *) ? _SH_SUB_TYPE_VOID \
|
||||
#define _SH_GENERIC_SUB(SUB) (__builtin_types_compatible_p(__typeof(SUB), void *) ? _SH_SUB_TYPE_NUL \
|
||||
: __builtin_types_compatible_p(__typeof(SUB), __typeof(_sh_generic_cp_fn)) ? _SH_SUB_TYPE_CPFN \
|
||||
: __builtin_types_compatible_p(__typeof(SUB), sh_cmd_t const[]) ? _SH_SUB_TYPE_SUB \
|
||||
: ~0u)
|
||||
@@ -285,7 +293,7 @@ typedef struct sh_obj_def // 对象内存结构
|
||||
static sh_cmd_t const _SH_NAME(cmd_data_)[] = { \
|
||||
__VA_ARGS__{0}}; \
|
||||
static sh_cmd_reg_t const NAME = { \
|
||||
.node = &_SH_NAME(cmd_node_), \
|
||||
.reg_node = &_SH_NAME(cmd_node_), \
|
||||
.cmd = _SH_NAME(cmd_data_), \
|
||||
.file = __FILE__, \
|
||||
.line = __LINE__, \
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
static sh_key_t const _SH_NAME(key_data_)[] = { \
|
||||
__VA_ARGS__{0}}; \
|
||||
static sh_key_reg_t const NAME = { \
|
||||
.node = &_SH_NAME(key_node_), \
|
||||
.reg_node = &_SH_NAME(key_node_), \
|
||||
.key = _SH_NAME(key_data_), \
|
||||
};
|
||||
|
||||
@@ -427,11 +427,11 @@ int sh_init_vt100(sh_t *sh_hdl, sh_vprint_fn vprint, sh_disconnect_fn disconnect
|
||||
sh_hdl->cmd_buf = sh_hdl->cmd_line;
|
||||
|
||||
sh_hdl->obj_key_data = _register_vt100_keys;
|
||||
sh_hdl->obj_key_data.node = &sh_hdl->obj_key_node;
|
||||
sh_hdl->obj_key_data.reg_node = &sh_hdl->obj_key_node;
|
||||
ret |= sh_register_key(sh_hdl, &sh_hdl->obj_key_data);
|
||||
|
||||
sh_hdl->obj_cmd_data = _register_cmd_clear;
|
||||
sh_hdl->obj_cmd_data.node = &sh_hdl->obj_cmd_node;
|
||||
sh_hdl->obj_cmd_data.reg_node = &sh_hdl->obj_cmd_node;
|
||||
ret |= sh_register_key_cmd(sh_hdl, &sh_hdl->obj_cmd_data);
|
||||
|
||||
return -!!ret;
|
||||
|
||||
Reference in New Issue
Block a user