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:
Wed Dec 19 18:42:25 2018 +0000
Parent:
15:242626d8d104
Child:
17:0739cb2f1930
Commit message:
added cat built-in

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
--- a/SimpleShell.cpp	Wed Dec 19 18:25:12 2018 +0000
+++ b/SimpleShell.cpp	Wed Dec 19 18:42:25 2018 +0000
@@ -22,6 +22,7 @@
     // Built-in shell commands
     attach(callback(this, &SimpleShell::help), "help");
     attach(callback(this, &SimpleShell::pwd), "pwd");
+    attach(callback(this, &SimpleShell::cat), "cat");
 }
 
 
@@ -42,6 +43,32 @@
 }
 
 
+void SimpleShell::cat(int argc, char **argv)
+{
+    FILE *fp;
+    //int status=0;
+    char *buf = new char[MAXBUF];
+    
+    for (int i=1; i < argc; i++) {
+        //resolveDirectory(path, arg);
+        if ((fp = fopen(argv[i], "r")) != NULL) {
+            while (!feof(fp)) {
+                fgets(buf, MAXBUF-1, fp);
+                fputs(buf, stdout);
+            }
+            fclose(fp);
+        } else {
+            fputs(argv[i], stdout);
+            fputs(": No such file\n", stdout);
+            //status = 1;
+        }
+    }
+    delete[] buf;
+
+    return;
+}
+
+
 void SimpleShell::run()
 {
     bool done=false;
--- a/SimpleShell.h	Wed Dec 19 18:25:12 2018 +0000
+++ b/SimpleShell.h	Wed Dec 19 18:42:25 2018 +0000
@@ -71,6 +71,9 @@
     /// Built-in shell command to print working directory
     void pwd(int argc, char **argv);
 
+    /// Built-in shell command to display contents of file
+    void cat(int argc, char **argv);
+
     /// Prints command prompt
     void printPrompt(void);