BashSpark
Loading...
Searching...
No Matches
shell_session.h
Go to the documentation of this file.
1
30#pragma once
31
32#include <functional>
33#include <iosfwd>
34#include <sstream>
35#include <memory>
36
37#include "shell_vtable.h"
42
43namespace bs {
44 class shell;
45
58 public:
61
62 public:
72 const shell *pShell,
73 std::istream &oStdIn,
74 std::ostream &oStdOut,
75 std::ostream &oStdErr
76 )
77 : m_pEnv(std::make_shared<shell_env>()),
78 m_pArg(std::make_shared<shell_arg>()),
79 m_pVar(std::make_shared<shell_var>()),
80 m_pVtable(std::make_shared<shell_vtable>()),
82 m_pShell(pShell),
83 m_oStdIn(oStdIn),
84 m_oStdOut(std::ref(oStdOut)),
85 m_oStdErr(std::ref(oStdErr)) {
86 }
87
99 const shell *pBash,
100 std::istream &oStdIn,
101 std::ostream &oStdOut,
102 std::ostream &oStdErr,
103 shell_env oEnv,
104 shell_arg oArg
105 )
106 : m_pEnv(std::make_shared<shell_env>(std::move(oEnv))),
107 m_pArg(std::make_shared<shell_arg>(std::move(oArg))),
108 m_pVar(std::make_shared<shell_var>()),
110 m_pShell(pBash),
111 m_oStdIn(oStdIn),
112 m_oStdOut(std::ref(oStdOut)),
113 m_oStdErr(std::ref(oStdErr)) {
114 }
115
117 virtual ~shell_session() = default;
118
119 // @section shell_pointer Shell Pointer
120
125 [[nodiscard]] const shell *get_shell() const noexcept {
126 return m_pShell;
127 }
128
129 // @section shell_env Environment
130
132 [[nodiscard]] shell_env &env() noexcept { return *m_pEnv; }
133
135 [[nodiscard]] const shell_env &env() const noexcept { return *m_pEnv; }
136
142 [[nodiscard]] bool has_env(const std::string &sVariable) const noexcept {
143 return m_pEnv->has_env(sVariable);
144 }
145
151 [[nodiscard]] std::string get_env(const std::string &sVariable) const {
152 return m_pEnv->get_env(sVariable);
153 }
154
163 [[nodiscard]] std::string get_env_hop2(const std::string &sVariable) const {
164 return m_pEnv->get_env_hop2(sVariable);
165 }
166
172 void set_env(const std::string &sVariable, std::string sValue) {
173 m_pEnv->set_env(sVariable, std::move(sValue));
174 }
175
176 // @section shell_var Variables
177
179 [[nodiscard]] const shell_var &var() const noexcept { return *m_pVar; }
180
182 [[nodiscard]] shell_var &var() noexcept { return *m_pVar; }
183
188 [[nodiscard]] bool has_var(const std::string &sVariable) const noexcept {
189 return m_pVar->has_var(sVariable);
190 }
191
196 [[nodiscard]] std::string get_var(const std::string &sVariable) const {
197 return m_pVar->get_var(sVariable);
198 }
199
204 [[nodiscard]] std::string get_var_hop2(const std::string &sVariable) const {
205 return m_pVar->get_var_hop2(sVariable);
206 }
207
213 void set_var(const std::string &sVariable, std::string sValue) {
214 m_pVar->set_var(sVariable, std::move(sValue));
215 }
216
217 // @section shell_arg Arguments
218
220 [[nodiscard]] const shell_arg &arg() const noexcept { return *m_pArg; }
221
223 [[nodiscard]] shell_arg &arg() noexcept { return *m_pArg; }
224
230 [[nodiscard]] std::string get_arg(const std::size_t nArg) const {
231 return m_pArg->get_arg(nArg);
232 }
233
237 [[nodiscard]] std::size_t get_arg_size() const {
238 return m_pArg->get_arg_size();
239 }
240
245 [[nodiscard]] const std::vector<std::string> &get_args() const noexcept {
246 return m_pArg->get_args();
247 }
248
249 // @section streams Streams
250
252 [[nodiscard]] std::istream &in() const noexcept { return m_oStdIn; }
253
255 [[nodiscard]] std::ostream &out() const noexcept { return m_oStdOut; }
256
258 [[nodiscard]] std::ostream &err() const noexcept { return m_oStdErr; }
259
260 // @section shell_status Status
261
265 [[nodiscard]] shell_status get_last_command_result() const noexcept {
267 }
268
272 void set_last_command_result(const shell_status nLastCommandResult) noexcept {
273 m_nLastCommandResult = nLastCommandResult;
274 }
275
276 // @section shell_subsession Subsession creation
277
290 virtual std::unique_ptr<shell_session> make_subsession(
291 std::istream &oStdIn,
292 std::ostream &oStdOut,
293 std::ostream &oStdErr
294 ) {
295 return std::unique_ptr<shell_session>(new shell_session(
296 m_pShell,
297 oStdIn,
298 oStdOut,
299 oStdErr,
300 std::make_shared<shell_env>(*m_pEnv),
301 m_pArg,
302 std::make_shared<shell_var>(*m_pVar),
303 std::make_shared<shell_vtable>(*m_pVtable)
304 ));
305 }
306
314 virtual std::unique_ptr<shell_session> make_function_call(shell_arg oArg) {
315 std::cout << "fcall " << m_pArg->get_arg(1) << std::endl;
316 return std::unique_ptr<shell_session>(new shell_session(
317 m_pShell, m_oStdIn, m_oStdOut, m_oStdErr,
318 m_pEnv,
319 std::make_shared<shell_arg>(std::move(oArg)),
320 std::make_shared<shell_var>(),
322 ));
323 }
324
332 virtual std::unique_ptr<shell_session> make_pipe_left(
333 std::ostringstream &oStdOut
334 ) {
335 return std::unique_ptr<shell_session>(new shell_session(
336 m_pShell, m_oStdIn, oStdOut, m_oStdErr,
338 ));
339 }
340
348 virtual std::unique_ptr<shell_session> make_pipe_right(
349 std::istringstream &oStdIn
350 ) {
351 return std::unique_ptr<shell_session>(new shell_session(
352 m_pShell,
353 oStdIn,
354 m_oStdOut,
355 m_oStdErr,
356 m_pEnv,
357 m_pArg,
358 m_pVar,
360 ));
361 }
362
363 // @section vtable Function vtable
364
370 [[nodiscard]] const func_type *get_func(const std::string &sVar) const {
371 return this->m_pVtable->get_func(sVar);
372 }
373
379 void set_func(const std::string &sName, const func_type *pFunction) {
380 this->m_pVtable->set_func(sName, pFunction);
381 }
382
388 [[nodiscard]] bool has_func(const std::string &sName) const noexcept {
389 return this->m_pVtable->has_func(sName);
390 }
391
396 [[nodiscard]] std::size_t get_vtable_size() const noexcept {
397 return this->m_pVtable->get_vtable_size();
398 }
399
400 // @section depth Shell Depth
401
402 public:
407 [[nodiscard]] std::size_t get_current_shell_depth() const noexcept {
408 return this->m_nCurrentDepth;
409 }
410
414 void decrease_shell_depth() noexcept {
415 if (this->m_nCurrentDepth > 0)this->m_nCurrentDepth--;
416 }
417
422 bool increase_shell_depth() noexcept {
423 if (this->m_nCurrentDepth + 1 <= SHELL_MAX_DEPTH) {
424 this->m_nCurrentDepth++;
425 return true;
426 }
427 return false;
428 }
429
430 protected:
435 const shell *pBash,
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
443 )
444 : m_pEnv(std::move(pEnv)),
445 m_pArg(std::move(pArg)),
446 m_pVar(std::move(pVar)),
447 m_pVtable(std::move(pVtable)),
449 m_pShell(pBash),
450 m_oStdIn(std::ref(oStdIn)),
451 m_oStdOut(std::ref(oStdOut)),
452 m_oStdErr(std::ref(oStdErr)) {
453 }
454
455 protected:
457 std::shared_ptr<shell_env> m_pEnv;
459 std::shared_ptr<shell_arg> m_pArg;
461 std::shared_ptr<shell_var> m_pVar;
463 std::shared_ptr<shell_vtable> m_pVtable;
466
467 private:
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;
478 };
479} // namespace bs
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.