4d669095d83ca556a0a2a6ec29464f021ea944f9
5 bool Shell::run(const string
&command_
, string
&output
) {
6 auto command
= prepareCommand(command_
);
7 auto *file
= popen(command
.c_str(), "r");
10 perror("Shell::run popen()");
16 auto read
= fread(data
, 1, sizeof(data
), file
);
19 output
.append(data
, read
);
22 cerr
<< "Shell::run read(): " << strerror(ferror(file
));
25 } while (!feof(file
));
27 auto error
= ferror(file
);
28 auto status
= pclose(file
);
29 return !error
&& !status
;
32 bool Shell::run(const string
&command
) {
33 auto error
= system(prepareCommand(command
).c_str());
35 perror("Shell::run system()");
41 Shell::Shell(bool redirect_stderr
)
42 : redirect
{redirect_stderr
}
44 if (!system(nullptr)) {
45 throw runtime_error("no shell available");
49 Shell::Shell(string prefix_
, bool redirect_stderr
)
50 : prefix
{move(prefix_
)}
51 , redirect
{redirect_stderr
}
53 if (!system(nullptr)) {
54 throw runtime_error("no shell available");
58 string
Shell::prepareCommand(const string
&command_
) {
60 if (prefix
.length()) {
61 command
.append(prefix
);
64 command
.append(command_
);
66 command
.append(" 2>&1");
68 INFO("Prepared command: " << command
);