Simple embedded shell with runtime pluggable commands.
Implements a simple unix-like shell for embedded systems with a pluggable command architecture.
SimpleShell.h@25:9340833d92c0, 2018-12-24 (annotated)
- Committer:
- shimniok
- Date:
- Mon Dec 24 19:58:18 2018 +0000
- Revision:
- 25:9340833d92c0
- Parent:
- 23:b1e49cfcaef6
- Child:
- 26:0067bd31653f
Updated documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shimniok | 22:b0e6d416ce99 | 1 | #ifndef __SIMPLESHELL_H |
shimniok | 22:b0e6d416ce99 | 2 | #define __SIMPLESHELL_H |
shimniok | 22:b0e6d416ce99 | 3 | |
shimniok | 22:b0e6d416ce99 | 4 | #include "mbed.h" |
shimniok | 22:b0e6d416ce99 | 5 | |
shimniok | 25:9340833d92c0 | 6 | /** A simple, flexible, embedded shell with dynamically added shell commands. |
shimniok | 25:9340833d92c0 | 7 | * Shell commands must be void(int argc, char **argv). |
shimniok | 22:b0e6d416ce99 | 8 | * @code |
shimniok | 22:b0e6d416ce99 | 9 | * #include "SimpleShell.h" |
shimniok | 22:b0e6d416ce99 | 10 | * |
shimniok | 25:9340833d92c0 | 11 | * void helloworld(int argc, char **argv) { printf("Hello world!\n"); } |
shimniok | 22:b0e6d416ce99 | 12 | * |
shimniok | 22:b0e6d416ce99 | 13 | * int main() { |
shimniok | 22:b0e6d416ce99 | 14 | * SimpleShell sh; |
shimniok | 22:b0e6d416ce99 | 15 | * sh.attach(helloworld, "test"); |
shimniok | 22:b0e6d416ce99 | 16 | * sh.run(); |
shimniok | 22:b0e6d416ce99 | 17 | * } |
shimniok | 22:b0e6d416ce99 | 18 | * @endcode |
shimniok | 22:b0e6d416ce99 | 19 | */ |
shimniok | 22:b0e6d416ce99 | 20 | class SimpleShell { |
shimniok | 22:b0e6d416ce99 | 21 | public: |
shimniok | 22:b0e6d416ce99 | 22 | |
shimniok | 22:b0e6d416ce99 | 23 | /// Callback type used for shell commands |
shimniok | 22:b0e6d416ce99 | 24 | typedef Callback<void(int, char**)> callback_t; |
shimniok | 22:b0e6d416ce99 | 25 | |
shimniok | 22:b0e6d416ce99 | 26 | /// Create a new shell instance |
shimniok | 22:b0e6d416ce99 | 27 | SimpleShell(); |
shimniok | 22:b0e6d416ce99 | 28 | |
shimniok | 22:b0e6d416ce99 | 29 | /** Call this to run the shell. |
shimniok | 22:b0e6d416ce99 | 30 | * @note The shell can be run in a new thread. |
shimniok | 22:b0e6d416ce99 | 31 | * @code |
shimniok | 22:b0e6d416ce99 | 32 | * SimpleShell sh; |
shimniok | 22:b0e6d416ce99 | 33 | * sh.run(); |
shimniok | 22:b0e6d416ce99 | 34 | * thread.start(callback(&sh, &SimpleShell::run)); |
shimniok | 22:b0e6d416ce99 | 35 | * @endcode |
shimniok | 22:b0e6d416ce99 | 36 | */ |
shimniok | 22:b0e6d416ce99 | 37 | void run(); |
shimniok | 22:b0e6d416ce99 | 38 | |
shimniok | 22:b0e6d416ce99 | 39 | /** Attaches a shell command |
shimniok | 22:b0e6d416ce99 | 40 | * @param cb is the callback function that implements the command |
shimniok | 22:b0e6d416ce99 | 41 | * @param command is the string used to invoke the command in the shell |
shimniok | 22:b0e6d416ce99 | 42 | * @code |
shimniok | 22:b0e6d416ce99 | 43 | * sh.attach(helloworld, "test"); |
shimniok | 22:b0e6d416ce99 | 44 | * @endcode |
shimniok | 22:b0e6d416ce99 | 45 | */ |
shimniok | 22:b0e6d416ce99 | 46 | void attach(callback_t cb, char *command); |
shimniok | 22:b0e6d416ce99 | 47 | |
shimniok | 22:b0e6d416ce99 | 48 | |
shimniok | 22:b0e6d416ce99 | 49 | private: |
shimniok | 22:b0e6d416ce99 | 50 | /// Maximum number of commands |
shimniok | 22:b0e6d416ce99 | 51 | static const int MAXLOOKUP=16; |
shimniok | 22:b0e6d416ce99 | 52 | |
shimniok | 22:b0e6d416ce99 | 53 | /// Maximum command line buffer size |
shimniok | 22:b0e6d416ce99 | 54 | static const int MAXBUF=64; |
shimniok | 22:b0e6d416ce99 | 55 | |
shimniok | 22:b0e6d416ce99 | 56 | /// internal struct to contain a single command |
shimniok | 22:b0e6d416ce99 | 57 | typedef struct { |
shimniok | 22:b0e6d416ce99 | 58 | char *command; |
shimniok | 22:b0e6d416ce99 | 59 | callback_t cb; |
shimniok | 22:b0e6d416ce99 | 60 | } command_entry_t; |
shimniok | 22:b0e6d416ce99 | 61 | |
shimniok | 22:b0e6d416ce99 | 62 | /// canonicalize path |
shimniok | 22:b0e6d416ce99 | 63 | char *canon(char *path); |
shimniok | 22:b0e6d416ce99 | 64 | |
shimniok | 23:b1e49cfcaef6 | 65 | /// return basename of path |
shimniok | 23:b1e49cfcaef6 | 66 | char *basename(char *path); |
shimniok | 23:b1e49cfcaef6 | 67 | |
shimniok | 22:b0e6d416ce99 | 68 | /** finds and eturns the callback for a command |
shimniok | 22:b0e6d416ce99 | 69 | * @return Callback to a function returning void |
shimniok | 22:b0e6d416ce99 | 70 | */ |
shimniok | 22:b0e6d416ce99 | 71 | callback_t findCommand(); |
shimniok | 22:b0e6d416ce99 | 72 | |
shimniok | 22:b0e6d416ce99 | 73 | /// Built-in shell command to display list of commands |
shimniok | 22:b0e6d416ce99 | 74 | void help(int argc, char **argv); |
shimniok | 22:b0e6d416ce99 | 75 | |
shimniok | 22:b0e6d416ce99 | 76 | /// Change current directory |
shimniok | 22:b0e6d416ce99 | 77 | void cd(int argc, char **argv); |
shimniok | 22:b0e6d416ce99 | 78 | |
shimniok | 22:b0e6d416ce99 | 79 | /// Built-in shell command to print working directory |
shimniok | 22:b0e6d416ce99 | 80 | void pwd(int argc, char **argv); |
shimniok | 22:b0e6d416ce99 | 81 | |
shimniok | 22:b0e6d416ce99 | 82 | /// Built-in shell command to list files in directory |
shimniok | 22:b0e6d416ce99 | 83 | void ls(int argc, char **argv); |
shimniok | 22:b0e6d416ce99 | 84 | |
shimniok | 22:b0e6d416ce99 | 85 | /// Built-in shell command to remove a file |
shimniok | 22:b0e6d416ce99 | 86 | void rm(int argc, char **argv); |
shimniok | 22:b0e6d416ce99 | 87 | |
shimniok | 22:b0e6d416ce99 | 88 | /// Built-in shell command to create a file |
shimniok | 22:b0e6d416ce99 | 89 | void touch(int argc, char **argv); |
shimniok | 22:b0e6d416ce99 | 90 | |
shimniok | 22:b0e6d416ce99 | 91 | /// Built-in shell command to display contents of file |
shimniok | 22:b0e6d416ce99 | 92 | void cat(int argc, char **argv); |
shimniok | 23:b1e49cfcaef6 | 93 | |
shimniok | 23:b1e49cfcaef6 | 94 | /// Built-in shell command to display contents of file |
shimniok | 23:b1e49cfcaef6 | 95 | void send(int argc, char **argv); |
shimniok | 22:b0e6d416ce99 | 96 | |
shimniok | 22:b0e6d416ce99 | 97 | /// Prints command prompt |
shimniok | 22:b0e6d416ce99 | 98 | void printPrompt(void); |
shimniok | 22:b0e6d416ce99 | 99 | |
shimniok | 22:b0e6d416ce99 | 100 | /// Reads a command from the prompt (with editing) |
shimniok | 22:b0e6d416ce99 | 101 | void readCommand(); |
shimniok | 22:b0e6d416ce99 | 102 | |
shimniok | 22:b0e6d416ce99 | 103 | /// Command lookup table |
shimniok | 22:b0e6d416ce99 | 104 | command_entry_t lookup[MAXLOOKUP]; |
shimniok | 22:b0e6d416ce99 | 105 | |
shimniok | 22:b0e6d416ce99 | 106 | /// Current end of lookup table |
shimniok | 22:b0e6d416ce99 | 107 | int lookupEnd; |
shimniok | 22:b0e6d416ce99 | 108 | |
shimniok | 22:b0e6d416ce99 | 109 | /// Maximum number of arguments |
shimniok | 22:b0e6d416ce99 | 110 | static const int MAXARGS=3; |
shimniok | 22:b0e6d416ce99 | 111 | |
shimniok | 22:b0e6d416ce99 | 112 | /// Command and arguments |
shimniok | 22:b0e6d416ce99 | 113 | char *argv[MAXARGS]; |
shimniok | 22:b0e6d416ce99 | 114 | |
shimniok | 22:b0e6d416ce99 | 115 | /// Size of argv |
shimniok | 22:b0e6d416ce99 | 116 | int argc; |
shimniok | 22:b0e6d416ce99 | 117 | |
shimniok | 22:b0e6d416ce99 | 118 | /// Current working directory |
shimniok | 22:b0e6d416ce99 | 119 | char _cwd[MAXBUF]; |
shimniok | 22:b0e6d416ce99 | 120 | |
shimniok | 22:b0e6d416ce99 | 121 | /// shell command history |
shimniok | 22:b0e6d416ce99 | 122 | |
shimniok | 22:b0e6d416ce99 | 123 | |
shimniok | 22:b0e6d416ce99 | 124 | }; // class |
shimniok | 22:b0e6d416ce99 | 125 | |
shimniok | 22:b0e6d416ce99 | 126 | #endif |