Skip to content

Commit cb2cf03

Browse files
committed
libplugin: wean ld_command_handle off referncing plugin->buffer.
Hand it in as a parameter to reduce churn in next patch. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 690f95f commit cb2cf03

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

plugins/libplugin.c

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,6 +2025,7 @@ static struct command_result *param_tok(struct command *cmd, const char *name,
20252025
}
20262026

20272027
static void ld_command_handle(struct plugin *plugin,
2028+
const char *buffer,
20282029
const jsmntok_t *toks)
20292030
{
20302031
const jsmntok_t *methtok, *paramstok, *filtertok;
@@ -2033,18 +2034,18 @@ static void ld_command_handle(struct plugin *plugin,
20332034
const char *id;
20342035
enum command_type type;
20352036

2036-
methtok = json_get_member(plugin->buffer, toks, "method");
2037-
paramstok = json_get_member(plugin->buffer, toks, "params");
2038-
filtertok = json_get_member(plugin->buffer, toks, "filter");
2037+
methtok = json_get_member(buffer, toks, "method");
2038+
paramstok = json_get_member(buffer, toks, "params");
2039+
filtertok = json_get_member(buffer, toks, "filter");
20392040

20402041
if (!methtok || !paramstok)
20412042
plugin_err(plugin, "Malformed JSON-RPC notification missing "
20422043
"\"method\" or \"params\": %.*s",
20432044
json_tok_full_len(toks),
2044-
json_tok_full(plugin->buffer, toks));
2045+
json_tok_full(buffer, toks));
20452046

2046-
methodname = json_strdup(NULL, plugin->buffer, methtok);
2047-
id = json_get_id(tmpctx, plugin->buffer, toks);
2047+
methodname = json_strdup(NULL, buffer, methtok);
2048+
id = json_get_id(tmpctx, buffer, toks);
20482049

20492050
if (!id)
20502051
type = COMMAND_TYPE_NOTIFICATION;
@@ -2060,7 +2061,7 @@ static void ld_command_handle(struct plugin *plugin,
20602061

20612062
if (!plugin->manifested) {
20622063
if (streq(cmd->methodname, "getmanifest")) {
2063-
handle_getmanifest(cmd, plugin->buffer, paramstok);
2064+
handle_getmanifest(cmd, buffer, paramstok);
20642065
plugin->manifested = true;
20652066
return;
20662067
}
@@ -2070,7 +2071,7 @@ static void ld_command_handle(struct plugin *plugin,
20702071

20712072
if (!plugin->initialized) {
20722073
if (streq(cmd->methodname, "init")) {
2073-
handle_init(cmd, plugin->buffer, paramstok);
2074+
handle_init(cmd, buffer, paramstok);
20742075
plugin->initialized = true;
20752076
return;
20762077
}
@@ -2088,7 +2089,7 @@ static void ld_command_handle(struct plugin *plugin,
20882089
const char *err;
20892090

20902091
plugin->deprecated_ok_override = tal(plugin, bool);
2091-
err = json_scan(tmpctx, plugin->buffer, paramstok,
2092+
err = json_scan(tmpctx, buffer, paramstok,
20922093
"{deprecated_oneshot:{deprecated_ok:%}}",
20932094
JSON_SCAN(json_to_bool,
20942095
plugin->deprecated_ok_override));
@@ -2102,7 +2103,7 @@ static void ld_command_handle(struct plugin *plugin,
21022103
|| is_asterix_notification(cmd->methodname,
21032104
plugin->notif_subs[i].name)) {
21042105
plugin->notif_subs[i].handle(cmd,
2105-
plugin->buffer,
2106+
buffer,
21062107
paramstok);
21072108
return;
21082109
}
@@ -2114,22 +2115,22 @@ static void ld_command_handle(struct plugin *plugin,
21142115

21152116
plugin_err(plugin, "Unregistered notification %.*s",
21162117
json_tok_full_len(methtok),
2117-
json_tok_full(plugin->buffer, methtok));
2118+
json_tok_full(buffer, methtok));
21182119
}
21192120

21202121
for (size_t i = 0; i < plugin->num_hook_subs; i++) {
21212122
if (streq(cmd->methodname, plugin->hook_subs[i].name)) {
21222123
cmd->type = COMMAND_TYPE_HOOK;
21232124
plugin->hook_subs[i].handle(cmd,
2124-
plugin->buffer,
2125+
buffer,
21252126
paramstok);
21262127
return;
21272128
}
21282129
}
21292130

21302131
if (filtertok) {
21312132
/* On error, this fails cmd */
2132-
if (parse_filter(cmd, "filter", plugin->buffer, filtertok)
2133+
if (parse_filter(cmd, "filter", buffer, filtertok)
21332134
!= NULL)
21342135
return;
21352136
}
@@ -2141,17 +2142,17 @@ static void ld_command_handle(struct plugin *plugin,
21412142

21422143
/* We're going to mangle it, so make a copy */
21432144
mod_params = json_tok_copy(cmd, paramstok);
2144-
if (!param_check(cmd, plugin->buffer, mod_params,
2145+
if (!param_check(cmd, buffer, mod_params,
21452146
p_req("command_to_check", param_tok, &method),
21462147
p_opt_any(),
21472148
NULL)) {
21482149
plugin_err(plugin,
21492150
"lightningd check without command_to_check: %.*s",
21502151
json_tok_full_len(toks),
2151-
json_tok_full(plugin->buffer, toks));
2152+
json_tok_full(buffer, toks));
21522153
}
21532154
tal_free(cmd->methodname);
2154-
cmd->methodname = json_strdup(cmd, plugin->buffer, method);
2155+
cmd->methodname = json_strdup(cmd, buffer, method);
21552156

21562157
/* Point method to the name, not the value */
21572158
if (mod_params->type == JSMN_OBJECT)
@@ -2164,7 +2165,7 @@ static void ld_command_handle(struct plugin *plugin,
21642165
for (size_t i = 0; i < plugin->num_commands; i++) {
21652166
if (streq(cmd->methodname, plugin->commands[i].name)) {
21662167
plugin->commands[i].handle(cmd,
2167-
plugin->buffer,
2168+
buffer,
21682169
paramstok);
21692170
/* Reset this */
21702171
plugin->deprecated_ok_override
@@ -2181,8 +2182,8 @@ static void ld_command_handle(struct plugin *plugin,
21812182
struct command_result *ret;
21822183
bool check_only;
21832184

2184-
config = json_strdup(tmpctx, plugin->buffer,
2185-
json_get_member(plugin->buffer, paramstok, "config"));
2185+
config = json_strdup(tmpctx, buffer,
2186+
json_get_member(buffer, paramstok, "config"));
21862187
popt = find_opt(plugin, config);
21872188
if (!popt) {
21882189
plugin_err(plugin,
@@ -2198,9 +2199,9 @@ static void ld_command_handle(struct plugin *plugin,
21982199
check_only = command_check_only(cmd);
21992200
plugin_log(plugin, LOG_DBG, "setconfig %s check_only=%i", config, check_only);
22002201

2201-
valtok = json_get_member(plugin->buffer, paramstok, "val");
2202+
valtok = json_get_member(buffer, paramstok, "val");
22022203
if (valtok)
2203-
val = json_strdup(tmpctx, plugin->buffer, valtok);
2204+
val = json_strdup(tmpctx, buffer, valtok);
22042205
else
22052206
val = "true";
22062207

@@ -2252,7 +2253,7 @@ static bool ld_read_json_one(struct plugin *plugin)
22522253

22532254
/* FIXME: Spark doesn't create proper jsonrpc 2.0! So we don't
22542255
* check for "jsonrpc" here. */
2255-
ld_command_handle(plugin, plugin->toks);
2256+
ld_command_handle(plugin, plugin->buffer, plugin->toks);
22562257

22572258
/* Move this object out of the buffer */
22582259
memmove(plugin->buffer, plugin->buffer + plugin->toks[0].end,

0 commit comments

Comments
 (0)