This repository has been archived by the owner on Jun 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathexamples.cpp
177 lines (142 loc) · 6.92 KB
/
examples.cpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "process.hpp"
#include <iostream>
using namespace std;
using namespace TinyProcessLib;
int main() {
#if !defined(_WIN32) || defined(MSYS_PROCESS_USE_SH)
//The following examples are for Unix-like systems and Windows through MSYS2
cout << "Example 1a - the mandatory Hello World through an executable" << endl;
Process process1a("echo Hello World", "", [](const char *bytes, size_t n) {
cout << "Output from stdout: " << string(bytes, n);
});
auto exit_status=process1a.get_exit_status();
cout << "Example 1a process returned: " << exit_status << " (" << (exit_status==0?"success":"failure") << ")" << endl;
this_thread::sleep_for(chrono::seconds(5));
#ifndef _WIN32
cout << endl << "Example 1b - Hello World through a function on Unix-like systems" << endl;
Process process1b([] {
cout << "Hello World" << endl;
exit(0);
}, [](const char *bytes, size_t n) {
cout << "Output from stdout: " << string(bytes, n);
});
exit_status=process1b.get_exit_status();
cout << "Example 1b process returned: " << exit_status << " (" << (exit_status==0?"success":"failure") << ")" << endl;
this_thread::sleep_for(chrono::seconds(5));
#endif
cout << endl << "Example 2 - cd into a nonexistent directory" << endl;
Process process2("cd a_nonexistent_directory", "", [](const char *bytes, size_t n) {
cout << "Output from stdout: " << string(bytes, n);
}, [](const char *bytes, size_t n) {
cout << "Output from stderr: " << string(bytes, n);
//add a newline for prettier output on some platforms:
if(bytes[n-1]!='\n')
cout << endl;
});
exit_status=process2.get_exit_status();
cout << "Example 2 process returned: " << exit_status << " (" << (exit_status==0?"success":"failure") << ")" << endl;
this_thread::sleep_for(chrono::seconds(5));
cout << endl << "Example 3 - async sleep process" << endl;
thread thread3([]() {
Process process3("sleep 5");
auto exit_status=process3.get_exit_status();
cout << "Example 3 process returned: " << exit_status << " (" << (exit_status==0?"success":"failure") << ")" << endl;
});
thread3.detach();
this_thread::sleep_for(chrono::seconds(10));
cout << endl << "Example 4 - killing async sleep process after 5 seconds" << endl;
auto process4=make_shared<Process>("sleep 10");
thread thread4([process4]() {
auto exit_status=process4->get_exit_status();
cout << "Example 4 process returned: " << exit_status << " (" << (exit_status==0?"success":"failure") << ")" << endl;
});
thread4.detach();
this_thread::sleep_for(chrono::seconds(5));
process4->kill();
this_thread::sleep_for(chrono::seconds(5));
cout << endl << "Example 5 - multiple commands, stdout and stderr" << endl;
Process process5("echo Hello && ls an_incorrect_path", "", [](const char *bytes, size_t n) {
cout << "Output from stdout: " << string(bytes, n);
}, [](const char *bytes, size_t n) {
cout << "Output from stderr: " << string(bytes, n);
//add a newline for prettier output on some platforms:
if(bytes[n-1]!='\n')
cout << endl;
});
exit_status=process5.get_exit_status();
cout << "Example 5 process returned: " << exit_status << " (" << (exit_status==0?"success":"failure") << ")" << endl;
this_thread::sleep_for(chrono::seconds(5));
cout << endl << "Example 6 - run bash with input from stdin" << endl;
Process process6("bash", "", [](const char *bytes, size_t n) {
cout << "Output from stdout: " << string(bytes, n);
}, nullptr, true);
process6.write("echo Hello from bash\n");
process6.write("exit\n");
exit_status=process6.get_exit_status();
cout << "Example 6 process returned: " << exit_status << " (" << (exit_status==0?"success":"failure") << ")" << endl;
this_thread::sleep_for(chrono::seconds(5));
cout << endl << "Example 7 - send data to cat through stdin" << endl;
Process process7("cat", "", [](const char *bytes, size_t n) {
cout << "Output from stdout: " << string(bytes, n);
}, nullptr, true);
process7.write("Hello cat\n");
process7.close_stdin();
exit_status=process7.get_exit_status();
cout << "Example 7 process returned: " << exit_status << " (" << (exit_status==0?"success":"failure") << ")" << endl;
this_thread::sleep_for(chrono::seconds(5));
cout << endl << "Example 8 - demonstrates Process::try_get_exit_status" << endl;
Process process8("sleep 5");
while(!process8.try_get_exit_status(exit_status)) {
cout << "Example 8 process is running" << endl;
this_thread::sleep_for(chrono::seconds(2));
}
cout << "Example 8 process returned: " << exit_status << " (" << (exit_status==0?"success":"failure") << ")" << endl;
#else
//Examples for Windows without MSYS2
cout << "Example 1 - the mandatory Hello World" << endl;
Process process1("cmd /C echo Hello World", "", [](const char *bytes, size_t n) {
cout << "Output from stdout: " << string(bytes, n);
});
auto exit_status=process1.get_exit_status();
cout << "Example 1 process returned: " << exit_status << " (" << (exit_status==0?"success":"failure") << ")" << endl;
this_thread::sleep_for(chrono::seconds(5));
cout << endl << "Example 2 - cd into a nonexistent directory" << endl;
Process process2("cmd /C cd a_nonexistent_directory", "", [](const char *bytes, size_t n) {
cout << "Output from stdout: " << string(bytes, n);
}, [](const char *bytes, size_t n) {
cout << "Output from stderr: " << string(bytes, n);
//add a newline for prettier output on some platforms:
if(bytes[n-1]!='\n')
cout << endl;
});
exit_status=process2.get_exit_status();
cout << "Example 2 process returned: " << exit_status << " (" << (exit_status==0?"success":"failure") << ")" << endl;
this_thread::sleep_for(chrono::seconds(5));
cout << endl << "Example 3 - async sleep process" << endl;
thread thread3([]() {
Process process3("timeout 5");
auto exit_status=process3.get_exit_status();
cout << "Example 3 process returned: " << exit_status << " (" << (exit_status==0?"success":"failure") << ")" << endl;
});
thread3.detach();
this_thread::sleep_for(chrono::seconds(10));
cout << endl << "Example 4 - killing async sleep process after 5 seconds" << endl;
auto process4=make_shared<Process>("timeout 10");
thread thread4([process4]() {
auto exit_status=process4->get_exit_status();
cout << "Example 4 process returned: " << exit_status << " (" << (exit_status==0?"success":"failure") << ")" << endl;
});
thread4.detach();
this_thread::sleep_for(chrono::seconds(5));
process4->kill();
this_thread::sleep_for(chrono::seconds(5));
cout << endl << "Example 5 - demonstrates Process::try_get_exit_status" << endl;
Process process5("timeout 5");
while(!process5.try_get_exit_status(exit_status)) {
cout << "Example 5 process is running" << endl;
this_thread::sleep_for(chrono::seconds(2));
}
cout << "Example 5 process returned: " << exit_status << " (" << (exit_status==0?"success":"failure") << ")" << endl;
#endif
return 0;
}