Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2022-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <common/run_command.h>7#include <tinyformat.h>8#include <univalue.h>9#include <util/string.h>10#include <util/subprocess.h>12#include <sstream>13#include <stdexcept>14#include <utility>16UniValue RunCommandParseJSON(const std::vector<std::string>& cmd_args, const std::string& str_std_in)17{18namespace sp = subprocess;20UniValue result_json;21std::istringstream stdout_stream;22std::istringstream stderr_stream;24if (cmd_args.empty()) return UniValue::VNULL;26auto c = sp::Popen(cmd_args, sp::input{sp::PIPE}, sp::output{sp::PIPE}, sp::error{sp::PIPE});27if (!str_std_in.empty()) {28c.send(str_std_in);29}30auto [out_res, err_res] = c.communicate();31stdout_stream.str(std::string{out_res.buf.begin(), out_res.buf.end()});32stderr_stream.str(std::string{err_res.buf.begin(), err_res.buf.end()});34std::string result;35std::string error;36std::getline(stdout_stream, result);37std::getline(stderr_stream, error);39const int n_error = c.retcode();40if (n_error) throw std::runtime_error(strprintf("RunCommandParseJSON error: process(%s) returned %d: %s\n", util::Join(cmd_args, " "), n_error, error));41if (!result_json.read(result)) throw std::runtime_error("Unable to parse JSON: " + result);43return result_json;44}