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