-
Notifications
You must be signed in to change notification settings - Fork 5
/
command.h
213 lines (146 loc) · 5.16 KB
/
command.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#ifndef __command_h__
#define __command_h__
#include <new>
#include <memory>
#include <vector>
#include <array>
#include <string>
#include "phase3.h"
typedef std::unique_ptr<struct command> command_ptr;
typedef std::vector<command_ptr> command_ptr_vector;
typedef std::array<command_ptr, 2> command_ptr_pair;
#include "environment.h"
class Environment;
class fdmask;
struct command {
command(int t = 0) : type(t)
{}
virtual bool terminal() const noexcept {
return type == EVALUATE || type == COMMAND || type == BREAK || type == CONTINUE || type == ERROR || type == EXIT;
}
int type = 0;
virtual ~command();
virtual int execute(Environment &e, const fdmask &fds, bool throwup = true) = 0;
};
struct error_command : public command {
template<class S>
error_command(int t, S &&s) : command(t), text(std::forward<S>(s))
{}
std::string text;
virtual int execute(Environment &e, const fdmask &fds, bool throwup = true) override final;
};
struct simple_command : public command {
template<class S>
simple_command(S &&s) : command(COMMAND), text(std::forward<S>(s))
{}
std::string text;
virtual int execute(Environment &e, const fdmask &fds, bool throwup = true) final override;
};
struct evaluate_command : public command {
template<class S>
evaluate_command(S &&s) : command(EVALUATE), text(std::forward<S>(s))
{}
std::string text;
virtual int execute(Environment &e, const fdmask &fds, bool throwup = true) final override;
};
struct break_command : public command {
template<class S>
break_command(S &&s) : command(BREAK), text(std::forward<S>(s))
{}
std::string text;
virtual int execute(Environment &e, const fdmask &fds, bool throwup) final override;
};
struct continue_command : public command {
template<class S>
continue_command(S &&s) : command(CONTINUE), text(std::forward<S>(s))
{}
std::string text;
virtual int execute(Environment &e, const fdmask &fds, bool throwup) final override;
};
struct exit_command : public command {
template<class S>
exit_command(S &&s) : command(EXIT), text(std::forward<S>(s))
{}
std::string text;
virtual int execute(Environment &e, const fdmask &fds, bool throwup) final override;
};
struct binary_command : public command {
binary_command(int t, command_ptr &&a, command_ptr &&b) :
command(t), children({{std::move(a), std::move(b)}})
{}
command_ptr_pair children;
virtual bool terminal() const noexcept final override {
return children[0]->terminal() && children[1]->terminal();
}
};
struct or_command : public binary_command {
or_command(command_ptr &&a, command_ptr &&b) :
binary_command(PIPE_PIPE, std::move(a), std::move(b))
{}
virtual int execute(Environment &e, const fdmask &fds, bool throwup) final override;
};
struct and_command : public binary_command {
and_command(command_ptr &&a, command_ptr &&b) :
binary_command(AMP_AMP, std::move(a), std::move(b))
{}
virtual int execute(Environment &e, const fdmask &fds, bool throwup) final override;
};
struct pipe_command : public binary_command {
pipe_command(command_ptr &&a, command_ptr &&b) :
binary_command(PIPE, std::move(a), std::move(b))
{}
virtual int execute(Environment &e, const fdmask &fds, bool throwup) final override;
};
struct vector_command : public command {
vector_command(int t, command_ptr_vector &&v) : command(t), children(std::move(v))
{}
command_ptr_vector children;
virtual int execute(Environment &e, const fdmask &fds, bool throwup = true) override;
};
struct begin_command : public vector_command {
template<class S1, class S2>
begin_command(int t, command_ptr_vector &&v, S1 &&b, S2 &&e) :
vector_command(t, std::move(v)), begin(std::forward<S1>(b)), end(std::forward<S2>(e))
{}
std::string begin;
std::string end;
virtual int execute(Environment &e, const fdmask &fds, bool throwup) final override;
};
struct loop_command : public vector_command {
template<class S1, class S2>
loop_command(int t, command_ptr_vector &&v, S1 &&b, S2 &&e) :
vector_command(t, std::move(v)), begin(std::forward<S1>(b)), end(std::forward<S2>(e))
{}
std::string begin;
std::string end;
virtual int execute(Environment &e, const fdmask &fds, bool throwup) final override;
};
struct for_command : public vector_command {
template<class S1, class S2>
for_command(int t, command_ptr_vector &&v, S1 &&b, S2 &&e) :
vector_command(t, std::move(v)), begin(std::forward<S1>(b)), end(std::forward<S2>(e))
{}
std::string begin;
std::string end;
virtual int execute(Environment &e, const fdmask &fds, bool throwup) final override;
};
typedef std::unique_ptr<struct if_else_clause> if_else_clause_ptr;
struct if_command : public command {
typedef std::vector<if_else_clause_ptr> clause_vector_type;
template<class S>
if_command(clause_vector_type &&v, S &&s) :
command(IF), clauses(std::move(v)), end(std::forward<S>(s))
{}
clause_vector_type clauses;
std::string end;
virtual int execute(Environment &e, const fdmask &fds, bool throwup) final override;
};
struct if_else_clause : public vector_command {
template<class S>
if_else_clause(int t, command_ptr_vector &&v, S &&s) :
vector_command(t, std::move(v)), clause(std::forward<S>(s))
{}
std::string clause;
//bool evaluate(const Environment &e);
};
#endif