BashSpark
Loading...
Searching...
No Matches
bs::shell Class Reference

A simplified shell environment inspired by Bash. More...

#include <shell.h>

Public Member Functions

 shell ()
 Construct bash object.
 
virtual ~shell ()=default
 Destruct bash object.
 
const commandget_command (const std::string &sCommand) const noexcept
 Retrieves a command pointer by its string.
 
void set_command (std::unique_ptr< command > &&pCommand)
 Sets a command using a unique pointer. Overwrites previous command.
 
template<typename CommandT , typename... Args>
void set_command (Args &&... args)
 Creates and sets a command of type CommandT.
 
std::unique_ptr< commandremove_command (const std::string &sCommand)
 Removes a command and returns it.
 
void erase_command (const std::string &sCommand)
 Erases a command by its string.
 
bool get_stop_on_command_not_found () const noexcept
 Checks if execution stops on command not found.
 
void set_stop_on_command_not_found (bool bStopOnCommandNotFound) noexcept
 Sets whether to stop execution on command not found.
 
virtual void msg_error_command_not_found (shell_session &oSession, const std::string &sCommand) const
 Displays the error message for “command not found”.
 
virtual void msg_error_invalid_function_name (shell_session &oSession, const std::string &sFunction) const
 Displays the error message for “function name invalid”.
 
virtual void msg_error_syntax_error (const shell_session &oSession, const shell_parser_exception &oException) const
 Displays the error message for “syntax errors”.
 

Static Public Member Functions

static std::unique_ptr< shellmake_default_shell ()
 Construct bash object.
 
static shell_status run (std::istream &oCommand, shell_session &oSession)
 Runs a command or script.
 
static shell_status run (const std::string &sCommand, shell_session &oSession)
 Runs a command or script.
 
static shell_status run (const std::string_view &sCommand, shell_session &oSession)
 Runs a command or script.
 

Static Public Attributes

static constexpr std::size_t MAX_DEPTH = SHELL_MAX_DEPTH
 Maximum depth the command interpreter can reach.
 
static constexpr std::string_view BASH_ERROR_SYNTAX {"BASH_ERROR_SYNTAX"}
 Message name for bash syntax error.
 
static constexpr std::string_view BASH_ERROR_COMMAND_NOT_FOUND {"BASH_ERROR_COMMAND_NOT_FOUND"}
 Message name for bash command not found.
 
static constexpr std::size_t DEFAULT_COMMAND_HASH_TABLE_SIZE = 1024
 Default size for the command hash table.
 

Detailed Description

A simplified shell environment inspired by Bash.

This class provides functionalities for command execution, command management, and error handling. Users can define and control commands while managing how errors are communicated.

The command management methods allow the programmer to strictly control the allowed behaviors, preventing security holes. This enhances the safety and reliability of command execution, ensuring that only authorized commands are executed and that errors are handled gracefully.

It is optimized in the find direction, while the construction is more costly. Uses a fixed size hash table bellow, which is expensive to initialize. You may modify ref shell::DEFAULT_COMMAND_HASH_TABLE_SIZE to change that behavior. In such case do not forget to rebuild the project.

Member Function Documentation

◆ erase_command()

void bs::shell::erase_command ( const std::string &  sCommand)

Erases a command by its string.

Parameters
sCommandThe command string to erase.

◆ get_command()

const command * bs::shell::get_command ( const std::string &  sCommand) const
noexcept

Retrieves a command pointer by its string.

Parameters
sCommandThe command string.
Returns
Pointer to the command, or nullptr if not found.

◆ get_stop_on_command_not_found()

bool bs::shell::get_stop_on_command_not_found ( ) const
noexcept

Checks if execution stops on command not found.

Returns
true if it stops; false otherwise.

◆ make_default_shell()

std::unique_ptr< shell > bs::shell::make_default_shell ( )
static

◆ msg_error_command_not_found()

void bs::shell::msg_error_command_not_found ( shell_session oSession,
const std::string &  sCommand 
) const
virtual

Displays the error message for “command not found”.

Can be overwritten with custom behaviour.

Parameters
oSessionShell session
sCommandCommand not found

◆ msg_error_invalid_function_name()

void bs::shell::msg_error_invalid_function_name ( shell_session oSession,
const std::string &  sFunction 
) const
virtual

Displays the error message for “function name invalid”.

Can be overwritten with custom behaviour.

Parameters
oSessionShell session
sFunctionInvalid function name

◆ msg_error_syntax_error()

void bs::shell::msg_error_syntax_error ( const shell_session oSession,
const shell_parser_exception oException 
) const
virtual

Displays the error message for “syntax errors”.

Can be overwritten with custom behaviour.

Parameters
oSessionShell session
oExceptionSyntax error

◆ remove_command()

std::unique_ptr< command > bs::shell::remove_command ( const std::string &  sCommand)

Removes a command and returns it.

Parameters
sCommandThe command string.
Returns
Unique pointer to the removed command, or nullptr if not found.

◆ run() [1/3]

shell_status bs::shell::run ( const std::string &  sCommand,
shell_session oSession 
)
static

Runs a command or script.

Parameters
sCommandCommand or script to run
oSessionShell session
Returns
Status code of last executed command

◆ run() [2/3]

shell_status bs::shell::run ( const std::string_view &  sCommand,
shell_session oSession 
)
static

Runs a command or script.

Parameters
sCommandCommand or script to run
oSessionShell session
Returns
Status code of last executed command

◆ run() [3/3]

shell_status bs::shell::run ( std::istream &  oCommand,
shell_session oSession 
)
static

Runs a command or script.

Parameters
oCommandCommand or script to run
oSessionShell session
Returns
Status code of last executed command

◆ set_command() [1/2]

template<typename CommandT , typename... Args>
void bs::shell::set_command ( Args &&...  args)
inline

Creates and sets a command of type CommandT.

This template function constructs a command derived from the base class command using the provided arguments and returns a unique pointer to it.

Template Parameters
CommandTThe derived command type.
Parameters
argsArguments to initialize the command.
Exceptions
std::logic_errorif CommandT is not derived from command.

◆ set_command() [2/2]

void bs::shell::set_command ( std::unique_ptr< command > &&  pCommand)

Sets a command using a unique pointer. Overwrites previous command.

Parameters
pCommandUnique pointer to the command.

◆ set_stop_on_command_not_found()

void bs::shell::set_stop_on_command_not_found ( bool  bStopOnCommandNotFound)
noexcept

Sets whether to stop execution on command not found.

Parameters
bStopOnCommandNotFoundIf true, execution stops; if false, it continues.

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