diff options
author | Thomas Haller <thaller@redhat.com> | 2021-04-27 08:48:43 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2021-05-03 10:11:21 +0200 |
commit | 7df7d46bfed9b8689297bec4721f1a7c5d5f1ff5 (patch) | |
tree | 00ff58b784a15ba6f51317aefcab3d8628cc720b | |
parent | bd9f941a3a425388c9adbee037afe88533cd241e (diff) | |
download | NetworkManager-7df7d46bfed9b8689297bec4721f1a7c5d5f1ff5.tar.gz |
cli: make nmc_string_to_ternary() more flexible
- use strstrip() to remove leading and trailing whitespace
- use _nm_utils_ascii_str_to_int64() for parsing numeric values
like -1, 0 and 1. In particular, this now also allows passing
the numeric values.
- also accept "default" as valid value for NM_TERNARY_DEFAULT.
With this change, nmc_string_to_ternary() can also parse everything that
we commonly and currently parse with _nm_utils_enum_from_str_full()
and NM_TYPE_TERNARY. This will allow to configure ternary values in
a more flexible way.
-rw-r--r-- | src/libnmc-base/nm-client-utils.c | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/src/libnmc-base/nm-client-utils.c b/src/libnmc-base/nm-client-utils.c index c2de8ddc1f..4737adb921 100644 --- a/src/libnmc-base/nm-client-utils.c +++ b/src/libnmc-base/nm-client-utils.c @@ -132,13 +132,15 @@ nmc_string_to_bool(const char *str, gboolean *val_bool, GError **error) gboolean nmc_string_to_ternary(const char *str, NMTernary *val, GError **error) { - const char *s_true[] = {"true", "yes", "on", NULL}; - const char *s_false[] = {"false", "no", "off", NULL}; - const char *s_unknown[] = {"unknown", NULL}; + gs_free char *str_to_free = NULL; + int i; + + nm_assert(!error || !*error); + nm_assert(val); - g_return_val_if_fail(error == NULL || *error == NULL, FALSE); + str = nm_strstrip_avoid_copy_a(300, str, &str_to_free); - if (g_strcmp0(str, "o") == 0) { + if (nm_streq0(str, "o")) { nm_utils_error_set(error, NM_UTILS_ERROR_UNKNOWN, /* TRANSLATORS: the first %s is the partial value entered by @@ -150,12 +152,14 @@ nmc_string_to_ternary(const char *str, NMTernary *val, 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 = NM_TERNARY_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 = NM_TERNARY_FALSE; - else if (nmc_string_is_valid(str, s_unknown, NULL)) + else if (nmc_string_is_valid(str, NM_MAKE_STRV("unknown", "default"), NULL)) *val = NM_TERNARY_DEFAULT; + else if ((i = _nm_utils_ascii_str_to_int64(str, 0, -1, 1, -2)) >= -1) + *val = (NMTernary) i; else { nm_utils_error_set(error, NM_UTILS_ERROR_UNKNOWN, |