From 037b14965e2953bd73394c762e184617ca834206 Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Wed, 12 Mar 2025 15:17:06 +0100 Subject: [PATCH] libnmc-setting: add new flag for property descriptors Add a new flag "print_hex_negative_as_base10" in the property descriptor _NMMetaPropertyTypData. Normally, when a property has "base = 16", it is printed as unsigned even if the gtype is signed. For some properties, we want to print the hexadecimal representation for positive values, and the base10 representation with minus sign for negative values. A typical use case is to encode the default value as "-1" and use positive values as a hexadecimal number. --- src/libnmc-setting/nm-meta-setting-desc.c | 17 +++++++++++++++-- src/libnmc-setting/nm-meta-setting-desc.h | 18 +++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/libnmc-setting/nm-meta-setting-desc.c b/src/libnmc-setting/nm-meta-setting-desc.c index b234504a07..2e95b0186f 100644 --- a/src/libnmc-setting/nm-meta-setting-desc.c +++ b/src/libnmc-setting/nm-meta-setting-desc.c @@ -1005,8 +1005,16 @@ _get_fcn_gobject_int(ARGS_GET_FCN) case 16: if (is_uint64) return_str = g_strdup_printf("0x%" G_GINT64_MODIFIER "x", v.u64); - else - return_str = g_strdup_printf("0x%" G_GINT64_MODIFIER "x", (guint64) v.i64); + else { + if (property_info->property_typ_data + && property_info->property_typ_data->subtype.gobject_int + .print_hex_negative_as_base10 + && v.i64 < 0) { + return_str = g_strdup_printf("%" G_GINT64_FORMAT, v.i64); + } else { + return_str = g_strdup_printf("0x%" G_GINT64_MODIFIER "x", (guint64) v.i64); + } + } break; default: return_str = NULL; @@ -1422,6 +1430,11 @@ _set_fcn_gobject_int(ARGS_SET_FCN) nm_meta_property_int_get_range(property_info, &min, &max); + /* See the comment on "print_hex_negative_as_base10" */ + nm_assert(!property_info->property_typ_data + || !property_info->property_typ_data->subtype.gobject_int.print_hex_negative_as_base10 + || (!is_uint64 && min.i64 > -10)); + if (is_uint64) v.u64 = _nm_utils_ascii_str_to_uint64(value, base, min.u64, max.u64, 0); else diff --git a/src/libnmc-setting/nm-meta-setting-desc.h b/src/libnmc-setting/nm-meta-setting-desc.h index 46b7f65002..0294b1f706 100644 --- a/src/libnmc-setting/nm-meta-setting-desc.h +++ b/src/libnmc-setting/nm-meta-setting-desc.h @@ -293,9 +293,21 @@ struct _NMMetaPropertyTypData { int value); } gobject_enum; struct { - NMMetaSignUnsignInt64 min; - NMMetaSignUnsignInt64 max; - guint base; + NMMetaSignUnsignInt64 min; + NMMetaSignUnsignInt64 max; + guint base; + + /* Normally, when a property has "base = 16", it is printed + * as unsigned even if the gtype is signed. For some properties, + * we want to print the hexadecimal representation for positive + * values, and the base10 representation with minus sign for negative + * values. A typical use case is to encode the default value as + * "-1" and use positive values as a hexadecimal number. To avoid + * ambiguity when setting the value via nmcli, the property minimum + * allowed value should not be <= -10. + */ + bool print_hex_negative_as_base10; + const NMMetaUtilsIntValueInfo *value_infos; } gobject_int; struct {