2012-05-03 11:25:13 +04:00
|
|
|
#include "shell_manager.hh"
|
|
|
|
|
2012-12-10 21:41:01 +04:00
|
|
|
#include "context.hh"
|
2013-04-09 22:05:40 +04:00
|
|
|
#include "debug.hh"
|
2012-09-06 16:28:07 +04:00
|
|
|
|
2012-05-03 11:25:13 +04:00
|
|
|
#include <cstring>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
2013-04-17 21:26:44 +04:00
|
|
|
|
2014-03-25 23:43:56 +04:00
|
|
|
static const Regex env_var_regex(R"(\bkak_(\w+)\b)");
|
2012-05-03 11:25:13 +04:00
|
|
|
|
|
|
|
ShellManager::ShellManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-20 15:15:31 +04:00
|
|
|
String ShellManager::eval(StringView cmdline, const Context& context,
|
2013-07-26 03:17:12 +04:00
|
|
|
memoryview<String> params,
|
2012-05-29 09:19:27 +04:00
|
|
|
const EnvVarMap& env_vars)
|
2012-05-29 14:39:03 +04:00
|
|
|
{
|
2012-09-09 19:10:53 +04:00
|
|
|
return pipe("", cmdline, context, params, env_vars);
|
2012-05-29 14:39:03 +04:00
|
|
|
}
|
|
|
|
|
2014-04-20 15:15:31 +04:00
|
|
|
String ShellManager::pipe(StringView input,
|
|
|
|
StringView cmdline, const Context& context,
|
2013-07-26 03:17:12 +04:00
|
|
|
memoryview<String> params,
|
2012-05-29 14:39:03 +04:00
|
|
|
const EnvVarMap& env_vars)
|
2012-05-03 11:25:13 +04:00
|
|
|
{
|
2012-09-06 16:28:07 +04:00
|
|
|
int write_pipe[2]; // child stdin
|
|
|
|
int read_pipe[2]; // child stdout
|
|
|
|
int error_pipe[2]; // child stderr
|
2012-05-03 11:25:13 +04:00
|
|
|
|
2012-05-29 14:39:03 +04:00
|
|
|
::pipe(write_pipe);
|
|
|
|
::pipe(read_pipe);
|
2012-09-06 16:28:07 +04:00
|
|
|
::pipe(error_pipe);
|
2012-05-03 11:25:13 +04:00
|
|
|
|
|
|
|
String output;
|
|
|
|
if (pid_t pid = fork())
|
|
|
|
{
|
|
|
|
close(write_pipe[0]);
|
|
|
|
close(read_pipe[1]);
|
2012-09-06 16:28:07 +04:00
|
|
|
close(error_pipe[1]);
|
2012-05-29 14:39:03 +04:00
|
|
|
|
2014-04-20 15:15:31 +04:00
|
|
|
write(write_pipe[1], input.data(), (int)input.length());
|
2012-05-03 11:25:13 +04:00
|
|
|
close(write_pipe[1]);
|
|
|
|
|
|
|
|
char buffer[1024];
|
|
|
|
while (size_t size = read(read_pipe[0], buffer, 1024))
|
|
|
|
{
|
|
|
|
if (size == -1)
|
|
|
|
break;
|
|
|
|
output += String(buffer, buffer+size);
|
|
|
|
}
|
|
|
|
close(read_pipe[0]);
|
2012-09-06 16:28:07 +04:00
|
|
|
|
|
|
|
String errorout;
|
|
|
|
while (size_t size = read(error_pipe[0], buffer, 1024))
|
|
|
|
{
|
|
|
|
if (size == -1)
|
|
|
|
break;
|
|
|
|
errorout += String(buffer, buffer+size);
|
|
|
|
}
|
|
|
|
close(error_pipe[0]);
|
|
|
|
if (not errorout.empty())
|
2013-02-28 00:31:48 +04:00
|
|
|
write_debug("shell stderr: <<<\n" + errorout + ">>>");
|
2012-09-06 16:28:07 +04:00
|
|
|
|
2013-07-28 18:40:02 +04:00
|
|
|
waitpid(pid, nullptr, 0);
|
2012-05-03 11:25:13 +04:00
|
|
|
}
|
2012-07-31 16:21:25 +04:00
|
|
|
else try
|
2012-05-03 11:25:13 +04:00
|
|
|
{
|
|
|
|
close(write_pipe[1]);
|
|
|
|
close(read_pipe[0]);
|
2012-09-06 16:28:07 +04:00
|
|
|
close(error_pipe[0]);
|
2012-05-03 11:25:13 +04:00
|
|
|
|
2012-08-29 02:08:39 +04:00
|
|
|
dup2(read_pipe[1], 1); close(read_pipe[1]);
|
2012-09-06 16:28:07 +04:00
|
|
|
dup2(error_pipe[1], 2); close(error_pipe[1]);
|
2012-08-29 02:08:39 +04:00
|
|
|
dup2(write_pipe[0], 0); close(write_pipe[0]);
|
2012-05-03 11:25:13 +04:00
|
|
|
|
2014-04-20 15:15:31 +04:00
|
|
|
boost::regex_iterator<StringView::iterator> it(cmdline.begin(), cmdline.end(), env_var_regex);
|
|
|
|
boost::regex_iterator<StringView::iterator> end;
|
2012-05-03 11:25:13 +04:00
|
|
|
|
|
|
|
while (it != end)
|
|
|
|
{
|
|
|
|
auto& match = *it;
|
|
|
|
|
2014-04-20 15:15:31 +04:00
|
|
|
StringView name;
|
2012-05-03 11:25:13 +04:00
|
|
|
if (match[1].matched)
|
2014-04-20 15:15:31 +04:00
|
|
|
name = StringView(match[1].first, match[1].second);
|
2012-05-03 11:25:13 +04:00
|
|
|
else if (match[2].matched)
|
2014-04-20 15:15:31 +04:00
|
|
|
name = StringView(match[2].first, match[2].second);
|
2012-05-03 11:25:13 +04:00
|
|
|
else
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(false);
|
|
|
|
kak_assert(name.length() > 0);
|
2012-05-03 11:25:13 +04:00
|
|
|
|
2012-05-29 09:19:27 +04: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 11:25:13 +04:00
|
|
|
{
|
2012-06-25 21:40:18 +04:00
|
|
|
auto env_var = std::find_if(
|
|
|
|
m_env_vars.begin(), m_env_vars.end(),
|
|
|
|
[&](const std::pair<Regex, EnvVarRetriever>& pair)
|
|
|
|
{ return boost::regex_match(name.begin(), name.end(),
|
|
|
|
pair.first); });
|
|
|
|
|
2012-05-29 09:19:27 +04:00
|
|
|
if (env_var != m_env_vars.end())
|
|
|
|
{
|
2013-02-19 22:06:13 +04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
String value = env_var->second(name, context);
|
2014-04-20 15:15:31 +04:00
|
|
|
setenv(("kak_"_str + name).c_str(), value.c_str(), 1);
|
2013-02-19 22:06:13 +04:00
|
|
|
}
|
|
|
|
catch (runtime_error&) {}
|
2012-05-29 09:19:27 +04:00
|
|
|
}
|
2012-05-03 11:25:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
++it;
|
|
|
|
}
|
2014-04-20 15:15:31 +04:00
|
|
|
const char* shell = "/bin/sh";
|
|
|
|
auto cmdlinezstr = cmdline.zstr();
|
|
|
|
std::vector<const char*> execparams = { shell, "-c", cmdlinezstr };
|
2012-09-09 19:10:53 +04:00
|
|
|
if (not params.empty())
|
2014-04-20 15:15:31 +04:00
|
|
|
execparams.push_back(shell);
|
2012-09-09 19:10:53 +04:00
|
|
|
for (auto& param : params)
|
|
|
|
execparams.push_back(param.c_str());
|
2013-07-28 18:40:02 +04:00
|
|
|
execparams.push_back(nullptr);
|
2012-09-09 19:10:53 +04:00
|
|
|
|
2014-04-20 15:15:31 +04:00
|
|
|
execvp(shell, (char* const*)execparams.data());
|
2012-07-31 16:21:25 +04:00
|
|
|
exit(-1);
|
2012-05-03 11:25:13 +04:00
|
|
|
}
|
2012-07-31 16:21:25 +04:00
|
|
|
catch (...) { exit(-1); }
|
2012-05-03 11:25:13 +04:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2014-04-20 15:15:31 +04:00
|
|
|
void ShellManager::register_env_var(StringView regex,
|
2012-05-03 11:25:13 +04:00
|
|
|
EnvVarRetriever retriever)
|
|
|
|
{
|
2012-06-25 21:40:18 +04:00
|
|
|
m_env_vars.push_back({ Regex(regex.begin(), regex.end()), std::move(retriever) });
|
2012-05-03 11:25:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|