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:
Mon Dec 24 17:44:29 2018 +0000
Revision:
22:b0e6d416ce99
Parent:
20:53f0b5dc30f9
Child:
23:b1e49cfcaef6
restored blanked-out file

Who changed what in which revision?

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