Simple embedded shell with runtime pluggable commands.

Dependents:   DataBus2018

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

Files at this revision

API Documentation at this revision

Comitter:
shimniok
Date:
Sat Dec 01 23:18:03 2018 +0000
Child:
1:998a7ed04f10
Commit message:
Initial functionality working

Changed in this revision

SimpleShell.cpp Show annotated file Show diff for this revision Revisions of this file
SimpleShell.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SimpleShell.cpp	Sat Dec 01 23:18:03 2018 +0000
@@ -0,0 +1,60 @@
+#include "SimpleShell.h"
+
+SimpleShell::SimpleShell()
+{
+}
+
+void SimpleShell::run()
+{
+    bool done=false;
+    strcpy(_cwd, "/log");
+
+    printf("Type help for assistance.\n");
+    while (!done) {
+        printPrompt();
+        readCommand();
+        //this.doCommand(cmdline);
+    }
+    puts("exiting shell\n");
+
+    return;
+}
+
+
+void SimpleShell::printPrompt()
+{
+    fputc('(', stdout);
+    fputs(_cwd, stdout);
+    fputs(")# ", stdout);
+}
+
+
+void SimpleShell::readCommand()
+{
+    int i=0;
+    char c;
+    bool done = false;
+    
+    memset(cmd, 0, MAXBUF);
+    do {
+        cmd[i] = 0;
+        c = fgetc(stdin);
+        if (c == '\r') { // if return is hit, we're done, don't add \r to cmd
+            done = true;
+        } else if (i < MAXBUF-1) {
+            if (c == 0x7f || c == '\b') { // backspace or delete
+                if (i > 0) { // if we're at the beginning, do nothing
+                    i--;
+                    fputs("\b \b", stdout);
+
+                }
+            } else {
+                fputc(c, stdout);
+                cmd[i++] = c;
+            }
+        }
+    } while (!done);
+    fputc('\n', stdout);
+
+
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SimpleShell.h	Sat Dec 01 23:18:03 2018 +0000
@@ -0,0 +1,19 @@
+#include "mbed.h"
+
+class SimpleShell {
+public:  
+    SimpleShell();
+
+    void attach(Callback<void()> func, char *command) {
+    }
+    
+//    void registerCommand(char *commandString, char *helpText, Callback<void(float)> cb);
+    void run();
+    
+private:
+    static const int MAXBUF=128;
+    void printPrompt(void);
+    void readCommand();
+    char cmd[MAXBUF];
+    char _cwd[MAXBUF];
+}; // class
\ No newline at end of file