diff --git a/extensions/shell_enhance/readme.md b/extensions/shell_enhance/readme.md index 7c9dc45..145ec8c 100644 --- a/extensions/shell_enhance/readme.md +++ b/extensions/shell_enhance/readme.md @@ -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 + ``` diff --git a/extensions/shell_enhance/shell_passthrough.c b/extensions/shell_enhance/shell_passthrough.c index bf1ef89..f951c58 100644 --- a/extensions/shell_enhance/shell_passthrough.c +++ b/extensions/shell_enhance/shell_passthrough.c @@ -9,6 +9,7 @@ * */ #include "shell_passthrough.h" +#include "string.h" /** * @brief shell passthrough 模式 @@ -16,38 +17,49 @@ * @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); } } } diff --git a/extensions/shell_enhance/shell_passthrough.h b/extensions/shell_enhance/shell_passthrough.h index 96a67cb..f257cf2 100644 --- a/extensions/shell_enhance/shell_passthrough.h +++ b/extensions/shell_enhance/shell_passthrough.h @@ -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 定义 @@ -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 \ No newline at end of file