mirror of
https://github.com/ksherlock/mpw-shell.git
synced 2025-08-09 21:25:09 +00:00
command updates
This commit is contained in:
25
command.cpp
25
command.cpp
@@ -80,7 +80,7 @@ void launch_mpw(const Environment &env, const std::vector<std::string> &argv, co
|
|||||||
// handle any indirection...
|
// handle any indirection...
|
||||||
fds.dup();
|
fds.dup();
|
||||||
|
|
||||||
execvp(cargv.front(), cargv.data());
|
execv(mpw_path().c_str(), cargv.data());
|
||||||
perror("execvp: ");
|
perror("execvp: ");
|
||||||
exit(EX_OSERR); // raise a signal?
|
exit(EX_OSERR); // raise a signal?
|
||||||
}
|
}
|
||||||
@@ -168,11 +168,11 @@ int simple_command::execute(Environment &env, const fdmask &fds, bool throwup) {
|
|||||||
|
|
||||||
std::string s = expand_vars(text, env);
|
std::string s = expand_vars(text, env);
|
||||||
|
|
||||||
env.echo("%s", s.c_str());
|
|
||||||
|
|
||||||
process p;
|
process p;
|
||||||
try {
|
try {
|
||||||
auto tokens = tokenize(s, false);
|
auto tokens = tokenize(s, false);
|
||||||
|
if (tokens.empty()) return 0;
|
||||||
parse_tokens(std::move(tokens), p);
|
parse_tokens(std::move(tokens), p);
|
||||||
} catch(std::exception &e) {
|
} catch(std::exception &e) {
|
||||||
fprintf(stderr, "%s\n", e.what());
|
fprintf(stderr, "%s\n", e.what());
|
||||||
@@ -180,6 +180,8 @@ int simple_command::execute(Environment &env, const fdmask &fds, bool throwup) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (p.arguments.empty()) return 0;
|
if (p.arguments.empty()) return 0;
|
||||||
|
|
||||||
|
env.echo("%s", s.c_str());
|
||||||
|
|
||||||
fdmask newfds = p.fds | fds;
|
fdmask newfds = p.fds | fds;
|
||||||
|
|
||||||
@@ -188,6 +190,7 @@ int simple_command::execute(Environment &env, const fdmask &fds, bool throwup) {
|
|||||||
|
|
||||||
auto iter = builtins.find(name);
|
auto iter = builtins.find(name);
|
||||||
if (iter != builtins.end()) {
|
if (iter != builtins.end()) {
|
||||||
|
env.set("command", name);
|
||||||
int status = iter->second(env, p.arguments, newfds);
|
int status = iter->second(env, p.arguments, newfds);
|
||||||
return env.status(status, throwup);
|
return env.status(status, throwup);
|
||||||
}
|
}
|
||||||
@@ -198,6 +201,15 @@ int simple_command::execute(Environment &env, const fdmask &fds, bool throwup) {
|
|||||||
return env.status(0);
|
return env.status(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
int status = execute_external(env, p.arguments, newfds);
|
int status = execute_external(env, p.arguments, newfds);
|
||||||
|
|
||||||
return env.status(status, throwup);
|
return env.status(status, throwup);
|
||||||
@@ -209,6 +221,8 @@ int evaluate_command::execute(Environment &env, const fdmask &fds, bool throwup)
|
|||||||
|
|
||||||
std::string s = expand_vars(text, env);
|
std::string s = expand_vars(text, env);
|
||||||
|
|
||||||
|
env.set("command", "evaluate");
|
||||||
|
|
||||||
env.echo("%s", s.c_str());
|
env.echo("%s", s.c_str());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -313,12 +327,17 @@ int begin_command::execute(Environment &env, const fdmask &fds, bool throwup) {
|
|||||||
std::string b = expand_vars(begin, env);
|
std::string b = expand_vars(begin, env);
|
||||||
std::string e = expand_vars(end, env);
|
std::string e = expand_vars(end, env);
|
||||||
|
|
||||||
|
|
||||||
|
env.set("command", type == BEGIN ? "end" : ")");
|
||||||
|
|
||||||
// echo!
|
// echo!
|
||||||
env.echo("%s ... %s",
|
env.echo("%s ... %s",
|
||||||
b.c_str(),
|
b.c_str(),
|
||||||
e.c_str()
|
e.c_str()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// tokenize the begin and end commands.
|
// tokenize the begin and end commands.
|
||||||
// begin may not have extra arguments. end may have redirection.
|
// begin may not have extra arguments. end may have redirection.
|
||||||
|
|
||||||
@@ -400,6 +419,8 @@ int if_command::execute(Environment &env, const fdmask &fds, bool throwup) {
|
|||||||
|
|
||||||
std::string e = expand_vars(end, env);
|
std::string e = expand_vars(end, env);
|
||||||
|
|
||||||
|
env.set("command", "end");
|
||||||
|
|
||||||
// parse end for indirection.
|
// parse end for indirection.
|
||||||
fdmask newfds;
|
fdmask newfds;
|
||||||
int status = check_ends(e, newfds);
|
int status = check_ends(e, newfds);
|
||||||
|
Reference in New Issue
Block a user