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 19 18:25:12 2018 +0000
Revision:
15:242626d8d104
Parent:
14:75b5918090ae
Child:
16:f2b9b7b2c71e
finished implementing shell args

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 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 13:a29fb89018e1 71 /// Built-in shell command to print working directory
shimniok 15:242626d8d104 72 void pwd(int argc, char **argv);
shimniok 13:a29fb89018e1 73
shimniok 7:b58450c94d32 74 /// Prints command prompt
shimniok 0:49820d5a38c9 75 void printPrompt(void);
shimniok 7:b58450c94d32 76
shimniok 7:b58450c94d32 77 /// Reads a command from the prompt (with editing)
shimniok 0:49820d5a38c9 78 void readCommand();
shimniok 7:b58450c94d32 79
shimniok 7:b58450c94d32 80 /// Command lookup table
shimniok 2:4f0affdb7db9 81 command_entry_t lookup[MAXLOOKUP];
shimniok 7:b58450c94d32 82
shimniok 7:b58450c94d32 83 /// Current end of lookup table
shimniok 2:4f0affdb7db9 84 int lookupEnd;
shimniok 7:b58450c94d32 85
shimniok 14:75b5918090ae 86 /// Maximum number of arguments
shimniok 15:242626d8d104 87 static const int MAXARGS=3;
shimniok 14:75b5918090ae 88
shimniok 14:75b5918090ae 89 /// Command and arguments
shimniok 14:75b5918090ae 90 char *argv[MAXARGS];
shimniok 14:75b5918090ae 91
shimniok 14:75b5918090ae 92 /// Size of argv
shimniok 14:75b5918090ae 93 int argc;
shimniok 7:b58450c94d32 94
shimniok 7:b58450c94d32 95 /// Current working directory
shimniok 0:49820d5a38c9 96 char _cwd[MAXBUF];
shimniok 10:c3faa7ffd23b 97
shimniok 10:c3faa7ffd23b 98 /// shell command history
shimniok 10:c3faa7ffd23b 99
shimniok 10:c3faa7ffd23b 100
shimniok 6:4da092220ba8 101 }; // class
shimniok 6:4da092220ba8 102
shimniok 6:4da092220ba8 103 #endif