From bd9f941a3a425388c9adbee037afe88533cd241e Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 27 Apr 2021 08:48:43 +0200 Subject: [PATCH] cli: make nmc_string_to_bool() more flexible - use strstrip() to remove leading and trailing whitespace - use _nm_utils_ascii_str_to_int64() for parsing numeric values like 0 and 1. The difference is small, for one, it also accepts hex numbers like 0x1. More interestingly, it uses our common number parsing function, and we will later do the same for parsing ternaries. --- src/libnmc-base/nm-client-utils.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/libnmc-base/nm-client-utils.c b/src/libnmc-base/nm-client-utils.c index 246d750287..c2de8ddc1f 100644 --- a/src/libnmc-base/nm-client-utils.c +++ b/src/libnmc-base/nm-client-utils.c @@ -91,12 +91,15 @@ nmc_string_to_uint(const char * str, gboolean nmc_string_to_bool(const char *str, gboolean *val_bool, GError **error) { - const char *s_true[] = {"true", "yes", "on", "1", NULL}; - const char *s_false[] = {"false", "no", "off", "0", NULL}; + gs_free char *str_to_free = NULL; + int i; - g_return_val_if_fail(error == NULL || *error == NULL, FALSE); + nm_assert(!error || !*error); + nm_assert(val_bool); - if (g_strcmp0(str, "o") == 0) { + str = nm_strstrip_avoid_copy_a(300, str, &str_to_free); + + if (nm_streq0(str, "o")) { nm_utils_error_set(error, NM_UTILS_ERROR_UNKNOWN, /* TRANSLATORS: the first %s is the partial value entered by @@ -108,11 +111,13 @@ nmc_string_to_bool(const char *str, gboolean *val_bool, GError **error) return FALSE; } - if (nmc_string_is_valid(str, s_true, NULL)) + if (nmc_string_is_valid(str, NM_MAKE_STRV("true", "yes", "on"), NULL)) *val_bool = TRUE; - else if (nmc_string_is_valid(str, s_false, NULL)) + else if (nmc_string_is_valid(str, NM_MAKE_STRV("false", "no", "off"), NULL)) *val_bool = FALSE; - else { + else if ((i = _nm_utils_ascii_str_to_int64(str, 0, 0, 1, -1)) >= 0) { + *val_bool = !!i; + } else { nm_utils_error_set(error, NM_UTILS_ERROR_UNKNOWN, _("'%s' is not valid; use [%s] or [%s]"),