diff options
author | Thomas Haller <thaller@redhat.com> | 2018-06-08 16:26:54 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2018-06-13 15:29:41 +0200 |
commit | 8a2552c800987864a67bc0c14e14733ddecc96bf (patch) | |
tree | f1cb2e82a043cf508ec271168dacab755b8218a5 | |
parent | 3d91144d9fc3e89ad373b61715496693b75e9215 (diff) | |
download | NetworkManager-8a2552c800987864a67bc0c14e14733ddecc96bf.tar.gz |
cli: handle field selector prefix in nm_meta_selection_create_parse_list()
The same fields, can be printed with or without a particular prefix,
for example:
$ nmcli -f AP.SSID device show wlan0
vs.
$ nmcli -f SSID device wifi
So, there are places, where we artificially must prepend a prefix to the
field selector string.
Move the code to do that to nm_meta_selection_create_parse_list().
-rw-r--r-- | clients/cli/common.c | 22 | ||||
-rw-r--r-- | clients/cli/connections.c | 11 | ||||
-rw-r--r-- | clients/cli/devices.c | 26 | ||||
-rw-r--r-- | clients/cli/general.c | 3 | ||||
-rw-r--r-- | clients/cli/settings.c | 11 | ||||
-rw-r--r-- | clients/cli/utils.c | 19 | ||||
-rw-r--r-- | clients/cli/utils.h | 1 | ||||
-rw-r--r-- | clients/common/nm-meta-setting-access.c | 48 | ||||
-rw-r--r-- | clients/common/nm-meta-setting-access.h | 1 |
9 files changed, 77 insertions, 65 deletions
diff --git a/clients/cli/common.c b/clients/cli/common.c index ede447bad0..6101199966 100644 --- a/clients/cli/common.c +++ b/clients/cli/common.c @@ -342,17 +342,10 @@ print_ip_config (NMIPConfig *cfg, const char *one_field) { gs_free_error GError *error = NULL; - gs_free char *field_str = NULL; if (!cfg) return FALSE; - if (one_field) { - field_str = g_strdup_printf ("IP%c.%s", - nm_utils_addr_family_to_char (addr_family), - one_field); - } - if (!nmc_print (nmc_config, (gpointer[]) { cfg, NULL }, NULL, @@ -360,7 +353,9 @@ print_ip_config (NMIPConfig *cfg, addr_family == AF_INET ? NMC_META_GENERIC_GROUP ("IP4", metagen_ip4_config, N_("GROUP")) : NMC_META_GENERIC_GROUP ("IP6", metagen_ip6_config, N_("GROUP")), - field_str, + addr_family == AF_INET + ? "IP4" : "IP6", + one_field, &error)) { return FALSE; } @@ -374,17 +369,10 @@ print_dhcp_config (NMDhcpConfig *dhcp, const char *one_field) { gs_free_error GError *error = NULL; - gs_free char *field_str = NULL; if (!dhcp) return FALSE; - if (one_field) { - field_str = g_strdup_printf ("DHCP%c.%s", - nm_utils_addr_family_to_char (addr_family), - one_field); - } - if (!nmc_print (nmc_config, (gpointer[]) { dhcp, NULL }, NULL, @@ -392,7 +380,9 @@ print_dhcp_config (NMDhcpConfig *dhcp, addr_family == AF_INET ? NMC_META_GENERIC_GROUP ("DHCP4", metagen_dhcp_config, N_("GROUP")) : NMC_META_GENERIC_GROUP ("DHCP6", metagen_dhcp_config, N_("GROUP")), - field_str, + addr_family == AF_INET + ? "DHCP4" : "DHCP6", + one_field, &error)) { return FALSE; } diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 993732643c..294c936d54 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -1364,17 +1364,13 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc) was_output = FALSE; if (nmc_fields_con_active_details_groups[group_idx]->nested == metagen_con_active_general) { - gs_free char *f = NULL; - - if (group_fld) - f = g_strdup_printf ("GENERAL.%s", group_fld); - nmc_print (&nmc->nmc_config, (gpointer[]) { acon, NULL }, NULL, NULL, NMC_META_GENERIC_GROUP ("GENERAL", metagen_con_active_general, N_("GROUP")), - f, + "GENERAL", + group_fld, NULL); was_output = TRUE; continue; @@ -1423,6 +1419,7 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc) NULL, NULL, NMC_META_GENERIC_GROUP ("VPN", metagen_con_active_vpn, N_("GROUP")), + NULL, group_fld, NULL); was_output = TRUE; @@ -1955,6 +1952,7 @@ do_connections_show (NmCli *nmc, int argc, char **argv) * a profile has multiple active connections, it will be listed multiple times. * If that's not the case, we filter out these duplicate lines. */ selection = nm_meta_selection_create_parse_list ((const NMMetaAbstractInfo *const*) metagen_con_show, + NULL, fields_str, FALSE, NULL); @@ -1984,6 +1982,7 @@ do_connections_show (NmCli *nmc, int argc, char **argv) ? _("NetworkManager active profiles") : _("NetworkManager connection profiles"), (const NMMetaAbstractInfo *const*) metagen_con_show, + NULL, fields_str, &err)) goto finish; diff --git a/clients/cli/devices.c b/clients/cli/devices.c index 62cf2d0599..ff03419cf7 100644 --- a/clients/cli/devices.c +++ b/clients/cli/devices.c @@ -1474,28 +1474,26 @@ show_device_info (NMDevice *device, NmCli *nmc) state = nm_device_get_state (device); if (nmc_fields_dev_show_sections[section_idx]->nested == metagen_device_detail_general) { - gs_free char *f = section_fld ? g_strdup_printf ("GENERAL.%s", section_fld) : NULL; - nmc_print (&nmc->nmc_config, (gpointer[]) { device, NULL }, NULL, NULL, NMC_META_GENERIC_GROUP ("GENERAL", metagen_device_detail_general, N_("GROUP")), - f, + "GENERAL", + section_fld, NULL); was_output = TRUE; continue; } if (nmc_fields_dev_show_sections[section_idx]->nested == metagen_device_detail_capabilities) { - gs_free char *f = section_fld ? g_strdup_printf ("CAPABILITIES.%s", section_fld) : NULL; - nmc_print (&nmc->nmc_config, (gpointer[]) { device, NULL }, NULL, NULL, NMC_META_GENERIC_GROUP ("CAPABILITIES", metagen_device_detail_capabilities, N_("GROUP")), - f, + "CAPABILITIES", + section_fld, NULL); was_output = TRUE; continue; @@ -1503,14 +1501,13 @@ show_device_info (NMDevice *device, NmCli *nmc) if (nmc_fields_dev_show_sections[section_idx]->nested == metagen_device_detail_wifi_properties) { if (NM_IS_DEVICE_WIFI (device)) { - gs_free char *f = section_fld ? g_strdup_printf ("WIFI-PROPERTIES.%s", section_fld) : NULL; - nmc_print (&nmc->nmc_config, (gpointer[]) { device, NULL }, NULL, NULL, NMC_META_GENERIC_GROUP ("WIFI-PROPERTIES", metagen_device_detail_wifi_properties, N_("GROUP")), - f, + "WIFI-PROPERTIES", + section_fld, NULL); was_output = TRUE; } @@ -1561,14 +1558,13 @@ show_device_info (NMDevice *device, NmCli *nmc) if (nmc_fields_dev_show_sections[section_idx]->nested == metagen_device_detail_wired_properties) { if ((NM_IS_DEVICE_ETHERNET (device))) { - gs_free char *f = section_fld ? g_strdup_printf ("WIRED-PROPERTIES.%s", section_fld) : NULL; - nmc_print (&nmc->nmc_config, (gpointer[]) { device, NULL }, NULL, NULL, NMC_META_GENERIC_GROUP ("WIRED-PROPERTIES", metagen_device_detail_wired_properties, N_("GROUP")), - f, + "WIRED-PROPERTIES", + section_fld, NULL); was_output = TRUE; } @@ -1663,14 +1659,13 @@ show_device_info (NMDevice *device, NmCli *nmc) } if (nmc_fields_dev_show_sections[section_idx]->nested == metagen_device_detail_connections) { - gs_free char *f = section_fld ? g_strdup_printf ("CONNECTIONS.%s", section_fld) : NULL; - nmc_print (&nmc->nmc_config, (gpointer[]) { device, NULL }, NULL, NULL, NMC_META_GENERIC_GROUP ("CONNECTIONS", metagen_device_detail_connections, N_("GROUP")), - f, + "CONNECTIONS", + section_fld, NULL); was_output = TRUE; continue; @@ -1730,6 +1725,7 @@ do_devices_status (NmCli *nmc, int argc, char **argv) NULL, N_("Status of devices"), (const NMMetaAbstractInfo *const*) metagen_device_status, + NULL, fields_str, &error)) { g_string_printf (nmc->return_text, _("Error: 'device status': %s"), error->message); diff --git a/clients/cli/general.c b/clients/cli/general.c index cb87c11054..563525e8e6 100644 --- a/clients/cli/general.c +++ b/clients/cli/general.c @@ -511,6 +511,7 @@ show_nm_status (NmCli *nmc, const char *pretty_header_name, const char *print_fl NULL, pretty_header_name ?: N_("NetworkManager status"), (const NMMetaAbstractInfo *const*) metagen_general_status, + NULL, fields_str, &error)) { g_string_printf (nmc->return_text, _("Error: only these fields are allowed: %s"), fields_all); @@ -569,6 +570,7 @@ print_permissions (void *user_data) NULL, _("NetworkManager permissions"), (const NMMetaAbstractInfo *const*) metagen_general_permissions, + NULL, fields_str, &error)) { g_string_printf (nmc->return_text, _("Error: 'general permissions': %s"), error->message); @@ -662,6 +664,7 @@ show_general_logging (NmCli *nmc) NULL, _("NetworkManager logging"), (const NMMetaAbstractInfo *const*) metagen_general_logging, + NULL, fields_str, &error)) { g_string_printf (nmc->return_text, _("Error: 'general logging': %s"), error->message); diff --git a/clients/cli/settings.c b/clients/cli/settings.c index b39713f901..820c392509 100644 --- a/clients/cli/settings.c +++ b/clients/cli/settings.c @@ -796,7 +796,6 @@ setting_details (const NmcConfig *nmc_config, NMSetting *setting, const char *on { const NMMetaSettingInfoEditor *setting_info; gs_free_error GError *error = NULL; - gs_free char *fields_str = NULL; g_return_val_if_fail (NM_IS_SETTING (setting), FALSE); @@ -804,19 +803,13 @@ setting_details (const NmcConfig *nmc_config, NMSetting *setting, const char *on if (!setting_info) return FALSE; - if (one_prop) { - /* hack around setting-details being called for one setting. Must prefix the - * property name with the setting name. Later we should remove setting_details() - * and merge it into the caller. */ - fields_str = g_strdup_printf ("%s.%s", nm_setting_get_name (setting), one_prop); - } - if (!nmc_print (nmc_config, (gpointer[]) { setting, NULL }, NULL, NULL, (const NMMetaAbstractInfo *const[]) { (const NMMetaAbstractInfo *) setting_info, NULL }, - fields_str, + nm_setting_get_name (setting), + one_prop, &error)) return FALSE; diff --git a/clients/cli/utils.c b/clients/cli/utils.c index a838eac585..72c680d3a1 100644 --- a/clients/cli/utils.c +++ b/clients/cli/utils.c @@ -791,6 +791,7 @@ _output_selection_complete (GArray *cols) /** * _output_selection_parse: * @fields: a %NULL terminated array of meta-data fields + * @fields_prefix: * @fields_str: a comma separated selector for fields. Nested fields * can be specified using '.' notation. * @out_cols: (transfer full): the result, parsed as an GArray of PrintDataCol items. @@ -806,6 +807,7 @@ _output_selection_complete (GArray *cols) */ static gboolean _output_selection_parse (const NMMetaAbstractInfo *const*fields, + const char *fields_prefix, const char *fields_str, GArray **out_cols, GPtrArray **out_gfree_keeper, @@ -816,7 +818,11 @@ _output_selection_parse (const NMMetaAbstractInfo *const*fields, gs_unref_array GArray *cols = NULL; guint i; - selection = nm_meta_selection_create_parse_list (fields, fields_str, FALSE, error); + selection = nm_meta_selection_create_parse_list (fields, + fields_prefix, + fields_str, + FALSE, + error); if (!selection) return FALSE; @@ -883,7 +889,7 @@ parse_output_fields (const char *fields_str, g_return_val_if_fail (!error || !*error, NULL); g_return_val_if_fail (!out_group_fields || !*out_group_fields, NULL); - selection = nm_meta_selection_create_parse_list (fields_array, fields_str, TRUE, error); + selection = nm_meta_selection_create_parse_list (fields_array, NULL, fields_str, TRUE, error); if (!selection) return NULL; @@ -1468,6 +1474,7 @@ nmc_print (const NmcConfig *nmc_config, gpointer targets_data, const char *header_name_no_l10n, const NMMetaAbstractInfo *const*fields, + const char *fields_prefix, const char *fields_str, GError **error) { @@ -1476,8 +1483,11 @@ nmc_print (const NmcConfig *nmc_config, gs_unref_array GArray *header_row = NULL; gs_unref_array GArray *cells = NULL; - if (!_output_selection_parse (fields, fields_str, - &cols, &gfree_keeper, + if (!_output_selection_parse (fields, + fields_prefix, + fields_str, + &cols, + &gfree_keeper, error)) return FALSE; @@ -1893,4 +1903,3 @@ print_data (const NmcConfig *nmc_config, indent, field_values); } } - diff --git a/clients/cli/utils.h b/clients/cli/utils.h index 253ca79aa7..6222b69fb0 100644 --- a/clients/cli/utils.h +++ b/clients/cli/utils.h @@ -384,6 +384,7 @@ gboolean nmc_print (const NmcConfig *nmc_config, gpointer targets_data, const char *header_name_no_l10n, const NMMetaAbstractInfo *const*fields, + const char *fields_prefix, const char *fields_str, GError **error); diff --git a/clients/common/nm-meta-setting-access.c b/clients/common/nm-meta-setting-access.c index ded0945fc6..d7417ba6e0 100644 --- a/clients/common/nm-meta-setting-access.c +++ b/clients/common/nm-meta-setting-access.c @@ -762,6 +762,7 @@ nm_meta_selection_create_parse_one (const NMMetaAbstractInfo *const* fields_arra NMMetaSelectionResultList * nm_meta_selection_create_parse_list (const NMMetaAbstractInfo *const* fields_array, + const char *fields_prefix, const char *fields_str, /* a comma separated list of selectors */ gboolean validate_nested, GError **error) @@ -769,33 +770,52 @@ nm_meta_selection_create_parse_list (const NMMetaAbstractInfo *const* fields_arr gs_unref_array GArray *array = NULL; nm_auto_free_gstring GString *str = NULL; gs_free char *fields_str_clone = NULL; - char *fields_str_cur; - char *fields_str_next; + guint i, j; + gs_free const char **fields_words_split = NULL; + gs_strfreev char **fields_words_prefixed = NULL; + guint fields_len = 0; + const char **fields_words = NULL; g_return_val_if_fail (!error || !*error, NULL); - if (!fields_str || !g_ascii_strcasecmp (fields_str, "all")) + fields_words_split = nm_utils_strsplit_set (fields_str ?: "all", ","); + if (fields_words_split) { + for (i = 0, j = 0; fields_words_split[i]; i++) { + /* it's safe to stip the words in-place, because of how + * nm_utils_strsplit_set() works. */ + g_strstrip ((char *) fields_words_split[i]); + if (fields_words_split[i][0]) + fields_words_split[j++] = fields_words_split[i]; + } + fields_words_split[j] = NULL; + fields_words = fields_words_split; + fields_len = j; + } + + if (fields_len == 0 + || ( fields_len == 1 + && !g_ascii_strcasecmp (fields_words[0], "all"))) return nm_meta_selection_create_all (fields_array); - else if (!g_ascii_strcasecmp (fields_str, "common")) { + else if ( fields_len == 1 + && !g_ascii_strcasecmp (fields_words[0], "common")) { gs_free gpointer f = NULL; fields_array = nm_meta_abstract_infos_select_included_in_common (fields_array, -1, NULL, &f); return nm_meta_selection_create_all (fields_array); } - fields_str_clone = g_strdup (fields_str); - for (fields_str_cur = fields_str_clone; fields_str_cur; fields_str_cur = fields_str_next) { - fields_str_cur = nm_str_skip_leading_spaces (fields_str_cur); - fields_str_next = strchr (fields_str_cur, ','); - if (fields_str_next) - *fields_str_next++ = '\0'; + if (fields_prefix) { + fields_words_prefixed = g_new (char *, i + 1); + for (i = 0; fields_words_split[i]; i++) + fields_words_prefixed[i] = g_strconcat (fields_prefix, ".", fields_words_split[i], NULL); + fields_words_prefixed[i] = NULL; + fields_words = (const char **) fields_words_prefixed; + } - g_strchomp (fields_str_cur); - if (!fields_str_cur[0]) - continue; + for (i = 0; i < fields_len; i++) { if (!_output_selection_select_one (fields_array, NULL, - fields_str_cur, + fields_words[i], validate_nested, &array, &str, diff --git a/clients/common/nm-meta-setting-access.h b/clients/common/nm-meta-setting-access.h index 368e38d159..5981094966 100644 --- a/clients/common/nm-meta-setting-access.h +++ b/clients/common/nm-meta-setting-access.h @@ -101,6 +101,7 @@ NMMetaSelectionResultList *nm_meta_selection_create_parse_one (const NMMetaAbstr gboolean validate_nested, GError **error); NMMetaSelectionResultList *nm_meta_selection_create_parse_list (const NMMetaAbstractInfo *const* fields_array, + const char *fields_prefix, const char *fields_str, gboolean validate_nested, GError **error); |