Browse Source

json: add and use a json_strdup() helper.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
plugin-7
Rusty Russell 6 years ago
parent
commit
22ca896b54
  1. 5
      common/json.c
  2. 3
      common/json.h
  3. 3
      lightningd/connect_control.c
  4. 16
      lightningd/plugin.c

5
common/json.c

@ -35,6 +35,11 @@ bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str)
return strncmp(buffer + tok->start, str, tok->end - tok->start) == 0; return strncmp(buffer + tok->start, str, tok->end - tok->start) == 0;
} }
char *json_strdup(const tal_t *ctx, const char *buffer, const jsmntok_t *tok)
{
return tal_strndup(ctx, buffer + tok->start, tok->end - tok->start);
}
bool json_to_u64(const char *buffer, const jsmntok_t *tok, bool json_to_u64(const char *buffer, const jsmntok_t *tok,
uint64_t *num) uint64_t *num)
{ {

3
common/json.h

@ -23,6 +23,9 @@ int json_tok_len(const jsmntok_t *t);
/* Is this a string equal to str? */ /* Is this a string equal to str? */
bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str); bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str);
/* Allocate a tal string copy */
char *json_strdup(const tal_t *ctx, const char *buffer, const jsmntok_t *tok);
/* Extract number from this (may be a string, or a number literal) */ /* Extract number from this (may be a string, or a number literal) */
bool json_to_number(const char *buffer, const jsmntok_t *tok, bool json_to_number(const char *buffer, const jsmntok_t *tok,
unsigned int *num); unsigned int *num);

3
lightningd/connect_control.c

@ -99,8 +99,7 @@ static void json_connect(struct command *cmd,
return; return;
/* Check for id@addrport form */ /* Check for id@addrport form */
id_str = tal_strndup(cmd, buffer + idtok->start, id_str = json_strdup(cmd, buffer, idtok);
idtok->end - idtok->start);
atptr = strchr(id_str, '@'); atptr = strchr(id_str, '@');
if (atptr) { if (atptr) {
int atidx = atptr - id_str; int atidx = atptr - id_str;

16
lightningd/plugin.c

@ -551,14 +551,12 @@ static bool plugin_opt_add(struct plugin *plugin, const char *buffer,
buffer + nametok->start); buffer + nametok->start);
popt->value = NULL; popt->value = NULL;
if (defaulttok) { if (defaulttok) {
popt->value = tal_strndup(popt, buffer + defaulttok->start, popt->value = json_strdup(popt, buffer, defaulttok);
defaulttok->end - defaulttok->start);
popt->description = tal_fmt( popt->description = tal_fmt(
popt, "%.*s (default: %s)", desctok->end - desctok->start, popt, "%.*s (default: %s)", desctok->end - desctok->start,
buffer + desctok->start, popt->value); buffer + desctok->start, popt->value);
} else { } else {
popt->description = tal_strndup(popt, buffer + desctok->start, popt->description = json_strdup(popt, buffer, desctok);
desctok->end - desctok->start);
} }
list_add_tail(&plugin->plugin_opts, &popt->list); list_add_tail(&plugin->plugin_opts, &popt->list);
@ -722,14 +720,10 @@ static bool plugin_rpcmethod_add(struct plugin *plugin,
} }
cmd = notleak(tal(plugin, struct json_command)); cmd = notleak(tal(plugin, struct json_command));
cmd->name = tal_strndup(cmd, buffer + nametok->start, cmd->name = json_strdup(cmd, buffer, nametok);
nametok->end - nametok->start); cmd->description = json_strdup(cmd, buffer, desctok);
cmd->description = tal_strndup(cmd, buffer + desctok->start,
desctok->end - desctok->start);
if (longdesctok) if (longdesctok)
cmd->verbose = cmd->verbose = json_strdup(cmd, buffer, longdesctok);
tal_strndup(cmd, buffer + longdesctok->start,
longdesctok->end - longdesctok->start);
else else
cmd->verbose = cmd->description; cmd->verbose = cmd->description;

Loading…
Cancel
Save