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

新增 字符串变量类型

移除 常量类型,改为使用变量只读属性实现
This commit is contained in:
Letter 2020-08-23 17:46:05 +08:00
parent 864ce4874f
commit 2bc0966db7
7 changed files with 81 additions and 47 deletions

View File

@ -1,8 +1,8 @@
# letter shell 3.0
![version](https://img.shields.io/badge/version-3.0.4-brightgreen.svg)
![version](https://img.shields.io/badge/version-3.0.5-brightgreen.svg)
![standard](https://img.shields.io/badge/standard-c99-brightgreen.svg)
![build](https://img.shields.io/badge/build-2020.08.01-brightgreen.svg)
![build](https://img.shields.io/badge/build-2020.08.23-brightgreen.svg)
![license](https://img.shields.io/badge/license-MIT-brightgreen.svg)
一个功能强大的嵌入式shell
@ -298,6 +298,8 @@ letter shell 3.0对可执行命令,按键,用户以及变量分别提供了
}
```
变量定义时,`_value`应该是变量的引用,如果变量不允许修改,则需要在增加`SHELL_CMD_READ_ONLY`属性
3. 用户定义
使用宏`SHELL_EXPORT_USER`定义用户,定义如下
@ -364,15 +366,15 @@ union
unsigned char permission : 8; /**< command权限 */
ShellCommandType type : 4; /**< command类型 */
unsigned char enableUnchecked : 1; /**< 在未校验密码的情况下可用 */
unsigned char disableReturn : 1; /**< 禁用返回值输出 */
unsigned char reserve : 2; /**< 保留 */
unsigned char readOnly : 1; /**< 只读 */
unsigned char reserve : 1; /**< 保留 */
unsigned char paramNum : 4; /**< 参数数量 */
} attrs;
int value;
} attr;
```
在定义命令时,需要给定这些值,可以通过宏`SHELL_CMD_PERMISSION(permission)`, `SHELL_CMD_TYPE(type)`, `SHELL_CMD_ENABLE_UNCHECKED`, `SHELL_CMD_DISABLE_RETURN`, `SHELL_CMD_PARAM_NUM(num)`快速声明
在定义命令时,需要给定这些值,可以通过宏`SHELL_CMD_PERMISSION(permission)`, `SHELL_CMD_TYPE(type)`, `SHELL_CMD_ENABLE_UNCHECKED`, `SHELL_CMD_DISABLE_RETURN`, `SHELL_CMD_READ_ONLY`, `SHELL_CMD_PARAM_NUM(num)`快速声明
## 代理函数和代理参数解析

View File

@ -155,7 +155,7 @@
/**
* @brief shell自动锁定超时
* 使`SHELL_USING_AUTH`shell
* shell当前用户密码有效的时候生shell
* 0`SHELL_GET_TICK()`
* @note 使`SHELL_GET_TICK()`
*/

View File

@ -155,7 +155,7 @@
/**
* @brief shell自动锁定超时
* 使`SHELL_USING_AUTH`shell
* shell当前用户密码有效的时候生shell
* 0`SHELL_GET_TICK()`
* @note 使`SHELL_GET_TICK()`
*/

View File

@ -154,7 +154,7 @@
/**
* @brief shell自动锁定超时
* 使`SHELL_USING_AUTH`shell
* shell当前用户密码有效的时候生shell
* 0`SHELL_GET_TICK()`
* @note 使`SHELL_GET_TICK()`
*/

View File

@ -62,7 +62,7 @@ enum
SHELL_TEXT_KEY_LIST, /**< 按键列表标题 */
SHELL_TEXT_CMD_NOT_FOUND, /**< 命令未找到 */
SHELL_TEXT_POINT_CANNOT_MODIFY, /**< 指针变量不允许修改 */
SHELL_TEXT_VAL_CANNOT_MODIFY, /**< 常量不允许修改 */
SHELL_TEXT_VAR_READ_ONLY_CANNOT_MODIFY, /**< 只读变量不允许修改 */
SHELL_TEXT_NOT_VAR, /**< 命令不是变量 */
SHELL_TEXT_VAR_NOT_FOUND, /**< 变量未找到 */
SHELL_TEXT_HELP_HEADER, /**< help头 */
@ -110,8 +110,8 @@ static const char *shellText[] =
"Command not Found\r\n",
[SHELL_TEXT_POINT_CANNOT_MODIFY] =
"can't set pointer\r\n",
[SHELL_TEXT_VAL_CANNOT_MODIFY] =
"can't set val\r\n",
[SHELL_TEXT_VAR_READ_ONLY_CANNOT_MODIFY] =
"can't set read only var\r\n",
[SHELL_TEXT_NOT_VAR] =
" is not a var\r\n",
[SHELL_TEXT_VAR_NOT_FOUND] =
@ -505,7 +505,7 @@ static const char* shellGetCommandName(ShellCommand *command)
{
return command->data.cmd.name;
}
else if (command->attr.attrs.type <= SHELL_TYPE_VAL)
else if (command->attr.attrs.type <= SHELL_TYPE_VAR_POINT)
{
return command->data.var.name;
}
@ -533,7 +533,7 @@ static const char* shellGetCommandDesc(ShellCommand *command)
{
return command->data.cmd.desc;
}
else if (command->attr.attrs.type <= SHELL_TYPE_VAL)
else if (command->attr.attrs.type <= SHELL_TYPE_VAR_POINT)
{
return command->data.var.desc;
}
@ -566,7 +566,7 @@ void shellListItem(Shell *shell, ShellCommand *item)
{
shellWriteString(shell, shellText[SHELL_TEXT_TYPE_CMD]);
}
else if (item->attr.attrs.type <= SHELL_TYPE_VAL)
else if (item->attr.attrs.type <= SHELL_TYPE_VAR_POINT)
{
shellWriteString(shell, shellText[SHELL_TEXT_TYPE_VAR]);
}
@ -627,7 +627,7 @@ void shellListVar(Shell *shell)
for (short i = 0; i < shell->commandList.count; i++)
{
if (base[i].attr.attrs.type > SHELL_TYPE_CMD_FUNC
&& base[i].attr.attrs.type <= SHELL_TYPE_VAL
&& base[i].attr.attrs.type <= SHELL_TYPE_VAR_POINT
&& shellCheckPermission(shell, &base[i]) == 0)
{
shellListItem(shell, &base[i]);
@ -647,7 +647,7 @@ void shellListUser(Shell *shell)
shellWriteString(shell, shellText[SHELL_TEXT_USER_LIST]);
for (short i = 0; i < shell->commandList.count; i++)
{
if (base[i].attr.attrs.type > SHELL_TYPE_VAL
if (base[i].attr.attrs.type > SHELL_TYPE_VAR_POINT
&& base[i].attr.attrs.type <= SHELL_TYPE_USER
&& shellCheckPermission(shell, &base[i]) == 0)
{
@ -959,8 +959,8 @@ int shellGetVarValue(Shell *shell, ShellCommand *command)
case SHELL_TYPE_VAR_CHAR:
value = *((char *)(command->data.var.value));
break;
case SHELL_TYPE_VAR_STRING:
case SHELL_TYPE_VAR_POINT:
case SHELL_TYPE_VAL:
value = (int)(command->data.var.value);
break;
default:
@ -979,25 +979,32 @@ int shellGetVarValue(Shell *shell, ShellCommand *command)
*/
void shellSetVarValue(Shell *shell, ShellCommand *command, int value)
{
switch (command->attr.attrs.type)
if (command->attr.attrs.readOnly)
{
case SHELL_TYPE_VAR_INT:
*((int *)(command->data.var.value)) = value;
break;
case SHELL_TYPE_VAR_SHORT:
*((short *)(command->data.var.value)) = value;
break;
case SHELL_TYPE_VAR_CHAR:
*((char *)(command->data.var.value)) = value;
break;
case SHELL_TYPE_VAR_POINT:
shellWriteString(shell, shellText[SHELL_TEXT_POINT_CANNOT_MODIFY]);
break;
case SHELL_TYPE_VAL:
shellWriteString(shell, shellText[SHELL_TEXT_VAL_CANNOT_MODIFY]);
break;
default:
break;
shellWriteString(shell, shellText[SHELL_TEXT_VAR_READ_ONLY_CANNOT_MODIFY]);
}
else
{
switch (command->attr.attrs.type)
{
case SHELL_TYPE_VAR_INT:
*((int *)(command->data.var.value)) = value;
break;
case SHELL_TYPE_VAR_SHORT:
*((short *)(command->data.var.value)) = value;
break;
case SHELL_TYPE_VAR_CHAR:
*((char *)(command->data.var.value)) = value;
break;
case SHELL_TYPE_VAR_STRING:
shellStringCopy(((char *)(command->data.var.value)), (char *)value);
break;
case SHELL_TYPE_VAR_POINT:
shellWriteString(shell, shellText[SHELL_TEXT_POINT_CANNOT_MODIFY]);
break;
default:
break;
}
}
shellShowVar(shell, command);
}
@ -1013,16 +1020,34 @@ static void shellShowVar(Shell *shell, ShellCommand *command)
{
char buffer[12] = "00000000000";
int value = shellGetVarValue(shell, command);
shellWriteString(shell, command->data.var.name);
shellWriteString(shell, " = ");
shellWriteString(shell, &buffer[11 - shellToDec(value, buffer)]);
shellWriteString(shell, ", 0x");
for (short i = 0; i < 11; i++)
switch (command->attr.attrs.type)
{
buffer[i] = '0';
case SHELL_TYPE_VAR_INT:
case SHELL_TYPE_VAR_SHORT:
case SHELL_TYPE_VAR_CHAR:
case SHELL_TYPE_VAR_POINT:
shellWriteString(shell, &buffer[11 - shellToDec(value, buffer)]);
shellWriteString(shell, ", 0x");
for (short i = 0; i < 11; i++)
{
buffer[i] = '0';
}
shellToHex(value, buffer);
shellWriteString(shell, buffer);
break;
case SHELL_TYPE_VAR_STRING:
shellWriteString(shell, "\"");
shellWriteString(shell, (char *)value);
shellWriteString(shell, "\"");
break;
default:
break;
}
shellToHex(value, buffer);
shellWriteString(shell, buffer);
shellWriteString(shell, "\r\n");
}
@ -1051,7 +1076,7 @@ int shellSetVar(char *name, int value)
return 0;
}
if (command->attr.attrs.type < SHELL_TYPE_VAR_INT
|| command->attr.attrs.type > SHELL_TYPE_VAL)
|| command->attr.attrs.type > SHELL_TYPE_VAR_POINT)
{
shellWriteString(shell, name);
shellWriteString(shell, shellText[SHELL_TEXT_NOT_VAR]);
@ -1097,7 +1122,7 @@ static void shellRunCommand(Shell *shell, ShellCommand *command)
}
}
else if (command->attr.attrs.type >= SHELL_TYPE_VAR_INT
&& command->attr.attrs.type <= SHELL_TYPE_VAL)
&& command->attr.attrs.type <= SHELL_TYPE_VAR_POINT)
{
shellShowVar(shell, command);
}

View File

@ -14,7 +14,7 @@
#include "shell_cfg.h"
#define SHELL_VERSION "3.0.4" /**< 版本号 */
#define SHELL_VERSION "3.0.5" /**< 版本号 */
/**
@ -56,6 +56,12 @@
#define SHELL_CMD_DISABLE_RETURN \
(1 << 13)
/**
* @brief ()
*/
#define SHELL_CMD_READ_ONLY \
(1 << 14)
/**
* @brief
*/
@ -284,8 +290,8 @@ typedef enum
SHELL_TYPE_VAR_INT, /**< int型变量 */
SHELL_TYPE_VAR_SHORT, /**< short型变量 */
SHELL_TYPE_VAR_CHAR, /**< char型变量 */
SHELL_TYPE_VAR_STRING, /**< string型变量 */
SHELL_TYPE_VAR_POINT, /**< 指针型变量 */
SHELL_TYPE_VAL, /**< 常量 */
SHELL_TYPE_USER, /**< 用户 */
SHELL_TYPE_KEY, /**< 按键 */
} ShellCommandType;
@ -351,7 +357,8 @@ typedef struct shell_command
ShellCommandType type : 4; /**< command类型 */
unsigned char enableUnchecked : 1; /**< 在未校验密码的情况下可用 */
unsigned char disableReturn : 1; /**< 禁用返回值输出 */
unsigned char reserve : 2; /**< 保留 */
unsigned char readOnly : 1; /**< 只读 */
unsigned char reserve : 1; /**< 保留 */
unsigned char paramNum : 4; /**< 参数数量 */
} attrs;
int value;

View File

@ -153,7 +153,7 @@
/**
* @brief shell自动锁定超时
* 使`SHELL_USING_AUTH`shell
* shell当前用户密码有效的时候生shell
* 0`SHELL_GET_TICK()`
* @note 使`SHELL_GET_TICK()`
*/