Snel
A shell implementation for A.U.TH (Operating Systems Lab)
|
#include <sequencer.h>
Public Types | |
enum | LogicOp { NONE =0, OR, AND } |
Enumerator for the logic operators between commands. | |
using | command_t = std::vector< std::string > |
A command type. | |
using | Error = std::runtime_error |
An error type. | |
Public Member Functions | |
~Child () | |
Destructor to free up all the resources. | |
Child (const command_t &c) | |
Child (command_t &&c) | |
Child & | make_arguments () |
A per child parser before execution. More... | |
bool | execute (std::vector< Child >::iterator it, bool first) |
Execute the child in separate process. More... | |
Pipe & | pipe () |
A pipe_ getter. | |
Private Member Functions | |
void | redirect_std_if (std::string fn, fd_t std_fd) |
Tool to redirect std. More... | |
void | restore_std_if (fd_t std_fd) |
Private Attributes | |
command_t | command {} |
Each command is a vector of string as passed on from Sequence::parse() More... | |
ArgList | arguments {} |
The after parsing, actual null terminated arguments to pass to execvp() | |
fd_t | files [3] = {-1, -1, -1} |
file descriptors for the user requested redirections | |
fd_t | sstd [3] = {-1, -1, -1} |
file descriptors to store std fd's before redirection | |
std::string | filenames [3] = {"", "", ""} |
filenames of the user requested redirections | |
LogicOp | logic {LogicOp::NONE} |
Logic flags to store && and || command separators. | |
Pipe | pipe_ |
An object to represent each process
We create Child objects from command_t
passed to constructor from Sequence::parse()
. In child we store information about:
(&&, ||) the command may have, to control the execution flow. Definition at line 98 of file sequencer.h.
bool snel::Child::execute | ( | std::vector< Child >::iterator | it, |
bool | first | ||
) |
Execute the child in separate process.
This is the hart of the snel. We use fork() - execvp()
pair to execute each child. In case of std redirection, the fd/dup2 handling is made in the parent. In case of piping, the fd handling is made in parent and the dup2() calls in the child process.
A child is a member of a vector containing the command chain. For ex:
is a chain with three childs (ls, cat, uname).
it | Iterator to the command chain execution |
first | flag to indicate the first command in the chain |
Definition at line 226 of file sequencer.cpp.
Child & snel::Child::make_arguments | ( | ) |
A per child parser before execution.
Parse the command of the current child to produce the actual arguments
Definition at line 167 of file sequencer.cpp.
|
private |
Tool to redirect std.
Private api
Redirect a std_fd
file descriptor to a file
fn | The file name to used as std |
std_fd | The file descriptor to redirect |
Definition at line 136 of file sequencer.cpp.
|
private |
Tool to restore std redirection
Restore and redirected std file descriptor to its original location
std_fd | The file descriptor to restore |
Definition at line 150 of file sequencer.cpp.
|
private |
Each command is a vector of string as passed on from Sequence::parse()
Data members
arguments
, files
, sstd
and pipe_
have linked resources so destructor(s) must be called. We use RAII by having all children to static allocated vectors. '}' will do the trick even if we throw ;) Definition at line 131 of file sequencer.h.
|
private |
Pipe object for each child
Definition at line 138 of file sequencer.h.