2012-05-03 09:25:13 +02:00
|
|
|
#include "shell_manager.hh"
|
|
|
|
|
2012-12-10 18:41:01 +01:00
|
|
|
#include "context.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "debug.hh"
|
2014-11-25 02:00:18 +01:00
|
|
|
#include "event_manager.hh"
|
2014-10-30 01:50:40 +01:00
|
|
|
#include "file.hh"
|
2012-09-06 14:28:07 +02:00
|
|
|
|
2012-05-03 09:25:13 +02:00
|
|
|
#include <cstring>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2014-10-13 20:28:02 +02:00
|
|
|
#include <unistd.h>
|
2012-05-03 09:25:13 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
2013-04-17 19:26:44 +02:00
|
|
|
|
2014-03-25 20:43:56 +01:00
|
|
|
static const Regex env_var_regex(R"(\bkak_(\w+)\b)");
|
2012-05-03 09:25:13 +02:00
|
|
|
|
|
|
|
ShellManager::ShellManager()
|
|
|
|
{
|
2014-10-30 01:50:40 +01:00
|
|
|
const char* path = getenv("PATH");
|
|
|
|
String new_path;
|
|
|
|
if (path)
|
|
|
|
new_path = path + ":"_str;
|
|
|
|
|
|
|
|
String kak_path = get_kak_binary_path();
|
|
|
|
StringView kak_dir = kak_path.substr(0_byte, (int)kak_path.find_last_of('/'));
|
|
|
|
new_path += kak_dir;
|
|
|
|
setenv("PATH", new_path.c_str(), 1);
|
2012-05-03 09:25:13 +02:00
|
|
|
}
|
|
|
|
|
2014-04-20 13:15:31 +02:00
|
|
|
String ShellManager::eval(StringView cmdline, const Context& context,
|
2013-07-26 01:17:12 +02:00
|
|
|
memoryview<String> params,
|
2014-05-05 19:00:24 +02:00
|
|
|
const EnvVarMap& env_vars,
|
|
|
|
int* exit_status)
|
2012-05-29 12:39:03 +02:00
|
|
|
{
|
2014-05-05 19:00:24 +02:00
|
|
|
return pipe("", cmdline, context, params, env_vars, exit_status);
|
2012-05-29 12:39:03 +02:00
|
|
|
}
|
|
|
|
|
2014-04-20 13:15:31 +02:00
|
|
|
String ShellManager::pipe(StringView input,
|
|
|
|
StringView cmdline, const Context& context,
|
2013-07-26 01:17:12 +02:00
|
|
|
memoryview<String> params,
|
2014-05-05 19:00:24 +02:00
|
|
|
const EnvVarMap& env_vars,
|
|
|
|
int* exit_status)
|
2012-05-03 09:25:13 +02:00
|
|
|
{
|
2012-09-06 14:28:07 +02:00
|
|
|
int write_pipe[2]; // child stdin
|
|
|
|
int read_pipe[2]; // child stdout
|
|
|
|
int error_pipe[2]; // child stderr
|
2012-05-03 09:25:13 +02:00
|
|
|
|
2012-05-29 12:39:03 +02:00
|
|
|
::pipe(write_pipe);
|
|
|
|
::pipe(read_pipe);
|
2012-09-06 14:28:07 +02:00
|
|
|
::pipe(error_pipe);
|
2012-05-03 09:25:13 +02:00
|
|
|
|
2014-12-02 19:59:39 +01:00
|
|
|
static sig_atomic_t exited; exited = 0;
|
|
|
|
static sig_atomic_t sig_exit_status;
|
|
|
|
signal(SIGCHLD, [](int) { wait(&sig_exit_status); exited = 1; });
|
|
|
|
|
2012-05-03 09:25:13 +02:00
|
|
|
String output;
|
|
|
|
if (pid_t pid = fork())
|
|
|
|
{
|
|
|
|
close(write_pipe[0]);
|
|
|
|
close(read_pipe[1]);
|
2012-09-06 14:28:07 +02:00
|
|
|
close(error_pipe[1]);
|
2012-05-29 12:39:03 +02:00
|
|
|
|
2014-04-20 13:15:31 +02:00
|
|
|
write(write_pipe[1], input.data(), (int)input.length());
|
2012-05-03 09:25:13 +02:00
|
|
|
close(write_pipe[1]);
|
|
|
|
|
2014-11-25 02:00:18 +01:00
|
|
|
String error;
|
2012-05-03 09:25:13 +02:00
|
|
|
{
|
2014-11-25 02:00:18 +01:00
|
|
|
auto pipe_reader = [](String& output, bool& closed) {
|
|
|
|
return [&output, &closed](FDWatcher& watcher, EventMode) {
|
|
|
|
if (closed)
|
|
|
|
return;
|
|
|
|
const int fd = watcher.fd();
|
|
|
|
char buffer[1024];
|
|
|
|
size_t size = read(fd, buffer, 1024);
|
|
|
|
if (size <= 0)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
closed = true;
|
|
|
|
}
|
|
|
|
output += String(buffer, buffer+size);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
bool stdout_closed = false, stderr_closed = false;
|
|
|
|
FDWatcher stdout_watcher{read_pipe[0], pipe_reader(output, stdout_closed)};
|
|
|
|
FDWatcher stderr_watcher{error_pipe[0], pipe_reader(error, stderr_closed)};
|
|
|
|
|
|
|
|
while (not stdout_closed or not stderr_closed)
|
|
|
|
EventManager::instance().handle_next_events(EventMode::Urgent);
|
2012-05-03 09:25:13 +02:00
|
|
|
}
|
2012-09-06 14:28:07 +02:00
|
|
|
|
2014-11-25 02:00:18 +01:00
|
|
|
if (not error.empty())
|
|
|
|
write_debug("shell stderr: <<<\n" + error + ">>>");
|
2012-09-06 14:28:07 +02:00
|
|
|
|
2014-12-02 19:59:39 +01:00
|
|
|
signal(SIGCHLD, SIG_DFL);
|
|
|
|
|
|
|
|
if (not exited)
|
|
|
|
waitpid(pid, exit_status, 0);
|
|
|
|
else if (exit_status)
|
|
|
|
*exit_status = sig_exit_status;
|
|
|
|
|
2014-05-05 19:00:24 +02:00
|
|
|
if (exit_status)
|
|
|
|
{
|
|
|
|
if (WIFEXITED(*exit_status))
|
|
|
|
*exit_status = WEXITSTATUS(*exit_status);
|
|
|
|
else
|
|
|
|
*exit_status = -1;
|
|
|
|
}
|
2012-05-03 09:25:13 +02:00
|
|
|
}
|
2012-07-31 14:21:25 +02:00
|
|
|
else try
|
2012-05-03 09:25:13 +02:00
|
|
|
{
|
|
|
|
close(write_pipe[1]);
|
|
|
|
close(read_pipe[0]);
|
2012-09-06 14:28:07 +02:00
|
|
|
close(error_pipe[0]);
|
2012-05-03 09:25:13 +02:00
|
|
|
|
2012-08-29 00:08:39 +02:00
|
|
|
dup2(read_pipe[1], 1); close(read_pipe[1]);
|
2012-09-06 14:28:07 +02:00
|
|
|
dup2(error_pipe[1], 2); close(error_pipe[1]);
|
2012-08-29 00:08:39 +02:00
|
|
|
dup2(write_pipe[0], 0); close(write_pipe[0]);
|
2012-05-03 09:25:13 +02:00
|
|
|
|
2014-10-13 14:12:33 +02:00
|
|
|
RegexIterator<StringView::iterator> it(cmdline.begin(), cmdline.end(), env_var_regex);
|
|
|
|
RegexIterator<StringView::iterator> end;
|
2012-05-03 09:25:13 +02:00
|
|
|
|
|
|
|
while (it != end)
|
|
|
|
{
|
|
|
|
auto& match = *it;
|
|
|
|
|
2014-11-25 02:00:18 +01:00
|
|
|
StringView name = StringView(match[1].first, match[1].second);
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(name.length() > 0);
|
2012-05-03 09:25:13 +02:00
|
|
|
|
2012-05-29 07:19:27 +02:00
|
|
|
auto local_var = env_vars.find(name);
|
|
|
|
if (local_var != env_vars.end())
|
|
|
|
setenv(("kak_" + name).c_str(), local_var->second.c_str(), 1);
|
|
|
|
else
|
2012-05-03 09:25:13 +02:00
|
|
|
{
|
2014-06-18 20:28:48 +02:00
|
|
|
try
|
2012-05-29 07:19:27 +02:00
|
|
|
{
|
2014-06-18 20:28:48 +02:00
|
|
|
String value = get_val(name, context);
|
|
|
|
setenv(("kak_"_str + name).c_str(), value.c_str(), 1);
|
2012-05-29 07:19:27 +02:00
|
|
|
}
|
2014-06-18 20:28:48 +02:00
|
|
|
catch (runtime_error&) {}
|
2012-05-03 09:25:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
++it;
|
|
|
|
}
|
2014-04-20 13:15:31 +02:00
|
|
|
const char* shell = "/bin/sh";
|
|
|
|
auto cmdlinezstr = cmdline.zstr();
|
|
|
|
std::vector<const char*> execparams = { shell, "-c", cmdlinezstr };
|
2012-09-09 17:10:53 +02:00
|
|
|
if (not params.empty())
|
2014-04-20 13:15:31 +02:00
|
|
|
execparams.push_back(shell);
|
2012-09-09 17:10:53 +02:00
|
|
|
for (auto& param : params)
|
|
|
|
execparams.push_back(param.c_str());
|
2013-07-28 16:40:02 +02:00
|
|
|
execparams.push_back(nullptr);
|
2012-09-09 17:10:53 +02:00
|
|
|
|
2014-04-20 13:15:31 +02:00
|
|
|
execvp(shell, (char* const*)execparams.data());
|
2012-07-31 14:21:25 +02:00
|
|
|
exit(-1);
|
2012-05-03 09:25:13 +02:00
|
|
|
}
|
2012-07-31 14:21:25 +02:00
|
|
|
catch (...) { exit(-1); }
|
2012-05-03 09:25:13 +02:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2014-04-20 13:15:31 +02:00
|
|
|
void ShellManager::register_env_var(StringView regex,
|
2012-05-03 09:25:13 +02:00
|
|
|
EnvVarRetriever retriever)
|
|
|
|
{
|
2012-06-25 19:40:18 +02:00
|
|
|
m_env_vars.push_back({ Regex(regex.begin(), regex.end()), std::move(retriever) });
|
2012-05-03 09:25:13 +02:00
|
|
|
}
|
|
|
|
|
2014-06-18 20:28:48 +02:00
|
|
|
String ShellManager::get_val(StringView name, const Context& context) const
|
|
|
|
{
|
|
|
|
auto env_var = std::find_if(
|
|
|
|
m_env_vars.begin(), m_env_vars.end(),
|
|
|
|
[&](const std::pair<Regex, EnvVarRetriever>& pair)
|
2014-10-13 14:12:33 +02:00
|
|
|
{ return regex_match(name.begin(), name.end(), pair.first); });
|
2014-06-18 20:28:48 +02:00
|
|
|
|
|
|
|
if (env_var == m_env_vars.end())
|
|
|
|
throw runtime_error("no such env var: " + name);
|
|
|
|
return env_var->second(name, context);
|
|
|
|
}
|
|
|
|
|
2012-05-03 09:25:13 +02:00
|
|
|
}
|