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 22 20:27:54 2018 +0000
Parent:
18:2b5ed529ab37
Child:
20:53f0b5dc30f9
Commit message:
Added rm, touch

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	Fri Dec 21 20:02:56 2018 +0000
+++ b/SimpleShell.cpp	Sat Dec 22 20:27:54 2018 +0000
@@ -37,6 +37,8 @@
     attach(callback(this, &SimpleShell::pwd), "pwd");
     attach(callback(this, &SimpleShell::cat), "cat");
     attach(callback(this, &SimpleShell::cd), "cd");
+    attach(callback(this, &SimpleShell::rm), "rm");
+    attach(callback(this, &SimpleShell::touch), "touch");
     attach(callback(this, &SimpleShell::ls), "ls");
 }
 
@@ -107,6 +109,39 @@
     return;
 }
 
+
+void SimpleShell::rm(int argc, char **argv)
+{
+    if (argc >= 2) {
+        for (int i=1; i < argc; i++) {
+            if (remove(canon(argv[i]))) {
+                printf("%s: cannot remove\n", argv[i]);
+            }
+        }
+    } else {
+        puts("usage: rm [file1 [file2 ...]]");
+    }
+}
+
+
+void SimpleShell::touch(int argc, char **argv)
+{
+    FILE *fp;
+
+    if (argc >= 2) {
+        for (int i=1; i < argc; i++) {
+            if ((fp = fopen(canon(argv[i]), "w")) != NULL) {
+                fclose(fp);
+            } else {
+                printf("%s: cannot touch\n", argv[1]);
+            }
+        }
+    } else {
+        puts("usage: touch [file1 [file2 ...]]");
+    }
+}
+
+
 void SimpleShell::cat(int argc, char **argv)
 {
     FILE *fp;
--- a/SimpleShell.h	Fri Dec 21 20:02:56 2018 +0000
+++ b/SimpleShell.h	Sat Dec 22 20:27:54 2018 +0000
@@ -79,6 +79,12 @@
 
     /// Built-in shell command to list files in directory
     void ls(int argc, char **argv);
+    
+    /// Built-in shell command to remove a file
+    void rm(int argc, char **argv);
+    
+    /// Built-in shell command to create a file
+    void touch(int argc, char **argv);
 
     /// Built-in shell command to display contents of file
     void cat(int argc, char **argv);