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:
Thu Dec 13 23:26:34 2018 +0000
Revision:
13:a29fb89018e1
Parent:
11:23f61057d877
Child:
14:75b5918090ae
fixed special key handling, added pwd

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