| 447 | } |
| 448 | |
| 449 | static int CommandLineRPC(int argc, char *argv[]) |
| 450 | { |
| 451 | std::string strPrint; |
| 452 | int nRet = 0; |
| 453 | try { |
| 454 | // Skip switches |
| 455 | while (argc > 1 && IsSwitchChar(argv[1][0])) { |
| 456 | argc--; |
| 457 | argv++; |
| 458 | } |
| 459 | std::string rpcPass; |
| 460 | if (gArgs.GetBoolArg("-stdinrpcpass", false)) { |
| 461 | if (!std::getline(std::cin, rpcPass)) { |
| 462 | throw std::runtime_error("-stdinrpcpass specified but failed to read from standard input"); |
| 463 | } |
| 464 | gArgs.ForceSetArg("-rpcpassword", rpcPass); |
| 465 | } |
| 466 | std::vector<std::string> args = std::vector<std::string>(&argv[1], &argv[argc]); |
| 467 | if (gArgs.GetBoolArg("-stdin", false)) { |
| 468 | // Read one arg per line from stdin and append |
| 469 | std::string line; |
| 470 | while (std::getline(std::cin, line)) { |
| 471 | args.push_back(line); |
| 472 | } |
| 473 | } |
| 474 | std::unique_ptr<BaseRequestHandler> rh; |
| 475 | std::string method; |
| 476 | if (gArgs.GetBoolArg("-getinfo", false)) { |
| 477 | rh.reset(new GetinfoRequestHandler()); |
| 478 | method = ""; |
| 479 | } else { |
| 480 | rh.reset(new DefaultRequestHandler()); |
| 481 | if (args.size() < 1) { |
| 482 | throw std::runtime_error("too few parameters (need at least command)"); |
| 483 | } |
| 484 | method = args[0]; |
| 485 | args.erase(args.begin()); // Remove trailing method name from arguments vector |
| 486 | } |
| 487 | |
| 488 | // Execute and handle connection failures with -rpcwait |
| 489 | const bool fWait = gArgs.GetBoolArg("-rpcwait", false); |
| 490 | do { |
| 491 | try { |
| 492 | const UniValue reply = CallRPC(rh.get(), method, args); |
| 493 | |
| 494 | // Parse reply |
| 495 | const UniValue& result = find_value(reply, "result"); |
| 496 | const UniValue& error = find_value(reply, "error"); |
| 497 | |
| 498 | if (!error.isNull()) { |
| 499 | // Error |
| 500 | int code = error["code"].get_int(); |
| 501 | if (fWait && code == RPC_IN_WARMUP) |
| 502 | throw CConnectionFailed("server in warmup"); |
| 503 | strPrint = "error: " + error.write(); |
| 504 | nRet = abs(code); |
| 505 | if (error.isObject()) |
| 506 | { |
no test coverage detected