1. 增加shell命令行支持记忆上一次操作
This commit is contained in:
parent
8631a84333
commit
399db9f4e9
@ -12,6 +12,13 @@ typedef uint8_t HShellLenType;
|
|||||||
|
|
||||||
// 是否使用密码
|
// 是否使用密码
|
||||||
#define HSHELL_USE_PASSWORD
|
#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
|
#ifndef HSHELL_CALL_MAX
|
||||||
@ -25,7 +32,7 @@ typedef uint8_t HShellLenType;
|
|||||||
|
|
||||||
///< 解析token参数的最大个数
|
///< 解析token参数的最大个数
|
||||||
#ifndef HSHELL_CMD_TOKEN_MAX
|
#ifndef HSHELL_CMD_TOKEN_MAX
|
||||||
#define HSHELL_CMD_TOKEN_MAX 3
|
#define HSHELL_CMD_TOKEN_MAX 4
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
///< 打印帮助命令行长度多少换行
|
///< 打印帮助命令行长度多少换行
|
||||||
|
|||||||
@ -41,11 +41,15 @@ struct __attribute__((packed)) ShellInfo {
|
|||||||
|
|
||||||
// 命令行缓存数据
|
// 命令行缓存数据
|
||||||
static HVECTOR_DEFINE(cmdBuffer_, HSHELL_CMD_LINE_BUFFER);
|
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 struct ShellInfo shellInfo_;
|
||||||
|
|
||||||
|
|
||||||
static void PrintAllCmd() {
|
static void PrintAllCmd() {
|
||||||
uint16_t len = 0;
|
uint16_t len = 0;
|
||||||
|
uint16_t pos = 10;
|
||||||
for (int i = 0; i < HSHELL_CALL_MAX; ++i) {
|
for (int i = 0; i < HSHELL_CALL_MAX; ++i) {
|
||||||
if (shellInfo_.shellCmd[i].match == NULL) {
|
if (shellInfo_.shellCmd[i].match == NULL) {
|
||||||
continue;
|
continue;
|
||||||
@ -56,10 +60,12 @@ static void PrintAllCmd() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
HSHELL_PRINTF("%s\t", (const char *)shellInfo_.shellCmd[i].match[j].match);
|
pos = pos > shellInfo_.shellCmd[i].match[j].matchLen ? pos : shellInfo_.shellCmd[i].match[j].matchLen;
|
||||||
len += shellInfo_.shellCmd[i].match[j].matchLen;
|
HSHELL_PRINTF("%-*s ", pos, (const char *)shellInfo_.shellCmd[i].match[j].match);
|
||||||
if (len >= HSHELL_CMD_LINE_NUM) {
|
len += pos;
|
||||||
|
if (len >= 80) {
|
||||||
HSHELL_PRINTF("\r\n");
|
HSHELL_PRINTF("\r\n");
|
||||||
|
pos = 10;
|
||||||
len = 0;
|
len = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -86,15 +92,18 @@ static void ShellParse()
|
|||||||
if (shellInfo_.password == 0 || shellInfo_.userName == 0) {
|
if (shellInfo_.password == 0 || shellInfo_.userName == 0) {
|
||||||
shellInfo_.needPrint = 1;
|
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) {
|
if (shellInfo_.password == 1 && shellInfo_.userName == 0) {
|
||||||
shellInfo_.password = 0;
|
shellInfo_.password = 0;
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define _ICONV(name) name, sizeof(name) - 1
|
||||||
|
#define ICONV(name) _ICONV(name)
|
||||||
// 输入用户名
|
// 输入用户名
|
||||||
if (shellInfo_.userName == 0) {
|
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;
|
shellInfo_.password = 1;
|
||||||
return ;
|
return ;
|
||||||
@ -105,7 +114,7 @@ static void ShellParse()
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (shellInfo_.password == 0) {
|
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;
|
shellInfo_.userName = 0;
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
@ -114,6 +123,8 @@ static void ShellParse()
|
|||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#undef _ICONV
|
||||||
|
#undef ICONV
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (shellInfo_.cmdTokens[0].len == 1 && shellInfo_.cmdTokens[0].str[0] == '?') {
|
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) {
|
static void AddCmdData(uint8_t data) {
|
||||||
enum eEscStaus {
|
enum eEscStaus {
|
||||||
kEscNone,
|
kEscNone,
|
||||||
@ -235,6 +261,7 @@ static void AddCmdData(uint8_t data) {
|
|||||||
} return ;
|
} return ;
|
||||||
case 'A': // 上键
|
case 'A': // 上键
|
||||||
case 'B': // 下键
|
case 'B': // 下键
|
||||||
|
SwapPrevBuffer();
|
||||||
shellInfo_.escStatus = kEscNone;
|
shellInfo_.escStatus = kEscNone;
|
||||||
return ;
|
return ;
|
||||||
case '3': {
|
case '3': {
|
||||||
@ -348,6 +375,7 @@ uint8_t HShellDiffString(const HShellCmdToken *token, uint8_t tokenLen, uint8_t
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (token[index].len != strLen) {
|
if (token[index].len != strLen) {
|
||||||
|
LogD("token[%d] len[%d] is not equal strLen[%d]", index, token[index].len, strLen);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -446,6 +474,12 @@ void HShellRun() {
|
|||||||
|
|
||||||
HSHELL_PRINTFL("");
|
HSHELL_PRINTFL("");
|
||||||
ShellParse();
|
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_);
|
HVectorClear(cmdBuffer_);
|
||||||
HVectorSetData(cmdBuffer_, 0, '\0');
|
HVectorSetData(cmdBuffer_, 0, '\0');
|
||||||
PrintCmdLine();
|
PrintCmdLine();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user