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:
Fri Dec 21 20:02:56 2018 +0000
Revision:
18:2b5ed529ab37
Parent:
17:0739cb2f1930
Child:
19:bf5f5ea4e762
bugfixes to ls

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 18:2b5ed529ab37 52 static const int MAXLOOKUP=16;
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 18:2b5ed529ab37 63 /// canonicalize path
shimniok 18:2b5ed529ab37 64 char *canon(char *path);
shimniok 18:2b5ed529ab37 65
shimniok 6:4da092220ba8 66 /** finds and eturns the callback for a command
shimniok 6:4da092220ba8 67 * @return Callback to a function returning void
shimniok 6:4da092220ba8 68 */
shimniok 15:242626d8d104 69 callback_t findCommand();
shimniok 8:41b7274a9753 70
shimniok 8:41b7274a9753 71 /// Built-in shell command to display list of commands
shimniok 15:242626d8d104 72 void help(int argc, char **argv);
shimniok 6:4da092220ba8 73
shimniok 17:0739cb2f1930 74 /// Change current directory
shimniok 17:0739cb2f1930 75 void cd(int argc, char **argv);
shimniok 17:0739cb2f1930 76
shimniok 13:a29fb89018e1 77 /// Built-in shell command to print working directory
shimniok 15:242626d8d104 78 void pwd(int argc, char **argv);
shimniok 13:a29fb89018e1 79
shimniok 18:2b5ed529ab37 80 /// Built-in shell command to list files in directory
shimniok 18:2b5ed529ab37 81 void ls(int argc, char **argv);
shimniok 18:2b5ed529ab37 82
shimniok 16:f2b9b7b2c71e 83 /// Built-in shell command to display contents of file
shimniok 16:f2b9b7b2c71e 84 void cat(int argc, char **argv);
shimniok 16:f2b9b7b2c71e 85
shimniok 7:b58450c94d32 86 /// Prints command prompt
shimniok 0:49820d5a38c9 87 void printPrompt(void);
shimniok 7:b58450c94d32 88
shimniok 7:b58450c94d32 89 /// Reads a command from the prompt (with editing)
shimniok 0:49820d5a38c9 90 void readCommand();
shimniok 7:b58450c94d32 91
shimniok 7:b58450c94d32 92 /// Command lookup table
shimniok 2:4f0affdb7db9 93 command_entry_t lookup[MAXLOOKUP];
shimniok 7:b58450c94d32 94
shimniok 7:b58450c94d32 95 /// Current end of lookup table
shimniok 2:4f0affdb7db9 96 int lookupEnd;
shimniok 7:b58450c94d32 97
shimniok 14:75b5918090ae 98 /// Maximum number of arguments
shimniok 15:242626d8d104 99 static const int MAXARGS=3;
shimniok 14:75b5918090ae 100
shimniok 14:75b5918090ae 101 /// Command and arguments
shimniok 14:75b5918090ae 102 char *argv[MAXARGS];
shimniok 14:75b5918090ae 103
shimniok 14:75b5918090ae 104 /// Size of argv
shimniok 14:75b5918090ae 105 int argc;
shimniok 7:b58450c94d32 106
shimniok 7:b58450c94d32 107 /// Current working directory
shimniok 0:49820d5a38c9 108 char _cwd[MAXBUF];
shimniok 10:c3faa7ffd23b 109
shimniok 10:c3faa7ffd23b 110 /// shell command history
shimniok 10:c3faa7ffd23b 111
shimniok 10:c3faa7ffd23b 112
shimniok 6:4da092220ba8 113 }; // class
shimniok 6:4da092220ba8 114
shimniok 6:4da092220ba8 115 #endif