amiro-os / core / src / aos_shell.c @ ae8211ea
History | View | Annotate | Download (54.8 KB)
1 |
/*
|
---|---|
2 |
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
|
3 |
Copyright (C) 2016..2019 Thomas Schöpping et al.
|
4 |
|
5 |
This program is free software: you can redistribute it and/or modify
|
6 |
it under the terms of the GNU General Public License as published by
|
7 |
the Free Software Foundation, either version 3 of the License, or
|
8 |
(at your option) any later version.
|
9 |
|
10 |
This program is distributed in the hope that it will be useful,
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
GNU General Public License for more details.
|
14 |
|
15 |
You should have received a copy of the GNU General Public License
|
16 |
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17 |
*/
|
18 |
|
19 |
/**
|
20 |
* @file aos_shell.c
|
21 |
* @brief Shell code.
|
22 |
* @details Shell code as well as shell related channels and streams.
|
23 |
*
|
24 |
* @addtogroup aos_shell
|
25 |
* @{
|
26 |
*/
|
27 |
|
28 |
#include <amiroos.h> |
29 |
#include <string.h> |
30 |
|
31 |
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__) |
32 |
|
33 |
/******************************************************************************/
|
34 |
/* LOCAL DEFINITIONS */
|
35 |
/******************************************************************************/
|
36 |
|
37 |
/**
|
38 |
* @brief Event mask to be set on OS related events.
|
39 |
*/
|
40 |
#define AOS_SHELL_EVENTMASK_OS EVENT_MASK(0) |
41 |
|
42 |
/**
|
43 |
* @brief Event mask to be set on a input event.
|
44 |
*/
|
45 |
#define AOS_SHELL_EVENTMASK_INPUT EVENT_MASK(1) |
46 |
|
47 |
/******************************************************************************/
|
48 |
/* EXPORTED VARIABLES */
|
49 |
/******************************************************************************/
|
50 |
|
51 |
/******************************************************************************/
|
52 |
/* LOCAL TYPES */
|
53 |
/******************************************************************************/
|
54 |
|
55 |
/*
|
56 |
* forward declarations
|
57 |
*/
|
58 |
static size_t _channelwrite(void *instance, const uint8_t *bp, size_t n); |
59 |
static size_t _channelread(void *instance, uint8_t *bp, size_t n); |
60 |
static msg_t _channelput(void *instance, uint8_t b); |
61 |
static msg_t _channelget(void *instance); |
62 |
static msg_t _channelputt(void *instance, uint8_t b, sysinterval_t time); |
63 |
static msg_t _channelgett(void *instance, sysinterval_t time); |
64 |
static size_t _channelwritet(void *instance, const uint8_t *bp, size_t n, sysinterval_t time); |
65 |
static size_t _channelreadt(void *instance, uint8_t *bp, size_t n, sysinterval_t time); |
66 |
static msg_t _channelctl(void *instance, unsigned int operation, void *arg); |
67 |
static size_t _streamwrite(void *instance, const uint8_t *bp, size_t n); |
68 |
static size_t _stremread(void *instance, uint8_t *bp, size_t n); |
69 |
static msg_t _streamput(void *instance, uint8_t b); |
70 |
static msg_t _streamget(void *instance); |
71 |
|
72 |
static const struct AosShellChannelVMT _channelvmt = { |
73 |
(size_t) 0,
|
74 |
_channelwrite, |
75 |
_channelread, |
76 |
_channelput, |
77 |
_channelget, |
78 |
_channelputt, |
79 |
_channelgett, |
80 |
_channelwritet, |
81 |
_channelreadt, |
82 |
_channelctl, |
83 |
}; |
84 |
|
85 |
static const struct AosShellStreamVMT _streamvmt = { |
86 |
(size_t) 0,
|
87 |
_streamwrite, |
88 |
_stremread, |
89 |
_streamput, |
90 |
_streamget, |
91 |
}; |
92 |
|
93 |
/**
|
94 |
* @brief Enumerator of special keyboard keys.
|
95 |
*/
|
96 |
typedef enum special_key { |
97 |
KEY_UNKNOWN, /**< any/unknow key */
|
98 |
KEY_AMBIGUOUS, /**< key is ambiguous */
|
99 |
KEY_TAB, /**< tabulator key */
|
100 |
KEY_ESCAPE, /**< escape key */
|
101 |
KEY_BACKSPACE, /**< backspace key */
|
102 |
KEY_INSERT, /**< insert key */
|
103 |
KEY_DELETE, /**< delete key */
|
104 |
KEY_HOME, /**< home key */
|
105 |
KEY_END, /**< end key */
|
106 |
KEY_PAGE_UP, /**< page up key */
|
107 |
KEY_PAGE_DOWN, /**< page down key */
|
108 |
KEY_ARROW_UP, /**< arrow up key */
|
109 |
KEY_ARROW_DOWN, /**< arrow down key */
|
110 |
KEY_ARROW_LEFT, /**< arrow left key */
|
111 |
KEY_ARROW_RIGHT, /**< arrow right key */
|
112 |
KEY_CTRL_ARROW_UP, /**< CTRL + arrow up key */
|
113 |
KEY_CTRL_ARROW_DOWN, /**< CTRL + arrow down key */
|
114 |
KEY_CTRL_ARROW_LEFT, /**< CTRL + arrow left key */
|
115 |
KEY_CTRL_ARROW_RIGHT, /**< CTRL + arrow right key */
|
116 |
} special_key_t; |
117 |
|
118 |
/**
|
119 |
* @brief Enumerator for case (in)sensitive character matching.
|
120 |
*/
|
121 |
typedef enum charmatch { |
122 |
CHAR_MATCH_NOT = 0, /**< Characters do not match at all. */ |
123 |
CHAR_MATCH_NCASE = 1, /**< Characters would match case insensitive. */ |
124 |
CHAR_MATCH_CASE = 2, /**< Characters do match with case. */ |
125 |
} charmatch_t; |
126 |
|
127 |
/******************************************************************************/
|
128 |
/* LOCAL VARIABLES */
|
129 |
/******************************************************************************/
|
130 |
|
131 |
/******************************************************************************/
|
132 |
/* LOCAL FUNCTIONS */
|
133 |
/******************************************************************************/
|
134 |
|
135 |
/**
|
136 |
* @brief Implementation of the BaseAsynchronous write() method (inherited from BaseSequentialStream).
|
137 |
*/
|
138 |
static size_t _channelwrite(void *instance, const uint8_t *bp, size_t n) |
139 |
{ |
140 |
if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
141 |
return streamWrite(((AosShellChannel*)instance)->asyncchannel, bp, n);
|
142 |
} else {
|
143 |
return 0; |
144 |
} |
145 |
} |
146 |
|
147 |
/**
|
148 |
* @brief Implementation of the BaseAsynchronous read() method (inherited from BaseSequentialStream).
|
149 |
*/
|
150 |
static size_t _channelread(void *instance, uint8_t *bp, size_t n) |
151 |
{ |
152 |
if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
153 |
return streamRead(((AosShellChannel*)instance)->asyncchannel, bp, n);
|
154 |
} else {
|
155 |
return 0; |
156 |
} |
157 |
} |
158 |
|
159 |
/**
|
160 |
* @brief Implementation of the BaseAsynchronous put() method (inherited from BaseSequentialStream).
|
161 |
*/
|
162 |
static msg_t _channelput(void *instance, uint8_t b) |
163 |
{ |
164 |
if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
165 |
return streamPut(((AosShellChannel*)instance)->asyncchannel, b);
|
166 |
} else {
|
167 |
return MSG_RESET;
|
168 |
} |
169 |
} |
170 |
|
171 |
/**
|
172 |
* @brief Implementation of the BaseAsynchronous get() method (inherited from BaseSequentialStream).
|
173 |
*/
|
174 |
static msg_t _channelget(void *instance) |
175 |
{ |
176 |
if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
177 |
return streamGet(((AosShellChannel*)instance)->asyncchannel);
|
178 |
} else {
|
179 |
return MSG_RESET;
|
180 |
} |
181 |
} |
182 |
|
183 |
/**
|
184 |
* @brief Implementation of the BaseAsynchronous putt() method.
|
185 |
*/
|
186 |
static msg_t _channelputt(void *instance, uint8_t b, sysinterval_t time) |
187 |
{ |
188 |
if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
189 |
return chnPutTimeout(((AosShellChannel*)instance)->asyncchannel, b, time);
|
190 |
} else {
|
191 |
return MSG_RESET;
|
192 |
} |
193 |
} |
194 |
|
195 |
/**
|
196 |
* @brief Implementation of the BaseAsynchronous gett() method.
|
197 |
*/
|
198 |
static msg_t _channelgett(void *instance, sysinterval_t time) |
199 |
{ |
200 |
if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
201 |
return chnGetTimeout(((AosShellChannel*)instance)->asyncchannel, time);
|
202 |
} else {
|
203 |
return MSG_RESET;
|
204 |
} |
205 |
} |
206 |
|
207 |
/**
|
208 |
* @brief Implementation of the BaseAsynchronous writet() method.
|
209 |
*/
|
210 |
static size_t _channelwritet(void *instance, const uint8_t *bp, size_t n, sysinterval_t time) |
211 |
{ |
212 |
if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
213 |
return chnWriteTimeout(((AosShellChannel*)instance)->asyncchannel, bp, n, time);
|
214 |
} else {
|
215 |
return 0; |
216 |
} |
217 |
} |
218 |
|
219 |
/**
|
220 |
* @brief Implementation of the BaseAsynchronous readt() method.
|
221 |
*/
|
222 |
static size_t _channelreadt(void *instance, uint8_t *bp, size_t n, sysinterval_t time) |
223 |
{ |
224 |
if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
225 |
return chnReadTimeout(((AosShellChannel*)instance)->asyncchannel, bp, n, time);
|
226 |
} else {
|
227 |
return 0; |
228 |
} |
229 |
} |
230 |
|
231 |
/**
|
232 |
* @brief Implementation of the BaseAsynchronousChannel ctl() method.
|
233 |
*/
|
234 |
static msg_t _channelctl(void *instance, unsigned int operation, void *arg) |
235 |
{ |
236 |
(void) instance;
|
237 |
|
238 |
switch (operation) {
|
239 |
case CHN_CTL_NOP:
|
240 |
osalDbgCheck(arg == NULL);
|
241 |
break;
|
242 |
case CHN_CTL_INVALID:
|
243 |
osalDbgAssert(false, "invalid CTL operation"); |
244 |
break;
|
245 |
default:
|
246 |
break;
|
247 |
} |
248 |
return MSG_OK;
|
249 |
} |
250 |
|
251 |
static size_t _streamwrite(void *instance, const uint8_t *bp, size_t n) |
252 |
{ |
253 |
aosDbgCheck(instance != NULL);
|
254 |
|
255 |
// local variables
|
256 |
AosShellChannel* channel = ((AosShellStream*)instance)->channel; |
257 |
size_t bytes; |
258 |
size_t maxbytes = 0;
|
259 |
|
260 |
// iterate through the list of channels
|
261 |
while (channel != NULL) { |
262 |
bytes = streamWrite(channel, bp, n); |
263 |
maxbytes = (bytes > maxbytes) ? bytes : maxbytes; |
264 |
channel = channel->next; |
265 |
} |
266 |
|
267 |
return maxbytes;
|
268 |
} |
269 |
|
270 |
static size_t _stremread(void *instance, uint8_t *bp, size_t n) |
271 |
{ |
272 |
(void)instance;
|
273 |
(void)bp;
|
274 |
(void)n;
|
275 |
|
276 |
return 0; |
277 |
} |
278 |
|
279 |
static msg_t _streamput(void *instance, uint8_t b) |
280 |
{ |
281 |
aosDbgCheck(instance != NULL);
|
282 |
|
283 |
// local variables
|
284 |
AosShellChannel* channel = ((AosShellStream*)instance)->channel; |
285 |
msg_t ret = MSG_OK; |
286 |
|
287 |
// iterate through the list of channels
|
288 |
while (channel != NULL) { |
289 |
msg_t ret_ = streamPut(channel, b); |
290 |
ret = (ret_ < ret) ? ret_ : ret; |
291 |
channel = channel->next; |
292 |
} |
293 |
|
294 |
return ret;
|
295 |
} |
296 |
|
297 |
static msg_t _streamget(void *instance) |
298 |
{ |
299 |
(void)instance;
|
300 |
|
301 |
return 0; |
302 |
} |
303 |
|
304 |
/**
|
305 |
* @brief Print the shell prompt
|
306 |
* @details Depending on the configuration flags, the system uptime is printed before the prompt string.
|
307 |
*
|
308 |
* @param[in] shell Pointer to the shell object.
|
309 |
*/
|
310 |
static void _printPrompt(aos_shell_t* shell) |
311 |
{ |
312 |
aosDbgCheck(shell != NULL);
|
313 |
|
314 |
// print some time informattion before prompt if configured
|
315 |
if (shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) {
|
316 |
// printf the system uptime
|
317 |
if ((shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) == AOS_SHELL_CONFIG_PROMPT_UPTIME) {
|
318 |
// get current system uptime
|
319 |
aos_timestamp_t uptime; |
320 |
aosSysGetUptime(&uptime); |
321 |
|
322 |
chprintf((BaseSequentialStream*)&shell->stream, "[%01u:%02u:%02u:%02u:%03u:%03u] ",
|
323 |
(uint32_t)(uptime / MICROSECONDS_PER_DAY), |
324 |
(uint8_t)(uptime % MICROSECONDS_PER_DAY / MICROSECONDS_PER_HOUR), |
325 |
(uint8_t)(uptime % MICROSECONDS_PER_HOUR / MICROSECONDS_PER_MINUTE), |
326 |
(uint8_t)(uptime % MICROSECONDS_PER_MINUTE / MICROSECONDS_PER_SECOND), |
327 |
(uint16_t)(uptime % MICROSECONDS_PER_SECOND / MICROSECONDS_PER_MILLISECOND), |
328 |
(uint16_t)(uptime % MICROSECONDS_PER_MILLISECOND / MICROSECONDS_PER_MICROSECOND)); |
329 |
} |
330 |
#if (HAL_USE_RTC == TRUE)
|
331 |
else if ((shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) == AOS_SHELL_CONFIG_PROMPT_DATETIME) { |
332 |
// get current RTC time
|
333 |
struct tm dt;
|
334 |
aosSysGetDateTime(&dt); |
335 |
chprintf((BaseSequentialStream*)&shell->stream, "[%02u-%02u-%04u|%02u:%02u:%02u] ",
|
336 |
dt.tm_mday, |
337 |
dt.tm_mon + 1,
|
338 |
dt.tm_year + 1900,
|
339 |
dt.tm_hour, |
340 |
dt.tm_min, |
341 |
dt.tm_sec); |
342 |
} |
343 |
#endif /* (HAL_USE_RTC == TRUE) */ |
344 |
else {
|
345 |
aosDbgAssert(false);
|
346 |
} |
347 |
} |
348 |
|
349 |
// print the actual prompt string
|
350 |
if (shell->prompt && !(shell->config & AOS_SHELL_CONFIG_PROMPT_MINIMAL)) {
|
351 |
chprintf((BaseSequentialStream*)&shell->stream, "%s$ ", shell->prompt);
|
352 |
} else {
|
353 |
chprintf((BaseSequentialStream*)&shell->stream, "%>$ ");
|
354 |
} |
355 |
|
356 |
return;
|
357 |
} |
358 |
|
359 |
/**
|
360 |
* @brief Interprete a escape sequence
|
361 |
* @details This function interpretes escape sequences (starting with ASCII
|
362 |
* "Escape" character 0x1B) according to the VT100 / VT52 ANSI escape
|
363 |
* sequence definitions.
|
364 |
* @note Only the most important escape sequences are implemented yet.
|
365 |
*
|
366 |
* @param[in] seq Character sequence to interprete.
|
367 |
* Must be terminated by NUL byte.
|
368 |
*
|
369 |
* @return A @p special_key value.
|
370 |
*/
|
371 |
static special_key_t _interpreteEscapeSequence(const char seq[]) |
372 |
{ |
373 |
// local variables
|
374 |
char str[AOS_SHELL_ESCSEQUENCE_LENGTH];
|
375 |
unsigned long strl = 0; |
376 |
const unsigned long seql = strlen(seq); |
377 |
bool ambiguous = false; |
378 |
|
379 |
// TAB
|
380 |
/* not supported yet; use "\x09" instead */
|
381 |
|
382 |
// BACKSPACE
|
383 |
/* not supported yet; use "\x08" instead */
|
384 |
|
385 |
// ESCAPE
|
386 |
strncpy(str, "\x1B", AOS_SHELL_ESCSEQUENCE_LENGTH);
|
387 |
strl = strlen(str); |
388 |
if (seql == strl && strncmp(seq, str, seql) == 0) { |
389 |
return KEY_ESCAPE;
|
390 |
} else if(seql < strl && strncmp(seq, str, seql) == 0) { |
391 |
ambiguous = true;
|
392 |
} |
393 |
|
394 |
// INSERT
|
395 |
strncpy(str, "\x1B\x5B\x32\x7E", AOS_SHELL_ESCSEQUENCE_LENGTH);
|
396 |
strl = strlen(str); |
397 |
if (seql == strl && strncmp(seq, str, seql) == 0) { |
398 |
return KEY_INSERT;
|
399 |
} else if(seql < strl && strncmp(seq, str, seql) == 0) { |
400 |
ambiguous = true;
|
401 |
} |
402 |
|
403 |
// DELETE
|
404 |
strncpy(str, "\x1B\x5B\x33\x7E", AOS_SHELL_ESCSEQUENCE_LENGTH);
|
405 |
strl = strlen(str); |
406 |
if (seql == strl && strncmp(seq, str, seql) == 0) { |
407 |
return KEY_DELETE;
|
408 |
} else if(seql < strl && strncmp(seq, str, seql) == 0) { |
409 |
ambiguous = true;
|
410 |
} |
411 |
|
412 |
// HOME
|
413 |
strncpy(str, "\x1B\x5B\x48", AOS_SHELL_ESCSEQUENCE_LENGTH);
|
414 |
strl = strlen(str); |
415 |
if (seql == strl && strncmp(seq, str, seql) == 0) { |
416 |
return KEY_HOME;
|
417 |
} else if(seql < strl && strncmp(seq, str, seql) == 0) { |
418 |
ambiguous = true;
|
419 |
} |
420 |
|
421 |
// END
|
422 |
strncpy(str, "\x1B\x5B\x46", AOS_SHELL_ESCSEQUENCE_LENGTH);
|
423 |
strl = strlen(str); |
424 |
if (seql == strl && strncmp(seq, str, seql) == 0) { |
425 |
return KEY_END;
|
426 |
} else if(seql < strl && strncmp(seq, str, seql) == 0) { |
427 |
ambiguous = true;
|
428 |
} |
429 |
|
430 |
// PAGE UP
|
431 |
strncpy(str, "\x1B\x5B\x35\x7E", AOS_SHELL_ESCSEQUENCE_LENGTH);
|
432 |
strl = strlen(str); |
433 |
if (seql == strl && strncmp(seq, str, seql) == 0) { |
434 |
return KEY_PAGE_UP;
|
435 |
} else if(seql < strl && strncmp(seq, str, seql) == 0) { |
436 |
ambiguous = true;
|
437 |
} |
438 |
|
439 |
// PAGE DOWN
|
440 |
strncpy(str, "\x1B\x5B\x36\x7E", AOS_SHELL_ESCSEQUENCE_LENGTH);
|
441 |
strl = strlen(str); |
442 |
if (seql == strl && strncmp(seq, str, seql) == 0) { |
443 |
return KEY_PAGE_DOWN;
|
444 |
} else if(seql < strl && strncmp(seq, str, seql) == 0) { |
445 |
ambiguous = true;
|
446 |
} |
447 |
|
448 |
// ARROW UP
|
449 |
strncpy(str, "\x1B\x5B\x41", AOS_SHELL_ESCSEQUENCE_LENGTH);
|
450 |
strl = strlen(str); |
451 |
if (seql == strl && strncmp(seq, str, seql) == 0) { |
452 |
return KEY_ARROW_UP;
|
453 |
} else if(seql < strl && strncmp(seq, str, seql) == 0) { |
454 |
ambiguous = true;
|
455 |
} |
456 |
|
457 |
// ARROW DOWN
|
458 |
strncpy(str, "\x1B\x5B\x42", AOS_SHELL_ESCSEQUENCE_LENGTH);
|
459 |
strl = strlen(str); |
460 |
if (seql == strl && strncmp(seq, str, seql) == 0) { |
461 |
return KEY_ARROW_DOWN;
|
462 |
} else if (seql < strl && strncmp(seq, str, seql) == 0) { |
463 |
ambiguous = true;
|
464 |
} |
465 |
|
466 |
// ARROW LEFT
|
467 |
strncpy(str, "\x1B\x5B\x44", AOS_SHELL_ESCSEQUENCE_LENGTH);
|
468 |
strl = strlen(str); |
469 |
if (seql == strl && strncmp(seq, str, seql) == 0) { |
470 |
return KEY_ARROW_LEFT;
|
471 |
} else if (seql < strl && strncmp(seq, str, seql) == 0) { |
472 |
ambiguous = true;
|
473 |
} |
474 |
|
475 |
// ARROW RIGHT
|
476 |
strncpy(str, "\x1B\x5B\x43", AOS_SHELL_ESCSEQUENCE_LENGTH);
|
477 |
strl = strlen(str); |
478 |
if (seql == strl && strncmp(seq, str, seql) == 0) { |
479 |
return KEY_ARROW_RIGHT;
|
480 |
} else if (seql < strl && strncmp(seq, str, seql) == 0) { |
481 |
ambiguous = true;
|
482 |
} |
483 |
|
484 |
// CTRL + ARROW UP
|
485 |
strncpy(str, "\x1B\x5B\x31\x3B\x35\x41", AOS_SHELL_ESCSEQUENCE_LENGTH);
|
486 |
strl = strlen(str); |
487 |
if (seql == strl && strncmp(seq, str, seql) == 0) { |
488 |
return KEY_CTRL_ARROW_UP;
|
489 |
} else if(seql < strl && strncmp(seq, str, seql) == 0) { |
490 |
ambiguous = true;
|
491 |
} |
492 |
|
493 |
// CTRL + ARROW DOWN
|
494 |
strncpy(str, "\x1B\x5B\x31\x3B\x35\x42", AOS_SHELL_ESCSEQUENCE_LENGTH);
|
495 |
strl = strlen(str); |
496 |
if (seql == strl && strncmp(seq, str, seql) == 0) { |
497 |
return KEY_CTRL_ARROW_DOWN;
|
498 |
} else if(seql < strl && strncmp(seq, str, seql) == 0) { |
499 |
ambiguous = true;
|
500 |
} |
501 |
|
502 |
// CTRL + ARROW LEFT
|
503 |
strncpy(str, "\x1B\x5B\x31\x3B\x35\x44", AOS_SHELL_ESCSEQUENCE_LENGTH);
|
504 |
strl = strlen(str); |
505 |
if (seql == strl && strncmp(seq, str, seql) == 0) { |
506 |
return KEY_CTRL_ARROW_LEFT;
|
507 |
} else if(seql < strl && strncmp(seq, str, seql) == 0) { |
508 |
ambiguous = true;
|
509 |
} |
510 |
|
511 |
// CTRL + ARROW RIGHT
|
512 |
strncpy(str, "\x1B\x5B\x31\x3B\x35\x43", AOS_SHELL_ESCSEQUENCE_LENGTH);
|
513 |
strl = strlen(str); |
514 |
if (seql == strl && strncmp(seq, str, seql) == 0) { |
515 |
return KEY_CTRL_ARROW_RIGHT;
|
516 |
} else if(seql < strl && strncmp(seq, str, seql) == 0) { |
517 |
ambiguous = true;
|
518 |
} |
519 |
|
520 |
return ambiguous ? KEY_AMBIGUOUS : KEY_UNKNOWN;
|
521 |
} |
522 |
|
523 |
/**
|
524 |
* @brief Move the cursor in the terminal
|
525 |
*
|
526 |
* @param[in] shell Pointer to the shell object.
|
527 |
* @param[in] from Starting position of the cursor.
|
528 |
* @param[in] to Target position to move the cursor to.
|
529 |
*
|
530 |
* @return The number of positions moved.
|
531 |
*/
|
532 |
static int _moveCursor(aos_shell_t* shell, const size_t from, const size_t to) |
533 |
{ |
534 |
aosDbgCheck(shell != NULL);
|
535 |
|
536 |
// local variables
|
537 |
size_t pos = from; |
538 |
|
539 |
// move cursor left by printing backspaces
|
540 |
while (pos > to) {
|
541 |
streamPut(&shell->stream, '\b');
|
542 |
--pos; |
543 |
} |
544 |
|
545 |
// move cursor right by printing line content
|
546 |
while (pos < to) {
|
547 |
streamPut(&shell->stream, (uint8_t)shell->input.line[pos]); |
548 |
++pos; |
549 |
} |
550 |
|
551 |
return (int)pos - (int)from; |
552 |
} |
553 |
|
554 |
/**
|
555 |
* @brief Print content of the shell line
|
556 |
*
|
557 |
* @param[in] shell Pointer to the shell object.
|
558 |
* @param[in] from First position to start printing from.
|
559 |
* @param[in] to Position after the last character to print.
|
560 |
*
|
561 |
* @return Number of characters printed.
|
562 |
*/
|
563 |
static inline size_t _printLine(aos_shell_t* shell, const size_t from, const size_t to) |
564 |
{ |
565 |
aosDbgCheck(shell != NULL);
|
566 |
|
567 |
// local variables
|
568 |
size_t cnt; |
569 |
|
570 |
for (cnt = 0; from + cnt < to; ++cnt) { |
571 |
streamPut(&shell->stream, (uint8_t)shell->input.line[from + cnt]); |
572 |
} |
573 |
|
574 |
return cnt;
|
575 |
} |
576 |
|
577 |
static int _readChar(aos_shell_t* shell, const char c) { |
578 |
aosDbgCheck(shell != NULL);
|
579 |
|
580 |
// check whether input line is already full
|
581 |
if (shell->inputdata.lineend + 1 >= shell->input.length) { |
582 |
return 0; |
583 |
} else {
|
584 |
// clear old line content on first input
|
585 |
if (shell->inputdata.noinput) {
|
586 |
memset(shell->input.line, '\0', shell->input.length);
|
587 |
shell->inputdata.noinput = false;
|
588 |
} |
589 |
// overwrite content
|
590 |
if (shell->config & AOS_SHELL_CONFIG_INPUT_OVERWRITE) {
|
591 |
shell->input.line[shell->inputdata.cursorpos] = c; |
592 |
++shell->inputdata.cursorpos; |
593 |
shell->inputdata.lineend = (shell->inputdata.cursorpos > shell->inputdata.lineend) ? shell->inputdata.cursorpos : shell->inputdata.lineend; |
594 |
streamPut(&shell->stream, (uint8_t)c); |
595 |
} |
596 |
// insert character
|
597 |
else {
|
598 |
memmove(&(shell->input.line[shell->inputdata.cursorpos+1]), &(shell->input.line[shell->inputdata.cursorpos]), shell->inputdata.lineend - shell->inputdata.cursorpos);
|
599 |
shell->input.line[shell->inputdata.cursorpos] = c; |
600 |
++shell->inputdata.lineend; |
601 |
_printLine(shell, shell->inputdata.cursorpos, shell->inputdata.lineend); |
602 |
++shell->inputdata.cursorpos; |
603 |
_moveCursor(shell, shell->inputdata.lineend, shell->inputdata.cursorpos); |
604 |
} |
605 |
return 1; |
606 |
} |
607 |
} |
608 |
|
609 |
/**
|
610 |
* @brief Compare two characters.
|
611 |
*
|
612 |
* @param[in] lhs First character to compare.
|
613 |
* @param[in] rhs Second character to compare.
|
614 |
*
|
615 |
* @return How well the characters match.
|
616 |
*/
|
617 |
static inline charmatch_t _charcmp(char lhs, char rhs) |
618 |
{ |
619 |
// if lhs is a upper case letter and rhs is a lower case letter
|
620 |
if (lhs >= 'A' && lhs <= 'Z' && rhs >= 'a' && rhs <= 'z') { |
621 |
return (lhs == (rhs - 'a' + 'A')) ? CHAR_MATCH_NCASE : CHAR_MATCH_NOT; |
622 |
} |
623 |
// if lhs is a lower case letter and rhs is a upper case letter
|
624 |
else if (lhs >= 'a' && lhs <= 'z' && rhs >= 'A' && rhs <= 'Z') { |
625 |
return ((lhs - 'a' + 'A') == rhs) ? CHAR_MATCH_NCASE : CHAR_MATCH_NOT; |
626 |
} |
627 |
// default
|
628 |
else {
|
629 |
return (lhs == rhs) ? CHAR_MATCH_CASE : CHAR_MATCH_NOT;
|
630 |
} |
631 |
} |
632 |
|
633 |
/**
|
634 |
* @brief Maps an character from ASCII to a modified custom encoding.
|
635 |
* @details The custom character encoding is very similar to ASCII and has the following structure:
|
636 |
* 0x00=NULL ... 0x40='@' (identically to ASCII)
|
637 |
* 0x4A='a'; 0x4B='A'; 0x4C='b'; 0x4D='B' ... 0x73='z'; 0x74='Z' (custom letter order)
|
638 |
* 0x75='[' ... 0x7A='`' (0x5B..0x60 is ASCII)
|
639 |
* 0x7B='{' ... 0x7F=DEL (identically to ASCII)
|
640 |
*
|
641 |
* @param[in] c Character to map to the custom encoding.
|
642 |
*
|
643 |
* @return The customly encoded character.
|
644 |
*/
|
645 |
static inline char _mapAscii2Custom(const char c) |
646 |
{ |
647 |
if (c >= 'A' && c <= 'Z') { |
648 |
return ((c - 'A') * 2) + 'A' + 1; |
649 |
} else if (c > 'Z' && c < 'a') { |
650 |
return c + ('z' - 'a') + 1; |
651 |
} else if (c >= 'a' && c <= 'z') { |
652 |
return ((c - 'a') * 2) + 'A'; |
653 |
} else {
|
654 |
return c;
|
655 |
} |
656 |
} |
657 |
|
658 |
/**
|
659 |
* @brief Compares two strings wrt letter case.
|
660 |
* @details Comparisson uses a custom character encoding or mapping.
|
661 |
* See @p _mapAscii2Custom for details.
|
662 |
*
|
663 |
* @param[in] str1 First string to compare.
|
664 |
* @param[in] str2 Second string to compare.
|
665 |
* @param[in] cs Flag indicating whether comparison shall be case sensitive.
|
666 |
* @param[in,out] n Maximum number of character to compare (in) and number of matching characters (out).
|
667 |
* If a null pointer is specified, this parameter is ignored.
|
668 |
* If the value pointed to is zero, comarison will not be limited.
|
669 |
* @param[out] m Optional indicator whether there was at least one case mismatch.
|
670 |
*
|
671 |
* @return Integer value indicating the relationship between the strings.
|
672 |
* @retval <0 The first character that does not match has a lower value in str1 than in str2.
|
673 |
* @retval 0 The contents of both strings are equal.
|
674 |
* @retval >0 The first character that does not match has a greater value in str1 than in str2.
|
675 |
*/
|
676 |
static int _strccmp(const char *str1, const char *str2, bool cs, size_t* n, charmatch_t* m) |
677 |
{ |
678 |
aosDbgCheck(str1 != NULL);
|
679 |
aosDbgCheck(str2 != NULL);
|
680 |
|
681 |
// initialize variables
|
682 |
if (m) {
|
683 |
*m = CHAR_MATCH_NOT; |
684 |
} |
685 |
size_t i = 0;
|
686 |
|
687 |
// iterate through the strings
|
688 |
while ((n == NULL) || (*n == 0) || (*n > 0 && i < *n)) { |
689 |
// break on NUL
|
690 |
if (str1[i] == '\0' || str2[i] == '\0') { |
691 |
if (n) {
|
692 |
*n = i; |
693 |
} |
694 |
break;
|
695 |
} |
696 |
// compare character
|
697 |
const charmatch_t match = _charcmp(str1[i], str2[i]);
|
698 |
if ((match == CHAR_MATCH_CASE) || (!cs && match == CHAR_MATCH_NCASE)) {
|
699 |
if (m != NULL && *m != CHAR_MATCH_NCASE) { |
700 |
*m = match; |
701 |
} |
702 |
++i; |
703 |
} else {
|
704 |
if (n) {
|
705 |
*n = i; |
706 |
} |
707 |
break;
|
708 |
} |
709 |
} |
710 |
|
711 |
return _mapAscii2Custom(str1[i]) - _mapAscii2Custom(str2[i]);
|
712 |
} |
713 |
|
714 |
/**
|
715 |
* @brief Read input from a channel as long as there is data available.
|
716 |
*
|
717 |
* @param[in] shell Pointer to the shell object.
|
718 |
* @param[in] channel The channel to read from.
|
719 |
* @param[out] n Optional pointer to a variable to store the number of read characters to.
|
720 |
*
|
721 |
* @return Indicator, whether the read character(s) indicated, that a command shall be executed.
|
722 |
*/
|
723 |
static bool _readChannel(aos_shell_t* shell, AosShellChannel* channel, size_t* n) |
724 |
{ |
725 |
aosDbgCheck(shell != NULL);
|
726 |
aosDbgCheck(channel != NULL);
|
727 |
|
728 |
// local variables
|
729 |
aos_shellaction_t action = AOS_SHELL_ACTION_NONE; |
730 |
char c;
|
731 |
special_key_t key; |
732 |
|
733 |
// initialize output variables
|
734 |
if (n) {
|
735 |
*n = 0;
|
736 |
} |
737 |
|
738 |
// read character by character from the channel
|
739 |
while (chnReadTimeout(channel, (uint8_t*)&c, 1, TIME_IMMEDIATE)) { |
740 |
key = KEY_UNKNOWN; |
741 |
|
742 |
// parse escape sequence
|
743 |
if (strlen(shell->inputdata.escseq) > 0) { |
744 |
shell->inputdata.escseq[strlen(shell->inputdata.escseq)] = c; |
745 |
key = _interpreteEscapeSequence(shell->inputdata.escseq); |
746 |
switch (key) {
|
747 |
case KEY_AMBIGUOUS:
|
748 |
// read next byte to resolve ambiguity
|
749 |
continue;
|
750 |
case KEY_UNKNOWN:
|
751 |
// do nothing here, but handle the unknown sequence below
|
752 |
break;
|
753 |
default:
|
754 |
// reset the sequence variable and buffer
|
755 |
memset(shell->inputdata.escseq, '\0', sizeof(shell->inputdata.escseq)*sizeof(shell->inputdata.escseq[0])); |
756 |
break;
|
757 |
} |
758 |
} |
759 |
|
760 |
/* interprete keys or character */
|
761 |
{ |
762 |
// default
|
763 |
action = AOS_SHELL_ACTION_NONE; |
764 |
|
765 |
// printable character
|
766 |
if (key == KEY_UNKNOWN && strlen(shell->inputdata.escseq) == 0 && c >= '\x20' && c <= '\x7E') { |
767 |
action = AOS_SHELL_ACTION_READCHAR; |
768 |
} |
769 |
|
770 |
// tab key or character
|
771 |
else if (key == KEY_TAB || c == '\x09') { |
772 |
/*
|
773 |
* pressing tab once applies auto fill
|
774 |
* pressing tab a second time prints suggestions
|
775 |
*/
|
776 |
if (shell->inputdata.lastaction == AOS_SHELL_ACTION_AUTOFILL || shell->inputdata.lastaction == AOS_SHELL_ACTION_SUGGEST) {
|
777 |
action = AOS_SHELL_ACTION_SUGGEST; |
778 |
} else {
|
779 |
action = AOS_SHELL_ACTION_AUTOFILL; |
780 |
} |
781 |
} |
782 |
|
783 |
// INS key
|
784 |
else if (key == KEY_INSERT) { |
785 |
action = AOS_SHELL_ACTION_INSERTTOGGLE; |
786 |
} |
787 |
|
788 |
// DEL key or character
|
789 |
else if (key == KEY_DELETE || c == '\x7F') { |
790 |
// ignore if cursor is at very right
|
791 |
if (shell->inputdata.cursorpos < shell->inputdata.lineend) {
|
792 |
action = AOS_SHELL_ACTION_DELETEFORWARD; |
793 |
} |
794 |
} |
795 |
|
796 |
// backspace key or character
|
797 |
else if (key == KEY_BACKSPACE || c == '\x08') { |
798 |
// ignore if cursor is at very left
|
799 |
if (shell->inputdata.cursorpos > 0) { |
800 |
action = AOS_SHELL_ACTION_DELETEBACKWARD; |
801 |
} |
802 |
} |
803 |
|
804 |
// 'page up', 'arrow up', or key or CTRL + 'arrow up' key combination
|
805 |
else if (key == KEY_PAGE_UP || key == KEY_ARROW_UP || key == KEY_CTRL_ARROW_UP) { |
806 |
// ignore if there was some input
|
807 |
if (shell->inputdata.noinput) {
|
808 |
action = AOS_SHELL_ACTION_RECALLLAST; |
809 |
} |
810 |
} |
811 |
|
812 |
// 'page down' key, 'arrow down' key, 'end of test' character or 'end of transmission' character, or CTRL + 'arrow down' key combination
|
813 |
else if (key == KEY_PAGE_DOWN || key == KEY_ARROW_DOWN || c == '\x03' || c == '\x03' || key == KEY_CTRL_ARROW_DOWN) { |
814 |
// ignore if line is empty
|
815 |
if (shell->inputdata.lineend > 0) { |
816 |
action = AOS_SHELL_ACTION_CLEAR; |
817 |
} |
818 |
} |
819 |
|
820 |
// 'home' key
|
821 |
else if (key == KEY_HOME) { |
822 |
// ignore if cursor is very left
|
823 |
if (shell->inputdata.cursorpos > 0) { |
824 |
action = AOS_SHELL_ACTION_CURSOR2START; |
825 |
} |
826 |
} |
827 |
|
828 |
// 'end' key
|
829 |
else if (key == KEY_END) { |
830 |
// ignore if cursos is very right
|
831 |
if (shell->inputdata.cursorpos < shell->inputdata.lineend) {
|
832 |
action = AOS_SHELL_ACTION_CURSOR2END; |
833 |
} |
834 |
} |
835 |
|
836 |
// 'arrow left' key
|
837 |
else if (key == KEY_ARROW_LEFT) { |
838 |
// ignore if cursor is very left
|
839 |
if (shell->inputdata.cursorpos > 0) { |
840 |
action = AOS_SHELL_ACTION_CURSORLEFT; |
841 |
} |
842 |
} |
843 |
|
844 |
// 'arrow right' key
|
845 |
else if (key == KEY_ARROW_RIGHT) { |
846 |
// ignore if cursor is very right
|
847 |
if (shell->inputdata.cursorpos < shell->inputdata.lineend) {
|
848 |
action = AOS_SHELL_ACTION_CURSORRIGHT; |
849 |
} |
850 |
} |
851 |
|
852 |
// CTRL + 'arrow left' key combination
|
853 |
else if (key == KEY_CTRL_ARROW_LEFT) { |
854 |
// ignore if cursor is very left
|
855 |
if (shell->inputdata.cursorpos > 0) { |
856 |
action = AOS_SHELL_ACTION_CURSORWORDLEFT; |
857 |
} |
858 |
} |
859 |
|
860 |
// CTRL + 'arrow right' key combination
|
861 |
else if (key == KEY_CTRL_ARROW_RIGHT) { |
862 |
// ignore if cursor is very right
|
863 |
if (shell->inputdata.cursorpos < shell->inputdata.lineend) {
|
864 |
action = AOS_SHELL_ACTION_CURSORWORDRIGHT; |
865 |
} |
866 |
} |
867 |
|
868 |
// carriage return ('\r') or line feed ('\n') character
|
869 |
else if (c == '\x0D' || c == '\x0A') { |
870 |
action = AOS_SHELL_ACTION_EXECUTE; |
871 |
} |
872 |
|
873 |
// ESC key or [ESCAPE] character
|
874 |
else if (key == KEY_ESCAPE || c == '\x1B') { |
875 |
action = AOS_SHELL_ACTION_ESCSTART; |
876 |
} |
877 |
|
878 |
// unknown escape sequence
|
879 |
else if (key == KEY_UNKNOWN && strlen(shell->inputdata.escseq) > 0) { |
880 |
action = AOS_SHELL_ACTION_PRINTUNKNOWNSEQUENCE; |
881 |
} |
882 |
} |
883 |
|
884 |
/* handle function */
|
885 |
switch (action) {
|
886 |
case AOS_SHELL_ACTION_READCHAR:
|
887 |
{ |
888 |
if (_readChar(shell, c) == 0) { |
889 |
// line is full
|
890 |
_moveCursor(shell, shell->inputdata.cursorpos, shell->inputdata.lineend); |
891 |
chprintf((BaseSequentialStream*)&shell->stream, "\n\tmaximum line width reached\n");
|
892 |
_printPrompt(shell); |
893 |
_printLine(shell, 0, shell->inputdata.lineend);
|
894 |
_moveCursor(shell, shell->inputdata.lineend, shell->inputdata.cursorpos); |
895 |
} |
896 |
break;
|
897 |
} |
898 |
|
899 |
case AOS_SHELL_ACTION_AUTOFILL:
|
900 |
{ |
901 |
const char* fill = shell->input.line; |
902 |
size_t cmatch = shell->inputdata.cursorpos; |
903 |
charmatch_t matchlevel = CHAR_MATCH_NOT; |
904 |
size_t n; |
905 |
// iterate through command list
|
906 |
for (aos_shellcommand_t* cmd = shell->commands; cmd != NULL; cmd = cmd->next) { |
907 |
// compare current match with command
|
908 |
n = cmatch; |
909 |
charmatch_t mlvl = CHAR_MATCH_NOT; |
910 |
_strccmp(fill, cmd->name, shell->config & AOS_SHELL_CONFIG_MATCH_CASE, (n == 0) ? NULL : &n, &mlvl); |
911 |
const int cmp = (n < cmatch) ? |
912 |
((int)n - (int)cmatch) : |
913 |
(cmd->name[n] != '\0') ?
|
914 |
(int)strlen(cmd->name) - (int)n : |
915 |
0;
|
916 |
// if an exact match was found
|
917 |
if ((size_t)((int)cmatch + cmp) == shell->inputdata.cursorpos) { |
918 |
cmatch = shell->inputdata.cursorpos; |
919 |
fill = cmd->name; |
920 |
// break the loop only if there are no case mismatches with the input
|
921 |
n = shell->inputdata.cursorpos; |
922 |
_strccmp(fill, shell->input.line, false, &n, &mlvl);
|
923 |
if (mlvl == CHAR_MATCH_CASE) {
|
924 |
break;
|
925 |
} |
926 |
} |
927 |
// if a not exact match was found
|
928 |
else if ((size_t)((int)cmatch + cmp) > shell->inputdata.cursorpos) { |
929 |
// if this is the first one
|
930 |
if (fill == shell->input.line) {
|
931 |
cmatch = (size_t)((int)cmatch + cmp);
|
932 |
fill = cmd->name; |
933 |
} |
934 |
// if this is a worse one
|
935 |
else if ((cmp < 0) || (cmp == 0 && mlvl == CHAR_MATCH_CASE)) { |
936 |
cmatch = (size_t)((int)cmatch + cmp);
|
937 |
} |
938 |
} |
939 |
// non matching commands are ignored
|
940 |
else {}
|
941 |
} |
942 |
// evaluate if there are case mismatches
|
943 |
n = cmatch; |
944 |
_strccmp(shell->input.line, fill, shell->config & AOS_SHELL_CONFIG_MATCH_CASE, &n, &matchlevel); |
945 |
// print the auto fill if any
|
946 |
if (cmatch > shell->inputdata.cursorpos || (cmatch == shell->inputdata.cursorpos && matchlevel == CHAR_MATCH_NCASE)) {
|
947 |
shell->inputdata.noinput = false;
|
948 |
// limit auto fill so it will not overflow the line width
|
949 |
if (shell->inputdata.lineend + (cmatch - shell->inputdata.cursorpos) > shell->input.length) {
|
950 |
cmatch = shell->input.length - shell->inputdata.lineend + shell->inputdata.cursorpos; |
951 |
} |
952 |
// move trailing memory further in the line
|
953 |
memmove(&(shell->input.line[cmatch]), &(shell->input.line[shell->inputdata.cursorpos]), shell->inputdata.lineend - shell->inputdata.cursorpos); |
954 |
shell->inputdata.lineend += cmatch - shell->inputdata.cursorpos; |
955 |
// if there was no incorrect case when matching
|
956 |
if (matchlevel == CHAR_MATCH_CASE) {
|
957 |
// insert fill command name to line
|
958 |
memcpy(&(shell->input.line[shell->inputdata.cursorpos]), &(fill[shell->inputdata.cursorpos]), cmatch - shell->inputdata.cursorpos); |
959 |
// print the output
|
960 |
_printLine(shell, shell->inputdata.cursorpos, shell->inputdata.lineend); |
961 |
} else {
|
962 |
// overwrite line with fill command name
|
963 |
memcpy(shell->input.line, fill, cmatch); |
964 |
// reprint the whole line
|
965 |
_moveCursor(shell, shell->inputdata.cursorpos, 0);
|
966 |
_printLine(shell, 0, shell->inputdata.lineend);
|
967 |
} |
968 |
// move cursor to the end of the matching sequence
|
969 |
shell->inputdata.cursorpos = cmatch; |
970 |
_moveCursor(shell, shell->inputdata.lineend, shell->inputdata.cursorpos); |
971 |
} |
972 |
break;
|
973 |
} |
974 |
|
975 |
case AOS_SHELL_ACTION_SUGGEST:
|
976 |
{ |
977 |
unsigned int matches = 0; |
978 |
// iterate through command list
|
979 |
for (aos_shellcommand_t* cmd = shell->commands; cmd != NULL; cmd = cmd->next) { |
980 |
// compare line content with command, excpet if cursorpos=0
|
981 |
size_t i = shell->inputdata.cursorpos; |
982 |
if (shell->inputdata.cursorpos > 0) { |
983 |
_strccmp(shell->input.line, cmd->name, true, &i, NULL); |
984 |
} |
985 |
const int cmp = (i < shell->inputdata.cursorpos) ? |
986 |
(int)(i - shell->inputdata.cursorpos) :
|
987 |
(cmd->name[i] != '\0') ?
|
988 |
(int)strlen(cmd->name) - (int)i : |
989 |
0;
|
990 |
// if a match was found
|
991 |
if (cmp > 0) { |
992 |
// if this is the first one
|
993 |
if (matches == 0) { |
994 |
_moveCursor(shell, shell->inputdata.cursorpos, shell->inputdata.lineend); |
995 |
streamPut(&shell->stream, '\n');
|
996 |
} |
997 |
// print the command
|
998 |
chprintf((BaseSequentialStream*)&shell->stream, "\t%s\n", cmd->name);
|
999 |
++matches; |
1000 |
} |
1001 |
} |
1002 |
// reprint the prompt and line if any matches have been found
|
1003 |
if (matches > 0) { |
1004 |
_printPrompt(shell); |
1005 |
_printLine(shell, 0, shell->inputdata.lineend);
|
1006 |
_moveCursor(shell, shell->inputdata.lineend, shell->inputdata.cursorpos); |
1007 |
shell->inputdata.noinput = false;
|
1008 |
} |
1009 |
break;
|
1010 |
} |
1011 |
|
1012 |
case AOS_SHELL_ACTION_INSERTTOGGLE:
|
1013 |
{ |
1014 |
if (shell->config & AOS_SHELL_CONFIG_INPUT_OVERWRITE) {
|
1015 |
shell->config &= ~AOS_SHELL_CONFIG_INPUT_OVERWRITE; |
1016 |
} else {
|
1017 |
shell->config |= AOS_SHELL_CONFIG_INPUT_OVERWRITE; |
1018 |
} |
1019 |
break;
|
1020 |
} |
1021 |
|
1022 |
case AOS_SHELL_ACTION_DELETEFORWARD:
|
1023 |
{ |
1024 |
--shell->inputdata.lineend; |
1025 |
memmove(&(shell->input.line[shell->inputdata.cursorpos]), &(shell->input.line[shell->inputdata.cursorpos+1]), shell->inputdata.lineend - shell->inputdata.cursorpos);
|
1026 |
_printLine(shell, shell->inputdata.cursorpos, shell->inputdata.lineend); |
1027 |
streamPut(&shell->stream, ' ');
|
1028 |
_moveCursor(shell, shell->inputdata.lineend + 1, shell->inputdata.cursorpos);
|
1029 |
break;
|
1030 |
} |
1031 |
|
1032 |
case AOS_SHELL_ACTION_DELETEBACKWARD:
|
1033 |
{ |
1034 |
--shell->inputdata.cursorpos; |
1035 |
memmove(&(shell->input.line[shell->inputdata.cursorpos]), &(shell->input.line[shell->inputdata.cursorpos+1]), shell->inputdata.lineend - shell->inputdata.cursorpos);
|
1036 |
--shell->inputdata.lineend; |
1037 |
shell->input.line[shell->inputdata.lineend] = '\0';
|
1038 |
_moveCursor(shell, shell->inputdata.cursorpos + 1, shell->inputdata.cursorpos);
|
1039 |
_printLine(shell, shell->inputdata.cursorpos, shell->inputdata.lineend); |
1040 |
streamPut(&shell->stream, ' ');
|
1041 |
_moveCursor(shell, shell->inputdata.lineend+1, shell->inputdata.cursorpos);
|
1042 |
break;
|
1043 |
} |
1044 |
|
1045 |
case AOS_SHELL_ACTION_RECALLLAST:
|
1046 |
{ |
1047 |
// replace any intermediate NUL bytes with spaces
|
1048 |
shell->inputdata.lineend = 0;
|
1049 |
size_t nul_start = 0;
|
1050 |
size_t nul_end = 0;
|
1051 |
// search line for a NUL byte
|
1052 |
while (nul_start < shell->input.length) {
|
1053 |
if (shell->input.line[nul_start] == '\0') { |
1054 |
nul_end = nul_start + 1;
|
1055 |
// keep searcjing for a byte that is not NUL
|
1056 |
while (nul_end < shell->input.length) {
|
1057 |
if (shell->input.line[nul_end] != '\0') { |
1058 |
// an intermediate NUL sequence was found
|
1059 |
memset(&(shell->input.line[nul_start]), ' ', nul_end - nul_start);
|
1060 |
shell->inputdata.lineend = nul_end + 1;
|
1061 |
break;
|
1062 |
} else {
|
1063 |
++nul_end; |
1064 |
} |
1065 |
} |
1066 |
nul_start = nul_end + 1;
|
1067 |
} else {
|
1068 |
++shell->inputdata.lineend; |
1069 |
++nul_start; |
1070 |
} |
1071 |
} |
1072 |
shell->inputdata.cursorpos = shell->inputdata.lineend; |
1073 |
// print the line
|
1074 |
shell->inputdata.noinput = _printLine(shell, 0, shell->inputdata.lineend) == 0; |
1075 |
break;
|
1076 |
} |
1077 |
|
1078 |
case AOS_SHELL_ACTION_CLEAR:
|
1079 |
{ |
1080 |
// clear output
|
1081 |
_moveCursor(shell, shell->inputdata.cursorpos, 0);
|
1082 |
for (shell->inputdata.cursorpos = 0; shell->inputdata.cursorpos < shell->inputdata.lineend; ++shell->inputdata.cursorpos) { |
1083 |
streamPut(&shell->stream, ' ');
|
1084 |
} |
1085 |
_moveCursor(shell, shell->inputdata.lineend, 0);
|
1086 |
shell->inputdata.cursorpos = 0;
|
1087 |
shell->inputdata.lineend = 0;
|
1088 |
shell->inputdata.noinput = true;
|
1089 |
break;
|
1090 |
} |
1091 |
|
1092 |
case AOS_SHELL_ACTION_CURSOR2START:
|
1093 |
{ |
1094 |
_moveCursor(shell, shell->inputdata.cursorpos, 0);
|
1095 |
shell->inputdata.cursorpos = 0;
|
1096 |
break;
|
1097 |
} |
1098 |
|
1099 |
case AOS_SHELL_ACTION_CURSOR2END:
|
1100 |
{ |
1101 |
_moveCursor(shell, shell->inputdata.cursorpos, shell->inputdata.lineend); |
1102 |
shell->inputdata.cursorpos = shell->inputdata.lineend; |
1103 |
break;
|
1104 |
} |
1105 |
|
1106 |
case AOS_SHELL_ACTION_CURSORLEFT:
|
1107 |
{ |
1108 |
_moveCursor(shell, shell->inputdata.cursorpos, shell->inputdata.cursorpos-1);
|
1109 |
--shell->inputdata.cursorpos; |
1110 |
break;
|
1111 |
} |
1112 |
|
1113 |
case AOS_SHELL_ACTION_CURSORRIGHT:
|
1114 |
{ |
1115 |
_moveCursor(shell, shell->inputdata.cursorpos, shell->inputdata.cursorpos+1);
|
1116 |
++shell->inputdata.cursorpos; |
1117 |
break;
|
1118 |
} |
1119 |
|
1120 |
case AOS_SHELL_ACTION_CURSORWORDLEFT:
|
1121 |
{ |
1122 |
size_t cpos = shell->inputdata.cursorpos; |
1123 |
while (cpos > 0 && shell->input.line[cpos-1] == ' ') { |
1124 |
--cpos; |
1125 |
} |
1126 |
while (cpos > 0 && shell->input.line[cpos-1] != ' ') { |
1127 |
--cpos; |
1128 |
} |
1129 |
_moveCursor(shell, shell->inputdata.cursorpos, cpos); |
1130 |
shell->inputdata.cursorpos = cpos; |
1131 |
break;
|
1132 |
} |
1133 |
|
1134 |
case AOS_SHELL_ACTION_CURSORWORDRIGHT:
|
1135 |
{ |
1136 |
size_t cpos = shell->inputdata.cursorpos; |
1137 |
while (cpos < shell->inputdata.lineend && shell->input.line[cpos] != ' ') { |
1138 |
++cpos; |
1139 |
} |
1140 |
while (cpos < shell->inputdata.lineend && shell->input.line[cpos] == ' ') { |
1141 |
++cpos; |
1142 |
} |
1143 |
_moveCursor(shell, shell->inputdata.cursorpos, cpos); |
1144 |
shell->inputdata.cursorpos = cpos; |
1145 |
break;
|
1146 |
} |
1147 |
|
1148 |
case AOS_SHELL_ACTION_EXECUTE:
|
1149 |
{ |
1150 |
streamPut(&shell->stream, '\n');
|
1151 |
// set the number of read bytes and return
|
1152 |
if (!shell->inputdata.noinput) {
|
1153 |
if (n) {
|
1154 |
*n = shell->input.length - shell->inputdata.lineend; |
1155 |
} |
1156 |
// fill the remainder of the line with NUL bytes
|
1157 |
memset(&(shell->input.line[shell->inputdata.lineend]), '\0', (shell->input.length - shell->inputdata.lineend));
|
1158 |
} |
1159 |
return true; |
1160 |
} |
1161 |
|
1162 |
case AOS_SHELL_ACTION_ESCSTART:
|
1163 |
{ |
1164 |
shell->inputdata.escseq[0] = c;
|
1165 |
break;
|
1166 |
} |
1167 |
|
1168 |
case AOS_SHELL_ACTION_PRINTUNKNOWNSEQUENCE:
|
1169 |
{ |
1170 |
size_t seqc = 1; // element 0 would be unprintible ESC character |
1171 |
while (shell->inputdata.escseq[seqc] != '\0') { |
1172 |
_readChar(shell, shell->inputdata.escseq[seqc]); |
1173 |
++seqc; |
1174 |
} |
1175 |
memset(shell->inputdata.escseq, '\0', sizeof(shell->inputdata.escseq)*sizeof(shell->inputdata.escseq[0])); |
1176 |
break;
|
1177 |
} |
1178 |
|
1179 |
case AOS_SHELL_ACTION_NONE:
|
1180 |
{ |
1181 |
// do nothing (ignore input) and read next byte
|
1182 |
continue;
|
1183 |
} |
1184 |
} /* end of switch */
|
1185 |
|
1186 |
shell->inputdata.lastaction = action; |
1187 |
} /* end of while */
|
1188 |
|
1189 |
// no more data could be read from the channel
|
1190 |
return false; |
1191 |
} |
1192 |
|
1193 |
/**
|
1194 |
* @brief Parses the content of the input buffer (line) to separate arguments.
|
1195 |
*
|
1196 |
* @param[in] shell Pointer to the shell object.
|
1197 |
* @param[out] argbuf Buffer to store argument pointers to.
|
1198 |
*
|
1199 |
* @return Number of arguments found.
|
1200 |
*/
|
1201 |
static size_t _parseArguments(aos_shell_t* shell, char** argbuf) |
1202 |
{ |
1203 |
aosDbgCheck(shell != NULL);
|
1204 |
aosDbgCheck(argbuf != NULL);
|
1205 |
|
1206 |
/*
|
1207 |
* States for a very small FSM.
|
1208 |
*/
|
1209 |
typedef enum { |
1210 |
START, |
1211 |
SPACE, |
1212 |
TEXT, |
1213 |
END, |
1214 |
} state_t; |
1215 |
|
1216 |
// local variables
|
1217 |
state_t state = START; |
1218 |
size_t nargs = 0;
|
1219 |
|
1220 |
// iterate through the line
|
1221 |
for (char* c = shell->input.line; c < shell->input.line + shell->input.length; ++c) { |
1222 |
// terminate at first NUL byte
|
1223 |
if (*c == '\0') { |
1224 |
state = END; |
1225 |
break;
|
1226 |
} |
1227 |
// spaces become NUL bytes
|
1228 |
else if (*c == ' ') { |
1229 |
*c = '\0';
|
1230 |
state = SPACE; |
1231 |
} |
1232 |
// handle non-NUL bytes
|
1233 |
else {
|
1234 |
switch (state) {
|
1235 |
case START:
|
1236 |
case SPACE:
|
1237 |
// ignore too many arguments
|
1238 |
if (nargs < shell->input.nargs) {
|
1239 |
argbuf[nargs] = c; |
1240 |
} |
1241 |
++nargs; |
1242 |
break;
|
1243 |
case TEXT:
|
1244 |
case END:
|
1245 |
break;
|
1246 |
} |
1247 |
state = TEXT; |
1248 |
} |
1249 |
} |
1250 |
|
1251 |
// set all remaining argument pointers to NULL
|
1252 |
for (size_t a = nargs; a < shell->input.nargs; ++a) {
|
1253 |
argbuf[a] = NULL;
|
1254 |
} |
1255 |
|
1256 |
return nargs;
|
1257 |
} |
1258 |
|
1259 |
/******************************************************************************/
|
1260 |
/* EXPORTED FUNCTIONS */
|
1261 |
/******************************************************************************/
|
1262 |
|
1263 |
/**
|
1264 |
* @brief Initializes a shell object with the specified parameters.
|
1265 |
*
|
1266 |
* @param[in] shell Pointer to the shell object.
|
1267 |
* @param[in] stream I/O stream to use.
|
1268 |
* @param[in] prompt Prompt line to print (NULL = use default prompt).
|
1269 |
* @param[in] line Pointer to the input buffer.
|
1270 |
* @param[in] linesize Size of the input buffer.
|
1271 |
* @param[in] numargs Maximum number of arguments (defines size of internal buffer).
|
1272 |
*/
|
1273 |
void aosShellInit(aos_shell_t* shell, event_source_t* oseventsource, const char* prompt, char* line, size_t linesize, size_t numargs) |
1274 |
{ |
1275 |
aosDbgCheck(shell != NULL);
|
1276 |
aosDbgCheck(oseventsource != NULL);
|
1277 |
aosDbgCheck(line != NULL);
|
1278 |
|
1279 |
// set parameters
|
1280 |
shell->thread = NULL;
|
1281 |
chEvtObjectInit(&shell->eventSource); |
1282 |
shell->os.eventSource = oseventsource; |
1283 |
aosShellStreamInit(&shell->stream); |
1284 |
shell->prompt = prompt; |
1285 |
shell->commands = NULL;
|
1286 |
shell->execstatus.command = NULL;
|
1287 |
shell->execstatus.retval = 0;
|
1288 |
shell->input.line = line; |
1289 |
shell->input.length = linesize; |
1290 |
shell->input.nargs= numargs; |
1291 |
shell->inputdata.lastaction = AOS_SHELL_ACTION_NONE; |
1292 |
memset(shell->inputdata.escseq, '\0', sizeof(shell->inputdata.escseq)*sizeof(shell->inputdata.escseq[0])); |
1293 |
shell->inputdata.cursorpos = 0;
|
1294 |
shell->inputdata.lineend = 0;
|
1295 |
shell->inputdata.noinput = true;
|
1296 |
shell->config = 0x00;
|
1297 |
|
1298 |
// initialize buffers
|
1299 |
memset(shell->input.line, '\0', shell->input.length);
|
1300 |
|
1301 |
return;
|
1302 |
} |
1303 |
|
1304 |
/**
|
1305 |
* @brief Initialize an AosShellStream object.
|
1306 |
*
|
1307 |
* @param[in] stream The AosShellStrem to initialize.
|
1308 |
*/
|
1309 |
void aosShellStreamInit(AosShellStream* stream)
|
1310 |
{ |
1311 |
aosDbgCheck(stream != NULL);
|
1312 |
|
1313 |
stream->vmt = &_streamvmt; |
1314 |
stream->channel = NULL;
|
1315 |
|
1316 |
return;
|
1317 |
} |
1318 |
|
1319 |
/**
|
1320 |
* @brief Initialize an AosShellChannel object with the specified parameters.
|
1321 |
*
|
1322 |
* @param[in] channel The AosShellChannel to initialize.
|
1323 |
* @param[in] asyncchannel An BaseAsynchronousChannel this AosShellChannel is associated with.
|
1324 |
*/
|
1325 |
void aosShellChannelInit(AosShellChannel* channel, BaseAsynchronousChannel* asyncchannel)
|
1326 |
{ |
1327 |
aosDbgCheck(channel != NULL);
|
1328 |
aosDbgCheck(asyncchannel != NULL);
|
1329 |
|
1330 |
channel->vmt = &_channelvmt; |
1331 |
channel->asyncchannel = asyncchannel; |
1332 |
channel->listener.wflags = 0;
|
1333 |
channel->next = NULL;
|
1334 |
channel->flags = 0;
|
1335 |
|
1336 |
return;
|
1337 |
} |
1338 |
|
1339 |
/**
|
1340 |
* @brief Inserts a command to the shells list of commands.
|
1341 |
*
|
1342 |
* @param[in] shell Pointer to the shell object.
|
1343 |
* @param[in] cmd Pointer to the command to add.
|
1344 |
*
|
1345 |
* @return A status value.
|
1346 |
* @retval AOS_SUCCESS The command was added successfully.
|
1347 |
* @retval AOS_ERROR Another command with identical name already exists.
|
1348 |
*/
|
1349 |
aos_status_t aosShellAddCommand(aos_shell_t *shell, aos_shellcommand_t *cmd) |
1350 |
{ |
1351 |
aosDbgCheck(shell != NULL);
|
1352 |
aosDbgCheck(cmd != NULL);
|
1353 |
aosDbgCheck(cmd->name != NULL && strlen(cmd->name) > 0 && strchr(cmd->name, ' ') == NULL && strchr(cmd->name, '\t') == NULL); |
1354 |
aosDbgCheck(cmd->callback != NULL);
|
1355 |
aosDbgCheck(cmd->next == NULL);
|
1356 |
|
1357 |
aos_shellcommand_t* prev = NULL;
|
1358 |
aos_shellcommand_t** curr = &(shell->commands); |
1359 |
|
1360 |
// insert the command to the list wrt lexographical order (exception: lower case characters preceed upper their uppercase counterparts)
|
1361 |
while (*curr != NULL) { |
1362 |
// iterate through the list as long as the command names are 'smaller'
|
1363 |
const int cmp = _strccmp((*curr)->name, cmd->name, true, NULL, NULL); |
1364 |
if (cmp < 0) { |
1365 |
prev = *curr; |
1366 |
curr = &((*curr)->next); |
1367 |
continue;
|
1368 |
} |
1369 |
// error if the command already exists
|
1370 |
else if (cmp == 0) { |
1371 |
return AOS_ERROR;
|
1372 |
} |
1373 |
// insert the command as soon as a 'larger' name was found
|
1374 |
else /* if (cmpval > 0) */ { |
1375 |
cmd->next = *curr; |
1376 |
// special case: the first command is larger
|
1377 |
if (prev == NULL) { |
1378 |
shell->commands = cmd; |
1379 |
} else {
|
1380 |
prev->next = cmd; |
1381 |
} |
1382 |
return AOS_SUCCESS;
|
1383 |
} |
1384 |
} |
1385 |
// the end of the list has been reached
|
1386 |
|
1387 |
// append the command
|
1388 |
*curr = cmd; |
1389 |
return AOS_SUCCESS;
|
1390 |
} |
1391 |
|
1392 |
/**
|
1393 |
* @brief Removes a command from the shells list of commands.
|
1394 |
*
|
1395 |
* @param[in] shell Pointer to the shell object.
|
1396 |
* @param[in] cmd Name of the command to removde.
|
1397 |
* @param[out] removed Optional pointer to the command that was removed.
|
1398 |
*
|
1399 |
* @return A status value.
|
1400 |
* @retval AOS_SUCCESS The command was removed successfully.
|
1401 |
* @retval AOS_ERROR The command name was not found.
|
1402 |
*/
|
1403 |
aos_status_t aosShellRemoveCommand(aos_shell_t *shell, char *cmd, aos_shellcommand_t **removed)
|
1404 |
{ |
1405 |
aosDbgCheck(shell != NULL);
|
1406 |
aosDbgCheck(cmd != NULL && strlen(cmd) > 0); |
1407 |
|
1408 |
aos_shellcommand_t* prev = NULL;
|
1409 |
aos_shellcommand_t** curr = &(shell->commands); |
1410 |
|
1411 |
// iterate through the list and seach for the specified command name
|
1412 |
while (curr != NULL) { |
1413 |
const int cmpval = strcmp((*curr)->name, cmd); |
1414 |
// iterate through the list as long as the command names are 'smaller'
|
1415 |
if (cmpval < 0) { |
1416 |
prev = *curr; |
1417 |
curr = &((*curr)->next); |
1418 |
continue;
|
1419 |
} |
1420 |
// remove the command when found
|
1421 |
else if (cmpval == 0) { |
1422 |
// special case: the first command matches
|
1423 |
if (prev == NULL) { |
1424 |
shell->commands = (*curr)->next; |
1425 |
} else {
|
1426 |
prev->next = (*curr)->next; |
1427 |
} |
1428 |
(*curr)->next = NULL;
|
1429 |
// set the optional output argument
|
1430 |
if (removed != NULL) { |
1431 |
*removed = *curr; |
1432 |
} |
1433 |
return AOS_SUCCESS;
|
1434 |
} |
1435 |
// break the loop if the command names are 'larger'
|
1436 |
else /* if (cmpval > 0) */ { |
1437 |
break;
|
1438 |
} |
1439 |
} |
1440 |
|
1441 |
// if the command was not found, return an error
|
1442 |
return AOS_ERROR;
|
1443 |
} |
1444 |
|
1445 |
/**
|
1446 |
* @brief Count the number of commands assigned to the shell.
|
1447 |
*
|
1448 |
* @param[in] shell The shell to count the commands for.
|
1449 |
*
|
1450 |
* @return The number of commands associated to the shell.
|
1451 |
*/
|
1452 |
unsigned int aosShellCountCommands(aos_shell_t* shell) |
1453 |
{ |
1454 |
aosDbgCheck(shell != NULL);
|
1455 |
|
1456 |
unsigned int count = 0; |
1457 |
aos_shellcommand_t* cmd = shell->commands; |
1458 |
while (cmd != NULL) { |
1459 |
++count; |
1460 |
cmd = cmd->next; |
1461 |
} |
1462 |
|
1463 |
return count;
|
1464 |
} |
1465 |
|
1466 |
/**
|
1467 |
* @brief Add a channel to a AosShellStream.
|
1468 |
*
|
1469 |
* @param[in] stream The AosShellStream to extend.
|
1470 |
* @param[in] channel The channel to be added to the stream.
|
1471 |
*/
|
1472 |
void aosShellStreamAddChannel(AosShellStream* stream, AosShellChannel* channel)
|
1473 |
{ |
1474 |
aosDbgCheck(stream != NULL);
|
1475 |
aosDbgCheck(channel != NULL && channel->asyncchannel != NULL && channel->next == NULL && (channel->flags & AOS_SHELLCHANNEL_ATTACHED) == 0); |
1476 |
|
1477 |
// prepend the new channel
|
1478 |
chSysLock(); |
1479 |
channel->flags |= AOS_SHELLCHANNEL_ATTACHED; |
1480 |
channel->next = stream->channel; |
1481 |
stream->channel = channel; |
1482 |
chSysUnlock(); |
1483 |
|
1484 |
return;
|
1485 |
} |
1486 |
|
1487 |
/**
|
1488 |
* @brief Remove a channel from an AosShellStream.
|
1489 |
*
|
1490 |
* @param[in] stream The AosShellStream to modify.
|
1491 |
* @param[in] channel The channel to remove.
|
1492 |
*
|
1493 |
* @return A status value.
|
1494 |
* @retval AOS_SUCCESS The channel was removed successfully.
|
1495 |
* @retval AOS_ERROR The specified channel was not found to be associated with the shell.
|
1496 |
*/
|
1497 |
aos_status_t aosShellStreamRemoveChannel(AosShellStream* stream, AosShellChannel* channel) |
1498 |
{ |
1499 |
aosDbgCheck(stream != NULL);
|
1500 |
aosDbgCheck(channel != NULL && channel->asyncchannel != NULL && channel->flags & AOS_SHELLCHANNEL_ATTACHED); |
1501 |
|
1502 |
// local varibales
|
1503 |
AosShellChannel* prev = NULL;
|
1504 |
AosShellChannel* curr = stream->channel; |
1505 |
|
1506 |
// iterate through the list and search for the specified channel
|
1507 |
while (curr != NULL) { |
1508 |
// if the channel was found
|
1509 |
if (curr == channel) {
|
1510 |
chSysLock(); |
1511 |
// special case: the first channel matches (prev is NULL)
|
1512 |
if (prev == NULL) { |
1513 |
stream->channel = curr->next; |
1514 |
} else {
|
1515 |
prev->next = channel->next; |
1516 |
} |
1517 |
curr->next = NULL;
|
1518 |
curr->flags &= ~AOS_SHELLCHANNEL_ATTACHED; |
1519 |
chSysUnlock(); |
1520 |
return AOS_SUCCESS;
|
1521 |
} |
1522 |
} |
1523 |
|
1524 |
// if the channel was not found, return an error
|
1525 |
return AOS_ERROR;
|
1526 |
} |
1527 |
|
1528 |
/**
|
1529 |
* @brief Enable a AosShellChannel as input.
|
1530 |
*
|
1531 |
* @param[in] channel The channel to enable as input.
|
1532 |
*/
|
1533 |
void aosShellChannelInputEnable(AosShellChannel* channel)
|
1534 |
{ |
1535 |
aosDbgCheck(channel != NULL && channel->asyncchannel != NULL); |
1536 |
|
1537 |
chSysLock(); |
1538 |
channel->listener.wflags |= CHN_INPUT_AVAILABLE; |
1539 |
channel->flags |= AOS_SHELLCHANNEL_INPUT_ENABLED; |
1540 |
chSysUnlock(); |
1541 |
|
1542 |
return;
|
1543 |
} |
1544 |
|
1545 |
/**
|
1546 |
* @brief Disable a AosShellChannel as input.
|
1547 |
*
|
1548 |
* @param[in] channel The channel to disable as input.
|
1549 |
*/
|
1550 |
void aosShellChannelInputDisable( AosShellChannel* channel)
|
1551 |
{ |
1552 |
aosDbgCheck(channel != NULL && channel->asyncchannel != NULL); |
1553 |
|
1554 |
chSysLock(); |
1555 |
channel->listener.wflags &= ~CHN_INPUT_AVAILABLE; |
1556 |
channel->flags &= ~AOS_SHELLCHANNEL_INPUT_ENABLED; |
1557 |
chSysUnlock(); |
1558 |
|
1559 |
return;
|
1560 |
} |
1561 |
|
1562 |
/**
|
1563 |
* @brief Enable a AosShellChannel as output.
|
1564 |
*
|
1565 |
* @param[in] channel The channel to enable as output.
|
1566 |
*/
|
1567 |
void aosShellChannelOutputEnable(AosShellChannel* channel)
|
1568 |
{ |
1569 |
aosDbgCheck(channel != NULL && channel->asyncchannel != NULL); |
1570 |
|
1571 |
channel->flags |= AOS_SHELLCHANNEL_OUTPUT_ENABLED; |
1572 |
|
1573 |
return;
|
1574 |
} |
1575 |
|
1576 |
/**
|
1577 |
* @brief Disable a AosShellChannel as output.
|
1578 |
*
|
1579 |
* @param[in] channel The channel to disable as output.
|
1580 |
*/
|
1581 |
void aosShellChannelOutputDisable(AosShellChannel* channel)
|
1582 |
{ |
1583 |
aosDbgCheck(channel != NULL && channel->asyncchannel != NULL); |
1584 |
|
1585 |
channel->flags &= ~AOS_SHELLCHANNEL_OUTPUT_ENABLED; |
1586 |
|
1587 |
return;
|
1588 |
} |
1589 |
|
1590 |
/**
|
1591 |
* @brief Thread main function.
|
1592 |
*
|
1593 |
* @param[in] aosShellThread Name of the function;
|
1594 |
* @param[in] shell Pointer to the shell object.
|
1595 |
*/
|
1596 |
void aosShellThread(void* shell) |
1597 |
{ |
1598 |
aosDbgCheck(shell != NULL);
|
1599 |
|
1600 |
// local variables
|
1601 |
eventmask_t eventmask; |
1602 |
eventflags_t eventflags; |
1603 |
AosShellChannel* channel; |
1604 |
aos_status_t readeval; |
1605 |
char* args[((aos_shell_t*)shell)->input.nargs];
|
1606 |
size_t nargs = 0;
|
1607 |
aos_shellcommand_t* cmd; |
1608 |
|
1609 |
// initialize variables
|
1610 |
for (size_t arg = 0; arg < ((aos_shell_t*)shell)->input.nargs; ++arg) { |
1611 |
args[arg] = NULL;
|
1612 |
} |
1613 |
|
1614 |
// register OS related events
|
1615 |
chEvtRegisterMask(((aos_shell_t*)shell)->os.eventSource, &(((aos_shell_t*)shell)->os.eventListener), AOS_SHELL_EVENTMASK_OS); |
1616 |
// register events to all input channels
|
1617 |
for (channel = ((aos_shell_t*)shell)->stream.channel; channel != NULL; channel = channel->next) { |
1618 |
chEvtRegisterMaskWithFlags(&(channel->asyncchannel->event), &(channel->listener), AOS_SHELL_EVENTMASK_INPUT, channel->listener.wflags); |
1619 |
} |
1620 |
|
1621 |
// fire start event
|
1622 |
chEvtBroadcastFlags(&(((aos_shell_t*)shell)->eventSource), AOS_SHELL_EVTFLAG_START); |
1623 |
|
1624 |
// print the prompt for the first time
|
1625 |
_printPrompt((aos_shell_t*)shell); |
1626 |
|
1627 |
// enter thread loop
|
1628 |
while (!chThdShouldTerminateX()) {
|
1629 |
// wait for event and handle it accordingly
|
1630 |
eventmask = chEvtWaitOne(ALL_EVENTS); |
1631 |
|
1632 |
// handle event
|
1633 |
switch (eventmask) {
|
1634 |
|
1635 |
// OS related events
|
1636 |
case AOS_SHELL_EVENTMASK_OS:
|
1637 |
{ |
1638 |
eventflags = chEvtGetAndClearFlags(&((aos_shell_t*)shell)->os.eventListener); |
1639 |
// handle shutdown/restart events
|
1640 |
if (eventflags & AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_MASK) {
|
1641 |
chThdTerminate(((aos_shell_t*)shell)->thread); |
1642 |
} else {
|
1643 |
// print an error message
|
1644 |
chprintf((BaseSequentialStream*)&((aos_shell_t*)shell)->stream, "\nERROR: unknown OS event received (0x%08X)\n", eventflags);
|
1645 |
} |
1646 |
break;
|
1647 |
} |
1648 |
|
1649 |
// input events
|
1650 |
case AOS_SHELL_EVENTMASK_INPUT:
|
1651 |
{ |
1652 |
// check and handle all channels
|
1653 |
channel = ((aos_shell_t*)shell)->stream.channel; |
1654 |
while (channel != NULL) { |
1655 |
eventflags = chEvtGetAndClearFlags(&channel->listener); |
1656 |
// if there is new input and a command shall be executed
|
1657 |
if ((eventflags & CHN_INPUT_AVAILABLE) && _readChannel((aos_shell_t*)shell, channel, NULL)) { |
1658 |
// parse input line to argument list
|
1659 |
nargs = _parseArguments((aos_shell_t*)shell, args); |
1660 |
// check number of arguments
|
1661 |
if (nargs > ((aos_shell_t*)shell)->input.nargs) {
|
1662 |
// error too many arguments
|
1663 |
chprintf((BaseSequentialStream*)&((aos_shell_t*)shell)->stream, "\ttoo many arguments\n");
|
1664 |
} else if (nargs > 0) { |
1665 |
// search command list for arg[0] and execute callback
|
1666 |
cmd = ((aos_shell_t*)shell)->commands; |
1667 |
while (cmd != NULL) { |
1668 |
if (strcmp(args[0], cmd->name) == 0) { |
1669 |
((aos_shell_t*)shell)->execstatus.command = cmd; |
1670 |
chEvtBroadcastFlags(&(((aos_shell_t*)shell)->eventSource), AOS_SHELL_EVTFLAG_EXEC); |
1671 |
((aos_shell_t*)shell)->execstatus.retval = cmd->callback((BaseSequentialStream*)&((aos_shell_t*)shell)->stream, (int)nargs, args);
|
1672 |
chEvtBroadcastFlags(&(((aos_shell_t*)shell)->eventSource), AOS_SHELL_EVTFLAG_DONE); |
1673 |
// notify if the command was not successful
|
1674 |
if (((aos_shell_t*)shell)->execstatus.retval != 0) { |
1675 |
chprintf((BaseSequentialStream*)&((aos_shell_t*)shell)->stream, "command returned exit status %d\n", ((aos_shell_t*)shell)->execstatus.retval);
|
1676 |
} |
1677 |
break;
|
1678 |
} |
1679 |
cmd = cmd->next; |
1680 |
} /* end of while */
|
1681 |
|
1682 |
// if no matching command was found, print an error
|
1683 |
if (cmd == NULL) { |
1684 |
chprintf((BaseSequentialStream*)&((aos_shell_t*)shell)->stream, "\tcommand '%s' not found\n", args[0]); |
1685 |
} |
1686 |
} |
1687 |
|
1688 |
// reset some internal variables and eprint a new prompt
|
1689 |
if (readeval == AOS_SUCCESS && !chThdShouldTerminateX()) {
|
1690 |
((aos_shell_t*)shell)->inputdata.cursorpos = 0;
|
1691 |
((aos_shell_t*)shell)->inputdata.lineend = 0;
|
1692 |
((aos_shell_t*)shell)->inputdata.noinput = true;
|
1693 |
_printPrompt((aos_shell_t*)shell); |
1694 |
} |
1695 |
} |
1696 |
|
1697 |
// iterate to next channel
|
1698 |
channel = channel->next; |
1699 |
} |
1700 |
break;
|
1701 |
} |
1702 |
|
1703 |
// other events
|
1704 |
default:
|
1705 |
{ |
1706 |
// print an error message
|
1707 |
chprintf((BaseSequentialStream*)&((aos_shell_t*)shell)->stream, "\nSHELL: ERROR: unknown event received (0x%08X)\n", eventmask);
|
1708 |
break;
|
1709 |
} |
1710 |
|
1711 |
} /* end of switch */
|
1712 |
|
1713 |
} /* end of while */
|
1714 |
|
1715 |
// fire event and exit the thread
|
1716 |
chSysLock(); |
1717 |
chEvtBroadcastFlagsI(&(((aos_shell_t*)shell)->eventSource), AOS_SHELL_EVTFLAG_EXIT); |
1718 |
chThdExitS(MSG_OK); |
1719 |
// no chSysUnlock() required since the thread has been terminated an all waiting threads have been woken up
|
1720 |
} |
1721 |
|
1722 |
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */ |
1723 |
|
1724 |
/** @} */
|