Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (46.811 KB)

1
/*
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
/**
20
 * @file    aos_shell.c
21
 * @brief   Shell code.
22
 * @details Shell code as well as shell related channels and streams.
23
 *
24
 * @addtogroup aos_shell
25
 * @{
26
 */
27

    
28
#include <aos_shell.h>
29

    
30
#if (AMIROOS_CFG_SHELL_ENABLE == true)
31
#include <aos_debug.h>
32
#include <aos_time.h>
33
#include <aos_system.h>
34
#include <string.h>
35

    
36
/**
37
 * @brief   Event mask to be set on OS related events.
38
 */
39
#define AOS_SHELL_EVENTMASK_OS                  EVENT_MASK(0)
40

    
41
/**
42
 * @brief   Event mask to be set on a input event.
43
 */
44
#define AOS_SHELL_EVENTMASK_INPUT               EVENT_MASK(1)
45

    
46
/**
47
 * @brief   Implementation of the BaseAsynchronous write() method (inherited from BaseSequentialStream).
48
 */
49
static size_t _channelwrite(void *instance, const uint8_t *bp, size_t n)
50
{
51
  if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
52
    return streamWrite(((AosShellChannel*)instance)->asyncchannel, bp, n);
53
  } else {
54
    return 0;
55
  }
56
}
57

    
58
/**
59
 * @brief   Implementation of the BaseAsynchronous read() method (inherited from BaseSequentialStream).
60
 */
61
static size_t _channelread(void *instance, uint8_t *bp, size_t n)
62
{
63
  if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
64
    return streamRead(((AosShellChannel*)instance)->asyncchannel, bp, n);
65
  } else {
66
    return 0;
67
  }
68
}
69

    
70
/**
71
 * @brief   Implementation of the BaseAsynchronous put() method (inherited from BaseSequentialStream).
72
 */
73
static msg_t _channelput(void *instance, uint8_t b)
74
{
75
  if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
76
    return streamPut(((AosShellChannel*)instance)->asyncchannel, b);
77
  } else {
78
    return MSG_RESET;
79
  }
80
}
81

    
82
/**
83
 * @brief   Implementation of the BaseAsynchronous get() method (inherited from BaseSequentialStream).
84
 */
85
static msg_t _channelget(void *instance)
86
{
87
  if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
88
    return streamGet(((AosShellChannel*)instance)->asyncchannel);
89
  } else {
90
    return MSG_RESET;
91
  }
92
}
93

    
94
/**
95
 * @brief   Implementation of the BaseAsynchronous putt() method.
96
 */
97
static msg_t _channelputt(void *instance, uint8_t b, sysinterval_t time)
98
{
99
  if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
100
    return chnPutTimeout(((AosShellChannel*)instance)->asyncchannel, b, time);
101
  } else {
102
    return MSG_RESET;
103
  }
104
}
105

    
106
/**
107
 * @brief   Implementation of the BaseAsynchronous gett() method.
108
 */
109
static msg_t _channelgett(void *instance, sysinterval_t time)
110
{
111
  if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
112
    return chnGetTimeout(((AosShellChannel*)instance)->asyncchannel, time);
113
  } else {
114
    return MSG_RESET;
115
  }
116
}
117

    
118
/**
119
 * @brief   Implementation of the BaseAsynchronous writet() method.
120
 */
121
static size_t _channelwritet(void *instance, const uint8_t *bp, size_t n, sysinterval_t time)
122
{
123
  if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
124
    return chnWriteTimeout(((AosShellChannel*)instance)->asyncchannel, bp, n, time);
125
  } else {
126
    return 0;
127
  }
128
}
129

    
130
/**
131
 * @brief   Implementation of the BaseAsynchronous readt() method.
132
 */
133
static size_t _channelreadt(void *instance, uint8_t *bp, size_t n, sysinterval_t time)
134
{
135
  if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
136
    return chnReadTimeout(((AosShellChannel*)instance)->asyncchannel, bp, n, time);
137
  } else {
138
    return 0;
139
  }
140
}
141

    
142
/**
143
 * @brief   Implementation of the BaseAsynchronousChannel ctl() method.
144
 */
145
static msg_t _channelctl(void *instance, unsigned int operation, void *arg) {
146
  (void) instance;
147

    
148
  switch (operation) {
149
  case CHN_CTL_NOP:
150
    osalDbgCheck(arg == NULL);
151
    break;
152
  case CHN_CTL_INVALID:
153
    osalDbgAssert(false, "invalid CTL operation");
154
    break;
155
  default:
156
    break;
157
  }
158
  return MSG_OK;
159
}
160

    
161
static const struct AosShellChannelVMT _channelvmt = {
162
  (size_t) 0,
163
  _channelwrite,
164
  _channelread,
165
  _channelput,
166
  _channelget,
167
  _channelputt,
168
  _channelgett,
169
  _channelwritet,
170
  _channelreadt,
171
  _channelctl,
172
};
173

    
174
static size_t _streamwrite(void *instance, const uint8_t *bp, size_t n)
175
{
176
  aosDbgCheck(instance != NULL);
177

    
178
  // local variables
179
  AosShellChannel* channel = ((AosShellStream*)instance)->channel;
180
  size_t bytes;
181
  size_t maxbytes = 0;
182

    
183
  // iterate through the list of channels
184
  while (channel != NULL) {
185
    bytes = streamWrite(channel, bp, n);
186
    maxbytes = (bytes > maxbytes) ? bytes : maxbytes;
187
    channel = channel->next;
188
  }
189

    
190
  return maxbytes;
191
}
192

    
193
static size_t _stremread(void *instance, uint8_t *bp, size_t n)
194
{
195
  (void)instance;
196
  (void)bp;
197
  (void)n;
198

    
199
  return 0;
200
}
201

    
202
static msg_t _streamput(void *instance, uint8_t b)
203
{
204
  aosDbgCheck(instance != NULL);
205

    
206
  // local variables
207
  AosShellChannel* channel = ((AosShellStream*)instance)->channel;
208
  msg_t ret = MSG_OK;
209

    
210
  // iterate through the list of channels
211
  while (channel != NULL) {
212
    msg_t ret_ = streamPut(channel, b);
213
    ret = (ret_ < ret) ? ret_ : ret;
214
    channel = channel->next;
215
  }
216

    
217
  return ret;
218
}
219

    
220
static msg_t _streamget(void *instance)
221
{
222
  (void)instance;
223

    
224
  return 0;
225
}
226

    
227
static const struct AosShellStreamVMT _streamvmt = {
228
  (size_t) 0,
229
  _streamwrite,
230
  _stremread,
231
  _streamput,
232
  _streamget,
233
};
234

    
235
/**
236
 * @brief   Enumerator of special keyboard keys.
237
 */
238
typedef enum special_key {
239
  KEY_UNKNOWN,      /**< any/unknow key */
240
  KEY_AMBIGUOUS,    /**< key is ambiguous */
241
  KEY_TAB,          /**< tabulator key */
242
  KEY_ESCAPE,       /**< escape key */
243
  KEY_BACKSPACE,    /**< backspace key */
244
  KEY_INSERT,       /**< insert key */
245
  KEY_DELETE,       /**< delete key */
246
  KEY_HOME,         /**< home key */
247
  KEY_END,          /**< end key */
248
  KEY_PAGE_UP,      /**< page up key */
249
  KEY_PAGE_DOWN,    /**< page down key */
250
  KEY_ARROW_UP,     /**< arrow up key */
251
  KEY_ARROW_DOWN,   /**< arrow down key */
252
  KEY_ARROW_LEFT,   /**< arrow left key */
253
  KEY_ARROW_RIGHT,  /**< arrow right key */
254
} special_key_t;
255

    
256
/**
257
 * @brief   Enumerator for case (in)sensitive character matching.
258
 */
259
typedef enum charmatch {
260
  CHAR_MATCH_NOT    = 0,  /**< Characters do not match at all. */
261
  CHAR_MATCH_NCASE  = 1,  /**< Characters would match case insensitive. */
262
  CHAR_MATCH_CASE   = 2,  /**< Characters do match with case. */
263
} charmatch_t;
264

    
265
/**
266
 * @brief   Print the shell prompt
267
 * @details Depending on the configuration flags, the system uptime is printed before the prompt string.
268
 *
269
 * @param[in] shell   Pointer to the shell object.
270
 */
271
static void _printPrompt(aos_shell_t* shell)
272
{
273
  aosDbgCheck(shell != NULL);
274

    
275
  // print some time informattion before prompt if configured
276
  if (shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) {
277
    // printf the system uptime
278
    if ((shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) == AOS_SHELL_CONFIG_PROMPT_UPTIME) {
279
      // get current system uptime
280
      aos_timestamp_t uptime;
281
      aosSysGetUptime(&uptime);
282

    
283
      chprintf((BaseSequentialStream*)&shell->stream, "[%01u:%02u:%02u:%02u:%03u:%03u] ",
284
               (uint32_t)(uptime / MICROSECONDS_PER_DAY),
285
               (uint8_t)(uptime % MICROSECONDS_PER_DAY / MICROSECONDS_PER_HOUR),
286
               (uint8_t)(uptime % MICROSECONDS_PER_HOUR / MICROSECONDS_PER_MINUTE),
287
               (uint8_t)(uptime % MICROSECONDS_PER_MINUTE / MICROSECONDS_PER_SECOND),
288
               (uint16_t)(uptime % MICROSECONDS_PER_SECOND / MICROSECONDS_PER_MILLISECOND),
289
               (uint16_t)(uptime % MICROSECONDS_PER_MILLISECOND / MICROSECONDS_PER_MICROSECOND));
290
    }
291
    else if ((shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) == AOS_SHELL_CONFIG_PROMPT_DATETIME) {
292
      // get current RTC time
293
      struct tm dt;
294
      aosSysGetDateTime(&dt);
295
      chprintf((BaseSequentialStream*)&shell->stream, "[%02u-%02u-%04u|%02u:%02u:%02u] ",
296
               dt.tm_mday,
297
               dt.tm_mon + 1,
298
               dt.tm_year + 1900,
299
               dt.tm_hour,
300
               dt.tm_min,
301
               dt.tm_sec);
302
    }
303
    else {
304
      aosDbgAssert(false);
305
    }
306
  }
307

    
308
  // print the actual prompt string
309
  if (shell->prompt && !(shell->config & AOS_SHELL_CONFIG_PROMPT_MINIMAL)) {
310
    chprintf((BaseSequentialStream*)&shell->stream, "%s$ ", shell->prompt);
311
  } else {
312
    chprintf((BaseSequentialStream*)&shell->stream, "%>$ ");
313
  }
314

    
315
  return;
316
}
317

    
318
/**
319
 * @brief   Interprete a escape sequence
320
 *
321
 * @param[in] seq   Character sequence to interprete.
322
 *                  Must be terminated by NUL byte.
323
 *
324
 * @return          A @p special_key value.
325
 */
326
static special_key_t _interpreteEscapeSequence(const char seq[])
327
{
328
  // local variables
329
  bool ambiguous = false;
330
  int cmp = 0;
331

    
332
  // TAB
333
  /* not supported yet; use "\x09" instead */
334

    
335
  // BACKSPACE
336
  /* not supported yet; use "\x08" instead */
337

    
338
  // ESCAPE
339
  cmp = strcmp(seq, "\x1B");
340
  if (cmp == 0) {
341
    return KEY_ESCAPE;
342
  } else {
343
    ambiguous |= (cmp < 0);
344
  }
345

    
346
  // INSERT
347
  cmp = strcmp(seq, "\x1B\x5B\x32\x7E");
348
  if (cmp == 0) {
349
    return KEY_INSERT;
350
  } else {
351
    ambiguous |= (cmp < 0);
352
  }
353

    
354
  // DELETE
355
  cmp = strcmp(seq, "\x1B\x5B\x33\x7E");
356
  if (cmp == 0) {
357
    return KEY_DELETE;
358
  } else {
359
    ambiguous |= (cmp < 0);
360
  }
361

    
362
  // HOME
363
  cmp = strcmp(seq, "\x1B\x4F\x48");
364
  if (cmp == 0) {
365
    return KEY_HOME;
366
  } else {
367
    ambiguous |= (cmp < 0);
368
  }
369

    
370
  // END
371
  cmp = strcmp(seq, "\x1B\x4F\x46");
372
  if (cmp == 0) {
373
    return KEY_END;
374
  } else {
375
    ambiguous |= (cmp < 0);
376
  }
377

    
378
  // PAGE UP
379
  cmp = strcmp(seq, "\x1B\x5B\x35\x7E");
380
  if (cmp == 0) {
381
    return KEY_PAGE_UP;
382
  } else {
383
    ambiguous |= (cmp < 0);
384
  }
385

    
386
  // PAGE DOWN
387
  cmp = strcmp(seq, "\x1B\x5B\x36\x7E");
388
  if (cmp == 0) {
389
    return KEY_PAGE_DOWN;
390
  } else {
391
    ambiguous |= (cmp < 0);
392
  }
393

    
394
  // ARROW UP
395
  cmp = strcmp(seq, "\x1B\x5B\x41");
396
  if (cmp == 0) {
397
    return KEY_ARROW_UP;
398
  } else {
399
    ambiguous |= (cmp < 0);
400
  }
401

    
402
  // ARROW DOWN
403
  cmp = strcmp(seq, "\x1B\x5B\x42");
404
  if (cmp == 0) {
405
    return KEY_ARROW_DOWN;
406
  } else {
407
    ambiguous |= (cmp < 0);
408
  }
409

    
410
  // ARROW LEFT
411
  cmp = strcmp(seq, "\x1B\x5B\x44");
412
  if (cmp == 0) {
413
    return KEY_ARROW_LEFT;
414
  } else {
415
    ambiguous |= (cmp < 0);
416
  }
417

    
418
  // ARROW RIGHT
419
  cmp = strcmp(seq, "\x1B\x5B\x43");
420
  if (cmp == 0) {
421
    return KEY_ARROW_RIGHT;
422
  } else {
423
    ambiguous |= (cmp < 0);
424
  }
425

    
426
  return ambiguous ? KEY_AMBIGUOUS : KEY_UNKNOWN;
427
}
428

    
429
/**
430
 * @brief   Move the cursor in the terminal
431
 *
432
 * @param[in] shell   Pointer to the shell object.
433
 * @param[in] from    Starting position of the cursor.
434
 * @param[in] to      Target position to move the cursor to.
435
 *
436
 * @return            The number of positions moved.
437
 */
438
static int _moveCursor(aos_shell_t* shell, const size_t from, const size_t to)
439
{
440
  aosDbgCheck(shell != NULL);
441

    
442
  // local variables
443
  size_t pos = from;
444

    
445
  // move cursor left by printing backspaces
446
  while (pos > to) {
447
    streamPut(&shell->stream, '\b');
448
    --pos;
449
  }
450

    
451
  // move cursor right by printing line content
452
  while (pos < to) {
453
    streamPut(&shell->stream, shell->line[pos]);
454
    ++pos;
455
  }
456

    
457
  return (int)pos - (int)from;
458
}
459

    
460
/**
461
 * @brief   Print content of the shell line
462
 *
463
 * @param[in] shell   Pointer to the shell object.
464
 * @param[in] from    First position to start printing from.
465
 * @param[in] to      Position after the last character to print.
466
 *
467
 * @return            Number of characters printed.
468
 */
469
static inline size_t _printLine(aos_shell_t* shell, const size_t from, const size_t to)
470
{
471
  aosDbgCheck(shell != NULL);
472

    
473
  // local variables
474
  size_t cnt;
475

    
476
  for (cnt = 0; from + cnt < to; ++cnt) {
477
    streamPut(&shell->stream, shell->line[from + cnt]);
478
  }
479

    
480
  return cnt;
481
}
482

    
483
/**
484
 * @brief   Compare two characters.
485
 *
486
 * @param[in] lhs       First character to compare.
487
 * @param[in] rhs       Second character to compare.
488
 *
489
 * @return              How well the characters match.
490
 */
491
static inline charmatch_t _charcmp(char lhs, char rhs)
492
{
493
  // if lhs is a upper case letter and rhs is a lower case letter
494
  if (lhs >= 'A' && lhs <= 'Z' && rhs >= 'a' && rhs <= 'z') {
495
    return (lhs == (rhs - 'a' + 'A')) ? CHAR_MATCH_NCASE : CHAR_MATCH_NOT;
496
  }
497
  // if lhs is a lower case letter and rhs is a upper case letter
498
  else if (lhs >= 'a' && lhs <= 'z' && rhs >= 'A' && rhs <= 'Z') {
499
    return ((lhs - 'a' + 'A') == rhs) ? CHAR_MATCH_NCASE : CHAR_MATCH_NOT;
500
  }
501
  // default
502
  else {
503
    return (lhs == rhs) ? CHAR_MATCH_CASE : CHAR_MATCH_NOT;
504
  }
505
}
506

    
507
/**
508
 * @brief   Maps an character from ASCII to a modified custom encoding.
509
 * @details The custom character encoding is very similar to ASCII and has the following structure:
510
 *          0x00=NULL ... 0x40='@' (identically to ASCII)
511
 *          0x4A='a'; 0x4B='A'; 0x4C='b'; 0x4D='B' ... 0x73='z'; 0x74='Z' (custom letter order)
512
 *          0x75='[' ... 0x7A='`' (0x5B..0x60 is ASCII)
513
 *          0x7B='{' ... 0x7F=DEL (identically to ASCII)
514
 *
515
 * @param[in] c   Character to map to the custom encoding.
516
 *
517
 * @return    The customly encoded character.
518
 */
519
static inline char _mapAscii2Custom(const char c)
520
{
521
  if (c >= 'A' && c <= 'Z') {
522
    return ((c - 'A') * 2) + 'A' + 1;
523
  } else if (c > 'Z' && c < 'a') {
524
    return c + ('z' - 'a') + 1;
525
  } else if (c >= 'a' && c <= 'z') {
526
    return ((c - 'a') * 2) + 'A';
527
  } else {
528
    return c;
529
  }
530
}
531

    
532
/**
533
 * @brief   Compares two strings wrt letter case.
534
 * @details Comparisson uses a custom character encoding or mapping.
535
 *          See @p _mapAscii2Custom for details.
536
 *
537
 * @param[in] str1    First string to compare.
538
 * @param[in] str2    Second string to compare.
539
 * @param[in] cs      Flag indicating whether comparison shall be case sensitive.
540
 * @param[in,out] n   Maximum number of character to compare (in) and number of matching characters (out).
541
 *                    If a null pointer is specified, this parameter is ignored.
542
 *                    If the value pointed to is zero, comarison will not be limited.
543
 * @param[out] m      Optional indicator whether there was at least one case mismatch.
544
 *
545
 * @return      Integer value indicating the relationship between the strings.
546
 * @retval <0   The first character that does not match has a lower value in str1 than in str2.
547
 * @retval  0   The contents of both strings are equal.
548
 * @retval >0   The first character that does not match has a greater value in str1 than in str2.
549
 */
550
static int _strccmp(const char *str1, const char *str2, bool cs, size_t* n, charmatch_t* m)
551
{
552
  aosDbgCheck(str1 != NULL);
553
  aosDbgCheck(str2 != NULL);
554

    
555
  // initialize variables
556
  if (m) {
557
    *m = CHAR_MATCH_NOT;
558
  }
559
  size_t i = 0;
560

    
561
  // iterate through the strings
562
  while ((n == NULL) || (*n == 0) || (*n > 0 && i < *n)) {
563
    // break on NUL
564
    if (str1[i] == '\0' || str2[i] == '\0') {
565
      if (n) {
566
        *n = i;
567
      }
568
      break;
569
    }
570
    // compare character
571
    const charmatch_t match = _charcmp(str1[i], str2[i]);
572
    if ((match == CHAR_MATCH_CASE) || (!cs && match == CHAR_MATCH_NCASE)) {
573
      if (m != NULL && *m != CHAR_MATCH_NCASE) {
574
        *m = match;
575
      }
576
      ++i;
577
    } else {
578
      if (n) {
579
        *n = i;
580
      }
581
      break;
582
    }
583
  }
584

    
585
  return _mapAscii2Custom(str1[i]) - _mapAscii2Custom(str2[i]);
586
}
587

    
588
static aos_status_t _readChannel(aos_shell_t* shell, AosShellChannel* channel, size_t* n)
589
{
590
  aosDbgCheck(shell != NULL);
591
  aosDbgCheck(channel != NULL);
592
  aosDbgCheck(n != NULL);
593

    
594
  // local variables
595
  aos_shellaction_t action = AOS_SHELL_ACTION_NONE;
596
  char c;
597
  special_key_t key;
598

    
599
  // initialize output variables
600
  *n = 0;
601

    
602
  // read character by character from the channel
603
  while (chnReadTimeout(channel, (uint8_t*)&c, 1, TIME_IMMEDIATE)) {
604
    key = KEY_UNKNOWN;
605

    
606
    // parse escape sequence
607
    if (shell->inputdata.escp > 0) {
608
      shell->inputdata.escseq[shell->inputdata.escp] = c;
609
      ++shell->inputdata.escp;
610
      key = _interpreteEscapeSequence(shell->inputdata.escseq);
611
      if (key == KEY_AMBIGUOUS) {
612
        // read next byte to resolve ambiguity
613
        continue;
614
      } else {
615
        /*
616
         * If the escape sequence could either be parsed sucessfully
617
         * or there is no match (KEY_UNKNOWN),
618
         * reset the sequence variable and interprete key/character
619
         */
620
        shell->inputdata.escp = 0;
621
        memset(shell->inputdata.escseq, '\0', sizeof(shell->inputdata.escseq)*sizeof(shell->inputdata.escseq[0]));
622
      }
623
    }
624

    
625
    /* interprete keys or character */
626
    {
627
      // default
628
      action = AOS_SHELL_ACTION_NONE;
629

    
630
      // printable character
631
      if (key == KEY_UNKNOWN && c >= '\x20' && c <= '\x7E') {
632
        action = AOS_SHELL_ACTION_READCHAR;
633
      }
634

    
635
      // tab key or character
636
      else if (key == KEY_TAB || c == '\x09') {
637
        /*
638
         * pressing tab once applies auto fill
639
         * pressing tab a second time prints suggestions
640
         */
641
        if (shell->inputdata.lastaction == AOS_SHELL_ACTION_AUTOFILL || shell->inputdata.lastaction == AOS_SHELL_ACTION_SUGGEST) {
642
          action = AOS_SHELL_ACTION_SUGGEST;
643
        } else {
644
          action = AOS_SHELL_ACTION_AUTOFILL;
645
        }
646
      }
647

    
648
      // INS key
649
      else if (key == KEY_INSERT) {
650
        action = AOS_SHELL_ACTION_INSERTTOGGLE;
651
      }
652

    
653
      // DEL key or character
654
      else if (key == KEY_DELETE || c == '\x7F') {
655
        // ignore if cursor is at very right
656
        if (shell->inputdata.cursorpos < shell->inputdata.lineend) {
657
          action = AOS_SHELL_ACTION_DELETEFORWARD;
658
        }
659
      }
660

    
661
      // backspace key or character
662
      else if (key == KEY_BACKSPACE || c == '\x08') {
663
        // ignore if cursor is at very left
664
        if (shell->inputdata.cursorpos > 0) {
665
          action = AOS_SHELL_ACTION_DELETEBACKWARD;
666
        }
667
      }
668

    
669
      // 'page up' of 'arrow up' key
670
      else if (key == KEY_PAGE_UP || key == KEY_ARROW_UP) {
671
        // ignore if there was some input
672
        if (shell->inputdata.noinput) {
673
          action = AOS_SHELL_ACTION_RECALLLAST;
674
        }
675
      }
676

    
677
      // 'page down' key, 'arrow done' key, 'end of test' character or 'end of transmission' character
678
      else if (key == KEY_PAGE_DOWN || key == KEY_ARROW_DOWN || c == '\x03' || c == '\x03') {
679
        // ignore if line is empty
680
        if (shell->inputdata.lineend > 0) {
681
          action = AOS_SHELL_ACTION_CLEAR;
682
        }
683
      }
684

    
685
      // 'home' key
686
      else if (key == KEY_HOME) {
687
        // ignore if cursor is very left
688
        if (shell->inputdata.cursorpos > 0) {
689
          action = AOS_SHELL_ACTION_CURSOR2START;
690
        }
691
      }
692

    
693
      // 'end' key
694
      else if (key == KEY_END) {
695
        // ignore if cursos is very right
696
        if (shell->inputdata.cursorpos < shell->inputdata.lineend) {
697
          action = AOS_SHELL_ACTION_CURSOR2END;
698
        }
699
      }
700

    
701
      // 'arrow left' key
702
      else if (key == KEY_ARROW_LEFT) {
703
        // ignore if cursor is very left
704
        if (shell->inputdata.cursorpos > 0) {
705
          action = AOS_SHELL_ACTION_CURSORLEFT;
706
        }
707
      }
708

    
709
      // 'arrow right' key
710
      else if (key == KEY_ARROW_RIGHT) {
711
        // irgnore if cursor is very right
712
        if (shell->inputdata.cursorpos < shell->inputdata.lineend) {
713
          action = AOS_SHELL_ACTION_CURSORRIGHT;
714
        }
715
      }
716

    
717
      // carriage return ('\r') or line feed ('\n') character
718
      else if (c == '\x0D' || c == '\x0A') {
719
        action = AOS_SHELL_ACTION_EXECUTE;
720
      }
721

    
722
      // ESC key or [ESCAPE] character
723
      else if (key == KEY_ESCAPE || c == '\x1B') {
724
        action = AOS_SHELL_ACTION_ESCSTART;
725
      }
726
    }
727