diff options
author | Thomas Haller <thaller@redhat.com> | 2018-04-26 17:43:13 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2018-06-13 15:29:41 +0200 |
commit | 3440ce41adb535ec1a687b182d14f30ae5871e03 (patch) | |
tree | 1f6112412083ba1837a1882561e3cbc8eaf1bac2 | |
parent | 95df8992b867877dd74d5afcc1d91f082d4304ba (diff) | |
download | NetworkManager-3440ce41adb535ec1a687b182d14f30ae5871e03.tar.gz |
cli: rework printing of dhcp options
-rw-r--r-- | clients/cli/common.c | 110 | ||||
-rw-r--r-- | clients/cli/common.h | 2 | ||||
-rw-r--r-- | clients/cli/connections.c | 4 | ||||
-rw-r--r-- | clients/cli/devices.c | 4 | ||||
-rw-r--r-- | clients/cli/nmcli.c | 2 | ||||
-rw-r--r-- | clients/cli/utils.h | 3 |
6 files changed, 81 insertions, 44 deletions
diff --git a/clients/cli/common.c b/clients/cli/common.c index 09c86334ab..39ee2c0b52 100644 --- a/clients/cli/common.c +++ b/clients/cli/common.c @@ -270,12 +270,67 @@ const NmcMetaGenericInfo *const metagen_ip6_config[_NMC_GENERIC_INFO_TYPE_IP6_CO /*****************************************************************************/ -const NmcMetaGenericInfo *const nmc_fields_dhcp_config[] = { - NMC_META_GENERIC ("GROUP"), /* 0 */ - NMC_META_GENERIC ("OPTION"), /* 1 */ - NULL, +static gconstpointer +_metagen_dhcp_config_get_fcn (NMC_META_GENERIC_INFO_GET_FCN_ARGS) +{ + NMDhcpConfig *dhcp = target; + guint i; + char **arr = NULL; + + NMC_HANDLE_COLOR (NM_META_COLOR_NONE); + + switch (info->info_type) { + case NMC_GENERIC_INFO_TYPE_DHCP_CONFIG_OPTION: + { + GHashTable *table; + gs_free char **arr2 = NULL; + guint n; + + if (!NM_FLAGS_HAS (get_flags, NM_META_ACCESSOR_GET_FLAGS_ACCEPT_STRV)) + return NULL; + + table = nm_dhcp_config_get_options (dhcp); + if (!table) + goto arr_out; + + arr2 = (char **) nm_utils_strdict_get_keys (table, TRUE, &n); + if (!n) + goto arr_out; + + nm_assert (arr2 && !arr2[n] && n == NM_PTRARRAY_LEN (arr2)); + for (i = 0; i < n; i++) { + const char *k = arr2[i]; + const char *v; + + nm_assert (k); + v = g_hash_table_lookup (table, k); + arr2[i] = g_strdup_printf ("%s = %s", k, v); + } + + arr = g_steal_pointer (&arr2); + goto arr_out; + } + default: + break; + } + + g_return_val_if_reached (NULL); + +arr_out: + NM_SET_OUT (out_is_default, !arr || !arr[0]); + *out_flags |= NM_META_ACCESSOR_GET_OUT_FLAGS_STRV; + *out_to_free = arr; + return arr; +} + +const NmcMetaGenericInfo *const metagen_dhcp_config[_NMC_GENERIC_INFO_TYPE_DHCP_CONFIG_NUM + 1] = { +#define _METAGEN_DHCP_CONFIG(type, name) \ + [type] = NMC_META_GENERIC(name, .info_type = type, .get_fcn = _metagen_dhcp_config_get_fcn) + _METAGEN_DHCP_CONFIG (NMC_GENERIC_INFO_TYPE_DHCP_CONFIG_OPTION, "OPTION"), }; +/*****************************************************************************/ + const NmcMetaGenericInfo *const nmc_fields_ip6_config[] = { NMC_META_GENERIC ("GROUP"), /* 0 */ NMC_META_GENERIC ("ADDRESS"), /* 1 */ @@ -343,45 +398,24 @@ print_dhcp_config (NMDhcpConfig *dhcp, const char *group_prefix, const char *one_field) { - GHashTable *table; - const NMMetaAbstractInfo *const*tmpl; - NmcOutputField *arr; + gs_free_error GError *error = NULL; + gs_free char *field_str = NULL; - if (dhcp == NULL) + if (!dhcp) return FALSE; - table = nm_dhcp_config_get_options (dhcp); - if (table) { - char **options_arr = NULL; - NMC_OUTPUT_DATA_DEFINE_SCOPED (out); - gs_free const char **keys = NULL; - guint i, nkeys; - - tmpl = (const NMMetaAbstractInfo *const*) nmc_fields_dhcp_config; - out_indices = parse_output_fields (one_field, - tmpl, FALSE, NULL, NULL); - arr = nmc_dup_fields_array (tmpl, NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (out.output_data, arr); - - keys = (const char **) g_hash_table_get_keys_as_array (table, &nkeys); - nm_utils_strv_sort (keys, nkeys); - - options_arr = g_new (char *, nkeys + 1); - for (i = 0; i < nkeys; i++) - options_arr[i] = g_strdup_printf ("%s = %s", keys[i], (const char *) g_hash_table_lookup (table, keys[i])); - options_arr[i] = NULL; - - arr = nmc_dup_fields_array (tmpl, NMC_OF_FLAG_SECTION_PREFIX); - set_val_strc (arr, 0, group_prefix); - set_val_arr (arr, 1, options_arr); - g_ptr_array_add (out.output_data, arr); - - print_data_prepare_width (out.output_data); - print_data (nmc_config, out_indices, NULL, 0, &out); + if (one_field) + field_str = g_strdup_printf ("%s.%s", group_prefix, one_field); - return TRUE; + if (!nmc_print (nmc_config, + (gpointer[]) { dhcp, NULL }, + NULL, + NMC_META_GENERIC_GROUP (group_prefix, metagen_dhcp_config, N_("GROUP")), + field_str, + &error)) { + return FALSE; } - return FALSE; + return TRUE; } /* diff --git a/clients/cli/common.h b/clients/cli/common.h index 8f4e47c28b..2a5e3ffbed 100644 --- a/clients/cli/common.h +++ b/clients/cli/common.h @@ -84,6 +84,6 @@ const char *nmc_error_get_simple_message (GError *error); extern const NmcMetaGenericInfo *const metagen_ip4_config[]; extern const NmcMetaGenericInfo *const nmc_fields_ip6_config[]; -extern const NmcMetaGenericInfo *const nmc_fields_dhcp_config[]; +extern const NmcMetaGenericInfo *const metagen_dhcp_config[]; #endif /* NMC_COMMON_H */ diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 08a3b9a1c5..d7f498ebc7 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -770,9 +770,9 @@ const NmcMetaGenericInfo *const metagen_con_active_vpn[_NMC_GENERIC_INFO_TYPE_CO const NmcMetaGenericInfo *const nmc_fields_con_active_details_groups[] = { NMC_META_GENERIC_WITH_NESTED ("GENERAL", metagen_con_active_general), /* 0 */ NMC_META_GENERIC_WITH_NESTED ("IP4", metagen_ip4_config), /* 1 */ - NMC_META_GENERIC_WITH_NESTED ("DHCP4", nmc_fields_dhcp_config + 1), /* 2 */ + NMC_META_GENERIC_WITH_NESTED ("DHCP4", metagen_dhcp_config), /* 2 */ NMC_META_GENERIC_WITH_NESTED ("IP6", nmc_fields_ip6_config + 1), /* 3 */ - NMC_META_GENERIC_WITH_NESTED ("DHCP6", nmc_fields_dhcp_config + 1), /* 4 */ + NMC_META_GENERIC_WITH_NESTED ("DHCP6", metagen_dhcp_config), /* 4 */ NMC_META_GENERIC_WITH_NESTED ("VPN", metagen_con_active_vpn), /* 5 */ NULL, }; diff --git a/clients/cli/devices.c b/clients/cli/devices.c index 81f11415d4..88b8f3230f 100644 --- a/clients/cli/devices.c +++ b/clients/cli/devices.c @@ -209,9 +209,9 @@ const NmcMetaGenericInfo *const nmc_fields_dev_show_sections[] = { NMC_META_GENERIC_WITH_NESTED ("WIMAX-PROPERTIES", nmc_fields_dev_show_wimax_prop + 1), /* 5 */ NMC_META_GENERIC_WITH_NESTED ("NSP", nmc_fields_dev_wimax_list + 1), /* 6 */ NMC_META_GENERIC_WITH_NESTED ("IP4", metagen_ip4_config), /* 7 */ - NMC_META_GENERIC_WITH_NESTED ("DHCP4", nmc_fields_dhcp_config + 1), /* 8 */ + NMC_META_GENERIC_WITH_NESTED ("DHCP4", metagen_dhcp_config), /* 8 */ NMC_META_GENERIC_WITH_NESTED ("IP6", nmc_fields_ip6_config + 1), /* 9 */ - NMC_META_GENERIC_WITH_NESTED ("DHCP6", nmc_fields_dhcp_config + 1), /* 10 */ + NMC_META_GENERIC_WITH_NESTED ("DHCP6", metagen_dhcp_config), /* 10 */ NMC_META_GENERIC_WITH_NESTED ("BOND", nmc_fields_dev_show_master_prop + 1), /* 11 */ NMC_META_GENERIC_WITH_NESTED ("TEAM", nmc_fields_dev_show_team_prop + 1), /* 12 */ NMC_META_GENERIC_WITH_NESTED ("BRIDGE", nmc_fields_dev_show_master_prop + 1), /* 13 */ diff --git a/clients/cli/nmcli.c b/clients/cli/nmcli.c index 81bd7256d6..2cecfeb189 100644 --- a/clients/cli/nmcli.c +++ b/clients/cli/nmcli.c @@ -186,7 +186,7 @@ complete_fields (const char *option, const char *prefix) h = g_hash_table_new_full (nm_str_hash, g_str_equal, g_free, NULL); complete_field (h, metagen_ip4_config); - complete_field (h, nmc_fields_dhcp_config); + complete_field (h, metagen_dhcp_config); complete_field (h, nmc_fields_ip6_config); complete_field (h, metagen_con_show); complete_field (h, metagen_con_active_general); diff --git a/clients/cli/utils.h b/clients/cli/utils.h index a80b88979b..acb5d63775 100644 --- a/clients/cli/utils.h +++ b/clients/cli/utils.h @@ -121,6 +121,9 @@ typedef enum { NMC_GENERIC_INFO_TYPE_IP6_CONFIG_DOMAIN, _NMC_GENERIC_INFO_TYPE_IP6_CONFIG_NUM, + NMC_GENERIC_INFO_TYPE_DHCP_CONFIG_OPTION = 0, + _NMC_GENERIC_INFO_TYPE_DHCP_CONFIG_NUM, + NMC_GENERIC_INFO_TYPE_CON_SHOW_NAME = 0, NMC_GENERIC_INFO_TYPE_CON_SHOW_UUID, NMC_GENERIC_INFO_TYPE_CON_SHOW_TYPE, |