mpw-shell/command.cpp

658 lines
13 KiB
C++
Raw Normal View History

2016-01-30 17:45:19 +00:00
#include "command.h"
#include "phase2-parser.h"
2016-02-02 01:38:29 +00:00
#include "environment.h"
#include "fdset.h"
#include "builtins.h"
#include "mpw-shell.h"
2016-02-05 18:19:20 +00:00
#include "error.h"
2016-02-02 01:38:29 +00:00
2016-01-30 17:45:19 +00:00
#include <stdexcept>
2016-02-02 01:38:29 +00:00
#include <unordered_map>
#include <cctype>
#include <cerrno>
#include <cstdlib>
2016-02-11 20:48:46 +00:00
#include "cxx/filesystem.h"
#include "cxx/string_splitter.h"
2016-02-02 01:38:29 +00:00
#include <unistd.h>
#include <sys/wait.h>
#include <sysexits.h>
#include <signal.h>
#include <atomic>
2016-02-02 01:38:29 +00:00
extern std::atomic<int> control_c;
2016-02-05 18:19:20 +00:00
2016-02-11 20:48:46 +00:00
namespace fs = filesystem;
extern fs::path mpw_path();
namespace ToolBox {
std::string MacToUnix(const std::string path);
std::string UnixToMac(const std::string path);
}
namespace {
struct break_command_t {};
struct continue_command_t {};
/*
* returns:
* <0 -> error
* 0 -> false
* >0 -> true
*/
int bad_if(const char *name) {
fprintf(stderr, "### %s - Missing if keyword.\n", name);
fprintf(stderr, "# Usage - %s [if expression...]\n", name);
return -3;
}
int evaluate(int type, const std::string &s, Environment &env) {
auto tokens = tokenize(s, true);
std::reverse(tokens.begin(), tokens.end());
int32_t e;
switch(type) {
default: return 0;
case BREAK:
case CONTINUE:
case ELSE:
tokens.pop_back();
if (tokens.empty()) return 1;
if (strcasecmp(tokens.back().string.c_str(), "if") != 0) {
const char *name = "";
switch(type) {
case BREAK: name = "Break"; break;
case CONTINUE: name = "Continue"; break;
case ELSE: name = "Else"; break;
}
return bad_if(name);
}
// fall through.
case IF:
tokens.pop_back();
try {
e = evaluate_expression("If", std::move(tokens));
}
catch (std::exception &ex) {
fprintf(stderr, "%s\n", ex.what());
return -5;
}
break;
}
return !!e;
}
}
2016-02-11 20:48:46 +00:00
fs::path which(const Environment &env, const std::string &name) {
std::error_code ec;
if (name.find_first_of("/:") != name.npos) {
// canonical?
fs::path p(name);
if (fs::exists(p, ec)) return name;
return "";
}
std::string s = env.get("commands");
for (string_splitter ss(s, ','); ss; ++ss) {
fs::path p(ToolBox::MacToUnix(*ss));
p /= name;
if (fs::exists(p, ec)) return p;
}
// error..
return "";
}
2016-02-06 04:00:42 +00:00
2016-02-06 04:00:42 +00:00
void launch_mpw(const Environment &env, const std::vector<std::string> &argv, const fdmask &fds) {
2016-02-06 04:00:42 +00:00
std::vector<char *> cargv;
cargv.reserve(argv.size() + 3);
cargv.push_back((char *)"mpw");
//cargv.push_back((char *)"--shell");
unsigned offset = cargv.size();
std::transform(argv.begin(), argv.end(), std::back_inserter(cargv),
[](const std::string &s) { return strdup(s.c_str()); }
);
cargv.push_back(nullptr);
2016-06-24 14:31:34 +00:00
// export environment...
2016-02-06 04:00:42 +00:00
2016-06-24 14:31:34 +00:00
for (const auto &kv : env) {
if (kv.second) { // exported
std::string name = "mpw$" + kv.first;
setenv(name.c_str(), kv.second.c_str(), 1);
2016-02-06 04:00:42 +00:00
}
2016-06-24 14:31:34 +00:00
}
2016-02-06 04:00:42 +00:00
2016-06-24 14:31:34 +00:00
// handle any indirection...
fds.dup();
2016-02-06 04:00:42 +00:00
// re-set all signal handlers.
/*
* execv modifies handlers as such:
* blocked -> blocked
* ignored -> ignored
* caught -> default action
*/
struct sigaction sig_action;
sig_action.sa_handler = SIG_DFL;
sig_action.sa_flags = 0;
sigemptyset(&sig_action.sa_mask);
for (int i = 1; i < NSIG; ++i) {
sigaction(i, &sig_action, NULL);
}
2016-06-24 14:31:34 +00:00
execv(mpw_path().c_str(), cargv.data());
perror("execvp: ");
exit(EX_OSERR); // raise a signal?
2016-02-06 04:00:42 +00:00
}
2016-02-02 01:38:29 +00:00
namespace {
std::string &lowercase(std::string &s) {
std::transform(s.begin(), s.end(), s.begin(), [](char c){ return std::tolower(c); });
return s;
}
std::unordered_map<std::string, int (*)(Environment &, const std::vector<std::string> &, const fdmask &)> builtins = {
{"aboutbox", builtin_aboutbox},
2016-06-22 17:47:48 +00:00
{"alias", builtin_alias},
2016-02-02 01:38:29 +00:00
{"directory", builtin_directory},
{"echo", builtin_echo},
2016-06-15 15:25:25 +00:00
{"exists", builtin_exists},
2016-02-11 20:51:00 +00:00
{"export", builtin_export},
2016-02-02 01:38:29 +00:00
{"parameters", builtin_parameters},
{"quote", builtin_quote},
{"set", builtin_set},
2016-06-16 20:48:04 +00:00
{"shift", builtin_shift},
2016-06-22 17:47:48 +00:00
{"unalias", builtin_unalias},
2016-02-02 01:38:29 +00:00
{"unexport", builtin_unexport},
2016-02-11 20:51:00 +00:00
{"unset", builtin_unset},
2016-06-15 17:25:41 +00:00
{"version", builtin_version},
2016-02-11 20:51:00 +00:00
{"which", builtin_which},
2016-02-02 01:38:29 +00:00
};
2016-02-06 04:00:42 +00:00
int execute_external(const Environment &env, const std::vector<std::string> &argv, const fdmask &fds) {
2016-02-02 01:38:29 +00:00
2016-02-06 04:00:42 +00:00
int status;
int pid;
2016-02-02 01:38:29 +00:00
pid = fork();
if (pid < 0) {
perror("fork: ");
exit(EX_OSERR);
}
if (pid == 0) {
2016-02-06 04:00:42 +00:00
launch_mpw(env, argv, fds);
2016-02-02 01:38:29 +00:00
}
for(;;) {
int status;
pid_t ok;
ok = waitpid(pid, &status, 0);
if (ok < 0) {
if (errno == EINTR) continue;
perror("waitpid:");
exit(EX_OSERR);
}
if (WIFEXITED(status)) return WEXITSTATUS(status);
if (WIFSIGNALED(status)) return -9; // command-. user abort exits via -9.
fprintf(stderr, "waitpid - unexpected result\n");
exit(EX_OSERR);
}
}
}
//std::string expand_vars(const std::string &s, const class Environment &);
2016-01-30 17:45:19 +00:00
command::~command()
{}
2016-02-02 01:38:29 +00:00
/*
* todo -- all errors should be handled the same, regardless of source. (throw, parse, etc).
* echo and error should respect the active fdmask.
*/
2016-01-30 17:45:19 +00:00
2016-02-02 21:19:13 +00:00
int simple_command::execute(Environment &env, const fdmask &fds, bool throwup) {
2016-02-05 18:19:20 +00:00
if (control_c) throw execution_of_input_terminated();
2016-02-02 01:38:29 +00:00
std::string s = expand_vars(text, env);
process p;
try {
auto tokens = tokenize(s, false);
2016-02-11 20:51:56 +00:00
if (tokens.empty()) return 0;
2016-02-02 01:38:29 +00:00
parse_tokens(std::move(tokens), p);
} catch(std::exception &e) {
2016-02-02 21:19:13 +00:00
fprintf(stderr, "%s\n", e.what());
return env.status(-4, throwup);
2016-02-02 01:38:29 +00:00
}
if (p.arguments.empty()) return 0;
2016-02-11 20:51:56 +00:00
env.echo("%s", s.c_str());
2016-02-02 01:38:29 +00:00
fdmask newfds = p.fds | fds;
std::string name = p.arguments.front();
lowercase(name);
2016-01-30 17:45:19 +00:00
2016-02-02 01:38:29 +00:00
auto iter = builtins.find(name);
if (iter != builtins.end()) {
2016-02-11 20:51:56 +00:00
env.set("command", name);
2016-02-02 01:38:29 +00:00
int status = iter->second(env, p.arguments, newfds);
2016-02-02 21:19:13 +00:00
return env.status(status, throwup);
2016-01-30 17:45:19 +00:00
}
2016-02-02 01:38:29 +00:00
if (env.test()) return env.status(0);
if (env.startup()) {
fprintf(stderr, "### MPW Shell - startup file may not contain external commands.\n");
return env.status(0);
}
2016-02-11 20:51:56 +00:00
name = p.arguments.front();
fs::path path = which(env, name);
if (path.empty()) {
fprintf(stderr, "### MPW Shell - Command \"%s\" was not found.\n", name.c_str());
return env.status(-1, throwup);
}
env.set("command", path);
p.arguments[0] = path;
2016-02-02 01:38:29 +00:00
int status = execute_external(env, p.arguments, newfds);
2016-02-02 21:19:13 +00:00
return env.status(status, throwup);
2016-02-02 01:38:29 +00:00
}
2016-02-02 21:19:13 +00:00
int evaluate_command::execute(Environment &env, const fdmask &fds, bool throwup) {
2016-02-02 01:38:29 +00:00
2016-02-05 18:19:20 +00:00
if (control_c) throw execution_of_input_terminated();
2016-02-02 01:38:29 +00:00
std::string s = expand_vars(text, env);
2016-02-11 20:51:56 +00:00
env.set("command", "evaluate");
2016-02-02 21:19:13 +00:00
env.echo("%s", s.c_str());
2016-02-02 01:38:29 +00:00
2016-02-02 21:19:13 +00:00
try {
auto tokens = tokenize(s, true);
if (tokens.empty()) return 0;
2016-02-02 01:38:29 +00:00
2016-02-02 21:19:13 +00:00
int status = builtin_evaluate(env, std::move(tokens), fds);
2016-02-02 01:38:29 +00:00
2016-02-02 21:19:13 +00:00
return env.status(status, throwup);
} catch (std::exception &e) {
fprintf(stderr, "%s\n", e.what());
return env.status(1, throwup);
}
2016-02-02 01:38:29 +00:00
}
int break_command::execute(Environment &env, const fdmask &fds, bool throwup) {
if (control_c) throw execution_of_input_terminated();
std::string s = expand_vars(text, env);
env.set("command", "break");
env.echo("%s", s.c_str());
if (!env.loop()) {
fputs("MPW Shell - Break must be within for or loop.\n", stderr);
return env.status(-3, throwup);
}
// check/evaluate if clause.
int status = evaluate(BREAK, s, env);
if (status > 0)
throw break_command_t();
return env.status(status, throwup);
}
int continue_command::execute(Environment &env, const fdmask &fds, bool throwup) {
if (control_c) throw execution_of_input_terminated();
std::string s = expand_vars(text, env);
env.set("command", "continue");
env.echo("%s", s.c_str());
if (!env.loop()) {
fputs("MPW Shell - Continue must be within for or loop.\n", stderr);
return env.status(-3, throwup);
}
// check/evaluate if clause.
int status = evaluate(CONTINUE, s, env);
if (status > 0)
throw continue_command_t();
return env.status(status, throwup);
}
2016-02-02 21:19:13 +00:00
int or_command::execute(Environment &e, const fdmask &fds, bool throwup) {
2016-01-30 17:45:19 +00:00
int rv = 0;
2016-02-02 01:38:29 +00:00
2016-01-30 17:45:19 +00:00
for (auto &c : children) {
2016-02-02 21:19:13 +00:00
if (!c) continue;
rv = c->execute(e, fds, false);
2016-01-30 17:45:19 +00:00
if (rv == 0) return rv;
}
2016-02-02 01:38:29 +00:00
return e.status(rv);
2016-01-30 17:45:19 +00:00
}
2016-02-02 21:19:13 +00:00
int and_command::execute(Environment &e, const fdmask &fds, bool throwup) {
2016-01-30 17:45:19 +00:00
int rv = 0;
for (auto &c : children) {
2016-02-02 21:19:13 +00:00
if (!c) continue;
2016-02-05 02:45:04 +00:00
rv = c->execute(e, fds, false);
2016-01-30 17:45:19 +00:00
if (rv != 0) return rv;
}
2016-02-02 01:38:29 +00:00
2016-01-30 17:45:19 +00:00
return rv;
}
2016-02-02 21:19:13 +00:00
int vector_command::execute(Environment &e, const fdmask &fds, bool throwup) {
2016-01-30 17:45:19 +00:00
int rv = 0;
for (auto &c : children) {
2016-02-05 02:45:04 +00:00
if (!c) continue;
2016-02-02 01:38:29 +00:00
rv = c->execute(e, fds);
}
return e.status(rv);
}
2016-02-02 21:19:13 +00:00
int error_command::execute(Environment &e, const fdmask &fds, bool throwup) {
2016-02-05 18:19:20 +00:00
if (control_c) throw execution_of_input_terminated();
2016-02-02 01:38:29 +00:00
std::string s = expand_vars(text, e);
2016-02-02 21:19:13 +00:00
e.echo("%s", s.c_str());
2016-02-02 01:38:29 +00:00
switch(type) {
case END:
fprintf(stderr, "### MPW Shell - Extra END command.\n");
break;
case RPAREN:
fprintf(stderr, "### MPW Shell - Extra ) command.\n");
break;
case ELSE:
case ELSE_IF:
fprintf(stderr, "### MPW Shell - ELSE must be within IF ... END.\n");
break;
}
return e.status(-3);
}
namespace {
int check_ends(const std::string &s, fdmask &fds) {
// MPW ignores any END tokens other than redirection.
process p;
try {
auto tokens = tokenize(s, false);
parse_tokens(std::move(tokens), p);
}
catch (std::exception &e) {
fprintf(stderr, "%s\n", e.what());
return -4;
}
fds = p.fds.to_mask();
return 0;
2016-01-30 17:45:19 +00:00
}
}
2016-02-02 21:19:13 +00:00
int begin_command::execute(Environment &env, const fdmask &fds, bool throwup) {
2016-01-30 17:45:19 +00:00
// todo -- parse end for redirection.
2016-02-02 01:38:29 +00:00
std::string b = expand_vars(begin, env);
std::string e = expand_vars(end, env);
2016-02-11 20:51:56 +00:00
env.set("command", type == BEGIN ? "end" : ")");
2016-01-31 05:41:16 +00:00
// echo!
2016-02-02 21:19:13 +00:00
env.echo("%s ... %s",
2016-02-02 01:38:29 +00:00
b.c_str(),
e.c_str()
2016-01-31 05:41:16 +00:00
);
2016-01-30 17:45:19 +00:00
2016-02-11 20:51:56 +00:00
2016-02-02 01:38:29 +00:00
// tokenize the begin and end commands.
// begin may not have extra arguments. end may have redirection.
auto bt = tokenize(b, true);
if (bt.size() != 1) {
fprintf(stderr, "### Begin - Too many parameters were specified.\n");
fprintf(stderr, "Usage - Begin\n");
return env.status(-3);
}
fdmask newfds;
int status = check_ends(e, newfds);
newfds |= fds;
if (status) return env.status(status);
2016-02-02 21:19:13 +00:00
int rv;
2016-02-11 02:58:00 +00:00
env.indent_and([&]{
rv = vector_command::execute(env, newfds);
});
2016-02-02 21:19:13 +00:00
env.echo("%s", type == BEGIN ? "end" : ")");
2016-02-02 01:38:29 +00:00
return env.status(rv);
2016-01-30 17:45:19 +00:00
}
2016-02-02 01:38:29 +00:00
int loop_command::execute(Environment &env, const fdmask &fds, bool throwup) {
2016-02-02 01:38:29 +00:00
std::string b = expand_vars(begin, env);
std::string e = expand_vars(end, env);
2016-02-02 01:38:29 +00:00
env.set("command", "end");
2016-02-02 01:38:29 +00:00
// echo!
env.echo("%s ... %s",
b.c_str(),
e.c_str()
);
2016-02-02 01:38:29 +00:00
// check for extra tokens...
auto bt = tokenize(b, true);
if (bt.size() != 1) {
fprintf(stderr, "### Loop - Too many parameters were specified.\n");
fprintf(stderr, "Usage - Loop\n");
return env.status(-3);
}
2016-02-02 01:38:29 +00:00
fdmask newfds;
int status = check_ends(e, newfds);
newfds |= fds;
if (status) return env.status(status);
2016-02-02 01:38:29 +00:00
int rv = 0;
2016-06-16 04:04:29 +00:00
for(;;) {
if (control_c) throw execution_of_input_terminated();
try {
env.loop_indent_and([&]{
rv = vector_command::execute(env, newfds);
});
2016-02-02 01:38:29 +00:00
}
catch (break_command_t &ex) {
2016-06-16 04:04:29 +00:00
env.echo("end");
break;
}
2016-06-16 04:04:29 +00:00
catch (continue_command_t &ex) {}
env.echo("end");
}
return env.status(rv);
}
int for_command::execute(Environment &env, const fdmask &fds, bool throwup) {
std::string b = expand_vars(begin, env);
std::string e = expand_vars(end, env);
env.set("command", "end");
// echo!
env.echo("%s ... %s",
b.c_str(),
e.c_str()
);
// check for extra tokens...
auto bt = tokenize(b, true);
if (bt.size() < 3 || strcasecmp(bt[2].string.c_str(), "in")) {
fprintf(stderr, "### For - Missing in keyword.\n");
fprintf(stderr, "Usage - For name in [word...]\n");
return env.status(-3);
}
fdmask newfds;
int status = check_ends(e, newfds);
newfds |= fds;
if (status) return env.status(status);
int rv = 0;
for (int i = 3; i < bt.size(); ++i ) {
if (control_c) throw execution_of_input_terminated();
env.set(bt[1].string, bt[i].string);
try {
env.loop_indent_and([&]{
rv = vector_command::execute(env, newfds);
});
}
catch (break_command_t &ex) {
env.echo("end");
break;
2016-06-16 03:19:34 +00:00
}
2016-06-16 04:04:29 +00:00
catch (continue_command_t &ex) {}
env.echo("end");
2016-02-02 01:38:29 +00:00
}
return env.status(rv);
2016-02-02 01:38:29 +00:00
}
2016-02-02 21:19:13 +00:00
/*
* the entire command prints even if there is an error with the expression.
*
*/
int if_command::execute(Environment &env, const fdmask &fds, bool throwup) {
2016-01-30 17:45:19 +00:00
int rv = 0;
bool ok = false;
2016-01-31 05:41:16 +00:00
2016-02-02 01:38:29 +00:00
std::string e = expand_vars(end, env);
2016-02-11 20:51:56 +00:00
env.set("command", "end");
2016-02-02 01:38:29 +00:00
// parse end for indirection.
fdmask newfds;
int status = check_ends(e, newfds);
newfds |= fds;
2016-02-02 21:19:13 +00:00
if (status) {
rv = status;
ok = true;
}
2016-01-31 05:41:16 +00:00
for (auto &c : clauses) {
2016-02-02 01:38:29 +00:00
std::string s = expand_vars(c->clause, env);
2016-02-02 21:19:13 +00:00
if (c->type == IF)
env.echo("%s ... %s", s.c_str(), e.c_str());
else
env.echo("%s", s.c_str());
2016-01-31 05:41:16 +00:00
2016-01-30 17:45:19 +00:00
if (ok) continue;
2016-02-11 02:58:00 +00:00
int tmp = evaluate(c->type, s, env);
if (tmp < 0) { ok = true; rv = tmp; continue; }
if (tmp == 0) continue;
env.indent_and([&](){
2016-02-03 02:57:42 +00:00
rv = c->execute(env, newfds);
2016-02-11 02:58:00 +00:00
});
2016-01-30 17:45:19 +00:00
}
2016-01-31 05:41:16 +00:00
2016-02-02 21:19:13 +00:00
env.echo("end");
2016-02-02 01:38:29 +00:00
return env.status(rv);
2016-01-30 17:45:19 +00:00
}