40 std::string executableCommand = command.toStdString() +
"\n";
43 if(executableCommand ==
"help-console\n")
46 "This console accepts RPC commands using the standard syntax.\n"
47 " example: getblockhash 0\n\n"
49 "This console can also accept RPC commands using parenthesized syntax.\n"
50 " example: getblockhash(0)\n\n"
52 "Commands may be nested when specified with the parenthesized syntax.\n"
53 " example: getblock(getblockhash(0) true)\n\n"
55 "A space or a comma can be used to delimit arguments for either syntax.\n"
56 " example: getblockhash 0\n"
59 "Named results can be queried with a non-quoted key string in brackets.\n"
60 " example: getblock(getblockhash(0) true)[tx]\n\n"
62 "Results without keys can be queried using an integer in brackets.\n"
63 " example: getblock(getblockhash(0),true)[tx][0]\n\n")));
68 Q_EMIT
reply(
CMD_ERROR, QString(
"Parse error: unbalanced ' or \""));
79 Q_EMIT
reply(
CMD_ERROR, QString::fromStdString(message) +
" (code " + QString::number(code) +
")");
81 }
catch (
const std::runtime_error&) {
86 }
catch (
const std::exception& e) {
87 Q_EMIT
reply(
CMD_ERROR, QString(
"Error: ") + QString::fromStdString(e.what()));
109 std::vector< std::vector<std::string> > stack;
110 stack.push_back(std::vector<std::string>());
115 STATE_EATING_SPACES_IN_ARG,
116 STATE_EATING_SPACES_IN_BRACKETS,
121 STATE_ESCAPE_DOUBLEQUOTED,
122 STATE_COMMAND_EXECUTED,
123 STATE_COMMAND_EXECUTED_INNER
124 } state = STATE_EATING_SPACES;
128 std::string strCommandTerminated = strCommand;
129 if (strCommandTerminated.back() !=
'\n')
130 strCommandTerminated +=
"\n";
131 for(
char ch: strCommandTerminated)
135 case STATE_COMMAND_EXECUTED_INNER:
136 case STATE_COMMAND_EXECUTED:
138 bool breakParsing =
true;
141 case '[': curarg.
clear(); state = STATE_COMMAND_EXECUTED_INNER;
break;
143 if (state == STATE_COMMAND_EXECUTED_INNER)
157 for(
char argch: curarg)
158 if (!std::isdigit(argch))
159 throw std::runtime_error(
"Invalid result query");
160 subelement = lastResult[
atoi(curarg.c_str())];
165 throw std::runtime_error(
"Invalid result query");
166 lastResult = subelement;
169 state = STATE_COMMAND_EXECUTED;
173 breakParsing =
false;
179 if (lastResult.
isStr())
182 curarg = lastResult.
write(2);
188 stack.back().push_back(curarg);
194 state = STATE_EATING_SPACES;
200 case STATE_EATING_SPACES_IN_ARG:
201 case STATE_EATING_SPACES_IN_BRACKETS:
202 case STATE_EATING_SPACES:
205 case '"': state = STATE_DOUBLEQUOTED;
break;
206 case '\'': state = STATE_SINGLEQUOTED;
break;
207 case '\\': state = STATE_ESCAPE_OUTER;
break;
208 case '(':
case ')':
case '\n':
209 if (state == STATE_EATING_SPACES_IN_ARG)
210 throw std::runtime_error(
"Invalid Syntax");
211 if (state == STATE_ARGUMENT)
213 if (ch ==
'(' && stack.size() && stack.back().size() > 0)
214 stack.push_back(std::vector<std::string>());
218 throw std::runtime_error(
"Invalid Syntax");
220 stack.back().push_back(curarg);
222 state = STATE_EATING_SPACES_IN_BRACKETS;
224 if ((ch ==
')' || ch ==
'\n') && stack.size() > 0)
226 std::string strPrint;
230 req.
params =
RPCConvertValues(stack.back()[0], std::vector<std::string>(stack.back().begin() + 1, stack.back().end()));
236 QByteArray encodedName = QUrl::toPercentEncoding(QString::fromStdString(
vpwallets[0]->GetName()));
237 req.
URI =
"/wallet/"+std::string(encodedName.constData(), encodedName.length());
241 state = STATE_COMMAND_EXECUTED;
245 case ' ':
case ',':
case '\t':
246 if(state == STATE_EATING_SPACES_IN_ARG && curarg.empty() && ch ==
',')
247 throw std::runtime_error(
"Invalid Syntax");
249 else if(state == STATE_ARGUMENT)
251 stack.back().push_back(curarg);
254 if ((state == STATE_EATING_SPACES_IN_BRACKETS || state == STATE_ARGUMENT) && ch ==
',')
256 state = STATE_EATING_SPACES_IN_ARG;
259 state = STATE_EATING_SPACES;
261 default: curarg += ch; state = STATE_ARGUMENT;
264 case STATE_SINGLEQUOTED:
267 case '\'': state = STATE_ARGUMENT;
break;
268 default: curarg += ch;
271 case STATE_DOUBLEQUOTED:
274 case '"': state = STATE_ARGUMENT;
break;
275 case '\\': state = STATE_ESCAPE_DOUBLEQUOTED;
break;
276 default: curarg += ch;
279 case STATE_ESCAPE_OUTER:
280 curarg += ch; state = STATE_ARGUMENT;
282 case STATE_ESCAPE_DOUBLEQUOTED:
283 if(ch !=
'"' && ch !=
'\\') curarg +=
'\\';
284 curarg += ch; state = STATE_DOUBLEQUOTED;
290 case STATE_COMMAND_EXECUTED:
291 if (lastResult.
isStr())
292 strResult = lastResult.
get_str();
294 strResult = lastResult.
write(2);
296 case STATE_EATING_SPACES:
UniValue execute(const JSONRPCRequest &request) const
Execute a method.
void reply(int category, const QString &command)
void request(const QString &command)
static QString categoryClass(int category)
bool ExecuteCommandLine(std::string &strResult, const std::string &strCommand)
Split shell command line into a list of arguments and execute the command(s).
const std::string & get_str() const
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
UniValue RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert positional arguments to command-specific RPC representation.
const UniValue & find_value(const UniValue &obj, const std::string &name)
int atoi(const std::string &str)
std::vector< CWalletRef > vpwallets