2012-05-03 09:25:13 +02:00
|
|
|
#ifndef shell_manager_hh_INCLUDED
|
|
|
|
#define shell_manager_hh_INCLUDED
|
|
|
|
|
2015-03-09 14:48:41 +01:00
|
|
|
#include "array_view.hh"
|
2014-04-07 22:25:44 +02:00
|
|
|
#include "env_vars.hh"
|
2015-09-03 14:53:17 +02:00
|
|
|
#include "string.hh"
|
|
|
|
#include "utils.hh"
|
2023-11-07 08:01:54 +01:00
|
|
|
#include "unique_descriptor.hh"
|
2016-04-17 20:21:43 +02:00
|
|
|
#include "completion.hh"
|
2012-05-03 09:25:13 +02:00
|
|
|
|
2023-11-07 08:01:54 +01:00
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2012-05-03 09:25:13 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-01-27 21:28:38 +01:00
|
|
|
class Context;
|
2014-11-12 22:27:07 +01:00
|
|
|
|
2015-10-22 14:48:57 +02:00
|
|
|
struct ShellContext
|
|
|
|
{
|
|
|
|
ConstArrayView<String> params;
|
|
|
|
EnvVarMap env_vars;
|
|
|
|
};
|
|
|
|
|
2018-02-18 04:52:29 +01:00
|
|
|
struct EnvVarDesc
|
|
|
|
{
|
2020-03-01 03:28:06 +01:00
|
|
|
using Retriever = Vector<String> (*)(StringView name, const Context&);
|
2018-02-18 04:52:29 +01:00
|
|
|
|
|
|
|
StringView str;
|
|
|
|
bool prefix;
|
|
|
|
Retriever func;
|
|
|
|
};
|
|
|
|
|
2023-11-07 08:01:54 +01:00
|
|
|
inline void closepid(int pid){ kill(pid, SIGTERM); int status = 0; waitpid(pid, &status, 0); }
|
|
|
|
|
|
|
|
using UniqueFd = UniqueDescriptor<::close>;
|
|
|
|
using UniquePid = UniqueDescriptor<closepid>;
|
|
|
|
|
|
|
|
struct Shell
|
|
|
|
{
|
|
|
|
UniquePid pid;
|
|
|
|
UniqueFd in;
|
|
|
|
UniqueFd out;
|
|
|
|
UniqueFd err;
|
|
|
|
};
|
|
|
|
|
2012-05-03 09:25:13 +02:00
|
|
|
class ShellManager : public Singleton<ShellManager>
|
|
|
|
{
|
|
|
|
public:
|
2018-02-18 04:52:29 +01:00
|
|
|
ShellManager(ConstArrayView<EnvVarDesc> builtin_env_vars);
|
2012-05-03 09:25:13 +02:00
|
|
|
|
2015-06-08 14:34:08 +02:00
|
|
|
enum class Flags
|
|
|
|
{
|
|
|
|
None = 0,
|
2015-06-08 23:42:51 +02:00
|
|
|
WaitForStdout = 1
|
2015-06-08 14:34:08 +02:00
|
|
|
};
|
2017-03-15 18:55:34 +01:00
|
|
|
friend constexpr bool with_bit_ops(Meta::Type<Flags>) { return true; }
|
2015-06-08 14:34:08 +02:00
|
|
|
|
2015-03-13 14:39:18 +01:00
|
|
|
std::pair<String, int> eval(StringView cmdline, const Context& context,
|
|
|
|
StringView input = {},
|
2015-06-08 23:42:51 +02:00
|
|
|
Flags flags = Flags::WaitForStdout,
|
2015-10-22 14:48:57 +02:00
|
|
|
const ShellContext& shell_context = {});
|
2012-05-29 12:39:03 +02:00
|
|
|
|
2023-11-07 08:01:54 +01:00
|
|
|
Shell spawn(StringView cmdline,
|
|
|
|
const Context& context,
|
|
|
|
bool open_stdin,
|
2023-12-26 08:09:25 +01:00
|
|
|
const ShellContext& shell_context = {});
|
2023-11-07 08:01:54 +01:00
|
|
|
|
2020-03-01 03:28:06 +01:00
|
|
|
Vector<String> get_val(StringView name, const Context& context) const;
|
2012-05-03 09:25:13 +02:00
|
|
|
|
2016-04-17 20:21:43 +02:00
|
|
|
CandidateList complete_env_var(StringView prefix, ByteCount cursor_pos) const;
|
|
|
|
|
2012-05-03 09:25:13 +02:00
|
|
|
private:
|
2016-12-16 00:04:53 +01:00
|
|
|
String m_shell;
|
|
|
|
|
2018-02-18 04:52:29 +01:00
|
|
|
ConstArrayView<EnvVarDesc> m_env_vars;
|
2012-05-03 09:25:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // shell_manager_hh_INCLUDED
|