amiro-os / core / src / aos_shell.c @ a93a1019
History | View | Annotate | Download (73.613 KB)
1 |
/*
|
---|---|
2 |
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
|
3 |
Copyright (C) 2016..2020 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 Size of the escape sequence buffer.
|
39 |
*/
|
40 |
#if !defined(AOS_SHELL_ESCSEQUENCE_LENGTH) || defined(__DOXYGEN__)
|
41 |
#define AOS_SHELL_ESCSEQUENCE_LENGTH 8 |
42 |
#endif
|
43 |
|
44 |
/**
|
45 |
* @brief The character the input buffer is initialized with.
|
46 |
*/
|
47 |
#define INBUF_INIT_CHAR '\x07' |
48 |
|
49 |
/**
|
50 |
* @brief Event mask to be set on OS related events.
|
51 |
*/
|
52 |
#define EVENTMASK_OS EVENT_MASK(0) |
53 |
|
54 |
/**
|
55 |
* @brief Event mask to be set on a input event.
|
56 |
*/
|
57 |
#define EVENTMASK_INPUT EVENT_MASK(1) |
58 |
|
59 |
/**
|
60 |
* @brief String that defines the INSERT key as specified by VT100.
|
61 |
*/
|
62 |
#define KEYSTRING_INSERT "\x1B\x5B\x32\x7E" |
63 |
|
64 |
/**
|
65 |
* @brief String that defines the DEL key as specified by VT100.
|
66 |
*/
|
67 |
#define KEYSTRING_DELETE "\x1B\x5B\x33\x7E" |
68 |
|
69 |
/**
|
70 |
* @brief String that defines the HOME key as specified by VT100.
|
71 |
*/
|
72 |
#define KEYSTRING_HOME "\x1B\x5B\x48" |
73 |
|
74 |
/**
|
75 |
* @brief String that defines the END key as specified by VT100.
|
76 |
*/
|
77 |
#define KEYSTRING_END "\x1B\x5B\x46" |
78 |
|
79 |
/**
|
80 |
* @brief String that defines the PGUP key as specified by VT100.
|
81 |
*/
|
82 |
#define KEYSTRING_PAGEUP "\x1B\x5B\x35\x7E" |
83 |
|
84 |
/**
|
85 |
* @brief String that defines the PGUP key as specified by VT100.
|
86 |
*/
|
87 |
#define KEYSTRING_PAGEDOWN "\x1B\x5B\x36\x7E" |
88 |
|
89 |
/**
|
90 |
* @brief String that defines the 'arrow down' key as specified by VT100.
|
91 |
*/
|
92 |
#define KEYSTRING_ARROWUP "\x1B\x5B\x41" |
93 |
|
94 |
/**
|
95 |
* @brief String that defines the 'arrow up' key as specified by VT100.
|
96 |
*/
|
97 |
#define KEYSTRING_ARROWDOWN "\x1B\x5B\x42" |
98 |
|
99 |
/**
|
100 |
* @brief String that defines the 'arrow left' key as specified by VT100.
|
101 |
*/
|
102 |
#define KEYSTRING_ARROWLEFT "\x1B\x5B\x44" |
103 |
|
104 |
/**
|
105 |
* @brief String that defines the 'arrow right' key as specified by VT100.
|
106 |
*/
|
107 |
#define KEYSTRING_ARROWRIGHT "\x1B\x5B\x43" |
108 |
|
109 |
/**
|
110 |
* @brief String that defines the CRTL + 'arrow up' key combination as specified by VT100.
|
111 |
*/
|
112 |
#define KEYSTRING_CTRL_ARROWUP "\x1B\x5B\x31\x3B\x35\x41" |
113 |
|
114 |
/**
|
115 |
* @brief String that defines the CRTL + 'arrow down' key combination as specified by VT100.
|
116 |
*/
|
117 |
#define KEYSTRING_CTRL_ARROWDOWN "\x1B\x5B\x31\x3B\x35\x42" |
118 |
|
119 |
/**
|
120 |
* @brief String that defines the CRTL + 'arrow left' key combination as specified by VT100.
|
121 |
*/
|
122 |
#define KEYSTRING_CTRL_ARROWLEFT "\x1B\x5B\x31\x3B\x35\x44" |
123 |
|
124 |
/**
|
125 |
* @brief String that defines the CRTL + 'arrow right' key combination as specified by VT100.
|
126 |
*/
|
127 |
#define KEYSTRING_CTRL_ARROWRIGHT "\x1B\x5B\x31\x3B\x35\x43" |
128 |
|
129 |
/******************************************************************************/
|
130 |
/* EXPORTED VARIABLES */
|
131 |
/******************************************************************************/
|
132 |
|
133 |
/******************************************************************************/
|
134 |
/* LOCAL TYPES */
|
135 |
/******************************************************************************/
|
136 |
|
137 |
/*
|
138 |
* forward declarations
|
139 |
*/
|
140 |
static size_t _channelwrite(void *instance, const uint8_t *bp, size_t n); |
141 |
static size_t _channelread(void *instance, uint8_t *bp, size_t n); |
142 |
static msg_t _channelput(void *instance, uint8_t b); |
143 |
static msg_t _channelget(void *instance); |
144 |
static msg_t _channelputt(void *instance, uint8_t b, sysinterval_t time); |
145 |
static msg_t _channelgett(void *instance, sysinterval_t time); |
146 |
static size_t _channelwritet(void *instance, const uint8_t *bp, size_t n, sysinterval_t time); |
147 |
static size_t _channelreadt(void *instance, uint8_t *bp, size_t n, sysinterval_t time); |
148 |
static msg_t _channelctl(void *instance, unsigned int operation, void *arg); |
149 |
static size_t _streamwrite(void *instance, const uint8_t *bp, size_t n); |
150 |
static size_t _stremread(void *instance, uint8_t *bp, size_t n); |
151 |
static msg_t _streamput(void *instance, uint8_t b); |
152 |
static msg_t _streamget(void *instance); |
153 |
|
154 |
static const struct AosShellChannelVMT _channelvmt = { |
155 |
(size_t) 0,
|
156 |
_channelwrite, |
157 |
_channelread, |
158 |
_channelput, |
159 |
_channelget, |
160 |
_channelputt, |
161 |
_channelgett, |
162 |
_channelwritet, |
163 |
_channelreadt, |
164 |
_channelctl, |
165 |
}; |
166 |
|
167 |
static const struct AosShellStreamVMT _streamvmt = { |
168 |
(size_t) 0,
|
169 |
_streamwrite, |
170 |
_stremread, |
171 |
_streamput, |
172 |
_streamget, |
173 |
}; |
174 |
|
175 |
/**
|
176 |
* @brief Enumerator of special keyboard keys.
|
177 |
*/
|
178 |
typedef enum special_key { |
179 |
KEY_UNKNOWN, /**< any/unknow key */
|
180 |
KEY_AMBIGUOUS, /**< key is ambiguous */
|
181 |
KEY_TAB, /**< tabulator key */
|
182 |
KEY_BACKSPACE, /**< backspace key */
|
183 |
KEY_INSERT, /**< insert key */
|
184 |
KEY_DELETE, /**< delete key */
|
185 |
KEY_ESCAPE, /**< escape key */
|
186 |
KEY_HOME, /**< home key */
|
187 |
KEY_END, /**< end key */
|
188 |
KEY_PAGEUP, /**< page up key */
|
189 |
KEY_PAGEDOWN, /**< page down key */
|
190 |
KEY_ARROWUP, /**< arrow up key */
|
191 |
KEY_ARROWDOWN, /**< arrow down key */
|
192 |
KEY_ARROWLEFT, /**< arrow left key */
|
193 |
KEY_ARROWRIGHT, /**< arrow right key */
|
194 |
KEY_CTRL_ARROWUP, /**< CTRL + arrow up key */
|
195 |
KEY_CTRL_ARROWDOWN, /**< CTRL + arrow down key */
|
196 |
KEY_CTRL_ARROWLEFT, /**< CTRL + arrow left key */
|
197 |
KEY_CTRL_ARROWRIGHT, /**< CTRL + arrow right key */
|
198 |
KEY_CTRL_C, /**< CTRL + C key */
|
199 |
} special_key_t; |
200 |
|
201 |
/**
|
202 |
* @brief Enumerator for case (in)sensitive character matching.
|
203 |
*/
|
204 |
typedef enum charmatch { |
205 |
CHAR_MATCH_NOT = 0, /**< Characters do not match at all. */ |
206 |
CHAR_MATCH_NCASE = 1, /**< Characters would match case insensitive. */ |
207 |
CHAR_MATCH_CASE = 2, /**< Characters do match with case. */ |
208 |
} charmatch_t; |
209 |
|
210 |
/**
|
211 |
* @brief Enumerator to encode shell actions.
|
212 |
*/
|
213 |
typedef enum aos_shellaction { |
214 |
ACTION_NONE, /**< No action at all. */
|
215 |
ACTION_READCHAR, /**< Read a printable character. */
|
216 |
ACTION_AUTOCOMPLETE, /**< Automatically comlete input by using available command. */
|
217 |
ACTION_SUGGEST, /**< Suggest matching available commands. */
|
218 |
ACTION_EXECUTE, /**< Execute input. */
|
219 |
ACTION_DELETEBACKWARD, /**< Delete a single character backwards. */
|
220 |
ACTION_DELETEFORWARD, /**< Delete a single character forwards. */
|
221 |
ACTION_CLEAR, /**< Clear the input. */
|
222 |
ACTION_RECALLPREVIOUS, /**< Recall the previous (older) entry in the history. */
|
223 |
ACTION_RECALLNEXT, /**< Recall the next (more recent) entry in the history. */
|
224 |
ACTION_RECALLOLDEST, /**< Recall the oldest entry in the history. */
|
225 |
ACTION_RECALLCURRENT, /**< Recall the current input. */
|
226 |
ACTION_CURSORLEFT, /**< Move cursor one character to the left. */
|
227 |
ACTION_CURSORRIGHT, /**< Move cursor one character to the right. */
|
228 |
ACTION_CURSORWORDLEFT, /**< Move cursor one word to the left. */
|
229 |
ACTION_CURSORWORDRIGHT, /**< Move cursor one word to the right. */
|
230 |
ACTION_CURSOR2END, /**< Move cursor to the very right. */
|
231 |
ACTION_CURSOR2START, /**< Move cursor to the very left. */
|
232 |
ACTION_RESET, /**< Reset the current input. */
|
233 |
ACTION_INSERTTOGGLE, /**< Toggle insertion mode. */
|
234 |
ACTION_ESCSTART, /**< Start an escape sequence (special keys). */
|
235 |
ACTION_PRINTUNKNOWNSEQUENCE, /**< Print an unknown escape sequence. */
|
236 |
} action_t; |
237 |
|
238 |
/**
|
239 |
* @brief Struct that holds most important runtime data for the shell.
|
240 |
* @details The structure is to be used by the shell thread main function as some kind of structured stack, which can be easily passed to other functions.
|
241 |
*/
|
242 |
typedef struct runtimedata { |
243 |
/**
|
244 |
* @brief Data related to the current input.
|
245 |
*/
|
246 |
struct {
|
247 |
/**
|
248 |
* @brief Length of the input.
|
249 |
*/
|
250 |
size_t length; |
251 |
|
252 |
/**
|
253 |
* @brief Current position of the cursor in the input line.
|
254 |
*/
|
255 |
size_t cursorpos; |
256 |
|
257 |
/**
|
258 |
* @brief Buffer to store escape sequences, which describe special characters.
|
259 |
*/
|
260 |
char escseq[AOS_SHELL_ESCSEQUENCE_LENGTH];
|
261 |
} input; |
262 |
|
263 |
/**
|
264 |
* @brief Data related to the entry or history buffer.
|
265 |
*/
|
266 |
struct {
|
267 |
/**
|
268 |
* @brief Current entry to be filled and executed.
|
269 |
*/
|
270 |
size_t current; |
271 |
|
272 |
/**
|
273 |
* @brief Selected entry in the 'history' as preview.
|
274 |
* @details A value of 0 indicates, that the line is cleared as a preview.
|
275 |
* A value of 1 indicates, that the current entry is selected.
|
276 |
* A value of t>1 indicates, that the entry t-1 in the past is selected.
|
277 |
* The value must never be greater than the number of entries available, of course.
|
278 |
*/
|
279 |
size_t selected; |
280 |
|
281 |
/**
|
282 |
* @brief Selected entry in the 'history' that has been edited by the user.
|
283 |
* A value of 0 indicates, that there was no modification by the user yet (i.e. charcters, deletions or autofill).
|
284 |
* A value of 1 indicates, that the current entry was edited.
|
285 |
* A value of t>1 indicated, that a history entry was recalled and then edited.
|
286 |
*/
|
287 |
size_t edited; |
288 |
} buffer; |
289 |
|
290 |
/**
|
291 |
* @brief The last action executed by the shell.
|
292 |
*/
|
293 |
action_t lastaction; |
294 |
} runtimedata_t; |
295 |
|
296 |
/******************************************************************************/
|
297 |
/* LOCAL VARIABLES */
|
298 |
/******************************************************************************/
|
299 |
|
300 |
/******************************************************************************/
|
301 |
/* LOCAL FUNCTIONS */
|
302 |
/******************************************************************************/
|
303 |
|
304 |
/**
|
305 |
* @brief Implementation of the BaseAsynchronous write() method (inherited from BaseSequentialStream).
|
306 |
*/
|
307 |
static size_t _channelwrite(void *instance, const uint8_t *bp, size_t n) |
308 |
{ |
309 |
if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
310 |
return streamWrite(((AosShellChannel*)instance)->asyncchannel, bp, n);
|
311 |
} else {
|
312 |
return 0; |
313 |
} |
314 |
} |
315 |
|
316 |
/**
|
317 |
* @brief Implementation of the BaseAsynchronous read() method (inherited from BaseSequentialStream).
|
318 |
*/
|
319 |
static size_t _channelread(void *instance, uint8_t *bp, size_t n) |
320 |
{ |
321 |
if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
322 |
return streamRead(((AosShellChannel*)instance)->asyncchannel, bp, n);
|
323 |
} else {
|
324 |
return 0; |
325 |
} |
326 |
} |
327 |
|
328 |
/**
|
329 |
* @brief Implementation of the BaseAsynchronous put() method (inherited from BaseSequentialStream).
|
330 |
*/
|
331 |
static msg_t _channelput(void *instance, uint8_t b) |
332 |
{ |
333 |
if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
334 |
return streamPut(((AosShellChannel*)instance)->asyncchannel, b);
|
335 |
} else {
|
336 |
return MSG_RESET;
|
337 |
} |
338 |
} |
339 |
|
340 |
/**
|
341 |
* @brief Implementation of the BaseAsynchronous get() method (inherited from BaseSequentialStream).
|
342 |
*/
|
343 |
static msg_t _channelget(void *instance) |
344 |
{ |
345 |
if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
346 |
return streamGet(((AosShellChannel*)instance)->asyncchannel);
|
347 |
} else {
|
348 |
return MSG_RESET;
|
349 |
} |
350 |
} |
351 |
|
352 |
/**
|
353 |
* @brief Implementation of the BaseAsynchronous putt() method.
|
354 |
*/
|
355 |
static msg_t _channelputt(void *instance, uint8_t b, sysinterval_t time) |
356 |
{ |
357 |
if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
358 |
return chnPutTimeout(((AosShellChannel*)instance)->asyncchannel, b, time);
|
359 |
} else {
|
360 |
return MSG_RESET;
|
361 |
} |
362 |
} |
363 |
|
364 |
/**
|
365 |
* @brief Implementation of the BaseAsynchronous gett() method.
|
366 |
*/
|
367 |
static msg_t _channelgett(void *instance, sysinterval_t time) |
368 |
{ |
369 |
if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
370 |
return chnGetTimeout(((AosShellChannel*)instance)->asyncchannel, time);
|
371 |
} else {
|
372 |
return MSG_RESET;
|
373 |
} |
374 |
} |
375 |
|
376 |
/**
|
377 |
* @brief Implementation of the BaseAsynchronous writet() method.
|
378 |
*/
|
379 |
static size_t _channelwritet(void *instance, const uint8_t *bp, size_t n, sysinterval_t time) |
380 |
{ |
381 |
if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
382 |
return chnWriteTimeout(((AosShellChannel*)instance)->asyncchannel, bp, n, time);
|
383 |
} else {
|
384 |
return 0; |
385 |
} |
386 |
} |
387 |
|
388 |
/**
|
389 |
* @brief Implementation of the BaseAsynchronous readt() method.
|
390 |
*/
|
391 |
static size_t _channelreadt(void *instance, uint8_t *bp, size_t n, sysinterval_t time) |
392 |
{ |
393 |
if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
394 |
return chnReadTimeout(((AosShellChannel*)instance)->asyncchannel, bp, n, time);
|
395 |
} else {
|
396 |
return 0; |
397 |
} |
398 |
} |
399 |
|
400 |
/**
|
401 |
* @brief Implementation of the BaseAsynchronousChannel ctl() method.
|
402 |
*/
|
403 |
static msg_t _channelctl(void *instance, unsigned int operation, void *arg) |
404 |
{ |
405 |
(void) instance;
|
406 |
|
407 |
switch (operation) {
|
408 |
case CHN_CTL_NOP:
|
409 |
osalDbgCheck(arg == NULL);
|
410 |
break;
|
411 |
case CHN_CTL_INVALID:
|
412 |
osalDbgAssert(false, "invalid CTL operation"); |
413 |
break;
|
414 |
default:
|
415 |
break;
|
416 |
} |
417 |
return MSG_OK;
|
418 |
} |
419 |
|
420 |
/**
|
421 |
* @brief Implementation of the BaseSequentialStream write() method.
|
422 |
*/
|
423 |
static size_t _streamwrite(void *instance, const uint8_t *bp, size_t n) |
424 |
{ |
425 |
aosDbgCheck(instance != NULL);
|
426 |
|
427 |
// local variables
|
428 |
AosShellChannel* channel = ((AosShellStream*)instance)->channel; |
429 |
size_t bytes; |
430 |
size_t maxbytes = 0;
|
431 |
|
432 |
// iterate through the list of channels
|
433 |
while (channel != NULL) { |
434 |
bytes = streamWrite(channel, bp, n); |
435 |
maxbytes = (bytes > maxbytes) ? bytes : maxbytes; |
436 |
channel = channel->next; |
437 |
} |
438 |
|
439 |
return maxbytes;
|
440 |
} |
441 |
|
442 |
/**
|
443 |
* @brief Implementation of the BaseSequentialStream read() method.
|
444 |
*/
|
445 |
static size_t _stremread(void *instance, uint8_t *bp, size_t n) |
446 |
{ |
447 |
(void)instance;
|
448 |
(void)bp;
|
449 |
(void)n;
|
450 |
|
451 |
return 0; |
452 |
} |
453 |
|
454 |
/**
|
455 |
* @brief Implementation of the BaseSequentialStream put() method.
|
456 |
*/
|
457 |
static msg_t _streamput(void *instance, uint8_t b) |
458 |
{ |
459 |
aosDbgCheck(instance != NULL);
|
460 |
|
461 |
// local variables
|
462 |
AosShellChannel* channel = ((AosShellStream*)instance)->channel; |
463 |
msg_t ret = MSG_OK; |
464 |
|
465 |
// iterate through the list of channels
|
466 |
while (channel != NULL) { |
467 |
msg_t ret_ = streamPut(channel, b); |
468 |
ret = (ret_ < ret) ? ret_ : ret; |
469 |
channel = channel->next; |
470 |
} |
471 |
|
472 |
return ret;
|
473 |
} |
474 |
|
475 |
/**
|
476 |
* @brief Implementation of the BaseSequentialStream get() method.
|
477 |
*/
|
478 |
static msg_t _streamget(void *instance) |
479 |
{ |
480 |
(void)instance;
|
481 |
|
482 |
return 0; |
483 |
} |
484 |
|
485 |
/**
|
486 |
* @brief Retrieve a pointer to the string buffer of a specified entry in the input buffer.
|
487 |
*
|
488 |
* @param[in] shell Pointer to a shell object.
|
489 |
* @param[in] entry Entry to be retrieved.
|
490 |
*
|
491 |
* @return Pointer to the entry in the input buffer.
|
492 |
*/
|
493 |
static inline char* _getAbsoluteEntry(const aos_shell_t* shell, size_t entry) |
494 |
{ |
495 |
aosDbgCheck(shell != NULL);
|
496 |
aosDbgCheck(entry < shell->input.nentries); |
497 |
|
498 |
return &(shell->input.buffer[entry * shell->input.linewidth * sizeof(char)]); |
499 |
} |
500 |
|
501 |
/**
|
502 |
* @brief Calculate absolute entry from history offset.
|
503 |
*
|
504 |
* @param[in] shell Pointer to a shell object.
|
505 |
* @param[in] rdata Pointer to a runtime data object.
|
506 |
* @param[in] offset Relative offset of the entry to be retrieved.
|
507 |
*
|
508 |
* @return Absolute index of the historic entry.
|
509 |
*/
|
510 |
static inline size_t _historyOffset2EntryIndex(const aos_shell_t* shell, const runtimedata_t* rdata, size_t offset) |
511 |
{ |
512 |
aosDbgCheck(shell != NULL);
|
513 |
aosDbgCheck(rdata != NULL);
|
514 |
aosDbgCheck(offset < shell->input.nentries); |
515 |
|
516 |
return ((shell->input.nentries + rdata->buffer.current - offset) % shell->input.nentries);
|
517 |
} |
518 |
|
519 |
/**
|
520 |
* @brief Retrieve a pointer to the string buffer of a historic entry in the input buffer.
|
521 |
*
|
522 |
* @param[in] shell Pointer to a shell object.
|
523 |
* @param[in] rdata Pointer to a runtime data object.
|
524 |
* @param[in] offset Relative offset of the entry to be retrieved.
|
525 |
*
|
526 |
* @return Pointer to the entry in the input buffer.
|
527 |
*/
|
528 |
static inline char* _getRelativeEntry(const aos_shell_t* shell, const runtimedata_t* rdata, size_t offset) |
529 |
{ |
530 |
aosDbgCheck(shell != NULL);
|
531 |
aosDbgCheck(rdata != NULL);
|
532 |
aosDbgCheck(offset < shell->input.nentries); |
533 |
|
534 |
return _getAbsoluteEntry(shell, _historyOffset2EntryIndex(shell, rdata, offset));
|
535 |
} |
536 |
|
537 |
/**
|
538 |
* @brief Retrieve a pointer to the current entry string in the input buffer.
|
539 |
*
|
540 |
* @param[in] shell Pointer to a shell object.
|
541 |
* @param[in] rdata Pointer to a runtime data object.
|
542 |
*
|
543 |
* @return Pointer to the string of the current entry in the input buffer.
|
544 |
*/
|
545 |
static inline char* _getCurrentEntry(const aos_shell_t* shell, const runtimedata_t* rdata) |
546 |
{ |
547 |
aosDbgCheck(shell != NULL);
|
548 |
aosDbgCheck(rdata != NULL);
|
549 |
|
550 |
return _getAbsoluteEntry(shell, rdata->buffer.current);
|
551 |
} |
552 |
|
553 |
/**
|
554 |
* @brief Retrieve a pointer to the currently selected entry.
|
555 |
*
|
556 |
* @param[in] shell Pointer to a shell object.
|
557 |
* @param[in] rdata Pointer to a runtime data object.
|
558 |
*
|
559 |
* @return Pointer to the currently selected entry or NULL if no entry is selected (cleared preview).
|
560 |
*/
|
561 |
static inline char* _getSelectedEntry(const aos_shell_t* shell, const runtimedata_t* rdata) |
562 |
{ |
563 |
aosDbgCheck(shell != NULL);
|
564 |
aosDbgCheck(rdata != NULL);
|
565 |
|
566 |
if (rdata->buffer.selected > 0) { |
567 |
return _getRelativeEntry(shell, rdata, rdata->buffer.selected - 1); |
568 |
} else {
|
569 |
return NULL; |
570 |
} |
571 |
} |
572 |
|
573 |
/**
|
574 |
* @brief Retrieve the currently visualized entry.
|
575 |
*
|
576 |
* @param[in] shell Pointer to a shell object.
|
577 |
* @param[in] rdata Pointer to a runtime data object.
|
578 |
*
|
579 |
* @return Pointer to the currently visualized entry or NULL if the input has been cleared (cleared preview).
|
580 |
*/
|
581 |
static inline char* _getVisualisedEntry(const aos_shell_t* shell, const runtimedata_t* rdata) |
582 |
{ |
583 |
aosDbgCheck(shell != NULL);
|
584 |
aosDbgCheck(rdata != NULL);
|
585 |
|
586 |
if (rdata->buffer.selected == 0) { |
587 |
// cleared preview, nothing visualized
|
588 |
return NULL; |
589 |
} else {
|
590 |
if (rdata->buffer.selected == 1 || rdata->buffer.selected == rdata->buffer.edited) { |
591 |
// the current or a modified entry is selected
|
592 |
return _getCurrentEntry(shell, rdata);
|
593 |
} else {
|
594 |
// a historic, unmodified entry is selected
|
595 |
return _getRelativeEntry(shell, rdata, rdata->buffer.selected - 1); |
596 |
} |
597 |
} |
598 |
} |
599 |
|
600 |
/**
|
601 |
* @brief Print the shell prompt
|
602 |
* @details Depending on the configuration flags, the system uptime is printed before the prompt string.
|
603 |
*
|
604 |
* @param[in] shell Pointer to the shell object.
|
605 |
*/
|
606 |
static void _printPrompt(aos_shell_t* shell) |
607 |
{ |
608 |
aosDbgCheck(shell != NULL);
|
609 |
|
610 |
// print some time informattion before prompt if configured
|
611 |
if (shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) {
|
612 |
// printf the system uptime
|
613 |
if ((shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) == AOS_SHELL_CONFIG_PROMPT_UPTIME) {
|
614 |
// get current system uptime
|
615 |
aos_timestamp_t uptime; |
616 |
aosSysGetUptime(&uptime); |
617 |
|
618 |
chprintf((BaseSequentialStream*)&shell->stream, "[%01u:%02u:%02u:%02u:%03u:%03u] ",
|
619 |
(uint32_t)(uptime / MICROSECONDS_PER_DAY), |
620 |
(uint8_t)(uptime % MICROSECONDS_PER_DAY / MICROSECONDS_PER_HOUR), |
621 |
(uint8_t)(uptime % MICROSECONDS_PER_HOUR / MICROSECONDS_PER_MINUTE), |
622 |
(uint8_t)(uptime % MICROSECONDS_PER_MINUTE / MICROSECONDS_PER_SECOND), |
623 |
(uint16_t)(uptime % MICROSECONDS_PER_SECOND / MICROSECONDS_PER_MILLISECOND), |
624 |
(uint16_t)(uptime % MICROSECONDS_PER_MILLISECOND / MICROSECONDS_PER_MICROSECOND)); |
625 |
} |
626 |
#if (HAL_USE_RTC == TRUE)
|
627 |
else if ((shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) == AOS_SHELL_CONFIG_PROMPT_DATETIME) { |
628 |
// get current RTC time
|
629 |
struct tm dt;
|
630 |
aosSysGetDateTime(&dt); |
631 |
chprintf((BaseSequentialStream*)&shell->stream, "[%02u-%02u-%04u|%02u:%02u:%02u] ",
|
632 |
dt.tm_mday, |
633 |
dt.tm_mon + 1,
|
634 |
dt.tm_year + 1900,
|
635 |
dt.tm_hour, |
636 |
dt.tm_min, |
637 |
dt.tm_sec); |
638 |
} |
639 |
#endif /* (HAL_USE_RTC == TRUE) */ |
640 |
else {
|
641 |
aosDbgAssert(false);
|
642 |
} |
643 |
} |
644 |
|
645 |
// print the actual prompt string
|
646 |
if (shell->prompt && !(shell->config & AOS_SHELL_CONFIG_PROMPT_MINIMAL)) {
|
647 |
chprintf((BaseSequentialStream*)&shell->stream, "%s$ ", shell->prompt);
|
648 |
} else {
|
649 |
chprintf((BaseSequentialStream*)&shell->stream, "%>$ ");
|
650 |
} |
651 |
|
652 |
return;
|
653 |
} |
654 |
|
655 |
/**
|
656 |
* @brief Interprete a escape sequence
|
657 |
* @details This function interpretes escape sequences (starting with ASCII
|
658 |
* "Escape" character 0x1B) according to the VT100 / VT52 ANSI escape
|
659 |
* sequence definitions.
|
660 |
* @note Only the most important escape sequences are implemented yet.
|
661 |
*
|
662 |
* @param[in] seq Character sequence to interprete.
|
663 |
* Must be terminated by NUL byte.
|
664 |
*
|
665 |
* @return A @p special_key value.
|
666 |
*/
|
667 |
static special_key_t _interpreteEscapeSequence(const char seq[]) |
668 |
{ |
669 |
// local variables
|
670 |
unsigned long strl = 0; |
671 |
const unsigned long seql = strlen(seq); |
672 |
bool ambiguous = false; |
673 |
|
674 |
// TAB
|
675 |
/* not supported yet; use '\x09' instead */
|
676 |
|
677 |
// BACKSPACE
|
678 |
/* not supported yet; use '\x08' instead */
|
679 |
|
680 |
// ESCAPE
|
681 |
/* not supported yes; use '\x1B' instead */
|
682 |
|
683 |
// CTRL + C
|
684 |
/* not defined yet; use '\x03' instead */
|
685 |
|
686 |
// INSERT
|
687 |
if (strncmp(seq, KEYSTRING_INSERT, seql) == 0) { |
688 |
strl = strlen(KEYSTRING_INSERT); |
689 |
if (seql == strl) {
|
690 |
return KEY_INSERT;
|
691 |
} else if (seql < strl) { |
692 |
ambiguous = true;
|
693 |
} |
694 |
} |
695 |
|
696 |
// DELETE
|
697 |
if (strncmp(seq, KEYSTRING_DELETE, seql) == 0) { |
698 |
strl = strlen(KEYSTRING_DELETE); |
699 |
if (seql == strl) {
|
700 |
return KEY_DELETE;
|
701 |
} else if (seql < strl) { |
702 |
ambiguous = true;
|
703 |
} |
704 |
} |
705 |
|
706 |
// HOME
|
707 |
if (strncmp(seq, KEYSTRING_HOME, seql) == 0) { |
708 |
strl = strlen(KEYSTRING_HOME); |
709 |
if (seql == strl) {
|
710 |
return KEY_HOME;
|
711 |
} else if (seql < strl) { |
712 |
ambiguous = true;
|
713 |
} |
714 |
} |
715 |
|
716 |
// END
|
717 |
if (strncmp(seq, KEYSTRING_END, seql) == 0) { |
718 |
strl = strlen(KEYSTRING_END); |
719 |
if (seql == strl) {
|
720 |
return KEY_END;
|
721 |
} else if (seql < strl) { |
722 |
ambiguous = true;
|
723 |
} |
724 |
} |
725 |
|
726 |
// PAGE UP
|
727 |
if (strncmp(seq, KEYSTRING_PAGEUP, seql) == 0) { |
728 |
strl = strlen(KEYSTRING_PAGEUP); |
729 |
if (seql == strl) {
|
730 |
return KEY_PAGEUP;
|
731 |
} else if (seql < strl) { |
732 |
ambiguous = true;
|
733 |
} |
734 |
} |
735 |
|
736 |
// PAGE DOWN
|
737 |
if (strncmp(seq, KEYSTRING_PAGEDOWN, seql) == 0) { |
738 |
strl = strlen(KEYSTRING_PAGEDOWN); |
739 |
if (seql == strl) {
|
740 |
return KEY_PAGEDOWN;
|
741 |
} else if (seql < strl) { |
742 |
ambiguous = true;
|
743 |
} |
744 |
} |
745 |
|
746 |