Simple embedded shell with runtime pluggable commands.

Dependents:   DataBus2018

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

Committer:
shimniok
Date:
Wed Dec 12 18:30:49 2018 +0000
Revision:
8:41b7274a9753
Parent:
7:b58450c94d32
Child:
9:05eb118e66d9
Added built-in help shell cmd, removed debug prints, display help on start.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shimniok 6:4da092220ba8 1 #ifndef __SIMPLESHELL_H
shimniok 6:4da092220ba8 2 #define __SIMPLESHELL_H
shimniok 6:4da092220ba8 3
shimniok 0:49820d5a38c9 4 #include "mbed.h"
shimniok 0:49820d5a38c9 5
shimniok 6:4da092220ba8 6 /** SimpleShell
shimniok 6:4da092220ba8 7 * A simple, flexible, embedded shell with dynamically added shell commands.
shimniok 7:b58450c94d32 8 * @code
shimniok 7:b58450c94d32 9 * #include "SimpleShell.h"
shimniok 7:b58450c94d32 10 *
shimniok 7:b58450c94d32 11 * void helloworld() { printf("Hello world!\n"); }
shimniok 7:b58450c94d32 12 *
shimniok 7:b58450c94d32 13 * int main() {
shimniok 7:b58450c94d32 14 * SimpleShell sh;
shimniok 7:b58450c94d32 15 * sh.attach(helloworld, "test");
shimniok 7:b58450c94d32 16 * sh.run();
shimniok 7:b58450c94d32 17 * }
shimniok 7:b58450c94d32 18 * @endcode
shimniok 6:4da092220ba8 19 */
shimniok 0:49820d5a38c9 20 class SimpleShell {
shimniok 0:49820d5a38c9 21 public:
shimniok 6:4da092220ba8 22 /// Create a new shell instance
shimniok 0:49820d5a38c9 23 SimpleShell();
shimniok 0:49820d5a38c9 24
shimniok 7:b58450c94d32 25 /** Call this to run the shell.
shimniok 7:b58450c94d32 26 * @note The shell can be run in a new thread.
shimniok 7:b58450c94d32 27 * @code
shimniok 7:b58450c94d32 28 * SimpleShell sh;
shimniok 7:b58450c94d32 29 * sh.run();
shimniok 7:b58450c94d32 30 * thread.start(callback(&sh, &SimpleShell::run));
shimniok 7:b58450c94d32 31 * @endcode
shimniok 7:b58450c94d32 32 */
shimniok 3:ebb4893f033d 33 void run();
shimniok 6:4da092220ba8 34
shimniok 6:4da092220ba8 35 /** Attaches a shell command
shimniok 6:4da092220ba8 36 * @param cb is the callback function that implements the command
shimniok 6:4da092220ba8 37 * @param command is the string used to invoke the command in the shell
shimniok 7:b58450c94d32 38 * @code
shimniok 7:b58450c94d32 39 * sh.attach(helloworld, "test");
shimniok 7:b58450c94d32 40 * @endcode
shimniok 6:4da092220ba8 41 */
shimniok 1:998a7ed04f10 42 void attach(Callback<void()> cb, char *command);
shimniok 6:4da092220ba8 43
shimniok 7:b58450c94d32 44
shimniok 7:b58450c94d32 45 private:
shimniok 7:b58450c94d32 46 /// Maximum number of commands
shimniok 7:b58450c94d32 47 static const int MAXLOOKUP=32;
shimniok 7:b58450c94d32 48
shimniok 7:b58450c94d32 49 /// Maximum command line buffer size
shimniok 7:b58450c94d32 50 static const int MAXBUF=64;
shimniok 7:b58450c94d32 51
shimniok 7:b58450c94d32 52 /// internal struct to contain a single command
shimniok 7:b58450c94d32 53 typedef struct {
shimniok 7:b58450c94d32 54 char *command;
shimniok 7:b58450c94d32 55 Callback<void()> cb;
shimniok 7:b58450c94d32 56 } command_entry_t;
shimniok 7:b58450c94d32 57
shimniok 6:4da092220ba8 58 /** finds and eturns the callback for a command
shimniok 6:4da092220ba8 59 * @return Callback to a function returning void
shimniok 6:4da092220ba8 60 */
shimniok 3:ebb4893f033d 61 Callback<void()> findCommand();
shimniok 8:41b7274a9753 62
shimniok 8:41b7274a9753 63 /// Built-in shell command to display list of commands
shimniok 8:41b7274a9753 64 void help();
shimniok 6:4da092220ba8 65
shimniok 7:b58450c94d32 66 /// Prints command prompt
shimniok 0:49820d5a38c9 67 void printPrompt(void);
shimniok 7:b58450c94d32 68
shimniok 7:b58450c94d32 69 /// Reads a command from the prompt (with editing)
shimniok 0:49820d5a38c9 70 void readCommand();
shimniok 7:b58450c94d32 71
shimniok 7:b58450c94d32 72 /// Command lookup table
shimniok 2:4f0affdb7db9 73 command_entry_t lookup[MAXLOOKUP];
shimniok 7:b58450c94d32 74
shimniok 7:b58450c94d32 75 /// Current end of lookup table
shimniok 2:4f0affdb7db9 76 int lookupEnd;
shimniok 7:b58450c94d32 77
shimniok 7:b58450c94d32 78 /// Temporary command string
shimniok 0:49820d5a38c9 79 char cmd[MAXBUF];
shimniok 7:b58450c94d32 80
shimniok 7:b58450c94d32 81 /// Current working directory
shimniok 0:49820d5a38c9 82 char _cwd[MAXBUF];
shimniok 6:4da092220ba8 83 }; // class
shimniok 6:4da092220ba8 84
shimniok 6:4da092220ba8 85 #endif