param: add ok flag to struct command

Callers to param() can now optionally set a flag to see if command_fail was
called.

This is necessary because the `cmd` is freed in case of failure.

I spent a bit of time trying to extend the lifetime of the `cmd` to the end
of parse_request(), but the destructors still needed to be called when they
were, and it was getting ugly.  So I took this minimal approach.

Signed-off-by: Mark Beckwith <wythe@intrig.com>
This commit is contained in:
Mark Beckwith
2018-09-15 12:12:15 -05:00
committed by Christian Decker
parent 30b67c0334
commit 1a4f355a7b
2 changed files with 7 additions and 0 deletions

View File

@@ -282,6 +282,8 @@ static void connection_complete_ok(struct json_connection *jcon,
{
assert(id != NULL);
assert(result != NULL);
if (cmd->ok)
*(cmd->ok) = true;
/* This JSON is simple enough that we build manually */
json_done(jcon, cmd, take(tal_fmt(jcon,
@@ -310,6 +312,9 @@ static void connection_complete_error(struct json_connection *jcon,
assert(id != NULL);
if (cmd->ok)
*(cmd->ok) = false;
json_done(jcon, cmd, take(tal_fmt(tmpctx,
"{ \"jsonrpc\": \"2.0\", "
" \"error\" : "
@@ -452,6 +457,7 @@ static void parse_request(struct json_connection *jcon, const jsmntok_t tok[])
json_tok_contents(jcon->buffer, id),
json_tok_len(id));
c->mode = CMD_NORMAL;
c->ok = NULL;
list_add(&jcon->commands, &c->list);
tal_add_destructor(c, destroy_cmd);

View File

@@ -37,6 +37,7 @@ struct command {
enum command_mode mode;
/* This is created if mode is CMD_USAGE */
const char *usage;
bool *ok;
};
struct json_connection {