Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_shell.c @ 00843891

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