From c5a247c4c081836f5995674a20a55f3266cabd2b Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 1 Mar 2019 08:55:30 +0100 Subject: cli: add nmc_complete_strv() which takes a string array for completion that may contain NULL This will allow for a convenient calling pattern when some elements should be printed optionally. (cherry picked from commit 62b939de4e2288d0a9d305706db4cf47c34122d5) --- clients/cli/common.c | 34 +++++++++++++++++++++++++--------- clients/cli/common.h | 4 +++- 2 files changed, 28 insertions(+), 10 deletions(-) (limited to 'clients/cli') diff --git a/clients/cli/common.c b/clients/cli/common.c index 50dd0eb5cb..3c1c315d5e 100644 --- a/clients/cli/common.c +++ b/clients/cli/common.c @@ -1349,23 +1349,39 @@ nmc_do_cmd (NmCli *nmc, const NMCCommand cmds[], const char *cmd, int argc, char /** * nmc_complete_strings: * @prefix: a string to match - * @...: a %NULL-terminated list of candidate strings + * @nargs: the number of elements in @args. Or -1 if @args is a NULL terminated + * strv array. + * @args: the argument list. If @nargs is not -1, then some elements may + * be %NULL to indicate to silently skip the values. * * Prints all the matching candidates for completion. Useful when there's * no better way to suggest completion other than a hardcoded string list. */ void -nmc_complete_strings (const char *prefix, ...) +nmc_complete_strv (const char *prefix, gssize nargs, const char *const*args) { - va_list args; - const char *candidate; + gsize i, n; + + if (prefix && !prefix[0]) + prefix = NULL; + + if (nargs < 0) { + nm_assert (nargs == -1); + n = NM_PTRARRAY_LEN (args); + } else + n = (gsize) nargs; + + for (i = 0; i < n; i++) { + const char *candidate = args[i]; - va_start (args, prefix); - while ((candidate = va_arg (args, const char *))) { - if (!*prefix || matches (prefix, candidate)) - g_print ("%s\n", candidate); + if (!candidate) + continue; + if ( prefix + && !matches (prefix, candidate)) + continue; + + g_print ("%s\n", candidate); } - va_end (args); } /** diff --git a/clients/cli/common.h b/clients/cli/common.h index 71734acc98..688f2819f6 100644 --- a/clients/cli/common.h +++ b/clients/cli/common.h @@ -88,7 +88,9 @@ typedef struct { void nmc_do_cmd (NmCli *nmc, const NMCCommand cmds[], const char *cmd, int argc, char **argv); -void nmc_complete_strings (const char *prefix, ...) G_GNUC_NULL_TERMINATED; +void nmc_complete_strv (const char *prefix, gssize nargs, const char *const*args); + +#define nmc_complete_strings(prefix, ...) nmc_complete_strv ((prefix), NM_NARG (__VA_ARGS__), (const char *const[]) { __VA_ARGS__ }) void nmc_complete_bool (const char *prefix); -- cgit v1.2.1