From 3e7902f7d790a486d3d9cb978df193f07f3a6ad9 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Sun, 29 Jun 2025 20:29:50 +0100 Subject: finish macOS support --- nihil.posix/exec.ccm | 78 +++++++++------------------------------------------- 1 file changed, 13 insertions(+), 65 deletions(-) (limited to 'nihil.posix/exec.ccm') diff --git a/nihil.posix/exec.ccm b/nihil.posix/exec.ccm index 6098318..cd03117 100644 --- a/nihil.posix/exec.ccm +++ b/nihil.posix/exec.ccm @@ -11,95 +11,43 @@ module; #include #include +#include "nihil.hh" + export module nihil.posix:exec; import nihil.error; +import :execv; +import :fexecv; import :argv; import :fd; namespace nihil { /* - * A concept to mark spawn executors. - */ - -export struct exec_tag{}; - -export template -concept executor = - requires (T e) { - std::same_as::tag>; - { e.exec() }; - }; - -/* - * fexecv: use a file descriptor and an argument vector to call ::fexecve(). - * This is the lowest-level executor which all others are implemented - * in terms of. - * - * TODO: Should have a way to pass the environment (envp). - */ -export struct fexecv final { - using tag = exec_tag; - - fexecv(fd &&execfd, argv &&args) noexcept; - - [[nodiscard]] auto exec(this fexecv &self) - -> std::expected; - - // Movable - fexecv(fexecv &&) noexcept; - auto operator=(this fexecv &, fexecv &&) noexcept -> fexecv&; - - // Not copyable (because we hold the open fd object) - fexecv(fexecv const &) = delete; - auto operator=(this fexecv &, fexecv const &) -> fexecv& = delete; - -private: - fd m_execfd; - argv m_args; -}; - -/* - * execv: equivalent to fexecv(), except the command is passed as - * a pathname instead of a file descriptor. Does not search $PATH. - */ -export [[nodiscard]] auto execv(std::string_view path, argv &&argv) - -> std::expected; - -/* - * execvp: equivalent to fexecv(), except the command is passed as - * a filename instead of a file descriptor. If the filename is not - * an absolute path, it will be searched for in $PATH. + * The lowest-level executor type, which is returned by other executors. + * Prefer fexecve() if it's available, otherwise use execve(). */ -export [[nodiscard]] auto execvp(std::string_view file, argv &&argv) - -> std::expected; +#ifdef NIHIL_HAVE_FEXECVE +using base_executor_type = fexecv; +#else +using base_executor_type = execv; +#endif /* * execl: equivalent to execv, except the arguments are passed as a * variadic pack of string-like objects. */ export [[nodiscard]] auto execl(std::string_view path, auto &&...args) - -> std::expected + -> std::expected { return execv(path, argv({std::string_view(args)...})); } -/* - * execlp: equivalent to execvp, except the arguments are passed as a - * variadic pack of string-like objects. - */ -export [[nodiscard]] auto execlp(std::string_view file, auto &&...args) - -> std::expected -{ - return execvp(file, argv({std::string_view(args)...})); -} - /* * shell: run the process by invoking /bin/sh -c with the single argument, * equivalent to system(3). */ export [[nodiscard]] auto shell(std::string_view const &command) - -> std::expected; + -> std::expected; } // namespace nihil -- cgit v1.2.3