Michael Shimniok / SimpleShell

Dependents:   DataBus2018

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SimpleShell.h Source File

SimpleShell.h

00001 #ifndef __SIMPLESHELL_H
00002 #define __SIMPLESHELL_H
00003 
00004 #include "mbed.h"
00005 #include <vector>
00006 
00007 /** A simple, flexible, embedded shell with dynamically added shell commands.
00008  * Shell commands must be: void(int argc, char **argv)
00009  * Built-in shell commands include:
00010  * help: display list of commands
00011  * cd: Change current directory
00012  * pwd: print working directory
00013  * ls: list files in directory
00014  * rm: remove a file
00015  * touch: create a file
00016  * cat: display contents of file
00017  * send: send a file with a simple file transfer protocol. 
00018  * @see https://github.com/shimniok/ascii-transfer
00019  * 
00020  * @code
00021  * #include "SimpleShell.h"
00022  *
00023  * void helloworld(int argc, char **argv) { printf("Hello world!\n"); }
00024  *
00025  * int main() {
00026  *   SimpleShell sh;
00027  *   sh.attach(helloworld, "test");
00028  *   sh.run();
00029  * }
00030  * @endcode
00031  */
00032 class SimpleShell {
00033 public:
00034 
00035     /// Callback type used for shell commands.
00036     typedef Callback<void(int,char**)> callback_t;
00037     
00038     /// Create a new shell instance.
00039     SimpleShell(char *home);
00040 
00041     /** Call this to run the shell.
00042      * @note The shell can be run in a new thread. Be sure to give it enough
00043      * stack space.
00044      * @code
00045      * SimpleShell sh;
00046      * sh.run();
00047      * thread.start(callback(&sh, &SimpleShell::run));
00048      * @endcode
00049      */
00050     void run();
00051 
00052     /** Adds a shell command.
00053      * @param cb is the callback function that implements the command
00054      * @param command is the string used to invoke the command in the shell
00055      * @code
00056      * sh.attach(helloworld, "test");
00057      * @endcode
00058      */
00059     void command(callback_t cb, char *command);
00060 
00061     /** Determine if the specified string includes a shell wildcard
00062      * @param s is the string to check for a wildcard character
00063      * @returns true or false
00064      */
00065     bool haswildcard(char *s);
00066 
00067     /** Canonicalize path following unix-style convention.
00068      * The specified path can be absolute or relative path names. If relative,
00069      * the path is appended to cwd.
00070      * @param path is the specified path
00071      * @returns canonicalized path name
00072      */
00073     char *canon(char *path);
00074 
00075     /** Get the basename of specified path.
00076      * For example, basename("/foo/bar/whee") returns "whee"
00077      * @param path is the path specification, assumed to be canonicalized
00078      * @returns the basename of the path
00079      */
00080     char *basename(char *path);
00081     
00082     /** Get the parent directory of specified path.
00083      * For example, basename("/foo/bar/whee") returns "/foo/bar"
00084      * @param path is the path specification, assumed to be canonicalized
00085      * @returns the parent directory of the path
00086      */
00087     char *dirname(char *path);
00088     
00089     /** Runs a callback on each matching file in the specified pattern
00090      * @param pattern is a filename pattern ala Linux fnmatch(3)
00091      * @param cb is the int(char*) function on which each file match is called
00092      * @return false if cb returns non-zero on any file, true otherwise
00093      *
00094      * @code
00095      * if (foreach("/test/\*.txt", callback(remove)) {
00096      *     printf("error!\n");
00097      * }
00098      * @endcode
00099      */
00100     char *foreach(char *pattern);
00101 
00102 private:
00103     /// Maximum number of commands
00104     static const int MAXLOOKUP=24;
00105 
00106     /// Maximum command line buffer size
00107     static const int MAXBUF=64;
00108     
00109     /// Maximum filename size
00110     static const int MAXNAMESIZE=64;
00111 
00112     /// internal struct to contain a single command
00113     typedef struct {
00114         char *command;
00115         callback_t cb;
00116     } command_entry_t;
00117 
00118     /// internal file list for wildcards
00119     typedef vector<char*> filelist_t;
00120 
00121     /** finds and eturns the callback for a command
00122      * @return Callback to a function returning void
00123      */
00124     callback_t findCommand();
00125 
00126     /// Built-in shell command to display list of commands
00127     void help(int argc, char **argv);
00128 
00129     /// Change current directory
00130     void cd(int argc, char **argv);
00131 
00132     /// Built-in shell command to print working directory
00133     void pwd(int argc, char **argv);
00134 
00135     /// Built-in shell command to list files in directory
00136     void ls(int argc, char **argv);
00137 
00138     /// Built-in shell command to remove a file
00139     void rm(int argc, char **argv);
00140 
00141     /// Built-in shell command to create a file
00142     void touch(int argc, char **argv);
00143 
00144     /// Built-in shell command to display contents of file
00145     void cat(int argc, char **argv);
00146     
00147     /// Built-in shell command to display contents of file
00148     void send(int argc, char **argv);
00149 
00150     /// Prints command prompt
00151     void printPrompt(void);
00152 
00153     /// Reads a command from the prompt (with editing)
00154     void readCommand();
00155 
00156     /// Command lookup table
00157     command_entry_t lookup[MAXLOOKUP];
00158 
00159     /// Current end of lookup table
00160     int lookupEnd;
00161 
00162     /// Maximum number of arguments
00163     static const int MAXARGS=5;
00164 
00165     /// Command and arguments
00166     char *argv[MAXARGS];
00167 
00168     /// Size of argv
00169     int argc;
00170 
00171     /// Current working directory
00172     char _cwd[MAXBUF];
00173     
00174     /// Home directory
00175     char _home[MAXBUF];
00176 
00177 }; // class
00178 
00179 #endif