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

Merge branch 'shell3.1' into master

This commit is contained in:
Letter 2021-05-31 22:45:24 +08:00 committed by GitHub
commit 26acf89e61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 176 additions and 11 deletions

View File

@ -10,6 +10,7 @@ add_executable(LetterShell
../../src/shell_ext.c
../../extensions/fs_support/shell_fs.c
../../extensions/log/log.c
../../extensions/shell_enhance/shell_passthrough.c
)
target_include_directories(LetterShell PUBLIC
@ -18,6 +19,7 @@ target_include_directories(LetterShell PUBLIC
../../extensions/fs_support
../../extensions/cpp_support
../../extensions/log
../../extensions/shell_enhance
)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})

View File

@ -11,6 +11,7 @@
#include "shell.h"
#include "shell_fs.h"
#include "shell_passthrough.h"
#include "log.h"
#include <stdio.h>
#include <dirent.h>
@ -217,3 +218,9 @@ SHELL_EXPORT_CMD(
SHELL_CMD_PERMISSION(0x00)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_DISABLE_RETURN,
scanTest, shellScanTest, test scan);
void shellPassthroughTest(char *data, unsigned short len)
{
printf("passthrough mode test, data: %s, len: %d\r\n", data, len);
}
SHELL_EXPORT_PASSTROUGH(SHELL_CMD_PERMISSION(0), passTest, passthrough>>, shellPassthroughTest, passthrough mode test);

View File

@ -9,6 +9,7 @@
- [简介](#简介)
- [组件](#组件)
- [shell_cmd_group](#shell_cmd_group)
- [shell_passthrough](#shell_passthrough)
## 简介
@ -19,8 +20,9 @@
`shell_enhance`目前包含的组件如下:
| 组件 | 描述 | 依赖文件 |
| --------------- | -------------- | ----------------------------------- |
| ----------------- | -------------- | --------------------------------------- |
| shell_cmd_group | 提供命令组功能 | shell_cmd_group.c shell_cmd_group.h |
| shell_passthrough | 提供透传功能 | shell_passthrough.c shell_passthrough.h |
### shell_cmd_group
@ -91,3 +93,40 @@
test2: command group test2
Return: 0, 0x00000000
```
### shell_passthrough
`shell_passthrough`提供了一个透传模式的功能,用户可以定义一个透传命令,执行透传命令后进入对应的透传模式,此后,命令行接收到的数据将以行为单位透传到定义的`handler`此扩展适用于一些需要进行终端数据转发的场景常见的场景比如说通过命令行发送AT指令
- 定义hanadler
passthrough模式需要定义一个函数用于处理命令行传过来的数据函数原型为`typedef int (*ShellPassthrough)(char *data, unsigned short len)`
```C
void shellPassthroughTest(char *data, unsigned short len)
{
printf("passthrough mode test, data: %s, len: %d\r\n", data, len);
}
```
- 定义passthrough
使用宏`SHELL_EXPORT_PASSTROUGH`定义`passthrough`
```C
SHELL_EXPORT_PASSTROUGH(SHELL_CMD_PERMISSION(0), passTest, passthrough>>, shellPassthroughTest, passthrough mode test);
```
- 调用
直接在命令行输入定义的`passthrough`名字可进入对应的透传模式,之后在输入的数据会按照行为单位通过`handler`调用
```sh
letter:/mnt/f/Github/letter shell/demo/x86-gcc$ passTest
passthrough>>hello
passthrough mode test, data: hello, len: 5
```
- 退出
使用组合键`Ctrl + D`退出`passthrough`,键值可以在`shell_passthrough.h`文件中的`SHELL_PASSTHROUGH_EXIT_KEY`宏修改

View File

@ -0,0 +1,55 @@
/**
* @file shell_passthrough.c
* @author Letter(nevermindzzt@gmail.com)
* @brief shell passthrough mode
* @version 0.1
* @date 2021-05-31
*
* @copyright (c) 2021 Letter
*
*/
#include "shell_passthrough.h"
/**
* @brief shell passthrough
*
* @param shell shell
* @param prompt passthrough
* @param handler pssthrough handler
*
* @return unsigned int
*/
unsigned int shellPassthrough(Shell *shell, const char *prompt, ShellPassthrough handler)
{
char data;
shell->status.isActive = 0;
shellWriteString(shell, prompt);
while (1)
{
if (shell->read && shell->read(&data, 1) == 1)
{
if (data == '\r' || data == '\n')
{
if (shell->parser.length == 0)
{
continue;
}
shellWriteString(shell, "\r\n");
shell->parser.buffer[shell->parser.length] = 0;
handler(shell->parser.buffer, shell->parser.length);
shellWriteString(shell, prompt);
}
else if (data == SHELL_PASSTHROUGH_EXIT_KEY)
{
shellWriteString(shell, "\r\n");
return -1;
}
else
{
shellHandler(shell, data);
}
}
}
return 0;
}

View File

@ -0,0 +1,62 @@
/**
* @file shell_passthrough.h
* @author Letter(nevermindzzt@gmail.com)
* @brief shell passthrough mode
* @version 0.1
* @date 2021-05-31
*
* @copyright (c) 2021 Letter
*
*/
#ifndef __SHELL_PASSTHROUGH_H__
#define __SHELL_PASSTHROUGH_H__
#include "shell.h"
/**
* @brief 退 passthrough
*/
#define SHELL_PASSTHROUGH_EXIT_KEY 0x04 // Ctrl + D
/**
* @brief passthrough handler
*
* @param data
* @param len
*
* @return int
*/
typedef int (*ShellPassthrough)(char *data, unsigned short len);
/**
* @brief shell passthrough
*/
#define SHELL_PASSTROUGH_FUNC_NAME(_name) agency##_name
/**
* @brief shell passthrough
*
* @param _name
* @param _prompt passthrough
* @param _handler passthrough handler
*/
#define SHELL_PASSTROUGH_FUNC(_name, _prompt, _handler) \
void SHELL_PASSTROUGH_FUNC_NAME(_name)(void) \
{ shellPassthrough(shellGetCurrent(), #_prompt, _handler); }
/**
* @brief shell passthrouh
*
* @param _attr
* @param _name passthrough
* @param _prompt passthrough
* @param _handler passthrough handler
* @param _desc passthrough
*/
#define SHELL_EXPORT_PASSTROUGH(_attr, _name, _prompt, _handler, _desc) \
SHELL_PASSTROUGH_FUNC(_name, _prompt, _handler) \
SHELL_EXPORT_CMD(_attr|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC), _name, SHELL_PASSTROUGH_FUNC_NAME(_name), _desc)
unsigned int shellPassthrough(Shell *shell, const char *prompt, ShellPassthrough handler);
#endif

View File

@ -150,7 +150,7 @@ static Shell *shellList[SHELL_MAX_NUMBER] = {NULL};
static void shellAdd(Shell *shell);
static void shellWriteCommandLine(Shell *shell, unsigned char newline);
static void shellWritePrompt(Shell *shell, unsigned char newline);
static void shellWriteReturnValue(Shell *shell, int value);
static int shellShowVar(Shell *shell, ShellCommand *command);
static void shellSetUser(Shell *shell, const ShellCommand *user);
@ -212,7 +212,7 @@ void shellInit(Shell *shell, char *buffer, unsigned short size)
SHELL_DEFAULT_USER,
shell->commandList.base,
0));
shellWriteCommandLine(shell, 1);
shellWritePrompt(shell, 1);
}
@ -324,7 +324,7 @@ static unsigned short shellWriteCommandDesc(Shell *shell, const char *string)
* @param newline
*
*/
static void shellWriteCommandLine(Shell *shell, unsigned char newline)
static void shellWritePrompt(Shell *shell, unsigned char newline)
{
if (shell->status.isChecked)
{
@ -778,7 +778,7 @@ void shellInsertByte(Shell *shell, char data)
if (shell->parser.length >= shell->parser.bufferSize - 1)
{
shellWriteString(shell, shellText[SHELL_TEXT_CMD_TOO_LONG]);
shellWriteCommandLine(shell, 1);
shellWritePrompt(shell, 1);
shellWriteString(shell, shell->parser.buffer);
return;
}
@ -1483,7 +1483,7 @@ void shellTab(Shell *shell)
if (shell->parser.length == 0)
{
shellListAll(shell);
shellWriteCommandLine(shell, 1);
shellWritePrompt(shell, 1);
}
else if (shell->parser.length > 0)
{
@ -1529,7 +1529,7 @@ void shellTab(Shell *shell)
if (matchNum > 1)
{
shellListItem(shell, &base[lastMatchIndex]);
shellWriteCommandLine(shell, 1);
shellWritePrompt(shell, 1);
shell->parser.length = maxMatch;
}
shell->parser.buffer[shell->parser.length] = 0;
@ -1599,7 +1599,7 @@ SHELL_EXPORT_KEY(SHELL_CMD_PERMISSION(0)|SHELL_CMD_ENABLE_UNCHECKED,
void shellEnter(Shell *shell)
{
shellExec(shell);
shellWriteCommandLine(shell, 1);
shellWritePrompt(shell, 1);
}
#if SHELL_ENTER_LF == 1
SHELL_EXPORT_KEY(SHELL_CMD_PERMISSION(0)|SHELL_CMD_ENABLE_UNCHECKED,
@ -1751,7 +1751,7 @@ void shellWriteEndLine(Shell *shell, char *buffer, int len)
if (!shell->status.isActive)
{
shellWriteCommandLine(shell, 0);
shellWritePrompt(shell, 0);
if (shell->parser.length > 0)
{
shellWriteString(shell, shell->parser.buffer);