修复一些错误

This commit is contained in:
hqm 2023-06-11 23:19:30 +08:00
parent 91e07ec772
commit 9f9a69a100
17 changed files with 759 additions and 1361 deletions

View File

@ -1,17 +0,0 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/opt/gcc-arm-none-eabi-9-2020-q2-update/bin/arm-none-eabi-gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-arm",
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
}

28
.vscode/settings.json vendored
View File

@ -29,6 +29,32 @@
"ex_keys.h": "c", "ex_keys.h": "c",
"ex_cmds.h": "c", "ex_cmds.h": "c",
"test.h": "c", "test.h": "c",
"ctype.h": "c" "ctype.h": "c",
"string.h": "c",
"stdarg.h": "c",
"stddef.h": "c",
"unistd.h": "c",
"stdlib.h": "c",
"stdio.h": "c",
"xnr_line.h": "c"
},
"cmake.sourceDirectory": "/home/hqm/project/xcmd/example/esp_idf",
"MicroPython.executeButton": [
{
"text": "▶",
"tooltip": "运行",
"alignment": "left",
"command": "extension.executeFile",
"priority": 3.5
} }
],
"MicroPython.syncButton": [
{
"text": "$(sync)",
"tooltip": "同步",
"alignment": "left",
"command": "extension.execute",
"priority": 4
}
]
} }

View File

@ -1,81 +0,0 @@
<mxfile host="65bd71144e">
<diagram id="Lwk3QMz05SXr6urNn0cS" name="第 1 页">
<mxGraphModel dx="927" dy="504" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
<root>
<mxCell id="0"/>
<mxCell id="1" parent="0"/>
<mxCell id="32" value="Node" style="swimlane;fontStyle=0;childLayout=stackLayout;horizontal=1;startSize=26;horizontalStack=0;resizeParent=1;resizeParentMax=0;resizeLast=0;collapsible=1;marginBottom=0;" vertex="1" parent="1">
<mxGeometry x="149" y="320" width="140" height="78" as="geometry"/>
</mxCell>
<mxCell id="33" value="next" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="32">
<mxGeometry y="26" width="140" height="26" as="geometry"/>
</mxCell>
<mxCell id="34" value="prev" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="32">
<mxGeometry y="52" width="140" height="26" as="geometry"/>
</mxCell>
<mxCell id="35" value="Node" style="swimlane;fontStyle=0;childLayout=stackLayout;horizontal=1;startSize=26;horizontalStack=0;resizeParent=1;resizeParentMax=0;resizeLast=0;collapsible=1;marginBottom=0;" vertex="1" parent="1">
<mxGeometry x="329" y="320" width="140" height="78" as="geometry"/>
</mxCell>
<mxCell id="36" value="next" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="35">
<mxGeometry y="26" width="140" height="26" as="geometry"/>
</mxCell>
<mxCell id="37" value="prev" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="35">
<mxGeometry y="52" width="140" height="26" as="geometry"/>
</mxCell>
<mxCell id="38" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.25;entryDx=0;entryDy=0;" edge="1" parent="1" source="33" target="35">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="39" value="Node" style="swimlane;fontStyle=0;childLayout=stackLayout;horizontal=1;startSize=26;horizontalStack=0;resizeParent=1;resizeParentMax=0;resizeLast=0;collapsible=1;marginBottom=0;" vertex="1" parent="1">
<mxGeometry x="509" y="320" width="140" height="78" as="geometry"/>
</mxCell>
<mxCell id="40" value="next" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="39">
<mxGeometry y="26" width="140" height="26" as="geometry"/>
</mxCell>
<mxCell id="41" value="prev" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="39">
<mxGeometry y="52" width="140" height="26" as="geometry"/>
</mxCell>
<mxCell id="42" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.25;entryDx=0;entryDy=0;" edge="1" parent="1" source="36" target="39">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="43" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.25;entryY=0;entryDx=0;entryDy=0;" edge="1" parent="1" source="44" target="32">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="44" value="head" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#60a917;strokeColor=#2D7600;fontColor=#ffffff;" vertex="1" parent="1">
<mxGeometry x="144" y="240" width="80" height="30" as="geometry"/>
</mxCell>
<mxCell id="45" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0.28;entryY=0.989;entryDx=0;entryDy=0;entryPerimeter=0;" edge="1" parent="1" source="40" target="34">
<mxGeometry relative="1" as="geometry">
<mxPoint x="199" y="400" as="targetPoint"/>
<Array as="points">
<mxPoint x="669" y="359"/>
<mxPoint x="669" y="440"/>
<mxPoint x="188" y="440"/>
</Array>
</mxGeometry>
</mxCell>
<mxCell id="46" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=1;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="41" target="37">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="47" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=1;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="37" target="34">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="48" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=1;entryY=0.25;entryDx=0;entryDy=0;" edge="1" parent="1" source="34" target="39">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="129" y="385"/>
<mxPoint x="129" y="490"/>
<mxPoint x="699" y="490"/>
<mxPoint x="699" y="340"/>
</Array>
</mxGeometry>
</mxCell>
<mxCell id="49" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.576;entryY=-0.051;entryDx=0;entryDy=0;entryPerimeter=0;" edge="1" parent="1" source="50" target="35">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="50" value="slider" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#0050ef;strokeColor=#001DBC;fontColor=#ffffff;" vertex="1" parent="1">
<mxGeometry x="369" y="240" width="80" height="30" as="geometry"/>
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>

View File

@ -4,37 +4,44 @@ OBJ += xcmd.o \
xcmd_default_keys.o \ xcmd_default_keys.o \
xcmd_default_cmds.o \ xcmd_default_cmds.o \
linux_main.o \ linux_main.o \
test.o \ xnr_io.o \
ex_keys.o \ xnr_line.o \
ex_list.o \ xnr_history.o \
ex_cmds.o \ xnr_key.o \
socket_cmds.o \ xnr_var.o \
diskio.o \ # test.o \
ff.o \ # ex_keys.o \
ffunicode.o \ # ex_list.o \
fatfs_port.o \ # ex_cmds.o \
fs_cmds.o\ # socket_cmds.o \
fatfs_disk_mmc.o\ # diskio.o \
fatfs_disk_ram.o # ff.o \
# ffunicode.o \
# fatfs_port.o \
# fs_cmds.o\
# fatfs_disk_mmc.o\
# fatfs_disk_ram.o
VPATH := ../../src \ VPATH := ../../src \
../../extensions/test \ ../../third_party/XNanoReadLine \
../../extensions/ex_keys \ # ../../extensions/test \
../../extensions/ex_list \ # ../../extensions/ex_keys \
../../extensions/ex_cmds \ # ../../extensions/ex_list \
../../extensions/net_cmds \ # ../../extensions/ex_cmds \
../../extensions/fs_cmds \ # ../../extensions/net_cmds \
../../extensions/fs_cmds/FatFs/source \ # ../../extensions/fs_cmds \
# ../../extensions/fs_cmds/FatFs/source \
INC += -I./ \ INC += -I./ \
-I../../inc \ -I../../inc \
-I../../extensions/test \ -I../../third_party/XNanoReadLine \
-I../../extensions/ex_keys \ # -I../../extensions/test \
-I../../extensions/ex_list \ # -I../../extensions/ex_keys \
-I../../extensions/ex_cmds \ # -I../../extensions/ex_list \
-I../../extensions/net_cmds \ # -I../../extensions/ex_cmds \
-I../../extensions/fs_cmds/FatFs/source \ # -I../../extensions/net_cmds \
-I../../extensions/fs_cmds \ # -I../../extensions/fs_cmds/FatFs/source \
# -I../../extensions/fs_cmds \
OBJ_WITH_BUILD_DIR:=$(addprefix build/,$(OBJ)) OBJ_WITH_BUILD_DIR:=$(addprefix build/,$(OBJ))

View File

@ -6,22 +6,25 @@
* @Description: In User Settings Edit * @Description: In User Settings Edit
* @FilePath: /xcmd/example/linux/linux_main.c * @FilePath: /xcmd/example/linux/linux_main.c
*/ */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include "xcmd_platform.h" #include "xcmd_platform.h"
#include "xcmd.h" #include "xcmd.h"
#include "test.h" // #include "test.h"
#include "ex_keys.h" // #include "ex_keys.h"
#include "ex_cmds.h" // #include "ex_cmds.h"
#include "ex_list.h" // #include "ex_list.h"
#include "fs_cmds.h" // #include "fs_cmds.h"
#include "socket_cmds.h" // #include "socket_cmds.h"
#include "ff.h" // #include "ff.h"
#include "fatfs_disk_mmc.h" // #include "fatfs_disk_mmc.h"
#include "fatfs_disk_ram.h" // #include "fatfs_disk_ram.h"
FIL g_fp; // FIL g_fp;
int getch(void) int getch(void)
{ {
@ -49,25 +52,49 @@ int getch(void)
return ch; return ch;
} }
int cmd_get_char(uint8_t *ch)
int io_write_t(int fd, const char *buf, size_t len)
{ {
*ch = getch(); return write(fd, buf, len);
return 1;
} }
int cmd_put_char(uint8_t ch) int io_read_t(int fd, char *buf, size_t len)
{ {
putchar(ch); for (int i = 0; i < len; i++)
return 1; {
buf[i] = getch();
}
return len;
} }
static int key_ctr_c(void *pv) static int cmd_ls(int argc, char* argv[])
{
xcmder_t *xcmder = XCMD_CURRENT();
int fd = xnr_io_out_fd(&xcmder->io);
if(fd != STDOUT_FILENO)
{
// 保存原始的标准输出文件描述符
int stdout_backup = dup(STDOUT_FILENO);
dup2(fd, STDOUT_FILENO);
close(fd);
system("ls");
// 恢复标准输出为终端
dup2(stdout_backup, STDOUT_FILENO);
}
else
{
system("ls");
}
return 0;
}
static int key_ctr_c(int argc, char* argv[])
{ {
exit(0); exit(0);
} }
static xcmd_key_t keys[] = static xcmd_key_t keys[] =
{ {
{KEY_CTR_C, key_ctr_c, "ctr+c", NULL}, {KEY_CTR_C, key_ctr_c, "ctr+c", NULL},
}; };
@ -76,41 +103,91 @@ void user_keys_init(void)
xcmd_key_register(keys, sizeof(keys) / sizeof(xcmd_key_t)); xcmd_key_register(keys, sizeof(keys) / sizeof(xcmd_key_t));
} }
void fatfs_test(char* path) // void fatfs_test(char* path)
// {
// FRESULT res;
// res = f_open(&g_fp, path, FA_CREATE_ALWAYS | FA_WRITE | FA_READ);
// if(res == FR_OK)
// {
// char buf[128];
// strcpy(buf, "hello world");
// int len = strlen(buf);
// UINT bw;
// UINT br;
// res = f_write(&g_fp, buf, len, &bw);
// memset(buf, 0, len);
// f_lseek(&g_fp,0);
// res = f_read(&g_fp, buf, len, &br);
// printf("test file:%s\r\n", buf);
// f_close(&g_fp);
// }
// }
static int pre_cmd(int argc, char* argv[])
{ {
FRESULT res; xcmder_t *xcmder = XCMD_CURRENT();
res = f_open(&g_fp, path, FA_CREATE_ALWAYS | FA_WRITE | FA_READ); for(int i=0; i< argc; i++)
if(res == FR_OK)
{ {
char buf[128]; if(strcmp(argv[i], ">") == 0)
strcpy(buf, "hello world"); {
int len = strlen(buf); if(i < argc-1)
UINT bw; {
UINT br; int fd = open(argv[i+1], O_WRONLY | O_TRUNC | O_CREAT, 0644);
res = f_write(&g_fp, buf, len, &bw); xnr_io_set_out_fd(&xcmder->io, fd);
memset(buf, 0, len);
f_lseek(&g_fp,0);
res = f_read(&g_fp, buf, len, &br);
printf("test file:%s\r\n", buf);
f_close(&g_fp);
} }
return i;
}
}
return argc;
}
static int after_cmd(int argc, char* argv[])
{
xcmder_t *xcmder = XCMD_CURRENT();
int fd = xnr_io_out_fd(&xcmder->io);
if(STDOUT_FILENO != fd)
{
close(fd);
xnr_io_set_out_fd(&xcmder->io, STDOUT_FILENO);
}
return argc;
}
#define HELP_LS ("like linux \"ls\"")
XCMD_EXPORT_CMD(ls, cmd_ls, HELP_LS)
static xcmd_t cmds[] =
{
#ifndef ENABLE_XCMD_EXPORT
{"ls", cmd_ls, HELP_LS, NULL},
#endif
};
void linux_cmds_init(void)
{
xcmd_cmd_register(cmds, sizeof(cmds)/sizeof(xcmd_t));
} }
int main(void) int main(void)
{ {
xcmd_init(cmd_get_char, cmd_put_char); xcmder_t xcmder;
ram_disk_init(); xcmd_init(&xcmder, io_write_t, io_read_t);
mmc_disk_init(); xcmd_reg_pre_cmd_cbk(&xcmder, pre_cmd);
test_cmd_init(); xcmd_reg_after_cmd_cbk(&xcmder, after_cmd);
test_keys_init();
user_keys_init(); user_keys_init();
ex_keys_init(); linux_cmds_init();
ex_cmds_init(); // ram_disk_init();
socket_cmds_init(); // mmc_disk_init();
fs_cmds_init(); // test_cmd_init();
ex_list_init(); // test_keys_init();
// ex_keys_init();
// ex_cmds_init();
// socket_cmds_init();
// fs_cmds_init();
// ex_list_init();
while (1) while (1)
{ {
xcmd_task(); xcmd_task(&xcmder);
} }
} }

View File

@ -13,8 +13,6 @@
extern "C" { extern "C" {
#endif #endif
#include "xcmd_define.h"
#define XCMD_LINE_MAX_LENGTH (128) /* 命令行支持的最大字符数 */ #define XCMD_LINE_MAX_LENGTH (128) /* 命令行支持的最大字符数 */
#define XCMD_HISTORY_MAX_NUM (16) /* 支持的历史记录个数, 这个参数对内存的影响很大建议arduino设置为00为不支持 */ #define XCMD_HISTORY_MAX_NUM (16) /* 支持的历史记录个数, 这个参数对内存的影响很大建议arduino设置为00为不支持 */
#define XCMD_PARAM_MAX_NUM (64) /* 支持输入的参数个数 */ #define XCMD_PARAM_MAX_NUM (64) /* 支持输入的参数个数 */

View File

@ -13,13 +13,14 @@
static int cmd_run(int argc, char* argv[]) static int cmd_run(int argc, char* argv[])
{ {
xcmder_t *xcmder = XCMD_CURRENT();
if(argc >= 2) if(argc >= 2)
{ {
xcmd_exec(argv[1]); xcmd_exec(argv[1]);
} }
else else
{ {
xcmd_print("%s\r\n", HELP_RUN); xcmd_print(xcmder, "%s\r\n", HELP_RUN);
} }
return 0; return 0;
} }

View File

@ -4,59 +4,65 @@
#define IS_ALPHA(c) ( (c>='A' && c<='Z') || (c>='a' && c<='z') ) #define IS_ALPHA(c) ( (c>='A' && c<='Z') || (c>='a' && c<='z') )
#define IS_NUMBER(c) (c>='0' && c<='9') #define IS_NUMBER(c) (c>='0' && c<='9')
static int xcmd_ctr_a(void *pv) static int xcmd_ctr_a(int argc, char* argv[])
{ {
/* 移动光标到头 */ /* 移动光标到头 */
xcmd_display_cursor_set(0); xcmder_t *xcmder = XCMD_CURRENT();
xnr_line_cursor_set(&xcmder->line, 0);
return 0; return 0;
} }
static int xcmd_ctr_e(void *pv) static int xcmd_ctr_e(int argc, char* argv[])
{ {
/* 移动光标到尾 */ /* 移动光标到尾 */
xcmd_display_cursor_set(-1); xcmder_t *xcmder = XCMD_CURRENT();
xnr_line_cursor_set(&xcmder->line, XCMD_LINE_MAX_LENGTH);
return 0; return 0;
} }
static int xcmd_ctr_u(void *pv) static int xcmd_ctr_u(int argc, char* argv[])
{ {
/* 删除光标左边的所有字符 */ /* 删除光标左边的所有字符 */
uint16_t pos = xcmd_display_cursor_get(); xcmder_t *xcmder = XCMD_CURRENT();
for(uint16_t i=0; i<pos; i++) int pos = xnr_line_cursor(&xcmder->line);
for(int i=0; i<pos; i++)
{ {
xcmd_display_delete_char(); xnr_line_delete(&xcmder->line);
} }
return 0; return 0;
} }
static int xcmd_ctr_k(void *pv) static int xcmd_ctr_k(int argc, char* argv[])
{ {
/* 删除光标右边的所有字符 */ /* 删除光标右边的所有字符 */
uint16_t pos = xcmd_display_cursor_get(); xcmder_t *xcmder = XCMD_CURRENT();
xcmd_display_cursor_set(-1); int pos = xnr_line_cursor(&xcmder->line);
xnr_line_cursor_set(&xcmder->line, XCMD_LINE_MAX_LENGTH);
while(1) while(1)
{ {
if(xcmd_display_cursor_get() == pos) if(xnr_line_cursor(&xcmder->line) == pos)
{ {
break; break;
} }
xcmd_display_delete_char(); xnr_line_delete(&xcmder->line);
} }
return 0; return 0;
} }
static int xcmd_ctr_l(void *pv) static int xcmd_ctr_l(int argc, char* argv[])
{ {
xcmd_exec("clear"); xcmder_t *xcmder = XCMD_CURRENT();
xcmd_exec(xcmder, "clear");
return 0; return 0;
} }
static int xcmd_ctr_left(void *pv) static int xcmd_ctr_left(int argc, char* argv[])
{ {
char *line = xcmd_display_get(); xcmder_t *xcmder = XCMD_CURRENT();
uint16_t pos = xcmd_display_cursor_get(); char *line = xnr_line_line(&xcmder->line);
uint16_t pos = xnr_line_cursor(&xcmder->line);
while(pos) while(pos)
{ {
pos--; pos--;
@ -74,15 +80,16 @@ static int xcmd_ctr_left(void *pv)
} }
pos--; pos--;
} }
xcmd_display_cursor_set(pos); xnr_line_cursor_set(&xcmder->line, pos);
return 0; return 0;
} }
static int xcmd_ctr_right(void *pv) static int xcmd_ctr_right(int argc, char* argv[])
{ {
char *line = xcmd_display_get(); xcmder_t *xcmder = XCMD_CURRENT();
uint16_t pos = xcmd_display_cursor_get(); char *line = xnr_line_line(&xcmder->line);
uint16_t pos = xnr_line_cursor(&xcmder->line);
while(line[pos++]) while(line[pos++])
{ {
if(IS_ALPHA(line[pos]) || IS_NUMBER(line[pos])) if(IS_ALPHA(line[pos]) || IS_NUMBER(line[pos]))
@ -98,7 +105,7 @@ static int xcmd_ctr_right(void *pv)
break; break;
} }
} }
xcmd_display_cursor_set(pos); xnr_line_cursor_set(&xcmder->line, pos);
return 0; return 0;
} }

View File

@ -12,6 +12,7 @@ struct student
static int list_test(int argc, char* argv[]) static int list_test(int argc, char* argv[])
{ {
xcmder_t *xcmder = XCMD_CURRENT();
struct student *p; struct student *p;
struct student stu1; struct student stu1;
struct student stu2; struct student stu2;
@ -55,6 +56,7 @@ static int list_test(int argc, char* argv[])
static int list_note_del(int argc, char* argv[]) static int list_note_del(int argc, char* argv[])
{ {
xcmder_t *xcmder = XCMD_CURRENT();
struct student *p; struct student *p;
struct student stu1; struct student stu1;
struct student stu2; struct student stu2;
@ -99,6 +101,7 @@ static int list_note_del(int argc, char* argv[])
static int list_note_rep(int argc, char* argv[]) static int list_note_rep(int argc, char* argv[])
{ {
xcmder_t *xcmder = XCMD_CURRENT();
struct student *p; struct student *p;
struct student stu1; struct student stu1;
struct student stu2; struct student stu2;
@ -142,6 +145,7 @@ static int list_note_rep(int argc, char* argv[])
static int list_note_del_insert(int argc, char* argv[]) static int list_note_del_insert(int argc, char* argv[])
{ {
xcmder_t *xcmder = XCMD_CURRENT();
struct student *p; struct student *p;
struct student stu1; struct student stu1;
struct student stu2; struct student stu2;
@ -183,6 +187,7 @@ static int list_note_del_insert(int argc, char* argv[])
static int List_to_merge(int argc, char* argv[]) static int List_to_merge(int argc, char* argv[])
{ {
xcmder_t *xcmder = XCMD_CURRENT();
struct student *p; struct student *p;
struct student *q; struct student *q;
struct student stu1; struct student stu1;
@ -235,6 +240,7 @@ static int List_to_merge(int argc, char* argv[])
static int List_traverse(int argc, char* argv[]) static int List_traverse(int argc, char* argv[])
{ {
xcmder_t *xcmder = XCMD_CURRENT();
struct student *p; struct student *p;
struct student stu1; struct student stu1;
struct student stu2; struct student stu2;

View File

@ -69,7 +69,7 @@ static FRESULT scan_files(
break; /* Break on error or end of dir */ break; /* Break on error or end of dir */
if (fno.fattrib & AM_DIR) if (fno.fattrib & AM_DIR)
{ /* It is a directory */ { /* It is a directory */
xcmd_print("\x1B[34m" xcmd_print(xcmder, "\x1B[34m"
"%s" "%s"
"\x1B[0m" "\x1B[0m"
" ", " ",
@ -77,7 +77,7 @@ static FRESULT scan_files(
} }
else else
{ /* It is a file. */ { /* It is a file. */
xcmd_print("%s ", fno.fname); xcmd_print(xcmder, "%s ", fno.fname);
} }
} }
f_closedir(&dir); f_closedir(&dir);
@ -85,7 +85,7 @@ static FRESULT scan_files(
} }
else else
{ {
xcmd_print("%s ", fno.fname); xcmd_print(xcmder, "%s ", fno.fname);
} }
return res; return res;
} }
@ -163,6 +163,7 @@ static FRESULT df(char* path, DWORD* totle_byte, DWORD* free_byte)
static int cmd_df(int argc, char* argv[]) static int cmd_df(int argc, char* argv[])
{ {
xcmder_t *xcmder = XCMD_CURRENT();
FRESULT res; FRESULT res;
DWORD fre_bytes, tot_bytes; DWORD fre_bytes, tot_bytes;
char * disk_path; char * disk_path;
@ -174,11 +175,11 @@ static int cmd_df(int argc, char* argv[])
res = df(disk_path, &tot_bytes, &fre_bytes); res = df(disk_path, &tot_bytes, &fre_bytes);
if (res != FR_OK) if (res != FR_OK)
{ {
xcmd_print("%s Failure:%s\r\n", disk_path, RESAULT_TO_STR(res)); xcmd_print(xcmder, "%s Failure:%s\r\n", disk_path, RESAULT_TO_STR(res));
} }
else else
{ {
xcmd_print("%s %lu/%lu KiB.\r\n", disk_path, fre_bytes/1024, tot_bytes / 1024); xcmd_print(xcmder, "%s %lu/%lu KiB.\r\n", disk_path, fre_bytes/1024, tot_bytes / 1024);
} }
} }
} }
@ -207,7 +208,7 @@ static int cmd_cd(int argc, char *argv[])
res = f_chdir(argv[1]); res = f_chdir(argv[1]);
if (res != FR_OK) if (res != FR_OK)
{ {
xcmd_print("Failure:%s\r\n", RESAULT_TO_STR(res)); xcmd_print(xcmder, "Failure:%s\r\n", RESAULT_TO_STR(res));
} }
else else
{ {
@ -218,7 +219,7 @@ static int cmd_cd(int argc, char *argv[])
} }
else else
{ {
xcmd_print("%s\r\n", HELP_CD); xcmd_print(xcmder, "%s\r\n", HELP_CD);
return -1; return -1;
} }
return 0; return 0;
@ -243,7 +244,7 @@ static int cmd_rm(int argc, char *argv[])
res = rm_dir(argv[2]); res = rm_dir(argv[2]);
if (res != FR_OK) if (res != FR_OK)
{ {
xcmd_print("Failure:%s\r\n", RESAULT_TO_STR(res)); xcmd_print(xcmder, "Failure:%s\r\n", RESAULT_TO_STR(res));
return -1; return -1;
} }
} }
@ -252,19 +253,19 @@ static int cmd_rm(int argc, char *argv[])
res = f_stat(argv[1], &fno); res = f_stat(argv[1], &fno);
if (res != FR_OK) if (res != FR_OK)
{ {
xcmd_print("Failure:%s\r\n", RESAULT_TO_STR(res)); xcmd_print(xcmder, "Failure:%s\r\n", RESAULT_TO_STR(res));
return -1; return -1;
} }
if (fno.fattrib & AM_DIR) if (fno.fattrib & AM_DIR)
{ {
xcmd_print("Failure:%s\r\n", "PATH is DIR"); xcmd_print(xcmder, "Failure:%s\r\n", "PATH is DIR");
} }
else else
{ {
res = f_unlink(argv[1]); res = f_unlink(argv[1]);
if (res != FR_OK) if (res != FR_OK)
{ {
xcmd_print("Failure:%s\r\n", RESAULT_TO_STR(res)); xcmd_print(xcmder, "Failure:%s\r\n", RESAULT_TO_STR(res));
return -1; return -1;
} }
} }
@ -272,7 +273,7 @@ static int cmd_rm(int argc, char *argv[])
} }
else else
{ {
xcmd_print("%s\r\n", HELP_RM); xcmd_print(xcmder, "%s\r\n", HELP_RM);
return -1; return -1;
} }
return 0; return 0;
@ -286,13 +287,13 @@ static int cmd_mv(int argc, char *argv[])
res = f_rename(argv[1], argv[2]); res = f_rename(argv[1], argv[2]);
if (res != FR_OK) if (res != FR_OK)
{ {
xcmd_print("Failure:%s\r\n", RESAULT_TO_STR(res)); xcmd_print(xcmder, "Failure:%s\r\n", RESAULT_TO_STR(res));
return -1; return -1;
} }
} }
else else
{ {
xcmd_print("%s\r\n", HELP_MV); xcmd_print(xcmder, "%s\r\n", HELP_MV);
return -1; return -1;
} }
return 0; return 0;
@ -312,13 +313,13 @@ static int cmd_mkdir(int argc, char *argv[])
res = f_mkdir(argv[1]); res = f_mkdir(argv[1]);
if (res != FR_OK) if (res != FR_OK)
{ {
xcmd_print("Failure:%s\r\n", RESAULT_TO_STR(res)); xcmd_print(xcmder, "Failure:%s\r\n", RESAULT_TO_STR(res));
return 0; return 0;
} }
} }
else else
{ {
xcmd_print("%s\r\n", HELP_MKDIR); xcmd_print(xcmder, "%s\r\n", HELP_MKDIR);
return -1; return -1;
} }
return 0; return 0;
@ -332,14 +333,14 @@ static int cmd_touch(int argc, char *argv[])
res = f_open(&fp, argv[1], FA_CREATE_NEW); res = f_open(&fp, argv[1], FA_CREATE_NEW);
if ((res != FR_OK) && (res != FR_EXIST)) if ((res != FR_OK) && (res != FR_EXIST))
{ {
xcmd_print("Failure:%s\r\n", RESAULT_TO_STR(res)); xcmd_print(xcmder, "Failure:%s\r\n", RESAULT_TO_STR(res));
return -1; return -1;
} }
f_close(&fp); f_close(&fp);
} }
else else
{ {
xcmd_print("%s\r\n", HELP_TOUCH); xcmd_print(xcmder, "%s\r\n", HELP_TOUCH);
return -1; return -1;
} }
return 0; return 0;
@ -353,7 +354,7 @@ static int cmd_cat(int argc, char *argv[])
res = f_open(&fp, argv[1], FA_READ); res = f_open(&fp, argv[1], FA_READ);
if (res != FR_OK) if (res != FR_OK)
{ {
xcmd_print("Failure:%s\r\n", RESAULT_TO_STR(res)); xcmd_print(xcmder, "Failure:%s\r\n", RESAULT_TO_STR(res));
return -1; return -1;
} }
else else
@ -365,7 +366,7 @@ static int cmd_cat(int argc, char *argv[])
res = f_read(&fp, buf, 128, &br); res = f_read(&fp, buf, 128, &br);
if (res != FR_OK) if (res != FR_OK)
{ {
xcmd_print("Failure:%s\r\n", RESAULT_TO_STR(res)); xcmd_print(xcmder, "Failure:%s\r\n", RESAULT_TO_STR(res));
f_close(&fp); f_close(&fp);
return -1; return -1;
} }
@ -375,14 +376,14 @@ static int cmd_cat(int argc, char *argv[])
} }
for(UINT i=0; i<br; i++) for(UINT i=0; i<br; i++)
{ {
xcmd_print("%c", buf[i]); xcmd_print(xcmder, "%c", buf[i]);
} }
} }
} }
} }
else else
{ {
xcmd_print("%s\r\n", HELP_CAT); xcmd_print(xcmder, "%s\r\n", HELP_CAT);
return -1; return -1;
} }
f_close(&fp); f_close(&fp);

View File

@ -11,7 +11,7 @@
#include "xcmd.h" #include "xcmd.h"
#include "test.h" #include "test.h"
#define EXIT_MESSAGE() xcmd_print(g_cmder, "press \"q\" or \"Q\" to exit!\r\n") #define EXIT_MESSAGE() xcmd_print(xcmder, g_cmder, "press \"q\" or \"Q\" to exit!\r\n")
#define EXIT_CHECK() \ #define EXIT_CHECK() \
do \ do \
@ -31,6 +31,7 @@
static uint8_t param_check(int need, int argc, char*argv[]) static uint8_t param_check(int need, int argc, char*argv[])
{ {
xcmder_t *xcmder = XCMD_CURRENT();
uint8_t i,ret = 0; uint8_t i,ret = 0;
if(need<(argc)) if(need<(argc))
{ {
@ -38,16 +39,16 @@ static uint8_t param_check(int need, int argc, char*argv[])
} }
else else
{ {
xcmd_print("err need %d but input %d:\r\n", need, argc-1); xcmd_print(xcmder, "err need %d but input %d:\r\n", need, argc-1);
xcmd_print("input= "); xcmd_print(xcmder, "input= ");
for(i=0; i<argc; i++) for(i=0; i<argc; i++)
{ {
if(argv[i] != NULL) if(argv[i] != NULL)
{ {
xcmd_print("%s ", argv[i]); xcmd_print(xcmder, "%s ", argv[i]);
} }
} }
xcmd_print("\r\n"); xcmd_print(xcmder, "\r\n");
ret = 0; ret = 0;
} }
return ret; return ret;
@ -55,6 +56,7 @@ static uint8_t param_check(int need, int argc, char*argv[])
static int cmd_example(int argc, char* argv[]) static int cmd_example(int argc, char* argv[])
{ {
xcmder_t *xcmder = XCMD_CURRENT();
uint8_t i; uint8_t i;
if(param_check(1, argc, argv)) if(param_check(1, argc, argv))
{ {
@ -62,21 +64,21 @@ static int cmd_example(int argc, char* argv[])
{ {
for(i=2; i<argc; i++) for(i=2; i<argc; i++)
{ {
xcmd_print("%s\r\n", argv[i]); xcmd_print(xcmder, "%s\r\n", argv[i]);
} }
} }
if(strcmp(argv[1], "-i") == 0) if(strcmp(argv[1], "-i") == 0)
{ {
for(i=2; i<argc; i++) for(i=2; i<argc; i++)
{ {
xcmd_print("%d\r\n", atoi(argv[i])); xcmd_print(xcmder, "%d\r\n", atoi(argv[i]));
} }
} }
if(strcmp(argv[1], "-f") == 0) if(strcmp(argv[1], "-f") == 0)
{ {
for(i=2; i<argc; i++) for(i=2; i<argc; i++)
{ {
xcmd_print("%f\r\n", atof(argv[i])); xcmd_print(xcmder, "%f\r\n", atof(argv[i]));
} }
} }
} }
@ -85,6 +87,7 @@ static int cmd_example(int argc, char* argv[])
static int cmd_history(int argc, char* argv[]) static int cmd_history(int argc, char* argv[])
{ {
xcmder_t *xcmder = XCMD_CURRENT();
uint16_t len = xcmd_history_len(); uint16_t len = xcmd_history_len();
do do
@ -97,7 +100,7 @@ static int cmd_history(int argc, char* argv[])
char *line = xcmd_history_prev(); char *line = xcmd_history_prev();
if(line) if(line)
{ {
xcmd_print("%s\r\n", line); xcmd_print(xcmder, "%s\r\n", line);
} }
else else
{ {
@ -109,6 +112,7 @@ static int cmd_history(int argc, char* argv[])
static int cmd_delete_cmd(int argc, char* argv[]) static int cmd_delete_cmd(int argc, char* argv[])
{ {
xcmder_t *xcmder = XCMD_CURRENT();
int res = 0; int res = 0;
if (argc == 2) if (argc == 2)
{ {
@ -120,12 +124,13 @@ static int cmd_delete_cmd(int argc, char* argv[])
} }
return 0; return 0;
error: error:
xcmd_print("Too many parameters are entered or there is no command\r\n"); xcmd_print(xcmder, "Too many parameters are entered or there is no command\r\n");
return -1; return -1;
} }
static int cmd_delete_key(int argc, char* argv[]) static int cmd_delete_key(int argc, char* argv[])
{ {
xcmder_t *xcmder = XCMD_CURRENT();
int res = 0; int res = 0;
if (argc == 2) if (argc == 2)
{ {
@ -137,35 +142,36 @@ static int cmd_delete_key(int argc, char* argv[])
} }
return 0; return 0;
error: error:
xcmd_print("Too many parameters are entered or there is no command\r\n"); xcmd_print(xcmder, "Too many parameters are entered or there is no command\r\n");
return -1; return -1;
} }
static int cmd_ctr_q(void* pv) static int cmd_ctr_q(void* pv)
{ {
xcmd_print("this is ctr+q\n"); xcmd_print(xcmder, "this is ctr+q\n");
return 0; return 0;
} }
static int cmd_print_color(int argc, char* argv[]) static int cmd_print_color(int argc, char* argv[])
{ {
xcmd_print(TX_DEF "txt_color = DEF \r\n" TX_DEF); xcmder_t *xcmder = XCMD_CURRENT();
xcmd_print(TX_RED "txt_color = RED \r\n" TX_DEF); xcmd_print(xcmder, TX_DEF "txt_color = DEF \r\n" TX_DEF);
xcmd_print(TX_BLACK "txt_color = BLACK \r\n" TX_DEF); xcmd_print(xcmder, TX_RED "txt_color = RED \r\n" TX_DEF);
xcmd_print(TX_GREEN "txt_color = GREEN \r\n" TX_DEF); xcmd_print(xcmder, TX_BLACK "txt_color = BLACK \r\n" TX_DEF);
xcmd_print(TX_YELLOW "txt_color = YELLOW \r\n" TX_DEF); xcmd_print(xcmder, TX_GREEN "txt_color = GREEN \r\n" TX_DEF);
xcmd_print(TX_BLUE "txt_color = BLUE \r\n" TX_DEF); xcmd_print(xcmder, TX_YELLOW "txt_color = YELLOW \r\n" TX_DEF);
xcmd_print(TX_WHITE "txt_color = WHITE \r\n" TX_DEF); xcmd_print(xcmder, TX_BLUE "txt_color = BLUE \r\n" TX_DEF);
xcmd_print(TX_WHITE "txt_color = WHITE \r\n" TX_DEF); xcmd_print(xcmder, TX_WHITE "txt_color = WHITE \r\n" TX_DEF);
xcmd_print(xcmder, TX_WHITE "txt_color = WHITE \r\n" TX_DEF);
xcmd_print(BK_DEF "background_color = BK_DEF" BK_DEF "\r\n"); xcmd_print(xcmder, BK_DEF "background_color = BK_DEF" BK_DEF "\r\n");
xcmd_print(BK_BLACK "background_color = BK_BLACK" BK_DEF "\r\n"); xcmd_print(xcmder, BK_BLACK "background_color = BK_BLACK" BK_DEF "\r\n");
xcmd_print(BK_RED "background_color = BK_RED" BK_DEF "\r\n"); xcmd_print(xcmder, BK_RED "background_color = BK_RED" BK_DEF "\r\n");
xcmd_print(BK_GREEN "background_color = BK_GREEN" BK_DEF "\r\n"); xcmd_print(xcmder, BK_GREEN "background_color = BK_GREEN" BK_DEF "\r\n");
xcmd_print(BK_YELLOW "background_color = BK_YELLOW" BK_DEF "\r\n"); xcmd_print(xcmder, BK_YELLOW "background_color = BK_YELLOW" BK_DEF "\r\n");
xcmd_print(BK_BLUE "background_color = BK_BLUE" BK_DEF "\r\n"); xcmd_print(xcmder, BK_BLUE "background_color = BK_BLUE" BK_DEF "\r\n");
xcmd_print(BK_WHITE "background_color = BK_WHITE" BK_DEF "\r\n"); xcmd_print(xcmder, BK_WHITE "background_color = BK_WHITE" BK_DEF "\r\n");
return 0; return 0;
} }

309
inc/xcmd.h Executable file → Normal file
View File

@ -1,132 +1,71 @@
#ifndef XCMD_H #ifndef XCMD_H
#define XCMD_H #define XCMD_H
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <math.h> #include <stdint.h>
#include <stdarg.h> #include <string.h>
#include "xcmd_define.h" #include "xnr_line.h"
#include "xnr_history.h"
#include "xnr_io.h"
#include "xnr_key.h"
#include "xnr_var.h"
#include "xcmd_default_confg.h" #include "xcmd_default_confg.h"
#ifdef __cplusplus typedef int (*cmd_func_t)(int, char **);
extern "C" { typedef struct cmd
#endif
typedef int(*cmd_func_t)(int, char**);
typedef int(*cmd_key_func_t)(void *);
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 #ifndef ENABLE_XCMD_EXPORT
struct __cmd *next; struct cmd *next;
#endif #endif
}xcmd_t; } xcmd_t;
typedef xcmd_t xcmd_key_t;
typedef struct __key #define XCMD_CURRENT() (xcmder_t *)(argv[argc])
{
const char* key;
cmd_key_func_t func;
const char* help;
#ifndef ENABLE_XCMD_EXPORT
struct __key *next;
#endif
}xcmd_key_t;
/**
* @description:
* @param {func*} get_c
* @param {func*} put_c
* @return {*}
*/
void xcmd_init( int (*get_c)(uint8_t*), int (*put_c)(uint8_t));
/**
* @description:
* @param {*}
* @return {*}
*/
void xcmd_task(void);
/**
* @description:
* @param {xcmd_t*} cmds
* @param {uint16_t} number
* @return {int}
*/
int xcmd_cmd_register(xcmd_t* cmds, uint16_t number);
/**
* @description:
* @param {xcmd_key_t*} keys
* @param {uint16_t} number
* @return {int}
*/
int xcmd_key_register(xcmd_key_t* keys, uint16_t number);
/**
* @description: cmd
* @param {char*} cmdcmd集
* @return {int}0success !0failed
*/
int xcmd_unregister_cmd(char *cmd);
/**
* @description:key
* @param {char*} keykey集
* @return {int}0success !0failed
*/
int xcmd_unregister_key(char *key);
#ifndef XCMD_SECTION #ifndef XCMD_SECTION
#if defined(__CC_ARM) || defined(__CLANG_ARM) #if defined(__CC_ARM) || defined(__CLANG_ARM)
#define XCMD_SECTION(x) __attribute__((section(x))) #define XCMD_SECTION(x) __attribute__((section(x)))
#elif defined (__IAR_SYSTEMS_ICC__) #elif defined(__IAR_SYSTEMS_ICC__)
#define XCMD_SECTION(x) @ x #define XCMD_SECTION(x) @x
#elif defined(__GNUC__) #elif defined(__GNUC__)
#define XCMD_SECTION(x) __attribute__((section(x))) #define XCMD_SECTION(x) __attribute__((section(x)))
#else #else
#define XCMD_SECTION(x) #define XCMD_SECTION(x)
#endif #endif
#endif #endif
#ifndef XCMD_USED #ifndef XCMD_USED
#if defined(__CC_ARM) || defined(__CLANG_ARM) #if defined(__CC_ARM) || defined(__CLANG_ARM)
#define XCMD_USED __attribute__((used)) #define XCMD_USED __attribute__((used))
#elif defined (__IAR_SYSTEMS_ICC__) #elif defined(__IAR_SYSTEMS_ICC__)
#define XCMD_USED __root #define XCMD_USED __root
#elif defined(__GNUC__) #elif defined(__GNUC__)
#define XCMD_USED __attribute__((used)) #define XCMD_USED __attribute__((used))
#else #else
#define XCMD_USED #define XCMD_USED
#endif #endif
#endif #endif
#ifdef ENABLE_XCMD_EXPORT #ifdef ENABLE_XCMD_EXPORT
#define XCMD_EXPORT_CMD(_name, _func, _help) XCMD_USED const xcmd_t XCMD_SECTION("_xcmd_cmd_list") \ #define XCMD_EXPORT_CMD(_name, _func, _help) XCMD_USED const xcmd_t XCMD_SECTION("_xcmd_cmd_list") \
xcmd_cmd_##_name={\ xcmd_cmd_##_name = { \
.name=#_name, \ .name = #_name, \
.func=_func, \ .func = _func, \
.help=_help\ .help = _help};
};
#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, \ .key = _key, \
.func=_func, \ .func = _func, \
.help=_help\ .help = _help};
};
extern xcmd_t _xcmd_cmd_list_start; extern xcmd_t _xcmd_cmd_list_start;
extern xcmd_t _xcmd_cmd_list_end; extern xcmd_t _xcmd_cmd_list_end;
extern xcmd_key_t _xcmd_key_list_start; extern xcmd_key_t _xcmd_key_list_start;
extern xcmd_key_t _xcmd_key_list_end; 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
/** /**
* @description: next指针可以遍历所有指令 * @description: next指针可以遍历所有指令
@ -148,140 +87,38 @@ 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
/** typedef struct xcmd
* @description: {
* @param {char* } str xnr_io_t io;
* @return {uint8_t} xnr_key_t key;
*/ xnr_var_tab_t var_tab;
int xcmd_exec(char *str); xnr_history_t history;
xnr_line_t line;
cmd_func_t unknow_cmd_cbk; // 输入为命令序列,输出未处理
cmd_func_t pre_cmd_cbk; // 输入为命令序列输出重新赋值给argc
cmd_func_t after_cmd_cbk; // 输入为命令序列,输出未处理
void *user_data; // 用户数据
int optind; // 选项在参数列表中的位置
char *optarg; // 选项参数的值
bool _initOK;
} xcmder_t;
/** void xcmd_init(xcmder_t *xcmder, _io_write_t write, _io_read_t read);
* @description: void xcmd_task(xcmder_t *xcmder);
*/ int xcmd_exec(xcmder_t *xcmder, char *str);
void xcmd_print(const char *fmt, ...); int xcmd_getopt(xcmder_t *xcmder, int argc, char * const argv[], const char *optstring);
void xcmd_put_str(const char *str); void xcmd_reg_unknow_cmd_cbk(xcmder_t *xcmder, cmd_func_t cbk);
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);
/** int xcmd_print(xcmder_t *xcmder, const char *fmt, ...);
* @description: int xcmd_puts(xcmder_t *xcmder, const char *str);
* @param {char} c int xcmd_putc(xcmder_t *xcmder, char c);
* @return int xcmd_getc(xcmder_t *xcmder);
*/
void xcmd_display_insert_char(char c);
/** int xcmd_key_register(xcmd_key_t *keys, uint16_t number);
* @description: int xcmd_unregister_key(char *key);
* @param {*} int xcmd_cmd_register(xcmd_t *cmds, uint16_t number);
* @return int xcmd_unregister_cmd(char *cmd);
*/
void xcmd_display_delete_char(void);
/**
* @description:
* @param {char*}cha存储返回的字符
* @return {uint8_t}01
*/
uint8_t xcmd_display_current_char(char *cha);
/**
* @description:
* @param {*}
* @return
*/
void xcmd_display_clear(void);
/**
* @description:
* @param {*}
* @return {char*} *
*/
char* xcmd_display_get(void);
/**
* @description:
* @param {char*}
* @return
*/
void xcmd_display_print(const char *fmt, ...);
void xcmd_display_write(const char* buf, uint16_t len);
/**
* @description:
* @param {*}
* @return {*}
*/
void xcmd_display_cursor_set(uint16_t pos);
uint16_t xcmd_display_cursor_get(void);
/**
* @description:
* @param {char*} prompt
* @return {*}
*/
void xcmd_set_prompt(const char* prompt);
const char* xcmd_get_prompt(void);
/**
* @description:
* @param {func_p} 01
* @return {*}
*/
void xcmd_register_rcv_hook_func(uint8_t(*func_p)(char*));
/**
* @description:
* @param {*}
* @return {uint16_t}
*/
uint16_t xcmd_history_len(void);
/**
* @description:
* @param {char*} str
* @return
*/
void xcmd_history_insert(char* str);
/**
* @description:
* @param {*}
* @return
*/
char *xcmd_history_next(void);
/**
* @description:
* @param {*}
* @return
*/
char *xcmd_history_prev(void);
/**
* @description:
* @param {*}
* @return
*/
char *xcmd_history_current(void);
/**
* @description:
* @param {*}
* @return
*/
void xcmd_history_slider_reset(void);
/**
* @description:
* @param {*}
* @return {*}
*/
char* xcmd_end_of_input(void);
#ifdef __cplusplus
}
#endif #endif
#endif /*XCMD_H*/

View File

@ -46,6 +46,10 @@ extern "C" {
#define XCMD_DEFAULT_PROMPT_CLOLR TX_GREEN /*提示符颜色*/ #define XCMD_DEFAULT_PROMPT_CLOLR TX_GREEN /*提示符颜色*/
#endif #endif
#ifndef XCMD_VAR_BUF_SZIE
#define XCMD_VAR_BUF_SZIE (32)
#endif
#ifndef ENABLE_XCMD_EXPORT #ifndef ENABLE_XCMD_EXPORT
//#define ENABLE_XCMD_EXPORT /*使能XCMD_EXPORT_CMD和XCMD_EXPORT_KEY*/ //#define ENABLE_XCMD_EXPORT /*使能XCMD_EXPORT_CMD和XCMD_EXPORT_KEY*/
#endif #endif

View File

@ -1,120 +0,0 @@
/*
* @Author: your name
* @Date: 2021-09-20 19:59:05
* @LastEditTime: 2022-02-17 22:41:13
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /xcmd/inc/xcmd_define.h
*/
#ifndef XCMD_DEFINE_H
#define XCMD_DEFINE_H
#ifdef __cplusplus
extern "C" {
#endif
#define KEY_CTR_A "\x01"
#define KEY_CTR_B "\x02"
#define KEY_CTR_C "\x03"
#define KEY_CTR_D "\x04"
#define KEY_CTR_E "\x05"
#define KEY_CTR_F "\x06"
#define KEY_CTR_G "\x07"
#define KEY_CTR_H "\x08"
#define KEY_CTR_I "\x09"
#define KEY_TAB "\x09"
#define KEY_CTR_J "\x0A"
#define KEY_CTR_K "\x0B"
#define KEY_CTR_L "\x0C"
#define KEY_CTR_M "\x0D"
#define KEY_CTR_N "\x0E"
#define KEY_CTR_O "\x0F"
#define KEY_CTR_P "\x10"
#define KEY_CTR_Q "\x11"
#define KEY_CTR_R "\x12"
#define KEY_CTR_S "\x13"
#define KEY_CTR_T "\x14"
#define KEY_CTR_U "\x15"
#define KEY_CTR_V "\x16"
#define KEY_CTR_W "\x17"
#define KEY_CTR_X "\x18"
#define KEY_CTR_Y "\x19"
#define KEY_CTR_Z "\x1A"
#define KEY_PAUSE "\x1A"
#define KEY_ESC "\x1B"
#define KEY_BACKSPACE "\x7F"
#define KEY_UP "\x1B[A"
#define KEY_DW "\x1B[B"
#define KEY_RIGHT "\x1B[C"
#define KEY_LEFT "\x1B[D"
#define KEY_HOME "\x1B[H"
#define KEY_EMD "\x1B[F"
#define KEY_CTR_UP "\x1B[1;5A"
#define KEY_CTR_DW "\x1B[1;5B"
#define KEY_CTR_RIGHT "\x1B[1;5C"
#define KEY_CTR_LEFT "\x1B[1;5D"
#define KEY_INSERT "\x1B[2~"
#define KEY_DELETE "\x1B[3~"
#define KEY_PAGE_UP "\x1B[5~"
#define KEY_PAGE_DOWN "\x1B[6~"
#define KEY_F1 "\x1BOP"
#define KEY_F2 "\x1BOQ"
#define KEY_F3 "\x1BOR"
#define KEY_F4 "\x1BOS"
#define KEY_F5 "\x1B[15~"
#define KEY_F6 "\x1B[17~"
#define KEY_F7 "\x1B[18~"
#define KEY_F8 "\x1B[19~"
#define KEY_F9 "\x1B[20~"
#define KEY_F10 "\x1B[21~"
#define KEY_F11 "\x1B[23~"
#define KEY_F12 "\x1B[24~"
/*光标操作符其中0x1B是ESC只适用于xcmd_print函数*/
#define CUU(n) "\x1B[%dA",n /* 光标向上 光标向上 <n> 行 */
#define CUD(n) "\x1B[%dB",n /* 光标向下 光标向下 <n> 行 */
#define CUF(n) "\x1B[%dC",n /* 光标向前 光标向前(右)<n> 行 */
#define CUB(n) "\x1B[%dD",n /* 光标向后 光标向后(左)<n> 行 */
#define CNL(n) "\x1B[%dE",n /* 光标下一行 光标从当前位置向下 <n> 行 */
#define CPL(n) "\x1B[%dF",n /* 光标当前行 光标从当前位置向上 <n> 行 */
#define CHA(n) "\x1B[%dG",n /* 绝对光标水平 光标在当前行中水平移动到第 <n> 个位置 */
#define VPA(n) "\x1B[%dd",n /* 绝对垂直行位置 光标在当前列中垂直移动到第 <n> 个位置 */
#define CUP(y,x) "\x1B[%d;%dH",y,x /* 光标位置 *光标移动到视区中的 <x>; <y> 坐标,其中 <x> 是 <y> 行的列 */
#define HVP(y,x) "\x1B[%d;%df",y,x /* 水平垂直位置 *光标移动到视区中的 <x>; <y> 坐标,其中 <x> 是 <y> 行的列 */
/*光标可见性*/
#define CU_START_BL "\x1B[?12h" /* ATT160 文本光标启用闪烁 开始光标闪烁 */
#define CU_STOP_BL "\x1B[?12l" /* ATT160 文本光标禁用闪烁 停止闪烁光标 */
#define CU_SHOW "\x1B[?25h" /* DECTCEM 文本光标启用模式显示 显示光标 */
#define CU_HIDE "\x1B[?25l" /* DECTCEM 文本光标启用模式隐藏 隐藏光标 */
/* 字符操作 */
#define ICH(n) "\x1B[%d@",n /* 插入字符 在当前光标位置插入 <n> 个空格,这会将所有现有文本移到右侧。 向右溢出屏幕的文本会被删除。*/
#define DCH(n) "\x1B[%dP",n /* 删除字符 删除当前光标位置的 <n> 个字符,这会从屏幕右边缘以空格字符移动。*/
#define ECH(n) "\x1B[%dX",n /* 擦除字符 擦除当前光标位置的 <n> 个字符,方法是使用空格字符覆盖它们。*/
#define IL(n) "\x1B[%dL",n /* 插入行 将 <n> 行插入光标位置的缓冲区。 光标所在的行及其下方的行将向下移动。*/
#define DL(n) "\x1B[%dM\r",n /* 删除行 从缓冲区中删除 <n> 行,从光标所在的行开始。*/
/* 打印字体颜色设置 */
#define TX_DEF "\x1b[0m"
#define TX_BLACK "\x1b[30m"
#define TX_RED "\x1b[31m"
#define TX_GREEN "\x1b[32m"
#define TX_YELLOW "\x1b[33m"
#define TX_BLUE "\x1b[34m"
#define TX_WHITE "\x1b[37m"
/* 打印背景颜色设置 */
#define BK_DEF "\x1b[0m"
#define BK_BLACK "\x1b[40m"
#define BK_RED "\x1b[41m"
#define BK_GREEN "\x1b[42m"
#define BK_YELLOW "\x1b[43m"
#define BK_BLUE "\x1b[44m"
#define BK_WHITE "\x1b[47m"
#ifdef __cplusplus
}
#endif
#endif /*XCMD_DEFINE_H*/

967
src/xcmd.c Executable file → Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,3 @@
/*
* @Author: your name
* @Date: 2021-09-15 00:11:50
* @LastEditTime: 2021-10-11 21:37:09
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /xcmd/src/xcmd_default_cmds.c
*/
#include "xcmd_default_confg.h" #include "xcmd_default_confg.h"
#include "xcmd_default_cmds.h" #include "xcmd_default_cmds.h"
#include "xcmd.h" #include "xcmd.h"
@ -13,41 +5,71 @@
static int cmd_clear(int argc, char* argv[]) static int cmd_clear(int argc, char* argv[])
{ {
xcmd_print("\033c"); xcmder_t *xcmder = XCMD_CURRENT();
xcmd_print(xcmder, "\033c");
return 0; return 0;
} }
static int cmd_help(int argc, char* argv[]) static int cmd_help(int argc, char* argv[])
{ {
xcmder_t *xcmder = XCMD_CURRENT();
xcmd_t *p = NULL; xcmd_t *p = NULL;
XCMD_CMD_FOR_EACH(p) XCMD_CMD_FOR_EACH(p)
{ {
xcmd_print("%-20s %s\r\n",p->name, p->help); xcmd_print(xcmder, "%-20s %s\r\n",p->name, p->help);
} }
return 0; return 0;
} }
static int cmd_keys(int argc, char* argv[]) static int cmd_keys(int argc, char* argv[])
{ {
xcmder_t *xcmder = XCMD_CURRENT();
xcmd_key_t *p = NULL; xcmd_key_t *p = NULL;
XCMD_KEY_FOR_EACH(p) XCMD_KEY_FOR_EACH(p)
{ {
xcmd_print("0x%08X\t", p->key); xcmd_print(xcmder, "0x%08X\t", p->name);
xcmd_print("%s\r\n",p->help); xcmd_print(xcmder, "%s\r\n",p->help);
} }
return 0; return 0;
} }
static int cmd_logo(int argc, char* argv[]) static int cmd_logo(int argc, char* argv[])
{ {
xcmder_t *xcmder = XCMD_CURRENT();
char *log = "\ char *log = "\
_ _ ___ __ __ ____ \r\n\ _ _ ___ __ __ ____ \r\n\
( \\/ )/ __)( \\/ )( _ \\ \r\n\ ( \\/ )/ __)( \\/ )( _ \\ \r\n\
) (( (__ ) ( )(_) )\r\n\ ) (( (__ ) ( )(_) )\r\n\
(_/\\_)\\___)(_/\\/\\_)(____/\r\n "; (_/\\_)\\___)(_/\\/\\_)(____/\r\n ";
xcmd_print("%s", log); xcmd_print(xcmder, "%s", log);
xcmd_print("\r\n%-10s %s %s\r\n","Build" ,__DATE__, __TIME__); xcmd_print(xcmder, "\r\n%-10s %s %s\r\n","Build" ,__DATE__, __TIME__);
xcmd_print("%-10s %s\r\n","Version", VERSION); xcmd_print(xcmder, "%-10s %s\r\n","Version", VERSION);
return 0;
}
static int cmd_set(int argc, char* argv[])
{
xcmder_t *xcmder = XCMD_CURRENT();
if(argc >= 3)
{
xnr_var_set(&xcmder->var_tab, argv[1], argv[2]);
}
return 0;
}
static int cmd_var(int argc, char* argv[])
{
xcmder_t *xcmder = XCMD_CURRENT();
int id = 0;
xnr_var_t *var;
while(1)
{
var = xnr_var(&xcmder->var_tab, id++);
if(NULL == var)
break;
if(var->name[0] != '\0')
xcmd_print(xcmder, "%-15s:%s\n", var->name, var->value);
}
return 0; return 0;
} }
@ -55,6 +77,8 @@ XCMD_EXPORT_CMD(clear, cmd_clear, "clear screen")
XCMD_EXPORT_CMD(help, cmd_help, "show this list") XCMD_EXPORT_CMD(help, cmd_help, "show this list")
XCMD_EXPORT_CMD(keys, cmd_keys, "show keys") XCMD_EXPORT_CMD(keys, cmd_keys, "show keys")
XCMD_EXPORT_CMD(logo, cmd_logo, "show logo") XCMD_EXPORT_CMD(logo, cmd_logo, "show logo")
XCMD_EXPORT_CMD(set, cmd_set, "set var")
XCMD_EXPORT_CMD(set, cmd_var, "get all var")
static xcmd_t cmds[] = static xcmd_t cmds[] =
{ {
@ -63,6 +87,8 @@ static xcmd_t cmds[] =
{"help", cmd_help, "show this list", NULL}, {"help", cmd_help, "show this list", NULL},
{"keys", cmd_keys, "show keys", NULL}, {"keys", cmd_keys, "show keys", NULL},
{"logo", cmd_logo, "show logo", NULL}, {"logo", cmd_logo, "show logo", NULL},
{"set", cmd_set, "set var", NULL},
{"var", cmd_var, "get all var", NULL},
#endif #endif
}; };

View File

@ -1,11 +1,3 @@
/*
* @Author: your name
* @Date: 2021-09-15 00:11:50
* @LastEditTime: 2021-10-27 09:16:27
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /xcmd/src/xcmd_default_keys.c
*/
#include "xcmd_default_confg.h" #include "xcmd_default_confg.h"
#include "xcmd.h" #include "xcmd.h"
#include "xcmd_default_keys.h" #include "xcmd_default_keys.h"
@ -23,88 +15,85 @@ static int xcmd_str_match(const char* str1, const char* str2)
return i; return i;
} }
static int xcmd_del_char(int argc, char* argv[])
static int xcmd_del_char(void *pv)
{ {
xcmd_display_delete_char(); xcmder_t *xcmder = XCMD_CURRENT();
xnr_line_delete(&xcmder->line);
return 0; return 0;
} }
XCMD_EXPORT_KEY(KEY_CTR_H, xcmd_del_char, "backspace") XCMD_EXPORT_KEY(KEY_CTR_H, xcmd_del_char, "backspace")
XCMD_EXPORT_KEY(KEY_BACKSPACE, xcmd_del_char, "delete") XCMD_EXPORT_KEY(KEY_BACKSPACE, xcmd_del_char, "delete")
static int xcmd_enter(void *pv) static int xcmd_enter(int argc, char* argv[])
{ {
char *cmd = xcmd_end_of_input(); xcmder_t *xcmder = XCMD_CURRENT();
xcmd_print("\n\r"); char out_line[XNR_LINE_MAX_LENGTH] = {0};
if(cmd[0]) xcmd_print(xcmder, "\n\r");
char *line = xnr_line_line(&xcmder->line);
xnr_history_append(&xcmder->history, line);
xnr_var_repalce(&xcmder->var_tab, line, out_line, XNR_LINE_MAX_LENGTH);
if(out_line[0])
{ {
xcmd_exec(cmd); xcmd_exec(xcmder, out_line);
cmd[0] = '\0';
} }
#ifndef XCMD_DEFAULT_PROMPT_CLOLR xnr_line_clear(&xcmder->line);
xcmd_print("%s", xcmd_get_prompt());
#else
xcmd_print(XCMD_DEFAULT_PROMPT_CLOLR "%s" TX_DEF, xcmd_get_prompt());
#endif
return 0; return 0;
} }
XCMD_EXPORT_KEY(KEY_CTR_M, xcmd_enter, "enter") XCMD_EXPORT_KEY(KEY_CTR_M, xcmd_enter, "enter")
XCMD_EXPORT_KEY(KEY_CTR_J, xcmd_enter, "enter") XCMD_EXPORT_KEY(KEY_CTR_J, xcmd_enter, "enter")
static int xcmd_cursor_left(void *pv) static int xcmd_cursor_left(int argc, char* argv[])
{ {
uint16_t pos = xcmd_display_cursor_get(); xcmder_t *xcmder = XCMD_CURRENT();
if(pos > 0) xnr_line_cursor_left(&xcmder->line);
{
pos--;
xcmd_display_cursor_set(pos);
}
return 0; return 0;
} }
XCMD_EXPORT_KEY(KEY_LEFT, xcmd_cursor_left, "left") XCMD_EXPORT_KEY(KEY_LEFT, xcmd_cursor_left, "left")
static int xcmd_cursor_right(void *pv) static int xcmd_cursor_right(int argc, char* argv[])
{ {
uint16_t pos = xcmd_display_cursor_get(); xcmder_t *xcmder = XCMD_CURRENT();
pos++; xnr_line_cursor_right(&xcmder->line);
xcmd_display_cursor_set(pos);
return 0; return 0;
} }
XCMD_EXPORT_KEY(KEY_RIGHT, xcmd_cursor_right, "right") XCMD_EXPORT_KEY(KEY_RIGHT, xcmd_cursor_right, "right")
#if XCMD_HISTORY_MAX_NUM #if XCMD_HISTORY_MAX_NUM
static int xcmd_history_dw(void *pv) static int xcmd_history_dw(int argc, char* argv[])
{ {
char *line = xcmd_history_prev(); xcmder_t *xcmder = XCMD_CURRENT();
xcmd_display_clear(); char out_line[XNR_LINE_MAX_LENGTH] = {0};
if(line) if (xnr_history_dw(&xcmder->history, out_line, XNR_LINE_MAX_LENGTH) > 0)
{ {
xcmd_display_print(line); xnr_line_clear(&xcmder->line);
xnr_line_puts(&xcmder->line, out_line);
} }
return 0; return 0;
} }
XCMD_EXPORT_KEY(KEY_DW, xcmd_history_dw, "down") XCMD_EXPORT_KEY(KEY_DW, xcmd_history_dw, "down")
static int xcmd_history_up(void *pv) static int xcmd_history_up(int argc, char* argv[])
{ {
char *line = xcmd_history_next(); xcmder_t *xcmder = XCMD_CURRENT();
if(line) char out_line[XNR_LINE_MAX_LENGTH] = {0};
if (xnr_history_up(&xcmder->history, out_line, XNR_LINE_MAX_LENGTH) > 0)
{ {
xcmd_display_clear(); xnr_line_clear(&xcmder->line);
xcmd_display_print(line); xnr_line_puts(&xcmder->line, out_line);
} }
return 0; return 0;
} }
XCMD_EXPORT_KEY(KEY_UP, xcmd_history_up, "up") XCMD_EXPORT_KEY(KEY_UP, xcmd_history_up, "up")
#endif #endif
static int xcmd_auto_completion(void *pv) static int xcmd_auto_completion(int argc, char* argv[])
{ {
xcmder_t *xcmder = XCMD_CURRENT();
xcmd_t *match_cmd_first = NULL; xcmd_t *match_cmd_first = NULL;
uint16_t match_num = 0; uint16_t match_num = 0;
uint16_t match_subscript_min = 0; uint16_t match_subscript_min = 0;
char *display_line = xcmd_display_get(); char *display_line = xnr_line_line(&xcmder->line);
uint16_t cursor_pos = xcmd_display_cursor_get(); uint16_t cursor_pos = xnr_line_cursor(&xcmder->line);
xcmd_t *p = NULL; xcmd_t *p = NULL;
XCMD_CMD_FOR_EACH(p) XCMD_CMD_FOR_EACH(p)
{ {
@ -117,14 +106,14 @@ static int xcmd_auto_completion(void *pv)
} }
else if(match_num == 1) else if(match_num == 1)
{ {
xcmd_print("\r\n%-15s%-15s", match_cmd_first->name, p->name); xcmd_print(xcmder,"\r\n%-15s%-15s", match_cmd_first->name, p->name);
} }
else else
{ {
xcmd_print("%-15s", p->name); xcmd_print(xcmder, "%-15s", p->name);
if((match_num%4) == 0) if((match_num%4) == 0)
{ {
xcmd_print("\r\n"); xcmd_print(xcmder, "\r\n");
} }
} }
uint16_t subscript = xcmd_str_match(match_cmd_first->name, p->name); uint16_t subscript = xcmd_str_match(match_cmd_first->name, p->name);
@ -138,14 +127,14 @@ static int xcmd_auto_completion(void *pv)
if(match_num == 1) if(match_num == 1)
{ {
xcmd_display_clear(); xnr_line_clear(&xcmder->line);
xcmd_display_print("%s", match_cmd_first->name); xnr_line_puts(&xcmder->line, match_cmd_first->name);
} }
else if(match_num > 1) else if(match_num > 1)
{ {
xcmd_print("\r\n"); xcmd_print(xcmder, "\r\n");
xcmd_display_clear(); xnr_line_clear(&xcmder->line);
xcmd_display_write(match_cmd_first->name, match_subscript_min); xnr_line_write(&xcmder->line, match_cmd_first->name, match_subscript_min);
} }
return 0; return 0;
} }