74 std::ostream &oStdOut,
84 m_oStdOut(std::ref(oStdOut)),
85 m_oStdErr(std::ref(oStdErr)) {
100 std::istream &oStdIn,
101 std::ostream &oStdOut,
102 std::ostream &oStdErr,
112 m_oStdOut(std::ref(oStdOut)),
113 m_oStdErr(std::ref(oStdErr)) {
142 [[nodiscard]]
bool has_env(
const std::string &sVariable)
const noexcept {
143 return m_pEnv->has_env(sVariable);
151 [[nodiscard]] std::string
get_env(
const std::string &sVariable)
const {
152 return m_pEnv->get_env(sVariable);
163 [[nodiscard]] std::string
get_env_hop2(
const std::string &sVariable)
const {
164 return m_pEnv->get_env_hop2(sVariable);
172 void set_env(
const std::string &sVariable, std::string sValue) {
173 m_pEnv->set_env(sVariable, std::move(sValue));
188 [[nodiscard]]
bool has_var(
const std::string &sVariable)
const noexcept {
189 return m_pVar->has_var(sVariable);
196 [[nodiscard]] std::string
get_var(
const std::string &sVariable)
const {
197 return m_pVar->get_var(sVariable);
204 [[nodiscard]] std::string
get_var_hop2(
const std::string &sVariable)
const {
205 return m_pVar->get_var_hop2(sVariable);
213 void set_var(
const std::string &sVariable, std::string sValue) {
214 m_pVar->set_var(sVariable, std::move(sValue));
230 [[nodiscard]] std::string
get_arg(
const std::size_t nArg)
const {
231 return m_pArg->get_arg(nArg);
238 return m_pArg->get_arg_size();
245 [[nodiscard]]
const std::vector<std::string> &
get_args() const noexcept {
246 return m_pArg->get_args();
252 [[nodiscard]] std::istream &
in() const noexcept {
return m_oStdIn; }
255 [[nodiscard]] std::ostream &
out() const noexcept {
return m_oStdOut; }
258 [[nodiscard]] std::ostream &
err() const noexcept {
return m_oStdErr; }
291 std::istream &oStdIn,
292 std::ostream &oStdOut,
293 std::ostream &oStdErr
300 std::make_shared<shell_env>(*
m_pEnv),
302 std::make_shared<shell_var>(*
m_pVar),
303 std::make_shared<shell_vtable>(*
m_pVtable)
315 std::cout <<
"fcall " <<
m_pArg->get_arg(1) << std::endl;
317 m_pShell, m_oStdIn, m_oStdOut, m_oStdErr,
319 std::make_shared<shell_arg>(std::move(oArg)),
320 std::make_shared<shell_var>(),
333 std::ostringstream &oStdOut
336 m_pShell, m_oStdIn, oStdOut, m_oStdErr,
349 std::istringstream &oStdIn
380 this->
m_pVtable->set_func(sName, pFunction);
388 [[nodiscard]]
bool has_func(
const std::string &sName)
const noexcept {
397 return this->
m_pVtable->get_vtable_size();
408 return this->m_nCurrentDepth;
415 if (this->m_nCurrentDepth > 0)this->m_nCurrentDepth--;
424 this->m_nCurrentDepth++;
436 std::istream &oStdIn,
437 std::ostream &oStdOut,
438 std::ostream &oStdErr,
439 std::shared_ptr<shell_env> pEnv,
440 std::shared_ptr<shell_arg> pArg,
441 std::shared_ptr<shell_var> pVar,
442 std::shared_ptr<shell_vtable> pVtable
444 :
m_pEnv(std::move(pEnv)),
450 m_oStdIn(std::ref(oStdIn)),
451 m_oStdOut(std::ref(oStdOut)),
452 m_oStdErr(std::ref(oStdErr)) {
469 const shell *m_pShell;
471 std::reference_wrapper<std::istream> m_oStdIn;
473 std::reference_wrapper<std::ostream> m_oStdOut;
475 std::reference_wrapper<std::ostream> m_oStdErr;
477 std::size_t m_nCurrentDepth = 0;
Represents a command line argument list for shell commands.
Definition shell_arg.h:42
Represents an environment for shell commands.
Definition shell_env.h:42
Base interface for nodes that can be evaluated (executed).
Definition shell_node.h:164
Represents an execution environment for a shell instance.
Definition shell_session.h:57
std::string get_arg(const std::size_t nArg) const
Retrieve a specific argument.
Definition shell_session.h:230
const shell * get_shell() const noexcept
Get the pointer to the underlying shell.
Definition shell_session.h:125
bool has_func(const std::string &sName) const noexcept
Checks if a function exists.
Definition shell_session.h:388
virtual std::unique_ptr< shell_session > make_function_call(shell_arg oArg)
Create a session for function calls.
Definition shell_session.h:314
const shell_env & env() const noexcept
Get const environment.
Definition shell_session.h:135
virtual ~shell_session()=default
Virtual destructor for subclassing.
std::shared_ptr< shell_vtable > m_pVtable
Function vTable.
Definition shell_session.h:463
std::ostream & err() const noexcept
Get stderr stream.
Definition shell_session.h:258
shell_env & env() noexcept
Get modifiable environment.
Definition shell_session.h:132
virtual std::unique_ptr< shell_session > make_subsession(std::istream &oStdIn, std::ostream &oStdOut, std::ostream &oStdErr)
Create a subsession with fresh streams but copied env/args/vars.
Definition shell_session.h:290
bool has_var(const std::string &sVariable) const noexcept
Check whether a local variable exists.
Definition shell_session.h:188
void set_env(const std::string &sVariable, std::string sValue)
Set an environment variable.
Definition shell_session.h:172
void decrease_shell_depth() noexcept
Decreases the current shell depth.
Definition shell_session.h:414
shell_status m_nLastCommandResult
Last return status.
Definition shell_session.h:465
shell_status get_last_command_result() const noexcept
Get status of last executed command.
Definition shell_session.h:265
void set_last_command_result(const shell_status nLastCommandResult) noexcept
Set status of last executed command.
Definition shell_session.h:272
void set_var(const std::string &sVariable, std::string sValue)
Set a local variable.
Definition shell_session.h:213
std::string get_var_hop2(const std::string &sVariable) const
Retrieve a local variable using hop-2 resolution.
Definition shell_session.h:204
std::size_t get_vtable_size() const noexcept
Gets the number of functions in the vtable.
Definition shell_session.h:396
std::ostream & out() const noexcept
Get stdout stream.
Definition shell_session.h:255
std::shared_ptr< shell_env > m_pEnv
Shared environment.
Definition shell_session.h:457
virtual std::unique_ptr< shell_session > make_pipe_right(std::istringstream &oStdIn)
Create a session representing the right side of a pipe.
Definition shell_session.h:348
virtual std::unique_ptr< shell_session > make_pipe_left(std::ostringstream &oStdOut)
Create a session representing the left side of a pipe.
Definition shell_session.h:332
const func_type * get_func(const std::string &sVar) const
Gets a function.
Definition shell_session.h:370
bool increase_shell_depth() noexcept
Tries to increase the shell depth.
Definition shell_session.h:422
void set_func(const std::string &sName, const func_type *pFunction)
Sets the a function.
Definition shell_session.h:379
std::string get_var(const std::string &sVariable) const
Retrieve a local variable.
Definition shell_session.h:196
std::shared_ptr< shell_var > m_pVar
Local variables.
Definition shell_session.h:461
std::size_t get_arg_size() const
Get number of arguments.
Definition shell_session.h:237
std::string get_env_hop2(const std::string &sVariable) const
Retrieve an environment variable using hop-2 resolution.
Definition shell_session.h:163
std::string get_env(const std::string &sVariable) const
Retrieve an environment variable.
Definition shell_session.h:151
const std::vector< std::string > & get_args() const noexcept
Get the full argument list.
Definition shell_session.h:245
shell_session(const shell *pBash, std::istream &oStdIn, std::ostream &oStdOut, std::ostream &oStdErr, std::shared_ptr< shell_env > pEnv, std::shared_ptr< shell_arg > pArg, std::shared_ptr< shell_var > pVar, std::shared_ptr< shell_vtable > pVtable)
Protected constructor used by session-creation helpers.
Definition shell_session.h:434
std::size_t get_current_shell_depth() const noexcept
Gets the current shell depth.
Definition shell_session.h:407
std::shared_ptr< shell_arg > m_pArg
Argument list.
Definition shell_session.h:459
shell_var & var() noexcept
Get modifiable variable table.
Definition shell_session.h:182
shell_session(const shell *pShell, std::istream &oStdIn, std::ostream &oStdOut, std::ostream &oStdErr)
Construct a new shell session with fresh env/var/arg.
Definition shell_session.h:71
const shell_arg & arg() const noexcept
Get const argument list.
Definition shell_session.h:220
shell_session(const shell *pBash, std::istream &oStdIn, std::ostream &oStdOut, std::ostream &oStdErr, shell_env oEnv, shell_arg oArg)
Construct a new shell session providing initial env and args.
Definition shell_session.h:98
bool has_env(const std::string &sVariable) const noexcept
Check whether an environment variable exists.
Definition shell_session.h:142
std::istream & in() const noexcept
Get stdin stream.
Definition shell_session.h:252
shell_arg & arg() noexcept
Get modifiable argument list.
Definition shell_session.h:223
const shell_var & var() const noexcept
Get const variable table.
Definition shell_session.h:179
Represents a variable map for shell commands.
Definition shell_var.h:42
Represents an environment for shell commands.
Definition shell_vtable.h:49
shell_node_evaluable func_type
Sell function type.
Definition shell_vtable.h:52
A simplified shell environment inspired by Bash.
Definition shell.h:63
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
@ SHELL_SUCCESS
Indicates successful execution.
Argument list class for managing shell commands.
Environment class for managing shell commands.
Defines status codes for the shell.
Variable class for managing shell commands.
Function table class for managing shell commands.