Simple embedded shell with runtime pluggable commands.

Dependents:   DataBus2018

Implements a simple unix-like shell for embedded systems with a pluggable command architecture.

SimpleShell.h

Committer:
shimniok
Date:
2018-12-24
Revision:
25:9340833d92c0
Parent:
23:b1e49cfcaef6
Child:
26:0067bd31653f

File content as of revision 25:9340833d92c0:

#ifndef __SIMPLESHELL_H
#define __SIMPLESHELL_H

#include "mbed.h"

/** A simple, flexible, embedded shell with dynamically added shell commands.
 * Shell commands must be void(int argc, char **argv).
 * @code
 * #include "SimpleShell.h"
 *
 * void helloworld(int argc, char **argv) { printf("Hello world!\n"); }
 *
 * int main() {
 *   SimpleShell sh;
 *   sh.attach(helloworld, "test");
 *   sh.run();
 * }
 * @endcode
 */
class SimpleShell {
public:

    /// Callback type used for shell commands
    typedef Callback<void(int, char**)> callback_t;

    /// Create a new shell instance
    SimpleShell();

    /** Call this to run the shell.
     * @note The shell can be run in a new thread.
     * @code
     * SimpleShell sh;
     * sh.run();
     * thread.start(callback(&sh, &SimpleShell::run));
     * @endcode
     */
    void run();

    /** Attaches a shell command
     * @param cb is the callback function that implements the command
     * @param command is the string used to invoke the command in the shell
     * @code
     * sh.attach(helloworld, "test");
     * @endcode
     */
    void attach(callback_t cb, char *command);


private:
    /// Maximum number of commands
    static const int MAXLOOKUP=16;

    /// Maximum command line buffer size
    static const int MAXBUF=64;

    /// internal struct to contain a single command
    typedef struct {
        char *command;
        callback_t cb;
    } command_entry_t;

    /// canonicalize path
    char *canon(char *path);

    /// return basename of path
    char *basename(char *path);

    /** finds and eturns the callback for a command
     * @return Callback to a function returning void
     */
    callback_t findCommand();

    /// Built-in shell command to display list of commands
    void help(int argc, char **argv);

    /// Change current directory
    void cd(int argc, char **argv);

    /// Built-in shell command to print working directory
    void pwd(int argc, char **argv);

    /// Built-in shell command to list files in directory
    void ls(int argc, char **argv);

    /// Built-in shell command to remove a file
    void rm(int argc, char **argv);

    /// Built-in shell command to create a file
    void touch(int argc, char **argv);

    /// Built-in shell command to display contents of file
    void cat(int argc, char **argv);
    
    /// Built-in shell command to display contents of file
    void send(int argc, char **argv);

    /// Prints command prompt
    void printPrompt(void);

    /// Reads a command from the prompt (with editing)
    void readCommand();

    /// Command lookup table
    command_entry_t lookup[MAXLOOKUP];

    /// Current end of lookup table
    int lookupEnd;

    /// Maximum number of arguments
    static const int MAXARGS=3;

    /// Command and arguments
    char *argv[MAXARGS];

    /// Size of argv
    int argc;

    /// Current working directory
    char _cwd[MAXBUF];

    /// shell command history


}; // class

#endif