2016-01-30 03:21:51 +00:00
|
|
|
#ifndef __command_h__
|
|
|
|
#define __command_h__
|
|
|
|
|
2016-02-02 21:19:13 +00:00
|
|
|
#include <new>
|
2016-01-27 15:43:34 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
#include <array>
|
2016-01-30 17:45:19 +00:00
|
|
|
#include <string>
|
2016-08-16 20:47:20 +00:00
|
|
|
#include "phase3.h"
|
2016-01-27 15:43:34 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-02-02 01:38:29 +00:00
|
|
|
#include "environment.h"
|
|
|
|
class Environment;
|
|
|
|
class fdmask;
|
2016-01-30 03:21:51 +00:00
|
|
|
|
2016-01-27 15:43:34 +00:00
|
|
|
struct command {
|
|
|
|
|
2016-01-30 03:21:51 +00:00
|
|
|
command(int t = 0) : type(t)
|
|
|
|
{}
|
|
|
|
|
2016-02-02 21:19:13 +00:00
|
|
|
virtual bool terminal() const noexcept {
|
2016-08-30 16:25:43 +00:00
|
|
|
return type == EVALUATE || type == COMMAND || type == BREAK || type == CONTINUE || type == ERROR || type == EXIT;
|
2016-02-02 21:19:13 +00:00
|
|
|
}
|
|
|
|
|
2016-01-30 03:21:51 +00:00
|
|
|
int type = 0;
|
2016-01-27 15:43:34 +00:00
|
|
|
virtual ~command();
|
2016-02-02 21:19:13 +00:00
|
|
|
virtual int execute(Environment &e, const fdmask &fds, bool throwup = true) = 0;
|
2016-02-02 01:38:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct error_command : public command {
|
|
|
|
|
|
|
|
template<class S>
|
|
|
|
error_command(int t, S &&s) : command(t), text(std::forward<S>(s))
|
|
|
|
{}
|
|
|
|
|
|
|
|
std::string text;
|
2016-02-02 21:19:13 +00:00
|
|
|
virtual int execute(Environment &e, const fdmask &fds, bool throwup = true) override final;
|
2016-01-27 15:43:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct simple_command : public command {
|
2016-01-30 03:21:51 +00:00
|
|
|
template<class S>
|
|
|
|
simple_command(S &&s) : command(COMMAND), text(std::forward<S>(s))
|
|
|
|
{}
|
|
|
|
|
2016-01-27 15:43:34 +00:00
|
|
|
std::string text;
|
2016-01-30 17:45:19 +00:00
|
|
|
|
2016-02-02 21:19:13 +00:00
|
|
|
virtual int execute(Environment &e, const fdmask &fds, bool throwup = true) final override;
|
2016-02-02 01:38:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct evaluate_command : public command {
|
|
|
|
template<class S>
|
|
|
|
evaluate_command(S &&s) : command(EVALUATE), text(std::forward<S>(s))
|
|
|
|
{}
|
|
|
|
|
|
|
|
std::string text;
|
|
|
|
|
2016-02-02 21:19:13 +00:00
|
|
|
virtual int execute(Environment &e, const fdmask &fds, bool throwup = true) final override;
|
2016-01-27 15:43:34 +00:00
|
|
|
};
|
|
|
|
|
2016-06-16 03:01:03 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2016-08-30 16:25:43 +00:00
|
|
|
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;
|
|
|
|
};
|
2016-06-16 03:01:03 +00:00
|
|
|
|
|
|
|
|
2016-01-27 15:43:34 +00:00
|
|
|
struct binary_command : public command {
|
2016-01-30 03:21:51 +00:00
|
|
|
|
|
|
|
binary_command(int t, command_ptr &&a, command_ptr &&b) :
|
|
|
|
command(t), children({{std::move(a), std::move(b)}})
|
|
|
|
{}
|
|
|
|
|
2016-01-27 15:43:34 +00:00
|
|
|
command_ptr_pair children;
|
2016-01-30 17:45:19 +00:00
|
|
|
|
2016-02-02 21:19:13 +00:00
|
|
|
virtual bool terminal() const noexcept final override {
|
|
|
|
return children[0]->terminal() && children[1]->terminal();
|
|
|
|
}
|
|
|
|
|
2016-01-27 15:43:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct or_command : public binary_command {
|
2016-01-30 03:21:51 +00:00
|
|
|
or_command(command_ptr &&a, command_ptr &&b) :
|
|
|
|
binary_command(PIPE_PIPE, std::move(a), std::move(b))
|
|
|
|
{}
|
2016-01-27 15:43:34 +00:00
|
|
|
|
2016-02-02 21:19:13 +00:00
|
|
|
virtual int execute(Environment &e, const fdmask &fds, bool throwup) final override;
|
2016-01-27 15:43:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct and_command : public binary_command {
|
2016-01-30 03:21:51 +00:00
|
|
|
and_command(command_ptr &&a, command_ptr &&b) :
|
|
|
|
binary_command(AMP_AMP, std::move(a), std::move(b))
|
|
|
|
{}
|
2016-01-30 17:45:19 +00:00
|
|
|
|
2016-02-02 21:19:13 +00:00
|
|
|
virtual int execute(Environment &e, const fdmask &fds, bool throwup) final override;
|
2016-01-30 17:45:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct pipe_command : public binary_command {
|
2016-07-26 18:20:11 +00:00
|
|
|
pipe_command(command_ptr &&a, command_ptr &&b) :
|
2016-01-30 17:45:19 +00:00
|
|
|
binary_command(PIPE, std::move(a), std::move(b))
|
|
|
|
{}
|
|
|
|
|
2016-07-26 18:20:11 +00:00
|
|
|
virtual int execute(Environment &e, const fdmask &fds, bool throwup) final override;
|
2016-01-27 15:43:34 +00:00
|
|
|
};
|
2016-01-30 17:45:19 +00:00
|
|
|
|
2016-01-27 15:43:34 +00:00
|
|
|
|
2016-01-30 03:21:51 +00:00
|
|
|
struct vector_command : public command {
|
|
|
|
vector_command(int t, command_ptr_vector &&v) : command(t), children(std::move(v))
|
|
|
|
{}
|
|
|
|
|
2016-01-27 15:43:34 +00:00
|
|
|
command_ptr_vector children;
|
2016-02-02 21:19:13 +00:00
|
|
|
virtual int execute(Environment &e, const fdmask &fds, bool throwup = true) override;
|
2016-01-30 03:21:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct begin_command : public vector_command {
|
2016-02-02 01:38:29 +00:00
|
|
|
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))
|
2016-01-30 03:21:51 +00:00
|
|
|
{}
|
|
|
|
|
2016-02-02 01:38:29 +00:00
|
|
|
std::string begin;
|
2016-01-27 15:43:34 +00:00
|
|
|
std::string end;
|
2016-01-30 17:45:19 +00:00
|
|
|
|
2016-02-02 21:19:13 +00:00
|
|
|
virtual int execute(Environment &e, const fdmask &fds, bool throwup) final override;
|
2016-01-27 15:43:34 +00:00
|
|
|
};
|
|
|
|
|
2016-06-16 03:01:03 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2016-06-16 04:04:29 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-01-30 17:45:19 +00:00
|
|
|
typedef std::unique_ptr<struct if_else_clause> if_else_clause_ptr;
|
2016-01-27 15:43:34 +00:00
|
|
|
struct if_command : public command {
|
2016-01-30 03:21:51 +00:00
|
|
|
|
2016-01-30 17:45:19 +00:00
|
|
|
typedef std::vector<if_else_clause_ptr> clause_vector_type;
|
2016-01-30 03:21:51 +00:00
|
|
|
|
|
|
|
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;
|
2016-01-27 15:43:34 +00:00
|
|
|
std::string end;
|
2016-01-30 17:45:19 +00:00
|
|
|
|
2016-02-02 21:19:13 +00:00
|
|
|
virtual int execute(Environment &e, const fdmask &fds, bool throwup) final override;
|
2016-01-27 15:43:34 +00:00
|
|
|
};
|
2016-01-30 03:21:51 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-02-02 21:19:13 +00:00
|
|
|
//bool evaluate(const Environment &e);
|
2016-01-30 03:21:51 +00:00
|
|
|
};
|
|
|
|
|
2016-06-16 03:01:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
#endif
|