Simple embedded shell with runtime pluggable commands.
Implements a simple unix-like shell for embedded systems with a pluggable command architecture.
Diff: SimpleShell.h
- Revision:
- 7:b58450c94d32
- Parent:
- 6:4da092220ba8
- Child:
- 8:41b7274a9753
- Child:
- 10:c3faa7ffd23b
diff -r 4da092220ba8 -r b58450c94d32 SimpleShell.h --- a/SimpleShell.h Wed Dec 12 17:46:29 2018 +0000 +++ b/SimpleShell.h Wed Dec 12 17:58:27 2018 +0000 @@ -5,39 +5,77 @@ /** SimpleShell * A simple, flexible, embedded shell with dynamically added shell commands. + * @code + * #include "SimpleShell.h" + * + * void helloworld() { printf("Hello world!\n"); } + * + * int main() { + * SimpleShell sh; + * sh.attach(helloworld, "test"); + * sh.run(); + * } + * @endcode */ class SimpleShell { public: /// Create a new shell instance SimpleShell(); - /// Call this to run the shell in a thread + /** Call this to run the shell. + * @note The shell can be run in a new thread. + * @code + * SimpleShell sh; + * sh.run(); + * thread.start(callback(&sh, &SimpleShell::run)); + * @endcode + */ void run(); /** Attaches a shell command * @param cb is the callback function that implements the command * @param command is the string used to invoke the command in the shell + * @code + * sh.attach(helloworld, "test"); + * @endcode */ void attach(Callback<void()> cb, char *command); + +private: + /// Maximum number of commands + static const int MAXLOOKUP=32; + + /// Maximum command line buffer size + static const int MAXBUF=64; + + /// internal struct to contain a single command + typedef struct { + char *command; + Callback<void()> cb; + } command_entry_t; + /** finds and eturns the callback for a command * @return Callback to a function returning void */ Callback<void()> findCommand(); - -private: - typedef struct { - char *command; - Callback<void()> cb; - } command_entry_t; - static const int MAXBUF=32; - static const int MAXLOOKUP=32; + /// Prints command prompt void printPrompt(void); + + /// Reads a command from the prompt (with editing) void readCommand(); + + /// Command lookup table command_entry_t lookup[MAXLOOKUP]; + + /// Current end of lookup table int lookupEnd; + + /// Temporary command string char cmd[MAXBUF]; + + /// Current working directory char _cwd[MAXBUF]; }; // class