BashSpark
Loading...
Searching...
No Matches
shell.h
Go to the documentation of this file.
1
35#pragma once
36
37#include <memory>
38#include <string_view>
39
40#include "BashSpark/command.h"
44
45namespace bs {
63 class shell {
64 public:
66 constexpr static std::size_t MAX_DEPTH = SHELL_MAX_DEPTH;
67
69 constexpr static std::string_view BASH_ERROR_SYNTAX{"BASH_ERROR_SYNTAX"};
70
72 constexpr static std::string_view BASH_ERROR_COMMAND_NOT_FOUND{"BASH_ERROR_COMMAND_NOT_FOUND"};
73
75 constexpr static std::size_t DEFAULT_COMMAND_HASH_TABLE_SIZE = 1024;
76
77 public:
91 static std::unique_ptr<shell> make_default_shell();
92
93 public:
98 : m_mCommands(DEFAULT_COMMAND_HASH_TABLE_SIZE) {
99 }
100
104 virtual ~shell() = default;
105
106 public:
113 static shell_status run(
114 std::istream &oCommand,
115 shell_session &oSession
116 );
117
124 static shell_status run(
125 const std::string &sCommand,
126 shell_session &oSession
127 );
128
135 static shell_status run(
136 const std::string_view &sCommand,
137 shell_session &oSession
138 );
139
140 public:
146 [[nodiscard]] const command *get_command(const std::string &sCommand) const noexcept;
147
152 void set_command(std::unique_ptr<command> &&pCommand);
153
164 template<typename CommandT, typename... Args>
165 void set_command(Args &&... args) {
166 static_assert(std::is_base_of_v<command, CommandT>,
167 "Template set_command only takes command derived classes.");
168 std::unique_ptr<command> pCommand(new CommandT(std::forward<Args>(args)...));
169 this->set_command(std::move(pCommand));
170 }
171
177 [[nodiscard]] std::unique_ptr<command> remove_command(const std::string &sCommand);
178
183 void erase_command(const std::string &sCommand);
184
185 public:
190 [[nodiscard]] bool get_stop_on_command_not_found() const noexcept;
191
196 void set_stop_on_command_not_found(bool bStopOnCommandNotFound) noexcept;
197
198 public:
207 virtual void msg_error_command_not_found(shell_session &oSession, const std::string &sCommand) const;
208
217 virtual void msg_error_invalid_function_name(shell_session &oSession, const std::string &sFunction) const;
218
227 virtual void msg_error_syntax_error(
228 const shell_session &oSession,
229 const shell_parser_exception &oException
230 ) const;
231
232 private:
234 std::unordered_map<std::string, std::unique_ptr<command>, shell_hash> m_mCommands;
236 mutable std::mutex m_oExecutionMutex;
238 bool m_bStopOnCommandNotFound = true;
239 };
240}
Abstract base class for all shell commands.
Definition command.h:50
Exception class for handling shell-specific errors.
Definition shell_parser_exception.h:45
Represents an execution environment for a shell instance.
Definition shell_session.h:57
A simplified shell environment inspired by Bash.
Definition shell.h:63
static constexpr std::size_t MAX_DEPTH
Maximum depth the command interpreter can reach.
Definition shell.h:66
virtual void msg_error_syntax_error(const shell_session &oSession, const shell_parser_exception &oException) const
Displays the error message for “syntax errors”.
Definition shell.cpp:111
virtual void msg_error_command_not_found(shell_session &oSession, const std::string &sCommand) const
Displays the error message for “command not found”.
Definition shell.cpp:103
void erase_command(const std::string &sCommand)
Erases a command by its string.
Definition shell.cpp:86
virtual void msg_error_invalid_function_name(shell_session &oSession, const std::string &sFunction) const
Displays the error message for “function name invalid”.
Definition shell.cpp:107
bool get_stop_on_command_not_found() const noexcept
Checks if execution stops on command not found.
Definition shell.cpp:95
shell()
Construct bash object.
Definition shell.h:97
void set_stop_on_command_not_found(bool bStopOnCommandNotFound) noexcept
Sets whether to stop execution on command not found.
Definition shell.cpp:99
void set_command(Args &&... args)
Creates and sets a command of type CommandT.
Definition shell.h:165
std::unique_ptr< command > remove_command(const std::string &sCommand)
Removes a command and returns it.
Definition shell.cpp:76
static constexpr std::string_view BASH_ERROR_COMMAND_NOT_FOUND
Message name for bash command not found.
Definition shell.h:72
static shell_status run(std::istream &oCommand, shell_session &oSession)
Runs a command or script.
Definition shell.cpp:150
const command * get_command(const std::string &sCommand) const noexcept
Retrieves a command pointer by its string.
Definition shell.cpp:65
static constexpr std::size_t DEFAULT_COMMAND_HASH_TABLE_SIZE
Default size for the command hash table.
Definition shell.h:75
static constexpr std::string_view BASH_ERROR_SYNTAX
Message name for bash syntax error.
Definition shell.h:69
static std::unique_ptr< shell > make_default_shell()
Construct bash object.
Definition shell.cpp:50
void set_command(std::unique_ptr< command > &&pCommand)
Sets a command using a unique pointer. Overwrites previous command.
Definition shell.cpp:72
virtual ~shell()=default
Destruct bash object.
Defines the bs::command interface and built-in shell commands.
BashSpark main namespace.
Definition command.h:39
constexpr std::size_t SHELL_MAX_DEPTH
Maximum depth allowed for shell.
Definition shell_status.h:41
shell_status
Shell status codes.
Definition shell_status.h:47
Defines the bs::shell_hash functor implementing a 64-bit FNV-1a hash.
Defines class shell_exception.
Defines the class bs::shell_session.
Custom hash functor for strings using the 64-bit FNV-1a algorithm.
Definition shell_hash.h:46