forked from NevermindZZT/letter-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.h
349 lines (312 loc) · 14.8 KB
/
shell.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* @file shell.h
* @author Letter ([email protected])
* @brief letter shell
* @version 2.0.0
* @date 2018-12-29
*
* @Copyright (c) 2018 Letter
*
*/
#ifndef __SHELL_H__
#define __SHELL_H__
#include "shell_cfg.h"
#define SHELL_VERSION "2.0.5" /**< 版本号 */
/**
* @brief shell键值定义
*
*/
#define SHELL_KEY_LF 0x0A
#define SHELL_KEY_CR 0x0D
#define SHELL_KEY_TAB 0x09
#define SHELL_KEY_BACKSPACE 0x08
#define SHELL_KEY_DELETE 0x7F
#define SHELL_KEY_ESC 0x1B
#define SHELL_KEY_CTRL_T 0x14
#define SHELL_KEY_CTRL_A 0x01
#define SHELL_KEY_CTRL_B 0x02
#define SHELL_KEY_CTRL_C 0x03
#define SHELL_KEY_CTRL_D 0x04
#define SHELL_KEY_CTRL_E 0x05
#define SHELL_KEY_CTRL_F 0x06
#define SHELL_KEY_CTRL_G 0x07
#define SHELL_KEY_CTRL_H 0x08
#define SHELL_KEY_CTRL_I 0x09
#define SHELL_KEY_CTRL_J 0x0A
#define SHELL_KEY_CTRL_K 0x0B
#define SHELL_KEY_CTRL_L 0x0C
#define SHELL_KEY_CTRL_M 0x0D
#define SHELL_KEY_CTRL_N 0x0E
#define SHELL_KEY_CTRL_O 0x0F
#define SHELL_KEY_CTRL_P 0x10
#define SHELL_KEY_CTRL_Q 0x11
#define SHELL_KEY_CTRL_R 0x12
#define SHELL_KEY_CTRL_S 0x13
#define SHELL_KEY_CTRL_T 0x14
#define SHELL_KEY_CTRL_U 0x15
#define SHELL_KEY_CTRL_V 0x16
#define SHELL_KEY_CTRL_W 0x17
#define SHELL_KEY_CTRL_X 0x18
#define SHELL_KEY_CTRL_Y 0x19
#define SHELL_KEY_CTRL_Z 0x1A
/**
* @brief shell变量类型定义
*
*/
#define SHELL_VAR_INT 0
#define SHELL_VAR_SHORT 1
#define SHELL_VAR_CHAR 2
#define SHELL_VAR_POINTER 3
#define SHELL_VAL 4
#if defined(__CC_ARM) || (defined(__ARMCC_VERSION) && __ARMCC_VERSION >= 6000000)
#define SECTION(x) __attribute__((section(x)))
#elif defined(__ICCARM__)
#define SECTION(x) @ x
#elif defined(__GNUC__)
#define SECTION(x) __attribute__((section(x)))
#else
#define SECTION(x)
#endif
/**
* @brief shell命令导出
*
* @attention 命令导出方式支持keil,iar的编译器以及gcc,具体参考readme
*/
#if SHELL_USING_CMD_EXPORT == 1
#if SHELL_LONG_HELP == 1
#define SHELL_EXPORT_CMD(cmd, func, desc) \
const char shellCmd##cmd[] = #cmd; \
const char shellDesc##cmd[] = #desc; \
const SHELL_CommandTypeDef \
shellCommand##cmd SECTION("shellCommand") = \
{ \
shellCmd##cmd, \
(int (*)())func, \
shellDesc##cmd, \
(void *)0 \
}
#define SHELL_EXPORT_CMD_EX(cmd, func, desc, help) \
const char shellCmd##cmd[] = #cmd; \
const char shellDesc##cmd[] = #desc; \
const char shellHelp##cmd[] = #help; \
const SHELL_CommandTypeDef \
shellCommand##cmd SECTION("shellCommand") = \
{ \
shellCmd##cmd, \
(int (*)())func, \
shellDesc##cmd, \
shellHelp##cmd \
}
#else /** SHELL_LONG_HELP == 1 */
#define SHELL_EXPORT_CMD(cmd, func, desc) \
const char shellCmd##cmd[] = #cmd; \
const char shellDesc##cmd[] = #desc; \
const SHELL_CommandTypeDef \
shellCommand##cmd SECTION("shellCommand") = \
{ \
shellCmd##cmd \
(int (*)())func, \
shellDesc##cmd \
}
#define SHELL_EXPORT_CMD_EX(cmd, func, desc, help) \
const char shellCmd##cmd[] = #cmd; \
const char shellDesc##cmd[] = #desc; \
const SHELL_CommandTypeDef \
shellCommand##cmd SECTION("shellCommand") = \
{ \
shellCmd##cmd, \
(int (*)())func, \
shellDesc##cmd \
}
#endif /** SHELL_LONG_HELP == 1 */
#if SHELL_USING_VAR == 1
#define SHELL_EXPORT_VAR(var, variable, desc, type) \
const char shellVar##var[] = #var; \
const char shellDesc##var[] = #desc; \
const SHELL_VaribaleTypeDef \
shellVariable##var SECTION("shellVariable") = \
{ \
shellVar##var, \
(void *)(variable), \
shellDesc##var, \
type \
}
#else
#define SHELL_EXPORT_VAR(var, variable, desc, type)
#endif /** SHELL_USING_VAR == 1 */
#else
#define SHELL_EXPORT_CMD(cmd, func, desc)
#define SHELL_EXPORT_CMD_EX(cmd, func, desc, help)
#define SHELL_EXPORT_VAR(var, variable, desc, type)
#endif /** SHELL_USING_CMD_EXPORT == 1 */
#define SHELL_EXPORT_VAR_INT(var, variable, desc) \
SHELL_EXPORT_VAR(var, &variable, desc, SHELL_VAR_INT)
#define SHELL_EXPORT_VAR_SHORT(var, variable, desc) \
SHELL_EXPORT_VAR(var, &variable, desc, SHELL_VAR_SHORT)
#define SHELL_EXPORT_VAR_CHAR(var, variable, desc) \
SHELL_EXPORT_VAR(var, &variable, desc, SHELL_VAR_CHAR)
#define SHELL_EXPORT_VAR_POINTER(var, variable, desc) \
SHELL_EXPORT_VAR(var, variable, desc, SHELL_VAR_POINTER)
#define SHELL_EXPORT_VAL(val, value, desc) \
SHELL_EXPORT_VAR(val, value, desc, SHELL_VAL)
/**
* @brief shell命令条目
*
* @note 用于shell命令通过命令表的方式定义
*/
#if SHELL_USING_CMD_EXPORT == 0
#if SHELL_LONG_HELP == 1
#define SHELL_CMD_ITEM(cmd, func, desc) \
{ \
#cmd, \
(int (*)())func, \
#desc, \
(void *)0 \
}
#define SHELL_CMD_ITEM_EX(cmd, func, desc, help) \
{ \
#cmd, \
(int (*)())func, \
#desc, \
#help \
}
#else /** SHELL_LONG_HELP == 1 */
#define SHELL_CMD_ITEM(cmd, func, desc) \
{ \
#cmd, \
(int (*)())func, \
#desc \
}
#define SHELL_CMD_ITEM_EX(cmd, func, desc, help) \
{ \
#cmd, \
(int (*)())func, \
#desc, \
}
#endif /** SHELL_LONG_HELP == 1 */
#define SHELL_VAR_ITEM(var, variable, desc, type) \
{ \
#var, \
varialbe, \
#desc, \
type, \
}
#define SHELL_VAR_ITEM_INT(var, variable, desc) \
SHELL_VAR_ITEM(var, &variable, desc, SHELL_VAR_INT)
#define SHELL_VAR_ITEM_SHORT(var, variable, desc) \
SHELL_VAR_ITEM(var, &variable, desc, SHELL_VAR_SHORT)
#define SHELL_VAR_ITEM_CHAR(var, variable, desc) \
SHELL_VAR_ITEM(var, &variable, desc, SHELL_VAR_CHAR)
#define SHELL_VAR_ITEM_POINTER(var, variable, desc) \
SHELL_VAR_ITEM(var, variable, desc, SHELL_VAR_POINTER)
#endif /** SHELL_USING_CMD_EXPORT == 0 */
/**
* @brief shell读取数据函数原型
*
* @param char shell读取的字符
*
* @return char 0 读取数据成功
* @return char -1 读取数据失败
*/
typedef signed char (*shellRead)(char *);
/**
* @brief shell写数据函数原型
*
* @param const char 需写的字符
*/
typedef void (*shellWrite)(const char);
/**
* @brief shell指令执行函数原型
*
*/
typedef int (*shellFunction)();
/**
* @brief shell输入状态
*
*/
typedef enum
{
SHELL_IN_NORMAL = 0,
SHELL_ANSI_ESC,
SHELL_ANSI_CSI,
}SHELL_InputMode;
/**
* @brief shell 命令定义
*
*/
typedef struct
{
const char *name; /**< shell命令名称 */
shellFunction function; /**< shell命令函数 */
const char *desc; /**< shell命令描述 */
#if SHELL_LONG_HELP == 1
const char *help; /**< shell长帮助信息 */
#endif
}SHELL_CommandTypeDef;
#if SHELL_USING_VAR == 1
/**
* @brief shell 变量定义
*
*/
typedef struct
{
const char *name; /**< shell变量名称 */
const void *value; /**< shell变量值 */
const char *desc; /**< shell变量描述 */
const int type; /**< shell变量类型 */
} SHELL_VaribaleTypeDef;
#endif /** SHELL_USING_VAR == 1 */
/**
* @brief shell对象定义
*
*/
typedef struct
{
char *command; /**< shell命令提示符 */
char buffer[SHELL_COMMAND_MAX_LENGTH]; /**< shell命令缓冲 */
unsigned short length; /**< shell命令长度 */
unsigned short cursor; /**< shell光标位置 */
char *param[SHELL_PARAMETER_MAX_NUMBER]; /**< shell参数 */
char history[SHELL_HISTORY_MAX_NUMBER][SHELL_COMMAND_MAX_LENGTH]; /**< 历史记录 */
unsigned short historyCount; /**< 历史记录数量 */
short historyFlag; /**< 当前记录位置 */
short historyOffset; /**< 历史记录偏移 */
SHELL_CommandTypeDef *commandBase; /**< 命令表基址 */
unsigned short commandNumber; /**< 命令数量 */
#if SHELL_USING_VAR == 1
SHELL_VaribaleTypeDef *variableBase; /**< 变量表基址 */
unsigned short variableNumber; /**< 变量数量 */
#endif
int keyFuncBase; /**< 按键响应表基址 */
unsigned short keyFuncNumber; /**< 按键响应数量 */
SHELL_InputMode status; /**< 输入状态 */
unsigned char isActive; /**< 是否是当前活动shell */
shellRead read; /**< shell读字符 */
shellWrite write; /**< shell写字符 */
}SHELL_TypeDef;
/**
* @brief shell按键功能定义
*
*/
typedef struct
{
unsigned char keyCode; /**< shell按键键值 */
void (*keyFunction)(SHELL_TypeDef *); /**< 按键响应函数 */
} SHELL_KeyFunctionDef;
void shellInit(SHELL_TypeDef *shell);
void shellSetCommandList(SHELL_TypeDef *shell, SHELL_CommandTypeDef *base, unsigned short size);
#if SHELL_USING_VAR == 1
void shellSetVariableList(SHELL_TypeDef *shell, SHELL_VaribaleTypeDef *base, unsigned short size);
int shellGetVariable(SHELL_TypeDef *shell, char *var);
#endif /** SHELL_USING_VAR == 1 */
void shellSetKeyFuncList(SHELL_TypeDef *shell, SHELL_KeyFunctionDef *base, unsigned short size);
SHELL_TypeDef *shellGetCurrent(void);
void shellPrint(SHELL_TypeDef *shell, char *fmt, ...);
unsigned short shellDisplay(SHELL_TypeDef *shell, const char *string);
void shellHandler(SHELL_TypeDef *shell, char data);
void shellHelp(int argc, char *argv[]);
void shellClear(void);
#if SHELL_USING_TASK == 1
void shellTask(void *param);
#endif
#endif