13 #ifndef __STOUT_OS_POSIX_SHELL_HPP__
14 #define __STOUT_OS_POSIX_SHELL_HPP__
24 #include <glog/logging.h>
41 constexpr
const char*
name =
"sh";
42 constexpr
const char*
arg0 =
"sh";
43 constexpr
const char*
arg1 =
"-c";
70 template <
typename... T>
79 std::ostringstream stdout;
81 if ((file = popen(command.
get().c_str(),
"r")) ==
nullptr) {
82 return Error(
"Failed to run '" + command.
get() +
"'");
88 while (fgets(line,
sizeof(line), file) !=
nullptr) {
92 if (ferror(file) != 0) {
94 return Error(
"Error reading output of '" + command.
get() +
"'");
98 if ((status = pclose(file)) == -1) {
99 return Error(
"Failed to get status of '" + command.
get() +
"'");
104 "Running '" + command.
get() +
"' was interrupted by signal '" +
106 }
else if ((
WEXITSTATUS(status) != EXIT_SUCCESS)) {
107 LOG(ERROR) <<
"Command '" << command.
get()
108 <<
"' failed; this is the output:\n" << stdout.str();
110 "Failed to execute '" + command.
get() +
"'; the command was either "
111 "not found or exited with a non-zero exit status: " +
130 inline int system(
const std::string& command)
132 pid_t pid = ::fork();
136 }
else if (pid == 0) {
144 while (::
waitpid(pid, &status, 0) == -1) {
145 if (errno != EINTR) {
160 const std::string& command,
161 const std::vector<std::string>& arguments)
163 pid_t pid = ::fork();
167 }
else if (pid == 0) {
174 while (::
waitpid(pid, &status, 0) == -1) {
175 if (errno != EINTR) {
185 template<
typename... T>
199 #endif // __STOUT_OS_POSIX_SHELL_HPP__
constexpr const char * arg1
Definition: shell.hpp:43
Definition: errorbase.hpp:35
int spawn(const std::string &command, const std::vector< std::string > &arguments)
Definition: shell.hpp:159
int execlp(const char *file, T...t)
Definition: shell.hpp:186
const char * strsignal(int signum)
Definition: windows.hpp:358
Result< ProcessStatus > status(pid_t pid)
Definition: proc.hpp:166
constexpr const char * arg0
Definition: shell.hpp:42
int execvp(const char *file, char *const argv[])
Definition: shell.hpp:192
Represent the argument list expected by execv routines.
Definition: argv.hpp:36
DWORD pid_t
Definition: windows.hpp:187
URI file(const std::string &path)
Creates a file URI with the given path on the local host.
Definition: file.hpp:33
#define WIFSIGNALED(x)
Definition: windows.hpp:380
Result< pid_t > waitpid(pid_t pid, int *status, int options)
Definition: os.hpp:141
#define WEXITSTATUS(x)
Definition: windows.hpp:376
static Try error(const E &e)
Definition: try.hpp:42
Try< std::string > shell(const std::string &fmt, const T &...t)
Runs a shell command with optional arguments.
Definition: shell.hpp:71
bool isError() const
Definition: try.hpp:71
#define WTERMSIG(x)
Definition: windows.hpp:386
Try< std::string > format(const std::string &fmt, va_list args)
Definition: format.hpp:68
std::string stringify(int flags)
const T & get() const
Definition: try.hpp:73
constexpr const char * name
Definition: shell.hpp:41
int system(const std::string &command)
Definition: shell.hpp:130