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

Represents an execution environment for a shell instance. More...

#include <shell_session.h>

Public Types

using func_type = shell_vtable::func_type
 Sell function type.
 

Public Member Functions

 shell_session (const shell *pShell, std::istream &oStdIn, std::ostream &oStdOut, std::ostream &oStdErr)
 Construct a new shell session with fresh env/var/arg.
 
 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.
 
virtual ~shell_session ()=default
 Virtual destructor for subclassing.
 
const shellget_shell () const noexcept
 Get the pointer to the underlying shell.
 
shell_envenv () noexcept
 Get modifiable environment.
 
const shell_envenv () const noexcept
 Get const environment.
 
bool has_env (const std::string &sVariable) const noexcept
 Check whether an environment variable exists.
 
std::string get_env (const std::string &sVariable) const
 Retrieve an environment variable.
 
std::string get_env_hop2 (const std::string &sVariable) const
 Retrieve an environment variable using hop-2 resolution.
 
void set_env (const std::string &sVariable, std::string sValue)
 Set an environment variable.
 
const shell_varvar () const noexcept
 Get const variable table.
 
shell_varvar () noexcept
 Get modifiable variable table.
 
bool has_var (const std::string &sVariable) const noexcept
 Check whether a local variable exists.
 
std::string get_var (const std::string &sVariable) const
 Retrieve a local variable.
 
std::string get_var_hop2 (const std::string &sVariable) const
 Retrieve a local variable using hop-2 resolution.
 
void set_var (const std::string &sVariable, std::string sValue)
 Set a local variable.
 
const shell_argarg () const noexcept
 Get const argument list.
 
shell_argarg () noexcept
 Get modifiable argument list.
 
std::string get_arg (const std::size_t nArg) const
 Retrieve a specific argument.
 
std::size_t get_arg_size () const
 Get number of arguments.
 
const std::vector< std::string > & get_args () const noexcept
 Get the full argument list.
 
std::istream & in () const noexcept
 Get stdin stream.
 
std::ostream & out () const noexcept
 Get stdout stream.
 
std::ostream & err () const noexcept
 Get stderr stream.
 
shell_status get_last_command_result () const noexcept
 Get status of last executed command.
 
void set_last_command_result (const shell_status nLastCommandResult) noexcept
 Set status of last executed command.
 
virtual std::unique_ptr< shell_sessionmake_subsession (std::istream &oStdIn, std::ostream &oStdOut, std::ostream &oStdErr)
 Create a subsession with fresh streams but copied env/args/vars.
 
virtual std::unique_ptr< shell_sessionmake_function_call (shell_arg oArg)
 Create a session for function calls.
 
virtual std::unique_ptr< shell_sessionmake_pipe_left (std::ostringstream &oStdOut)
 Create a session representing the left side of a pipe.
 
virtual std::unique_ptr< shell_sessionmake_pipe_right (std::istringstream &oStdIn)
 Create a session representing the right side of a pipe.
 
const func_typeget_func (const std::string &sVar) const
 Gets a function.
 
void set_func (const std::string &sName, const func_type *pFunction)
 Sets the a function.
 
bool has_func (const std::string &sName) const noexcept
 Checks if a function exists.
 
std::size_t get_vtable_size () const noexcept
 Gets the number of functions in the vtable.
 
std::size_t get_current_shell_depth () const noexcept
 Gets the current shell depth.
 
void decrease_shell_depth () noexcept
 Decreases the current shell depth.
 
bool increase_shell_depth () noexcept
 Tries to increase the shell depth.
 

Protected Member Functions

 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.
 

Protected Attributes

std::shared_ptr< shell_envm_pEnv
 Shared environment.
 
std::shared_ptr< shell_argm_pArg
 Argument list.
 
std::shared_ptr< shell_varm_pVar
 Local variables.
 
std::shared_ptr< shell_vtablem_pVtable
 Function vTable.
 
shell_status m_nLastCommandResult
 Last return status.
 

Detailed Description

Represents an execution environment for a shell instance.

A shell_session encapsulates the streams (stdin/stdout/stderr), the environment variables (shell_env), local shell variables (shell_var), and argument lists (shell_arg) used by the running shell.

It provides the interface for creating subsessions, function-call sessions, and pipe-left/right sessions.

Constructor & Destructor Documentation

◆ shell_session() [1/2]

bs::shell_session::shell_session ( const shell pShell,
std::istream &  oStdIn,
std::ostream &  oStdOut,
std::ostream &  oStdErr 
)
inline

Construct a new shell session with fresh env/var/arg.

Parameters
pShellPointer to the associated shell.
oStdInInput stream for the session.
oStdOutOutput stream for the session.
oStdErrError stream for the session.

◆ shell_session() [2/2]

bs::shell_session::shell_session ( const shell pBash,
std::istream &  oStdIn,
std::ostream &  oStdOut,
std::ostream &  oStdErr,
shell_env  oEnv,
shell_arg  oArg 
)
inline

Construct a new shell session providing initial env and args.

Parameters
pBashPointer to the associated shell.
oStdInInput stream for the session.
oStdOutOutput stream for the session.
oStdErrError stream for the session.
oEnvInitial environment object.
oArgInitial argument object.

Member Function Documentation

◆ get_arg()

std::string bs::shell_session::get_arg ( const std::size_t  nArg) const
inline

Retrieve a specific argument.

Parameters
nArgIndex of argument.
Returns
Argument string (empty if out of bounds).

◆ get_args()

const std::vector< std::string > & bs::shell_session::get_args ( ) const
inlinenoexcept

Get the full argument list.

Returns
const reference to vector of strings.

◆ get_current_shell_depth()

std::size_t bs::shell_session::get_current_shell_depth ( ) const
inlinenoexcept

Gets the current shell depth.

Returns
Current shell depth

◆ get_env()

std::string bs::shell_session::get_env ( const std::string &  sVariable) const
inline

Retrieve an environment variable.

Parameters
sVariableVariable name.
Returns
The value of the variable, or empty string if nonexistent.

◆ get_env_hop2()

std::string bs::shell_session::get_env_hop2 ( const std::string &  sVariable) const
inline

Retrieve an environment variable using hop-2 resolution.

Hop-2 resolution is implementation-dependent behavior from shell_env.

Parameters
sVariableVariable name.
Returns
The resolved value.

◆ get_func()

const func_type * bs::shell_session::get_func ( const std::string &  sVar) const
inline

Gets a function.

Parameters
sVarThe name of the function to get
Returns
The function if there is or nullptr

◆ get_shell()

const shell * bs::shell_session::get_shell ( ) const
inlinenoexcept

Get the pointer to the underlying shell.

Returns
const shell* The associated shell instance.

◆ get_var()

std::string bs::shell_session::get_var ( const std::string &  sVariable) const
inline

Retrieve a local variable.

Parameters
sVariableVariable name.

◆ get_var_hop2()

std::string bs::shell_session::get_var_hop2 ( const std::string &  sVariable) const
inline

Retrieve a local variable using hop-2 resolution.

Parameters
sVariableVariable name.

◆ get_vtable_size()

std::size_t bs::shell_session::get_vtable_size ( ) const
inlinenoexcept

Gets the number of functions in the vtable.

Returns
The size of the function map.

◆ has_env()

bool bs::shell_session::has_env ( const std::string &  sVariable) const
inlinenoexcept

Check whether an environment variable exists.

Parameters
sVariableVariable name.
Returns
true if the variable exists.

◆ has_func()

bool bs::shell_session::has_func ( const std::string &  sName) const
inlinenoexcept

Checks if a function exists.

Parameters
sNameThe name of the function.
Returns
True if the function exists, false otherwise.

◆ has_var()

bool bs::shell_session::has_var ( const std::string &  sVariable) const
inlinenoexcept

Check whether a local variable exists.

Parameters
sVariableVariable name.

◆ increase_shell_depth()

bool bs::shell_session::increase_shell_depth ( )
inlinenoexcept

Tries to increase the shell depth.

Returns
Whether the shell depth could be increased

◆ make_function_call()

virtual std::unique_ptr< shell_session > bs::shell_session::make_function_call ( shell_arg  oArg)
inlinevirtual

Create a session for function calls.

Keeps env and streams but provides new arguments and a new variable table.

Parameters
oArgArgument list to use for the function call.

◆ make_pipe_left()

virtual std::unique_ptr< shell_session > bs::shell_session::make_pipe_left ( std::ostringstream &  oStdOut)
inlinevirtual

Create a session representing the left side of a pipe.

Output is redirected to the given ostringstream.

Parameters
oStdOutOutput buffer receiving piped data.

◆ make_pipe_right()

virtual std::unique_ptr< shell_session > bs::shell_session::make_pipe_right ( std::istringstream &  oStdIn)
inlinevirtual

Create a session representing the right side of a pipe.

Input is taken from the given istringstream.

Parameters
oStdInInput buffer from previous pipe stage.

◆ make_subsession()

virtual std::unique_ptr< shell_session > bs::shell_session::make_subsession ( std::istream &  oStdIn,
std::ostream &  oStdOut,
std::ostream &  oStdErr 
)
inlinevirtual

Create a subsession with fresh streams but copied env/args/vars.

Used for subshells. Implementations may extend the stored data, such as by adding a working directory.

Parameters
oStdInNew input stream.
oStdOutNew output stream.
oStdErrNew error stream.
Returns
Newly allocated independent session.

◆ set_env()

void bs::shell_session::set_env ( const std::string &  sVariable,
std::string  sValue 
)
inline

Set an environment variable.

Parameters
sVariableVariable name.
sValueValue to assign.

◆ set_func()

void bs::shell_session::set_func ( const std::string &  sName,
const func_type pFunction 
)
inline

Sets the a function.

Parameters
sNameThe name of the function.
pFunctionThe function to be set.

◆ set_var()

void bs::shell_session::set_var ( const std::string &  sVariable,
std::string  sValue 
)
inline

Set a local variable.

Parameters
sVariableVariable name.
sValueAssigned value.

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