mpw-shell/command.cpp

101 lines
1.5 KiB
C++
Raw Normal View History

2016-01-30 17:45:19 +00:00
#include "command.h"
#include "phase2-parser.h"
#include <stdexcept>
command::~command()
{}
int simple_command::execute() {
2016-01-31 05:41:16 +00:00
// echo
fprintf(stderr, " %s\n", text.c_str());
2016-01-30 17:45:19 +00:00
return 0;
}
int or_command::execute() {
/*
for (const auto &c : children) {
if (!c) throw std::runtime_error("corruption in || command.");
}
*/
int rv = 0;
for (auto &c : children) {
rv = c->execute();
if (rv == 0) return rv;
}
return rv;
}
int and_command::execute() {
int rv = 0;
for (auto &c : children) {
rv = c->execute();
if (rv != 0) return rv;
}
return rv;
}
int vector_command::execute() {
int rv = 0;
for (auto &c : children) {
rv = c->execute();
// todo -- env.exit
}
return rv;
}
int begin_command::execute() {
// todo -- parse end for redirection.
2016-01-31 05:41:16 +00:00
// echo!
fprintf(stderr, " %s ... %s\n",
type == BEGIN ? "begin" : "(",
end.c_str()
);
2016-01-30 17:45:19 +00:00
2016-01-31 05:41:16 +00:00
int rv = vector_command::execute();
fprintf(stderr, " %s\n", type == BEGIN ? "end" : ")");
2016-01-30 17:45:19 +00:00
return rv;
}
int if_command::execute() {
int rv = 0;
bool ok = false;
2016-01-31 05:41:16 +00:00
2016-01-30 17:45:19 +00:00
// todo -- parse end for redirection.
2016-01-31 05:41:16 +00:00
for (auto &c : clauses) {
if (c->type == IF) {// special.
fprintf(stderr, " %s ... %s\n",
c->clause.c_str(), end.c_str());
}
else {
fprintf(stderr, " %s\n", c->clause.c_str());
}
2016-01-30 17:45:19 +00:00
if (ok) continue;
ok = c->evaluate();
if (!ok) continue;
rv = c->execute();
}
2016-01-31 05:41:16 +00:00
fprintf(stderr, " end\n");
2016-01-30 17:45:19 +00:00
return rv;
}
/*
int if_else_clause::execute() {
return 0;
}
*/
bool if_else_clause::evaluate() {
return false;
}