修改支持历史记录和变量个数支持0个

This commit is contained in:
hqm 2023-07-03 21:05:19 +08:00
parent 278e20ec83
commit 579b3b92d0
3 changed files with 24 additions and 20 deletions

View File

@ -9,9 +9,9 @@ extern "C" {
#define XCMD_PRINT_BUF_MAX_LENGTH (64) /* xcmd_print缓存 */ #define XCMD_PRINT_BUF_MAX_LENGTH (64) /* xcmd_print缓存 */
#define XCMD_HISTORY_BUF_SZIE (128) /* 支持的历史记录缓存大小实际历史记录个数取决于记录的命令长度建议内存较小的单片机设置为0 */ #define XCMD_HISTORY_BUF_SZIE (0) /* 支持的历史记录缓存大小实际历史记录个数取决于记录的命令长度建议内存较小的单片机设置为0 */
#define XCMD_PARAM_MAX_NUM (8) /* 支持输入的参数个数 */ #define XCMD_PARAM_MAX_NUM (6) /* 支持输入的参数个数 */
#if XCMD_PRINT_BUF_MAX_LENGTH < (XCMD_LINE_MAX_LENGTH+32) #if XCMD_PRINT_BUF_MAX_LENGTH < (XCMD_LINE_MAX_LENGTH+32)
#undef XCMD_PRINT_BUF_MAX_LENGTH #undef XCMD_PRINT_BUF_MAX_LENGTH

View File

@ -6,6 +6,7 @@
#include "xcmd_cmds.h" #include "xcmd_cmds.h"
#include "xcmd_keys.h" #include "xcmd_keys.h"
#ifndef ENABLE_XCMD_EXPORT
static struct static struct
{ {
xcmd_t head; xcmd_t head;
@ -15,6 +16,7 @@ static struct
{ {
xcmd_key_t head; xcmd_key_t head;
} g_key_list; } g_key_list;
#endif
int xcmd_key_register(xcmd_key_t *keys, uint16_t number) int xcmd_key_register(xcmd_key_t *keys, uint16_t number)
{ {

View File

@ -17,19 +17,19 @@ extern "C"
#endif #endif
typedef int (*cmd_func_t)(int, char **); typedef int (*cmd_func_t)(int, char **);
typedef struct cmd typedef struct cmd
{ {
const char *name; const char *name;
cmd_func_t func; cmd_func_t func;
const char *help; const char *help;
#ifndef ENABLE_XCMD_EXPORT
struct cmd *next; struct cmd *next;
#endif
} xcmd_t; } xcmd_t;
typedef xcmd_t xcmd_key_t; typedef xcmd_t xcmd_key_t;
#ifndef XCMD_PARAM_MAX_NUM #ifndef XCMD_PARAM_MAX_NUM
#define XCMD_PARAM_MAX_NUM (8) /* 支持输入的参数个数 */ #define XCMD_PARAM_MAX_NUM (8) /* 支持输入的参数个数 */
#endif #endif
#define XCMD_CURRENT() ((xcmder_t *)(argv[argc])) #define XCMD_CURRENT() ((xcmder_t *)(argv[argc]))
@ -63,16 +63,18 @@ typedef xcmd_t xcmd_key_t;
xcmd_cmd_##_name = { \ xcmd_cmd_##_name = { \
.name = #_name, \ .name = #_name, \
.func = _func, \ .func = _func, \
.help = _help}; .help = _help, \
.next = NULL};
#define XCMD_EXPORT_KEY(_key, _func, _help) XCMD_USED const xcmd_key_t XCMD_SECTION("_xcmd_key_list") \ #define XCMD_EXPORT_KEY(_key, _func, _help) XCMD_USED const xcmd_key_t XCMD_SECTION("_xcmd_key_list") \
xcmd_key_##_key = { \ xcmd_key_##_key = { \
.key = _key, \ .name = _key, \
.func = _func, \ .func = _func, \
.help = _help}; .help = _help, \
extern xcmd_t _xcmd_cmd_list_start; .next = NULL};
extern xcmd_t _xcmd_cmd_list_end; extern xcmd_t _xcmd_cmd_list_start;
extern xcmd_key_t _xcmd_key_list_start; extern xcmd_t _xcmd_cmd_list_end;
extern xcmd_key_t _xcmd_key_list_end; extern xcmd_key_t _xcmd_key_list_start;
extern xcmd_key_t _xcmd_key_list_end;
#define XCMD_CMD_FOR_EACH(pos) for ((pos) = &_xcmd_cmd_list_start; (pos) < &_xcmd_cmd_list_end; ++(pos)) #define XCMD_CMD_FOR_EACH(pos) for ((pos) = &_xcmd_cmd_list_start; (pos) < &_xcmd_cmd_list_end; ++(pos))
#define XCMD_KEY_FOR_EACH(pos) for ((pos) = &_xcmd_key_list_start; (pos) < &_xcmd_key_list_end; ++(pos)) #define XCMD_KEY_FOR_EACH(pos) for ((pos) = &_xcmd_key_list_start; (pos) < &_xcmd_key_list_end; ++(pos))
#else #else
@ -96,16 +98,16 @@ xcmd_key_t *xcmd_keylist_get(void);
#define XCMD_KEY_FOR_EACH(pos) for ((pos) = xcmd_keylist_get(); (pos); (pos) = (pos)->next) #define XCMD_KEY_FOR_EACH(pos) for ((pos) = xcmd_keylist_get(); (pos); (pos) = (pos)->next)
#endif #endif
#define xcmd_print(xcmder, fmt, args...) xnr_io_printf(&(xcmder)->io, fmt, ##args) #define xcmd_print(xcmder, fmt, args...) xnr_io_printf(&(xcmder)->io, fmt, ##args)
#define xcmd_puts(xcmder, str) xnr_io_puts(&(xcmder)->io, str) #define xcmd_puts(xcmder, str) xnr_io_puts(&(xcmder)->io, str)
#define xcmd_putc(xcmder, c) xnr_io_putc(&(xcmder)->io, c) #define xcmd_putc(xcmder, c) xnr_io_putc(&(xcmder)->io, c)
#define xcmd_getc(xcmder) xnr_io_getc(&(xcmder)->io) #define xcmd_getc(xcmder) xnr_io_getc(&(xcmder)->io)
typedef struct xcmd typedef struct xcmd
{ {
xnr_io_t io; xnr_io_t io;
xnr_key_t key; xnr_key_t key;
#if XCMD_VAR_NUM!=0 #if XCMD_VAR_NUM != 0
xnr_var_tab_t var_tab; xnr_var_tab_t var_tab;
xnr_var_t var_buf[XCMD_VAR_NUM]; xnr_var_t var_buf[XCMD_VAR_NUM];
#endif #endif
@ -117,8 +119,8 @@ typedef struct xcmd
cmd_func_t pre_cmd_cbk; // 输入为命令序列输出重新赋值给argc cmd_func_t pre_cmd_cbk; // 输入为命令序列输出重新赋值给argc
cmd_func_t after_cmd_cbk; // 输入为命令序列,输出未处理 cmd_func_t after_cmd_cbk; // 输入为命令序列,输出未处理
void *user_data; // 用户数据 void *user_data; // 用户数据
int optind; // 选项在参数列表中的位置 int optind; // 选项在参数列表中的位置
char *optarg; // 选项参数的值 char *optarg; // 选项参数的值
bool _initOK; bool _initOK;
} xcmder_t; } xcmder_t;
@ -129,7 +131,7 @@ void xcmd_reg_pre_cmd_cbk(xcmder_t *xcmder, cmd_func_t cbk);
void xcmd_reg_after_cmd_cbk(xcmder_t *xcmder, cmd_func_t cbk); void xcmd_reg_after_cmd_cbk(xcmder_t *xcmder, cmd_func_t cbk);
int xcmd_exec(xcmder_t *xcmder, char *str); int xcmd_exec(xcmder_t *xcmder, char *str);
int xcmd_getopt(xcmder_t *xcmder, int argc, char * const argv[], const char *optstring); int xcmd_getopt(xcmder_t *xcmder, int argc, char *const argv[], const char *optstring);
int xcmd_key_register(xcmd_key_t *keys, uint16_t number); int xcmd_key_register(xcmd_key_t *keys, uint16_t number);
int xcmd_unregister_key(char *key); int xcmd_unregister_key(char *key);