1
0
mirror of https://github.com/NevermindZZT/letter-shell.git synced 2025-01-01 09:58:41 +08:00

新增 shell命令组组件

This commit is contained in:
Letter 2020-10-18 16:00:55 +08:00
parent 711257c5f3
commit 6019d58b04
5 changed files with 251 additions and 4 deletions

View File

@ -1,9 +1,9 @@
all : mkdir out
out : main.o shell_port.o shell.o shell_ext.o shell_cmd_list.o shell_companion.o shell_fs.o
out : main.o shell_port.o shell.o shell_ext.o shell_cmd_list.o shell_companion.o shell_fs.o shell_cmd_group.o
gcc -o build/out -T shell.lds build/main.o build/shell_port.o \
build/shell.o build/shell_ext.o build/shell_cmd_list.o \
build/shell_companion.o build/shell_fs.o
build/shell_companion.o build/shell_fs.o build/shell_cmd_group.o
main.o : main.c shell_port.h
gcc -c main.c -I ./ -I ../../src -o build/main.o
@ -26,6 +26,9 @@ shell_companion.o : ../../src/shell_companion.c ../../src/shell.h shell_cfg.h
shell_fs.o : ../../extensions/fs_support/shell_fs.c ../../extensions/fs_support/shell_fs.h shell_cfg.h ../../src/shell.h
gcc -c ../../extensions/fs_support/shell_fs.c -I ./ -I ../../src -I ../../extensions/fs_support -o build/shell_fs.o
shell_cmd_group.o : ../../extensions/shell_enhance/shell_cmd_group.c ../../extensions/shell_enhance/shell_cmd_group.h shell_cfg.h ../../src/shell.h
gcc -c ../../extensions/shell_enhance/shell_cmd_group.c -I ./ -I ../../src -I ../../extensions/shell_enhance -o build/shell_cmd_group.o
.PHONY : all clean mkdir
clean :
-rm -r build

View File

@ -0,0 +1,93 @@
# shell_enhance
![version](https://img.shields.io/badge/version-1.0.0-brightgreen.svg)
![standard](https://img.shields.io/badge/standard-c99-brightgreen.svg)
![build](https://img.shields.io/badge/build-2020.10.18-brightgreen.svg)
![license](https://img.shields.io/badge/license-MIT-brightgreen.svg)
- [shell_enhance](#shell_enhance)
- [简介](#简介)
- [组件](#组件)
- [shell_cmd_group](#shell_cmd_group)
## 简介
`shell_enhance``letter shell 3.0`上用于增强shell功能的组件`shell_enhance`分离了一些不常用的shell功能对于需要这些功能的用户只需要将对应的文件加入到编译系统中即可
## 组件
`shell_enhance`目前包含的组件如下:
| 组件 | 描述 | 依赖文件 |
| --------------- | -------------- | ----------------------------------- |
| shell_cmd_group | 提供命令组功能 | shell_cmd_group.c shell_cmd_group.h |
### shell_cmd_group
`shell_cmd_group`提供了一个命令组的功能,用户可以将多个相关的命令打包成一个命令组,然后通过形如`cmdgroup subcmd [param]`的方式进行命令调用
- 定义命令数组
命令数组用于将多个命令关联到一个数组,这个数组就是定义命令组需要的,你需要想正常命令一样,为每个子命令对应定义一个函数,然后将他们添加到一个数组中,可以使用宏`SHELL_CMD_GROUP_ITEM`进行定义
```c
int test(int a, char *b)
{
printf("hello world, %d, %s\r\n", a, b);
return 0;
}
int test2(int argc, char *argv[])
{
printf("%d parameters\r\n", argc);
for (short i = 0; i < argc; i++)
{
printf("%s\t", argv[i]);
}
printf("\r\n");
}
ShellCommand testGroup[] =
{
SHELL_CMD_GROUP_ITEM(SHELL_TYPE_CMD_FUNC, test, test, command group test1),
SHELL_CMD_GROUP_ITEM(SHELL_TYPE_CMD_MAIN, test2, test2, command group test2),
SHELL_CMD_GROUP_END()
};
```
其中`SHELL_CMD_GROUP_END()`需要写在每个命令数组的最后
- 定义命令组
使用宏`SHELL_EXPORT_CMD_GROUP`定义命令组
```c
SHELL_EXPORT_CMD_GROUP(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), gt, testGroup, command group test);
```
- 调用
在命令行,使用`cmdgroup subcmd [param]`的形式调用命令
```sh
letter:/$ gt test 666 Letter
hello world, 666, Letter
Return: 0, 0x00000000
letter:/$ gt test2 hello world
3 parameters
test2 hello world
Return: 0, 0x00000000
```
- 命令帮助
通过使用`cmdgroup -h`的方式查看完整的命令帮助信息
```sh
letter:/$ gt -h
command group help of gt
test: command group test1
test2: command group test2
Return: 0, 0x00000000
```

View File

@ -0,0 +1,85 @@
/**
* @file shell_cmd_group.c
* @author Letter(nevermindzzt@gmail.com)
* @brief shell command group support
* @version 0.1
* @date 2020-10-18
*
* @copyright (c) 2020 Letter
*
*/
#include "shell_cmd_group.h"
#include "string.h"
#include "stdio.h"
unsigned int shellRunCommand(Shell *shell, ShellCommand *command);
static void shellCmdGroupHelp(Shell *shell, char *name, ShellCommand *group);
/**
* @brief shell命令组运行
*
* @param group
* @param argc
* @param argv
*
* @return unsigned int
*/
unsigned int shellCmdGroupRun(ShellCommand *group, int argc, char *argv[])
{
ShellCommand *item = group;
Shell *shell = shellGetCurrent();
if (argc < 2 || !shell)
{
return -1;
}
if (strcmp("-h", argv[1]) == 0)
{
shellCmdGroupHelp(shell, argv[0], group);
return 0;
}
while (item->data.cmd.name)
{
if (strcmp(item->data.cmd.name, argv[1]) == 0)
{
for (short i = 0; i < argc - 1; i++)
{
argv[i] = argv[i + 1];
}
shell->parser.paramCount--;
return shellRunCommand(shell, item);
break;
}
item++;
}
shellWriteString(shell, "sub command not found\r\n");
return -1;
}
/**
* @brief shell
*
* @param shell shell对象
* @param name
* @param group
*/
static void shellCmdGroupHelp(Shell *shell, char *name, ShellCommand *group)
{
ShellCommand *item = group;
shellWriteString(shell, "command group help of ");
shellWriteString(shell, name);
shellWriteString(shell, "\r\n");
while (item->data.cmd.name)
{
shellWriteString(shell, item->data.cmd.name);
shellWriteString(shell, ": ");
if (item->data.cmd.desc) {
shellWriteString(shell, item->data.cmd.desc);
}
shellWriteString(shell, "\r\n");
item++;
}
}

View File

@ -0,0 +1,62 @@
/**
* @file shell_cmd_group.h
* @author Letter(nevermindzzt@gmail.com)
* @brief shell command group support
* @version 0.1
* @date 2020-10-18
*
* @copyright (c) 2020 Letter
*
*/
#include "shell.h"
/**
* @brief shell
*/
#define SHELL_CMD_GROUP_FUNC_NAME(_group) agency##_group
/**
* @brief shell命令组函数定义
*
* @param _group
*/
#define SHELL_CMD_GROUP_FUNC(_group) \
void SHELL_CMD_GROUP_FUNC_NAME(_group)(int p1, int p2) \
{ shellCmdGroupRun(&_group, p1, p2); }
/**
* @brief shell
*
* @param _attr
* @param _name
* @param _group
* @param _desc
*/
#define SHELL_EXPORT_CMD_GROUP(_attr, _name, _group, _desc) \
SHELL_CMD_GROUP_FUNC(_group) \
SHELL_EXPORT_CMD(_attr, _name, SHELL_CMD_GROUP_FUNC_NAME(_group), _desc)
/**
* @brief shell item定义
*
* @param _type (SHELL_TYPE_CMD_MAIN or SHELL_TYPE_CMD_FUNC)
* @param _func
* @param _desc
*/
#define SHELL_CMD_GROUP_ITEM(_type, _name, _func, _desc) \
{ \
.attr.value = SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(_type)|SHELL_CMD_DISABLE_RETURN, \
.data.cmd.name = #_name, \
.data.cmd.function = (int (*)())_func, \
.data.cmd.desc = #_desc \
}
/**
* @brief shell
*
* @note
*/
#define SHELL_CMD_GROUP_END() {0}
unsigned int shellCmdGroupRun(ShellCommand *group, int argc, char *argv[]);

View File

@ -1118,10 +1118,12 @@ setVar, shellSetVar, set var);
*
* @param shell shell对象
* @param command
*
* @return unsigned int
*/
static void shellRunCommand(Shell *shell, ShellCommand *command)
unsigned int shellRunCommand(Shell *shell, ShellCommand *command)
{
int returnValue;
int returnValue = 0;
shell->status.isActive = 1;
if (command->attr.attrs.type == SHELL_TYPE_CMD_MAIN)
{
@ -1154,6 +1156,8 @@ static void shellRunCommand(Shell *shell, ShellCommand *command)
shellSetUser(shell, command);
}
shell->status.isActive = 0;
return returnValue;
}