Michael Shimniok / SimpleShell

Dependents:   DataBus2018

Files at this revision

API Documentation at this revision

Comitter:
shimniok
Date:
Wed Dec 19 18:25:12 2018 +0000
Parent:
14:75b5918090ae
Child:
16:f2b9b7b2c71e
Commit message:
finished implementing shell args

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 00:29:05 2018 +0000
+++ b/SimpleShell.cpp	Wed Dec 19 18:25:12 2018 +0000
@@ -25,7 +25,7 @@
 }
 
 
-void SimpleShell::help()
+void SimpleShell::help(int argc, char **argv)
 {
     printf("Available commands: ");
     for (int i=0; i < lookupEnd; i++) {
@@ -35,7 +35,7 @@
 }
 
 
-void SimpleShell::pwd()
+void SimpleShell::pwd(int argc, char **argv)
 {
     puts(_cwd);
     return;
@@ -45,7 +45,7 @@
 void SimpleShell::run()
 {
     bool done=false;
-    Callback<void()> cb;
+    callback_t cb;
     //int status; // TODO implement command status return
     std::string x;
     
@@ -53,13 +53,13 @@
     strncpy(_cwd, "/log", MAXBUF);
 
     printf("Type help for assistance.\n");
-    help();   
+    help(0, NULL);   
     while (!done) {
         printPrompt();
         readCommand();
         if (argv[0]) { // skip blank command
             if (cb = findCommand()) {
-                cb.call();
+                cb.call(argc, argv);
             } else {
                 printf("command <%s> not found\n", argv[0]);
             }
@@ -71,7 +71,7 @@
 }
 
 
-void SimpleShell::attach(Callback<void()> cb, char *command) 
+void SimpleShell::attach(callback_t cb, char *command) 
 {  
     if (lookupEnd < MAXLOOKUP) {
         lookup[lookupEnd].cb = cb;
@@ -83,9 +83,9 @@
 }
 
 
-Callback<void()> SimpleShell::findCommand()
+SimpleShell::callback_t SimpleShell::findCommand()
 {
-    Callback<void()> cb=NULL;
+    SimpleShell::callback_t cb=NULL;
     
     for (int i=0; i < lookupEnd; i++) {
         if (strncmp(argv[0], lookup[i].command, MAXBUF) == 0) {
@@ -193,10 +193,6 @@
         argv[argc++] = t;
         t = strtok(NULL, " ");
     }
-    // Print out argv
-    for (int i=0; i < argc; i++) {
-        printf("argv[%d] = <%s>\n", i, argv[i]);
-    }
 
     return;
 }
\ No newline at end of file
--- a/SimpleShell.h	Wed Dec 19 00:29:05 2018 +0000
+++ b/SimpleShell.h	Wed Dec 19 18:25:12 2018 +0000
@@ -20,6 +20,10 @@
  */
 class SimpleShell {
 public:  
+
+    /// Callback type used for shell commands
+    typedef Callback<void(int, char**)> callback_t;
+
     /// Create a new shell instance
     SimpleShell();
 
@@ -40,7 +44,7 @@
      * sh.attach(helloworld, "test");
      * @endcode
      */
-    void attach(Callback<void()> cb, char *command);
+    void attach(callback_t cb, char *command);
 
     
 private:
@@ -53,19 +57,19 @@
     /// internal struct to contain a single command
     typedef struct {
         char *command;
-        Callback<void()> cb;
+        callback_t cb;
     } command_entry_t;
 
     /** finds and eturns the callback for a command
      * @return Callback to a function returning void
      */
-    Callback<void()> findCommand();  
+    callback_t findCommand();  
     
     /// Built-in shell command to display list of commands
-    void help();
+    void help(int argc, char **argv);
 
     /// Built-in shell command to print working directory
-    void pwd();
+    void pwd(int argc, char **argv);
 
     /// Prints command prompt
     void printPrompt(void);
@@ -80,7 +84,7 @@
     int lookupEnd;
     
     /// Maximum number of arguments
-    static const int MAXARGS=5;
+    static const int MAXARGS=3;
     
     /// Command and arguments
     char *argv[MAXARGS];