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
|
// SPDX-License-Identifier: LGPL-3.0-or-later
// Author: Kristian Lytje
#include <shell/Command.h>
#include <cstdio>
#include <stdexcept>
#include <string>
#include <array>
using namespace ausaxs::shell;
Command::Command() noexcept = default;
Command::Command(const std::string& cmd) : cmd(cmd) {}
void Command::set(const std::string& cmd) {
this->cmd = cmd;
}
std::string Command::get() const {
return cmd;
}
Command& Command::append(const shell::Argument& arg) {
cmd += " " + arg.get();
return *this;
}
Command& Command::append(const shell::Flag& flag) {
cmd += " " + flag.get();
return *this;
}
Command& Command::append(const std::string& arg) {
cmd += " " + arg;
return *this;
}
Command& Command::prepend(const std::string& arg) {
cmd = arg + " " + cmd;
return *this;
}
Command& Command::mute() {
#ifdef __linux__
cmd += " 2>/dev/null";
#elif defined (_WIN32)
cmd += " 2>NUL";
#endif
return *this;
}
#ifdef _WIN32
#define popen _popen
#define pclose _pclose
#endif
CommandResult Command::execute() const {
std::array<char, 128> buffer;
std::string result;
FILE* pipe = popen(cmd.data(), "r");
if (pipe == nullptr) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) {
result += buffer.data();
}
int exit_code = pclose(pipe);
return {std::move(result), exit_code};
}
|