1 Star 4 Fork 0

ischen.x/FH_cmdline

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cmdline.c 4.06 KB
一键复制 编辑 原始数据 按行查看 历史
ischen.x 提交于 2024-07-25 20:43 . 完善readme
#include "cmdline.h"
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define CMDLINE_VERSION "1.0"
typedef struct {
int argc; // 参数个数
char **argv; // 参数列表
} Args;
static Cmd cmdline_head_node = {
.cb = NULL,
.next = NULL,
.cmd = NULL,
};
static Args* parseArguments(char *command) {
Args *result = malloc(sizeof(Args));
result->argc = 0;
const char *delim = " "; // 分隔符为空格
// 动态分配内存给参数列表
result->argv = malloc(CMD_ARG_COUNT_MAX * sizeof(char*)); // 假设最多10个参数
if (result->argv == NULL) {
printf("Memory allocation failed.\n");
}
// 使用strtok分割字符串
char *token = strtok(command, delim);
while (token != NULL) {
// 再次动态分配内存给每个参数
result->argv[result->argc] = strdup(token);
if (result->argv[result->argc] == NULL) {
printf("Memory allocation failed.\n");
}
result->argc++;
token = strtok(NULL, delim);
}
return result;
}
// 释放Args结构体中分配的内存
static void freeArguments(Args *args) {
for (int i = 0; i < args->argc; i++) {
free(args->argv[i]);
}
free(args->argv);
free(args);
}
static void completion(const char *buf, linenoiseCompletions *lc) {
Cmd *i = &cmdline_head_node;
while (i->next != NULL) {
if (i->next->cmd[0] == buf[0]) {
linenoiseAddCompletion(lc, i->next->cmd);
}
i = i->next;
}
}
// static char *hints(const char *buf, int *color, int *bold) {
// if (!strcasecmp(buf,"hello")) {
// *color = 35;
// *bold = 0;
// return " World";
// }
// return NULL;
// }
int cmdline_new(const char *cmd, cmdline_cb cb)
{
Cmd *cmd_new = (Cmd*)malloc(sizeof(Cmd));
cmd_new->cmd = strdup(cmd);
cmd_new->cb = cb;
cmd_new->next = NULL;
Cmd *i = &cmdline_head_node;
while (i->next != NULL) {
i = i->next;
}
i->next = cmd_new;
return 0;
}
static Cmd* cmdline_find(const char *cmd)
{
Cmd *i = &cmdline_head_node;
while (i->next != NULL) {
if (strcmp(i->next->cmd, cmd) == 0) {
return i->next;
}
i = i->next;
}
return NULL;
}
int cmdline_del(const char *cmd)
{
Cmd *i = cmdline_find(cmd);
if (i == NULL) {
printf("%s %d:cmd not found\r\n", __FILE__, __LINE__);
return -1;
} else {
free(i->cmd);
free(i);
}
return 0;
}
static void cmdline(char *line_in)
{
if (strlen(line_in) == 0) { // 命令行仅输入回车
return;
}
char *line = strdup(line_in);
Args *args = parseArguments(line);
Cmd *cmd = cmdline_find(args->argv[0]);
if (cmd == NULL) {
printf("%s %d %s: (%s) not found\r\n", __FILE__, __LINE__, __func__, args->argv[0]);
goto err;
} else {
optind = 1;
cmd->cb(args->argc, args->argv);
}
// 释放内存
err:
freeArguments(args);
free(line);
}
static int cmdline_list(int argc, char **argv)
{
Cmd *i = &cmdline_head_node;
while (i->next != NULL) {
printf(" * %s\n", i->next->cmd);
i = i->next;
}
return 0;
}
int cmdine_start(const char *username)
{
char *line;
linenoiseSetCompletionCallback(completion);
// linenoiseSetHintsCallback(hints);
printf(
"\r\n\r\n"
"Welcome to cmdine "CMDLINE_VERSION" (STM32)"
"\r\n\r\n"
" * Use cmdline_list to list all commands"
"\r\n\r\n"
" * Use the tab completion command"
"\r\n\r\n"
" * Use the up and down arrows to browse the history command"
"\r\n\r\n"
"Firmware compile time: %s %s\r\n", __DATE__, __TIME__
);
cmdline_new("cmdlist", cmdline_list);
while (1){
line = linenoise(username);
if (line == NULL) {
printf("\r\nlinenoise err!!!");
break;
}
linenoiseHistoryAdd(line);
cmdline(line);
free(line);
}
return -1;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/ischen-x/fh_cmdline.git
git@gitee.com:ischen-x/fh_cmdline.git
ischen-x
fh_cmdline
FH_cmdline
master

搜索帮助