iash  v0.5
Library to provide a bash-like shell in an application
iash Class Reference

The main shell class for iash. More...

#include <iash.h>

Public Member Functions

 iash (const std::string &appName="iash")
 Creates an iash shell object with the given application name (for use in the prompt). More...
 
EnvironmentgetEnv ()
 Gets the current Environment object for this shell instance. More...
 
const DirectorygetCwd () const
 Gets the current working directory for this shell. More...
 
void addCommand (Command *cmd)
 Adds a Command instance to this shell's CommandDispatcher registry. More...
 
template<typename ExtCommand , typename... Args>
void addCommand (Args &&... args)
 Adds a Command instance of the given type to this shell's CommandDispatcher registry. More...
 
int runInteractive ()
 Runs the shell in interactive mode. More...
 
int runScript (const char *fname)
 Executes the commands in the specified script and quits. More...
 
int exec (std::string &cmd)
 Executes the specified command and quits. More...
 
int exec (UserCommand *cmd)
 Executes the specified, already parsed command and quits. More...
 

Friends

class ExitCommand
 

Detailed Description

The main shell class for iash.

This class provides an interface to register commands with the shell, and then execute those commands in a number of ways. It also provides an interface for commands to inspect (and, to an extent, modify) the current state of the shell.

While this class has always existed, its interface was completely rewritten in v0.5. Versions that are greater than or equal to v0.5 are not backwards- compatible with prior versions.

Since
0.5

Constructor & Destructor Documentation

◆ iash()

iash::iash ( const std::string &  appName = "iash")

Creates an iash shell object with the given application name (for use in the prompt).

Parameters
appNamethe name of the application, to be shown in prompts and to check for in scripts

Member Function Documentation

◆ addCommand() [1/2]

void iash::addCommand ( Command cmd)

Adds a Command instance to this shell's CommandDispatcher registry.

This function is to be used by directly injecting dynamically allocated Command object pointers into the iash class (iash will handle the dynamic memory internally), for example:

iash shell ("myapp");
shell.addCommand(new FooCommand());

This should not be called more than once per each unique command; the CommandDispatcher will refuse to add the repeated command.

It is not recommended that commands are registered in this way. Please use the templated iash::addCommand() instead.

Parameters
cmda pointer to a dynamically allocated Command object

◆ addCommand() [2/2]

template<typename ExtCommand , typename... Args>
void iash::addCommand ( Args &&...  args)
inline

Adds a Command instance of the given type to this shell's CommandDispatcher registry.

This function will internally allocate an object of the given Command type dynamically.

iash shell ("myapp");
shell.addCommand<FooCommand>();

This should not be called more than once per each unique command; the CommandDispatcher will refuse to add the repeated command.

Parameters
argsthe arguments to pass to the Command subclass' constructor
Template Parameters
ExtCommandthe type of the Command to register with the shell
Argsthe types of the arguments to pass to the Command subclass constructor

◆ exec() [1/2]

int iash::exec ( std::string &  cmd)

Executes the specified command and quits.

This does not support I/O redirection; this will do all input and output from/to stdin/stdout. To run a command with I/O redirection, use exec(UserCommand*).

Parameters
cmda string containing the command to execute
Returns
the exit status of the command

◆ exec() [2/2]

int iash::exec ( UserCommand cmd)
inline

Executes the specified, already parsed command and quits.

Command I/O is done to/from the specified streams in the UserCommand object.

Parameters
cmda UserCommand to execute
Returns
the exit status of the command

◆ getCwd()

const Directory * iash::getCwd ( ) const

Gets the current working directory for this shell.

Note that this is NOT the same as the program's current working directory; this is never changed by iash. Internally, iash stores a reference to another directory that is specified by the current user to work in.

Returns
a pointer to a Directory object representing the shell's current working directory.

◆ getEnv()

Environment * iash::getEnv ( )

Gets the current Environment object for this shell instance.

Returns
a pointer to the current Environment for this shell

◆ runInteractive()

int iash::runInteractive ( )

Runs the shell in interactive mode.

This will print out a command prompt to stdout and read the user's commands in via stdin.

When running iash in interactive mode, this should be called directly in the return statement for main, i.e.:

return shell.runInteractive();
Returns
the exit status of the shell. Presently, this will always be 0.

◆ runScript()

int iash::runScript ( const char *  fname)

Executes the commands in the specified script and quits.

Command I/O will be done from/to stdin/stdout unless otherwise specified by the script.

iash scripts are identified by having this line at the top of the file:

#!iash@appname

where appname is the configured name of the application. If the @appname specifier is missing, by default iash will refuse to run the script.

Parameters
fnamethe filename (absolute or relative to the application's current working directory) of the script to run.
Returns
the exit status of the last command to execute.

The documentation for this class was generated from the following files: