-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsub_exec.cpp
More file actions
154 lines (131 loc) · 4.14 KB
/
sub_exec.cpp
File metadata and controls
154 lines (131 loc) · 4.14 KB
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
//
// A contribution to https://github.com/xairline/xa-snow by zodiac1214
//
// Copyright (C) 2025 Holger Teutsch
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
// USA
//
#include <string>
#include <cstdlib>
#ifdef _WIN32
#include <windows.h>
#include <system_error>
#endif
#include "xa-snow.h"
// returns 0 on success, != 0 some exit code
int
sub_exec(const std::string& command)
{
std::string output;
#ifdef _WIN32
std::error_code ec;
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.dwFlags |= STARTF_USESTDHANDLES;
si.dwFlags |= STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
SECURITY_ATTRIBUTES security_attributes;
ZeroMemory(&security_attributes, sizeof(security_attributes));
security_attributes.nLength = sizeof(security_attributes);
security_attributes.bInheritHandle = TRUE;
// Create pipes for the child's STDOUT and STDIN.
HANDLE hStdOutRead, hStdOutWrite;
if (!CreatePipe(&hStdOutRead, &hStdOutWrite, &security_attributes, 0))
{
ec = std::error_code(GetLastError(), std::system_category());
return -1;
}
// Ensure the read handle to the pipe for STDOUT is not inherited.
if (!SetHandleInformation(hStdOutRead, HANDLE_FLAG_INHERIT, 0))
{
ec = std::error_code(GetLastError(), std::system_category());
return -1;
}
si.hStdOutput = hStdOutWrite;
si.hStdError = hStdOutWrite;
// Start the child process.
if (!CreateProcess(NULL,
const_cast<char*>(command.c_str()),
NULL,
NULL,
TRUE,
0,
NULL,
NULL,
&si,
&pi)){
//logMessage(simple_format("CreateProcess failed %.", GetLastError()));
LogMsg("CreateProcess failed");
ec = std::error_code(GetLastError(), std::system_category());
CloseHandle(hStdOutWrite);
CloseHandle(hStdOutRead);
return -1;
}
// Close handles to the child's STDOUT and stdin.
CloseHandle(hStdOutWrite);
// Read from pipe and invoke callback.
char buffer[256];
DWORD readBytes;
while (ReadFile(hStdOutRead, buffer, sizeof(buffer) - 1, &readBytes, NULL) && readBytes > 0) {
buffer[readBytes] = '\0';
output += buffer;
}
// Close remaining handles.
CloseHandle(hStdOutRead);
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD exit_code;
if (!GetExitCodeProcess(pi.hProcess, (LPDWORD)&exit_code)) {
ec = std::error_code(GetLastError(), std::system_category());
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return -1;
}
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
ec.clear();
if (exit_code != 0)
LogMsg("sub_exec output: '%s', exit_code: %ld", output.c_str(), exit_code);
return exit_code;
#else
int exit_code = 0;
// For Unix-like systems
char buffer[512];
FILE* pipe = popen(command.c_str(), "r");
if (!pipe) {
LogMsg("popen() failed!");
return 1;
}
while (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
output += buffer;
}
exit_code = pclose(pipe);
if (exit_code != 0)
LogMsg("sub_exec output: '%s', exit_code: %d", output.c_str(), exit_code);
return exit_code;
#endif
}
#if 0
#include <iostream>
int
main()
{
int res = exec("bin\\WIN32wgrib2.exe");
std::cout << res << std::endl;
}
#endif