Skip to content

Commit

Permalink
json_tok_len, json_tok_contents: rename to json_tok_full_len and json…
Browse files Browse the repository at this point in the history
…_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 <[email protected]>
  • Loading branch information
rustyrussell committed Dec 20, 2018
1 parent 465d5d5 commit 12731c4
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 32 deletions.
8 changes: 4 additions & 4 deletions cli/lightning-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions common/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
#include <stdio.h>
#include <string.h>

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;
return buffer + t->start;
}

/* 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;
Expand Down
4 changes: 2 additions & 2 deletions common/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions lightningd/bitcoind.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
4 changes: 2 additions & 2 deletions lightningd/connect_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions lightningd/gossip_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
6 changes: 3 additions & 3 deletions lightningd/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
12 changes: 6 additions & 6 deletions lightningd/jsonrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion lightningd/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions lightningd/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
8 changes: 4 additions & 4 deletions lightningd/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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));
}
}

Expand Down

0 comments on commit 12731c4

Please sign in to comment.