| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #pragma once |
| 4 | |
| 5 | #include "parser.hpp" |
| 6 | |
| 7 | #include <cassert> |
| 8 | #include <ctime> |
| 9 | #include <filesystem> |
| 10 | #include <map> |
| 11 | #include <memory> |
| 12 | #include <vector> |
| 13 | |
| 14 | struct LaunchContext |
| 15 | { |
| 16 | explicit LaunchContext(std::unique_ptr<ProgramSpec> &&program_spec); |
| 17 | |
| 18 | explicit LaunchContext(const std::vector<std::string> &argv); |
| 19 | |
| 20 | /** |
| 21 | * @brief Get the command name, i.e. the first argument as argv[0] |
| 22 | * |
| 23 | * @return std::string |
| 24 | */ |
| 25 | std::string command() const |
| 26 | { |
| 27 | assert(!argv.empty()); |
| 28 | return argv[0]; |
| 29 | } |
| 30 | |
| 31 | void redirect(int fd, std::unique_ptr<BaseRedirection> &&redirection) |
| 32 | { |
| 33 | redirections[fd] = std::move(redirection); |
| 34 | } |
| 35 | |
| 36 | std::filesystem::path program_path() const |
| 37 | { |
| 38 | return m_program_path; |
| 39 | } |
| 40 | |
| 41 | // resolve the program path |
| 42 | bool resolve_program_path(); |
| 43 | |
| 44 | // start the program (or builtin, or alias) |
| 45 | bool start(); |
| 46 | |
| 47 | std::vector<std::string> argv; //< the arguments (including the command), e.g. {"ls", "-l", "-a"} |
| 48 | |
| 49 | bool should_wait = true; |
| 50 | int exit_code = 0; |
| 51 | int exit_signal = 0; |
| 52 | |
| 53 | std::map<int, std::unique_ptr<BaseRedirection>> redirections; // fd -> redirection |
| 54 | |
| 55 | struct |
| 56 | { |
| 57 | bool builtin, program, alias; |
| 58 | } launch_type = { .builtin: true, .program: true, .alias: true }; |
| 59 | |
| 60 | private: |
| 61 | bool try_start_alias() const; |
| 62 | bool try_start_builtin() const; |
| 63 | bool try_start_program(); |
| 64 | |
| 65 | bool spawn_in_child() const; |
| 66 | |
| 67 | private: |
| 68 | std::unique_ptr<ProgramSpec> program_spec; |
| 69 | std::filesystem::path m_program_path; //< the path to the program that to be executed, e.g. "/bin/ls" |
| 70 | }; |
| 71 | |