From: Lennart Poettering Date: Mon, 27 May 2024 09:37:33 +0000 (+0200) Subject: varlinkctl: as convencience to users, accept empty string in place of {} for empty... X-Git-Tag: v257-rc1~1166^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F33046%2Fhead;p=thirdparty%2Fsystemd.git varlinkctl: as convencience to users, accept empty string in place of {} for empty parameter list It makes it a bit easier to use "varlinkctl call", since you can just hit ^D to call a function without params, instead of spelling out {}. --- diff --git a/src/varlinkctl/varlinkctl.c b/src/varlinkctl/varlinkctl.c index a994c640ca0..fb2c3876a63 100644 --- a/src/varlinkctl/varlinkctl.c +++ b/src/varlinkctl/varlinkctl.c @@ -451,7 +451,7 @@ static int reply_callback( static int verb_call(int argc, char *argv[], void *userdata) { _cleanup_(sd_json_variant_unrefp) sd_json_variant *jp = NULL; _cleanup_(varlink_unrefp) Varlink *vl = NULL; - const char *url, *method, *parameter; + const char *url, *method, *parameter, *source; unsigned line = 0, column = 0; int r; @@ -470,18 +470,25 @@ static int verb_call(int argc, char *argv[], void *userdata) { arg_json_format_flags |= SD_JSON_FORMAT_NEWLINE; if (parameter) { + source = ""; + /* is correct, as dispatch_verb() shifts arguments by one for the verb. */ - r = sd_json_parse_with_source(parameter, "", 0, &jp, &line, &column); - if (r < 0) - return log_error_errno(r, "Failed to parse parameters at :%u:%u: %m", line, column); + r = sd_json_parse_with_source(parameter, source, 0, &jp, &line, &column); } else { if (isatty(STDIN_FILENO) > 0 && !arg_quiet) - log_notice("Expecting method call parameter JSON object on standard input."); + log_notice("Expecting method call parameter JSON object on standard input. (Provide empty string or {} for no parameters.)"); - r = sd_json_parse_file_at(stdin, AT_FDCWD, "", 0, &jp, &line, &column); - if (r < 0) - return log_error_errno(r, "Failed to parse parameters at :%u:%u: %m", line, column); + source = ""; + + r = sd_json_parse_file_at(stdin, AT_FDCWD, source, 0, &jp, &line, &column); } + if (r < 0 && r != -ENODATA) + return log_error_errno(r, "Failed to parse parameters at %s:%u:%u: %m", source, line, column); + + /* If parsing resulted in ENODATA the provided string was empty. As convenience to users we'll accept + * that and treat it as equivalent to an empty object: as a call with empty set of parameters. This + * mirrors how we do this in our C APIs too, where we are happy to accept NULL instead of a proper + * JsonVariant object for method calls. */ r = varlink_connect_auto(&vl, url); if (r < 0)