#include "mologram/ui/TearOff.hpp"
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <sstream>
#include <sys/wait.h>
#include <unistd.h>
namespace mologram::ui {
TearOff::Env TearOff::realEnv() {
Env e;
e.getenv = [](const char* n) { const char* v = std::getenv(n); return v ? std::string(v) : std::string(); };
e.inPath = [](const std::string& exe) {
if (exe.find('/') != std::string::npos) return ::access(exe.c_str(), X_OK) == 0;
const char* path = std::getenv("PATH");
std::string p = path ? path : "";
std::stringstream ss(p);
std::string dir;
while (std::getline(ss, dir, ':')) if (!dir.empty() && ::access((dir + "/" + exe).c_str(), X_OK) == 0) return true;
return false;
};
return e;
}
std::vector<std::string> TearOff::appCommand(const Config& cfg, const std::string& self, const std::string& peerKey, const std::string& socket) {
std::vector<std::string> v{self, "--chat", peerKey, "--socket", socket};
std::string acct = cfg.str("account");
if (!acct.empty() && acct != "main") { v.push_back("--account"); v.push_back(acct); }
return v;
}
std::vector<std::string> TearOff::command(const Config& cfg, const std::string& self, const std::string& peerKey, const std::string& socket, const Env& env) {
std::vector<std::string> app = appCommand(cfg, self, peerKey, socket);
std::string tpl = cfg.str("terminal.command");
if (!tpl.empty()) {
std::vector<std::string> out;
std::stringstream ss(tpl);
std::string tok;
bool placed = false;
while (ss >> tok) {
if (tok == "{cmd}") { out.insert(out.end(), app.begin(), app.end()); placed = true; }
else out.push_back(tok);
}
if (!placed) { out.push_back("-e"); out.insert(out.end(), app.begin(), app.end()); }
return out;
}
auto with = [&](const std::string& term, const char* flag) {
std::vector<std::string> out{term};
if (flag && *flag) out.push_back(flag);
out.insert(out.end(), app.begin(), app.end());
return out;
};
std::string t = env.getenv("TERMINAL");
if (!t.empty() && env.inPath(t)) return with(t, "-e");
if (env.inPath("x-terminal-emulator")) return with("x-terminal-emulator", "-e");
if (env.inPath("gnome-terminal")) return with("gnome-terminal", "--");
if (env.inPath("xterm")) return with("xterm", "-e");
return {};
}
bool TearOff::spawn(const std::vector<std::string>& argv, std::string& err) {
if (argv.empty()) { err = "empty command"; return false; }
int pipefd[2];
if (::pipe2(pipefd, O_CLOEXEC) != 0) { err = std::string("pipe: ") + std::strerror(errno); return false; }
pid_t pid = ::fork();
if (pid < 0) { err = std::string("fork: ") + std::strerror(errno); ::close(pipefd[0]); ::close(pipefd[1]); return false; }
if (pid == 0) {
::setsid();
pid_t pid2 = ::fork();
if (pid2 != 0) _exit(0); // the grandchild is re-parented, so no zombie stays behind
int devnull = ::open("/dev/null", O_RDWR);
if (devnull >= 0) { ::dup2(devnull, 0); ::dup2(devnull, 1); ::dup2(devnull, 2); }
std::vector<char*> args;
for (const auto& a : argv) args.push_back(const_cast<char*>(a.c_str()));
args.push_back(nullptr);
::execvp(args[0], args.data());
int e = errno;
ssize_t w = ::write(pipefd[1], &e, sizeof(e));
(void)w;
_exit(127);
}
::close(pipefd[1]);
int status = 0;
::waitpid(pid, &status, 0);
int e = 0;
ssize_t n = ::read(pipefd[0], &e, sizeof(e)); // closed without data when exec succeeded (CLOEXEC)
::close(pipefd[0]);
if (n == (ssize_t)sizeof(e)) { err = argv[0] + ": " + std::strerror(e); return false; }
return true;
}
} // namespace mologram::ui