Simple embedded shell with runtime pluggable commands.
Implements a simple unix-like shell for embedded systems with a pluggable command architecture.
SimpleShell.h@10:c3faa7ffd23b, 2018-12-13 (annotated)
- Committer:
- shimniok
- Date:
- Thu Dec 13 08:56:58 2018 +0000
- Revision:
- 10:c3faa7ffd23b
- Parent:
- 7:b58450c94d32
- Child:
- 11:23f61057d877
Fixed recognition of esc sequence keys (home, end, etc.)
Who changed what in which revision?
User | Revision | Line number | New 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 | 6:4da092220ba8 | 62 | |
shimniok | 7:b58450c94d32 | 63 | /// Prints command prompt |
shimniok | 0:49820d5a38c9 | 64 | void printPrompt(void); |
shimniok | 7:b58450c94d32 | 65 | |
shimniok | 7:b58450c94d32 | 66 | /// Reads a command from the prompt (with editing) |
shimniok | 0:49820d5a38c9 | 67 | void readCommand(); |
shimniok | 7:b58450c94d32 | 68 | |
shimniok | 7:b58450c94d32 | 69 | /// Command lookup table |
shimniok | 2:4f0affdb7db9 | 70 | command_entry_t lookup[MAXLOOKUP]; |
shimniok | 7:b58450c94d32 | 71 | |
shimniok | 7:b58450c94d32 | 72 | /// Current end of lookup table |
shimniok | 2:4f0affdb7db9 | 73 | int lookupEnd; |
shimniok | 7:b58450c94d32 | 74 | |
shimniok | 7:b58450c94d32 | 75 | /// Temporary command string |
shimniok | 0:49820d5a38c9 | 76 | char cmd[MAXBUF]; |
shimniok | 7:b58450c94d32 | 77 | |
shimniok | 7:b58450c94d32 | 78 | /// Current working directory |
shimniok | 0:49820d5a38c9 | 79 | char _cwd[MAXBUF]; |
shimniok | 10:c3faa7ffd23b | 80 | |
shimniok | 10:c3faa7ffd23b | 81 | /// shell command history |
shimniok | 10:c3faa7ffd23b | 82 | |
shimniok | 10:c3faa7ffd23b | 83 | |
shimniok | 6:4da092220ba8 | 84 | }; // class |
shimniok | 6:4da092220ba8 | 85 | |
shimniok | 6:4da092220ba8 | 86 | #endif |