2012-05-03 09:25:13 +02:00
|
|
|
#include "shell_manager.hh"
|
|
|
|
|
2012-09-06 14:28:07 +02:00
|
|
|
#include "debug.hh"
|
2012-12-10 18:41:01 +01:00
|
|
|
#include "context.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>
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
2012-08-10 18:47:18 +02:00
|
|
|
String env_var_regex(R"(\$\{kak_(\w+)[^}]*\}|\$kak_(\w+))");
|
2012-05-03 09:25:13 +02:00
|
|
|
|
|
|
|
ShellManager::ShellManager()
|
2012-05-29 12:39:03 +02:00
|
|
|
: m_regex(env_var_regex.begin(), env_var_regex.end())
|
2012-05-03 09:25:13 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-05-29 07:19:27 +02:00
|
|
|
String ShellManager::eval(const String& cmdline, const Context& context,
|
2012-09-09 17:10:53 +02:00
|
|
|
const memoryview<String>& params,
|
2012-05-29 07:19:27 +02:00
|
|
|
const EnvVarMap& env_vars)
|
2012-05-29 12:39:03 +02:00
|
|
|
{
|
2012-09-09 17:10:53 +02:00
|
|
|
return pipe("", cmdline, context, params, env_vars);
|
2012-05-29 12:39:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String ShellManager::pipe(const String& input,
|
|
|
|
const String& cmdline, const Context& context,
|
2012-09-09 17:10:53 +02:00
|
|
|
const memoryview<String>& params,
|
2012-05-29 12:39:03 +02:00
|
|
|
const EnvVarMap& env_vars)
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
memoryview<char> data = input.data();
|
|
|
|
write(write_pipe[1], data.pointer(), data.size());
|
2012-05-03 09:25:13 +02: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 14:28:07 +02: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-27 21:31:48 +01:00
|
|
|
write_debug("shell stderr: <<<\n" + errorout + ">>>");
|
2012-09-06 14:28:07 +02:00
|
|
|
|
2012-05-03 09:25:13 +02:00
|
|
|
waitpid(pid, NULL, 0);
|
|
|
|
}
|
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
|
|
|
|
2013-03-29 14:21:55 +01:00
|
|
|
boost::regex_iterator<String::const_iterator> it(cmdline.begin(), cmdline.end(), m_regex);
|
|
|
|
boost::regex_iterator<String::const_iterator> end;
|
2012-05-03 09:25:13 +02:00
|
|
|
|
|
|
|
while (it != end)
|
|
|
|
{
|
|
|
|
auto& match = *it;
|
|
|
|
|
|
|
|
String name;
|
|
|
|
if (match[1].matched)
|
|
|
|
name = String(match[1].first, match[1].second);
|
|
|
|
else if (match[2].matched)
|
|
|
|
name = String(match[2].first, match[2].second);
|
|
|
|
else
|
|
|
|
assert(false);
|
|
|
|
assert(name.length() > 0);
|
|
|
|
|
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
|
|
|
{
|
2012-06-25 19:40:18 +02: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 07:19:27 +02:00
|
|
|
if (env_var != m_env_vars.end())
|
|
|
|
{
|
2013-02-19 19:06:13 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
String value = env_var->second(name, context);
|
|
|
|
setenv(("kak_" + name).c_str(), value.c_str(), 1);
|
|
|
|
}
|
|
|
|
catch (runtime_error&) {}
|
2012-05-29 07:19:27 +02:00
|
|
|
}
|
2012-05-03 09:25:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
++it;
|
|
|
|
}
|
2013-03-03 17:25:40 +01:00
|
|
|
String shell = context.options()["shell"].get<String>();
|
2012-12-10 18:41:01 +01:00
|
|
|
std::vector<const char*> execparams = { shell.c_str(), "-c", cmdline.c_str() };
|
2012-09-09 17:10:53 +02:00
|
|
|
if (not params.empty())
|
2012-12-10 18:41:01 +01:00
|
|
|
execparams.push_back(shell.c_str());
|
2012-09-09 17:10:53 +02:00
|
|
|
for (auto& param : params)
|
|
|
|
execparams.push_back(param.c_str());
|
|
|
|
execparams.push_back(NULL);
|
|
|
|
|
2012-12-10 18:41:01 +01:00
|
|
|
execvp(shell.c_str(), (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;
|
|
|
|
}
|
|
|
|
|
2012-06-25 19:40:18 +02:00
|
|
|
void ShellManager::register_env_var(const String& 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
|
|
|
}
|
|
|
|
|
|
|
|
}
|