-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
77 lines (56 loc) · 2.31 KB
/
main.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
#include "System2.hpp"
#include <iostream>
#define EXIT_IF_FAILED(result) \
if(result != SYSTEM2_RESULT_SUCCESS) \
{\
printf("Error at %d: %d", __LINE__, result);\
return -1;\
}
int main(...)
{
{
System2CommandInfo commandInfo = {};
commandInfo.RedirectInput = true;
commandInfo.RedirectOutput = true;
SYSTEM2_RESULT result;
#if defined(__unix__) || defined(__APPLE__)
result = System2CppRun("read testVar && echo testVar is \\\"$testVar\\\"", commandInfo);
#endif
#if defined(_WIN32)
result = System2CppRun("set /p testVar= && echo testVar is \"!testVar!\"", commandInfo);
#endif
EXIT_IF_FAILED(result);
std::string input = "test content\n";
result = System2CppWriteToInput(commandInfo, input);
EXIT_IF_FAILED(result);
//Waiting here simulates the child process has "finished" and we read the output of it
//Sleep(2000);
std::string output;
result = System2CppReadFromOutput(commandInfo, output);
EXIT_IF_FAILED(result);
int returnCode = -1;
result = System2CppGetCommandReturnValueSync(commandInfo, returnCode);
EXIT_IF_FAILED(result);
std::cout << output << std::endl;
std::cout << "Command has executed with return value: " << returnCode << std::endl;
}
//Output: Command has executed with return value: : 0
//Output: testVar is "test content"
std::cout << "Using stdin now, enter the value of testVar: " << std::flush;
{
System2CommandInfo commandInfo = {};
SYSTEM2_RESULT result;
#if defined(__unix__) || defined(__APPLE__)
result = System2CppRun("read testVar && echo testVar is \\\"$testVar\\\"", commandInfo);
#endif
#if defined(_WIN32)
result = System2CppRun("set /p testVar= && echo testVar is \"!testVar!\"", commandInfo);
#endif
EXIT_IF_FAILED(result);
int returnCode = -1;
result = System2CppGetCommandReturnValueSync(commandInfo, returnCode);
EXIT_IF_FAILED(result);
std::cout << "Command has executed with return value: " << returnCode << std::endl;
}
return 0;
}