Snel
A shell implementation for A.U.TH (Operating Systems Lab)
sequencer.h
Go to the documentation of this file.
1 
10 #ifndef __sequencer_h__
11 #define __sequencer_h__
12 
13 #include <exception>
14 #include <string>
15 #include <iostream>
16 #include <sstream>
17 #include <fstream>
18 #include <vector>
19 #include <utility>
20 #include <algorithm>
21 
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <sys/wait.h>
25 
26 
27 namespace snel {
28 
30  using fd_t = int;
31 
32  constexpr fd_t STDIN_ = STDIN_FILENO;
33  constexpr fd_t STDOUT_ = STDOUT_FILENO;
34  constexpr fd_t STDERR_ = STDERR_FILENO;
35 
36  std::string filter (const std::string in);
37 
52  struct ArgList {
53  using type = typename std::string::value_type;
54  using vtype = type*;
55  using vtype_ptr = vtype*;
56 
57  ~ArgList();
58  ArgList() = default;
59  ArgList(const ArgList&) = default;
60  ArgList(ArgList&&) = default;
61 
62  ArgList& push_back(const std::string& item);
63  vtype front () { return args_.front(); }
64  size_t size () { return args_.size(); }
65 
67  vtype_ptr data() noexcept { return args_.data(); }
69  vtype_ptr operator*() noexcept { return data(); }
70  private:
72  std::vector<vtype> args_ {nullptr};
73  };
74 
75 
82  struct Pipe {
83  ~Pipe();
84  fd_t fd[2] {-1, -1};
85  bool from {false};
86  bool to {false};
87  };
88 
98  class Child {
99  public:
101  enum class LogicOp {
102  NONE=0, OR, AND
103  };
104  using command_t = std::vector<std::string>;
105  using Error = std::runtime_error;
106 
107  public:
108  ~Child ();
109  Child () = default;
110  Child (const command_t& c) : command{c} { }
111  Child (command_t&& c) : command{std::move(c)} { }
112 
113  Child& make_arguments ();
114  bool execute(std::vector<Child>::iterator it, bool first);
115  Pipe& pipe() { return pipe_; }
116 
119  private:
120  void redirect_std_if(std::string fn, fd_t std_fd);
121  void restore_std_if(fd_t std_fd);
122 
130  private:
131  command_t command {};
132  ArgList arguments {};
133 
134  fd_t files[3] = {-1, -1, -1};
135  fd_t sstd [3] = {-1, -1, -1};
136  std::string filenames[3] = {"", "", ""};
137  LogicOp logic {LogicOp::NONE};
139  };
141 
160  class Sequencer {
161  public:
162  Sequencer& parse (const std::string& input);
163  Sequencer& execute ();
164 
167  private:
169  bool is_seperator (std::string& s) {
170  return (s == "&&" || s == "||" || s == "|") ? true : false;
171  }
173  bool is_pipe (std::string& s) {
174  return (s == "|") ? true : false;
175  }
177  private:
179  std::vector <
180  std::vector <Child>
181  > seq_ {};
182  };
183 
184 }
185 
186 #endif /* __sequencer_h__ */
std::string filter(const std::string in)
Definition: sequencer.cpp:49
bool is_pipe(std::string &s)
pipe trait predicate
Definition: sequencer.h:173
constexpr fd_t STDERR_
Constant for stderr file descriptor.
Definition: sequencer.h:34
vtype * vtype_ptr
Pointer to vector type.
Definition: sequencer.h:55
std::vector< vtype > args_
underling data for the execvp() arguments
Definition: sequencer.h:72
vtype front()
A vector::front() wrapper.
Definition: sequencer.h:63
std::vector< std::string > command_t
A command type.
Definition: sequencer.h:104
std::runtime_error Error
An error type.
Definition: sequencer.h:105
Pipe & pipe()
A pipe_ getter.
Definition: sequencer.h:115
bool is_seperator(std::string &s)
separator trait predicate
Definition: sequencer.h:169
typename std::string::value_type type
Basic data type (aka char)
Definition: sequencer.h:53
LogicOp
Enumerator for the logic operators between commands.
Definition: sequencer.h:101
size_t size()
A vector::size() wrapper.
Definition: sequencer.h:64
int fd_t
file descriptor type
Definition: sequencer.h:30
vtype_ptr data() noexcept
return a pointer to underling data for execvp()
Definition: sequencer.h:67
vtype_ptr operator*() noexcept
same as data()
Definition: sequencer.h:69
ArgList & push_back(const std::string &item)
A vector::push_back wrapper.
Definition: sequencer.cpp:99
type * vtype
Vector type.
Definition: sequencer.h:54
constexpr fd_t STDIN_
Constant for stdin file descriptor.
Definition: sequencer.h:32
Pipe pipe_
Definition: sequencer.h:138
constexpr fd_t STDOUT_
Constant for stdout file descriptor.
Definition: sequencer.h:33