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
14struct 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 bool success = false;
53
54 std::map<int, std::unique_ptr<BaseRedirection>> redirections; // fd -> redirection
55
56 struct
57 {
58 bool builtin, program, alias;
59 } launch_type = { .builtin: true, .program: true, .alias: true };
60
61 private:
62 bool try_start_alias();
63 bool try_start_builtin();
64 bool try_start_program();
65
66 bool spawn_in_child() const;
67
68 private:
69 std::unique_ptr<ProgramSpec> program_spec;
70 std::filesystem::path m_program_path; //< the path to the program that to be executed, e.g. "/bin/ls"
71};
72