diff --git a/include/HShellLex.h b/include/HShellLex.h index 19420ac..7121fa3 100644 --- a/include/HShellLex.h +++ b/include/HShellLex.h @@ -12,6 +12,13 @@ typedef uint8_t HShellLenType; // 是否使用密码 #define HSHELL_USE_PASSWORD +#define HSHELL_USER "hdroot" +#define HSHELL_PASSWORD "hd456321;" + +// 是否使用上一次缓存 +#define HSHELL_USE_BACK_BUFFER +#ifdef HSHELL_USE_BACK_BUFFER +#endif ///< 可配置最多可注册回调多少个 #ifndef HSHELL_CALL_MAX @@ -25,7 +32,7 @@ typedef uint8_t HShellLenType; ///< 解析token参数的最大个数 #ifndef HSHELL_CMD_TOKEN_MAX -#define HSHELL_CMD_TOKEN_MAX 3 +#define HSHELL_CMD_TOKEN_MAX 4 #endif ///< 打印帮助命令行长度多少换行 diff --git a/src/HShellLex.c b/src/HShellLex.c index 6f76597..a961c22 100644 --- a/src/HShellLex.c +++ b/src/HShellLex.c @@ -41,11 +41,15 @@ struct __attribute__((packed)) ShellInfo { // 命令行缓存数据 static HVECTOR_DEFINE(cmdBuffer_, HSHELL_CMD_LINE_BUFFER); +#ifdef HSHELL_USE_BACK_BUFFER +static HVECTOR_DEFINE(prevBuffer_, HSHELL_CMD_LINE_BUFFER); +#endif static struct ShellInfo shellInfo_; static void PrintAllCmd() { uint16_t len = 0; + uint16_t pos = 10; for (int i = 0; i < HSHELL_CALL_MAX; ++i) { if (shellInfo_.shellCmd[i].match == NULL) { continue; @@ -56,10 +60,12 @@ static void PrintAllCmd() { continue; } - HSHELL_PRINTF("%s\t", (const char *)shellInfo_.shellCmd[i].match[j].match); - len += shellInfo_.shellCmd[i].match[j].matchLen; - if (len >= HSHELL_CMD_LINE_NUM) { + pos = pos > shellInfo_.shellCmd[i].match[j].matchLen ? pos : shellInfo_.shellCmd[i].match[j].matchLen; + HSHELL_PRINTF("%-*s ", pos, (const char *)shellInfo_.shellCmd[i].match[j].match); + len += pos; + if (len >= 80) { HSHELL_PRINTF("\r\n"); + pos = 10; len = 0; } } @@ -86,15 +92,18 @@ static void ShellParse() if (shellInfo_.password == 0 || shellInfo_.userName == 0) { shellInfo_.needPrint = 1; + LogD("password[%d] userName[%d], data[%s]", shellInfo_.password, shellInfo_.userName, shellInfo_.cmdTokens[0].str); // 这个状态下说明用户名输入错误, 无论输入什么都是失败 if (shellInfo_.password == 1 && shellInfo_.userName == 0) { shellInfo_.password = 0; return ; } +#define _ICONV(name) name, sizeof(name) - 1 +#define ICONV(name) _ICONV(name) // 输入用户名 if (shellInfo_.userName == 0) { - if (HShellDiffString(shellInfo_.cmdTokens, len, 0, "hdroot", 6, 0) == 0) { + if (HShellDiffString(shellInfo_.cmdTokens, len, 0, ICONV(HSHELL_USER), 0) == 0) { // 用户名错误, 给输入假的密码机会 shellInfo_.password = 1; return ; @@ -105,7 +114,7 @@ static void ShellParse() } if (shellInfo_.password == 0) { - if (HShellDiffString(shellInfo_.cmdTokens, len, 0, "hd123", 5, 0) == 0) { + if (HShellDiffString(shellInfo_.cmdTokens, len, 0, ICONV(HSHELL_PASSWORD), 0) == 0) { shellInfo_.userName = 0; return ; } @@ -114,6 +123,8 @@ static void ShellParse() return ; } } +#undef _ICONV +#undef ICONV #endif if (shellInfo_.cmdTokens[0].len == 1 && shellInfo_.cmdTokens[0].str[0] == '?') { @@ -194,6 +205,21 @@ static void DeleteAfterByte() { } +static void SwapPrevBuffer() { +#ifdef HSHELL_USE_BACK_BUFFER + int len = HVectorGetUseByteLen(cmdBuffer_); + char *swapBuf[HSHELL_CMD_LINE_BUFFER]; + memcpy(swapBuf, HVectorGetByteDataPtr(cmdBuffer_, 0), len); + HVectorClear(cmdBuffer_); + HVectorAddBytes(cmdBuffer_, (const uint8_t *)HVectorGetByteDataPtr(prevBuffer_, 0), HVectorGetUseByteLen(prevBuffer_)); + HVectorClear(prevBuffer_); + HVectorAddBytes(prevBuffer_, (const uint8_t *)swapBuf, len); + shellInfo_.cursorPos = HVectorGetUseLen(cmdBuffer_); + shellInfo_.needPrint = 1; +#endif +} + + static void AddCmdData(uint8_t data) { enum eEscStaus { kEscNone, @@ -235,6 +261,7 @@ static void AddCmdData(uint8_t data) { } return ; case 'A': // 上键 case 'B': // 下键 + SwapPrevBuffer(); shellInfo_.escStatus = kEscNone; return ; case '3': { @@ -348,6 +375,7 @@ uint8_t HShellDiffString(const HShellCmdToken *token, uint8_t tokenLen, uint8_t } if (token[index].len != strLen) { + LogD("token[%d] len[%d] is not equal strLen[%d]", index, token[index].len, strLen); return 0; } @@ -446,6 +474,12 @@ void HShellRun() { HSHELL_PRINTFL(""); ShellParse(); +#ifdef HSHELL_USE_BACK_BUFFER + if (HVectorEmpty(cmdBuffer_) == 0) { + HVectorClear(prevBuffer_); + HVectorAddBytes(prevBuffer_, (const uint8_t *)HVectorGetByteDataPtr(cmdBuffer_, 0), HVectorGetUseByteLen(cmdBuffer_)); + } +#endif HVectorClear(cmdBuffer_); HVectorSetData(cmdBuffer_, 0, '\0'); PrintCmdLine();