|
BashSpark
|
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 command * | get_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< command > | remove_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< shell > | make_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. | |
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.
| void bs::shell::erase_command | ( | const std::string & | sCommand | ) |
Erases a command by its string.
| sCommand | The command string to erase. |
|
noexcept |
Retrieves a command pointer by its string.
| sCommand | The command string. |
|
noexcept |
Checks if execution stops on command not found.
|
static |
Construct bash object.
Included commands: Command echo Command eval Command getenv Command getvar Command setenv Command setvar Command seq Command test
|
virtual |
Displays the error message for “command not found”.
Can be overwritten with custom behaviour.
| oSession | Shell session |
| sCommand | Command not found |
|
virtual |
Displays the error message for “function name invalid”.
Can be overwritten with custom behaviour.
| oSession | Shell session |
| sFunction | Invalid function name |
|
virtual |
Displays the error message for “syntax errors”.
Can be overwritten with custom behaviour.
| oSession | Shell session |
| oException | Syntax error |
| std::unique_ptr< command > bs::shell::remove_command | ( | const std::string & | sCommand | ) |
Removes a command and returns it.
| sCommand | The command string. |
|
static |
Runs a command or script.
| sCommand | Command or script to run |
| oSession | Shell session |
|
static |
Runs a command or script.
| sCommand | Command or script to run |
| oSession | Shell session |
|
static |
Runs a command or script.
| oCommand | Command or script to run |
| oSession | Shell session |
|
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.
| CommandT | The derived command type. |
| args | Arguments to initialize the command. |
| std::logic_error | if CommandT is not derived from command. |
| void bs::shell::set_command | ( | std::unique_ptr< command > && | pCommand | ) |
Sets a command using a unique pointer. Overwrites previous command.
| pCommand | Unique pointer to the command. |
|
noexcept |
Sets whether to stop execution on command not found.
| bStopOnCommandNotFound | If true, execution stops; if false, it continues. |