Snel
A shell implementation for A.U.TH (Operating Systems Lab)
snel::Child Class Reference

#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)
 
Childmake_arguments ()
 A per child parser before execution. More...
 
bool execute (std::vector< Child >::iterator it, bool first)
 Execute the child in separate process. More...
 
Pipepipe ()
 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_
 

Detailed Description

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 file descriptors for std redirection the process may use
  • Pipes that may needed for communication with the next child
  • Logic flags (&&, ||) the command may have, to control the execution flow.

Definition at line 98 of file sequencer.h.

Member Function Documentation

◆ execute()

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:

ls && cat tralala || uname -a

is a chain with three childs (ls, cat, uname).

See also
Sequencer
Parameters
itIterator to the command chain execution
firstflag to indicate the first command in the chain
Returns
True if the chain can stop.

Definition at line 226 of file sequencer.cpp.

◆ make_arguments()

Child & snel::Child::make_arguments ( )

A per child parser before execution.

Parse the command of the current child to produce the actual arguments

Returns
Reference to Child for chaining

Definition at line 167 of file sequencer.cpp.

◆ redirect_std_if()

void snel::Child::redirect_std_if ( std::string  fn,
fd_t  std_fd 
)
private

Tool to redirect std.

Private api

Redirect a std_fd file descriptor to a file

Parameters
fnThe file name to used as std
std_fdThe file descriptor to redirect

Definition at line 136 of file sequencer.cpp.

◆ restore_std_if()

void snel::Child::restore_std_if ( fd_t  std_fd)
private

Tool to restore std redirection

Restore and redirected std file descriptor to its original location

Parameters
std_fdThe file descriptor to restore

Definition at line 150 of file sequencer.cpp.

Member Data Documentation

◆ command

command_t snel::Child::command {}
private

Each command is a vector of string as passed on from Sequence::parse()

Data members

Note
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.

◆ pipe_

Pipe snel::Child::pipe_
private

Pipe object for each child

Definition at line 138 of file sequencer.h.


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