Simple embedded shell with runtime pluggable commands.
Implements a simple unix-like shell for embedded systems with a pluggable command architecture.
SimpleShell.h@17:0739cb2f1930, 2018-12-19 (annotated)
- Committer:
- shimniok
- Date:
- Wed Dec 19 19:02:17 2018 +0000
- Revision:
- 17:0739cb2f1930
- Parent:
- 16:f2b9b7b2c71e
- Child:
- 18:2b5ed529ab37
implemented built-in change dir (cd)
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 | 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 | 15:242626d8d104 | 23 | |
shimniok | 15:242626d8d104 | 24 | /// Callback type used for shell commands |
shimniok | 15:242626d8d104 | 25 | typedef Callback<void(int, char**)> callback_t; |
shimniok | 15:242626d8d104 | 26 | |
shimniok | 6:4da092220ba8 | 27 | /// Create a new shell instance |
shimniok | 0:49820d5a38c9 | 28 | SimpleShell(); |
shimniok | 0:49820d5a38c9 | 29 | |
shimniok | 7:b58450c94d32 | 30 | /** Call this to run the shell. |
shimniok | 7:b58450c94d32 | 31 | * @note The shell can be run in a new thread. |
shimniok | 7:b58450c94d32 | 32 | * @code |
shimniok | 7:b58450c94d32 | 33 | * SimpleShell sh; |
shimniok | 7:b58450c94d32 | 34 | * sh.run(); |
shimniok | 7:b58450c94d32 | 35 | * thread.start(callback(&sh, &SimpleShell::run)); |
shimniok | 7:b58450c94d32 | 36 | * @endcode |
shimniok | 7:b58450c94d32 | 37 | */ |
shimniok | 3:ebb4893f033d | 38 | void run(); |
shimniok | 6:4da092220ba8 | 39 | |
shimniok | 6:4da092220ba8 | 40 | /** Attaches a shell command |
shimniok | 6:4da092220ba8 | 41 | * @param cb is the callback function that implements the command |
shimniok | 6:4da092220ba8 | 42 | * @param command is the string used to invoke the command in the shell |
shimniok | 7:b58450c94d32 | 43 | * @code |
shimniok | 7:b58450c94d32 | 44 | * sh.attach(helloworld, "test"); |
shimniok | 7:b58450c94d32 | 45 | * @endcode |
shimniok | 6:4da092220ba8 | 46 | */ |
shimniok | 15:242626d8d104 | 47 | void attach(callback_t cb, char *command); |
shimniok | 6:4da092220ba8 | 48 | |
shimniok | 7:b58450c94d32 | 49 | |
shimniok | 7:b58450c94d32 | 50 | private: |
shimniok | 7:b58450c94d32 | 51 | /// Maximum number of commands |
shimniok | 7:b58450c94d32 | 52 | static const int MAXLOOKUP=32; |
shimniok | 7:b58450c94d32 | 53 | |
shimniok | 7:b58450c94d32 | 54 | /// Maximum command line buffer size |
shimniok | 7:b58450c94d32 | 55 | static const int MAXBUF=64; |
shimniok | 7:b58450c94d32 | 56 | |
shimniok | 7:b58450c94d32 | 57 | /// internal struct to contain a single command |
shimniok | 7:b58450c94d32 | 58 | typedef struct { |
shimniok | 7:b58450c94d32 | 59 | char *command; |
shimniok | 15:242626d8d104 | 60 | callback_t cb; |
shimniok | 7:b58450c94d32 | 61 | } command_entry_t; |
shimniok | 7:b58450c94d32 | 62 | |
shimniok | 6:4da092220ba8 | 63 | /** finds and eturns the callback for a command |
shimniok | 6:4da092220ba8 | 64 | * @return Callback to a function returning void |
shimniok | 6:4da092220ba8 | 65 | */ |
shimniok | 15:242626d8d104 | 66 | callback_t findCommand(); |
shimniok | 8:41b7274a9753 | 67 | |
shimniok | 8:41b7274a9753 | 68 | /// Built-in shell command to display list of commands |
shimniok | 15:242626d8d104 | 69 | void help(int argc, char **argv); |
shimniok | 6:4da092220ba8 | 70 | |
shimniok | 17:0739cb2f1930 | 71 | /// Change current directory |
shimniok | 17:0739cb2f1930 | 72 | void cd(int argc, char **argv); |
shimniok | 17:0739cb2f1930 | 73 | |
shimniok | 13:a29fb89018e1 | 74 | /// Built-in shell command to print working directory |
shimniok | 15:242626d8d104 | 75 | void pwd(int argc, char **argv); |
shimniok | 13:a29fb89018e1 | 76 | |
shimniok | 16:f2b9b7b2c71e | 77 | /// Built-in shell command to display contents of file |
shimniok | 16:f2b9b7b2c71e | 78 | void cat(int argc, char **argv); |
shimniok | 16:f2b9b7b2c71e | 79 | |
shimniok | 7:b58450c94d32 | 80 | /// Prints command prompt |
shimniok | 0:49820d5a38c9 | 81 | void printPrompt(void); |
shimniok | 7:b58450c94d32 | 82 | |
shimniok | 7:b58450c94d32 | 83 | /// Reads a command from the prompt (with editing) |
shimniok | 0:49820d5a38c9 | 84 | void readCommand(); |
shimniok | 7:b58450c94d32 | 85 | |
shimniok | 7:b58450c94d32 | 86 | /// Command lookup table |
shimniok | 2:4f0affdb7db9 | 87 | command_entry_t lookup[MAXLOOKUP]; |
shimniok | 7:b58450c94d32 | 88 | |
shimniok | 7:b58450c94d32 | 89 | /// Current end of lookup table |
shimniok | 2:4f0affdb7db9 | 90 | int lookupEnd; |
shimniok | 7:b58450c94d32 | 91 | |
shimniok | 14:75b5918090ae | 92 | /// Maximum number of arguments |
shimniok | 15:242626d8d104 | 93 | static const int MAXARGS=3; |
shimniok | 14:75b5918090ae | 94 | |
shimniok | 14:75b5918090ae | 95 | /// Command and arguments |
shimniok | 14:75b5918090ae | 96 | char *argv[MAXARGS]; |
shimniok | 14:75b5918090ae | 97 | |
shimniok | 14:75b5918090ae | 98 | /// Size of argv |
shimniok | 14:75b5918090ae | 99 | int argc; |
shimniok | 7:b58450c94d32 | 100 | |
shimniok | 7:b58450c94d32 | 101 | /// Current working directory |
shimniok | 0:49820d5a38c9 | 102 | char _cwd[MAXBUF]; |
shimniok | 10:c3faa7ffd23b | 103 | |
shimniok | 10:c3faa7ffd23b | 104 | /// shell command history |
shimniok | 10:c3faa7ffd23b | 105 | |
shimniok | 10:c3faa7ffd23b | 106 | |
shimniok | 6:4da092220ba8 | 107 | }; // class |
shimniok | 6:4da092220ba8 | 108 | |
shimniok | 6:4da092220ba8 | 109 | #endif |