|
|
|
@ -12,39 +12,34 @@
|
|
|
|
|
#include <univalue.h>
|
|
|
|
|
|
|
|
|
|
#ifdef ENABLE_EXTERNAL_SIGNER
|
|
|
|
|
#include <boost/process.hpp>
|
|
|
|
|
#include <util/subprocess.hpp>
|
|
|
|
|
#endif // ENABLE_EXTERNAL_SIGNER
|
|
|
|
|
|
|
|
|
|
UniValue RunCommandParseJSON(const std::string& str_command, const std::string& str_std_in)
|
|
|
|
|
{
|
|
|
|
|
#ifdef ENABLE_EXTERNAL_SIGNER
|
|
|
|
|
namespace bp = boost::process;
|
|
|
|
|
namespace sp = subprocess;
|
|
|
|
|
|
|
|
|
|
UniValue result_json;
|
|
|
|
|
bp::opstream stdin_stream;
|
|
|
|
|
bp::ipstream stdout_stream;
|
|
|
|
|
bp::ipstream stderr_stream;
|
|
|
|
|
std::istringstream stdout_stream;
|
|
|
|
|
std::istringstream stderr_stream;
|
|
|
|
|
|
|
|
|
|
if (str_command.empty()) return UniValue::VNULL;
|
|
|
|
|
|
|
|
|
|
bp::child c(
|
|
|
|
|
str_command,
|
|
|
|
|
bp::std_out > stdout_stream,
|
|
|
|
|
bp::std_err > stderr_stream,
|
|
|
|
|
bp::std_in < stdin_stream
|
|
|
|
|
);
|
|
|
|
|
auto c = sp::Popen(str_command, sp::input{sp::PIPE}, sp::output{sp::PIPE}, sp::error{sp::PIPE});
|
|
|
|
|
if (!str_std_in.empty()) {
|
|
|
|
|
stdin_stream << str_std_in << std::endl;
|
|
|
|
|
c.send(str_std_in);
|
|
|
|
|
}
|
|
|
|
|
stdin_stream.pipe().close();
|
|
|
|
|
auto [out_res, err_res] = c.communicate();
|
|
|
|
|
stdout_stream.str(std::string{out_res.buf.begin(), out_res.buf.end()});
|
|
|
|
|
stderr_stream.str(std::string{err_res.buf.begin(), err_res.buf.end()});
|
|
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
|
std::string error;
|
|
|
|
|
std::getline(stdout_stream, result);
|
|
|
|
|
std::getline(stderr_stream, error);
|
|
|
|
|
|
|
|
|
|
c.wait();
|
|
|
|
|
const int n_error = c.exit_code();
|
|
|
|
|
const int n_error = c.retcode();
|
|
|
|
|
if (n_error) throw std::runtime_error(strprintf("RunCommandParseJSON error: process(%s) returned %d: %s\n", str_command, n_error, error));
|
|
|
|
|
if (!result_json.read(result)) throw std::runtime_error("Unable to parse JSON: " + result);
|
|
|
|
|
|
|
|
|
|