Skip to content

Commit

Permalink
Merge pull request #88 from NevermindZZT/shell3.1
Browse files Browse the repository at this point in the history
修复 passthrough模式bug
  • Loading branch information
NevermindZZT authored Jun 2, 2021
2 parents 26acf89 + 6c555d4 commit abba67c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
9 changes: 9 additions & 0 deletions extensions/shell_enhance/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,12 @@
- 退出

使用组合键`Ctrl + D`退出`passthrough`,键值可以在`shell_passthrough.h`文件中的`SHELL_PASSTHROUGH_EXIT_KEY`宏修改

- 单次调用

某些情况下,使用`passthrough`模式时,我们可能只需要单词数据的透传,此时可以不进入`passthrough`命令行,直接调用命令带上透传的数据即可

```sh
letter:/mnt/f/Github/letter shell/demo/x86-gcc$ passTest "hello world"
passthrough mode test, data: hello world, len: 11
```
54 changes: 33 additions & 21 deletions extensions/shell_enhance/shell_passthrough.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,57 @@
*
*/
#include "shell_passthrough.h"
#include "string.h"

/**
* @brief shell passthrough 模式
*
* @param shell shell
* @param prompt passthrough 提示符
* @param handler pssthrough handler
* @param argc 命令行参数数量
* @param argv 命令行参数
*
* @return unsigned int 返回值
*/
unsigned int shellPassthrough(Shell *shell, const char *prompt, ShellPassthrough handler)
unsigned int shellPassthrough(Shell *shell, const char *prompt, ShellPassthrough handler, int argc, char *argv[])
{
char data;

shell->status.isActive = 0;
shellWriteString(shell, prompt);
while (1)
if (argc > 1)
{
if (shell->read && shell->read(&data, 1) == 1)
handler(argv[1], strlen(argv[1]));
}
else
{
shell->status.isActive = 0;
shellWriteString(shell, prompt);
while (1)
{
if (data == '\r' || data == '\n')
if (shell->read && shell->read(&data, 1) == 1)
{
if (shell->parser.length == 0)
if (data == '\r' || data == '\n')
{
continue;
if (shell->parser.length == 0)
{
continue;
}
shellWriteString(shell, "\r\n");
shell->parser.buffer[shell->parser.length] = 0;
handler(shell->parser.buffer, shell->parser.length);
shell->parser.length = 0;
shell->parser.cursor = 0;
shellWriteString(shell, prompt);
}
else if (data == SHELL_PASSTHROUGH_EXIT_KEY)
{
shellWriteString(shell, "\r\n");
return -1;
}
else
{
shellHandler(shell, data);
}
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);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions extensions/shell_enhance/shell_passthrough.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ typedef int (*ShellPassthrough)(char *data, unsigned short len);
* @param _handler passthrough 模式 handler
*/
#define SHELL_PASSTROUGH_FUNC(_name, _prompt, _handler) \
void SHELL_PASSTROUGH_FUNC_NAME(_name)(void) \
{ shellPassthrough(shellGetCurrent(), #_prompt, _handler); }
void SHELL_PASSTROUGH_FUNC_NAME(_name)(int p1, int p2) \
{ shellPassthrough(shellGetCurrent(), #_prompt, _handler, p1, (void *)p2); }

/**
* @brief shell passthrouh 定义
Expand All @@ -55,8 +55,8 @@ typedef int (*ShellPassthrough)(char *data, unsigned short len);
*/
#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)
SHELL_EXPORT_CMD(_attr|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), _name, SHELL_PASSTROUGH_FUNC_NAME(_name), _desc)

unsigned int shellPassthrough(Shell *shell, const char *prompt, ShellPassthrough handler);
unsigned int shellPassthrough(Shell *shell, const char *prompt, ShellPassthrough handler, int argc, char *argv[]);

#endif

0 comments on commit abba67c

Please sign in to comment.