-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.hpp
160 lines (135 loc) · 3.12 KB
/
process.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#pragma once
#include <memory>
#include <optional>
#include <span>
#include <string>
#include <vector>
#ifdef _MSC_VER
#include <basetsd.h>
typedef SSIZE_T ssize_t;
#endif
namespace process {
using std::unique_ptr;
class ChildStdin {
public:
ChildStdin();
~ChildStdin();
ChildStdin(ChildStdin &&other);
ChildStdin &operator=(ChildStdin &&other);
size_t write(std::span<const std::byte> buffer);
private:
struct Impl;
unique_ptr<Impl> impl_;
friend class Stdio;
friend class Child;
friend class Command;
};
class ChildStdout {
public:
ChildStdout();
~ChildStdout();
ChildStdout(ChildStdout &&other);
ChildStdout &operator=(ChildStdout &&other);
size_t read(std::span<std::byte> buffer);
size_t read_to_end(std::vector<std::byte> &buffer);
size_t read_to_string(std::string &buffer);
private:
struct Impl;
unique_ptr<Impl> impl_;
friend class Stdio;
friend class Child;
friend class Command;
};
class ChildStderr {
public:
ChildStderr();
~ChildStderr();
ChildStderr(ChildStderr &&other);
ChildStderr &operator=(ChildStderr &&other);
size_t read(std::span<std::byte> buffer);
size_t read_to_end(std::vector<std::byte> &buffer);
size_t read_to_string(std::string &buffer);
private:
struct Impl;
unique_ptr<Impl> impl_;
friend class Stdio;
friend class Child;
friend class Command;
};
class ExitStatus {
public:
bool success();
std::optional<int> code();
ExitStatus();
ExitStatus(ExitStatus &&);
ExitStatus &operator=(ExitStatus &&);
~ExitStatus();
private:
struct Impl;
unique_ptr<Impl> impl_;
friend class Child;
};
struct Output {
ExitStatus status;
std::string std_out;
std::string std_err;
};
class Stdio {
public:
~Stdio();
Stdio(Stdio &&other);
Stdio &operator=(Stdio &&other);
enum class Value { Inherit, NewPipe, FromPipe, Null };
static Stdio pipe();
static Stdio inherit();
static Stdio null();
static Stdio from(ChildStdin);
static Stdio from(ChildStdout);
static Stdio from(ChildStderr);
private:
Stdio(Value value);
struct Impl;
std::unique_ptr<Impl> impl_;
friend class Command;
};
class Child {
public:
std::optional<ChildStdin> io_stdin;
std::optional<ChildStdout> io_stdout;
std::optional<ChildStderr> io_stderr;
~Child();
Child(Child &&other);
Child &operator=(Child &&other);
int id();
void kill();
Output wait_with_output();
ExitStatus wait();
void try_wait();
private:
Child();
struct Impl;
unique_ptr<Impl> impl_;
friend class Command;
};
class Command {
public:
Command(const std::string &app = std::string());
~Command();
Command(Command &&other); // move ctor
Command &operator=(Command &&other); // move assignment
Command &&arg(const std::string &arg);
Command &&args(const std::vector<std::string> &args);
Command &&std_in(Stdio io);
Command &&std_out(Stdio io);
Command &&std_err(Stdio io);
Command &¤t_dir(const std::string &path);
Command &&env(const std::string &key, const std::string &value);
Command &&env_clear();
ExitStatus status();
Output output();
Child spawn();
private:
class Impl;
unique_ptr<Impl> impl_;
};
} // namespace process