-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathProcess.hpp
156 lines (125 loc) · 4.32 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
#pragma once
#include <nstd/String.hpp>
#include <nstd/Map.hpp>
class Process
{
public:
Process();
~Process();
/**
* Start an external process.
* @param[in] command The command to start the process. The first word in \c command should be the name of the executable or a path
* to the executable. Further words in \c command are considered to be arguments for the process.
* @param[in] environment Environment variables to be used for new process. The environment variable from the current process will be inherited if the map is empty.
* @return The process id of the newly started process or \c 0 if an errors occurred.
*/
uint32 start(const String& command, const Map<String, String>& environment = Map<String, String>());
/**
* Start an external process.
* @param[in] executable The path to the executable to be started.
* @param[in] argc The amount of parameters in \c argv.
* @param[in] argv Arguments to the process.
* @param[in] environment Environment variables to be used for new process. The environment variable from the current process will be inherited if the map is empty.
* @return The process id of the newly started process or \c 0 if an errors occurred.
*/
uint32 start(const String& executable, int argc, char* const argv[], const Map<String, String>& environment = Map<String, String>());
/**
* Get id of the process.
* @return The process id or \c 0 if a process was not started.
*/
uint32 getProcessId() const {return pid;}
/**
* Return the running state of the process.
* @return \c true when the process is currently running and can be joined using \c join().
*/
bool isRunning() const;
/**
* Wait for the process to terminate and get its exit code.
* @param[out] exitCode The exit code of the process.
* @return Whether the process terminated properly.
*/
bool join(uint32& exitCode);
/**
* Wait for the process to terminate.
* @return Whether the process terminated properly.
*/
bool join();
/**
* Kill and join the process. The method send a hard KILL signal to the process and waits for it to terminate.
* @return Whether the process terminated properly.
*/
bool kill();
enum Stream
{
stdoutStream = 0x01,
stderrStream = 0x02,
stdinStream = 0x04,
};
bool open(const String& command, uint streams = stdoutStream);
bool open(const String& executable, int argc, char* const argv[], uint streams = stdoutStream);
bool open(const String& executable, const List<String>& args, uint streams = stdoutStream);
/*
* Closes the streams of an opened process.
* @param [in] streams The streams to be closed.
*/
void close(uint streams = stdoutStream | stderrStream | stdinStream);
ssize read(void* buffer, usize length);
ssize read(void* buffer, usize length, uint& streams);
ssize write(const void* buffer, usize length);
static uint32 getCurrentProcessId();
static void exit(uint32 exitCode);
static String getEnvironmentVariable(const String& name, const String& defaultValue = String());
static bool setEnvironmentVariable(const String& name, const String& value);
static Map<String, String> getEnvironmentVariables();
static String getExecutablePath();
static Process* wait(Process** processes, usize count);
static void interrupt();
#ifndef _WIN32
static bool daemonize(const String& logFile = "/dev/null");
#endif
public:
enum OptionFlags
{
optionFlag = 0x0,
argumentFlag = 0x1,
optionalFlag = 0x2,
};
struct Option
{
int character;
const char* name;
uint32 flags;
};
class Arguments
{
public:
template<usize N> Arguments(int argc, char* argv[], const Option(&options)[N]) : argv(argv), argvEnd(argv + argc), options(options), optionsEnd(options + N), arg(""), inOpt(false), skipOpt(false) {++this->argv;}
bool read(int& character, String& argument);
private:
char** argv;
char** argvEnd;
const Option* options;
const Option* optionsEnd;
const char* arg;
bool inOpt;
bool skipOpt;
private:
bool nextChar();
};
private:
#ifdef _WIN32
void* hProcess;
void* hStdOutRead;
void* hStdErrRead;
void* hStdInWrite;
#else
int fdStdOutRead;
int fdStdErrRead;
int fdStdInWrite;
#endif
uint32 pid;
Process(const Process&);
Process& operator=(const Process&);
private:
class Private;
};