summaryrefslogtreecommitdiff
path: root/src/shell_manager.cc
blob: 8ac683ef45dc57a6410a76497ed316f198923c97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "shell_manager.hh"

#include "context.hh"
#include "debug.hh"
#include "file.hh"

#include <cstring>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

namespace Kakoune
{

static const Regex env_var_regex(R"(\bkak_(\w+)\b)");

ShellManager::ShellManager()
{
    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);
}

String ShellManager::eval(StringView cmdline, const Context& context,
                          memoryview<String> params,
                          const EnvVarMap& env_vars,
                          int* exit_status)
{
    return pipe("", cmdline, context, params, env_vars, exit_status);
}

String ShellManager::pipe(StringView input,
                          StringView cmdline, const Context& context,
                          memoryview<String> params,
                          const EnvVarMap& env_vars,
                          int* exit_status)
{
    int write_pipe[2]; // child stdin
    int read_pipe[2];  // child stdout
    int error_pipe[2]; // child stderr

    ::pipe(write_pipe);
    ::pipe(read_pipe);
    ::pipe(error_pipe);

    String output;
    if (pid_t pid = fork())
    {
        close(write_pipe[0]);
        close(read_pipe[1]);
        close(error_pipe[1]);

        write(write_pipe[1], input.data(), (int)input.length());
        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]);

        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())
            write_debug("shell stderr: <<<\n" + errorout + ">>>");

        waitpid(pid, exit_status, 0);
        if (exit_status)
        {
            if (WIFEXITED(*exit_status))
                *exit_status = WEXITSTATUS(*exit_status);
            else
                *exit_status = -1;
        }
    }
    else try
    {
        close(write_pipe[1]);
        close(read_pipe[0]);
        close(error_pipe[0]);

        dup2(read_pipe[1], 1); close(read_pipe[1]);
        dup2(error_pipe[1], 2); close(error_pipe[1]);
        dup2(write_pipe[0], 0); close(write_pipe[0]);

        RegexIterator<StringView::iterator> it(cmdline.begin(), cmdline.end(), env_var_regex);
        RegexIterator<StringView::iterator> end;

        while (it != end)
        {
            auto& match = *it;

            StringView name;
            if (match[1].matched)
                name = StringView(match[1].first, match[1].second);
            else if (match[2].matched)
                name = StringView(match[2].first, match[2].second);
            else
                kak_assert(false);
            kak_assert(name.length() > 0);

            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
            {
                try
                {
                    String value = get_val(name, context);
                    setenv(("kak_"_str + name).c_str(), value.c_str(), 1);
                }
                catch (runtime_error&) {}
            }

            ++it;
        }
        const char* shell = "/bin/sh";
        auto cmdlinezstr = cmdline.zstr();
        std::vector<const char*> execparams = { shell, "-c", cmdlinezstr };
        if (not params.empty())
            execparams.push_back(shell);
        for (auto& param : params)
            execparams.push_back(param.c_str());
        execparams.push_back(nullptr);

        execvp(shell, (char* const*)execparams.data());
        exit(-1);
    }
    catch (...) { exit(-1); }
    return output;
}

void ShellManager::register_env_var(StringView regex,
                                    EnvVarRetriever retriever)
{
    m_env_vars.push_back({ Regex(regex.begin(), regex.end()), std::move(retriever) });
}

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)
        { return regex_match(name.begin(), name.end(), pair.first); });

    if (env_var == m_env_vars.end())
        throw runtime_error("no such env var: " + name);
    return env_var->second(name, context);
}

}