Simple embedded shell with runtime pluggable commands.

Dependents:   DataBus2018

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

Revision:
24:004e33abce37
Parent:
23:b1e49cfcaef6
Child:
28:753db82debb1
--- a/SimpleShell.cpp	Mon Dec 24 18:35:29 2018 +0000
+++ b/SimpleShell.cpp	Mon Dec 24 18:52:57 2018 +0000
@@ -29,7 +29,17 @@
 
 
 char *SimpleShell::basename(char *path) {
-    return path;
+    char *result = path + strlen(path);
+    
+    while (result != path) {
+        if (*result == '/') {
+            result++;
+            break;
+        }
+        result--;
+    }
+    
+    return result;
 }
 
 
@@ -182,13 +192,37 @@
     const char EOT=0x04;
     const char ACK=0x07;
     const char NACK=0x18;
+    char buf[1024];
     FILE *fp;
+    char c;
 
     int i = 1;
     if (argc >= 2) {
         if ((fp = fopen(canon(argv[i]), "r")) == 0) {
             printf("%s: can't open file\n", basename(argv[i]));
         } else {
+            puts("Ready; initiate receive on client.");
+            c = getchar();
+            if (c == SOF) {
+                puts(basename(argv[i]));
+                c = getchar();
+                if (c == ACK) {
+                    while (!feof(fp)) {
+                        fgets(buf, 1024, fp);
+                        fputs(buf, stdout);
+                    }
+                    putchar(EOT);
+                } else if (c == NACK) {
+                    puts("client error.");
+                } else {
+                    puts("ACK/NACK expected");
+                }
+            } else if (c == NACK) {
+                puts("server cancelled.");
+            } else {
+                puts("SOF/NACK expected.");
+            }
+            fclose(fp);
         }
     } else {
         puts("usage: send file1 [file2 ...]");