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