Michael Shimniok / SimpleShell

Dependents:   DataBus2018

Embed: (wiki syntax)

« Back to documentation index

SimpleShell Class Reference

SimpleShell Class Reference

A simple, flexible, embedded shell with dynamically added shell commands. More...

#include <SimpleShell.h>

Public Types

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

Public Member Functions

 SimpleShell (char *home)
 Create a new shell instance.
void run ()
 Call this to run the shell.
void command (callback_t cb, char *command)
 Adds a shell command.
bool haswildcard (char *s)
 Determine if the specified string includes a shell wildcard.
char * canon (char *path)
 Canonicalize path following unix-style convention.
char * basename (char *path)
 Get the basename of specified path.
char * dirname (char *path)
 Get the parent directory of specified path.
char * foreach (char *pattern)
 Runs a callback on each matching file in the specified pattern.

Detailed Description

A simple, flexible, embedded shell with dynamically added shell commands.

Shell commands must be: void(int argc, char **argv) Built-in shell commands include: help: display list of commands cd: Change current directory pwd: print working directory ls: list files in directory rm: remove a file touch: create a file cat: display contents of file send: send a file with a simple file transfer protocol.

See also:
https://github.com/shimniok/ascii-transfer
 #include "SimpleShell.h"

 void helloworld(int argc, char **argv) { printf("Hello world!\n"); }

 int main() {
   SimpleShell sh;
   sh.attach(helloworld, "test");
   sh.run();
 }

Definition at line 32 of file SimpleShell.h.


Member Typedef Documentation

typedef Callback<void(int,char**) callback_t)

Callback type used for shell commands.

Definition at line 36 of file SimpleShell.h.


Constructor & Destructor Documentation

SimpleShell ( char *  home )

Create a new shell instance.

Definition at line 20 of file SimpleShell.cpp.


Member Function Documentation

char * basename ( char *  path )

Get the basename of specified path.

For example, basename("/foo/bar/whee") returns "whee"

Parameters:
pathis the path specification, assumed to be canonicalized
Returns:
the basename of the path

Definition at line 106 of file SimpleShell.cpp.

char * canon ( char *  path )

Canonicalize path following unix-style convention.

The specified path can be absolute or relative path names. If relative, the path is appended to cwd.

Parameters:
pathis the specified path
Returns:
canonicalized path name

Definition at line 51 of file SimpleShell.cpp.

void command ( callback_t  cb,
char *  command 
)

Adds a shell command.

Parameters:
cbis the callback function that implements the command
commandis the string used to invoke the command in the shell

 sh.attach(helloworld, "test");

Definition at line 425 of file SimpleShell.cpp.

char * dirname ( char *  path )

Get the parent directory of specified path.

For example, basename("/foo/bar/whee") returns "/foo/bar"

Parameters:
pathis the path specification, assumed to be canonicalized
Returns:
the parent directory of the path

Definition at line 188 of file SimpleShell.cpp.

char * foreach ( char *  pattern )

Runs a callback on each matching file in the specified pattern.

Parameters:
patternis a filename pattern ala Linux fnmatch(3)
cbis the int(char*) function on which each file match is called
Returns:
false if cb returns non-zero on any file, true otherwise
 if (foreach("/test/\*.txt", callback(remove)) {
     printf("error!\n");
 }

Definition at line 257 of file SimpleShell.cpp.

bool haswildcard ( char *  s )

Determine if the specified string includes a shell wildcard.

Parameters:
sis the string to check for a wildcard character
Returns:
true or false

Definition at line 42 of file SimpleShell.cpp.

void run (  )

Call this to run the shell.

Note:
The shell can be run in a new thread. Be sure to give it enough stack space.
 SimpleShell sh;
 sh.run();
 thread.start(callback(&sh, &SimpleShell::run));

Definition at line 399 of file SimpleShell.cpp.