From 12731c4a6086f0939a0d2536cb361ad3d295cfaf Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sun, 16 Dec 2018 15:18:06 +1030 Subject: [PATCH] json_tok_len, json_tok_contents: rename to json_tok_full_len and json_tok_full These are only supposed to be used when you want the token contents including surrounding "". We should use this when reporting errors, but usually we just want to access the tok members directly. Signed-off-by: Rusty Russell --- cli/lightning-cli.c | 8 ++++---- common/json.c | 4 ++-- common/json.h | 4 ++-- lightningd/bitcoind.c | 4 ++-- lightningd/connect_control.c | 4 ++-- lightningd/gossip_control.c | 4 ++-- lightningd/json.c | 6 +++--- lightningd/jsonrpc.c | 12 ++++++------ lightningd/log.c | 3 ++- lightningd/options.c | 4 ++-- lightningd/peer_control.c | 3 +-- lightningd/plugin.c | 8 ++++---- 12 files changed, 32 insertions(+), 32 deletions(-) diff --git a/cli/lightning-cli.c b/cli/lightning-cli.c index 9a608337b..209b93216 100644 --- a/cli/lightning-cli.c +++ b/cli/lightning-cli.c @@ -363,7 +363,7 @@ int main(int argc, char *argv[]) if (!json_tok_streq(resp, id, idstr)) errx(ERROR_TALKING_TO_LIGHTNINGD, "Incorrect 'id' in response: %.*s", - json_tok_len(id), json_tok_contents(resp, id)); + json_tok_full_len(id), json_tok_full(resp, id)); if (!error || json_tok_is_null(resp, error)) { // if we have specific help command @@ -374,8 +374,8 @@ int main(int argc, char *argv[]) human_readable(resp, result, '\n'); else printf("%.*s\n", - json_tok_len(result), - json_tok_contents(resp, result)); + json_tok_full_len(result), + json_tok_full(resp, result)); tal_free(lightning_dir); tal_free(rpc_filename); tal_free(ctx); @@ -384,7 +384,7 @@ int main(int argc, char *argv[]) } printf("%.*s\n", - json_tok_len(error), json_tok_contents(resp, error)); + json_tok_full_len(error), json_tok_full(resp, error)); tal_free(lightning_dir); tal_free(rpc_filename); tal_free(ctx); diff --git a/common/json.c b/common/json.c index 89a738446..0e9d0f3c2 100644 --- a/common/json.c +++ b/common/json.c @@ -12,7 +12,7 @@ #include #include -const char *json_tok_contents(const char *buffer, const jsmntok_t *t) +const char *json_tok_full(const char *buffer, const jsmntok_t *t) { if (t->type == JSMN_STRING) return buffer + t->start - 1; @@ -20,7 +20,7 @@ const char *json_tok_contents(const char *buffer, const jsmntok_t *t) } /* Include " if it's a string. */ -int json_tok_len(const jsmntok_t *t) +int json_tok_full_len(const jsmntok_t *t) { if (t->type == JSMN_STRING) return t->end - t->start + 2; diff --git a/common/json.h b/common/json.h index a80d87858..954eebf72 100644 --- a/common/json.h +++ b/common/json.h @@ -15,10 +15,10 @@ struct json_escaped; struct short_channel_id; /* Include " if it's a string. */ -const char *json_tok_contents(const char *buffer, const jsmntok_t *t); +const char *json_tok_full(const char *buffer, const jsmntok_t *t); /* Include " if it's a string. */ -int json_tok_len(const jsmntok_t *t); +int json_tok_full_len(const jsmntok_t *t); /* Is this a string equal to str? */ bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str); diff --git a/lightningd/bitcoind.c b/lightningd/bitcoind.c index e3fedec4f..da0bbca87 100644 --- a/lightningd/bitcoind.c +++ b/lightningd/bitcoind.c @@ -646,8 +646,8 @@ static bool process_getblock(struct bitcoin_cli *bcli) &txid)) fatal("%s: had bad txid (%.*s)?", bcli_args(tmpctx, bcli), - txidtok->end - txidtok->start, - bcli->output + txidtok->start); + json_tok_full_len(txidtok), + json_tok_full(bcli->output, txidtok)); go->cb = cb; /* Now get the raw tx output. */ diff --git a/lightningd/connect_control.c b/lightningd/connect_control.c index 127d88e34..a15c29d80 100644 --- a/lightningd/connect_control.c +++ b/lightningd/connect_control.c @@ -111,8 +111,8 @@ static void json_connect(struct command *cmd, if (!json_to_pubkey(buffer, idtok, &id)) { command_fail(cmd, JSONRPC2_INVALID_PARAMS, "id %.*s not valid", - idtok->end - idtok->start, - buffer + idtok->start); + json_tok_full_len(idtok), + json_tok_full(buffer, idtok)); return; } diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 8c3f2538a..6b6c6bc52 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -474,8 +474,8 @@ static void json_dev_query_scids(struct command *cmd, if (!json_to_short_channel_id(buffer, t, &scids[i])) { command_fail(cmd, JSONRPC2_INVALID_PARAMS, "scid %zu '%.*s' is not an scid", - i, t->end - t->start, - buffer + t->start); + i, json_tok_full_len(t), + json_tok_full(buffer, t)); return; } } diff --git a/lightningd/json.c b/lightningd/json.c index e94be2c34..af97c2d20 100644 --- a/lightningd/json.c +++ b/lightningd/json.c @@ -114,7 +114,7 @@ bool json_tok_pubkey(struct command *cmd, const char *name, command_fail(cmd, JSONRPC2_INVALID_PARAMS, "'%s' should be a pubkey, not '%.*s'", - name, tok->end - tok->start, buffer + tok->start); + name, json_tok_full_len(tok), json_tok_full(buffer, tok)); return false; } @@ -143,7 +143,7 @@ bool json_tok_short_channel_id(struct command *cmd, const char *name, command_fail(cmd, JSONRPC2_INVALID_PARAMS, "'%s' should be a short channel id, not '%.*s'", - name, tok->end - tok->start, buffer + tok->start); + name, json_tok_full_len(tok), json_tok_full(buffer, tok)); return false; } @@ -178,7 +178,7 @@ bool json_tok_feerate_style(struct command *cmd, const char *name, name, json_feerate_style_name(FEERATE_PER_KSIPA), json_feerate_style_name(FEERATE_PER_KBYTE), - tok->end - tok->start, buffer + tok->start); + json_tok_full_len(tok), json_tok_full(buffer, tok)); return false; } diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index 123badf3c..ed6ea02f5 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -332,8 +332,8 @@ static void json_help(struct command *cmd, } command_fail(cmd, JSONRPC2_METHOD_NOT_FOUND, "Unknown command '%.*s'", - cmdtok->end - cmdtok->start, - buffer + cmdtok->start); + json_tok_full_len(cmdtok), + json_tok_full(buffer, cmdtok)); return; } @@ -539,8 +539,8 @@ static void parse_request(struct json_connection *jcon, const jsmntok_t tok[]) c->pending = false; c->have_json_stream = false; c->id = tal_strndup(c, - json_tok_contents(jcon->buffer, id), - json_tok_len(id)); + json_tok_full(jcon->buffer, id), + json_tok_full_len(id)); c->mode = CMD_NORMAL; list_add_tail(&jcon->commands, &c->list); tal_add_destructor(c, destroy_command); @@ -561,8 +561,8 @@ static void parse_request(struct json_connection *jcon, const jsmntok_t tok[]) if (!c->json_cmd) { command_fail(c, JSONRPC2_METHOD_NOT_FOUND, "Unknown command '%.*s'", - method->end - method->start, - jcon->buffer + method->start); + json_tok_full_len(method), + json_tok_full(jcon->buffer, method)); return; } if (c->json_cmd->deprecated && !deprecated_apis) { diff --git a/lightningd/log.c b/lightningd/log.c index 9b672e48e..57de95bcd 100644 --- a/lightningd/log.c +++ b/lightningd/log.c @@ -719,7 +719,8 @@ bool json_tok_loglevel(struct command *cmd, const char *name, command_fail(cmd, JSONRPC2_INVALID_PARAMS, "'%s' should be 'io', 'debug', 'info', or " "'unusual', not '%.*s'", - name, tok->end - tok->start, buffer + tok->start); + name, + json_tok_full_len(tok), json_tok_full(buffer, tok)); return false; } return true; diff --git a/lightningd/options.c b/lightningd/options.c index d156134e9..95d4620a1 100644 --- a/lightningd/options.c +++ b/lightningd/options.c @@ -1099,8 +1099,8 @@ static void json_listconfigs(struct command *cmd, if (configtok && !response) { command_fail(cmd, JSONRPC2_INVALID_PARAMS, "Unknown config option '%.*s'", - configtok->end - configtok->start, - buffer + configtok->start); + json_tok_full_len(configtok), + json_tok_full(buffer, configtok)); return; } json_object_end(response); diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index d210f618a..6bc8e632e 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -886,8 +886,7 @@ command_find_channel(struct command *cmd, command_fail(cmd, JSONRPC2_INVALID_PARAMS, "Given id is not a channel ID or " "short channel ID: '%.*s'", - tok->end - tok->start, - buffer + tok->start); + json_tok_full_len(tok), json_tok_full(buffer, tok)); return NULL; } } diff --git a/lightningd/plugin.c b/lightningd/plugin.c index 86d80c4fa..614c4e1b8 100644 --- a/lightningd/plugin.c +++ b/lightningd/plugin.c @@ -259,8 +259,8 @@ static void plugin_log_handle(struct plugin *plugin, const jsmntok_t *paramstok) plugin_kill(plugin, "Unknown log-level %.*s, valid values are " "\"debug\", \"info\", \"warn\", or \"error\".", - json_tok_len(leveltok), - json_tok_contents(plugin->buffer, leveltok)); + json_tok_full_len(leveltok), + json_tok_full(plugin->buffer, leveltok)); return; } @@ -293,8 +293,8 @@ static void plugin_notification_handle(struct plugin *plugin, plugin_log_handle(plugin, paramstok); } else { plugin_kill(plugin, "Unknown notification method %.*s", - json_tok_len(methtok), - json_tok_contents(plugin->buffer, methtok)); + json_tok_full_len(methtok), + json_tok_full(plugin->buffer, methtok)); } }