Simple embedded shell with runtime pluggable commands.

Dependents:   DataBus2018

Implements a simple unix-like shell for embedded systems with a pluggable command architecture.

Revision:
12:ecf3fc049bca
Parent:
11:23f61057d877
Child:
13:a29fb89018e1
--- a/SimpleShell.cpp	Thu Dec 13 09:00:28 2018 +0000
+++ b/SimpleShell.cpp	Thu Dec 13 09:19:51 2018 +0000
@@ -1,7 +1,18 @@
 #include "SimpleShell.h"
 #include <ctype.h>
 
-#define ESC 0x1b
+#define ESC     0x1b
+#define UP      0x41
+#define DOWN    0x42
+#define RIGHT   0x43
+#define LEFT    0x44
+#define HOME    0x31
+#define INS     0x32
+#define DEL     0x33
+#define PGUP    0x35
+#define PGDN    0x36
+#define TAIL    0x7e
+
 
 SimpleShell::SimpleShell()
 {
@@ -26,7 +37,7 @@
 {
     bool done=false;
     Callback<void()> cb;
-    int status; // TODO implement command status return
+    //int status; // TODO implement command status return
     
     strcpy(_cwd, "/log");
 
@@ -99,56 +110,38 @@
         if (c == '\r') { // if return is hit, we're done, don't add \r to cmd
             done = true;
         } else if (c == ESC) { // keyboard escape codes (arrow keys, etc)
-            char c2 = getchar();
-            char c3 = getchar();
+            int c2 = getchar();
+            int c3 = getchar();
 
             if (c2 == 0x4f && c3 == 0x46) {
                 printf("<END>");
             } else if (c2 == 0x5b) {
-                switch (c3) {
-                case 0x41 : // up
+
+                if (c3 == UP) {
                     printf("<UP>");
-                    break;
-                case 0x42 : // down
+                } else if (c3 == DOWN) {
                     printf("<DOWN>");
-                    break;
-                case 0x43 : // right
+                } else if (c3 == RIGHT) {
                     printf("<RIGHT>");
-                    break;
-                case 0x44 : // left
+                } else if (c3 == LEFT) {
                     printf("<LEFT>");
-                    break;
-                case 0x31 : // home
-                case 0x32 : // ins
-                case 0x33: // del
-                case 0x35: // pgup
-                case 0x36: // pgdn
+                } else if (c3 == HOME || c3 == INS || c3 == DEL ||
+                           c3 == PGUP || c3 == PGDN) {
                     char c4 = getchar();
-                    if (c4 == 0x7e) {
-                        switch (c3) {
-                        case 0x31 :
+                    if (c4 == TAIL) {
+                        if (c4 == HOME) {
                             printf("<HOME>");
-                            break;
-                        case 0x32 : // ins
+                        } else if (c4 == INS) {
                             printf("<INS>");
-                            break;
-                        case 0x33: // del
+                        } else if (c4 == DEL) {
                             printf("<DEL>");
-                            break;                
-                        case 0x35: // pgup
+                        } else if (c4 == PGUP) {
                             printf("<PGUP>");
-                            break;
-                        case 0x36: // pgdn
+                        } else if (c4 == PGDN) {
                             printf("<PGDN>");
-                            break;
-                        default:
-                            break;
-                        }//switch
+                        }//if
                     }//if
-                default:
-                    //printf("<0x%02x>", c3);
-                    break;
-                }//switch
+                }//if
             }//if            
             //printf("\n");
         } else if (i < MAXBUF-1) {